3 namespace Drupal\Tests\node\Functional;
5 use Drupal\block\Entity\Block;
6 use Drupal\Core\EventSubscriber\MainContentViewSubscriber;
7 use Drupal\system\Tests\Cache\AssertPageCacheContextsAndTagsTrait;
8 use Drupal\user\RoleInterface;
11 * Tests node block functionality.
15 class NodeBlockFunctionalTest extends NodeTestBase {
17 use AssertPageCacheContextsAndTagsTrait;
20 * An administrative user for testing.
22 * @var \Drupal\user\UserInterface
27 * An unprivileged user for testing.
29 * @var \Drupal\user\UserInterface
38 public static $modules = ['block', 'views'];
40 protected function setUp() {
43 // Create users and test node.
44 $this->adminUser = $this->drupalCreateUser(['administer content types', 'administer nodes', 'administer blocks', 'access content overview']);
45 $this->webUser = $this->drupalCreateUser(['access content', 'create article content']);
49 * Tests the recent comments block.
51 public function testRecentNodeBlock() {
52 $this->drupalLogin($this->adminUser);
54 // Disallow anonymous users to view content.
55 user_role_change_permissions(RoleInterface::ANONYMOUS_ID, [
56 'access content' => FALSE,
59 // Enable the recent content block with two items.
60 $block = $this->drupalPlaceBlock('views_block:content_recent-block_1', ['id' => 'test_block', 'items_per_page' => 2]);
62 // Test that block is not visible without nodes.
64 $this->assertText(t('No content available.'), 'Block with "No content available." found.');
66 // Add some test nodes.
67 $default_settings = ['uid' => $this->webUser->id(), 'type' => 'article'];
68 $node1 = $this->drupalCreateNode($default_settings);
69 $node2 = $this->drupalCreateNode($default_settings);
70 $node3 = $this->drupalCreateNode($default_settings);
72 // Change the changed time for node so that we can test ordering.
73 db_update('node_field_data')
75 'changed' => $node1->getChangedTime() + 100,
77 ->condition('nid', $node2->id())
79 db_update('node_field_data')
81 'changed' => $node1->getChangedTime() + 200,
83 ->condition('nid', $node3->id())
86 // Test that a user without the 'access content' permission cannot
88 $this->drupalLogout();
90 $this->assertNoText($block->label(), 'Block was not found.');
92 // Test that only the 2 latest nodes are shown.
93 $this->drupalLogin($this->webUser);
94 $this->assertNoText($node1->label(), 'Node not found in block.');
95 $this->assertText($node2->label(), 'Node found in block.');
96 $this->assertText($node3->label(), 'Node found in block.');
98 // Check to make sure nodes are in the right order.
99 $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.');
101 $this->drupalLogout();
102 $this->drupalLogin($this->adminUser);
104 // Set the number of recent nodes to show to 10.
105 $block->getPlugin()->setConfigurationValue('items_per_page', 10);
108 // Post an additional node.
109 $node4 = $this->drupalCreateNode($default_settings);
111 // Test that all four nodes are shown.
112 $this->drupalGet('');
113 $this->assertText($node1->label(), 'Node found in block.');
114 $this->assertText($node2->label(), 'Node found in block.');
115 $this->assertText($node3->label(), 'Node found in block.');
116 $this->assertText($node4->label(), 'Node found in block.');
118 $this->assertCacheContexts(['languages:language_content', 'languages:language_interface', 'theme', 'url.query_args:' . MainContentViewSubscriber::WRAPPER_FORMAT, 'user']);
120 // Enable the "Powered by Drupal" block only on article nodes.
122 'id' => strtolower($this->randomMachineName()),
123 'region' => 'sidebar_first',
124 'visibility[node_type][bundles][article]' => 'article',
126 $theme = \Drupal::service('theme_handler')->getDefault();
127 $this->drupalPostForm("admin/structure/block/add/system_powered_by_block/$theme", $edit, t('Save block'));
129 $block = Block::load($edit['id']);
130 $visibility = $block->getVisibility();
131 $this->assertTrue(isset($visibility['node_type']['bundles']['article']), 'Visibility settings were saved to configuration');
133 // Create a page node.
134 $node5 = $this->drupalCreateNode(['uid' => $this->adminUser->id(), 'type' => 'page']);
136 $this->drupalLogout();
137 $this->drupalLogin($this->webUser);
139 // Verify visibility rules.
140 $this->drupalGet('');
141 $label = $block->label();
142 $this->assertNoText($label, 'Block was not displayed on the front page.');
143 $this->assertCacheContexts(['languages:language_content', 'languages:language_interface', 'theme', 'url.query_args:' . MainContentViewSubscriber::WRAPPER_FORMAT, 'user', 'route']);
144 $this->drupalGet('node/add/article');
145 $this->assertText($label, 'Block was displayed on the node/add/article page.');
146 $this->assertCacheContexts(['languages:language_content', 'languages:language_interface', 'session', 'theme', 'url.path', 'url.query_args', 'user', 'route']);
147 $this->drupalGet('node/' . $node1->id());
148 $this->assertText($label, 'Block was displayed on the node/N when node is of type article.');
149 $this->assertCacheContexts(['languages:language_content', 'languages:language_interface', 'theme', 'url.query_args:' . MainContentViewSubscriber::WRAPPER_FORMAT, 'user', 'route', 'timezone']);
150 $this->drupalGet('node/' . $node5->id());
151 $this->assertNoText($label, 'Block was not displayed on nodes of type page.');
152 $this->assertCacheContexts(['languages:language_content', 'languages:language_interface', 'theme', 'url.query_args:' . MainContentViewSubscriber::WRAPPER_FORMAT, 'user', 'route', 'timezone']);
154 $this->drupalLogin($this->adminUser);
155 $this->drupalGet('admin/structure/block');
156 $this->assertText($label, 'Block was displayed on the admin/structure/block page.');
157 $this->assertLinkByHref($block->url());