TrackerTest.php 9.3 KB
Newer Older
1
2
<?php

3
4
/**
 * @file
5
 * Definition of Drupal\tracker\Tests\TrackerTest.
6
7
 */

8
9
namespace Drupal\tracker\Tests;

10
use Drupal\simpletest\WebTestBase;
11

12
13
14
/**
 * Defines a base class for testing tracker.module.
 */
15
class TrackerTest extends WebTestBase {
16

17
18
19
20
21
  /**
   * Modules to enable.
   *
   * @var array
   */
22
  public static $modules = array('comment', 'tracker', 'history');
23

24
25
26
27
28
  /**
   * The main user for testing.
   *
   * @var object
   */
29
  protected $user;
30
31
32
33
34
35

  /**
   * A second user that will 'create' comments and nodes.
   *
   * @var object
   */
36
37
  protected $other_user;

38
  public static function getInfo() {
39
    return array(
40
      'name' => 'Tracker',
41
      'description' => 'Create and delete nodes and check for their display in the tracker listings.',
42
      'group' => 'Tracker'
43
44
45
46
    );
  }

  function setUp() {
47
    parent::setUp();
48

49
50
    $this->drupalCreateContentType(array('type' => 'page', 'name' => 'Basic page'));

51
    $permissions = array('access comments', 'create page content', 'post comments', 'skip comment approval');
52
53
    $this->user = $this->drupalCreateUser($permissions);
    $this->other_user = $this->drupalCreateUser($permissions);
54
55
56

    // Make node preview optional.
    variable_set('comment_preview_page', 0);
57
58
59
  }

  /**
60
   * Tests for the presence of nodes on the global tracker listing.
61
62
63
64
   */
  function testTrackerAll() {
    $this->drupalLogin($this->user);

65
    $unpublished = $this->drupalCreateNode(array(
66
      'title' => $this->randomName(8),
67
      'status' => 0,
68
69
    ));
    $published = $this->drupalCreateNode(array(
70
      'title' => $this->randomName(8),
71
72
      'status' => 1,
    ));
73
74

    $this->drupalGet('tracker');
75
76
77
    $this->assertNoText($unpublished->label(), 'Unpublished node do not show up in the tracker listing.');
    $this->assertText($published->label(), 'Published node show up in the tracker listing.');
    $this->assertLink(t('My recent content'), 0, 'User tab shows up on the global tracker page.');
78
79
80
81

    // Delete a node and ensure it no longer appears on the tracker.
    node_delete($published->nid);
    $this->drupalGet('tracker');
82
    $this->assertNoText($published->label(), 'Deleted node do not show up in the tracker listing.');
83
84
85
  }

  /**
86
   * Tests for the presence of nodes on a user's tracker listing.
87
88
89
90
   */
  function testTrackerUser() {
    $this->drupalLogin($this->user);

91
    $unpublished = $this->drupalCreateNode(array(
92
      'title' => $this->randomName(8),
93
      'uid' => $this->user->uid,
94
95
96
      'status' => 0,
    ));
    $my_published = $this->drupalCreateNode(array(
97
      'title' => $this->randomName(8),
98
      'uid' => $this->user->uid,
99
100
101
      'status' => 1,
    ));
    $other_published_no_comment = $this->drupalCreateNode(array(
102
      'title' => $this->randomName(8),
103
104
105
106
      'uid' => $this->other_user->uid,
      'status' => 1,
    ));
    $other_published_my_comment = $this->drupalCreateNode(array(
107
      'title' => $this->randomName(8),
108
109
110
111
112
      'uid' => $this->other_user->uid,
      'status' => 1,
    ));
    $comment = array(
      'subject' => $this->randomName(),
113
      'comment_body[' . LANGUAGE_NOT_SPECIFIED . '][0][value]' => $this->randomName(20),
114
    );
115
    $this->drupalPost('comment/reply/' . $other_published_my_comment->nid, $comment, t('Save'));
116
117

    $this->drupalGet('user/' . $this->user->uid . '/track');
118
119
120
121
    $this->assertNoText($unpublished->label(), "Unpublished nodes do not show up in the users's tracker listing.");
    $this->assertText($my_published->label(), "Published nodes show up in the user's tracker listing.");
    $this->assertNoText($other_published_no_comment->label(), "Other user's nodes do not show up in the user's tracker listing.");
    $this->assertText($other_published_my_comment->label(), "Nodes that the user has commented on appear in the user's tracker listing.");
122
123

    // Verify that unpublished comments are removed from the tracker.
124
    $admin_user = $this->drupalCreateUser(array('post comments', 'administer comments', 'access user profiles'));
125
126
127
    $this->drupalLogin($admin_user);
    $this->drupalPost('comment/1/edit', array('status' => COMMENT_NOT_PUBLISHED), t('Save'));
    $this->drupalGet('user/' . $this->user->uid . '/track');
128
    $this->assertNoText($other_published_my_comment->label(), 'Unpublished comments are not counted on the tracker listing.');
129
130
131
  }

