annotate core/modules/node/tests/src/Functional/NodeBlockFunctionalTest.php @ 6:875880e46745

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