ILIAS  release_7 Revision v7.30-3-g800a261c036
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 = '<ul id="id_1" class="il-tree" role="tree" aria-label="label">' . $this->getInnerTreePart() . '</ul>';
88
89 $this->assertEquals(
90 $this->brutallyTrimHTML($expected),
91 $this->brutallyTrimHTML($html)
92 );
93 }
94
95 public function testRenderingAsSubTree()
96 {
97 $r = $this->getDefaultRenderer();
98 $html = $r->render($this->tree->withIsSubTree(true));
99
100 $expected = $this->getInnerTreePart();
101
102 $this->assertEquals(
103 $this->brutallyTrimHTML($expected),
104 $this->brutallyTrimHTML($html)
105 );
106 }
107
108 protected function getInnerTreePart()
109 {
110 return '<li id="" class="il-tree-node node-simple expandable" role="treeitem" aria-expanded="false">
111 <span class="node-line"><span class="node-label">1</span></span>
112
113 <ul role="group">
114 <li id="" class="il-tree-node node-simple" role="treeitem">
115 <span class="node-line"><span class="node-label">1.1</span></span>
116 </li>
117 <li id="" class="il-tree-node node-simple expandable" role="treeitem" aria-expanded="false">
118 <span class="node-line"><span class="node-label">1.2</span></span>
119
120 <ul role="group">
121 <li id="" class="il-tree-node node-simple" role="treeitem">
122 <span class="node-line"><span class="node-label">1.2.1</span></span>
123 </li>
124 </ul>
125 </li>
126 </ul>
127 </li>
128 <li id="" class="il-tree-node node-simple" role="treeitem">
129 <span class="node-line"><span class="node-label">2</span></span>
130 </li>';
131 }
132}
simple()
Definition: simple.php:5
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:263
getDefaultRenderer(JavaScriptBinding $js_binding=null, $with_stub_renderings=[])
Definition: Base.php:311
build(C\Tree\Node\Factory $factory, $record, $environment=null)
getChildren($record, $environment=null)
$factory
Definition: metadata.php:58
$data
Definition: storeScorm.php:23