aboutsummaryrefslogtreecommitdiff
path: root/tests/jsonapi/BlubberThreadsCreateTest.php
blob: d2bdaea1a7eaa64b9beab279d3b0fa5c6885d5c4 (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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php

use JsonApi\Routes\Blubber\ThreadsCreate;
use JsonApi\Schemas\BlubberThread as Schema;
use JsonApi\Schemas\User as UsersSchema;

require_once 'BlubberTestHelper.php';

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

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

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

        // Create global template factory if neccessary
        $has_template_factory = isset($GLOBALS['template_factory']);
        if (!$has_template_factory) {
            $GLOBALS['template_factory'] = new Flexi\Factory($GLOBALS['STUDIP_BASE_PATH'] . '/templates');
        }
    }

    protected function _after()
    {
    }

    // tests
    public function testCreatePrivateThreadSucessfully()
    {
        // given
        $credentials = $this->tester->getCredentialsForTestAutor();

        $response = $this->createThread($credentials, 'private');
        $this->tester->assertTrue($response->isSuccessfulDocument([201]));

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

        $resourceObject = $document->primaryResource();

        $this->tester->assertSame('private', $resourceObject->attribute('context-type'));
        $this->tester->assertSame('', $resourceObject->attribute('content'));

        // check predicates
        $this->tester->assertTrue($resourceObject->attribute('is-commentable'));
        $this->tester->assertTrue($resourceObject->attribute('is-readable'));
        $this->tester->assertTrue($resourceObject->attribute('is-writable'));
        $this->tester->assertTrue($resourceObject->attribute('is-visible-in-stream'));

        // check author relationship
        $authorRel = $resourceObject->relationship('author');
        $this->tester->assertTrue($authorRel->isToOneRelationship());
        $this->tester->assertCount(1, $links = $authorRel->resourceLinks());

        $this->tester->assertSame(UsersSchema::TYPE, $links[0]['type']);
        $this->tester->assertSame($credentials['id'], $links[0]['id']);

        // check mentions relationship
        $mentionsRel = $resourceObject->relationship('mentions');
        $this->tester->assertTrue($mentionsRel->isToManyRelationship());
        $this->tester->assertCount(1, $links = $mentionsRel->resourceLinks());

        $this->tester->assertSame(UsersSchema::TYPE, $links[0]['type']);
        $this->tester->assertSame($credentials['id'], $links[0]['id']);
    }

    public function testFailToCreateAnotherTypeOfThread()
    {
        // given
        $credentials = $this->tester->getCredentialsForTestAutor();

        $response = $this->createThread($credentials, 'course');
        $this->tester->assertSame(400, $response->getStatusCode());

        $response = $this->createThread($credentials, 'institute');
        $this->tester->assertSame(400, $response->getStatusCode());

        $response = $this->createThread($credentials, 'public');
        $this->tester->assertSame(400, $response->getStatusCode());
    }


    private function createThread($credentials, $contextType = 'private')
    {
        $body = [
            'data' => [
                'type' => Schema::TYPE,
                'attributes' => [
                    'context-type' => $contextType
                ]
            ]
        ];

        $app = $this->tester->createApp($credentials, 'post', '/blubber-threads', ThreadsCreate::class);

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

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