ILIAS  trunk Revision v11.0_alpha-1723-g8e69f309bab
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
DefaultRendererTest.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
21 require_once(__DIR__ . "/TestComponent.php");
22 require_once(__DIR__ . "/../Base.php");
23 
30 use ILIAS\UI\Component as C;
36 
38 {
41 
42  public function testGetRendererSuccessfully(): void
43  {
44  // There should be a renderer for Glyph...
45  $dr = $this->getDefaultRenderer();
46  $r = $dr->_getRendererFor(new Glyph("up", "up"));
47  $this->assertInstanceOf(ComponentRenderer::class, $r);
48  }
49 
50  public function testGetRendererCaching(): void
51  {
52  $dr = $this->getDefaultRenderer();
53  $r1 = $dr->_getRendererFor(new Glyph("up", "up"));
54  $r2 = $dr->_getRendererFor(new Glyph("up", "up"));
55  $this->assertTrue($r1 === $r2, "Instances not equal");
56  }
57 
59  {
60  $this->resource_registry = parent::getResourceRegistry();
62  }
63 
64  public function testInvokesRegistry(): void
65  {
66  $dr = $this->getDefaultRenderer();
67  $component = new TestComponent("foo");
68 
69  $dr->render($component);
70 
71  $this->assertEquals(array("test.js"), $this->resource_registry->resources);
72  }
73 
78  public function testRenderingChainAndContextStack(): void
79  {
80  $component1 = new TestComponent("component1");
81  $component2 = new TestComponent("component2");
82  $component3 = new TestComponent("component3");
83  $glue = '.';
84 
85  $component_renderer = $this->createMock(ComponentRenderer::class);
86  $component_renderer->method('render')->willReturnCallback(
87  static function (TestComponent $component, DefaultRenderer $renderer) use (
88  $component1,
89  $component2,
90  $component3,
91  $glue
92  ) {
93  return match ($component->text) {
94  $component1->text => $component1->text . $glue . $renderer->render($component2),
95  $component2->text => $component2->text . $glue . $renderer->render($component3),
96  $component3->text => $component3->text,
97  };
98  }
99  );
100 
101  $renderer = new class (
102  $this->createMock(Render\Loader::class),
103  $this->createMock(JavaScriptBinding::class),
104  $this->createMock(\ILIAS\Language\Language::class),
106  ) extends DefaultRenderer {
107  public array $context_history = [];
108 
109  public function __construct(
110  Render\Loader $component_renderer_loader,
111  JavaScriptBinding $java_script_binding,
112  \ILIAS\Language\Language $language,
113  protected ComponentRenderer $component_renderer
114  ) {
115  parent::__construct($component_renderer_loader, $java_script_binding, $language);
116  }
117 
118  protected function getRendererFor(Component $component): ComponentRenderer
119  {
121  }
122 
123  protected function pushContext(Component $component): void
124  {
125  parent::pushContext($component);
126  $this->context_history[] = $this->getContexts();
127  }
128 
129  protected function popContext(): void
130  {
131  parent::popContext();
132  $this->context_history[] = $this->getContexts();
133  }
134  };
135 
136  $expected_context_history = [
137  [$component1],
138  [$component1, $component2],
139  [$component1, $component2, $component3],
140  [$component1, $component2],
141  [$component1],
142  []
143  ];
144 
145  $expected_html =
146  $component1->text . $glue .
147  $component2->text . $glue .
148  $component3->text;
149 
150  $html = $renderer->render($component1);
151 
152  $this->assertEquals($expected_context_history, $renderer->context_history);
153  $this->assertEquals($expected_html, $html);
154  }
155 
156  public function testRender(): void
157  {
158  $c1 = new TestComponent("foo");
159  $renderer = $this->getDefaultRenderer();
160  $html = $renderer->render($c1);
161  $this->assertEquals("foo", $html);
162  }
163 
164  public function testRenderAsyncNoJs(): void
165  {
166  $c1 = new TestComponent("foo");
167  $renderer = $this->getDefaultRenderer(
169  $this->getTemplateFactory()->getTemplate("tpl.main.html", false, false)
170  )
171  );
172  $html = $renderer->renderAsync($c1);
173  $this->assertEquals("foo", $html);
174  }
175 
176  public function testRenderAsyncWithJs(): void
177  {
178  $c1 = new JSTestComponent("foo");
179  $renderer = $this->getDefaultRenderer(
180  new ilJavaScriptBinding($this->getTemplateFactory()->getTemplate("tpl.main.html", false, false))
181  );
182  $html = $renderer->renderAsync($c1);
183  $this->assertEquals('foo<script data-replace-marker="script">id:foo.id content:foo</script>', $html);
184  }
185 
186  public function testRenderAsyncWithJsTwice(): void
187  {
188  $c1 = new TestComponent("foo");
189  $c2 = new JSTestComponent("foo");
190  $renderer = $this->getDefaultRenderer(
191  new ilJavaScriptBinding($this->getTemplateFactory()->getTemplate("tpl.main.html", false, false))
192  );
193  $html = $renderer->renderAsync($c2);
194  $this->assertEquals('foo<script data-replace-marker="script">id:foo.id content:foo</script>', $html);
195  $html = $renderer->renderAsync($c1);
196  $this->assertEquals("foo", $html);
197  $html = $renderer->renderAsync($c2);
198  $this->assertEquals('foo<script data-replace-marker="script">id:foo.id content:foo</script>', $html);
199  }
200 
201  public function testRenderAsyncArray(): void
202  {
203  $c1 = new TestComponent("foo");
204 
205  $renderer = $this->getDefaultRenderer(
206  new ilJavaScriptBinding($this->getTemplateFactory()->getTemplate("tpl.main.html", false, false))
207  );
208  $html = $renderer->renderAsync([$c1,$c1]);
209  $this->assertEquals('foofoo', $html);
210  }
211 
215  public function testPassesSelfAsRootIfNoRootExist($render_type)
216  {
217  $this->component_renderer = $this->createMock(ComponentRenderer::class);
218  $component = $this->createMock(C\Component::class);
219 
220  $loader = $this->createMock(Loader::class);
221  $loader->method('getRendererFor')->willReturn($this->component_renderer);
222 
223  $renderer = new TestDefaultRenderer($loader, $this->getJavaScriptBinding(), $this->getLanguage());
224 
225  $this->component_renderer->expects($this->once())
226  ->method("render")
227  ->with($component, $renderer);
228 
229  $renderer->$render_type($component);
230  }
231 
235  public function testPassesOtherOnAsRoot($render_type)
236  {
237  $this->component_renderer = $this->createMock(ComponentRenderer::class);
238  $component = $this->createMock(C\Component::class);
239  $root = $this->createMock(Renderer::class);
240 
241  $loader = $this->createMock(Loader::class);
242  $loader->method('getRendererFor')->willReturn($this->component_renderer);
243 
244  $renderer = new TestDefaultRenderer($loader, $this->getJavaScriptBinding(), $this->getLanguage());
245 
246  $this->component_renderer->expects($this->once())
247  ->method("render")
248  ->with($component, $root);
249 
250  $renderer->$render_type($component, $root);
251  }
252 
253  public static function getRenderType()
254  {
255  return [
256  ["render"],
257  ["renderAsync"]
258  ];
259  }
260 
262  {
263  $component = $this->createMock(C\Component::class);
264  $root = $this->createMock(Renderer::class);
265 
266  $renderer = $this->getDefaultRenderer();
267 
268  $root->expects($this->exactly(2))
269  ->method("render")
270  ->with($component)
271  ->willReturn(".");
272 
273  $res = $renderer->render([$component, $component], $root);
274  $this->assertEquals("..", $res);
275  }
276 }
render($component, ?Renderer $root=null)
Render given component.If an array of components is passed, this method returns a concatenated output...
$res
Definition: ltiservices.php:66
$renderer
Interface Observer Contains several chained tasks and infos about them.
An entity that renders components to a string output.
Wraps global ilTemplate to provide JavaScriptBinding.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
testRenderingChainAndContextStack()
Simulates the rendering chain and tests if the contexts are properly stacked.
ComponentRenderer $component_renderer
getLanguage()
Provides methods to interface with javascript.
testPassesSelfAsRootIfNoRootExist($render_type)
getRenderType
LoggingRegistry $resource_registry
testPassesOtherOnAsRoot($render_type)
getRenderType
__construct(Container $dic, ilPlugin $plugin)
Loads renderers for components.
Definition: Loader.php:29
$r