MDL-20204 converting yes_no selects
[moodle.git] / lib / simpletest / testoutputlib.php
CommitLineData
571fa828 1<?php
2
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
18
19/**
20 * Unit tests for (some of) ../outputlib.php.
21 *
22 * @package moodlecore
23 * @copyright 2009 Tim Hunt
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later (5)
25 */
26
27if (!defined('MOODLE_INTERNAL')) {
28 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
29}
30require_once($CFG->libdir . '/outputlib.php');
31
a6855934
PS
32//TODO: MDL-21361
33return;
34
35$e = <<<EOT
36
571fa828 37/**
38 * Subclass of renderer_factory_base for testing. Implement abstract method and
39 * count calls, so we can test caching behaviour.
ebebf55c 40 *
41 * @copyright 2009 Tim Hunt
42 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
571fa828 43 */
44class testable_renderer_factory extends renderer_factory_base {
45 public $createcalls = array();
46
47 public function __construct() {
db8d89d8 48 parent::__construct(null);
571fa828 49 }
50
897b5c82 51 public function get_renderer($module, $page, $subtype=null) {
52 if (!in_array(array($module, $subtype), $this->createcalls)) {
53 $this->createcalls[] = array($module, $subtype);
54 }
78946b9b 55 return new core_renderer($page);
571fa828 56 }
57
78946b9b
PS
58 public function standard_renderer_class_for_plugin($module, $subtype=null) {
59 return parent::standard_renderer_class_for_plugin($module, $subtype);
571fa828 60 }
61}
62
63
64/**
65 * Renderer class for testing.
ebebf55c 66 *
67 * @copyright 2009 Tim Hunt
68 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
571fa828 69 */
78946b9b 70class mod_test_renderer extends core_renderer {
34a2777c 71 public function __construct($containerstack, $page) {
c84a2dbe 72 parent::__construct($containerstack, $page, null);
571fa828 73 }
74
75 public function greeting($name = 'world') {
76 return '<h1>Hello ' . $name . '!</h1>';
77 }
78
79 public function box($content, $id = '') {
80 return box_start($id) . $content . box_end();
81 }
82
83 public function box_start($id = '') {
84 if ($id) {
85 $id = ' id="' . $id . '"';
86 }
87 $this->containerstack->push('box', '</div>');
88 return '<div' . $id . '>';
89 }
90
91 public function box_end() {
92 return $this->containerstack->pop('box');
93 }
94}
95
96
897b5c82 97/**
98 * Renderer class for testing subrendering feature
99 *
100 * @copyright 2009 David Mudrak
101 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
102 */
78946b9b 103class mod_test_subtype_renderer extends core_renderer {
897b5c82 104 public function __construct($containerstack, $page) {
105 parent::__construct($containerstack, $page, null);
106 }
107
108 public function signature($user = 'Administrator') {
109 return '<div class="signature">Best regards, ' . $user . '</div>';
110 }
111}
112
113
571fa828 114/**
115 * Unit tests for the requriement_base base class.
116 *
117 * @copyright 2009 Tim Hunt
118 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
119 */
120class renderer_factory_base_test extends UnitTestCase {
081a63a9 121
122 public static $includecoverage = array('lib/outputlib.php');
123
571fa828 124 public function test_get_calls_create() {
125 // Set up.
126 $factory = new testable_renderer_factory();
127 // Exercise SUT.
897b5c82 128 $renderer = $factory->get_renderer('modulename', new moodle_page);
129 $subrenderer = $factory->get_renderer('modulename', new moodle_page, 'subtype');
130 $cached = $factory->get_renderer('modulename', new moodle_page);
571fa828 131 // Verify outcome
897b5c82 132 $this->assertEqual(array(array('modulename', null), array('modulename', 'subtype')), $factory->createcalls);
133
571fa828 134 }
135
78946b9b 136 public function test_standard_renderer_class_for_plugin_core() {
571fa828 137 // Set up.
138 $factory = new testable_renderer_factory();
139 // Exercise SUT.
78946b9b 140 $classname = $factory->standard_renderer_class_for_plugin('core');
571fa828 141 // Verify outcome
78946b9b 142 $this->assertEqual('core_renderer', $classname);
571fa828 143 }
144
78946b9b 145 public function test_standard_renderer_class_for_plugin_test() {
571fa828 146 // Set up.
147 $factory = new testable_renderer_factory();
148 // Exercise SUT.
78946b9b 149 $classname = $factory->standard_renderer_class_for_plugin('mod_test');
571fa828 150 // Verify outcome
78946b9b 151 $this->assertEqual('mod_test_renderer', $classname);
571fa828 152 }
153
78946b9b 154 public function test_standard_renderer_class_for_plugin_test_with_subtype() {
897b5c82 155 // Set up.
156 $factory = new testable_renderer_factory();
157 // Exercise SUT.
78946b9b 158 $classname = $factory->standard_renderer_class_for_plugin('mod_test', 'subtype');
897b5c82 159 // Verify outcome
78946b9b 160 $this->assertEqual('mod_test_subtype_renderer', $classname);
897b5c82 161 }
162
78946b9b 163 public function test_standard_renderer_class_for_plugin_unknown() {
571fa828 164 // Set up.
165 $factory = new testable_renderer_factory();
166 $this->expectException();
167 // Exercise SUT.
78946b9b 168 $classname = $factory->standard_renderer_class_for_plugin('something_that_does_not_exist');
571fa828 169 }
170}
171
172
173/**
174 * Unit tests for the standard_renderer_factory class.
175 *
176 * @copyright 2009 Tim Hunt
177 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
178 */
179class standard_renderer_factory_test extends UnitTestCase {
7755599d 180
181 public static $includecoverage = array('lib/outputrenderers.php', 'lib/outputcomponents.php');
571fa828 182 protected $factory;
183
184 public function setUp() {
185 parent::setUp();
34a2777c 186 $this->factory = new standard_renderer_factory(null, null);
571fa828 187 }
188
189 public function tearDown() {
190 $this->factory = null;
191 parent::tearDown();
192 }
193
194 public function test_get_core_renderer() {
db8d89d8 195 $renderer = $this->factory->get_renderer('core', new moodle_page);
78946b9b 196 $this->assertIsA($renderer, 'core_renderer');
571fa828 197 }
198
199 public function test_get_test_renderer() {
ebebf55c 200 $renderer = $this->factory->get_renderer('mod_test', new moodle_page);
78946b9b 201 $this->assertIsA($renderer, 'mod_test_renderer');
571fa828 202 }
897b5c82 203
204 public function test_get_test_subtype_renderer() {
205 $renderer = $this->factory->get_renderer('mod_test', new moodle_page, 'subtype');
78946b9b 206 $this->assertIsA($renderer, 'mod_test_subtype_renderer');
897b5c82 207 }
571fa828 208}
209
210
211/**
212 * Test-specific subclass that implements a getter for $prefixes.
213 *
214 * @copyright 2009 Tim Hunt
215 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
216 */
217class testable_theme_overridden_renderer_factory extends theme_overridden_renderer_factory {
7755599d 218
219 public static $includecoverage = array('lib/outputlib.php');
571fa828 220 public function get_prefixes() {
221 return $this->prefixes;
222 }
223}
224
225
226/**
227 * Unit tests for the theme_overridden_renderer_factory class.
228 *
229 * @copyright 2009 Tim Hunt
230 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
231 */
78946b9b 232/*class theme_overridden_renderer_factory_test extends UnitTestCase { // TODO: MDL-21138 rewrite theme unit tests
7755599d 233
234 public static $includecoverage = array('lib/outputlib.php');
571fa828 235 protected $originalcfgthemedir;
236 protected $workspace;
237 protected $page;
238 protected $foldertocleanup = null;
239
240 public function setUp() {
241 global $CFG;
242 parent::setUp();
78946b9b 243
571fa828 244 $this->originalcfgthemedir = $CFG->themedir;
245
246 $this->workspace = 'temp/theme_overridden_renderer_factory_fixtures';
247 make_upload_directory($this->workspace);
248 $CFG->themedir = $CFG->dataroot . '/' . $this->workspace;
249 $this->foldertocleanup = $CFG->themedir;
250
251 $this->page = new stdClass;
571fa828 252 }
253
254 public function tearDown() {
255 global $CFG;
256 if (!empty($this->foldertocleanup)) {
257 fulldelete($this->foldertocleanup);
258 $this->foldertocleanup = null;
259 }
260 $CFG->themedir = $this->originalcfgthemedir;
261 parent::tearDown();
262 }
263
264 protected function make_theme($name) {
265 global $CFG;
266 $theme = new stdClass;
267 $theme->name = $name;
268 $theme->dir = $CFG->themedir . '/' . $name;
269 make_upload_directory($this->workspace . '/' . $name);
270 return $theme;
271 }
272
273 protected function write_renderers_file($theme, $code) {
78946b9b 274 $filename = $theme->dir . '/lib.php';
571fa828 275 file_put_contents($filename, "<?php\n" . $code);
276 }
277
278 public function test_constructor_theme_with_renderes() {
279 // Set up.
280 $theme = $this->make_theme('mytheme');
281 $this->write_renderers_file($theme, '');
282
283 // Exercise SUT.
284 $factory = new testable_theme_overridden_renderer_factory($theme, $this->page);
285
286 // Verify outcome
287 $this->assertEqual(array('mytheme_'), $factory->get_prefixes());
288 }
289
290 public function test_constructor_theme_without_renderes() {
291 // Set up.
292 $theme = $this->make_theme('mytheme');
293
294 // Exercise SUT.
295 $factory = new testable_theme_overridden_renderer_factory($theme, $this->page);
296
297 // Verify outcome
298 $this->assertEqual(array(), $factory->get_prefixes());
299 }
300
301 public function test_constructor_theme_with_parent() {
302 // Set up.
303 $theme = $this->make_theme('mytheme');
304 $theme->parent = 'parenttheme';
305 $parenttheme = $this->make_theme('parenttheme');
306 $this->write_renderers_file($parenttheme, '');
307
308 // Exercise SUT.
309 $factory = new testable_theme_overridden_renderer_factory($theme, $this->page);
310
311 // Verify outcome
312 $this->assertEqual(array('parenttheme_'), $factory->get_prefixes());
313 }
314
315 public function test_get_renderer_not_overridden() {
316 // Set up.
317 $theme = $this->make_theme('mytheme');
318 $this->write_renderers_file($theme, '');
319 $factory = new testable_theme_overridden_renderer_factory($theme, $this->page);
320
321 // Exercise SUT.
897b5c82 322 $renderer = $factory->get_renderer('mod_test', new moodle_page);
323 $subrenderer = $factory->get_renderer('mod_test', new moodle_page, 'subtype');
571fa828 324
325 // Verify outcome
78946b9b
PS
326 $this->assertIsA($renderer, 'mod_test_renderer');
327 $this->assertIsA($subrenderer, 'mod_test_subtype_renderer');
571fa828 328 }
329
330 public function test_get_renderer_overridden() {
331 // Set up - be very careful because the class under test uses require-once. Pick a unique theme name.
332 $theme = $this->make_theme('testrenderertheme');
333 $this->write_renderers_file($theme, '
78946b9b 334 class testrenderertheme_mod_test_renderer extends mod_test_renderer {
571fa828 335 }');
336 $factory = new testable_theme_overridden_renderer_factory($theme, $this->page);
337
338 // Exercise SUT.
897b5c82 339 $renderer = $factory->get_renderer('mod_test', new moodle_page);
340 $subrenderer = $factory->get_renderer('mod_test', new moodle_page, 'subtype');
571fa828 341
342 // Verify outcome
ebebf55c 343 $this->assertIsA($renderer, 'testrenderertheme_mod_test_renderer');
78946b9b 344 $this->assertIsA($subrenderer, 'mod_test_subtype_renderer');
571fa828 345 }
346
347 public function test_get_renderer_overridden_in_parent() {
348 // Set up.
349 $theme = $this->make_theme('childtheme');
350 $theme->parent = 'parentrenderertheme';
351 $parenttheme = $this->make_theme('parentrenderertheme');
352 $this->write_renderers_file($theme, '');
353 $this->write_renderers_file($parenttheme, '
78946b9b 354 class parentrenderertheme_core_renderer extends core_renderer {
571fa828 355 }');
356 $factory = new testable_theme_overridden_renderer_factory($theme, $this->page);
357
358 // Exercise SUT.
db8d89d8 359 $renderer = $factory->get_renderer('core', new moodle_page);
571fa828 360
361 // Verify outcome
362 $this->assertIsA($renderer, 'parentrenderertheme_core_renderer');
363 }
364
365 public function test_get_renderer_overridden_in_both() {
366 // Set up.
367 $theme = $this->make_theme('ctheme');
368 $theme->parent = 'ptheme';
369 $parenttheme = $this->make_theme('ptheme');
370 $this->write_renderers_file($theme, '
78946b9b 371 class ctheme_core_renderer extends core_renderer {
571fa828 372 }');
373 $this->write_renderers_file($parenttheme, '
78946b9b 374 class ptheme_core_renderer extends core_renderer {
571fa828 375 }');
376 $factory = new testable_theme_overridden_renderer_factory($theme, $this->page);
377
378 // Exercise SUT.
db8d89d8 379 $renderer = $factory->get_renderer('core', new moodle_page);
571fa828 380
381 // Verify outcome
382 $this->assertIsA($renderer, 'ctheme_core_renderer');
383 }
78946b9b 384}*/
571fa828 385
386/**
387 * Unit tests for the xhtml_container_stack class.
388 *
389 * These tests assume that developer debug mode is on, which, at the time of
390 * writing, is true. admin/report/unittest/index.php forces it on.
391 *
392 * @copyright 2009 Tim Hunt
393 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
394 */
395class xhtml_container_stack_test extends UnitTestCase {
7755599d 396
397 public static $includecoverage = array('lib/outputlib.php');
571fa828 398 protected function start_capture() {
399 ob_start();
400 }
401
402 protected function end_capture() {
403 $result = ob_get_contents();
404 ob_end_clean();
405 return $result;
406 }
407
408 public function test_push_then_pop() {
409 // Set up.
410 $stack = new xhtml_container_stack();
411 // Exercise SUT.
412 $this->start_capture();
413 $stack->push('testtype', '</div>');
414 $html = $stack->pop('testtype');
415 $errors = $this->end_capture();
416 // Verify outcome
417 $this->assertEqual('</div>', $html);
418 $this->assertEqual('', $errors);
419 }
420
421 public function test_mismatched_pop_prints_warning() {
422 // Set up.
423 $stack = new xhtml_container_stack();
424 $stack->push('testtype', '</div>');
425 // Exercise SUT.
426 $this->start_capture();
427 $html = $stack->pop('mismatch');
428 $errors = $this->end_capture();
429 // Verify outcome
430 $this->assertEqual('</div>', $html);
431 $this->assertNotEqual('', $errors);
432 }
433
434 public function test_pop_when_empty_prints_warning() {
435 // Set up.
436 $stack = new xhtml_container_stack();
437 // Exercise SUT.
438 $this->start_capture();
439 $html = $stack->pop('testtype');
440 $errors = $this->end_capture();
441 // Verify outcome
442 $this->assertEqual('', $html);
443 $this->assertNotEqual('', $errors);
444 }
445
446 public function test_correct_nesting() {
447 // Set up.
448 $stack = new xhtml_container_stack();
449 // Exercise SUT.
450 $this->start_capture();
451 $stack->push('testdiv', '</div>');
452 $stack->push('testp', '</p>');
453 $html2 = $stack->pop('testp');
454 $html1 = $stack->pop('testdiv');
455 $errors = $this->end_capture();
456 // Verify outcome
457 $this->assertEqual('</p>', $html2);
458 $this->assertEqual('</div>', $html1);
459 $this->assertEqual('', $errors);
460 }
461
462 public function test_pop_all_but_last() {
463 // Set up.
464 $stack = new xhtml_container_stack();
465 $stack->push('test1', '</h1>');
466 $stack->push('test2', '</h2>');
467 $stack->push('test3', '</h3>');
468 // Exercise SUT.
469 $this->start_capture();
470 $html = $stack->pop_all_but_last();
471 $errors = $this->end_capture();
472 // Verify outcome
473 $this->assertEqual('</h3></h2>', $html);
474 $this->assertEqual('', $errors);
475 // Tear down.
476 $stack->discard();
477 }
478
479 public function test_pop_all_but_last_only_one() {
480 // Set up.
481 $stack = new xhtml_container_stack();
482 $stack->push('test1', '</h1>');
483 // Exercise SUT.
484 $this->start_capture();
485 $html = $stack->pop_all_but_last();
486 $errors = $this->end_capture();
487 // Verify outcome
488 $this->assertEqual('', $html);
489 $this->assertEqual('', $errors);
490 // Tear down.
491 $stack->discard();
492 }
493
494 public function test_pop_all_but_last_empty() {
495 // Set up.
496 $stack = new xhtml_container_stack();
497 // Exercise SUT.
498 $this->start_capture();
499 $html = $stack->pop_all_but_last();
500 $errors = $this->end_capture();
501 // Verify outcome
502 $this->assertEqual('', $html);
503 $this->assertEqual('', $errors);
504 }
505
506 public function test_destruct() {
507 // Set up.
508 $stack = new xhtml_container_stack();
509 $stack->push('test1', '</somethingdistinctive>');
510 // Exercise SUT.
511 $this->start_capture();
512 $stack = null;
513 $errors = $this->end_capture();
514 // Verify outcome
515 $this->assertPattern('/<\/somethingdistinctive>/', $errors);
516 }
517
518 public function test_destruct_empty() {
519 // Set up.
520 $stack = new xhtml_container_stack();
521 // Exercise SUT.
522 $this->start_capture();
523 $stack = null;
524 $errors = $this->end_capture();
525 // Verify outcome
526 $this->assertEqual('', $errors);
527 }
528
529 public function test_discard() {
530 // Set up.
531 $stack = new xhtml_container_stack();
532 $stack->push('test1', '</somethingdistinctive>');
533 $stack->discard();
534 // Exercise SUT.
535 $this->start_capture();
536 $stack = null;
537 $errors = $this->end_capture();
538 // Verify outcome
539 $this->assertEqual('', $errors);
540 }
541}
542
8954245a 543/**
78946b9b 544 * Unit tests for the core_renderer class.
8954245a 545 *
546 * @copyright 2009 Tim Hunt
547 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
548 */
78946b9b 549class core_renderer_test extends UnitTestCase {
7755599d 550
551 public static $includecoverage = array('lib/outputrenderers.php', 'lib/outputcomponents.php');
8954245a 552 protected $renderer;
553
554 public function setUp() {
555 parent::setUp();
78946b9b 556 $this->renderer = new core_renderer(new moodle_page);
8954245a 557 }
558
49c8c8d2 559 public function test_select_simple() {
7b1f2c82 560 $select = html_select::make(array(10 => 'ten', 'c2' => 'two'), 'mymenu');
49c8c8d2 561 $html = $this->renderer->select($select);
49690843 562 $this->assert(new ContainsTagWithAttributes('select', array('class' => 'menumymenu select', 'name' => 'mymenu', 'id' => 'menumymenu')), $html);
8954245a 563 $this->assert(new ContainsTagWithContents('option', 'ten'), $html);
564 $this->assert(new ContainsTagWithAttribute('option', 'value', '10'), $html);
565 $this->assert(new ContainsTagWithContents('option', 'two'), $html);
566 $this->assert(new ContainsTagWithAttribute('option', 'value', 'c2'), $html);
567 }
568
7755599d 569 public function test_select_with_label() {
570 $select = html_select::make(array(10 => 'ten', 'c2' => 'two'), 'mymenu');
571 $select->set_label('Cool menu');
572 $html = $this->renderer->select($select);
573 $this->assert(new ContainsTagWithAttribute('label', 'for', $select->id), $html);
574 $this->assert(new ContainsTagWithContents('label', 'Cool menu'), $html);
575 }
576
8954245a 577 public function test_error_text() {
578 $html = $this->renderer->error_text('message');
579 $this->assert(new ContainsTagWithContents('span', 'message'), $html);
580 $this->assert(new ContainsTagWithAttribute('span', 'class', 'error'), $html);
581 }
582
583 public function test_error_text_blank() {
584 $html = $this->renderer->error_text('');
585 $this->assertEqual('', $html);
586 }
f8065dd2 587
588 public function test_link_to_popup_empty_link() {
589 // Empty link object: link MUST have a text value
590 $link = new html_link();
591 $popupaction = new popup_action('click', 'http://test.com', 'my_popup');
55536d84 592 $link->add_action($popupaction);
f8065dd2 593 $this->expectException();
594 $html = $this->renderer->link_to_popup($link);
595 }
596
597 public function test_link_to_popup() {
598 $link = new html_link();
599 $link->text = 'Click here';
600 $link->url = 'http://test.com';
601 $link->title = 'Popup window';
602 $popupaction = new popup_action('click', 'http://test.com', 'my_popup');
55536d84 603 $link->add_action($popupaction);
f8065dd2 604
605 $html = $this->renderer->link_to_popup($link);
606 $expectedattributes = array('title' => 'Popup window', 'href' => 'http://test.com');
607 $this->assert(new ContainsTagWithAttributes('a', $expectedattributes), $html);
608 $this->assert(new ContainsTagWithContents('a', 'Click here'), $html);
609
610 // Try a different url for the link than for the popup
611 $link->url = 'http://otheraddress.com';
612 $html = $this->renderer->link_to_popup($link);
613
614 $this->assert(new ContainsTagWithAttribute('a', 'title', 'Popup window'), $html);
615 $this->assert(new ContainsTagWithAttribute('a', 'href', 'http://otheraddress.com'), $html);
616 $this->assert(new ContainsTagWithContents('a', 'Click here'), $html);
617
618 // Give it a moodle_url object instead of a string
619 $link->url = new moodle_url('http://otheraddress.com');
620 $html = $this->renderer->link_to_popup($link);
621 $this->assert(new ContainsTagWithAttribute('a', 'title', 'Popup window'), $html);
622 $this->assert(new ContainsTagWithAttribute('a', 'href', 'http://otheraddress.com'), $html);
623 $this->assert(new ContainsTagWithContents('a', 'Click here'), $html);
624
625 }
626
627 public function test_button() {
628 global $CFG;
629 $originalform = new html_form();
051bf8c7 630 $originalform->button->text = 'Click Here';
f8065dd2 631 $originalform->url = '/index.php';
632
633 $form = clone($originalform);
634
635 $html = $this->renderer->button($form);
636 $this->assert(new ContainsTagWithAttribute('div', 'class', 'singlebutton'), $html);
637 $this->assert(new ContainsTagWithAttributes('form', array('method' => 'post', 'action' => $CFG->wwwroot . '/index.php')), $html);
638 $this->assert(new ContainsTagWithAttributes('input', array('type' => 'hidden', 'name' => 'sesskey', 'value' => sesskey())), $html);
639 $this->assert(new ContainsTagWithAttributes('input', array('value' => 'Click Here', 'type' => 'submit')), $html);
640
641 $form = clone($originalform);
642 $form->button->confirmmessage = 'Are you sure?';
643
644 $html = $this->renderer->button($form);
645 $this->assert(new ContainsTagWithAttribute('div', 'class', 'singlebutton'), $html);
646 $this->assert(new ContainsTagWithAttributes('form', array('method' => 'post', 'action' => $CFG->wwwroot . '/index.php')), $html);
647 $this->assert(new ContainsTagWithAttribute('input', 'type', 'submit'), $html);
648
649 $form = clone($originalform);
650 $form->url = new moodle_url($form->url, array('var1' => 'value1', 'var2' => 'value2'));
651 $html = $this->renderer->button($form);
652 $this->assert(new ContainsTagWithAttributes('input', array('name' => 'var1', 'type' => 'hidden', 'value' => 'value1')), $html);
653
654 }
655
656 public function test_link() {
657 $link = new html_link();
658 $link->url = 'http://test.com';
659 $link->text = 'Resource 1';
660
661 $html = $this->renderer->link($link);
662 $this->assert(new ContainsTagWithAttribute('a', 'href', 'http://test.com'), $html);
663 $this->assert(new ContainsTagWithContents('a', 'Resource 1'), $html);
664
665 // Add a title
666 $link->title = 'Link to resource 1';
667 $html = $this->renderer->link($link);
668 $this->assert(new ContainsTagWithAttributes('a', array('title' => 'Link to resource 1', 'href' => 'http://test.com')), $html);
669 $this->assert(new ContainsTagWithContents('a', 'Resource 1'), $html);
670
671 // Use a moodle_url object instead of string
672 $link->url = new moodle_url($link->url);
673 $html = $this->renderer->link($link);
674 $this->assert(new ContainsTagWithAttributes('a', array('title' => 'Link to resource 1', 'href' => 'http://test.com')), $html);
675 $this->assert(new ContainsTagWithContents('a', 'Resource 1'), $html);
676
677 // Add a few classes to the link object
678 $link->add_classes('cool blue');
679 $html = $this->renderer->link($link);
4c53f626 680 $this->assert(new ContainsTagWithAttributes('a', array('title' => 'Link to resource 1', 'class' => 'cool blue', 'href' => 'http://test.com')), $html);
f8065dd2 681 $this->assert(new ContainsTagWithContents('a', 'Resource 1'), $html);
682
683 // Simple use of link() without a html_link object
684 $html = $this->renderer->link($link->url->out(), $link->text);
685 $expected_html = '<a href="http://test.com">Resource 1</a>';
686 $this->assert(new ContainsTagWithAttribute('a', 'href', 'http://test.com'), $html);
687 $this->assert(new ContainsTagWithContents('a', 'Resource 1'), $html);
688
689 // Missing second param when first is a string: exception
690 $this->expectException();
691 $html = $this->renderer->link($link->url->out());
692 }
051bf8c7 693
f8065dd2 694 /**
051bf8c7 695 * NOTE: consider the degree of detail in which we test HTML output, because
f8065dd2 696 * the unit tests may be run under a different theme, with different HTML
697 * renderers. Maybe we should limit unit tests to standardwhite.
698 */
699 public function test_confirm() {
700 // Basic test with string URLs
701 $continueurl = 'http://www.test.com/index.php?continue=1';
702 $cancelurl = 'http://www.test.com/index.php?cancel=1';
703 $message = 'Are you sure?';
704
705 $html = $this->renderer->confirm($message, $continueurl, $cancelurl);
706 $this->assert(new ContainsTagWithAttributes('div', array('id' => 'notice', 'class' => 'box generalbox')), $html);
707 $this->assert(new ContainsTagWithContents('p', $message), $html);
708 $this->assert(new ContainsTagWithAttribute('div', 'class', 'buttons'), $html);
709 $this->assert(new ContainsTagWithAttribute('div', 'class', 'singlebutton'), $html);
710 $this->assert(new ContainsTagWithAttributes('form', array('method' => 'post', 'action' => 'http://www.test.com/index.php')), $html);
711 $this->assert(new ContainsTagWithAttributes('input', array('type' => 'hidden', 'name' => 'continue', 'value' => 1)), $html);
712 $this->assert(new ContainsTagWithAttributes('input', array('type' => 'hidden', 'name' => 'sesskey', 'value' => sesskey())), $html);
46aa52bf 713 $this->assert(new ContainsTagWithAttributes('input', array('type' => 'submit', 'value' => get_string('continue'), 'class' => 'singlebutton')), $html);
f8065dd2 714 $this->assert(new ContainsTagWithAttributes('input', array('type' => 'hidden', 'name' => 'cancel', 'value' => 1)), $html);
715 $this->assert(new ContainsTagWithAttributes('input', array('type' => 'hidden', 'name' => 'sesskey', 'value' => sesskey())), $html);
46aa52bf 716 $this->assert(new ContainsTagWithAttributes('input', array('type' => 'submit', 'value' => get_string('cancel'), 'class' => 'singlebutton')), $html);
051bf8c7 717
f8065dd2 718 // Use html_forms with default values, should produce exactly the same output as above
719 $formcontinue = new html_form();
720 $formcancel = new html_form();
721 $formcontinue->url = new moodle_url($continueurl);
722 $formcancel->url = new moodle_url($cancelurl);
46aa52bf 723 $formcontinue->button->text = get_string('continue');
724 $formcancel->button->text = get_string('cancel');
f8065dd2 725 $html = $this->renderer->confirm($message, $formcontinue, $formcancel);
726 $this->assert(new ContainsTagWithAttributes('div', array('id' => 'notice', 'class' => 'box generalbox')), $html);
727 $this->assert(new ContainsTagWithContents('p', $message), $html);
728 $this->assert(new ContainsTagWithAttribute('div', 'class', 'buttons'), $html);
729 $this->assert(new ContainsTagWithAttribute('div', 'class', 'singlebutton'), $html);
730 $this->assert(new ContainsTagWithAttributes('form', array('method' => 'post', 'action' => 'http://www.test.com/index.php')), $html);
731 $this->assert(new ContainsTagWithAttributes('input', array('type' => 'hidden', 'name' => 'continue', 'value' => 1)), $html);
732 $this->assert(new ContainsTagWithAttributes('input', array('type' => 'hidden', 'name' => 'sesskey', 'value' => sesskey())), $html);
46aa52bf 733 $this->assert(new ContainsTagWithAttributes('input', array('type' => 'submit', 'value' => get_string('continue'), 'class' => 'singlebutton')), $html);
f8065dd2 734 $this->assert(new ContainsTagWithAttributes('input', array('type' => 'hidden', 'name' => 'cancel', 'value' => 1)), $html);
735 $this->assert(new ContainsTagWithAttributes('input', array('type' => 'hidden', 'name' => 'sesskey', 'value' => sesskey())), $html);
46aa52bf 736 $this->assert(new ContainsTagWithAttributes('input', array('type' => 'submit', 'value' => get_string('cancel'), 'class' => 'singlebutton')), $html);
051bf8c7 737
f8065dd2 738 // Give the buttons some different labels
739 $formcontinue = new html_form();
740 $formcancel = new html_form();
741 $formcontinue->url = new moodle_url($continueurl);
742 $formcancel->url = new moodle_url($cancelurl);
051bf8c7 743 $formcontinue->button->text = 'Continue anyway';
744 $formcancel->button->text = 'Wow OK, I get it, backing out!';
f8065dd2 745 $html = $this->renderer->confirm($message, $formcontinue, $formcancel);
746 $this->assert(new ContainsTagWithAttributes('form', array('method' => 'post', 'action' => 'http://www.test.com/index.php')), $html);
747 $this->assert(new ContainsTagWithAttributes('input', array('type' => 'hidden', 'name' => 'continue', 'value' => 1)), $html);
748 $this->assert(new ContainsTagWithAttributes('input', array('type' => 'hidden', 'name' => 'sesskey', 'value' => sesskey())), $html);
051bf8c7 749 $this->assert(new ContainsTagWithAttributes('input', array('type' => 'submit', 'value' => $formcontinue->button->text, 'class' => 'singlebutton')), $html);
f8065dd2 750 $this->assert(new ContainsTagWithAttributes('input', array('type' => 'hidden', 'name' => 'cancel', 'value' => 1)), $html);
751 $this->assert(new ContainsTagWithAttributes('input', array('type' => 'hidden', 'name' => 'sesskey', 'value' => sesskey())), $html);
051bf8c7 752 $this->assert(new ContainsTagWithAttributes('input', array('type' => 'submit', 'value' => $formcancel->button->text, 'class' => 'singlebutton')), $html);
753
f8065dd2 754 // Change the method and add extra variables
755 $formcontinue = new html_form();
756 $formcancel = new html_form();
46aa52bf 757 $formcontinue->button->text = get_string('continue');
758 $formcancel->button->text = get_string('cancel');
f8065dd2 759 $formcontinue->url = new moodle_url($continueurl, array('var1' => 'val1', 'var2' => 'val2'));
760 $formcancel->url = new moodle_url($cancelurl, array('var3' => 'val3', 'var4' => 'val4'));
761 $html = $this->renderer->confirm($message, $formcontinue, $formcancel);
762 $this->assert(new ContainsTagWithAttributes('form', array('method' => 'post', 'action' => 'http://www.test.com/index.php')), $html);
763 $this->assert(new ContainsTagWithAttributes('input', array('type' => 'hidden', 'name' => 'continue', 'value' => 1)), $html);
764 $this->assert(new ContainsTagWithAttributes('input', array('type' => 'hidden', 'name' => 'sesskey', 'value' => sesskey())), $html);
765 $this->assert(new ContainsTagWithAttributes('input', array('type' => 'hidden', 'name' => 'var1', 'value' => 'val1')), $html);
766 $this->assert(new ContainsTagWithAttributes('input', array('type' => 'hidden', 'name' => 'var2', 'value' => 'val2')), $html);
46aa52bf 767 $this->assert(new ContainsTagWithAttributes('input', array('type' => 'submit', 'value' => get_string('continue'), 'class' => 'singlebutton')), $html);
f8065dd2 768 $this->assert(new ContainsTagWithAttributes('input', array('type' => 'hidden', 'name' => 'cancel', 'value' => 1)), $html);
769 $this->assert(new ContainsTagWithAttributes('input', array('type' => 'hidden', 'name' => 'sesskey', 'value' => sesskey())), $html);
46aa52bf 770 $this->assert(new ContainsTagWithAttributes('input', array('type' => 'submit', 'value' => get_string('cancel'), 'class' => 'singlebutton')), $html);
f8065dd2 771 $this->assert(new ContainsTagWithAttributes('input', array('type' => 'hidden', 'name' => 'var3', 'value' => 'val3')), $html);
772 $this->assert(new ContainsTagWithAttributes('input', array('type' => 'hidden', 'name' => 'var4', 'value' => 'val4')), $html);
773
774 }
775
776 public function test_spacer() {
777 global $CFG;
778
779 $spacer = new html_image();
780
781 $html = $this->renderer->spacer($spacer);
782 $this->assert(new ContainsTagWithAttributes('img', array('class' => 'image spacer',
783 'src' => $CFG->wwwroot . '/pix/spacer.gif',
784 'alt' => ''
785 )), $html);
786 $spacer = new html_image();
c39e5ba2 787 $spacer->src = $this->renderer->pix_url('myspacer');
f8065dd2 788 $spacer->alt = 'sometext';
789 $spacer->add_class('my');
790
791 $html = $this->renderer->spacer($spacer);
792
793 $this->assert(new ContainsTagWithAttributes('img', array(
794 'class' => 'my image spacer',
c39e5ba2 795 'src' => $this->renderer->pix_url('myspacer'),
f8065dd2 796 'alt' => 'sometext')), $html);
797
798 }
799
800 public function test_paging_bar() {
46aa52bf 801 global $CFG;
f8065dd2 802
803 $totalcount = 5;
804 $perpage = 4;
805 $page = 1;
a6855934 806 $baseurl = new moodle_url('/index.php');
f8065dd2 807 $pagevar = 'mypage';
808
809 $pagingbar = new moodle_paging_bar();
810 $pagingbar->totalcount = $totalcount;
811 $pagingbar->page = $page;
812 $pagingbar->perpage = $perpage;
813 $pagingbar->baseurl = $baseurl;
814 $pagingbar->pagevar = $pagevar;
f8065dd2 815
816 $originalbar = clone($pagingbar);
817
46aa52bf 818 $html = $this->renderer->paging_bar($pagingbar);
f8065dd2 819
820 $this->assert(new ContainsTagWithAttribute('div', 'class', 'paging'), $html);
f43cdceb 821 // the 'Previous' link
f8065dd2 822 $this->assert(new ContainsTagWithAttributes('a', array('class' => 'previous', 'href' => $baseurl->out().'?mypage=0')), $html);
f43cdceb 823 // the numeric link to the previous page '1' (does not have the 'previous' class)
f8065dd2 824 $this->assert(new ContainsTagWithAttributes('a', array('href' => $baseurl->out().'?mypage=0'), array('class' => 'previous')), $html);
f43cdceb 825 // no link to the current page, it's the last page
826 $expectation = new ContainsTagWithAttributes('a', array('href' => $baseurl->out().'?mypage=1'), array());
f8065dd2 827 $this->assertFalse($expectation->test($html));
828
829 // TODO test with more different parameters
830 }
8e127eb3 831
832 public function test_html_list() {
833 $htmllist = new html_list();
834 $data = array('item1', 'item2', array('item1-1', 'item1-2'));
835 $htmllist->load_data($data);
836 $htmllist->items[2]->type = 'ordered';
837 $html = $this->renderer->htmllist($htmllist);
838 }
49690843 839
7b1f2c82 840 public function test_html_select() {
49690843 841 $options = array('var1' => 'value1', 'var2' => 'value2', 'var3' => 'value3');
7b1f2c82 842 $select = html_select::make($options, 'mymenu', 'var2');
49c8c8d2 843 $html = $this->renderer->select($select);
49690843 844 $this->assert(new ContainsTagWithAttributes('select', array('name' => 'mymenu')), $html);
845 $this->assert(new ContainsTagWithAttributes('option', array('value' => 'var1'), array('selected' => 'selected')), $html);
846 $this->assert(new ContainsTagWithAttributes('option', array('value' => 'var2', 'selected' => 'selected')), $html);
847 $this->assert(new ContainsTagWithAttributes('option', array('value' => 'var3'), array('selected' => 'selected')), $html);
848 $this->assert(new ContainsTagWithContents('option', 'value1'), $html);
849 $this->assert(new ContainsTagWithContents('option', 'value2'), $html);
850 $this->assert(new ContainsTagWithContents('option', 'value3'), $html);
851
852 $options = array('group1' => '--group1', 'var1' => 'value1', 'var2' => 'value2', 'group2' => '--', 'group2' => '--group2', 'var3' => 'value3', 'var4' => 'value4');
7b1f2c82 853 $select = html_select::make($options, 'mymenu', 'var2');
49c8c8d2 854 $html = $this->renderer->select($select);
49690843 855 $this->assert(new ContainsTagWithAttributes('select', array('name' => 'mymenu')), $html);
856 $this->assert(new ContainsTagWithAttributes('optgroup', array('label' => 'group1')), $html);
857 $this->assert(new ContainsTagWithAttributes('optgroup', array('label' => 'group2')), $html);
858 $this->assert(new ContainsTagWithAttributes('option', array('value' => 'var1'), array('selected' => 'selected')), $html);
859 $this->assert(new ContainsTagWithAttributes('option', array('value' => 'var2', 'selected' => 'selected')), $html);
860 $this->assert(new ContainsTagWithAttributes('option', array('value' => 'var3'), array('selected' => 'selected')), $html);
861 $this->assert(new ContainsTagWithAttributes('option', array('value' => 'var4'), array('selected' => 'selected')), $html);
862 $this->assert(new ContainsTagWithContents('option', 'value1'), $html);
863 $this->assert(new ContainsTagWithContents('option', 'value2'), $html);
864 $this->assert(new ContainsTagWithContents('option', 'value3'), $html);
865 $this->assert(new ContainsTagWithContents('option', 'value4'), $html);
866 }
beb56299 867
2c78f462 868 public function test_userpicture() {
869 global $CFG;
870 // Set up the user with the required fields
871 $user = new stdClass();
872 $user->firstname = 'Test';
873 $user->lastname = 'User';
874 $user->picture = false;
875 $user->imagealt = false;
876 $user->id = 1;
812dbaf7 877 $userpic = new user_picture();
2c78f462 878 $userpic->user = $user;
879 $userpic->courseid = 1;
880 $userpic->url = true;
881 // Setting popup to true adds JS for the link to open in a popup
882 $userpic->popup = true;
883 $html = $this->renderer->user_picture($userpic);
884 $this->assert(new ContainsTagWithAttributes('a', array('title' => 'Test User', 'href' => $CFG->wwwroot.'/user/view.php?id=1&course=1')), $html);
885 }
e1cc8840 886
887 public function test_heading_with_help() {
4bcc5118 888 $originalicon = new help_icon();
e1cc8840 889 $originalicon->page = 'myhelppage';
890 $originalicon->text = 'Cool help text';
891
892 $helpicon = clone($originalicon);
893 $html = $this->renderer->heading_with_help($helpicon);
894 $this->assert(new ContainsTagWithAttribute('div', 'class', 'heading-with-help'), $html);
895 $this->assert(new ContainsTagWithAttribute('span', 'class', 'helplink'), $html);
896 $this->assert(new ContainsTagWithAttribute('h2', 'class', 'main help'), $html);
c39e5ba2 897 $this->assert(new ContainsTagWithAttributes('img', array('class' => 'iconhelp image', 'src' => $this->renderer->pix_url('help'))), $html);
e1cc8840 898 $this->assert(new ContainsTagWithContents('h2', 'Cool help text'), $html);
899
900 $helpicon = clone($originalicon);
901 $helpicon->image = false;
902
903 $html = $this->renderer->heading_with_help($helpicon);
904 $this->assert(new ContainsTagWithAttribute('div', 'class', 'heading-with-help'), $html);
905 $this->assert(new ContainsTagWithAttribute('span', 'class', 'helplink'), $html);
906 $this->assert(new ContainsTagWithAttribute('h2', 'class', 'main help'), $html);
c39e5ba2 907 $this->assert(new ContainsTagWithAttributes('img', array('class' => 'iconhelp image', 'src' => $this->renderer->pix_url('help'))), $html);
e1cc8840 908 $this->assert(new ContainsTagWithContents('h2', 'Cool help text'), $html);
909 }
8954245a 910}
a6855934
PS
911
912EOT;