From 4441075f49476000d1029dc224c82393221783f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julius=20H=C3=A4rtl?= Date: Fri, 7 Sep 2018 09:47:56 +0200 Subject: [PATCH] Add tests for param parsing MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Julius Härtl --- lib/Activity/DeckProvider.php | 55 ++++--- tests/unit/Activity/DeckProviderTest.php | 185 +++++++++++++++++++++++ 2 files changed, 216 insertions(+), 24 deletions(-) diff --git a/lib/Activity/DeckProvider.php b/lib/Activity/DeckProvider.php index 2a9fed122..e8ff3e070 100644 --- a/lib/Activity/DeckProvider.php +++ b/lib/Activity/DeckProvider.php @@ -113,26 +113,10 @@ class DeckProvider implements IProvider { $params = $this->parseParamForStack('stack', $subjectParams, $params); $params = $this->parseParamForStack('stackBefore', $subjectParams, $params); $params = $this->parseParamForAttachment('attachment', $subjectParams, $params); - - if (array_key_exists('label', $subjectParams)) { - $params['label'] = [ - 'type' => 'highlight', - 'id' => $subjectParams['label']['id'], - 'name' => $subjectParams['label']['title'] - ]; - } - - if (array_key_exists('assigneduser', $subjectParams)) { - $user = $this->userManager->get($subjectParams['assigneduser']); - $params['assigneduser'] = [ - 'type' => 'user', - 'id' => $subjectParams['assigneduser'], - 'name' => $user !== null ? $user->getDisplayName() : $subjectParams['assigneduser'] - ]; - } - - $params = $this->parseParamsForAcl($subjectParams, $params); - $params = $this->parseParamsForChanges($subjectParams, $params, $event); + $params = $this->parseParamForLabel($subjectParams, $params); + $params = $this->parseParamForAssignedUser($subjectParams, $params); + $params = $this->parseParamForAcl($subjectParams, $params); + $params = $this->parseParamForChanges($subjectParams, $params, $event); try { $subject = $this->activityManager->getActivityFormat($subjectIdentifier, $subjectParams, $ownActivity); @@ -189,7 +173,7 @@ class DeckProvider implements IProvider { } private function parseParamForAttachment($paramName, $subjectParams, $params) { - if (array_key_exists('attachment', $subjectParams)) { + if (array_key_exists($paramName, $subjectParams)) { $params[$paramName] = [ 'type' => 'highlight', 'id' => $subjectParams[$paramName]['id'], @@ -200,7 +184,30 @@ class DeckProvider implements IProvider { return $params; } - private function parseParamsForAcl($subjectParams, $params) { + private function parseParamForAssignedUser($subjectParams, $params) { + if (array_key_exists('assigneduser', $subjectParams)) { + $user = $this->userManager->get($subjectParams['assigneduser']); + $params['assigneduser'] = [ + 'type' => 'user', + 'id' => $subjectParams['assigneduser'], + 'name' => $user !== null ? $user->getDisplayName() : $subjectParams['assigneduser'] + ]; + } + return $params; + } + + private function parseParamForLabel($subjectParams, $params) { + if (array_key_exists('label', $subjectParams)) { + $params['label'] = [ + 'type' => 'highlight', + 'id' => $subjectParams['label']['id'], + 'name' => $subjectParams['label']['title'] + ]; + } + return $params; + } + + private function parseParamForAcl($subjectParams, $params) { if (array_key_exists('acl', $subjectParams)) { if ($subjectParams['acl']['type'] === Acl::PERMISSION_TYPE_USER) { $user = $this->userManager->get($subjectParams['acl']['participant']); @@ -228,7 +235,7 @@ class DeckProvider implements IProvider { * @param $params * @return mixed */ - private function parseParamsForChanges($subjectParams, $params, $event) { + private function parseParamForChanges($subjectParams, $params, $event) { if (array_key_exists('diff', $subjectParams) && $subjectParams['diff']) { $diff = new Diff(); $event->setMessage($subjectParams['after']); @@ -242,7 +249,7 @@ class DeckProvider implements IProvider { 'name' => $subjectParams['before'] ]; } - if (array_key_exists('before', $subjectParams)) { + if (array_key_exists('after', $subjectParams)) { $params['after'] = [ 'type' => 'highlight', 'id' => $subjectParams['after'], diff --git a/tests/unit/Activity/DeckProviderTest.php b/tests/unit/Activity/DeckProviderTest.php index d4dfb6531..81a023585 100644 --- a/tests/unit/Activity/DeckProviderTest.php +++ b/tests/unit/Activity/DeckProviderTest.php @@ -24,6 +24,7 @@ namespace OCA\Deck\Activity; use OC\Activity\Event; +use OCA\Deck\Db\Acl; use OCP\Activity\IEvent; use OCP\IL10N; use OCP\IURLGenerator; @@ -266,4 +267,188 @@ class DeckProviderTest extends TestCase { $this->assertEquals('
ABCD
', $event->getParsedMessage()); } + public function testParseParamForBoard() { + $params = []; + $subjectParams = [ + 'board' => [ + 'id' => 1, + 'title' => 'Board name', + ], + ]; + $expected = [ + 'board' => [ + 'type' => 'highlight', + 'id' => 1, + 'name' => 'Board name', + 'link' => '#!/board/1/', + ], + ]; + $actual = $this->invokePrivate($this->provider, 'parseParamForBoard', ['board', $subjectParams, $params]); + $this->assertEquals($expected, $actual); + } + + public function testParseParamForStack() { + $params = []; + $subjectParams = [ + 'stack' => [ + 'id' => 1, + 'title' => 'Stack name', + ], + ]; + $expected = [ + 'stack' => [ + 'type' => 'highlight', + 'id' => 1, + 'name' => 'Stack name', + ], + ]; + $actual = $this->invokePrivate($this->provider, 'parseParamForStack', ['stack', $subjectParams, $params]); + $this->assertEquals($expected, $actual); + } + + public function testParseParamForAttachment() { + $this->urlGenerator->expects($this->once()) + ->method('linkToRoute') + ->willReturn('/link/to/attachment'); + $params = []; + $subjectParams = [ + 'attachment' => [ + 'id' => 1, + 'data' => 'File name', + ], + 'card' => [ + 'id' => 1, + ] + ]; + $expected = [ + 'attachment' => [ + 'type' => 'highlight', + 'id' => 1, + 'name' => 'File name', + 'link' => '/link/to/attachment', + ], + ]; + $actual = $this->invokePrivate($this->provider, 'parseParamForAttachment', ['attachment', $subjectParams, $params]); + $this->assertEquals($expected, $actual); + } + + public function testParseParamForAssignedUser() { + $user = $this->createMock(IUser::class); + $user->expects($this->once()) + ->method('getDisplayName') + ->willReturn('User 1'); + $this->userManager->expects($this->once()) + ->method('get') + ->willReturn($user); + $params = []; + $subjectParams = [ + 'assigneduser' => 'user1', + ]; + $expected = [ + 'assigneduser' => [ + 'type' => 'user', + 'id' => 'user1', + 'name' => 'User 1' + ], + ]; + $actual = $this->invokePrivate($this->provider, 'parseParamForAssignedUser', [$subjectParams, $params]); + $this->assertEquals($expected, $actual); + } + + public function testParseParamForLabel() { + $params = []; + $subjectParams = [ + 'label' => [ + 'id' => 1, + 'title' => 'Label title', + ], + ]; + $expected = [ + 'label' => [ + 'type' => 'highlight', + 'id' => 1, + 'name' => 'Label title' + ], + ]; + $actual = $this->invokePrivate($this->provider, 'parseParamForLabel', [$subjectParams, $params]); + $this->assertEquals($expected, $actual); + } + + public function testParseParamForAclUser() { + $user = $this->createMock(IUser::class); + $user->expects($this->once()) + ->method('getDisplayName') + ->willReturn('User 1'); + $this->userManager->expects($this->once()) + ->method('get') + ->willReturn($user); + $params = []; + $subjectParams = [ + 'acl' => [ + 'id' => 1, + 'type' => Acl::PERMISSION_TYPE_USER, + 'participant' => 'user1' + ], + ]; + $expected = [ + 'acl' => [ + 'type' => 'user', + 'id' => 'user1', + 'name' => 'User 1' + ], + ]; + $actual = $this->invokePrivate($this->provider, 'parseParamForAcl', [$subjectParams, $params]); + $this->assertEquals($expected, $actual); + } + + public function testParseParamForAclGroup() { + $params = []; + $subjectParams = [ + 'acl' => [ + 'id' => 1, + 'type' => Acl::PERMISSION_TYPE_GROUP, + 'participant' => 'group' + ], + ]; + $expected = [ + 'acl' => [ + 'type' => 'highlight', + 'id' => 'group', + 'name' => 'group' + ], + ]; + $actual = $this->invokePrivate($this->provider, 'parseParamForAcl', [$subjectParams, $params]); + $this->assertEquals($expected, $actual); + } + + public function testParseParamForChanges() { + $event = $this->createMock(IEvent::class); + $params = []; + $subjectParams = [ + 'before' => 'ABC', + 'after' => 'BCD' + ]; + $expected = [ + 'before' => [ + 'type' => 'highlight', + 'id' => 'ABC', + 'name' => 'ABC' + ], + 'after' => [ + 'type' => 'highlight', + 'id' => 'BCD', + 'name' => 'BCD' + ], + ]; + $actual = $this->invokePrivate($this->provider, 'parseParamForChanges', [$subjectParams, $params, $event]); + $this->assertEquals($expected, $actual); + } + + public function invokePrivate(&$object, $methodName, array $parameters = array()) + { + $reflection = new \ReflectionClass(get_class($object)); + $method = $reflection->getMethod($methodName); + $method->setAccessible(true); + return $method->invokeArgs($object, $parameters); + } }