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 backup_activity_task; 20 21 /** 22 * Unit tests for {@see \core\moodlenet\course_partial_packager}. 23 * 24 * @coversDefaultClass \core\moodlenet\course_partial_packager 25 * @package core 26 * @copyright 2023 Huong Nguyen <huongnv13@gmail.com> 27 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 28 */ 29 class course_partial_packager_test extends \advanced_testcase { 30 31 /** 32 * Test fetching task settings. 33 * 34 * @covers ::remove_unselected_activities 35 * @covers ::get_all_activity_tasks 36 * @covers ::get_backup_controller 37 */ 38 public function test_remove_unselected_activities(): void { 39 global $USER; 40 $this->resetAfterTest(); 41 $this->setAdminUser(); 42 43 $generator = $this->getDataGenerator(); 44 $course = $generator->create_course(); 45 $page1 = $generator->create_module('page', ['course' => $course->id]); 46 $page2 = $generator->create_module('page', ['course' => $course->id]); 47 48 // Load the course packager. 49 $packager = new course_partial_packager($course, [$page1->cmid], $USER->id); 50 51 // Fetch all backup task settings. 52 $rc = new \ReflectionClass(course_partial_packager::class); 53 $rcmgetbackup = $rc->getMethod('get_backup_controller'); 54 $rcmgetbackup->setAccessible(true); 55 $controller = $rcmgetbackup->invoke($packager); 56 57 $rcmremove = $rc->getMethod('remove_unselected_activities'); 58 $rcmremove->setAccessible(true); 59 $rcmremove->invoke($packager, $controller); 60 61 // Fetch all backup task settings for asserting them. 62 $finalsetting = []; 63 foreach ($controller->get_plan()->get_tasks() as $task) { 64 if (! $task instanceof backup_activity_task) { // Only for activity tasks. 65 continue; 66 } 67 $tasksettings = $task->get_settings(); 68 foreach ($tasksettings as $setting) { 69 if (in_array($task->get_moduleid(), [$page1->cmid, $page2->cmid]) && 70 strpos($setting->get_name(), '_included') !== false) { 71 $finalsetting[$task->get_moduleid()] = [ 72 'name' => $setting->get_name(), 73 'value' => $setting->get_value(), 74 ]; 75 } 76 } 77 } 78 79 // Check the number of partial sharing tasks. 80 // Expected 2, Page 1 and Page 2. 81 $this->assertCount(2, $finalsetting); 82 83 // Check the value of the task of Page 1. 1 mean enabled, the backup will include the Page 1 activity. 84 $this->assertEquals('page_' . $page1->cmid . '_included', $finalsetting[$page1->cmid]['name']); 85 $this->assertEquals(1, $finalsetting[$page1->cmid]['value']); 86 87 // Check the value of the task of Page 2. 0 mean disabled, the backup will not include the Page 2 activity. 88 $this->assertEquals('page_' . $page2->cmid . '_included', $finalsetting[$page2->cmid]['name']); 89 $this->assertEquals(0, $finalsetting[$page2->cmid]['value']); 90 91 // We have finished with the backup controller, so destroy it. 92 $controller->destroy(); 93 } 94 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body