forked from nextcloud/forms
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Jonas Rittershofer <jotoeri@users.noreply.github.com>
- Loading branch information
Showing
9 changed files
with
962 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,4 +29,7 @@ js/ | |
v8-compile-cache-0/ | ||
|
||
# php-cs cache | ||
.php_cs.cache | ||
.php_cs.cache | ||
|
||
# phpunit cache | ||
.phpunit.result.cache |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
Oops, something went wrong.