From eba88fe8693c4240cf5abfbda628156807dee95c Mon Sep 17 00:00:00 2001 From: Gordon Anderson Date: Thu, 7 Jan 2016 13:59:10 +0700 Subject: [PATCH 1/3] ENHANCEMENT: Increase test coverage from 54% to 92% --- .travis.yml | 44 +- README.md | 21 +- tests/CommentAdminTest.php | 57 ++ tests/CommentListTest.php | 138 ++++ tests/CommentTestHelper.php | 25 + tests/CommentingControllerTest.php | 168 ++++- tests/CommentingTest.php | 173 +++++ tests/CommentsExtensionTest.php | 407 ++++++++++++ tests/CommentsGridFieldActionTest.php | 133 ++++ tests/CommentsGridFieldBulkActionTest.php | 12 + tests/CommentsGridFieldConfigTest.php | 9 + tests/CommentsGridFieldTest.php | 40 ++ tests/CommentsTest.php | 751 ++++++++++++++++++++-- tests/CommentsTest.yml | 30 +- 14 files changed, 1933 insertions(+), 75 deletions(-) create mode 100644 tests/CommentAdminTest.php create mode 100644 tests/CommentListTest.php create mode 100644 tests/CommentTestHelper.php create mode 100644 tests/CommentingTest.php create mode 100644 tests/CommentsExtensionTest.php create mode 100644 tests/CommentsGridFieldActionTest.php create mode 100644 tests/CommentsGridFieldBulkActionTest.php create mode 100644 tests/CommentsGridFieldConfigTest.php create mode 100644 tests/CommentsGridFieldTest.php diff --git a/.travis.yml b/.travis.yml index 48c7f95..4efe9bd 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,9 +1,12 @@ -# See https://github.com/silverstripe-labs/silverstripe-travis-support for setup details - -sudo: false - language: php +sudo: true + +addons: + apt: + packages: + - tidy + php: - 5.3 - 5.4 @@ -11,11 +14,22 @@ php: - 5.6 - 7.0 +before_install: + - pip install --user codecov + env: - - DB=MYSQL CORE_RELEASE=3.2 + global: + - DB=MYSQL CORE_RELEASE=3.1 + - MODULE_PATH=comments + + # Set to 1 in the matrix to enable code coverage + - COVERAGE=0 matrix: include: + - php: 5.6 + #CommentsListTest breaks with this env: DB=MYSQL CORE_RELEASE=3.2 COVERAGE=1 + env: DB=SQLITE CORE_RELEASE=3.2 COVERAGE=1 - php: 5.6 env: DB=MYSQL CORE_RELEASE=3 - php: 5.6 @@ -26,11 +40,23 @@ matrix: - php: 7.0 before_script: + - phpenv rehash - composer self-update || true - - git clone git://github.com/silverstripe-labs/silverstripe-travis-support.git ~/travis-support - - php ~/travis-support/travis_setup.php --source `pwd` --target ~/builds/ss + - git clone https://github.com/gordonbanderson/silverstripe-travis-support.git --branch pull-24 ~/travis-support +# - 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" - cd ~/builds/ss - - composer install script: - - vendor/bin/phpunit comments/tests + # Execute tests with no coverage. This is the fastest option + - "if [ \"$COVERAGE\" = \"0\" ]; then vendor/bin/phpunit $MODULE_PATH/tests/; fi" + + # Execute tests with coverage. Do this for a small + - "if [ \"$COVERAGE\" = \"1\" ]; then vendor/bin/phpunit --coverage-clover=coverage.clover $MODULE_PATH/tests/; fi" + +after_script: + - "if [ \"$COVERAGE\" = \"1\" ]; then mv coverage.clover ~/build/$TRAVIS_REPO_SLUG/; fi" + - cd ~/build/$TRAVIS_REPO_SLUG + - wget https://scrutinizer-ci.com/ocular.phar + - "if [ \"$COVERAGE\" = \"1\" ]; then travis_retry codecov && travis_retry php ocular.phar code-coverage:upload --format=php-clover coverage.clover; fi" diff --git a/README.md b/README.md index 51f53e4..aa7d1f9 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,21 @@ # Comments +[![Build Status](https://travis-ci.org/gordonbanderson/silverstripe-comments.svg?branch=testing)](https://travis-ci.org/gordonbanderson/silverstripe-comments) +[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/gordonbanderson/silverstripe-comments/badges/quality-score.png?b=testing)](https://scrutinizer-ci.com/g/gordonbanderson/silverstripe-comments/?branch=testing) +[![Code Coverage](https://scrutinizer-ci.com/g/gordonbanderson/silverstripe-comments/badges/coverage.png?b=testing)](https://scrutinizer-ci.com/g/gordonbanderson/silverstripe-comments/?branch=testing) +[![Build Status](https://scrutinizer-ci.com/g/gordonbanderson/silverstripe-comments/badges/build.png?b=testing)](https://scrutinizer-ci.com/g/gordonbanderson/silverstripe-comments/build-status/testing) +[![codecov.io](https://codecov.io/github/gordonbanderson/silverstripe-comments/coverage.svg?branch=testing)](https://codecov.io/github/gordonbanderson/silverstripe-comments?branch=testing) -[![Build Status](https://secure.travis-ci.org/silverstripe/silverstripe-comments.png?branch=master)](http://travis-ci.org/silverstripe/silverstripe-comments) +[![Latest Stable Version](https://poser.pugx.org/silverstripe/comments/version)](https://packagist.org/packages/silverstripe/comments) +[![Latest Unstable Version](https://poser.pugx.org/silverstripe/comments/v/unstable)](//packagist.org/packages/silverstripe/comments) +[![Total Downloads](https://poser.pugx.org/silverstripe/comments/downloads)](https://packagist.org/packages/silverstripe/comments) +[![License](https://poser.pugx.org/silverstripe/comments/license)](https://packagist.org/packages/silverstripe/comments) +[![Monthly Downloads](https://poser.pugx.org/silverstripe/comments/d/monthly)](https://packagist.org/packages/silverstripe/comments) +[![Daily Downloads](https://poser.pugx.org/silverstripe/comments/d/daily)](https://packagist.org/packages/silverstripe/comments) + +[![Dependency Status](https://www.versioneye.com/php/silverstripe:comments/badge.svg)](https://www.versioneye.com/php/silverstripe:comments) +[![Reference Status](https://www.versioneye.com/php/silverstripe:comments/reference_badge.svg?style=flat)](https://www.versioneye.com/php/silverstripe:comments/references) + +![codecov.io](https://codecov.io/github/gordonbanderson/silverstripe-comments/branch.svg?branch=testing) [Changelog](CHANGELOG.md) @@ -12,7 +27,7 @@ ## Introduction This module provides commenting functionality for Pages and other DataObjects -on your SilverStripe site. +on your SilverStripe site. For more documentation about the module see the provided documentation located inside the docs folder. @@ -41,4 +56,4 @@ and any new translations will be merged back to the project source code. Please use https://www.transifex.com/projects/p/silverstripe-comments/ to contribute translations, rather than sending pull requests with YAML files. -See the ["i18n" topic](http://doc.silverstripe.org/framework/en/trunk/topics/i18n) on doc.silverstripe.org for more details. \ No newline at end of file +See the ["i18n" topic](http://doc.silverstripe.org/framework/en/trunk/topics/i18n) on doc.silverstripe.org for more details. diff --git a/tests/CommentAdminTest.php b/tests/CommentAdminTest.php new file mode 100644 index 0000000..2463f4b --- /dev/null +++ b/tests/CommentAdminTest.php @@ -0,0 +1,57 @@ + array( + # FIXME - this is a bug, missing from lang.yml files + 'name' => 'Access to \'Comments\' section', + 'category' => 'Accès au CMS' + ) + ); + $this->assertEquals($expected, $commentAdmin->providePermissions()); + + i18n::set_locale($locale); + $expected = array( + 'CMS_ACCESS_CommentAdmin' => array( + # FIXME - this is a bug, missing from lang.yml files + 'name' => 'Access to \'Comments\' section', + 'category' => 'CMS Access' + ) + ); + $this->assertEquals($expected, $commentAdmin->providePermissions()); + } + + public function testGetEditForm() { + $commentAdmin = new CommentAdmin(); + $this->logInWithPermission('CMS_ACCESS_CommentAdmin'); + $form = $commentAdmin->getEditForm(); + $names = $this->getFormFieldNames($form); + $expected = array( + 'NewComments', + 'ApprovedComments', + 'SpamComments' + ); + $this->assertEquals($expected, $names); + + if($member = Member::currentUser()) $member->logOut(); + + $form = $commentAdmin->getEditForm(); + } + + private function getFormFieldNames($form) { + $result = array(); + $fields = $form->Fields(); + $tab = $fields->findOrMakeTab('Root'); + $fields = $tab->FieldList(); + foreach ($fields as $field) { + array_push($result, $field->getName()); + } + return $result; + } + +} diff --git a/tests/CommentListTest.php b/tests/CommentListTest.php new file mode 100644 index 0000000..19d01c4 --- /dev/null +++ b/tests/CommentListTest.php @@ -0,0 +1,138 @@ +update('CommentsExtension', 'comments', array( + 'enabled' => true, + 'enabled_cms' => false, + 'require_login' => false, + 'require_login_cms' => false, + 'required_permission' => false, + 'require_moderation_nonmembers' => false, + 'require_moderation' => false, + 'require_moderation_cms' => false, + 'frontend_moderation' => false, + 'frontend_spam' => false, + )); + + // Configure this dataobject + Config::inst()->update('CommentableItem', 'comments', array( + 'enabled_cms' => true + )); + } + + public function tearDown() { + Config::unnest(); + parent::tearDown(); + } + + public function testGetForeignClass() { + $item = $this->objFromFixture('CommentableItem', 'first'); + + // This is the class the Comments are related to + $this->assertEquals('CommentableItem', + $item->Comments()->getForeignClass()); + } + + public function testAddNonComment() { + $item = $this->objFromFixture('CommentableItem', 'first'); + $comments = $item->Comments(); + $this->assertEquals(4, $comments->count()); + $member = Member::get()->first(); + try { + $comments->add($member); + $this->fail('Should not have been able to add member to comments'); + } catch (InvalidArgumentException $e) { + $this->assertEquals( + 'CommentList::add() expecting a Comment object, or ID value', + $e->getMessage() + ); + } + } + + public function testAddComment() { + $item = $this->objFromFixture('CommentableItem', 'first'); + $firstComment = $this->objFromFixture('Comment', 'firstComA'); + $comments = $item->Comments();//->sort('Created'); + + foreach ($comments as $comment) { + error_log($comment->ID . ' ' . $comment->Created .' ' . $comment->Comment); + } + + $this->assertEquals(4, $comments->count()); + $newComment = new Comment(); + $newComment->Name = 'Fred Bloggs'; + $newComment->Comment = 'This is a test comment'; + $newComment->write(); + + $comments->add($newComment); + // As a comment has been added, there should be 5 comments now + $this->assertEquals(5, $item->Comments()->count()); + + $newComment2 = new Comment(); + $newComment2->Name = 'John Smith'; + $newComment2->Comment = 'This is another test comment'; + $newComment2->write(); + + // test adding the same comment by ID + $comments->add($newComment2->ID); + $this->assertEquals(6, $item->Comments()->count()); + + $this->setExpectedException( + 'InvalidArgumentException', + "CommentList::add() can't be called until a single foreign ID is set" + ); + $list = new CommentList('CommentableItem'); + $list->add($newComment); + } + + public function testRemoveComment() { + // remove by comment + $item = $this->objFromFixture('CommentableItem', 'first'); + $this->assertEquals(4, $item->Comments()->count()); + $comments = $item->Comments(); + $comment = $comments->first(); + $comments->remove($comment); + + // now remove by ID + $comments = $item->Comments(); + $comment = $comments->first(); + $comments->remove($comment->ID); + $this->assertEquals(2, $item->Comments()->count()); + } + + public function testRemoveNonComment() { + $item = $this->objFromFixture('CommentableItem', 'first'); + $this->assertEquals(4, $item->Comments()->count()); + $comments = $item->Comments(); + + // try and remove a non comment + $member = Member::get()->first(); + + + + try { + $comments->remove($member); + $this->fail('Should not have been able to remove member from comments'); + } catch (InvalidArgumentException $e) { + $this->assertEquals( + 'CommentList::remove() expecting a Comment object, or ID', + $e->getMessage() + ); + } + } + +} diff --git a/tests/CommentTestHelper.php b/tests/CommentTestHelper.php new file mode 100644 index 0000000..3a139d3 --- /dev/null +++ b/tests/CommentTestHelper.php @@ -0,0 +1,25 @@ +findOrMakeTab($tabName); + $fields = $tab->FieldList(); + CommentTestHelper::assertFieldNames($context, $expected, $fields); + } + + public static function assertFieldNames($context, $expected, $fields) { + $actual = array(); + foreach ($fields as $field) { + if (get_class($field) == 'FieldGroup') { + array_push($actual, $field->Name()); + } else { + array_push($actual, $field->getName()); + } + } + $context->assertEquals($expected, $actual); + } +} diff --git a/tests/CommentingControllerTest.php b/tests/CommentingControllerTest.php index 94a09e7..9368f82 100644 --- a/tests/CommentingControllerTest.php +++ b/tests/CommentingControllerTest.php @@ -5,13 +5,13 @@ * @subpackage tests */ class CommentingControllerTest extends FunctionalTest { - + public static $fixture_file = 'CommentsTest.yml'; protected $extraDataObjects = array( 'CommentableItem' ); - + protected $securityEnabled; public function tearDown() { @@ -28,8 +28,92 @@ class CommentingControllerTest extends FunctionalTest { $this->securityEnabled = SecurityToken::is_enabled(); } + public function testApprove() { + SecurityToken::disable(); + + // mark a comment as spam then approve it + $this->logInWithPermission('CMS_ACCESS_CommentAdmin'); + $comment = $this->objFromFixture('Comment', 'firstComA'); + $comment->markSpam(); + $st = new Comment_SecurityToken($comment); + $url = 'CommentingController/approve/' . $comment->ID; + $url = $st->addToUrl($url, Member::currentUser()); + $response = $this->get($url); + $this->assertEquals(200, $response->getStatusCode()); + $comment = DataObject::get_by_id('Comment', $comment->ID); + + // Need to use 0,1 here instead of false, true for SQLite + $this->assertEquals(0, $comment->IsSpam); + $this->assertEquals(1, $comment->Moderated); + + // try and approve a non existent comment + $response = $this->get('CommentingController/approve/100000'); + $this->assertEquals(404, $response->getStatusCode()); + + } + + public function testSetGetOwnerController() { + $commController = new CommentingController(); + $commController->setOwnerController(Controller::curr()); + $this->assertEquals(Controller::curr(), $commController->getOwnerController()); + $commController->setOwnerController(null); + $this->assertNull($commController->getOwnerController()); + } + + public function testHam() { + SecurityToken::disable(); + + // mark a comment as spam then ham it + $this->logInWithPermission('CMS_ACCESS_CommentAdmin'); + $comment = $this->objFromFixture('Comment', 'firstComA'); + $comment->markSpam(); + $st = new Comment_SecurityToken($comment); + $url = 'CommentingController/ham/' . $comment->ID; + $url = $st->addToUrl($url, Member::currentUser()); + $response = $this->get($url); + $this->assertEquals(200, $response->getStatusCode()); + $comment = DataObject::get_by_id('Comment', $comment->ID); + + // Need to use 0,1 here instead of false, true for SQLite + $this->assertEquals(0, $comment->IsSpam); + $this->assertEquals(1, $comment->Moderated); + + // try and ham a non existent comment + $response = $this->get('CommentingController/ham/100000'); + $this->assertEquals(404, $response->getStatusCode()); + + } + + public function testSpam() { + // mark a comment as approved then spam it + $this->logInWithPermission('CMS_ACCESS_CommentAdmin'); + $comment = $this->objFromFixture('Comment', 'firstComA'); + $comment->markApproved(); + $st = new Comment_SecurityToken($comment); + $url = 'CommentingController/spam/' . $comment->ID; + $url = $st->addToUrl($url, Member::currentUser()); + $response = $this->get($url); + $this->assertEquals(200, $response->getStatusCode()); + $comment = DataObject::get_by_id('Comment', $comment->ID); + + // Need to use 0,1 here instead of false, true for SQLite + $this->assertEquals(1, $comment->IsSpam); + $this->assertEquals(1, $comment->Moderated); + + // try and spam a non existent comment + $response = $this->get('CommentingController/spam/100000'); + $this->assertEquals(404, $response->getStatusCode()); + + } + public function testRSS() { - $item = $this->objFromFixture('CommentableItem', 'first'); + // Delete the newly added children of firstComA so as not to have to recalculate values below + $this->objFromFixture('Comment', 'firstComAChild1')->delete(); + $this->objFromFixture('Comment', 'firstComAChild2')->delete(); + $this->objFromFixture('Comment', 'firstComAChild3')->delete(); + + $item = $this->objFromFixture('CommentableItem', 'first'); + // comments sitewide $response = $this->get('CommentingController/rss'); @@ -55,7 +139,81 @@ class CommentingControllerTest extends FunctionalTest { $this->assertEquals(404, $response->getStatusCode()); } + // This is returning a 404 which looks logical code wise but also a bit weird. + // Test module on a clean install and check what the actual URL is first +/* public function testReply() { + $this->logInWithPermission('CMS_ACCESS_CommentAdmin'); + $comment = $this->objFromFixture('Comment', 'firstComA'); + $item = $this->objFromFixture('CommentableItem', 'first'); + + $st = new Comment_SecurityToken($comment); + $url = 'CommentingController/reply/' . $item->ID.'?ParentCommentID=' . $comment->ID; + error_log($url); + $response = $this->get($url); + error_log(print_r($response,1)); + + $this->assertEquals(200, $response->getStatusCode()); + + } +*/ +/* + public function testCommentsFormLoadMemberData() { + Config::inst()->update('CommentableItem', 'comments', array( + 'use_preview' => false + )); + $this->logInAs('visitor'); + SecurityToken::disable(); + $parent = $this->objFromFixture('CommentableItem', 'first'); + $parent->CommentsRequireLogin = true; + $parent->PostingRequiredPermission = true; + //$parent->write(); + $commController = new CommentingController(); + $commController->setOwnerRecord($parent); + + $form = $commController->CommentsForm(); + $commentsFields = $form->Fields()->first()->FieldList(); + $expected = array('Name', 'Email', 'URL', 'Comment', 'PreviewComment'); + CommentTestHelper::assertFieldNames($this, $expected, $commentsFields); + } +*/ + + public function testCommentsFormUsePreview() { + // test with preview on + Config::inst()->update('CommentableItem', 'comments', array( + 'use_preview' => true + )); + + $this->objFromFixture('Comment', 'firstComAChild1')->delete(); + $this->objFromFixture('Comment', 'firstComAChild2')->delete(); + $this->objFromFixture('Comment', 'firstComAChild3')->delete(); + + SecurityToken::disable(); + $this->autoFollowRedirection = false; + $parent = $this->objFromFixture('CommentableItem', 'first'); + $commController = new CommentingController(); + $commController->setOwnerRecord($parent); + + $form = $commController->CommentsForm(); + $commentsFields = $form->Fields()->first()->FieldList(); + $expected = array('Name', 'Email', 'URL', 'Comment', 'PreviewComment'); + CommentTestHelper::assertFieldNames($this, $expected, $commentsFields); + + // Turn off preview. Assert lack of preview field + Config::inst()->update('CommentableItem', 'comments', array( + 'use_preview' => false + )); + $form = $commController->CommentsForm(); + $commentsFields = $form->Fields()->first()->FieldList(); + $expected = array('Name', 'Email', 'URL', 'Comment'); + CommentTestHelper::assertFieldNames($this, $expected, $commentsFields); + } + public function testCommentsForm() { + // Delete the newly added children of firstComA so as not to change this test + $this->objFromFixture('Comment', 'firstComAChild1')->delete(); + $this->objFromFixture('Comment', 'firstComAChild2')->delete(); + $this->objFromFixture('Comment', 'firstComAChild3')->delete(); + SecurityToken::disable(); $this->autoFollowRedirection = false; $parent = $this->objFromFixture('CommentableItem', 'first'); @@ -83,7 +241,7 @@ class CommentingControllerTest extends FunctionalTest { )), Comment::get()->filter('Email', 'guy@test.com') ); - + // Test posting to parent comment $parentComment = $this->objFromFixture('Comment', 'firstComA'); $this->assertEquals(0, $parentComment->ChildComments()->count()); @@ -110,5 +268,7 @@ class CommentingControllerTest extends FunctionalTest { 'BaseClass' => 'CommentableItem', 'ParentCommentID' => $parentComment->ID )), $parentComment->ChildComments()); + + } } diff --git a/tests/CommentingTest.php b/tests/CommentingTest.php new file mode 100644 index 0000000..6b0acfb --- /dev/null +++ b/tests/CommentingTest.php @@ -0,0 +1,173 @@ +assertEquals($expected, $e->getMessage()); + } + } + } + + + public function test_set_config_value() { + // public static function set_config_value($class, $key, $value = false) { + Commenting::set_config_value( + 'CommentableItem', + 'comments_holder_id', + 'commentable_item' + ); + + $config = Config::inst()->get( + 'CommentableItem', + 'comments' + ); + $actual = $config['comments_holder_id']; + + $this->assertEquals( + 'commentable_item', + $actual + ); + Commenting::set_config_value( + 'all', + 'comments_holder_id', + 'all_items_actually_commentsextension' + ); + + $config = Config::inst()->get( + 'CommentsExtension', + 'comments' + ); + $actual = $config['comments_holder_id']; + $this->assertEquals( + 'all_items_actually_commentsextension', + $actual + ); + } + + public function test_get_config_value() { + Config::inst()->update('CommentableItem', 'comments', + array( + 'comments_holder_id' => 'commentable_item' + ) + ); + $this->assertEquals( + 'commentable_item', + Commenting::get_config_value('CommentableItem', 'comments_holder_id') + ); + + Config::inst()->update('CommentsExtension', 'comments', + array( + 'comments_holder_id' => 'comments_extension' + ) + ); + // if class is null, method uses the CommentsExtension property + $this->assertEquals( + 'comments_extension', + Commenting::get_config_value(null, 'comments_holder_id') + ); + + $this->setExpectedException( + 'InvalidArgumentException', + 'Member does not have commenting enabled' + ); + Commenting::get_config_value('Member', 'comments_holder_id'); + } + + public function test_config_value_equals() { + Config::inst()->update('CommentableItem', 'comments', + array( + 'comments_holder_id' => 'some_value' + ) + ); + + $this->assertTrue( + Commenting::config_value_equals( + 'CommentableItem', + 'comments_holder_id', + 'some_value' + ) + ); + + $this->assertNull( + Commenting::config_value_equals( + 'CommentableItem', + 'comments_holder_id', + 'not_some_value' + ) + ); + } + + public function test_add() { + Commenting::add('Member', array('comments_holder_id' => 'test_add_value')); + + $config = Config::inst()->get( + 'Member', + 'comments' + ); + $actual = $config['comments_holder_id']; + $this->assertEquals( + 'test_add_value', + $actual + ); + + Commenting::add('Member'); + + $config = Config::inst()->get( + 'Member', + 'comments' + ); + $actual = $config['comments_holder_id']; + // no settings updated + $this->assertEquals( + 'test_add_value', + $actual + ); + + $this->setExpectedException('InvalidArgumentException', "\$settings needs to be an array or null"); + Commenting::add('Member', 'illegal format, not an array'); + + + } + + public function test_can_member_post() { + // logout + if($member = Member::currentUser()) $member->logOut(); + + Config::inst()->update('CommentableItem', 'comments', + array( + 'require_login' => false + ) + ); + $this->assertTrue(Commenting::can_member_post('CommentableItem')); + + Config::inst()->update('CommentableItem', 'comments', + array( + 'require_login' => true + ) + ); + $this->assertFalse(Commenting::can_member_post('CommentableItem')); + + $this->logInWithPermission('CMS_ACCESS_CommentAdmin'); + $this->assertTrue(Commenting::can_member_post('CommentableItem')); + + Config::inst()->update('CommentableItem', 'comments', + array( + 'require_login' => false + ) + ); + + $this->assertTrue(Commenting::can_member_post('CommentableItem')); + } + +} diff --git a/tests/CommentsExtensionTest.php b/tests/CommentsExtensionTest.php new file mode 100644 index 0000000..f705189 --- /dev/null +++ b/tests/CommentsExtensionTest.php @@ -0,0 +1,407 @@ +update('CommentsExtension', 'comments', array( + 'enabled' => true, + 'enabled_cms' => false, + 'require_login' => false, + 'require_login_cms' => false, + 'required_permission' => false, + 'require_moderation_nonmembers' => false, + 'require_moderation' => false, + 'require_moderation_cms' => false, + 'frontend_moderation' => false, + 'Member' => false, + )); + + $this->requiredExtensions = array( + 'CommentableItem' => 'CommentsExtension' + ); + + // Configure this dataobject + Config::inst()->update('CommentableItem', 'comments', array( + 'enabled_cms' => true + )); + } + + public function tearDown() { + Config::unnest(); + parent::tearDown(); + } + + public function testPopulateDefaults() { + $this->markTestSkipped('TODO'); + } + + public function testUpdateSettingsFields() { + $this->markTestSkipped('This needs SiteTree installed'); + } + + public function testGetModerationRequired() { + + // the 3 options take precedence in this order, executed if true + Config::inst()->update('CommentableItem', 'comments', array( + 'require_moderation_cms' => true, + 'require_moderation' => true, + 'require_moderation_nonmembers' => true + )); + + // With require moderation CMS set to true, the value of the field + // 'ModerationRequired' is returned + $item = $this->objFromFixture('CommentableItem', 'first'); + $item->ModerationRequired = 'None'; + $this->assertEquals('None', $item->getModerationRequired()); + $item->ModerationRequired = 'Required'; + $this->assertEquals('Required', $item->getModerationRequired()); + $item->ModerationRequired = 'NonMembersOnly'; + $this->assertEquals('NonMembersOnly', $item->getModerationRequired()); + + Config::inst()->update('CommentableItem', 'comments', array( + 'require_moderation_cms' => false, + 'require_moderation' => true, + 'require_moderation_nonmembers' => true + )); + $this->assertEquals('Required', $item->getModerationRequired()); + + Config::inst()->update('CommentableItem', 'comments', array( + 'require_moderation_cms' => false, + 'require_moderation' => false, + 'require_moderation_nonmembers' => true + )); + $this->assertEquals('NonMembersOnly', $item->getModerationRequired()); + + Config::inst()->update('CommentableItem', 'comments', array( + 'require_moderation_cms' => false, + 'require_moderation' => false, + 'require_moderation_nonmembers' => false + )); + $this->assertEquals('None', $item->getModerationRequired()); + } + + public function testGetCommentsRequireLogin() { + Config::inst()->update('CommentableItem', 'comments', array( + 'require_login_cms' => true + )); + + // With require moderation CMS set to true, the value of the field + // 'ModerationRequired' is returned + $item = $this->objFromFixture('CommentableItem', 'first'); + $item->CommentsRequireLogin = true; + $this->assertTrue($item->getCommentsRequireLogin()); + $item->CommentsRequireLogin = false; + $this->assertFalse($item->getCommentsRequireLogin()); + + Config::inst()->update('CommentableItem', 'comments', array( + 'require_login_cms' => false, + 'require_login' => false + )); + $this->assertFalse($item->getCommentsRequireLogin()); + Config::inst()->update('CommentableItem', 'comments', array( + 'require_login_cms' => false, + 'require_login' => true + )); + $this->assertTrue($item->getCommentsRequireLogin()); + + } + + public function testAllComments() { + $this->markTestSkipped('TODO'); + } + + public function testAllVisibleComments() { + $this->markTestSkipped('TODO'); + } + + public function testComments() { + $this->markTestSkipped('TODO'); + } + + public function testGetCommentsEnabled() { + $this->markTestSkipped('TODO'); + } + + public function testGetCommentHolderID() { + $item = $this->objFromFixture('CommentableItem', 'first'); + Config::inst()->update('CommentableItem', 'comments', array( + 'comments_holder_id' => 'commentid_test1', + )); + $this->assertEquals('commentid_test1', $item->getCommentHolderID()); + + Config::inst()->update('CommentableItem', 'comments', array( + 'comments_holder_id' => 'commtentid_test_another', + )); + $this->assertEquals('commtentid_test_another', $item->getCommentHolderID()); + } + + + public function testGetPostingRequiredPermission() { + $this->markTestSkipped('TODO'); + } + + public function testCanModerateComments() { + // ensure nobody logged in + if(Member::currentUser()) { Member::currentUser()->logOut(); } + + $item = $this->objFromFixture('CommentableItem', 'first'); + $this->assertFalse($item->canModerateComments()); + + $this->logInWithPermission('CMS_ACCESS_CommentAdmin'); + $this->assertTrue($item->canModerateComments()); + + } + + public function testGetCommentRSSLink() { + $item = $this->objFromFixture('CommentableItem', 'first'); + $link = $item->getCommentRSSLink(); + $this->assertEquals('/CommentingController/rss', $link); + } + + + public function testGetCommentRSSLinkPage() { + $item = $this->objFromFixture('CommentableItem', 'first'); + $page = $item->getCommentRSSLinkPage(); + $this->assertEquals( + '/CommentingController/rss/CommentableItem/' . $item->ID, + $page + ); + } + + public function testCommentsForm() { + Config::inst()->update('CommentableItem', 'comments', array( + 'include_js' => false + ) + ); + $item = $this->objFromFixture('CommentableItem', '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 = ''; + $this->assertContains($expected, $cf); + + $expected = '