View source
<?php
namespace Drupal\Tests\workspace\Functional;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\multiversion\Entity\WorkspaceInterface;
use Drupal\Tests\BrowserTestBase;
class WorkspacePermissionsTest extends BrowserTestBase {
use WorkspaceTestUtilities;
public static $modules = [
'workspace',
'multiversion',
];
public function testCreateWorkspace() {
$editor = $this
->drupalCreateUser([
'access administration pages',
'administer site configuration',
'create_workspace',
]);
$this
->drupalLogin($editor);
$session = $this
->getSession();
$this
->drupalGet('/admin/structure/workspace/add');
$this
->assertEquals(200, $session
->getStatusCode());
$page = $session
->getPage();
$page
->fillField('label', 'Bears');
$page
->fillField('machine_name', 'bears');
$page
->findButton(t('Save'))
->click();
$session
->getPage()
->hasContent('Bears (bears)');
$etm = \Drupal::service('entity_type.manager');
$entity_list = $etm
->getStorage('workspace')
->loadByProperties([
'label' => 'Bears',
]);
$bears = current($entity_list);
$this
->drupalGet("/admin/structure/workspace/{$bears->id()}/edit");
$this
->assertEquals(403, $session
->getStatusCode());
}
public function testEditOwnWorkspace() {
$permissions = [
'access administration pages',
'administer site configuration',
'create_workspace',
'edit_own_workspace',
];
$editor1 = $this
->drupalCreateUser($permissions);
$this
->drupalLogin($editor1);
$this
->createWorkspaceThroughUI('Bears', 'bears');
$bears = $this
->getOneWorkspaceByLabel('Bears');
$session = $this
->getSession();
$this
->drupalGet("/admin/structure/workspace/{$bears->id()}/edit");
$this
->assertEquals(200, $session
->getStatusCode());
$page = $session
->getPage();
$page
->fillField('label', 'Bears again');
$page
->fillField('machine_name', 'bears');
$page
->findButton(t('Save'))
->click();
$session
->getPage()
->hasContent('Bears again (bears)');
$editor2 = $this
->drupalCreateUser($permissions);
$this
->drupalLogin($editor2);
$session = $this
->getSession();
$this
->createWorkspaceThroughUI('Packers', 'packers');
$packers = $this
->getOneWorkspaceByLabel('Packers');
$this
->drupalGet("/admin/structure/workspace/{$packers->id()}/edit");
$this
->assertEquals(200, $session
->getStatusCode());
$this
->drupalGet("/admin/structure/workspace/{$bears->id()}/edit");
$this
->assertEquals(403, $session
->getStatusCode());
}
public function testEditAnyWorkspace() {
$permissions = [
'access administration pages',
'administer site configuration',
'create_workspace',
'edit_own_workspace',
];
$editor1 = $this
->drupalCreateUser($permissions);
$this
->drupalLogin($editor1);
$this
->createWorkspaceThroughUI('Bears', 'bears');
$bears = $this
->getOneWorkspaceByLabel('Bears');
$session = $this
->getSession();
$this
->drupalGet("/admin/structure/workspace/{$bears->id()}/edit");
$this
->assertEquals(200, $session
->getStatusCode());
$page = $session
->getPage();
$page
->fillField('label', 'Bears again');
$page
->fillField('machine_name', 'bears');
$page
->findButton(t('Save'))
->click();
$session
->getPage()
->hasContent('Bears again (bears)');
$admin = $this
->drupalCreateUser(array_merge($permissions, [
'edit_any_workspace',
]));
$this
->drupalLogin($admin);
$session = $this
->getSession();
$this
->createWorkspaceThroughUI('Packers', 'packers');
$packers = $this
->getOneWorkspaceByLabel('Packers');
$this
->drupalGet("/admin/structure/workspace/{$packers->id()}/edit");
$this
->assertEquals(200, $session
->getStatusCode());
$this
->drupalGet("/admin/structure/workspace/{$bears->id()}/edit");
$this
->assertEquals(200, $session
->getStatusCode());
}
}