Passed
Push — master ( 2d425f...eb03d1 )
by Jan
04:57 queued 10s
created

PermissionsEmbedTest::testInvalidBit1()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 6
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 - 2020 Jan Böhmer (https://github.com/jbtronics)
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU Affero General Public License as published
9
 * by the Free Software Foundation, either version 3 of the License, or
10
 * (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 Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License
18
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19
 */
20
21
declare(strict_types=1);
22
23
/**
24
 * This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
25
 *
26
 * Copyright (C) 2019 Jan Böhmer (https://github.com/jbtronics)
27
 *
28
 * This program is free software; you can redistribute it and/or
29
 * modify it under the terms of the GNU General Public License
30
 * as published by the Free Software Foundation; either version 2
31
 * of the License, or (at your option) any later version.
32
 *
33
 * This program is distributed in the hope that it will be useful,
34
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
35
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36
 * GNU General Public License for more details.
37
 *
38
 * You should have received a copy of the GNU General Public License
39
 * along with this program; if not, write to the Free Software
40
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
41
 */
42
43
namespace App\Tests\Entity\UserSystem;
44
45
use App\Entity\UserSystem\PermissionsEmbed;
46
use Doctrine\ORM\Mapping\Embedded;
47
use InvalidArgumentException;
48
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...
49
use ReflectionClass;
50
51
class PermissionsEmbedTest extends TestCase
52
{
53
    public function testGetPermissionValue(): void
54
    {
55
        $embed = new PermissionsEmbed();
56
        //For newly created embedded, all things should be set to inherit => null
57
        //Test both normal name and constants
58
59
        $this->assertNull($embed->getPermissionValue(PermissionsEmbed::PARTS, 0));
60
        $this->assertNull($embed->getPermissionValue(PermissionsEmbed::CONFIG, 0));
61
        $this->assertNull($embed->getPermissionValue(PermissionsEmbed::ATTACHMENT_TYPES, 0));
62
        $this->assertNull($embed->getPermissionValue(PermissionsEmbed::CATEGORIES, 0));
63
        $this->assertNull($embed->getPermissionValue(PermissionsEmbed::DATABASE, 0));
64
        $this->assertNull($embed->getPermissionValue(PermissionsEmbed::DEVICE_PARTS, 0));
65
        $this->assertNull($embed->getPermissionValue(PermissionsEmbed::DEVICES, 0));
66
        $this->assertNull($embed->getPermissionValue(PermissionsEmbed::FOOTRPINTS, 0));
67
        $this->assertNull($embed->getPermissionValue(PermissionsEmbed::GROUPS, 0));
68
        $this->assertNull($embed->getPermissionValue(PermissionsEmbed::DATABASE, 0));
69
        $this->assertNull($embed->getPermissionValue(PermissionsEmbed::LABELS, 0));
70
        $this->assertNull($embed->getPermissionValue(PermissionsEmbed::MANUFACTURERS, 0));
71
        $this->assertNull($embed->getPermissionValue(PermissionsEmbed::PARTS_ATTACHMENTS, 0));
72
        $this->assertNull($embed->getPermissionValue(PermissionsEmbed::PARTS_COMMENT, 0));
73
        $this->assertNull($embed->getPermissionValue(PermissionsEmbed::PARTS_DESCRIPTION, 0));
74
        $this->assertNull($embed->getPermissionValue(PermissionsEmbed::PARTS_FOOTPRINT, 0));
75
        $this->assertNull($embed->getPermissionValue(PermissionsEmbed::PARTS_MANUFACTURER, 0));
76
        $this->assertNull($embed->getPermissionValue(PermissionsEmbed::PARTS_MINAMOUNT, 0));
77
        $this->assertNull($embed->getPermissionValue(PermissionsEmbed::PARTS_NAME, 0));
78
        $this->assertNull($embed->getPermissionValue(PermissionsEmbed::PARTS_ORDER, 0));
79
        $this->assertNull($embed->getPermissionValue(PermissionsEmbed::PARTS_ORDERDETAILS, 0));
80
        $this->assertNull($embed->getPermissionValue(PermissionsEmbed::USERS, 0));
81
82
        //Set a value for testing to the part property
83
        $reflection = new ReflectionClass($embed);
84
        $property = $reflection->getProperty('parts');
85
        $property->setAccessible(true);
86
87
        $property->setValue($embed, 0b11011000); // 11 01 10 00
88
89
        //Test if function is working correctly
90
        $this->assertNull($embed->getPermissionValue(PermissionsEmbed::PARTS, 0));
91
        $this->assertFalse($embed->getPermissionValue(PermissionsEmbed::PARTS, 2));
92
        $this->assertTrue($embed->getPermissionValue(PermissionsEmbed::PARTS, 4));
93
        // 11 is reserved, but it should be also treat as INHERIT.
94
        $this->assertNull($embed->getPermissionValue(PermissionsEmbed::PARTS, 6));
95
    }
96
97
    public function testGetBitValue(): void
98
    {
99
        $embed = new PermissionsEmbed();
100
101
        //Set a value for testing to the part property
102
        $reflection = new ReflectionClass($embed);
103
        $property = $reflection->getProperty('parts');
104
        $property->setAccessible(true);
105
106
        $property->setValue($embed, 0b11011000); // 11 01 10 00
107
108
        //Test if function is working correctly
109
        $this->assertSame(PermissionsEmbed::INHERIT, $embed->getBitValue(PermissionsEmbed::PARTS, 0));
110
        $this->assertSame(PermissionsEmbed::DISALLOW, $embed->getBitValue(PermissionsEmbed::PARTS, 2));
111
        $this->assertSame(PermissionsEmbed::ALLOW, $embed->getBitValue(PermissionsEmbed::PARTS, 4));
112
        // 11 is reserved, but it should be also treat as INHERIT.
113
        $this->assertSame(0b11, $embed->getBitValue(PermissionsEmbed::PARTS, 6));
114
    }
115
116
    public function testInvalidPermissionName(): void
117
    {
118
        $embed = new PermissionsEmbed();
119
        //When encoutering an unknown permission name the class must throw an exception
120
        $this->expectException(InvalidArgumentException::class);
121
        $embed->getPermissionValue('invalid', 0);
122
    }
123
124
    public function testInvalidBit1(): void
125
    {
126
        $embed = new PermissionsEmbed();
127
        //When encoutering an negative bit the class must throw an exception
128
        $this->expectException(InvalidArgumentException::class);
129
        $embed->getPermissionValue('parts', -1);
130
    }
131
132
    public function testInvalidBit2(): void
133
    {
134
        $embed = new PermissionsEmbed();
135
        //When encoutering an odd bit number it must throw an error.
136
        $this->expectException(InvalidArgumentException::class);
137
        $embed->getPermissionValue('parts', 1);
138
    }
139
140
    public function getStatesBINARY()
141
    {
142
        return [
143
            'ALLOW' => [PermissionsEmbed::ALLOW],
144
            'DISALLOW' => [PermissionsEmbed::DISALLOW],
145
            'INHERIT' => [PermissionsEmbed::INHERIT],
146
            '0b11' => [0b11],
147
        ];
148
    }
149
150
    public function getStatesBOOL()
151
    {
152
        return [
153
            'ALLOW' => [true],
154
            'DISALLOW' => [false],
155
            'INHERIT' => [null],
156
            '0b11' => [null],
157
        ];
158
    }
159
160
    /**
161
     * @dataProvider getStatesBINARY
162
     */
163
    public function testTestsetBitValue($value): void
164
    {
165
        $embed = new PermissionsEmbed();
166
        //Check if it returns itself, for chaining.
167
        $this->assertSame($embed, $embed->setBitValue(PermissionsEmbed::PARTS, 0, $value));
168
        $this->assertSame($value, $embed->getBitValue(PermissionsEmbed::PARTS, 0));
169
    }
170
171
    /**
172
     * @dataProvider getStatesBOOL
173
     */
174
    public function testSetPermissionValue($value): void
175
    {
176
        $embed = new PermissionsEmbed();
177
        //Check if it returns itself, for chaining.
178
        $this->assertSame($embed, $embed->setPermissionValue(PermissionsEmbed::PARTS, 0, $value));
179
        $this->assertSame($value, $embed->getPermissionValue(PermissionsEmbed::PARTS, 0));
180
    }
181
182
    public function testSetRawPermissionValue(): void
183
    {
184
        $embed = new PermissionsEmbed();
185
        $embed->setRawPermissionValue(PermissionsEmbed::PARTS, 10);
186
        $this->assertSame(10, $embed->getRawPermissionValue(PermissionsEmbed::PARTS));
187
    }
188
189
    public function testSetRawPermissionValues(): void
190
    {
191
        $embed = new PermissionsEmbed();
192
        $embed->setRawPermissionValues([
193
            PermissionsEmbed::PARTS => 0,
194
            PermissionsEmbed::USERS => 100,
195
            PermissionsEmbed::CATEGORIES => 1304,
196
        ]);
197
198
        $this->assertSame(0, $embed->getRawPermissionValue(PermissionsEmbed::PARTS));
199
        $this->assertSame(100, $embed->getRawPermissionValue(PermissionsEmbed::USERS));
200
        $this->assertSame(1304, $embed->getRawPermissionValue(PermissionsEmbed::CATEGORIES));
201
202
        //Test second method to pass perm names and values
203
        $embed->setRawPermissionValues(
204
            [PermissionsEmbed::PARTS, PermissionsEmbed::USERS, PermissionsEmbed::CATEGORIES],
205
            [0, 100, 1304]
206
        );
207
208
        $this->assertSame(0, $embed->getRawPermissionValue(PermissionsEmbed::PARTS));
209
        $this->assertSame(100, $embed->getRawPermissionValue(PermissionsEmbed::USERS));
210
        $this->assertSame(1304, $embed->getRawPermissionValue(PermissionsEmbed::CATEGORIES));
211
    }
212
}
213