2016-01-07 07:59:10 +01:00
|
|
|
<?php
|
|
|
|
|
2017-01-16 20:57:37 +01:00
|
|
|
namespace SilverStripe\Comments\Tests;
|
|
|
|
|
2017-10-09 06:26:07 +02:00
|
|
|
use SilverStripe\Comments\Admin\CommentAdmin;
|
2017-01-16 20:57:37 +01:00
|
|
|
use SilverStripe\Comments\Admin\CommentsGridField;
|
|
|
|
use SilverStripe\Comments\Admin\CommentsGridFieldAction;
|
|
|
|
use SilverStripe\Comments\Admin\CommentsGridFieldConfig;
|
|
|
|
use SilverStripe\Comments\Model\Comment;
|
2017-01-27 04:42:26 +01:00
|
|
|
use SilverStripe\Comments\Tests\Stubs\CommentableItem;
|
2018-06-20 06:53:14 +02:00
|
|
|
use SilverStripe\Comments\Tests\Stubs\Team;
|
2017-01-16 20:57:37 +01:00
|
|
|
use SilverStripe\Control\Controller;
|
|
|
|
use SilverStripe\Dev\SapphireTest;
|
|
|
|
use SilverStripe\Forms\FieldList;
|
|
|
|
use SilverStripe\Forms\Form;
|
2017-12-14 05:57:03 +01:00
|
|
|
use SilverStripe\Forms\GridField\GridField;
|
2017-01-16 20:57:37 +01:00
|
|
|
use SilverStripe\Forms\GridField\GridFieldDeleteAction;
|
2017-12-14 05:57:03 +01:00
|
|
|
use SilverStripe\ORM\ArrayList;
|
2017-01-16 20:57:37 +01:00
|
|
|
use SilverStripe\ORM\DataList;
|
|
|
|
use SilverStripe\ORM\DataObject;
|
2022-11-28 21:35:55 +01:00
|
|
|
use SilverStripe\Dev\Deprecation;
|
2017-01-16 20:57:37 +01:00
|
|
|
|
2016-02-19 01:48:25 +01:00
|
|
|
class CommentsGridFieldActionTest extends SapphireTest
|
|
|
|
{
|
2017-01-27 03:56:19 +01:00
|
|
|
protected $usesDatabase = true;
|
|
|
|
|
2017-12-14 05:57:03 +01:00
|
|
|
protected static $extra_dataobjects = [
|
|
|
|
CommentableItem::class,
|
2018-06-20 06:47:56 +02:00
|
|
|
Team::class,
|
2017-12-14 05:57:03 +01:00
|
|
|
];
|
|
|
|
|
2016-01-07 07:59:10 +01:00
|
|
|
/** @var ArrayList */
|
|
|
|
protected $list;
|
|
|
|
|
|
|
|
/** @var GridField */
|
|
|
|
protected $gridField;
|
|
|
|
|
|
|
|
/** @var Form */
|
|
|
|
protected $form;
|
|
|
|
|
2021-10-27 07:03:55 +02:00
|
|
|
protected function setUp(): void
|
2016-02-19 01:48:25 +01:00
|
|
|
{
|
2016-01-07 07:59:10 +01:00
|
|
|
parent::setUp();
|
2022-11-28 21:35:55 +01:00
|
|
|
if (Deprecation::isEnabled()) {
|
|
|
|
$this->markTestSkipped('Test calls deprecated code');
|
|
|
|
}
|
2017-01-16 20:57:37 +01:00
|
|
|
$this->list = new DataList(Team::class);
|
2016-01-07 07:59:10 +01:00
|
|
|
$config = CommentsGridFieldConfig::create()->addComponent(new GridFieldDeleteAction());
|
|
|
|
$this->gridField = new CommentsGridField('testfield', 'testfield', $this->list, $config);
|
2017-10-09 06:26:07 +02:00
|
|
|
$this->form = new Form(new CommentAdmin(), 'mockform', new FieldList(array($this->gridField)), new FieldList());
|
2016-01-07 07:59:10 +01:00
|
|
|
}
|
|
|
|
|
2016-02-19 01:48:25 +01:00
|
|
|
public function testAugmentColumns()
|
|
|
|
{
|
2016-01-07 07:59:10 +01:00
|
|
|
$action = new CommentsGridFieldAction();
|
|
|
|
|
|
|
|
// an entry called 'Actions' is added to the columns array
|
|
|
|
$columns = array();
|
|
|
|
$action->augmentColumns($this->gridField, $columns);
|
|
|
|
$expected = array('Actions');
|
|
|
|
$this->assertEquals($expected, $columns);
|
|
|
|
|
|
|
|
$columns = array('Actions');
|
|
|
|
$action->augmentColumns($this->gridField, $columns);
|
|
|
|
$expected = array('Actions');
|
|
|
|
$this->assertEquals($expected, $columns);
|
2016-02-19 01:48:25 +01:00
|
|
|
}
|
2016-01-07 07:59:10 +01:00
|
|
|
|
2016-02-19 01:48:25 +01:00
|
|
|
public function testGetColumnAttributes()
|
|
|
|
{
|
|
|
|
$action = new CommentsGridFieldAction();
|
2016-01-07 07:59:10 +01:00
|
|
|
$record = new Comment();
|
2017-01-16 20:57:37 +01:00
|
|
|
$attrs = $action->getColumnAttributes($this->gridField, $record, Comment::class);
|
2016-01-07 07:59:10 +01:00
|
|
|
$this->assertEquals(array('class' => 'col-buttons'), $attrs);
|
2016-02-19 01:48:25 +01:00
|
|
|
}
|
2016-01-07 07:59:10 +01:00
|
|
|
|
2016-02-19 01:48:25 +01:00
|
|
|
public function testGetColumnMetadata()
|
|
|
|
{
|
|
|
|
$action = new CommentsGridFieldAction();
|
2016-01-07 07:59:10 +01:00
|
|
|
$result = $action->getColumnMetadata($this->gridField, 'Actions');
|
|
|
|
$this->assertEquals(array('title' => ''), $result);
|
|
|
|
$result = $action->getColumnMetadata($this->gridField, 'SomethingElse');
|
|
|
|
$this->assertNull($result);
|
2016-02-19 01:48:25 +01:00
|
|
|
}
|
2016-01-07 07:59:10 +01:00
|
|
|
|
2016-02-19 01:48:25 +01:00
|
|
|
public function testGetColumnsHandled()
|
|
|
|
{
|
|
|
|
$action = new CommentsGridFieldAction();
|
2016-01-07 07:59:10 +01:00
|
|
|
$result = $action->getColumnsHandled($this->gridField);
|
|
|
|
$this->assertEquals(array('Actions'), $result);
|
2016-02-19 01:48:25 +01:00
|
|
|
}
|
2016-01-07 07:59:10 +01:00
|
|
|
|
2016-02-19 01:48:25 +01:00
|
|
|
public function testGetColumnContent()
|
|
|
|
{
|
2016-01-07 07:59:10 +01:00
|
|
|
$this->logInWithPermission('CMS_ACCESS_CommentAdmin');
|
2016-02-19 01:48:25 +01:00
|
|
|
$action = new CommentsGridFieldAction();
|
2016-01-07 07:59:10 +01:00
|
|
|
$record = new Comment();
|
|
|
|
$record->Name = 'Name of commeter';
|
|
|
|
$record->Comment = 'This is a comment';
|
|
|
|
$record->write();
|
|
|
|
$recordID = $record->ID;
|
2017-01-16 20:57:37 +01:00
|
|
|
$html = $action->getColumnContent($this->gridField, $record, Comment::class);
|
2021-10-27 07:03:55 +02:00
|
|
|
$this->assertStringContainsString('data-url="admin/comments/mockform/field/testfield', $html);
|
2016-01-07 07:59:10 +01:00
|
|
|
|
2021-10-27 07:03:55 +02:00
|
|
|
$this->assertStringContainsString('value="Spam"', $html);
|
|
|
|
$this->assertStringContainsString('id="action_CustomAction' . $recordID . 'Spam"', $html);
|
2017-12-14 05:57:03 +01:00
|
|
|
|
2021-10-27 07:03:55 +02:00
|
|
|
$this->assertStringContainsString('value="Approve"', $html);
|
|
|
|
$this->assertStringContainsString('id="action_CustomAction' . $recordID . 'Approve"', $html);
|
2016-01-07 07:59:10 +01:00
|
|
|
|
|
|
|
// If marked as spam, only the approve button should be available
|
|
|
|
$record->markSpam();
|
|
|
|
$record->write();
|
2017-01-16 20:57:37 +01:00
|
|
|
$html = $action->getColumnContent($this->gridField, $record, Comment::class);
|
2021-10-27 07:03:55 +02:00
|
|
|
$this->assertStringContainsString('value="Approve"', $html);
|
|
|
|
$this->assertStringNotContainsString('value="Spam"', $html);
|
2016-01-07 07:59:10 +01:00
|
|
|
|
|
|
|
// If marked as spam, only the approve button should be available
|
|
|
|
$record->markApproved();
|
|
|
|
$record->write();
|
2017-01-16 20:57:37 +01:00
|
|
|
$html = $action->getColumnContent($this->gridField, $record, Comment::class);
|
2021-10-27 07:03:55 +02:00
|
|
|
$this->assertStringNotContainsString('value="Approve"', $html);
|
|
|
|
$this->assertStringContainsString('value="Spam"', $html);
|
2016-02-19 01:48:25 +01:00
|
|
|
}
|
2016-01-07 07:59:10 +01:00
|
|
|
|
2016-02-19 01:48:25 +01:00
|
|
|
public function testGetActions()
|
|
|
|
{
|
|
|
|
$action = new CommentsGridFieldAction();
|
2016-01-07 07:59:10 +01:00
|
|
|
$result = $action->getActions($this->gridField);
|
|
|
|
$this->assertEquals(array('spam', 'approve'), $result);
|
2016-02-19 01:48:25 +01:00
|
|
|
}
|
2016-01-07 07:59:10 +01:00
|
|
|
|
2016-02-19 01:48:25 +01:00
|
|
|
public function testHandleAction()
|
|
|
|
{
|
2017-01-27 04:20:14 +01:00
|
|
|
$this->logInWithPermission('CMS_ACCESS_CommentAdmin');
|
2017-01-27 04:42:26 +01:00
|
|
|
$item = new CommentableItem;
|
|
|
|
$item->write();
|
|
|
|
|
2016-02-19 01:48:25 +01:00
|
|
|
$action = new CommentsGridFieldAction();
|
2016-01-07 07:59:10 +01:00
|
|
|
$record = new Comment();
|
2017-01-16 20:57:37 +01:00
|
|
|
$record->Name = 'Name of commenter';
|
2016-01-07 07:59:10 +01:00
|
|
|
$record->Comment = 'This is a comment';
|
2017-01-27 04:42:26 +01:00
|
|
|
$record->ParentID = $item->ID;
|
|
|
|
$record->ParentClass = $item->class;
|
2016-01-07 07:59:10 +01:00
|
|
|
$record->write();
|
|
|
|
$recordID = $record->ID;
|
|
|
|
$arguments = array('RecordID' => $recordID);
|
|
|
|
$data = array();
|
2016-02-19 01:48:25 +01:00
|
|
|
$result = $action->handleAction($this->gridField, 'spam', $arguments, $data);
|
2016-01-07 07:59:10 +01:00
|
|
|
$this->assertEquals(200, Controller::curr()->getResponse()->getStatusCode());
|
|
|
|
$this->assertEquals(
|
|
|
|
'Comment marked as spam.',
|
|
|
|
Controller::curr()->getResponse()->getStatusDescription()
|
|
|
|
);
|
2017-01-16 20:57:37 +01:00
|
|
|
$record = DataObject::get_by_id(Comment::class, $recordID);
|
2016-01-07 07:59:10 +01:00
|
|
|
$this->assertEquals(1, $record->Moderated);
|
|
|
|
$this->assertEquals(1, $record->IsSpam);
|
|
|
|
|
2017-01-16 20:57:37 +01:00
|
|
|
//getStatusDescription
|
2016-02-19 01:48:25 +01:00
|
|
|
$result = $action->handleAction($this->gridField, 'approve', $arguments, $data);
|
2016-01-07 07:59:10 +01:00
|
|
|
$this->assertEquals(200, Controller::curr()->getResponse()->getStatusCode());
|
|
|
|
$this->assertEquals(
|
|
|
|
'Comment approved.',
|
|
|
|
Controller::curr()->getResponse()->getStatusDescription()
|
|
|
|
);
|
|
|
|
|
2017-01-16 20:57:37 +01:00
|
|
|
$record = DataObject::get_by_id(Comment::class, $recordID);
|
2016-01-07 07:59:10 +01:00
|
|
|
$this->assertEquals(1, $record->Moderated);
|
|
|
|
$this->assertEquals(0, $record->IsSpam);
|
2016-02-19 01:48:25 +01:00
|
|
|
}
|
2016-01-07 07:59:10 +01:00
|
|
|
}
|