TrackerTest.php 15.6 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\comment\CommentInterface;
11
use Drupal\comment\Tests\CommentTestTrait;
12
use Drupal\Core\Cache\Cache;
13
use Drupal\field\Entity\FieldStorageConfig;
14
use Drupal\simpletest\WebTestBase;
15
use Drupal\system\Tests\Cache\AssertPageCacheContextsAndTagsTrait;
16

17
/**
18 19 20
 * Create and delete nodes and check for their display in the tracker listings.
 *
 * @group tracker
21
 */
22
class TrackerTest extends WebTestBase {
23

24
  use CommentTestTrait;
25
  use AssertPageCacheContextsAndTagsTrait;
26

27 28 29 30 31
  /**
   * Modules to enable.
   *
   * @var array
   */
32
  public static $modules = array('comment', 'tracker', 'history', 'node_test');
33

34 35 36
  /**
   * The main user for testing.
   *
37
   * @var \Drupal\user\UserInterface
38
   */
39
  protected $user;
40 41 42 43

  /**
   * A second user that will 'create' comments and nodes.
   *
44
   * @var \Drupal\user\UserInterface
45
   */
46
  protected $otherUser;
47

48
  protected function setUp() {
49
    parent::setUp();
50

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

53
    $permissions = array('access comments', 'create page content', 'post comments', 'skip comment approval');
54
    $this->user = $this->drupalCreateUser($permissions);
55
    $this->otherUser = $this->drupalCreateUser($permissions);
56
    $this->addDefaultCommentField('node', 'page');
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->randomMachineName(8),
67
      'status' => 0,
68 69
    ));
    $published = $this->drupalCreateNode(array(
70
      'title' => $this->randomMachineName(8),
71 72
      'status' => 1,
    ));
73

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

79 80 81 82 83
    // Assert cache contexts, specifically the pager and node access contexts.
    $this->assertCacheContexts(['languages:language_interface', 'theme', 'url.query_args.pagers:0', 'user.node_grants:view', 'user.permissions']);
    // Assert cache tags for the visible node and node list cache tag.
    $this->assertCacheTags(Cache::mergeTags($published->getCacheTags(), $published->getOwner()->getCacheTags(), ['node_list', 'rendered']));

84
    // Delete a node and ensure it no longer appears on the tracker.
85
    $published->delete();
86
    $this->drupalGet('activity');
87
    $this->assertNoText($published->label(), 'Deleted node does not show up in the tracker listing.');
88 89 90 91 92 93 94 95 96 97 98 99 100 101

    // Test proper display of time on activity page when comments are disabled.
    // Disable comments.
    FieldStorageConfig::loadByName('node', 'comment')->delete();
    $node = $this->drupalCreateNode([
      // This title is required to trigger the custom changed time set in the
      // node_test module. This is needed in order to ensure a sufficiently
      // large 'time ago' interval that isn't numbered in seconds.
      'title' => 'testing_node_presave',
      'status' => 1,
    ]);

    $this->drupalGet('activity');
    $this->assertText($node->label(), 'Published node shows up in the tracker listing.');
102
    $this->assertText(\Drupal::service('date.formatter')->formatTimeDiffSince($node->getChangedTime()), 'The changed time was displayed on the tracker listing.');
