ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
ExpandableTreeTest.php
Go to the documentation of this file.
1<?php
2
3/* Copyright (c) 2019 Nils Haagen <nils.haagen@concepts-and-training.de> Extended GPL, see docs/LICENSE */
4
5require_once("libs/composer/vendor/autoload.php");
6require_once(__DIR__ . "../../../Base.php");
7
8use \ILIAS\UI\Component as C;
9use \ILIAS\UI\Implementation\Component as I;
10
11class DataNode
12{
13 public function __construct(string $label, array $children = [])
14 {
15 $this->label = $label;
16 $this->children = $children;
17 }
18 public function getLabel()
19 {
20 return $this->label;
21 }
22 public function getChildren()
23 {
24 return $this->children;
25 }
26}
27
29{
30 public function getChildren($record, $environment = null) : array
31 {
32 return $record->getChildren();
33 }
34
35 public function build(
36 C\Tree\Node\Factory $factory,
37 $record,
38 $environment = null
39 ) : C\Tree\Node\Node {
40 return $factory->simple($record->getLabel());
41 }
42}
43
48{
49 public function getUIFactory()
50 {
51 $factory = new class extends NoUIFactory {
52 public function tree()
53 {
54 return new I\Tree\Factory();
55 }
56 };
57 return $factory;
58 }
59
60 public function setUp() : void
61 {
62 $n11 = new DataNode('1.1');
63 $n12 = new DataNode('1.2', array(new DataNode('1.2.1')));
64 $n1 = new DataNode('1', [$n11, $n12]);
65 $n2 = new DataNode('2');
66 $data = [$n1, $n2];
67
68 $label = "label";
69 $recursion = new Recursion();
70 $f = $this->getUIFactory();
71 $this->tree = $f->tree()->expandable($label, $recursion)
72 ->withData($data);
73 }
74
75 public function brutallyTrimHTML($html)
76 {
77 $html = str_replace(["\n", "\r", "\t"], "", $html);
78 $html = preg_replace('# {2,}#', " ", $html);
79 return trim($html);
80 }
81
82 public function testRendering()
83 {
84 $r = $this->getDefaultRenderer();
85 $html = $r->render($this->tree);
86
87 $expected = <<<EOT
88 <ul id="id_1" class="il-tree" role="tree" aria-label="label">
89 <li id="" class="il-tree-node node-simple expandable" role="treeitem" aria-expanded="false">
90 <span class="node-line"><span class="node-label">1</span></span>
91
92 <ul role="group">
93 <li id="" class="il-tree-node node-simple" role="none">
94 <span class="node-line"><span class="node-label">1.1</span></span>
95 </li>
96 <li id="" class="il-tree-node node-simple expandable" role="treeitem" aria-expanded="false">
97 <span class="node-line"><span class="node-label">1.2</span></span>
98
99 <ul role="group">
100 <li id="" class="il-tree-node node-simple" role="none">
101 <span class="node-line"><span class="node-label">1.2.1</span></span>
102 </li>
103 </ul>
104 </li>
105 </ul>
106 </li>
107 <li id="" class="il-tree-node node-simple" role="none">
108 <span class="node-line"><span class="node-label">2</span></span>
109 </li>
110 </ul>
111EOT;
112
113 $this->assertEquals(
114 $this->brutallyTrimHTML($expected),
115 $this->brutallyTrimHTML($html)
116 );
117 }
118}
An exception for terminatinating execution or to throw for unit testing.
__construct(string $label, array $children=[])
Tests for the Expandable Tree.
brutallyTrimHTML($html)
A more radical version of normalizeHTML.
Provides common functionality for UI tests.
Definition: Base.php:225
getDefaultRenderer(JavaScriptBinding $js_binding=null)
Definition: Base.php:268
build(C\Tree\Node\Factory $factory, $record, $environment=null)
getChildren($record, $environment=null)
$factory
Definition: metadata.php:58
simple()
Definition: simple.php:2
$data
Definition: storeScorm.php:23