3 use BookStack\Entities\Page;
4 use BookStack\Entities\Repos\EntityRepo;
5 use BookStack\Entities\Repos\PageRepo;
7 class PageContentTest extends TestCase
10 public function test_page_includes()
12 $page = Page::first();
13 $secondPage = Page::where('id', '!=', $page->id)->first();
15 $secondPage->html = "<p id='section1'>Hello, This is a test</p><p id='section2'>This is a second block of content</p>";
20 $pageContent = $this->get($page->getUrl());
21 $pageContent->assertDontSee('Hello, This is a test');
23 $originalHtml = $page->html;
24 $page->html .= "{{@{$secondPage->id}}}";
27 $pageContent = $this->get($page->getUrl());
28 $pageContent->assertSee('Hello, This is a test');
29 $pageContent->assertSee('This is a second block of content');
31 $page->html = $originalHtml . " Well {{@{$secondPage->id}#section2}}";
34 $pageContent = $this->get($page->getUrl());
35 $pageContent->assertDontSee('Hello, This is a test');
36 $pageContent->assertSee('Well This is a second block of content');
39 public function test_saving_page_with_includes()
41 $page = Page::first();
42 $secondPage = Page::where('id', '!=', $page->id)->first();
45 $includeTag = '{{@' . $secondPage->id . '}}';
46 $page->html = '<p>' . $includeTag . '</p>';
48 $resp = $this->put($page->getUrl(), ['name' => $page->name, 'html' => $page->html, 'summary' => '']);
50 $resp->assertStatus(302);
52 $page = Page::find($page->id);
53 $this->assertContains($includeTag, $page->html);
54 $this->assertEquals('', $page->text);
57 public function test_page_includes_do_not_break_tables()
59 $page = Page::first();
60 $secondPage = Page::where('id', '!=', $page->id)->first();
62 $content = '<table id="table"><tbody><tr><td>test</td></tr></tbody></table>';
63 $secondPage->html = $content;
66 $page->html = "{{@{$secondPage->id}#table}}";
70 $pageResp = $this->get($page->getUrl());
71 $pageResp->assertSee($content);
74 public function test_page_content_scripts_escaped_by_default()
77 $page = Page::first();
78 $script = '<script>console.log("hello-test")</script>';
79 $page->html = "escape {$script}";
82 $pageView = $this->get($page->getUrl());
83 $pageView->assertDontSee($script);
84 $pageView->assertSee(htmlentities($script));
87 public function test_page_content_scripts_show_when_configured()
90 $page = Page::first();
91 config()->push('app.allow_content_scripts', 'true');
92 $script = '<script>console.log("hello-test")</script>';
93 $page->html = "no escape {$script}";
96 $pageView = $this->get($page->getUrl());
97 $pageView->assertSee($script);
98 $pageView->assertDontSee(htmlentities($script));
101 public function test_duplicate_ids_does_not_break_page_render()
104 $pageA = Page::first();
105 $pageB = Page::query()->where('id', '!=', $pageA->id)->first();
107 $content = '<ul id="bkmrk-xxx-%28"></ul> <ul id="bkmrk-xxx-%28"></ul>';
108 $pageA->html = $content;
111 $pageB->html = '<ul id="bkmrk-xxx-%28"></ul> <p>{{@'. $pageA->id .'#test}}</p>';
114 $pageView = $this->get($pageB->getUrl());
115 $pageView->assertSuccessful();
118 public function test_duplicate_ids_fixed_on_page_save()
121 $page = Page::first();
123 $content = '<ul id="bkmrk-test"><li>test a</li><li><ul id="bkmrk-test"><li>test b</li></ul></li></ul>';
124 $pageSave = $this->put($page->getUrl(), [
125 'name' => $page->name,
129 $pageSave->assertRedirect();
131 $updatedPage = Page::where('id', '=', $page->id)->first();
132 $this->assertEquals(substr_count($updatedPage->html, "bkmrk-test\""), 1);