X-Git-Url: http://www.aleph1.co.uk/gitweb/?p=yaffs-website;a=blobdiff_plain;f=web%2Fcore%2Fmodules%2Fuser%2Ftests%2Fsrc%2FFunctional%2FViews%2FAccessRoleTest.php;fp=web%2Fcore%2Fmodules%2Fuser%2Ftests%2Fsrc%2FFunctional%2FViews%2FAccessRoleTest.php;h=0b8c14f329213c5b533334cfcee3fc0b4702aa1b;hp=0000000000000000000000000000000000000000;hb=af6d1fb995500ae68849458ee10d66abbdcfb252;hpb=680c79a86e3ed402f263faeac92e89fb6d9edcc0 diff --git a/web/core/modules/user/tests/src/Functional/Views/AccessRoleTest.php b/web/core/modules/user/tests/src/Functional/Views/AccessRoleTest.php new file mode 100644 index 000000000..0b8c14f32 --- /dev/null +++ b/web/core/modules/user/tests/src/Functional/Views/AccessRoleTest.php @@ -0,0 +1,140 @@ +getStorage('view')->load('test_access_role'); + $display = &$view->getDisplay('default'); + $display['display_options']['access']['options']['role'] = [ + $this->normalRole => $this->normalRole, + ]; + $view->save(); + $this->container->get('router.builder')->rebuildIfNeeded(); + $expected = [ + 'config' => ['user.role.' . $this->normalRole], + 'module' => ['user', 'views_test_data'], + ]; + $this->assertIdentical($expected, $view->calculateDependencies()->getDependencies()); + + $executable = Views::executableFactory()->get($view); + $executable->setDisplay('page_1'); + + $access_plugin = $executable->display_handler->getPlugin('access'); + $this->assertTrue($access_plugin instanceof Role, 'Make sure the right class got instantiated.'); + + // Test the access() method on the access plugin. + $this->assertFalse($executable->display_handler->access($this->webUser)); + $this->assertTrue($executable->display_handler->access($this->normalUser)); + + $this->drupalLogin($this->webUser); + $this->drupalGet('test-role'); + $this->assertResponse(403); + $this->assertCacheContext('user.roles'); + + $this->drupalLogin($this->normalUser); + $this->drupalGet('test-role'); + $this->assertResponse(200); + $this->assertCacheContext('user.roles'); + + // Test allowing multiple roles. + $view = Views::getView('test_access_role')->storage; + $display = &$view->getDisplay('default'); + $display['display_options']['access']['options']['role'] = [ + $this->normalRole => $this->normalRole, + 'anonymous' => 'anonymous', + ]; + $view->save(); + $this->container->get('router.builder')->rebuildIfNeeded(); + + // Ensure that the list of roles is sorted correctly, if the generated role + // ID comes before 'anonymous', see https://www.drupal.org/node/2398259. + $roles = ['user.role.anonymous', 'user.role.' . $this->normalRole]; + sort($roles); + $expected = [ + 'config' => $roles, + 'module' => ['user', 'views_test_data'], + ]; + $this->assertIdentical($expected, $view->calculateDependencies()->getDependencies()); + $this->drupalLogin($this->webUser); + $this->drupalGet('test-role'); + $this->assertResponse(403); + $this->assertCacheContext('user.roles'); + $this->drupalLogout(); + $this->drupalGet('test-role'); + $this->assertResponse(200); + $this->assertCacheContext('user.roles'); + $this->drupalLogin($this->normalUser); + $this->drupalGet('test-role'); + $this->assertResponse(200); + $this->assertCacheContext('user.roles'); + } + + /** + * Tests access on render caching. + */ + public function testRenderCaching() { + $view = Views::getView('test_access_role'); + $display = &$view->storage->getDisplay('default'); + $display['display_options']['cache'] = [ + 'type' => 'tag', + ]; + $display['display_options']['access']['options']['role'] = [ + $this->normalRole => $this->normalRole, + ]; + $view->save(); + + /** @var \Drupal\Core\Render\RendererInterface $renderer */ + $renderer = \Drupal::service('renderer'); + /** @var \Drupal\Core\Session\AccountSwitcherInterface $account_switcher */ + $account_switcher = \Drupal::service('account_switcher'); + + // First access as user with access. + $build = DisplayPluginBase::buildBasicRenderable('test_access_role', 'default'); + $account_switcher->switchTo($this->normalUser); + $result = $renderer->renderPlain($build); + $this->assertTrue(in_array('user.roles', $build['#cache']['contexts'])); + $this->assertEqual(['config:views.view.test_access_role'], $build['#cache']['tags']); + $this->assertEqual(Cache::PERMANENT, $build['#cache']['max-age']); + $this->assertNotEqual($result, ''); + + // Then without access. + $build = DisplayPluginBase::buildBasicRenderable('test_access_role', 'default'); + $account_switcher->switchTo($this->webUser); + $result = $renderer->renderPlain($build); + // @todo Fix this in https://www.drupal.org/node/2551037, + // DisplayPluginBase::applyDisplayCacheabilityMetadata() is not invoked when + // using buildBasicRenderable() and a Views access plugin returns FALSE. + // $this->assertTrue(in_array('user.roles', $build['#cache']['contexts'])); + // $this->assertEqual([], $build['#cache']['tags']); + $this->assertEqual(Cache::PERMANENT, $build['#cache']['max-age']); + $this->assertEqual($result, ''); + } + +}