aboutsummaryrefslogtreecommitdiff
path: root/tests/jsonapi/BlubberCommentsUpdateTest.php
blob: d9f4a1e705baaf80ea7a3fb3cc3a7aa9e6425983 (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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<?php

use JsonApi\Schemas\BlubberComment as Schema;
use JsonApi\Routes\Blubber\CommentsUpdate;

require_once 'BlubberTestHelper.php';

class BlubberCommentsUpdateTest extends \Codeception\Test\Unit
{
    use BlubberTestHelper;

    /**
     * @var \UnitTester
     */
    protected $tester;

    protected function _before()
    {
        \DBManager::getInstance()->setConnection('studip', $this->getModule('\\Helper\\StudipDb')->dbh);
    }

    protected function _after()
    {
    }

    // tests
    public function testUpdateOwnComment()
    {
        // given
        $credentials = $this->tester->getCredentialsForTestAutor();
        $thread = $this->createPublicBlubberThreadForUser($credentials, 'Who knows Daskylos?');

        $num = \BlubberComment::countBySQL('1');
        $comment = $this->createBlubberComment($credentials, $thread, 'Autolykos knows him.');
        $this->tester->assertEquals($num + 1, \BlubberComment::countBySQL('1'));

        $content = 'Who knows Erginos?';
        $response = $this->updateBlubberCommentJSONAPI($credentials, $comment, $content);
        $this->tester->assertSame(204, $response->getStatusCode());
    }

    public function testUpdateOtherCommentFail()
    {
        // given
        $credentialsAutor = $this->tester->getCredentialsForTestAutor();
        $credentialsDozent = $this->tester->getCredentialsForTestDozent();
        $thread = $this->createPublicBlubberThreadForUser($credentialsDozent, 'Who knows Daskylos?');

        $num = \BlubberComment::countBySQL('1');
        $comment = $this->createBlubberComment($credentialsAutor, $thread, 'Autolykos knows him.');
        $this->tester->assertEquals($num + 1, \BlubberComment::countBySQL('1'));

        $content = 'Who knows Erginos?';
        $response = $this->updateBlubberCommentJSONAPI($credentialsDozent, $comment, $content);
        $this->tester->assertSame(403, $response->getStatusCode());
    }

    public function testUpdateOtherCommentSuccess()
    {
        // given
        $credentialsAutor = $this->tester->getCredentialsForTestAutor();
        $credentialsRoot = $this->tester->getCredentialsForRoot();
        $thread = $this->createPublicBlubberThreadForUser($credentialsRoot, 'Who knows Daskylos?');

        $num = \BlubberComment::countBySQL('1');
        $comment = $this->createBlubberComment($credentialsAutor, $thread, 'Autolykos knows him.');
        $this->tester->assertEquals($num + 1, \BlubberComment::countBySQL('1'));

        $content = 'Who knows Erginos?';
        $response = $this->updateBlubberCommentJSONAPI($credentialsRoot, $comment, $content);
        $this->tester->assertSame(204, $response->getStatusCode());
    }

    private function updateBlubberCommentJSONAPI(array $credentials, \BlubberComment $comment, string $content)
    {
        $body = [
            'data' => [
                'id' => $comment->id,
                'type' => Schema::TYPE,
                'attributes' => [
                    'content' => $content
                ]
            ]
        ];

        $app = $this->tester->createApp($credentials, 'patch', '/blubber-comments/{id}', CommentsUpdate::class);

        $requestBuilder = $this->tester->createRequestBuilder($credentials);
        $requestBuilder
            ->setUri('/blubber-comments/' . $comment->id)
            ->setJsonApiBody($body)
            ->update();

        return $this->tester->sendMockRequest($app, $requestBuilder->getRequest());
    }
}