From 09ed58c5a19fe2c16245dd94d012dfdd7326e9af Mon Sep 17 00:00:00 2001 From: Laurent David Date: Thu, 20 Oct 2022 11:15:19 +0200 Subject: [PATCH] MDL-76083 mod_data: Fix preset importer * We should delete fields that have not been marked as to preserve. --- mod/data/lib.php | 18 ++++----- mod/data/tests/behat/import_presets.feature | 36 ++++++++++++++++++ mod/data/tests/behat/use_presets.feature | 36 ++++++++++++++++++ .../tests/fixtures/image_gallery_preset.zip | Bin 0 -> 3951 bytes 4 files changed, 80 insertions(+), 10 deletions(-) create mode 100644 mod/data/tests/behat/import_presets.feature create mode 100644 mod/data/tests/behat/use_presets.feature create mode 100644 mod/data/tests/fixtures/image_gallery_preset.zip 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 0000000000000000000000000000000000000000..7d0cebf731ba37a1868fa945f0ac9efbb8fba52d GIT binary patch literal 3951 zcmbVO2{hDu8y?GKtXXnV5>dn$23@(Kh%z&bX)Hu^r8S_ zASv9cbm_p!29Pe5MDQfKu8DHm<;MbLnnD*Q4NcRBa)a7WDsOq*J*+S`AmlV=mO0RU zizR)Z!t1*tnRq2_Q-M4Q%&n#%o^>9~iEy^ja&)$>yb=pJF zV84crvVaxR^QiKparfXpi&)N8Wsv<$=nLIMfIY}@b@nB|$$Ef2kxKoWIq?0nT6;wz zDdn1?A!p{pf|}E;?_O_yl5E_5^1|C(uP9RzD$!;f_o0t^cQXuGjC$zj?>iY5?*l36 ziR|z0hu!oN4!4Khr51Bs^JmPIM>>NQp7Yxkcq%1{i@)2n!23?t0P8ti6>?MDdMD+U z_5-zki}WFMVhFuUp3qL#Qv0*r$stmYE&jshdWK*ACB4dI!j^VudDzZ#6M-LRamQl< z5**BtJ*|eXc_9#CKtS?8`jLMsUz)WMB}h}c zcuvijpu=H#H^z*K^sd{)bT}iu`{Mq2`R&^-aY)mTG}77HNm+5P<*zr)l*G>%oDgSZ zIL(-|R3zDZ7b*$BE%9UhvfaGaHMr9?kf>J`jIKU=NL8YlYEnkv^Xq4Y=*gJo z)&XkOZl?^{{j!)YmvGIe_aol+R!>V_+a^A7@r`$-R3VwjH&7KxwnXJjY#1mR=#1(< zK-K9TXG`g!DNMifO>jL}x*VF-sn<^vGu1t0G~rVp#rIe7r*bErE*VM*6l0iphmmkb zLg#s22eL^<;zr+JS88jYJUki``|ZuELZ(n1MG!vNeO|jJrZ(Zv0JHbHU=fXwvQNlB z0l`wOUPLr-Dh3qM-Hl57`x*So%p+Oqi3Dp3jn7Dmr7Ln$o_&bASSHF7WgI9w`FJuO z1FO*C=!6<{6<%@GPo1vmfo%4EY0#Yd5W{4+i3>;8)r2ObXpCad@;?=GbCHtHV_Ubf z8(hkJ3z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