3 namespace Tests\Uploads;
5 use BookStack\Entities\Models\Page;
6 use BookStack\Uploads\Image;
9 class DrawioTest extends TestCase
13 public function test_get_image_as_base64_with_png_content()
15 $page = Page::first();
17 $imageName = 'first-image.png';
19 $this->uploadImage($imageName, $page->id);
20 $image = Image::first();
21 $image->type = 'drawio';
24 $imageGet = $this->getJson("/images/drawio/base64/{$image->id}");
25 $imageGet->assertJson([
26 'content' => 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAIAAAACDbGyAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAB3RJTUUH4gEcDCo5iYNs+gAAAB1pVFh0Q29tbWVudAAAAAAAQ3JlYXRlZCB3aXRoIEdJTVBkLmUHAAAAFElEQVQI12O0jN/KgASYGFABqXwAZtoBV6Sl3hIAAAAASUVORK5CYII=',
30 public function test_get_image_as_base64_with_svg_content()
32 $page = Page::first();
35 $this->uploadImage('my-drawing.svg', $page->id, 'image/svg+xml', 'diagram.svg');
36 $image = Image::first();
37 $image->type = 'drawio';
40 $imageGet = $this->getJson("/images/drawio/base64/{$image->id}");
41 $imageGet->assertJson([
42 'content' => 'data:image/svg+xml;base64,' . base64_encode(file_get_contents($this->getTestImageFilePath('diagram.svg'))),
46 public function test_drawing_base64_upload_with_png()
48 $page = Page::first();
49 $editor = $this->getEditor();
50 $this->actingAs($editor);
52 $upload = $this->postJson('images/drawio', [
53 'uploaded_to' => $page->id,
54 'image' => 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAIAAAACDbGyAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAB3RJTUUH4gEcDCo5iYNs+gAAAB1pVFh0Q29tbWVudAAAAAAAQ3JlYXRlZCB3aXRoIEdJTVBkLmUHAAAAFElEQVQI12O0jN/KgASYGFABqXwAZtoBV6Sl3hIAAAAASUVORK5CYII=',
57 $upload->assertStatus(200);
60 'uploaded_to' => $page->id,
61 'created_by' => $editor->id,
62 'updated_by' => $editor->id,
65 $image = Image::where('type', '=', 'drawio')->first();
66 $this->assertTrue(file_exists(public_path($image->path)), 'Uploaded image not found at path: ' . public_path($image->path));
68 $testImageData = file_get_contents($this->getTestImageFilePath());
69 $uploadedImageData = file_get_contents(public_path($image->path));
70 $this->assertTrue($testImageData === $uploadedImageData, 'Uploaded image file data does not match our test image as expected');
73 public function test_drawing_base64_upload_with_svg()
75 $page = Page::first();
76 $editor = $this->getEditor();
77 $this->actingAs($editor);
79 $upload = $this->postJson('images/drawio', [
80 'uploaded_to' => $page->id,
81 'image' => 'data:image/svg+xml;base64,' . base64_encode(file_get_contents($this->getTestImageFilePath('diagram.svg'))),
84 $upload->assertStatus(200);
87 'uploaded_to' => $page->id,
88 'created_by' => $editor->id,
89 'updated_by' => $editor->id,
92 $image = Image::where('type', '=', 'drawio')->first();
93 $this->assertStringEndsWith('.svg', $image->path);
94 $this->assertTrue(file_exists(public_path($image->path)), 'Uploaded image not found at path: ' . public_path($image->path));
96 $testImageData = file_get_contents($this->getTestImageFilePath('diagram.svg'));
97 $uploadedImageData = file_get_contents(public_path($image->path));
98 $this->assertTrue($testImageData === $uploadedImageData, 'Uploaded image file data does not match our test image as expected');
101 public function test_drawio_url_can_be_configured()
103 config()->set('services.drawio', 'http://cats.com?dog=tree');
104 $page = Page::first();
105 $editor = $this->getEditor();
107 $resp = $this->actingAs($editor)->get($page->getUrl('/edit'));
108 $resp->assertSee('drawio-url="http://cats.com?dog=tree"', false);
111 public function test_drawio_url_can_be_disabled()
113 config()->set('services.drawio', true);
114 $page = Page::first();
115 $editor = $this->getEditor();
117 $resp = $this->actingAs($editor)->get($page->getUrl('/edit'));
118 $resp->assertSee('drawio-url="https://embed.diagrams.net/?embed=1&proto=json&spin=1&configure=1"', false);
120 config()->set('services.drawio', false);
121 $resp = $this->actingAs($editor)->get($page->getUrl('/edit'));
122 $resp->assertDontSee('drawio-url', false);