ILIAS  trunk Revision v11.0_alpha-1702-gfd3ecb7f852
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
PCDataTableTest.php
Go to the documentation of this file.
1 <?php
2 
20 
25 {
26  public function testConstruction(): void
27  {
28  $page = $this->getEmptyPageWithDom();
29  $pc = new ilPCDataTable($page);
30  $this->assertEquals(
31  ilPCDataTable::class,
32  get_class($pc)
33  );
34  }
35 
36  public function testCreate(): void
37  {
38  $page = $this->getEmptyPageWithDom();
39  $pc = new ilPCDataTable($page);
40  $pc->create($page, "pg");
41  $this->assertXmlEquals(
42  '<PageObject HierId="pg"><PageContent><Table Language="" DataTable="y"></Table></PageContent></PageObject>',
43  $page->getXMLFromDom()
44  );
45  }
46 
47  public function testData(): void
48  {
49  $page = $this->getEmptyPageWithDom();
50  $pc = new ilPCDataTable($page);
51  $pc->create($page, "pg");
52  $row1 = $pc->addRow();
53  $pc->addCell($row1, "one", "en");
54  $pc->addCell($row1, "two", "en");
55  $row2 = $pc->addRow();
56  $pc->addCell($row2, "three", "en");
57  $pc->addCell($row2, "four", "en");
58 
59  $expected = <<<EOT
60 <PageObject HierId="pg"><PageContent><Table Language="" DataTable="y">
61 <TableRow>
62 <TableData>
63 <PageContent><Paragraph Language="en" Characteristic="TableContent">one</Paragraph></PageContent>
64 </TableData>
65 <TableData>
66 <PageContent><Paragraph Language="en" Characteristic="TableContent">two</Paragraph></PageContent>
67 </TableData></TableRow>
68 <TableRow><TableData>
69 <PageContent><Paragraph Language="en" Characteristic="TableContent">three</Paragraph></PageContent>
70 </TableData>
71 <TableData>
72 <PageContent><Paragraph Language="en" Characteristic="TableContent">four</Paragraph></PageContent>
73 </TableData></TableRow></Table></PageContent></PageObject>
74 EOT;
75 
76  $this->assertXmlEquals(
77  $expected,
78  $page->getXMLFromDom()
79  );
80 
81  $page->addHierIDs();
82  $pc->setHierId("1");
83 
84  $pc->setData([
85  ["five", "six"],
86  ["seven", "eight"]
87  ]);
88 
89  $expected = <<<EOT
90 <PageObject HierId="pg"><PageContent HierId="1"><Table Language="" DataTable="y"><TableRow HierId="1_1"><TableData HierId="1_1_1"><PageContent HierId="1_1_1_1"><Paragraph Language="en" Characteristic="TableContent">five</Paragraph></PageContent></TableData><TableData HierId="1_1_2"><PageContent HierId="1_1_2_1"><Paragraph Language="en" Characteristic="TableContent">six</Paragraph></PageContent></TableData></TableRow><TableRow HierId="1_2"><TableData HierId="1_2_1"><PageContent HierId="1_2_1_1"><Paragraph Language="en" Characteristic="TableContent">seven</Paragraph></PageContent></TableData><TableData HierId="1_2_2"><PageContent HierId="1_2_2_1"><Paragraph Language="en" Characteristic="TableContent">eight</Paragraph></PageContent></TableData></TableRow></Table></PageContent></PageObject>
91 EOT;
92 
93  $this->assertXmlEquals(
94  $expected,
95  $page->getXMLFromDom()
96  );
97  }
98 
99  public function testEmptyCell(): void
100  {
101  $page = $this->getEmptyPageWithDom();
102  $pc = new ilPCDataTable($page);
103  $pc->create($page, "pg");
104  $pc->setLanguage("en");
105  $row1 = $pc->addRow();
106  $pc->addCell($row1, "one", "en");
107  $page->addHierIDs();
108  $pc->setHierId("1");
109  // note: this gives us the paragraph node in the cell
110  $cell = $pc->getCellNode(0, 0);
111  $pc->makeEmptyCell($cell->parentNode->parentNode);
112  $page->stripHierIDs();
113  $page->validateDom();
114 
115  $expected = <<<EOT
116 <PageObject><PageContent><Table Language="en" DataTable="y">
117 <TableRow>
118 <TableData>
119 <PageContent><Paragraph Characteristic="TableContent" Language="en"></Paragraph></PageContent>
120 </TableData>
121 </TableRow>
122 </Table></PageContent></PageObject>
123 EOT;
124 
125  $this->assertXmlEquals(
126  $expected,
127  $page->getXMLFromDom()
128  );
129  }
130 
131  public function testCellText(): void
132  {
133  $page = $this->getEmptyPageWithDom();
134  $pc = new ilPCDataTable($page);
135  $pc->create($page, "pg");
136  $pc->setLanguage("en");
137  $row1 = $pc->addRow();
138  $pc->addCell($row1, "one", "en");
139  $page->addHierIDs();
140  $pc->setHierId("1");
141 
142  // note: this gives us the paragraph node in the cell
143  $text = $pc->getCellText(0, 0);
144 
145  $this->assertEquals(
146  "one",
147  $text
148  );
149  }
150 
151  public function testAddRows(): void
152  {
153  $page = $this->getEmptyPageWithDom();
154  $pc = new ilPCDataTable($page);
155  $pc->create($page, "pg");
156  $pc->setLanguage("en");
157  $pc->addRows(1, 2);
158  $page->validateDom();
159 
160  $expected = <<<EOT
161 <PageObject><PageContent><Table Language="en" DataTable="y"><TableRow><TableData></TableData><TableData></TableData></TableRow></Table></PageContent></PageObject>
162 EOT;
163 
164  $this->assertXmlEquals(
165  $expected,
166  $page->getXMLFromDom()
167  );
168  }
169 
170  public function testImportSpreadsheet(): void
171  {
172  $page = $this->getEmptyPageWithDom();
173  $pc = new ilPCDataTable($page);
174  $pc->create($page, "pg");
175  $pc->setLanguage("en");
176  $pc->importSpreadsheet("en", "one\ttwo\nthree\tfour");
177  $page->validateDom();
178 
179  $expected = <<<EOT
180 <PageObject><PageContent><Table Language="en" DataTable="y"><TableRow><TableData><PageContent><Paragraph Language="en" Characteristic="TableContent">one</Paragraph></PageContent></TableData><TableData><PageContent><Paragraph Language="en" Characteristic="TableContent">two</Paragraph></PageContent></TableData></TableRow><TableRow><TableData><PageContent><Paragraph Language="en" Characteristic="TableContent">three</Paragraph></PageContent></TableData><TableData><PageContent><Paragraph Language="en" Characteristic="TableContent">four</Paragraph></PageContent></TableData></TableRow></Table></PageContent></PageObject>
181 EOT;
182 
183  $this->assertXmlEquals(
184  $expected,
185  $page->getXMLFromDom()
186  );
187  }
188 
189  public function testLanguage(): void
190  {
191  $page = $this->getEmptyPageWithDom();
192  $pc = new ilPCDataTable($page);
193  $pc->create($page, "pg");
194  $pc->setLanguage("fr");
195  $pc->addRows(1, 1);
196  $page->validateDom();
197  $this->assertEquals(
198  "fr",
199  $pc->getLanguage()
200  );
201  }
202 
203  public function testWidth(): void
204  {
205  $page = $this->getEmptyPageWithDom();
206  $pc = new ilPCDataTable($page);
207  $pc->create($page, "pg");
208  $pc->setLanguage("en");
209  $pc->addRows(1, 1);
210  $page->validateDom();
211  $pc->setWidth("200");
212  $this->assertEquals(
213  "200",
214  $pc->getWidth()
215  );
216  }
217 
218  public function testAlign(): void
219  {
220  $page = $this->getEmptyPageWithDom();
221  $pc = new ilPCDataTable($page);
222  $pc->create($page, "pg");
223  $pc->setLanguage("en");
224  $pc->addRows(1, 1);
225  $page->validateDom();
226  $pc->setHorizontalAlign("Right");
227  $this->assertEquals(
228  "Right",
229  $pc->getHorizontalAlign()
230  );
231  }
232 
233  public function testTDWidth(): void
234  {
235  $page = $this->getEmptyPageWithDom();
236  $pc = new ilPCDataTable($page);
237  $pc->create($page, "pg");
238  $pc->setLanguage("en");
239  $pc->addRows(1, 1);
240  $page->addHierIDs();
241  $pc->setTDWidth("1_1_1", "33");
242  $this->assertEquals(
243  [
244  "1_1_1:" => "33"
245  ],
246  $pc->getAllCellWidths()
247  );
248  }
249 
250  public function testSpans(): void
251  {
252  $page = $this->getEmptyPageWithDom();
253  $pc = new ilPCDataTable($page);
254  $pc->create($page, "pg");
255  $pc->setLanguage("en");
256  $pc->addRows(1, 2);
257  $page->addHierIDs();
258  $pc->setTdSpans(
259  ["1_1_1:" => "2", "1_1_2:" => "1"],
260  ["1_1_1:" => "1", "1_1_2:" => "1"],
261  );
262 
263  $spans = $pc->getAllCellSpans();
264 
265  $this->assertEquals(
266  "2",
267  $spans["1_1_1:"]["colspan"]
268  );
269  $this->assertEquals(
270  "",
271  $spans["1_1_1:"]["rowspan"]
272  );
273  $this->assertEquals(
274  "",
275  $spans["1_1_2:"]["colspan"]
276  );
277  $this->assertEquals(
278  "",
279  $spans["1_1_2:"]["rowspan"]
280  );
281  }
282 
283  public function testCellHidden(): void
284  {
285  $page = $this->getEmptyPageWithDom();
286  $pc = new ilPCDataTable($page);
287  $pc->create($page, "pg");
288  $colspans = [
289  0 => [0 => 2, 1 => 1]
290  ];
291  $rowspans = [
292  0 => [0 => 1, 1 => 1]
293  ];
294  $h1 = $pc->checkCellHidden(
295  $colspans,
296  $rowspans,
297  0,
298  0,
299  );
300  $h2 = $pc->checkCellHidden(
301  $colspans,
302  $rowspans,
303  1,
304  0,
305  );
306 
307  $this->assertEquals(
308  false,
309  $h1
310  );
311  $this->assertEquals(
312  true,
313  $h2
314  );
315  }
316 
317  public function testTDClass(): void
318  {
319  $page = $this->getEmptyPageWithDom();
320  $pc = new ilPCDataTable($page);
321  $pc->create($page, "pg");
322  $pc->setLanguage("en");
323  $pc->addRows(1, 1);
324  $page->addHierIDs();
325  $pc->setTDClass("1_1_1", "MyClass");
326  $this->assertEquals(
327  [
328  "1_1_1:" => "MyClass"
329  ],
330  $pc->getAllCellClasses()
331  );
332  }
333 
334  public function testTDAlignment(): void
335  {
336  $page = $this->getEmptyPageWithDom();
337  $pc = new ilPCDataTable($page);
338  $pc->create($page, "pg");
339  $pc->setLanguage("en");
340  $pc->addRows(1, 1);
341  $page->addHierIDs();
342  $pc->setTDAlignment("1_1_1", "Right");
343  $this->assertEquals(
344  [
345  "1_1_1:" => "Right"
346  ],
347  $pc->getAllCellAlignments()
348  );
349  }
350 
351  public function testCaption(): void
352  {
353  $page = $this->getEmptyPageWithDom();
354  $pc = new ilPCDataTable($page);
355  $pc->create($page, "pg");
356  $pc->setLanguage("en");
357  $pc->addRows(1, 1);
358  $page->addHierIDs();
359  $pc->setHierId("1");
360  $pc->setCaption("Moin", "Top");
361  $this->assertEquals(
362  "Moin",
363  $pc->getCaption()
364  );
365  $this->assertEquals(
366  "Top",
367  $pc->getCaptionAlign()
368  );
369  }
370 
371  public function testFirstRowStyle(): void
372  {
373  $page = $this->getEmptyPageWithDom();
374  $pc = new ilPCDataTable($page);
375  $pc->create($page, "pg");
376  $pc->setLanguage("en");
377  $pc->addRows(2, 2);
378  $page->addHierIDs();
379  $pc->setFirstRowStyle("MyClass");
380  $this->assertEquals(
381  [
382  '1_1_1:' => 'MyClass',
383  '1_1_2:' => 'MyClass',
384  '1_2_1:' => '',
385  '1_2_2:' => ''
386  ],
387  $pc->getAllCellClasses()
388  );
389  }
390 
391  public function testClass(): void
392  {
393  $page = $this->getEmptyPageWithDom();
394  $pc = new ilPCDataTable($page);
395  $pc->create($page, "pg");
396  $pc->setLanguage("en");
397  $pc->addRows(1, 1);
398  $page->addHierIDs();
399  $pc->setHierId("1");
400  $pc->setClass("MyClass");
401  $this->assertEquals(
402  "MyClass",
403  $pc->getClass()
404  );
405  }
406 
407  public function testTemplate(): void
408  {
409  $page = $this->getEmptyPageWithDom();
410  $pc = new ilPCDataTable($page);
411  $pc->create($page, "pg");
412  $pc->setLanguage("en");
413  $pc->addRows(1, 1);
414  $page->addHierIDs();
415  $pc->setHierId("1");
416  $pc->setTemplate("MyTemplate");
417  $this->assertEquals(
418  "MyTemplate",
419  $pc->getTemplate()
420  );
421  }
422 
423  public function testHeaderRows(): void
424  {
425  $page = $this->getEmptyPageWithDom();
426  $pc = new ilPCDataTable($page);
427  $pc->create($page, "pg");
428  $pc->setHeaderRows(2);
429  $this->assertEquals(
430  2,
431  $pc->getHeaderRows()
432  );
433  }
434 
435  public function testFooterRows(): void
436  {
437  $page = $this->getEmptyPageWithDom();
438  $pc = new ilPCDataTable($page);
439  $pc->create($page, "pg");
440  $pc->setFooterRows(2);
441  $this->assertEquals(
442  2,
443  $pc->getFooterRows()
444  );
445  }
446 
447  public function testHeaderCols(): void
448  {
449  $page = $this->getEmptyPageWithDom();
450  $pc = new ilPCDataTable($page);
451  $pc->create($page, "pg");
452  $pc->setHeaderCols(2);
453  $this->assertEquals(
454  2,
455  $pc->getHeaderCols()
456  );
457  }
458 
459  public function testFooterCols(): void
460  {
461  $page = $this->getEmptyPageWithDom();
462  $pc = new ilPCDataTable($page);
463  $pc->create($page, "pg");
464  $pc->setFooterCols(2);
465  $this->assertEquals(
466  2,
467  $pc->getFooterCols()
468  );
469  }
470 
471  public function testModel(): void
472  {
473  $page = $this->getEmptyPageWithDom();
474  $pc = new ilPCDataTable($page);
475  $pc->create($page, "pg");
476  $pc->setLanguage("en");
477  $pc->importSpreadsheet("en", "one\ttwo\nthree\tfour");
478  $page->addHierIDs();
479  $pc->setHierId("1");
480  //$page->validateDom();
481 
482  $expected = new \stdClass();
483  $expected->content = [
484  0 => [
485  0 => "one",
486  1 => "two"
487  ],
488  1 => [
489  0 => "three",
490  1 => "four"
491  ]
492  ];
493  $expected->characteristic = '';
494  $expected->template = '';
495  $expected->hasHeaderRows = false;
496 
497  $this->assertEquals(
498  $expected,
499  $pc->getModel()
500  );
501  }
502 }
assertXmlEquals(string $expected_xml_as_string, string $html_xml_string)
Class ilPCDataTable.