| 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_backup;
  | 
        
        
            | 
            | 
           18 | 
              | 
        
        
            | 
            | 
           19 | 
           use backup;
  | 
        
        
            | 
            | 
           20 | 
           use base_task;
  | 
        
        
            | 
            | 
           21 | 
           use base_task_exception;
  | 
        
        
            | 
            | 
           22 | 
           use backup_controller;
  | 
        
        
            | 
            | 
           23 | 
           use backup_plan;
  | 
        
        
            | 
            | 
           24 | 
           use backup_task;
  | 
        
        
            | 
            | 
           25 | 
           use backup_task_exception;
  | 
        
        
            | 
            | 
           26 | 
              | 
        
        
            | 
            | 
           27 | 
           defined('MOODLE_INTERNAL') || die();
  | 
        
        
            | 
            | 
           28 | 
              | 
        
        
            | 
            | 
           29 | 
           require_once(__DIR__.'/fixtures/plan_fixtures.php');
  | 
        
        
            | 
            | 
           30 | 
              | 
        
        
            | 
            | 
           31 | 
           /**
  | 
        
        
            | 
            | 
           32 | 
            * @package    core_backup
  | 
        
        
            | 
            | 
           33 | 
            * @category   test
  | 
        
        
            | 
            | 
           34 | 
            * @copyright  2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
  | 
        
        
            | 
            | 
           35 | 
            * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  | 
        
        
            | 
            | 
           36 | 
            */
  | 
        
        
            | 
            | 
           37 | 
           class task_test extends \advanced_testcase {
  | 
        
        
            | 
            | 
           38 | 
              | 
        
        
            | 
            | 
           39 | 
               protected $moduleid;  // course_modules id used for testing
  | 
        
        
            | 
            | 
           40 | 
               protected $sectionid; // course_sections id used for testing
  | 
        
        
            | 
            | 
           41 | 
               protected $courseid;  // course id used for testing
  | 
        
        
            | 
            | 
           42 | 
               protected $userid;      // user record used for testing
  | 
        
        
            | 
            | 
           43 | 
              | 
        
        
            | 
            | 
           44 | 
               protected function setUp(): void {
  | 
        
        
            | 
            | 
           45 | 
                   global $DB, $CFG;
  | 
        
        
            | 
            | 
           46 | 
                   parent::setUp();
  | 
        
        
            | 
            | 
           47 | 
              | 
        
        
            | 
            | 
           48 | 
                   $this->resetAfterTest(true);
  | 
        
        
            | 
            | 
           49 | 
              | 
        
        
            | 
            | 
           50 | 
                   $course = $this->getDataGenerator()->create_course();
  | 
        
        
            | 
            | 
           51 | 
                   $page = $this->getDataGenerator()->create_module('page', array('course'=>$course->id), array('section'=>3));
  | 
        
        
            | 
            | 
           52 | 
                   $coursemodule = $DB->get_record('course_modules', array('id'=>$page->cmid));
  | 
        
        
            | 
            | 
           53 | 
              | 
        
        
            | 
            | 
           54 | 
                   $this->moduleid  = $coursemodule->id;
  | 
        
        
            | 
            | 
           55 | 
                   $this->sectionid = $DB->get_field("course_sections", 'id', array("section"=>$coursemodule->section, "course"=>$course->id));
  | 
        
        
            | 
            | 
           56 | 
                   $this->courseid  = $coursemodule->course;
  | 
        
        
            | 
            | 
           57 | 
                   $this->userid = 2; // admin
  | 
        
        
            | 
            | 
           58 | 
              | 
        
        
            | 
            | 
           59 | 
                   // Disable all loggers
  | 
        
        
            | 
            | 
           60 | 
                   $CFG->backup_error_log_logger_level = backup::LOG_NONE;
  | 
        
        
            | 
            | 
           61 | 
                   $CFG->backup_file_logger_level = backup::LOG_NONE;
  | 
        
        
            | 
            | 
           62 | 
                   $CFG->backup_database_logger_level = backup::LOG_NONE;
  | 
        
        
            | 
            | 
           63 | 
                   $CFG->backup_file_logger_level_extra = backup::LOG_NONE;
  | 
        
        
            | 
            | 
           64 | 
               }
  | 
        
        
            | 
            | 
           65 | 
              | 
        
        
            | 
            | 
           66 | 
               /**
  | 
        
        
            | 
            | 
           67 | 
                * test base_task class
  | 
        
        
            | 
            | 
           68 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           69 | 
               function test_base_task(): void {
  | 
        
        
           | 1 | 
           efrain | 
           70 | 
              | 
        
        
            | 
            | 
           71 | 
                   $bp = new \mock_base_plan('planname'); // We need one plan
  | 
        
        
            | 
            | 
           72 | 
                   // Instantiate
  | 
        
        
            | 
            | 
           73 | 
                   $bt = new \mock_base_task('taskname', $bp);
  | 
        
        
            | 
            | 
           74 | 
                   $this->assertTrue($bt instanceof base_task);
  | 
        
        
            | 
            | 
           75 | 
                   $this->assertEquals($bt->get_name(), 'taskname');
  | 
        
        
            | 
            | 
           76 | 
                   $this->assertTrue(is_array($bt->get_settings()));
  | 
        
        
            | 
            | 
           77 | 
                   $this->assertEquals(count($bt->get_settings()), 0);
  | 
        
        
            | 
            | 
           78 | 
                   $this->assertTrue(is_array($bt->get_steps()));
  | 
        
        
            | 
            | 
           79 | 
                   $this->assertEquals(count($bt->get_steps()), 0);
  | 
        
        
            | 
            | 
           80 | 
               }
  | 
        
        
            | 
            | 
           81 | 
              | 
        
        
            | 
            | 
           82 | 
               /**
  | 
        
        
            | 
            | 
           83 | 
                * test backup_task class
  | 
        
        
            | 
            | 
           84 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           85 | 
               function test_backup_task(): void {
  | 
        
        
           | 1 | 
           efrain | 
           86 | 
              | 
        
        
            | 
            | 
           87 | 
                   // We need one (non interactive) controller for instatiating plan
  | 
        
        
            | 
            | 
           88 | 
                   $bc = new backup_controller(backup::TYPE_1ACTIVITY, $this->moduleid, backup::FORMAT_MOODLE,
  | 
        
        
            | 
            | 
           89 | 
                       backup::INTERACTIVE_NO, backup::MODE_GENERAL, $this->userid);
  | 
        
        
            | 
            | 
           90 | 
                   // We need one plan
  | 
        
        
            | 
            | 
           91 | 
                   $bp = new backup_plan($bc);
  | 
        
        
            | 
            | 
           92 | 
                   // Instantiate task
  | 
        
        
            | 
            | 
           93 | 
                   $bt = new \mock_backup_task('taskname', $bp);
  | 
        
        
            | 
            | 
           94 | 
                   $this->assertTrue($bt instanceof backup_task);
  | 
        
        
            | 
            | 
           95 | 
                   $this->assertEquals($bt->get_name(), 'taskname');
  | 
        
        
            | 
            | 
           96 | 
              | 
        
        
            | 
            | 
           97 | 
                   // Calculate checksum and check it
  | 
        
        
            | 
            | 
           98 | 
                   $checksum = $bt->calculate_checksum();
  | 
        
        
            | 
            | 
           99 | 
                   $this->assertTrue($bt->is_checksum_correct($checksum));
  | 
        
        
            | 
            | 
           100 | 
              | 
        
        
            | 
            | 
           101 | 
                   $bc->destroy();
  | 
        
        
            | 
            | 
           102 | 
               }
  | 
        
        
            | 
            | 
           103 | 
              | 
        
        
            | 
            | 
           104 | 
               /**
  | 
        
        
            | 
            | 
           105 | 
                * wrong base_task class tests
  | 
        
        
            | 
            | 
           106 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           107 | 
               function test_base_task_wrong(): void {
  | 
        
        
           | 1 | 
           efrain | 
           108 | 
              | 
        
        
            | 
            | 
           109 | 
                   // Try to pass one wrong plan
  | 
        
        
            | 
            | 
           110 | 
                   try {
  | 
        
        
            | 
            | 
           111 | 
                       $bt = new \mock_base_task('tasktest', new \stdClass());
  | 
        
        
            | 
            | 
           112 | 
                       $this->assertTrue(false, 'base_task_exception expected');
  | 
        
        
            | 
            | 
           113 | 
                   } catch (\Exception $e) {
  | 
        
        
            | 
            | 
           114 | 
                       $this->assertTrue($e instanceof base_task_exception);
  | 
        
        
            | 
            | 
           115 | 
                       $this->assertEquals($e->errorcode, 'wrong_base_plan_specified');
  | 
        
        
            | 
            | 
           116 | 
                   }
  | 
        
        
            | 
            | 
           117 | 
              | 
        
        
            | 
            | 
           118 | 
                   // Add wrong step to task
  | 
        
        
            | 
            | 
           119 | 
                   $bp = new \mock_base_plan('planname'); // We need one plan
  | 
        
        
            | 
            | 
           120 | 
                   // Instantiate
  | 
        
        
            | 
            | 
           121 | 
                   $bt = new \mock_base_task('taskname', $bp);
  | 
        
        
            | 
            | 
           122 | 
                   try {
  | 
        
        
            | 
            | 
           123 | 
                       $bt->add_step(new \stdClass());
  | 
        
        
            | 
            | 
           124 | 
                       $this->assertTrue(false, 'base_task_exception expected');
  | 
        
        
            | 
            | 
           125 | 
                   } catch (\Exception $e) {
  | 
        
        
            | 
            | 
           126 | 
                       $this->assertTrue($e instanceof base_task_exception);
  | 
        
        
            | 
            | 
           127 | 
                       $this->assertEquals($e->errorcode, 'wrong_base_step_specified');
  | 
        
        
            | 
            | 
           128 | 
                   }
  | 
        
        
            | 
            | 
           129 | 
              | 
        
        
            | 
            | 
           130 | 
               }
  | 
        
        
            | 
            | 
           131 | 
              | 
        
        
            | 
            | 
           132 | 
               /**
  | 
        
        
            | 
            | 
           133 | 
                * wrong backup_task class tests
  | 
        
        
            | 
            | 
           134 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           135 | 
               function test_backup_task_wrong(): void {
  | 
        
        
           | 1 | 
           efrain | 
           136 | 
              | 
        
        
            | 
            | 
           137 | 
                   // Try to pass one wrong plan
  | 
        
        
            | 
            | 
           138 | 
                   try {
  | 
        
        
            | 
            | 
           139 | 
                       $bt = new \mock_backup_task('tasktest', new \stdClass());
  | 
        
        
            | 
            | 
           140 | 
                       $this->assertTrue(false, 'backup_task_exception expected');
  | 
        
        
            | 
            | 
           141 | 
                   } catch (\Exception $e) {
  | 
        
        
            | 
            | 
           142 | 
                       $this->assertTrue($e instanceof backup_task_exception);
  | 
        
        
            | 
            | 
           143 | 
                       $this->assertEquals($e->errorcode, 'wrong_backup_plan_specified');
  | 
        
        
            | 
            | 
           144 | 
                   }
  | 
        
        
            | 
            | 
           145 | 
               }
  | 
        
        
            | 
            | 
           146 | 
           }
  |