diff --git a/src/BaseCollection.php b/src/BaseCollection.php index 1aa13ae96..d2212e244 100644 --- a/src/BaseCollection.php +++ b/src/BaseCollection.php @@ -87,7 +87,7 @@ class BaseCollection extends \ArrayIterator */ public function column($column, $index_key = null) { - return array_column($this->getArrayCopy(), $column, $index_key); + return array_column($this->getArrayCopy(true), $column, $index_key); } /** @@ -124,4 +124,21 @@ class BaseCollection extends \ArrayIterator { return new static(array_reverse($this->getArrayCopy()), $this->getTotalCount()); } + + /** + * @inheritDoc + * + * includes recursion for entity::toArray() function + * @see BaseEntity::toArray() + */ + public function getArrayCopy(bool $recursive = false): array + { + if (!$recursive) { + return parent::getArrayCopy(); + } + + return array_map(function ($item) { + return is_object($item) ? $item->toArray() : $item; + }, parent::getArrayCopy()); + } } diff --git a/src/Module/Settings/Profile/Index.php b/src/Module/Settings/Profile/Index.php index 92d6356b8..efb3ac9d3 100644 --- a/src/Module/Settings/Profile/Index.php +++ b/src/Module/Settings/Profile/Index.php @@ -287,6 +287,9 @@ class Index extends BaseSettings $profileFieldInputs['new']['value'], $permissionSet )); + + unset($profileFieldInputs['new']); + unset($profileFieldOrder['new']); } foreach ($profileFieldInputs as $id => $profileFieldInput) { diff --git a/src/Profile/ProfileField/Depository/ProfileField.php b/src/Profile/ProfileField/Depository/ProfileField.php index 228fc7760..590e3f45a 100644 --- a/src/Profile/ProfileField/Depository/ProfileField.php +++ b/src/Profile/ProfileField/Depository/ProfileField.php @@ -30,6 +30,7 @@ use Friendica\Profile\ProfileField\Factory; use Friendica\Profile\ProfileField\Entity; use Friendica\Profile\ProfileField\Collection; use Friendica\Security\PermissionSet\Depository\PermissionSet as PermissionSetDepository; +use Friendica\Util\DateTimeFormat; use Psr\Log\LoggerInterface; class ProfileField extends BaseDepository @@ -46,7 +47,7 @@ class ProfileField extends BaseDepository { parent::__construct($database, $logger, $factory); - $this->permissionSetDepository = permissionSetDepository; + $this->permissionSetDepository = $permissionSetDepository; } /** @@ -91,11 +92,13 @@ class ProfileField extends BaseDepository protected function convertToTableRow(Entity\ProfileField $profileField): array { return [ + 'uid' => $profileField->uid, 'label' => $profileField->label, 'value' => $profileField->value, 'order' => $profileField->order, - 'created' => $profileField->created, - 'edited' => $profileField->edited, + 'created' => $profileField->created->format(DateTimeFormat::MYSQL), + 'edited' => $profileField->edited->format(DateTimeFormat::MYSQL), + 'psid' => $profileField->permissionSetId ]; } @@ -205,7 +208,7 @@ class ProfileField extends BaseDepository try { if ($profileField->id) { - $this->db->update(self::$table_name, $fields, ['id' => $profileField]); + $this->db->update(self::$table_name, $fields, ['id' => $profileField->id]); } else { $this->db->insert(self::$table_name, $fields); @@ -233,7 +236,7 @@ class ProfileField extends BaseDepository // Update the order based on the new Profile Field Collection $order = 0; - $labelProfileFieldsOld = $profileFieldsOld->column('id', 'label'); + $labelProfileFieldsOld = array_flip($profileFieldsOld->column('label')); foreach ($profileFields as $profileField) { // Update existing field (preserve diff --git a/src/Profile/ProfileField/Entity/ProfileField.php b/src/Profile/ProfileField/Entity/ProfileField.php index deab955d3..c46ad12de 100644 --- a/src/Profile/ProfileField/Entity/ProfileField.php +++ b/src/Profile/ProfileField/Entity/ProfileField.php @@ -58,7 +58,7 @@ class ProfileField extends BaseEntity /** @var int */ protected $order; /** @var int */ - protected $psid; + protected $permissionSetId; /** @var string */ protected $label; /** @var string */ @@ -68,6 +68,9 @@ class ProfileField extends BaseEntity /** @var \DateTime */ protected $edited; + /** + * @throws UnexpectedPermissionSetException In case no Permission Set can be retrieved + */ public function __construct(PermissionSetDepository $permissionSetDepository, int $uid, int $order, int $permissionSetId, string $label, string $value, \DateTime $created, \DateTime $edited, int $id = null, PermissionSet $permissionSet = null) { $this->permissionSetDepository = $permissionSetDepository; @@ -75,12 +78,16 @@ class ProfileField extends BaseEntity $this->uid = $uid; $this->order = $order; - $this->psid = $permissionSetId; + $this->permissionSetId = $permissionSetId ?? ($permissionSet ? $permissionSet->id : null); $this->label = $label; $this->value = $value; $this->created = $created; $this->edited = $edited; $this->id = $id; + + if (is_null($this->permissionSetId)) { + throw new UnexpectedPermissionSetException('Either set the permission set ID or the permission set itself'); + } } /** @@ -93,7 +100,7 @@ class ProfileField extends BaseEntity case 'permissionSet': if (empty($this->permissionSet)) { try { - $permissionSet = $this->permissionSetDepository->selectOneById($this->psid, $this->uid); + $permissionSet = $this->permissionSetDepository->selectOneById($this->permissionSetId, $this->uid); if ($permissionSet->uid !== $this->uid) { throw new UnexpectedPermissionSetException(sprintf('PermissionSet %d (user-id: %d) for ProfileField %d (user-id: %d) is invalid.', $permissionSet->id, $permissionSet->uid, $this->id, $this->uid)); } @@ -130,7 +137,7 @@ class ProfileField extends BaseEntity $this->value = $value; $this->order = $order; $this->permissionSet = $permissionSet; - $this->psid = $permissionSet->id; + $this->permissionSetId = $permissionSet->id; $this->edited = new \DateTime('now', new \DateTimeZone('UTC')); } diff --git a/tests/Util/CollectionDouble.php b/tests/Util/CollectionDouble.php new file mode 100644 index 000000000..ed5e371e4 --- /dev/null +++ b/tests/Util/CollectionDouble.php @@ -0,0 +1,9 @@ +<?php + +namespace Friendica\Test\Util; + +use Friendica\BaseCollection; + +class CollectionDouble extends BaseCollection +{ +} diff --git a/tests/Util/EntityDouble.php b/tests/Util/EntityDouble.php new file mode 100644 index 000000000..414e96dc9 --- /dev/null +++ b/tests/Util/EntityDouble.php @@ -0,0 +1,26 @@ +<?php + +namespace Friendica\Test\Util; + +use Friendica\BaseEntity; + +/** + * @property-read string $protString + * @property-read int $protInt + * @property-read \DateTime $protDateTime + */ +class EntityDouble extends BaseEntity +{ + protected $protString; + protected $protInt; + protected $protDateTime; + private $privString; + + public function __construct(string $protString, int $protInt, \DateTime $protDateTime, string $privString) + { + $this->protString = $protString; + $this->protInt = $protInt; + $this->protDateTime = $protDateTime; + $this->privString = $privString; + } +} diff --git a/tests/src/CollectionTest.php b/tests/src/CollectionTest.php new file mode 100644 index 000000000..9e198c985 --- /dev/null +++ b/tests/src/CollectionTest.php @@ -0,0 +1,24 @@ +<?php + +namespace Friendica\Test\src; + +use Friendica\Test\MockedTest; +use Friendica\Test\Util\CollectionDouble; +use Friendica\Test\Util\EntityDouble; + +class CollectionTest extends MockedTest +{ + /** + * Test if the BaseCollection::column() works as expected + */ + public function testGetArrayCopy() + { + $collection = new CollectionDouble(); + $collection->append(new EntityDouble('test', 23, new \DateTime('now', new \DateTimeZone('UTC')), 'privTest')); + $collection->append(new EntityDouble('test2', 25, new \DateTime('now', new \DateTimeZone('UTC')), 'privTest23')); + + self::assertEquals(['test', 'test2'], $collection->column('protString')); + self::assertEmpty($collection->column('privString')); + self::assertEquals([23,25], $collection->column('protInt')); + } +}