Chris@0: adminUser = $this->drupalCreateUser(['administer content types', 'administer nodes', 'administer blocks', 'access content overview']); Chris@0: $this->webUser = $this->drupalCreateUser(['access content', 'create article content']); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests the recent comments block. Chris@0: */ Chris@0: public function testRecentNodeBlock() { Chris@0: $this->drupalLogin($this->adminUser); Chris@0: Chris@0: // Disallow anonymous users to view content. Chris@0: user_role_change_permissions(RoleInterface::ANONYMOUS_ID, [ Chris@0: 'access content' => FALSE, Chris@0: ]); Chris@0: Chris@0: // Enable the recent content block with two items. Chris@0: $block = $this->drupalPlaceBlock('views_block:content_recent-block_1', ['id' => 'test_block', 'items_per_page' => 2]); Chris@0: Chris@0: // Test that block is not visible without nodes. Chris@0: $this->drupalGet(''); Chris@0: $this->assertText(t('No content available.'), 'Block with "No content available." found.'); Chris@0: Chris@0: // Add some test nodes. Chris@0: $default_settings = ['uid' => $this->webUser->id(), 'type' => 'article']; Chris@0: $node1 = $this->drupalCreateNode($default_settings); Chris@0: $node2 = $this->drupalCreateNode($default_settings); Chris@0: $node3 = $this->drupalCreateNode($default_settings); Chris@0: Chris@18: $connection = Database::getConnection(); Chris@0: // Change the changed time for node so that we can test ordering. Chris@18: $connection->update('node_field_data') Chris@0: ->fields([ Chris@0: 'changed' => $node1->getChangedTime() + 100, Chris@0: ]) Chris@0: ->condition('nid', $node2->id()) Chris@0: ->execute(); Chris@18: $connection->update('node_field_data') Chris@0: ->fields([ Chris@0: 'changed' => $node1->getChangedTime() + 200, Chris@0: ]) Chris@0: ->condition('nid', $node3->id()) Chris@0: ->execute(); Chris@0: Chris@0: // Test that a user without the 'access content' permission cannot Chris@0: // see the block. Chris@0: $this->drupalLogout(); Chris@0: $this->drupalGet(''); Chris@0: $this->assertNoText($block->label(), 'Block was not found.'); Chris@0: Chris@0: // Test that only the 2 latest nodes are shown. Chris@0: $this->drupalLogin($this->webUser); Chris@0: $this->assertNoText($node1->label(), 'Node not found in block.'); Chris@0: $this->assertText($node2->label(), 'Node found in block.'); Chris@0: $this->assertText($node3->label(), 'Node found in block.'); Chris@0: Chris@0: // Check to make sure nodes are in the right order. Chris@0: $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: Chris@0: $this->drupalLogout(); Chris@0: $this->drupalLogin($this->adminUser); Chris@0: Chris@0: // Set the number of recent nodes to show to 10. Chris@0: $block->getPlugin()->setConfigurationValue('items_per_page', 10); Chris@0: $block->save(); Chris@0: Chris@0: // Post an additional node. Chris@0: $node4 = $this->drupalCreateNode($default_settings); Chris@0: Chris@0: // Test that all four nodes are shown. Chris@0: $this->drupalGet(''); Chris@0: $this->assertText($node1->label(), 'Node found in block.'); Chris@0: $this->assertText($node2->label(), 'Node found in block.'); Chris@0: $this->assertText($node3->label(), 'Node found in block.'); Chris@0: $this->assertText($node4->label(), 'Node found in block.'); Chris@0: Chris@0: $this->assertCacheContexts(['languages:language_content', 'languages:language_interface', 'theme', 'url.query_args:' . MainContentViewSubscriber::WRAPPER_FORMAT, 'user']); Chris@0: Chris@0: // Enable the "Powered by Drupal" block only on article nodes. Chris@0: $edit = [ Chris@0: 'id' => strtolower($this->randomMachineName()), Chris@0: 'region' => 'sidebar_first', Chris@0: 'visibility[node_type][bundles][article]' => 'article', Chris@0: ]; Chris@0: $theme = \Drupal::service('theme_handler')->getDefault(); Chris@0: $this->drupalPostForm("admin/structure/block/add/system_powered_by_block/$theme", $edit, t('Save block')); Chris@0: Chris@0: $block = Block::load($edit['id']); Chris@0: $visibility = $block->getVisibility(); Chris@0: $this->assertTrue(isset($visibility['node_type']['bundles']['article']), 'Visibility settings were saved to configuration'); Chris@0: Chris@0: // Create a page node. Chris@0: $node5 = $this->drupalCreateNode(['uid' => $this->adminUser->id(), 'type' => 'page']); Chris@0: Chris@0: $this->drupalLogout(); Chris@0: $this->drupalLogin($this->webUser); Chris@0: Chris@0: // Verify visibility rules. Chris@0: $this->drupalGet(''); Chris@0: $label = $block->label(); Chris@0: $this->assertNoText($label, 'Block was not displayed on the front page.'); Chris@0: $this->assertCacheContexts(['languages:language_content', 'languages:language_interface', 'theme', 'url.query_args:' . MainContentViewSubscriber::WRAPPER_FORMAT, 'user', 'route']); Chris@17: Chris@17: // Ensure that a page that does not have a node context can still be cached, Chris@17: // the front page is the user page which is already cached from the login Chris@17: // request above. Chris@17: $this->assertSame('HIT', $this->getSession()->getResponseHeader('X-Drupal-Dynamic-Cache')); Chris@17: Chris@0: $this->drupalGet('node/add/article'); Chris@0: $this->assertText($label, 'Block was displayed on the node/add/article page.'); Chris@0: $this->assertCacheContexts(['languages:language_content', 'languages:language_interface', 'session', 'theme', 'url.path', 'url.query_args', 'user', 'route']); Chris@17: Chris@17: // The node/add/article page is an admin path and currently uncacheable. Chris@17: $this->assertSame('UNCACHEABLE', $this->getSession()->getResponseHeader('X-Drupal-Dynamic-Cache')); Chris@17: Chris@0: $this->drupalGet('node/' . $node1->id()); Chris@0: $this->assertText($label, 'Block was displayed on the node/N when node is of type article.'); Chris@18: $this->assertCacheContexts(['languages:language_content', 'languages:language_interface', 'theme', 'url.query_args:' . MainContentViewSubscriber::WRAPPER_FORMAT, 'url.site', 'user', 'route', 'timezone']); Chris@17: $this->assertSame('MISS', $this->getSession()->getResponseHeader('X-Drupal-Dynamic-Cache')); Chris@17: $this->drupalGet('node/' . $node1->id()); Chris@17: $this->assertSame('HIT', $this->getSession()->getResponseHeader('X-Drupal-Dynamic-Cache')); Chris@17: Chris@0: $this->drupalGet('node/' . $node5->id()); Chris@0: $this->assertNoText($label, 'Block was not displayed on nodes of type page.'); Chris@18: $this->assertCacheContexts(['languages:language_content', 'languages:language_interface', 'theme', 'url.query_args:' . MainContentViewSubscriber::WRAPPER_FORMAT, 'url.site', 'user', 'route', 'timezone']); Chris@17: $this->assertSame('MISS', $this->getSession()->getResponseHeader('X-Drupal-Dynamic-Cache')); Chris@17: $this->drupalGet('node/' . $node5->id()); Chris@17: $this->assertSame('HIT', $this->getSession()->getResponseHeader('X-Drupal-Dynamic-Cache')); Chris@0: Chris@0: $this->drupalLogin($this->adminUser); Chris@0: $this->drupalGet('admin/structure/block'); Chris@0: $this->assertText($label, 'Block was displayed on the admin/structure/block page.'); Chris@18: $this->assertLinkByHref($block->toUrl()->toString()); Chris@0: } Chris@0: Chris@0: }