]> BookStack Code Mirror - bookstack/blob - tests/Helpers/FileProvider.php
Merge branch 'fix/oidc-logout' into development
[bookstack] / tests / Helpers / FileProvider.php
1 <?php
2
3 namespace Tests\Helpers;
4
5 use BookStack\Entities\Models\Page;
6 use BookStack\Uploads\Attachment;
7 use BookStack\Uploads\AttachmentService;
8 use Illuminate\Http\UploadedFile;
9 use Illuminate\Testing\TestResponse;
10 use stdClass;
11 use Tests\TestCase;
12
13 class FileProvider
14 {
15     /**
16      * Get the path to a file in the test-data-directory.
17      */
18     public function testFilePath(string $fileName): string
19     {
20         return base_path('tests/test-data/' . $fileName);
21     }
22
23     /**
24      * Creates a new temporary image file using the given name,
25      * with the content decoded from the given bas64 file name.
26      * Is generally used for testing sketchy files that could trip AV.
27      */
28     public function imageFromBase64File(string $base64FileName, string $imageFileName): UploadedFile
29     {
30         $imagePath = implode(DIRECTORY_SEPARATOR, [sys_get_temp_dir(), $imageFileName]);
31         $base64FilePath = $this->testFilePath($base64FileName);
32         $data = file_get_contents($base64FilePath);
33         $decoded = base64_decode($data);
34         file_put_contents($imagePath, $decoded);
35
36         return new UploadedFile($imagePath, $imageFileName, 'image/png', null, true);
37     }
38
39     /**
40      * Get a test image UploadedFile instance, that can be uploaded via test requests.
41      */
42     public function uploadedImage(string $fileName, string $testDataFileName = ''): UploadedFile
43     {
44         return new UploadedFile($this->testFilePath($testDataFileName ?: 'test-image.png'), $fileName, 'image/png', null, true);
45     }
46
47     /**
48      * Get a test txt UploadedFile instance, that can be uploaded via test requests.
49      */
50     public function uploadedTextFile(string $fileName): UploadedFile
51     {
52         return new UploadedFile($this->testFilePath('test-file.txt'), $fileName, 'text/plain', null, true);
53     }
54
55     /**
56      * Get raw data for a PNG image test file.
57      */
58     public function pngImageData(): string
59     {
60         return file_get_contents($this->testFilePath('test-image.png'));
61     }
62
63     /**
64      * Get the expected relative path for an uploaded image of the given type and filename.
65      */
66     public function expectedImagePath(string $imageType, string $fileName): string
67     {
68         return '/uploads/images/' . $imageType . '/' . date('Y-m') . '/' . $fileName;
69     }
70
71     /**
72      * Performs an image gallery upload request with the given name.
73      */
74     public function uploadGalleryImage(TestCase $case, string $name, int $uploadedTo = 0, string $contentType = 'image/png', string $testDataFileName = ''): TestResponse
75     {
76         $file = $this->uploadedImage($name, $testDataFileName);
77
78         return $case->call('POST', '/images/gallery', ['uploaded_to' => $uploadedTo], [], ['file' => $file], ['CONTENT_TYPE' => $contentType]);
79     }
80
81     /**
82      * Upload a new gallery image and return a set of details about the image,
83      * including the json decoded response of the upload.
84      * Ensures the upload succeeds.
85      *
86      * @return array{name: string, path: string, page: Page, response: stdClass}
87      */
88     public function uploadGalleryImageToPage(TestCase $case, Page $page, string $testDataFileName = ''): array
89     {
90         $imageName = $testDataFileName ?: 'first-image.png';
91         $relPath = $this->expectedImagePath('gallery', $imageName);
92         $this->deleteAtRelativePath($relPath);
93
94         $upload = $this->uploadGalleryImage($case, $imageName, $page->id, 'image/png', $testDataFileName);
95         $upload->assertStatus(200);
96
97         return [
98             'name' => $imageName,
99             'path' => $relPath,
100             'page' => $page,
101             'response' => json_decode($upload->getContent()),
102         ];
103     }
104
105     /**
106      * Uploads an attachment file with the given name.
107      */
108     public function uploadAttachmentFile(TestCase $case, string $name, int $uploadedTo = 0): TestResponse
109     {
110         $file = $this->uploadedTextFile($name);
111
112         return $case->call('POST', '/attachments/upload', ['uploaded_to' => $uploadedTo], [], ['file' => $file], []);
113     }
114
115     /**
116      * Upload a new attachment from the given raw data of the given type, to the given page.
117      * Returns the attachment
118      */
119     public function uploadAttachmentDataToPage(TestCase $case, Page $page, string $filename, string $content, string $mimeType): Attachment
120     {
121         $file = tmpfile();
122         $filePath = stream_get_meta_data($file)['uri'];
123         file_put_contents($filePath, $content);
124         $upload = new UploadedFile($filePath, $filename, $mimeType, null, true);
125
126         $case->call('POST', '/attachments/upload', ['uploaded_to' => $page->id], [], ['file' => $upload], []);
127
128         return $page->attachments()->where('uploaded_to', '=', $page->id)->latest()->firstOrFail();
129     }
130
131     /**
132      * Delete an uploaded image.
133      */
134     public function deleteAtRelativePath(string $path): void
135     {
136         $fullPath = $this->relativeToFullPath($path);
137         if (file_exists($fullPath)) {
138             unlink($fullPath);
139         }
140     }
141
142     /**
143      * Convert a relative path used by default in this provider to a full
144      * absolute local filesystem path.
145      */
146     public function relativeToFullPath(string $path): string
147     {
148         return public_path($path);
149     }
150
151     /**
152      * Delete all uploaded files.
153      * To assist with cleanup.
154      */
155     public function deleteAllAttachmentFiles(): void
156     {
157         $fileService = app()->make(AttachmentService::class);
158         foreach (Attachment::all() as $file) {
159             $fileService->deleteFile($file);
160         }
161     }
162
163     /**
164      * Reset the application favicon image in the public path.
165      */
166     public function resetAppFavicon(): void
167     {
168         file_put_contents(public_path('favicon.ico'), file_get_contents(public_path('icon.ico')));
169     }
170 }