Passed
Push — master ( d286b7...24e4a5 )
by Jan
04:36
created

PermissionResolverTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 47
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 31
nc 1
nop 0
dl 0
loc 47
rs 9.424
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
4
 *
5
 * Copyright (C) 2019 Jan Böhmer (https://github.com/jbtronics)
6
 *
7
 * This program is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU General Public License
9
 * as published by the Free Software Foundation; either version 2
10
 * of the License, or (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with this program; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
20
 */
21
22
namespace App\Tests\Services;
23
24
use App\Entity\UserSystem\Group;
25
use App\Entity\UserSystem\PermissionsEmbed;
26
use App\Entity\UserSystem\User;
27
use App\Services\PermissionResolver;
28
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
29
30
class PermissionResolverTest extends WebTestCase
31
{
32
    /**
33
     * @var PermissionResolver
34
     */
35
    protected $service;
36
37
    protected $user;
38
    protected $user_withoutGroup;
39
    protected $group;
40
41
    public function setUp()
42
    {
43
        parent::setUp(); // TODO: Change the autogenerated stub
44
45
        //Get an service instance.
46
        self::bootKernel();
47
        $this->service = self::$container->get(PermissionResolver::class);
48
49
        //Set up a mocked user
50
        $user_embed = new PermissionsEmbed();
51
        $user_embed->setPermissionValue('parts', 0, true) //read
52
            ->setPermissionValue('parts', 2, false) //edit
53
            ->setPermissionValue('parts', 4, null) //create
54
            ->setPermissionValue('parts', 30, null) //move
55
            ->setPermissionValue('parts', 8, null); //delete
56
57
        $this->user = $this->createMock(User::class);
58
        $this->user->method('getPermissions')->willReturn($user_embed);
59
60
        $this->user_withoutGroup = $this->createMock(User::class);
61
        $this->user_withoutGroup->method('getPermissions')->willReturn($user_embed);
62
        $this->user_withoutGroup->method('getGroup')->willReturn(null);
63
64
        //Set up a faked group
65
        $group1_embed = new PermissionsEmbed();
66
        $group1_embed->setPermissionValue('parts', 6, true)
67
            ->setPermissionValue('parts', 8, false)
68
            ->setPermissionValue('parts', 10, null)
69
            ->setPermissionValue('parts', 0, false)
70
            ->setPermissionValue('parts', 30, true)
71
            ->setPermissionValue('parts', 2, true);
72
73
        $this->group = $this->createMock(Group::class);
74
        $this->group->method('getPermissions')->willReturn($group1_embed);
75
76
        //Set this group for the user
77
        $this->user->method('getGroup')->willReturn($this->group);
78
79
        //parent group
80
        $parent_group_embed = new PermissionsEmbed();
81
        $parent_group_embed->setPermissionValue('parts', 12, true)
82
            ->setPermissionValue('parts', 14, false)
83
            ->setPermissionValue('parts', 16, null);
84
        $parent_group = $this->createMock(Group::class);
85
        $parent_group->method('getPermissions')->willReturn($parent_group_embed);
86
87
        $this->group->method('getParent')->willReturn($parent_group);
88
    }
89
90
    public function getPermissionNames()
91
    {
92
        //List all possible operation names.
93
        return [
94
            [PermissionsEmbed::PARTS],
95
            [PermissionsEmbed::USERS],
96
            [PermissionsEmbed::PARTS_ORDERDETAILS],
97
            [PermissionsEmbed::PARTS_NAME],
98
            [PermissionsEmbed::PARTS_ORDER],
99
            [PermissionsEmbed::PARTS_MINAMOUNT],
100
            [PermissionsEmbed::PARTS_MANUFACTURER],
101
            [PermissionsEmbed::DEVICES],
102
            [PermissionsEmbed::PARTS_FOOTPRINT],
103
            [PermissionsEmbed::PARTS_DESCRIPTION],
104
            [PermissionsEmbed::PARTS_COMMENT],
105
            [PermissionsEmbed::PARTS_ATTACHMENTS],
106
            [PermissionsEmbed::MANUFACTURERS],
107
            [PermissionsEmbed::LABELS],
108
            [PermissionsEmbed::DATABASE],
109
            [PermissionsEmbed::GROUPS],
110
            [PermissionsEmbed::FOOTRPINTS],
111
            [PermissionsEmbed::DEVICE_PARTS],
112
            [PermissionsEmbed::CATEGORIES],
113
            [PermissionsEmbed::PARTS_PRICES],
114
            [PermissionsEmbed::ATTACHMENT_TYPES],
115
            [PermissionsEmbed::CONFIG],
116
        ];
117
    }
118
119
    /**
120
     * @dataProvider getPermissionNames
121
     */
122
    public function testListOperationsForPermission($perm_name)
123
    {
124
        $arr = $this->service->listOperationsForPermission($perm_name);
125
126
        //Every entry should not be empty.
127
        $this->assertNotEmpty($arr);
128
    }
129
130
    public function testInvalidListOperationsForPermission()
131
    {
132
        $this->expectException(\InvalidArgumentException::class);
133
        //Must throw an exception
134
        $this->service->listOperationsForPermission('invalid');
135
    }
136
137
    public function testisValidPermission()
138
    {
139
        $this->assertTrue($this->service->isValidPermission('parts'));
140
        $this->assertFalse($this->service->isValidPermission('invalid'));
141
    }
142
143
    public function testIsValidOperation()
144
    {
145
        $this->assertTrue($this->service->isValidOperation('parts', 'read'));
146
147
        //Must return false if either the permission or the operation is not existing
148
        $this->assertFalse($this->service->isValidOperation('parts', 'invalid'));
149
        $this->assertFalse($this->service->isValidOperation('invalid', 'read'));
150
        $this->assertFalse($this->service->isValidOperation('invalid', 'invalid'));
151
    }
152
153
    public function testDontInherit()
154
    {
155
        //Check with faked object
156
        $this->assertTrue($this->service->dontInherit($this->user, 'parts', 'read'));
157
        $this->assertFalse($this->service->dontInherit($this->user, 'parts', 'edit'));
158
        $this->assertNull($this->service->dontInherit($this->user, 'parts', 'create'));
159
        $this->assertNull($this->service->dontInherit($this->user, 'parts', 'show_history'));
160
        $this->assertNull($this->service->dontInherit($this->user, 'parts', 'delete'));
161
162
        //Test for user without group
163
        $this->assertTrue($this->service->dontInherit($this->user_withoutGroup, 'parts', 'read'));
164
        $this->assertFalse($this->service->dontInherit($this->user_withoutGroup, 'parts', 'edit'));
165
        $this->assertNull($this->service->dontInherit($this->user_withoutGroup, 'parts', 'create'));
166
        $this->assertNull($this->service->dontInherit($this->user_withoutGroup, 'parts', 'show_history'));
167
        $this->assertNull($this->service->dontInherit($this->user_withoutGroup, 'parts', 'delete'));
168
    }
169
170
    public function testInherit()
171
    {
172
        //Not inherited values should be same as dont inherit:
173
        $this->assertTrue($this->service->inherit($this->user, 'parts', 'read'));
174
        $this->assertFalse($this->service->inherit($this->user, 'parts', 'edit'));
175
        //When thing can not be resolved null should be returned
176
        $this->assertNull($this->service->inherit($this->user, 'parts', 'create'));
177
178
        //Check for inherit from group
179
        $this->assertTrue($this->service->inherit($this->user, 'parts', 'show_history'));
180
        $this->assertFalse($this->service->inherit($this->user, 'parts', 'delete'));
181
        $this->assertNull($this->service->inherit($this->user, 'parts', 'search'));
182
183
        //Check for inherit from group and parent group
184
        $this->assertTrue($this->service->inherit($this->user, 'parts', 'all_parts'));
185
        $this->assertFalse($this->service->inherit($this->user, 'parts', 'no_price_parts'));
186
        $this->assertNull($this->service->inherit($this->user, 'parts', 'obsolete_parts'));
187
188
        //Test for user without group
189
        $this->assertTrue($this->service->inherit($this->user_withoutGroup, 'parts', 'read'));
190
        $this->assertFalse($this->service->inherit($this->user_withoutGroup, 'parts', 'edit'));
191
        $this->assertNull($this->service->inherit($this->user_withoutGroup, 'parts', 'create'));
192
        $this->assertNull($this->service->inherit($this->user_withoutGroup, 'parts', 'show_history'));
193
        $this->assertNull($this->service->inherit($this->user_withoutGroup, 'parts', 'delete'));
194
    }
195
}
196