aboutsummaryrefslogtreecommitdiff
path: root/tests/jsonapi/MessagesUpdateTest.php
blob: 6af9b18e564f8469fc2d04660551f6643d9211db (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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()
        );
    }
}