2016-01-07 07:59:10 +01:00
|
|
|
<?php
|
|
|
|
|
2017-01-16 20:57:37 +01:00
|
|
|
namespace SilverStripe\Comments\Tests;
|
|
|
|
|
|
|
|
use PHPUnit_Framework_Error_Deprecated;
|
|
|
|
use SilverStripe\Comments\Commenting;
|
|
|
|
use SilverStripe\Comments\Extensions\CommentsExtension;
|
|
|
|
use SilverStripe\Comments\Tests\Stubs\CommentableItem;
|
|
|
|
use SilverStripe\Core\Config\Config;
|
|
|
|
use SilverStripe\Dev\SapphireTest;
|
|
|
|
use SilverStripe\Security\Member;
|
|
|
|
|
2016-02-19 01:48:25 +01:00
|
|
|
class CommentingTest extends SapphireTest
|
|
|
|
{
|
|
|
|
public function setUpOnce()
|
|
|
|
{
|
2016-01-07 07:59:10 +01:00
|
|
|
parent::setUpOnce();
|
|
|
|
}
|
|
|
|
|
2016-02-19 01:48:25 +01:00
|
|
|
public function testDeprecatedMethods()
|
|
|
|
{
|
2016-01-07 07:59:10 +01:00
|
|
|
$methods = array('add', 'remove', 'has_commenting');
|
|
|
|
foreach ($methods as $methodName) {
|
|
|
|
try {
|
2017-01-16 20:57:37 +01:00
|
|
|
Commenting::$methodName(Member::class);
|
2016-01-07 07:59:10 +01:00
|
|
|
} catch (PHPUnit_Framework_Error_Deprecated $e) {
|
|
|
|
$expected = 'Using Commenting:' . $methodName .' is deprecated.'
|
|
|
|
. ' Please use the config API instead';
|
|
|
|
$this->assertEquals($expected, $e->getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-16 20:57:37 +01:00
|
|
|
public function testSetConfigValue()
|
2016-02-19 01:48:25 +01:00
|
|
|
{
|
|
|
|
// public static function set_config_value($class, $key, $value = false) {
|
2016-01-07 07:59:10 +01:00
|
|
|
Commenting::set_config_value(
|
2017-01-16 20:57:37 +01:00
|
|
|
CommentableItem::class,
|
2016-01-07 07:59:10 +01:00
|
|
|
'comments_holder_id',
|
|
|
|
'commentable_item'
|
|
|
|
);
|
|
|
|
|
|
|
|
$config = Config::inst()->get(
|
2017-01-16 20:57:37 +01:00
|
|
|
CommentableItem::class,
|
2016-01-07 07:59:10 +01:00
|
|
|
'comments'
|
|
|
|
);
|
|
|
|
$actual = $config['comments_holder_id'];
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
'commentable_item',
|
|
|
|
$actual
|
|
|
|
);
|
|
|
|
Commenting::set_config_value(
|
|
|
|
'all',
|
|
|
|
'comments_holder_id',
|
|
|
|
'all_items_actually_commentsextension'
|
|
|
|
);
|
|
|
|
|
2016-02-19 01:48:25 +01:00
|
|
|
$config = Config::inst()->get(
|
2017-01-16 20:57:37 +01:00
|
|
|
CommentsExtension::class,
|
2016-01-07 07:59:10 +01:00
|
|
|
'comments'
|
|
|
|
);
|
|
|
|
$actual = $config['comments_holder_id'];
|
|
|
|
$this->assertEquals(
|
|
|
|
'all_items_actually_commentsextension',
|
|
|
|
$actual
|
|
|
|
);
|
2016-02-19 01:48:25 +01:00
|
|
|
}
|
2016-01-07 07:59:10 +01:00
|
|
|
|
2017-01-16 20:57:37 +01:00
|
|
|
public function testGetConfigValue()
|
2016-02-19 01:48:25 +01:00
|
|
|
{
|
2017-01-16 20:57:37 +01:00
|
|
|
Config::inst()->update(
|
|
|
|
CommentableItem::class,
|
|
|
|
'comments',
|
2016-01-07 07:59:10 +01:00
|
|
|
array(
|
|
|
|
'comments_holder_id' => 'commentable_item'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
'commentable_item',
|
2017-01-16 20:57:37 +01:00
|
|
|
Commenting::get_config_value(CommentableItem::class, 'comments_holder_id')
|
2016-01-07 07:59:10 +01:00
|
|
|
);
|
|
|
|
|
2017-01-16 20:57:37 +01:00
|
|
|
Config::inst()->update(
|
|
|
|
CommentsExtension::class,
|
|
|
|
'comments',
|
2016-01-07 07:59:10 +01:00
|
|
|
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'
|
|
|
|
);
|
2017-01-16 20:57:37 +01:00
|
|
|
Commenting::get_config_value(Member::class, 'comments_holder_id');
|
2016-02-19 01:48:25 +01:00
|
|
|
}
|
2016-01-07 07:59:10 +01:00
|
|
|
|
2017-01-16 20:57:37 +01:00
|
|
|
public function testConfigValueEquals()
|
2016-02-19 01:48:25 +01:00
|
|
|
{
|
2017-01-16 20:57:37 +01:00
|
|
|
Config::inst()->update(
|
|
|
|
CommentableItem::class,
|
|
|
|
'comments',
|
2016-01-07 07:59:10 +01:00
|
|
|
array(
|
|
|
|
'comments_holder_id' => 'some_value'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertTrue(
|
|
|
|
Commenting::config_value_equals(
|
2017-01-16 20:57:37 +01:00
|
|
|
CommentableItem::class,
|
2016-01-07 07:59:10 +01:00
|
|
|
'comments_holder_id',
|
|
|
|
'some_value'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertNull(
|
|
|
|
Commenting::config_value_equals(
|
2017-01-16 20:57:37 +01:00
|
|
|
CommentableItem::class,
|
2016-01-07 07:59:10 +01:00
|
|
|
'comments_holder_id',
|
|
|
|
'not_some_value'
|
|
|
|
)
|
|
|
|
);
|
2016-02-19 01:48:25 +01:00
|
|
|
}
|
2016-01-07 07:59:10 +01:00
|
|
|
|
2017-01-16 20:57:37 +01:00
|
|
|
public function testAdd()
|
2016-02-19 01:48:25 +01:00
|
|
|
{
|
2017-01-16 20:57:37 +01:00
|
|
|
Commenting::add(Member::class, array('comments_holder_id' => 'test_add_value'));
|
2016-01-07 07:59:10 +01:00
|
|
|
|
|
|
|
$config = Config::inst()->get(
|
2017-01-16 20:57:37 +01:00
|
|
|
Member::class,
|
|
|
|
'comments'
|
|
|
|
);
|
2016-01-07 07:59:10 +01:00
|
|
|
$actual = $config['comments_holder_id'];
|
|
|
|
$this->assertEquals(
|
|
|
|
'test_add_value',
|
|
|
|
$actual
|
|
|
|
);
|
|
|
|
|
2017-01-16 20:57:37 +01:00
|
|
|
Commenting::add(Member::class);
|
2016-01-07 07:59:10 +01:00
|
|
|
|
|
|
|
$config = Config::inst()->get(
|
2017-01-16 20:57:37 +01:00
|
|
|
Member::class,
|
|
|
|
'comments'
|
|
|
|
);
|
2016-01-07 07:59:10 +01:00
|
|
|
$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");
|
2017-01-16 20:57:37 +01:00
|
|
|
Commenting::add(Member::class, 'illegal format, not an array');
|
2016-01-07 07:59:10 +01:00
|
|
|
}
|
|
|
|
|
2017-01-16 20:57:37 +01:00
|
|
|
public function testCanMemberPost()
|
2016-02-19 01:48:25 +01:00
|
|
|
{
|
2016-01-07 07:59:10 +01:00
|
|
|
// logout
|
2016-02-19 01:48:25 +01:00
|
|
|
if ($member = Member::currentUser()) {
|
|
|
|
$member->logOut();
|
|
|
|
}
|
2016-01-07 07:59:10 +01:00
|
|
|
|
2017-01-16 20:57:37 +01:00
|
|
|
Config::inst()->update(
|
|
|
|
CommentableItem::class,
|
|
|
|
'comments',
|
2016-01-07 07:59:10 +01:00
|
|
|
array(
|
2017-01-16 20:57:37 +01:00
|
|
|
'require_login' => false
|
2016-01-07 07:59:10 +01:00
|
|
|
)
|
|
|
|
);
|
2017-01-16 20:57:37 +01:00
|
|
|
$this->assertTrue(Commenting::can_member_post(CommentableItem::class));
|
2016-01-07 07:59:10 +01:00
|
|
|
|
2017-01-16 20:57:37 +01:00
|
|
|
Config::inst()->update(
|
|
|
|
CommentableItem::class,
|
|
|
|
'comments',
|
2016-01-07 07:59:10 +01:00
|
|
|
array(
|
2017-01-16 20:57:37 +01:00
|
|
|
'require_login' => true
|
2016-01-07 07:59:10 +01:00
|
|
|
)
|
|
|
|
);
|
2017-01-16 20:57:37 +01:00
|
|
|
$this->assertFalse(Commenting::can_member_post(CommentableItem::class));
|
2016-01-07 07:59:10 +01:00
|
|
|
|
|
|
|
$this->logInWithPermission('CMS_ACCESS_CommentAdmin');
|
2017-01-16 20:57:37 +01:00
|
|
|
$this->assertTrue(Commenting::can_member_post(CommentableItem::class));
|
2016-01-07 07:59:10 +01:00
|
|
|
|
2017-01-16 20:57:37 +01:00
|
|
|
Config::inst()->update(
|
|
|
|
CommentableItem::class,
|
|
|
|
'comments',
|
2016-01-07 07:59:10 +01:00
|
|
|
array(
|
2017-01-16 20:57:37 +01:00
|
|
|
'require_login' => false
|
2016-01-07 07:59:10 +01:00
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2017-01-16 20:57:37 +01:00
|
|
|
$this->assertTrue(Commenting::can_member_post(CommentableItem::class));
|
2016-02-19 01:48:25 +01:00
|
|
|
}
|
2016-01-07 07:59:10 +01:00
|
|
|
}
|