Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 39 and 401]

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