-
-
Notifications
You must be signed in to change notification settings - Fork 622
Expand file tree
/
Copy pathBrowserTest.php
More file actions
396 lines (338 loc) · 13.6 KB
/
BrowserTest.php
File metadata and controls
396 lines (338 loc) · 13.6 KB
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
<?php
namespace Tests\Feature\Assets;
use Illuminate\Http\UploadedFile;
use PHPUnit\Framework\Attributes\Test;
use Statamic\Facades\AssetContainer;
use Statamic\Facades\User;
use Tests\FakesRoles;
use Tests\PreventSavingStacheItemsToDisk;
use Tests\TestCase;
class BrowserTest extends TestCase
{
use FakesRoles;
use PreventSavingStacheItemsToDisk;
private $tempDir;
public function setUp(): void
{
parent::setUp();
config(['filesystems.disks.test' => [
'driver' => 'local',
'root' => $this->tempDir = __DIR__.'/tmp',
]]);
}
public function tearDown(): void
{
app('files')->deleteDirectory($this->tempDir);
app('files')->deleteDirectory(storage_path('statamic/dimension-cache'));
parent::tearDown();
}
#[Test]
public function it_redirects_to_the_first_container_from_the_index()
{
$this->setTestRoles(['test' => ['access cp', 'view one assets', 'view two assets']]);
$user = User::make()->assignRole('test')->save();
$containerOne = AssetContainer::make('one')->save();
$containerTwo = AssetContainer::make('two')->save();
$this
->actingAs($user)
->get(cp_route('assets.browse.index'))
->assertRedirect($containerOne->showUrl());
}
#[Test]
public function it_redirects_to_the_first_authorized_container_from_the_index()
{
$this->setTestRoles(['test' => ['access cp', 'view two assets']]);
$user = User::make()->assignRole('test')->save();
$containerOne = AssetContainer::make('one')->save();
$containerTwo = AssetContainer::make('two')->save();
$this
->actingAs($user)
->get(cp_route('assets.browse.index'))
->assertRedirect($containerTwo->showUrl());
}
#[Test]
public function no_authorized_containers_results_in_a_403_from_the_index()
{
$this->setTestRoles(['test' => ['access cp']]);
$user = User::make()->assignRole('test')->save();
$containerOne = AssetContainer::make('one')->save();
$containerTwo = AssetContainer::make('two')->save();
$this
->from('/original')
->actingAs($user)
->get(cp_route('assets.browse.index'))
->assertRedirect('/original');
}
#[Test]
public function no_containers_at_all_results_in_a_403_from_the_index()
{
$this->setTestRoles(['test' => ['access cp']]);
$user = User::make()->assignRole('test')->save();
$this
->from('/original')
->actingAs($user)
->get(cp_route('assets.browse.index'))
->assertRedirect('/original');
}
#[Test]
public function no_containers_but_permission_to_create_redirects_to_the_index()
{
$this->setTestRoles(['test' => ['access cp', 'configure asset containers']]);
$user = User::make()->assignRole('test')->save();
$this
->actingAs($user)
->get(cp_route('assets.browse.index'))
->assertRedirect(cp_route('assets.index'));
}
#[Test]
public function it_denies_access()
{
$container = AssetContainer::make('test')->save();
$this
->from('/original')
->actingAs($this->userWithoutPermission())
->get($container->showUrl())
->assertRedirect('/original');
}
#[Test]
public function it_shows_the_page()
{
$container = AssetContainer::make('test')->save();
$this
->actingAs($this->userWithPermission())
->get($container->showUrl())
->assertSuccessful();
}
#[Test]
public function it_lists_assets_in_the_root_folder()
{
$this->withoutExceptionHandling();
$container = AssetContainer::make('test')->disk('test')->save();
$assetOne = $container
->makeAsset('one.txt')
->upload(UploadedFile::fake()->create('one.txt'));
$assetTwo = $container
->makeAsset('two.jpg')
->upload(UploadedFile::fake()->image('two.jpg'));
$assetInOtherFolder = $container
->makeAsset('subdirectory/other.txt')
->upload(UploadedFile::fake()->create('other.txt'));
$this
->actingAs($this->userWithPermission())
->getJson('/cp/assets/browse/folders/test/')
->assertSuccessful()
->assertJsonStructure($this->jsonStructure());
}
#[Test]
public function it_lists_assets_in_a_subfolder()
{
$container = AssetContainer::make('test')->disk('test')->save();
$assetOne = $container
->makeAsset('nested/subdirectory/one.txt')
->upload(UploadedFile::fake()->create('one.txt'));
$assetTwo = $container
->makeAsset('nested/subdirectory/two.jpg')
->upload(UploadedFile::fake()->image('two.jpg'));
$assetInOtherFolder = $container
->makeAsset('other.txt')
->upload(UploadedFile::fake()->create('other.txt'));
$this
->actingAs($this->userWithPermission())
->getJson('/cp/assets/browse/folders/test/nested/subdirectory')
->assertSuccessful()
->assertJsonStructure($this->jsonStructure());
}
#[Test]
public function it_denies_access_to_the_root_folder_without_permission()
{
AssetContainer::make('test')->disk('test')->save();
$this
->actingAs($this->userWithoutPermission())
->getJson('/cp/assets/browse/folders/test')
->assertForbidden();
}
#[Test]
public function it_denies_access_to_a_subfolder_without_permission()
{
AssetContainer::make('test')->disk('test')->save();
$this
->actingAs($this->userWithoutPermission())
->getJson('/cp/assets/browse/folders/test/nested/subdirectory')
->assertForbidden();
}
#[Test]
public function it_404s_when_requesting_a_folder_in_a_container_that_doesnt_exist()
{
$this
->actingAs($this->userWithPermission())
->getJson('/cp/assets/browse/folders/unknown')
->assertNotFound();
}
#[Test]
public function it_searches_for_assets()
{
$containerOne = AssetContainer::make('one')->disk('test')->save();
$containerTwo = AssetContainer::make('two')->disk('test')->save();
$containerOne
->makeAsset('asset-one.txt')
->upload(UploadedFile::fake()->create('asset-one.txt'));
$containerOne
->makeAsset('no-match.txt')
->upload(UploadedFile::fake()->create('no-match.txt'));
$containerOne
->makeAsset('nested/asset-two.txt')
->upload(UploadedFile::fake()->create('asset-two.txt'));
$containerOne
->makeAsset('nested/nope.txt')
->upload(UploadedFile::fake()->create('nope.txt'));
$containerOne
->makeAsset('nested/subdirectory/asset-three.txt')
->upload(UploadedFile::fake()->create('asset-three.txt'));
$containerTwo
->makeAsset('asset-four.txt')
->upload(UploadedFile::fake()->create('asset-four.txt'));
$this
->actingAs($this->userWithPermission())
->getJson('/cp/assets/browse/search/one?search=asset')
->assertSuccessful()
->assertJsonCount(3, 'data')
->assertJsonPath('data.0.id', 'one::asset-one.txt')
->assertJsonPath('data.1.id', 'one::nested/asset-two.txt')
->assertJsonPath('data.2.id', 'one::nested/subdirectory/asset-three.txt');
$this
->actingAs($this->userWithPermission())
->getJson('/cp/assets/browse/search/one/nested?search=asset')
->assertSuccessful()
->assertJsonCount(1, 'data')
->assertJsonPath('data.0.id', 'one::nested/asset-two.txt');
$this
->actingAs($this->userWithPermission())
->getJson('/cp/assets/browse/search/one/nested/subdirectory?search=asset')
->assertSuccessful()
->assertJsonCount(1, 'data')
->assertJsonPath('data.0.id', 'one::nested/subdirectory/asset-three.txt');
}
#[Test]
public function it_filters_assets()
{
$containerOne = AssetContainer::make('one')->disk('test')->save();
$containerTwo = AssetContainer::make('two')->disk('test')->save();
$containerOne
->makeAsset('asset-one.txt')
->upload(UploadedFile::fake()->create('asset-one.txt'));
$containerOne
->makeAsset('asset-two.jpg')
->upload(UploadedFile::fake()->image('asset-two.jpg', 100, 200));
$containerOne
->makeAsset('nested/asset-three.jpg')
->upload(UploadedFile::fake()->image('asset-three.jpg', 200, 100));
$containerTwo
->makeAsset('asset-four.txt')
->upload(UploadedFile::fake()->create('asset-four.txt'));
$containerTwo
->makeAsset('nested/asset-five.jpg')
->upload(UploadedFile::fake()->image('asset-five.jpg', 200, 100))
->set('alt', 'An image')
->save();
$txtFilter = ['asset_properties' => ['extension' => ['operator' => '=', 'value' => 'txt']]];
$imageFilter = ['asset_properties' => ['type' => ['operator' => '=', 'value' => 'image']]];
$widthFilter = ['asset_properties' => ['dimensions' => ['dimension' => 'width', 'operator' => '>', 'value' => 100]]];
$altFilter = ['fields' => ['alt' => ['operator' => 'like', 'value' => 'image']]];
$this
->actingAs($this->userWithPermission())
->getJson('/cp/assets/browse/search/one?filters='.base64_encode(json_encode($txtFilter)))
->assertSuccessful()
->assertJsonCount(1, 'data')
->assertJsonPath('data.0.id', 'one::asset-one.txt');
$this
->actingAs($this->userWithPermission())
->getJson('/cp/assets/browse/search/one?filters='.base64_encode(json_encode($imageFilter)))
->assertSuccessful()
->assertJsonCount(2, 'data')
->assertJsonPath('data.0.id', 'one::asset-two.jpg')
->assertJsonPath('data.1.id', 'one::nested/asset-three.jpg');
$this
->actingAs($this->userWithPermission())
->getJson('/cp/assets/browse/search/one?filters='.base64_encode(json_encode($widthFilter)))
->assertSuccessful()
->assertJsonCount(1, 'data')
->assertJsonPath('data.0.id', 'one::nested/asset-three.jpg');
$this
->actingAs($this->userWithPermission())
->getJson('/cp/assets/browse/search/one/nested?filters='.base64_encode(json_encode($imageFilter)))
->assertSuccessful()
->assertJsonCount(1, 'data')
->assertJsonPath('data.0.id', 'one::nested/asset-three.jpg');
$this
->actingAs($this->userWithPermission())
->getJson('/cp/assets/browse/search/two?filters='.base64_encode(json_encode($altFilter)))
->assertSuccessful()
->assertJsonCount(1, 'data')
->assertJsonPath('data.0.id', 'two::nested/asset-five.jpg');
}
#[Test]
public function it_shows_an_assets_edit_page()
{
$container = AssetContainer::make('test')->disk('test')->save();
$container
->makeAsset('one.txt')
->upload(UploadedFile::fake()->create('one.txt'));
$this->setTestRoles(['test' => ['access cp', 'view test assets']]);
$user = User::make()->assignRole('test')->save();
$this
->actingAs($user)
->getJson('/cp/assets/browse/test/one.txt/edit')
->assertSuccessful()
->assertInertia(fn ($page) => $page->component('assets/Browse'));
}
#[Test]
public function it_404s_when_the_asset_doesnt_exist()
{
$container = AssetContainer::make('test')->disk('test')->save();
$this->setTestRoles(['test' => ['access cp', 'edit test assets']]);
$user = User::make()->assignRole('test')->save();
$this
->actingAs($user)
->getJson('/cp/assets/browse/test/unknown.txt/edit')
->assertNotFound();
}
#[Test]
public function it_denies_access_without_permission_to_view_asset()
{
$container = AssetContainer::make('test')->disk('test')->save();
$container
->makeAsset('one.txt')
->upload(UploadedFile::fake()->create('one.txt'));
$this->setTestRoles(['test' => ['access cp']]);
$user = User::make()->assignRole('test')->save();
$this
->actingAs($user)
->getJson('/cp/assets/browse/test/one.txt/edit')
->assertForbidden();
}
private function userWithPermission()
{
$this->setTestRoles(['test' => ['access cp', 'view test assets', 'view one assets', 'view two assets']]);
return User::make()->assignRole('test')->save();
}
private function userWithoutPermission()
{
$this->setTestRoles(['test' => ['access cp']]);
return User::make()->assignRole('test')->save();
}
private function jsonStructure()
{
return [
'links' => ['folder_action', 'asset_action'],
'data' => [
['id', 'size_formatted', 'last_modified_relative'],
['id', 'size_formatted', 'last_modified_relative', 'thumbnail'],
],
'meta' => [
'folder' => [
'title', 'path', 'parent_path', 'actions', 'folders',
],
],
];
}
}