Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
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
/**
18
 * coursecompleted enrolment plugin tests.
19
 *
20
 * @package   enrol_coursecompleted
21
 * @copyright 2017 eWallah (www.eWallah.net)
22
 * @author    Renaat Debleu <info@eWallah.net>
23
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24
 */
25
 
26
namespace enrol_coursecompleted;
27
 
28
use advanced_testcase;
29
use stdClass;
30
 
31
/**
32
 * coursecompleted enrolment plugin tests.
33
 *
34
 * @package   enrol_coursecompleted
35
 * @copyright 2017 eWallah (www.eWallah.net)
36
 * @author    Renaat Debleu <info@eWallah.net>
37
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38
 * @coversDefaultClass \enrol_coursecompleted_plugin
39
 */
40
final class backup_test extends advanced_testcase {
41
    /** @var stdClass Student. */
42
    private $student;
43
 
44
    /** @var stdClass First course. */
45
    private $course1;
46
 
47
    /** @var stdClass Second course. */
48
    private $course2;
49
 
50
    /**
51
     * Tests initial setup.
52
     */
53
    protected function setUp(): void {
54
        global $CFG, $DB;
55
 
56
        $CFG->enablecompletion = true;
57
        $this->resetAfterTest(true);
58
        $enabled = enrol_get_plugins(true);
59
        $enabled['coursecompleted'] = true;
60
        set_config('enrol_plugins_enabled', implode(',', array_keys($enabled)));
61
        $generator = $this->getDataGenerator();
62
        $this->course1 = $generator->create_course(['shortname' => 'A1', 'enablecompletion' => 1]);
63
        $this->course2 = $generator->create_course(['shortname' => 'A2', 'enablecompletion' => 1]);
64
        $studentrole = $DB->get_field('role', 'id', ['shortname' => 'student']);
65
        $this->setAdminUser();
66
        $plugin = enrol_get_plugin('coursecompleted');
67
        $plugin->add_instance(
68
            $this->course2,
69
            ['customint1' => $this->course1->id, 'customint2' => 0, 'roleid' => $studentrole]
70
        );
71
        $this->student = $generator->create_and_enrol($this->course1, 'student');
72
    }
73
 
74
    /**
75
     * Test backup.
76
     * @covers \enrol_coursecompleted_plugin
77
     */
78
    public function test_backup_restore(): void {
79
        global $CFG, $DB, $PAGE;
80
        require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php');
81
        require_once($CFG->dirroot . '/backup/util/includes/restore_includes.php');
82
        require_once($CFG->dirroot . '/enrol/locallib.php');
83
 
84
        $ccompletion = new \completion_completion(['course' => $this->course1->id, 'userid' => $this->student->id]);
85
        $ccompletion->mark_complete(time());
86
        $this->runAdhocTasks();
87
        $bc = new \backup_controller(
88
            \backup::TYPE_1COURSE,
89
            $this->course2->id,
90
            \backup::FORMAT_MOODLE,
91
            \backup::INTERACTIVE_NO,
92
            \backup::MODE_GENERAL,
93
            2
94
        );
95
        $bc->execute_plan();
96
        $results = $bc->get_results();
97
        $file = $results['backup_destination'];
98
        $fp = get_file_packer('application/vnd.moodle.backup');
99
        $filepath = $CFG->dataroot . '/temp/backup/test-restore-course-event';
100
        $file->extract_to_pathname($fp, $filepath);
101
        $bc->destroy();
102
        $rc = new \restore_controller(
103
            'test-restore-course-event',
104
            $this->course2->id,
105
            \backup::INTERACTIVE_NO,
106
            \backup::MODE_GENERAL,
107
            2,
108
            \backup::TARGET_NEW_COURSE
109
        );
110
        $rc->execute_precheck();
111
        $rc->execute_plan();
112
        $newid = $rc->get_courseid();
113
        $rc->destroy();
114
        $this->assertEquals(2, $DB->count_records('enrol', ['enrol' => 'coursecompleted']));
115
        $this->assertTrue(is_enrolled(\context_course::instance($newid), $this->student->id));
116
        $url = new \moodle_url('/user/index.php', ['id' => $newid]);
117
        $PAGE->set_url($url);
118
        $course = get_course($newid);
119
        $manager = new \course_enrolment_manager($PAGE, $course);
120
        $enrolments = $manager->get_user_enrolments($this->student->id);
121
        $this->assertCount(2, $enrolments);
122
        $bc = new \backup_controller(
123
            \backup::TYPE_1COURSE,
124
            $this->course2->id,
125
            \backup::FORMAT_MOODLE,
126
            \backup::INTERACTIVE_NO,
127
            \backup::MODE_GENERAL,
128
            2
129
        );
130
        $bc->execute_plan();
131
        $results = $bc->get_results();
132
        $file = $results['backup_destination'];
133
        $fp = get_file_packer('application/vnd.moodle.backup');
134
        $filepath = $CFG->dataroot . '/temp/backup/test-restore-course-event';
135
        $file->extract_to_pathname($fp, $filepath);
136
        $bc->destroy();
137
        $rc = new \restore_controller(
138
            'test-restore-course-event',
139
            $newid,
140
            \backup::INTERACTIVE_NO,
141
            \backup::MODE_GENERAL,
142
            2,
143
            \backup::TARGET_EXISTING_ADDING
144
        );
145
        $rc->execute_precheck();
146
        $rc->execute_plan();
147
        $rc->destroy();
148
        $this->assertEquals(2, $DB->count_records('enrol', ['enrol' => 'coursecompleted']));
149
        $this->assertTrue(is_enrolled(\context_course::instance($newid), $this->student->id));
150
    }
151
}