  /**
132
   * Tests for the presence of the "new" flag for nodes.
133
134
135
136
137
   */
  function testTrackerNewNodes() {
    $this->drupalLogin($this->user);

    $edit = array(
138
      'title' => $this->randomName(8),
139
140
    );

141
    $node = $this->drupalCreateNode($edit);
142
    $title = $edit['title'];
143
    $this->drupalGet('tracker');
144
    $this->assertPattern('/' . $title . '.*new/', 'New nodes are flagged as such in the tracker listing.');
145
146
147

    $this->drupalGet('node/' . $node->nid);
    $this->drupalGet('tracker');
148
    $this->assertNoPattern('/' . $title . '.*new/', 'Visited nodes are not flagged as new.');
149
150
151

    $this->drupalLogin($this->other_user);
    $this->drupalGet('tracker');
152
    $this->assertPattern('/' . $title . '.*new/', 'For another user, new nodes are flagged as such in the tracker listing.');
153
154
155

    $this->drupalGet('node/' . $node->nid);
    $this->drupalGet('tracker');
156
    $this->assertNoPattern('/' . $title . '.*new/', 'For another user, visited nodes are not flagged as new.');
157
158
159
  }

  /**
160
   * Tests for comment counters on the tracker listing.
161
162
163
164
   */
  function testTrackerNewComments() {
    $this->drupalLogin($this->user);

165
    $node = $this->drupalCreateNode(array(
166
      'comment' => 2,
167
      'title' => $this->randomName(8),
168
    ));
169
170
171
172

    // Add a comment to the page.
    $comment = array(
      'subject' => $this->randomName(),
173
      'comment_body[' . LANGUAGE_NOT_SPECIFIED . '][0][value]' => $this->randomName(20),
174
    );
175
176
    // The new comment is automatically viewed by the current user.
    $this->drupalPost('comment/reply/' . $node->nid, $comment, t('Save'));
177
178
179

    $this->drupalLogin($this->other_user);
    $this->drupalGet('tracker');
180
    $this->assertText('1 new', 'New comments are counted on the tracker listing pages.');
181
182
183
184
185
    $this->drupalGet('node/' . $node->nid);

    // Add another comment as other_user.
    $comment = array(
      'subject' => $this->randomName(),
186
      'comment_body[' . LANGUAGE_NOT_SPECIFIED . '][0][value]' => $this->randomName(20),
187
    );
188
    // If the comment is posted in the same second as the last one then Drupal
189
    // can't tell the difference, so we wait one second here.
190
    sleep(1);
191
192
193
194
    $this->drupalPost('comment/reply/' . $node->nid, $comment, t('Save'));

    $this->drupalLogin($this->user);
    $this->drupalGet('tracker');
195
    $this->assertText('1 new', 'New comments are counted on the tracker listing pages.');
196
  }
197
198

  /**
199
   * Tests that existing nodes are indexed by cron.
200
201
202
203
204
205
206
207
208
209
   */
  function testTrackerCronIndexing() {
    $this->drupalLogin($this->user);

    // Create 3 nodes.
    $edits = array();
    $nodes = array();
    for ($i = 1; $i <= 3; $i++) {
      $edits[$i] = array(
        'comment' => 2,
210
        'title' => $this->randomName(),
211
212
213
214
215
216
217
218
      );
      $nodes[$i] = $this->drupalCreateNode($edits[$i]);
    }

    // Add a comment to the last node as other user.
    $this->drupalLogin($this->other_user);
    $comment = array(
      'subject' => $this->randomName(),
219
      'comment_body[' . LANGUAGE_NOT_SPECIFIED . '][0][value]' => $this->randomName(20),
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
    );
    $this->drupalPost('comment/reply/' . $nodes[3]->nid, $comment, t('Save'));

    // Start indexing backwards from node 3.
    variable_set('tracker_index_nid', 3);

    // Clear the current tracker tables and rebuild them.
    db_delete('tracker_node')
      ->execute();
    db_delete('tracker_user')
      ->execute();
    tracker_cron();

    $this->drupalLogin($this->user);

    // Fetch the user's tracker.
    $this->drupalGet('tracker/' . $this->user->uid);

    // Assert that all node titles are displayed.
    foreach ($nodes as $i => $node) {
240
      $this->assertText($node->label(), format_string('Node @i is displayed on the tracker listing pages.', array('@i' => $i)));
241
    }
242
243
    $this->assertText('1 new', 'New comment is counted on the tracker listing pages.');
    $this->assertText('updated', 'Node is listed as updated');
244
245
246
247
248
249

    // Fetch the site-wide tracker.
    $this->drupalGet('tracker');

    // Assert that all node titles are displayed.
    foreach ($nodes as $i => $node) {
250
      $this->assertText($node->label(), format_string('Node @i is displayed on the tracker listing pages.', array('@i' => $i)));
251
    }
252
    $this->assertText('1 new', 'New comment is counted on the tracker listing pages.');
253
254
255
  }

  /**
256
   * Tests that publish/unpublish works at admin/content/node.
257
258
   */
  function testTrackerAdminUnpublish() {
259
    $admin_user = $this->drupalCreateUser(array('access content overview', 'administer nodes', 'bypass node access'));
260
261
262
263
    $this->drupalLogin($admin_user);

    $node = $this->drupalCreateNode(array(
      'comment' => 2,
264
      'title' => $this->randomName(),
265
266
267
268
    ));

    // Assert that the node is displayed.
    $this->drupalGet('tracker');
269
    $this->assertText($node->label(), 'Node is displayed on the tracker listing pages.');
270
271
272
273
274
275
276
277
278

    // Unpublish the node and ensure that it's no longer displayed.
    $edit = array(
      'operation' => 'unpublish',
      'nodes[' . $node->nid . ']' => $node->nid,
    );
    $this->drupalPost('admin/content', $edit, t('Update'));

    $this->drupalGet('tracker');
279
    $this->assertText(t('No content available.'), 'Node is displayed on the tracker listing pages.');
280
  }
281
}