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_removed_by_default()
77 $page = Page::first();
78 $script = 'abc123<script>console.log("hello-test")</script>abc123';
79 $page->html = "escape {$script}";
82 $pageView = $this->get($page->getUrl());
83 $pageView->assertDontSee($script);
84 $pageView->assertSee('abc123abc123');
87 public function test_page_inline_on_attributes_removed_by_default()
90 $page = Page::first();
91 $script = '<p onmouseenter="console.log(\'test\')">Hello</p>';
92 $page->html = "escape {$script}";
95 $pageView = $this->get($page->getUrl());
96 $pageView->assertDontSee($script);
97 $pageView->assertSee('<p>Hello</p>');
100 public function test_page_content_scripts_show_when_configured()
103 $page = Page::first();
104 config()->push('app.allow_content_scripts', 'true');
106 $script = 'abc123<script>console.log("hello-test")</script>abc123';
107 $page->html = "no escape {$script}";
110 $pageView = $this->get($page->getUrl());
111 $pageView->assertSee($script);
112 $pageView->assertDontSee('abc123abc123');
115 public function test_page_inline_on_attributes_show_if_configured()
118 $page = Page::first();
119 config()->push('app.allow_content_scripts', 'true');
121 $script = '<p onmouseenter="console.log(\'test\')">Hello</p>';
122 $page->html = "escape {$script}";
125 $pageView = $this->get($page->getUrl());
126 $pageView->assertSee($script);
127 $pageView->assertDontSee('<p>Hello</p>');
130 public function test_duplicate_ids_does_not_break_page_render()
133 $pageA = Page::first();
134 $pageB = Page::query()->where('id', '!=', $pageA->id)->first();
136 $content = '<ul id="bkmrk-xxx-%28"></ul> <ul id="bkmrk-xxx-%28"></ul>';
137 $pageA->html = $content;
140 $pageB->html = '<ul id="bkmrk-xxx-%28"></ul> <p>{{@'. $pageA->id .'#test}}</p>';
143 $pageView = $this->get($pageB->getUrl());
144 $pageView->assertSuccessful();
147 public function test_duplicate_ids_fixed_on_page_save()
150 $page = Page::first();
152 $content = '<ul id="bkmrk-test"><li>test a</li><li><ul id="bkmrk-test"><li>test b</li></ul></li></ul>';
153 $pageSave = $this->put($page->getUrl(), [
154 'name' => $page->name,
158 $pageSave->assertRedirect();
160 $updatedPage = Page::where('id', '=', $page->id)->first();
161 $this->assertEquals(substr_count($updatedPage->html, "bkmrk-test\""), 1);