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\Repository;
24 use Friendica\Profile\ProfileField\Collection\ProfileFields;
25 use Friendica\Profile\ProfileField\Repository\ProfileField as ProfileFieldRepository;
26 use Friendica\Profile\ProfileField\Exception\ProfileFieldPersistenceException;
27 use Friendica\Profile\ProfileField\Factory\ProfileField as ProfileFieldFactory;
28 use Friendica\Security\PermissionSet\Repository\PermissionSet;
29 use Friendica\Security\PermissionSet\Factory\PermissionSet as PermissionSetFactory;
30 use Friendica\Security\PermissionSet\Repository\PermissionSet as PermissionSetRepository;
31 use Friendica\Test\FixtureTest;
34 class ProfileFieldTest extends FixtureTest
36 /** @var ProfileFieldRepository */
38 /** @var ProfileFieldFactory */
40 /** @var PermissionSetFactory */
41 private $permissionSetFactory;
42 /** @var PermissionSetRepository */
43 private $permissionSetRepository;
45 public function setUp(): void
49 $this->depository = DI::profileField();
50 $this->factory = DI::profileFieldFactory();
51 $this->permissionSetFactory = DI::permissionSetFactory();
52 $this->permissionSetRepository = DI::permissionSet();
56 * Test create ProfileField without a valid PermissionSet
58 public function testSavingWithoutPermissionSet()
60 self::expectExceptionMessage('PermissionSet needs to be saved first.');
61 self::expectException(ProfileFieldPersistenceException::class);
63 $profileField = $this->factory->createFromValues(42, 0, 'public', 'value', $this->permissionSetFactory->createFromString(42, '', '<~>'));
65 self::assertEquals($profileField->uid, $profileField->permissionSet->uid);
67 $this->depository->save($profileField);
71 * Test saving a new entity
73 public function testSaveNew()
75 $profileField = $this->factory->createFromValues(42, 0, 'public', 'value', $this->permissionSetRepository->save($this->permissionSetFactory->createFromString(42, '', '<~>')));
77 self::assertEquals($profileField->uid, $profileField->permissionSet->uid);
79 $savedProfileField = $this->depository->save($profileField);
81 self::assertNotNull($savedProfileField->id);
82 self::assertNull($profileField->id);
84 $selectedProfileField = $this->depository->selectOneById($savedProfileField->id);
86 self::assertEquals($savedProfileField, $selectedProfileField);
88 $profileFields = new ProfileFields([$selectedProfileField]);
89 $this->depository->deleteCollection($profileFields);
93 * Test updating the order of a ProfileField
95 public function testUpdateOrder()
97 $profileField = $this->factory->createFromValues(42, 0, 'public', 'value', $this->permissionSetRepository->save($this->permissionSetFactory->createFromString(42, '', '<~>')));
99 self::assertEquals($profileField->uid, $profileField->permissionSet->uid);
101 $savedProfileField = $this->depository->save($profileField);
103 self::assertNotNull($savedProfileField->id);
104 self::assertNull($profileField->id);
106 $selectedProfileField = $this->depository->selectOneById($savedProfileField->id);
108 self::assertEquals($savedProfileField, $selectedProfileField);
110 $selectedProfileField->setOrder(66);
112 $updatedOrderProfileField = $this->depository->save($selectedProfileField);
114 self::assertEquals($selectedProfileField->id, $updatedOrderProfileField->id);
115 self::assertEquals(66, $updatedOrderProfileField->order);
117 // Even using the ID of the old, saved ProfileField returns the right instance
118 $updatedFromOldProfileField = $this->depository->selectOneById($savedProfileField->id);
119 self::assertEquals(66, $updatedFromOldProfileField->order);
121 $profileFields = new ProfileFields([$updatedFromOldProfileField]);
122 $this->depository->deleteCollection($profileFields);
126 * Test updating a whole entity
128 public function testUpdate()
130 $profileField = $this->factory->createFromValues(42, 0, 'public', 'value', $this->permissionSetRepository->save($this->permissionSetFactory->createFromString(42, '', '<~>')));
132 self::assertEquals($profileField->uid, $profileField->permissionSet->uid);
134 $savedProfileField = $this->depository->save($profileField);
136 self::assertNotNull($savedProfileField->id);
137 self::assertNull($profileField->id);
139 $selectedProfileField = $this->depository->selectOneById($savedProfileField->id);
141 self::assertEquals($savedProfileField, $selectedProfileField);
143 $savedProfileField->update('another', 5, $this->permissionSetRepository->selectPublicForUser(42));
144 self::assertEquals(PermissionSet::PUBLIC, $savedProfileField->permissionSet->id);
146 $publicProfileField = $this->depository->save($savedProfileField);
148 self::assertEquals($this->permissionSetRepository->selectPublicForUser(42), $publicProfileField->permissionSet);
149 self::assertEquals('another', $publicProfileField->value);
150 self::assertEquals(5, $publicProfileField->order);
152 $profileFields = new ProfileFields([$publicProfileField]);
153 $this->depository->deleteCollection($profileFields);