aboutsummaryrefslogtreecommitdiff
path: root/tests/jsonapi/FilesTestHelper.php
blob: 3a7650e59344d6d3dbec02f93f05f5b62e8d8358 (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
106
107
108
109
110
111
112
113
114
115
116
117
<?php

use JsonApi\Schemas\ContentTermsOfUse;
use JsonApi\Schemas\FileRef;
use JsonApi\Schemas\File as FileSchema;

trait FilesTestHelper
{
    /**
     * @SuppressWarnings(PHPMD.Superglobals)
     */
    protected function prepareTopFolder($credentials, $courseId)
    {
        $course = \Course::find($courseId);
        $this->assertNotNull($course);

        $oldUser = $GLOBALS['user'] ?? null;
        $GLOBALS['user'] = new \Seminar_User($credentials['id']);

        $rootFolder = Folder::createTopFolder($course->id, 'course');
        $this->assertNotNull($rootFolder);

        $GLOBALS['user'] = $oldUser;

        return $rootFolder;
    }

    protected function getSampleLicense()
    {
        $this->assertTrue(\ContentTermsOfUse::countBySql('1') > 0);

        return \ContentTermsOfUse::findOneBySql('1');
    }

    protected function createLicense($name = "Another License")
    {
        return \ContentTermsOfUse::create(
            [
                'name' => $name
            ]
        );
    }

    protected function prepareValidFileRefBody($name, $description, $license, \FileType $filetype = null)
    {
        $json = [
            'data' => [
                'type' => FileRef::TYPE,
                'attributes' => [
                    'name' => $name,
                    'description' => $description,
                ],
                'relationships' =>
                $filetype
                ? ['file' => [
                        'data' => [
                            'type' => FileSchema::TYPE,
                            'id' => $filetype->getFileRef()['file_id']
                        ]
                    ]
                ]
                : [],
            ],
        ];

        if ($license) {
            $json['data']['relationships']['terms-of-use'] = [
                'data' => [
                    'type' => ContentTermsOfUse::TYPE,
                    'id' => (string) $license->id,
                ],
            ];
        }

        return $json;
    }

    /**
     * @SuppressWarnings(PHPMD.Superglobals)
     */
    protected function getTmpPath()
    {
        return $GLOBALS['TMP_PATH'];
    }

    protected function getTmpFile()
    {
        $filename = tempnam($this->getTmpPath(), 'jsonapi');

        $handle = fopen($filename, "w");
        fwrite($handle, "writing to tempfile");
        fclose($handle);

        return $filename;
    }

    protected function createFileInFolder($credentials, $folder, $name, $description, $license = null)
    {
        $numFiles = \File::countBySQL('1');
        $numFileRefs = \FileRef::countBySQL('1');
        $file = \StandardFile::create(
            [
                'name' => $name,
                'description' => $description,
                'size' => 0,
                'tmp_name' => $this->getTmpFile(),
                'content_terms_of_use_id' => $license
            ],
            $credentials['id']
        );
        $file = $file->addToFolder($folder->getTypedFolder(), $name, $credentials['id']);
        $this->assertSame($numFiles + 1, \File::countBySQL('1'));
        $this->assertSame($numFileRefs + 1, \FileRef::countBySQL('1'));

        return $file;
    }
}