Skip to content

Commit

Permalink
MDL-75140 mod_data: Tests to cover skipping mapping workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
Amaia Anabitarte committed Oct 11, 2022
1 parent bc7caa3 commit 80a6272
Show file tree
Hide file tree
Showing 8 changed files with 217 additions and 15 deletions.
Expand Up @@ -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 |
Expand Down
2 changes: 0 additions & 2 deletions mod/data/preset/journal/tests/behat/journal_preset.feature
Expand Up @@ -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 |
Expand Down
Expand Up @@ -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 |
Expand Down
Expand Up @@ -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 |
Expand Down
121 changes: 121 additions & 0 deletions 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"
9 changes: 2 additions & 7 deletions mod/data/tests/behat/preview_preset.feature
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand All @@ -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"
Expand All @@ -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"
Binary file not shown.
94 changes: 94 additions & 0 deletions mod/data/tests/preset_importer_test.php
@@ -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());
}
}

0 comments on commit 80a6272

Please sign in to comment.