Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

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