X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/193d7fb3fe71a76a43ebc1ccdb617b4b627d1e09..refs/pull/3364/head:/tests/Entity/PageContentTest.php diff --git a/tests/Entity/PageContentTest.php b/tests/Entity/PageContentTest.php index 45c27c9f9..b9680d23f 100644 --- a/tests/Entity/PageContentTest.php +++ b/tests/Entity/PageContentTest.php @@ -62,7 +62,9 @@ class PageContentTest extends TestCase public function test_page_includes_do_not_break_tables() { + /** @var Page $page */ $page = Page::query()->first(); + /** @var Page $secondPage */ $secondPage = Page::query()->where('id', '!=', $page->id)->first(); $content = '
test
'; @@ -72,9 +74,26 @@ class PageContentTest extends TestCase $page->html = "{{@{$secondPage->id}#table}}"; $page->save(); - $this->asEditor(); - $pageResp = $this->get($page->getUrl()); - $pageResp->assertSee($content); + $pageResp = $this->asEditor()->get($page->getUrl()); + $pageResp->assertSee($content, false); + } + + public function test_page_includes_do_not_break_code() + { + /** @var Page $page */ + $page = Page::query()->first(); + /** @var Page $secondPage */ + $secondPage = Page::query()->where('id', '!=', $page->id)->first(); + + $content = '
var cat = null;
'; + $secondPage->html = $content; + $secondPage->save(); + + $page->html = "{{@{$secondPage->id}#bkmrk-code}}"; + $page->save(); + + $pageResp = $this->asEditor()->get($page->getUrl()); + $pageResp->assertSee($content, false); } public function test_page_includes_rendered_on_book_export() @@ -106,7 +125,7 @@ class PageContentTest extends TestCase $pageView = $this->get($page->getUrl()); $pageView->assertStatus(200); - $pageView->assertDontSee($script); + $pageView->assertDontSee($script, false); $pageView->assertSee('abc123abc123'); } @@ -260,8 +279,8 @@ class PageContentTest extends TestCase $pageView = $this->get($page->getUrl()); $pageView->assertStatus(200); - $pageView->assertDontSee($script); - $pageView->assertSee('

Hello

'); + $pageView->assertDontSee($script, false); + $pageView->assertSee('

Hello

', false); } public function test_more_complex_inline_on_attributes_escaping_scenarios() @@ -301,7 +320,7 @@ class PageContentTest extends TestCase $page->save(); $pageView = $this->get($page->getUrl()); - $pageView->assertSee($script); + $pageView->assertSee($script, false); $pageView->assertDontSee('abc123abc123'); } @@ -338,8 +357,8 @@ class PageContentTest extends TestCase $page->save(); $pageView = $this->get($page->getUrl()); - $pageView->assertSee($script); - $pageView->assertDontSee('

Hello

'); + $pageView->assertSee($script, false); + $pageView->assertDontSee('

Hello

', false); } public function test_duplicate_ids_does_not_break_page_render() @@ -545,7 +564,7 @@ class PageContentTest extends TestCase $pageView = $this->get($page->getUrl()); $pageView->assertStatus(200); - $pageView->assertSee($content); + $pageView->assertSee($content, false); } public function test_base64_images_get_extracted_from_page_content() @@ -594,17 +613,122 @@ class PageContentTest extends TestCase $this->deleteImage($imagePath); } - public function test_base64_images_blanked_if_not_supported_extension_for_extract() + public function test_base64_images_within_html_blanked_if_not_supported_extension_for_extract() + { + // Relevant to https://github.com/BookStackApp/BookStack/issues/3010 and other cases + $extensions = [ + 'jiff', 'pngr', 'png ', ' png', '.png', 'png.', 'p.ng', ',png', + 'data:image/png', ',data:image/png', + ]; + + foreach ($extensions as $extension) { + $this->asEditor(); + $page = Page::query()->first(); + + $this->put($page->getUrl(), [ + 'name' => $page->name, 'summary' => '', + 'html' => '

test

', + ]); + + $page->refresh(); + $this->assertStringContainsString('html); + } + } + + public function test_base64_images_get_extracted_from_markdown_page_content() { $this->asEditor(); $page = Page::query()->first(); $this->put($page->getUrl(), [ - 'name' => $page->name, 'summary' => '', - 'html' => '

test

', + 'name' => $page->name, 'summary' => '', + 'markdown' => 'test ![test](data:image/jpeg;base64,' . $this->base64Jpeg . ')', ]); $page->refresh(); - $this->assertStringContainsString('html); + $this->assertStringMatchesFormat('%Atest test%A

%A', $page->html); + + $matches = []; + preg_match('/src="https:\/\/p.rizon.top:443\/http\/localhost(.*?)"/', $page->html, $matches); + $imagePath = $matches[1]; + $imageFile = public_path($imagePath); + $this->assertEquals(base64_decode($this->base64Jpeg), file_get_contents($imageFile)); + + $this->deleteImage($imagePath); + } + + public function test_markdown_base64_extract_not_limited_by_pcre_limits() + { + $pcreBacktrackLimit = ini_get('pcre.backtrack_limit'); + $pcreRecursionLimit = ini_get('pcre.recursion_limit'); + + $this->asEditor(); + $page = Page::query()->first(); + + ini_set('pcre.backtrack_limit', '500'); + ini_set('pcre.recursion_limit', '500'); + + $content = str_repeat('a', 5000); + $base64Content = base64_encode($content); + + $this->put($page->getUrl(), [ + 'name' => $page->name, 'summary' => '', + 'markdown' => 'test ![test](data:image/jpeg;base64,' . $base64Content . ') ![test](data:image/jpeg;base64,' . $base64Content . ')', + ]); + + $page->refresh(); + $this->assertStringMatchesFormat('test test test%A

%A', $page->html); + + $matches = []; + preg_match('/src="https:\/\/p.rizon.top:443\/http\/localhost(.*?)"/', $page->html, $matches); + $imagePath = $matches[1]; + $imageFile = public_path($imagePath); + $this->assertEquals($content, file_get_contents($imageFile)); + + $this->deleteImage($imagePath); + ini_set('pcre.backtrack_limit', $pcreBacktrackLimit); + ini_set('pcre.recursion_limit', $pcreRecursionLimit); + } + + public function test_base64_images_within_markdown_blanked_if_not_supported_extension_for_extract() + { + $page = Page::query()->first(); + + $this->asEditor()->put($page->getUrl(), [ + 'name' => $page->name, 'summary' => '', + 'markdown' => 'test ![test](data:image/jiff;base64,' . $this->base64Jpeg . ')', + ]); + + $this->assertStringContainsString('refresh()->html); + } + + public function test_nested_headers_gets_assigned_an_id() + { + $page = Page::query()->first(); + + $content = '
Simple Test
'; + $this->asEditor()->put($page->getUrl(), [ + 'name' => $page->name, + 'html' => $content, + ]); + + // The top level node will get assign the bkmrk-simple-test id because the system will + // take the node value of h5 + // So the h5 should get the bkmrk-simple-test-1 id + $this->assertStringContainsString('
Simple Test
', $page->refresh()->html); + } + + public function test_non_breaking_spaces_are_preserved() + { + /** @var Page $page */ + $page = Page::query()->first(); + + $content = '

 

'; + $this->asEditor()->put($page->getUrl(), [ + 'name' => $page->name, + 'html' => $content, + ]); + + $this->assertStringContainsString('

 

', $page->refresh()->html); } }