7d3c0a4b3ddd5a2bfe89abf714d673c72caafd02
[yaffs-website] / web / core / modules / system / tests / src / Functional / Menu / MenuAccessTest.php
1 <?php
2
3 namespace Drupal\Tests\system\Functional\Menu;
4
5 use Drupal\Core\Url;
6 use Drupal\Tests\BrowserTestBase;
7
8 /**
9  * Tests the route access checks on menu links.
10  *
11  * @group Menu
12  */
13 class MenuAccessTest extends BrowserTestBase {
14
15   /**
16    * Modules to enable.
17    *
18    * @var array
19    */
20   public static $modules = ['block', 'menu_test'];
21
22   /**
23    * {@inheritdoc}
24    */
25   protected function setUp() {
26     parent::setUp();
27
28     $this->drupalPlaceBlock('local_tasks_block');
29   }
30
31   /**
32    * Tests menu link for route with access check.
33    *
34    * @see \Drupal\menu_test\Access\AccessCheck::access()
35    */
36   public function testMenuBlockLinksAccessCheck() {
37     $this->drupalPlaceBlock('system_menu_block:account');
38     // Test that there's link rendered on the route.
39     $this->drupalGet('menu_test_access_check_session');
40     $this->assertLink('Test custom route access check');
41     // Page is still accessible but there should be no menu link.
42     $this->drupalGet('menu_test_access_check_session');
43     $this->assertResponse(200);
44     $this->assertNoLink('Test custom route access check');
45     // Test that page is no more accessible.
46     $this->drupalGet('menu_test_access_check_session');
47     $this->assertResponse(403);
48
49     // Check for access to a restricted local task from a default local task.
50     $this->drupalGet('foo/asdf');
51     $this->assertResponse(200);
52     $this->assertLinkByHref('foo/asdf');
53     $this->assertLinkByHref('foo/asdf/b');
54     $this->assertNoLinkByHref('foo/asdf/c');
55
56     // Attempt to access a restricted local task.
57     $this->drupalGet('foo/asdf/c');
58     $this->assertResponse(403);
59     $elements = $this->xpath('//ul[@class=:class]/li/a[@href=:href]', [
60       ':class' => 'tabs primary',
61       ':href' => Url::fromRoute('menu_test.router_test1', ['bar' => 'asdf'])->toString(),
62     ]);
63     $this->assertTrue(empty($elements), 'No tab linking to foo/asdf found');
64     $this->assertNoLinkByHref('foo/asdf/b');
65     $this->assertNoLinkByHref('foo/asdf/c');
66   }
67
68 }