+ 'name' => $imgDetails['name'],
+ ]);
+ }
+
+ public function test_image_edit()
+ {
+ $editor = $this->getEditor();
+ $this->actingAs($editor);
+
+ $imgDetails = $this->uploadGalleryImage();
+ $image = Image::query()->first();
+
+ $newName = str_random();
+ $update = $this->put('/images/' . $image->id, ['name' => $newName]);
+ $update->assertSuccessful();
+ $update->assertJson([
+ 'id' => $image->id,
+ 'name' => $newName,
+ 'type' => 'gallery',
+ ]);
+
+ $this->deleteImage($imgDetails['path']);
+
+ $this->assertDatabaseHas('images', [
+ 'type' => 'gallery',
+ 'name' => $newName
+ ]);
+ }
+
+ public function test_gallery_get_list_format()
+ {
+ $this->asEditor();
+
+ $imgDetails = $this->uploadGalleryImage();
+ $image = Image::query()->first();
+
+ $emptyJson = ['images' => [], 'has_more' => false];
+ $resultJson = [
+ 'images' => [
+ [
+ 'id' => $image->id,
+ 'name' => $imgDetails['name'],
+ ]
+ ],
+ 'has_more' => false,
+ ];
+
+ $pageId = $imgDetails['page']->id;
+ $firstPageRequest = $this->get("/images/gallery?page=1&uploaded_to={$pageId}");
+ $firstPageRequest->assertSuccessful()->assertJson($resultJson);
+
+ $secondPageRequest = $this->get("/images/gallery?page=2&uploaded_to={$pageId}");
+ $secondPageRequest->assertSuccessful()->assertExactJson($emptyJson);
+
+ $namePartial = substr($imgDetails['name'], 0, 3);
+ $searchHitRequest = $this->get("/images/gallery?page=1&uploaded_to={$pageId}&search={$namePartial}");
+ $searchHitRequest->assertSuccessful()->assertJson($resultJson);
+
+ $namePartial = str_random(16);
+ $searchHitRequest = $this->get("/images/gallery?page=1&uploaded_to={$pageId}&search={$namePartial}");
+ $searchHitRequest->assertSuccessful()->assertExactJson($emptyJson);
+ }
+
+ public function test_image_usage()
+ {
+ $page = Page::first();
+ $editor = $this->getEditor();
+ $this->actingAs($editor);
+
+ $imgDetails = $this->uploadGalleryImage($page);
+
+ $image = Image::query()->first();
+ $page->html = '<img src="'.$image->url.'">';
+ $page->save();
+
+ $usage = $this->get('/images/usage/' . $image->id);
+ $usage->assertSuccessful();
+ $usage->assertJson([
+ [
+ 'id' => $page->id,
+ 'name' => $page->name
+ ]
+ ]);
+
+ $this->deleteImage($imgDetails['path']);
+ }
+
+ public function test_php_files_cannot_be_uploaded()
+ {
+ $page = Page::first();
+ $admin = $this->getAdmin();
+ $this->actingAs($admin);
+
+ $fileName = 'bad.php';
+ $relPath = $this->getTestImagePath('gallery', $fileName);
+ $this->deleteImage($relPath);
+
+ $file = $this->getTestImage($fileName);
+ $upload = $this->withHeader('Content-Type', 'image/jpeg')->call('POST', '/images/gallery', ['uploaded_to' => $page->id], [], ['file' => $file], []);
+ $upload->assertStatus(302);
+
+ $this->assertFalse(file_exists(public_path($relPath)), 'Uploaded php file was uploaded but should have been stopped');
+
+ $this->assertDatabaseMissing('images', [
+ 'type' => 'gallery',
+ 'name' => $fileName