Skip to content

Commit adbed28

Browse files
committed
Issue #110: Fix restore not correctly finding CMS type.
Restore now correctly finds the matching CMS type for an instance. Does not restore CMS types from backup. No longer crashes if it cannot find a matching CMS type for an instance.
1 parent b6bb4ef commit adbed28

6 files changed

+17
-31
lines changed

backup/moodle2/restore_cms_stepslib.php

+5-29
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,13 @@ protected function process_cms_types(array $data) {
8989

9090
$typeid = $this->find_existing_cms_type($data);
9191

92+
// TODO: Ignoring a missing CMS type currently causes a crash during the restore process.
9293
if ($typeid === false) {
93-
$typeid = $DB->insert_record('cms_types', $data);
94+
throw new moodle_exception('CMS type not found for idnumber ' . $data->idnumber);
9495
}
9596

97+
// If we can find a type ID, we can restore the mapping.
9698
$this->set_mapping('cms_types', $oldid, $typeid, true);
97-
9899
$DB->update_record('cms', (object) ['id' => $this->get_new_parentid('cms'), 'typeid' => $typeid]);
99100
}
100101

@@ -111,33 +112,8 @@ protected function find_existing_cms_type(\stdClass $data) {
111112
return $typeid;
112113
}
113114

114-
if ($this->task->is_samesite()) {
115-
if ($DB->record_exists('cms_types', ['id' => $data->id])) {
116-
return $data->id;
117-
}
118-
}
119-
120-
// Try to find a CMS type is that is a match for content. Returns false if none found.
121-
$sqltitlemustache = $DB->sql_compare_text('title_mustache');
122-
$sqltitlemustacheparam = $DB->sql_compare_text(':title_mustache');
123-
$sqlmustache = $DB->sql_compare_text('mustache');
124-
$sqlmustacheparam = $DB->sql_compare_text(':mustache');
125-
126-
$sql = "SELECT id
127-
FROM {cms_types}
128-
WHERE name = :name
129-
AND idnumber = :idnumber
130-
AND datasources = :datasources
131-
AND $sqltitlemustache = $sqltitlemustacheparam
132-
AND $sqlmustache = $sqlmustacheparam";
133-
$params = [
134-
'name' => $data->name,
135-
'idnumber' => $data->idnumber,
136-
'datasources' => $data->datasources,
137-
'title_mustache' => $data->title_mustache,
138-
'mustache' => $data->mustache,
139-
];
140-
$record = $DB->get_record_sql($sql, $params);
115+
// Try to find a CMS type that matches idnumber. Returns false if none found.
116+
$record = $DB->get_record('cms_types', ['idnumber' => $data->idnumber]);
141117

142118
return $record->id ?? false;
143119
}

classes/customfield/cms_restore.php

-2
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,5 @@ public function cms_restore_instance_data_from_backup(\restore_task $task, array
5656
return $d->get('id');
5757
}
5858
}
59-
// Should not get here.
60-
throw new \moodle_exception("Bad shorname {$data['shortname']}, and type {$data['type']}");
6159
}
6260
}

tests/datasource_fields_test.php

+3
Original file line numberDiff line numberDiff line change
@@ -329,5 +329,8 @@ public function test_duplicate() {
329329
$newds = new dsfields($newcms);
330330

331331
$this->assertEquals($ds->get_data(), $newds->get_data());
332+
333+
// Assert that the CMS type is not duplicated.
334+
$this->assertEquals($cms->get('typeid'), $newcms->get('typeid'));
332335
}
333336
}

tests/datasource_images_test.php

+3
Original file line numberDiff line numberDiff line change
@@ -183,5 +183,8 @@ public function test_duplicate() {
183183
$newds = new dsimages($newcms);
184184

185185
$this->assertEquals($ds->get_data(), $newds->get_data());
186+
187+
// Assert that the CMS type is not duplicated.
188+
$this->assertEquals($cms->get('typeid'), $newcms->get('typeid'));
186189
}
187190
}

tests/datasource_roles_test.php

+3
Original file line numberDiff line numberDiff line change
@@ -311,5 +311,8 @@ public function test_duplicate() {
311311
$newds = new dsroles($newcms);
312312

313313
$this->assertEquals($ds->get_data(), $newds->get_data());
314+
315+
// Assert that the CMS type is not duplicated.
316+
$this->assertEquals($cms->get('typeid'), $newcms->get('typeid'));
314317
}
315318
}

tests/datasource_userlist_test.php

+3
Original file line numberDiff line numberDiff line change
@@ -375,5 +375,8 @@ public function test_duplicate() {
375375
$newds = new dsuserlist($newcms);
376376

377377
$this->assertEquals($ds->get_data(), $newds->get_data());
378+
379+
// Assert that the CMS type is not duplicated.
380+
$this->assertEquals($cms->get('typeid'), $newcms->get('typeid'));
378381
}
379382
}

0 commit comments

Comments
 (0)