TestPermissionHolder::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
/*
3
 * This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
4
 *
5
 *  Copyright (C) 2019 - 2023 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
namespace App\Tests\Services\UserSystem;
22
23
use App\Entity\UserSystem\PermissionData;
24
use App\Security\Interfaces\HasPermissionsInterface;
25
use App\Services\UserSystem\PermissionSchemaUpdater;
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
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
28
29
class TestPermissionHolder implements HasPermissionsInterface
30
{
31
    private PermissionData $perm_data;
32
33
    public function __construct(PermissionData $perm_data)
34
    {
35
        $this->perm_data = $perm_data;
36
    }
37
38
    public function getPermissions(): PermissionData
39
    {
40
        return $this->perm_data;
41
    }
42
}
43
44
class PermissionSchemaUpdaterTest extends WebTestCase
45
{
46
    /**
47
     * @var PermissionSchemaUpdater
48
     */
49
    protected $service;
50
51
    public function setUp(): void
52
    {
53
        parent::setUp();
54
        self::bootKernel();
55
56
        $this->service = self::$container->get(PermissionSchemaUpdater::class);
57
    }
58
59
    public function testIsSchemaUpdateNeeded()
60
    {
61
        $perm_data = new PermissionData();
62
        $perm_data->setSchemaVersion(0);
63
        $user = new TestPermissionHolder($perm_data);
64
65
        //With schema version 0, an update should be needed
66
        self::assertTrue($this->service->isSchemaUpdateNeeded($user));
67
68
        //With a very high scheme number no update should be needed
69
        $perm_data->setSchemaVersion(123456);
70
        self::assertFalse($this->service->isSchemaUpdateNeeded($user));
71
    }
72
73
    public function testUpgradeSchema()
74
    {
75
        $perm_data = new PermissionData();
76
        $perm_data->setSchemaVersion(0);
77
        $user = new TestPermissionHolder($perm_data);
78
79
        //With schema version 0, an update should be done and the schema version should be updated
80
        self::assertTrue($this->service->upgradeSchema($user));
81
        self::assertEquals(PermissionData::CURRENT_SCHEMA_VERSION, $user->getPermissions()->getSchemaVersion());
82
83
        //If we redo it with the same schema version, no update should be done
84
        self::assertFalse($this->service->upgradeSchema($user));
85
    }
86
87
    public function testUpgradeSchemaToVersion1()
88
    {
89
        $perm_data = new PermissionData();
90
        $perm_data->setSchemaVersion(0);
91
        $perm_data->setPermissionValue('parts', 'edit', PermissionData::ALLOW);
92
        $user = new TestPermissionHolder($perm_data);
93
94
        //Do an upgrade and afterwards the move, add, and withdraw permissions should be set to ALLOW
95
        self::assertTrue($this->service->upgradeSchema($user, 1));
96
        self::assertEquals(PermissionData::ALLOW, $user->getPermissions()->getPermissionValue('parts_stock', 'move'));
97
        self::assertEquals(PermissionData::ALLOW, $user->getPermissions()->getPermissionValue('parts_stock', 'add'));
98
        self::assertEquals(PermissionData::ALLOW, $user->getPermissions()->getPermissionValue('parts_stock', 'withdraw'));
99
    }
100
101
    public function testUpgradeSchemaToVersion2()
102
    {
103
        $perm_data = new PermissionData();
104
        $perm_data->setSchemaVersion(1);
105
        $perm_data->setPermissionValue('devices', 'read', PermissionData::ALLOW);
106
        $perm_data->setPermissionValue('devices', 'edit', PermissionData::INHERIT);
107
        $perm_data->setPermissionValue('devices', 'delete', PermissionData::DISALLOW);
108
        $user = new TestPermissionHolder($perm_data);
109
110
        //After the upgrade all operations should be available under the name "projects" with the same values
111
        self::assertTrue($this->service->upgradeSchema($user, 2));
112
        self::assertEquals(PermissionData::ALLOW, $user->getPermissions()->getPermissionValue('projects', 'read'));
113
        self::assertEquals(PermissionData::INHERIT, $user->getPermissions()->getPermissionValue('projects', 'edit'));
114
        self::assertEquals(PermissionData::DISALLOW, $user->getPermissions()->getPermissionValue('projects', 'delete'));
115
    }
116
}
117