Differences Between: [Versions 402 and 403]
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 * Unit tests for {@see activity_packager}. 23 * 24 * @coversDefaultClass \core\moodlenet\activity_packager 25 * @package core 26 * @copyright 2023 Michael Hawkins <michaelh@moodle.com> 27 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 28 */ 29 class activity_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 $assigndata = [ 46 'course' => $course->id, 47 'name' => 'Extremely interesting assignment', 48 'intro' => 'A great assignment to share', 49 ]; 50 $assign = $generator->create_module('assign', $assigndata); 51 $cminfo = get_fast_modinfo($course->id)->get_cm($assign->cmid); 52 $packager = new activity_packager($cminfo, $USER->id); 53 54 // Fetch all backup task settings. 55 $rc = new \ReflectionClass(activity_packager::class); 56 $rcmgetbackup = $rc->getMethod('get_backup_controller'); 57 $rcmgetbackup->setAccessible(true); 58 $controller = $rcmgetbackup->invoke($packager); 59 $rcmgetall = $rc->getMethod('get_all_task_settings'); 60 $rcmgetall->setAccessible(true); 61 $tasksettings = $rcmgetall->invoke($packager, $controller); 62 63 // Fetch the default settings and grab an example value (setting_root_users). 64 $rootsettings = $tasksettings[\backup_root_task::class]; 65 $testsettingname = 'setting_root_users'; 66 67 $oldvalue = 99; 68 foreach ($rootsettings as $setting) { 69 $name = $setting->get_ui_name(); 70 if ($name == $testsettingname) { 71 $oldvalue = $setting->get_value(); 72 break; 73 } 74 } 75 76 // Check we found the setting value (either 0 or 1 are valid). 77 $this->assertNotEquals(99, $oldvalue); 78 $this->assertLessThanOrEqual(1, $oldvalue); 79 80 // Override the setting_root_users value, then re-fetch the settings to check the change is reflected. 81 $overridevalue = ($oldvalue == 1) ? 0 : 1; 82 $rcmoverridesetting = $rc->getMethod('override_task_setting'); 83 $rcmoverridesetting->setAccessible(true); 84 $rcmoverridesetting->invoke($packager, $tasksettings, $testsettingname, $overridevalue); 85 $tasksettings = $rcmgetall->invoke($packager, $controller); 86 $rootsettings = $tasksettings[\backup_root_task::class]; 87 88 $newvalue = 99; 89 foreach ($rootsettings as $setting) { 90 $name = $setting->get_ui_name(); 91 if ($name == $testsettingname) { 92 $newvalue = $setting->get_value(); 93 break; 94 } 95 } 96 97 $this->assertEquals($overridevalue, $newvalue); 98 99 // We have finished with the backup controller, so destroy it. 100 $controller->destroy(); 101 } 102 103 /** 104 * Test overriding a backup task setting. 105 * 106 * @covers ::get_package 107 * @covers ::package 108 */ 109 public function test_get_package(): void { 110 global $USER; 111 $this->resetAfterTest(); 112 $this->setAdminUser(); 113 114 $currenttime = time(); 115 $generator = $this->getDataGenerator(); 116 $course = $generator->create_course(); 117 118 $assigndata = [ 119 'course' => $course->id, 120 'name' => 'Extremely interesting assignment', 121 'intro' => 'A great assignment to share', 122 ]; 123 $assign = $generator->create_module('assign', $assigndata); 124 $cminfo = get_fast_modinfo($course->id)->get_cm($assign->cmid); 125 $packager = new activity_packager($cminfo, $USER->id); 126 $package = $packager->get_package(); 127 128 // Confirm the expected stored_file object is returned. 129 $this->assertInstanceOf(\stored_file::class, $package); 130 131 // Check some known values in the returned stored_file object to confirm they match the file we have packaged. 132 $this->assertNotEmpty($package->get_contenthash()); 133 $this->assertEquals(user::instance($USER->id)->id, $package->get_contextid()); 134 $this->assertEquals('user', $package->get_component()); 135 $this->assertEquals('draft', $package->get_filearea()); 136 $this->assertEquals('assign_backup.mbz', $package->get_filename()); 137 $this->assertGreaterThan(0, $package->get_filesize()); 138 $timecreated = $package->get_timecreated(); 139 $this->assertGreaterThanOrEqual($currenttime, $timecreated); 140 $this->assertEquals($timecreated, $package->get_timemodified()); 141 } 142 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body