Skip to content

Commit

Permalink
Activitiy Tests
Browse files Browse the repository at this point in the history
Signed-off-by: Jonas Rittershofer <jotoeri@users.noreply.github.com>
  • Loading branch information
jotoeri committed Mar 18, 2021
1 parent dec97a3 commit 71d8692
Show file tree
Hide file tree
Showing 9 changed files with 962 additions and 9 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,7 @@ js/
v8-compile-cache-0/

# php-cs cache
.php_cs.cache
.php_cs.cache

# phpunit cache
.phpunit.result.cache
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"scripts": {
"cs:fix": "php-cs-fixer fix",
"cs:check": "php-cs-fixer fix --dry-run --diff",
"lint": "find . -name \\*.php -not -path './vendor/*' -print0 | xargs -0 -n1 php -l"
"lint": "find . -name \\*.php -not -path './vendor/*' -print0 | xargs -0 -n1 php -l",
"phpunit": "phpunit"
},
"require-dev": {
"christophwurst/nextcloud": "^20.0",
Expand Down
14 changes: 7 additions & 7 deletions lib/Activity/Provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public function parse($language, IEvent $event, IEvent $previousEvent = null): I
* @return string
* @throws \InvalidArgumentException
*/
private function getSubjectString(IL10N $l10n, string $subject): string {
public function getSubjectString(IL10N $l10n, string $subject): string {
switch ($subject) {
case ActivityConstants::SUBJECT_NEWSHARE:
return $l10n->t('{user} has shared the form {formTitle} with you');
Expand All @@ -145,7 +145,7 @@ private function getSubjectString(IL10N $l10n, string $subject): string {
* @param array $parameters Array of Rich-Parameters as created by getRichParams()
* @return string
*/
private function parseSubjectString(string $subjectString, array $parameters): string {
public function parseSubjectString(string $subjectString, array $parameters): string {
$placeholders = [];
$replacements = [];
foreach ($parameters as $paramKey => $paramValue) {
Expand All @@ -161,7 +161,7 @@ private function parseSubjectString(string $subjectString, array $parameters): s
* @param array $params Array of stored SubjectParameters
* @return array
*/
private function getRichParams(IL10N $l10n, string $subject, array $params): array {
public function getRichParams(IL10N $l10n, string $subject, array $params): array {
switch ($subject) {
case ActivityConstants::SUBJECT_NEWSHARE:
return [
Expand Down Expand Up @@ -189,7 +189,7 @@ private function getRichParams(IL10N $l10n, string $subject, array $params): arr
* @param string $userId
* @return array
*/
private function getRichUser(IL10N $l10n, string $userId): array {
public function getRichUser(IL10N $l10n, string $userId): array {
// Special handling for anonyomous users
if (substr($userId, 0, 10) === 'anon-user-') {
return [
Expand Down Expand Up @@ -221,7 +221,7 @@ private function getRichUser(IL10N $l10n, string $userId): array {
* @param string $groupId
* @return array
*/
private function getRichGroup(string $groupId): array {
public function getRichGroup(string $groupId): array {
// Get Displayname, if group still exists. Otherwise just show stored groupId
$group = $this->groupManager->get($groupId);
$displayName = '';
Expand All @@ -246,7 +246,7 @@ private function getRichGroup(string $groupId): array {
* @param string $route Optional Path of specific route to append to hash-url
* @return array
*/
private function getRichFormTitle(string $formTitle, string $formHash, string $route = ''): array {
public function getRichFormTitle(string $formTitle, string $formHash, string $route = ''): array {
// Base-url to Forms app. Fallback in case of MapperException.
$formLink = $this->urlGenerator->linkToRouteAbsolute('forms.page.index');

Expand Down Expand Up @@ -287,7 +287,7 @@ private function getRichFormTitle(string $formTitle, string $formHash, string $r
* @param string $subject The events subject
* @return string
*/
private function getEventIcon(string $subject): string {
public function getEventIcon(string $subject): string {
switch ($subject) {
case ActivityConstants::SUBJECT_NEWSHARE:
case ActivityConstants::SUBJECT_NEWGROUPSHARE:
Expand Down
176 changes: 176 additions & 0 deletions tests/Unit/Activity/ActivityManagerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
<?php

declare(strict_types=1);
/**
* @copyright Copyright (c) 2021 Jonas Rittershofer <jotoeri@users.noreply.github.com>
*
* @author Jonas Rittershofer <jotoeri@users.noreply.github.com>
*
* @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\Forms\Tests\Unit\Activity;

use OCA\Forms\Activity\ActivityManager;

use OCA\Forms\Db\Form;
use OCP\Activity\IEvent;
use OCP\Activity\IManager;
use OCP\IGroup;
use OCP\IGroupManager;
use OCP\ILogger;
use OCP\IUser;
use OCP\IUserSession;
use PHPUnit\Framework\MockObject\MockObject;
use Test\TestCase;

class ActivityManagerTest extends TestCase {

/** @var ActivityManager */
private $activityManager;

/** @var IManager|MockObject */
private $manager;

/** @var IGroupManager|MockObject */
private $groupManager;

/** @var ILogger|MockObject */
private $logger;

public function setUp(): void {
$this->manager = $this->createMock(IManager::class);
$this->groupManager = $this->createMock(IGroupManager::class);
$this->logger = $this->createMock(ILogger::class);
$userSession = $this->createMock(IUserSession::class);

$user = $this->createMock(IUser::class);
$user->expects($this->any())
->method('getUID')
->willReturn('currentUser');
$userSession->expects($this->once())
->method('getUser')
->willReturn($user);

$this->activityManager = new ActivityManager('forms', $this->manager, $this->groupManager, $this->logger, $userSession);
}

public function testPublishNewShare() {
// Can't mock the DB-Classes, as their Property-Methods are not explicitely defined.
$form = new Form();
$form->setId(5);
$form->setTitle('TestForm-Title');
$form->setHash('abcdefg12345');
$shareeId = 'sharedUser';

$event = $this->createMock(IEvent::class);
$this->manager->expects($this->once())
->method('generateEvent')
->willReturn($event);
$event->expects($this->once())->method('setApp')->with('forms')->willReturn($event);
$event->expects($this->once())->method('setType')->with('forms_newshare')->willReturn($event);
$event->expects($this->once())->method('setAffectedUser')->with($shareeId)->willReturn($event);
$event->expects($this->once())->method('setAuthor')->with('currentUser')->willReturn($event);
$event->expects($this->once())->method('setObject')->with('form', 5)->willReturn($event);
$event->expects($this->once())->method('setSubject')->with('newshare', [
'userId' => 'currentUser',
'formTitle' => 'TestForm-Title',
'formHash' => 'abcdefg12345'
])->willReturn($event);

$this->manager->expects($this->once())
->method('publish')
->with($event);

$this->activityManager->publishNewShare($form, $shareeId);
}

public function testPublishNewGroupShare() {
// Can't mock the DB-Classes, as their Property-Methods are not explicitely defined.
$form = new Form();
$form->setId(5);
$form->setTitle('TestForm-Title');
$form->setHash('abcdefg12345');
$groupId = 'sharedGroup';

$group = $this->createMock(IGroup::class);
$user = $this->createMock(IUser::class);

$user->expects($this->exactly(3))
->method('getUID')
->will($this->onConsecutiveCalls('user1', 'user2', 'user3'));
$group->expects($this->once())
->method('getUsers')
->willReturn([$user, $user, $user]);
$this->groupManager->expects($this->once())
->method('get')
->with($groupId)
->willReturn($group);

$event = $this->createMock(IEvent::class);
$this->manager->expects($this->exactly(3))
->method('generateEvent')
->willReturn($event);
$event->expects($this->exactly(3))->method('setApp')->with('forms')->willReturn($event);
$event->expects($this->exactly(3))->method('setType')->with('forms_newshare')->willReturn($event);
$event->expects($this->exactly(3))->method('setAffectedUser')->withConsecutive(['user1'], ['user2'], ['user3'])->willReturn($event);
$event->expects($this->exactly(3))->method('setAuthor')->with('currentUser')->willReturn($event);
$event->expects($this->exactly(3))->method('setObject')->with('form', 5)->willReturn($event);
$event->expects($this->exactly(3))->method('setSubject')->with('newgroupshare', [
'userId' => 'currentUser',
'formTitle' => 'TestForm-Title',
'formHash' => 'abcdefg12345',
'groupId' => 'sharedGroup'
])->willReturn($event);

$this->manager->expects($this->exactly(3))
->method('publish')
->with($event);

$this->activityManager->publishNewGroupShare($form, $groupId);
}

public function testPublishNewSubmission() {
// Can't mock the DB-Classes, as their Property-Methods are not explicitely defined.
$form = new Form();
$form->setId(5);
$form->setTitle('TestForm-Title');
$form->setHash('abcdefg12345');
$form->setOwnerId('formOwner');
$submittorId = 'submittingUser';

$event = $this->createMock(IEvent::class);
$this->manager->expects($this->once())
->method('generateEvent')
->willReturn($event);
$event->expects($this->once())->method('setApp')->with('forms')->willReturn($event);
$event->expects($this->once())->method('setType')->with('forms_newsubmission')->willReturn($event);
$event->expects($this->once())->method('setAffectedUser')->with('formOwner')->willReturn($event);
$event->expects($this->once())->method('setAuthor')->with('submittingUser')->willReturn($event);
$event->expects($this->once())->method('setObject')->with('form', 5)->willReturn($event);
$event->expects($this->once())->method('setSubject')->with('newsubmission', [
'userId' => 'submittingUser',
'formTitle' => 'TestForm-Title',
'formHash' => 'abcdefg12345'
])->willReturn($event);

$this->manager->expects($this->once())
->method('publish')
->with($event);

$this->activityManager->publishNewSubmission($form, $submittorId);
}
}
88 changes: 88 additions & 0 deletions tests/Unit/Activity/FilterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

declare(strict_types=1);
/**
* @copyright Copyright (c) 2021 Jonas Rittershofer <jotoeri@users.noreply.github.com>
*
* @author Jonas Rittershofer <jotoeri@users.noreply.github.com>
*
* @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\Forms\Tests\Unit\Activity;

use OCA\Forms\Activity\Filter;

use OCP\IL10N;
use OCP\IURLGenerator;
use PHPUnit\Framework\MockObject\MockObject;
use Test\TestCase;

class FilterTest extends TestCase {

/** @var Filter */
private $filter;

/** @var IL10N|MockObject */
private $l10n;

/** @var IURLGenerator|MockObject */
private $urlGenerator;

public function setUp(): void {
$this->l10n = $this->createMock(IL10N::class);
$this->urlGenerator = $this->createMock(IURLGenerator::class);
$this->filter = new Filter('forms', $this->l10n, $this->urlGenerator);
}

public function testGetIdentifier() {
$this->assertEquals('forms', $this->filter->getIdentifier());
}

public function testGetName() {
$this->l10n->expects($this->once())
->method('t')
->with('Forms')
->willReturn('Forms');
$this->assertEquals('Forms', $this->filter->getName());
}

public function testGetIcon() {
$this->urlGenerator->expects($this->once())
->method('imagePath')
->with('forms', 'forms-dark.svg')
->willReturn('apps/forms/img/forms-dark.svg');
$this->urlGenerator->expects($this->once())
->method('getAbsoluteUrl')
->will($this->returnCallback(function ($path) {
return 'http://localhost/' . $path;
}));
$this->assertEquals('http://localhost/apps/forms/img/forms-dark.svg', $this->filter->getIcon());
}

public function testGetPriority() {
$this->assertEquals(60, $this->filter->getPriority());
}

public function testAllowedApps() {
$this->assertEquals(['forms'], $this->filter->allowedApps());
}

public function testFilterTypes() {
$data = ['forms_newshare', 'forms_newsubmission'];
$this->assertEquals($data, $this->filter->filterTypes($data));
}
}
Loading

0 comments on commit 71d8692

Please sign in to comment.