103 104 105
  }

  /**
106
   * Tests for the presence of nodes on a user's tracker listing.
107 108 109 110
   */
  function testTrackerUser() {
    $this->drupalLogin($this->user);

111
    $unpublished = $this->drupalCreateNode(array(
112
      'title' => $this->randomMachineName(8),
113
      'uid' => $this->user->id(),
114 115 116
      'status' => 0,
    ));
    $my_published = $this->drupalCreateNode(array(
117
      'title' => $this->randomMachineName(8),
118
      'uid' => $this->user->id(),
119 120 121
      'status' => 1,
    ));
    $other_published_no_comment = $this->drupalCreateNode(array(
122
      'title' => $this->randomMachineName(8),
123
      'uid' => $this->otherUser->id(),
124 125 126
      'status' => 1,
    ));
    $other_published_my_comment = $this->drupalCreateNode(array(
127
      'title' => $this->randomMachineName(8),
128
      'uid' => $this->otherUser->id(),
129 130 131
      'status' => 1,
    ));
    $comment = array(
132 133
      'subject[0][value]' => $this->randomMachineName(),
      'comment_body[0][value]' => $this->randomMachineName(20),
134
    );
135
    $this->drupalPostForm('comment/reply/node/' . $other_published_my_comment->id() . '/comment', $comment, t('Save'));
136

137
    $this->drupalGet('user/' . $this->user->id() . '/activity');
138
    $this->assertNoText($unpublished->label(), "Unpublished nodes do not show up in the user's tracker listing.");
139
    $this->assertText($my_published->label(), "Published nodes show up in the user's tracker listing.");
140
    $this->assertNoText($other_published_no_comment->label(), "Another user's nodes do not show up in the user's tracker listing.");
141
    $this->assertText($other_published_my_comment->label(), "Nodes that the user has commented on appear in the user's tracker listing.");
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156

    // Assert cache contexts; the node grant context is not directly visible due
    // to it being implied by the user context.
    $this->assertCacheContexts(['languages:language_interface', 'theme', 'url.query_args.pagers:0', 'user']);
    // Assert cache tags for the visible nodes (including owners) and node list
    // cache tag.
    $tags = Cache::mergeTags(
      $my_published->getCacheTags(),
      $my_published->getOwner()->getCacheTags(),
      $other_published_my_comment->getCacheTags(),
      $other_published_my_comment->getOwner()->getCacheTags(),
      ['node_list', 'rendered']
    );
    $this->assertCacheTags($tags);

157 158
    $this->assertLink($my_published->label());
    $this->assertNoLink($unpublished->label());
159
    // Verify that title and tab title have been set correctly.
160
    $this->assertText('Activity', 'The user activity tab has the name "Activity".');
161
    $this->assertTitle(t('@name | @site', array('@name' => $this->user->getUsername(), '@site' => $this->config('system.site')->get('name'))), 'The user tracker page has the correct page title.');
162 163

    // Verify that unpublished comments are removed from the tracker.
164
    $admin_user = $this->drupalCreateUser(array('post comments', 'administer comments', 'access user profiles'));
165
    $this->drupalLogin($admin_user);
166
    $this->drupalPostForm('comment/1/edit', array('status' => CommentInterface::NOT_PUBLISHED), t('Save'));
167
    $this->drupalGet('user/' . $this->user->id() . '/activity');
168
    $this->assertNoText($other_published_my_comment->label(), 'Unpublished comments are not counted on the tracker listing.');
169 170 171
  }

  /**
172
   * Tests for the presence of the "new" flag for nodes.
173 174 175 176 177
   */
  function testTrackerNewNodes() {
    $this->drupalLogin($this->user);

    $edit = array(
178
      'title' => $this->randomMachineName(8),
179 180
    );

181
    $node = $this->drupalCreateNode($edit);
182
    $title = $edit['title'];
183 184
    $this->drupalGet('activity');
    $this->assertPattern('/' . $title . '.*new/', 'New nodes are flagged as such in the activity listing.');
185

186
    $this->drupalGet('node/' . $node->id());
187
    // Simulate the JavaScript on the node page to mark the node as read.
188 189
    // @todo Get rid of curlExec() once https://www.drupal.org/node/2074037
    //   lands.
190
    $this->curlExec(array(
191
      CURLOPT_URL => \Drupal::url('history.read_node', ['node' => $node->id()], array('absolute' => TRUE)),
192 193 194 195
      CURLOPT_HTTPHEADER => array(
        'Accept: application/json',
      ),
    ));
196
    $this->drupalGet('activity');
197
    $this->assertNoPattern('/' . $title . '.*new/', 'Visited nodes are not flagged as new.');
198

199
    $this->drupalLogin($this->otherUser);
200
    $this->drupalGet('activity');
201
    $this->assertPattern('/' . $title . '.*new/', 'For another user, new nodes are flagged as such in the tracker listing.');
202

203
    $this->drupalGet('node/' . $node->id());
204
    // Simulate the JavaScript on the node page to mark the node as read.
205 206
    // @todo Get rid of curlExec() once https://www.drupal.org/node/2074037
    //   lands.
207
    $this->curlExec(array(
208
      CURLOPT_URL => \Drupal::url('history.read_node', ['node' => $node->id()], array('absolute' => TRUE)),
209 210 211 212
      CURLOPT_HTTPHEADER => array(
        'Accept: application/json',
      ),
    ));
213
    $this->drupalGet('activity');
214
    $this->assertNoPattern('/' . $title . '.*new/', 'For another user, visited nodes are not flagged as new.');
215 216 217
  }

  /**
218
   * Tests for comment counters on the tracker listing.
219 220 221 222
   */
  function testTrackerNewComments() {
    $this->drupalLogin($this->user);

223
    $node = $this->drupalCreateNode(array(
224
      'title' => $this->randomMachineName(8),
225
    ));
226 227 228

    // Add a comment to the page.
    $comment = array(
229 230
      'subject[0][value]' => $this->randomMachineName(),
      'comment_body[0][value]' => $this->randomMachineName(20),
231
    );
232
    $this->drupalPostForm('comment/reply/node/' . $node->id() . '/comment', $comment, t('Save'));
233 234
    // The new comment is automatically viewed by the current user. Simulate the
    // JavaScript that does this.
235 236
    // @todo Get rid of curlExec() once https://www.drupal.org/node/2074037
    //   lands.
237
    $this->curlExec(array(
238
      CURLOPT_URL => \Drupal::url('history.read_node', ['node' => $node->id()], array('absolute' => TRUE)),
239 240 241 242
      CURLOPT_HTTPHEADER => array(
        'Accept: application/json',
      ),
    ));
243

244
    $this->drupalLogin($this->otherUser);
245
    $this->drupalGet('activity');
246
    $this->assertText('1 new', 'New comments are counted on the tracker listing pages.');
247
    $this->drupalGet('node/' . $node->id());
248

249
    // Add another comment as otherUser.
250
    $comment = array(
251 252
      'subject[0][value]' => $this->randomMachineName(),
      'comment_body[0][value]' => $this->randomMachineName(20),
253
    );
254
    // If the comment is posted in the same second as the last one then Drupal
255
    // can't tell the difference, so we wait one second here.
256
    sleep(1);
257
    $this->drupalPostForm('comment/reply/node/' . $node->id(). '/comment', $comment, t('Save'));
258 259

    $this->drupalLogin($this->user);
260
    $this->drupalGet('activity');
261
    $this->assertText('1 new', 'New comments are counted on the tracker listing pages.');
262
    $this->assertLink(t('1 new'));
263
  }
