function NavigationLinkBlockTest::testNavigationLinkBlockVisibility
Test block visibility based on the link access logic.
File
-
core/
modules/ navigation/ tests/ src/ Functional/ NavigationLinkBlockTest.php, line 114
Class
- NavigationLinkBlockTest
- Tests for \Drupal\navigation\Plugin\Block\NavigationLinkBlockTest.
Namespace
Drupal\Tests\navigation\FunctionalCode
public function testNavigationLinkBlockVisibility() : void {
// Add a link to an external URL.
$external_label = 'External Link Block';
$external_link_title = 'Link to example';
$this->appendNavigationLinkBlock($external_label, $external_link_title, 'http://example.com', 'external');
// Create an entity and create a link to it.
$entity_type_manager = \Drupal::entityTypeManager();
$entity_test_storage = $entity_type_manager->getStorage('entity_test');
$entity_test_link = $entity_test_storage->create([
'name' => 'test',
]);
$entity_test_link->save();
$entity_label = 'Entity Link BLock';
$entity_link_title = 'Link to entity';
$this->appendNavigationLinkBlock($entity_label, $entity_link_title, 'entity:entity_test/' . $entity_test_link->id(), 'entity');
// Link to admin page.
$admin_label = 'Admin Main Page';
$admin_link_title = 'Navigation Settings';
$this->appendNavigationLinkBlock($admin_label, $admin_link_title, 'internal:/admin/config/user-interface/navigation/settings', 'admin');
// Link to generic internal page (Help Link).
$help_label = 'Help Block';
$help_link_title = 'Link to help';
$this->appendNavigationLinkBlock($help_label, $help_link_title, 'internal:/admin/help', 'internal');
// Admin user should be capable to access to all the links but the internal
// one, since Help module is not enabled.
$test_page_url = Url::fromRoute('test_page_test.test_page');
$this->drupalLogin($this->adminUser);
$this->drupalGet($test_page_url);
$this->assertSession()
->pageTextContains($external_label);
$this->assertSession()
->pageTextContains($external_link_title);
$this->assertSession()
->pageTextContains($entity_label);
$this->assertSession()
->pageTextContains($entity_link_title);
$this->assertSession()
->pageTextContains($admin_label);
$this->assertSession()
->pageTextContains($admin_link_title);
$this->assertSession()
->pageTextNotContains($help_label);
$this->assertSession()
->pageTextNotContains($help_link_title);
// Normal user should not have access only to the external link.
$this->drupalLogin($this->normalUser);
$this->drupalGet($test_page_url);
$this->assertSession()
->pageTextContains($external_label);
$this->assertSession()
->pageTextContains($external_link_title);
$this->assertSession()
->pageTextNotContains($entity_label);
$this->assertSession()
->pageTextNotContains($entity_link_title);
$this->assertSession()
->pageTextNotContains($admin_label);
$this->assertSession()
->pageTextNotContains($admin_link_title);
$this->assertSession()
->pageTextNotContains($help_label);
$this->assertSession()
->pageTextNotContains($help_link_title);
// Enable Help module and grant permissions to admin user.
// Admin user should be capable to access to all the links
\Drupal::service('module_installer')->install([
'help',
]);
$this->adminUser
->addRole($this->drupalCreateRole([
'access help pages',
]))
->save();
$this->drupalLogin($this->adminUser);
$this->drupalGet($test_page_url);
$this->assertSession()
->pageTextContains($external_label);
$this->assertSession()
->pageTextContains($external_link_title);
$this->assertSession()
->pageTextContains($entity_label);
$this->assertSession()
->pageTextContains($entity_link_title);
$this->assertSession()
->pageTextContains($admin_label);
$this->assertSession()
->pageTextContains($admin_link_title);
$this->assertSession()
->pageTextContains($help_label);
$this->assertSession()
->pageTextContains($help_link_title);
// Normal user should not have access only to the external link.
$this->drupalLogin($this->normalUser);
$this->drupalGet($test_page_url);
$this->assertSession()
->pageTextContains($external_label);
$this->assertSession()
->pageTextContains($external_link_title);
$this->assertSession()
->pageTextNotContains($entity_label);
$this->assertSession()
->pageTextNotContains($entity_link_title);
$this->assertSession()
->pageTextNotContains($admin_label);
$this->assertSession()
->pageTextNotContains($admin_link_title);
$this->assertSession()
->pageTextNotContains($help_label);
$this->assertSession()
->pageTextNotContains($help_link_title);
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.