| 1 | efrain | 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 |  */
 | 
        
           | 1441 | ariadna | 29 | final class ajaxlib_test extends \advanced_testcase {
 | 
        
           | 1 | efrain | 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 |   | 
        
           | 11 | efrain | 82 |     public function test_output_capture_normal_debug_none(): void {
 | 
        
           | 1 | efrain | 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 |   | 
        
           | 11 | efrain | 88 |     public function test_output_capture_normal_debug_normal(): void {
 | 
        
           | 1 | efrain | 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 |   | 
        
           | 11 | efrain | 94 |     public function test_output_capture_normal_debug_all(): void {
 | 
        
           | 1 | efrain | 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 |   | 
        
           | 11 | efrain | 100 |     public function test_output_capture_normal_debugdeveloper(): void {
 | 
        
           | 1 | efrain | 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 |   | 
        
           | 11 | efrain | 106 |     public function test_output_capture_error_debug_none(): void {
 | 
        
           | 1 | efrain | 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 |   | 
        
           | 11 | efrain | 112 |     public function test_output_capture_error_debug_normal(): void {
 | 
        
           | 1 | efrain | 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 |   | 
        
           | 11 | efrain | 118 |     public function test_output_capture_error_debug_all(): void {
 | 
        
           | 1441 | ariadna | 119 |         // In error conditions, and with DEBUG_ALL set, we should throw an exceptions.
 | 
        
           | 1 | efrain | 120 |         set_debugging(DEBUG_ALL);
 | 
        
           | 1441 | ariadna | 121 |         $this->helper_test_dirty_output(true);
 | 
        
           | 1 | efrain | 122 |     }
 | 
        
           |  |  | 123 |   | 
        
           | 11 | efrain | 124 |     public function test_output_capture_error_debugdeveloper(): void {
 | 
        
           | 1 | efrain | 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 | }
 |