]> BookStack Code Mirror - bookstack/blob - tests/Unit/PageIncludeParserTest.php
Includes: Added block-level handling to new include system
[bookstack] / tests / Unit / PageIncludeParserTest.php
1 <?php
2
3 namespace Tests\Unit;
4
5 use BookStack\Entities\Tools\PageIncludeParser;
6 use Tests\TestCase;
7
8 class PageIncludeParserTest extends TestCase
9 {
10     public function test_simple_inline_text()
11     {
12         $this->runParserTest(
13             '<p>{{@45#content}}</p>',
14             ['45' => '<p id="content">Testing</p>'],
15             '<p>Testing</p>',
16         );
17     }
18
19     public function test_simple_inline_text_with_existing_siblings()
20     {
21         $this->runParserTest(
22             '<p>{{@45#content}} <strong>Hi</strong>there!</p>',
23             ['45' => '<p id="content">Testing</p>'],
24             '<p>Testing <strong>Hi</strong>there!</p>',
25         );
26     }
27
28     public function test_simple_inline_text_within_other_text()
29     {
30         $this->runParserTest(
31             '<p>Hello {{@45#content}}there!</p>',
32             ['45' => '<p id="content">Testing</p>'],
33             '<p>Hello Testingthere!</p>',
34         );
35     }
36
37     public function test_block_content_types()
38     {
39         $inputs = [
40             '<table id="content"><td>Text</td></table>',
41             '<ul id="content"><li>Item A</li></ul>',
42             '<ol id="content"><li>Item A</li></ol>',
43             '<pre id="content">Code</pre>',
44         ];
45
46         foreach ($inputs as $input) {
47             $this->runParserTest(
48                 '<p>A{{@45#content}}B</p>',
49                 ['45' => $input],
50                 '<p>A</p>' . $input . '<p>B</p>',
51             );
52         }
53     }
54
55     public function test_block_content_nested_origin_gets_placed_before()
56     {
57         $this->runParserTest(
58             '<p><strong>A {{@45#content}} there!</strong></p>',
59             ['45' => '<pre id="content">Testing</pre>'],
60             '<pre id="content">Testing</pre><p><strong>A  there!</strong></p>',
61         );
62     }
63
64     public function test_block_content_nested_origin_gets_placed_after()
65     {
66         $this->runParserTest(
67             '<p><strong>Some really good {{@45#content}} there!</strong></p>',
68             ['45' => '<pre id="content">Testing</pre>'],
69             '<p><strong>Some really good  there!</strong></p><pre id="content">Testing</pre>',
70         );
71     }
72
73     public function test_block_content_in_shallow_origin_gets_split()
74     {
75         $this->runParserTest(
76             '<p>Some really good {{@45#content}} there!</p>',
77             ['45' => '<pre id="content">doggos</pre>'],
78             '<p>Some really good </p><pre id="content">doggos</pre><p> there!</p>',
79         );
80     }
81
82     public function test_block_content_in_shallow_origin_split_does_not_duplicate_id()
83     {
84         $this->runParserTest(
85             '<p id="test" title="Hi">Some really good {{@45#content}} there!</p>',
86             ['45' => '<pre id="content">doggos</pre>'],
87             '<p title="Hi">Some really good </p><pre id="content">doggos</pre><p id="test" title="Hi"> there!</p>',
88         );
89     }
90
91     public function test_block_content_in_shallow_origin_does_not_leave_empty_nodes()
92     {
93         $this->runParserTest(
94             '<p>{{@45#content}}</p>',
95             ['45' => '<pre id="content">doggos</pre>'],
96             '<pre id="content">doggos</pre>',
97         );
98     }
99
100     public function test_simple_whole_document()
101     {
102         $this->runParserTest(
103             '<p>{{@45}}</p>',
104             ['45' => '<p id="content">Testing</p>'],
105             '<p id="content">Testing</p>',
106         );
107     }
108
109     public function test_multi_source_elem_whole_document()
110     {
111         $this->runParserTest(
112             '<p>{{@45}}</p>',
113             ['45' => '<p>Testing</p><blockquote>This</blockquote>'],
114             '<p>Testing</p><blockquote>This</blockquote>',
115         );
116     }
117
118     public function test_multi_source_elem_whole_document_with_shared_content_origin()
119     {
120         $this->runParserTest(
121             '<p>This is {{@45}} some text</p>',
122             ['45' => '<p>Testing</p><blockquote>This</blockquote>'],
123             '<p>This is </p><p>Testing</p><blockquote>This</blockquote><p> some text</p>',
124         );
125     }
126
127     protected function runParserTest(string $html, array $contentById, string $expected)
128     {
129         $parser = new PageIncludeParser($html, function (int $id) use ($contentById) {
130             return $contentById[strval($id)] ?? '';
131         });
132
133         $result = $parser->parse();
134         $this->assertEquals($expected, $result);
135     }
136 }