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

use Courseware\Instance;
use Courseware\StructuralElement;
use JsonApi\Routes\Courseware\StructuralElementsShow;
use JsonApi\Schemas\Courseware\StructuralElement as Schema;
use WoohooLabs\Yang\JsonApi\Response\JsonApiResponse;

class StructuralElementsShowTest extends \Codeception\Test\Unit
{
    /**
     * @var \JsonapiTester
     */
    protected $tester;

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

    protected function _after()
    {
    }

    // tests
    public function testShouldShowStructuralElement()
    {
        $credentials = $this->tester->getCredentialsForTestAutor();
        $structuralElement = $this->createCourseware($credentials, 5);

        $response = $this->fetchStructuralElement($credentials, $structuralElement);
        $this->assertTrue($response->isSuccessfulDocument([200]));

        $document = $response->document();
        $this->assertEquals($structuralElement->id, $document->primaryResource()->id());
        $this->assertFalse($document->hasAnyIncludedResources());
    }

    public function testShouldIncludeChildren()
    {
        $credentials = $this->tester->getCredentialsForTestAutor();
        $structuralElement = $this->createCourseware($credentials, 5);

        $response = $this->fetchStructuralElement($credentials, $structuralElement, ['include' => 'children']);
        $this->assertTrue($response->isSuccessfulDocument([200]));

        $document = $response->document();
        $this->assertEquals($structuralElement->id, $document->primaryResource()->id());
        $this->assertTrue($document->hasAnyIncludedResources());

        $includedResources = $document->includedResources();
        $childIDs = $structuralElement->children->pluck('id');
        $this->assertCount(count($childIDs), $includedResources);
        foreach ($includedResources as $included) {
            $this->assertContainsEquals($included->id(), $childIDs);
        }
    }

    // **** helper functions ****
    private function createCourseware(iterable $credentials, int $numberOfChildren): StructuralElement
    {
        $instance = StructuralElement::createEmptyCourseware($credentials['id'], 'user');
        $root = $instance->getRoot();

        for ($i = 0; $i < $numberOfChildren; $i++) {
            $child = StructuralElement::build([
                'range_id' => $root['range_id'],
                'range_type' => $root['range_type'],
                'owner_id' => $root['owner_id'],
                'editor_id' => $root['editor_id'],
                'title' => _('neue Seite'),
            ]);

            $root->children[] = $child;
        }

        $root->store();

        return $root;
    }

    private function fetchStructuralElement(
        iterable $credentials,
        StructuralElement $resource,
        iterable $options = []
    ): JsonApiResponse {
        $app = $this->tester->createApp(
            $credentials,
            'get',
            '/courseware-structural-elements/{id}',
            StructuralElementsShow::class
        );

        $requestBuilder = $this->tester
            ->createRequestBuilder($credentials)
            ->setUri('/courseware-structural-elements/' . $resource->id)
            ->fetch();

        if (array_key_exists('include', $options)) {
            $requestBuilder->setJsonApiIncludes($options['include']);
        }

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