CommentActionsTest.php 3.95 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
<?php

/**
 * @file
 * Definition of Drupal\comment\Tests\CommentActionsTest.
 */

namespace Drupal\comment\Tests;

/**
 * Tests actions provided by the Comment module.
 */
class CommentActionsTest extends CommentTestBase {
14
15
16
17
18
19

  /**
   * Modules to enable.
   *
   * @var array
   */
20
  public static $modules = array('dblog', 'action');
21

22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
  public static function getInfo() {
    return array(
      'name' => 'Comment actions',
      'description' => 'Test actions provided by the comment module.',
      'group' => 'Comment',
    );
  }

  /**
   * Tests comment publish and unpublish actions.
   */
  function testCommentPublishUnpublishActions() {
    $this->drupalLogin($this->web_user);
    $comment_text = $this->randomName();
    $subject = $this->randomName();
    $comment = $this->postComment($this->node, $comment_text, $subject);

    // Unpublish a comment (direct form: doesn't actually save the comment).
    comment_unpublish_action($comment);
41
    $this->assertEqual($comment->status->value, COMMENT_NOT_PUBLISHED, 'Comment was unpublished');
42
    $this->assertWatchdogMessage('Unpublished comment %subject.', array('%subject' => $subject), 'Found watchdog message');
43
44
45
    $this->clearWatchdog();

    // Unpublish a comment (indirect form: modify the comment in the database).
46
47
    comment_unpublish_action(NULL, array('cid' => $comment->id()));
    $this->assertEqual(comment_load($comment->id())->status->value, COMMENT_NOT_PUBLISHED, 'Comment was unpublished');
48
    $this->assertWatchdogMessage('Unpublished comment %subject.', array('%subject' => $subject), 'Found watchdog message');
49
50
51

    // Publish a comment (direct form: doesn't actually save the comment).
    comment_publish_action($comment);
52
    $this->assertEqual($comment->status->value, COMMENT_PUBLISHED, 'Comment was published');
53
    $this->assertWatchdogMessage('Published comment %subject.', array('%subject' => $subject), 'Found watchdog message');
54
55
56
    $this->clearWatchdog();

    // Publish a comment (indirect form: modify the comment in the database).
57
58
    comment_publish_action(NULL, array('cid' => $comment->id()));
    $this->assertEqual(comment_load($comment->id())->status->value, COMMENT_PUBLISHED, 'Comment was published');
59
    $this->assertWatchdogMessage('Published comment %subject.', array('%subject' => $subject), 'Found watchdog message');
60
61
62
    $this->clearWatchdog();
  }

63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
  /**
   * Tests the unpublish comment by keyword action.
   */
  function testCommentUnpublishByKeyword() {
    $this->drupalLogin($this->admin_user);
    $keyword_1 = $this->randomName();
    $keyword_2 = $this->randomName();
    $aid = action_save('comment_unpublish_by_keyword_action', 'comment', array('keywords' => array($keyword_1, $keyword_2)), $this->randomName());

    $this->assertTrue(action_load($aid), 'The action could be loaded.');

    $comment = $this->postComment($this->node, $keyword_2, $this->randomName());

    // Load the full comment so that status is available.
    $comment = comment_load($comment->id());

    $this->assertTrue($comment->status->value == COMMENT_PUBLISHED, 'The comment status was set to published.');

    actions_do($aid, $comment, array());
    $this->assertTrue($comment->status->value == COMMENT_NOT_PUBLISHED, 'The comment status was set to not published.');
  }

85
86
87
88
89
90
91
92
93
94
95
96
  /**
   * Verifies that a watchdog message has been entered.
   *
   * @param $watchdog_message
   *   The watchdog message.
   * @param $variables
   *   The array of variables passed to watchdog().
   * @param $message
   *   The assertion message.
   */
  function assertWatchdogMessage($watchdog_message, $variables, $message) {
    $status = (bool) db_query_range("SELECT 1 FROM {watchdog} WHERE message = :message AND variables = :variables", 0, 1, array(':message' => $watchdog_message, ':variables' => serialize($variables)))->fetchField();
97
    return $this->assert($status, format_string('@message', array('@message'=> $message)));
98
99
100
101
102
103
104
105
106
  }

  /**
   * Clears watchdog.
   */
  function clearWatchdog() {
    db_truncate('watchdog')->execute();
  }
}