annotate core/modules/node/tests/src/Functional/NodeBlockFunctionalTest.php @ 19:fa3358dc1485 tip

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