1<?php
2/**
3 * @group plugin_latexport
4 * @group plugins
5 */
6
7require_once DOKU_PLUGIN . 'latexport/_test/decorator_mock.php';
8require_once DOKU_PLUGIN . 'latexport/implementation/decorator_includer.php';
9
10class DecoratorIncluderTest extends DokuWikiTest {
11
12 	const LINK = "a:page:id";
13 	const TITLE = "A page";
14
15 	const LINK2 = "a:page:id2";
16 	const TITLE2 = "A second page";
17
18    protected $pluginsEnabled = array('latexport', 'mathjax');
19	private $decoratorMock;
20	private $includes;
21	private $decoratorIncluder;
22
23    public static function setUpBeforeClass(){
24        parent::setUpBeforeClass();
25	}
26
27	public function setUp() {
28		$this->includes = new SplQueue();
29		$this->decoratorMock = new DecoratorMock();
30		$this->decoratorIncluder = new DecoratorIncluder($this->includes, $this->decoratorMock);
31    }
32
33    public function testStandaloneInternalLinksInUnorderedListsAreIncludes() {
34		$this->decoratorIncluder->listu_open();
35		$this->decoratorIncluder->listitem_open(1);
36		$this->decoratorIncluder->listcontent_open();
37
38		$this->decoratorIncluder->cdata(' ');
39		$this->decoratorIncluder->internallink(DecoratorIncluderTest::LINK, DecoratorIncluderTest::TITLE);
40
41		$this->decoratorIncluder->listcontent_close();
42		$this->decoratorIncluder->listitem_close();
43		$this->decoratorIncluder->listu_close();
44
45		$this->assertTrue(  $this->decoratorMock->noCommands(),                     "Should not have any command");
46		$this->assertEquals(1,                            $this->includes->count(), "Should have one include");
47
48		$link = $this->includes->pop();
49		$this->assertEquals(DecoratorIncluderTest::LINK,  $link->getLink(),          "Link");
50		$this->assertEquals(DecoratorIncluderTest::TITLE, $link->getTitle(),         "Title is not as expected");
51    }
52
53	public function testInternalLinksMixedWithTextInAnUnorderedListItemAreRendered() {
54		$this->decoratorIncluder->listu_open();
55		$this->decoratorIncluder->listitem_open(1);
56		$this->decoratorIncluder->listcontent_open();
57
58		$this->decoratorIncluder->cdata('Follow the link:');
59		$this->decoratorIncluder->internallink(DecoratorIncluderTest::LINK, DecoratorIncluderTest::TITLE);
60
61		$this->decoratorIncluder->listcontent_close();
62		$this->decoratorIncluder->listitem_close();
63		$this->decoratorIncluder->listu_close();
64
65		$this->assertEquals($this->includes->count(),             0, "Should not have any include");
66
67		$this->assertEquals(new CommandListUOpen(),                $this->decoratorMock->nextCommand());
68		$this->assertEquals(new CommandListItemOpen(1),            $this->decoratorMock->nextCommand());
69		$this->assertEquals(new CommandListContentOpen(),          $this->decoratorMock->nextCommand());
70		$this->assertEquals(new CommandCData('Follow the link:'),  $this->decoratorMock->nextCommand());
71		$this->assertEquals(new CommandInternalLink(
72		                        	DecoratorIncluderTest::LINK,
73						        	DecoratorIncluderTest::TITLE), $this->decoratorMock->nextCommand());
74		$this->assertEquals(new CommandListContentClose(),         $this->decoratorMock->nextCommand());
75		$this->assertEquals(new CommandListItemClose(),            $this->decoratorMock->nextCommand());
76		$this->assertEquals(new CommandListUClose(),               $this->decoratorMock->nextCommand());
77	}
78
79	public function testCanRenderAListWithSeveralItems() {
80		$this->decoratorIncluder->listu_open();
81
82		$this->decoratorIncluder->listitem_open(1);
83		$this->decoratorIncluder->listcontent_open();
84		$this->decoratorIncluder->cdata('List item 1');
85		$this->decoratorIncluder->listcontent_close();
86		$this->decoratorIncluder->listitem_close();
87
88		$this->decoratorIncluder->listitem_open(1);
89		$this->decoratorIncluder->listcontent_open();
90		$this->decoratorIncluder->cdata('List item 2');
91		$this->decoratorIncluder->listcontent_close();
92		$this->decoratorIncluder->listitem_close();
93
94		$this->decoratorIncluder->listu_close();
95
96		$this->assertEquals($this->includes->count(), 0, "Should not have any include");
97
98		$this->assertEquals($this->decoratorMock->nextCommand(), new CommandListUOpen());
99
100		$this->assertEquals($this->decoratorMock->nextCommand(), new CommandListItemOpen(1));
101		$this->assertEquals($this->decoratorMock->nextCommand(), new CommandListContentOpen());
102		$this->assertEquals($this->decoratorMock->nextCommand(), new CommandCData('List item 1'));
103		$this->assertEquals($this->decoratorMock->nextCommand(), new CommandListContentClose());
104		$this->assertEquals($this->decoratorMock->nextCommand(), new CommandListItemClose());
105
106		$this->assertEquals($this->decoratorMock->nextCommand(), new CommandListItemOpen(1));
107		$this->assertEquals($this->decoratorMock->nextCommand(), new CommandListContentOpen());
108		$this->assertEquals($this->decoratorMock->nextCommand(), new CommandCData('List item 2'));
109		$this->assertEquals($this->decoratorMock->nextCommand(), new CommandListContentClose());
110		$this->assertEquals($this->decoratorMock->nextCommand(), new CommandListItemClose());
111
112		$this->assertEquals($this->decoratorMock->nextCommand(), new CommandListUClose());
113	}
114
115	public function testCanRenderANestedList() {
116
117		$this->decoratorIncluder->listu_open();
118		$this->decoratorIncluder->listitem_open(1);
119		$this->decoratorIncluder->listcontent_open();
120
121		$this->decoratorIncluder->listu_open();
122		$this->decoratorIncluder->listitem_open(2);
123		$this->decoratorIncluder->listcontent_open();
124		$this->decoratorIncluder->cdata('List item 2');
125		$this->decoratorIncluder->listcontent_close();
126		$this->decoratorIncluder->listitem_close();
127		$this->decoratorIncluder->listu_close();
128		$this->decoratorIncluder->listcontent_close();
129		$this->decoratorIncluder->listitem_close();
130		$this->decoratorIncluder->listu_close();
131
132		$this->assertEquals($this->includes->count(), 0, "Should not have any include");
133
134		$this->assertEquals($this->decoratorMock->nextCommand(), new CommandListUOpen());
135		$this->assertEquals($this->decoratorMock->nextCommand(), new CommandListItemOpen(1));
136		$this->assertEquals($this->decoratorMock->nextCommand(), new CommandListContentOpen());
137
138		$this->assertEquals($this->decoratorMock->nextCommand(), new CommandListUOpen());
139		$this->assertEquals($this->decoratorMock->nextCommand(), new CommandListItemOpen(2));
140		$this->assertEquals($this->decoratorMock->nextCommand(), new CommandListContentOpen());
141		$this->assertEquals($this->decoratorMock->nextCommand(), new CommandCData('List item 2'));
142		$this->assertEquals($this->decoratorMock->nextCommand(), new CommandListContentClose());
143		$this->assertEquals($this->decoratorMock->nextCommand(), new CommandListItemClose());
144		$this->assertEquals($this->decoratorMock->nextCommand(), new CommandListUClose());
145
146		$this->assertEquals($this->decoratorMock->nextCommand(), new CommandListContentClose());
147		$this->assertEquals($this->decoratorMock->nextCommand(), new CommandListItemClose());
148		$this->assertEquals($this->decoratorMock->nextCommand(), new CommandListUClose());
149	}
150
151	public function testStandaloneInternalMixedWithTextAreNotIncludes() {
152		$this->decoratorIncluder->listu_open();
153
154		$this->decoratorIncluder->listitem_open(1);
155		$this->decoratorIncluder->listcontent_open();
156		$this->decoratorIncluder->cdata(' ');
157		$this->decoratorIncluder->internallink(DecoratorIncluderTest::LINK2, DecoratorIncluderTest::TITLE2);
158		$this->decoratorIncluder->listcontent_close();
159		$this->decoratorIncluder->listitem_close();
160
161		$this->decoratorIncluder->listitem_open(1);
162		$this->decoratorIncluder->listcontent_open();
163		$this->decoratorIncluder->cdata('Follow the link:');
164		$this->decoratorIncluder->internallink(DecoratorIncluderTest::LINK, DecoratorIncluderTest::TITLE);
165		$this->decoratorIncluder->listcontent_close();
166		$this->decoratorIncluder->listitem_close();
167
168		$this->decoratorIncluder->listu_close();
169
170		$this->assertEquals($this->decoratorMock->nextCommand(), new CommandListUOpen());
171		$this->assertEquals($this->decoratorMock->nextCommand(), new CommandListItemOpen(1));
172		$this->assertEquals($this->decoratorMock->nextCommand(), new CommandListContentOpen());
173		$this->assertEquals($this->decoratorMock->nextCommand(), new CommandCData('Follow the link:'));
174		$this->assertEquals($this->decoratorMock->nextCommand(), new CommandInternalLink(DecoratorIncluderTest::LINK, DecoratorIncluderTest::TITLE));
175		$this->assertEquals($this->decoratorMock->nextCommand(), new CommandListContentClose());
176		$this->assertEquals($this->decoratorMock->nextCommand(), new CommandListItemClose());
177		$this->assertEquals($this->decoratorMock->nextCommand(), new CommandListUClose());
178
179		$this->assertTrue($this->decoratorMock->noCommands(), "Should not have more commands");
180
181		$this->assertEquals($this->includes->count(), 1, "Should have one include");
182		$link = $this->includes->pop();
183		$this->assertEquals($link->getLink(), DecoratorIncluderTest::LINK2, "Link is not as expected");
184		$this->assertEquals($link->getTitle(), DecoratorIncluderTest::TITLE2, "Title is not as expected");
185	}
186
187	public function testCanComputeTheHeadingLevelOfLinks() {
188		$this->decoratorIncluder->header("Any", 3, 0);
189
190		$this->decoratorIncluder->listu_open();
191
192		$this->decoratorIncluder->listitem_open(1);
193		$this->decoratorIncluder->listcontent_open();
194		$this->decoratorIncluder->cdata(' ');
195		$this->decoratorIncluder->internallink(DecoratorIncluderTest::LINK2, DecoratorIncluderTest::TITLE2);
196		$this->decoratorIncluder->listcontent_close();
197		$this->decoratorIncluder->listitem_close();
198
199		$this->decoratorIncluder->listu_close();
200
201		$link = $this->includes->pop();
202		$this->assertEquals($link->getHeadingLevel(), 3, "Heading level is not as expected");
203
204		$this->assertEquals($this->decoratorMock->nextCommand(), new CommandHeader("Any", 3, 0));
205
206		$this->assertTrue($this->decoratorMock->noCommands(), "Should not have more commands");
207	}
208
209	public function testDoesNotInterfereWithEnumeratedLists() {
210		$this->decoratorIncluder->listo_open();
211
212		$this->decoratorIncluder->listitem_open(1);
213		$this->decoratorIncluder->listcontent_open();
214		$this->decoratorIncluder->internallink(DecoratorIncluderTest::LINK, DecoratorIncluderTest::TITLE);
215		$this->decoratorIncluder->listcontent_close();
216		$this->decoratorIncluder->listitem_close();
217
218		$this->decoratorIncluder->listitem_open(1);
219		$this->decoratorIncluder->listcontent_open();
220		$this->decoratorIncluder->cdata('Enumerated List item');
221		$this->decoratorIncluder->listcontent_close();
222		$this->decoratorIncluder->listitem_close();
223
224		$this->decoratorIncluder->listo_close();
225
226		$this->assertEquals($this->decoratorMock->nextCommand(), new CommandListOOpen());
227
228		$this->assertEquals($this->decoratorMock->nextCommand(), new CommandListItemOpen(1));
229		$this->assertEquals($this->decoratorMock->nextCommand(), new CommandListContentOpen());
230		$this->assertEquals($this->decoratorMock->nextCommand(), new CommandInternalLink(DecoratorIncluderTest::LINK, DecoratorIncluderTest::TITLE));
231		$this->assertEquals($this->decoratorMock->nextCommand(), new CommandListContentClose());
232		$this->assertEquals($this->decoratorMock->nextCommand(), new CommandListItemClose());
233
234		$this->assertEquals($this->decoratorMock->nextCommand(), new CommandListItemOpen(1));
235		$this->assertEquals($this->decoratorMock->nextCommand(), new CommandListContentOpen());
236		$this->assertEquals($this->decoratorMock->nextCommand(), new CommandCData('Enumerated List item'));
237		$this->assertEquals($this->decoratorMock->nextCommand(), new CommandListContentClose());
238		$this->assertEquals($this->decoratorMock->nextCommand(), new CommandListItemClose());
239
240		$this->assertEquals($this->decoratorMock->nextCommand(), new CommandListOClose());
241
242		$this->assertTrue($this->decoratorMock->noCommands(), "Should not have more commands");
243		$this->assertEquals($this->includes->count(), 0, "Should not have any include");
244
245
246	}
247
248    public function testIncludesHaveSameLevelCurrentHeader() {
249		$this->decoratorIncluder->header("a", 4, 0);
250
251		$this->decoratorIncluder->listu_open();
252		$this->decoratorIncluder->listitem_open(1);
253		$this->decoratorIncluder->listcontent_open();
254
255		$this->decoratorIncluder->cdata(' ');
256		$this->decoratorIncluder->internallink(DecoratorIncluderTest::LINK, DecoratorIncluderTest::TITLE);
257
258		$this->decoratorIncluder->listcontent_close();
259		$this->decoratorIncluder->listitem_close();
260		$this->decoratorIncluder->listu_close();
261
262		$link = $this->includes->pop();
263
264		$this->assertEquals(4, $link->getHeadingLevel(),  "Heading level");
265    }
266
267    public function testIncludesHaveLevelNeverLessThan2() {
268		$this->decoratorIncluder->listu_open();
269		$this->decoratorIncluder->listitem_open(1);
270		$this->decoratorIncluder->listcontent_open();
271
272		$this->decoratorIncluder->cdata(' ');
273		$this->decoratorIncluder->internallink(DecoratorIncluderTest::LINK, DecoratorIncluderTest::TITLE);
274
275		$this->decoratorIncluder->listcontent_close();
276		$this->decoratorIncluder->listitem_close();
277		$this->decoratorIncluder->listu_close();
278
279		$link = $this->includes->pop();
280
281		$this->assertEquals(2, $link->getHeadingLevel(),  "Heading level");
282    }
283}
284?>
285