264

265 266 267 268 269 270 271
  /**
   * Tests for ordering on a users tracker listing when comments are posted.
   */
  function testTrackerOrderingNewComments() {
    $this->drupalLogin($this->user);

    $node_one = $this->drupalCreateNode(array(
272
      'title' => $this->randomMachineName(8),
273 274 275
    ));

    $node_two = $this->drupalCreateNode(array(
276
      'title' => $this->randomMachineName(8),
277 278
    ));

279 280
    // Now get otherUser to track these pieces of content.
    $this->drupalLogin($this->otherUser);
281 282 283

    // Add a comment to the first page.
    $comment = array(
284 285
      'subject[0][value]' => $this->randomMachineName(),
      'comment_body[0][value]' => $this->randomMachineName(20),
286 287 288 289 290 291 292 293 294
    );
    $this->drupalPostForm('comment/reply/node/' . $node_one->id() . '/comment', $comment, t('Save'));

    // If the comment is posted in the same second as the last one then Drupal
    // can't tell the difference, so we wait one second here.
    sleep(1);

    // Add a comment to the second page.
    $comment = array(
295 296
      'subject[0][value]' => $this->randomMachineName(),
      'comment_body[0][value]' => $this->randomMachineName(20),
297 298 299
    );
    $this->drupalPostForm('comment/reply/node/' . $node_two->id() . '/comment', $comment, t('Save'));

300
    // We should at this point have in our tracker for otherUser:
301 302 303 304 305 306 307 308 309 310 311 312 313 314
    // 1. node_two
    // 2. node_one
    // Because that's the reverse order of the posted comments.

    // Now we're going to post a comment to node_one which should jump it to the
    // top of the list.

    $this->drupalLogin($this->user);
    // If the comment is posted in the same second as the last one then Drupal
    // can't tell the difference, so we wait one second here.
    sleep(1);

    // Add a comment to the second page.
    $comment = array(
315 316
      'subject[0][value]' => $this->randomMachineName(),
      'comment_body[0][value]' => $this->randomMachineName(20),
317 318 319
    );
    $this->drupalPostForm('comment/reply/node/' . $node_one->id() . '/comment', $comment, t('Save'));

320 321 322
    // Switch back to the otherUser and assert that the order has swapped.
    $this->drupalLogin($this->otherUser);
    $this->drupalGet('user/' . $this->otherUser->id() . '/activity');
323 324 325 326 327 328 329 330
    // This is a cheeky way of asserting that the nodes are in the right order
    // on the tracker page.
    // It's almost certainly too brittle.
    $pattern = '/' . preg_quote($node_one->getTitle()) . '.+' . preg_quote($node_two->getTitle()) . '/s';
    $this->verbose($pattern);
    $this->assertPattern($pattern, 'Most recently commented on node appears at the top of tracker');
  }

