View source
<?php
namespace Drupal\node\Tests;
use Drupal\block\Entity\Block;
use Drupal\Core\EventSubscriber\MainContentViewSubscriber;
use Drupal\system\Tests\Cache\AssertPageCacheContextsAndTagsTrait;
use Drupal\user\RoleInterface;
class NodeBlockFunctionalTest extends NodeTestBase {
use AssertPageCacheContextsAndTagsTrait;
protected $adminUser;
protected $webUser;
public static $modules = array(
'block',
'views',
);
protected function setUp() {
parent::setUp();
$this->adminUser = $this
->drupalCreateUser(array(
'administer content types',
'administer nodes',
'administer blocks',
'access content overview',
));
$this->webUser = $this
->drupalCreateUser(array(
'access content',
'create article content',
));
}
public function testRecentNodeBlock() {
$this
->drupalLogin($this->adminUser);
user_role_change_permissions(RoleInterface::ANONYMOUS_ID, array(
'access content' => FALSE,
));
$block = $this
->drupalPlaceBlock('views_block:content_recent-block_1', array(
'id' => 'test_block',
'items_per_page' => 2,
));
$this
->drupalGet('');
$this
->assertText(t('No content available.'), 'Block with "No content available." found.');
$default_settings = array(
'uid' => $this->webUser
->id(),
'type' => 'article',
);
$node1 = $this
->drupalCreateNode($default_settings);
$node2 = $this
->drupalCreateNode($default_settings);
$node3 = $this
->drupalCreateNode($default_settings);
db_update('node_field_data')
->fields(array(
'changed' => $node1
->getChangedTime() + 100,
))
->condition('nid', $node2
->id())
->execute();
db_update('node_field_data')
->fields(array(
'changed' => $node1
->getChangedTime() + 200,
))
->condition('nid', $node3
->id())
->execute();
$this
->drupalLogout();
$this
->drupalGet('');
$this
->assertNoText($block
->label(), 'Block was not found.');
$this
->drupalLogin($this->webUser);
$this
->assertNoText($node1
->label(), 'Node not found in block.');
$this
->assertText($node2
->label(), 'Node found in block.');
$this
->assertText($node3
->label(), 'Node found in block.');
$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.');
$this
->drupalLogout();
$this
->drupalLogin($this->adminUser);
$block
->getPlugin()
->setConfigurationValue('items_per_page', 10);
$block
->save();
$node4 = $this
->drupalCreateNode($default_settings);
$this
->drupalGet('');
$this
->assertText($node1
->label(), 'Node found in block.');
$this
->assertText($node2
->label(), 'Node found in block.');
$this
->assertText($node3
->label(), 'Node found in block.');
$this
->assertText($node4
->label(), 'Node found in block.');
$this
->assertCacheContexts([
'languages:language_content',
'languages:language_interface',
'theme',
'url.query_args:' . MainContentViewSubscriber::WRAPPER_FORMAT,
'user',
]);
$edit = [
'id' => strtolower($this
->randomMachineName()),
'region' => 'sidebar_first',
'visibility[node_type][bundles][article]' => 'article',
];
$theme = \Drupal::service('theme_handler')
->getDefault();
$this
->drupalPostForm("admin/structure/block/add/system_powered_by_block/{$theme}", $edit, t('Save block'));
$block = Block::load($edit['id']);
$visibility = $block
->getVisibility();
$this
->assertTrue(isset($visibility['node_type']['bundles']['article']), 'Visibility settings were saved to configuration');
$node5 = $this
->drupalCreateNode(array(
'uid' => $this->adminUser
->id(),
'type' => 'page',
));
$this
->drupalLogout();
$this
->drupalLogin($this->webUser);
$this
->drupalGet('');
$label = $block
->label();
$this
->assertNoText($label, 'Block was not displayed on the front page.');
$this
->assertCacheContexts([
'languages:language_content',
'languages:language_interface',
'theme',
'url.query_args:' . MainContentViewSubscriber::WRAPPER_FORMAT,
'user',
'route',
]);
$this
->drupalGet('node/add/article');
$this
->assertText($label, 'Block was displayed on the node/add/article page.');
$this
->assertCacheContexts([
'languages:language_content',
'languages:language_interface',
'session',
'theme',
'url.path',
'url.query_args',
'user',
'route',
]);
$this
->drupalGet('node/' . $node1
->id());
$this
->assertText($label, 'Block was displayed on the node/N when node is of type article.');
$this
->assertCacheContexts([
'languages:language_content',
'languages:language_interface',
'theme',
'url.query_args:' . MainContentViewSubscriber::WRAPPER_FORMAT,
'user',
'route',
'timezone',
]);
$this
->drupalGet('node/' . $node5
->id());
$this
->assertNoText($label, 'Block was not displayed on nodes of type page.');
$this
->assertCacheContexts([
'languages:language_content',
'languages:language_interface',
'theme',
'url.query_args:' . MainContentViewSubscriber::WRAPPER_FORMAT,
'user',
'route',
'timezone',
]);
$this
->drupalLogin($this->adminUser);
$this
->drupalGet('admin/structure/block');
$this
->assertText($label, 'Block was displayed on the admin/structure/block page.');
$this
->assertLinkByHref($block
->url());
}
}