From 13a5badff5876b8d77c68db4937a4ce2190522c0 Mon Sep 17 00:00:00 2001 From: Robbie Averill Date: Thu, 15 Dec 2016 16:41:49 +1300 Subject: [PATCH] Move "code" to PSR-4 friendly "src" folders. Add namespacing. --- _config.php | 16 +- _config/comments.yml | 6 +- _config/config.yml | 4 +- code/compat/pages/BlogEntry.php | 112 ---- code/compat/pages/BlogHolder.php | 77 --- code/compat/pages/BlogTree.php | 56 -- code/compat/tasks/BlogMigrationTask.php | 92 --- code/compat/tasks/MigratableObject.php | 9 - code/compat/widgets/ArchiveWidget.php | 54 -- code/compat/widgets/TagCloudWidget.php | 47 -- composer.json | 79 +-- src/.upgrade.yml | 28 + .../Admin}/GridFieldCategorisationConfig.php | 21 +- .../Admin}/GridFieldFormAction.php | 4 + .../Admin}/GridFieldMergeAction.php | 11 + src/Controllers/BlogController.php | 517 ++++++++++++++++ src/Controllers/BlogPostController.php | 14 + .../forms => src/Forms}/BlogAdminSidebar.php | 5 + .../GridField}/GridFieldAddByDBField.php | 17 +- .../GridField}/GridFieldBlogPostState.php | 7 + .../GridField}/GridFieldConfig_BlogPost.php | 4 + {code/model => src/Model}/Blog.php | 561 ++---------------- {code/model => src/Model}/BlogCategory.php | 20 +- .../Model}/BlogCommentExtension.php | 5 + {code/extensions => src/Model}/BlogFilter.php | 25 +- .../Model}/BlogMemberExtension.php | 20 +- {code/traits => src/Model}/BlogObject.php | 43 +- {code/model => src/Model}/BlogPost.php | 70 ++- .../Model}/BlogPostFilter.php | 29 +- .../Model}/BlogPostNotifications.php | 4 + {code/model => src/Model}/BlogTag.php | 22 +- .../Model}/CategorisationObject.php | 3 + .../Widgets}/BlogArchiveWidget.php | 8 +- .../Widgets}/BlogCategoriesWidget.php | 10 +- .../Widgets}/BlogRecentPostsWidget.php | 10 +- .../Widgets}/BlogTagsCloudWidget.php | 8 +- .../Widgets}/BlogTagsWidget.php | 10 +- tests/BlogCategoryTest.php | 57 +- tests/BlogPostFilterTest.php | 12 +- tests/BlogPostNotificationsTest.php | 13 +- tests/BlogPostTest.php | 26 +- tests/BlogTagTest.php | 64 +- tests/BlogTagsCloudWidgetTest.php | 15 +- tests/BlogTest.php | 70 ++- tests/blog.yml | 130 ++-- 45 files changed, 1143 insertions(+), 1272 deletions(-) delete mode 100644 code/compat/pages/BlogEntry.php delete mode 100644 code/compat/pages/BlogHolder.php delete mode 100644 code/compat/pages/BlogTree.php delete mode 100644 code/compat/tasks/BlogMigrationTask.php delete mode 100644 code/compat/tasks/MigratableObject.php delete mode 100644 code/compat/widgets/ArchiveWidget.php delete mode 100644 code/compat/widgets/TagCloudWidget.php create mode 100644 src/.upgrade.yml rename {code/admin => src/Admin}/GridFieldCategorisationConfig.php (72%) rename {code/admin => src/Admin}/GridFieldFormAction.php (89%) rename {code/admin => src/Admin}/GridFieldMergeAction.php (95%) create mode 100644 src/Controllers/BlogController.php create mode 100644 src/Controllers/BlogPostController.php rename {code/forms => src/Forms}/BlogAdminSidebar.php (73%) rename {code/forms/gridfield => src/Forms/GridField}/GridFieldAddByDBField.php (89%) rename {code/forms/gridfield => src/Forms/GridField}/GridFieldBlogPostState.php (94%) rename {code/forms/gridfield => src/Forms/GridField}/GridFieldConfig_BlogPost.php (81%) rename {code/model => src/Model}/Blog.php (56%) rename {code/model => src/Model}/BlogCategory.php (71%) rename {code/extensions => src/Model}/BlogCommentExtension.php (82%) rename {code/extensions => src/Model}/BlogFilter.php (78%) rename {code/extensions => src/Model}/BlogMemberExtension.php (81%) rename {code/traits => src/Model}/BlogObject.php (82%) rename {code/model => src/Model}/BlogPost.php (91%) rename {code/extensions => src/Model}/BlogPostFilter.php (64%) rename {code/extensions => src/Model}/BlogPostNotifications.php (91%) rename {code/model => src/Model}/BlogTag.php (68%) rename {code/model => src/Model}/CategorisationObject.php (69%) rename {code/widgets => src/Widgets}/BlogArchiveWidget.php (94%) rename {code/widgets => src/Widgets}/BlogCategoriesWidget.php (90%) rename {code/widgets => src/Widgets}/BlogRecentPostsWidget.php (86%) rename {code/widgets => src/Widgets}/BlogTagsCloudWidget.php (92%) rename {code/widgets => src/Widgets}/BlogTagsWidget.php (90%) diff --git a/_config.php b/_config.php index d8ae592..f45d2d2 100755 --- a/_config.php +++ b/_config.php @@ -1,8 +1,8 @@ - 'SS_Datetime', - 'Author' => 'Text', - 'Tags' => 'Text', - ); - - /** - * {@inheritdoc} - */ - public function canCreate($member = null, $context = array()) - { - return false; - } - - /** - * {@inheritdoc} - */ - public function up() - { - - //Migrate comma separated tags into BlogTag objects. - foreach ($this->TagNames() as $tag) { - $existingTag = BlogTag::get()->filter(array('Title' => $tag, 'BlogID' => $this->ParentID)); - if ($existingTag->count()) { - //if tag already exists we will simply add it to this post. - $tagObject = $existingTag->First(); - } else { - //if the tag is now we create it and add it to this post. - $tagObject = new BlogTag(); - $tagObject->Title = $tag; - $tagObject->BlogID = $this->ParentID; - $tagObject->write(); - } - - if ($tagObject) { - $this->Tags()->add($tagObject); - } - } - - //Store if the original entity was published or not (draft) - $published = $this->IsPublished(); - // If a user has subclassed BlogEntry, it should not be turned into a BlogPost. - if ($this->ClassName === 'BlogEntry') { - $this->ClassName = 'BlogPost'; - $this->RecordClassName = 'BlogPost'; - } - //Migrate these key data attributes - $this->PublishDate = $this->Date; - $this->AuthorNames = $this->Author; - $this->InheritSideBar = true; - - //Write and additionally publish the item if it was published before. - $this->write(); - if ($published) { - $this->publish('Stage', 'Live'); - $message = "PUBLISHED: "; - } else { - $message = "DRAFT: "; - } - - return $message . $this->Title; - } - - /** - * Safely split and parse all distinct tags assigned to this BlogEntry. - * - * @deprecated since version 2.0 - * - * @return array - */ - public function TagNames() - { - $tags = preg_split('/\s*,\s*/', trim($this->Tags)); - - $results = array(); - - foreach ($tags as $tag) { - if ($tag) { - $results[mb_strtolower($tag)] = $tag; - } - } - - return $results; - } -} - -/** - * @deprecated since version 2.0 - */ -class BlogEntry_Controller extends BlogPost_Controller -{ -} diff --git a/code/compat/pages/BlogHolder.php b/code/compat/pages/BlogHolder.php deleted file mode 100644 index 653a856..0000000 --- a/code/compat/pages/BlogHolder.php +++ /dev/null @@ -1,77 +0,0 @@ - 'Boolean', - 'ShowFullEntry' => 'Boolean', - ); - - /** - * @var array - */ - private static $has_one = array( - 'Owner' => 'Member', - ); - - /** - * {@inheritdoc} - */ - public function canCreate($member = null, $context = array()) - { - return false; - } - - - //Overload these to stop the Uncaught Exception: Object->__call(): the method 'parent' does not exist on 'BlogHolder' error. - public function validURLSegment() - { - return true; - } - public function syncLinkTracking() - { - return null; - } - - /** - * {@inheritdoc} - */ - public function up() - { - $published = $this->IsPublished(); - - if ($this->ClassName === 'BlogHolder') { - $this->ClassName = 'Blog'; - $this->RecordClassName = 'Blog'; - $this->PostsPerPage = 10; - $this->write(); - } - - if ($published) { - $this->publish('Stage', 'Live'); - $message = "PUBLISHED: "; - } else { - $message = "DRAFT: "; - } - - return $message . $this->Title; - } -} - -/** - * @deprecated since version 2.0 - */ -class BlogHolder_Controller extends BlogTree_Controller -{ -} diff --git a/code/compat/pages/BlogTree.php b/code/compat/pages/BlogTree.php deleted file mode 100644 index 9fa2617..0000000 --- a/code/compat/pages/BlogTree.php +++ /dev/null @@ -1,56 +0,0 @@ - 'Varchar(255)', - 'LandingPageFreshness' => 'Varchar', - ); - - /** - * {@inheritdoc} - */ - public function canCreate($member = null, $context = array()) - { - return false; - } - - /** - * {@inheritdoc} - */ - public function up() - { - $published = $this->IsPublished(); - if ($this->ClassName === 'BlogTree') { - $this->ClassName = 'Page'; - $this->RecordClassName = 'Page'; - $this->write(); - } - if ($published) { - $this->publish('Stage', 'Live'); - $message = "PUBLISHED: "; - } else { - $message = "DRAFT: "; - } - - return $message . $this->Title; - } -} - -/** - * @deprecated since version 2.0 - */ -class BlogTree_Controller extends Page_Controller -{ -} diff --git a/code/compat/tasks/BlogMigrationTask.php b/code/compat/tasks/BlogMigrationTask.php deleted file mode 100644 index 143fb7c..0000000 --- a/code/compat/tasks/BlogMigrationTask.php +++ /dev/null @@ -1,92 +0,0 @@ -message('Migrating legacy blog records'); - - foreach ($classes as $class) { - $this->upClass($class); - } - } - - /** - * @param string $text - */ - protected function message($text) - { - if (Controller::curr() instanceof DatabaseAdmin) { - DB::alteration_message($text, 'obsolete'); - } else { - echo $text . "
"; - } - } - - /** - * Migrate records of a single class - * - * @param string $class - * @param null|string $stage - */ - protected function upClass($class) - { - if (!class_exists($class)) { - return; - } - - if (is_subclass_of($class, 'SiteTree')) { - $items = SiteTree::get()->filter('ClassName', $class); - } else { - $items = $class::get(); - } - - if ($count = $items->count()) { - $this->message( - sprintf( - 'Migrating %s legacy %s records.', - $count, - $class - ) - ); - - foreach ($items as $item) { - $cancel = $item->extend('onBeforeUp'); - - if ($cancel && min($cancel) === false) { - continue; - } - - /** - * @var MigratableObject $item - */ - $result = $item->up(); - $this->message($result); - - $item->extend('onAfterUp'); - } - } - } - - /** - * {@inheritdoc} - */ - public function down() - { - $this->message('BlogMigrationTask::down() not implemented'); - } -} diff --git a/code/compat/tasks/MigratableObject.php b/code/compat/tasks/MigratableObject.php deleted file mode 100644 index 2c747a8..0000000 --- a/code/compat/tasks/MigratableObject.php +++ /dev/null @@ -1,9 +0,0 @@ - 'Varchar', - ); - - /** - * @var array - */ - private static $only_available_in = array( - 'none', - ); - - /** - * {@inheritdoc} - */ - public function canCreate($member = null) - { - return false; - } - - /** - * {@inheritdoc} - */ - public function up() - { - if ($this->DisplayMode) { - $this->ArchiveType = 'Monthly'; - - if ($this->DisplayMode === 'year') { - $this->ArchiveType = 'Yearly'; - } - } - - $this->ClassName = 'BlogArchiveWidget'; - $this->write(); - return "Migrated " . $this->ArchiveType . " archive widget"; - } -} diff --git a/code/compat/widgets/TagCloudWidget.php b/code/compat/widgets/TagCloudWidget.php deleted file mode 100644 index a527ddd..0000000 --- a/code/compat/widgets/TagCloudWidget.php +++ /dev/null @@ -1,47 +0,0 @@ - 'Varchar', - 'Limit' => 'Int', - 'Sortby' => 'Varchar', - ); - - /** - * @var array - */ - private static $only_available_in = array( - 'none', - ); - - /** - * {@inheritdoc} - */ - public function canCreate($member = null) - { - return false; - } - - /** - * {@inheritdoc} - */ - public function up() - { - $this->ClassName = 'BlogTagsWidget'; - $this->write(); - return "Migrated " . $this->Title . " widget"; - } -} diff --git a/composer.json b/composer.json index 76fae8b..ae08702 100755 --- a/composer.json +++ b/composer.json @@ -1,39 +1,44 @@ { - "name": "silverstripe/blog", - "description": "A fresh take on blogging in Silverstripe set out to tackle the issue of a cluttered Site Tree.", - "keywords": [ - "silverstripe", - "blog", - "news" - ], - "type": "silverstripe-module", - "require": { - "silverstripe/cms": "^4.0", - "silverstripe/lumberjack": "^2.0", - "silverstripe/tagfield": "^2.0" - }, - "require-dev": { - "phpunit/PHPUnit": "~4.8" - }, - "extra": { - "branch-alias": { - "dev-master": "3.x-dev" - } - }, - "license": "BSD-2-Clause", - "authors": [ - { - "name": "Michael Strong", - "email": "github@michaelstrong.co.uk" - } - ], - "suggest": { - "silverstripe/widgets": "Some widgets come with the blog which are compatible with the widgets module.", - "silverstripe/comments": "This module adds comments to your blog." - }, - "replace": { - "micmania1/silverstripe-blog": "*" - }, - "minimum-stability": "dev", - "prefer-stable": true + "name": "silverstripe/blog", + "description": "A fresh take on blogging in Silverstripe set out to tackle the issue of a cluttered Site Tree.", + "keywords": [ + "silverstripe", + "blog", + "news" + ], + "type": "silverstripe-module", + "require": { + "silverstripe/cms": "^4.0", + "silverstripe/lumberjack": "^2.0", + "silverstripe/tagfield": "^2.0" + }, + "require-dev": { + "phpunit/PHPUnit": "~4.8" + }, + "extra": { + "branch-alias": { + "dev-master": "3.0.x-dev" + } + }, + "autoload": { + "psr-4": { + "SilverStripe\\Blog\\": "src/" + } + }, + "license": "BSD-2-Clause", + "authors": [ + { + "name": "Michael Strong", + "email": "github@michaelstrong.co.uk" + } + ], + "suggest": { + "silverstripe/widgets": "Some widgets come with the blog which are compatible with the widgets module.", + "silverstripe/comments": "This module adds comments to your blog." + }, + "replace": { + "micmania1/silverstripe-blog": "*" + }, + "minimum-stability": "dev", + "prefer-stable": true } diff --git a/src/.upgrade.yml b/src/.upgrade.yml new file mode 100644 index 0000000..cefd607 --- /dev/null +++ b/src/.upgrade.yml @@ -0,0 +1,28 @@ +mappings: + GridFieldCategorisationConfig: SilverStripe\Blog\Admin\GridFieldCategorisationConfig + GridFieldFormAction: SilverStripe\Blog\Admin\GridFieldFormAction + GridFieldMergeAction: SilverStripe\Blog\Admin\GridFieldMergeAction + BlogCommentExtension: SilverStripe\Blog\Model\BlogCommentExtension + BlogFilter: SilverStripe\Blog\Model\BlogFilter + BlogFilter_GridField: SilverStripe\Blog\Model\BlogFilter_GridField + BlogMemberExtension: SilverStripe\Blog\Model\BlogMemberExtension + BlogPostFilter: SilverStripe\Blog\Model\BlogPostFilter + BlogPostNotifications: SilverStripe\Blog\Model\BlogPostNotifications + Blog: SilverStripe\Blog\Model\Blog + Blog_Controller: SilverStripe\Blog\Controllers\BlogController + BlogController: SilverStripe\Blog\Controllers\BlogController + BlogCategory: SilverStripe\Blog\Model\BlogCategory + BlogPost: SilverStripe\Blog\Model\BlogPost + BlogPost_Controller: SilverStripe\Blog\Controllers\BlogPostController + BlogPostController: SilverStripe\Blog\Controllers\BlogPostController + BlogTag: SilverStripe\Blog\Model\BlogTag + CategorisationObject: SilverStripe\Blog\Model\CategorisationObject + BlogAdminSidebar: SilverStripe\Blog\Forms\BlogAdminSidebar + GridFieldAddByDBField: SilverStripe\Blog\Forms\GridField\GridFieldAddByDBField + GridFieldBlogPostState: SilverStripe\Blog\Forms\GridField\GridFieldBlogPostState + GridFieldConfig_BlogPost: SilverStripe\Blog\Forms\GridField\GridFieldConfig_BlogPost + BlogArchiveWidget: SilverStripe\Blog\Widgets\BlogArchiveWidget + BlogCategoriesWidget: SilverStripe\Blog\Widgets\BlogCategoriesWidget + BlogRecentPostsWidget: SilverStripe\Blog\Widgets\BlogRecentPostsWidget + BlogTagsCloudWidget: SilverStripe\Blog\Widgets\BlogTagsCloudWidget + BlogTagsWidget: SilverStripe\Blog\Widgets\BlogTagsWidget diff --git a/code/admin/GridFieldCategorisationConfig.php b/src/Admin/GridFieldCategorisationConfig.php similarity index 72% rename from code/admin/GridFieldCategorisationConfig.php rename to src/Admin/GridFieldCategorisationConfig.php index e390bff..0b0f88f 100644 --- a/code/admin/GridFieldCategorisationConfig.php +++ b/src/Admin/GridFieldCategorisationConfig.php @@ -1,5 +1,12 @@ getComponentByType('GridFieldDataColumns'); - $columns->setDisplayFields(array( - 'Title' => 'Title', - 'BlogPostsCount' => 'Posts', - 'MergeAction' => 'MergeAction', - 'Actions' => 'Actions', - )); + $columns->setDisplayFields( + array( + 'Title' => 'Title', + 'BlogPostsCount' => 'Posts', + 'MergeAction' => 'MergeAction', + 'Actions' => 'Actions' + ) + ); } } diff --git a/code/admin/GridFieldFormAction.php b/src/Admin/GridFieldFormAction.php similarity index 89% rename from code/admin/GridFieldFormAction.php rename to src/Admin/GridFieldFormAction.php index 47d448f..e48ba80 100644 --- a/code/admin/GridFieldFormAction.php +++ b/src/Admin/GridFieldFormAction.php @@ -1,5 +1,9 @@ 'tag', + 'category/$Category!/$Rss' => 'category', + 'archive/$Year!/$Month/$Day' => 'archive', + 'profile/$URLSegment!' => 'profile' + ); + + /** + * @var array + */ + private static $casting = array( + 'MetaTitle' => 'Text', + 'FilterDescription' => 'Text' + ); + + /** + * The current Blog Post DataList query. + * + * @var DataList + */ + protected $blogPosts; + + /** + * @return string + */ + public function index() + { + /** + * @var Blog $dataRecord + */ + $dataRecord = $this->dataRecord; + + $this->blogPosts = $dataRecord->getBlogPosts(); + + return $this->render(); + } + + /** + * Renders a Blog Member's profile. + * + * @return SS_HTTPResponse + */ + public function profile() + { + $profile = $this->getCurrentProfile(); + + if (!$profile) { + return $this->httpError(404, 'Not Found'); + } + + $this->blogPosts = $this->getCurrentProfilePosts(); + + return $this->render(); + } + + /** + * Get the Member associated with the current URL segment. + * + * @return null|Member + */ + public function getCurrentProfile() + { + $urlSegment = $this->request->param('URLSegment'); + + if ($urlSegment) { + return Member::get() + ->filter('URLSegment', $urlSegment) + ->first(); + } + + return null; + } + + /** + * Get posts related to the current Member profile. + * + * @return null|DataList + */ + public function getCurrentProfilePosts() + { + $profile = $this->getCurrentProfile(); + + if ($profile) { + return $profile->BlogPosts()->filter('ParentID', $this->ID); + } + + return null; + } + + /** + * Renders an archive for a specified date. This can be by year or year/month. + * + * @return null|SS_HTTPResponse + */ + public function archive() + { + /** + * @var Blog $dataRecord + */ + $dataRecord = $this->dataRecord; + + $year = $this->getArchiveYear(); + $month = $this->getArchiveMonth(); + $day = $this->getArchiveDay(); + + if ($this->request->param('Month') && !$month) { + $this->httpError(404, 'Not Found'); + } + + if ($month && $this->request->param('Day') && !$day) { + $this->httpError(404, 'Not Found'); + } + + if ($year) { + $this->blogPosts = $dataRecord->getArchivedBlogPosts($year, $month, $day); + + return $this->render(); + } + + $this->httpError(404, 'Not Found'); + + return null; + } + + /** + * Fetches the archive year from the url. + * + * @return int + */ + public function getArchiveYear() + { + if ($this->request->param('Year')) { + if (preg_match('/^[0-9]{4}$/', $year = $this->request->param('Year'))) { + return (int) $year; + } + } elseif ($this->request->param('Action') == 'archive') { + return DBDatetime::now()->Year(); + } + + return null; + } + + /** + * Fetches the archive money from the url. + * + * @return null|int + */ + public function getArchiveMonth() + { + $month = $this->request->param('Month'); + + if (preg_match('/^[0-9]{1,2}$/', $month)) { + if ($month > 0 && $month < 13) { + if (checkdate($month, 01, $this->getArchiveYear())) { + return (int) $month; + } + } + } + + return null; + } + + /** + * Fetches the archive day from the url. + * + * @return null|int + */ + public function getArchiveDay() + { + $day = $this->request->param('Day'); + + if (preg_match('/^[0-9]{1,2}$/', $day)) { + if (checkdate($this->getArchiveMonth(), $day, $this->getArchiveYear())) { + return (int) $day; + } + } + + return null; + } + + /** + * Renders the blog posts for a given tag. + * + * @return null|HTTPResponse + */ + public function tag() + { + $tag = $this->getCurrentTag(); + + if ($tag) { + $this->blogPosts = $tag->BlogPosts(); + + if($this->isRSS()) { + return $this->rssFeed($this->blogPosts, $tag->getLink()); + } else { + return $this->render(); + } + } + + $this->httpError(404, 'Not Found'); + + return null; + } + + /** + * Tag Getter for use in templates. + * + * @return null|BlogTag + */ + public function getCurrentTag() + { + /** + * @var Blog $dataRecord + */ + $dataRecord = $this->dataRecord; + $tag = $this->request->param('Tag'); + if ($tag) { + return $dataRecord->Tags() + ->filter('URLSegment', array($tag, rawurlencode($tag))) + ->first(); + } + return null; + } + + /** + * Renders the blog posts for a given category. + * + * @return null|SS_HTTPResponse + */ + public function category() + { + $category = $this->getCurrentCategory(); + + if ($category) { + $this->blogPosts = $category->BlogPosts(); + + if($this->isRSS()) { + return $this->rssFeed($this->blogPosts, $category->getLink()); + } else { + return $this->render(); + } + } + + $this->httpError(404, 'Not Found'); + + return null; + } + + /** + * Category Getter for use in templates. + * + * @return null|BlogCategory + */ + public function getCurrentCategory() + { + /** + * @var Blog $dataRecord + */ + $dataRecord = $this->dataRecord; + $category = $this->request->param('Category'); + if ($category) { + return $dataRecord->Categories() + ->filter('URLSegment', array($category, rawurlencode($category))) + ->first(); + } + return null; + } + + /** + * Get the meta title for the current action. + * + * @return string + */ + public function getMetaTitle() + { + $title = $this->data()->getTitle(); + $filter = $this->getFilterDescription(); + + if ($filter) { + $title = sprintf('%s - %s', $title, $filter); + } + + $this->extend('updateMetaTitle', $title); + + return $title; + } + + /** + * Returns a description of the current filter. + * + * @return string + */ + public function getFilterDescription() + { + $items = array(); + + $list = $this->PaginatedList(); + $currentPage = $list->CurrentPage(); + + if ($currentPage > 1) { + $items[] = _t( + 'Blog.FILTERDESCRIPTION_PAGE', + 'Page {page}', + null, + array( + 'page' => $currentPage + ) + ); + } + + if ($author = $this->getCurrentProfile()) { + $items[] = _t( + 'Blog.FILTERDESCRIPTION_AUTHOR', + 'By {author}', + null, + array( + 'author' => $author->Title + ) + ); + } + + if ($tag = $this->getCurrentTag()) { + $items[] = _t( + 'Blog.FILTERDESCRIPTION_TAG', + 'Tagged with {tag}', + null, + array( + 'tag' => $tag->Title + ) + ); + } + + if ($category = $this->getCurrentCategory()) { + $items[] = _t( + 'Blog.FILTERDESCRIPTION_CATEGORY', + 'In category {category}', + null, + array( + 'category' => $category->Title + ) + ); + } + + if ($this->owner->getArchiveYear()) { + if ($this->owner->getArchiveDay()) { + $date = $this->owner->getArchiveDate()->Nice(); + } elseif ($this->owner->getArchiveMonth()) { + $date = $this->owner->getArchiveDate()->format('F, Y'); + } else { + $date = $this->owner->getArchiveDate()->format('Y'); + } + + $items[] = _t( + 'Blog.FILTERDESCRIPTION_DATE', + 'In {date}', + null, + array( + 'date' => $date, + ) + ); + } + + $result = ''; + + if ($items) { + $result = implode(', ', $items); + } + + $this->extend('updateFilterDescription', $result); + + return $result; + } + + /** + * Returns a list of paginated blog posts based on the BlogPost dataList. + * + * @return PaginatedList + */ + public function PaginatedList() + { + $allPosts = $this->blogPosts ?: ArrayList::create(); + + $posts = PaginatedList::create($allPosts); + + // Set appropriate page size + if ($this->PostsPerPage > 0) { + $pageSize = $this->PostsPerPage; + } elseif ($count = $allPosts->count()) { + $pageSize = $count; + } else { + $pageSize = 99999; + } + $posts->setPageLength($pageSize); + + // Set current page + $start = $this->request->getVar($posts->getPaginationGetVar()); + $posts->setPageStart($start); + + return $posts; + } + + /** + * Displays an RSS feed of blog posts. + * + * @return string + */ + public function rss() + { + /** + * @var Blog $dataRecord + */ + $dataRecord = $this->dataRecord; + + $this->blogPosts = $dataRecord->getBlogPosts(); + + return $this->rssFeed($this->blogPosts, $this->Link()); + } + + /** + * Returns the current archive date. + * + * @return null|Date + */ + public function getArchiveDate() + { + $year = $this->getArchiveYear(); + $month = $this->getArchiveMonth(); + $day = $this->getArchiveDay(); + + if ($year) { + if ($month) { + $date = sprintf('%s-%s-01', $year, $month); + + if ($day) { + $date = sprintf('%s-%s-%s', $year, $month, $day); + } + } else { + $date = sprintf('%s-01-01', $year); + } + + $obj = DBDatetime::create('date'); + $obj->setValue($date); + return $obj; + } + + return null; + } + + /** + * Returns a link to the RSS feed. + * + * @return string + */ + public function getRSSLink() + { + return $this->Link('rss'); + } + + /** + * Displays an RSS feed of the given blog posts. + * + * @param DataList $blogPosts + * @param string $link + * + * @return string + */ + protected function rssFeed($blogPosts, $link) + { + $rss = new RSSFeed($blogPosts, $link, $this->MetaTitle, $this->MetaDescription); + + $this->extend('updateRss', $rss); + + return $rss->outputToBrowser(); + } + + /** + * Returns true if the $Rss sub-action for categories/tags has been set to "rss" + * + * @return bool + */ + protected function isRSS() + { + $rss = $this->request->param('Rss'); + return (is_string($rss) && strcasecmp($rss, 'rss') == 0); + } +} diff --git a/src/Controllers/BlogPostController.php b/src/Controllers/BlogPostController.php new file mode 100644 index 0000000..56f7dc2 --- /dev/null +++ b/src/Controllers/BlogPostController.php @@ -0,0 +1,14 @@ +Fields->push($addAction); return array( - $this->targetFragment => $forTemplate->renderWith('GridFieldAddByDBField') + $this->targetFragment => $forTemplate->renderWith('SilverStripe\\Blog\\Form\\GridField\\GridFieldAddByDBField') ); } } diff --git a/code/forms/gridfield/GridFieldBlogPostState.php b/src/Forms/GridField/GridFieldBlogPostState.php similarity index 94% rename from code/forms/gridfield/GridFieldBlogPostState.php rename to src/Forms/GridField/GridFieldBlogPostState.php index 3d01ce4..609f014 100644 --- a/code/forms/gridfield/GridFieldBlogPostState.php +++ b/src/Forms/GridField/GridFieldBlogPostState.php @@ -1,5 +1,12 @@ 'BlogTag', - 'Categories' => 'BlogCategory', + 'Tags' => 'SilverStripe\\Blog\\Model\\BlogTag', + 'Categories' => 'SilverStripe\\Blog\\Model\\BlogCategory', ); /** * @var array */ private static $many_many = array( - 'Editors' => 'Member', - 'Writers' => 'Member', - 'Contributors' => 'Member', + 'Editors' => 'SilverStripe\\Security\\Member', + 'Writers' => 'SilverStripe\\Security\\Member', + 'Contributors' => 'SilverStripe\\Security\\Member', ); /** * @var array */ private static $allowed_children = array( - 'BlogPost', + 'SilverStripe\\Blog\\Model\\BlogPost', ); /** * @var array */ private static $extensions = array( - 'BlogFilter', + 'SilverStripe\\Blog\\Model\\BlogFilter', ); /** @@ -95,7 +123,7 @@ class Blog extends Page implements PermissionProvider */ private static $defaults = array( 'ProvideComments' => false, - 'PostsPerPage' => 10, + 'PostsPerPage' => 10 ); /** @@ -124,14 +152,14 @@ class Blog extends Page implements PermissionProvider 'Categories', _t('Blog.Categories', 'Categories'), $self->Categories(), - GridFieldCategorisationConfig::create(15, $self->Categories()->sort('Title'), 'BlogCategory', 'Categories', 'BlogPosts') + GridFieldCategorisationConfig::create(15, $self->Categories()->sort('Title'), 'SilverStripe\\Blog\\Model\\BlogCategory', 'Categories', 'BlogPosts') ); $tags = GridField::create( 'Tags', _t('Blog.Tags', 'Tags'), $self->Tags(), - GridFieldCategorisationConfig::create(15, $self->Tags()->sort('Title'), 'BlogTag', 'Tags', 'BlogPosts') + GridFieldCategorisationConfig::create(15, $self->Tags()->sort('Title'), 'SilverStripe\\Blog\\Model\\BlogTag', 'Tags', 'BlogPosts') ); /** @@ -473,7 +501,7 @@ class Blog extends Page implements PermissionProvider $stage = '_' . $stage; } - $query->innerJoin('BlogPost', sprintf('"SiteTree%s"."ID" = "BlogPost%s"."ID"', $stage, $stage)); + $query->innerJoin('SilverStripe\\Blog\\Model\\BlogPost', sprintf('"SiteTree%s"."ID" = "BlogPost%s"."ID"', $stage, $stage)); $conn = DB::getConn(); @@ -624,512 +652,3 @@ class Blog extends Page implements PermissionProvider return $group; } } - -/** - * @package silverstripe - * @subpackage blog - */ -class Blog_Controller extends Page_Controller -{ - /** - * @var array - */ - private static $allowed_actions = array( - 'archive', - 'tag', - 'category', - 'rss', - 'profile', - ); - - /** - * @var array - */ - private static $url_handlers = array( - 'tag/$Tag!/$Rss' => 'tag', - 'category/$Category!/$Rss' => 'category', - 'archive/$Year!/$Month/$Day' => 'archive', - 'profile/$URLSegment!' => 'profile', - ); - - /** - * @var array - */ - private static $casting = array( - 'MetaTitle' => 'Text', - 'FilterDescription' => 'Text', - ); - - /** - * The current Blog Post DataList query. - * - * @var DataList - */ - protected $blogPosts; - - /** - * @return string - */ - public function index() - { - /** - * @var Blog $dataRecord - */ - $dataRecord = $this->dataRecord; - - $this->blogPosts = $dataRecord->getBlogPosts(); - - return $this->render(); - } - - /** - * Renders a Blog Member's profile. - * - * @return SS_HTTPResponse - */ - public function profile() - { - $profile = $this->getCurrentProfile(); - - if (!$profile) { - return $this->httpError(404, 'Not Found'); - } - - $this->blogPosts = $this->getCurrentProfilePosts(); - - return $this->render(); - } - - /** - * Get the Member associated with the current URL segment. - * - * @return null|Member - */ - public function getCurrentProfile() - { - $urlSegment = $this->request->param('URLSegment'); - - if ($urlSegment) { - return Member::get() - ->filter('URLSegment', $urlSegment) - ->first(); - } - - return null; - } - - /** - * Get posts related to the current Member profile. - * - * @return null|DataList - */ - public function getCurrentProfilePosts() - { - $profile = $this->getCurrentProfile(); - - if ($profile) { - return $profile->BlogPosts()->filter('ParentID', $this->ID); - } - - return null; - } - - /** - * Renders an archive for a specified date. This can be by year or year/month. - * - * @return null|SS_HTTPResponse - */ - public function archive() - { - /** - * @var Blog $dataRecord - */ - $dataRecord = $this->dataRecord; - - $year = $this->getArchiveYear(); - $month = $this->getArchiveMonth(); - $day = $this->getArchiveDay(); - - if ($this->request->param('Month') && !$month) { - $this->httpError(404, 'Not Found'); - } - - if ($month && $this->request->param('Day') && !$day) { - $this->httpError(404, 'Not Found'); - } - - if ($year) { - $this->blogPosts = $dataRecord->getArchivedBlogPosts($year, $month, $day); - - return $this->render(); - } - - $this->httpError(404, 'Not Found'); - - return null; - } - - /** - * Fetches the archive year from the url. - * - * @return int - */ - public function getArchiveYear() - { - if ($this->request->param('Year')) { - if (preg_match('/^[0-9]{4}$/', $year = $this->request->param('Year'))) { - return (int) $year; - } - } elseif ($this->request->param('Action') == 'archive') { - return SS_Datetime::now()->Year(); - } - - return null; - } - - /** - * Fetches the archive money from the url. - * - * @return null|int - */ - public function getArchiveMonth() - { - $month = $this->request->param('Month'); - - if (preg_match('/^[0-9]{1,2}$/', $month)) { - if ($month > 0 && $month < 13) { - if (checkdate($month, 01, $this->getArchiveYear())) { - return (int) $month; - } - } - } - - return null; - } - - /** - * Fetches the archive day from the url. - * - * @return null|int - */ - public function getArchiveDay() - { - $day = $this->request->param('Day'); - - if (preg_match('/^[0-9]{1,2}$/', $day)) { - if (checkdate($this->getArchiveMonth(), $day, $this->getArchiveYear())) { - return (int) $day; - } - } - - return null; - } - - /** - * Renders the blog posts for a given tag. - * - * @return null|SS_HTTPResponse - */ - public function tag() - { - $tag = $this->getCurrentTag(); - - if ($tag) { - $this->blogPosts = $tag->BlogPosts(); - - if($this->isRSS()) { - return $this->rssFeed($this->blogPosts, $tag->getLink()); - } else { - return $this->render(); - } - } - - $this->httpError(404, 'Not Found'); - - return null; - } - - /** - * Tag Getter for use in templates. - * - * @return null|BlogTag - */ - public function getCurrentTag() - { - /** - * @var Blog $dataRecord - */ - $dataRecord = $this->dataRecord; - $tag = $this->request->param('Tag'); - if ($tag) { - return $dataRecord->Tags() - ->filter('URLSegment', array($tag, rawurlencode($tag))) - ->first(); - } - return null; - } - - /** - * Renders the blog posts for a given category. - * - * @return null|SS_HTTPResponse - */ - public function category() - { - $category = $this->getCurrentCategory(); - - if ($category) { - $this->blogPosts = $category->BlogPosts(); - - if($this->isRSS()) { - return $this->rssFeed($this->blogPosts, $category->getLink()); - } else { - return $this->render(); - } - } - - $this->httpError(404, 'Not Found'); - - return null; - } - - /** - * Category Getter for use in templates. - * - * @return null|BlogCategory - */ - public function getCurrentCategory() - { - /** - * @var Blog $dataRecord - */ - $dataRecord = $this->dataRecord; - $category = $this->request->param('Category'); - if ($category) { - return $dataRecord->Categories() - ->filter('URLSegment', array($category, rawurlencode($category))) - ->first(); - } - return null; - } - - /** - * Get the meta title for the current action. - * - * @return string - */ - public function getMetaTitle() - { - $title = $this->data()->getTitle(); - $filter = $this->getFilterDescription(); - - if ($filter) { - $title = sprintf('%s - %s', $title, $filter); - } - - $this->extend('updateMetaTitle', $title); - - return $title; - } - - /** - * Returns a description of the current filter. - * - * @return string - */ - public function getFilterDescription() - { - $items = array(); - - $list = $this->PaginatedList(); - $currentPage = $list->CurrentPage(); - - if ($currentPage > 1) { - $items[] = _t( - 'Blog.FILTERDESCRIPTION_PAGE', - 'Page {page}', - null, - array( - 'page' => $currentPage, - ) - ); - } - - if ($author = $this->getCurrentProfile()) { - $items[] = _t( - 'Blog.FILTERDESCRIPTION_AUTHOR', - 'By {author}', - null, - array( - 'author' => $author->Title, - ) - ); - } - - if ($tag = $this->getCurrentTag()) { - $items[] = _t( - 'Blog.FILTERDESCRIPTION_TAG', - 'Tagged with {tag}', - null, - array( - 'tag' => $tag->Title, - ) - ); - } - - if ($category = $this->getCurrentCategory()) { - $items[] = _t( - 'Blog.FILTERDESCRIPTION_CATEGORY', - 'In category {category}', - null, - array( - 'category' => $category->Title, - ) - ); - } - - if ($this->owner->getArchiveYear()) { - if ($this->owner->getArchiveDay()) { - $date = $this->owner->getArchiveDate()->Nice(); - } elseif ($this->owner->getArchiveMonth()) { - $date = $this->owner->getArchiveDate()->format('F, Y'); - } else { - $date = $this->owner->getArchiveDate()->format('Y'); - } - - $items[] = _t( - 'Blog.FILTERDESCRIPTION_DATE', - 'In {date}', - null, - array( - 'date' => $date, - ) - ); - } - - $result = ''; - - if ($items) { - $result = implode(', ', $items); - } - - $this->extend('updateFilterDescription', $result); - - return $result; - } - - /** - * Returns a list of paginated blog posts based on the BlogPost dataList. - * - * @return PaginatedList - */ - public function PaginatedList() - { - $allPosts = $this->blogPosts ?: new ArrayList(); - - $posts = new PaginatedList($allPosts); - - // Set appropriate page size - if ($this->PostsPerPage > 0) { - $pageSize = $this->PostsPerPage; - } elseif ($count = $allPosts->count()) { - $pageSize = $count; - } else { - $pageSize = 99999; - } - $posts->setPageLength($pageSize); - - // Set current page - $start = $this->request->getVar($posts->getPaginationGetVar()); - $posts->setPageStart($start); - - return $posts; - } - - /** - * Displays an RSS feed of blog posts. - * - * @return string - */ - public function rss() - { - /** - * @var Blog $dataRecord - */ - $dataRecord = $this->dataRecord; - - $this->blogPosts = $dataRecord->getBlogPosts(); - - return $this->rssFeed($this->blogPosts, $this->Link()); - } - - /** - * Returns the current archive date. - * - * @return null|Date - */ - public function getArchiveDate() - { - $year = $this->getArchiveYear(); - $month = $this->getArchiveMonth(); - $day = $this->getArchiveDay(); - - if ($year) { - if ($month) { - $date = sprintf('%s-%s-01', $year, $month); - - if ($day) { - $date = sprintf('%s-%s-%s', $year, $month, $day); - } - } else { - $date = sprintf('%s-01-01', $year); - } - - $obj = new DBDatetime('date'); - $obj->setValue($date); - return $obj; - } - - return null; - } - - /** - * Returns a link to the RSS feed. - * - * @return string - */ - public function getRSSLink() - { - return $this->Link('rss'); - } - - /** - * Displays an RSS feed of the given blog posts. - * - * @param DataList $blogPosts - * @param string $link - * - * @return string - */ - protected function rssFeed($blogPosts, $link) - { - $rss = new RSSFeed($blogPosts, $link, $this->MetaTitle, $this->MetaDescription); - - $this->extend('updateRss', $rss); - - return $rss->outputToBrowser(); - } - - /** - * Returns true if the $Rss sub-action for categories/tags has been set to "rss" - */ - private function isRSS() - { - $rss = $this->request->param('Rss'); - if(is_string($rss) && strcasecmp($rss, "rss") == 0) { - return true; - } else { - return false; - } - } -} diff --git a/code/model/BlogCategory.php b/src/Model/BlogCategory.php similarity index 71% rename from code/model/BlogCategory.php rename to src/Model/BlogCategory.php index c4ea804..28b6dd5 100644 --- a/code/model/BlogCategory.php +++ b/src/Model/BlogCategory.php @@ -1,5 +1,11 @@ 'Varchar(255)', - 'URLSegment' => 'Varchar(255)', + 'Title' => 'Varchar(255)', + 'URLSegment' => 'Varchar(255)' ); /** * @var array */ private static $has_one = array( - 'Blog' => 'Blog', + 'Blog' => 'SilverStripe\\Blog\\Model\\Blog', ); /** * @var array */ private static $belongs_many_many = array( - 'BlogPosts' => 'BlogPost', + 'BlogPosts' => 'SilverStripe\\Blog\\Model\\BlogPost', ); /** diff --git a/code/extensions/BlogCommentExtension.php b/src/Model/BlogCommentExtension.php similarity index 82% rename from code/extensions/BlogCommentExtension.php rename to src/Model/BlogCommentExtension.php index de4576a..9768a60 100644 --- a/code/extensions/BlogCommentExtension.php +++ b/src/Model/BlogCommentExtension.php @@ -1,5 +1,10 @@ dataQuery() ->innerJoin('BlogPost', sprintf('"BlogPost%s"."ID" = "SiteTree%s"."ID"', $stage, $stage)) - ->where(sprintf('"PublishDate" < \'%s\'', Convert::raw2sql(SS_Datetime::now()))); + ->where(sprintf('"PublishDate" < \'%s\'', Convert::raw2sql(DBDatetime::now()))); $staged = $staged->setDataQuery($dataQuery); } @@ -40,7 +55,7 @@ class BlogFilter extends Lumberjack */ protected function subclassForBlog() { - return in_array(get_class($this->owner), ClassInfo::subClassesFor('Blog')); + return in_array(get_class($this->owner), ClassInfo::subclassesFor('Blog')); } /** @@ -53,7 +68,7 @@ class BlogFilter extends Lumberjack if (!$this->shouldFilter() && $this->isBlog() && !Permission::check('VIEW_DRAFT_CONTENT')) { $dataQuery = $staged->dataQuery() ->innerJoin('BlogPost', '"BlogPost_Live"."ID" = "SiteTree_Live"."ID"') - ->where(sprintf('"PublishDate" < \'%s\'', Convert::raw2sql(SS_Datetime::now()))); + ->where(sprintf('"PublishDate" < \'%s\'', Convert::raw2sql(DBDatetime::now()))); $staged = $staged->setDataQuery($dataQuery); } @@ -82,14 +97,14 @@ class BlogFilter extends Lumberjack 'ClassName' => $excluded )); - $gridField = new BlogFilter_GridField( + $gridField = BlogFilter_GridField::create( 'ChildPages', $this->getLumberjackTitle(), $pages, $this->getLumberjackGridFieldConfig() ); - $tab = new Tab('ChildPages', $this->getLumberjackTitle(), $gridField); + $tab = Tab::create('ChildPages', $this->getLumberjackTitle(), $gridField); $fields->insertBefore($tab, 'Main'); } diff --git a/code/extensions/BlogMemberExtension.php b/src/Model/BlogMemberExtension.php similarity index 81% rename from code/extensions/BlogMemberExtension.php rename to src/Model/BlogMemberExtension.php index 1a3f2d7..eb2c152 100644 --- a/code/extensions/BlogMemberExtension.php +++ b/src/Model/BlogMemberExtension.php @@ -1,5 +1,16 @@ 'BlogPost', + 'BlogPosts' => 'SilverStripe\\Blog\\Model\\BlogPost', ); /** @@ -80,7 +91,6 @@ class BlogMemberExtension extends DataExtension return $conflict->count() == 0; } - /** * {@inheritdoc} */ @@ -97,13 +107,13 @@ class BlogMemberExtension extends DataExtension Requirements::css(BLOGGER_DIR . '/css/cms.css'); Requirements::javascript(BLOGGER_DIR . '/js/cms.js'); - $tab = new Tab('BlogPosts', 'Blog Posts'); + $tab = Tab::create('BlogPosts', 'Blog Posts'); - $gridField = new GridField( + $gridField = GridField::create( 'BlogPosts', 'Blog Posts', $this->owner->BlogPosts(), - new GridFieldConfig_BlogPost() + GridFieldConfig_BlogPost::create() ); $tab->Fields()->add($gridField); diff --git a/code/traits/BlogObject.php b/src/Model/BlogObject.php similarity index 82% rename from code/traits/BlogObject.php rename to src/Model/BlogObject.php index 1d0b091..07fd0d1 100644 --- a/code/traits/BlogObject.php +++ b/src/Model/BlogObject.php @@ -1,13 +1,24 @@ valid()) { + if (!$validation->isValid()) { return $validation; } $blog = $this->Blog(); - if(!$blog || !$blog->exists()) { + if (!$blog || !$blog->exists()) { return $validation; } - if($this->getDuplicatesByUrlSegment()->count() > 0) { - $validation->error($this->getDuplicateError(), self::DUPLICATE_EXCEPTION); + if ($this->getDuplicatesByUrlSegment()->count() > 0) { + $validation->addError($this->getDuplicateError(), self::DUPLICATE_EXCEPTION); } return $validation; } @@ -163,7 +176,7 @@ trait BlogObject { public function generateURLSegment($increment = 0) { $increment = (int) $increment; - $filter = new URLSegmentFilter(); + $filter = URLSegmentFilter::create(); $this->URLSegment = $filter->filter($this->owner->Title); @@ -185,10 +198,13 @@ trait BlogObject { */ protected function getDuplicatesByUrlSegment() { - $duplicates = DataList::create(self::class)->filter(array( - 'URLSegment' => $this->URLSegment, - 'BlogID' => (int) $this->BlogID, - )); + $duplicates = DataList::create(self::class) + ->filter( + array( + 'URLSegment' => $this->URLSegment, + 'BlogID' => (int) $this->BlogID, + ) + ); if ($this->ID) { $duplicates = $duplicates->exclude('ID', $this->ID); @@ -217,5 +233,4 @@ trait BlogObject { * @return string */ abstract protected function getDuplicateError(); - } diff --git a/code/model/BlogPost.php b/src/Model/BlogPost.php similarity index 91% rename from code/model/BlogPost.php rename to src/Model/BlogPost.php index 4f5abb8..aaf93af 100644 --- a/code/model/BlogPost.php +++ b/src/Model/BlogPost.php @@ -1,5 +1,27 @@ 'SS_Datetime', + 'PublishDate' => 'Datetime', 'AuthorNames' => 'Varchar(1024)', - 'Summary' => 'HTMLText', + 'Summary' => 'HTMLText', ); /** * @var array */ private static $has_one = array( - 'FeaturedImage' => 'Image', + 'FeaturedImage' => 'SilverStripe\\Assets\\Image', ); /** * @var array */ private static $many_many = array( - 'Categories' => 'BlogCategory', - 'Tags' => 'BlogTag', - 'Authors' => 'Member', + 'Categories' => 'SilverStripe\\Blog\\Model\\BlogCategory', + 'Tags' => 'SilverStripe\\Blog\\Model\\BlogTag', + 'Authors' => 'SilverStripe\\Security\\Member', ); /** * @var array */ private static $defaults = array( - 'ShowInMenus' => false, - 'InheritSideBar' => true, + 'ShowInMenus' => false, + 'InheritSideBar' => true, 'ProvideComments' => true, ); @@ -62,21 +90,21 @@ class BlogPost extends Page * @var array */ private static $extensions = array( - 'BlogPostFilter', + 'SilverStripe\\Blog\\Model\\BlogPostFilter' ); /** * @var array */ private static $searchable_fields = array( - 'Title', + 'Title' ); /** * @var array */ private static $summary_fields = array( - 'Title', + 'Title' ); /** @@ -84,7 +112,7 @@ class BlogPost extends Page */ private static $casting = array( 'Excerpt' => 'HTMLText', - 'Date' => 'SS_Datetime', + 'Date' => 'SS_Datetime' ); /** @@ -412,12 +440,12 @@ class BlogPost extends Page public function onBeforePublish() { /** - * @var SS_Datetime $publishDate + * @var DBDatetime $publishDate */ $publishDate = $this->dbObject('PublishDate'); if (!$publishDate->getValue()) { - $this->PublishDate = SS_Datetime::now()->getValue(); + $this->PublishDate = DBDatetime::now()->getValue(); $this->write(); } } @@ -464,7 +492,7 @@ class BlogPost extends Page } /** - * @var SS_Datetime $publishDate + * @var DBDatetime $publishDate */ $publishDate = $this->dbObject('PublishDate'); if(!$publishDate->exists()) { @@ -563,7 +591,7 @@ class BlogPost extends Page public function getMonthlyArchiveLink($type = 'day') { /** - * @var SS_Datetime $date + * @var DBDatetime $date */ $date = $this->dbObject('PublishDate'); @@ -591,7 +619,7 @@ class BlogPost extends Page public function getYearlyArchiveLink() { /** - * @var SS_Datetime $date + * @var DBDatetime $date */ $date = $this->dbObject('PublishDate'); @@ -706,11 +734,3 @@ class BlogPost extends Page } } } - -/** - * @package silverstripe - * @subpackage blog - */ -class BlogPost_Controller extends Page_Controller -{ -} diff --git a/code/extensions/BlogPostFilter.php b/src/Model/BlogPostFilter.php similarity index 64% rename from code/extensions/BlogPostFilter.php rename to src/Model/BlogPostFilter.php index 26949eb..3efd13a 100644 --- a/code/extensions/BlogPostFilter.php +++ b/src/Model/BlogPostFilter.php @@ -1,5 +1,17 @@ addWhere(sprintf( '"PublishDate" < \'%s\'', - Convert::raw2sql(SS_Datetime::now()) + Convert::raw2sql(DBDatetime::now()) )); } } /** + * {@inheritDoc} + * * This is a fix so that when we try to fetch subclasses of BlogPost, lazy loading includes the * BlogPost table in its query. Leaving this table out means the default sort order column * PublishDate causes an error. * * @see https://github.com/silverstripe/silverstripe-framework/issues/1682 * - * @param SQLQuery $query - * @param mixed $dataQuery - * @param mixed $parent + * @param SQLSelect $query + * @param DataQuery $dataQuery + * @param DataObject $dataObject */ - public function augmentLoadLazyFields( - SQLSelect &$query, - DataQuery &$dataQuery = null, - $dataObject - ) { + public function augmentLoadLazyFields(SQLSelect &$query, DataQuery &$dataQuery = null, $dataObject) + { $dataQuery->innerJoin('BlogPost', '"SiteTree"."ID" = "BlogPost"."ID"'); } } diff --git a/code/extensions/BlogPostNotifications.php b/src/Model/BlogPostNotifications.php similarity index 91% rename from code/extensions/BlogPostNotifications.php rename to src/Model/BlogPostNotifications.php index e3d7ca2..733c338 100644 --- a/code/extensions/BlogPostNotifications.php +++ b/src/Model/BlogPostNotifications.php @@ -1,5 +1,9 @@ 'Varchar(255)', - 'URLSegment' => 'Varchar(255)', + 'Title' => 'Varchar(255)', + 'URLSegment' => 'Varchar(255)' ); /** * @var array */ private static $has_one = array( - 'Blog' => 'Blog', + 'Blog' => 'SilverStripe\\Blog\\Model\\Blog', ); /** * @var array */ private static $belongs_many_many = array( - 'BlogPosts' => 'BlogPost', + 'BlogPosts' => 'SilverStripe\\Blog\\Model\\BlogPost', ); /** diff --git a/code/model/CategorisationObject.php b/src/Model/CategorisationObject.php similarity index 69% rename from code/model/CategorisationObject.php rename to src/Model/CategorisationObject.php index 811419e..827035b 100644 --- a/code/model/CategorisationObject.php +++ b/src/Model/CategorisationObject.php @@ -1,8 +1,11 @@ 'Blog', + 'Blog' => 'SilverStripe\\Blog\\Model\\Blog', ); /** @@ -72,7 +76,7 @@ class BlogArchiveWidget extends Widget * @var FieldList $fields */ $fields->merge(array( - DropdownField::create('BlogID', _t('BlogArchiveWidget.Blog', 'Blog'), Blog::get()->map()), + DropdownField::create('BlogID', _t('BlogArchiveWidget.Blog', 'SilverStripe\\Blog\\Model\\Blog'), Blog::get()->map()), DropdownField::create('ArchiveType', _t('BlogArchiveWidget.ArchiveType', 'ArchiveType'), $type), NumericField::create('NumberToDisplay', _t('BlogArchiveWidget.NumberToDisplay', 'No. to Display')) )); diff --git a/code/widgets/BlogCategoriesWidget.php b/src/Widgets/BlogCategoriesWidget.php similarity index 90% rename from code/widgets/BlogCategoriesWidget.php rename to src/Widgets/BlogCategoriesWidget.php index 1429dab..6cc1ccc 100644 --- a/code/widgets/BlogCategoriesWidget.php +++ b/src/Widgets/BlogCategoriesWidget.php @@ -1,6 +1,10 @@ 'Blog', + 'Blog' => 'SilverStripe\\Blog\\Model\\Blog', ); /** @@ -47,7 +51,7 @@ class BlogCategoriesWidget extends Widget { $this->beforeUpdateCMSFields(function (FieldList $fields) { $fields[] = DropdownField::create( - 'BlogID', _t('BlogCategoriesWidget.Blog', 'Blog'), Blog::get()->map() + 'BlogID', _t('BlogCategoriesWidget.Blog', 'SilverStripe\\Blog\\Model\\Blog'), Blog::get()->map() ); $fields[] = NumericField::create( diff --git a/code/widgets/BlogRecentPostsWidget.php b/src/Widgets/BlogRecentPostsWidget.php similarity index 86% rename from code/widgets/BlogRecentPostsWidget.php rename to src/Widgets/BlogRecentPostsWidget.php index 2456cb4..a097fda 100644 --- a/code/widgets/BlogRecentPostsWidget.php +++ b/src/Widgets/BlogRecentPostsWidget.php @@ -1,6 +1,10 @@ 'Blog', + 'Blog' => 'SilverStripe\\Blog\\Model\\Blog', ); /** @@ -50,7 +54,7 @@ class BlogRecentPostsWidget extends Widget * @var FieldList $fields */ $fields->merge(array( - DropdownField::create('BlogID', _t('BlogRecentPostsWidget.Blog', 'Blog'), Blog::get()->map()), + DropdownField::create('BlogID', _t('BlogRecentPostsWidget.Blog', 'SilverStripe\\Blog\\Model\\Blog'), Blog::get()->map()), NumericField::create('NumberOfPosts', _t('BlogRecentPostsWidget.NumberOfPosts', 'Number of Posts')) )); }); diff --git a/code/widgets/BlogTagsCloudWidget.php b/src/Widgets/BlogTagsCloudWidget.php similarity index 92% rename from code/widgets/BlogTagsCloudWidget.php rename to src/Widgets/BlogTagsCloudWidget.php index 7f81e3e..baf2805 100644 --- a/code/widgets/BlogTagsCloudWidget.php +++ b/src/Widgets/BlogTagsCloudWidget.php @@ -1,5 +1,9 @@ 'Blog', + 'Blog' => 'SilverStripe\\Blog\\Model\\Blog', ); /** @@ -47,7 +51,7 @@ class BlogTagsCloudWidget extends Widget */ $fields->push( DropdownField::create('BlogID', _t('BlogTagsCloudWidget.Blog', - 'Blog'), Blog::get()->map()) + 'SilverStripe\\Blog\\Model\\Blog'), Blog::get()->map()) ); }); diff --git a/code/widgets/BlogTagsWidget.php b/src/Widgets/BlogTagsWidget.php similarity index 90% rename from code/widgets/BlogTagsWidget.php rename to src/Widgets/BlogTagsWidget.php index f2b4677..bcc2564 100644 --- a/code/widgets/BlogTagsWidget.php +++ b/src/Widgets/BlogTagsWidget.php @@ -1,6 +1,10 @@ 'Blog', + 'Blog' => 'SilverStripe\\Blog\\Model\\Blog', ); /** @@ -47,7 +51,7 @@ class BlogTagsWidget extends Widget { $this->beforeUpdateCMSFields(function (Fieldlist $fields) { $fields[] = DropdownField::create( - 'BlogID', _t('BlogTagsWidget.Blog', 'Blog'), Blog::get()->map() + 'BlogID', _t('BlogTagsWidget.Blog', 'SilverStripe\\Blog\\Model\\Blog'), Blog::get()->map() ); $fields[] = NumericField::create( diff --git a/tests/BlogCategoryTest.php b/tests/BlogCategoryTest.php index c40e976..2c1d0cb 100755 --- a/tests/BlogCategoryTest.php +++ b/tests/BlogCategoryTest.php @@ -1,5 +1,13 @@ logout(); } - $this->objFromFixture('BlogPost', 'FirstBlogPost'); + $this->objFromFixture('SilverStripe\\Blog\\Model\\BlogPost', 'FirstBlogPost'); /** * @var BlogCategory $category */ - $category = $this->objFromFixture('BlogCategory', 'FirstCategory'); + $category = $this->objFromFixture('SilverStripe\\Blog\\Model\\BlogCategory', 'FirstCategory'); $this->assertEquals(5, $category->BlogPosts()->count(), 'Category blog post count'); } @@ -72,10 +80,10 @@ class BlogCategoryTest extends FunctionalTest { $this->useDraftSite(); - $this->objFromFixture('Member', 'Admin'); + $this->objFromFixture('SilverStripe\\Security\\Member', 'Admin'); - $editor = $this->objFromFixture('Member', 'Editor'); - $category = $this->objFromFixture('BlogCategory', 'SecondCategory'); + $editor = $this->objFromFixture('SilverStripe\\Security\\Member', 'Editor'); + $category = $this->objFromFixture('SilverStripe\\Blog\\Model\\BlogCategory', 'SecondCategory'); $this->assertFalse($category->canView($editor), 'Editor should not be able to view category.'); } @@ -87,20 +95,20 @@ class BlogCategoryTest extends FunctionalTest { $this->useDraftSite(); - $admin = $this->objFromFixture('Member', 'Admin'); - $editor = $this->objFromFixture('Member', 'Editor'); + $admin = $this->objFromFixture('SilverStripe\\Security\\Member', 'Admin'); + $editor = $this->objFromFixture('SilverStripe\\Security\\Member', 'Editor'); - $category = $this->objFromFixture('BlogCategory', 'FirstCategory'); + $category = $this->objFromFixture('SilverStripe\\Blog\\Model\\BlogCategory', 'FirstCategory'); $this->assertTrue($category->canEdit($admin), 'Admin should be able to edit category.'); $this->assertTrue($category->canEdit($editor), 'Editor should be able to edit category.'); - $category = $this->objFromFixture('BlogCategory', 'SecondCategory'); + $category = $this->objFromFixture('SilverStripe\\Blog\\Model\\BlogCategory', 'SecondCategory'); $this->assertTrue($category->canEdit($admin), 'Admin should be able to edit category.'); $this->assertFalse($category->canEdit($editor), 'Editor should not be able to edit category.'); - $category = $this->objFromFixture('BlogCategory', 'ThirdCategory'); + $category = $this->objFromFixture('SilverStripe\\Blog\\Model\\BlogCategory', 'ThirdCategory'); $this->assertTrue($category->canEdit($admin), 'Admin should always be able to edit category.'); $this->assertTrue($category->canEdit($editor), 'Editor should be able to edit category.'); @@ -110,10 +118,10 @@ class BlogCategoryTest extends FunctionalTest { $this->useDraftSite(); - $admin = $this->objFromFixture('Member', 'Admin'); - $editor = $this->objFromFixture('Member', 'Editor'); + $admin = $this->objFromFixture('SilverStripe\\Security\\Member', 'Admin'); + $editor = $this->objFromFixture('SilverStripe\\Security\\Member', 'Editor'); - $category = singleton('BlogCategory'); + $category = singleton('SilverStripe\\Blog\\Model\\BlogCategory'); $this->assertTrue($category->canCreate($admin), 'Admin should be able to create category.'); $this->assertTrue($category->canCreate($editor), 'Editor should be able to create category.'); @@ -123,24 +131,25 @@ class BlogCategoryTest extends FunctionalTest { $this->useDraftSite(); - $admin = $this->objFromFixture('Member', 'Admin'); - $editor = $this->objFromFixture('Member', 'Editor'); + $admin = $this->objFromFixture('SilverStripe\\Security\\Member', 'Admin'); + $editor = $this->objFromFixture('SilverStripe\\Security\\Member', 'Editor'); - $category = $this->objFromFixture('BlogCategory', 'FirstCategory'); + $category = $this->objFromFixture('SilverStripe\\Blog\\Model\\BlogCategory', 'FirstCategory'); $this->assertTrue($category->canDelete($admin), 'Admin should be able to delete category.'); $this->assertTrue($category->canDelete($editor), 'Editor should be able to category category.'); - $category = $this->objFromFixture('BlogCategory', 'SecondCategory'); + $category = $this->objFromFixture('SilverStripe\\Blog\\Model\\BlogCategory', 'SecondCategory'); $this->assertTrue($category->canDelete($admin), 'Admin should be able to delete category.'); $this->assertFalse($category->canDelete($editor), 'Editor should not be able to delete category.'); - $category = $this->objFromFixture('BlogCategory', 'ThirdCategory'); + $category = $this->objFromFixture('SilverStripe\\Blog\\Model\\BlogCategory', 'ThirdCategory'); $this->assertTrue($category->canDelete($admin), 'Admin should always be able to delete category.'); $this->assertTrue($category->canDelete($editor), 'Editor should be able to delete category.'); } - public function testDuplicateCategories() { + public function testDuplicateCategories() + { $blog = new Blog(); $blog->Title = 'Testing for duplicate categories'; $blog->write(); @@ -159,9 +168,9 @@ class BlogCategoryTest extends FunctionalTest $category->write(); $this->fail('Duplicate BlogCategory written'); } catch (ValidationException $e) { - $codeList = $e->getResult()->codeList(); - $this->assertCount(1, $codeList); - $this->assertEquals(BlogTag::DUPLICATE_EXCEPTION, $codeList[0]); + $messages = $e->getResult()->getMessages(); + $this->assertCount(1, $messages); + $this->assertEquals(BlogTag::DUPLICATE_EXCEPTION, $messages[0]['messageType']); } } } diff --git a/tests/BlogPostFilterTest.php b/tests/BlogPostFilterTest.php index e878e40..a43026d 100755 --- a/tests/BlogPostFilterTest.php +++ b/tests/BlogPostFilterTest.php @@ -1,5 +1,9 @@ objFromFixture('Blog', 'FirstBlog'); + $blog = $this->objFromFixture('SilverStripe\\Blog\\Model\\Blog', 'FirstBlog'); $this->assertEquals(3, $blog->AllChildren()->Count(), 'Filtered blog posts'); - SS_Datetime::set_mock_now('2020-01-01 00:00:00'); + DBDatetime::set_mock_now('2020-01-01 00:00:00'); $this->assertEquals(5, $blog->AllChildren()->Count(), 'Unfiltered blog posts'); } diff --git a/tests/BlogPostNotificationsTest.php b/tests/BlogPostNotificationsTest.php index 25f4073..417502b 100644 --- a/tests/BlogPostNotificationsTest.php +++ b/tests/BlogPostNotificationsTest.php @@ -1,8 +1,11 @@ markTestSkipped('Comments Notification module is not installed'); } - $blogPost = $this->objFromFixture('BlogPost', 'PostC'); + $blogPost = $this->objFromFixture('SilverStripe\\Blog\\Model\\BlogPost', 'PostC'); $comment = new Comment(); $comment->Comment = 'This is a comment'; $comment->write(); @@ -27,8 +30,10 @@ class BlogPostNotificationsTest extends SapphireTest } sort($segments); - $this->assertEquals(array('blog-contributor', 'blog-editor', - 'blog-writer', ), $segments); + $this->assertEquals( + array('blog-contributor', 'blog-editor', 'blog-writer'), + $segments + ); } public function testUpdateNotificationSubject() @@ -36,7 +41,7 @@ class BlogPostNotificationsTest extends SapphireTest if (!class_exists('CommentNotifier')) { $this->markTestSkipped('Comments Notification module is not installed'); } - $blogPost = $this->objFromFixture('BlogPost', 'PostC'); + $blogPost = $this->objFromFixture('SilverStripe\\Blog\\Model\\BlogPost', 'PostC'); $comment = new Comment(); $comment->Comment = 'This is a comment'; $comment->write(); diff --git a/tests/BlogPostTest.php b/tests/BlogPostTest.php index c527604..a5c5a69 100644 --- a/tests/BlogPostTest.php +++ b/tests/BlogPostTest.php @@ -1,8 +1,13 @@ objFromFixture('Member', $user); - $pageRecord = $this->objFromFixture('BlogPost', $page); - SS_Datetime::set_mock_now($date); + $userRecord = $this->objFromFixture('SilverStripe\\Security\\Member', $user); + $pageRecord = $this->objFromFixture('SilverStripe\\Blog\\Model\\BlogPost', $page); + DBDatetime::set_mock_now($date); $this->assertEquals($canView, $pageRecord->canView($userRecord)); } + /** + * @return array + */ public function canViewProvider() { $someFutureDate = '2013-10-10 20:00:00'; @@ -70,12 +78,12 @@ class BlogPostTest extends SapphireTest public function testCandidateAuthors() { - $blogpost = $this->objFromFixture('BlogPost', 'PostC'); + $blogpost = $this->objFromFixture('SilverStripe\\Blog\\Model\\BlogPost', 'PostC'); $this->assertEquals(7, $blogpost->getCandidateAuthors()->count()); //Set the group to draw Members from - Config::inst()->update('BlogPost', 'restrict_authors_to_group', 'blogusers'); + Config::inst()->update('SilverStripe\\Blog\\Model\\BlogPost', 'restrict_authors_to_group', 'blogusers'); $this->assertEquals(3, $blogpost->getCandidateAuthors()->count()); @@ -86,12 +94,12 @@ class BlogPostTest extends SapphireTest public function testCanViewFuturePost() { - $blogPost = $this->objFromFixture('BlogPost', 'NullPublishDate'); + $blogPost = $this->objFromFixture('SilverStripe\\Blog\\Model\\BlogPost', 'NullPublishDate'); - $editor = $this->objFromFixture('Member', 'BlogEditor'); + $editor = $this->objFromFixture('SilverStripe\\Security\\Member', 'BlogEditor'); $this->assertTrue($blogPost->canView($editor)); - $visitor = $this->objFromFixture('Member', 'Visitor'); + $visitor = $this->objFromFixture('SilverStripe\\Security\\Member', 'Visitor'); $this->assertFalse($blogPost->canView($visitor)); } diff --git a/tests/BlogTagTest.php b/tests/BlogTagTest.php index fd61cf5..c27a46c 100755 --- a/tests/BlogTagTest.php +++ b/tests/BlogTagTest.php @@ -1,11 +1,19 @@ logout(); } - $this->objFromFixture('BlogPost', 'FirstBlogPost'); + $this->objFromFixture('SilverStripe\\Blog\\Model\\BlogPost', 'FirstBlogPost'); /** * @var BlogTag $tag */ - $tag = $this->objFromFixture('BlogTag', 'FirstTag'); + $tag = $this->objFromFixture('SilverStripe\\Blog\\Model\\BlogTag', 'FirstTag'); $this->assertEquals(1, $tag->BlogPosts()->count(), 'Tag blog post count'); } @@ -75,15 +83,15 @@ class BlogTagTest extends FunctionalTest { $this->useDraftSite(); - $admin = $this->objFromFixture('Member', 'Admin'); - $editor = $this->objFromFixture('Member', 'Editor'); + $admin = $this->objFromFixture('SilverStripe\\Security\\Member', 'Admin'); + $editor = $this->objFromFixture('SilverStripe\\Security\\Member', 'Editor'); - $tag = $this->objFromFixture('BlogTag', 'FirstTag'); + $tag = $this->objFromFixture('SilverStripe\\Blog\\Model\\BlogTag', 'FirstTag'); $this->assertTrue($tag->canView($admin), 'Admin should be able to view tag.'); $this->assertTrue($tag->canView($editor), 'Editor should be able to view tag.'); - $tag = $this->objFromFixture('BlogTag', 'SecondTag'); + $tag = $this->objFromFixture('SilverStripe\\Blog\\Model\\BlogTag', 'SecondTag'); $this->assertTrue($tag->canView($admin), 'Admin should be able to view tag.'); $this->assertFalse($tag->canView($editor), 'Editor should not be able to view tag.'); @@ -93,20 +101,20 @@ class BlogTagTest extends FunctionalTest { $this->useDraftSite(); - $admin = $this->objFromFixture('Member', 'Admin'); - $editor = $this->objFromFixture('Member', 'Editor'); + $admin = $this->objFromFixture('SilverStripe\\Security\\Member', 'Admin'); + $editor = $this->objFromFixture('SilverStripe\\Security\\Member', 'Editor'); - $tag = $this->objFromFixture('BlogTag', 'FirstTag'); + $tag = $this->objFromFixture('SilverStripe\\Blog\\Model\\BlogTag', 'FirstTag'); $this->assertTrue($tag->canEdit($admin), 'Admin should be able to edit tag.'); $this->assertTrue($tag->canEdit($editor), 'Editor should be able to edit tag.'); - $tag = $this->objFromFixture('BlogTag', 'SecondTag'); + $tag = $this->objFromFixture('SilverStripe\\Blog\\Model\\BlogTag', 'SecondTag'); $this->assertTrue($tag->canEdit($admin), 'Admin should be able to edit tag.'); $this->assertFalse($tag->canEdit($editor), 'Editor should not be able to edit tag.'); - $tag = $this->objFromFixture('BlogTag', 'ThirdTag'); + $tag = $this->objFromFixture('SilverStripe\\Blog\\Model\\BlogTag', 'ThirdTag'); $this->assertTrue($tag->canEdit($admin), 'Admin should always be able to edit tags.'); $this->assertTrue($tag->canEdit($editor), 'Editor should be able to edit tag.'); @@ -116,10 +124,10 @@ class BlogTagTest extends FunctionalTest { $this->useDraftSite(); - $admin = $this->objFromFixture('Member', 'Admin'); - $editor = $this->objFromFixture('Member', 'Editor'); + $admin = $this->objFromFixture('SilverStripe\\Security\\Member', 'Admin'); + $editor = $this->objFromFixture('SilverStripe\\Security\\Member', 'Editor'); - $tag = singleton('BlogTag'); + $tag = singleton('SilverStripe\\Blog\\Model\\BlogTag'); $this->assertTrue($tag->canCreate($admin), 'Admin should be able to create tag.'); $this->assertTrue($tag->canCreate($editor), 'Editor should be able to create tag.'); @@ -129,26 +137,27 @@ class BlogTagTest extends FunctionalTest { $this->useDraftSite(); - $admin = $this->objFromFixture('Member', 'Admin'); - $editor = $this->objFromFixture('Member', 'Editor'); + $admin = $this->objFromFixture('SilverStripe\\Security\\Member', 'Admin'); + $editor = $this->objFromFixture('SilverStripe\\Security\\Member', 'Editor'); - $tag = $this->objFromFixture('BlogTag', 'FirstTag'); + $tag = $this->objFromFixture('SilverStripe\\Blog\\Model\\BlogTag', 'FirstTag'); $this->assertTrue($tag->canDelete($admin), 'Admin should be able to delete tag.'); $this->assertTrue($tag->canDelete($editor), 'Editor should be able to delete tag.'); - $tag = $this->objFromFixture('BlogTag', 'SecondTag'); + $tag = $this->objFromFixture('SilverStripe\\Blog\\Model\\BlogTag', 'SecondTag'); $this->assertTrue($tag->canDelete($admin), 'Admin should be able to delete tag.'); $this->assertFalse($tag->canDelete($editor), 'Editor should not be able to delete tag.'); - $tag = $this->objFromFixture('BlogTag', 'ThirdTag'); + $tag = $this->objFromFixture('SilverStripe\\Blog\\Model\\BlogTag', 'ThirdTag'); $this->assertTrue($tag->canDelete($admin), 'Admin should always be able to delete tags.'); $this->assertTrue($tag->canDelete($editor), 'Editor should be able to delete tag.'); } - public function testDuplicateTagsForURLSegment() { + public function testDuplicateTagsForURLSegment() + { $blog = new Blog(); $blog->Title = 'Testing for duplicates blog'; $blog->write(); @@ -163,10 +172,10 @@ class BlogTagTest extends FunctionalTest $tag2->BlogID = $blog->ID; $tag2->write(); $this->assertEquals('cat-test-1', $tag2->URLSegment); - } - public function testDuplicateTags() { + public function testDuplicateTags() + { $blog = new Blog(); $blog->Title = 'Testing for duplicate tags'; $blog->write(); @@ -185,10 +194,9 @@ class BlogTagTest extends FunctionalTest $tag->write(); $this->fail('Duplicate BlogTag written'); } catch (ValidationException $e) { - $codeList = $e->getResult()->codeList(); - $this->assertCount(1, $codeList); - $this->assertEquals(BlogTag::DUPLICATE_EXCEPTION, $codeList[0]); + $messages = $e->getResult()->getMessages(); + $this->assertCount(1, $messages); + $this->assertEquals(BlogTag::DUPLICATE_EXCEPTION, $messages[0]['messageType']); } } - } diff --git a/tests/BlogTagsCloudWidgetTest.php b/tests/BlogTagsCloudWidgetTest.php index d37a2bc..f2564cc 100644 --- a/tests/BlogTagsCloudWidgetTest.php +++ b/tests/BlogTagsCloudWidgetTest.php @@ -1,13 +1,19 @@ markTestSkipped('Widgets module not installed'); } @@ -23,12 +29,13 @@ class BlogTagsCloudWidgetTest extends SapphireTest { $this->assertEquals($expected, $names); } - public function testGetTags() { + public function testGetTags() + { if (!class_exists('Widget')) { $this->markTestSkipped('Widgets module not installed'); } $widget = new BlogTagsCloudWidget(); - $blog = $this->objFromFixture('Blog', 'FourthBlog'); + $blog = $this->objFromFixture('SilverStripe\\Blog\\Model\\Blog', 'FourthBlog'); $widget->BlogID = $blog->ID; $widget->write(); $tags = $widget->getTags()->toArray(); diff --git a/tests/BlogTest.php b/tests/BlogTest.php index bfe63fb..1eeaab0 100755 --- a/tests/BlogTest.php +++ b/tests/BlogTest.php @@ -1,5 +1,17 @@ objFromFixture('Blog', 'FirstBlog'); + $blog = $this->objFromFixture('SilverStripe\\Blog\\Model\\Blog', 'FirstBlog'); $blog->publish('Stage', 'Live'); } @@ -33,7 +45,7 @@ class BlogTest extends SapphireTest */ public function tearDown() { - SS_Datetime::clear_mock_now(); + DBDatetime::clear_mock_now(); Config::unnest(); parent::tearDown(); @@ -50,17 +62,17 @@ class BlogTest extends SapphireTest /** * @var Blog $blog */ - $blog = $this->objFromFixture('Blog', 'FirstBlog'); + $blog = $this->objFromFixture('SilverStripe\\Blog\\Model\\Blog', 'FirstBlog'); - Config::inst()->update('BlogPost', 'show_in_sitetree', true); + Config::inst()->update('SilverStripe\\Blog\\Model\\BlogPost', 'show_in_sitetree', true); $classes = $blog->getExcludedSiteTreeClassNames(); - $this->assertNotContains('BlogPost', $classes, 'BlogPost class should be hidden.'); + $this->assertNotContains('SilverStripe\\Blog\\Model\\BlogPost', $classes, 'BlogPost class should be hidden.'); - Config::inst()->update('BlogPost', 'show_in_sitetree', false); + Config::inst()->update('SilverStripe\\Blog\\Model\\BlogPost', 'show_in_sitetree', false); $classes = $blog->getExcludedSiteTreeClassNames(); - $this->assertContains('BlogPost', $classes, 'BlogPost class should be hidden.'); + $this->assertContains('SilverStripe\\Blog\\Model\\BlogPost', $classes, 'BlogPost class should be hidden.'); } public function testGetArchivedBlogPosts() @@ -74,7 +86,7 @@ class BlogTest extends SapphireTest /** * @var Blog $blog */ - $blog = $this->objFromFixture('Blog', 'FirstBlog'); + $blog = $this->objFromFixture('SilverStripe\\Blog\\Model\\Blog', 'FirstBlog'); $archive = $blog->getArchivedBlogPosts(2013); @@ -96,7 +108,7 @@ class BlogTest extends SapphireTest /** * @var Blog $blog */ - $blog = $this->objFromFixture('Blog', 'FirstBlog'); + $blog = $this->objFromFixture('SilverStripe\\Blog\\Model\\Blog', 'FirstBlog'); $link = Controller::join_links($blog->Link('archive'), '2013', '10', '01'); @@ -135,8 +147,8 @@ class BlogTest extends SapphireTest */ public function testArchiveYear() { - $blog = $this->objFromFixture('Blog', 'FirstBlog'); - $controller = new Blog_Controller($blog); + $blog = $this->objFromFixture('SilverStripe\\Blog\\Model\\Blog', 'FirstBlog'); + $controller = new BlogController($blog); $this->requestURL($controller, 'first-post/archive/'); $this->assertEquals(2013, $controller->getArchiveYear(), 'getArchiveYear should return 2013'); } @@ -156,47 +168,47 @@ class BlogTest extends SapphireTest /** * @var Blog $firstBlog */ - $firstBlog = $this->objFromFixture('Blog', 'FirstBlog'); + $firstBlog = $this->objFromFixture('SilverStripe\\Blog\\Model\\Blog', 'FirstBlog'); /** * @var Blog $fourthBlog */ - $fourthBlog = $this->objFromFixture('Blog', 'FourthBlog'); + $fourthBlog = $this->objFromFixture('SilverStripe\\Blog\\Model\\Blog', 'FourthBlog'); /** * @var BlogPost $postA */ - $postA = $this->objFromFixture('BlogPost', 'PostA'); + $postA = $this->objFromFixture('SilverStripe\\Blog\\Model\\BlogPost', 'PostA'); /** * @var BlogPost $postB */ - $postB = $this->objFromFixture('BlogPost', 'PostB'); + $postB = $this->objFromFixture('SilverStripe\\Blog\\Model\\BlogPost', 'PostB'); /** * @var BlogPost $postC */ - $postC = $this->objFromFixture('BlogPost', 'PostC'); + $postC = $this->objFromFixture('SilverStripe\\Blog\\Model\\BlogPost', 'PostC'); /** * @var Member $editor */ - $editor = $this->objFromFixture('Member', 'BlogEditor'); + $editor = $this->objFromFixture('SilverStripe\\Security\\Member', 'BlogEditor'); /** * @var Member $writer */ - $writer = $this->objFromFixture('Member', 'Writer'); + $writer = $this->objFromFixture('SilverStripe\\Security\\Member', 'Writer'); /** * @var Member $contributor */ - $contributor = $this->objFromFixture('Member', 'Contributor'); + $contributor = $this->objFromFixture('SilverStripe\\Security\\Member', 'Contributor'); /** * @var Member $visitor */ - $visitor = $this->objFromFixture('Member', 'Visitor'); + $visitor = $this->objFromFixture('SilverStripe\\Security\\Member', 'Visitor'); $this->assertEquals('Editor', $fourthBlog->RoleOf($editor)); $this->assertEquals('Contributor', $fourthBlog->RoleOf($contributor)); @@ -274,9 +286,9 @@ class BlogTest extends SapphireTest public function testFilteredCategories() { - $blog = $this->objFromFixture('Blog', 'FirstBlog'); - $controller = new Blog_Controller($blog); - + $blog = $this->objFromFixture('SilverStripe\\Blog\\Model\\Blog', 'FirstBlog'); + $controller = new BlogController($blog); + // Root url $this->requestURL($controller, 'first-post'); $this->assertIDsEquals( @@ -293,10 +305,10 @@ class BlogTest extends SapphireTest ); // Posts - $firstPostID = $this->idFromFixture('BlogPost', 'FirstBlogPost'); - $secondPostID = $this->idFromFixture('BlogPost', 'SecondBlogPost'); - $firstFuturePostID = $this->idFromFixture('BlogPost', 'FirstFutureBlogPost'); - $secondFuturePostID = $this->idFromFixture('BlogPost', 'SecondFutureBlogPost'); + $firstPostID = $this->idFromFixture('SilverStripe\\Blog\\Model\\BlogPost', 'FirstBlogPost'); + $secondPostID = $this->idFromFixture('SilverStripe\\Blog\\Model\\BlogPost', 'SecondBlogPost'); + $firstFuturePostID = $this->idFromFixture('SilverStripe\\Blog\\Model\\BlogPost', 'FirstFutureBlogPost'); + $secondFuturePostID = $this->idFromFixture('SilverStripe\\Blog\\Model\\BlogPost', 'SecondFutureBlogPost'); // Request first tag $this->requestURL($controller, 'first-post/tag/first-tag'); @@ -321,7 +333,7 @@ class BlogTest extends SapphireTest */ protected function requestURL(ContentController $controller, $url) { - $request = new SS_HTTPRequest('get', $url); + $request = new HTTPRequest('get', $url); $request->match('$URLSegment//$Action/$ID/$OtherID'); $request->shift(); $controller->init(); diff --git a/tests/blog.yml b/tests/blog.yml index cad0fc0..79a09f3 100755 --- a/tests/blog.yml +++ b/tests/blog.yml @@ -1,6 +1,6 @@ # Mock date is set to 2013-10-01 20:00:00 -Group: +SilverStripe\Security\Group: Administrators: Title: Administrators Editors: @@ -9,193 +9,193 @@ Group: Title: Blog Users Code: blogusers -Permission: +SilverStripe\Security\Permission: Administrators: Code: ADMIN - Group: =>Group.Administrators + Group: =>SilverStripe\Security\Group.Administrators Editors: Code: CMS_ACCESS_CMSMain - Group: =>Group.Editors + Group: =>SilverStripe\Security\Group.Editors BlogUsers: Code: CMS_ACCESS_CMSMain - Group: =>Group.BlogUsers + Group: =>SilverStripe\Security\Group.BlogUsers -SiteConfig: +SilverStripe\SiteConfig\SiteConfig: Default: CanEditType: 'OnlyTheseUsers' CanCreateTopLevelType: 'OnlyTheseUsers' - EditorGroups: =>Group.Administrators,=>Group.Editors - CreateTopLevelGroups: =>Group.Administrators,=>Group.Editors + EditorGroups: =>SilverStripe\Security\Group.Administrators,=>SilverStripe\Security\Group.Editors + CreateTopLevelGroups: =>SilverStripe\Security\Group.Administrators,=>SilverStripe\Security\Group.Editors -Member: +SilverStripe\Security\Member: Admin: FirstName: Test Surname: Administrator - Groups: =>Group.Administrators + Groups: =>SilverStripe\Security\Group.Administrators Editor: FirstName: Test Surname: Editor - Groups: =>Group.Editors + Groups: =>SilverStripe\Security\Group.Editors BlogEditor: FirstName: Blog Surname: Editor - Groups: =>Group.BlogUsers + Groups: =>SilverStripe\Security\Group.BlogUsers Writer: FirstName: Blog Surname: Writer - Groups: =>Group.BlogUsers + Groups: =>SilverStripe\Security\Group.BlogUsers Contributor: FirstName: Blog Surname: Contributor - Groups: =>Group.BlogUsers + Groups: =>SilverStripe\Security\Group.BlogUsers Visitor: FirstName: Blog Surname: Visitor -Blog: +SilverStripe\Blog\Model\Blog: FirstBlog: Title: 'First Blog' SecondBlog: Title: 'Second Blog' CanViewType: 'OnlyTheseUsers' CanEditType: 'OnlyTheseUsers' - ViewerGroups: =>Group.Administrators - EditorGroups: =>Group.Administrators + ViewerGroups: =>SilverStripe\Security\Group.Administrators + EditorGroups: =>SilverStripe\Security\Group.Administrators ThirdBlog: Title: 'Third Blog' CanEditType: 'OnlyTheseUsers' - EditorGroups: =>Group.Editors + EditorGroups: =>SilverStripe\Security\Group.Editors FourthBlog: Title: 'Fourth Blog' - Editors: =>Member.BlogEditor - Writers: =>Member.Writer - Contributors: =>Member.Contributor + Editors: =>SilverStripe\Security\Member.BlogEditor + Writers: =>SilverStripe\Security\Member.Writer + Contributors: =>SilverStripe\Security\Member.Contributor -BlogTag: +SilverStripe\Blog\Model\BlogTag: FirstTag: Title: 'First Tag' URLSegment: 'first-tag' - Blog: =>Blog.FirstBlog + Blog: =>SilverStripe\Blog\Model\Blog.FirstBlog SecondTag: Title: 'Second Tag' URLSegment: 'second-tag' - Blog: =>Blog.SecondBlog + Blog: =>SilverStripe\Blog\Model\Blog.SecondBlog ThirdTag: Title: 'Third Tag' URLSegment: 'third-tag' - Blog: =>Blog.ThirdBlog + Blog: =>SilverStripe\Blog\Model\Blog.ThirdBlog #Tags for Tag Cloud widget PopularTag: Title: 'Popular' URLSegment: 'popular-tag' - Blog: =>Blog.FourthBlog + Blog: =>SilverStripe\Blog\Model\Blog.FourthBlog CoolTag: Title: 'Cool' URLSegment: 'cool-tag' - Blog: =>Blog.FourthBlog + Blog: =>SilverStripe\Blog\Model\Blog.FourthBlog CatTag: Title: 'Cat' URLSegment: 'cat-tag' - Blog: =>Blog.FourthBlog + Blog: =>SilverStripe\Blog\Model\Blog.FourthBlog KiwiTag: Title: 'Kiwi' URLSegment: 'kiwi-tag' - Blog: =>Blog.FourthBlog + Blog: =>SilverStripe\Blog\Model\Blog.FourthBlog -BlogCategory: +SilverStripe\Blog\Model\BlogCategory: FirstCategory: Title: 'First Category' URLSegment: 'first-category' - Blog: =>Blog.FirstBlog + Blog: =>SilverStripe\Blog\Model\Blog.FirstBlog SecondCategory: Title: 'Second Category' URLSegment: 'second-category' - Blog: =>Blog.SecondBlog + Blog: =>SilverStripe\Blog\Model\Blog.SecondBlog ThirdCategory: Title: 'Third Category' URLSegment: 'third-category' - Blog: =>Blog.ThirdBlog + Blog: =>SilverStripe\Blog\Model\Blog.ThirdBlog -BlogPost: +SilverStripe\Blog\Model\BlogPost: FirstBlogPost: Title: 'First Post' URLSegment: first-post PublishDate: '2013-10-01 15:00:00' - Parent: =>Blog.FirstBlog - Tags: =>BlogTag.FirstTag - Categories: =>BlogCategory.FirstCategory + Parent: =>SilverStripe\Blog\Model\Blog.FirstBlog + Tags: =>SilverStripe\Blog\Model\BlogTag.FirstTag + Categories: =>SilverStripe\Blog\Model\BlogCategory.FirstCategory SecondBlogPost: Title: 'Second Post' URLSegment: second-post PublishDate: '2013-09-01 15:00:00' - Parent: =>Blog.FirstBlog + Parent: =>SilverStripe\Blog\Model\Blog.FirstBlog ThirdBlogPost: Title: 'Old Post' URLSegment: old-post PublishDate: '2012-01-09 15:00:00' - Parent: =>Blog.FirstBlog + Parent: =>SilverStripe\Blog\Model\Blog.FirstBlog FirstFutureBlogPost: Title: 'Future Post' URLSegment: future-post PublishDate: '2015-01-01 00:00:00' - Tags: =>BlogTag.FirstTag - Categories: =>BlogCategory.FirstCategory - Parent: =>Blog.FirstBlog + Tags: =>SilverStripe\Blog\Model\BlogTag.FirstTag + Categories: =>SilverStripe\Blog\Model\BlogCategory.FirstCategory + Parent: =>SilverStripe\Blog\Model\Blog.FirstBlog SecondFutureBlogPost: Title: 'Future Post 2' URLSegment: future-post-2 PublishDate: '2013-11-01 00:00:00' - Tags: =>BlogTag.FirstTag - Categories: =>BlogCategory.FirstCategory - Parent: =>Blog.FirstBlog + Tags: =>SilverStripe\Blog\Model\BlogTag.FirstTag + Categories: =>SilverStripe\Blog\Model\BlogCategory.FirstCategory + Parent: =>SilverStripe\Blog\Model\Blog.FirstBlog PostA: Title: 'One Post' PublishDate: '2012-01-09 15:00:00' - Parent: =>Blog.FourthBlog - Authors: =>Member.Writer,=>Member.Contributor + Parent: =>SilverStripe\Blog\Model\Blog.FourthBlog + Authors: =>SilverStripe\Security\Member.Writer,=>SilverStripe\Security\Member.Contributor PostB: Title: 'Second Post' PublishDate: '2012-01-09 15:00:00' - Parent: =>Blog.FourthBlog - Authors: =>Member.BlogEditor + Parent: =>SilverStripe\Blog\Model\Blog.FourthBlog + Authors: =>SilverStripe\Security\Member.BlogEditor PostC: Title: 'Third Post' PublishDate: '2012-01-09 15:00:00' - Parent: =>Blog.FourthBlog - Authors: =>Member.BlogEditor,=>Member.Writer,=>Member.Contributor + Parent: =>SilverStripe\Blog\Model\Blog.FourthBlog + Authors: =>SilverStripe\Security\Member.BlogEditor,=>SilverStripe\Security\Member.Writer,=>SilverStripe\Security\Member.Contributor NullPublishDate: Title: 'No publish date' PublishDate: '' - Parent: =>Blog.FourthBlog - Authors: =>Member.BlogEditor,=>Member.Writer,=>Member.Contributor + Parent: =>SilverStripe\Blog\Model\Blog.FourthBlog + Authors: =>SilverStripe\Security\Member.BlogEditor,=>SilverStripe\Security\Member.Writer,=>SilverStripe\Security\Member.Contributor #Posts for the tag cloud widget test TaggedPost1: Title: 'Tagged Post 1' URLSegment: tagged-post-1 PublishDate: '2012-01-09 15:00:00' - Tags: =>BlogTag.PopularTag,=>BlogTag.CoolTag - Categories: =>BlogCategory.FirstCategory - Parent: =>Blog.FourthBlog + Tags: =>SilverStripe\Blog\Model\BlogTag.PopularTag,=>SilverStripe\Blog\Model\BlogTag.CoolTag + Categories: =>SilverStripe\Blog\Model\BlogCategory.FirstCategory + Parent: =>SilverStripe\Blog\Model\Blog.FourthBlog TaggedPost2: Title: 'Tagged Post 2' URLSegment: tagged-post-2 PublishDate: '2012-01-09 15:00:00' - Tags: =>BlogTag.PopularTag,=>BlogTag.CoolTag,=>BlogTag.CatTag - Categories: =>BlogCategory.FirstCategory - Parent: =>Blog.FourthBlog + Tags: =>SilverStripe\Blog\Model\BlogTag.PopularTag,=>SilverStripe\Blog\Model\BlogTag.CoolTag,=>SilverStripe\Blog\Model\BlogTag.CatTag + Categories: =>SilverStripe\Blog\Model\BlogCategory.FirstCategory + Parent: =>SilverStripe\Blog\Model\Blog.FourthBlog TaggedPost3: Title: 'Tagged Post 3' URLSegment: tagged-post-3 PublishDate: '2012-01-09 17:20:00' - Tags: =>BlogTag.PopularTag,=>BlogTag.CoolTag,=>BlogTag.CatTag,=>BlogTag.KiwiTag - Categories: =>BlogCategory.FirstCategory - Parent: =>Blog.FourthBlog + Tags: =>SilverStripe\Blog\Model\BlogTag.PopularTag,=>SilverStripe\Blog\Model\BlogTag.CoolTag,=>SilverStripe\Blog\Model\BlogTag.CatTag,=>SilverStripe\Blog\Model\BlogTag.KiwiTag + Categories: =>SilverStripe\Blog\Model\BlogCategory.FirstCategory + Parent: =>SilverStripe\Blog\Model\Blog.FourthBlog TaggedPost4: Title: 'Tagged Post 4' URLSegment: tagged-post-4 PublishDate: '2012-04-09 15:00:00' - Tags: =>BlogTag.PopularTag - Categories: =>BlogCategory.FirstCategory - Parent: =>Blog.FourthBlog + Tags: =>SilverStripe\Blog\Model\BlogTag.PopularTag + Categories: =>SilverStripe\Blog\Model\BlogCategory.FirstCategory + Parent: =>SilverStripe\Blog\Model\Blog.FourthBlog