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

use JsonApi\Routes\Files\FileRefsDelete;
use JsonApi\Schemas\ContentTermsOfUse;
use JsonApi\Schemas\FileRef;
use JsonApi\Errors\RecordNotFoundException;

require_once 'FilesTestHelper.php';

class FileRefsDeleteTest 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 testShouldDeleteFileRef()
    {
        $credentials = $this->tester->getCredentialsForTestDozent();
        $courseId = 'a07535cf2f8a72df33c12ddfa4b53dde';
        $folder = $this->prepareTopFolder($credentials, $courseId);
        $file = $this->createFileInFolder($credentials, $folder, 'file.txt', 'some description');

        $response = $this->sendDeleteFileRef($credentials, $file->getId());
        $this->tester->assertSame($response->getStatusCode(), 204);
    }

    public function testShouldNotDeleteMissingFileRef()
    {
        $credentials = $this->tester->getCredentialsForTestDozent();
        $missingId = 'missing-id';

        $response = $this->sendDeleteFileRef($credentials, $missingId);
        $this->tester->assertSame(404, $response->getStatusCode());
    }

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

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

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