Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

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

   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  /**
  20   * Code quality unit tests that are fast enough to run each time.
  21   *
  22   * @package    core
  23   * @category   test
  24   * @copyright  2013 Andrew Nicols
  25   * @license    http://www.gnu.org/copyleft/gpl.html GNU Public License
  26   * @covers ::ajax_capture_output
  27   * @covers ::ajax_check_captured_output
  28   */
  29  class ajaxlib_test extends \advanced_testcase {
  30      /** @var string Original error log */
  31      protected $oldlog;
  32  
  33      protected function setUp(): void {
  34          global $CFG;
  35  
  36          parent::setUp();
  37          // Discard error logs.
  38          $this->oldlog = ini_get('error_log');
  39          ini_set('error_log', "$CFG->dataroot/testlog.log");
  40      }
  41  
  42      protected function tearDown(): void {
  43          ini_set('error_log', $this->oldlog);
  44          parent::tearDown();
  45      }
  46  
  47      protected function helper_test_clean_output() {
  48          $this->resetAfterTest();
  49  
  50          $result = ajax_capture_output();
  51  
  52          // ob_start should normally return without issue.
  53          $this->assertTrue($result);
  54  
  55          $result = ajax_check_captured_output();
  56          $this->assertEmpty($result);
  57      }
  58  
  59      protected function helper_test_dirty_output($expectexception = false) {
  60          $this->resetAfterTest();
  61  
  62          // Keep track of the content we will output.
  63          $content = "Some example content";
  64  
  65          $result = ajax_capture_output();
  66  
  67          // ob_start should normally return without issue.
  68          $this->assertTrue($result);
  69  
  70          // Fill the output buffer.
  71          echo $content;
  72  
  73          if ($expectexception) {
  74              $this->expectException('coding_exception');
  75              ajax_check_captured_output();
  76          } else {
  77              $result = ajax_check_captured_output();
  78              $this->assertEquals($result, $content);
  79          }
  80      }
  81  
  82      public function test_output_capture_normal_debug_none() {
  83          // In normal conditions, and with DEBUG_NONE set, we should not receive any output or throw any exceptions.
  84          set_debugging(DEBUG_NONE);
  85          $this->helper_test_clean_output();
  86      }
  87  
  88      public function test_output_capture_normal_debug_normal() {
  89          // In normal conditions, and with DEBUG_NORMAL set, we should not receive any output or throw any exceptions.
  90          set_debugging(DEBUG_NORMAL);
  91          $this->helper_test_clean_output();
  92      }
  93  
  94      public function test_output_capture_normal_debug_all() {
  95          // In normal conditions, and with DEBUG_ALL set, we should not receive any output or throw any exceptions.
  96          set_debugging(DEBUG_ALL);
  97          $this->helper_test_clean_output();
  98      }
  99  
 100      public function test_output_capture_normal_debugdeveloper() {
 101          // In normal conditions, and with DEBUG_DEVELOPER set, we should not receive any output or throw any exceptions.
 102          set_debugging(DEBUG_DEVELOPER);
 103          $this->helper_test_clean_output();
 104      }
 105  
 106      public function test_output_capture_error_debug_none() {
 107          // With DEBUG_NONE set, we should not throw any exception, but the output will be returned.
 108          set_debugging(DEBUG_NONE);
 109          $this->helper_test_dirty_output();
 110      }
 111  
 112      public function test_output_capture_error_debug_normal() {
 113          // With DEBUG_NORMAL set, we should not throw any exception, but the output will be returned.
 114          set_debugging(DEBUG_NORMAL);
 115          $this->helper_test_dirty_output();
 116      }
 117  
 118      public function test_output_capture_error_debug_all() {
 119          // In error conditions, and with DEBUG_ALL set, we should not receive any output or throw any exceptions.
 120          set_debugging(DEBUG_ALL);
 121          $this->helper_test_dirty_output();
 122      }
 123  
 124      public function test_output_capture_error_debugdeveloper() {
 125          // With DEBUG_DEVELOPER set, we should throw an exception.
 126          set_debugging(DEBUG_DEVELOPER);
 127          $this->helper_test_dirty_output(true);
 128      }
 129  
 130  }