Skip to content

Commit

Permalink
Merge branch 'MDL-74220-master' of https://github.com/NoelDeMartin/mo…
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewnicols committed Jun 30, 2022
2 parents 68cc906 + df9f610 commit 956bb8e
Show file tree
Hide file tree
Showing 2 changed files with 153 additions and 21 deletions.
38 changes: 17 additions & 21 deletions mod/data/tests/behat/view_entries.feature
Original file line number Diff line number Diff line change
Expand Up @@ -24,30 +24,26 @@ Feature: Users can view and search database entries
And the following "activities" exist:
| activity | name | intro | course | idnumber |
| data | Test database name | Database intro | C1 | data1 |
And I log in as "teacher1"
And I am on "Course 1" course homepage
And I add a "Text input" field to "Test database name" database and I fill the form with:
| Field name | Test field name |
| Field description | Test field description |
And I add a "Text input" field to "Test database name" database and I fill the form with:
| Field name | Test field 2 name |
| Field description | Test field 2 description |
# To generate the default templates.
And I navigate to "Templates" in current page administration
And I log out
And the following "mod_data > fields" exist:
| database | type | name | description |
| data1 | text | Test field name | Test field description |
| data1 | text | Test field 2 name | Test field 2 description |
And the following "mod_data > templates" exist:
| database | name |
| data1 | singletemplate |
| data1 | listtemplate |
| data1 | addtemplate |
| data1 | asearchtemplate |
| data1 | rsstemplate |

@javascript
Scenario: Students can add view, list and search entries
Given I am on the "Test database name" "data activity" page logged in as student1
And I add an entry to "Test database name" database with:
| Test field name | Student entry 1 |
And I press "Save and add another"
And I add an entry to "Test database name" database with:
| Test field name | Student entry 2 |
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"
Given the following "mod_data > entries" exist:
| database | Test field name | Test field 2 name |
| data1 | Student entry 1 | |
| data1 | Student entry 2 | |
| data1 | Student entry 3 | |
When I log in as "student1"
And I am on the "Test database name" "data activity" page
Then I should see "Student entry 1"
And I should see "Student entry 2"
Expand Down
136 changes: 136 additions & 0 deletions mod/data/tests/generator/behat_mod_data_generator.php
Original file line number Diff line number Diff line change
@@ -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;
}

}

0 comments on commit 956bb8e

Please sign in to comment.