Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 310 and 311] [Versions 39 and 311]

   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  namespace core;
  18  
  19  use xhtml_container_stack;
  20  
  21  defined('MOODLE_INTERNAL') || die();
  22  
  23  global $CFG;
  24  require_once($CFG->libdir . '/outputlib.php');
  25  
  26  /**
  27   * Unit tests for the xhtml_container_stack class.
  28   *
  29   * These tests assume that developer debug mode is on which is enforced by our phpunit integration.
  30   *
  31   * @package   core
  32   * @category  test
  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 \advanced_testcase {
  37      public function test_push_then_pop() {
  38          // Set up.
  39          $stack = new xhtml_container_stack();
  40          // Exercise SUT.
  41          $stack->push('testtype', '</div>');
  42          $html = $stack->pop('testtype');
  43          // Verify outcome.
  44          $this->assertEquals('</div>', $html);
  45          $this->assertDebuggingNotCalled();
  46      }
  47  
  48      public function test_mismatched_pop_prints_warning() {
  49          // Set up.
  50          $stack = new xhtml_container_stack();
  51          $stack->push('testtype', '</div>');
  52          // Exercise SUT.
  53          $html = $stack->pop('mismatch');
  54          // Verify outcome.
  55          $this->assertEquals('</div>', $html);
  56          $this->assertDebuggingCalled();
  57      }
  58  
  59      public function test_pop_when_empty_prints_warning() {
  60          // Set up.
  61          $stack = new xhtml_container_stack();
  62          // Exercise SUT.
  63          $html = $stack->pop('testtype');
  64          // Verify outcome.
  65          $this->assertEquals('', $html);
  66          $this->assertDebuggingCalled();
  67      }
  68  
  69      public function test_correct_nesting() {
  70          // Set up.
  71          $stack = new xhtml_container_stack();
  72          // Exercise SUT.
  73          $stack->push('testdiv', '</div>');
  74          $stack->push('testp', '</p>');
  75          $html2 = $stack->pop('testp');
  76          $html1 = $stack->pop('testdiv');
  77          // Verify outcome.
  78          $this->assertEquals('</p>', $html2);
  79          $this->assertEquals('</div>', $html1);
  80          $this->assertDebuggingNotCalled();
  81      }
  82  
  83      public function test_pop_all_but_last() {
  84          // Set up.
  85          $stack = new xhtml_container_stack();
  86          $stack->push('test1', '</h1>');
  87          $stack->push('test2', '</h2>');
  88          $stack->push('test3', '</h3>');
  89          // Exercise SUT.
  90          $html = $stack->pop_all_but_last();
  91          // Verify outcome.
  92          $this->assertEquals('</h3></h2>', $html);
  93          $this->assertDebuggingNotCalled();
  94          // Tear down.
  95          $stack->discard();
  96      }
  97  
  98      public function test_pop_all_but_last_only_one() {
  99          // Set up.
 100          $stack = new xhtml_container_stack();
 101          $stack->push('test1', '</h1>');
 102          // Exercise SUT.
 103          $html = $stack->pop_all_but_last();
 104          // Verify outcome.
 105          $this->assertEquals('', $html);
 106          $this->assertDebuggingNotCalled();
 107          // Tear down.
 108          $stack->discard();
 109      }
 110  
 111      public function test_pop_all_but_last_empty() {
 112          // Set up.
 113          $stack = new xhtml_container_stack();
 114          // Exercise SUT.
 115          $html = $stack->pop_all_but_last();
 116          // Verify outcome.
 117          $this->assertEquals('', $html);
 118          $this->assertDebuggingNotCalled();
 119      }
 120  
 121      public function test_discard() {
 122          // Set up.
 123          $stack = new xhtml_container_stack();
 124          $stack->push('test1', '</somethingdistinctive>');
 125          $stack->discard();
 126          // Exercise SUT.
 127          $stack = null;
 128          // Verify outcome.
 129          $this->assertDebuggingNotCalled();
 130      }
 131  }