1<?php
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * Unit tests for xhtml stack.
19 *
20 * @package   core
21 * @category  phpunit
22 * @copyright 2009 Tim Hunt
23 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later (5)
24 */
25
26defined('MOODLE_INTERNAL') || die();
27
28global $CFG;
29require_once($CFG->libdir . '/outputlib.php');
30
31
32/**
33 * Unit tests for the xhtml_container_stack class.
34 *
35 * These tests assume that developer debug mode is on which is enforced by our phpunit integration.
36 *
37 * @copyright 2009 Tim Hunt
38 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39 */
40class core_xhtml_container_stack_testcase extends advanced_testcase {
41    public function test_push_then_pop() {
42        // Set up.
43        $stack = new xhtml_container_stack();
44        // Exercise SUT.
45        $stack->push('testtype', '</div>');
46        $html = $stack->pop('testtype');
47        // Verify outcome.
48        $this->assertEquals('</div>', $html);
49        $this->assertDebuggingNotCalled();
50    }
51
52    public function test_mismatched_pop_prints_warning() {
53        // Set up.
54        $stack = new xhtml_container_stack();
55        $stack->push('testtype', '</div>');
56        // Exercise SUT.
57        $html = $stack->pop('mismatch');
58        // Verify outcome.
59        $this->assertEquals('</div>', $html);
60        $this->assertDebuggingCalled();
61    }
62
63    public function test_pop_when_empty_prints_warning() {
64        // Set up.
65        $stack = new xhtml_container_stack();
66        // Exercise SUT.
67        $html = $stack->pop('testtype');
68        // Verify outcome.
69        $this->assertEquals('', $html);
70        $this->assertDebuggingCalled();
71    }
72
73    public function test_correct_nesting() {
74        // Set up.
75        $stack = new xhtml_container_stack();
76        // Exercise SUT.
77        $stack->push('testdiv', '</div>');
78        $stack->push('testp', '</p>');
79        $html2 = $stack->pop('testp');
80        $html1 = $stack->pop('testdiv');
81        // Verify outcome.
82        $this->assertEquals('</p>', $html2);
83        $this->assertEquals('</div>', $html1);
84        $this->assertDebuggingNotCalled();
85    }
86
87    public function test_pop_all_but_last() {
88        // Set up.
89        $stack = new xhtml_container_stack();
90        $stack->push('test1', '</h1>');
91        $stack->push('test2', '</h2>');
92        $stack->push('test3', '</h3>');
93        // Exercise SUT.
94        $html = $stack->pop_all_but_last();
95        // Verify outcome.
96        $this->assertEquals('</h3></h2>', $html);
97        $this->assertDebuggingNotCalled();
98        // Tear down.
99        $stack->discard();
100    }
101
102    public function test_pop_all_but_last_only_one() {
103        // Set up.
104        $stack = new xhtml_container_stack();
105        $stack->push('test1', '</h1>');
106        // Exercise SUT.
107        $html = $stack->pop_all_but_last();
108        // Verify outcome.
109        $this->assertEquals('', $html);
110        $this->assertDebuggingNotCalled();
111        // Tear down.
112        $stack->discard();
113    }
114
115    public function test_pop_all_but_last_empty() {
116        // Set up.
117        $stack = new xhtml_container_stack();
118        // Exercise SUT.
119        $html = $stack->pop_all_but_last();
120        // Verify outcome.
121        $this->assertEquals('', $html);
122        $this->assertDebuggingNotCalled();
123    }
124
125    public function test_discard() {
126        // Set up.
127        $stack = new xhtml_container_stack();
128        $stack->push('test1', '</somethingdistinctive>');
129        $stack->discard();
130        // Exercise SUT.
131        $stack = null;
132        // Verify outcome.
133        $this->assertDebuggingNotCalled();
134    }
135}
136