Adapt ProfileField & Fix current PermissionSet Usage at ProfileFields

This commit is contained in:
Philipp 2021-10-17 15:32:50 +02:00
parent 8a354dac82
commit 5fecc9ecf7
No known key found for this signature in database
GPG key ID: 24A7501396EB5432
11 changed files with 145 additions and 144 deletions

View file

@ -279,7 +279,7 @@ class UserImport
$profile['id'] = DBA::lastInsertId(); $profile['id'] = DBA::lastInsertId();
} }
Profile::migrateFromLegacyProfile($profile); Profile::migrate($profile);
} }
$permissionSet = DI::permissionSet()->selectDefaultForUser($newuid); $permissionSet = DI::permissionSet()->selectDefaultForUser($newuid);

View file

@ -945,7 +945,7 @@ class Profile
* @param array $profile One profile array * @param array $profile One profile array
* @throws \Exception * @throws \Exception
*/ */
public static function migrateFromLegacyProfile(array $profile) public static function migrate(array $profile)
{ {
// Already processed, aborting // Already processed, aborting
if ($profile['is-default'] === null) { if ($profile['is-default'] === null) {
@ -999,7 +999,7 @@ class Profile
foreach ($custom_fields as $field => $label) { foreach ($custom_fields as $field => $label) {
if (!empty($profile[$field]) && $profile[$field] > DBA::NULL_DATE && $profile[$field] > DBA::NULL_DATETIME) { if (!empty($profile[$field]) && $profile[$field] > DBA::NULL_DATE && $profile[$field] > DBA::NULL_DATETIME) {
DI::profileField()->save(DI::profileFieldFactory()->createFromString( DI::profileField()->save(DI::profileFieldFactory()->createFromValues(
$profile['uid'], $profile['uid'],
$order, $order,
trim($label, ':'), trim($label, ':'),

View file

@ -274,13 +274,13 @@ class Index extends BaseSettings
if (!empty($profileFieldInputs['new']['label'])) { if (!empty($profileFieldInputs['new']['label'])) {
$permissionSet = DI::permissionSet()->selectOrCreate(DI::permissionSetFactory()->createFromString( $permissionSet = DI::permissionSet()->selectOrCreate(DI::permissionSetFactory()->createFromString(
$uid, $uid,
$profileFieldInputs['new']['contact_allow'] ?? '', DI::aclFormatter()->toString($profileFieldInputs['new']['contact_allow'] ?? ''),
$profileFieldInputs['new']['group_allow'] ?? '', DI::aclFormatter()->toString($profileFieldInputs['new']['group_allow'] ?? ''),
$profileFieldInputs['new']['contact_deny'] ?? '', DI::aclFormatter()->toString($profileFieldInputs['new']['contact_deny'] ?? ''),
$profileFieldInputs['new']['group_deny'] ?? '' DI::aclFormatter()->toString($profileFieldInputs['new']['group_deny'] ?? '')
)); ));
$profileFields->append(DI::profileFieldFactory()->createFromString( $profileFields->append(DI::profileFieldFactory()->createFromValues(
$uid, $uid,
$profileFieldOrder['new'], $profileFieldOrder['new'],
$profileFieldInputs['new']['label'], $profileFieldInputs['new']['label'],
@ -295,13 +295,13 @@ class Index extends BaseSettings
foreach ($profileFieldInputs as $id => $profileFieldInput) { foreach ($profileFieldInputs as $id => $profileFieldInput) {
$permissionSet = DI::permissionSet()->selectOrCreate(DI::permissionSetFactory()->createFromString( $permissionSet = DI::permissionSet()->selectOrCreate(DI::permissionSetFactory()->createFromString(
$uid, $uid,
$profileFieldInput['contact_allow'] ?? '', DI::aclFormatter()->toString($profileFieldInput['contact_allow'] ?? ''),
$profileFieldInput['group_allow'] ?? '', DI::aclFormatter()->toString($profileFieldInput['group_allow'] ?? ''),
$profileFieldInput['contact_deny'] ?? '', DI::aclFormatter()->toString($profileFieldInput['contact_deny'] ?? ''),
$profileFieldInput['group_deny'] ?? '' DI::aclFormatter()->toString($profileFieldInput['group_deny'] ?? '')
)); ));
$profileFields->append(DI::profileFieldFactory()->createFromString( $profileFields->append(DI::profileFieldFactory()->createFromValues(
$uid, $uid,
$profileFieldOrder[$id], $profileFieldOrder[$id],
$profileFieldInput['label'], $profileFieldInput['label'],

View file

@ -35,7 +35,7 @@ class ProfileFields extends BaseCollection
* @param callable $callback * @param callable $callback
* @return ProfileFields * @return ProfileFields
*/ */
public function map(callable $callback): self public function map(callable $callback): ProfileFields
{ {
return parent::map($callback); return parent::map($callback);
} }
@ -45,7 +45,7 @@ class ProfileFields extends BaseCollection
* @param int $flag * @param int $flag
* @return ProfileFields * @return ProfileFields
*/ */
public function filter(callable $callback = null, int $flag = 0): self public function filter(callable $callback = null, int $flag = 0): ProfileFields
{ {
return parent::filter($callback, $flag); return parent::filter($callback, $flag);
} }

View file

@ -23,9 +23,10 @@ namespace Friendica\Profile\ProfileField\Depository;
use Friendica\BaseDepository; use Friendica\BaseDepository;
use Friendica\Database\Database; use Friendica\Database\Database;
use Friendica\Network\HTTPException\NotFoundException; use Friendica\Database\DBA;
use Friendica\Profile\ProfileField\Exception\ProfileFieldNotFoundException; use Friendica\Profile\ProfileField\Exception\ProfileFieldNotFoundException;
use Friendica\Profile\ProfileField\Exception\ProfileFieldPersistenceException; use Friendica\Profile\ProfileField\Exception\ProfileFieldPersistenceException;
use Friendica\Profile\ProfileField\Exception\UnexpectedPermissionSetException;
use Friendica\Profile\ProfileField\Factory; use Friendica\Profile\ProfileField\Factory;
use Friendica\Profile\ProfileField\Entity; use Friendica\Profile\ProfileField\Entity;
use Friendica\Profile\ProfileField\Collection; use Friendica\Profile\ProfileField\Collection;
@ -40,6 +41,8 @@ class ProfileField extends BaseDepository
protected static $table_name = 'profile_field'; protected static $table_name = 'profile_field';
protected static $view_name = 'profile_field-view';
/** @var PermissionSetDepository */ /** @var PermissionSetDepository */
protected $permissionSetDepository; protected $permissionSetDepository;
@ -53,16 +56,20 @@ class ProfileField extends BaseDepository
/** /**
* @param array $condition * @param array $condition
* @param array $params * @param array $params
*
* @return Entity\ProfileField * @return Entity\ProfileField
*
* @throws ProfileFieldNotFoundException * @throws ProfileFieldNotFoundException
* @throws UnexpectedPermissionSetException
*/ */
private function selectOne(array $condition, array $params = []): Entity\ProfileField private function selectOne(array $condition, array $params = []): Entity\ProfileField
{ {
try { $fields = $this->db->selectFirst(static::$view_name, [], $condition, $params);
return parent::_selectOne($condition, $params); if (!$this->db->isResult($fields)) {
} catch (NotFoundException $exception) { throw new ProfileFieldNotFoundException();
throw new ProfileFieldNotFoundException($exception->getMessage());
} }
return $this->factory->createFromTableRow($fields);
} }
/** /**
@ -72,14 +79,19 @@ class ProfileField extends BaseDepository
* @return Collection\ProfileFields * @return Collection\ProfileFields
* *
* @throws ProfileFieldPersistenceException In case of underlying persistence exceptions * @throws ProfileFieldPersistenceException In case of underlying persistence exceptions
* @throws UnexpectedPermissionSetException
*/ */
private function select(array $condition, array $params = []): Collection\ProfileFields private function select(array $condition, array $params = []): Collection\ProfileFields
{ {
try { $rows = $this->db->selectToArray(static::$view_name, [], $condition, $params);
return new Collection\ProfileFields(parent::_select($condition, $params)->getArrayCopy());
} catch (\Exception $exception) { $Entities = new Collection\ProfileFields();
throw new ProfileFieldPersistenceException('Cannot select ProfileFields', $exception); foreach ($rows as $fields) {
$this->logger->warning('row', ['row' => $fields]);
$Entities[] = $this->factory->createFromTableRow($fields);
} }
return $Entities;
} }
/** /**
@ -98,7 +110,7 @@ class ProfileField extends BaseDepository
'order' => $profileField->order, 'order' => $profileField->order,
'created' => $profileField->created->format(DateTimeFormat::MYSQL), 'created' => $profileField->created->format(DateTimeFormat::MYSQL),
'edited' => $profileField->edited->format(DateTimeFormat::MYSQL), 'edited' => $profileField->edited->format(DateTimeFormat::MYSQL),
'psid' => $profileField->permissionSetId 'psid' => $profileField->permissionSet->id
]; ];
} }
@ -170,7 +182,7 @@ class ProfileField extends BaseDepository
* *
* @ProfileFieldNotFoundException In case there is no ProfileField found * @ProfileFieldNotFoundException In case there is no ProfileField found
*/ */
public function selectOnyById(int $id): Entity\ProfileField public function selectOneById(int $id): Entity\ProfileField
{ {
try { try {
return $this->selectOne(['id' => $id]); return $this->selectOne(['id' => $id]);
@ -212,7 +224,7 @@ class ProfileField extends BaseDepository
} else { } else {
$this->db->insert(self::$table_name, $fields); $this->db->insert(self::$table_name, $fields);
$profileField = $this->selectOnyById($this->db->lastInsertId()); $profileField = $this->selectOneById($this->db->lastInsertId());
} }
} catch (\Exception $exception) { } catch (\Exception $exception) {
throw new ProfileFieldPersistenceException(sprintf('Cannot save ProfileField with id "%d" and label "%s"', $profileField->id, $profileField->label), $exception); throw new ProfileFieldPersistenceException(sprintf('Cannot save ProfileField with id "%d" and label "%s"', $profileField->id, $profileField->label), $exception);

View file

@ -23,10 +23,7 @@ namespace Friendica\Profile\ProfileField\Entity;
use Friendica\BaseEntity; use Friendica\BaseEntity;
use Friendica\Network\HTTPException\InternalServerErrorException; use Friendica\Network\HTTPException\InternalServerErrorException;
use Friendica\Network\HTTPException\NotFoundException;
use Friendica\Profile\ProfileField\Exception\ProfileFieldNotFoundException; use Friendica\Profile\ProfileField\Exception\ProfileFieldNotFoundException;
use Friendica\Profile\ProfileField\Exception\UnexpectedPermissionSetException;
use Friendica\Security\PermissionSet\Depository\PermissionSet as PermissionSetDepository;
use Friendica\Security\PermissionSet\Entity\PermissionSet; use Friendica\Security\PermissionSet\Entity\PermissionSet;
/** /**
@ -38,7 +35,6 @@ use Friendica\Security\PermissionSet\Entity\PermissionSet;
* @property-read int|null $id * @property-read int|null $id
* @property-read int $uid * @property-read int $uid
* @property-read int $order * @property-read int $order
* @property-read int $permissionSetId
* @property-read string $label * @property-read string $label
* @property-read string $value * @property-read string $value
* @property-read \DateTime $created * @property-read \DateTime $created
@ -51,14 +47,10 @@ class ProfileField extends BaseEntity
protected $id; protected $id;
/** @var PermissionSet */ /** @var PermissionSet */
protected $permissionSet; protected $permissionSet;
/** @var PermissionSetDepository */
protected $permissionSetDepository;
/** @var int */ /** @var int */
protected $uid; protected $uid;
/** @var int */ /** @var int */
protected $order; protected $order;
/** @var int */
protected $permissionSetId;
/** @var string */ /** @var string */
protected $label; protected $label;
/** @var string */ /** @var string */
@ -68,54 +60,28 @@ class ProfileField extends BaseEntity
/** @var \DateTime */ /** @var \DateTime */
protected $edited; protected $edited;
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) public function __construct(int $uid, int $order, string $label, string $value, \DateTime $created, \DateTime $edited, PermissionSet $permissionSet, int $id = null)
{ {
$this->permissionSetDepository = $permissionSetDepository; $this->permissionSet = $permissionSet;
$this->permissionSet = $permissionSet; $this->uid = $uid;
$this->order = $order;
$this->uid = $uid; $this->label = $label;
$this->order = $order; $this->value = $value;
$this->permissionSetId = $permissionSetId; $this->created = $created;
$this->label = $label; $this->edited = $edited;
$this->value = $value; $this->id = $id;
$this->created = $created;
$this->edited = $edited;
$this->id = $id;
} }
/** /**
* @throws ProfileFieldNotFoundException * @throws ProfileFieldNotFoundException
* @throws UnexpectedPermissionSetException
*/ */
public function __get($name) public function __get($name)
{ {
switch ($name) { try {
case 'permissionSet': return parent::__get($name);
if (empty($this->permissionSet)) { } catch (InternalServerErrorException $exception) {
try { throw new ProfileFieldNotFoundException($exception->getMessage());
$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));
}
$this->permissionSet = $permissionSet;
} catch (NotFoundException $exception) {
throw new UnexpectedPermissionSetException(sprintf('No PermissionSet found for ProfileField %d (user-id: %d).', $this->id, $this->uid));
}
}
$return = $this->permissionSet;
break;
default:
try {
$return = parent::__get($name);
} catch (InternalServerErrorException $exception) {
throw new ProfileFieldNotFoundException($exception->getMessage());
}
break;
} }
return $return;
} }
/** /**
@ -127,11 +93,10 @@ class ProfileField extends BaseEntity
*/ */
public function update(string $value, int $order, PermissionSet $permissionSet) public function update(string $value, int $order, PermissionSet $permissionSet)
{ {
$this->value = $value; $this->value = $value;
$this->order = $order; $this->order = $order;
$this->permissionSet = $permissionSet; $this->permissionSet = $permissionSet;
$this->permissionSetId = $permissionSet->id; $this->edited = new \DateTime('now', new \DateTimeZone('UTC'));
$this->edited = new \DateTime('now', new \DateTimeZone('UTC'));
} }
/** /**

View file

@ -23,7 +23,7 @@ namespace Friendica\Profile\ProfileField\Factory;
use Friendica\BaseFactory; use Friendica\BaseFactory;
use Friendica\Profile\ProfileField\Exception\UnexpectedPermissionSetException; use Friendica\Profile\ProfileField\Exception\UnexpectedPermissionSetException;
use Friendica\Security\PermissionSet\Depository\PermissionSet as PermissionSetDepository; use Friendica\Security\PermissionSet\Factory\PermissionSet as PermissionSetFactory;
use Friendica\Profile\ProfileField\Entity; use Friendica\Profile\ProfileField\Entity;
use Friendica\Capabilities\ICanCreateFromTableRow; use Friendica\Capabilities\ICanCreateFromTableRow;
use Friendica\Security\PermissionSet\Entity\PermissionSet; use Friendica\Security\PermissionSet\Entity\PermissionSet;
@ -31,45 +31,68 @@ use Psr\Log\LoggerInterface;
class ProfileField extends BaseFactory implements ICanCreateFromTableRow class ProfileField extends BaseFactory implements ICanCreateFromTableRow
{ {
/** @var PermissionSetDepository */ /** @var PermissionSetFactory */
private $permissionSetDepository; private $permissionSetFactory;
public function __construct(LoggerInterface $logger, PermissionSetDepository $permissionSetDepository) public function __construct(LoggerInterface $logger, PermissionSetFactory $permissionSetFactory)
{ {
parent::__construct($logger); parent::__construct($logger);
$this->permissionSetDepository = $permissionSetDepository; $this->permissionSetFactory = $permissionSetFactory;
} }
/** /**
* @inheritDoc * @inheritDoc
*
* @throws UnexpectedPermissionSetException
*/ */
public function createFromTableRow(array $row, PermissionSet $permissionSet = null): Entity\ProfileField public function createFromTableRow(array $row, PermissionSet $permissionSet = null): Entity\ProfileField
{ {
if (empty($permissionSet) && empty($row['psid'])) { if (empty($permissionSet) &&
throw new UnexpectedPermissionSetException('Either set the permission set ID or the permission set itself'); (empty($row['psid']) || !array_key_exists('allow_cid', $row) || !array_key_exists('allow_gid', $row) || !array_key_exists('deny_cid', $row) || !array_key_exists('deny_gid', $row))
) {
throw new UnexpectedPermissionSetException('Either set the PermissionSet fields (join) or the PermissionSet itself');
} }
return new Entity\ProfileField( return new Entity\ProfileField(
$this->permissionSetDepository,
$row['uid'], $row['uid'],
$row['order'], $row['order'],
$row['psid'] ?? $permissionSet->id,
$row['label'], $row['label'],
$row['value'], $row['value'],
new \DateTime($row['created'] ?? 'now', new \DateTimeZone('UTC')), new \DateTime($row['created'] ?? 'now', new \DateTimeZone('UTC')),
new \DateTime($row['edited'] ?? 'now', new \DateTimeZone('UTC')), new \DateTime($row['edited'] ?? 'now', new \DateTimeZone('UTC')),
$row['id'] ?? null, $permissionSet ?? $this->permissionSetFactory->createFromString(
$permissionSet $row['uid'],
$row['allow_cid'],
$row['allow_gid'],
$row['deny_cid'],
$row['deny_gid'],
$row['psid']
),
$row['id'] ?? null
); );
} }
public function createFromString( /**
* Creates a ProfileField instance based on it's values
*
* @param int $uid
* @param int $order
* @param string $label
* @param string $value
* @param PermissionSet $permissionSet
* @param int|null $id
*
* @return Entity\ProfileField
* @throws UnexpectedPermissionSetException
*/
public function createFromValues(
int $uid, int $uid,
int $order, int $order,
string $label, string $label,
string $value, string $value,
PermissionSet $permissionSet PermissionSet $permissionSet,
int $id = null
): Entity\ProfileField { ): Entity\ProfileField {
return $this->createFromTableRow([ return $this->createFromTableRow([
'uid' => $uid, 'uid' => $uid,
@ -77,6 +100,7 @@ class ProfileField extends BaseFactory implements ICanCreateFromTableRow
'psid' => $permissionSet->id, 'psid' => $permissionSet->id,
'label' => $label, 'label' => $label,
'value' => $value, 'value' => $value,
'id' => $id,
], $permissionSet); ], $permissionSet);
} }
} }

View file

@ -51,7 +51,8 @@ class PermissionSet extends BaseFactory implements ICanCreateFromTableRow
string $allow_cid = '', string $allow_cid = '',
string $allow_gid = '', string $allow_gid = '',
string $deny_cid = '', string $deny_cid = '',
string $deny_gid = ''): Entity\PermissionSet string $deny_gid = '',
int $id = null): Entity\PermissionSet
{ {
return $this->createFromTableRow([ return $this->createFromTableRow([
'uid' => $uid, 'uid' => $uid,
@ -59,6 +60,7 @@ class PermissionSet extends BaseFactory implements ICanCreateFromTableRow
'allow_gid' => $allow_gid, 'allow_gid' => $allow_gid,
'deny_cid' => $deny_cid, 'deny_cid' => $deny_cid,
'deny_gid' => $deny_gid, 'deny_gid' => $deny_gid,
'id' => $id,
]); ]);
} }
} }

View file

@ -1047,5 +1047,21 @@
INNER JOIN `workerqueue` ON `workerqueue`.`pid` = `process`.`pid` INNER JOIN `workerqueue` ON `workerqueue`.`pid` = `process`.`pid`
WHERE NOT `workerqueue`.`done`" WHERE NOT `workerqueue`.`done`"
], ],
"profile_field-view" => [
"fields" => [
"id" => ["profile_field", "id"],
"uid" => ["profile_field", "uid"],
"label" => ["profile_field", "label"],
"value" => ["profile_field", "value"],
"order" => ["profile_field", "order"],
"psid"=> ["profile_field", "psid"],
"allow_cid" => ["permissionset", "allow_cid"],
"allow_gid" => ["permissionset", "allow_gid"],
"deny_cid" => ["permissionset", "deny_cid"],
"deny_gid" => ["permissionset", "deny_gid"]
],
"query" => "FROM `profile_field`
INNER JOIN `permissionset` ON `permissionset`.`id` = `profile_field`.`psid`"
],
]; ];

View file

@ -2,7 +2,6 @@
namespace Friendica\Test\src\Profile\ProfileField\Entity; namespace Friendica\Test\src\Profile\ProfileField\Entity;
use Friendica\Network\HTTPException\NotFoundException;
use Friendica\Profile\ProfileField\Entity\ProfileField; use Friendica\Profile\ProfileField\Entity\ProfileField;
use Friendica\Profile\ProfileField\Exception\ProfileFieldNotFoundException; use Friendica\Profile\ProfileField\Exception\ProfileFieldNotFoundException;
use Friendica\Profile\ProfileField\Exception\UnexpectedPermissionSetException; use Friendica\Profile\ProfileField\Exception\UnexpectedPermissionSetException;
@ -21,7 +20,7 @@ class ProfileFieldTest extends MockedTest
protected $permissionSetDepository; protected $permissionSetDepository;
/** @var ProfileFieldFactory */ /** @var ProfileFieldFactory */
protected $profileFieldFactory; protected $profileFieldFactory;
/** @var PermissionSetFactory */ /** @var MockInterface|PermissionSetFactory */
protected $permissionSetFactory; protected $permissionSetFactory;
protected function setUp(): void protected function setUp(): void
@ -29,8 +28,8 @@ class ProfileFieldTest extends MockedTest
parent::setUp(); parent::setUp();
$this->permissionSetDepository = \Mockery::mock(PermissionSetDepository::class); $this->permissionSetDepository = \Mockery::mock(PermissionSetDepository::class);
$this->profileFieldFactory = new ProfileFieldFactory(new VoidLogger(), $this->permissionSetDepository);
$this->permissionSetFactory = new PermissionSetFactory(new VoidLogger(), new ACLFormatter()); $this->permissionSetFactory = new PermissionSetFactory(new VoidLogger(), new ACLFormatter());
$this->profileFieldFactory = new ProfileFieldFactory(new VoidLogger(), $this->permissionSetFactory);
} }
public function dataEntity() public function dataEntity()
@ -44,6 +43,14 @@ class ProfileFieldTest extends MockedTest
'value' => 'more', 'value' => 'more',
'created' => new \DateTime('2021-10-10T21:12:00.000000+0000', new \DateTimeZone('UTC')), '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')), 'edited' => new \DateTime('2021-10-10T21:12:00.000000+0000', new \DateTimeZone('UTC')),
'permissionSet' => [
'uid' => 23,
'allow_cid' => "<1>",
'allow_gid' => "<~>",
'deny_cid' => '<2>',
'deny_gid' => '<3>',
'id' => 2,
]
], ],
'withId' => [ 'withId' => [
'uid' => 23, 'uid' => 23,
@ -53,6 +60,14 @@ class ProfileFieldTest extends MockedTest
'value' => 'more', 'value' => 'more',
'created' => new \DateTime('2021-10-10T21:12:00.000000+0000', new \DateTimeZone('UTC')), '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')), 'edited' => new \DateTime('2021-10-10T21:12:00.000000+0000', new \DateTimeZone('UTC')),
'permissionSet' => [
'uid' => 23,
'allow_cid' => "<1>",
'allow_gid' => "<~>",
'deny_cid' => '<2>',
'deny_gid' => '<3>',
'id' => 2,
],
'id' => 54, 'id' => 54,
], ],
]; ];
@ -61,13 +76,13 @@ class ProfileFieldTest extends MockedTest
/** /**
* @dataProvider dataEntity * @dataProvider dataEntity
*/ */
public function testEntity(int $uid, int $order, int $psid, string $label, string $value, \DateTime $created, \DateTime $edited, $id = null) public function testEntity(int $uid, int $order, int $psid, string $label, string $value, \DateTime $created, \DateTime $edited, array $permissionSet, $id = null)
{ {
$entity = new ProfileField($this->permissionSetDepository, $uid, $order, $psid, $label, $value, $created, $edited, $id); $entity = new ProfileField($uid, $order, $label, $value, $created, $edited, $this->permissionSetFactory->createFromTableRow($permissionSet), $id);
self::assertEquals($uid, $entity->uid); self::assertEquals($uid, $entity->uid);
self::assertEquals($order, $entity->order); self::assertEquals($order, $entity->order);
self::assertEquals($psid, $entity->permissionSetId); self::assertEquals($psid, $entity->permissionSet->id);
self::assertEquals($label, $entity->label); self::assertEquals($label, $entity->label);
self::assertEquals($value, $entity->value); self::assertEquals($value, $entity->value);
self::assertEquals($created, $entity->created); self::assertEquals($created, $entity->created);
@ -78,7 +93,7 @@ class ProfileFieldTest extends MockedTest
/** /**
* @dataProvider dataEntity * @dataProvider dataEntity
*/ */
public function testUpdate(int $uid, int $order, int $psid, string $label, string $value, \DateTime $created, \DateTime $edited, $id = null) public function testUpdate(int $uid, int $order, int $psid, string $label, string $value, \DateTime $created, \DateTime $edited, array $permissionSet, $id = null)
{ {
$permissionSet = $this->permissionSetFactory->createFromTableRow(['uid' => 2, 'id' => $psid]); $permissionSet = $this->permissionSetFactory->createFromTableRow(['uid' => 2, 'id' => $psid]);
@ -103,7 +118,7 @@ class ProfileFieldTest extends MockedTest
self::assertEquals($uid, $entity->uid); self::assertEquals($uid, $entity->uid);
self::assertEquals(2345, $entity->order); self::assertEquals(2345, $entity->order);
self::assertEquals(23, $entity->permissionSetId); self::assertEquals(23, $entity->permissionSet->id);
self::assertEquals($label, $entity->label); self::assertEquals($label, $entity->label);
self::assertEquals('updatedValue', $entity->value); self::assertEquals('updatedValue', $entity->value);
self::assertEquals($created, $entity->created); self::assertEquals($created, $entity->created);
@ -114,7 +129,7 @@ class ProfileFieldTest extends MockedTest
/** /**
* @dataProvider dataEntity * @dataProvider dataEntity
*/ */
public function testSetOrder(int $uid, int $order, int $psid, string $label, string $value, \DateTime $created, \DateTime $edited, $id = null) public function testSetOrder(int $uid, int $order, int $psid, string $label, string $value, \DateTime $created, \DateTime $edited, array $permissionSet, $id = null)
{ {
$permissionSet = $this->permissionSetFactory->createFromTableRow(['uid' => 2, 'id' => $psid]); $permissionSet = $this->permissionSetFactory->createFromTableRow(['uid' => 2, 'id' => $psid]);
@ -133,7 +148,7 @@ class ProfileFieldTest extends MockedTest
self::assertEquals($uid, $entity->uid); self::assertEquals($uid, $entity->uid);
self::assertEquals(2345, $entity->order); self::assertEquals(2345, $entity->order);
self::assertEquals($psid, $entity->permissionSetId); self::assertEquals($psid, $entity->permissionSet->id);
self::assertEquals($label, $entity->label); self::assertEquals($label, $entity->label);
self::assertEquals($value, $entity->value); self::assertEquals($value, $entity->value);
self::assertEquals($created, $entity->created); self::assertEquals($created, $entity->created);
@ -146,9 +161,9 @@ class ProfileFieldTest extends MockedTest
* *
* @dataProvider dataEntity * @dataProvider dataEntity
*/ */
public function testWrongGet(int $uid, int $order, int $psid, string $label, string $value, \DateTime $created, \DateTime $edited, $id = null) public function testWrongGet(int $uid, int $order, int $psid, string $label, string $value, \DateTime $created, \DateTime $edited, array $permissionSet, $id = null)
{ {
$entity = new ProfileField($this->permissionSetDepository, $uid, $order, $psid, $label, $value, $created, $edited, $id); $entity = new ProfileField($uid, $order, $label, $value, $created, $edited, $this->permissionSetFactory->createFromTableRow($permissionSet), $id);
self::expectException(ProfileFieldNotFoundException::class); self::expectException(ProfileFieldNotFoundException::class);
$entity->wrong; $entity->wrong;
@ -159,9 +174,10 @@ class ProfileFieldTest extends MockedTest
* *
* @dataProvider dataEntity * @dataProvider dataEntity
*/ */
public function testPermissionSet(int $uid, int $order, int $psid, string $label, string $value, \DateTime $created, \DateTime $edited, $id = null) public function testPermissionSet(int $uid, int $order, int $psid, string $label, string $value, \DateTime $created, \DateTime $edited, array $permissionSet, $id = null)
{ {
$entity = new ProfileField($this->permissionSetDepository, $uid, $order, $psid, $label, $value, $created, $edited, $id); $entity = new ProfileField($uid, $order, $label, $value, $created, $edited, $this->permissionSetFactory->createFromTableRow($permissionSet), $id);
$permissionSet = $this->permissionSetFactory->createFromTableRow(['uid' => $uid, 'id' => $psid]); $permissionSet = $this->permissionSetFactory->createFromTableRow(['uid' => $uid, 'id' => $psid]);
$this->permissionSetDepository->shouldReceive('selectOneById')->with($psid, $uid)->andReturns($permissionSet); $this->permissionSetDepository->shouldReceive('selectOneById')->with($psid, $uid)->andReturns($permissionSet);
@ -169,49 +185,15 @@ class ProfileFieldTest extends MockedTest
self::assertEquals($psid, $entity->permissionSet->id); 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, $uid)->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, $uid)
->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 * Test the exception because the factory cannot find a permissionSet ID, nor the permissionSet itself
* *
* @dataProvider dataEntity * @dataProvider dataEntity
*/ */
public function testMissingPermissionFactory(int $uid, int $order, int $psid, string $label, string $value, \DateTime $created, \DateTime $edited, $id = null) public function testMissingPermissionFactory(int $uid, int $order, int $psid, string $label, string $value, \DateTime $created, \DateTime $edited, array $permissionSet, $id = null)
{ {
self::expectException(UnexpectedPermissionSetException::class); self::expectException(UnexpectedPermissionSetException::class);
self::expectExceptionMessage('Either set the permission set ID or the permission set itself'); self::expectExceptionMessage('Either set the PermissionSet fields (join) or the PermissionSet itself');
$entity = $this->profileFieldFactory->createFromTableRow([ $entity = $this->profileFieldFactory->createFromTableRow([
'uid' => $uid, 'uid' => $uid,

View file

@ -206,7 +206,7 @@ function update_1332()
$profiles = DBA::select('profile', [], $condition); $profiles = DBA::select('profile', [], $condition);
while ($profile = DBA::fetch($profiles)) { while ($profile = DBA::fetch($profiles)) {
Profile::migrateFromLegacyProfile($profile); Profile::migrate($profile);
} }
DBA::close($profiles); DBA::close($profiles);