View source
<?php
namespace Drupal\Tests\comment\Kernel;
use Drupal\comment\Entity\Comment;
use Drupal\comment\Entity\CommentType;
use Drupal\comment\Tests\CommentTestTrait;
use Drupal\Core\Datetime\Entity\DateFormat;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\filter\Entity\FilterFormat;
use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
use Drupal\system\Entity\Action;
class CommentActionsTest extends EntityKernelTestBase {
use CommentTestTrait;
protected static $modules = [
'comment',
'entity_test',
];
protected $keywords;
protected $comment;
protected function setUp() : void {
parent::setUp();
$this
->installConfig([
'user',
'comment',
]);
$this
->installSchema('comment', [
'comment_entity_statistics',
]);
CommentType::create([
'id' => 'comment',
'label' => 'Default comments',
'description' => 'Default comment field',
'target_entity_type_id' => 'entity_test',
])
->save();
$this
->addDefaultCommentField('entity_test', 'entity_test', 'comment');
DateFormat::create([
'id' => 'fallback',
'pattern' => 'D, m/d/Y - H:i',
])
->save();
FilterFormat::create([
'format' => 'no_filters',
'name' => 'No filters',
'filters' => [],
])
->save();
$this
->drupalSetCurrentUser($this
->drupalCreateUser());
$this->keywords = [
$this
->randomMachineName(),
$this
->randomMachineName(),
];
$host = EntityTest::create();
$host
->save();
$this->comment = Comment::create([
'entity_type' => 'entity_test',
'name' => $this
->randomString(),
'hostname' => 'magic.example.com',
'mail' => 'tonythemagicalpony@example.com',
'subject' => $this->keywords[0],
'comment_body' => $this->keywords[1],
'entity_id' => $host
->id(),
'comment_type' => 'comment',
'field_name' => 'comment',
]);
$this->comment
->get('comment_body')->format = 'no_filters';
$this->comment
->setPublished();
}
public function testCommentDefaultConfigActions() {
$this
->assertTrue($this->comment
->isNew());
$action = Action::load('comment_save_action');
$action
->execute([
$this->comment,
]);
$this
->assertFalse($this->comment
->isNew());
$this
->assertTrue($this->comment
->isPublished());
$action = Action::load('comment_unpublish_action');
$action
->execute([
$this->comment,
]);
$this
->assertFalse($this->comment
->isPublished(), 'Comment was unpublished');
$this
->assertSame([
'module' => [
'comment',
],
], $action
->getDependencies());
$action = Action::load('comment_publish_action');
$action
->execute([
$this->comment,
]);
$this
->assertTrue($this->comment
->isPublished(), 'Comment was published');
$action = Action::load('comment_delete_action');
$action
->execute([
$this->comment,
]);
$temp_store = $this->container
->get('tempstore.private');
$account_id = $this->container
->get('current_user')
->id();
$store_entries = $temp_store
->get('entity_delete_multiple_confirm')
->get($account_id . ':comment');
$this
->assertSame([
$account_id => [
'en' => 'en',
],
], $store_entries);
}
public function testCommentUnpublishByKeyword() {
$this->comment
->save();
$action = Action::create([
'id' => 'comment_unpublish_by_keyword_action',
'label' => $this
->randomMachineName(),
'type' => 'comment',
'plugin' => 'comment_unpublish_by_keyword_action',
]);
$action
->execute([
$this->comment,
]);
$this
->assertTrue($this->comment
->isPublished(), 'The comment status was set to published.');
$action
->set('configuration', [
'keywords' => [
$this->keywords[0],
],
]);
$action
->execute([
$this->comment,
]);
$this
->assertFalse($this->comment
->isPublished(), 'The comment status was set to not published.');
$this->comment
->setPublished();
$action
->set('configuration', [
'keywords' => [
$this->keywords[1],
],
]);
$action
->execute([
$this->comment,
]);
$this
->assertFalse($this->comment
->isPublished(), 'The comment status was set to not published.');
}
}