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

use JsonApi\Routes\Wiki\WikiUpdate;

class WikiUpdateTest extends \Codeception\Test\Unit
{
    /**
     * @var \UnitTester
     */
    protected $tester;

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

    protected function _after()
    {
    }

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

        $keyword = 'KaineusKalais';
        $content = 'This is just fake wiki.';
        $createdpage = $this->createWikiPage($rangeId, $keyword, $content);

        $newContent = 'Es gibt im Moment in diese Mannschaft, oh, einige Spieler vergessenihren Profi was sie sind. Ich lese nicht sehr viele Zeitungen, aberich habe gehört viele Situationen. Erstens: Wir haben nicht offensivgespielt. Es gibt keine deutsche Mannschaft spielt offensiv und dieNamen offensiv wie Bayern. Letzte Spiel hatten wir in Platz dreiSpitzen: Elber, Jancker und dann Zickler. Wir mussen nicht vergessenZickler. Zickler ist eine Spitzen mehr, Mehmet mehr Basler. Ist klardiese Wörter, ist möglich verstehen, was ich hab’ gesagt? Danke.';

        $response = $this->updateWiki($credentials, $rangeId, $createdpage->id, $newContent);
        $this->tester->assertSame(200, $response->getStatusCode());
        $page = $response->document()->primaryResource();

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

    //helpers:
    private function updateWiki($credentials, $rangeId, $page_id, $content)
    {
        $json = [
            'data' => [
                'type' => 'wiki',
                'id' => $page_id,
                'attributes' => compact('content')
            ],
        ];
        $app = $this->tester->createApp($credentials, 'patch', '/wiki-pages/{id}', WikiUpdate::class);
        $app->get('/wiki-pages/{id}', function () {})->setName('get-wiki-page');

        return $this->tester->sendMockRequest(
                $app,
                $this->tester->createRequestBuilder($credentials)
                ->setUri('/wiki-pages/'.$page_id)
                ->setJsonApiBody($json)
                ->update()
                ->getRequest()
        );
    }

    private function createWikiPage($rangeId, $keyword, $body)
    {
        $credentials = $this->tester->getCredentialsForTestDozent();
        // EVIL HACK
        $oldPerm = $GLOBALS['perm'] ?? null;
        $oldUser = $GLOBALS['user'] ?? null;
        $GLOBALS['perm'] = new \Seminar_Perm();
        $GLOBALS['user'] = new \Seminar_User(\User::find($credentials['id']));

        $wikiPage = new \WikiPage();
        $wikiPage->range_id = $rangeId;
        $wikiPage->name = $keyword;
        $wikiPage->content = $body;
        $wikiPage->user_id = 'nobody';
        $wikiPage->store();

        $GLOBALS['perm'] = $oldPerm;
        $GLOBALS['user'] = $oldUser;

        return $wikiPage;
    }
}