View source
<?php
namespace Drupal\Tests\workspaces\Functional;
use Drupal\Tests\BrowserTestBase;
use Drupal\workspaces\Entity\Workspace;
class WorkspacePermissionsTest extends BrowserTestBase {
use WorkspaceTestUtilities;
protected static $modules = [
'workspaces',
];
protected $defaultTheme = 'stark';
public function testCreateWorkspace() {
$editor = $this
->drupalCreateUser([
'access administration pages',
'administer site configuration',
'create workspace',
]);
$this
->drupalLogin($editor);
$this
->createWorkspaceThroughUi('Bears', 'bears');
$etm = \Drupal::service('entity_type.manager');
$entity_list = $etm
->getStorage('workspace')
->loadByProperties([
'label' => 'Bears',
]);
$bears = current($entity_list);
$this
->drupalGet("/admin/config/workflow/workspaces/manage/{$bears->id()}/edit");
$this
->assertSession()
->statusCodeEquals(403);
}
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 = Workspace::load('bears');
$this
->drupalGet("/admin/config/workflow/workspaces/manage/{$bears->id()}/edit");
$this
->assertSession()
->statusCodeEquals(200);
$page = $this
->getSession()
->getPage();
$page
->fillField('label', 'Bears again');
$page
->fillField('id', 'bears');
$page
->findButton('Save')
->click();
$page
->hasContent('Bears again (bears)');
$editor2 = $this
->drupalCreateUser($permissions);
$this
->drupalLogin($editor2);
$this
->createWorkspaceThroughUi('Packers', 'packers');
$packers = Workspace::load('packers');
$this
->drupalGet("/admin/config/workflow/workspaces/manage/{$packers->id()}/edit");
$this
->assertSession()
->statusCodeEquals(200);
$this
->drupalGet("/admin/config/workflow/workspaces/manage/{$bears->id()}/edit");
$this
->assertSession()
->statusCodeEquals(403);
}
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 = Workspace::load('bears');
$this
->drupalGet("/admin/config/workflow/workspaces/manage/{$bears->id()}/edit");
$this
->assertSession()
->statusCodeEquals(200);
$page = $this
->getSession()
->getPage();
$page
->fillField('label', 'Bears again');
$page
->fillField('id', 'bears');
$page
->findButton('Save')
->click();
$page
->hasContent('Bears again (bears)');
$admin = $this
->drupalCreateUser(array_merge($permissions, [
'edit any workspace',
]));
$this
->drupalLogin($admin);
$this
->createWorkspaceThroughUi('Packers', 'packers');
$packers = Workspace::load('packers');
$this
->drupalGet("/admin/config/workflow/workspaces/manage/{$packers->id()}/edit");
$this
->assertSession()
->statusCodeEquals(200);
$this
->drupalGet("/admin/config/workflow/workspaces/manage/{$bears->id()}/edit");
$this
->assertSession()
->statusCodeEquals(200);
}
public function testDeleteOwnWorkspace() {
$permissions = [
'access administration pages',
'administer site configuration',
'create workspace',
'delete own workspace',
];
$editor1 = $this
->drupalCreateUser($permissions);
$this
->drupalLogin($editor1);
$bears = $this
->createWorkspaceThroughUi('Bears', 'bears');
$this
->drupalGet("/admin/config/workflow/workspaces/manage/{$bears->id()}/delete");
$this
->assertSession()
->statusCodeEquals(200);
$editor2 = $this
->drupalCreateUser($permissions);
$this
->drupalLogin($editor2);
$packers = $this
->createWorkspaceThroughUi('Packers', 'packers');
$this
->drupalGet("/admin/config/workflow/workspaces/manage/{$packers->id()}/delete");
$this
->assertSession()
->statusCodeEquals(200);
$this
->drupalGet("/admin/config/workflow/workspaces/manage/{$bears->id()}/delete");
$this
->assertSession()
->statusCodeEquals(403);
}
public function testDeleteAnyWorkspace() {
$permissions = [
'access administration pages',
'administer site configuration',
'create workspace',
'delete own workspace',
];
$editor1 = $this
->drupalCreateUser($permissions);
$this
->drupalLogin($editor1);
$bears = $this
->createWorkspaceThroughUi('Bears', 'bears');
$this
->drupalGet("/admin/config/workflow/workspaces/manage/{$bears->id()}/delete");
$this
->assertSession()
->statusCodeEquals(200);
$admin = $this
->drupalCreateUser(array_merge($permissions, [
'delete any workspace',
]));
$this
->drupalLogin($admin);
$packers = $this
->createWorkspaceThroughUi('Packers', 'packers');
$this
->drupalGet("/admin/config/workflow/workspaces/manage/{$packers->id()}/delete");
$this
->assertSession()
->statusCodeEquals(200);
$this
->drupalGet("/admin/config/workflow/workspaces/manage/{$bears->id()}/delete");
$this
->assertSession()
->statusCodeEquals(200);
}
}