From 0bade2747902b22002203c31e7e13f36256f42bc Mon Sep 17 00:00:00 2001 From: Arthur Schiwon Date: Wed, 26 May 2021 18:39:43 +0200 Subject: [PATCH] add IAccountPropertyCollection with implementation Signed-off-by: Arthur Schiwon --- lib/composer/composer/autoload_classmap.php | 2 + lib/composer/composer/autoload_static.php | 2 + .../Accounts/AccountPropertyCollection.php | 86 +++++++ .../Accounts/IAccountPropertyCollection.php | 77 +++++++ .../AccountPropertyCollectionTest.php | 209 ++++++++++++++++++ 5 files changed, 376 insertions(+) create mode 100644 lib/private/Accounts/AccountPropertyCollection.php create mode 100644 lib/public/Accounts/IAccountPropertyCollection.php create mode 100644 tests/lib/Accounts/AccountPropertyCollectionTest.php diff --git a/lib/composer/composer/autoload_classmap.php b/lib/composer/composer/autoload_classmap.php index f63d74b560..0912eb5396 100644 --- a/lib/composer/composer/autoload_classmap.php +++ b/lib/composer/composer/autoload_classmap.php @@ -10,6 +10,7 @@ return array( 'OCP\\Accounts\\IAccount' => $baseDir . '/lib/public/Accounts/IAccount.php', 'OCP\\Accounts\\IAccountManager' => $baseDir . '/lib/public/Accounts/IAccountManager.php', 'OCP\\Accounts\\IAccountProperty' => $baseDir . '/lib/public/Accounts/IAccountProperty.php', + 'OCP\\Accounts\\IAccountPropertyCollection' => $baseDir . '/lib/public/Accounts/IAccountPropertyCollection.php', 'OCP\\Accounts\\PropertyDoesNotExistException' => $baseDir . '/lib/public/Accounts/PropertyDoesNotExistException.php', 'OCP\\Activity\\ActivitySettings' => $baseDir . '/lib/public/Activity/ActivitySettings.php', 'OCP\\Activity\\IConsumer' => $baseDir . '/lib/public/Activity/IConsumer.php', @@ -581,6 +582,7 @@ return array( 'OC\\Accounts\\Account' => $baseDir . '/lib/private/Accounts/Account.php', 'OC\\Accounts\\AccountManager' => $baseDir . '/lib/private/Accounts/AccountManager.php', 'OC\\Accounts\\AccountProperty' => $baseDir . '/lib/private/Accounts/AccountProperty.php', + 'OC\\Accounts\\AccountPropertyCollection' => $baseDir . '/lib/private/Accounts/AccountPropertyCollection.php', 'OC\\Accounts\\Hooks' => $baseDir . '/lib/private/Accounts/Hooks.php', 'OC\\Activity\\ActivitySettingsAdapter' => $baseDir . '/lib/private/Activity/ActivitySettingsAdapter.php', 'OC\\Activity\\Event' => $baseDir . '/lib/private/Activity/Event.php', diff --git a/lib/composer/composer/autoload_static.php b/lib/composer/composer/autoload_static.php index 9c861fd327..04adffc410 100644 --- a/lib/composer/composer/autoload_static.php +++ b/lib/composer/composer/autoload_static.php @@ -39,6 +39,7 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c 'OCP\\Accounts\\IAccount' => __DIR__ . '/../../..' . '/lib/public/Accounts/IAccount.php', 'OCP\\Accounts\\IAccountManager' => __DIR__ . '/../../..' . '/lib/public/Accounts/IAccountManager.php', 'OCP\\Accounts\\IAccountProperty' => __DIR__ . '/../../..' . '/lib/public/Accounts/IAccountProperty.php', + 'OCP\\Accounts\\IAccountPropertyCollection' => __DIR__ . '/../../..' . '/lib/public/Accounts/IAccountPropertyCollection.php', 'OCP\\Accounts\\PropertyDoesNotExistException' => __DIR__ . '/../../..' . '/lib/public/Accounts/PropertyDoesNotExistException.php', 'OCP\\Activity\\ActivitySettings' => __DIR__ . '/../../..' . '/lib/public/Activity/ActivitySettings.php', 'OCP\\Activity\\IConsumer' => __DIR__ . '/../../..' . '/lib/public/Activity/IConsumer.php', @@ -610,6 +611,7 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c 'OC\\Accounts\\Account' => __DIR__ . '/../../..' . '/lib/private/Accounts/Account.php', 'OC\\Accounts\\AccountManager' => __DIR__ . '/../../..' . '/lib/private/Accounts/AccountManager.php', 'OC\\Accounts\\AccountProperty' => __DIR__ . '/../../..' . '/lib/private/Accounts/AccountProperty.php', + 'OC\\Accounts\\AccountPropertyCollection' => __DIR__ . '/../../..' . '/lib/private/Accounts/AccountPropertyCollection.php', 'OC\\Accounts\\Hooks' => __DIR__ . '/../../..' . '/lib/private/Accounts/Hooks.php', 'OC\\Activity\\ActivitySettingsAdapter' => __DIR__ . '/../../..' . '/lib/private/Activity/ActivitySettingsAdapter.php', 'OC\\Activity\\Event' => __DIR__ . '/../../..' . '/lib/private/Activity/Event.php', diff --git a/lib/private/Accounts/AccountPropertyCollection.php b/lib/private/Accounts/AccountPropertyCollection.php new file mode 100644 index 0000000000..8e74d45d52 --- /dev/null +++ b/lib/private/Accounts/AccountPropertyCollection.php @@ -0,0 +1,86 @@ + + * + * @author Arthur Schiwon + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + +namespace OC\Accounts; + +use InvalidArgumentException; +use OCP\Accounts\IAccountProperty; +use OCP\Accounts\IAccountPropertyCollection; + +class AccountPropertyCollection implements IAccountPropertyCollection { + + /** @var string */ + protected $collectionName = ''; + + /** @var IAccountProperty[] */ + protected $properties = []; + + public function __construct(string $collectionName) { + $this->collectionName = $collectionName; + } + + public function setProperties(array $properties): IAccountPropertyCollection { + /** @var IAccountProperty $property */ + $this->properties = []; + foreach ($properties as $property) { + $this->addProperty($property); + } + return $this; + } + + public function getProperties(): array { + return $this->properties; + } + + public function addProperty(IAccountProperty $property): IAccountPropertyCollection { + if ($property->getName() !== $this->collectionName) { + throw new InvalidArgumentException('Provided property does not match collection name'); + } + $this->properties[] = $property; + return $this; + } + + public function removeProperty(IAccountProperty $property): IAccountPropertyCollection { + $ref = array_search($property, $this->properties, true); + if ($ref !== false) { + unset($this->properties[$ref]); + } + return $this; + } + + public function removePropertyByValue(string $value): IAccountPropertyCollection { + foreach ($this->properties as $i => $property) { + if ($property->getValue() === $value) { + unset($this->properties[$i]); + } + } + return $this; + } + + public function jsonSerialize() { + return [$this->collectionName => $this->properties]; + } +} diff --git a/lib/public/Accounts/IAccountPropertyCollection.php b/lib/public/Accounts/IAccountPropertyCollection.php new file mode 100644 index 0000000000..b67fa5646f --- /dev/null +++ b/lib/public/Accounts/IAccountPropertyCollection.php @@ -0,0 +1,77 @@ + + * + * @author Arthur Schiwon + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + +namespace OCP\Accounts; + +use InvalidArgumentException; +use JsonSerializable; + +/** + * Interface IAccountPropertyCollection + * + * @package OCP\Accounts + * + * @since 22.0.0 + */ +interface IAccountPropertyCollection extends JsonSerializable { + + /** + * set properties of this collection + * + * @param IAccountProperty[] $properties + * @throws InvalidArgumentException + * @since 22.0.0 + */ + public function setProperties(array $properties): IAccountPropertyCollection; + + /** + * @return IAccountProperty[] + * @since 22.0.0 + */ + public function getProperties(): array; + + /** + * adds a property to this collection + * + * @throws InvalidArgumentException + * @since 22.0.0 + */ + public function addProperty(IAccountProperty $property): IAccountPropertyCollection; + + /** + * removes a property of this collection + * + * @since 22.0.0 + */ + public function removeProperty(IAccountProperty $property): IAccountPropertyCollection; + + /** + * removes a property identified by its value + * + * @since 22.0.0 + */ + public function removePropertyByValue(string $value): IAccountPropertyCollection; +} diff --git a/tests/lib/Accounts/AccountPropertyCollectionTest.php b/tests/lib/Accounts/AccountPropertyCollectionTest.php new file mode 100644 index 0000000000..d8a6bafd24 --- /dev/null +++ b/tests/lib/Accounts/AccountPropertyCollectionTest.php @@ -0,0 +1,209 @@ + + * + * @author Arthur Schiwon + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + +namespace lib\Accounts; + +use InvalidArgumentException; +use OC\Accounts\AccountPropertyCollection; +use OCP\Accounts\IAccountProperty; +use OCP\Accounts\IAccountPropertyCollection; +use PHPUnit\Framework\MockObject\MockObject; +use Test\TestCase; + +class AccountPropertyCollectionTest extends TestCase { + /** @var IAccountPropertyCollection */ + protected $collection; + + protected const COLLECTION_NAME = 'my_multivalue_property'; + + public function setUp(): void { + parent::setUp(); + + $this->collection = new AccountPropertyCollection(self::COLLECTION_NAME); + } + + /** + * @return IAccountProperty|MockObject + */ + protected function makePropertyMock(string $propertyName): MockObject { + $mock = $this->createMock(IAccountProperty::class); + $mock->expects($this->any()) + ->method('getName') + ->willReturn($propertyName); + + return $mock; + } + + public function testSetAndGetProperties() { + $propsBefore = $this->collection->getProperties(); + $this->assertIsArray($propsBefore); + $this->assertEmpty($propsBefore); + + $props = [ + $this->makePropertyMock(self::COLLECTION_NAME), + $this->makePropertyMock(self::COLLECTION_NAME), + $this->makePropertyMock(self::COLLECTION_NAME), + ]; + + $this->collection->setProperties($props); + $propsAfter = $this->collection->getProperties(); + $this->assertIsArray($propsAfter); + $this->assertCount(count($props), $propsAfter); + } + + public function testSetPropertiesMixedInvalid() { + $props = [ + $this->makePropertyMock(self::COLLECTION_NAME), + $this->makePropertyMock('sneaky_property'), + $this->makePropertyMock(self::COLLECTION_NAME), + ]; + + $this->expectException(InvalidArgumentException::class); + $this->collection->setProperties($props); + } + + public function testAddProperty() { + $props = [ + $this->makePropertyMock(self::COLLECTION_NAME), + $this->makePropertyMock(self::COLLECTION_NAME), + $this->makePropertyMock(self::COLLECTION_NAME), + ]; + $this->collection->setProperties($props); + + $additionalProperty = $this->makePropertyMock(self::COLLECTION_NAME); + $this->collection->addProperty($additionalProperty); + + $propsAfter = $this->collection->getProperties(); + $this->assertCount(count($props) + 1, $propsAfter); + $this->assertNotFalse(array_search($additionalProperty, $propsAfter, true)); + } + + public function testAddPropertyInvalid() { + $props = [ + $this->makePropertyMock(self::COLLECTION_NAME), + $this->makePropertyMock(self::COLLECTION_NAME), + $this->makePropertyMock(self::COLLECTION_NAME), + ]; + $this->collection->setProperties($props); + + $additionalProperty = $this->makePropertyMock('sneaky_property'); + $exceptionThrown = false; + try { + $this->collection->addProperty($additionalProperty); + } catch (\InvalidArgumentException $e) { + $exceptionThrown = true; + } finally { + $propsAfter = $this->collection->getProperties(); + $this->assertCount(count($props), $propsAfter); + $this->assertFalse(array_search($additionalProperty, $propsAfter, true)); + $this->assertTrue($exceptionThrown); + } + } + + public function testRemoveProperty() { + $additionalProperty = $this->makePropertyMock(self::COLLECTION_NAME); + $props = [ + $this->makePropertyMock(self::COLLECTION_NAME), + $this->makePropertyMock(self::COLLECTION_NAME), + $additionalProperty, + $this->makePropertyMock(self::COLLECTION_NAME), + ]; + $this->collection->setProperties($props); + + $propsBefore = $this->collection->getProperties(); + $this->collection->removeProperty($additionalProperty); + $propsAfter = $this->collection->getProperties(); + + $this->assertTrue(count($propsBefore) > count($propsAfter)); + $this->assertCount(count($propsBefore) - 1, $propsAfter); + $this->assertFalse(array_search($additionalProperty, $propsAfter, true)); + } + + public function testRemovePropertyNotFound() { + $additionalProperty = $this->makePropertyMock(self::COLLECTION_NAME); + $props = [ + $this->makePropertyMock(self::COLLECTION_NAME), + $this->makePropertyMock(self::COLLECTION_NAME), + $this->makePropertyMock(self::COLLECTION_NAME), + ]; + $this->collection->setProperties($props); + + $propsBefore = $this->collection->getProperties(); + $this->collection->removeProperty($additionalProperty); + $propsAfter = $this->collection->getProperties(); + + // no errors, gently + $this->assertCount(count($propsBefore), $propsAfter); + } + + public function testRemovePropertyByValue() { + $additionalProperty = $this->makePropertyMock(self::COLLECTION_NAME); + $additionalProperty->expects($this->any()) + ->method('getValue') + ->willReturn('Lorem ipsum'); + + $additionalPropertyTwo = clone $additionalProperty; + + $props = [ + $this->makePropertyMock(self::COLLECTION_NAME), + $this->makePropertyMock(self::COLLECTION_NAME), + $additionalProperty, + $this->makePropertyMock(self::COLLECTION_NAME), + $additionalPropertyTwo + ]; + $this->collection->setProperties($props); + + $propsBefore = $this->collection->getProperties(); + $this->collection->removePropertyByValue('Lorem ipsum'); + $propsAfter = $this->collection->getProperties(); + + $this->assertTrue(count($propsBefore) > count($propsAfter)); + $this->assertCount(count($propsBefore) - 2, $propsAfter); + $this->assertFalse(array_search($additionalProperty, $propsAfter, true)); + $this->assertFalse(array_search($additionalPropertyTwo, $propsAfter, true)); + } + + public function testRemovePropertyByValueNotFound() { + $additionalProperty = $this->makePropertyMock(self::COLLECTION_NAME); + $additionalProperty->expects($this->any()) + ->method('getValue') + ->willReturn('Lorem ipsum'); + + $props = [ + $this->makePropertyMock(self::COLLECTION_NAME), + $this->makePropertyMock(self::COLLECTION_NAME), + $this->makePropertyMock(self::COLLECTION_NAME), + ]; + $this->collection->setProperties($props); + + $propsBefore = $this->collection->getProperties(); + $this->collection->removePropertyByValue('Lorem ipsum'); + $propsAfter = $this->collection->getProperties(); + + // no errors, gently + $this->assertCount(count($propsBefore), $propsAfter); + } +}