Skip to content

Commit 04bf629

Browse files
committed
Add trash tests for editor.
1 parent 863674a commit 04bf629

13 files changed

+324
-109
lines changed

test/GraphQLTestCase.php

Lines changed: 0 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,6 @@ public static function rememberOriginalAccessRules($accessRules)
8484
}
8585
}
8686

87-
if (isset($accessRules['permissions'])) {
88-
$originals['permissions'] = Utils::permissions()->find("name!=0")->explode('name');
89-
}
90-
9187
self::$originalAccessRules = $originals;
9288
return $originals;
9389
}
@@ -130,30 +126,6 @@ public static function setUpBeforeClass()
130126
if (isset($settings['access'])) {
131127
self::rememberOriginalAccessRules($settings['access']);
132128

133-
if (isset($settings['access']['permissions'])) {
134-
$permissions = $settings['access']['permissions'];
135-
if (isset($permissions['add'])) {
136-
foreach ($permissions['add'] as $permissionName) {
137-
$permission = Utils::permissions()->get($permissionName);
138-
if ($permission->id) {
139-
continue;
140-
}
141-
$permission = Utils::permissions()->add($permissionName);
142-
$permission->title = $permissionName;
143-
$permission->save();
144-
}
145-
}
146-
if (isset($permissions['remove'])) {
147-
foreach ($permissions['remove'] as $permissionName) {
148-
$permission = Utils::permissions()->get($permissionName);
149-
if (!$permission->id) {
150-
continue;
151-
}
152-
Utils::permissions()->delete($permission);
153-
}
154-
}
155-
}
156-
157129
if (isset($settings['access']['roles'])) {
158130
foreach ($settings['access']['roles'] as $rules) {
159131
$role = Utils::roles()->get($rules['name']);
@@ -286,28 +258,6 @@ public static function tearDownAfterClass()
286258
}
287259
}
288260
}
289-
290-
if (isset($accessRules['permissions'])) {
291-
// remove all permissions that were not in the
292-
// original permissions list
293-
$oldPermissions = implode('|', $accessRules['permissions']);
294-
$newPermissions = Utils::permissions()->find("name!=$oldPermissions");
295-
foreach ($newPermissions as $permission) {
296-
Utils::permissions()->delete($permission);
297-
}
298-
299-
// install back the permission if it is in the original list
300-
// but not installed
301-
foreach ($accessRules['permissions'] as $permissionName) {
302-
$permission = Utils::permissions()->get($permissionName);
303-
if ($permission->id) {
304-
continue;
305-
}
306-
$permission = Utils::permissions()->add($permissionName);
307-
$permission->title = $permissionName;
308-
$permission->save();
309-
}
310-
}
311261
}
312262

313263
public static function execute($payload = null, $variables = null)

test/Permissions/Editor/Trash/Allowed.php renamed to test/Permissions/Editor/Trash/Allowed/AllowedDeletePermissionTest.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,30 @@
33
use ProcessWire\GraphQL\Test\GraphqlTestCase;
44
use ProcessWire\GraphQL\Utils;
55

