MDL-76083 mod_data: Fix preset importer to remove existing fields.

* We should delete fields that have not been marked as to preserve.

Co-authored by: Laurent David <laurent.david@moodle.com>
This commit is contained in:
Amaia Anabitarte 2022-11-16 13:08:08 +01:00
parent 7577776565
commit 5f6d35e471
4 changed files with 45 additions and 21 deletions

View file

@ -283,17 +283,12 @@ abstract class preset_importer {
} }
// Get rid of all old unused data. // Get rid of all old unused data.
if (!empty($preservedfields)) { foreach ($currentfields as $cid => $currentfield) {
foreach ($currentfields as $cid => $currentfield) { if (!array_key_exists($cid, $this->fieldstoupdate)) {
if (!array_key_exists($cid, $preservedfields)) { $id = $currentfield->id;
// Data not used anymore so wipe! // Why delete existing data records and related comments/ratings??
print "Deleting field $currentfield->name<br />"; $DB->delete_records('data_content', ['fieldid' => $id]);
$DB->delete_records('data_fields', ['id' => $id]);
$id = $currentfield->id;
// Why delete existing data records and related comments/ratings??
$DB->delete_records('data_content', ['fieldid' => $id]);
$DB->delete_records('data_fields', ['id' => $id]);
}
} }
} }

View file

@ -2544,17 +2544,15 @@ abstract class data_preset_importer {
} }
/* Get rid of all old unused data */ /* Get rid of all old unused data */
if (!empty($preservedfields)) { foreach ($currentfields as $cid => $currentfield) {
foreach ($currentfields as $cid => $currentfield) { if (!array_key_exists($cid, $preservedfields)) {
if (!array_key_exists($cid, $preservedfields)) { /* Data not used anymore so wipe! */
/* Data not used anymore so wipe! */ echo "Deleting field $currentfield->name<br />";
print "Deleting field $currentfield->name<br />";
$id = $currentfield->id; $id = $currentfield->id;
//Why delete existing data records and related comments/ratings?? // Why delete existing data records and related comments/ratings??
$DB->delete_records('data_content', array('fieldid'=>$id)); $DB->delete_records('data_content', ['fieldid' => $id]);
$DB->delete_records('data_fields', array('id'=>$id)); $DB->delete_records('data_fields', ['id' => $id]);
}
} }
} }

View file

@ -161,3 +161,20 @@ Feature: Users can import presets
And I click on "Import preset and apply" "button" And I click on "Import preset and apply" "button"
Then I should not see "Fields mappings" Then I should not see "Fields mappings"
And I should see "Preset applied" And I should see "Preset applied"
Scenario: Teacher can import from field page on a non-empty database and previous fields will be removed
Given the following "mod_data > fields" exist:
| database | type | name | description |
| data1 | text | Test field name | Test field description |
And I am on the "Mountain landscapes" "data activity" page logged in as teacher1
And I follow "Presets"
And I click on "Actions" "button"
And I choose "Import preset" in the open action menu
And I upload "mod/data/tests/fixtures/image_gallery_preset.zip" file to "Preset file" filemanager
When I click on "Import preset and apply" "button"
And I click on "Continue" "button"
Then I should see "Preset applied."
And I follow "Fields"
And I should see "image"
And I should see "title"
And I should not see "Test field name"

View file

@ -238,3 +238,17 @@ Feature: Users can use predefined presets
And I should see "Existing fields to be deleted: number" And I should see "Existing fields to be deleted: number"
And I should not see "If fields to be deleted are of the same type as fields to be created" And I should not see "If fields to be deleted are of the same type as fields to be created"
And I should see "If fields to be deleted are of the same type as new fields in the preset" And I should see "If fields to be deleted are of the same type as new fields in the preset"
Scenario: Teacher can use a preset on a non-empty database and previous fields will be removed
Given I am on the "Mountain landscapes" "data activity" page logged in as teacher1
And I follow "Fields"
And I should see "Test field name"
And I follow "Presets"
And I click on "fullname" "radio" in the "Image gallery" "table_row"
And I click on "Use this preset" "button"
And I should see "Existing fields to be deleted: Test field name"
When I click on "Apply preset" "button"
Then I should see "Preset applied."
And I should see "image"
And I should see "title"
And I should not see "Test field name"