MDL-74220 data: Implement behat generators

This commit is contained in:
Noel De Martin 2022-05-12 15:42:10 +02:00
parent 6c114e2a80
commit df9f610d1d
2 changed files with 153 additions and 21 deletions

View File

@ -24,30 +24,26 @@ Feature: Users can view and search database entries
And the following "activities" exist: And the following "activities" exist:
| activity | name | intro | course | idnumber | | activity | name | intro | course | idnumber |
| data | Test database name | Database intro | C1 | data1 | | data | Test database name | Database intro | C1 | data1 |
And I log in as "teacher1" And the following "mod_data > fields" exist:
And I am on "Course 1" course homepage | database | type | name | description |
And I add a "Text input" field to "Test database name" database and I fill the form with: | data1 | text | Test field name | Test field description |
| Field name | Test field name | | data1 | text | Test field 2 name | Test field 2 description |
| Field description | Test field description | And the following "mod_data > templates" exist:
And I add a "Text input" field to "Test database name" database and I fill the form with: | database | name |
| Field name | Test field 2 name | | data1 | singletemplate |
| Field description | Test field 2 description | | data1 | listtemplate |
# To generate the default templates. | data1 | addtemplate |
And I navigate to "Templates" in current page administration | data1 | asearchtemplate |
And I log out | data1 | rsstemplate |
@javascript @javascript
Scenario: Students can add view, list and search entries Scenario: Students can add view, list and search entries
Given I am on the "Test database name" "data activity" page logged in as student1 Given the following "mod_data > entries" exist:
And I add an entry to "Test database name" database with: | database | Test field name | Test field 2 name |
| Test field name | Student entry 1 | | data1 | Student entry 1 | |
And I press "Save and add another" | data1 | Student entry 2 | |
And I add an entry to "Test database name" database with: | data1 | Student entry 3 | |
| Test field name | Student entry 2 | When I log in as "student1"
And I press "Save and add another"
And I add an entry to "Test database name" database with:
| Test field name | Student entry 3 |
And I press "Save"
And I am on the "Test database name" "data activity" page And I am on the "Test database name" "data activity" page
Then I should see "Student entry 1" Then I should see "Student entry 1"
And I should see "Student entry 2" And I should see "Student entry 2"

View File

@ -0,0 +1,136 @@
<?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/>.
/**
* Behat data generator for mod_data.
*
* @package mod_data
* @category test
* @copyright 2022 Noel De Martin
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class behat_mod_data_generator extends behat_generator_base {
/**
* Get a list of the entities that Behat can create using the generator step.
*
* @return array
*/
protected function get_creatable_entities(): array {
return [
'entries' => [
'singular' => 'entry',
'datagenerator' => 'entry',
'required' => ['database'],
'switchids' => ['database' => 'databaseid'],
],
'fields' => [
'singular' => 'field',
'datagenerator' => 'field',
'required' => ['database', 'type', 'name'],
'switchids' => ['database' => 'databaseid'],
],
'templates' => [
'singular' => 'template',
'datagenerator' => 'template',
'required' => ['database', 'name'],
'switchids' => ['database' => 'databaseid'],
],
];
}
/**
* Get the database id using an activity idnumber.
*
* @param string $idnumber
* @return int The database id
*/
protected function get_database_id(string $idnumber): int {
$cm = $this->get_cm_by_activity_name('data', $idnumber);
return $cm->instance;
}
/**
* Add an entry.
*
* @param array $data Entry data.
*/
public function process_entry(array $data): void {
global $DB;
$database = $DB->get_record('data', ['id' => $data['databaseid']], '*', MUST_EXIST);
unset($data['databaseid']);
$data = array_reduce(array_keys($data), function ($fields, $fieldname) use ($data, $database) {
global $DB;
$field = $DB->get_record('data_fields', ['name' => $fieldname, 'dataid' => $database->id], 'id', MUST_EXIST);
$fields[$field->id] = $data[$fieldname];
return $fields;
}, []);
$this->get_data_generator()->create_entry($database, $data);
}
/**
* Add a field.
*
* @param array $data Field data.
*/
public function process_field(array $data): void {
global $DB;
$database = $DB->get_record('data', ['id' => $data['databaseid']], '*', MUST_EXIST);
unset($data['databaseid']);
$this->get_data_generator()->create_field((object) $data, $database);
}
/**
* Add a template.
*
* @param array $data Template data.
*/
public function process_template(array $data): void {
global $DB;
$database = $DB->get_record('data', ['id' => $data['databaseid']], '*', MUST_EXIST);
if (empty($data['content'])) {
data_generate_default_template($database, $data['name']);
} else {
$newdata = new stdClass();
$newdata->id = $database->id;
$newdata->{$data['name']} = $data['content'];
$DB->update_record('data', $newdata);
}
}
/**
* Get the module data generator.
*
* @return mod_data_generator Database data generator.
*/
protected function get_data_generator(): mod_data_generator {
return $this->componentdatagenerator;
}
}