Commit 9ba65aa2 authored by alexpott's avatar alexpott

Issue #2463659 by andypost, Wim Leers, rpayanm, Fabianx, googletorp:...

Issue #2463659 by andypost, Wim Leers, rpayanm, Fabianx, googletorp: Regression test coverage: integration test for an uncacheable menu link that depends on session data
parent 28088f38
......@@ -2,7 +2,7 @@
/**
* @file
* Contains \Drupal\system\Tests\Menu\MenuTranslateTest.
* Contains \Drupal\system\Tests\Menu\MenuAccessTest.
*/
namespace Drupal\system\Tests\Menu;
......@@ -11,12 +11,11 @@
use Drupal\simpletest\WebTestBase;
/**
* Tests the _menu_translate() method.
* Tests the route access checks on menu links.
*
* @group Menu
* @see _menu_translate().
*/
class MenuTranslateTest extends WebTestBase {
class MenuAccessTest extends WebTestBase {
/**
* Modules to enable.
......@@ -35,9 +34,23 @@ protected function setUp() {
}
/**
* Tests _menu_translate().
* Tests menu link for route with access check.
*
* @see \Drupal\menu_test\Access\AccessCheck::access()
*/
public function testMenuTranslate() {
public function testMenuBlockLinksAccessCheck() {
$this->drupalPlaceBlock('system_menu_block:account');
// Test that there's link rendered on the route.
$this->drupalGet('menu_test_access_check_session');
$this->assertLink('Test custom route access check');
// Page still accessible but thre should not be menu link.
$this->drupalGet('menu_test_access_check_session');
$this->assertResponse(200);
$this->assertNoLink('Test custom route access check');
// Test that page is no more accessible.
$this->drupalGet('menu_test_access_check_session');
$this->assertResponse(403);
// Check for access to a restricted local task from a default local task.
$this->drupalGet('foo/asdf');
$this->assertResponse(200);
......
......@@ -83,3 +83,8 @@ menu_test.child:
menu_test.unsafe:
route_name: menu_test.menu_name_test
deriver: '\Drupal\menu_test\Plugin\Derivative\MenuLinkTestWithUnsafeTitle'
menu_test.access_check:
title: 'Test custom route access check'
route_name: menu_test.router_test_session
menu_name: account
......@@ -74,6 +74,15 @@ menu_test.router_test4:
requirements:
_access: 'TRUE'
menu_test.router_test_session:
path: '/menu_test_access_check_session'
defaults:
_controller: '\Drupal\menu_test\TestControllers::testSession'
options:
no_cache: TRUE
requirements:
_menu_test_session_access: 'TRUE'
menu_test.local_action1:
path: '/menu-test-local-action'
defaults:
......
......@@ -3,3 +3,8 @@ services:
class: Drupal\menu_test\Theme\TestThemeNegotiator
tags:
- { name: theme_negotiator }
access_check.menu_test_session:
class: Drupal\menu_test\Access\AccessCheck
tags:
- { name: access_check, applies_to: _menu_test_session_access }
<?php
/**
* @file
* Contains \Drupal\menu_test\Access\AccessCheck.
*/
namespace Drupal\menu_test\Access;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Routing\Access\AccessInterface;
/**
* Checks access based on the 'menu_test' key in session.
*/
class AccessCheck implements AccessInterface {
/**
* Check to see if user accessed this page.
*
* @return \Drupal\Core\Access\AccessResultInterface
* The access result.
*/
public function access() {
if (!isset($_SESSION['menu_test'])) {
$result = AccessResult::allowed();
}
else {
$result = AccessResult::allowedIf($_SESSION['menu_test'] < 2);
}
return $result->setCacheMaxAge(0);
}
}
......@@ -36,6 +36,17 @@ public function test2() {
return ['#markup' => 'test2'];
}
/**
* Prints out test data.
*/
public function testSession() {
if (!isset($_SESSION['menu_test'])) {
$_SESSION['menu_test'] = 0;
}
$_SESSION['menu_test']++;
return ['#markup' => SafeMarkup::format('Session menu_test is @count', ['@count' => $_SESSION['menu_test']])];
}
/**
* Prints out test data.
*/
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment