3 * @copyright Copyright (C) 2010-2023, the Friendica project
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 namespace Friendica\Test\src\Profile\ProfileField\Entity;
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;
36 class ProfileFieldTest extends MockedTest
38 /** @var MockInterface|PermissionSetRepository */
39 protected $permissionSetRepository;
40 /** @var ProfileFieldFactory */
41 protected $profileFieldFactory;
42 /** @var MockInterface|PermissionSetFactory */
43 protected $permissionSetFactory;
45 protected function setUp(): void
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);
54 public function dataEntity()
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')),
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')),
96 * @dataProvider dataEntity
98 public function testEntity(int $uid, int $order, int $psid, string $label, string $value, \DateTime $created, \DateTime $edited, array $permissionSet, $id = null)
100 $entity = new ProfileField($uid, $order, $label, $value, $created, $edited, $this->permissionSetFactory->createFromTableRow($permissionSet), $id);
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);
113 * @dataProvider dataEntity
115 public function testUpdate(int $uid, int $order, int $psid, string $label, string $value, \DateTime $created, \DateTime $edited, array $permissionSet, $id = null)
117 $permissionSet = $this->permissionSetFactory->createFromTableRow(['uid' => 2, 'id' => $psid]);
119 $entity = $this->profileFieldFactory->createFromTableRow([
125 'created' => $created->format(DateTimeFormat::MYSQL),
126 'edited' => $edited->format(DateTimeFormat::MYSQL),
130 $permissionSetNew = $this->permissionSetFactory->createFromTableRow([
132 'allow_cid' => '<1>',
136 $entity->update('updatedValue', 2345, $permissionSetNew);
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);
149 * @dataProvider dataEntity
151 public function testSetOrder(int $uid, int $order, int $psid, string $label, string $value, \DateTime $created, \DateTime $edited, array $permissionSet, $id = null)
153 $permissionSet = $this->permissionSetFactory->createFromTableRow(['uid' => 2, 'id' => $psid]);
155 $entity = $this->profileFieldFactory->createFromTableRow([
161 'created' => $created->format(DateTimeFormat::MYSQL),
162 'edited' => $edited->format(DateTimeFormat::MYSQL),
166 $entity->setOrder(2345);
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);
179 * Test the exception because of a wrong property
181 * @dataProvider dataEntity
183 public function testWrongGet(int $uid, int $order, int $psid, string $label, string $value, \DateTime $created, \DateTime $edited, array $permissionSet, $id = null)
185 $entity = new ProfileField($uid, $order, $label, $value, $created, $edited, $this->permissionSetFactory->createFromTableRow($permissionSet), $id);
187 self::expectException(ProfileFieldNotFoundException::class);
192 * Test gathering the permissionset
194 * @dataProvider dataEntity
196 public function testPermissionSet(int $uid, int $order, int $psid, string $label, string $value, \DateTime $created, \DateTime $edited, array $permissionSet, $id = null)
198 $entity = new ProfileField($uid, $order, $label, $value, $created, $edited, $this->permissionSetFactory->createFromTableRow($permissionSet), $id);
200 $permissionSet = $this->permissionSetFactory->createFromTableRow(['uid' => $uid, 'id' => $psid]);
202 $this->permissionSetRepository->shouldReceive('selectOneById')->with($psid, $uid)->andReturns($permissionSet);
204 self::assertEquals($psid, $entity->permissionSet->id);
208 * Test the exception because the factory cannot find a permissionSet ID, nor the permissionSet itself
210 * @dataProvider dataEntity
212 public function testMissingPermissionFactory(int $uid, int $order, int $psid, string $label, string $value, \DateTime $created, \DateTime $edited, array $permissionSet, $id = null)
214 self::expectException(UnexpectedPermissionSetException::class);
215 self::expectExceptionMessage('Either set the PermissionSet fields (join) or the PermissionSet itself');
217 $entity = $this->profileFieldFactory->createFromTableRow([
222 'created' => $created->format(DateTimeFormat::MYSQL),
223 'edited' => $edited->format(DateTimeFormat::MYSQL),