handler = new Handler( \OCP\Server::get(IDBConnection::class), \OCP\Server::get(IManager::class), ); $this->handler->delete($this->getNotification([ 'getApp' => 'testing_notifications', ])); } protected function tearDown(): void { $this->handler = new Handler( \OCP\Server::get(IDBConnection::class), \OCP\Server::get(IManager::class), ); $this->handler->delete($this->getNotification([ 'getApp' => 'testing_notifications', ])); parent::tearDown(); } public function testFull(): void { $notification = $this->getNotification([ 'getApp' => 'testing_notifications', 'getUser' => 'test_user1', 'getDateTime' => new \DateTime(), 'getObjectType' => 'notification', 'getObjectId' => '1337', 'getSubject' => 'subject', 'getSubjectParameters' => [], 'getMessage' => 'message', 'getMessageParameters' => [], 'getLink' => 'https://example.tld/notification', 'getIcon' => 'https://example.tld/icon', 'getActions' => [ [ 'getLabel' => 'action_label', 'getLink' => 'https://example.tld/action', 'getRequestType' => 'GET', 'isPrimary' => false, ] ], ]); $limitedNotification1 = $this->getNotification([ 'getApp' => 'testing_notifications', 'getUser' => 'test_user1', ]); $limitedNotification2 = $this->getNotification([ 'getApp' => 'testing_notifications', 'getUser' => 'test_user2', ]); // Make sure there is no notification $this->assertSame(0, $this->handler->count($limitedNotification1), 'Wrong notification count for user1 before adding'); $notifications = $this->handler->get($limitedNotification1); $this->assertCount(0, $notifications, 'Wrong notification count for user1 before beginning'); $this->assertSame(0, $this->handler->count($limitedNotification2), 'Wrong notification count for user2 before adding'); $notifications = $this->handler->get($limitedNotification2); $this->assertCount(0, $notifications, 'Wrong notification count for user2 before beginning'); // Add and count $this->assertGreaterThan(0, $this->handler->add($notification)); $this->assertSame(1, $this->handler->count($limitedNotification1), 'Wrong notification count for user1 after adding'); $this->assertSame(0, $this->handler->count($limitedNotification2), 'Wrong notification count for user2 after adding'); // Get and count $notifications = $this->handler->get($limitedNotification1); $this->assertCount(1, $notifications, 'Wrong notification get for user1 after adding'); $notifications = $this->handler->get($limitedNotification2); $this->assertCount(0, $notifications, 'Wrong notification get for user2 after adding'); // Delete and count again $this->handler->delete($notification); $this->assertSame(0, $this->handler->count($limitedNotification1), 'Wrong notification count for user1 after deleting'); $this->assertSame(0, $this->handler->count($limitedNotification2), 'Wrong notification count for user2 after deleting'); } public function testFullEmptyMessageForOracle(): void { $notification = $this->getNotification([ 'getApp' => 'testing_notifications', 'getUser' => 'test_user1', 'getDateTime' => new \DateTime(), 'getObjectType' => 'notification', 'getObjectId' => '1337', 'getSubject' => 'subject', 'getSubjectParameters' => [], 'getMessage' => '', 'getMessageParameters' => [], 'getLink' => 'https://example.tld/notification', 'getIcon' => 'https://example.tld/icon', 'getActions' => [ [ 'getLabel' => 'action_label', 'getLink' => 'https://example.tld/action', 'getRequestType' => 'GET', 'isPrimary' => false, ] ], ]); $limitedNotification1 = $this->getNotification([ 'getApp' => 'testing_notifications', 'getUser' => 'test_user1', ]); $limitedNotification2 = $this->getNotification([ 'getApp' => 'testing_notifications', 'getUser' => 'test_user2', ]); // Make sure there is no notification $this->assertSame(0, $this->handler->count($limitedNotification1), 'Wrong notification count for user1 before adding'); $notifications = $this->handler->get($limitedNotification1); $this->assertCount(0, $notifications, 'Wrong notification count for user1 before beginning'); $this->assertSame(0, $this->handler->count($limitedNotification2), 'Wrong notification count for user2 before adding'); $notifications = $this->handler->get($limitedNotification2); $this->assertCount(0, $notifications, 'Wrong notification count for user2 before beginning'); // Add and count $this->assertGreaterThan(0, $this->handler->add($notification)); $this->assertSame(1, $this->handler->count($limitedNotification1), 'Wrong notification count for user1 after adding'); $this->assertSame(0, $this->handler->count($limitedNotification2), 'Wrong notification count for user2 after adding'); // Get and count $notifications = $this->handler->get($limitedNotification1); $this->assertCount(1, $notifications, 'Wrong notification get for user1 after adding'); $notifications = $this->handler->get($limitedNotification2); $this->assertCount(0, $notifications, 'Wrong notification get for user2 after adding'); // Delete and count again $this->handler->delete($notification); $this->assertSame(0, $this->handler->count($limitedNotification1), 'Wrong notification count for user1 after deleting'); $this->assertSame(0, $this->handler->count($limitedNotification2), 'Wrong notification count for user2 after deleting'); } public function testDeleteById(): void { $notification = $this->getNotification([ 'getApp' => 'testing_notifications', 'getUser' => 'test_user1', 'getDateTime' => new \DateTime(), 'getObjectType' => 'notification', 'getObjectId' => '1337', 'getSubject' => 'subject', 'getSubjectParameters' => [], 'getMessage' => 'message', 'getMessageParameters' => [], 'getLink' => 'https://example.tld/notification', 'getIcon' => 'https://example.tld/icon', 'getActions' => [ [ 'getLabel' => 'action_label', 'getLink' => 'https://example.tld/action', 'getRequestType' => 'GET', 'isPrimary' => true, ] ], ]); $limitedNotification = $this->getNotification([ 'getApp' => 'testing_notifications', 'getUser' => 'test_user1', ]); // Make sure there is no notification $this->assertSame(0, $this->handler->count($limitedNotification)); $notifications = $this->handler->get($limitedNotification); $this->assertCount(0, $notifications); // Add and count $this->handler->add($notification); $this->assertSame(1, $this->handler->count($limitedNotification)); // Get and count $notifications = $this->handler->get($limitedNotification); $this->assertCount(1, $notifications); $notificationId = array_key_first($notifications); // Get with wrong user try { $this->handler->getById($notificationId, 'test_user2'); $this->fail('Exception of type NotificationNotFoundException expected'); } catch (\Exception $e) { $this->assertInstanceOf(NotificationNotFoundException::class, $e); } // Delete with wrong user $this->handler->deleteById($notificationId, 'test_user2', $notification); $this->assertSame(1, $this->handler->count($limitedNotification), 'Wrong notification count for user1 after trying to delete for user2'); // Get with correct user $getNotification = $this->handler->getById($notificationId, 'test_user1'); $this->assertInstanceOf(INotification::class, $getNotification); // Delete and count $this->handler->deleteById($notificationId, 'test_user1'); $this->assertSame(0, $this->handler->count($limitedNotification), 'Wrong notification count for user1 after deleting'); } protected function getNotification(array $values = []): INotification&MockObject { $notification = $this->getMockBuilder(INotification::class) ->getMock(); foreach ($values as $method => $returnValue) { if ($method === 'getActions') { $actions = []; foreach ($returnValue as $actionData) { $action = $this->getMockBuilder(IAction::class) ->getMock(); foreach ($actionData as $actionMethod => $actionValue) { $action->expects($this->any()) ->method($actionMethod) ->willReturn($actionValue); } $actions[] = $action; } $notification->expects($this->any()) ->method($method) ->willReturn($actions); } else { $notification->expects($this->any()) ->method($method) ->willReturn($returnValue); } } $defaultDateTime = new \DateTime(); $defaultDateTime->setTimestamp(0); $defaultValues = [ 'getApp' => '', 'getUser' => '', 'getDateTime' => $defaultDateTime, 'getObjectType' => '', 'getObjectId' => '', 'getSubject' => '', 'getSubjectParameters' => [], 'getMessage' => '', 'getMessageParameters' => [], 'getLink' => '', 'getIcon' => '', 'getActions' => [], ]; foreach ($defaultValues as $method => $returnValue) { if (isset($values[$method])) { continue; } $notification->expects($this->any()) ->method($method) ->willReturn($returnValue); } $defaultValues = [ 'setApp', 'setUser', 'setDateTime', 'setObject', 'setSubject', 'setMessage', 'setLink', 'setIcon', 'addAction', ]; foreach ($defaultValues as $method) { $notification->expects($this->any()) ->method($method) ->willReturnSelf(); } return $notification; } }