From d9333b3160a8b05fa5cb96b55e2ec9d82a965f6a Mon Sep 17 00:00:00 2001 From: Will Rossiter Date: Thu, 14 Sep 2017 11:12:07 +1200 Subject: [PATCH] Test fixes --- _config/comments.yml | 2 + src/Controllers/CommentingController.php | 4 + src/Extensions/CommentsExtension.php | 3 +- src/Model/Comment.php | 11 ++- tests/CommentsExtensionTest.php | 94 ++++++++++++++++++------ 5 files changed, 90 insertions(+), 24 deletions(-) diff --git a/_config/comments.yml b/_config/comments.yml index 8a09bf0..c6c1b52 100644 --- a/_config/comments.yml +++ b/_config/comments.yml @@ -5,5 +5,7 @@ only: moduleexists: 'cms' --- SilverStripe\CMS\Model\SiteTree: + extensions: + - SilverStripe\Comments\Extensions\CommentsExtension comments: enabled_cms: true diff --git a/src/Controllers/CommentingController.php b/src/Controllers/CommentingController.php index a3af14a..6af2259 100644 --- a/src/Controllers/CommentingController.php +++ b/src/Controllers/CommentingController.php @@ -558,6 +558,10 @@ class CommentingController extends Controller 'ReturnURL' => $this->request->getURL(), 'ParentClassName' => $this->getParentClass() )); + + if ($holder = $record->getCommentHolderID()) { + $form->setHTMLID($holder); + } } // Set it so the user gets redirected back down to the form upon form fail diff --git a/src/Extensions/CommentsExtension.php b/src/Extensions/CommentsExtension.php index 8463efe..be3fc07 100644 --- a/src/Extensions/CommentsExtension.php +++ b/src/Extensions/CommentsExtension.php @@ -258,6 +258,7 @@ class CommentsExtension extends DataExtension // Filter spam comments for non-administrators if configured $showSpam = $this->owner->getCommentsOption('frontend_spam') && $this->owner->canModerateComments(); + if (!$showSpam) { $list = $list->filter('IsSpam', 0); } @@ -363,7 +364,7 @@ class CommentsExtension extends DataExtension if (!$this->owner->CommentsEnabled) { return false; } - + if (!$this->owner->canView($member)) { // deny if current user cannot view the underlying record. return false; diff --git a/src/Model/Comment.php b/src/Model/Comment.php index 4afe4fa..79ca1d2 100755 --- a/src/Model/Comment.php +++ b/src/Model/Comment.php @@ -718,8 +718,11 @@ class Comment extends DataObject */ public function purifyHtml($dirtyHtml) { - $purifier = $this->getHtmlPurifierService(); - return $purifier->purify($dirtyHtml); + if ($service = $this->getHtmlPurifierService()) { + return $service->purify($dirtyHtml); + } + + return $dirtyHtml; } /** @@ -727,6 +730,10 @@ class Comment extends DataObject */ public function getHtmlPurifierService() { + if (!class_exists(HTMLPurifier_Config::class)) { + return null; + } + $config = HTMLPurifier_Config::createDefault(); $allowedElements = (array) $this->getOption('html_allowed_elements'); if (!empty($allowedElements)) { diff --git a/tests/CommentsExtensionTest.php b/tests/CommentsExtensionTest.php index 72eda31..843b2e2 100644 --- a/tests/CommentsExtensionTest.php +++ b/tests/CommentsExtensionTest.php @@ -66,7 +66,28 @@ class CommentsExtensionTest extends SapphireTest public function testPopulateDefaults() { - $this->markTestSkipped('TODO'); + Config::inst()->update(CommentableItem::class, 'comments', array( + 'require_moderation_cms' => true, + 'require_moderation' => true, + 'require_moderation_nonmembers' => true + )); + + $item = $this->objFromFixture(CommentableItem::class, 'first'); + $item->populateDefaults(); + + $this->assertTrue($item->CommentsRequireLogin); + + Config::inst()->update(CommentableItem::class, 'comments', array( + 'require_moderation_cms' => true, + 'require_moderation' => true, + 'require_moderation_nonmembers' => true + )); + + $item = $this->objFromFixture(CommentableItem::class, 'first'); + $item->populateDefaults(); + + $this->assertFalse($item->CommentsRequireLogin); + } public function testUpdateSettingsFields() @@ -88,10 +109,17 @@ class CommentsExtensionTest extends SapphireTest // 'ModerationRequired' is returned $item = $this->objFromFixture(CommentableItem::class, 'first'); $item->ModerationRequired = 'None'; + $item->write(); + $this->assertEquals('None', $item->getModerationRequired()); $item->ModerationRequired = 'Required'; + $item->write(); + $this->assertEquals('Required', $item->getModerationRequired()); + $item->ModerationRequired = 'NonMembersOnly'; + $item->write(); + $this->assertEquals('NonMembersOnly', $item->getModerationRequired()); Config::inst()->update(CommentableItem::class, 'comments', array( @@ -144,29 +172,54 @@ class CommentsExtensionTest extends SapphireTest public function testAllComments() { - $this->markTestSkipped('TODO'); + $item = $this->objFromFixture(CommentableItem::class, 'first'); + $this->assertEquals(4, $items->AllComments()->count()); } public function testAllVisibleComments() { - $this->markTestSkipped('TODO'); + if (Member::currentUser()) { + Member::currentUser()->logOut(); + } + + $item = $this->objFromFixture(CommentableItem::class, 'second'); + $this->assertEquals(4, $items->AllVisibleComments()->count()); } public function testComments() { - $this->markTestSkipped('TODO'); + Config::inst()->update(CommentableItem::class, 'comments', array( + 'nested_comments' => false + )); + + $item = $this->objFromFixture(CommentableItem::class, 'first'); + $this->assertEquals(4, $items->Comments()->count()); + + Config::inst()->update(CommentableItem::class, 'comments', array( + 'nested_comments' => true + )); + + $this->assertEquals(1, $items->Comments()->count()); } public function testGetCommentsEnabled() { - $this->markTestSkipped('TODO'); + Config::inst()->update(CommentableItem::class, 'comments', array( + 'enabled_cms' => true + )); + + $item = $this->objFromFixture(CommentableItem::class, 'first'); + $this->assertTrue($item->getCommentsEnabled()); + + $item->ProvideComments = 0; + $this->assertFalse($item->getCommentsEnabled()); } public function testGetCommentHolderID() { $item = $this->objFromFixture(CommentableItem::class, 'first'); Config::inst()->update(CommentableItem::class, 'comments', array( - 'comments_holder_id' => 'commentid_test1', + 'comments_holder_id' => 'comments-holder', )); $this->assertEquals('commentid_test1', $item->getCommentHolderID()); @@ -219,40 +272,39 @@ class CommentsExtensionTest extends SapphireTest public function testCommentsForm() { - Config::inst()->update( - CommentableItem::class, - 'comments', - array( - 'include_js' => false - ) - ); + Config::inst()->update(CommentableItem::class, 'comments', array( + 'include_js' => false, + 'comments_holder_id' => 'comments-holder', + )); + $item = $this->objFromFixture(CommentableItem::class, 'first'); // The comments form is HTML to do assertions by contains $cf = $item->CommentsForm(); - $expected = '
'; + $this->assertContains($expected, $cf); $this->assertContains('

Post your comment

', $cf); // check the comments form exists - $expected = 'assertContains($expected, $cf); - $expected = 'assertContains($expected, $cf); - $expected = 'assertContains($expected, $cf); - $expected = ''; + $expected = ''; $this->assertContains($expected, $cf); - $expected = '