Completed
Push — 4 ( 9c4d0c...83273c )
by Maxime
24s queued 11s
created

DuplicateElementMutationTest::setUp()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 5
rs 10
1
<?php
2
3
namespace DNADesign\Elemental\Tests\GraphQL;
4
5
use DNADesign\Elemental\GraphQL\DuplicateElementMutation;
6
use DNADesign\Elemental\Models\BaseElement;
7
use DNADesign\Elemental\Models\ElementalArea;
8
use GraphQL\Type\Definition\ResolveInfo;
9
use InvalidArgumentException;
10
use SilverStripe\Dev\SapphireTest;
11
use SilverStripe\Security\Security;
12
13
class DuplicateElementMutationTest extends SapphireTest
14
{
15
    protected function setUp()
16
    {
17
        parent::setUp();
18
        if (!class_exists(Schema::class)) {
0 ignored issues
show
Bug introduced by
The type DNADesign\Elemental\Tests\GraphQL\Schema was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
19
            $this->markTestSkipped('Skipped GraphQL 4 test ' . __CLASS__);
20
        }
21
    }
22
23
    public function testResolvePermissions()
24
    {
25
        $cannotEditClass = new class extends BaseElement {
26
            public function canEdit($member = null)
27
            {
28
                return false;
29
            }
30
            public function canCreate($member = null, $context = [])
31
            {
32
                return true;
33
            }
34
        };
35
36
        $cannotCreateClass = new class extends BaseElement {
37
            public function canEdit($member = null)
38
            {
39
                return true;
40
            }
41
            public function canCreate($member = null, $context = [])
42
            {
43
                return false;
44
            }
45
        };
46
47
        $area = new ElementalArea();
48
        $area->write();
49
50
        $testCases = [
51
            [$cannotEditClass, 'edit'],
52
            [$cannotCreateClass, 'create'],
53
        ];
54
55
        foreach ($testCases as $data) {
56
            [$class, $operation] = $data;
57
58
            $element = new $class();
59
            $element->ParentID = $area->ID;
60
            $element->write();
61
            $mutation = new DuplicateElementMutation();
0 ignored issues
show
Deprecated Code introduced by
The class DNADesign\Elemental\Grap...uplicateElementMutation has been deprecated: 4.8..5.0 Use silverstripe/graphql:^4 functionality. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

61
            $mutation = /** @scrutinizer ignore-deprecated */ new DuplicateElementMutation();
Loading history...
62
            $object = null;
63
            $args = ['id' => $element->ID];
64
            $context = ['currentUser' => Security::getCurrentUser()];
65
            $resolveInfo = new ResolveInfo([]);
66
67
            $this->expectException(InvalidArgumentException::class);
68
            $this->expectExceptionMessageRegExp("#insufficient permission to {$operation}#");
69
            $mutation->resolve($object, $args, $context, $resolveInfo);
70
        }
71
    }
72
}
73