Proyectos de Subversion Moodle

Rev

Rev 11 | | Comparar con el anterior | 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
namespace enrol_database;
18
 
19
/**
20
 * External database enrolment sync tests
21
 *
22
 * This also tests adodb drivers that are matching
23
 * our four supported Moodle database drivers.
24
 *
25
 * @package    enrol_database
26
 * @category   test
27
 * @copyright  2011 Petr Skoda {@link http://skodak.org}
28
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29
 */
1441 ariadna 30
final class sync_test extends \advanced_testcase {
1 efrain 31
    protected static $courses = array();
32
    protected static $users = array();
33
    protected static $roles = array();
34
 
35
    /** @var string Original error log */
36
    protected $oldlog;
37
 
38
    public static function tearDownAfterClass(): void {
39
        global $DB;
40
        // Apply sqlsrv native driver error and logging default
41
        // settings while finishing the AdoDB tests.
42
        if ($DB->get_dbfamily() === 'mssql') {
43
            sqlsrv_configure("WarningsReturnAsErrors", false);
44
            sqlsrv_configure("LogSubsystems", SQLSRV_LOG_SYSTEM_OFF);
45
            sqlsrv_configure("LogSeverity", SQLSRV_LOG_SEVERITY_ERROR);
46
        }
1441 ariadna 47
        parent::tearDownAfterClass();
1 efrain 48
    }
49
 
50
    protected function init_enrol_database() {
51
        global $DB, $CFG;
52
 
53
        // Discard error logs from AdoDB.
54
        $this->oldlog = ini_get('error_log');
55
        ini_set('error_log', "$CFG->dataroot/testlog.log");
56
 
57
        $dbman = $DB->get_manager();
58
 
59
        set_config('dbencoding', 'utf-8', 'enrol_database');
60
 
61
        set_config('dbhost', $CFG->dbhost, 'enrol_database');
62
        set_config('dbuser', $CFG->dbuser, 'enrol_database');
63
        set_config('dbpass', $CFG->dbpass, 'enrol_database');
64
        set_config('dbname', $CFG->dbname, 'enrol_database');
65
 
66
        if (!empty($CFG->dboptions['dbport'])) {
67
            set_config('dbhost', $CFG->dbhost.':'.$CFG->dboptions['dbport'], 'enrol_database');
68
        }
69
 
70
        switch ($DB->get_dbfamily()) {
71
 
72
            case 'mysql':
73
                set_config('dbtype', 'mysqli', 'enrol_database');
74
                set_config('dbsetupsql', "SET NAMES 'UTF-8'", 'enrol_database');
75
                set_config('dbsybasequoting', '0', 'enrol_database');
76
                if (!empty($CFG->dboptions['dbsocket'])) {
77
                    $dbsocket = $CFG->dboptions['dbsocket'];
78
                    if ((strpos($dbsocket, '/') === false and strpos($dbsocket, '\\') === false)) {
79
                        $dbsocket = ini_get('mysqli.default_socket');
80
                    }
81
                    set_config('dbtype', 'mysqli://'.rawurlencode($CFG->dbuser).':'.rawurlencode($CFG->dbpass).'@'.rawurlencode($CFG->dbhost).'/'.rawurlencode($CFG->dbname).'?socket='.rawurlencode($dbsocket), 'enrol_database');
82
                }
83
                break;
84
 
85
            case 'postgres':
86
                set_config('dbtype', 'postgres7', 'enrol_database');
87
                $setupsql = "SET NAMES 'UTF-8'";
88
                if (!empty($CFG->dboptions['dbschema'])) {
89
                    $setupsql .= "; SET search_path = '".$CFG->dboptions['dbschema']."'";
90
                }
91
                set_config('dbsetupsql', $setupsql, 'enrol_database');
92
                set_config('dbsybasequoting', '0', 'enrol_database');
93
                if (!empty($CFG->dboptions['dbsocket']) and ($CFG->dbhost === 'localhost' or $CFG->dbhost === '127.0.0.1')) {
94
                    if (strpos($CFG->dboptions['dbsocket'], '/') !== false) {
95
                        $socket = $CFG->dboptions['dbsocket'];
96
                        if (!empty($CFG->dboptions['dbport'])) {
97
                            $socket .= ':' . $CFG->dboptions['dbport'];
98
                        }
99
                        set_config('dbhost', $socket, 'enrol_database');
100
                    } else {
101
                      set_config('dbhost', '', 'enrol_database');
102
                    }
103
                }
104
                break;
105
 
106
            case 'mssql':
107
                set_config('dbtype', 'mssqlnative', 'enrol_database');
108
                set_config('dbsybasequoting', '1', 'enrol_database');
109
 
110
                // The native sqlsrv driver uses a comma as separator between host and port.
111
                $dbhost = $CFG->dbhost;
112
                if (!empty($dboptions['dbport'])) {
113
                    $dbhost .= ',' . $dboptions['dbport'];
114
                }
115
                set_config('dbhost', $dbhost, 'enrol_database');
116
                break;
117
 
118
            default:
119
                throw new exception('Unknown database driver '.get_class($DB));
120
        }
121
 
122
        // NOTE: It is stongly discouraged to create new tables in advanced_testcase classes,
123
        //       but there is no other simple way to test ext database enrol sync, so let's
124
        //       disable transactions are try to cleanup after the tests.
125
 
126
        $table = new \xmldb_table('enrol_database_test_enrols');
127
        $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
128
        $table->add_field('courseid', XMLDB_TYPE_CHAR, '255', null, null, null);
129
        $table->add_field('userid', XMLDB_TYPE_CHAR, '255', null, null, null);
130
        $table->add_field('roleid', XMLDB_TYPE_CHAR, '255', null, null, null);
131
        $table->add_field('otheruser', XMLDB_TYPE_CHAR, '1', null, XMLDB_NOTNULL, null, '0');
132
        $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
133
        if ($dbman->table_exists($table)) {
134
            $dbman->drop_table($table);
135
        }
136
        $dbman->create_table($table);
137
        set_config('remoteenroltable', $CFG->prefix.'enrol_database_test_enrols', 'enrol_database');
138
        set_config('remotecoursefield', 'courseid', 'enrol_database');
139
        set_config('remoteuserfield', 'userid', 'enrol_database');
140
        set_config('remoterolefield', 'roleid', 'enrol_database');
141
        set_config('remoteotheruserfield', 'otheruser', 'enrol_database');
142
 
143
        $table = new \xmldb_table('enrol_database_test_courses');
144
        $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
145
        $table->add_field('fullname', XMLDB_TYPE_CHAR, '255', null, null, null);
146
        $table->add_field('shortname', XMLDB_TYPE_CHAR, '255', null, null, null);
147
        $table->add_field('idnumber', XMLDB_TYPE_CHAR, '255', null, null, null);
148
        $table->add_field('category', XMLDB_TYPE_CHAR, '255', null, null, null);
1441 ariadna 149
        $table->add_field('startdate', XMLDB_TYPE_CHAR, '255', null, null, null);
150
        $table->add_field('enddate', XMLDB_TYPE_CHAR, '255', null, null, null);
1 efrain 151
        $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
152
        if ($dbman->table_exists($table)) {
153
            $dbman->drop_table($table);
154
        }
155
        $dbman->create_table($table);
156
        set_config('newcoursetable', $CFG->prefix.'enrol_database_test_courses', 'enrol_database');
157
        set_config('newcoursefullname', 'fullname', 'enrol_database');
158
        set_config('newcourseshortname', 'shortname', 'enrol_database');
159
        set_config('newcourseidnumber', 'idnumber', 'enrol_database');
160
        set_config('newcoursecategory', 'category', 'enrol_database');
161
 
162
        // Create some test users and courses.
163
        for ($i = 1; $i <= 4; $i++) {
164
            self::$courses[$i] = $this->getDataGenerator()->create_course(array('fullname' => 'Test course '.$i, 'shortname' => 'tc'.$i, 'idnumber' => 'courseid'.$i));
165
        }
166
 
167
        for ($i = 1; $i <= 10; $i++) {
168
            self::$users[$i] = $this->getDataGenerator()->create_user(array('username' => 'username'.$i, 'idnumber' => 'userid'.$i, 'email' => 'user'.$i.'@example.com'));
169
        }
170
 
171
        foreach (get_all_roles() as $role) {
172
            self::$roles[$role->shortname] = $role;
173
        }
174
    }
175
 
176
    protected function cleanup_enrol_database() {
177
        global $DB;
178
 
179
        $dbman = $DB->get_manager();
180
        $table = new \xmldb_table('enrol_database_test_enrols');
181
        $dbman->drop_table($table);
182
        $table = new \xmldb_table('enrol_database_test_courses');
183
        $dbman->drop_table($table);
184
 
185
        self::$courses = null;
186
        self::$users = null;
187
        self::$roles = null;
188
 
189
        ini_set('error_log', $this->oldlog);
190
    }
191
 
192
    protected function reset_enrol_database() {
193
        global $DB;
194
 
195
        $DB->delete_records('enrol_database_test_enrols', array());
196
        $DB->delete_records('enrol_database_test_courses', array());
197
 
198
        $plugin = enrol_get_plugin('database');
199
        $instances = $DB->get_records('enrol', array('enrol' => 'database'));
200
        foreach($instances as $instance) {
201
            $plugin->delete_instance($instance);
202
        }
203
    }
204
 
205
    protected function assertIsEnrolled($userindex, $courseindex, $status=null, $rolename = null) {
206
        global $DB;
207
        $dbinstance = $DB->get_record('enrol', array('courseid' => self::$courses[$courseindex]->id, 'enrol' => 'database'), '*', MUST_EXIST);
208
 
209
        $conditions = array('enrolid' => $dbinstance->id, 'userid' => self::$users[$userindex]->id);
210
        if ($status !== null) {
211
            $conditions['status'] = $status;
212
        }
213
        $this->assertTrue($DB->record_exists('user_enrolments', $conditions));
214
 
215
        $this->assertHasRoleAssignment($userindex, $courseindex, $rolename);
216
    }
217
 
218
    protected function assertHasRoleAssignment($userindex, $courseindex, $rolename = null) {
219
        global $DB;
220
        $dbinstance = $DB->get_record('enrol', array('courseid' => self::$courses[$courseindex]->id, 'enrol' => 'database'), '*', MUST_EXIST);
221
 
222
        $coursecontext = \context_course::instance(self::$courses[$courseindex]->id);
223
        if ($rolename === false) {
224
            $this->assertFalse($DB->record_exists('role_assignments', array('component' => 'enrol_database', 'itemid' => $dbinstance->id, 'userid' => self::$users[$userindex]->id, 'contextid' => $coursecontext->id)));
225
        } else if ($rolename !== null) {
226
            $this->assertTrue($DB->record_exists('role_assignments', array('component' => 'enrol_database', 'itemid' => $dbinstance->id, 'userid' => self::$users[$userindex]->id, 'contextid' => $coursecontext->id, 'roleid' => self::$roles[$rolename]->id)));
227
        }
228
    }
229
 
230
    protected function assertIsNotEnrolled($userindex, $courseindex) {
231
        global $DB;
232
        if (!$dbinstance = $DB->get_record('enrol', array('courseid' => self::$courses[$courseindex]->id, 'enrol' => 'database'))) {
233
            return;
234
        }
235
        $this->assertFalse($DB->record_exists('user_enrolments', array('enrolid' => $dbinstance->id, 'userid' => self::$users[$userindex]->id)));
236
    }
237
 
11 efrain 238
    public function test_sync_user_enrolments(): void {
1 efrain 239
        global $DB;
240
 
241
        $this->init_enrol_database();
242
 
243
        $this->resetAfterTest(false);
244
        $this->preventResetByRollback();
245
 
246
        $plugin = enrol_get_plugin('database');
247
 
248
        // Test basic enrol sync for one user after login.
249
 
250
        $this->reset_enrol_database();
251
        $plugin->set_config('localcoursefield', 'idnumber');
252
        $plugin->set_config('localuserfield', 'idnumber');
253
        $plugin->set_config('localrolefield', 'shortname');
254
 
255
        $plugin->set_config('defaultrole', self::$roles['student']->id);
256
 
257
        $DB->insert_record('enrol_database_test_enrols', array('userid' => 'userid1', 'courseid' => 'courseid1', 'roleid' => 'student'));
258
        $DB->insert_record('enrol_database_test_enrols', array('userid' => 'userid1', 'courseid' => 'courseid2', 'roleid' => 'teacher'));
259
        $DB->insert_record('enrol_database_test_enrols', array('userid' => 'userid2', 'courseid' => 'courseid1', 'roleid' => null));
260
        $DB->insert_record('enrol_database_test_enrols', array('userid' => 'userid4', 'courseid' => 'courseid4', 'roleid' => 'editingteacher', 'otheruser' => '1'));
261
        $DB->insert_record('enrol_database_test_enrols', array('userid' => 'xxxxxxx', 'courseid' => 'courseid1', 'roleid' => 'student')); // Bogus record to be ignored.
262
        $DB->insert_record('enrol_database_test_enrols', array('userid' => 'userid1', 'courseid' => 'xxxxxxxxx', 'roleid' => 'student')); // Bogus record to be ignored.
263
 
264
        $this->assertEquals(0, $DB->count_records('user_enrolments', array()));
265
        $this->assertEquals(0, $DB->count_records('enrol', array('enrol' => 'database')));
266
        $this->assertEquals(0, $DB->count_records('role_assignments', array('component' => 'enrol_database')));
267
 
268
        $plugin->sync_user_enrolments(self::$users[1]);
269
        $this->assertEquals(2, $DB->count_records('user_enrolments', array()));
270
        $this->assertEquals(2, $DB->count_records('enrol', array('enrol' => 'database')));
271
        $this->assertEquals(2, $DB->count_records('role_assignments', array('component' => 'enrol_database')));
272
        $this->assertIsEnrolled(1, 1, ENROL_USER_ACTIVE, 'student');
273
        $this->assertIsEnrolled(1, 2, ENROL_USER_ACTIVE, 'teacher');
274
 
275
        // Make sure there are no errors or changes on the next login.
276
 
277
        $plugin->sync_user_enrolments(self::$users[1]);
278
        $this->assertEquals(2, $DB->count_records('user_enrolments', array()));
279
        $this->assertEquals(2, $DB->count_records('enrol', array('enrol' => 'database')));
280
        $this->assertEquals(2, $DB->count_records('role_assignments', array('component' => 'enrol_database')));
281
        $this->assertIsEnrolled(1, 1, ENROL_USER_ACTIVE, 'student');
282
        $this->assertIsEnrolled(1, 2, ENROL_USER_ACTIVE, 'teacher');
283
 
284
        $plugin->sync_user_enrolments(self::$users[2]);
285
        $this->assertEquals(3, $DB->count_records('user_enrolments', array()));
286
        $this->assertEquals(2, $DB->count_records('enrol', array('enrol' => 'database')));
287
        $this->assertEquals(3, $DB->count_records('role_assignments', array('component' => 'enrol_database')));
288
        $this->assertIsEnrolled(1, 1, ENROL_USER_ACTIVE, 'student');
289
        $this->assertIsEnrolled(1, 2, ENROL_USER_ACTIVE, 'teacher');
290
        $this->assertIsEnrolled(2, 1, ENROL_USER_ACTIVE, 'student');
291
 
292
        $plugin->sync_user_enrolments(self::$users[4]);
293
        $this->assertEquals(3, $DB->count_records('user_enrolments', array()));
294
        $this->assertEquals(3, $DB->count_records('enrol', array('enrol' => 'database')));
295
        $this->assertEquals(4, $DB->count_records('role_assignments', array('component' => 'enrol_database')));
296
        $this->assertIsEnrolled(1, 1, ENROL_USER_ACTIVE, 'student');
297
        $this->assertIsEnrolled(1, 2, ENROL_USER_ACTIVE, 'teacher');
298
        $this->assertIsEnrolled(2, 1, ENROL_USER_ACTIVE, 'student');
299
        $this->assertIsNotEnrolled(4, 4);
300
        $this->assertHasRoleAssignment(4, 4, 'editingteacher');
301
 
302
        // Enrolment removals.
303
 
304
        $DB->delete_records('enrol_database_test_enrols', array('userid' => 'userid1', 'courseid' => 'courseid1', 'roleid' => 'student'));
305
        $plugin->set_config('unenrolaction', ENROL_EXT_REMOVED_KEEP);
306
        $plugin->sync_user_enrolments(self::$users[1]);
307
        $this->assertEquals(3, $DB->count_records('user_enrolments', array()));
308
        $this->assertEquals(3, $DB->count_records('enrol', array('enrol' => 'database')));
309
        $this->assertEquals(4, $DB->count_records('role_assignments', array('component' => 'enrol_database')));
310
        $this->assertIsEnrolled(1, 1, ENROL_USER_ACTIVE, 'student');
311
        $this->assertIsEnrolled(1, 2, ENROL_USER_ACTIVE, 'teacher');
312
 
313
 
314
        $plugin->set_config('unenrolaction', ENROL_EXT_REMOVED_SUSPEND);
315
        $plugin->sync_user_enrolments(self::$users[1]);
316
        $this->assertEquals(3, $DB->count_records('user_enrolments', array()));
317
        $this->assertEquals(3, $DB->count_records('enrol', array('enrol' => 'database')));
318
        $this->assertEquals(4, $DB->count_records('role_assignments', array('component' => 'enrol_database')));
319
        $this->assertIsEnrolled(1, 1, ENROL_USER_SUSPENDED, 'student');
320
        $this->assertIsEnrolled(1, 2, ENROL_USER_ACTIVE, 'teacher');
321
 
322
        $DB->insert_record('enrol_database_test_enrols', array('userid' => 'userid1', 'courseid' => 'courseid1', 'roleid' => 'student'));
323
        $plugin->sync_user_enrolments(self::$users[1]);
324
        $this->assertEquals(3, $DB->count_records('user_enrolments', array()));
325
        $this->assertEquals(3, $DB->count_records('enrol', array('enrol' => 'database')));
326
        $this->assertEquals(4, $DB->count_records('role_assignments', array('component' => 'enrol_database')));
327
        $this->assertIsEnrolled(1, 1, ENROL_USER_ACTIVE, 'student');
328
        $this->assertIsEnrolled(1, 2, ENROL_USER_ACTIVE, 'teacher');
329
 
330
 
331
        $DB->delete_records('enrol_database_test_enrols', array('userid' => 'userid1', 'courseid' => 'courseid1', 'roleid' => 'student'));
332
        $plugin->set_config('unenrolaction', ENROL_EXT_REMOVED_SUSPENDNOROLES);
333
        $plugin->sync_user_enrolments(self::$users[1]);
334
        $this->assertEquals(3, $DB->count_records('user_enrolments', array()));
335
        $this->assertEquals(3, $DB->count_records('enrol', array('enrol' => 'database')));
336
        $this->assertEquals(3, $DB->count_records('role_assignments', array('component' => 'enrol_database')));
337
        $this->assertIsEnrolled(1, 1, ENROL_USER_SUSPENDED, false);
338
        $this->assertIsEnrolled(1, 2, ENROL_USER_ACTIVE, 'teacher');
339
 
340
        $DB->insert_record('enrol_database_test_enrols', array('userid' => 'userid1', 'courseid' => 'courseid1', 'roleid' => 'student'));
341
        $plugin->sync_user_enrolments(self::$users[1]);
342
        $this->assertEquals(3, $DB->count_records('user_enrolments', array()));
343
        $this->assertEquals(3, $DB->count_records('enrol', array('enrol' => 'database')));
344
        $this->assertEquals(4, $DB->count_records('role_assignments', array('component' => 'enrol_database')));
345
        $this->assertIsEnrolled(1, 1, ENROL_USER_ACTIVE, 'student');
346
        $this->assertIsEnrolled(1, 2, ENROL_USER_ACTIVE, 'teacher');
347
 
348
 
349
        $DB->delete_records('enrol_database_test_enrols', array('userid' => 'userid1', 'courseid' => 'courseid1', 'roleid' => 'student'));
350
        $plugin->set_config('unenrolaction', ENROL_EXT_REMOVED_UNENROL);
351
        $plugin->sync_user_enrolments(self::$users[1]);
352
        $this->assertEquals(2, $DB->count_records('user_enrolments', array()));
353
        $this->assertEquals(3, $DB->count_records('enrol', array('enrol' => 'database')));
354
        $this->assertEquals(3, $DB->count_records('role_assignments', array('component' => 'enrol_database')));
355
        $this->assertIsNotEnrolled(1, 1);
356
        $this->assertIsEnrolled(1, 2, ENROL_USER_ACTIVE, 'teacher');
357
 
358
        $DB->delete_records('enrol_database_test_enrols', array('userid' => 'userid4', 'courseid' => 'courseid4', 'roleid' => 'editingteacher'));
359
        $plugin->set_config('unenrolaction', ENROL_EXT_REMOVED_SUSPENDNOROLES);
360
        $plugin->sync_user_enrolments(self::$users[4]);
361
        $this->assertEquals(2, $DB->count_records('user_enrolments', array()));
362
        $this->assertEquals(3, $DB->count_records('enrol', array('enrol' => 'database')));
363
        $this->assertEquals(2, $DB->count_records('role_assignments', array('component' => 'enrol_database')));
364
        $this->assertIsNotEnrolled(4, 4);
365
        $this->assertHasRoleAssignment(4, 4, false);
366
 
367
        // Test all other mapping options.
368
 
369
        $this->reset_enrol_database();
370
 
371
        $this->assertEquals(0, $DB->count_records('user_enrolments', array()));
372
        $this->assertEquals(0, $DB->count_records('enrol', array('enrol' => 'database')));
373
        $this->assertEquals(0, $DB->count_records('role_assignments', array('component' => 'enrol_database')));
374
 
375
        $plugin->set_config('localcoursefield', 'id');
376
        $plugin->set_config('localuserfield', 'id');
377
        $plugin->set_config('localrolefield', 'id');
378
 
379
        $DB->insert_record('enrol_database_test_enrols', array('userid' => self::$users[1]->id, 'courseid' => self::$courses[1]->id, 'roleid' => self::$roles['student']->id));
380
        $DB->insert_record('enrol_database_test_enrols', array('userid' => self::$users[1]->id, 'courseid' => self::$courses[2]->id, 'roleid' => self::$roles['teacher']->id));
381
        $DB->insert_record('enrol_database_test_enrols', array('userid' => self::$users[2]->id, 'courseid' => self::$courses[1]->id, 'roleid' => self::$roles['student']->id));
382
 
383
        $plugin->sync_user_enrolments(self::$users[1]);
384
        $this->assertEquals(2, $DB->count_records('user_enrolments', array()));
385
        $this->assertEquals(2, $DB->count_records('enrol', array('enrol' => 'database')));
386
        $this->assertEquals(2, $DB->count_records('role_assignments', array('component' => 'enrol_database')));
387
        $this->assertIsEnrolled(1, 1, ENROL_USER_ACTIVE, 'student');
388
        $this->assertIsEnrolled(1, 2, ENROL_USER_ACTIVE, 'teacher');
389
 
390
 
391
        $this->reset_enrol_database();
392
        $plugin->set_config('localcoursefield', 'shortname');
393
        $plugin->set_config('localuserfield', 'email');
394
        $plugin->set_config('localrolefield', 'id');
395
 
396
        $DB->insert_record('enrol_database_test_enrols', array('userid' => self::$users[1]->email, 'courseid' => self::$courses[1]->shortname, 'roleid' => self::$roles['student']->id));
397
        $DB->insert_record('enrol_database_test_enrols', array('userid' => self::$users[1]->email, 'courseid' => self::$courses[2]->shortname, 'roleid' => self::$roles['teacher']->id));
398
        $DB->insert_record('enrol_database_test_enrols', array('userid' => self::$users[2]->email, 'courseid' => self::$courses[1]->shortname, 'roleid' => self::$roles['student']->id));
399
 
400
        $plugin->sync_user_enrolments(self::$users[1]);
401
        $this->assertEquals(2, $DB->count_records('user_enrolments', array()));
402
        $this->assertEquals(2, $DB->count_records('enrol', array('enrol' => 'database')));
403
        $this->assertEquals(2, $DB->count_records('role_assignments', array('component' => 'enrol_database')));
404
        $this->assertIsEnrolled(1, 1, ENROL_USER_ACTIVE, 'student');
405
        $this->assertIsEnrolled(1, 2, ENROL_USER_ACTIVE, 'teacher');
406
 
407
 
408
        $this->reset_enrol_database();
409
        $plugin->set_config('localcoursefield', 'id');
410
        $plugin->set_config('localuserfield', 'username');
411
        $plugin->set_config('localrolefield', 'id');
412
 
413
        $DB->insert_record('enrol_database_test_enrols', array('userid' => self::$users[1]->username, 'courseid' => self::$courses[1]->id, 'roleid' => self::$roles['student']->id));
414
        $DB->insert_record('enrol_database_test_enrols', array('userid' => self::$users[1]->username, 'courseid' => self::$courses[2]->id, 'roleid' => self::$roles['teacher']->id));
415
        $DB->insert_record('enrol_database_test_enrols', array('userid' => self::$users[2]->username, 'courseid' => self::$courses[1]->id, 'roleid' => self::$roles['student']->id));
416
 
417
        $plugin->sync_user_enrolments(self::$users[1]);
418
        $this->assertEquals(2, $DB->count_records('user_enrolments', array()));
419
        $this->assertEquals(2, $DB->count_records('enrol', array('enrol' => 'database')));
420
        $this->assertEquals(2, $DB->count_records('role_assignments', array('component' => 'enrol_database')));
421
        $this->assertIsEnrolled(1, 1, ENROL_USER_ACTIVE, 'student');
422
        $this->assertIsEnrolled(1, 2, ENROL_USER_ACTIVE, 'teacher');
423
    }
424
 
425
    /**
426
     * @depends test_sync_user_enrolments
427
     */
11 efrain 428
    public function test_sync_users(): void {
1 efrain 429
        global $DB;
430
 
431
        $this->resetAfterTest(false);
432
        $this->preventResetByRollback();
433
        $this->reset_enrol_database();
434
 
435
        $plugin = enrol_get_plugin('database');
436
 
437
        $trace = new \null_progress_trace();
438
 
439
        // Test basic enrol sync for one user after login.
440
 
441
        $plugin->set_config('localcoursefield', 'idnumber');
442
        $plugin->set_config('localuserfield', 'idnumber');
443
        $plugin->set_config('localrolefield', 'shortname');
444
 
445
        $DB->insert_record('enrol_database_test_enrols', array('userid' => 'userid1', 'courseid' => 'courseid1', 'roleid' => 'student'));
446
        $DB->insert_record('enrol_database_test_enrols', array('userid' => 'userid1', 'courseid' => 'courseid2', 'roleid' => 'editingteacher'));
447
        $DB->insert_record('enrol_database_test_enrols', array('userid' => 'userid2', 'courseid' => 'courseid1', 'roleid' => 'student'));
448
        $DB->insert_record('enrol_database_test_enrols', array('userid' => 'userid4', 'courseid' => 'courseid4', 'roleid' => 'editingteacher', 'otheruser' => '1'));
449
        $DB->insert_record('enrol_database_test_enrols', array('userid' => 'xxxxxxx', 'courseid' => 'courseid1', 'roleid' => 'student')); // Bogus record to be ignored.
450
        $DB->insert_record('enrol_database_test_enrols', array('userid' => 'userid1', 'courseid' => 'xxxxxxxxx', 'roleid' => 'student')); // Bogus record to be ignored.
451
        $this->assertEquals(0, $DB->count_records('user_enrolments', array()));
452
        $this->assertEquals(0, $DB->count_records('enrol', array('enrol' => 'database')));
453
        $this->assertEquals(0, $DB->count_records('role_assignments', array('component' => 'enrol_database')));
454
 
455
        $plugin->sync_enrolments($trace);
456
        $this->assertEquals(3, $DB->count_records('user_enrolments', array()));
457
        $this->assertEquals(3, $DB->count_records('enrol', array('enrol' => 'database')));
458
        $this->assertEquals(4, $DB->count_records('role_assignments', array('component' => 'enrol_database')));
459
        $this->assertIsEnrolled(1, 1, ENROL_USER_ACTIVE, 'student');
460
        $this->assertIsEnrolled(1, 2, ENROL_USER_ACTIVE, 'editingteacher');
461
        $this->assertIsEnrolled(2, 1, ENROL_USER_ACTIVE, 'student');
462
        $this->assertIsNotEnrolled(4, 4);
463
        $this->assertHasRoleAssignment(4, 4, 'editingteacher');
464
 
465
        $plugin->set_config('defaultrole', self::$roles['teacher']->id);
466
        $DB->insert_record('enrol_database_test_enrols', array('userid' => 'userid3', 'courseid' => 'courseid3'));
467
        $plugin->sync_enrolments($trace);
468
        $this->assertEquals(4, $DB->count_records('user_enrolments', array()));
469
        $this->assertEquals(4, $DB->count_records('enrol', array('enrol' => 'database')));
470
        $this->assertEquals(5, $DB->count_records('role_assignments', array('component' => 'enrol_database')));
471
        $this->assertIsEnrolled(1, 1, ENROL_USER_ACTIVE, 'student');
472
        $this->assertIsEnrolled(1, 2, ENROL_USER_ACTIVE, 'editingteacher');
473
        $this->assertIsEnrolled(2, 1, ENROL_USER_ACTIVE, 'student');
474
        $this->assertIsNotEnrolled(4, 4);
475
        $this->assertHasRoleAssignment(4, 4, 'editingteacher');
476
        $this->assertIsEnrolled(3, 3, ENROL_USER_ACTIVE, 'teacher');
477
 
478
 
479
        // Test different unenrolment options.
480
 
481
        $DB->delete_records('enrol_database_test_enrols', array('userid' => 'userid1', 'courseid' => 'courseid1', 'roleid' => 'student'));
482
        $plugin->set_config('unenrolaction', ENROL_EXT_REMOVED_KEEP);
483
        $plugin->sync_enrolments($trace);
484
        $this->assertEquals(4, $DB->count_records('user_enrolments', array()));
485
        $this->assertEquals(4, $DB->count_records('enrol', array('enrol' => 'database')));
486
        $this->assertEquals(5, $DB->count_records('role_assignments', array('component' => 'enrol_database')));
487
        $this->assertIsEnrolled(1, 1, ENROL_USER_ACTIVE, 'student');
488
        $this->assertIsEnrolled(1, 2, ENROL_USER_ACTIVE, 'editingteacher');
489
        $this->assertIsEnrolled(2, 1, ENROL_USER_ACTIVE, 'student');
490
        $this->assertIsNotEnrolled(4, 4);
491
        $this->assertHasRoleAssignment(4, 4, 'editingteacher');
492
        $this->assertIsEnrolled(3, 3, ENROL_USER_ACTIVE, 'teacher');
493
 
494
 
495
        $plugin->set_config('unenrolaction', ENROL_EXT_REMOVED_SUSPEND);
496
        $plugin->sync_enrolments($trace);
497
        $this->assertEquals(4, $DB->count_records('user_enrolments', array()));
498
        $this->assertEquals(4, $DB->count_records('enrol', array('enrol' => 'database')));
499
        $this->assertEquals(5, $DB->count_records('role_assignments', array('component' => 'enrol_database')));
500
        $this->assertIsEnrolled(1, 1, ENROL_USER_SUSPENDED, 'student');
501
        $this->assertIsEnrolled(1, 2, ENROL_USER_ACTIVE, 'editingteacher');
502
        $this->assertIsEnrolled(2, 1, ENROL_USER_ACTIVE, 'student');
503
        $this->assertIsNotEnrolled(4, 4);
504
        $this->assertHasRoleAssignment(4, 4, 'editingteacher');
505
        $this->assertIsEnrolled(3, 3, ENROL_USER_ACTIVE, 'teacher');
506
 
507
        $DB->insert_record('enrol_database_test_enrols', array('userid' => 'userid1', 'courseid' => 'courseid1', 'roleid' => 'student'));
508
        $plugin->sync_enrolments($trace);
509
        $this->assertEquals(4, $DB->count_records('user_enrolments', array()));
510
        $this->assertEquals(4, $DB->count_records('enrol', array('enrol' => 'database')));
511
        $this->assertEquals(5, $DB->count_records('role_assignments', array('component' => 'enrol_database')));
512
        $this->assertIsEnrolled(1, 1, ENROL_USER_ACTIVE, 'student');
513
        $this->assertIsEnrolled(1, 2, ENROL_USER_ACTIVE, 'editingteacher');
514
        $this->assertIsEnrolled(2, 1, ENROL_USER_ACTIVE, 'student');
515
        $this->assertIsNotEnrolled(4, 4);
516
        $this->assertHasRoleAssignment(4, 4, 'editingteacher');
517
        $this->assertIsEnrolled(3, 3, ENROL_USER_ACTIVE, 'teacher');
518
 
519
 
520
        $DB->delete_records('enrol_database_test_enrols', array('userid' => 'userid1', 'courseid' => 'courseid1', 'roleid' => 'student'));
521
        $plugin->set_config('unenrolaction', ENROL_EXT_REMOVED_SUSPENDNOROLES);
522
        $plugin->sync_enrolments($trace);
523
        $this->assertEquals(4, $DB->count_records('user_enrolments', array()));
524
        $this->assertEquals(4, $DB->count_records('enrol', array('enrol' => 'database')));
525
        $this->assertEquals(4, $DB->count_records('role_assignments', array('component' => 'enrol_database')));
526
        $this->assertIsEnrolled(1, 1, ENROL_USER_SUSPENDED, false);
527
        $this->assertIsEnrolled(1, 2, ENROL_USER_ACTIVE, 'editingteacher');
528
        $this->assertIsEnrolled(2, 1, ENROL_USER_ACTIVE, 'student');
529
        $this->assertIsNotEnrolled(4, 4);
530
        $this->assertHasRoleAssignment(4, 4, 'editingteacher');
531
        $this->assertIsEnrolled(3, 3, ENROL_USER_ACTIVE, 'teacher');
532
 
533
        $DB->insert_record('enrol_database_test_enrols', array('userid' => 'userid1', 'courseid' => 'courseid1', 'roleid' => 'student'));
534
        $plugin->sync_enrolments($trace);
535
        $this->assertEquals(4, $DB->count_records('user_enrolments', array()));
536
        $this->assertEquals(4, $DB->count_records('enrol', array('enrol' => 'database')));
537
        $this->assertEquals(5, $DB->count_records('role_assignments', array('component' => 'enrol_database')));
538
        $this->assertIsEnrolled(1, 1, ENROL_USER_ACTIVE, 'student');
539
        $this->assertIsEnrolled(1, 2, ENROL_USER_ACTIVE, 'editingteacher');
540
        $this->assertIsEnrolled(2, 1, ENROL_USER_ACTIVE, 'student');
541
        $this->assertIsNotEnrolled(4, 4);
542
        $this->assertHasRoleAssignment(4, 4, 'editingteacher');
543
        $this->assertIsEnrolled(3, 3, ENROL_USER_ACTIVE, 'teacher');
544
 
545
 
546
        $DB->delete_records('enrol_database_test_enrols', array('userid' => 'userid1', 'courseid' => 'courseid1', 'roleid' => 'student'));
547
        $plugin->set_config('unenrolaction', ENROL_EXT_REMOVED_UNENROL);
548
        $plugin->sync_enrolments($trace);
549
        $this->assertEquals(3, $DB->count_records('user_enrolments', array()));
550
        $this->assertEquals(4, $DB->count_records('enrol', array('enrol' => 'database')));
551
        $this->assertEquals(4, $DB->count_records('role_assignments', array('component' => 'enrol_database')));
552
        $this->assertIsNotEnrolled(1, 1);
553
        $this->assertIsEnrolled(1, 2, ENROL_USER_ACTIVE, 'editingteacher');
554
        $this->assertIsEnrolled(2, 1, ENROL_USER_ACTIVE, 'student');
555
        $this->assertIsNotEnrolled(4, 4);
556
        $this->assertHasRoleAssignment(4, 4, 'editingteacher');
557
        $this->assertIsEnrolled(3, 3, ENROL_USER_ACTIVE, 'teacher');
558
 
559
        $DB->insert_record('enrol_database_test_enrols', array('userid' => 'userid1', 'courseid' => 'courseid1', 'roleid' => 'student'));
560
        $DB->insert_record('enrol_database_test_enrols', array('userid' => 'userid1', 'courseid' => 'courseid1', 'roleid' => 'teacher'));
561
        $plugin->sync_enrolments($trace);
562
        $this->assertEquals(4, $DB->count_records('user_enrolments', array()));
563
        $this->assertEquals(4, $DB->count_records('enrol', array('enrol' => 'database')));
564
        $this->assertEquals(6, $DB->count_records('role_assignments', array('component' => 'enrol_database')));
565
        $this->assertIsEnrolled(1, 1, ENROL_USER_ACTIVE, 'student');
566
        $this->assertIsEnrolled(1, 1, ENROL_USER_ACTIVE, 'teacher');
567
        $this->assertIsEnrolled(1, 2, ENROL_USER_ACTIVE, 'editingteacher');
568
        $this->assertIsEnrolled(2, 1, ENROL_USER_ACTIVE, 'student');
569
        $this->assertIsNotEnrolled(4, 4);
570
        $this->assertHasRoleAssignment(4, 4, 'editingteacher');
571
        $this->assertIsEnrolled(3, 3, ENROL_USER_ACTIVE, 'teacher');
572
 
573
        $DB->delete_records('enrol_database_test_enrols', array('userid' => 'userid1', 'courseid' => 'courseid1', 'roleid' => 'teacher'));
574
        $plugin->sync_enrolments($trace);
575
        $this->assertEquals(4, $DB->count_records('user_enrolments', array()));
576
        $this->assertEquals(4, $DB->count_records('enrol', array('enrol' => 'database')));
577
        $this->assertEquals(5, $DB->count_records('role_assignments', array('component' => 'enrol_database')));
578
        $this->assertIsEnrolled(1, 1, ENROL_USER_ACTIVE, 'student');
579
        $this->assertIsEnrolled(1, 2, ENROL_USER_ACTIVE, 'editingteacher');
580
        $this->assertIsEnrolled(2, 1, ENROL_USER_ACTIVE, 'student');
581
        $this->assertIsNotEnrolled(4, 4);
582
        $this->assertHasRoleAssignment(4, 4, 'editingteacher');
583
        $this->assertIsEnrolled(3, 3, ENROL_USER_ACTIVE, 'teacher');
584
 
585
 
586
        // Test all other mapping options.
587
 
588
        $this->reset_enrol_database();
589
 
590
        $this->assertEquals(0, $DB->count_records('user_enrolments', array()));
591
        $this->assertEquals(0, $DB->count_records('enrol', array('enrol' => 'database')));
592
        $this->assertEquals(0, $DB->count_records('role_assignments', array('component' => 'enrol_database')));
593
 
594
        $plugin->set_config('localcoursefield', 'id');
595
        $plugin->set_config('localuserfield', 'id');
596
        $plugin->set_config('localrolefield', 'id');
597
 
598
        $DB->insert_record('enrol_database_test_enrols', array('userid' => self::$users[1]->id, 'courseid' => self::$courses[1]->id, 'roleid' => self::$roles['student']->id));
599
        $DB->insert_record('enrol_database_test_enrols', array('userid' => self::$users[1]->id, 'courseid' => self::$courses[2]->id, 'roleid' => self::$roles['teacher']->id));
600
        $DB->insert_record('enrol_database_test_enrols', array('userid' => self::$users[2]->id, 'courseid' => self::$courses[1]->id, 'roleid' => self::$roles['student']->id));
601
 
602
        $plugin->sync_enrolments($trace);
603
        $this->assertEquals(3, $DB->count_records('user_enrolments', array()));
604
        $this->assertEquals(2, $DB->count_records('enrol', array('enrol' => 'database')));
605
        $this->assertEquals(3, $DB->count_records('role_assignments', array('component' => 'enrol_database')));
606
        $this->assertIsEnrolled(1, 1, ENROL_USER_ACTIVE, 'student');
607
        $this->assertIsEnrolled(1, 2, ENROL_USER_ACTIVE, 'teacher');
608
        $this->assertIsEnrolled(2, 1, ENROL_USER_ACTIVE, 'student');
609
 
610
 
611
        $this->reset_enrol_database();
612
        $plugin->set_config('localcoursefield', 'shortname');
613
        $plugin->set_config('localuserfield', 'email');
614
        $plugin->set_config('localrolefield', 'id');
615
 
616
        $DB->insert_record('enrol_database_test_enrols', array('userid' => self::$users[1]->email, 'courseid' => self::$courses[1]->shortname, 'roleid' => self::$roles['student']->id));
617
        $DB->insert_record('enrol_database_test_enrols', array('userid' => self::$users[1]->email, 'courseid' => self::$courses[2]->shortname, 'roleid' => self::$roles['teacher']->id));
618
        $DB->insert_record('enrol_database_test_enrols', array('userid' => self::$users[2]->email, 'courseid' => self::$courses[1]->shortname, 'roleid' => self::$roles['student']->id));
619
 
620
        $plugin->sync_enrolments($trace);
621
        $this->assertEquals(3, $DB->count_records('user_enrolments', array()));
622
        $this->assertEquals(2, $DB->count_records('enrol', array('enrol' => 'database')));
623
        $this->assertEquals(3, $DB->count_records('role_assignments', array('component' => 'enrol_database')));
624
        $this->assertIsEnrolled(1, 1, ENROL_USER_ACTIVE, 'student');
625
        $this->assertIsEnrolled(1, 2, ENROL_USER_ACTIVE, 'teacher');
626
        $this->assertIsEnrolled(2, 1, ENROL_USER_ACTIVE, 'student');
627
 
628
 
629
        $this->reset_enrol_database();
630
        $plugin->set_config('localcoursefield', 'id');
631
        $plugin->set_config('localuserfield', 'username');
632
        $plugin->set_config('localrolefield', 'id');
633
 
634
        $DB->insert_record('enrol_database_test_enrols', array('userid' => self::$users[1]->username, 'courseid' => self::$courses[1]->id, 'roleid' => self::$roles['student']->id));
635
        $DB->insert_record('enrol_database_test_enrols', array('userid' => self::$users[1]->username, 'courseid' => self::$courses[2]->id, 'roleid' => self::$roles['teacher']->id));
636
        $DB->insert_record('enrol_database_test_enrols', array('userid' => self::$users[2]->username, 'courseid' => self::$courses[1]->id, 'roleid' => self::$roles['student']->id));
637
 
638
        $plugin->sync_enrolments($trace);
639
        $this->assertEquals(3, $DB->count_records('user_enrolments', array()));
640
        $this->assertEquals(2, $DB->count_records('enrol', array('enrol' => 'database')));
641
        $this->assertEquals(3, $DB->count_records('role_assignments', array('component' => 'enrol_database')));
642
        $this->assertIsEnrolled(1, 1, ENROL_USER_ACTIVE, 'student');
643
        $this->assertIsEnrolled(1, 2, ENROL_USER_ACTIVE, 'teacher');
644
        $this->assertIsEnrolled(2, 1, ENROL_USER_ACTIVE, 'student');
645
 
646
 
647
        // Test sync of one course only.
648
 
649
        $this->reset_enrol_database();
650
 
651
        $DB->insert_record('enrol_database_test_enrols', array('userid' => self::$users[1]->username, 'courseid' => self::$courses[1]->id, 'roleid' => self::$roles['student']->id));
652
        $DB->insert_record('enrol_database_test_enrols', array('userid' => self::$users[1]->username, 'courseid' => self::$courses[2]->id, 'roleid' => self::$roles['teacher']->id));
653
        $DB->insert_record('enrol_database_test_enrols', array('userid' => self::$users[2]->username, 'courseid' => self::$courses[1]->id, 'roleid' => self::$roles['student']->id));
654
 
655
        $this->assertEquals(0, $DB->count_records('user_enrolments', array()));
656
        $this->assertEquals(0, $DB->count_records('enrol', array('enrol' => 'database')));
657
        $this->assertEquals(0, $DB->count_records('role_assignments', array('component' => 'enrol_database')));
658
 
659
        $plugin->sync_enrolments($trace, self::$courses[3]->id);
660
        $this->assertEquals(0, $DB->count_records('user_enrolments', array()));
661
        $this->assertEquals(1, $DB->count_records('enrol', array('enrol' => 'database')));
662
        $this->assertEquals(0, $DB->count_records('role_assignments', array('component' => 'enrol_database')));
663
 
664
        $plugin->sync_enrolments($trace, self::$courses[1]->id);
665
        $this->assertEquals(2, $DB->count_records('user_enrolments', array()));
666
        $this->assertEquals(2, $DB->count_records('enrol', array('enrol' => 'database')));
667
        $this->assertEquals(2, $DB->count_records('role_assignments', array('component' => 'enrol_database')));
668
        $this->assertIsEnrolled(1, 1, ENROL_USER_ACTIVE, 'student');
669
        $this->assertIsEnrolled(2, 1, ENROL_USER_ACTIVE, 'student');
670
 
671
        $plugin->sync_enrolments($trace, self::$courses[2]->id);
672
        $this->assertEquals(3, $DB->count_records('user_enrolments', array()));
673
        $this->assertEquals(3, $DB->count_records('enrol', array('enrol' => 'database')));
674
        $this->assertEquals(3, $DB->count_records('role_assignments', array('component' => 'enrol_database')));
675
        $this->assertIsEnrolled(1, 1, ENROL_USER_ACTIVE, 'student');
676
        $this->assertIsEnrolled(1, 2, ENROL_USER_ACTIVE, 'teacher');
677
        $this->assertIsEnrolled(2, 1, ENROL_USER_ACTIVE, 'student');
678
 
679
 
680
        $plugin->set_config('unenrolaction', ENROL_EXT_REMOVED_UNENROL);
681
 
682
        $DB->delete_records('enrol_database_test_enrols', array());
683
 
684
        $plugin->sync_enrolments($trace, self::$courses[1]->id);
685
        $this->assertEquals(1, $DB->count_records('user_enrolments', array()));
686
        $this->assertEquals(3, $DB->count_records('enrol', array('enrol' => 'database')));
687
        $this->assertEquals(1, $DB->count_records('role_assignments', array('component' => 'enrol_database')));
688
        $this->assertIsEnrolled(1, 2, ENROL_USER_ACTIVE, 'teacher');
689
 
690
        $plugin->sync_enrolments($trace, self::$courses[2]->id);
691
        $this->assertEquals(0, $DB->count_records('user_enrolments', array()));
692
        $this->assertEquals(3, $DB->count_records('enrol', array('enrol' => 'database')));
693
        $this->assertEquals(0, $DB->count_records('role_assignments', array('component' => 'enrol_database')));
694
    }
695
 
696
    /**
697
     * @depends test_sync_users
698
     */
11 efrain 699
    public function test_sync_courses(): void {
1 efrain 700
        global $DB;
701
 
702
        $this->resetAfterTest(true);
703
        $this->preventResetByRollback();
704
        $this->reset_enrol_database();
705
 
706
        $plugin = enrol_get_plugin('database');
707
 
708
        $trace = new \null_progress_trace();
709
 
710
        $plugin->set_config('localcategoryfield', 'id');
711
        $coursecat = $this->getDataGenerator()->create_category(array('name' => 'Test category 1', 'idnumber' => 'tcid1'));
712
        $defcat = $DB->get_record('course_categories', array('id' => $plugin->get_config('defaultcategory')));
713
 
714
        $course1 = array('fullname' => 'New course 1', 'shortname' => 'nc1', 'idnumber' => 'ncid1', 'category' => $coursecat->id);
715
        $course2 = array('fullname' => 'New course 2', 'shortname' => 'nc2', 'idnumber' => 'ncid2', 'category' => null);
716
        // Duplicate records are to be ignored.
717
        $course3 = array('fullname' => 'New course 3', 'shortname' => 'xx', 'idnumber' => 'yy2', 'category' => $defcat->id);
718
        $course4 = array('fullname' => 'New course 4', 'shortname' => 'xx', 'idnumber' => 'yy3', 'category' => $defcat->id);
719
        $course5 = array('fullname' => 'New course 5', 'shortname' => 'xx1', 'idnumber' => 'yy', 'category' => $defcat->id);
720
        $course6 = array('fullname' => 'New course 6', 'shortname' => 'xx2', 'idnumber' => 'yy', 'category' => $defcat->id);
721
 
722
        $DB->insert_record('enrol_database_test_courses', $course1);
723
        $DB->insert_record('enrol_database_test_courses', $course2);
724
        $DB->insert_record('enrol_database_test_courses', $course3);
725
        $DB->insert_record('enrol_database_test_courses', $course4);
726
        $DB->insert_record('enrol_database_test_courses', $course5);
727
        $DB->insert_record('enrol_database_test_courses', $course6);
728
 
729
        $this->assertEquals(1+count(self::$courses), $DB->count_records('course'));
730
 
731
        $plugin->sync_courses($trace);
732
 
733
        $this->assertEquals(4+1+count(self::$courses), $DB->count_records('course'));
734
 
735
        $this->assertTrue($DB->record_exists('course', $course1));
736
        $course2['category'] = $defcat->id;
737
        $this->assertTrue($DB->record_exists('course', $course2));
738
 
739
 
740
        // People should NOT push duplicates there because the results are UNDEFINED! But anyway skip the duplicates.
741
 
742
        $this->assertEquals(1, $DB->count_records('course', array('idnumber' => 'yy')));
743
        $this->assertEquals(1, $DB->count_records('course', array('shortname' => 'xx')));
744
 
745
        // Check default number of sections matches with the created course sections.
746
 
747
        $recordcourse1 = $DB->get_record('course', $course1);
748
        $courseconfig = get_config('moodlecourse');
749
        $numsections = $DB->count_records('course_sections', array('course' => $recordcourse1->id));
750
        // To compare numsections we have to add topic 0 to default numsections.
751
        $this->assertEquals(($courseconfig->numsections + 1), $numsections);
752
 
753
        // Test category mapping via idnumber.
754
 
755
        $plugin->set_config('localcategoryfield', 'idnumber');
756
        $course7 = array('fullname' => 'New course 7', 'shortname' => 'nc7', 'idnumber' => 'ncid7', 'category' => 'tcid1');
757
        $DB->insert_record('enrol_database_test_courses', $course7);
758
        $plugin->sync_courses($trace);
759
 
760
        $this->assertEquals(1+4+1+count(self::$courses), $DB->count_records('course'));
761
        $this->assertTrue($DB->record_exists('course', $course1));
762
        $this->assertTrue($DB->record_exists('course', $course2));
763
        $course7['category'] = $coursecat->id;
764
        $this->assertTrue($DB->record_exists('course', $course7));
765
 
766
 
767
        // Test course template.
768
 
769
        $template = $this->getDataGenerator()->create_course(array('numsections' => 666, 'shortname' => 'crstempl'));
770
        $plugin->set_config('templatecourse', 'crstempl');
771
 
772
        $course8 = array('fullname' => 'New course 8', 'shortname' => 'nc8', 'idnumber' => 'ncid8', 'category' => null);
773
        $DB->insert_record('enrol_database_test_courses', $course8);
774
        $plugin->sync_courses($trace);
775
 
776
        $this->assertEquals(2+1+4+1+count(self::$courses), $DB->count_records('course'));
777
        $course8['category'] = $defcat->id;
778
        $record = $DB->get_record('course', $course8);
779
        $this->assertFalse(empty($record));
780
        $this->assertEquals(666, course_get_format($record)->get_last_section_number());
781
 
782
        // Test invalid category.
783
 
784
        $course9 = array('fullname' => 'New course 9', 'shortname' => 'nc9', 'idnumber' => 'ncid9', 'category' => 'xxxxxxx');
785
        $DB->insert_record('enrol_database_test_courses', $course9);
786
        $plugin->sync_courses($trace);
787
        $this->assertEquals(2+1+4+1+count(self::$courses), $DB->count_records('course'));
788
        $this->assertFalse($DB->record_exists('course', array('idnumber' => 'ncid9')));
789
 
790
 
791
        // Test when categories not specified.
792
 
793
        $plugin->set_config('newcoursecategory', '');
794
        $plugin->sync_courses($trace);
795
        $this->assertEquals(1+2+1+4+1+count(self::$courses), $DB->count_records('course'));
796
        $this->assertTrue($DB->record_exists('course', array('idnumber' => 'ncid9')));
797
 
798
        // Final cleanup - remove extra tables, fixtures and caches.
799
        $this->cleanup_enrol_database();
800
    }
1441 ariadna 801
 
802
    /**
803
     * Test syncing courses with start and end dates.
804
     *
805
     * @covers \enrol_database_plugin::sync_courses
806
     */
807
    public function test_sync_courses_start_end_dates(): void {
808
        global $DB;
809
 
810
        $this->resetAfterTest();
811
        $this->preventResetByRollback();
812
        $this->init_enrol_database();
813
 
814
        $courseconfig = get_config('moodlecourse');
815
        $nextyear = (int) date('Y') + 1;
816
        $prev = (int) date('Y') - 1;
817
 
818
        $midnightstartdate = usergetmidnight(time());
819
        $midnightenddate = usergetmidnight(time()) + $courseconfig->courseduration;
820
 
821
        $plugin = enrol_get_plugin('database');
822
 
823
        $trace = new \null_progress_trace();
824
 
825
        $course1 = ['fullname' => 'C1', 'shortname' => 'c1', 'idnumber' => 'c1', 'startdate' => 0,
826
            'enddate' => 0];
827
        $course2 = ['fullname' => 'C2', 'shortname' => 'c2', 'idnumber' => 'c2', 'startdate' => null,
828
            'enddate' => null];
829
        // This course won't be created. Broken start date.
830
        $course3 = ['fullname' => 'C3', 'shortname' => 'c3', 'idnumber' => 'c3', 'startdate' => 'not date',
831
            'enddate' => 0];
832
        // This course won't be created. Broken end date.
833
        $course4 = ['fullname' => 'C4', 'shortname' => 'c4', 'idnumber' => 'c4', 'startdate' => 0,
834
            'enddate' => 'not date'];
835
        // This course won't be created. Start date after end date.
836
        $course5 = ['fullname' => 'C5', 'shortname' => 'c5', 'idnumber' => 'c5', 'startdate' => '12.05.2024',
837
            'enddate' => '12.05.2021'];
838
        $course6 = ['fullname' => 'C6', 'shortname' => 'c6', 'idnumber' => 'c6', 'startdate' => '2024-05-22',
839
            'enddate' => '2027-05-12'];
840
        $course7 = ['fullname' => 'C7', 'shortname' => 'c7', 'idnumber' => 'c7', 'startdate' => null,
841
            'enddate' => '12.05.' . $nextyear];
842
        $course8 = ['fullname' => 'C8', 'shortname' => 'c8', 'idnumber' => 'c8', 'startdate' => '12.05.2024',
843
            'enddate' => null];
844
        // This course won't be created. Start date is not set, but it should be set to date after end date.
845
        $course9 = ['fullname' => 'C9', 'shortname' => 'c9', 'idnumber' => 'c9', 'startdate' => null,
846
            'enddate' => '12.05.' . $prev];
847
 
848
        $DB->insert_record('enrol_database_test_courses', $course1);
849
        $DB->insert_record('enrol_database_test_courses', $course2);
850
        $DB->insert_record('enrol_database_test_courses', $course3);
851
        $DB->insert_record('enrol_database_test_courses', $course4);
852
        $DB->insert_record('enrol_database_test_courses', $course5);
853
        $DB->insert_record('enrol_database_test_courses', $course6);
854
        $DB->insert_record('enrol_database_test_courses', $course7);
855
        $DB->insert_record('enrol_database_test_courses', $course8);
856
        $DB->insert_record('enrol_database_test_courses', $course9);
857
 
858
        $plugin->set_config('newcoursestartdate', 'startdate');
859
        $plugin->set_config('newcourseenddate', 'enddate');
860
 
861
        $plugin->sync_courses($trace);
862
 
863
        // Course 3, course 4, course 5 and course 9 should not be created.
864
        $this->assertTrue($DB->record_exists('course', ['shortname' => $course1['shortname']]));
865
        $this->assertTrue($DB->record_exists('course', ['shortname' => $course2['shortname']]));
866
        $this->assertFalse($DB->record_exists('course', ['shortname' => $course3['shortname']]));
867
        $this->assertFalse($DB->record_exists('course', ['shortname' => $course4['shortname']]));
868
        $this->assertFalse($DB->record_exists('course', ['shortname' => $course5['shortname']]));
869
        $this->assertTrue($DB->record_exists('course', ['shortname' => $course6['shortname']]));
870
        $this->assertTrue($DB->record_exists('course', ['shortname' => $course7['shortname']]));
871
        $this->assertTrue($DB->record_exists('course', ['shortname' => $course8['shortname']]));
872
        $this->assertFalse($DB->record_exists('course', ['shortname' => $course9['shortname']]));
873
 
874
        // Check dates for created courses.
875
        $this->assertEquals($midnightstartdate, $DB->get_field('course', 'startdate', ['shortname' => $course1['shortname']]));
876
        $this->assertEquals($midnightenddate, $DB->get_field('course', 'enddate', ['shortname' => $course1['shortname']]));
877
 
878
        $this->assertEquals($midnightstartdate, $DB->get_field('course', 'startdate', ['shortname' => $course2['shortname']]));
879
        $this->assertEquals($midnightenddate, $DB->get_field('course', 'enddate', ['shortname' => $course2['shortname']]));
880
 
881
        $this->assertEquals(strtotime('22.05.2024'), $DB->get_field('course', 'startdate', ['shortname' => $course6['shortname']]));
882
        $this->assertEquals(strtotime('12.05.2027'), $DB->get_field('course', 'enddate', ['shortname' => $course6['shortname']]));
883
 
884
        $this->assertEquals($midnightstartdate, $DB->get_field('course', 'startdate', ['shortname' => $course7['shortname']]));
885
        $expected = strtotime('12.05.' . $nextyear);
886
        $this->assertEquals($expected, $DB->get_field('course', 'enddate', ['shortname' => $course7['shortname']]));
887
 
888
        $this->assertEquals(strtotime('12.05.2024'), $DB->get_field('course', 'startdate', ['shortname' => $course8['shortname']]));
889
        $expected = strtotime('12.05.2024') + $courseconfig->courseduration;
890
        $this->assertEquals($expected, $DB->get_field('course', 'enddate', ['shortname' => $course8['shortname']]));
891
 
892
        // Push course with dates as timestamp.
893
        $course10 = ['fullname' => 'C10', 'shortname' => 'c10', 'idnumber' => 'c10', 'startdate' => 1810051200,
894
            'enddate' => 1810051211];
895
        $DB->insert_record('enrol_database_test_courses', $course10);
896
 
897
        $plugin->sync_courses($trace);
898
 
899
        $this->assertTrue($DB->record_exists('course', ['shortname' => $course10['shortname']]));
900
        $this->assertEquals(1810051200, $DB->get_field('course', 'startdate', ['shortname' => $course10['shortname']]));
901
        $this->assertEquals(1810051211, $DB->get_field('course', 'enddate', ['shortname' => $course10['shortname']]));
902
 
903
        // Push course with broken dates, but delete dates from plugin configuration before syncing.
904
        $course11 = ['fullname' => 'C11', 'shortname' => 'c11', 'idnumber' => 'c11', 'startdate' => 'not date',
905
            'enddate' => 'not date'];
906
        $DB->insert_record('enrol_database_test_courses', $course11);
907
 
908
        $plugin->set_config('newcoursestartdate', '');
909
        $plugin->set_config('newcourseenddate', '');
910
        $plugin->sync_courses($trace);
911
 
912
        $this->assertTrue($DB->record_exists('course', ['shortname' => $course11['shortname']]));
913
        $this->assertEquals($midnightstartdate, $DB->get_field('course', 'startdate', ['shortname' => $course11['shortname']]));
914
        $this->assertEquals($midnightenddate, $DB->get_field('course', 'enddate', ['shortname' => $course11['shortname']]));
915
 
916
        // Push courses with correct dates, but set date configuration to not existing date fields.
917
        $course12 = ['fullname' => 'C12', 'shortname' => 'c12', 'idnumber' => 'c12', 'startdate' => '2024-05-22',
918
            'enddate' => '2027-05-12'];
919
        $DB->insert_record('enrol_database_test_courses', $course11);
920
 
921
        $plugin->set_config('newcoursestartdate', 'startdate');
922
        $plugin->set_config('newcourseenddate', 'ed');
923
        $plugin->sync_courses($trace);
924
 
925
        // Course should not be synced to prevent setting up incorrect dates.
926
        $this->assertFalse($DB->record_exists('course', ['shortname' => $course12['shortname']]));
927
 
928
        $course13 = ['fullname' => 'C13', 'shortname' => 'c13', 'idnumber' => 'c13', 'startdate' => '2024-05-22',
929
            'enddate' => '2027-05-12'];
930
        $DB->insert_record('enrol_database_test_courses', $course11);
931
 
932
        $plugin->set_config('newcoursestartdate', 'sd');
933
        $plugin->set_config('newcourseenddate', 'enddate');
934
        $plugin->sync_courses($trace);
935
 
936
        // Course should not be synced to prevent setting up incorrect dates.
937
        $this->assertFalse($DB->record_exists('course', ['shortname' => $course13['shortname']]));
938
 
939
        $this->cleanup_enrol_database();
940
    }
1 efrain 941
}