From ab3f35257b8a85a2d9cba5d4a463a3e16af0d008 Mon Sep 17 00:00:00 2001 From: Robbie Averill Date: Tue, 17 Jan 2017 08:57:37 +1300 Subject: [PATCH] SS4 compatibility updates - ORM, Form, Controller changes * Remove CommentList and replace with a polymorphic has_one relationship * Tweaks for unit tests. Add tests for encode/decodeClassName. --- .travis.yml | 2 +- .upgrade.yml | 28 ++ _config.php | 2 +- _config/routes.yml | 7 +- composer.json | 7 +- docs/en/Configuration.md | 66 +-- docs/en/Installing.md | 4 +- src/Admin/CommentAdmin.php | 12 + src/Admin/CommentsGridField.php | 5 + src/Admin/CommentsGridFieldAction.php | 15 +- src/Admin/CommentsGridFieldBulkAction.php | 64 +-- .../CommentsGridFieldBulkAction/Handler.php | 78 ++++ src/Admin/CommentsGridFieldConfig.php | 25 +- src/Commenting.php | 36 +- src/Controllers/CommentingController.php | 214 ++++++---- src/Extensions/CommentsExtension.php | 56 ++- src/Model/Comment.php | 283 ++++++------ src/Model/Comment/SecurityToken.php | 120 ++++++ src/Model/CommentList.php | 98 ----- src/Tasks/MigrateCommentParentsTask.php | 47 ++ templates/CommentReplies.ss | 23 - templates/CommentsInterface.ss | 2 +- templates/Includes/CommentReplies.ss | 25 ++ .../CommentsInterface_pendingcomment.ss | 0 .../CommentsInterface_singlecomment.ss | 2 +- tests/CommentAdminTest.php | 13 +- tests/CommentListTest.php | 145 ------- tests/CommentTestHelper.php | 21 +- tests/CommentingControllerTest.php | 200 +++++---- tests/CommentingTest.php | 94 ++-- tests/CommentsExtensionTest.php | 180 +++++--- tests/CommentsGridFieldActionTest.php | 46 +- tests/CommentsGridFieldBulkActionTest.php | 4 + tests/CommentsGridFieldConfigTest.php | 8 +- tests/CommentsGridFieldTest.php | 13 +- tests/CommentsTest.php | 401 ++++++++---------- tests/CommentsTest.yml | 104 +++-- tests/Stubs/CommentableItem.php | 62 +++ tests/Stubs/CommentableItemController.php | 19 + tests/Stubs/CommentableItemDisabled.php | 14 + tests/Stubs/CommentableItemEnabled.php | 14 + 41 files changed, 1418 insertions(+), 1141 deletions(-) create mode 100644 .upgrade.yml create mode 100644 src/Admin/CommentsGridFieldBulkAction/Handler.php create mode 100644 src/Model/Comment/SecurityToken.php delete mode 100644 src/Model/CommentList.php create mode 100644 src/Tasks/MigrateCommentParentsTask.php delete mode 100644 templates/CommentReplies.ss create mode 100644 templates/Includes/CommentReplies.ss rename templates/{ => Includes}/CommentsInterface_pendingcomment.ss (100%) rename templates/{ => Includes}/CommentsInterface_singlecomment.ss (97%) delete mode 100644 tests/CommentListTest.php create mode 100644 tests/Stubs/CommentableItem.php create mode 100644 tests/Stubs/CommentableItemController.php create mode 100644 tests/Stubs/CommentableItemDisabled.php create mode 100644 tests/Stubs/CommentableItemEnabled.php diff --git a/.travis.yml b/.travis.yml index 2587d8a..00b0a82 100644 --- a/.travis.yml +++ b/.travis.yml @@ -43,7 +43,7 @@ before_script: - composer self-update || true - git clone git://github.com/silverstripe-labs/silverstripe-travis-support.git ~/travis-support # Install suggested modules in order to maximize test coverage - - php ~/travis-support/travis_setup.php --source `pwd` --target ~/builds/ss --require "ezyang/htmlpurifier:4.*,silverstripe/cms:~3.1" + - php ~/travis-support/travis_setup.php --source `pwd` --target ~/builds/ss --require "silverstripe/cms:^4.0@dev" - cd ~/builds/ss script: diff --git a/.upgrade.yml b/.upgrade.yml new file mode 100644 index 0000000..42fd6b0 --- /dev/null +++ b/.upgrade.yml @@ -0,0 +1,28 @@ +mappings: + Commenting: SilverStripe\Comments\Commenting + CommentAdmin: SilverStripe\Comments\Admin\CommentAdmin + CommentsGridField: SilverStripe\Comments\Admin\CommentsGridField + CommentsGridFieldAction: SilverStripe\Comments\Admin\CommentsGridFieldAction + CommentsGridFieldBulkAction: SilverStripe\Comments\Admin\CommentsGridFieldBulkAction + CommentsGridFieldBulkAction_Handlers: SilverStripe\Comments\Admin\CommentsGridFieldBulkAction_Handlers + CommentsGridFieldConfig: SilverStripe\Comments\Admin\CommentsGridFieldConfig + CommentingController: SilverStripe\Comments\Controllers\CommentingController + CommentsExtension: SilverStripe\Comments\Extensions\CommentsExtension + Comment: SilverStripe\Comments\Model\Comment + Comment_SecurityToken: SilverStripe\Comments\Model\Comment_SecurityToken + CommentList: SilverStripe\Comments\Model\CommentList + CommentAdminTest: SilverStripe\Comments\Tests\CommentAdminTest + CommentingControllerTest: SilverStripe\Comments\Tests\CommentingControllerTest + CommentingTest: SilverStripe\Comments\Tests\CommentingTest + CommentListTest: SilverStripe\Comments\Tests\CommentListTest + CommentsExtensionTest: SilverStripe\Comments\Tests\CommentsExtensionTest + CommentsGridFieldActionTest: SilverStripe\Comments\Tests\CommentsGridFieldActionTest + CommentsGridFieldBulkActionTest: SilverStripe\Comments\Tests\CommentsGridFieldBulkActionTest + CommentsGridFieldConfigTest: SilverStripe\Comments\Tests\CommentsGridFieldConfigTest + CommentsGridFieldTest: SilverStripe\Comments\Tests\CommentsGridFieldTest + CommentsTest: SilverStripe\Comments\Tests\CommentsTest + CommentableItem: SilverStripe\Comments\Tests\CommentableItem + CommentableItemEnabled: SilverStripe\Comments\Tests\CommentableItemEnabled + CommentableItemDisabled: SilverStripe\Comments\Tests\CommentableItemDisabled + CommentableItem_Controller: SilverStripe\Comments\Tests\CommentableItem_Controller + CommentTestHelper: SilverStripe\Comments\Tests\CommentTestHelper diff --git a/_config.php b/_config.php index 3fbfa6d..48f849c 100644 --- a/_config.php +++ b/_config.php @@ -1,6 +1,6 @@ getCommentsOption('require_login'); +$loginRequired = singleton('SilverStripe\\CMS\\Model\\SiteTree')->getCommentsOption('require_login'); ``` - + ## HTML Comments -Comments can be configured to contain a restricted set of HTML tags through the -`html_allowed` and `html_allowed_elements` settings. Raw HTML is hardly user -friendly, but combined with a rich-text editor of your own choosing it can +Comments can be configured to contain a restricted set of HTML tags through the +`html_allowed` and `html_allowed_elements` settings. Raw HTML is hardly user +friendly, but combined with a rich-text editor of your own choosing it can allow rich comment formatting. In order to use this feature, you need to install the -[HTMLPurifier](http://htmlpurifier.org/) library. The easiest way to do this is +[HTMLPurifier](http://htmlpurifier.org/) library. The easiest way to do this is through [Composer](http://getcomposer.org). - { - "require": {"ezyang/htmlpurifier": "4.*"} - } +```json + { + "require": {"ezyang/htmlpurifier": "^4.8"} + } +``` -**Important**: Rendering user-provided HTML on your website always risks -exposing your users to cross-site scripting (XSS) attacks, if the HTML isn't +**Important**: Rendering user-provided HTML on your website always risks +exposing your users to cross-site scripting (XSS) attacks, if the HTML isn't properly sanitized. Don't allow tags like `my comment

