1<?php 2 3/* Copyright (c) 2016 Richard Klees <richard.klees@concepts-and-training.de> Extended GPL, see docs/LICENSE */ 4 5require_once("libs/composer/vendor/autoload.php"); 6 7require_once(__DIR__ . "/Renderer/ilIndependentTemplate.php"); 8require_once(__DIR__ . "/../../Services/Language/classes/class.ilLanguage.php"); 9 10use ILIAS\UI\Component as C; 11use ILIAS\UI\Component\Component as IComponent; 12use ILIAS\UI\Implementaiton\Component as I; 13use ILIAS\UI\Implementation\Render\TemplateFactory; 14use ILIAS\UI\Implementation\Render\ResourceRegistry; 15use ILIAS\UI\Implementation\Render\JavaScriptBinding; 16use ILIAS\UI\Implementation\Render\DefaultRendererFactory; 17use ILIAS\UI\Implementation\DefaultRenderer; 18use ILIAS\UI\Implementation\ComponentRendererFSLoader; 19use ILIAS\UI\Implementation\Render; 20use ILIAS\UI\Implementation\Component\Symbol\Glyph\GlyphRendererFactory; 21use ILIAS\UI\Implementation\Component\Input\Field\FieldRendererFactory; 22use ILIAS\UI\Factory; 23use PHPUnit\Framework\TestCase; 24 25class ilIndependentTemplateFactory implements TemplateFactory 26{ 27 public function getTemplate($path, $purge_unfilled_vars, $purge_unused_blocks) 28 { 29 return new ilIndependentGlobalTemplate($path, $purge_unfilled_vars, $purge_unused_blocks); 30 } 31} 32 33class NoUIFactory implements Factory 34{ 35 public function counter() 36 { 37 } 38 public function button() 39 { 40 } 41 public function card() 42 { 43 } 44 public function deck(array $cards) 45 { 46 } 47 public function listing() 48 { 49 } 50 public function image() 51 { 52 } 53 public function legacy($content) 54 { 55 } 56 public function panel() 57 { 58 } 59 public function modal() 60 { 61 } 62 public function dropzone() 63 { 64 } 65 public function popover() 66 { 67 } 68 public function divider() 69 { 70 } 71 public function link() 72 { 73 } 74 public function dropdown() 75 { 76 } 77 public function item() 78 { 79 } 80 public function viewControl() 81 { 82 } 83 public function breadcrumbs(array $crumbs) 84 { 85 } 86 public function chart() 87 { 88 } 89 public function input() 90 { 91 } 92 public function table() 93 { 94 } 95 public function messageBox() 96 { 97 } 98 public function layout() : C\Layout\Factory 99 { 100 } 101 public function mainControls() : C\MainControls\Factory 102 { 103 } 104 public function tree() 105 { 106 } 107 public function menu() : C\Menu\Factory 108 { 109 } 110 public function symbol() : C\Symbol\Factory 111 { 112 } 113} 114 115class LoggingRegistry implements ResourceRegistry 116{ 117 public $resources = array(); 118 119 public function register($name) 120 { 121 $this->resources[] = $name; 122 } 123} 124 125class ilLanguageMock extends \ilLanguage 126{ 127 public $requested = array(); 128 public function __construct() 129 { 130 } 131 public function txt($a_topic, $a_default_lang_fallback_mod = "") 132 { 133 $this->requested[] = $a_topic; 134 return $a_topic; 135 } 136 public function toJS($a_lang_key, ilGlobalTemplateInterface $a_tpl = null) 137 { 138 } 139 public $lang_module = 'common'; 140 public function loadLanguageModule($lang_module) 141 { 142 } 143} 144 145class LoggingJavaScriptBinding implements JavaScriptBinding 146{ 147 private $count = 0; 148 public $ids = array(); 149 public function createId() 150 { 151 $this->count++; 152 $id = "id_" . $this->count; 153 $this->ids[] = $id; 154 return $id; 155 } 156 public $on_load_code = array(); 157 public function addOnLoadCode($code) 158 { 159 $this->on_load_code[] = $code; 160 } 161 public function getOnLoadCodeAsync() 162 { 163 } 164} 165 166class TestDefaultRenderer extends DefaultRenderer 167{ 168 public function _getRendererFor(IComponent $component) 169 { 170 return $this->getRendererFor($component); 171 } 172 public function _getContexts() 173 { 174 return $this->getContexts(); 175 } 176} 177 178class IncrementalSignalGenerator extends \ILIAS\UI\Implementation\Component\SignalGenerator 179{ 180 protected $id = 0; 181 182 protected function createId() 183 { 184 return 'signal_' . ++$this->id; 185 } 186} 187 188class SignalGeneratorMock extends \ILIAS\UI\Implementation\Component\SignalGenerator 189{ 190} 191 192class DummyComponent implements IComponent 193{ 194 public function getCanonicalName() 195 { 196 return "DummyComponent"; 197 } 198} 199 200/** 201 * Provides common functionality for UI tests. 202 */ 203abstract class ILIAS_UI_TestBase extends TestCase 204{ 205 public function setUp() : void 206 { 207 assert_options(ASSERT_WARNING, 0); 208 } 209 210 public function tearDown() : void 211 { 212 assert_options(ASSERT_WARNING, 1); 213 } 214 215 public function getUIFactory() 216 { 217 return new NoUIFactory(); 218 } 219 220 public function getTemplateFactory() 221 { 222 return new ilIndependentTemplateFactory(); 223 } 224 225 public function getResourceRegistry() 226 { 227 return new LoggingRegistry(); 228 } 229 230 public function getLanguage() 231 { 232 return new ilLanguageMock(); 233 } 234 235 public function getJavaScriptBinding() 236 { 237 return new LoggingJavaScriptBinding(); 238 } 239 240 public function getRefinery() 241 { 242 return $this->getMockBuilder(\ILIAS\Refinery\Factory::class) 243 ->disableOriginalConstructor() 244 ->getMock(); 245 } 246 247 public function getDefaultRenderer(JavaScriptBinding $js_binding = null) 248 { 249 $ui_factory = $this->getUIFactory(); 250 $tpl_factory = $this->getTemplateFactory(); 251 $resource_registry = $this->getResourceRegistry(); 252 $lng = $this->getLanguage(); 253 if (!$js_binding) { 254 $js_binding = $this->getJavaScriptBinding(); 255 } 256 257 $refinery = $this->getRefinery(); 258 259 $component_renderer_loader 260 = new Render\LoaderCachingWrapper( 261 new Render\LoaderResourceRegistryWrapper( 262 $resource_registry, 263 new Render\FSLoader( 264 new DefaultRendererFactory( 265 $ui_factory, 266 $tpl_factory, 267 $lng, 268 $js_binding, 269 $refinery 270 ), 271 new GlyphRendererFactory( 272 $ui_factory, 273 $tpl_factory, 274 $lng, 275 $js_binding, 276 $refinery 277 ), 278 new FieldRendererFactory( 279 $ui_factory, 280 $tpl_factory, 281 $lng, 282 $js_binding, 283 $refinery 284 ) 285 ) 286 ) 287 ); 288 return new TestDefaultRenderer($component_renderer_loader); 289 } 290 291 public function normalizeHTML($html) 292 { 293 return trim(str_replace(["\n", "\r"], "", $html)); 294 } 295 296 /** 297 * @param string $expected_html_as_string 298 * @param string $html_as_string 299 */ 300 public function assertHTMLEquals($expected_html_as_string, $html_as_string) 301 { 302 $html = new DOMDocument(); 303 $html->formatOutput = true; 304 $html->preserveWhiteSpace = false; 305 $expected = new DOMDocument(); 306 $expected->formatOutput = true; 307 $expected->preserveWhiteSpace = false; 308 $html->loadXML($this->normalizeHTML($html_as_string)); 309 $expected->loadXML($this->normalizeHTML($expected_html_as_string)); 310 $this->assertEquals($expected->saveHTML(), $html->saveHTML()); 311 } 312 313 /** 314 * A more radical version of normalizeHTML. Use if hard to tackle issues 315 * occur by asserting due string outputs produce an equal DOM 316 * 317 * @param $html 318 * @return string 319 */ 320 protected function brutallyTrimHTML($html) 321 { 322 $html = str_replace(["\n", "\r", "\t"], "", $html); 323 $html = preg_replace('# {2,}#', " ", $html); 324 $html = str_replace("> <", "><", $html); 325 $html = str_replace(" >", ">", $html); 326 327 return trim($html); 328 } 329} 330