Skip to content

Commit

Permalink
update restore to call code to upgrade courses restored from old mbz …
Browse files Browse the repository at this point in the history
…files (uploaded via old authoring tool versions)
  • Loading branch information
Glutamat42 committed Sep 18, 2024
1 parent 21e59be commit 85c5a07
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 7 deletions.
32 changes: 25 additions & 7 deletions backup/moodle2/restore_local_adler_plugin.class.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<?php

use local_adler\local\course\db as course_db;
use local_adler\local\exceptions\not_an_adler_course_exception;
use local_adler\local\upgrade\upgrade_3_2_0_to_4_0_0_completionlib;
use local_logging\logger;

/**
* Restoring logic for the local Adler plugin.
Expand All @@ -11,11 +14,14 @@ class restore_local_adler_plugin extends restore_local_plugin {
protected $db;

private $plugin_set_version;
private $log;

public function __construct($name, $plugin, $restore) {
global $DB;
parent::__construct($name, $plugin, $restore);
$this->db = $DB;
$this->log = new logger('local_adler', self::class);
$this->plugin_set_version = null;
}

protected function define_course_plugin_structure(): array {
Expand All @@ -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'];
}

/**
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
}
}
}
Expand Down
40 changes: 40 additions & 0 deletions tests/backup/moodle2/restore_adler_plugin_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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();
}
}

0 comments on commit 85c5a07

Please sign in to comment.