'; $comment1->write(); $this->assertEquals( @@ -454,7 +478,7 @@ class CommentsTest extends FunctionalTest // With HTML allowed $comment2 = new Comment(); $comment2->AllowHtml = true; - $comment2->BaseClass = 'CommentableItem'; + $comment2->ParentClass = CommentableItem::class; $comment2->Comment = '

my comment

'; $comment2->write(); $this->assertEquals( @@ -466,11 +490,11 @@ class CommentsTest extends FunctionalTest public function testDefaultTemplateRendersHtmlWithAllowHtml() { - if (!class_exists('HTMLPurifier')) { + if (!class_exists('\\HTMLPurifier')) { $this->markTestSkipped('HTMLPurifier class not found'); } - Config::inst()->update('CommentableItem', 'comments', array( + Config::inst()->update(CommentableItem::class, 'comments', array( 'html_allowed_elements' => array('p'), )); @@ -482,7 +506,7 @@ class CommentsTest extends FunctionalTest $comment->Comment = '

my comment

'; $comment->AllowHtml = false; $comment->ParentID = $item->ID; - $comment->BaseClass = 'CommentableItem'; + $comment->BaseClass = CommentableItem::class; $comment->write(); $html = $item->customise(array('CommentsEnabled' => true))->renderWith('CommentsInterface'); @@ -507,7 +531,7 @@ class CommentsTest extends FunctionalTest public function testDefaultEnabled() { // Ensure values are set via cms (not via config) - Config::inst()->update('CommentableItem', 'comments', array( + Config::inst()->update(CommentableItem::class, 'comments', array( 'enabled_cms' => true, 'require_moderation_cms' => true, 'require_login_cms' => true @@ -532,13 +556,13 @@ class CommentsTest extends FunctionalTest // With default = false // Because of config rules about falsey values, apply config to object directly - Config::inst()->update('CommentableItem', 'comments', array( + Config::inst()->update(CommentableItem::class, 'comments', array( 'enabled' => false, 'require_login' => true, 'require_moderation' => true )); $obj = new CommentableItem(); - $this->assertFalse((bool)$obj->getCommentsOption('enabled'), "Default setting is disabled"); + $this->assertFalse((bool)$obj->getCommentsOption('enabled'), 'Default setting is disabled'); $this->assertFalse((bool)$obj->ProvideComments); $this->assertEquals('Required', $obj->ModerationRequired); $this->assertTrue((bool)$obj->CommentsRequireLogin); @@ -559,7 +583,7 @@ class CommentsTest extends FunctionalTest */ public function testOnBeforeDelete() { - $comment = $this->objFromFixture('Comment', 'firstComA'); + $comment = $this->objFromFixture(Comment::class, 'firstComA'); $child = new Comment(); $child->Name = 'Fred Bloggs'; @@ -574,8 +598,8 @@ class CommentsTest extends FunctionalTest $comment->delete(); // assert that the new child been deleted - $this->assertFalse(DataObject::get_by_id('Comment', $commentID)); - $this->assertFalse(DataObject::get_by_id('Comment', $childCommentID)); + $this->assertFalse(DataObject::get_by_id(Comment::class, $commentID)); + $this->assertFalse(DataObject::get_by_id(Comment::class, $childCommentID)); } public function testRequireDefaultRecords() @@ -585,41 +609,42 @@ class CommentsTest extends FunctionalTest public function testLink() { - $comment = $this->objFromFixture('Comment', 'thirdComD'); - $this->assertEquals('CommentableItem_Controller#comment-'.$comment->ID, - $comment->Link()); + $comment = $this->objFromFixture(Comment::class, 'thirdComD'); + $this->assertEquals( + 'CommentableItemController#comment-' . $comment->ID, + $comment->Link() + ); $this->assertEquals($comment->ID, $comment->ID); // An orphan comment has no link $comment->ParentID = 0; + $comment->ParentClass = null; $comment->write(); $this->assertEquals('', $comment->Link()); } public function testPermalink() { - $comment = $this->objFromFixture('Comment', 'thirdComD'); + $comment = $this->objFromFixture(Comment::class, 'thirdComD'); $this->assertEquals('comment-' . $comment->ID, $comment->Permalink()); } - /* - Test field labels in 2 languages + /** + * Test field labels in 2 languages */ public function testFieldLabels() { $locale = i18n::get_locale(); i18n::set_locale('fr'); - $comment = $this->objFromFixture('Comment', 'firstComA'); + $comment = $this->objFromFixture(Comment::class, 'firstComA'); $labels = $comment->FieldLabels(); $expected = array( 'Name' => 'Nom de l\'Auteur', 'Comment' => 'Commentaire', 'Email' => 'Email', 'URL' => 'URL', - 'BaseClass' => 'Base Class', 'Moderated' => 'Modéré?', 'IsSpam' => 'Spam?', - 'ParentID' => 'Parent ID', 'AllowHtml' => 'Allow Html', 'SecretToken' => 'Secret Token', 'Depth' => 'Depth', @@ -627,7 +652,8 @@ class CommentsTest extends FunctionalTest 'ParentComment' => 'Parent Comment', 'ChildComments' => 'Child Comments', 'ParentTitle' => 'Parent', - 'Created' => 'Date de publication' + 'Created' => 'Date de publication', + 'Parent' => 'Parent' ); i18n::set_locale($locale); $this->assertEquals($expected, $labels); @@ -637,10 +663,8 @@ class CommentsTest extends FunctionalTest 'Comment' => 'Comment', 'Email' => 'Email', 'URL' => 'URL', - 'BaseClass' => 'Base Class', 'Moderated' => 'Moderated?', 'IsSpam' => 'Spam?', - 'ParentID' => 'Parent ID', 'AllowHtml' => 'Allow Html', 'SecretToken' => 'Secret Token', 'Depth' => 'Depth', @@ -648,8 +672,8 @@ class CommentsTest extends FunctionalTest 'ParentComment' => 'Parent Comment', 'ChildComments' => 'Child Comments', 'ParentTitle' => 'Parent', - 'Created' => 'Date posted' - + 'Created' => 'Date posted', + 'Parent' => 'Parent' ); $this->assertEquals($expected, $labels); } @@ -661,29 +685,31 @@ class CommentsTest extends FunctionalTest public function testGetParent() { - $comment = $this->objFromFixture('Comment', 'firstComA'); - $item = $this->objFromFixture('CommentableItem', 'first'); - $parent = $comment->getParent(); - $this->assertEquals($item, $parent); + $comment = $this->objFromFixture(Comment::class, 'firstComA'); + $item = $this->objFromFixture(CommentableItem::class, 'first'); + $parent = $comment->Parent(); + $this->assertSame($item->getClassName(), $parent->getClassName()); + $this->assertSame($item->ID, $parent->ID); } public function testGetParentTitle() { - $comment = $this->objFromFixture('Comment', 'firstComA'); + $comment = $this->objFromFixture(Comment::class, 'firstComA'); $title = $comment->getParentTitle(); $this->assertEquals('First', $title); // Title from a comment with no parent is blank $comment->ParentID = 0; + $comment->ParentClass = null; $comment->write(); $this->assertEquals('', $comment->getParentTitle()); } public function testGetParentClassName() { - $comment = $this->objFromFixture('Comment', 'firstComA'); + $comment = $this->objFromFixture(Comment::class, 'firstComA'); $className = $comment->getParentClassName(); - $this->assertEquals('CommentableItem', $className); + $this->assertEquals(CommentableItem::class, $className); } public function testCastingHelper() @@ -708,7 +734,7 @@ class CommentsTest extends FunctionalTest public function testCanCreate() { - $comment = $this->objFromFixture('Comment', 'firstComA'); + $comment = $this->objFromFixture(Comment::class, 'firstComA'); // admin can create - this is always false $this->logInAs('commentadmin'); @@ -721,7 +747,7 @@ class CommentsTest extends FunctionalTest public function testCanView() { - $comment = $this->objFromFixture('Comment', 'firstComA'); + $comment = $this->objFromFixture(Comment::class, 'firstComA'); // admin can view $this->logInAs('commentadmin'); @@ -738,7 +764,7 @@ class CommentsTest extends FunctionalTest public function testCanEdit() { - $comment = $this->objFromFixture('Comment', 'firstComA'); + $comment = $this->objFromFixture(Comment::class, 'firstComA'); // admin can edit $this->logInAs('commentadmin'); @@ -755,7 +781,7 @@ class CommentsTest extends FunctionalTest public function testCanDelete() { - $comment = $this->objFromFixture('Comment', 'firstComA'); + $comment = $this->objFromFixture(Comment::class, 'firstComA'); // admin can delete $this->logInAs('commentadmin'); @@ -774,7 +800,7 @@ class CommentsTest extends FunctionalTest { $this->logInAs('visitor'); $current = Member::currentUser(); - $comment = $this->objFromFixture('Comment', 'firstComA'); + $comment = $this->objFromFixture(Comment::class, 'firstComA'); $method = $this->getMethod('getMember'); // null case @@ -792,7 +818,7 @@ class CommentsTest extends FunctionalTest public function testGetAuthorName() { - $comment = $this->objFromFixture('Comment', 'firstComA'); + $comment = $this->objFromFixture(Comment::class, 'firstComA'); $this->assertEquals( 'FA', $comment->getAuthorName() @@ -804,7 +830,7 @@ class CommentsTest extends FunctionalTest $comment->getAuthorName() ); - $author = $this->objFromFixture('Member', 'visitor'); + $author = $this->objFromFixture(Member::class, 'visitor'); $comment->AuthorID = $author->ID; $comment->write(); $this->assertEquals( @@ -821,44 +847,44 @@ class CommentsTest extends FunctionalTest public function testLinks() { - $comment = $this->objFromFixture('Comment', 'firstComA'); + $comment = $this->objFromFixture(Comment::class, 'firstComA'); $this->logInAs('commentadmin'); $method = $this->getMethod('ActionLink'); // test with starts of strings and tokens and salts change each time $this->assertStringStartsWith( - '/CommentingController/theaction/'.$comment->ID, + '/comments/theaction/' . $comment->ID, $method->invokeArgs($comment, array('theaction')) ); $this->assertStringStartsWith( - '/CommentingController/delete/'.$comment->ID, + '/comments/delete/' . $comment->ID, $comment->DeleteLink() ); $this->assertStringStartsWith( - '/CommentingController/spam/'.$comment->ID, + '/comments/spam/' . $comment->ID, $comment->SpamLink() ); $comment->markSpam(); $this->assertStringStartsWith( - '/CommentingController/ham/'.$comment->ID, + '/comments/ham/' . $comment->ID, $comment->HamLink() ); //markApproved $comment->markUnapproved(); $this->assertStringStartsWith( - '/CommentingController/approve/'.$comment->ID, + '/comments/approve/' . $comment->ID, $comment->ApproveLink() ); } public function testMarkSpam() { - $comment = $this->objFromFixture('Comment', 'firstComA'); + $comment = $this->objFromFixture(Comment::class, 'firstComA'); $comment->markSpam(); $this->assertTrue($comment->Moderated); $this->assertTrue($comment->IsSpam); @@ -866,7 +892,7 @@ class CommentsTest extends FunctionalTest public function testMarkApproved() { - $comment = $this->objFromFixture('Comment', 'firstComA'); + $comment = $this->objFromFixture(Comment::class, 'firstComA'); $comment->markApproved(); $this->assertTrue($comment->Moderated); $this->assertFalse($comment->IsSpam); @@ -874,14 +900,14 @@ class CommentsTest extends FunctionalTest public function testMarkUnapproved() { - $comment = $this->objFromFixture('Comment', 'firstComA'); + $comment = $this->objFromFixture(Comment::class, 'firstComA'); $comment->markApproved(); $this->assertTrue($comment->Moderated); } public function testSpamClass() { - $comment = $this->objFromFixture('Comment', 'firstComA'); + $comment = $this->objFromFixture(Comment::class, 'firstComA'); $this->assertEquals('notspam', $comment->spamClass()); $comment->Moderated = false; $this->assertEquals('unmoderated', $comment->spamClass()); @@ -891,7 +917,7 @@ class CommentsTest extends FunctionalTest public function testGetTitle() { - $comment = $this->objFromFixture('Comment', 'firstComA'); + $comment = $this->objFromFixture(Comment::class, 'firstComA'); $this->assertEquals( 'Comment by FA on First', $comment->getTitle() @@ -900,7 +926,7 @@ class CommentsTest extends FunctionalTest public function testGetCMSFields() { - $comment = $this->objFromFixture('Comment', 'firstComA'); + $comment = $this->objFromFixture(Comment::class, 'firstComA'); $fields = $comment->getCMSFields(); $names = array(); foreach ($fields as $field) { @@ -912,7 +938,7 @@ class CommentsTest extends FunctionalTest 'Comment', 'Email', 'URL', - null #FIXME this is suspicious + 'Options' ); $this->assertEquals($expected, $names); } @@ -920,7 +946,7 @@ class CommentsTest extends FunctionalTest public function testGetCMSFieldsCommentHasAuthor() { $member = Member::get()->filter('FirstName', 'visitor')->first(); - $comment = $this->objFromFixture('Comment', 'firstComA'); + $comment = $this->objFromFixture(Comment::class, 'firstComA'); $comment->AuthorID = $member->ID; $comment->write(); @@ -936,14 +962,14 @@ class CommentsTest extends FunctionalTest 'Comment', 'Email', 'URL', - null #FIXME this is suspicious + 'Options' ); $this->assertEquals($expected, $names); } public function testGetCMSFieldsWithParentComment() { - $comment = $this->objFromFixture('Comment', 'firstComA'); + $comment = $this->objFromFixture(Comment::class, 'firstComA'); $child = new Comment(); $child->Name = 'John Smith'; @@ -962,7 +988,7 @@ class CommentsTest extends FunctionalTest 'Comment', 'Email', 'URL', - null, #FIXME this is suspicious + 'Options', 'ParentComment_Title', 'ParentComment_Created', 'ParentComment_AuthorName', @@ -971,10 +997,9 @@ class CommentsTest extends FunctionalTest $this->assertEquals($expected, $names); } - public function testPurifyHtml() { - $comment = $this->objFromFixture('Comment', 'firstComA'); + $comment = $this->objFromFixture(Comment::class, 'firstComA'); $dirtyHTML = '

