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\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 |
$controller = $rcmgetbackup->invoke($packager);
|
|
|
55 |
|
|
|
56 |
$rcmremove = $rc->getMethod('remove_unselected_activities');
|
|
|
57 |
$rcmremove->invoke($packager, $controller);
|
|
|
58 |
|
|
|
59 |
// Fetch all backup task settings for asserting them.
|
|
|
60 |
$finalsetting = [];
|
|
|
61 |
foreach ($controller->get_plan()->get_tasks() as $task) {
|
|
|
62 |
if (! $task instanceof backup_activity_task) { // Only for activity tasks.
|
|
|
63 |
continue;
|
|
|
64 |
}
|
|
|
65 |
$tasksettings = $task->get_settings();
|
|
|
66 |
foreach ($tasksettings as $setting) {
|
|
|
67 |
if (in_array($task->get_moduleid(), [$page1->cmid, $page2->cmid]) &&
|
|
|
68 |
strpos($setting->get_name(), '_included') !== false) {
|
|
|
69 |
$finalsetting[$task->get_moduleid()] = [
|
|
|
70 |
'name' => $setting->get_name(),
|
|
|
71 |
'value' => $setting->get_value(),
|
|
|
72 |
];
|
|
|
73 |
}
|
|
|
74 |
}
|
|
|
75 |
}
|
|
|
76 |
|
|
|
77 |
// Check the number of partial sharing tasks.
|
|
|
78 |
// Expected 2, Page 1 and Page 2.
|
|
|
79 |
$this->assertCount(2, $finalsetting);
|
|
|
80 |
|
|
|
81 |
// Check the value of the task of Page 1. 1 mean enabled, the backup will include the Page 1 activity.
|
|
|
82 |
$this->assertEquals('page_' . $page1->cmid . '_included', $finalsetting[$page1->cmid]['name']);
|
|
|
83 |
$this->assertEquals(1, $finalsetting[$page1->cmid]['value']);
|
|
|
84 |
|
|
|
85 |
// Check the value of the task of Page 2. 0 mean disabled, the backup will not include the Page 2 activity.
|
|
|
86 |
$this->assertEquals('page_' . $page2->cmid . '_included', $finalsetting[$page2->cmid]['name']);
|
|
|
87 |
$this->assertEquals(0, $finalsetting[$page2->cmid]['value']);
|
|
|
88 |
|
|
|
89 |
// We have finished with the backup controller, so destroy it.
|
|
|
90 |
$controller->destroy();
|
|
|
91 |
}
|
|
|
92 |
}
|