1<?php
2
3namespace Drupal\Tests\comment\Functional;
4
5use Drupal\comment\Entity\Comment;
6use Drupal\system\Entity\Action;
7
8/**
9 * Tests actions provided by the Comment module.
10 *
11 * @group comment
12 */
13class CommentActionsTest extends CommentTestBase {
14
15  /**
16   * Modules to install.
17   *
18   * @var array
19   */
20  public static $modules = ['dblog', 'action'];
21
22  /**
23   * {@inheritdoc}
24   */
25  protected $defaultTheme = 'stark';
26
27  /**
28   * Tests comment publish and unpublish actions.
29   */
30  public function testCommentPublishUnpublishActions() {
31    $this->drupalLogin($this->webUser);
32    $comment_text = $this->randomMachineName();
33    $subject = $this->randomMachineName();
34    $comment = $this->postComment($this->node, $comment_text, $subject);
35
36    // Unpublish a comment.
37    $action = Action::load('comment_unpublish_action');
38    $action->execute([$comment]);
39    $this->assertTrue($comment->isPublished() === FALSE, 'Comment was unpublished');
40    $this->assertArraySubset(['module' => ['comment']], $action->getDependencies());
41    // Publish a comment.
42    $action = Action::load('comment_publish_action');
43    $action->execute([$comment]);
44    $this->assertTrue($comment->isPublished() === TRUE, 'Comment was published');
45  }
46
47  /**
48   * Tests the unpublish comment by keyword action.
49   */
50  public function testCommentUnpublishByKeyword() {
51    $this->drupalLogin($this->adminUser);
52    $keyword_1 = $this->randomMachineName();
53    $keyword_2 = $this->randomMachineName();
54    $action = Action::create([
55      'id' => 'comment_unpublish_by_keyword_action',
56      'label' => $this->randomMachineName(),
57      'type' => 'comment',
58      'configuration' => [
59        'keywords' => [$keyword_1, $keyword_2],
60      ],
61      'plugin' => 'comment_unpublish_by_keyword_action',
62    ]);
63    $action->save();
64
65    $comment = $this->postComment($this->node, $keyword_2, $this->randomMachineName());
66
67    // Load the full comment so that status is available.
68    $comment = Comment::load($comment->id());
69
70    $this->assertTrue($comment->isPublished() === TRUE, 'The comment status was set to published.');
71
72    $action->execute([$comment]);
73    $this->assertTrue($comment->isPublished() === FALSE, 'The comment status was set to not published.');
74  }
75
76}
77