c6f786df8e34f53bcb77858dfc90ee64ed136678
[yaffs-website] / web / core / modules / node / tests / src / Functional / NodeBlockFunctionalTest.php
1 <?php
2
3 namespace Drupal\Tests\node\Functional;
4
5 use Drupal\block\Entity\Block;
6 use Drupal\Core\EventSubscriber\MainContentViewSubscriber;
7 use Drupal\Tests\system\Functional\Cache\AssertPageCacheContextsAndTagsTrait;
8 use Drupal\user\RoleInterface;
9
10 /**
11  * Tests node block functionality.
12  *
13  * @group node
14  */
15 class NodeBlockFunctionalTest extends NodeTestBase {
16
17   use AssertPageCacheContextsAndTagsTrait;
18
19   /**
20    * An administrative user for testing.
21    *
22    * @var \Drupal\user\UserInterface
23    */
24   protected $adminUser;
25
26   /**
27    * An unprivileged user for testing.
28    *
29    * @var \Drupal\user\UserInterface
30    */
31   protected $webUser;
32
33   /**
34    * Modules to enable.
35    *
36    * @var array
37    */
38   public static $modules = ['block', 'views'];
39
40   protected function setUp() {
41     parent::setUp();
42
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']);
46   }
47
48   /**
49    * Tests the recent comments block.
50    */
51   public function testRecentNodeBlock() {
52     $this->drupalLogin($this->adminUser);
53
54     // Disallow anonymous users to view content.
55     user_role_change_permissions(RoleInterface::ANONYMOUS_ID, [
56       'access content' => FALSE,
57     ]);
58
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]);
61
62     // Test that block is not visible without nodes.
63     $this->drupalGet('');
64     $this->assertText(t('No content available.'), 'Block with "No content available." found.');
65
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);
71
72     // Change the changed time for node so that we can test ordering.
73     db_update('node_field_data')
74       ->fields([
75         'changed' => $node1->getChangedTime() + 100,
76       ])
77       ->condition('nid', $node2->id())
78       ->execute();
79     db_update('node_field_data')
80       ->fields([
81         'changed' => $node1->getChangedTime() + 200,
82       ])
83       ->condition('nid', $node3->id())
84       ->execute();
85
86     // Test that a user without the 'access content' permission cannot
87     // see the block.
88     $this->drupalLogout();
89     $this->drupalGet('');
90     $this->assertNoText($block->label(), 'Block was not found.');
91
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.');
97
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.');
100
101     $this->drupalLogout();
102     $this->drupalLogin($this->adminUser);
103
104     // Set the number of recent nodes to show to 10.
105     $block->getPlugin()->setConfigurationValue('items_per_page', 10);
106     $block->save();
107
108     // Post an additional node.
109     $node4 = $this->drupalCreateNode($default_settings);
110
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.');
117
118     $this->assertCacheContexts(['languages:language_content', 'languages:language_interface', 'theme', 'url.query_args:' . MainContentViewSubscriber::WRAPPER_FORMAT, 'user']);
119
120     // Enable the "Powered by Drupal" block only on article nodes.
121     $edit = [
122       'id' => strtolower($this->randomMachineName()),
123       'region' => 'sidebar_first',
124       'visibility[node_type][bundles][article]' => 'article',
125     ];
126     $theme = \Drupal::service('theme_handler')->getDefault();
127     $this->drupalPostForm("admin/structure/block/add/system_powered_by_block/$theme", $edit, t('Save block'));
128
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');
132
133     // Create a page node.
134     $node5 = $this->drupalCreateNode(['uid' => $this->adminUser->id(), 'type' => 'page']);
135
136     $this->drupalLogout();
137     $this->drupalLogin($this->webUser);
138
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
145     // Ensure that a page that does not have a node context can still be cached,
146     // the front page is the user page which is already cached from the login
147     // request above.
148     $this->assertSame('HIT', $this->getSession()->getResponseHeader('X-Drupal-Dynamic-Cache'));
149
150     $this->drupalGet('node/add/article');
151     $this->assertText($label, 'Block was displayed on the node/add/article page.');
152     $this->assertCacheContexts(['languages:language_content', 'languages:language_interface', 'session', 'theme', 'url.path', 'url.query_args', 'user', 'route']);
153
154     // The node/add/article page is an admin path and currently uncacheable.
155     $this->assertSame('UNCACHEABLE', $this->getSession()->getResponseHeader('X-Drupal-Dynamic-Cache'));
156
157     $this->drupalGet('node/' . $node1->id());
158     $this->assertText($label, 'Block was displayed on the node/N when node is of type article.');
159     $this->assertCacheContexts(['languages:language_content', 'languages:language_interface', 'theme', 'url.query_args:' . MainContentViewSubscriber::WRAPPER_FORMAT, 'user', 'route', 'timezone']);
160     $this->assertSame('MISS', $this->getSession()->getResponseHeader('X-Drupal-Dynamic-Cache'));
161     $this->drupalGet('node/' . $node1->id());
162     $this->assertSame('HIT', $this->getSession()->getResponseHeader('X-Drupal-Dynamic-Cache'));
163
164     $this->drupalGet('node/' . $node5->id());
165     $this->assertNoText($label, 'Block was not displayed on nodes of type page.');
166     $this->assertCacheContexts(['languages:language_content', 'languages:language_interface', 'theme', 'url.query_args:' . MainContentViewSubscriber::WRAPPER_FORMAT, 'user', 'route', 'timezone']);
167     $this->assertSame('MISS', $this->getSession()->getResponseHeader('X-Drupal-Dynamic-Cache'));
168     $this->drupalGet('node/' . $node5->id());
169     $this->assertSame('HIT', $this->getSession()->getResponseHeader('X-Drupal-Dynamic-Cache'));
170
171     $this->drupalLogin($this->adminUser);
172     $this->drupalGet('admin/structure/block');
173     $this->assertText($label, 'Block was displayed on the admin/structure/block page.');
174     $this->assertLinkByHref($block->url());
175   }
176
177 }