diff --git a/mod/data/preset/imagegallery/tests/behat/imagegallery_preset.feature b/mod/data/preset/imagegallery/tests/behat/imagegallery_preset.feature index a050dca2194..09ec21ee46a 100644 --- a/mod/data/preset/imagegallery/tests/behat/imagegallery_preset.feature +++ b/mod/data/preset/imagegallery/tests/behat/imagegallery_preset.feature @@ -23,8 +23,6 @@ Feature: Users can use the Image gallery preset And I follow "Presets" And I click on "fullname" "radio" in the "Image gallery" "table_row" And I click on "Use preset" "button" - And I click on "Continue" "button" - And I click on "Continue" "button" And the following "mod_data > entries" exist: | database | user | title | description | image | | data1 | student1 | First image | This is the description text for image 1 | first.png | diff --git a/mod/data/preset/journal/tests/behat/journal_preset.feature b/mod/data/preset/journal/tests/behat/journal_preset.feature index d138c6ba13b..1e7aa582293 100644 --- a/mod/data/preset/journal/tests/behat/journal_preset.feature +++ b/mod/data/preset/journal/tests/behat/journal_preset.feature @@ -23,8 +23,6 @@ Feature: Users can use the Journal preset And I follow "Presets" And I click on "fullname" "radio" in the "Journal" "table_row" And I click on "Use preset" "button" - And I click on "Continue" "button" - And I click on "Continue" "button" And the following "mod_data > entries" exist: | database | user | Title | Content | | data1 | student1 | Reflection created by student | This is the content for the entry 1 | diff --git a/mod/data/preset/proposals/tests/behat/proposals_preset.feature b/mod/data/preset/proposals/tests/behat/proposals_preset.feature index d14c498175d..cad72a7acf6 100644 --- a/mod/data/preset/proposals/tests/behat/proposals_preset.feature +++ b/mod/data/preset/proposals/tests/behat/proposals_preset.feature @@ -23,8 +23,6 @@ Feature: Users can use the Proposals preset And I follow "Presets" And I click on "fullname" "radio" in the "Proposals" "table_row" And I click on "Use preset" "button" - And I click on "Continue" "button" - And I click on "Continue" "button" And the following "mod_data > entries" exist: | database | user | Title | Summary | Content | Status | | data1 | student1 | Project created by student | Summary 1 | Content for entry 1 | Pending | diff --git a/mod/data/preset/resources/tests/behat/resources_preset.feature b/mod/data/preset/resources/tests/behat/resources_preset.feature index c288acae6c9..884057ad3d7 100644 --- a/mod/data/preset/resources/tests/behat/resources_preset.feature +++ b/mod/data/preset/resources/tests/behat/resources_preset.feature @@ -23,8 +23,6 @@ Feature: Users can use the Resources preset And I follow "Presets" And I click on "fullname" "radio" in the "Resources" "table_row" And I click on "Use preset" "button" - And I click on "Continue" "button" - And I click on "Continue" "button" And the following "mod_data > entries" exist: | database | user | Title | Description | Type | Author | Web link | Cover | | data1 | student1 | My favourite book | Book content | Type1 | The book author | http://myfavouritebook.cat | first.png | diff --git a/mod/data/tests/behat/import_presets.feature b/mod/data/tests/behat/import_presets.feature new file mode 100644 index 00000000000..b57e7ea5145 --- /dev/null +++ b/mod/data/tests/behat/import_presets.feature @@ -0,0 +1,121 @@ +@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 | + And the following "mod_data > presets" exist: + | database | name | description | + | data1 | Saved preset 1 | The preset1 has description | + | data1 | Saved preset 2 | | + + Scenario: Teacher can import from preset page on an empty database + Given 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" + Then I should not see "Field mappings" + And I should see "Image" in the "image" "table_row" + + Scenario: Teacher can import from preset page on a database with fields + 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" + Then I should see "Field mappings" + And I should see "image" + And I should see "Create a new field" in the "image" "table_row" + + Scenario: Teacher can import from preset page on a database with entries + And the following "mod_data > fields" exist: + | database | type | name | description | + | data1 | text | field1 | Test field description | + And the following "mod_data > templates" exist: + | database | name | + | data1 | singletemplate | + | data1 | listtemplate | + | data1 | addtemplate | + | data1 | asearchtemplate | + | data1 | rsstemplate | + And the following "mod_data > entries" exist: + | database | field1 | + | data1 | Student entry 1 | + 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" + Then I should see "Field mappings" + And I should see "image" + And I should see "Create a new field" in the "image" "table_row" + + Scenario: Teacher can import from field page on an empty database + Given I am on the "Mountain landscapes" "data activity" page logged in as teacher1 + And I follow "Presets" + And I click on "Import" "button" + And I upload "mod/data/tests/fixtures/image_gallery_preset.zip" file to "Choose file" filemanager + When I click on "Save" "button" + Then I should not see "Field mappings" + And I should see "Image" in the "image" "table_row" + + Scenario: Teacher can import from field page on a database with fields + 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" "button" + And I upload "mod/data/tests/fixtures/image_gallery_preset.zip" file to "Choose file" filemanager + When I click on "Save" "button" + Then I should see "Field mappings" + And I should see "image" + And I should see "Create a new field" in the "image" "table_row" + + Scenario: Teacher can import from field page on a database with entries + And the following "mod_data > fields" exist: + | database | type | name | description | + | data1 | text | field1 | Test field description | + And the following "mod_data > templates" exist: + | database | name | + | data1 | singletemplate | + | data1 | listtemplate | + | data1 | addtemplate | + | data1 | asearchtemplate | + | data1 | rsstemplate | + And the following "mod_data > entries" exist: + | database | field1 | + | data1 | Student entry 1 | + And I am on the "Mountain landscapes" "data activity" page logged in as teacher1 + And I follow "Presets" + And I click on "Import" "button" + And I upload "mod/data/tests/fixtures/image_gallery_preset.zip" file to "Choose file" filemanager + When I click on "Save" "button" + Then I should see "Field mappings" + And I should see "image" + And I should see "Create a new field" in the "image" "table_row" + + Scenario: Teacher can import from zero state page on an empty database + Given I am on the "Mountain landscapes" "data activity" page logged in as teacher1 + And I click on "Import a preset" "button" + And I upload "mod/data/tests/fixtures/image_gallery_preset.zip" file to "Choose file" filemanager + When I click on "Save" "button" + Then I should not see "Field mappings" + And I should see "Image" in the "image" "table_row" diff --git a/mod/data/tests/behat/preview_preset.feature b/mod/data/tests/behat/preview_preset.feature index 3b7e576ecdf..489d4973099 100644 --- a/mod/data/tests/behat/preview_preset.feature +++ b/mod/data/tests/behat/preview_preset.feature @@ -28,7 +28,6 @@ Feature: Users can preview presets And I click on "Import" "button" And I upload "mod/data/tests/fixtures/behat_preset.zip" file to "Choose file" filemanager And I click on "Save" "button" - And I click on "Continue" "button" And I follow "Templates" And I click on "Save as preset" "button" And I set the field "Name" to "Saved preset by teacher1" @@ -75,7 +74,6 @@ Feature: Users can preview presets And I click on "Import" "button" And I upload "mod/data/tests/fixtures/behat_preset.zip" file to "Choose file" filemanager And I click on "Save" "button" - And I click on "Continue" "button" And I follow "Templates" And I click on "Save as preset" "button" And I set the field "Name" to "Saved preset by teacher1" @@ -141,8 +139,7 @@ Feature: Users can preview presets Given I follow "Presets" And I click on "Image gallery" "link" When I click on "Use preset" "button" - Then I should see "Field mappings" - And I should see "image" + Then I should see "image" And I should see "title" @javascript @_file_upload @@ -151,7 +148,6 @@ Feature: Users can preview presets And I click on "Import" "button" And I upload "mod/data/tests/fixtures/behat_preset.zip" file to "Choose file" filemanager And I click on "Save" "button" - And I click on "Continue" "button" And I follow "Templates" And I click on "Save as preset" "button" And I set the field "Name" to "Saved preset by teacher1" @@ -160,5 +156,4 @@ Feature: Users can preview presets When I follow "Presets" And I click on "Saved preset by teacher1" "link" And I click on "Use preset" "button" - Then I should see "Field mappings" - And I should see "My URL field" + Then I should see "My URL field" 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..b11e53698b3 Binary files /dev/null and b/mod/data/tests/fixtures/image_gallery_preset.zip differ diff --git a/mod/data/tests/preset_importer_test.php b/mod/data/tests/preset_importer_test.php new file mode 100644 index 00000000000..db78968cd3b --- /dev/null +++ b/mod/data/tests/preset_importer_test.php @@ -0,0 +1,94 @@ +. + +namespace mod_data; + +use mod_data\local\importer\preset_existing_importer; +use mod_data\local\importer\preset_upload_importer; + +/** + * Preset importer tests class for mod_data. + * + * @package mod_data + * @category test + * @copyright 2022 Amaia Anabitarte + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * @coversDefaultClass \mod_data\local\importer\preset_importer + */ +class preset_importer_test extends \advanced_testcase { + + /** + * Test for needs_mapping method. + * + * @covers ::needs_mapping + */ + public function test_needs_mapping() { + global $CFG, $USER; + + $this->resetAfterTest(); + $this->setAdminUser(); + + // Create a course and a database activity. + $course = $this->getDataGenerator()->create_course(); + $activity = $this->getDataGenerator()->create_module(manager::MODULE, ['course' => $course]); + $manager = manager::create_from_instance($activity); + + // Create presets and importers. + $pluginname = 'imagegallery'; + $plugin = preset::create_from_plugin(null, $pluginname); + $pluginimporter = new preset_existing_importer($manager, '/' . $pluginname); + + $plugingenerator = $this->getDataGenerator()->get_plugin_generator('mod_data'); + $record = (object) [ + 'name' => 'Testing preset name', + 'description' => 'Testing preset description', + ]; + $saved = $plugingenerator->create_preset($activity, $record); + $savedimporter = new preset_existing_importer($manager, $USER->id . '/Testing preset name'); + + $fixturepath = $CFG->dirroot . '/mod/data/tests/fixtures/image_gallery_preset.zip'; + + // Create a storage file. + $draftid = file_get_unused_draft_itemid(); + $filerecord = [ + 'component' => 'user', + 'filearea' => 'draft', + 'contextid' => \context_user::instance($USER->id)->id, + 'itemid' => $draftid, + 'filename' => 'image_gallery_preset.zip', + 'filepath' => '/' + ]; + $fs = get_file_storage(); + $file = $fs->create_file_from_pathname($filerecord, $fixturepath); + $uploadedimporter = new preset_upload_importer($manager, $file->get_filepath()); + + // Needs mapping returns false for empty databases. + $this->assertFalse($pluginimporter->needs_mapping()); + $this->assertFalse($savedimporter->needs_mapping()); + $this->assertFalse($uploadedimporter->needs_mapping()); + + // Add a field to the database. + $fieldrecord = new \stdClass(); + $fieldrecord->name = 'field1'; + $fieldrecord->type = 'text'; + $plugingenerator->create_field($fieldrecord, $activity); + + // Needs mapping returns true for non-empty databases. + $this->assertTrue($pluginimporter->needs_mapping()); + $this->assertTrue($savedimporter->needs_mapping()); + $this->assertTrue($uploadedimporter->needs_mapping()); + } +}