]> git.mxchange.org Git - friendica.git/blob - tests/src/Security/PermissionSet/Factory/PermissionSetTest.php
Add yet another case to DateTimeFormat::fix
[friendica.git] / tests / src / Security / PermissionSet / Factory / PermissionSetTest.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
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
9  * published by the Free Software Foundation, either version 3 of the
10  * 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 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
22 namespace Friendica\Test\src\Security\PermissionSet\Factory;
23
24 use Friendica\Security\PermissionSet\Factory\PermissionSet;
25 use Friendica\Test\MockedTest;
26 use Friendica\Util\ACLFormatter;
27 use Psr\Log\NullLogger;
28
29 class PermissionSetTest extends MockedTest
30 {
31         /** @var PermissionSet */
32         protected $permissionSet;
33
34         protected function setUp(): void
35         {
36                 parent::setUp();
37
38                 $this->permissionSet = new PermissionSet(new NullLogger(), new ACLFormatter());
39         }
40
41         public function dataInput()
42         {
43                 return [
44                         'new' => [
45                                 'input' => [
46                                         'uid'       => 12,
47                                         'allow_cid' => '<1>,<2>',
48                                         'allow_gid' => '<3>,<4>',
49                                         'deny_cid'  => '<6>',
50                                         'deny_gid'  => '<8>',
51                                 ],
52                                 'assertion' => [
53                                         'id'        => null,
54                                         'uid'       => 12,
55                                         'allow_cid' => ['1', '2'],
56                                         'allow_gid' => ['3', '4'],
57                                         'deny_cid'  => ['6'],
58                                         'deny_gid'  => ['8'],
59                                 ],
60                         ],
61                         'full' => [
62                                 'input' => [
63                                         'id'        => 3,
64                                         'uid'       => 12,
65                                         'allow_cid' => '<1>,<2>',
66                                         'allow_gid' => '<3>,<4>',
67                                         'deny_cid'  => '<6>',
68                                         'deny_gid'  => '<8>',
69                                 ],
70                                 'assertion' => [
71                                         'id'        => 3,
72                                         'uid'       => 12,
73                                         'allow_cid' => ['1', '2'],
74                                         'allow_gid' => ['3', '4'],
75                                         'deny_cid'  => ['6'],
76                                         'deny_gid'  => ['8'],
77                                 ],
78                         ],
79                         'mini' => [
80                                 'input' => [
81                                         'id'  => null,
82                                         'uid' => 12,
83                                 ],
84                                 'assertion' => [
85                                         'id'        => null,
86                                         'uid'       => 12,
87                                         'allow_cid' => [],
88                                         'allow_gid' => [],
89                                         'deny_cid'  => [],
90                                         'deny_gid'  => [],
91                                 ],
92                         ],
93                         'wrong' => [
94                                 'input' => [
95                                         'id'        => 3,
96                                         'uid'       => 12,
97                                         'allow_cid' => '<1,<2>',
98                                 ],
99                                 'assertion' => [
100                                         'id'        => 3,
101                                         'uid'       => 12,
102                                         'allow_cid' => ['2'],
103                                         'allow_gid' => [],
104                                         'deny_cid'  => [],
105                                         'deny_gid'  => [],
106                                 ],
107                         ]
108                 ];
109         }
110
111         protected function assertPermissionSet(\Friendica\Security\PermissionSet\Entity\PermissionSet $permissionSet, array $assertion)
112         {
113                 self::assertEquals($assertion['id'] ?? null, $permissionSet->id);
114                 self::assertNotNull($permissionSet->uid);
115                 self::assertEquals($assertion['uid'], $permissionSet->uid);
116                 self::assertEquals($assertion['allow_cid'], $permissionSet->allow_cid);
117                 self::assertEquals($assertion['allow_gid'], $permissionSet->allow_gid);
118                 self::assertEquals($assertion['deny_cid'], $permissionSet->deny_cid);
119                 self::assertEquals($assertion['deny_gid'], $permissionSet->deny_gid);
120         }
121
122         /**
123          * Test the createFromTableRow method
124          *
125          * @dataProvider dataInput
126          */
127         public function testCreateFromTableRow(array $input, array $assertion)
128         {
129                 $permissionSet = $this->permissionSet->createFromTableRow($input);
130
131                 $this->assertPermissionSet($permissionSet, $assertion);
132         }
133
134         /**
135          * Test the createFromString method
136          *
137          * @dataProvider dataInput
138          */
139         public function testCreateFromString(array $input, array $assertion)
140         {
141                 $permissionSet = $this->permissionSet->createFromString(
142                         $input['uid'],
143                         $input['allow_cid'] ?? '',
144                         $input['allow_gid'] ?? '',
145                         $input['deny_cid'] ?? '',
146                         $input['deny_gid'] ?? ''
147                 );
148
149                 unset($assertion['id']);
150
151                 $this->assertPermissionSet($permissionSet, $assertion);
152         }
153 }