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

require_once 'WikiTestHelper.php';
use JsonApi\Routes\Wiki\WikiCreate;

class WikiCreateTest extends \Codeception\Test\Unit
{
    use WikiTestHelper;

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

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

    protected function _after()
    {
    }

    //testszenario:
    public function testWikiCreate()
    {
        $credentials = $this->tester->getCredentialsForTestAutor();
        $rangeId = 'a07535cf2f8a72df33c12ddfa4b53dde';

        $keyword = 'IphiklosIphitos';
        $content = 'This is just fake wiki.';

        $json = [
            'data' => [
                'type' => 'wiki',
                'attributes' => compact('keyword', 'content'),
            ],
        ];

        $this->tester->assertCount(0, \WikiPage::findLatestPages($rangeId));

        $response = $this->createWikiPage($credentials, $rangeId, $json);
        $this->tester->assertSame(201, $response->getStatusCode());

        $this->tester->assertCount(1, \WikiPage::findLatestPages($rangeId));

        $page = $response->document()->primaryResource();

        $this->tester->assertEquals($content, $page->attribute('content'));
    }

    //helpers:
    private function createWikiPage($credentials, $rangeId, $json)
    {
        $app = $this->tester->createApp(
            $credentials,
            'post',
            '/courses/{id}/wiki',
            WikiCreate::class
        );
        $this->addNamedGetWikiPageRoute($app);

        return $this->tester->sendMockRequest(
            $app,
            $this->tester->createRequestBuilder($credentials)
            ->setUri('/courses/'.$rangeId.'/wiki')
            ->setJsonApiBody($json)
            ->create()
            ->getRequest()
        );
    }
}