diff --git a/mod/data/lib.php b/mod/data/lib.php
index 877310c31a7..0d70fe37624 100644
--- a/mod/data/lib.php
+++ b/mod/data/lib.php
@@ -2704,17 +2704,15 @@ abstract class data_preset_importer {
}
/* Get rid of all old unused data */
- if (!empty($preservedfields)) {
- foreach ($currentfields as $cid => $currentfield) {
- if (!array_key_exists($cid, $preservedfields)) {
- /* Data not used anymore so wipe! */
- print "Deleting field $currentfield->name
";
+ foreach ($currentfields as $cid => $currentfield) {
+ if (!array_key_exists($cid, $preservedfields)) {
+ /* Data not used anymore so wipe! */
+ echo "Deleting field $currentfield->name
";
- $id = $currentfield->id;
- //Why delete existing data records and related comments/ratings??
- $DB->delete_records('data_content', array('fieldid'=>$id));
- $DB->delete_records('data_fields', array('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]);
}
}
diff --git a/mod/data/tests/behat/import_presets.feature b/mod/data/tests/behat/import_presets.feature
new file mode 100644
index 00000000000..ef4d266f90a
--- /dev/null
+++ b/mod/data/tests/behat/import_presets.feature
@@ -0,0 +1,36 @@
+@mod @mod_data @javascript @_file_upload
+Feature: Users can import presets
+ In order to use presets
+ As a user
+ I need to import and apply presets from zip files
+
+ Background:
+ Given the following "users" exist:
+ | username | firstname | lastname | email |
+ | teacher1 | Teacher | 1 | teacher1@example.com |
+ And the following "courses" exist:
+ | fullname | shortname | category |
+ | Course 1 | C1 | 0 |
+ And the following "course enrolments" exist:
+ | user | course | role |
+ | teacher1 | C1 | editingteacher |
+ And the following "activities" exist:
+ | activity | name | intro | course | idnumber |
+ | data | Mountain landscapes | n | C1 | data1 |
+
+ Scenario: Teacher can import from field page on a database with fields 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 "Import" "link"
+ And I upload "mod/data/tests/fixtures/image_gallery_preset.zip" file to "Choose file" filemanager
+ When I click on "Save" "button"
+ And I click on "Continue" "button"
+ Then I should see "The preset has been successfully applied."
+ And I follow "Fields"
+ And I should see "image"
+ And I should see "title"
+ And I should not see "Test field name"
diff --git a/mod/data/tests/behat/use_presets.feature b/mod/data/tests/behat/use_presets.feature
new file mode 100644
index 00000000000..96b8f12f578
--- /dev/null
+++ b/mod/data/tests/behat/use_presets.feature
@@ -0,0 +1,36 @@
+@mod @mod_data @javascript
+Feature: Users can use predefined presets
+ In order to use presets
+ As a user
+ I need to select an existing preset
+
+ Background:
+ Given the following "users" exist:
+ | username | firstname | lastname | email |
+ | teacher1 | Teacher | 1 | teacher1@example.com |
+ And the following "courses" exist:
+ | fullname | shortname | category |
+ | Course 1 | C1 | 0 |
+ And the following "course enrolments" exist:
+ | user | course | role |
+ | teacher1 | C1 | editingteacher |
+ And the following "activities" exist:
+ | activity | name | intro | course | idnumber |
+ | data | Mountain landscapes | introduction... | C1 | data1 |
+ And the following "mod_data > fields" exist:
+ | database | type | name | description |
+ | data1 | text | Test field name | Test field description |
+
+ Scenario: If Teacher use another preset then the previous fields are 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 preset" "button"
+ When I click on "Continue" "button"
+ Then I should see "The preset has been successfully applied."
+ And I follow "Fields"
+ And I should see "image"
+ And I should see "title"
+ And I should not see "Test field name"
diff --git a/mod/data/tests/fixtures/image_gallery_preset.zip b/mod/data/tests/fixtures/image_gallery_preset.zip
new file mode 100644
index 00000000000..7d0cebf731b
Binary files /dev/null and b/mod/data/tests/fixtures/image_gallery_preset.zip differ