diff --git a/backup/moodle2/restore_local_adler_plugin.class.php b/backup/moodle2/restore_local_adler_plugin.class.php index 4962f17..ced39d3 100644 --- a/backup/moodle2/restore_local_adler_plugin.class.php +++ b/backup/moodle2/restore_local_adler_plugin.class.php @@ -1,6 +1,9 @@ db = $DB; + $this->log = new logger('local_adler', self::class); + $this->plugin_set_version = null; } protected function define_course_plugin_structure(): array { @@ -26,10 +32,14 @@ protected function define_course_plugin_structure(): array { } public function process_plugin_set_version($data) { - if (isset($data['plugin_set_version']) && version_compare($data['plugin_set_version'], '4.0.0', '<')) { - throw new moodle_exception('invalid_plugin_set_version', 'local_adler', '', null, 'plugin_set_version is below 3.2.0'); + $data = (object)$data; + + if (property_exists($data, 'plugin_set_version') ) { + if (version_compare($data->plugin_set_version, '4.0.0', '<')) { + throw new moodle_exception('invalid_plugin_set_version', 'local_adler', '', null, 'plugin_set_version is below 3.2.0'); + } + $this->plugin_set_version = $data->plugin_set_version; } - $this->plugin_set_version = $data['plugin_set_version']; } /** @@ -101,8 +111,6 @@ protected function define_module_plugin_structure(): array { * @throws dml_exception */ public function process_adler_module($data) { - global $DB; - // Cast $data to object if it is an array // This is required because the object can sometimes randomly be an array $data = (object)$data; @@ -121,12 +129,22 @@ public function process_adler_module($data) { // The information whether availability is enabled or not is not (easily) available here -> not checking for it // Insert the record into the database - $DB->insert_record('local_adler_course_modules', $data); + $this->db->insert_record('local_adler_course_modules', $data); } + /** + * @throws moodle_exception + * @throws not_an_adler_course_exception + */ public function after_restore_course() { + $this->log->info('Restoring course with plugin set version ' . $this->plugin_set_version); if (empty($this->plugin_set_version)) { - // TODO: run migration code + try { + (new upgrade_3_2_0_to_4_0_0_completionlib($this->task->get_courseid()))->execute(); + } catch (not_an_adler_course_exception $e) { + $this->log->info('Course is not an Adler course, skipping adler completionlib change upgrade. This should not happen as this logic should not be called at all if there is no adler course information in the backup.'); + return; + } } } } diff --git a/tests/backup/moodle2/restore_adler_plugin_test.php b/tests/backup/moodle2/restore_adler_plugin_test.php index 60bac4d..061b482 100644 --- a/tests/backup/moodle2/restore_adler_plugin_test.php +++ b/tests/backup/moodle2/restore_adler_plugin_test.php @@ -3,6 +3,7 @@ // -> no namespace for this test as backup/restore is not namespaced use local_adler\lib\adler_testcase; +use local_adler\local\upgrade\upgrade_3_2_0_to_4_0_0_completionlib; global $CFG; require_once($CFG->dirroot . '/local/adler/tests/lib/adler_testcase.php'); @@ -389,4 +390,43 @@ public function test_process_adler_section($restore_data) { // call the method to test $restore_mock->process_adler_section($restore_data); } + + public function provide_test_call_upgrade_3_2_0_to_4_0_0_completionlib_data() { + return [ + 'legacy course' => ['version' => null], + 'new course' => ['version' => '4.0.0'], + 'invalid course' => ['version' => '1.1.0'], + ]; + } + + /** + * @dataProvider provide_test_call_upgrade_3_2_0_to_4_0_0_completionlib_data + */ + public function test_call_upgrade_3_2_0_to_4_0_0_completionlib($version) { + // setup mock + $upgrade_mock = Mockery::spy('overload:' . upgrade_3_2_0_to_4_0_0_completionlib::class); + if ($version === null) { + $upgrade_mock->shouldReceive('execute')->once(); + } else { + $upgrade_mock->shouldReceive('execute')->never(); + } + + if ($version === '1.1.0') { + $this->expectException(moodle_exception::class); + $this->expectExceptionMessage('invalid_plugin_set_version'); + } + + // setup data + list($data, $stub) = $this->setUpCourse(); + if ($version !== null) { + $data->plugin_set_version = $version; + } + + // create test object + $plugin = new restore_local_adler_plugin('local', 'adler', $stub); + + // call the method to test + $plugin->process_plugin_set_version($data); + $plugin->after_restore_course(); + } } \ No newline at end of file