1<?php
2
3namespace Drupal\Tests\comment\Kernel;
4
5use Drupal\comment\Entity\Comment;
6use Drupal\comment\Entity\CommentType;
7use Drupal\comment\Tests\CommentTestTrait;
8use Drupal\Core\Datetime\Entity\DateFormat;
9use Drupal\entity_test\Entity\EntityTest;
10use Drupal\filter\Entity\FilterFormat;
11use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
12use Drupal\system\Entity\Action;
13
14/**
15 * Tests actions provided by the Comment module.
16 *
17 * @group comment
18 */
19class CommentActionsTest extends EntityKernelTestBase {
20  use CommentTestTrait;
21
22  /**
23   * {@inheritdoc}
24   */
25  protected static $modules = ['comment', 'entity_test'];
26
27  /**
28   * Keywords used for testing.
29   *
30   * @var string[]
31   */
32  protected $keywords;
33
34  /**
35   * The comment entity.
36   *
37   * @var \Drupal\comment\CommentInterface
38   */
39  protected $comment;
40
41  /**
42   * {@inheritdoc}
43   */
44  protected function setUp(): void {
45    parent::setUp();
46    $this->installConfig(['user', 'comment']);
47    $this->installSchema('comment', ['comment_entity_statistics']);
48
49    // Create a comment type.
50    CommentType::create([
51      'id' => 'comment',
52      'label' => 'Default comments',
53      'description' => 'Default comment field',
54      'target_entity_type_id' => 'entity_test',
55    ])->save();
56    $this->addDefaultCommentField('entity_test', 'entity_test', 'comment');
57
58    // Setup date format to render comment date.
59    DateFormat::create([
60      'id' => 'fallback',
61      'pattern' => 'D, m/d/Y - H:i',
62    ])->save();
63
64    // Create format without filters to prevent filtering.
65    FilterFormat::create([
66      'format' => 'no_filters',
67      'filters' => [],
68    ])->save();
69
70    // Set current user to allow filters display comment body.
71    $this->drupalSetCurrentUser($this->drupalCreateUser());
72
73    $this->keywords = [$this->randomMachineName(), $this->randomMachineName()];
74
75    // Create a comment against a test entity.
76    $host = EntityTest::create();
77    $host->save();
78
79    $this->comment = Comment::create([
80      'entity_type' => 'entity_test',
81      'name' => $this->randomString(),
82      'hostname' => 'magic.example.com',
83      'mail' => 'tonythemagicalpony@example.com',
84      'subject' => $this->keywords[0],
85      'comment_body' => $this->keywords[1],
86      'entity_id' => $host->id(),
87      'comment_type' => 'comment',
88      'field_name' => 'comment',
89    ]);
90    $this->comment->get('comment_body')->format = 'no_filters';
91    $this->comment->setPublished();
92  }
93
94  /**
95   * Tests comment module's default config actions.
96   *
97   * @see \Drupal\Core\Entity\Form\DeleteMultipleForm::submitForm()
98   * @see \Drupal\Core\Action\Plugin\Action\DeleteAction
99   * @see \Drupal\Core\Action\Plugin\Action\Derivative\EntityDeleteActionDeriver
100   * @see \Drupal\Core\Action\Plugin\Action\PublishAction
101   * @see \Drupal\Core\Action\Plugin\Action\SaveAction
102   */
103  public function testCommentDefaultConfigActions() {
104    $this->assertTrue($this->comment->isNew());
105    $action = Action::load('comment_save_action');
106    $action->execute([$this->comment]);
107    $this->assertFalse($this->comment->isNew());
108
109    $this->assertTrue($this->comment->isPublished());
110    // Tests comment unpublish.
111    $action = Action::load('comment_unpublish_action');
112    $action->execute([$this->comment]);
113    $this->assertFalse($this->comment->isPublished(), 'Comment was unpublished');
114    $this->assertSame(['module' => ['comment']], $action->getDependencies());
115    // Tests comment publish.
116    $action = Action::load('comment_publish_action');
117    $action->execute([$this->comment]);
118    $this->assertTrue($this->comment->isPublished(), 'Comment was published');
119
120    $action = Action::load('comment_delete_action');
121    $action->execute([$this->comment]);
122    /** @var \Drupal\Core\TempStore\PrivateTempStoreFactory $temp_store */
123    $temp_store = $this->container->get('tempstore.private');
124    $account_id = $this->container->get('current_user')->id();
125    $store_entries = $temp_store->get('entity_delete_multiple_confirm')->get($account_id . ':comment');
126    $this->assertSame([$account_id => ['en' => 'en']], $store_entries);
127  }
128
129  /**
130   * Tests the unpublish comment by keyword action.
131   *
132   * @see \Drupal\comment\Plugin\Action\UnpublishByKeywordComment
133   */
134  public function testCommentUnpublishByKeyword() {
135    $this->comment->save();
136    $action = Action::create([
137      'id' => 'comment_unpublish_by_keyword_action',
138      'label' => $this->randomMachineName(),
139      'type' => 'comment',
140      'plugin' => 'comment_unpublish_by_keyword_action',
141    ]);
142
143    // Tests no keywords.
144    $action->execute([$this->comment]);
145    $this->assertTrue($this->comment->isPublished(), 'The comment status was set to published.');
146
147    // Tests keyword in subject.
148    $action->set('configuration', ['keywords' => [$this->keywords[0]]]);
149    $action->execute([$this->comment]);
150    $this->assertFalse($this->comment->isPublished(), 'The comment status was set to not published.');
151
152    // Tests keyword in comment body.
153    $this->comment->setPublished();
154    $action->set('configuration', ['keywords' => [$this->keywords[1]]]);
155    $action->execute([$this->comment]);
156    $this->assertFalse($this->comment->isPublished(), 'The comment status was set to not published.');
157  }
158
159}
160