Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
more unit tests
Signed-off-by: Artur Neumann <[email protected]>
  • Loading branch information
individual-it committed Nov 8, 2022
commit ab4665509e9dd54b146d16c55bf9c042bee28a9e
243 changes: 221 additions & 22 deletions tests/lib/Service/OpenProjectAPIServiceCheckNotificationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

namespace OCA\OpenProject\Service;

use OCA\Notifications\Handler;
use OCP\Http\Client\IClientService;
use OCP\Http\Client\IResponse;
use OCP\IConfig;
use OCP\IUserManager;
Expand All @@ -17,9 +19,16 @@
use PHPUnit\Framework\TestCase;

class OpenProjectAPIServiceCheckNotificationsTest extends TestCase {
public function testCheckNotifications(): void {
$configMock = $this->getMockBuilder(IConfig::class)->getMock();
$configMock
/** @var IConfig $configMock */
private $configMock;

/**
* @return void
* @before
*/
public function setUpMocks(): void {
$this->configMock = $this->getMockBuilder(IConfig::class)->getMock();
$this->configMock
->method('getUserValue')
->withConsecutive(
[$this->anything(), 'integration_openproject', 'token'],
Expand All @@ -34,7 +43,7 @@ public function testCheckNotifications(): void {
'refresh-token',
);

$configMock
$this->configMock
->method('getAppValue')
->withConsecutive(
['integration_openproject', 'default_enable_notifications','0'],
Expand All @@ -47,26 +56,211 @@ public function testCheckNotifications(): void {
'SECRET',
'https://openproject',
);
}

/**
* @param string|null $oPNotificationAPIResponse
* @return IClientService
*/
private function getClientServiceMock($oPNotificationAPIResponse = null): IClientService {
$response = $this->getMockBuilder(IResponse::class)->getMock();
$oPNotificationAPIResponse = '{
if ($oPNotificationAPIResponse === null) {
$oPNotificationAPIResponse = '{
"_type": "Collection",
"_embedded": {
"elements":
';
$oPNotificationAPIResponse .= file_get_contents(
__DIR__ . '/../../jest/fixtures/notificationsResponse.json'
);
$oPNotificationAPIResponse .= '}}';
$oPNotificationAPIResponse .= file_get_contents(
__DIR__ . '/../../jest/fixtures/notificationsResponse.json'
);
$oPNotificationAPIResponse .= '}}';
}
$response->method('getBody')->willReturn($oPNotificationAPIResponse);
$ocClient = $this->getMockBuilder('\OCP\Http\Client\IClient')->getMock();
$ocClient->method('get')->willReturn($response);
$clientService = $this->getMockBuilder('\OCP\Http\Client\IClientService')->getMock();
$clientService->method('newClient')->willReturn($ocClient);
return $clientService;
}

/**
* @param IManager $notificationManagerMock
* @param IClientService $clientServiceMock
* @param Handler $handlerMock
* @return OpenProjectAPIService
*/
private function getService($notificationManagerMock, $clientServiceMock, $handlerMock): OpenProjectAPIService {
return new OpenProjectAPIService(
'integration_openproject',
\OC::$server->get(IUserManager::class),
$this->createMock(\OCP\IAvatarManager::class),
$this->createMock(\Psr\Log\LoggerInterface::class),
$this->createMock(\OCP\IL10N::class),
$this->configMock,
$notificationManagerMock,
$clientServiceMock,
$this->createMock(\OCP\Files\IRootFolder::class),
$this->createMock(\OCP\IURLGenerator::class),
$this->createMock(\OCP\ICacheFactory::class),
$handlerMock,
);
}

public function testCheckNotifications(): void {
$notificationManagerMock = $this->getMockBuilder(IManager::class)->getMock();
$notificationMock = $this->getMockBuilder(INotification::class)
->getMock();

$notificationMock
->expects($this->exactly(3))
->method('setSubject')
->withConsecutive(
[
'op_notification',
[
'wpId' => '36',
'resourceTitle' => 'write a software',
'projectTitle' => 'Dev-large',
'count' => 2,
'reasons' => ['assigned'],
'actors' => ['Admin de DEV user'],
'updatedAt' => '2022-08-17T10:28:12Z'
]
],
[
'op_notification',
[
'wpId' => '17',
'resourceTitle' => 'Create wireframes for new landing page',
'projectTitle' => 'Scrum project',
'count' => 5,
'reasons' => [0 => 'assigned', 3 => 'mentioned'],
'actors' => [0 => 'Admin de DEV user', 2 => 'Artur Neumann'],
'updatedAt' => '2022-08-17T10:27:41Z'
]
],
[
'op_notification',
[
'wpId' => '18',
'resourceTitle' => 'Contact form',
'projectTitle' => 'Scrum project',
'count' => 1,
'reasons' => ['mentioned'],
'actors' => ['Artur Neumann'],
'updatedAt' => '2022-08-09T08:00:08Z'
]
]
);

$notificationManagerMock
->expects($this->exactly(4)) //once for marking as read and once for every notification
->method('createNotification')
->willReturn($notificationMock);

$notificationManagerMock
->expects($this->exactly(3))
->method('notify');

$service = $this->getService(
$notificationManagerMock,
$this->getClientServiceMock(),
$this->createMock(Handler::class)
);
$service->checkNotifications();
}
public function testCheckNotificationsAfterAllNotificationsAreMarkedAsRead(): void {
$oPNotificationAPIResponse = '{
"_type": "Collection",
"_embedded": {
"elements": []
}
}
';
$notificationManagerMock = $this->getMockBuilder(IManager::class)->getMock();
$notificationMock = $this->getMockBuilder(INotification::class)
->getMock();

$notificationMock
->expects($this->never())
->method('setSubject');

$notificationManagerMock
->expects($this->exactly(1)) //for marking as read
->method('createNotification')
->willReturn($notificationMock);

$notificationManagerMock
->expects($this->exactly(2))
->method('markProcessed');

$currentNotificationMock0 = $this->getMockBuilder(INotification::class)->getMock();
$currentNotificationMock0
->method('getSubjectParameters')
->willReturn(['wpId' => 34, 'updatedAt' => '2022-11-08T06:34:40Z']);
$currentNotificationMock1 = $this->getMockBuilder(INotification::class)->getMock();
$currentNotificationMock1
->method('getSubjectParameters')
->willReturn(['wpId' => 16, 'updatedAt' => '2022-11-07T06:34:40Z']);

$handlerMock = $this->getMockBuilder(Handler::class)->disableOriginalConstructor()->getMock();
$handlerMock->method('get')
->willReturn(
[12 => $currentNotificationMock0, 13 => $currentNotificationMock1]
);
$service = $this->getService(
$notificationManagerMock,
$this->getClientServiceMock($oPNotificationAPIResponse),
$handlerMock
);
$service->checkNotifications();
}
public function testCheckNotificationsAfterAllNotificationsOfOneWPAreMarkedAsRead(): void {
$notificationManagerMock = $this->getMockBuilder(IManager::class)->getMock();
$notificationMock = $this->getMockBuilder(INotification::class)
->getMock();

$notificationMock
->expects($this->exactly(3)) // new notifications should be set
->method('setSubject');

$notificationManagerMock
->expects($this->exactly(4)) //once for marking as read and once for every notification
->method('createNotification')
->willReturn($notificationMock);

$notificationManagerMock
->expects($this->exactly(3)) // for new notifications
->method('notify');

$notificationManagerMock
->expects($this->exactly(2))
->method('markProcessed'); // for current ones that do not exist in the new response

// current notifications of WP that do not exist in the new response
$currentNotificationMock0 = $this->getMockBuilder(INotification::class)->getMock();
$currentNotificationMock0
->method('getSubjectParameters')
->willReturn(['wpId' => 34, 'updatedAt' => '2022-11-08T06:34:40Z']);
$currentNotificationMock1 = $this->getMockBuilder(INotification::class)->getMock();
$currentNotificationMock1
->method('getSubjectParameters')
->willReturn(['wpId' => 16, 'updatedAt' => '2022-11-07T06:34:40Z']);

$handlerMock = $this->getMockBuilder(Handler::class)->disableOriginalConstructor()->getMock();
$handlerMock->method('get')
->willReturn(
[12 => $currentNotificationMock0, 13 => $currentNotificationMock1]
);
$service = $this->getService(
$notificationManagerMock,
$this->getClientServiceMock(),
$handlerMock
);
$service->checkNotifications();
}
public function testCheckNotificationsAfterOneWPReceivedANewNotification(): void {
$notificationManagerMock = $this->getMockBuilder(IManager::class)->getMock();
$notificationMock = $this->getMockBuilder(INotification::class)
->getMock();

Expand Down Expand Up @@ -118,22 +312,27 @@ public function testCheckNotifications(): void {
->willReturn($notificationMock);

$notificationManagerMock
->expects($this->exactly(3))
->expects($this->exactly(3)) // for new notifications
->method('notify');

$service = new OpenProjectAPIService(
'integration_openproject',
\OC::$server->get(IUserManager::class),
$this->createMock(\OCP\IAvatarManager::class),
$this->createMock(\Psr\Log\LoggerInterface::class),
$this->createMock(\OCP\IL10N::class),
$configMock,
$notificationManagerMock
->expects($this->exactly(1))
->method('markProcessed'); // for the notification that needs to be upldated

// this notification is also part of the response, but the response also
// contains an other newer notification of that WP
$currentNotificationMock0 = $this->getMockBuilder(INotification::class)->getMock();
$currentNotificationMock0
->method('getSubjectParameters')
->willReturn(['wpId' => 36, 'updatedAt' => '2022-08-17T10:13:25Z']);

$handlerMock = $this->getMockBuilder(Handler::class)->disableOriginalConstructor()->getMock();
$handlerMock->method('get')
->willReturn([12 => $currentNotificationMock0]);
$service = $this->getService(
$notificationManagerMock,
$clientService,
$this->createMock(\OCP\Files\IRootFolder::class),
$this->createMock(\OCP\IURLGenerator::class),
$this->createMock(\OCP\ICacheFactory::class),
$this->createMock(\OCA\Notifications\Handler::class),
$this->getClientServiceMock(),
$handlerMock
);
$service->checkNotifications();
}
Expand Down