Test fixes

This commit is contained in:
Will Rossiter 2017-09-14 11:12:07 +12:00
parent 7fff17b317
commit d9333b3160
5 changed files with 90 additions and 24 deletions

View File

@ -5,5 +5,7 @@ only:
moduleexists: 'cms' moduleexists: 'cms'
--- ---
SilverStripe\CMS\Model\SiteTree: SilverStripe\CMS\Model\SiteTree:
extensions:
- SilverStripe\Comments\Extensions\CommentsExtension
comments: comments:
enabled_cms: true enabled_cms: true

View File

@ -558,6 +558,10 @@ class CommentingController extends Controller
'ReturnURL' => $this->request->getURL(), 'ReturnURL' => $this->request->getURL(),
'ParentClassName' => $this->getParentClass() '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 // Set it so the user gets redirected back down to the form upon form fail

View File

@ -258,6 +258,7 @@ class CommentsExtension extends DataExtension
// Filter spam comments for non-administrators if configured // Filter spam comments for non-administrators if configured
$showSpam = $this->owner->getCommentsOption('frontend_spam') && $this->owner->canModerateComments(); $showSpam = $this->owner->getCommentsOption('frontend_spam') && $this->owner->canModerateComments();
if (!$showSpam) { if (!$showSpam) {
$list = $list->filter('IsSpam', 0); $list = $list->filter('IsSpam', 0);
} }
@ -363,7 +364,7 @@ class CommentsExtension extends DataExtension
if (!$this->owner->CommentsEnabled) { if (!$this->owner->CommentsEnabled) {
return false; return false;
} }
if (!$this->owner->canView($member)) { if (!$this->owner->canView($member)) {
// deny if current user cannot view the underlying record. // deny if current user cannot view the underlying record.
return false; return false;

View File

@ -718,8 +718,11 @@ class Comment extends DataObject
*/ */
public function purifyHtml($dirtyHtml) public function purifyHtml($dirtyHtml)
{ {
$purifier = $this->getHtmlPurifierService(); if ($service = $this->getHtmlPurifierService()) {
return $purifier->purify($dirtyHtml); return $service->purify($dirtyHtml);
}
return $dirtyHtml;
} }
/** /**
@ -727,6 +730,10 @@ class Comment extends DataObject
*/ */
public function getHtmlPurifierService() public function getHtmlPurifierService()
{ {
if (!class_exists(HTMLPurifier_Config::class)) {
return null;
}
$config = HTMLPurifier_Config::createDefault(); $config = HTMLPurifier_Config::createDefault();
$allowedElements = (array) $this->getOption('html_allowed_elements'); $allowedElements = (array) $this->getOption('html_allowed_elements');
if (!empty($allowedElements)) { if (!empty($allowedElements)) {

View File

@ -66,7 +66,28 @@ class CommentsExtensionTest extends SapphireTest
public function testPopulateDefaults() 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() public function testUpdateSettingsFields()
@ -88,10 +109,17 @@ class CommentsExtensionTest extends SapphireTest
// 'ModerationRequired' is returned // 'ModerationRequired' is returned
$item = $this->objFromFixture(CommentableItem::class, 'first'); $item = $this->objFromFixture(CommentableItem::class, 'first');
$item->ModerationRequired = 'None'; $item->ModerationRequired = 'None';
$item->write();
$this->assertEquals('None', $item->getModerationRequired()); $this->assertEquals('None', $item->getModerationRequired());
$item->ModerationRequired = 'Required'; $item->ModerationRequired = 'Required';
$item->write();
$this->assertEquals('Required', $item->getModerationRequired()); $this->assertEquals('Required', $item->getModerationRequired());
$item->ModerationRequired = 'NonMembersOnly'; $item->ModerationRequired = 'NonMembersOnly';
$item->write();
$this->assertEquals('NonMembersOnly', $item->getModerationRequired()); $this->assertEquals('NonMembersOnly', $item->getModerationRequired());
Config::inst()->update(CommentableItem::class, 'comments', array( Config::inst()->update(CommentableItem::class, 'comments', array(
@ -144,29 +172,54 @@ class CommentsExtensionTest extends SapphireTest
public function testAllComments() public function testAllComments()
{ {
$this->markTestSkipped('TODO'); $item = $this->objFromFixture(CommentableItem::class, 'first');
$this->assertEquals(4, $items->AllComments()->count());
} }
public function testAllVisibleComments() 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() 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() 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() public function testGetCommentHolderID()
{ {
$item = $this->objFromFixture(CommentableItem::class, 'first'); $item = $this->objFromFixture(CommentableItem::class, 'first');
Config::inst()->update(CommentableItem::class, 'comments', array( Config::inst()->update(CommentableItem::class, 'comments', array(
'comments_holder_id' => 'commentid_test1', 'comments_holder_id' => 'comments-holder',
)); ));
$this->assertEquals('commentid_test1', $item->getCommentHolderID()); $this->assertEquals('commentid_test1', $item->getCommentHolderID());
@ -219,40 +272,39 @@ class CommentsExtensionTest extends SapphireTest
public function testCommentsForm() public function testCommentsForm()
{ {
Config::inst()->update( Config::inst()->update(CommentableItem::class, 'comments', array(
CommentableItem::class, 'include_js' => false,
'comments', 'comments_holder_id' => 'comments-holder',
array( ));
'include_js' => false
)
);
$item = $this->objFromFixture(CommentableItem::class, 'first'); $item = $this->objFromFixture(CommentableItem::class, 'first');
// The comments form is HTML to do assertions by contains // The comments form is HTML to do assertions by contains
$cf = $item->CommentsForm(); $cf = $item->CommentsForm();
$expected = '<form id="Form_CommentsForm" action="/comments' $expected = '<form id="comments-holder" action="/comments'
. '/CommentsForm" method="post" enctype="application/x-www-form-urlenco' . '/CommentsForm/" method="post" enctype="application/x-www-form-urlenco'
. 'ded">'; . 'ded">';
$this->assertContains($expected, $cf); $this->assertContains($expected, $cf);
$this->assertContains('<h4>Post your comment</h4>', $cf); $this->assertContains('<h4>Post your comment</h4>', $cf);
// check the comments form exists // check the comments form exists
$expected = '<input type="text" name="Name" value="ADMIN User" class="text" id="Form_CommentsForm_Name" required="required"'; $expected = '<input type="text" name="Name" value="ADMIN User" class="text" id="comments-holder_Name" required="required"';
$this->assertContains($expected, $cf); $this->assertContains($expected, $cf);
$expected = '<input type="email" name="Email" value="ADMIN@example.org" class="email text" id="Form_CommentsForm_Email"'; $expected = '<input type="email" name="Email" value="ADMIN@example.org" class="email text" id="comments-holder_Email"';
$this->assertContains($expected, $cf); $this->assertContains($expected, $cf);
$expected = '<input type="text" name="URL" class="text" id="Form_CommentsForm_URL" data-msg-url="Please enter a valid URL"'; $expected = '<input type="text" name="URL" class="text" id="comments-holder_URL" data-msg-url="Please enter a valid URL"';
$this->assertContains($expected, $cf); $this->assertContains($expected, $cf);
$expected = '<input type="hidden" name="ParentID" value="' . $item->ID . '" class="hidden" id="Form_CommentsForm_ParentID" />'; $expected = '<input type="hidden" name="ParentID" value="' . $item->ID . '" class="hidden" id="comments-holder_ParentID" />';
$this->assertContains($expected, $cf); $this->assertContains($expected, $cf);
$expected = '<textarea name="Comment" class="textarea" id="Form_CommentsForm_Comment" required="required"'; $expected = '<textarea name="Comment" class="textarea" id="comments-holder_Comment" required="required"';
$this->assertContains($expected, $cf); $this->assertContains($expected, $cf);
$expected = '<input type="submit" name="action_doPostComment" value="Post" class="action" id="Form_CommentsForm_action_doPostComment"'; $expected = '<input type="submit" name="action_doPostComment" value="Post" class="action" id="comments-holder_action_doPostComment"';
$this->assertContains($expected, $cf); $this->assertContains($expected, $cf);
$expected = '<a href="/comments/spam/'; $expected = '<a href="/comments/spam/';