ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
AbstractRendererTest.php
Go to the documentation of this file.
1 <?php
2 
3 /* Copyright (c) 2016 Richard Klees <richard.klees@concepts-and-training.de> Extended GPL, see docs/LICENSE */
4 
6  require_once("libs/composer/vendor/autoload.php");
7 
8  use \ILIAS\UI\Component\Component;
9  use \ILIAS\UI\Renderer;
10  use \ILIAS\UI\Implementation\Render\AbstractComponentRenderer;
12  public function render(Component $component, Renderer $default_renderer) {
13  }
14  public function _getTemplate($a, $b, $c) {
15  return $this->getTemplate($a, $b, $c);
16  }
17  protected function getComponentInterfaceName() {
18  return "\\ILIAS\\UI\\Component\\Glyph\\Glyph";
19  }
20  }
21 
23  public $ids = array();
24  public function render(Component $component, Renderer $default_renderer) {
25  $this->ids[] = $this->bindJavaScript($component);
26  return "";
27  }
28  }
29 }
30 
32  use \ILIAS\UI\Component\Component;
33  use \ILIAS\UI\Renderer;
34  use \ILIAS\UI\Implementation\Render\AbstractComponentRenderer;
36  public function render(Component $component, Renderer $default_renderer) {
37  }
38  public function _getTemplate($a, $b, $c) {
39  return $this->getTemplate($a, $b, $c);
40  }
41  protected function getComponentInterfaceName() {
42  return "\\ILIAS\\UI\\Component\\Counter\\Counter";
43  }
44  }
45 }
46 
47 namespace {
48 
49  require_once(__DIR__."/../Base.php");
50 
51 
52  use \ILIAS\UI\Component as C;
53  use \ILIAS\UI\Implementation\Render\Template;
54  use \ILIAS\UI\Implementation\Render\JavaScriptBinding;
55  use \ILIAS\UI\Implementation\Render\TemplateFactory;
56 
57  class NullTemplate implements Template {
58  public function setCurrentBlock($name) {}
59  public function parseCurrentBlock() {}
60  public function touchBlock($name) {}
61  public function setVariable($name, $value) {}
62  public function get($name = null) { return ""; }
63  public function addOnLoadCode($code) { return ""; }
64  }
65 
67  public $files = array();
68  public function getTemplate($file_name, $purge_unfilled_vars, $purge_unused_blocks) {
69  $file_name = realpath(__DIR__."/../../../".$file_name);
70  $this->files[$file_name] = array($purge_unfilled_vars, $purge_unused_blocks);
71 
72  if (!file_exists($file_name)) {
73  throw new \InvalidArgumentException();
74  }
75 
76  return new NullTemplate();
77  }
78  }
79 
81  public function render(C\Component $component) {
82  return "";
83  }
84  }
85 
87  public function setUp() {
88  parent::setUp();
89  $this->tpl_factory = new TemplateFactoryMock();
90  $this->ui_factory = new NoUIFactory();
91  $this->lng = new ilLanguageMock();
92  $this->js_binding = new LoggingJavaScriptBinding();
93  }
94 
95  public function test_getTemplate_successfull() {
96  $r = new \ILIAS\UI\Implementation\Component\Glyph\GlyphNonAbstractRenderer($this->ui_factory, $this->tpl_factory, $this->lng, $this->js_binding);
97  $tpl = $r->_getTemplate("tpl.glyph.html", true, false);
98 
99  $expected = array
100  ( realpath(__DIR__."/../../../src/UI/templates/default/Glyph/tpl.glyph.html")
101  => array(true, false)
102  );
103 
104  $this->assertEquals($expected, $this->tpl_factory->files);
105  }
106 
107  public function test_getTemplate_unsuccessfull() {
108  $r = new \ILIAS\UI\Implementation\Component\Counter\CounterNonAbstractRenderer($this->ui_factory, $this->tpl_factory, $this->lng, $this->js_binding);
109 
110  try {
111  $tpl = $r->_getTemplate("tpl.counter_foo.html", true, false);
112  $this->assertFalse("We should not get here");
113  } catch (\InvalidArgumentException $e) {};
114 
115  $expected = array
116  ( realpath(__DIR__."/../../src/UI/templates/default/Counter/tpl.counter_foo.html")
117  => array(true, false)
118  );
119  $this->assertEquals($expected, $this->tpl_factory->files);
120  }
121 
123  $r = new \ILIAS\UI\Implementation\Component\Glyph\GlyphNonAbstractRendererWithJS($this->ui_factory, $this->tpl_factory, $this->lng, $this->js_binding);
124 
125  $g = new \ILIAS\UI\Implementation\Component\Glyph\Glyph(\ILIAS\UI\Component\Glyph\Glyph::SETTINGS, "aria_label");
126 
127  $ids = array();
128  $g = $g->withOnLoadCode(function($id) use (&$ids) {
129  $ids[] = $id;
130  return "ID: $id";
131  });
132  $r->render($g, new NullDefaultRenderer());
133 
134  $this->assertEquals($this->js_binding->ids, $ids);
135  $this->assertEquals(array("id_1"), $ids);
136  $this->assertEquals(array("ID: id_1"), $this->js_binding->on_load_code);
137  }
138 
139  public function test_bindJavaScript_no_string() {
140  $r = new \ILIAS\UI\Implementation\Component\Glyph\GlyphNonAbstractRendererWithJS($this->ui_factory, $this->tpl_factory, $this->lng, $this->js_binding);
141 
142  $g = new \ILIAS\UI\Implementation\Component\Glyph\Glyph(\ILIAS\UI\Component\Glyph\Glyph::SETTINGS, "aria_label");
143 
144  $g = $g->withOnLoadCode(function($id) {
145  return null;
146  });
147 
148  try {
149  $r->render($g, new NullDefaultRenderer());
150  $this->assertFalse("This should not happen...");
151  }
152  catch (\LogicException $e) {
153  $this->assertTrue(true);
154  }
155  }
156  }
157 }
$files
Definition: add-vimline.php:18
An entity that renders components to a string output.
Definition: Renderer.php:12
Interface Card .
$code
Definition: example_050.php:99
Interface to templating as it is used in the UI framework.
Definition: Template.php:13
getTemplate($file_name, $purge_unfilled_vars, $purge_unused_blocks)
Get template instance.
render(Component $component, Renderer $default_renderer)
global $tpl
Definition: ilias.php:8
$r
Definition: example_031.php:79
setVariable($name, $value)
Set a variable in the current block.
render(C\Component $component)
render(Component $component, Renderer $default_renderer)
Provides common functionality for UI tests.
Definition: Base.php:69
addOnLoadCode($code)
Add some javascript to be executed on_load of the rendered page.
setCurrentBlock($name)
Set the block to work on.
Create styles array
The data for the language used.
parseCurrentBlock()
Parse the block that is currently worked on.
getTemplate($name, $purge_unfilled_vars, $purge_unused_blocks)
Get template of component this renderer is made for.
Interface for a factory that provides templates.
touchBlock($name)
Touch a block without working further on it.
bindJavaScript(JavaScriptBindable $component)
Bind the component to JavaScript.