document = $document; $this->model = $model; } } class NotificationsTest extends TestCase { private Authorization $authorization; private Database $database; protected function setUp(): void { $this->authorization = new Authorization(); $this->authorization->addRole(Role::any()->toString()); $this->database = new Database(new Memory(), new Cache(new NoCache())); $this->database ->setAuthorization($this->authorization) ->setDatabase('notificationEndpointTests') ->setNamespace('notifications_' . \uniqid()); $permissions = [ Permission::create(Role::any()), Permission::read(Role::any()), Permission::update(Role::any()), Permission::delete(Role::any()), ]; $this->database->create(); $this->database->createCollection('alerts', [], [], $permissions, false); $this->database->createAttribute('alerts', 'messageId', Database::VAR_STRING, 255, false); $this->database->createAttribute('alerts', 'recipientHash', Database::VAR_STRING, 64, true); $this->database->createAttribute('alerts', 'type', Database::VAR_STRING, 64, false, 'info'); $this->database->createAttribute('alerts', 'channel', Database::VAR_STRING, 64, true); $this->database->createAttribute('alerts', 'projectId', Database::VAR_STRING, 255, true); $this->database->createAttribute('alerts', 'projectInternalId', Database::VAR_ID, 0, true); $this->database->createAttribute('alerts', 'resourceType', Database::VAR_STRING, 64, true); $this->database->createAttribute('alerts', 'resourceId', Database::VAR_STRING, 255, true); $this->database->createAttribute('alerts', 'resourceInternalId', Database::VAR_ID, 0, true); $this->database->createAttribute('alerts', 'parentResourceType', Database::VAR_STRING, 64, true); $this->database->createAttribute('alerts', 'parentResourceId', Database::VAR_STRING, 255, true); $this->database->createAttribute('alerts', 'parentResourceInternalId', Database::VAR_ID, 0, true); $this->database->createAttribute('alerts', 'title', Database::VAR_STRING, 256, true); $this->database->createAttribute('alerts', 'body', Database::VAR_STRING, 16384, true); $this->database->createAttribute('alerts', 'read', Database::VAR_BOOLEAN, 0, false, false); } protected function tearDown(): void { $this->authorization->cleanRoles(); $this->authorization->addRole(Role::any()->toString()); } public function testListReturnsOnlyCurrentUserNotifications(): void { $this->createNotification('user-alert', RESOURCE_TYPE_USERS, 'user-a'); $this->createNotification('team-alert', RESOURCE_TYPE_TEAMS, 'team-a'); $response = new CapturingNotificationsResponse(); (new XList())->action( [], $response, $this->database, new Document(['$id' => 'console']), new Document(['$id' => 'user-a']), ); $notifications = $response->document->getAttribute('notifications'); $this->assertCount(1, $notifications); $this->assertSame('user-alert', $notifications[0]->getId()); $this->assertSame(1, $response->document->getAttribute('total')); $this->assertSame(Response::MODEL_NOTIFICATION_LIST, $response->model); } private function createNotification(string $id, string $resourceType, string $resourceId): Document { return $this->database->createDocument('alerts', new Document([ '$id' => $id, 'messageId' => $id, 'recipientHash' => \substr(\md5($id), 0, 16), 'type' => 'info', 'channel' => NOTIFICATION_TYPE_CONSOLE, 'projectId' => 'project', 'projectInternalId' => '10', 'resourceType' => $resourceType, 'resourceId' => $resourceId, 'resourceInternalId' => '20', 'parentResourceType' => RESOURCE_TYPE_PROJECTS, 'parentResourceId' => 'project', 'parentResourceInternalId' => '10', 'title' => 'Title', 'body' => 'Body', 'read' => false, ])); } }