From 7c0c102f9fd04ee1c6b29a6499ed01d87c39ebae Mon Sep 17 00:00:00 2001 From: Philipp Date: Sun, 10 Oct 2021 23:30:21 +0200 Subject: [PATCH] Add tests --- .../ProfileField/Entity/ProfileField.php | 35 ++- .../ProfileField/Factory/ProfileField.php | 9 +- .../ProfileField/Entity/ProfileFieldTest.php | 223 ++++++++++++++++++ 3 files changed, 244 insertions(+), 23 deletions(-) create mode 100644 tests/src/Profile/ProfileField/Entity/ProfileFieldTest.php diff --git a/src/Profile/ProfileField/Entity/ProfileField.php b/src/Profile/ProfileField/Entity/ProfileField.php index c46ad12de8..f3cb33ae95 100644 --- a/src/Profile/ProfileField/Entity/ProfileField.php +++ b/src/Profile/ProfileField/Entity/ProfileField.php @@ -30,7 +30,7 @@ use Friendica\Security\PermissionSet\Depository\PermissionSet as PermissionSetDe use Friendica\Security\PermissionSet\Entity\PermissionSet; /** - * Custom profile field model class. + * Custom profile field entity class. * * Custom profile fields are user-created arbitrary profile fields that can be assigned a permission set to restrict its * display to specific Friendica contacts as it requires magic authentication to work. @@ -68,26 +68,19 @@ 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; $this->permissionSet = $permissionSet; - $this->uid = $uid; - $this->order = $order; - $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'); - } + $this->uid = $uid; + $this->order = $order; + $this->permissionSetId = $permissionSetId; + $this->label = $label; + $this->value = $value; + $this->created = $created; + $this->edited = $edited; + $this->id = $id; } /** @@ -134,11 +127,11 @@ class ProfileField extends BaseEntity */ public function update(string $value, int $order, PermissionSet $permissionSet) { - $this->value = $value; - $this->order = $order; - $this->permissionSet = $permissionSet; - $this->permissionSetId = $permissionSet->id; - $this->edited = new \DateTime('now', new \DateTimeZone('UTC')); + $this->value = $value; + $this->order = $order; + $this->permissionSet = $permissionSet; + $this->permissionSetId = $permissionSet->id; + $this->edited = new \DateTime('now', new \DateTimeZone('UTC')); } /** diff --git a/src/Profile/ProfileField/Factory/ProfileField.php b/src/Profile/ProfileField/Factory/ProfileField.php index caec34af0b..e7016ec280 100644 --- a/src/Profile/ProfileField/Factory/ProfileField.php +++ b/src/Profile/ProfileField/Factory/ProfileField.php @@ -22,6 +22,7 @@ namespace Friendica\Profile\ProfileField\Factory; use Friendica\BaseFactory; +use Friendica\Profile\ProfileField\Exception\UnexpectedPermissionSetException; use Friendica\Security\PermissionSet\Depository\PermissionSet as PermissionSetDepository; use Friendica\Profile\ProfileField\Entity; use Friendica\Capabilities\ICanCreateFromTableRow; @@ -45,16 +46,20 @@ class ProfileField extends BaseFactory implements ICanCreateFromTableRow */ public function createFromTableRow(array $row, PermissionSet $permissionSet = null): Entity\ProfileField { + if (empty($permissionSet) && empty($row['psid'])) { + throw new UnexpectedPermissionSetException('Either set the permission set ID or the permission set itself'); + } + return new Entity\ProfileField( $this->permissionSetDepository, $row['uid'], $row['order'], - $row['psid'], + $row['psid'] ?? $permissionSet->id, $row['label'], $row['value'], new \DateTime($row['created'] ?? 'now', new \DateTimeZone('UTC')), new \DateTime($row['edited'] ?? 'now', new \DateTimeZone('UTC')), - $row['id'], + $row['id'] ?? null, $permissionSet ); } diff --git a/tests/src/Profile/ProfileField/Entity/ProfileFieldTest.php b/tests/src/Profile/ProfileField/Entity/ProfileFieldTest.php new file mode 100644 index 0000000000..40f06a0890 --- /dev/null +++ b/tests/src/Profile/ProfileField/Entity/ProfileFieldTest.php @@ -0,0 +1,223 @@ +permissionSetDepository = \Mockery::mock(PermissionSetDepository::class); + $this->profileFieldFactory = new ProfileFieldFactory(new VoidLogger(), $this->permissionSetDepository); + $this->permissionSetFactory = new PermissionSetFactory(new VoidLogger(), new ACLFormatter()); + } + + public function dataEntity() + { + return [ + 'default' => [ + 'uid' => 23, + 'order' => 1, + 'psid' => 2, + 'label' => 'test', + 'value' => 'more', + 'created' => new \DateTime('2021-10-10T21:12:00.000000+0000', new \DateTimeZone('UTC')), + 'edited' => new \DateTime('2021-10-10T21:12:00.000000+0000', new \DateTimeZone('UTC')), + ], + 'withId' => [ + 'uid' => 23, + 'order' => 1, + 'psid' => 2, + 'label' => 'test', + 'value' => 'more', + 'created' => new \DateTime('2021-10-10T21:12:00.000000+0000', new \DateTimeZone('UTC')), + 'edited' => new \DateTime('2021-10-10T21:12:00.000000+0000', new \DateTimeZone('UTC')), + 'id' => 54, + ], + ]; + } + + /** + * @dataProvider dataEntity + */ + public function testEntity(int $uid, int $order, int $psid, string $label, string $value, \DateTime $created, \DateTime $edited, $id = null) + { + $entity = new ProfileField($this->permissionSetDepository, $uid, $order, $psid, $label, $value, $created, $edited, $id); + + self::assertEquals($uid, $entity->uid); + self::assertEquals($order, $entity->order); + self::assertEquals($psid, $entity->permissionSetId); + self::assertEquals($label, $entity->label); + self::assertEquals($value, $entity->value); + self::assertEquals($created, $entity->created); + self::assertEquals($edited, $entity->edited); + self::assertEquals($id, $entity->id); + } + + /** + * @dataProvider dataEntity + */ + public function testUpdate(int $uid, int $order, int $psid, string $label, string $value, \DateTime $created, \DateTime $edited, $id = null) + { + $permissionSet = $this->permissionSetFactory->createFromTableRow(['uid' => 2, 'id' => $psid]); + + $entity = $this->profileFieldFactory->createFromTableRow([ + 'uid' => $uid, + 'order' => $order, + 'psid' => $psid, + 'label' => $label, + 'value' => $value, + 'created' => $created->format(DateTimeFormat::MYSQL), + 'edited' => $edited->format(DateTimeFormat::MYSQL), + 'id' => $id, + ], $permissionSet); + + $permissionSetNew = $this->permissionSetFactory->createFromTableRow(['uid' => 2, 'allow_cid' => '<1>', + 'id' => 23]); + + $entity->update('updatedValue', 2345, $permissionSetNew); + + self::assertEquals($uid, $entity->uid); + self::assertEquals(2345, $entity->order); + self::assertEquals(23, $entity->permissionSetId); + self::assertEquals($label, $entity->label); + self::assertEquals('updatedValue', $entity->value); + self::assertEquals($created, $entity->created); + self::assertGreaterThan($edited, $entity->edited); + self::assertEquals($id, $entity->id); + } + + /** + * @dataProvider dataEntity + */ + public function testSetOrder(int $uid, int $order, int $psid, string $label, string $value, \DateTime $created, \DateTime $edited, $id = null) + { + $permissionSet = $this->permissionSetFactory->createFromTableRow(['uid' => 2, 'id' => $psid]); + + $entity = $this->profileFieldFactory->createFromTableRow([ + 'uid' => $uid, + 'order' => $order, + 'psid' => $psid, + 'label' => $label, + 'value' => $value, + 'created' => $created->format(DateTimeFormat::MYSQL), + 'edited' => $edited->format(DateTimeFormat::MYSQL), + 'id' => $id, + ], $permissionSet); + + $entity->setOrder(2345); + + self::assertEquals($uid, $entity->uid); + self::assertEquals(2345, $entity->order); + self::assertEquals($psid, $entity->permissionSetId); + self::assertEquals($label, $entity->label); + self::assertEquals($value, $entity->value); + self::assertEquals($created, $entity->created); + self::assertGreaterThan($edited, $entity->edited); + self::assertEquals($id, $entity->id); + } + + /** + * Test the exception because of a wrong property + * + * @dataProvider dataEntity + */ + public function testWrongGet(int $uid, int $order, int $psid, string $label, string $value, \DateTime $created, \DateTime $edited, $id = null) + { + $entity = new ProfileField($this->permissionSetDepository, $uid, $order, $psid, $label, $value, $created, $edited, $id); + + self::expectException(ProfileFieldNotFoundException::class); + $entity->wrong; + } + + /** + * Test gathering the permissionset + * + * @dataProvider dataEntity + */ + public function testPermissionSet(int $uid, int $order, int $psid, string $label, string $value, \DateTime $created, \DateTime $edited, $id = null) + { + $entity = new ProfileField($this->permissionSetDepository, $uid, $order, $psid, $label, $value, $created, $edited, $id); + $permissionSet = $this->permissionSetFactory->createFromTableRow(['uid' => $uid, 'id' => $psid]); + + $this->permissionSetDepository->shouldReceive('selectOneById')->with($psid)->andReturns($permissionSet); + + self::assertEquals($psid, $entity->permissionSet->id); + } + + /** + * Test the exception because of incompatible user id + * + * @dataProvider dataEntity + */ + public function testWrongPermissionSet(int $uid, int $order, int $psid, string $label, string $value, \DateTime $created, \DateTime $edited, $id = null) + { + $entity = new ProfileField($this->permissionSetDepository, $uid, $order, $psid, $label, $value, $created, $edited, $id); + $permissionSet = $this->permissionSetFactory->createFromTableRow(['uid' => 12345, 'id' => $psid]); + + $this->permissionSetDepository->shouldReceive('selectOneById')->with($psid)->andReturns($permissionSet); + + self::expectException(UnexpectedPermissionSetException::class); + self::expectExceptionMessage(sprintf('PermissionSet %d (user-id: %d) for ProfileField %d (user-id: %d) is invalid.', $psid, 12345, $id, $uid)); + $entity->permissionSet; + } + + /** + * Test the exception because of missing permission set + * + * @dataProvider dataEntity + */ + public function testMissingPermissionSet(int $uid, int $order, int $psid, string $label, string $value, \DateTime $created, \DateTime $edited, $id = null) + { + $entity = new ProfileField($this->permissionSetDepository, $uid, $order, $psid, $label, $value, $created, $edited, $id); + + $this->permissionSetDepository->shouldReceive('selectOneById')->with($psid) + ->andThrow(new NotFoundException('test')); + + self::expectException(UnexpectedPermissionSetException::class); + self::expectExceptionMessage(sprintf('No PermissionSet found for ProfileField %d (user-id: %d).', $id, $uid)); + $entity->permissionSet; + } + + /** + * Test the exception because the factory cannot find a permissionSet ID, nor the permissionSet itself + * + * @dataProvider dataEntity + */ + public function testMissingPermissionFactory(int $uid, int $order, int $psid, string $label, string $value, \DateTime $created, \DateTime $edited, $id = null) + { + self::expectException(UnexpectedPermissionSetException::class); + self::expectExceptionMessage('Either set the permission set ID or the permission set itself'); + + $entity = $this->profileFieldFactory->createFromTableRow([ + 'uid' => $uid, + 'order' => $order, + 'label' => $label, + 'value' => $value, + 'created' => $created->format(DateTimeFormat::MYSQL), + 'edited' => $edited->format(DateTimeFormat::MYSQL), + 'id' => $id, + ]); + } +}