Unverified Commit 71d8692b authored by Jonas Rittershofer's avatar Jonas Rittershofer
Browse files

Activitiy Tests


Signed-off-by: default avatarJonas Rittershofer <jotoeri@users.noreply.github.com>
parent dec97a39
......@@ -29,4 +29,7 @@ js/
v8-compile-cache-0/
# php-cs cache
.php_cs.cache
\ No newline at end of file
.php_cs.cache
# phpunit cache
.phpunit.result.cache
......@@ -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",
......
......@@ -122,7 +122,7 @@ class Provider implements IProvider {
* @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');
......@@ -145,7 +145,7 @@ class Provider implements IProvider {
* @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) {
......@@ -161,7 +161,7 @@ class Provider implements IProvider {
* @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 [
......@@ -189,7 +189,7 @@ class Provider implements IProvider {
* @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 [
......@@ -221,7 +221,7 @@ class Provider implements IProvider {
* @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 = '';
......@@ -246,7 +246,7 @@ class Provider implements IProvider {
* @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');
......@@ -287,7 +287,7 @@ class Provider implements IProvider {
* @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:
......
<?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);
}
}
<?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));
}
}
<?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\Provider;
use OCA\Forms\Db\Form;
use OCA\Forms\Db\FormMapper;
use OCP\Activity\IEvent;
use OCP\Activity\IEventMerger;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\IGroup;
use OCP\IGroupManager;
use OCP\IL10N;
use OCP\ILogger;
use OCP\IURLGenerator;
use OCP\IUser;
use OCP\IUserManager;
use OCP\L10N\IFactory;
use OCP\RichObjectStrings\InvalidObjectExeption;
use OCP\RichObjectStrings\IValidator;
use PHPUnit\Framework\MockObject\MockObject;
use Test\TestCase;
class ProviderTest extends TestCase {
/** @var Provider */
private $provider;
/** @var FormMapper|MockObject */
private $formMapper;
/** @var IEventMerger|MockObject */
private $eventMerger;
/** @var IGroupManager|MockObject */
private $groupManager;
/** @var IL10N|MockObject */
private $l10n;
/** @var ILogger|MockObject */
private $logger;
/** @var IURLGenerator|MockObject */
private $urlGenerator;
/** @var IUserManager|MockObject */
private $userManager;
/** @var IFactory|MockObject */
private $l10nFactory;
/** @var IValidator|MockObject */
private $validator;
public function setUp(): void {
$this->formMapper = $this->createMock(FormMapper::class);
$this->eventMerger = $this->createMock(IEventMerger::class);
$this->groupManager = $this->createMock(IGroupManager::class);
$this->logger = $this->createMock(ILogger::class);
$this->urlGenerator = $this->createMock(IURLGenerator::class);
$this->userManager = $this->createMock(IUserManager::class);
$this->l10nFactory = $this->createMock(IFactory::class);
$this->validator = $this->createMock(IValidator::class);
$this->urlGenerator->expects($this->any())
->method('linkToRouteAbsolute')
->with('forms.page.index')
->willReturn('http://localhost/apps/forms');
$this->urlGenerator->expects($this->any())
->method('getAbsoluteUrl')
->will($this->returnCallback(function (string $path) {
return 'http://localhost' . $path;
}));
$this->urlGenerator->expects($this->any())
->method('imagePath')
->will($this->returnValueMap([
['core', 'actions/shared.svg', '/core/img/actions/shared.svg'],
['core', 'actions/add.svg', '/core/img/actions/add.svg'],
['forms', 'forms-dark.svg', '/apps/forms/img/forms-dark.svg']
]));
$this->provider = new Provider('forms',
$this->formMapper,
$this->eventMerger,
$this->groupManager,
$this->logger,
$this->urlGenerator,
$this->userManager,
$this->l10nFactory,
$this->validator);
// Only for the test, Provider creates it from Factory
$this->l10n = $this->createMock(IL10N::class);
}
// Wrong app-name should be blocked
public function testWrongApp() {
$event = $this->createMock(IEvent::class);
$event->expects($this->once())
->method('getApp')
->willReturn('someOtherApp');
$this->expectException(\InvalidArgumentException::class);
$this->provider->parse('de_DE', $event);
}
// Testing the parse function needs a full parse.
public function testFullParse() {
$l10n = $this->createMock(IL10N::class);
$l10n->expects($this->once())
->method('t')
->will($this->returnCallback(function (string $identity) {
return $identity;
}));
$this->l10nFactory->expects($this->once())
->method('get')
->willReturn($l10n);
$user = $this->createMock(IUser::class);
$user->expects($this->any())
->method('getDisplayName')
->willReturn('The affected User');
$this->userManager->expects($this->once())
->method('get')
->with('affectedUser')
->willReturn($user);
$form = new Form;
$form->setTitle('SomeChangedNiceFormTitle');
$this->formMapper->expects($this->once())
->method('findbyHash')
->with('54321gfedcba')
->willReturn($form);
$event = $this->createMock(IEvent::class);
$event->expects($this->once())
->method('getApp')
->willReturn('forms');
$event->expects($this->atLeastOnce())
->method('getSubject')
->willReturn('newsubmission');
$event->expects($this->atLeastOnce())
->method('getSubjectParameters')
->willReturn([
'userId' => 'affectedUser',
'formTitle' => 'SomeNiceFormTitle',
'formHash' => '54321gfedcba'
]);
$event->expects($this->once())
->method('setParsedSubject')
->with('The affected User answered your form SomeChangedNiceFormTitle');
$event->expects($this->once())
->method('setRichSubject')
->with('{user} answered your form {formTitle}', [
'user' => [
'type' => 'user',
'id' => 'affectedUser',
'name' => 'The affected User'
],
'formTitle' => [
'type' => 'forms-form',
'id' => '54321gfedcba',
'name' => 'SomeChangedNiceFormTitle',
'link' => 'http://localhost/apps/forms/54321gfedcba/results'
]]);
$event->expects($this->once())
->method('setIcon')
->with('http://localhost/core/img/actions/add.svg');
$this->eventMerger->expects($this->once())
->method('mergeEvents')
->with('user', $event, $event)
->willReturn($event);
$this->assertEquals($event, $this->provider->parse('de_DE', $event, $event));
}
// Expected data for Subject-Strings
public function dataGetSubjectString() {
return [
['newshare', '{user} has shared the form {formTitle} with you'],
['newgroupshare', '{user} has shared the form {formTitle} with group {group}'],
['newsubmission', '{user} answered your form {formTitle}']
];
}
/**
* @dataProvider dataGetSubjectString
*
* @param string $subject
* @param string $expected
*/
public function testGetSubjectString(string $subject, string $expected) {
$l10n = $this->createMock(IL10N::class);
$l10n->expects($this->once())
->method('t')
->will($this->returnCallback(function (string $identity) {
return $identity;
}));
$this->assertEquals($expected, $this->provider->getSubjectString($l10n, $subject));
}
// Unknown Subject throws exception
public function testGetUnknownSubjectString() {
$l10n = $this->createMock(IL10N::class);
$l10n->expects($this->never())
->method('t');
$this->expectException(\InvalidArgumentException::class);
$this->provider->getSubjectString($l10n, 'someUnknownSubject');
}
// Test insertions of parameters name
public function testParseSubjectString() {
$this->assertEquals('Heinz and Ernst in a Text with Erna',
$this->provider->parseSubjectString('{param1} and {param2} in a Text with {paramz}', [
'param1' => [
'type' => 'xy',
'name' => 'Heinz'
],
'param2' => [
'type' => 'ab',
'name' => 'Ernst',
'link' => 'http://somehost'
],
'paramz' => [
'type' => 'highlight',
'name' =>