Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / workspaces / tests / src / Kernel / WorkspaceAccessTest.php
1 <?php
2
3 namespace Drupal\Tests\workspaces\Kernel;
4
5 use Drupal\KernelTests\KernelTestBase;
6 use Drupal\Tests\user\Traits\UserCreationTrait;
7 use Drupal\workspaces\Entity\Workspace;
8
9 /**
10  * Tests access on workspaces.
11  *
12  * @group workspaces
13  */
14 class WorkspaceAccessTest extends KernelTestBase {
15
16   use UserCreationTrait;
17
18   /**
19    * {@inheritdoc}
20    */
21   protected static $modules = [
22     'user',
23     'system',
24     'workspaces',
25   ];
26
27   /**
28    * {@inheritdoc}
29    */
30   protected function setUp() {
31     parent::setUp();
32
33     $this->installSchema('system', ['sequences']);
34
35     $this->installEntitySchema('workspace');
36     $this->installEntitySchema('workspace_association');
37     $this->installEntitySchema('user');
38
39     // User 1.
40     $this->createUser();
41   }
42
43   /**
44    * Test cases for testWorkspaceAccess().
45    *
46    * @return array
47    *   An array of operations and permissions to test with.
48    */
49   public function operationCases() {
50     return [
51       ['create', 'create workspace'],
52       ['view', 'view any workspace'],
53       ['view', 'view own workspace'],
54       ['update', 'edit any workspace'],
55       ['update', 'edit own workspace'],
56       ['delete', 'delete any workspace'],
57       ['delete', 'delete own workspace'],
58     ];
59   }
60
61   /**
62    * Verifies all workspace roles have the correct access for the operation.
63    *
64    * @param string $operation
65    *   The operation to test with.
66    * @param string $permission
67    *   The permission to test with.
68    *
69    * @dataProvider operationCases
70    */
71   public function testWorkspaceAccess($operation, $permission) {
72     $user = $this->createUser();
73     $this->setCurrentUser($user);
74     $workspace = Workspace::create(['id' => 'oak']);
75     $workspace->save();
76
77     $this->assertFalse($workspace->access($operation, $user));
78
79     \Drupal::entityTypeManager()->getAccessControlHandler('workspace')->resetCache();
80     $role = $this->createRole([$permission]);
81     $user->addRole($role);
82     $this->assertTrue($workspace->access($operation, $user));
83   }
84
85 }