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

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

require_once 'BlubberTestHelper.php';

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

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

    protected $oldUser;

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

    protected function _after()
    {
        // Workaround old-style Stud.IP-API using $GLOBALS['user']
        $GLOBALS['user'] = $this->oldUser;
    }

    // tests
    public function testCreateComment()
    {
        // given
        $credentials = $this->tester->getCredentialsForTestAutor();
        $num = \BlubberComment::countBySQL('1');
        $thread = $this->createPublicBlubberThreadForUser($credentials, 'Who knows Daskylos?');

        $content = 'Augias tried it too.';
        $response = $this->createBlubberCommentJSONAPI($credentials, $thread, $content);
        $this->tester->assertTrue($response->isSuccessfulDocument([201]));

        $document = $response->document();
        $this->tester->assertTrue($document->isSingleResourceDocument());

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

        $resourceObject = $document->primaryResource();
        $this->tester->assertTrue(is_string($resourceObject->id()));
        $this->tester->assertSame(SCHEMA::TYPE, $resourceObject->type());

        $this->tester->assertSame($content, $resourceObject->attribute('content'));
    }

    private function createBlubberCommentJSONAPI(array $credentials, \BlubberThread $thread, $content)
    {
        $body = [
            'data' => [
                'type' => Schema::TYPE,
                'attributes' => [
                    'content' => $content
                ]
            ]
        ];

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

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

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