MDL-21361 removed invalid unit tests for features that were removed or significantly...
[moodle.git] / lib / simpletest / testoutputlib.php
1 <?php
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/>.
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  */
27 if (!defined('MOODLE_INTERNAL')) {
28     die('Direct access to this script is forbidden.');    ///  It must be included from a Moodle page
29 }
30 require_once($CFG->libdir . '/outputlib.php');
33 /**
34  * Unit tests for the xhtml_container_stack class.
35  *
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.
38  *
39  * @copyright 2009 Tim Hunt
40  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
41  */
42 class xhtml_container_stack_test extends UnitTestCase {
44     public static $includecoverage = array('lib/outputlib.php');
45     protected function start_capture() {
46         ob_start();
47     }
49     protected function end_capture() {
50         $result = ob_get_contents();
51         ob_end_clean();
52         return $result;
53     }
55     public function test_push_then_pop() {
56         // Set up.
57         $stack = new xhtml_container_stack();
58         // Exercise SUT.
59         $this->start_capture();
60         $stack->push('testtype', '</div>');
61         $html = $stack->pop('testtype');
62         $errors = $this->end_capture();
63         // Verify outcome
64         $this->assertEqual('</div>', $html);
65         $this->assertEqual('', $errors);
66     }
68     public function test_mismatched_pop_prints_warning() {
69         // Set up.
70         $stack = new xhtml_container_stack();
71         $stack->push('testtype', '</div>');
72         // Exercise SUT.
73         $this->start_capture();
74         $html = $stack->pop('mismatch');
75         $errors = $this->end_capture();
76         // Verify outcome
77         $this->assertEqual('</div>', $html);
78         $this->assertNotEqual('', $errors);
79     }
81     public function test_pop_when_empty_prints_warning() {
82         // Set up.
83         $stack = new xhtml_container_stack();
84         // Exercise SUT.
85         $this->start_capture();
86         $html = $stack->pop('testtype');
87         $errors = $this->end_capture();
88         // Verify outcome
89         $this->assertEqual('', $html);
90         $this->assertNotEqual('', $errors);
91     }
93     public function test_correct_nesting() {
94         // Set up.
95         $stack = new xhtml_container_stack();
96         // Exercise SUT.
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();
103         // Verify outcome
104         $this->assertEqual('</p>', $html2);
105         $this->assertEqual('</div>', $html1);
106         $this->assertEqual('', $errors);
107     }
109     public function test_pop_all_but_last() {
110         // Set up.
111         $stack = new xhtml_container_stack();
112         $stack->push('test1', '</h1>');
113         $stack->push('test2', '</h2>');
114         $stack->push('test3', '</h3>');
115         // Exercise SUT.
116         $this->start_capture();
117         $html = $stack->pop_all_but_last();
118         $errors = $this->end_capture();
119         // Verify outcome
120         $this->assertEqual('</h3></h2>', $html);
121         $this->assertEqual('', $errors);
122         // Tear down.
123         $stack->discard();
124     }
126     public function test_pop_all_but_last_only_one() {
127         // Set up.
128         $stack = new xhtml_container_stack();
129         $stack->push('test1', '</h1>');
130         // Exercise SUT.
131         $this->start_capture();
132         $html = $stack->pop_all_but_last();
133         $errors = $this->end_capture();
134         // Verify outcome
135         $this->assertEqual('', $html);
136         $this->assertEqual('', $errors);
137         // Tear down.
138         $stack->discard();
139     }
141     public function test_pop_all_but_last_empty() {
142         // Set up.
143         $stack = new xhtml_container_stack();
144         // Exercise SUT.
145         $this->start_capture();
146         $html = $stack->pop_all_but_last();
147         $errors = $this->end_capture();
148         // Verify outcome
149         $this->assertEqual('', $html);
150         $this->assertEqual('', $errors);
151     }
153     public function test_discard() {
154         // Set up.
155         $stack = new xhtml_container_stack();
156         $stack->push('test1', '</somethingdistinctive>');
157         $stack->discard();
158         // Exercise SUT.
159         $this->start_capture();
160         $stack = null;
161         $errors = $this->end_capture();
162         // Verify outcome
163         $this->assertEqual('', $errors);
164     }