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

PermissionsEmbedTest::testSetRawPermissionValues()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

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