/lib/tests/ajaxlib_test.php

https://github.com/markn86/moodle · PHP · 129 lines · 68 code · 25 blank · 36 comment · 3 complexity · 0262e2785f0206103d2dfb76e37f4b05 MD5 · raw file

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