Add tests for activity classes

Signed-off-by: Julius Härtl <jus@bitgrid.net>
This commit is contained in:
Julius Härtl
2018-09-06 13:19:54 +02:00
parent 4ba47045b2
commit 0cc4fffd08
8 changed files with 385 additions and 51 deletions

View File

@@ -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]);

View File

@@ -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() {

View File

@@ -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'));
}

View File

@@ -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');
}
/**

View File

@@ -0,0 +1,58 @@
<?php
/**
* @copyright Copyright (c) 2018 Julius Härtl <jus@bitgrid.net>
*
* @author Julius Härtl <jus@bitgrid.net>
*
* @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 <http://www.gnu.org/licenses/>.
*
*/
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());
}
}

View File

@@ -0,0 +1,119 @@
<?php
/**
* @copyright Copyright (c) 2018 Julius Härtl <jus@bitgrid.net>
*
* @author Julius Härtl <jus@bitgrid.net>
*
* @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 <http://www.gnu.org/licenses/>.
*
*/
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')
);
}
}

View File

@@ -0,0 +1,81 @@
<?php
/**
* @copyright Copyright (c) 2018 Julius Härtl <jus@bitgrid.net>
*
* @author Julius Härtl <jus@bitgrid.net>
*
* @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 <http://www.gnu.org/licenses/>.
*
*/
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());
}
}

View File

@@ -0,0 +1,65 @@
<?php
/**
* @copyright Copyright (c) 2018 Julius Härtl <jus@bitgrid.net>
*
* @author Julius Härtl <jus@bitgrid.net>
*
* @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 <http://www.gnu.org/licenses/>.
*
*/
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());
}
}