]> BookStack Code Mirror - bookstack/blobdiff - tests/Uploads/ImageTest.php
Testing: Added entity decode flag and phpunit env option
[bookstack] / tests / Uploads / ImageTest.php
index fb98565fc3d8a0774167703b097b5f26d2973f67..a9684eef72a9e52a55897571227ddf680f11daae 100644 (file)
@@ -92,6 +92,52 @@ class ImageTest extends TestCase
         ]);
     }
 
+    public function test_image_file_update()
+    {
+        $page = $this->entities->page();
+        $this->asEditor();
+
+        $imgDetails = $this->files->uploadGalleryImageToPage($this, $page);
+        $relPath = $imgDetails['path'];
+
+        $newUpload = $this->files->uploadedImage('updated-image.png', 'compressed.png');
+        $this->assertFileEquals($this->files->testFilePath('test-image.png'), public_path($relPath));
+
+        $imageId = $imgDetails['response']->id;
+        $image = Image::findOrFail($imageId);
+        $image->updated_at = now()->subMonth();
+        $image->save();
+
+        $this->call('PUT', "/images/{$imageId}/file", [], [], ['file' => $newUpload])
+            ->assertOk();
+
+        $this->assertFileEquals($this->files->testFilePath('compressed.png'), public_path($relPath));
+
+        $image->refresh();
+        $this->assertTrue($image->updated_at->gt(now()->subMinute()));
+
+        $this->files->deleteAtRelativePath($relPath);
+    }
+
+    public function test_image_file_update_does_not_allow_change_in_image_extension()
+    {
+        $page = $this->entities->page();
+        $this->asEditor();
+
+        $imgDetails = $this->files->uploadGalleryImageToPage($this, $page);
+        $relPath = $imgDetails['path'];
+        $newUpload = $this->files->uploadedImage('updated-image.jpg', 'compressed.png');
+
+        $imageId = $imgDetails['response']->id;
+        $this->call('PUT', "/images/{$imageId}/file", [], [], ['file' => $newUpload])
+            ->assertJson([
+                "message" => "Image file replacements must be of the same type",
+                "status" => "error",
+            ]);
+
+        $this->files->deleteAtRelativePath($relPath);
+    }
+
     public function test_gallery_get_list_format()
     {
         $this->asEditor();
@@ -120,6 +166,17 @@ class ImageTest extends TestCase
         $this->withHtml($searchFailRequest)->assertElementNotExists('div');
     }
 
+    public function test_image_gallery_lists_for_draft_page()
+    {
+        $this->actingAs($this->users->editor());
+        $draft = $this->entities->newDraftPage();
+        $this->files->uploadGalleryImageToPage($this, $draft);
+        $image = Image::query()->where('uploaded_to', '=', $draft->id)->firstOrFail();
+
+        $resp = $this->get("/images/gallery?page=1&uploaded_to={$draft->id}");
+        $resp->assertSee($image->getThumb(150, 150));
+    }
+
     public function test_image_usage()
     {
         $page = $this->entities->page();
@@ -152,7 +209,8 @@ class ImageTest extends TestCase
 
         $file = $this->files->imageFromBase64File('bad-php.base64', $fileName);
         $upload = $this->withHeader('Content-Type', 'image/jpeg')->call('POST', '/images/gallery', ['uploaded_to' => $page->id], [], ['file' => $file], []);
-        $upload->assertStatus(302);
+        $upload->assertStatus(500);
+        $this->assertStringContainsString('The file must have a valid & supported image extension', $upload->json('message'));
 
         $this->assertFalse(file_exists(public_path($relPath)), 'Uploaded php file was uploaded but should have been stopped');
 
@@ -174,7 +232,8 @@ class ImageTest extends TestCase
 
         $file = $this->files->imageFromBase64File('bad-phtml.base64', $fileName);
         $upload = $this->withHeader('Content-Type', 'image/jpeg')->call('POST', '/images/gallery', ['uploaded_to' => $page->id], [], ['file' => $file], []);
-        $upload->assertStatus(302);
+        $upload->assertStatus(500);
+        $this->assertStringContainsString('The file must have a valid & supported image extension', $upload->json('message'));
 
         $this->assertFalse(file_exists(public_path($relPath)), 'Uploaded php file was uploaded but should have been stopped');
     }
@@ -480,15 +539,15 @@ class ImageTest extends TestCase
         $image = Image::first();
 
         $resp = $this->get("/images/edit/{$image->id}");
-        $this->withHtml($resp)->assertElementExists('button#image-manager-delete[title="Delete"]');
+        $this->withHtml($resp)->assertElementExists('button#image-manager-delete');
 
         $resp = $this->actingAs($viewer)->get("/images/edit/{$image->id}");
-        $this->withHtml($resp)->assertElementNotExists('button#image-manager-delete[title="Delete"]');
+        $this->withHtml($resp)->assertElementNotExists('button#image-manager-delete');
 
         $this->permissions->grantUserRolePermissions($viewer, ['image-delete-all']);
 
         $resp = $this->actingAs($viewer)->get("/images/edit/{$image->id}");
-        $this->withHtml($resp)->assertElementExists('button#image-manager-delete[title="Delete"]');
+        $this->withHtml($resp)->assertElementExists('button#image-manager-delete');
 
         $this->files->deleteAtRelativePath($relPath);
     }