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\moodlenet; 18 19 use core\context\user; 20 21 /** 22 * Test coverage for moodlenet course packager. 23 * 24 * @package core 25 * @copyright 2023 Safat Shahin <safat.shahin@moodle.com> 26 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 27 * @coversDefaultClass \core\moodlenet\course_packager 28 */ 29 class course_packager_test extends \advanced_testcase { 30 31 /** 32 * Test fetching and overriding a backup task setting. 33 * 34 * @covers ::override_task_setting 35 * @covers ::get_all_task_settings 36 * @covers ::get_backup_controller 37 */ 38 public function test_override_task_setting(): void { 39 global $USER; 40 $this->resetAfterTest(); 41 $this->setAdminUser(); 42 43 $generator = $this->getDataGenerator(); 44 $course = $generator->create_course(); 45 46 // Load the course packager. 47 $packager = new course_packager($course, $USER->id); 48 49 // Fetch all backup task settings. 50 $rc = new \ReflectionClass(course_packager::class); 51 $rcmgetbackup = $rc->getMethod('get_backup_controller'); 52 $rcmgetbackup->setAccessible(true); 53 $controller = $rcmgetbackup->invoke($packager); 54 $rcmgetall = $rc->getMethod('get_all_task_settings'); 55 $rcmgetall->setAccessible(true); 56 $tasksettings = $rcmgetall->invoke($packager, $controller); 57 58 // Fetch the default settings and grab an example value (setting_root_users). 59 $rootsettings = $tasksettings[\backup_root_task::class]; 60 $testsettingname = 'setting_root_users'; 61 62 $oldvalue = 99; 63 foreach ($rootsettings as $setting) { 64 $name = $setting->get_ui_name(); 65 if ($name == $testsettingname) { 66 $oldvalue = $setting->get_value(); 67 break; 68 } 69 } 70 71 // Check we found the setting value (either 0 or 1 are valid). 72 $this->assertNotEquals(99, $oldvalue); 73 $this->assertLessThanOrEqual(1, $oldvalue); 74 75 // Override the setting_root_users value, then re-fetch the settings to check the change is reflected. 76 $overridevalue = ($oldvalue == 1) ? 0 : 1; 77 $rcmoverridesetting = $rc->getMethod('override_task_setting'); 78 $rcmoverridesetting->setAccessible(true); 79 $rcmoverridesetting->invoke($packager, $tasksettings, $testsettingname, $overridevalue); 80 $tasksettings = $rcmgetall->invoke($packager, $controller); 81 $rootsettings = $tasksettings[\backup_root_task::class]; 82 83 $newvalue = 99; 84 foreach ($rootsettings as $setting) { 85 $name = $setting->get_ui_name(); 86 if ($name == $testsettingname) { 87 $newvalue = $setting->get_value(); 88 break; 89 } 90 } 91 92 $this->assertEquals($overridevalue, $newvalue); 93 94 // We have finished with the backup controller, so destroy it. 95 $controller->destroy(); 96 } 97 98 /** 99 * Test the course package file. 100 * 101 * @covers ::get_package 102 * @covers ::package 103 */ 104 public function test_get_package(): void { 105 global $USER; 106 $this->resetAfterTest(); 107 $this->setAdminUser(); 108 109 $currenttime = time(); 110 $generator = $this->getDataGenerator(); 111 $course = $generator->create_course(); 112 113 // Load the course packager. 114 $packager = new course_packager($course, $USER->id); 115 $package = $packager->get_package(); 116 117 // Confirm the expected stored_file object is returned. 118 $this->assertInstanceOf(\stored_file::class, $package); 119 120 // Check some known values in the returned stored_file object to confirm they match the file we have packaged. 121 $this->assertNotEmpty($package->get_contenthash()); 122 $this->assertEquals(user::instance($USER->id)->id, $package->get_contextid()); 123 $this->assertEquals('user', $package->get_component()); 124 $this->assertEquals('draft', $package->get_filearea()); 125 $this->assertEquals($course->shortname . '_backup.mbz', $package->get_filename()); 126 $this->assertGreaterThan(0, $package->get_filesize()); 127 $timecreated = $package->get_timecreated(); 128 $this->assertGreaterThanOrEqual($currenttime, $timecreated); 129 $this->assertEquals($timecreated, $package->get_timemodified()); 130 } 131 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body