View source
<?php
namespace Drupal\Tests\node\Functional;
use Drupal\block\Entity\Block;
use Drupal\Core\Database\Database;
use Drupal\Core\EventSubscriber\MainContentViewSubscriber;
use Drupal\Core\Url;
use Drupal\Tests\system\Functional\Cache\AssertPageCacheContextsAndTagsTrait;
use Drupal\user\RoleInterface;
class NodeBlockFunctionalTest extends NodeTestBase {
use AssertPageCacheContextsAndTagsTrait;
protected $defaultTheme = 'stark';
protected $adminUser;
protected $webUser;
protected static $modules = [
'block',
'views',
'node_block_test',
];
protected function setUp() : void {
parent::setUp();
$this->adminUser = $this
->drupalCreateUser([
'administer content types',
'administer nodes',
'bypass node access',
'administer blocks',
'access content overview',
]);
$this->webUser = $this
->drupalCreateUser([
'access content',
'create article content',
]);
}
public function testRecentNodeBlock() {
$this
->drupalLogin($this->adminUser);
user_role_change_permissions(RoleInterface::ANONYMOUS_ID, [
'access content' => FALSE,
]);
$block = $this
->drupalPlaceBlock('views_block:content_recent-block_1', [
'id' => 'test_block',
'items_per_page' => 2,
]);
$this
->drupalGet('');
$this
->assertSession()
->pageTextContains('No content available.');
$default_settings = [
'uid' => $this->webUser
->id(),
'type' => 'article',
];
$node1 = $this
->drupalCreateNode($default_settings);
$node2 = $this
->drupalCreateNode($default_settings);
$node3 = $this
->drupalCreateNode($default_settings);
$node1_revision_1 = $node1;
$node1
->setNewRevision(TRUE);
$node1
->setTitle('Node revision 2 title');
$node1
->save();
$connection = Database::getConnection();
$connection
->update('node_field_data')
->fields([
'changed' => $node1
->getChangedTime() + 100,
])
->condition('nid', $node2
->id())
->execute();
$connection
->update('node_field_data')
->fields([
'changed' => $node1
->getChangedTime() + 200,
])
->condition('nid', $node3
->id())
->execute();
$this
->drupalLogout();
$this
->drupalGet('');
$this
->assertSession()
->pageTextNotContains($block
->label());
$this
->drupalLogin($this->webUser);
$this
->assertSession()
->pageTextNotContains($node1
->label());
$this
->assertSession()
->pageTextContains($node2
->label());
$this
->assertSession()
->pageTextContains($node3
->label());
$this
->assertSession()
->elementExists('xpath', '//div[@id="block-test-block"]//div[@class="item-list"]/ul/li[1]/div/span/a[text() = "' . $node3
->label() . '"]');
$this
->drupalLogout();
$this
->drupalLogin($this->adminUser);
$block
->getPlugin()
->setConfigurationValue('items_per_page', 10);
$block
->save();
$node4 = $this
->drupalCreateNode($default_settings);
$this
->drupalGet('');
$this
->assertSession()
->pageTextContains($node1
->label());
$this
->assertSession()
->pageTextContains($node2
->label());
$this
->assertSession()
->pageTextContains($node3
->label());
$this
->assertSession()
->pageTextContains($node4
->label());
$this
->assertCacheContexts([
'languages:language_content',
'languages:language_interface',
'theme',
'url.query_args:' . MainContentViewSubscriber::WRAPPER_FORMAT,
'url.site',
'user',
]);
$theme = \Drupal::service('theme_handler')
->getDefault();
$this
->drupalGet("admin/structure/block/add/system_powered_by_block/{$theme}");
$this
->assertSession()
->pageTextContains('Content type');
$edit = [
'id' => strtolower($this
->randomMachineName()),
'region' => 'sidebar_first',
'visibility[entity_bundle:node][bundles][article]' => 'article',
];
$this
->submitForm($edit, 'Save block');
$block = Block::load($edit['id']);
$visibility = $block
->getVisibility();
$this
->assertTrue(isset($visibility['entity_bundle:node']['bundles']['article']), 'Visibility settings were saved to configuration');
$node5 = $this
->drupalCreateNode([
'uid' => $this->adminUser
->id(),
'type' => 'page',
]);
$this
->drupalLogout();
$this
->drupalLogin($this->webUser);
$this
->drupalGet('');
$label = $block
->label();
$this
->assertSession()
->pageTextNotContains($label);
$this
->assertCacheContexts([
'languages:language_content',
'languages:language_interface',
'theme',
'url.query_args:' . MainContentViewSubscriber::WRAPPER_FORMAT,
'url.site',
'user',
'route',
]);
$this
->assertSession()
->responseHeaderEquals('X-Drupal-Dynamic-Cache', 'HIT');
$this
->drupalGet('node/add/article');
$this
->assertSession()
->pageTextContains($label);
$this
->assertCacheContexts([
'languages:language_content',
'languages:language_interface',
'session',
'theme',
'url.path',
'url.query_args',
'user',
'route',
]);
$this
->assertSession()
->responseHeaderEquals('X-Drupal-Dynamic-Cache', 'UNCACHEABLE');
$this
->drupalGet('node/' . $node1
->id());
$this
->assertSession()
->pageTextContains($label);
$this
->assertCacheContexts([
'languages:language_content',
'languages:language_interface',
'theme',
'url.query_args:' . MainContentViewSubscriber::WRAPPER_FORMAT,
'url.site',
'user',
'route',
'timezone',
]);
$this
->assertSession()
->responseHeaderEquals('X-Drupal-Dynamic-Cache', 'MISS');
$this
->drupalGet('node/' . $node1
->id());
$this
->assertSession()
->responseHeaderEquals('X-Drupal-Dynamic-Cache', 'HIT');
$this
->drupalGet('node/' . $node5
->id());
$this
->assertSession()
->pageTextNotContains($label);
$this
->assertCacheContexts([
'languages:language_content',
'languages:language_interface',
'theme',
'url.query_args:' . MainContentViewSubscriber::WRAPPER_FORMAT,
'url.site',
'user',
'route',
'timezone',
]);
$this
->assertSession()
->responseHeaderEquals('X-Drupal-Dynamic-Cache', 'MISS');
$this
->drupalGet('node/' . $node5
->id());
$this
->assertSession()
->responseHeaderEquals('X-Drupal-Dynamic-Cache', 'HIT');
$this
->drupalPlaceBlock('node_block_test_context', [
'context_mapping' => [
'node' => '@node.node_route_context:node',
],
]);
$this
->drupalLogin($this->adminUser);
$this
->drupalGet('node/' . $node1
->id());
$this
->assertSession()
->pageTextContains($label);
$this
->assertSession()
->pageTextContains('Displaying node #' . $node1
->id() . ', revision #' . $node1
->getRevisionId() . ': Node revision 2 title');
$this
->drupalGet('node/' . $node1
->id() . '/edit');
$this
->submitForm([], 'Preview');
$this
->assertSession()
->pageTextContains($label);
$this
->assertSession()
->pageTextContains('Displaying node #' . $node1
->id() . ', revision #: Node revision 2 title');
$this
->drupalGet(Url::fromRoute('entity.node.revision', [
'node' => $node1
->id(),
'node_revision' => $node1_revision_1
->getRevisionId(),
]));
$this
->assertSession()
->pageTextContains($label);
$this
->assertSession()
->pageTextContains('Displaying node #' . $node1
->id() . ', revision #' . $node1_revision_1
->getRevisionId() . ': ' . $node1_revision_1
->label());
$this
->drupalGet(Url::fromRoute('entity.node.revision', [
'node' => $node1
->id(),
'node_revision' => $node1
->getRevisionId(),
]));
$this
->assertSession()
->pageTextContains($label);
$this
->assertSession()
->pageTextContains('Displaying node #' . $node1
->id() . ', revision #' . $node1
->getRevisionId() . ': Node revision 2 title');
$this
->drupalGet('admin/structure/block');
$this
->assertSession()
->pageTextContains($label);
$this
->assertSession()
->linkByHrefExists($block
->toUrl()
->toString());
}
}