3 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
20 * Unit tests for (some of) ../outputlib.php.
23 * @copyright 2009 Tim Hunt
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later (5)
27 if (!defined('MOODLE_INTERNAL')) {
28 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
30 require_once($CFG->libdir . '/outputlib.php');
34 * Unit tests for the xhtml_container_stack class.
36 * These tests assume that developer debug mode is on, which, at the time of
37 * writing, is true. admin/report/unittest/index.php forces it on.
39 * @copyright 2009 Tim Hunt
40 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
42 class xhtml_container_stack_test extends UnitTestCase {
44 public static $includecoverage = array('lib/outputlib.php');
45 protected function start_capture() {
49 protected function end_capture() {
50 $result = ob_get_contents();
55 public function test_push_then_pop() {
57 $stack = new xhtml_container_stack();
59 $this->start_capture();
60 $stack->push('testtype', '</div>');
61 $html = $stack->pop('testtype');
62 $errors = $this->end_capture();
64 $this->assertEqual('</div>', $html);
65 $this->assertEqual('', $errors);
68 public function test_mismatched_pop_prints_warning() {
70 $stack = new xhtml_container_stack();
71 $stack->push('testtype', '</div>');
73 $this->start_capture();
74 $html = $stack->pop('mismatch');
75 $errors = $this->end_capture();
77 $this->assertEqual('</div>', $html);
78 $this->assertNotEqual('', $errors);
81 public function test_pop_when_empty_prints_warning() {
83 $stack = new xhtml_container_stack();
85 $this->start_capture();
86 $html = $stack->pop('testtype');
87 $errors = $this->end_capture();
89 $this->assertEqual('', $html);
90 $this->assertNotEqual('', $errors);
93 public function test_correct_nesting() {
95 $stack = new xhtml_container_stack();
97 $this->start_capture();
98 $stack->push('testdiv', '</div>');
99 $stack->push('testp', '</p>');
100 $html2 = $stack->pop('testp');
101 $html1 = $stack->pop('testdiv');
102 $errors = $this->end_capture();
104 $this->assertEqual('</p>', $html2);
105 $this->assertEqual('</div>', $html1);
106 $this->assertEqual('', $errors);
109 public function test_pop_all_but_last() {
111 $stack = new xhtml_container_stack();
112 $stack->push('test1', '</h1>');
113 $stack->push('test2', '</h2>');
114 $stack->push('test3', '</h3>');
116 $this->start_capture();
117 $html = $stack->pop_all_but_last();
118 $errors = $this->end_capture();
120 $this->assertEqual('</h3></h2>', $html);
121 $this->assertEqual('', $errors);
126 public function test_pop_all_but_last_only_one() {
128 $stack = new xhtml_container_stack();
129 $stack->push('test1', '</h1>');
131 $this->start_capture();
132 $html = $stack->pop_all_but_last();
133 $errors = $this->end_capture();
135 $this->assertEqual('', $html);
136 $this->assertEqual('', $errors);
141 public function test_pop_all_but_last_empty() {
143 $stack = new xhtml_container_stack();
145 $this->start_capture();
146 $html = $stack->pop_all_but_last();
147 $errors = $this->end_capture();
149 $this->assertEqual('', $html);
150 $this->assertEqual('', $errors);
153 public function test_discard() {
155 $stack = new xhtml_container_stack();
156 $stack->push('test1', '</somethingdistinctive>');
159 $this->start_capture();
161 $errors = $this->end_capture();
163 $this->assertEqual('', $errors);