Skip to content
Snippets Groups Projects
Unverified Commit 41c24295 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #3042536 by claudiu.cristea, Lendude: Convert AccessPermissionTest into a Kernel test

parent 5aa09075
No related branches found
No related tags found
2 merge requests!7452Issue #1797438. HTML5 validation is preventing form submit and not fully...,!789Issue #3210310: Adjust Database API to remove deprecated Drupal 9 code in Drupal 10
<?php
namespace Drupal\Tests\user\Functional\Views;
namespace Drupal\Tests\user\Kernel\Views;
use Drupal\KernelTests\KernelTestBase;
use Drupal\Tests\user\Traits\UserCreationTrait;
use Drupal\user\Plugin\views\access\Permission;
use Drupal\views\Plugin\views\display\DisplayPluginBase;
use Drupal\views\Tests\ViewTestData;
use Drupal\views\Views;
/**
......@@ -12,15 +15,59 @@
* @group user
* @see \Drupal\user\Plugin\views\access\Permission
*/
class AccessPermissionTest extends AccessTestBase {
class AccessPermissionTest extends KernelTestBase {
use UserCreationTrait;
/**
* Views used by this test.
*
* @var array
* {@inheritdoc}
*/
protected static $modules = [
'node',
'system',
'user',
'user_test_views',
'views',
'views_test_data',
];
/**
* {@inheritdoc}
*/
public static $testViews = ['test_access_perm'];
/**
* A user with no special permissions.
*
* @var \Drupal\user\UserInterface
*/
protected $webUser;
/**
* A user with 'views_test_data test permission' permission.
*
* @var \Drupal\user\UserInterface
*/
protected $normalUser;
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->installSchema('system', ['sequences']);
$this->installEntitySchema('user');
$this->installEntitySchema('node');
// Create first UID1 so, the other users are not super-admin.
$this->createUser([], NULL, FALSE, ['uid' => 1]);
$this->webUser = $this->createUser();
$this->normalUser = $this->createUser(['views_test_data test permission']);
ViewTestData::createTestViews(get_class($this), ['user_test_views']);
}
/**
* Tests perm access plugin.
*/
......@@ -29,8 +76,8 @@ public function testAccessPerm() {
$view->setDisplay();
$access_plugin = $view->display_handler->getPlugin('access');
$this->assertTrue($access_plugin instanceof Permission, 'Make sure the right class got instantiated.');
$this->assertEqual($access_plugin->pluginTitle(), t('Permission'));
$this->assertInstanceOf(Permission::class, $access_plugin);
$this->assertEquals('Permission', $access_plugin->pluginTitle());
$this->assertFalse($view->display_handler->access($this->webUser));
$this->assertTrue($view->display_handler->access($this->normalUser));
......@@ -46,22 +93,18 @@ public function testRenderCaching() {
'type' => 'tag',
];
/** @var \Drupal\Core\Render\RendererInterface $renderer */
$renderer = \Drupal::service('renderer');
/** @var \Drupal\Core\Session\AccountSwitcherInterface $account_switcher */
$account_switcher = \Drupal::service('account_switcher');
$renderer = $this->container->get('renderer');
$account_switcher = $this->container->get('account_switcher');
// First access as user without access.
$build = DisplayPluginBase::buildBasicRenderable('test_access_perm', 'default');
$account_switcher->switchTo($this->normalUser);
$result = $renderer->renderPlain($build);
$this->assertNotEqual($result, '');
$account_switcher->switchTo($this->webUser);
$this->assertEmpty($renderer->renderPlain($build));
// Then with access.
$build = DisplayPluginBase::buildBasicRenderable('test_access_perm', 'default');
$account_switcher->switchTo($this->webUser);
$result = $renderer->renderPlain($build);
$this->assertEqual($result, '');
$account_switcher->switchTo($this->normalUser);
$this->assertNotEmpty($renderer->renderPlain($build));
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment