Merge branch 'MDL-64012' of https://github.com/timhunt/moodle
[moodle.git] / lib / tests / ajaxlib_test.php
blob18b459463a2455cd830053ce76141db2bbaab47f
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.
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/>.
17 /**
18 * Code quality unit tests that are fast enough to run each time.
20 * @package core
21 * @category phpunit
22 * @copyright 2013 Andrew Nicols
23 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
26 defined('MOODLE_INTERNAL') || die();
28 class core_ajaxlib_testcase extends advanced_testcase {
29 /** @var string Original error log */
30 protected $oldlog;
32 protected function setUp() {
33 global $CFG;
35 parent::setUp();
36 // Discard error logs.
37 $this->oldlog = ini_get('error_log');
38 ini_set('error_log', "$CFG->dataroot/testlog.log");
41 protected function tearDown() {
42 ini_set('error_log', $this->oldlog);
43 parent::tearDown();
46 protected function helper_test_clean_output() {
47 $this->resetAfterTest();
49 $result = ajax_capture_output();
51 // ob_start should normally return without issue.
52 $this->assertTrue($result);
54 $result = ajax_check_captured_output();
55 $this->assertEmpty($result);
58 protected function helper_test_dirty_output($expectexception = false) {
59 $this->resetAfterTest();
61 // Keep track of the content we will output.
62 $content = "Some example content";
64 $result = ajax_capture_output();
66 // ob_start should normally return without issue.
67 $this->assertTrue($result);
69 // Fill the output buffer.
70 echo $content;
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);
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();
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();
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();
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();
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();
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();
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();
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);