NodeAccessPagerTest.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. namespace Drupal\Tests\node\Functional;
  3. use Drupal\comment\CommentInterface;
  4. use Drupal\comment\Tests\CommentTestTrait;
  5. use Drupal\comment\Entity\Comment;
  6. use Drupal\Tests\BrowserTestBase;
  7. /**
  8. * Tests access controlled node views have the right amount of comment pages.
  9. *
  10. * @group node
  11. */
  12. class NodeAccessPagerTest extends BrowserTestBase {
  13. use CommentTestTrait;
  14. /**
  15. * Modules to enable.
  16. *
  17. * @var array
  18. */
  19. public static $modules = ['node_access_test', 'comment', 'forum'];
  20. protected function setUp() {
  21. parent::setUp();
  22. node_access_rebuild();
  23. $this->drupalCreateContentType(['type' => 'page', 'name' => t('Basic page')]);
  24. $this->addDefaultCommentField('node', 'page');
  25. $this->webUser = $this->drupalCreateUser(['access content', 'access comments', 'node test view']);
  26. }
  27. /**
  28. * Tests the comment pager for nodes with multiple grants per realm.
  29. */
  30. public function testCommentPager() {
  31. // Create a node.
  32. $node = $this->drupalCreateNode();
  33. // Create 60 comments.
  34. for ($i = 0; $i < 60; $i++) {
  35. $comment = Comment::create([
  36. 'entity_id' => $node->id(),
  37. 'entity_type' => 'node',
  38. 'field_name' => 'comment',
  39. 'subject' => $this->randomMachineName(),
  40. 'comment_body' => [
  41. ['value' => $this->randomMachineName()],
  42. ],
  43. 'status' => CommentInterface::PUBLISHED,
  44. ]);
  45. $comment->save();
  46. }
  47. $this->drupalLogin($this->webUser);
  48. // View the node page. With the default 50 comments per page there should
  49. // be two pages (0, 1) but no third (2) page.
  50. $this->drupalGet('node/' . $node->id());
  51. $this->assertText($node->label());
  52. $this->assertText(t('Comments'));
  53. $this->assertRaw('page=1');
  54. $this->assertNoRaw('page=2');
  55. }
  56. /**
  57. * Tests the forum node pager for nodes with multiple grants per realm.
  58. */
  59. public function testForumPager() {
  60. // Look up the forums vocabulary ID.
  61. $vid = $this->config('forum.settings')->get('vocabulary');
  62. $this->assertTrue($vid, 'Forum navigation vocabulary ID is set.');
  63. // Look up the general discussion term.
  64. $tree = \Drupal::entityManager()->getStorage('taxonomy_term')->loadTree($vid, 0, 1);
  65. $tid = reset($tree)->tid;
  66. $this->assertTrue($tid, 'General discussion term is found in the forum vocabulary.');
  67. // Create 30 nodes.
  68. for ($i = 0; $i < 30; $i++) {
  69. $this->drupalCreateNode([
  70. 'nid' => NULL,
  71. 'type' => 'forum',
  72. 'taxonomy_forums' => [
  73. ['target_id' => $tid],
  74. ],
  75. ]);
  76. }
  77. // View the general discussion forum page. With the default 25 nodes per
  78. // page there should be two pages for 30 nodes, no more.
  79. $this->drupalLogin($this->webUser);
  80. $this->drupalGet('forum/' . $tid);
  81. $this->assertRaw('page=1');
  82. $this->assertNoRaw('page=2');
  83. }
  84. }