aboutsummaryrefslogtreecommitdiff
path: root/tests/jsonapi/BlubberCommentsDeleteTest.php
blob: cbe4b7b07ab31ef86d10f9b38f4a3be674966cd2 (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
<?php

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

require_once 'BlubberTestHelper.php';

class BlubberCommentsDeleteTest 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 testCreateComment()
    {
        // given
        $credentials1 = $this->tester->getCredentialsForTestAutor();
        $credentials2 = $this->tester->getCredentialsForRoot();
        $thread = $this->createPublicBlubberThreadForUser($credentials2, 'Who knows Daskylos?');

        // Workaround old-style Stud.IP-API using $GLOBALS['user']
        $oldUser = $GLOBALS['user'] ?? null;
        $GLOBALS['user'] = new \Seminar_User(\User::find($credentials1['id']));

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

        // Workaround old-style Stud.IP-API using $GLOBALS['user']
        $GLOBALS['user'] = $oldUser;

        $response = $this->deleteBlubberCommentJSONAPI($credentials2, $comment);
        $this->tester->assertSame(204, $response->getStatusCode());

        $this->tester->assertEquals($num, \BlubberComment::countBySQL('1'));
    }

    private function deleteBlubberCommentJSONAPI(array $credentials, \BlubberComment $comment)
    {
        $app = $this->tester->createApp($credentials, 'delete', '/blubber-comments/{id}', CommentsDelete::class);

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

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