331
  /**
332
   * Tests that existing nodes are indexed by cron.
333 334 335 336 337 338 339 340 341
   */
  function testTrackerCronIndexing() {
    $this->drupalLogin($this->user);

    // Create 3 nodes.
    $edits = array();
    $nodes = array();
    for ($i = 1; $i <= 3; $i++) {
      $edits[$i] = array(
342
        'title' => $this->randomMachineName(),
343 344 345 346 347
      );
      $nodes[$i] = $this->drupalCreateNode($edits[$i]);
    }

    // Add a comment to the last node as other user.
348
    $this->drupalLogin($this->otherUser);
349
    $comment = array(
350 351
      'subject[0][value]' => $this->randomMachineName(),
      'comment_body[0][value]' => $this->randomMachineName(20),
352
    );
353
    $this->drupalPostForm('comment/reply/node/' . $nodes[3]->id() . '/comment', $comment, t('Save'));
354 355

    // Start indexing backwards from node 3.
356
    \Drupal::state()->set('tracker.index_nid', 3);
357 358 359 360 361 362 363 364 365 366 367

    // 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.
368
    $this->drupalGet('activity/' . $this->user->id());
369 370 371

    // Assert that all node titles are displayed.
    foreach ($nodes as $i => $node) {
372
      $this->assertText($node->label(), format_string('Node @i is displayed on the tracker listing pages.', array('@i' => $i)));
373
    }
374
    $this->assertText('1 new', 'One new comment is counted on the tracker listing pages.');
375
    $this->assertText('updated', 'Node is listed as updated');
376 377

    // Fetch the site-wide tracker.
378
    $this->drupalGet('activity');
379 380 381

    // Assert that all node titles are displayed.
    foreach ($nodes as $i => $node) {
382
      $this->assertText($node->label(), format_string('Node @i is displayed on the tracker listing pages.', array('@i' => $i)));
383
    }
384
    $this->assertText('1 new', 'New comment is counted on the tracker listing pages.');
385 386 387
  }

  /**
388
   * Tests that publish/unpublish works at admin/content/node.
389 390
   */
  function testTrackerAdminUnpublish() {
391
    \Drupal::service('module_installer')->install(array('views'));
392
    \Drupal::service('router.builder')->rebuild();
393
    $admin_user = $this->drupalCreateUser(array('access content overview', 'administer nodes', 'bypass node access'));
394 395 396
    $this->drupalLogin($admin_user);

    $node = $this->drupalCreateNode(array(
397
      'title' => $this->randomMachineName(),
398 399 400
    ));

    // Assert that the node is displayed.
401
    $this->drupalGet('activity');
402
    $this->assertText($node->label(), 'A node is displayed on the tracker listing pages.');
403 404 405

    // Unpublish the node and ensure that it's no longer displayed.
    $edit = array(
406
      'action' => 'node_unpublish_action',
407
      'node_bulk_form[0]' => $node->id(),
408
    );
409
    $this->drupalPostForm('admin/content', $edit, t('Apply'));
410

411
    $this->drupalGet('activity');
412
    $this->assertText(t('No content available.'), 'A node is displayed on the tracker listing pages.');
413
  }
414
}