NodeBlockFunctionalTest.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. namespace Drupal\Tests\node\Functional;
  3. use Drupal\block\Entity\Block;
  4. use Drupal\Core\EventSubscriber\MainContentViewSubscriber;
  5. use Drupal\Tests\system\Functional\Cache\AssertPageCacheContextsAndTagsTrait;
  6. use Drupal\user\RoleInterface;
  7. /**
  8. * Tests node block functionality.
  9. *
  10. * @group node
  11. */
  12. class NodeBlockFunctionalTest extends NodeTestBase {
  13. use AssertPageCacheContextsAndTagsTrait;
  14. /**
  15. * An administrative user for testing.
  16. *
  17. * @var \Drupal\user\UserInterface
  18. */
  19. protected $adminUser;
  20. /**
  21. * An unprivileged user for testing.
  22. *
  23. * @var \Drupal\user\UserInterface
  24. */
  25. protected $webUser;
  26. /**
  27. * Modules to enable.
  28. *
  29. * @var array
  30. */
  31. public static $modules = ['block', 'views'];
  32. protected function setUp() {
  33. parent::setUp();
  34. // Create users and test node.
  35. $this->adminUser = $this->drupalCreateUser(['administer content types', 'administer nodes', 'administer blocks', 'access content overview']);
  36. $this->webUser = $this->drupalCreateUser(['access content', 'create article content']);
  37. }
  38. /**
  39. * Tests the recent comments block.
  40. */
  41. public function testRecentNodeBlock() {
  42. $this->drupalLogin($this->adminUser);
  43. // Disallow anonymous users to view content.
  44. user_role_change_permissions(RoleInterface::ANONYMOUS_ID, [
  45. 'access content' => FALSE,
  46. ]);
  47. // Enable the recent content block with two items.
  48. $block = $this->drupalPlaceBlock('views_block:content_recent-block_1', ['id' => 'test_block', 'items_per_page' => 2]);
  49. // Test that block is not visible without nodes.
  50. $this->drupalGet('');
  51. $this->assertText(t('No content available.'), 'Block with "No content available." found.');
  52. // Add some test nodes.
  53. $default_settings = ['uid' => $this->webUser->id(), 'type' => 'article'];
  54. $node1 = $this->drupalCreateNode($default_settings);
  55. $node2 = $this->drupalCreateNode($default_settings);
  56. $node3 = $this->drupalCreateNode($default_settings);
  57. // Change the changed time for node so that we can test ordering.
  58. db_update('node_field_data')
  59. ->fields([
  60. 'changed' => $node1->getChangedTime() + 100,
  61. ])
  62. ->condition('nid', $node2->id())
  63. ->execute();
  64. db_update('node_field_data')
  65. ->fields([
  66. 'changed' => $node1->getChangedTime() + 200,
  67. ])
  68. ->condition('nid', $node3->id())
  69. ->execute();
  70. // Test that a user without the 'access content' permission cannot
  71. // see the block.
  72. $this->drupalLogout();
  73. $this->drupalGet('');
  74. $this->assertNoText($block->label(), 'Block was not found.');
  75. // Test that only the 2 latest nodes are shown.
  76. $this->drupalLogin($this->webUser);
  77. $this->assertNoText($node1->label(), 'Node not found in block.');
  78. $this->assertText($node2->label(), 'Node found in block.');
  79. $this->assertText($node3->label(), 'Node found in block.');
  80. // Check to make sure nodes are in the right order.
  81. $this->assertTrue($this->xpath('//div[@id="block-test-block"]//div[@class="item-list"]/ul/li[1]/div/span/a[text() = "' . $node3->label() . '"]'), 'Nodes were ordered correctly in block.');
  82. $this->drupalLogout();
  83. $this->drupalLogin($this->adminUser);
  84. // Set the number of recent nodes to show to 10.
  85. $block->getPlugin()->setConfigurationValue('items_per_page', 10);
  86. $block->save();
  87. // Post an additional node.
  88. $node4 = $this->drupalCreateNode($default_settings);
  89. // Test that all four nodes are shown.
  90. $this->drupalGet('');
  91. $this->assertText($node1->label(), 'Node found in block.');
  92. $this->assertText($node2->label(), 'Node found in block.');
  93. $this->assertText($node3->label(), 'Node found in block.');
  94. $this->assertText($node4->label(), 'Node found in block.');
  95. $this->assertCacheContexts(['languages:language_content', 'languages:language_interface', 'theme', 'url.query_args:' . MainContentViewSubscriber::WRAPPER_FORMAT, 'user']);
  96. // Enable the "Powered by Drupal" block only on article nodes.
  97. $edit = [
  98. 'id' => strtolower($this->randomMachineName()),
  99. 'region' => 'sidebar_first',
  100. 'visibility[node_type][bundles][article]' => 'article',
  101. ];
  102. $theme = \Drupal::service('theme_handler')->getDefault();
  103. $this->drupalPostForm("admin/structure/block/add/system_powered_by_block/$theme", $edit, t('Save block'));
  104. $block = Block::load($edit['id']);
  105. $visibility = $block->getVisibility();
  106. $this->assertTrue(isset($visibility['node_type']['bundles']['article']), 'Visibility settings were saved to configuration');
  107. // Create a page node.
  108. $node5 = $this->drupalCreateNode(['uid' => $this->adminUser->id(), 'type' => 'page']);
  109. $this->drupalLogout();
  110. $this->drupalLogin($this->webUser);
  111. // Verify visibility rules.
  112. $this->drupalGet('');
  113. $label = $block->label();
  114. $this->assertNoText($label, 'Block was not displayed on the front page.');
  115. $this->assertCacheContexts(['languages:language_content', 'languages:language_interface', 'theme', 'url.query_args:' . MainContentViewSubscriber::WRAPPER_FORMAT, 'user', 'route']);
  116. // Ensure that a page that does not have a node context can still be cached,
  117. // the front page is the user page which is already cached from the login
  118. // request above.
  119. $this->assertSame('HIT', $this->getSession()->getResponseHeader('X-Drupal-Dynamic-Cache'));
  120. $this->drupalGet('node/add/article');
  121. $this->assertText($label, 'Block was displayed on the node/add/article page.');
  122. $this->assertCacheContexts(['languages:language_content', 'languages:language_interface', 'session', 'theme', 'url.path', 'url.query_args', 'user', 'route']);
  123. // The node/add/article page is an admin path and currently uncacheable.
  124. $this->assertSame('UNCACHEABLE', $this->getSession()->getResponseHeader('X-Drupal-Dynamic-Cache'));
  125. $this->drupalGet('node/' . $node1->id());
  126. $this->assertText($label, 'Block was displayed on the node/N when node is of type article.');
  127. $this->assertCacheContexts(['languages:language_content', 'languages:language_interface', 'theme', 'url.query_args:' . MainContentViewSubscriber::WRAPPER_FORMAT, 'user', 'route', 'timezone']);
  128. $this->assertSame('MISS', $this->getSession()->getResponseHeader('X-Drupal-Dynamic-Cache'));
  129. $this->drupalGet('node/' . $node1->id());
  130. $this->assertSame('HIT', $this->getSession()->getResponseHeader('X-Drupal-Dynamic-Cache'));
  131. $this->drupalGet('node/' . $node5->id());
  132. $this->assertNoText($label, 'Block was not displayed on nodes of type page.');
  133. $this->assertCacheContexts(['languages:language_content', 'languages:language_interface', 'theme', 'url.query_args:' . MainContentViewSubscriber::WRAPPER_FORMAT, 'user', 'route', 'timezone']);
  134. $this->assertSame('MISS', $this->getSession()->getResponseHeader('X-Drupal-Dynamic-Cache'));
  135. $this->drupalGet('node/' . $node5->id());
  136. $this->assertSame('HIT', $this->getSession()->getResponseHeader('X-Drupal-Dynamic-Cache'));
  137. $this->drupalLogin($this->adminUser);
  138. $this->drupalGet('admin/structure/block');
  139. $this->assertText($label, 'Block was displayed on the admin/structure/block page.');
  140. $this->assertLinkByHref($block->url());
  141. }
  142. }