forked from advoor/nova-editor-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEditorJsImageUploadControllerTest.php
207 lines (174 loc) · 7.03 KB
/
EditorJsImageUploadControllerTest.php
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<?php
declare(strict_types=1);
namespace Tests\Feature\Http\Controllers;
use Advoor\NovaEditorJs\Events\EditorJsImageUploaded;
use Advoor\NovaEditorJs\Events\EditorJsThumbnailCreated;
use Advoor\NovaEditorJs\Http\Controllers\EditorJsImageUploadController;
use finfo;
use Illuminate\Http\Response;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use Tests\TestCase;
class EditorJsImageUploadControllerTest extends TestCase
{
/**
* @before
*/
public function registerTestRoutes(): void
{
$this->afterApplicationCreated(function () {
Route::post('/test/image/file', [EditorJsImageUploadController::class, 'file']);
Route::post('/test/image/url', [EditorJsImageUploadController::class, 'url']);
});
}
/**
* Test an image upload.
*
* @param string $path Path to the image file
*
* @dataProvider provideValidFilesForImageUpload
*/
public function testImageUpload(string $path): void
{
Storage::fake();
Storage::fake('public');
$fake = Event::fake();
DB::setEventDispatcher($fake);
$uploadedFile = UploadedFile::fake()->create('file', 1024, (new finfo)->file($path, FILEINFO_MIME_TYPE));
if ($fp = $uploadedFile->openFile('w')) {
$fp->fwrite(file_get_contents($path));
}
$response = $this->post('/test/image/file', [
'image' => $uploadedFile,
])->assertOk()->assertJson(['success' => 1]);
$responseUrl = $response->json('file.url');
$this->assertNotEmpty($responseUrl, 'Response file URL is empty');
$storageBaseUrl = Storage::disk('public')->url('');
$this->assertStringStartsWith($storageBaseUrl, $responseUrl, 'Response URL seems to not be in a public folder');
$createdFiles = Storage::disk()->allFiles();
$this->assertCount(2, $createdFiles, 'Storage seems to not contain exactly two files (one upload, one saved)');
$filesThatLookLikeTheUpload = array_filter(
$createdFiles,
fn ($file) => Str::endsWith($file, basename($responseUrl)),
);
Event::assertDispatched(EditorJsImageUploaded::class);
Event::assertDispatched(EditorJsThumbnailCreated::class);
$this->assertCount(1, $filesThatLookLikeTheUpload, 'Storage doesn\'t seem to contain a file with the same name as the returned URL');
}
/**
* Test uploading a non-image.
*/
public function testNonImageUpload(): void
{
Storage::fake();
Storage::fake('public');
$uploadedFile = UploadedFile::fake()->createWithContent('upload', 'Hello World!');
$response = $this->post('/test/image/file', [
'image' => $uploadedFile,
])->assertOk()->assertJson(['success' => 0]);
}
/**
* Test submitting an image URL causes the file to be stored to disk and returned.
*
* @param string $file path to the file returned by the URL
*
* @dataProvider provideValidFiles
*/
public function testValidImageUrlSubmission(string $file): void
{
Storage::fake();
Storage::fake('public');
$fake = Event::fake();
DB::setEventDispatcher($fake);
Http::fake([
'https://example.com/image.bin' => Http::response(file_get_contents($file)),
])->preventStrayRequests();
$response = $this->post('/test/image/url', [
'url' => 'https://example.com/image.bin',
])->assertOk()->assertJson(['success' => 1]);
$responseUrl = $response->json('file.url');
$this->assertNotEmpty($responseUrl, 'Response file URL is empty');
$storageBaseUrl = Storage::disk('public')->url('');
$this->assertStringStartsWith($storageBaseUrl, $responseUrl, 'Response URL seems to not be in a public folder');
$createdFiles = Storage::disk()->allFiles();
$this->assertCount(1, $createdFiles, 'Storage seems to not contain exactly one file');
Event::assertDispatched(EditorJsImageUploaded::class);
$this->assertEquals(basename($createdFiles[0]), basename($responseUrl), 'Response URL filename doesn\'t match created file basename');
}
/**
* Test submitting a non-image URL causes the request to fail.
*/
public function testInvalidImageUrlSubmission(): void
{
Http::fake([
'https://example.com/image.bin' => Http::response('Hello World!'),
])->preventStrayRequests();
$this->post('/test/image/url', [
'url' => 'https://example.com/image.bin',
])->assertOk()->assertJson(['success' => 0]);
}
/**
* Test submitting a URL that's not valid, but is a properly formed HTTP
* URL, still sends out a ping (but fails, eventually).
*/
public function testSubmittingADeadUrl(): void
{
Http::fake([
'https://example.invalid/image.bin' => Http::response('Hello World!'),
])->preventStrayRequests();
$this->post('/test/image/url', [
'url' => 'https://example.invalid/image.bin',
])->assertOk()->assertJson(['success' => 0]);
Http::assertSentCount(1);
}
/**
* Test submitting a URL which the server won't or cannot provide returns an error.
* Also implicitly handles timeouts, since that's the same block.
*/
public function testSubmittingImageUrlWithErrors(): void
{
Http::fake([
'https://example.com/client/image.bin' => Http::response(test_resource('responses/image.png'), Response::HTTP_BAD_GATEWAY),
'https://example.com/server/image.bin' => Http::response(test_resource('responses/image.png'), Response::HTTP_GONE),
])->preventStrayRequests();
$this->post('/test/image/url', [
'url' => 'https://example.com/client/image.bin',
])->assertOk()->assertJson(['success' => 0]);
$this->post('/test/image/url', [
'url' => 'https://example.com/server/image.bin',
])->assertOk()->assertJson(['success' => 0]);
Http::assertSentCount(2);
}
/**
* Provides a list of valid image files to test.
*
* @return string[][]
*/
public static function provideValidFiles(): array
{
return [
'gif' => [test_resource('responses/image.gif')],
'jpg' => [test_resource('responses/image.jpg')],
'png' => [test_resource('responses/image.png')],
'svg' => [test_resource('responses/image.svg')],
'svg' => [test_resource('responses/image.svg')],
];
}
/**
* Provides a subset of the available image formats, since svg isn't supported by the GD library.
*
* @return string[][]
*/
public static function provideValidFilesForImageUpload(): array
{
return Arr::except(self::provideValidFiles(), [
'svg',
]);
}
}