View source
<?php
namespace Drupal\cacheflush_ui\Tests;
use Drupal\cacheflush\Controller\CacheflushApi;
use Drupal\Core\Routing\RouteBuilderInterface;
use Drupal\Tests\BrowserTestBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
class CacheFlushUICRUDAccessTest extends BrowserTestBase {
protected $defaultTheme = 'stark';
public static $modules = [
'cacheflush_ui',
];
protected $urls = [
'admin' => 'admin/structure/cacheflush',
'new' => 'admin/structure/cacheflush/add',
'clear' => 'admin/cacheflush/clear/',
'view' => 'cacheflush/',
'edit' => 'cacheflush/[ID]/edit',
'delete' => 'cacheflush/[ID]/delete',
];
protected $adminUser;
protected $loggedUser;
protected $interfaceUser;
protected $container;
protected $routerBuilder;
public function __construct(ContainerInterface $container, RouteBuilderInterface $router_builder) {
parent::__construct();
$this->container = $container;
$this->routerBuilder = $router_builder;
}
public static function create(ContainerInterface $container) {
return new static($container, $container
->get('router.builder'));
}
public function setUp() {
parent::setUp();
$role1 = [
'cacheflush clear cache',
'cacheflush administer',
'cacheflush clear any',
'cacheflush create new',
'cacheflush view any',
'cacheflush edit any',
'cacheflush delete any',
];
$this->adminUser = $this
->drupalCreateUser($role1);
$role2 = [
'cacheflush clear cache',
'cacheflush clear own',
'cacheflush create new',
'cacheflush view own',
'cacheflush edit own',
'cacheflush delete own',
];
$this->loggedUser = $this
->drupalCreateUser($role2);
$role3 = [
'cacheflush administer',
'cacheflush view own',
'cacheflush edit own',
'cacheflush delete own',
];
$this->interfaceUser = $this
->drupalCreateUser($role3);
$role4 = [
'cacheflush administer',
'cacheflush view any',
'cacheflush edit own',
];
$this->interfaceUser2 = $this
->drupalCreateUser($role4);
}
public function testAccess() {
$this
->createTestEntitys();
$this
->accessAnonymous();
$this
->accessLogged();
$this
->accessAdmin();
$this
->accessInterface();
$this
->accessInterface2();
}
public function accessAnonymous() {
$this
->cacheflushUrlAccess($this->urls['admin'], 403);
$this
->cacheflushUrlAccess($this->urls['new'], 403);
$this
->cacheflushUrlAccess($this->urls['clear'] . '1', 403);
$this
->cacheflushUrlAccess($this->urls['clear'] . '2', 403);
$this
->cacheflushUrlAccess($this->urls['view'] . '1', 403);
$this
->cacheflushUrlAccess(str_replace("[ID]", 1, $this->urls['edit']), 403);
$this
->cacheflushUrlAccess(str_replace("[ID]", 1, $this->urls['delete']), 403);
}
public function accessLogged() {
$this
->drupalLogin($this->loggedUser);
$this
->cacheflushUrlAccess($this->urls['admin'], 403);
$this
->cacheflushUrlAccess($this->urls['new'], 200);
$this
->cacheflushUrlAccess($this->urls['clear'] . '1', 200);
$this
->cacheflushUrlAccess($this->urls['clear'] . '2', 403);
$this
->cacheflushUrlAccess($this->urls['view'] . '1', 200);
$this
->cacheflushUrlAccess(str_replace("[ID]", 1, $this->urls['edit']), 200);
$this
->cacheflushUrlAccess(str_replace("[ID]", 1, $this->urls['delete']), 200);
$this
->cacheflushUrlAccess($this->urls['view'] . '2', 403);
$this
->cacheflushUrlAccess(str_replace("[ID]", 2, $this->urls['edit']), 403);
$this
->cacheflushUrlAccess(str_replace("[ID]", 2, $this->urls['delete']), 403);
$this
->drupalLogout();
}
public function accessAdmin() {
$this
->drupalLogin($this->adminUser);
$this
->cacheflushUrlAccess($this->urls['admin'], 200);
$this
->cacheflushUrlAccess($this->urls['new'], 200);
$this
->cacheflushUrlAccess($this->urls['clear'] . '1', 200);
$this
->cacheflushUrlAccess($this->urls['clear'] . '2', 200);
$this
->cacheflushUrlAccess($this->urls['view'] . '2', 200);
$this
->cacheflushUrlAccess(str_replace("[ID]", 2, $this->urls['edit']), 200);
$this
->cacheflushUrlAccess(str_replace("[ID]", 2, $this->urls['delete']), 200);
$this
->cacheflushUrlAccess($this->urls['view'] . '1', 200);
$this
->cacheflushUrlAccess(str_replace("[ID]", 1, $this->urls['edit']), 200);
$this
->cacheflushUrlAccess(str_replace("[ID]", 1, $this->urls['delete']), 200);
$this
->drupalGet('admin/structure/cacheflush');
$this
->assertRaw('LoggedUserEntity');
$this
->assertRaw('AdminUserEntity');
$this
->assertRaw('InterfaceUserEntity');
$this
->assertRaw('InterfaceUser2Entity');
$this
->assertLink('Edit', 3);
$this
->assertLink('Delete', 3);
$this
->drupalLogout();
}
public function accessInterface() {
$this
->drupalLogin($this->interfaceUser);
$this
->drupalGet('admin/structure/cacheflush');
$this
->assertNoRaw('LoggedUserEntity');
$this
->assertNoRaw('AdminUserEntity');
$this
->assertRaw('InterfaceUserEntity');
$this
->assertNoRaw('InterfaceUser2Entity');
$this
->assertLink('Edit', 0);
$this
->assertLink('Delete', 0);
$this
->drupalLogout();
}
public function accessInterface2() {
$this
->drupalLogin($this->interfaceUser2);
$this
->drupalGet('admin/structure/cacheflush');
$this
->assertRaw('LoggedUserEntity');
$this
->assertRaw('AdminUserEntity');
$this
->assertRaw('InterfaceUserEntity');
$this
->assertRaw('InterfaceUser2Entity');
$this
->assertLink('Edit', 0);
$this
->assertNoLink('Delete');
$this
->drupalLogout();
}
public function cacheflushUrlAccess($url, $code) {
$this
->drupalGet($url);
$this
->assertResponse($code);
}
public function createTestEntitys() {
$options = CacheflushApi::create($this->container)
->getOptionList();
$data = [];
$data['bootstrap']['functions'] = $options['bootstrap']['functions'];
$data['config']['functions'] = $options['config']['functions'];
$data['default']['functions'] = $options['default']['functions'];
$data = serialize($data);
$entity = cacheflush_create([
'title' => 'LoggedUserEntity',
'status' => 1,
'menu' => 1,
'data' => $data,
'uid' => $this->loggedUser
->id(),
]);
$entity
->save();
$entity = cacheflush_create([
'title' => 'AdminUserEntity',
'status' => 1,
'menu' => 1,
'data' => $data,
'uid' => $this->adminUser
->id(),
]);
$entity
->save();
$entity = cacheflush_create([
'title' => 'InterfaceUserEntity',
'status' => 1,
'menu' => 1,
'data' => $data,
'uid' => $this->interfaceUser
->id(),
]);
$entity
->save();
$entity = cacheflush_create([
'title' => 'InterfaceUser2Entity',
'status' => 1,
'menu' => 1,
'data' => $data,
'uid' => $this->interfaceUser2
->id(),
]);
$entity
->save();
$this->routerBuilder
->rebuild();
}
}