PageRenderTime 25ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/simpletest/testoutputlib.php

http://github.com/moodle/moodle
PHP | 165 lines | 93 code | 16 blank | 56 comment | 1 complexity | b3fa410c0dff2bf59e8c950b72793281 MD5 | raw file
Possible License(s): MIT, AGPL-3.0, MPL-2.0-no-copyleft-exception, LGPL-3.0, GPL-3.0, Apache-2.0, LGPL-2.1, BSD-3-Clause
  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. * Unit tests for (some of) ../outputlib.php.
  18. *
  19. * @package moodlecore
  20. * @copyright 2009 Tim Hunt
  21. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later (5)
  22. */
  23. if (!defined('MOODLE_INTERNAL')) {
  24. die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
  25. }
  26. require_once($CFG->libdir . '/outputlib.php');
  27. /**
  28. * Unit tests for the xhtml_container_stack class.
  29. *
  30. * These tests assume that developer debug mode is on, which, at the time of
  31. * writing, is true. admin/tool/unittest/index.php forces it on.
  32. *
  33. * @copyright 2009 Tim Hunt
  34. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35. */
  36. class xhtml_container_stack_test extends UnitTestCase {
  37. public static $includecoverage = array('lib/outputlib.php');
  38. protected function start_capture() {
  39. ob_start();
  40. }
  41. protected function end_capture() {
  42. $result = ob_get_contents();
  43. ob_end_clean();
  44. return $result;
  45. }
  46. public function test_push_then_pop() {
  47. // Set up.
  48. $stack = new xhtml_container_stack();
  49. // Exercise SUT.
  50. $this->start_capture();
  51. $stack->push('testtype', '</div>');
  52. $html = $stack->pop('testtype');
  53. $errors = $this->end_capture();
  54. // Verify outcome
  55. $this->assertEqual('</div>', $html);
  56. $this->assertEqual('', $errors);
  57. }
  58. public function test_mismatched_pop_prints_warning() {
  59. // Set up.
  60. $stack = new xhtml_container_stack();
  61. $stack->push('testtype', '</div>');
  62. // Exercise SUT.
  63. $this->start_capture();
  64. $html = $stack->pop('mismatch');
  65. $errors = $this->end_capture();
  66. // Verify outcome
  67. $this->assertEqual('</div>', $html);
  68. $this->assertNotEqual('', $errors);
  69. }
  70. public function test_pop_when_empty_prints_warning() {
  71. // Set up.
  72. $stack = new xhtml_container_stack();
  73. // Exercise SUT.
  74. $this->start_capture();
  75. $html = $stack->pop('testtype');
  76. $errors = $this->end_capture();
  77. // Verify outcome
  78. $this->assertEqual('', $html);
  79. $this->assertNotEqual('', $errors);
  80. }
  81. public function test_correct_nesting() {
  82. // Set up.
  83. $stack = new xhtml_container_stack();
  84. // Exercise SUT.
  85. $this->start_capture();
  86. $stack->push('testdiv', '</div>');
  87. $stack->push('testp', '</p>');
  88. $html2 = $stack->pop('testp');
  89. $html1 = $stack->pop('testdiv');
  90. $errors = $this->end_capture();
  91. // Verify outcome
  92. $this->assertEqual('</p>', $html2);
  93. $this->assertEqual('</div>', $html1);
  94. $this->assertEqual('', $errors);
  95. }
  96. public function test_pop_all_but_last() {
  97. // Set up.
  98. $stack = new xhtml_container_stack();
  99. $stack->push('test1', '</h1>');
  100. $stack->push('test2', '</h2>');
  101. $stack->push('test3', '</h3>');
  102. // Exercise SUT.
  103. $this->start_capture();
  104. $html = $stack->pop_all_but_last();
  105. $errors = $this->end_capture();
  106. // Verify outcome
  107. $this->assertEqual('</h3></h2>', $html);
  108. $this->assertEqual('', $errors);
  109. // Tear down.
  110. $stack->discard();
  111. }
  112. public function test_pop_all_but_last_only_one() {
  113. // Set up.
  114. $stack = new xhtml_container_stack();
  115. $stack->push('test1', '</h1>');
  116. // Exercise SUT.
  117. $this->start_capture();
  118. $html = $stack->pop_all_but_last();
  119. $errors = $this->end_capture();
  120. // Verify outcome
  121. $this->assertEqual('', $html);
  122. $this->assertEqual('', $errors);
  123. // Tear down.
  124. $stack->discard();
  125. }
  126. public function test_pop_all_but_last_empty() {
  127. // Set up.
  128. $stack = new xhtml_container_stack();
  129. // Exercise SUT.
  130. $this->start_capture();
  131. $html = $stack->pop_all_but_last();
  132. $errors = $this->end_capture();
  133. // Verify outcome
  134. $this->assertEqual('', $html);
  135. $this->assertEqual('', $errors);
  136. }
  137. public function test_discard() {
  138. // Set up.
  139. $stack = new xhtml_container_stack();
  140. $stack->push('test1', '</somethingdistinctive>');
  141. $stack->discard();
  142. // Exercise SUT.
  143. $this->start_capture();
  144. $stack = null;
  145. $errors = $this->end_capture();
  146. // Verify outcome
  147. $this->assertEqual('', $errors);
  148. }
  149. }