]> BookStack Code Mirror - bookstack/blobdiff - tests/Uploads/UsesImages.php
Guest create page: name field autofocus
[bookstack] / tests / Uploads / UsesImages.php
index 64f26dea8a9be7c847909d0d192a780a2ed25f02..e2c16c37c65b84141a2ac7b2a3fd44782b11de76 100644 (file)
@@ -1,15 +1,17 @@
-<?php namespace Tests\Uploads;
+<?php
+
+namespace Tests\Uploads;
 
 use BookStack\Entities\Models\Page;
 use Illuminate\Http\UploadedFile;
+use stdClass;
 
 trait UsesImages
 {
     /**
-     * Get the path to our basic test image.
-     * @return string
+     * Get the path to a file in the test-data-directory.
      */
-    protected function getTestImageFilePath(?string $fileName = null)
+    protected function getTestImageFilePath(?string $fileName = null): string
     {
         if (is_null($fileName)) {
             $fileName = 'test-image.png';
@@ -19,17 +21,32 @@ trait UsesImages
     }
 
     /**
-     * Get a test image that can be uploaded
-     * @param $fileName
-     * @return UploadedFile
+     * Creates a new temporary image file using the given name,
+     * with the content decoded from the given bas64 file name.
+     * Is generally used for testing sketchy files that could trip AV.
+     */
+    protected function newTestImageFromBase64(string $base64FileName, $imageFileName): UploadedFile
+    {
+        $imagePath = implode(DIRECTORY_SEPARATOR, [sys_get_temp_dir(), $imageFileName]);
+        $base64FilePath = $this->getTestImageFilePath($base64FileName);
+        $data = file_get_contents($base64FilePath);
+        $decoded = base64_decode($data);
+        file_put_contents($imagePath, $decoded);
+
+        return new UploadedFile($imagePath, $imageFileName, 'image/png', null, true);
+    }
+
+    /**
+     * Get a test image that can be uploaded.
      */
-    protected function getTestImage($fileName, ?string $testDataFileName = null)
+    protected function getTestImage(string $fileName, ?string $testDataFileName = null): UploadedFile
     {
-        return new UploadedFile($this->getTestImageFilePath($testDataFileName), $fileName, 'image/png', 5238, null, true);
+        return new UploadedFile($this->getTestImageFilePath($testDataFileName), $fileName, 'image/png', null, true);
     }
 
     /**
      * Get the raw file data for the test image.
+     *
      * @return false|string
      */
     protected function getTestImageContent()
@@ -42,19 +59,22 @@ trait UsesImages
      */
     protected function getTestImagePath(string $type, string $fileName): string
     {
-        return '/uploads/images/' . $type . '/' . Date('Y-m') . '/' . $fileName;
+        return '/uploads/images/' . $type . '/' . date('Y-m') . '/' . $fileName;
     }
 
     /**
      * Uploads an image with the given name.
+     *
      * @param $name
-     * @param int $uploadedTo
+     * @param int    $uploadedTo
      * @param string $contentType
+     *
      * @return \Illuminate\Foundation\Testing\TestResponse
      */
     protected function uploadImage($name, $uploadedTo = 0, $contentType = 'image/png', ?string $testDataFileName = null)
     {
         $file = $this->getTestImage($name, $testDataFileName);
+
         return $this->withHeader('Content-Type', $contentType)
             ->call('POST', '/images/gallery', ['uploaded_to' => $uploadedTo], [], ['file' => $file], []);
     }
@@ -63,13 +83,15 @@ trait UsesImages
      * Upload a new gallery image.
      * Returns the image name.
      * Can provide a page to relate the image to.
+     *
      * @param Page|null $page
-     * @return array
+     *
+     * @return array{name: string, path: string, page: Page, response: stdClass}
      */
     protected function uploadGalleryImage(Page $page = null, ?string $testDataFileName = null)
     {
         if ($page === null) {
-            $page = Page::query()->first();
+            $page = $this->entities->page();
         }
 
         $imageName = $testDataFileName ?? 'first-image.png';
@@ -78,24 +100,23 @@ trait UsesImages
 
         $upload = $this->uploadImage($imageName, $page->id, 'image/png', $testDataFileName);
         $upload->assertStatus(200);
+
         return [
-            'name' => $imageName,
-            'path' => $relPath,
-            'page' => $page,
+            'name'     => $imageName,
+            'path'     => $relPath,
+            'page'     => $page,
             'response' => json_decode($upload->getContent()),
         ];
     }
 
     /**
      * Delete an uploaded image.
-     * @param $relPath
      */
-    protected function deleteImage($relPath)
+    protected function deleteImage(string $relPath)
     {
         $path = public_path($relPath);
         if (file_exists($path)) {
             unlink($path);
         }
     }
-
-}
\ No newline at end of file
+}