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

use JsonApi\Errors\RecordNotFoundException;
use JsonApi\Errors\UnprocessableEntityException;
use JsonApi\Routes\Files\FileRefsShow;
use JsonApi\Schemas\ContentTermsOfUse;
use JsonApi\Schemas\FileRef;

require_once 'FilesTestHelper.php';

class FileRefsShowTest extends \Codeception\Test\Unit
{
    use FilesTestHelper;

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

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

    protected function _after()
    {
    }

    public function testShouldShowFile()
    {
        $credentials = $this->tester->getCredentialsForTestDozent();
        $courseId = 'a07535cf2f8a72df33c12ddfa4b53dde';
        $folder = $this->prepareTopFolder($credentials, $courseId);
        $license = $this->getSampleLicense();
        $name = 'filename.jpg';
        $description = 'a description';

        $file = $this->createFileInFolder($credentials, $folder, $name, $description, $license->id);

        $response = $this->sendShowFileRef($credentials, $file->getId());
        $this->assertFileRefCreated($response, $name, $description, $license);
    }

    public function testShouldFailIfFileIsMissing()
    {
        $credentials = $this->tester->getCredentialsForTestDozent();

        $response = $this->sendShowFileRef($credentials, 'missing-id');
        $this->tester->assertSame(404, $response->getStatusCode());
    }

    // **** helper functions ****
    private function sendShowFileRef($user, $fileId)
    {
        $app = $this->tester->createApp(
            $user,
            'GET',
            '/file-refs/{id}',
            FileRefsShow::class
        );

        $requestBuilder = $this->tester->createRequestBuilder($user);
        $requestBuilder
            ->setUri('/file-refs/'.($fileId))
            ->fetch();

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

    private function assertFileRefCreated($response, $name, $description, $license)
    {
        $this->tester->assertTrue($response->isSuccessfulDocument([200]));

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

        $resource = $document->primaryResource();
        $this->tester->assertNotEmpty($resource->id());
        $this->tester->assertSame(FileRef::TYPE, $resource->type());

        $this->tester->assertSame($name, $resource->attribute('name'));
        $this->tester->assertSame($description, $resource->attribute('description'));

        $this->tester->assertTrue($resource->attribute('is-readable'));
        $this->tester->assertTrue($resource->attribute('is-downloadable'));
        $this->tester->assertTrue($resource->attribute('is-editable'));
        $this->tester->assertTrue($resource->attribute('is-writable'));

        $resourceLink = $resource->relationship('terms-of-use')->firstResourceLink();
        $this->tester->assertSame($license->id, $resourceLink['id']);
    }
}