Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 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_backup\hook;
18
 
19
use core\di;
20
use copy_helper;
21
use advanced_testcase;
22
use core\hook\manager;
23
use core\task\manager as taskmanager;
24
use core\event\course_restored;
25
use core_backup\hook\fixtures\copy_course_hook_callbacks;
26
 
27
/**
28
 * Class to test the hook inside asynchronous_copy_task.
29
 *
30
 * @package core_backup
31
 * @copyright 2024 Monash University (https://www.monash.edu)
32
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33
 */
34
final class copy_course_hook_test extends advanced_testcase {
35
 
36
    /**
37
     * Test the hook.
38
     *
39
     * @covers \core\task\asynchronous_copy_task::execute
40
     * @covers \core_backup\hook\before_copy_course_execute
41
     */
42
    public function test_copy_course_hook(): void {
43
        // Load the callback classes.
44
        require_once(__DIR__ . '/fixtures/copy_course_hooks.php');
45
 
46
        // Replace the version of the manager in the DI container with a phpunit one.
47
        di::set(
48
            manager::class,
49
            manager::phpunit_get_instance([
50
                // Load a list of hooks for `test_plugin1` from the fixture file.
51
                'test_plugin1' => __DIR__ .
52
                    '/fixtures/copy_course_hooks.php',
53
            ]),
54
        );
55
 
56
        $this->resetAfterTest(true);
57
        $this->setAdminUser();
58
 
59
        $generator = $this->getDataGenerator();
60
        $course = $generator->create_course();
61
 
62
        $copydata = (object) [
63
            'courseid' => $course->id,
64
            'fullname' => 'Name',
65
            'shortname' => 'name',
66
            'category' => 1,
67
            'visible' => 1,
68
            'startdate' => '123456789',
69
            'enddate' => '123456789',
70
            'idnumber' => 'dnum',
71
            'userdata' => false,
72
        ];
73
 
74
        $processed = copy_helper::process_formdata($copydata);
75
        copy_helper::create_copy($processed);
76
        $sink = $this->redirectEvents();
77
 
78
        // Capture mtrace output.
79
        ob_start();
80
 
81
        // Execute adhoc task.
82
        $now = time();
83
        $task = taskmanager::get_next_adhoc_task($now);
84
        $this->assertInstanceOf('\\core\\task\\asynchronous_copy_task', $task);
85
        $task->execute();
86
        taskmanager::adhoc_task_complete($task);
87
 
88
        ob_get_clean();
89
 
90
        $this->assertGreaterThan(0, copy_course_hook_callbacks::$count);
91
 
92
        // Check that the restore settings have been added to the event data.
93
        $events = $sink->get_events();
94
        $count = 0;
95
        foreach ($events as $event) {
96
            if ($event instanceof course_restored) {
97
                $count++;
98
                $data = $event->get_data();
99
                $this->assertNotEmpty($data['other']['settings']);
100
            }
101
        }
102
        $this->assertGreaterThan(0, $count);
103
    }
104
}