Passed
Push — master ( 7d7588...6405de )
by Jan
04:46
created

PermissionsEmbedTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
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\Entity\UserSystem;
23
24
use App\Entity\UserSystem\PermissionsEmbed;
25
use Doctrine\ORM\Mapping\Embedded;
26
use PHPUnit\Framework\TestCase;
0 ignored issues
show
Bug introduced by
The type PHPUnit\Framework\TestCase 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...
27
28
class PermissionsEmbedTest extends TestCase
29
{
30
31
    public function testGetPermissionValue()
32
    {
33
        $embed = new PermissionsEmbed();
34
        //For newly created embedded, all things should be set to inherit => null
35
        //Test both normal name and constants
36
37
        $this->assertNull($embed->getPermissionValue(PermissionsEmbed::PARTS, 0));
38
        $this->assertNull($embed->getPermissionValue(PermissionsEmbed::CONFIG, 0));
39
        $this->assertNull($embed->getPermissionValue(PermissionsEmbed::ATTACHMENT_TYPES, 0));
40
        $this->assertNull($embed->getPermissionValue(PermissionsEmbed::CATEGORIES, 0));
41
        $this->assertNull($embed->getPermissionValue(PermissionsEmbed::DATABASE, 0));
42
        $this->assertNull($embed->getPermissionValue(PermissionsEmbed::DEVICE_PARTS, 0));
43
        $this->assertNull($embed->getPermissionValue(PermissionsEmbed::DEVICES, 0));
44
        $this->assertNull($embed->getPermissionValue(PermissionsEmbed::FOOTRPINTS, 0));
45
        $this->assertNull($embed->getPermissionValue(PermissionsEmbed::GROUPS, 0));
46
        $this->assertNull($embed->getPermissionValue(PermissionsEmbed::DATABASE, 0));
47
        $this->assertNull($embed->getPermissionValue(PermissionsEmbed::LABELS, 0));
48
        $this->assertNull($embed->getPermissionValue(PermissionsEmbed::MANUFACTURERS, 0));
49
        $this->assertNull($embed->getPermissionValue(PermissionsEmbed::PARTS_ATTACHMENTS, 0));
50
        $this->assertNull($embed->getPermissionValue(PermissionsEmbed::PARTS_COMMENT, 0));
51
        $this->assertNull($embed->getPermissionValue(PermissionsEmbed::PARTS_DESCRIPTION, 0));
52
        $this->assertNull($embed->getPermissionValue(PermissionsEmbed::PARTS_FOOTPRINT, 0));
53
        $this->assertNull($embed->getPermissionValue(PermissionsEmbed::PARTS_MANUFACTURER, 0));
54
        $this->assertNull($embed->getPermissionValue(PermissionsEmbed::PARTS_MINAMOUNT, 0));
55
        $this->assertNull($embed->getPermissionValue(PermissionsEmbed::PARTS_NAME, 0));
56
        $this->assertNull($embed->getPermissionValue(PermissionsEmbed::PARTS_ORDER, 0));
57
        $this->assertNull($embed->getPermissionValue(PermissionsEmbed::PARTS_ORDERDETAILS, 0));
58
        $this->assertNull($embed->getPermissionValue(PermissionsEmbed::USERS, 0));
59
60
        //Set a value for testing to the part property
61
        $reflection = new \ReflectionClass($embed);
62
        $property = $reflection->getProperty('parts');
63
        $property->setAccessible(true);
64
65
        $property->setValue($embed, 0b11011000); // 11 01 10 00
66
67
        //Test if function is working correctly
68
        $this->assertNull($embed->getPermissionValue(PermissionsEmbed::PARTS, 0));
69
        $this->assertFalse($embed->getPermissionValue(PermissionsEmbed::PARTS, 2));
70
        $this->assertTrue($embed->getPermissionValue(PermissionsEmbed::PARTS, 4));
71
        // 11 is reserved, but it should be also treat as INHERIT.
72
        $this->assertNull($embed->getPermissionValue(PermissionsEmbed::PARTS, 6));
73
    }
74
75
    public function testGetBitValue()
76
    {
77
        $embed = new PermissionsEmbed();
78
79
        //Set a value for testing to the part property
80
        $reflection = new \ReflectionClass($embed);
81
        $property = $reflection->getProperty('parts');
82
        $property->setAccessible(true);
83
84
        $property->setValue($embed, 0b11011000); // 11 01 10 00
85
86
        //Test if function is working correctly
87
        $this->assertEquals(PermissionsEmbed::INHERIT, $embed->getBitValue(PermissionsEmbed::PARTS, 0));
88
        $this->assertEquals(PermissionsEmbed::DISALLOW, $embed->getBitValue(PermissionsEmbed::PARTS, 2));
89
        $this->assertEquals(PermissionsEmbed::ALLOW, $embed->getBitValue(PermissionsEmbed::PARTS, 4));
90
        // 11 is reserved, but it should be also treat as INHERIT.
91
        $this->assertEquals(0b11, $embed->getBitValue(PermissionsEmbed::PARTS, 6));
92
    }
93
94
    public function testInvalidPermissionName()
95
    {
96
        $embed = new PermissionsEmbed();
97
        //When encoutering an unknown permission name the class must throw an exception
98
        $this->expectException(\InvalidArgumentException::class);
99
        $embed->getPermissionValue('invalid', 0);
100
    }
101
102
    public function testInvalidBit1()
103
    {
104
        $embed = new PermissionsEmbed();
105
        //When encoutering an negative bit the class must throw an exception
106
        $this->expectException(\InvalidArgumentException::class);
107
        $embed->getPermissionValue('parts', -1);
108
    }
109
110
    public function testInvalidBit2()
111
    {
112
        $embed = new PermissionsEmbed();
113
        //When encoutering an odd bit number it must throw an error.
114
        $this->expectException(\InvalidArgumentException::class);
115
        $embed->getPermissionValue('parts', 1);
116
    }
117
118
    public function testInvalidBit3()
119
    {
120
        $embed = new PermissionsEmbed();
121
        //When encoutering an too high bit number it must throw an error.
122
        $this->expectException(\InvalidArgumentException::class);
123
        $embed->getPermissionValue('parts', 32);
124
    }
125
126
    public function getStatesBINARY()
127
    {
128
        return [
129
            'ALLOW' => [PermissionsEmbed::ALLOW],
130
            'DISALLOW' => [PermissionsEmbed::DISALLOW],
131
            'INHERIT' => [PermissionsEmbed::INHERIT],
132
            '0b11' => [0b11],
133
        ];
134
    }
135
136
    public function getStatesBOOL()
137
    {
138
        return [
139
            'ALLOW' => [true],
140
            'DISALLOW' => [false],
141
            'INHERIT' => [null],
142
            '0b11' => [null],
143
        ];
144
    }
145
146
    /**
147
     * @dataProvider getStatesBINARY
148
     */
149
    public function testsetBitValue($value)
150
    {
151
        $embed = new PermissionsEmbed();
152
        //Check if it returns itself, for chaining.
153
        $this->assertEquals($embed, $embed->setBitValue(PermissionsEmbed::PARTS, 0, $value));
154
        $this->assertEquals($value, $embed->getBitValue(PermissionsEmbed::PARTS, 0));
155
    }
156
157
    /**
158
     * @dataProvider getStatesBOOL
159
     */
160
    public function testSetPermissionValue($value)
161
    {
162
        $embed = new PermissionsEmbed();
163
        //Check if it returns itself, for chaining.
164
        $this->assertEquals($embed, $embed->setPermissionValue(PermissionsEmbed::PARTS, 0, $value));
165
        $this->assertEquals($value, $embed->getPermissionValue(PermissionsEmbed::PARTS, 0));
166
    }
167
168
    public function testSetRawPermissionValue()
169
    {
170
        $embed = new PermissionsEmbed();
171
        $embed->setRawPermissionValue(PermissionsEmbed::PARTS, 10);
172
        $this->assertEquals(10, $embed->getRawPermissionValue(PermissionsEmbed::PARTS));
173
    }
174
175
    public function testSetRawPermissionValues()
176
    {
177
        $embed = new PermissionsEmbed();
178
        $embed->setRawPermissionValues([
179
            PermissionsEmbed::PARTS => 0,
180
            PermissionsEmbed::USERS => 100,
181
            PermissionsEmbed::CATEGORIES => 1304,
182
        ]);
183
184
        $this->assertEquals(0, $embed->getRawPermissionValue(PermissionsEmbed::PARTS));
185
        $this->assertEquals(100, $embed->getRawPermissionValue(PermissionsEmbed::USERS));
186
        $this->assertEquals(1304, $embed->getRawPermissionValue(PermissionsEmbed::CATEGORIES));
187
188
        //Test second method to pass perm names and values
189
        $embed->setRawPermissionValues(
190
            [PermissionsEmbed::PARTS, PermissionsEmbed::USERS, PermissionsEmbed::CATEGORIES],
191
            [0, 100, 1304]
192
        );
193
194
        $this->assertEquals(0, $embed->getRawPermissionValue(PermissionsEmbed::PARTS));
195
        $this->assertEquals(100, $embed->getRawPermissionValue(PermissionsEmbed::USERS));
196
        $this->assertEquals(1304, $embed->getRawPermissionValue(PermissionsEmbed::CATEGORIES));
197
    }
198
}
199