Differences Between: [Versions 310 and 403] [Versions 39 and 403]
1 <?php 2 3 // This file is part of Moodle - http://moodle.org/ 4 // 5 // Moodle is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // Moodle is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU General Public License for more details. 14 // 15 // You should have received a copy of the GNU General Public License 16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 17 18 namespace core_backup; 19 20 use backup; 21 use backup_check; 22 use backup_controller; 23 use backup_controller_exception; 24 25 defined('MOODLE_INTERNAL') || die(); 26 27 // Include all the needed stuff 28 global $CFG; 29 require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php'); 30 31 /** 32 * Check tests (all). 33 * 34 * @package core_backup 35 * @category test 36 * @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com} 37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 38 */ 39 class checks_test extends \advanced_testcase { 40 41 protected $moduleid; // course_modules id used for testing 42 protected $sectionid; // course_sections id used for testing 43 protected $courseid; // course id used for testing 44 protected $userid; // user record id 45 46 protected function setUp(): void { 47 global $DB, $CFG; 48 parent::setUp(); 49 50 $this->resetAfterTest(true); 51 52 $course = $this->getDataGenerator()->create_course(array(), array('createsections' => true)); 53 $page = $this->getDataGenerator()->create_module('page', array('course'=>$course->id), array('section'=>3)); 54 $coursemodule = $DB->get_record('course_modules', array('id'=>$page->cmid)); 55 56 $this->moduleid = $coursemodule->id; 57 $this->sectionid = $coursemodule->section; 58 $this->courseid = $coursemodule->course; 59 $this->userid = 2; // admin 60 61 $CFG->backup_error_log_logger_level = backup::LOG_NONE; 62 $CFG->backup_output_indented_logger_level = backup::LOG_NONE; 63 $CFG->backup_file_logger_level = backup::LOG_NONE; 64 $CFG->backup_database_logger_level = backup::LOG_NONE; 65 unset($CFG->backup_file_logger_extra); 66 $CFG->backup_file_logger_level_extra = backup::LOG_NONE; 67 } 68 69 /* 70 * test backup_check class 71 */ 72 public function test_backup_check() { 73 74 // Check against existing course module/section course or fail 75 $this->assertTrue(backup_check::check_id(backup::TYPE_1ACTIVITY, $this->moduleid)); 76 $this->assertTrue(backup_check::check_id(backup::TYPE_1SECTION, $this->sectionid)); 77 $this->assertTrue(backup_check::check_id(backup::TYPE_1COURSE, $this->courseid)); 78 $this->assertTrue(backup_check::check_user($this->userid)); 79 80 // Check against non-existing course module/section/course (0) 81 try { 82 backup_check::check_id(backup::TYPE_1ACTIVITY, 0); 83 $this->assertTrue(false, 'backup_controller_exception expected'); 84 } catch (\Exception $e) { 85 $this->assertTrue($e instanceof backup_controller_exception); 86 $this->assertEquals($e->errorcode, 'backup_check_module_not_exists'); 87 } 88 try { 89 backup_check::check_id(backup::TYPE_1SECTION, 0); 90 $this->assertTrue(false, 'backup_controller_exception expected'); 91 } catch (\exception $e) { 92 $this->assertTrue($e instanceof backup_controller_exception); 93 $this->assertEquals($e->errorcode, 'backup_check_section_not_exists'); 94 } 95 try { 96 backup_check::check_id(backup::TYPE_1COURSE, 0); 97 $this->assertTrue(false, 'backup_controller_exception expected'); 98 } catch (\Exception $e) { 99 $this->assertTrue($e instanceof backup_controller_exception); 100 $this->assertEquals($e->errorcode, 'backup_check_course_not_exists'); 101 } 102 103 // Try wrong type 104 try { 105 backup_check::check_id(12345678,0); 106 $this->assertTrue(false, 'backup_controller_exception expected'); 107 } catch (\Exception $e) { 108 $this->assertTrue($e instanceof backup_controller_exception); 109 $this->assertEquals($e->errorcode, 'backup_check_incorrect_type'); 110 } 111 112 // Test non-existing user 113 $userid = 0; 114 try { 115 backup_check::check_user($userid); 116 $this->assertTrue(false, 'backup_controller_exception expected'); 117 } catch (\Exception $e) { 118 $this->assertTrue($e instanceof backup_controller_exception); 119 $this->assertEquals($e->errorcode, 'backup_check_user_not_exists'); 120 } 121 122 // Security check tests 123 // Try to pass wrong controller 124 try { 125 backup_check::check_security(new \stdClass(), true); 126 $this->assertTrue(false, 'backup_controller_exception expected'); 127 } catch (\Exception $e) { 128 $this->assertTrue($e instanceof backup_controller_exception); 129 $this->assertEquals($e->errorcode, 'backup_check_security_requires_backup_controller'); 130 } 131 132 // Pass correct controller, check must return true in any case with $apply enabled 133 // and $bc must continue being mock_backup_controller 134 $bc = new backup_controller(backup::TYPE_1ACTIVITY, $this->moduleid, backup::FORMAT_MOODLE, 135 backup::INTERACTIVE_NO, backup::MODE_GENERAL, $this->userid); 136 $this->assertTrue(backup_check::check_security($bc, true)); 137 $this->assertTrue($bc instanceof backup_controller); 138 $bc->destroy(); 139 140 } 141 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body