ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
AbstractRendererTest.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22  require_once("vendor/composer/vendor/autoload.php");
23 
28 
30  {
31  public function render(Component $component, Renderer $default_renderer): string
32  {
33  }
34 
35  public function _getTemplate(string $a, bool $b, bool $c): Template
36  {
37  return $this->getTemplate($a, $b, $c);
38  }
39  }
40 
42  {
43  public array $ids = array();
44 
45  public function render(Component $component, Renderer $default_renderer): string
46  {
47  $this->ids[] = $this->bindJavaScript($component);
48  return "";
49  }
50  }
51 }
52 
58 
60  {
61  public function render(Component $component, Renderer $default_renderer): string
62  {
63  }
64 
65  public function _getTemplate(string $a, bool $b, bool $c): Template
66  {
67  return $this->getTemplate($a, $b, $c);
68  }
69  }
70 }
71 
72 namespace {
89  require_once(__DIR__ . "/../Base.php");
90 
91  use ILIAS\UI\Component as C;
102 
103  class NullTemplate implements Template
104  {
105  public function setCurrentBlock(string $name): bool
106  {
107  return true;
108  }
109 
110  public function parseCurrentBlock(): bool
111  {
112  return true;
113  }
114 
115  public function touchBlock(string $name): bool
116  {
117  return true;
118  }
119 
120  public function setVariable(string $name, $value): void
121  {
122  }
123 
124  public function get(string $block = null): string
125  {
126  return "";
127  }
128 
129  public function addOnLoadCode(string $code): void
130  {
131  }
132  }
133 
135  {
136  public array $files = array();
137 
138  public function getTemplate(string $path, bool $purge_unfilled_vars, bool $purge_unused_blocks): Template
139  {
140  $file_name = realpath(__DIR__ . "/../../../../../" . $path);
141  $this->files[$file_name] = array($purge_unfilled_vars, $purge_unused_blocks);
142 
143  if (!file_exists($file_name)) {
144  throw new InvalidArgumentException();
145  }
146 
147  return new NullTemplate();
148  }
149  }
150 
151  class NullDefaultRenderer implements Renderer
152  {
153  public function render($component, ?Renderer $root = null)
154  {
155  return "";
156  }
157  public function renderAsync($component, ?Renderer $root = null)
158  {
159  return '';
160  }
161  }
162 
164  {
167  protected Language $lng;
174 
175  public function setUp(): void
176  {
177  parent::setUp();
178  $this->tpl_factory = new TemplateFactoryMock();
179  $this->ui_factory = $this->getUIFactory(); //new NoUIFactory();
180  $this->lng = new LanguageMock();
181  $this->js_binding = new LoggingJavaScriptBinding();
182  $this->image_path_resolver = $this->getMockBuilder(ILIAS\UI\Implementation\Render\ImagePathResolver::class)
183  ->getMock();
184  $this->help_text_retriever = $this->createMock(ILIAS\UI\HelpTextRetriever::class);
185  }
186 
187  public function testGetTemplateSuccessfull(): void
188  {
190  $this->ui_factory,
191  $this->tpl_factory,
192  $this->lng,
193  $this->js_binding,
194  $this->image_path_resolver,
195  $this->getDataFactory(),
196  $this->help_text_retriever,
197  $this->getUploadLimitResolver()
198  );
199  $r->_getTemplate("tpl.glyph.html", true, false);
200 
201  $expected = array(realpath(__DIR__ . "/../../../../../components/ILIAS/UI/src/templates/default/Symbol/tpl.glyph.html")
202  => array(true, false)
203  );
204 
205  $this->assertEquals($expected, $this->tpl_factory->files);
206  }
207 
208  public function testGetTemplateUnsuccessfull(): void
209  {
211  $this->ui_factory,
212  $this->tpl_factory,
213  $this->lng,
214  $this->js_binding,
215  $this->image_path_resolver,
216  $this->getDataFactory(),
217  $this->help_text_retriever,
218  $this->getUploadLimitResolver()
219  );
220 
221  $this->expectException(TypeError::class);
222  $r->_getTemplate("tpl.counter_foo.html", true, false);
223 
224  $expected = array(realpath(__DIR__ . "/../../components/ILIAS/UI/src/templates/default/Counter/tpl.counter_foo.html")
225  => array(true, false)
226  );
227  $this->assertEquals($expected, $this->tpl_factory->files);
228  }
229 
230  public function testBindJavaScriptSuccessfull(): void
231  {
233  $this->ui_factory,
234  $this->tpl_factory,
235  $this->lng,
236  $this->js_binding,
237  $this->image_path_resolver,
238  $this->getDataFactory(),
239  $this->help_text_retriever,
240  $this->getUploadLimitResolver()
241  );
242 
243  $g = new Glyph(C\Symbol\Glyph\Glyph::SETTINGS, "aria_label");
244 
245  $ids = array();
246  $g = $g->withOnLoadCode(function ($id) use (&$ids) {
247  $ids[] = $id;
248  return "ID: $id";
249  });
250  $r->render($g, new NullDefaultRenderer());
251 
252  $this->assertEquals($this->js_binding->ids, $ids);
253  $this->assertEquals(array("id_1"), $ids);
254  $this->assertEquals(array("ID: id_1"), $this->js_binding->on_load_code);
255  }
256 
257  public function testBindJavaScriptNoString(): void
258  {
260  $this->ui_factory,
261  $this->tpl_factory,
262  $this->lng,
263  $this->js_binding,
264  $this->image_path_resolver,
265  $this->getDataFactory(),
266  $this->help_text_retriever,
267  $this->getUploadLimitResolver()
268  );
269 
270  $g = new Glyph(C\Symbol\Glyph\Glyph::SETTINGS, "aria_label");
271 
272  $g = $g->withOnLoadCode(function ($id) {
273  return null;
274  });
275 
276  try {
277  $r->render($g, new NullDefaultRenderer());
278  $this->assertFalse("This should not happen...");
279  } catch (LogicException $e) {
280  $this->assertTrue(true);
281  }
282  }
283  }
284 }
LoggingJavaScriptBinding $js_binding
TemplateFactoryMock $tpl_factory
renderAsync($component, ?Renderer $root=null)
Same as render, except that this version also returns any javascript code bound to the on load event...
touchBlock(string $name)
Touch a block without working further on it.
Interface Observer Contains several chained tasks and infos about them.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
render($component, ?Renderer $root=null)
Render given component.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$c
Definition: deliver.php:9
$path
Definition: ltiservices.php:30
setVariable(string $name, $value)
Set a variable in the current block.
render(Component $component, Renderer $default_renderer)
addOnLoadCode(string $code)
Add some javascript to be executed on_load of the rendered page.
getTemplate(string $name, bool $purge_unfilled_vars, bool $purge_unused_blocks)
Get template of component this renderer is made for.
ILIAS UI HelpTextRetriever $help_text_retriever
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: Counter.php:21
getTemplate(string $path, bool $purge_unfilled_vars, bool $purge_unused_blocks)
Get template instance.
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:24
setCurrentBlock(string $name)
Set the block to work on.
$a
thx to https://mlocati.github.io/php-cs-fixer-configurator for the examples
parseCurrentBlock()
Parse the block that is currently worked on.
Interface for a factory that provides templates.
$r
bindJavaScript(JavaScriptBindable $component)
Bind the component to JavaScript.