NodeAccessPagerTest.php 2.91 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Contains \Drupal\node\Tests\NodeAccessPagerTest.
6 7 8 9
 */

namespace Drupal\node\Tests;

10
use Drupal\comment\CommentInterface;
11
use Drupal\comment\Tests\CommentTestTrait;
12
use Drupal\simpletest\WebTestBase;
13
use Drupal\comment\Entity\Comment;
14 15

/**
16 17 18
 * Tests access controlled node views have the right amount of comment pages.
 *
 * @group node
19 20 21
 */
class NodeAccessPagerTest extends WebTestBase {

22 23
  use CommentTestTrait;

24 25 26 27 28 29 30
  /**
   * Modules to enable.
   *
   * @var array
   */
  public static $modules = array('node_access_test', 'comment', 'forum');

31
  protected function setUp() {
32 33
    parent::setUp();

34
    node_access_rebuild();
35
    $this->drupalCreateContentType(array('type' => 'page', 'name' => t('Basic page')));
36
    $this->addDefaultCommentField('node', 'page');
37
    $this->webUser = $this->drupalCreateUser(array('access content', 'access comments', 'node test view'));
38 39 40 41 42 43 44 45 46 47 48
  }

  /**
   * Tests the comment pager for nodes with multiple grants per realm.
   */
  public function testCommentPager() {
    // Create a node.
    $node = $this->drupalCreateNode();

    // Create 60 comments.
    for ($i = 0; $i < 60; $i++) {
49
      $comment = Comment::create(array(
50 51 52
        'entity_id' => $node->id(),
        'entity_type' => 'node',
        'field_name' => 'comment',
53
        'subject' => $this->randomMachineName(),
54
        'comment_body' => array(
55
          array('value' => $this->randomMachineName()),
56
        ),
57
        'status' => CommentInterface::PUBLISHED,
58 59 60 61
      ));
      $comment->save();
    }

62
    $this->drupalLogin($this->webUser);
63 64 65

    // View the node page. With the default 50 comments per page there should
    // be two pages (0, 1) but no third (2) page.
66
    $this->drupalGet('node/' . $node->id());
67 68 69 70
    $this->assertText($node->label());
    $this->assertText(t('Comments'));
    $this->assertRaw('page=1');
    $this->assertNoRaw('page=2');
71 72 73 74 75 76
  }

  /**
   * Tests the forum node pager for nodes with multiple grants per realm.
   */
  public function testForumPager() {
77
    // Look up the forums vocabulary ID.
78
    $vid = $this->config('forum.settings')->get('vocabulary');
79
    $this->assertTrue($vid, 'Forum navigation vocabulary ID is set.');
80

81
    // Look up the general discussion term.
82
    $tree = \Drupal::entityManager()->getStorage('taxonomy_term')->loadTree($vid, 0, 1);
83
    $tid = reset($tree)->tid;
84
    $this->assertTrue($tid, 'General discussion term is found in the forum vocabulary.');
85 86 87 88 89 90 91

    // Create 30 nodes.
    for ($i = 0; $i < 30; $i++) {
      $this->drupalCreateNode(array(
        'nid' => NULL,
        'type' => 'forum',
        'taxonomy_forums' => array(
92
          array('target_id' => $tid),
93 94 95 96 97 98
        ),
      ));
    }

    // View the general discussion forum page. With the default 25 nodes per
    // page there should be two pages for 30 nodes, no more.
99
    $this->drupalLogin($this->webUser);
100
    $this->drupalGet('forum/' . $tid);
101 102
    $this->assertRaw('page=1');
    $this->assertNoRaw('page=2');
103 104
  }
}