aboutsummaryrefslogtreecommitdiff
path: root/tests/jsonapi/MessagesUpdateTest.php
diff options
context:
space:
mode:
authorJan-Hendrik Willms <tleilax+github@gmail.com>2021-07-22 16:07:19 +0200
committerJan-Hendrik Willms <tleilax+github@gmail.com>2021-07-22 16:19:12 +0200
commita3da1483a9e689846179159355badfec8073dbec (patch)
tree770dcca6bdf5f6f2a11b0e7fcbbeda6919a3fc52 /tests/jsonapi/MessagesUpdateTest.php
current code from svn, revision 62608
Diffstat (limited to 'tests/jsonapi/MessagesUpdateTest.php')
-rw-r--r--tests/jsonapi/MessagesUpdateTest.php77
1 files changed, 77 insertions, 0 deletions
diff --git a/tests/jsonapi/MessagesUpdateTest.php b/tests/jsonapi/MessagesUpdateTest.php
new file mode 100644
index 0000000..6af9b18
--- /dev/null
+++ b/tests/jsonapi/MessagesUpdateTest.php
@@ -0,0 +1,77 @@
+<?php
+
+use JsonApi\Routes\Messages\MessageShow;
+use JsonApi\Routes\Messages\MessageUpdate;
+
+class MessagesUpdateTest extends \Codeception\Test\Unit
+{
+ /**
+ * @var \UnitTester
+ */
+ protected $tester;
+
+ protected function _before()
+ {
+ \DBManager::getInstance()->setConnection('studip', $this->getModule('\\Helper\\StudipDb')->dbh);
+ }
+
+ protected function _after()
+ {
+ }
+
+ // tests
+ public function testMarkMessageAsRead()
+ {
+ $credentials = $this->tester->getCredentialsForTestAutor();
+ $recipients = [$this->tester->getCredentialsForTestDozent()['username']];
+ $message = \Message::send($credentials['id'], $recipients, 'empty subject', 'empty message');
+
+ $response = $this->fetchMessage($credentials, $message);
+
+ $this->tester->assertTrue($response->isSuccessfulDocument([200]));
+ $document = $response->document();
+ $this->tester->assertTrue($document->isSingleResourceDocument());
+ $resourceObject = $document->primaryResource();
+ $this->tester->assertSame($message->subject, $resourceObject->attribute('subject'));
+ $this->tester->assertFalse($resourceObject->attribute('is-read'));
+
+ $response2 = $this->markMessageAsRead($credentials, $message);
+ $this->tester->assertTrue($response2->isSuccessfulDocument([200]));
+ $this->tester->assertTrue($response2->document()->primaryResource()->attribute('is-read'));
+ }
+
+ // helpers
+ private function fetchMessage(array $credentials, \Message $message)
+ {
+ return $this->tester->sendMockRequest(
+ $this->tester->createApp($credentials, 'get', '/messages/{id}', MessageShow::class),
+ $this->tester
+ ->createRequestBuilder($credentials)
+ ->setUri('/messages/'.$message->id)
+ ->fetch()
+ ->getRequest()
+ );
+ }
+
+ private function markMessageAsRead(array $credentials, \Message $message)
+ {
+ $json = [
+ 'data' => [
+ 'type' => 'messages',
+ 'id' => $message->id,
+ 'attributes' => [
+ 'is-read' => true,
+ ],
+ ],
+ ];
+
+ return $this->tester->sendMockRequest(
+ $this->tester->createApp($credentials, 'patch', '/messages/{id}', MessageUpdate::class),
+ $this->tester->createRequestBuilder($credentials)
+ ->setUri('/messages/'.$message->id)
+ ->setJsonApiBody($json)
+ ->update()
+ ->getRequest()
+ );
+ }
+}