From 0cc4fffd08e7d78c6812dc65b4fb13c871ee68f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julius=20H=C3=A4rtl?= Date: Thu, 6 Sep 2018 13:19:54 +0200 Subject: [PATCH] Add tests for activity classes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Julius Härtl --- lib/Activity/ActivityManager.php | 78 +++++++-------- lib/Activity/ChangeSet.php | 12 ++- lib/Activity/DeckProvider.php | 14 +-- lib/Activity/Filter.php | 9 +- tests/unit/Activity/ChangeSetTest.php | 58 +++++++++++ tests/unit/Activity/DeckProviderTest.php | 119 +++++++++++++++++++++++ tests/unit/Activity/FilterTest.php | 81 +++++++++++++++ tests/unit/Activity/SettingTest.php | 65 +++++++++++++ 8 files changed, 385 insertions(+), 51 deletions(-) create mode 100644 tests/unit/Activity/ChangeSetTest.php create mode 100644 tests/unit/Activity/DeckProviderTest.php create mode 100644 tests/unit/Activity/FilterTest.php create mode 100644 tests/unit/Activity/SettingTest.php diff --git a/lib/Activity/ActivityManager.php b/lib/Activity/ActivityManager.php index c4bac4f58..4637f5d1f 100644 --- a/lib/Activity/ActivityManager.php +++ b/lib/Activity/ActivityManager.php @@ -117,40 +117,6 @@ class ActivityManager { $this->userId = $userId; } - /** - * @param $objectType - * @param $entity - * @return null|\OCA\Deck\Db\RelationalEntity|\OCP\AppFramework\Db\Entity - * @throws \OCP\AppFramework\Db\DoesNotExistException - * @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException - */ - protected function findObjectForEntity($objectType, $entity) { - $className = \get_class($entity); - $objectId = null; - switch ($className) { - case Board::class: - case Card::class: - $objectId = $entity->getId(); - break; - case Attachment::class: - case Label::class: - case AssignedUsers::class: - $objectId = $entity->getCardId(); - break; - case Stack::class: - $objectId = $entity->getBoardId(); - } - - if ($objectType === self::DECK_OBJECT_CARD) { - return $this->cardMapper->find($objectId); - } - if ($objectType === self::DECK_OBJECT_BOARD) { - return $this->boardMapper->find($objectId); - } - - return null; - } - /** * @param $subjectIdentifier * @param array $subjectParams @@ -321,7 +287,7 @@ class ActivityManager { * @return IEvent * @throws \Exception */ - public function createEvent($objectType, $entity, $subject, $additionalParams = []) { + private function createEvent($objectType, $entity, $subject, $additionalParams = []) { try { $object = $this->findObjectForEntity($objectType, $entity); } catch (DoesNotExistException $e) { @@ -405,7 +371,7 @@ class ActivityManager { * * @param IEvent $event */ - public function sendToUsers(IEvent $event) { + private function sendToUsers(IEvent $event) { switch ($event->getObjectType()) { case self::DECK_OBJECT_BOARD: $mapper = $this->boardMapper; @@ -423,7 +389,41 @@ class ActivityManager { } } - public function findDetailsForStack($stackId) { + /** + * @param $objectType + * @param $entity + * @return null|\OCA\Deck\Db\RelationalEntity|\OCP\AppFramework\Db\Entity + * @throws \OCP\AppFramework\Db\DoesNotExistException + * @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException + */ + private function findObjectForEntity($objectType, $entity) { + $className = \get_class($entity); + $objectId = null; + switch ($className) { + case Board::class: + case Card::class: + $objectId = $entity->getId(); + break; + case Attachment::class: + case Label::class: + case AssignedUsers::class: + $objectId = $entity->getCardId(); + break; + case Stack::class: + $objectId = $entity->getBoardId(); + } + + if ($objectType === self::DECK_OBJECT_CARD) { + return $this->cardMapper->find($objectId); + } + if ($objectType === self::DECK_OBJECT_BOARD) { + return $this->boardMapper->find($objectId); + } + + return null; + } + + private function findDetailsForStack($stackId) { $stack = $this->stackMapper->find($stackId); $board = $this->boardMapper->find($stack->getBoardId()); return [ @@ -432,7 +432,7 @@ class ActivityManager { ]; } - public function findDetailsForCard($cardId) { + private function findDetailsForCard($cardId) { $card = $this->cardMapper->find($cardId); $stack = $this->stackMapper->find($card->getStackId()); $board = $this->boardMapper->find($stack->getBoardId()); @@ -443,7 +443,7 @@ class ActivityManager { ]; } - public function findDetailsForAttachment($attachmentId) { + private function findDetailsForAttachment($attachmentId) { $attachment = $this->attachmentMapper->find($attachmentId); $data = $this->findDetailsForCard($attachment->getCardId()); return array_merge($data, ['attachment' => $attachment]); diff --git a/lib/Activity/ChangeSet.php b/lib/Activity/ChangeSet.php index 237b48670..dba63200c 100644 --- a/lib/Activity/ChangeSet.php +++ b/lib/Activity/ChangeSet.php @@ -48,11 +48,19 @@ class ChangeSet { } public function setBefore($before) { - $this->before = clone $before; + if (is_object($before)) { + $this->before = clone $before; + } else { + $this->before = $before; + } } public function setAfter($after) { - $this->after = clone $after; + if (is_object($after)) { + $this->after = clone $after; + } else { + $this->after = $after; + } } public function getBefore() { diff --git a/lib/Activity/DeckProvider.php b/lib/Activity/DeckProvider.php index 2429e0007..8589a62ff 100644 --- a/lib/Activity/DeckProvider.php +++ b/lib/Activity/DeckProvider.php @@ -60,24 +60,24 @@ class DeckProvider implements IProvider { throw new \InvalidArgumentException(); } - $event->setIcon(\OC::$server->getURLGenerator()->imagePath('deck', 'deck-dark.svg')); + $event->setIcon($this->urlGenerator->imagePath('deck', 'deck-dark.svg')); if (strpos($event->getSubject(), '_update') !== false) { - $event->setIcon(\OC::$server->getURLGenerator()->imagePath('files', 'change.svg')); + $event->setIcon($this->urlGenerator->imagePath('files', 'change.svg')); } if (strpos($event->getSubject(), '_create') !== false) { - $event->setIcon(\OC::$server->getURLGenerator()->imagePath('files', 'add-color.svg')); + $event->setIcon($this->urlGenerator->imagePath('files', 'add-color.svg')); } if (strpos($event->getSubject(), '_delete') !== false) { - $event->setIcon(\OC::$server->getURLGenerator()->imagePath('files', 'delete-color.svg')); + $event->setIcon($this->urlGenerator->imagePath('files', 'delete-color.svg')); } if (strpos($event->getSubject(), 'archive') !== false) { - $event->setIcon(\OC::$server->getURLGenerator()->imagePath('deck', 'archive.svg')); + $event->setIcon($this->urlGenerator->imagePath('deck', 'archive.svg')); } if (strpos($event->getSubject(), '_restore') !== false) { - $event->setIcon(\OC::$server->getURLGenerator()->imagePath('core', 'actions/history.svg')); + $event->setIcon($this->urlGenerator->imagePath('core', 'actions/history.svg')); } if (strpos($event->getSubject(), 'attachment_') !== false) { - $event->setIcon(\OC::$server->getURLGenerator()->imagePath('core', 'places/files.svg')); + $event->setIcon($this->urlGenerator->imagePath('core', 'places/files.svg')); } diff --git a/lib/Activity/Filter.php b/lib/Activity/Filter.php index 038603236..d4209b7cc 100644 --- a/lib/Activity/Filter.php +++ b/lib/Activity/Filter.php @@ -24,15 +24,19 @@ namespace OCA\Deck\Activity; use OCP\IL10N; +use OCP\IURLGenerator; class Filter implements \OCP\Activity\IFilter { private $l10n; + private $urlGenerator; public function __construct( - IL10N $l10n + IL10N $l10n, + IURLGenerator $urlGenerator ) { $this->l10n = $l10n; + $this->urlGenerator = $urlGenerator; } /** @@ -66,8 +70,7 @@ class Filter implements \OCP\Activity\IFilter { * @since 11.0.0 */ public function getIcon() { - //TODO: inject - return \OC::$server->getURLGenerator()->imagePath('deck', 'deck-dark.svg'); + return $this->urlGenerator->imagePath('deck', 'deck-dark.svg'); } /** diff --git a/tests/unit/Activity/ChangeSetTest.php b/tests/unit/Activity/ChangeSetTest.php new file mode 100644 index 000000000..0ae843d34 --- /dev/null +++ b/tests/unit/Activity/ChangeSetTest.php @@ -0,0 +1,58 @@ + + * + * @author Julius Härtl + * + * @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 OCA\Deck\Activity; + +use PHPUnit\Framework\TestCase; + + +class ChangeSetTest extends TestCase { + + public function setUp() { + } + + public function testChangeSetScalar() { + $changeSet = new ChangeSet('A', 'B'); + $this->assertEquals('A', $changeSet->getBefore()); + $this->assertEquals('B', $changeSet->getAfter()); + $this->assertFalse($changeSet->getDiff()); + $changeSet->enableDiff(); + $this->assertTrue($changeSet->getDiff()); + } + + public function testChangeSetObject() { + $a = new \stdClass; + $a->data = 'A'; + $b = new \stdClass; + $b->data = 'B'; + $changeSet = new ChangeSet($a, $b); + $this->assertEquals('A', $changeSet->getBefore()->data); + $this->assertEquals('B', $changeSet->getAfter()->data); + $this->assertNotSame($a, $changeSet->getBefore()); + $this->assertNotSame($b, $changeSet->getAfter()); + $this->assertFalse($changeSet->getDiff()); + $changeSet->enableDiff(); + $this->assertTrue($changeSet->getDiff()); + } + +} diff --git a/tests/unit/Activity/DeckProviderTest.php b/tests/unit/Activity/DeckProviderTest.php new file mode 100644 index 000000000..6eb6a8e69 --- /dev/null +++ b/tests/unit/Activity/DeckProviderTest.php @@ -0,0 +1,119 @@ + + * + * @author Julius Härtl + * + * @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 OCA\Deck\Activity; + +use OCP\Activity\IEvent; +use OCP\IL10N; +use OCP\IURLGenerator; +use PHPUnit\Framework\TestCase; +use PHPUnit_Framework_MockObject_MockObject as MockObject; + +class DeckProviderTest extends TestCase { + + /** @var DeckProvider */ + private $provider; + + /** @var IURLGenerator|MockObject */ + private $urlGenerator; + + /** @var ActivityManager|MockObject */ + private $activityManager; + + /** @var string */ + private $userId = 'admin'; + + public function setUp() { + $this->l10n = $this->createMock(IL10N::class); + $this->urlGenerator = $this->createMock(IURLGenerator::class); + $this->activityManager = $this->createMock(ActivityManager::class); + $this->provider = new DeckProvider($this->urlGenerator, $this->activityManager, $this->userId); + } + + private function mockEvent($objectType, $objectId, $objectName, $subject, $subjectParameters = []) { + $data = []; + $event = $this->createMock(IEvent::class); + $event->expects($this->any())->method('getApp')->willReturn('deck'); + $event->expects($this->any())->method('getSubject')->willReturn($subject); + $event->expects($this->any())->method('getSubjectParameters')->willReturn($subjectParameters); + $event->expects($this->any())->method('getObjectType')->willReturn($objectType); + $event->expects($this->any())->method('getObjectId')->willReturn($objectId); + $event->expects($this->any())->method('getObjectName')->willReturn($objectName); + $event->expects($this->any())->method('getAuthor')->willReturn('admin'); + $event->expects($this->any())->method('getMessage')->willReturn(''); + $event->expects($this->any())->method('setIcon')->will($this->returnCallback(function($icon) use (&$data) { + $data['icon'] = $icon; + })); + $event->expects($this->any())->method('getIcon')->will( + $this->returnCallback(function() use (&$data) { + return array_key_exists('icon', $data) ? $data['icon'] : 'noicon'; + }) + ); + return $event; + } + + /** + * @expectedException \InvalidArgumentException + */ + public function testParseFailureApp() { + $event = $this->createMock(IEvent::class); + $event->expects($this->once())->method('getApp')->willReturn('notdeck'); + $this->provider->parse('en_US', $event, $event); + } + + public function dataEventIcons() { + return [ + [ActivityManager::SUBJECT_CARD_MOVE_STACK, 'deck', 'deck-dark.svg'], + [ActivityManager::SUBJECT_CARD_UPDATE, 'files', 'change.svg'], + ]; + } + /** + * @dataProvider dataEventIcons + * @param $subject + * @param $icon + */ + public function testEventIcons($subject, $app, $icon) { + $event = $this->mockEvent( + ActivityManager::DECK_OBJECT_BOARD, 1, 'Board', + $subject); + $this->urlGenerator->expects($this->any()) + ->method('imagePath') + ->will($this->returnCallback(function($a, $i) { + return $a . '/' . $i; + })); + $this->provider->parse('en_US', $event); + $this->assertEquals($app . '/' . $icon, $event->getIcon()); + } + + public function testDeckUrl() { + $this->urlGenerator->expects($this->once()) + ->method('linkToRoute') + ->with('deck.page.index') + ->willReturn('http://localhost/index.php/apps/deck/'); + $this->assertEquals( + 'http://localhost/index.php/apps/deck/#!board/1/card/1', + $this->provider->deckUrl('board/1/card/1') + ); + } + +} diff --git a/tests/unit/Activity/FilterTest.php b/tests/unit/Activity/FilterTest.php new file mode 100644 index 000000000..d3e6b82c5 --- /dev/null +++ b/tests/unit/Activity/FilterTest.php @@ -0,0 +1,81 @@ + + * + * @author Julius Härtl + * + * @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 OCA\Deck\Activity; + +use OCP\IL10N; +use OCP\IURLGenerator; +use PHPUnit\Framework\TestCase; +use PHPUnit_Framework_MockObject_MockObject as MockObject; + +class FilterTest extends TestCase { + + /** @var Filter */ + private $filter; + + /** @var IL10N|MockObject */ + private $l10n; + + /** @var IURLGenerator|MockObject */ + private $urlGenerator; + + public function setUp() { + $this->l10n = $this->createMock(IL10N::class); + $this->urlGenerator = $this->createMock(IURLGenerator::class); + $this->filter = new Filter($this->l10n, $this->urlGenerator); + } + + public function testGetIdentifier() { + $this->assertEquals('deck', $this->filter->getIdentifier()); + } + + public function testGetName() { + $this->l10n->expects($this->once()) + ->method('t') + ->with('Deck') + ->willReturn('Deck'); + $this->assertEquals('Deck', $this->filter->getName()); + } + + public function testGetPriority() { + $this->assertEquals(90, $this->filter->getPriority()); + } + + public function testGetIcon() { + $this->urlGenerator->expects($this->once()) + ->method('imagePath') + ->with('deck', 'deck-dark.svg') + ->willReturn('http://localhost/apps/deck/img/deck-dark.svg'); + $this->assertEquals('http://localhost/apps/deck/img/deck-dark.svg', $this->filter->getIcon()); + } + + public function testFilterTypes() { + $data = ['deck_board', 'deck_card']; + $this->assertEquals($data, $this->filter->filterTypes($data)); + } + + public function testAllowedApps() { + $this->assertEquals(['deck'], $this->filter->allowedApps()); + } + +} diff --git a/tests/unit/Activity/SettingTest.php b/tests/unit/Activity/SettingTest.php new file mode 100644 index 000000000..956d5c1d5 --- /dev/null +++ b/tests/unit/Activity/SettingTest.php @@ -0,0 +1,65 @@ + + * + * @author Julius Härtl + * + * @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 OCA\Deck\Activity; + +use PHPUnit\Framework\TestCase; + +class SettingTest extends TestCase { + + /** @var Setting */ + private $setting; + + public function setUp() { + $this->setting = new Setting(); + } + + public function testGetIdentifier() { + $this->assertEquals('deck', $this->setting->getIdentifier()); + } + + public function testGetName() { + $this->assertEquals('Deck', $this->setting->getName()); + } + + public function testGetPriority() { + $this->assertEquals(90, $this->setting->getPriority()); + } + + public function testCanChangeStream() { + $this->assertTrue($this->setting->canChangeStream()); + } + + public function testIsDefaultEnabledStream() { + $this->assertTrue($this->setting->isDefaultEnabledStream()); + } + + public function testCanChangeMail() { + $this->assertTrue($this->setting->canChangeMail()); + } + + public function testIsDefaultEnabledMail() { + $this->assertFalse($this->setting->isDefaultEnabledMail()); + } + +}