6-
class EditorTrashAllowedTest extends GraphqlTestCase {
6+
class EditorTrashAllowedDeletePermissionTest extends GraphqlTestCase {
77

88
/**
9-
* + For Superuser
9+
* + For editor
1010
* + The template is legal.
11+
* + The user has all required permissions
1112
*/
1213
public static function getSettings()
1314
{
1415
return [
1516
'login' => 'editor',
1617
'legalTemplates' => ['skyscraper'],
18+
'access' => [
19+
'templates' => [
20+
[
21+
'name' => 'skyscraper',
22+
'roles' => ['editor'],
23+
'editRoles' => ['editor'],
24+
'rolesPermissions' => [
25+
'editor' => ['page-delete'] // <-- has page-delete permission
26+
]
27+
]
28+
],
29+
]
1730
];
1831
}
1932

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php namespace ProcessWire\GraphQL\Test\Permissions;
2+
3+
use ProcessWire\GraphQL\Test\GraphqlTestCase;
4+
use ProcessWire\GraphQL\Utils;
5+
6+
class EditorTrashAllowedEditTrashCreatedTest extends GraphqlTestCase {
7+
8+
/**
9+
* + For editor
10+
* + The template is legal.
11+
* + The user has page-edit-trash-created permission
12+
*/
13+
public static function getSettings()
14+
{
15+
return [
16+
'login' => 'editor',
17+
'legalTemplates' => ['skyscraper'],
18+
'access' => [
19+
'templates' => [
20+
[
21+
'name' => 'skyscraper',
22+
'roles' => ['editor'],
23+
'editRoles' => ['editor'],
24+
'rolesPermissions' => [
25+
'editor' => ['page-edit-trash-created'] // <-- has page-edit-trash-created permission
26+
]
27+
]
28+
],
29+
]
30+
];
31+
}
32+
33+
private static $target = null;
34+
35+
public static function setUpBeforeClass()
36+
{
37+
$skyscraper = Utils::pages()->get("template=skyscraper, sort=random");
38+
$skyscraper->template->allowChangeUser = 1;
39+
$skyscraper->of(false);
40+
$skyscraper->created_users_id = Utils::users()->get('editor')->id;
41+
$skyscraper->save('created_users_id', ['quite' => true]);
42+
$skyscraper->template->allowChangeUser = 0;
43+
$skyscraper->of(true);
44+
45+
$skyscraper = Utils::pages()->get("id={$skyscraper->id}");
46+
self::$target = $skyscraper;
47+
parent::setUpBeforeClass();
48+
}
49+
50+
public function testPermission() {
51+
$skyscraper = self::$target;
52+
$query = 'mutation trashPage($id: ID!) {
53+
trash(id: $id) {
54+
id
55+
name
56+
}
57+
}';
58+
$variables = [
59+
'id' => $skyscraper->id,
60+
];
61+
62+
assertFalse($skyscraper->isTrash());
63+
$res = self::execute($query, $variables);
64+
assertEquals($res->data->trash->id, $skyscraper->id, 'Trashes the page.');
65+
assertTrue($skyscraper->isTrash(), 'Trashes the correct page.');
66+
}
67+
}

test/Permissions/Editor/Trash/Available.php

Lines changed: 0 additions & 24 deletions
This file was deleted.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php namespace ProcessWire\GraphQL\Test\Permissions;
2+
3+
use ProcessWire\GraphQL\Test\GraphqlTestCase;
4+
use ProcessWire\GraphQL\Utils;
5+
6+
use function ProcessWire\GraphQL\Test\Assert\assertTypePathExists;
7+
8+
class EditorTrashAvailableDeletePermissionTest extends GraphqlTestCase {
9+
10+
/**
11+
* + For editor
12+
* + The template is legal.
13+
* + The user has page delete permission on a legal template.
14+
*/
15+
public static function getSettings()
16+
{
17+
return [
18+
'login' => 'editor',
19+
'legalTemplates' => ['city'],
20+
'access' => [
21+
'templates' => [
22+
[
23+
'name' => 'city',
24+
'roles' => ['editor'],
25+
'editRoles' => ['editor'],
26+
'rolesPermissions' => [
27+
'editor' => ['page-delete'] // <-- user has page-delete permission on this template
28+
]
29+
]
30+
],
31+
]
32+
];
33+
}
34+
35+
public function testPermission() {
36+
assertTypePathExists(['Mutation', 'trash']);
37+
}
38+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php namespace ProcessWire\GraphQL\Test\Permissions;
2+
3+
use ProcessWire\GraphQL\Test\GraphqlTestCase;
4+
use ProcessWire\GraphQL\Utils;
5+
use ProcessWire\Permissions;
6+
7+
use function ProcessWire\GraphQL\Test\Assert\assertTypePathExists;
8+
9+
class EditorTrashAvailableEditTrashCreatedPermissionTest extends GraphqlTestCase {
10+
11+
/**
12+
* + For editor
13+
* + The template is legal.
14+
* + The user has page-edit-trash-created permission on a legal template.
15+
*/
16+
public static function getSettings()
17+
{
18+
return [
19+
'login' => 'editor',
20+
'legalTemplates' => ['architect'],
21+
'access' => [
22+
'templates' => [
23+
[
24+
'name' => 'architect',
25+
'roles' => ['editor'],
26+
'editRoles' => ['editor'],
27+
'rolesPermissions' => [
28+
'editor' => ['page-edit-trash-created'], // <-- has page-edit-trash-created permission
29+
]
30+
]
31+
]
32+
]
33+
];
34+
}
35+
36+
public function testPermission() {
37+
assertTypePathExists(['Mutation', 'trash']);
38+
}
39+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php namespace ProcessWire\GraphQL\Test\Permissions;
2+
3+
use ProcessWire\GraphQL\Test\GraphqlTestCase;
4+
use ProcessWire\GraphQL\Utils;
5+
6+
use function ProcessWire\GraphQL\Test\Assert\assertStringContainsString;
7+
8+
class EditorTrashNotAllowedTemplateDeletePermissionTest extends GraphqlTestCase {
9+
10+
/**
11+
* + For Superuser
12+
* + The targett template is not legal.
13+
*/
14+
public static function getSettings()
15+
{
16+
return [
17+
'login' => 'editor',
18+
'legalTemplates' => ['city', 'skyscraper'], // <-- skyscraper template is not legal
19+
'access' => [
20+
'templates' => [
21+
[
22+
'name' => 'skyscraper',
23+
'roles' => ['editor'],
24+
'editRoles' => ['editor'],
25+
'rolesPermissions' => [
26+
'editor' => ['page-sort'] // <-- no page-delete or page-edit-trash-created permissions
27+
]
28+
],
29+
[
30+
'name' => 'city',
31+
'roles' => ['editor'],
32+
'editRoles' => ['editor'],
33+
'rolesPermissions' => [
34+
'editor' => ['page-delete']
35+
]
36+
]
37+
],
38+
]
39+
];
40+
}
41+
42+
public function testPermission() {
43+
$skyscraper = Utils::pages()->get("template=skyscraper, sort=random");
44+
$query = 'mutation trashPage($id: ID!) {
45+
trash(id: $id) {
46+
id
47+
name
48+
}
49+
}';
50+
$variables = [
51+
'id' => $skyscraper->id,
52+
];
53+
54+
assertFalse($skyscraper->isTrash());
55+
$res = self::execute($query, $variables);
56+
assertEquals(1, count($res->errors), 'Errors without trashing the page.');
57+
assertStringContainsString('trash', $res->errors[0]->message);
58+
assertFalse($skyscraper->isTrash(), 'Does not trashes the target page.');
59+
}
60+
}

test/Permissions/Editor/Trash/NotAllowed/Template.php renamed to test/Permissions/Editor/Trash/NotAllowed/TemplateTest.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,27 @@ public static function getSettings()
1515
{
1616
return [
1717
'login' => 'editor',
18-
'legalTemplates' => ['city'],
18+
'legalTemplates' => ['city'], // <-- skyscraper template is not legal
19+
'access' => [
20+
'templates' => [
21+
[
22+
'name' => 'skyscraper',
23+
'roles' => ['editor'],
24+
'editRoles' => ['editor'],
25+
'rolesPermissions' => [
26+
'editor' => ['page-delete']
27+
]
28+
],
29+
[
30+
'name' => 'city',
31+
'roles' => ['editor'],
32+
'editRoles' => ['editor'],
33+
'rolesPermissions' => [
34+
'editor' => ['page-delete']
35+
]
36+
]
37+
],
38+
]
1939
];
2040
}
2141

test/Permissions/Editor/Trash/NotAvailable.php

Lines changed: 0 additions & 23 deletions
This file was deleted.

0 commit comments

Comments
 (0)