2021-06-26 11:23:15 -04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tests\Uploads;
|
2020-04-05 12:27:16 -04:00
|
|
|
|
|
|
|
use BookStack\Uploads\Image;
|
|
|
|
use Tests\TestCase;
|
|
|
|
|
|
|
|
class DrawioTest extends TestCase
|
|
|
|
{
|
|
|
|
public function test_get_image_as_base64()
|
|
|
|
{
|
2023-01-26 07:16:23 -05:00
|
|
|
$page = $this->entities->page();
|
2020-04-05 12:27:16 -04:00
|
|
|
$this->asAdmin();
|
|
|
|
$imageName = 'first-image.png';
|
|
|
|
|
2023-02-08 09:39:13 -05:00
|
|
|
$this->files->uploadGalleryImage($this, $imageName, $page->id);
|
2023-01-26 07:16:23 -05:00
|
|
|
/** @var Image $image */
|
|
|
|
$image = Image::query()->first();
|
2020-04-05 12:27:16 -04:00
|
|
|
$image->type = 'drawio';
|
|
|
|
$image->save();
|
|
|
|
|
|
|
|
$imageGet = $this->getJson("/images/drawio/base64/{$image->id}");
|
|
|
|
$imageGet->assertJson([
|
2021-06-26 11:23:15 -04:00
|
|
|
'content' => 'iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAIAAAACDbGyAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAB3RJTUUH4gEcDCo5iYNs+gAAAB1pVFh0Q29tbWVudAAAAAAAQ3JlYXRlZCB3aXRoIEdJTVBkLmUHAAAAFElEQVQI12O0jN/KgASYGFABqXwAZtoBV6Sl3hIAAAAASUVORK5CYII=',
|
2020-04-05 12:27:16 -04:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2023-01-26 07:16:23 -05:00
|
|
|
public function test_non_accessible_image_returns_404_error_and_message()
|
|
|
|
{
|
|
|
|
$page = $this->entities->page();
|
|
|
|
$this->asEditor();
|
|
|
|
$imageName = 'non-accessible-image.png';
|
|
|
|
|
2023-02-08 09:39:13 -05:00
|
|
|
$this->files->uploadGalleryImage($this, $imageName, $page->id);
|
2023-01-26 07:16:23 -05:00
|
|
|
/** @var Image $image */
|
|
|
|
$image = Image::query()->first();
|
|
|
|
$image->type = 'drawio';
|
|
|
|
$image->save();
|
|
|
|
$this->permissions->disableEntityInheritedPermissions($page);
|
|
|
|
|
|
|
|
$imageGet = $this->getJson("/images/drawio/base64/{$image->id}");
|
|
|
|
$imageGet->assertNotFound();
|
|
|
|
$imageGet->assertJson([
|
|
|
|
'message' => 'Drawing data could not be loaded. The drawing file might no longer exist or you may not have permission to access it.',
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2020-04-05 12:27:16 -04:00
|
|
|
public function test_drawing_base64_upload()
|
|
|
|
{
|
2023-01-26 07:16:23 -05:00
|
|
|
$page = $this->entities->page();
|
2023-01-21 06:08:34 -05:00
|
|
|
$editor = $this->users->editor();
|
2020-04-05 12:27:16 -04:00
|
|
|
$this->actingAs($editor);
|
|
|
|
|
|
|
|
$upload = $this->postJson('images/drawio', [
|
|
|
|
'uploaded_to' => $page->id,
|
2021-06-26 11:23:15 -04:00
|
|
|
'image' => 'image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAIAAAACDbGyAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAB3RJTUUH4gEcDCo5iYNs+gAAAB1pVFh0Q29tbWVudAAAAAAAQ3JlYXRlZCB3aXRoIEdJTVBkLmUHAAAAFElEQVQI12O0jN/KgASYGFABqXwAZtoBV6Sl3hIAAAAASUVORK5CYII=',
|
2020-04-05 12:27:16 -04:00
|
|
|
]);
|
|
|
|
|
|
|
|
$upload->assertStatus(200);
|
|
|
|
$upload->assertJson([
|
2021-06-26 11:23:15 -04:00
|
|
|
'type' => 'drawio',
|
2020-04-05 12:27:16 -04:00
|
|
|
'uploaded_to' => $page->id,
|
2021-06-26 11:23:15 -04:00
|
|
|
'created_by' => $editor->id,
|
|
|
|
'updated_by' => $editor->id,
|
2020-04-05 12:27:16 -04:00
|
|
|
]);
|
|
|
|
|
|
|
|
$image = Image::where('type', '=', 'drawio')->first();
|
2021-06-26 11:23:15 -04:00
|
|
|
$this->assertTrue(file_exists(public_path($image->path)), 'Uploaded image not found at path: ' . public_path($image->path));
|
2020-04-05 12:27:16 -04:00
|
|
|
|
2023-02-08 09:39:13 -05:00
|
|
|
$testImageData = $this->files->pngImageData();
|
2020-04-05 12:27:16 -04:00
|
|
|
$uploadedImageData = file_get_contents(public_path($image->path));
|
2021-06-26 11:23:15 -04:00
|
|
|
$this->assertTrue($testImageData === $uploadedImageData, 'Uploaded image file data does not match our test image as expected');
|
2020-04-05 12:27:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function test_drawio_url_can_be_configured()
|
|
|
|
{
|
|
|
|
config()->set('services.drawio', 'http://cats.com?dog=tree');
|
2023-01-26 07:16:23 -05:00
|
|
|
$page = $this->entities->page();
|
2023-01-21 06:08:34 -05:00
|
|
|
$editor = $this->users->editor();
|
2020-04-05 12:27:16 -04:00
|
|
|
|
|
|
|
$resp = $this->actingAs($editor)->get($page->getUrl('/edit'));
|
2021-10-26 17:04:18 -04:00
|
|
|
$resp->assertSee('drawio-url="http://cats.com?dog=tree"', false);
|
2020-04-05 12:27:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function test_drawio_url_can_be_disabled()
|
|
|
|
{
|
|
|
|
config()->set('services.drawio', true);
|
2023-01-26 07:16:23 -05:00
|
|
|
$page = $this->entities->page();
|
2023-01-21 06:08:34 -05:00
|
|
|
$editor = $this->users->editor();
|
2020-04-05 12:27:16 -04:00
|
|
|
|
|
|
|
$resp = $this->actingAs($editor)->get($page->getUrl('/edit'));
|
2022-04-20 18:42:47 -04:00
|
|
|
$resp->assertSee('drawio-url="https://embed.diagrams.net/?embed=1&proto=json&spin=1&configure=1"', false);
|
2020-04-05 12:27:16 -04:00
|
|
|
|
|
|
|
config()->set('services.drawio', false);
|
|
|
|
$resp = $this->actingAs($editor)->get($page->getUrl('/edit'));
|
2021-10-26 17:04:18 -04:00
|
|
|
$resp->assertDontSee('drawio-url', false);
|
2020-04-05 12:27:16 -04:00
|
|
|
}
|
2021-06-26 11:23:15 -04:00
|
|
|
}
|