Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 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 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403] [Versions 39 and 310]

   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   * @package    core_backup
  19   * @category   phpunit
  20   * @copyright  2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
  21   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22   */
  23  
  24  defined('MOODLE_INTERNAL') || die();
  25  
  26  // Include all the needed stuff
  27  global $CFG;
  28  require_once($CFG->dirroot . '/backup/util/interfaces/checksumable.class.php');
  29  require_once($CFG->dirroot . '/backup/backup.class.php');
  30  require_once($CFG->dirroot . '/backup/util/loggers/base_logger.class.php');
  31  require_once($CFG->dirroot . '/backup/util/loggers/error_log_logger.class.php');
  32  require_once($CFG->dirroot . '/backup/util/loggers/output_indented_logger.class.php');
  33  require_once($CFG->dirroot . '/backup/util/loggers/database_logger.class.php');
  34  require_once($CFG->dirroot . '/backup/util/loggers/file_logger.class.php');
  35  require_once($CFG->dirroot . '/backup/util/factories/backup_factory.class.php');
  36  
  37  
  38  /**
  39   * backup_factory tests (all)
  40   */
  41  class backup_factories_testcase extends advanced_testcase {
  42  
  43      public function setUp(): void {
  44          global $CFG;
  45          parent::setUp();
  46  
  47          $this->resetAfterTest(true);
  48  
  49          $CFG->backup_error_log_logger_level = backup::LOG_NONE;
  50          $CFG->backup_output_indented_logger_level = backup::LOG_NONE;
  51          $CFG->backup_file_logger_level = backup::LOG_NONE;
  52          $CFG->backup_database_logger_level = backup::LOG_NONE;
  53          unset($CFG->backup_file_logger_extra);
  54          $CFG->backup_file_logger_level_extra = backup::LOG_NONE;
  55      }
  56  
  57      /**
  58       * test get_logger_chain() method
  59       */
  60      public function test_backup_factory() {
  61          global $CFG;
  62  
  63          // Default instantiate, all levels = backup::LOG_NONE
  64          // With debugdisplay enabled
  65          $CFG->debugdisplay = true;
  66          $logger1 = backup_factory::get_logger_chain(backup::INTERACTIVE_YES, backup::EXECUTION_INMEDIATE, 'test');
  67          $this->assertTrue($logger1 instanceof error_log_logger);  // 1st logger is error_log_logger
  68          $this->assertEquals($logger1->get_level(), backup::LOG_NONE);
  69          $logger2 = $logger1->get_next();
  70          $this->assertTrue($logger2 instanceof output_indented_logger);  // 2nd logger is output_indented_logger
  71          $this->assertEquals($logger2->get_level(), backup::LOG_NONE);
  72          $logger3 = $logger2->get_next();
  73          $this->assertTrue($logger3 instanceof file_logger);  // 3rd logger is file_logger
  74          $this->assertEquals($logger3->get_level(), backup::LOG_NONE);
  75          $logger4 = $logger3->get_next();
  76          $this->assertTrue($logger4 instanceof database_logger);  // 4th logger is database_logger
  77          $this->assertEquals($logger4->get_level(), backup::LOG_NONE);
  78          $logger5 = $logger4->get_next();
  79          $this->assertTrue($logger5 === null);
  80  
  81          // With debugdisplay disabled
  82          $CFG->debugdisplay = false;
  83          $logger1 = backup_factory::get_logger_chain(backup::INTERACTIVE_YES, backup::EXECUTION_INMEDIATE, 'test');
  84          $this->assertTrue($logger1 instanceof error_log_logger);  // 1st logger is error_log_logger
  85          $this->assertEquals($logger1->get_level(), backup::LOG_NONE);
  86          $logger2 = $logger1->get_next();
  87          $this->assertTrue($logger2 instanceof file_logger);  // 2nd logger is file_logger
  88          $this->assertEquals($logger2->get_level(), backup::LOG_NONE);
  89          $logger3 = $logger2->get_next();
  90          $this->assertTrue($logger3 instanceof database_logger);  // 3rd logger is database_logger
  91          $this->assertEquals($logger3->get_level(), backup::LOG_NONE);
  92          $logger4 = $logger3->get_next();
  93          $this->assertTrue($logger4 === null);
  94  
  95          // Instantiate with debugging enabled and $CFG->backup_error_log_logger_level not set
  96          $CFG->debugdisplay = true;
  97          unset($CFG->backup_error_log_logger_level);
  98          $logger1 = backup_factory::get_logger_chain(backup::INTERACTIVE_YES, backup::EXECUTION_INMEDIATE, 'test');
  99          $this->assertTrue($logger1 instanceof error_log_logger);  // 1st logger is error_log_logger
 100          $this->assertEquals($logger1->get_level(), backup::LOG_DEBUG); // and must have backup::LOG_DEBUG level
 101          // Set $CFG->backup_error_log_logger_level to backup::LOG_WARNING and test again
 102          $CFG->backup_error_log_logger_level = backup::LOG_WARNING;
 103          $logger1 = backup_factory::get_logger_chain(backup::INTERACTIVE_YES, backup::EXECUTION_INMEDIATE, 'test');
 104          $this->assertTrue($logger1 instanceof error_log_logger);  // 1st logger is error_log_logger
 105          $this->assertEquals($logger1->get_level(), backup::LOG_WARNING); // and must have backup::LOG_WARNING level
 106  
 107          // Instantiate in non-interactive mode, output_indented_logger must be out
 108          $logger1 = backup_factory::get_logger_chain(backup::INTERACTIVE_NO, backup::EXECUTION_INMEDIATE, 'test');
 109          $logger2 = $logger1->get_next();
 110          $this->assertTrue($logger2 instanceof file_logger);  // 2nd logger is file_logger (output_indented_logger skiped)
 111  
 112          // Define extra file logger and instantiate, should be 5th and last logger
 113          $CFG->backup_file_logger_extra = $CFG->tempdir.'/test.html';
 114          $CFG->backup_file_logger_level_extra = backup::LOG_NONE;
 115          $logger1 = backup_factory::get_logger_chain(backup::INTERACTIVE_YES, backup::EXECUTION_INMEDIATE, 'test');
 116          $logger2 = $logger1->get_next();
 117          $logger3 = $logger2->get_next();
 118          $logger4 = $logger3->get_next();
 119          $logger5 = $logger4->get_next();
 120          $this->assertTrue($logger5 instanceof file_logger);  // 5rd logger is file_logger (extra)
 121          $this->assertEquals($logger3->get_level(), backup::LOG_NONE);
 122          $logger6 = $logger5->get_next();
 123          $this->assertTrue($logger6 === null);
 124      }
 125  }