my comment

'; $this->assertEquals( @@ -986,22 +1011,22 @@ class CommentsTest extends FunctionalTest public function testGravatar() { // Turn gravatars on - Config::inst()->update('CommentableItem', 'comments', array( + Config::inst()->update(CommentableItem::class, 'comments', array( 'use_gravatar' => true )); - $comment = $this->objFromFixture('Comment', 'firstComA'); + $comment = $this->objFromFixture(Comment::class, 'firstComA'); $this->assertEquals( - 'http://www.gravatar.com/avatar/d41d8cd98f00b204e9800998ecf8427e?s'. - '=80&d=identicon&r=g', + 'http://www.gravatar.com/avatar/d41d8cd98f00b204e9800998ecf8427e?s' + . '=80&d=identicon&r=g', $comment->gravatar() ); // Turn gravatars off - Config::inst()->update('CommentableItem', 'comments', array( + Config::inst()->update(CommentableItem::class, 'comments', array( 'use_gravatar' => false )); - $comment = $this->objFromFixture('Comment', 'firstComA'); + $comment = $this->objFromFixture(Comment::class, 'firstComA'); $this->assertEquals( '', @@ -1011,13 +1036,13 @@ class CommentsTest extends FunctionalTest public function testGetRepliesEnabled() { - $comment = $this->objFromFixture('Comment', 'firstComA'); - Config::inst()->update('CommentableItem', 'comments', array( + $comment = $this->objFromFixture(Comment::class, 'firstComA'); + Config::inst()->update(CommentableItem::class, 'comments', array( 'nested_comments' => false )); $this->assertFalse($comment->getRepliesEnabled()); - Config::inst()->update('CommentableItem', 'comments', array( + Config::inst()->update(CommentableItem::class, 'comments', array( 'nested_comments' => true, 'nested_depth' => 4 )); @@ -1028,7 +1053,7 @@ class CommentsTest extends FunctionalTest // 0 indicates no limit for nested_depth - Config::inst()->update('CommentableItem', 'comments', array( + Config::inst()->update(CommentableItem::class, 'comments', array( 'nested_comments' => true, 'nested_depth' => 0 )); @@ -1046,11 +1071,11 @@ class CommentsTest extends FunctionalTest public function testAllReplies() { - Config::inst()->update('CommentableItem', 'comments', array( + Config::inst()->update(CommentableItem::class, 'comments', array( 'nested_comments' => true, 'nested_depth' => 4 )); - $comment = $this->objFromFixture('Comment', 'firstComA'); + $comment = $this->objFromFixture(Comment::class, 'firstComA'); $this->assertEquals( 3, $comment->allReplies()->count() @@ -1069,7 +1094,7 @@ class CommentsTest extends FunctionalTest $comment->allReplies()->count() ); - Config::inst()->update('CommentableItem', 'comments', array( + Config::inst()->update(CommentableItem::class, 'comments', array( 'nested_comments' => false )); @@ -1078,13 +1103,13 @@ class CommentsTest extends FunctionalTest public function testReplies() { - CommentableItem::add_extension('CommentsExtension'); + CommentableItem::add_extension(CommentsExtension::class); $this->logInWithPermission('ADMIN'); - Config::inst()->update('CommentableItem', 'comments', array( + Config::inst()->update(CommentableItem::class, 'comments', array( 'nested_comments' => true, 'nested_depth' => 4 )); - $comment = $this->objFromFixture('Comment', 'firstComA'); + $comment = $this->objFromFixture(Comment::class, 'firstComA'); $this->assertEquals( 3, $comment->Replies()->count() @@ -1114,17 +1139,17 @@ class CommentsTest extends FunctionalTest // Test moderation required on the front end - $item = $this->objFromFixture('CommentableItem', 'first'); + $item = $this->objFromFixture(CommentableItem::class, 'first'); $item->ModerationRequired = 'Required'; $item->write(); - Config::inst()->update('CommentableItemDisabled', 'comments', array( + Config::inst()->update(CommentableItemDisabled::class, 'comments', array( 'nested_comments' => true, 'nested_depth' => 4, 'frontend_moderation' => true )); - $comment = DataObject::get_by_id('Comment', $comment->ID); + $comment = DataObject::get_by_id(Comment::class, $comment->ID); $this->assertEquals( 2, @@ -1132,7 +1157,7 @@ class CommentsTest extends FunctionalTest ); // Turn off nesting, empty array should be returned - Config::inst()->update('CommentableItem', 'comments', array( + Config::inst()->update(CommentableItem::class, 'comments', array( 'nested_comments' => false )); @@ -1141,18 +1166,18 @@ class CommentsTest extends FunctionalTest $comment->Replies()->count() ); - CommentableItem::remove_extension('CommentsExtension'); + CommentableItem::remove_extension(CommentsExtension::class); } public function testPagedReplies() { - Config::inst()->update('CommentableItem', 'comments', array( + Config::inst()->update(CommentableItem::class, 'comments', array( 'nested_comments' => true, 'nested_depth' => 4, 'comments_per_page' => 2 #Force 2nd page for 3 items )); - $comment = $this->objFromFixture('Comment', 'firstComA'); + $comment = $this->objFromFixture(Comment::class, 'firstComA'); $pagedList = $comment->pagedReplies(); $this->assertEquals( 2, @@ -1164,7 +1189,7 @@ class CommentsTest extends FunctionalTest ); //TODO - 2nd page requires controller // - Config::inst()->update('CommentableItem', 'comments', array( + Config::inst()->update(CommentableItem::class, 'comments', array( 'nested_comments' => false )); @@ -1173,19 +1198,19 @@ class CommentsTest extends FunctionalTest public function testReplyForm() { - Config::inst()->update('CommentableItem', 'comments', array( + Config::inst()->update(CommentableItem::class, 'comments', array( 'nested_comments' => false, 'nested_depth' => 4 )); - $comment = $this->objFromFixture('Comment', 'firstComA'); + $comment = $this->objFromFixture(Comment::class, 'firstComA'); // No nesting, no reply form $form = $comment->replyForm(); $this->assertNull($form); // parent item so show form - Config::inst()->update('CommentableItem', 'comments', array( + Config::inst()->update(CommentableItem::class, 'comments', array( 'nested_comments' => true, 'nested_depth' => 4 )); @@ -1198,11 +1223,11 @@ class CommentsTest extends FunctionalTest $this->assertEquals( array( - null, #FIXME suspicious + 'NameEmailURLComment', // The CompositeField name? 'ParentID', + 'ParentClassName', 'ReturnURL', - 'ParentCommentID', - 'BaseClass' + 'ParentCommentID' ), $names ); @@ -1210,6 +1235,7 @@ class CommentsTest extends FunctionalTest // no parent, no reply form $comment->ParentID = 0; + $comment->ParentClass = null; $comment->write(); $form = $comment->replyForm(); $this->assertNull($form); @@ -1217,12 +1243,12 @@ class CommentsTest extends FunctionalTest public function testUpdateDepth() { - Config::inst()->update('CommentableItem', 'comments', array( + Config::inst()->update(CommentableItem::class, 'comments', array( 'nested_comments' => true, 'nested_depth' => 4 )); - $comment = $this->objFromFixture('Comment', 'firstComA'); + $comment = $this->objFromFixture(Comment::class, 'firstComA'); $children = $comment->allReplies()->toArray(); // Make the second child a child of the first // Make the third child a child of the second @@ -1262,98 +1288,11 @@ class CommentsTest extends FunctionalTest $this->markTestSkipped('TODO'); } - protected static function getMethod($name) { - $class = new ReflectionClass('Comment'); + $class = new ReflectionClass(Comment::class); $method = $class->getMethod($name); $method->setAccessible(true); return $method; } } - - -/** - * @package comments - * @subpackage tests - */ -class CommentableItem extends DataObject implements TestOnly -{ - - private static $db = array( - 'Title' => 'Varchar' - ); - - private static $extensions = array( - 'CommentsExtension' - ); - - public function RelativeLink() - { - return "CommentableItem_Controller"; - } - - public function canView($member = null) - { - return true; - } - - // This is needed for canModerateComments - public function canEdit($member = null) - { - if ($member instanceof Member) { - $memberID = $member->ID; - } elseif (is_numeric($member)) { - $memberID = $member; - } else { - $memberID = Member::currentUserID(); - } - - if ($memberID && Permission::checkMember($memberID, array("ADMIN", "CMS_ACCESS_CommentAdmin"))) { - return true; - } - return false; - } - - public function Link() - { - return $this->RelativeLink(); - } - - public function AbsoluteLink() - { - return Director::absoluteURL($this->RelativeLink()); - } -} - -class CommentableItemEnabled extends CommentableItem -{ - private static $defaults = array( - 'ProvideComments' => true, - 'ModerationRequired' => 'Required', - 'CommentsRequireLogin' => true - ); -} - - -class CommentableItemDisabled extends CommentableItem -{ - private static $defaults = array( - 'ProvideComments' => false, - 'ModerationRequired' => 'None', - 'CommentsRequireLogin' => false - ); -} - -/** - * @package comments - * @subpackage tests - */ -class CommentableItem_Controller extends Controller implements TestOnly -{ - - public function index() - { - return CommentableItem::get()->first()->CommentsForm(); - } -} diff --git a/tests/CommentsTest.yml b/tests/CommentsTest.yml index 7f5ee3e..a25a3c9 100644 --- a/tests/CommentsTest.yml +++ b/tests/CommentsTest.yml @@ -1,4 +1,4 @@ -Member: +SilverStripe\Security\Member: commentadmin: FirstName: admin commentadmin2: @@ -6,17 +6,17 @@ Member: visitor: FirstName: visitor -Group: +SilverStripe\Security\Group: commentadmins: Title: Admin - Members: =>Member.commentadmin, =>Member.commentadmin2 + Members: =>SilverStripe\Security\Member.commentadmin, =>SilverStripe\Security\Member.commentadmin2 -Permission: +SilverStripe\Security\Permission: admin: Code: CMS_ACCESS_CommentAdmin - Group: =>Group.commentadmins + Group: =>SilverStripe\Security\Group.commentadmins -CommentableItem: +SilverStripe\Comments\Tests\Stubs\CommentableItem: first: Title: First ProvideComments: 1 @@ -35,166 +35,158 @@ CommentableItem: ProvideComments: 1 Title: spammed ModerationRequired: 'None' + moderated: + ProvideComments: 1 + Title: Moderate everything + ModerationRequired: Required + CommentsRequireLogin: 0 -Comment: +SilverStripe\Comments\Model\Comment: firstComA: - ParentID: =>CommentableItem.first + Parent: =>SilverStripe\Comments\Tests\Stubs\CommentableItem.first Name: FA Comment: textFA - BaseClass: CommentableItem Moderated: 1 IsSpam: Depth: 1 firstComAChild1: - ParentID: =>CommentableItem.first - ParentCommentID: =>Comment.firstComA + Parent: =>SilverStripe\Comments\Tests\Stubs\CommentableItem.first + ParentComment: =>SilverStripe\Comments\Model\Comment.firstComA Name: John Smith Comment: Reply to firstComA 1 - BaseClass: CommentableItem Moderated: 1 IsSpam: 0 Depth: 2 firstComAChild2: - ParentID: =>CommentableItem.first - ParentCommentID: =>Comment.firstComA + Parent: =>SilverStripe\Comments\Tests\Stubs\CommentableItem.first + ParentComment: =>SilverStripe\Comments\Model\Comment.firstComA Name: John Smith Comment: Reply to firstComA 2 - BaseClass: CommentableItem Moderated: 1 IsSpam: 0 Depth: 2 firstComAChild3: - ParentID: =>CommentableItem.first - ParentCommentID: =>Comment.firstComA + Parent: =>SilverStripe\Comments\Tests\Stubs\CommentableItem.first + ParentComment: =>SilverStripe\Comments\Model\Comment.firstComA Name: John Smith Comment: Reply to firstComA 3 - BaseClass: CommentableItem Moderated: 1 IsSpam: 0 Depth: 2 secondComA: - ParentID: =>CommentableItem.second + Parent: =>SilverStripe\Comments\Tests\Stubs\CommentableItem.second Name: SA Comment: textSA Moderated: 1 IsSpam: 0 - BaseClass: CommentableItem secondComB: - ParentID: =>CommentableItem.second + Parent: =>SilverStripe\Comments\Tests\Stubs\CommentableItem.second Name: SB Comment: textSB Moderated: 0 IsSpam: 0 - BaseClass: CommentableItem secondComC: - ParentID: =>CommentableItem.second + Parent: =>SilverStripe\Comments\Tests\Stubs\CommentableItem.second Name: SB Comment: textSB Moderated: 1 IsSpam: 1 - BaseClass: CommentableItem thirdComA: - ParentID: =>CommentableItem.third + Parent: =>SilverStripe\Comments\Tests\Stubs\CommentableItem.third Name: TA Comment: textTA Moderated: 1 IsSpam: 0 - BaseClass: CommentableItem thirdComB: - ParentID: =>CommentableItem.third + Parent: =>SilverStripe\Comments\Tests\Stubs\CommentableItem.third Name: TB Comment: textTB Moderated: 1 IsSpam: 0 - BaseClass: CommentableItem thirdComC: - ParentID: =>CommentableItem.third + Parent: =>SilverStripe\Comments\Tests\Stubs\CommentableItem.third Name: TC Comment: textTC Moderated: 1 IsSpam: 0 - BaseClass: CommentableItem thirdComD: - ParentID: =>CommentableItem.third + Parent: =>SilverStripe\Comments\Tests\Stubs\CommentableItem.third Name: TC Comment: textTC Moderated: 1 - BaseClass: CommentableItem thirdComE: - ParentID: =>CommentableItem.third + Parent: =>SilverStripe\Comments\Tests\Stubs\CommentableItem.third Name: TC Comment: textTC Moderated: 1 - BaseClass: CommentableItem thirdComF: - ParentID: =>CommentableItem.third + Parent: =>SilverStripe\Comments\Tests\Stubs\CommentableItem.third Name: TC Comment: textTC Moderated: 1 IsSpam: 0 - BaseClass: CommentableItem thirdComG: - ParentID: =>CommentableItem.third + Parent: =>SilverStripe\Comments\Tests\Stubs\CommentableItem.third Name: TC Comment: textTC Moderated: 1 IsSpam: 0 - BaseClass: CommentableItem thirdComH: - ParentID: =>CommentableItem.third + Parent: =>SilverStripe\Comments\Tests\Stubs\CommentableItem.third Name: TC Comment: textTC Moderated: 1 IsSpam: 0 - BaseClass: CommentableItem thirdComI: - ParentID: =>CommentableItem.third + Parent: =>SilverStripe\Comments\Tests\Stubs\CommentableItem.third Name: TC Comment: textTC Moderated: 1 IsSpam: 0 - BaseClass: CommentableItem thirdComJ: - ParentID: =>CommentableItem.third + Parent: =>SilverStripe\Comments\Tests\Stubs\CommentableItem.third Name: TC Comment: textTC Moderated: 1 IsSpam: 0 - BaseClass: CommentableItem thirdComK: - ParentID: =>CommentableItem.third + Parent: =>SilverStripe\Comments\Tests\Stubs\CommentableItem.third Name: TC Comment: textTC Moderated: 1 IsSpam: 0 - BaseClass: CommentableItem disabledCom: - ParentID: =>CommentableItem.nocomments + Parent: =>SilverStripe\Comments\Tests\Stubs\CommentableItem.nocomments Name: Disabled Moderated: 0 IsSpam: 1 - BaseClass: CommentableItem testCommentList1: - ParentID: =>CommentableItem.spammed + Parent: =>SilverStripe\Comments\Tests\Stubs\CommentableItem.spammed Name: Comment 1 Moderated: 0 IsSpam: 0 - BaseClass: CommentableItem testCommentList2: - ParentID: =>CommentableItem.spammed + Parent: =>SilverStripe\Comments\Tests\Stubs\CommentableItem.spammed Name: Comment 2 Moderated: 1 IsSpam: 1 - BaseClass: CommentableItem testCommentList3: - ParentID: =>CommentableItem.spammed + Parent: =>SilverStripe\Comments\Tests\Stubs\CommentableItem.spammed Name: Comment 3 Moderated: 1 IsSpam: 0 - BaseClass: CommentableItem testCommentList4: - ParentID: =>CommentableItem.spammed + Parent: =>SilverStripe\Comments\Tests\Stubs\CommentableItem.spammed Name: Comment 4 Moderated: 0 IsSpam: 1 - BaseClass: CommentableItem + testModeratedComment1: + Parent: =>SilverStripe\Comments\Tests\Stubs\CommentableItem.moderated + Name: Moderated comment 1 + Moderated: 0 + IsSpam: 0 + testModeratedComment2: + Parent: =>SilverStripe\Comments\Tests\Stubs\CommentableItem.moderated + Name: Moderated comment 2 + Moderated: 0 + IsSpam: 1 diff --git a/tests/Stubs/CommentableItem.php b/tests/Stubs/CommentableItem.php new file mode 100644 index 0000000..79cdaac --- /dev/null +++ b/tests/Stubs/CommentableItem.php @@ -0,0 +1,62 @@ + 'Varchar' + ); + + private static $extensions = array( + CommentsExtension::class + ); + + public function RelativeLink() + { + return 'CommentableItemController'; + } + + public function canView($member = null) + { + return true; + } + + // This is needed for canModerateComments + public function canEdit($member = null) + { + if ($member instanceof Member) { + $memberID = $member->ID; + } elseif (is_numeric($member)) { + $memberID = $member; + } else { + $memberID = Member::currentUserID(); + } + + if ($memberID && Permission::checkMember($memberID, array('ADMIN', 'CMS_ACCESS_CommentAdmin'))) { + return true; + } + return false; + } + + public function Link() + { + return $this->RelativeLink(); + } + + public function AbsoluteLink() + { + return Director::absoluteURL($this->RelativeLink()); + } +} diff --git a/tests/Stubs/CommentableItemController.php b/tests/Stubs/CommentableItemController.php new file mode 100644 index 0000000..211e20a --- /dev/null +++ b/tests/Stubs/CommentableItemController.php @@ -0,0 +1,19 @@ +first()->CommentsForm(); + } +} diff --git a/tests/Stubs/CommentableItemDisabled.php b/tests/Stubs/CommentableItemDisabled.php new file mode 100644 index 0000000..c5c4402 --- /dev/null +++ b/tests/Stubs/CommentableItemDisabled.php @@ -0,0 +1,14 @@ + false, + 'ModerationRequired' => 'None', + 'CommentsRequireLogin' => false + ); +} diff --git a/tests/Stubs/CommentableItemEnabled.php b/tests/Stubs/CommentableItemEnabled.php new file mode 100644 index 0000000..5489827 --- /dev/null +++ b/tests/Stubs/CommentableItemEnabled.php @@ -0,0 +1,14 @@ + true, + 'ModerationRequired' => 'Required', + 'CommentsRequireLogin' => true + ); +}