]> git.mxchange.org Git - friendica.git/blob - tests/src/Profile/ProfileField/Entity/ProfileFieldTest.php
Merge pull request #12697 from MrPetovan/bug/deprecated
[friendica.git] / tests / src / Profile / ProfileField / Entity / ProfileFieldTest.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\Profile\ProfileField\Entity;
23
24 use Friendica\Profile\ProfileField\Entity\ProfileField;
25 use Friendica\Profile\ProfileField\Exception\ProfileFieldNotFoundException;
26 use Friendica\Profile\ProfileField\Exception\UnexpectedPermissionSetException;
27 use Friendica\Profile\ProfileField\Factory\ProfileField as ProfileFieldFactory;
28 use Friendica\Security\PermissionSet\Repository\PermissionSet as PermissionSetRepository;
29 use Friendica\Security\PermissionSet\Factory\PermissionSet as PermissionSetFactory;
30 use Friendica\Test\MockedTest;
31 use Friendica\Util\ACLFormatter;
32 use Friendica\Util\DateTimeFormat;
33 use Mockery\MockInterface;
34 use Psr\Log\NullLogger;
35
36 class ProfileFieldTest extends MockedTest
37 {
38         /** @var MockInterface|PermissionSetRepository */
39         protected $permissionSetRepository;
40         /** @var ProfileFieldFactory */
41         protected $profileFieldFactory;
42         /** @var MockInterface|PermissionSetFactory */
43         protected $permissionSetFactory;
44
45         protected function setUp(): void
46         {
47                 parent::setUp();
48
49                 $this->permissionSetRepository = \Mockery::mock(PermissionSetRepository::class);
50                 $this->permissionSetFactory    = new PermissionSetFactory(new NullLogger(), new ACLFormatter());
51                 $this->profileFieldFactory     = new ProfileFieldFactory(new NullLogger(), $this->permissionSetFactory);
52         }
53
54         public function dataEntity()
55         {
56                 return [
57                         'default' => [
58                                 'uid'           => 23,
59                                 'order'         => 1,
60                                 'psid'          => 2,
61                                 'label'         => 'test',
62                                 'value'         => 'more',
63                                 'created'       => new \DateTime('2021-10-10T21:12:00.000000+0000', new \DateTimeZone('UTC')),
64                                 'edited'        => new \DateTime('2021-10-10T21:12:00.000000+0000', new \DateTimeZone('UTC')),
65                                 'permissionSet' => [
66                                         'uid'       => 23,
67                                         'allow_cid' => "<1>",
68                                         'allow_gid' => "<~>",
69                                         'deny_cid'  => '<2>',
70                                         'deny_gid'  => '<3>',
71                                         'id'        => 2,
72                                 ]
73                         ],
74                         'withId' => [
75                                 'uid'           => 23,
76                                 'order'         => 1,
77                                 'psid'          => 2,
78                                 'label'         => 'test',
79                                 'value'         => 'more',
80                                 'created'       => new \DateTime('2021-10-10T21:12:00.000000+0000', new \DateTimeZone('UTC')),
81                                 'edited'        => new \DateTime('2021-10-10T21:12:00.000000+0000', new \DateTimeZone('UTC')),
82                                 'permissionSet' => [
83                                         'uid'       => 23,
84                                         'allow_cid' => "<1>",
85                                         'allow_gid' => "<~>",
86                                         'deny_cid'  => '<2>',
87                                         'deny_gid'  => '<3>',
88                                         'id'        => 2,
89                                 ],
90                                 'id' => 54,
91                         ],
92                 ];
93         }
94
95         /**
96          * @dataProvider dataEntity
97          */
98         public function testEntity(int $uid, int $order, int $psid, string $label, string $value, \DateTime $created, \DateTime $edited, array $permissionSet, $id = null)
99         {
100                 $entity = new ProfileField($uid, $order, $label, $value, $created, $edited, $this->permissionSetFactory->createFromTableRow($permissionSet), $id);
101
102                 self::assertEquals($uid, $entity->uid);
103                 self::assertEquals($order, $entity->order);
104                 self::assertEquals($psid, $entity->permissionSet->id);
105                 self::assertEquals($label, $entity->label);
106                 self::assertEquals($value, $entity->value);
107                 self::assertEquals($created, $entity->created);
108                 self::assertEquals($edited, $entity->edited);
109                 self::assertEquals($id, $entity->id);
110         }
111
112         /**
113          * @dataProvider dataEntity
114          */
115         public function testUpdate(int $uid, int $order, int $psid, string $label, string $value, \DateTime $created, \DateTime $edited, array $permissionSet,  $id = null)
116         {
117                 $permissionSet = $this->permissionSetFactory->createFromTableRow(['uid' => 2, 'id' => $psid]);
118
119                 $entity = $this->profileFieldFactory->createFromTableRow([
120                         'uid'     => $uid,
121                         'order'   => $order,
122                         'psid'    => $psid,
123                         'label'   => $label,
124                         'value'   => $value,
125                         'created' => $created->format(DateTimeFormat::MYSQL),
126                         'edited'  => $edited->format(DateTimeFormat::MYSQL),
127                         'id'      => $id,
128                 ], $permissionSet);
129
130                 $permissionSetNew = $this->permissionSetFactory->createFromTableRow([
131                         'uid'       => 2,
132                         'allow_cid' => '<1>',
133                         'id'        => 23
134                 ]);
135
136                 $entity->update('updatedValue', 2345, $permissionSetNew);
137
138                 self::assertEquals($uid, $entity->uid);
139                 self::assertEquals(2345, $entity->order);
140                 self::assertEquals(23, $entity->permissionSet->id);
141                 self::assertEquals($label, $entity->label);
142                 self::assertEquals('updatedValue', $entity->value);
143                 self::assertEquals($created, $entity->created);
144                 self::assertGreaterThan($edited, $entity->edited);
145                 self::assertEquals($id, $entity->id);
146         }
147
148         /**
149          * @dataProvider dataEntity
150          */
151         public function testSetOrder(int $uid, int $order, int $psid, string $label, string $value, \DateTime $created, \DateTime $edited, array $permissionSet, $id = null)
152         {
153                 $permissionSet = $this->permissionSetFactory->createFromTableRow(['uid' => 2, 'id' => $psid]);
154
155                 $entity = $this->profileFieldFactory->createFromTableRow([
156                         'uid'     => $uid,
157                         'order'   => $order,
158                         'psid'    => $psid,
159                         'label'   => $label,
160                         'value'   => $value,
161                         'created' => $created->format(DateTimeFormat::MYSQL),
162                         'edited'  => $edited->format(DateTimeFormat::MYSQL),
163                         'id'      => $id,
164                 ], $permissionSet);
165
166                 $entity->setOrder(2345);
167
168                 self::assertEquals($uid, $entity->uid);
169                 self::assertEquals(2345, $entity->order);
170                 self::assertEquals($psid, $entity->permissionSet->id);
171                 self::assertEquals($label, $entity->label);
172                 self::assertEquals($value, $entity->value);
173                 self::assertEquals($created, $entity->created);
174                 self::assertGreaterThan($edited, $entity->edited);
175                 self::assertEquals($id, $entity->id);
176         }
177
178         /**
179          * Test the exception because of a wrong property
180          *
181          * @dataProvider dataEntity
182          */
183         public function testWrongGet(int $uid, int $order, int $psid, string $label, string $value, \DateTime $created, \DateTime $edited, array $permissionSet, $id = null)
184         {
185                 $entity = new ProfileField($uid, $order, $label, $value, $created, $edited, $this->permissionSetFactory->createFromTableRow($permissionSet), $id);
186
187                 self::expectException(ProfileFieldNotFoundException::class);
188                 $entity->wrong;
189         }
190
191         /**
192          * Test gathering the permissionset
193          *
194          * @dataProvider dataEntity
195          */
196         public function testPermissionSet(int $uid, int $order, int $psid, string $label, string $value, \DateTime $created, \DateTime $edited, array $permissionSet, $id = null)
197         {
198                 $entity = new ProfileField($uid, $order, $label, $value, $created, $edited, $this->permissionSetFactory->createFromTableRow($permissionSet), $id);
199
200                 $permissionSet = $this->permissionSetFactory->createFromTableRow(['uid' => $uid, 'id' => $psid]);
201
202                 $this->permissionSetRepository->shouldReceive('selectOneById')->with($psid, $uid)->andReturns($permissionSet);
203
204                 self::assertEquals($psid, $entity->permissionSet->id);
205         }
206
207         /**
208          * Test the exception because the factory cannot find a permissionSet ID, nor the permissionSet itself
209          *
210          * @dataProvider dataEntity
211          */
212         public function testMissingPermissionFactory(int $uid, int $order, int $psid, string $label, string $value, \DateTime $created, \DateTime $edited, array $permissionSet, $id = null)
213         {
214                 self::expectException(UnexpectedPermissionSetException::class);
215                 self::expectExceptionMessage('Either set the PermissionSet fields (join) or the PermissionSet itself');
216
217                 $entity = $this->profileFieldFactory->createFromTableRow([
218                         'uid'     => $uid,
219                         'order'   => $order,
220                         'label'   => $label,
221                         'value'   => $value,
222                         'created' => $created->format(DateTimeFormat::MYSQL),
223                         'edited'  => $edited->format(DateTimeFormat::MYSQL),
224                         'id'      => $id,
225                 ]);
226         }
227 }