mirrored from git://git.moodle.org/moodle.git
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MDL-75140 mod_data: Tests to cover skipping mapping workflow
- Loading branch information
Amaia Anabitarte
committed
Oct 11, 2022
1 parent
bc7caa3
commit 80a6272
Showing
8 changed files
with
217 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
<?php | ||
// This file is part of Moodle - http://moodle.org/ | ||
// | ||
// Moodle is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// Moodle is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
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 <amaia@moodle.com> | ||
* @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()); | ||
} | ||
} |