diff --git a/mod/glossary/tests/behat/search_entries.feature b/mod/glossary/tests/behat/search_entries.feature index be92ad73923..394b2399de7 100644 --- a/mod/glossary/tests/behat/search_entries.feature +++ b/mod/glossary/tests/behat/search_entries.feature @@ -19,20 +19,14 @@ Feature: Glossary entries can be searched or browsed by alphabet, category, date And the following "activities" exist: | activity | name | intro | displayformat | course | idnumber | | glossary | Test glossary name | Test glossary description | fullwithauthor | C1 | g1 | - And I am on the "Test glossary name" "glossary activity" page logged in as teacher1 - And I add a glossary entries category named "The ones I like" - And I add a glossary entries category named "All for you" - And I add a glossary entry with the following data: - | Concept | Eggplant | - | Definition | Sour eggplants | - | Categories | All for you | - And I log out - And I am on the "Test glossary name" "glossary activity" page logged in as student1 - And I add a glossary entry with the following data: - | Concept | Cucumber | - | Definition | Sweet cucumber | - | Categories | The ones I like | - And I log out + And the following "mod_glossary > categories" exist: + | glossary | name | + | g1 | The ones I like | + | g1 | All for you | + And the following "mod_glossary > entries" exist: + | glossary | concept | definition | user | categories | + | g1 | Eggplant | Sour eggplants | teacher1 | All for you | + | g1 | Cucumber | Sweet cucumber | student1 | The ones I like | And I am on the "Test glossary name" "glossary activity" page logged in as teacher1 @javascript diff --git a/mod/glossary/tests/generator/behat_mod_glossary_generator.php b/mod/glossary/tests/generator/behat_mod_glossary_generator.php new file mode 100644 index 00000000000..39af67d4d3a --- /dev/null +++ b/mod/glossary/tests/generator/behat_mod_glossary_generator.php @@ -0,0 +1,110 @@ +. + +/** + * Behat data generator for mod_glossary. + * + * @package mod_glossary + * @category test + * @copyright 2021 Noel De Martin + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class behat_mod_glossary_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 [ + 'categories' => [ + 'singular' => 'category', + 'datagenerator' => 'category', + 'required' => ['glossary', 'name'], + 'switchids' => ['glossary' => 'glossaryid'], + ], + 'entries' => [ + 'singular' => 'entry', + 'datagenerator' => 'entry', + 'required' => ['glossary', 'concept', 'definition'], + 'switchids' => ['glossary' => 'glossaryid', 'user' => 'userid'], + ], + ]; + } + + /** + * Get the glossary id using an activity idnumber. + * + * @param string $idnumber + * @return int The glossary id + */ + protected function get_glossary_id(string $idnumber): int { + $cm = $this->get_cm_by_activity_name('glossary', $idnumber); + + return $cm->instance; + } + + /** + * Add a category. + * + * @param array $data Category data. + */ + public function process_category(array $data) { + global $DB; + + $glossary = $DB->get_record('glossary', ['id' => $data['glossaryid']], '*', MUST_EXIST); + + unset($data['glossaryid']); + + $this->get_data_generator()->create_category($glossary, $data); + } + + /** + * Preprocess entry data. + * + * @param array $data Raw data. + * @return array Processed data. + */ + protected function preprocess_entry(array $data): array { + if (isset($data['categories'])) { + $categorynames = array_map('trim', explode(',', $data['categories'])); + $categoryids = array_map(function ($categoryname) { + global $DB; + + if (!$id = $DB->get_field('glossary_categories', 'id', ['name' => $categoryname])) { + throw new Exception('The specified category with name "' . $categoryname . '" could not be found.'); + } + + return $id; + }, $categorynames); + + $data['categoryids'] = $categoryids; + unset($data['categories']); + } + + return $data; + } + + /** + * Get the module data generator. + * + * @return mod_glossary_generator Glossary data generator. + */ + protected function get_data_generator() { + return $this->componentdatagenerator; + } +} diff --git a/mod/glossary/tests/generator/lib.php b/mod/glossary/tests/generator/lib.php index f0092c669ba..56debf62cca 100644 --- a/mod/glossary/tests/generator/lib.php +++ b/mod/glossary/tests/generator/lib.php @@ -107,10 +107,58 @@ class mod_glossary_generator extends testing_module_generator { } public function create_content($glossary, $record = array(), $aliases = array()) { + global $DB; + + $entry = $this->create_entry((array)$record + ['glossaryid' => $glossary->id]); + + if ($aliases) { + foreach ($aliases as $alias) { + $ar = new stdClass(); + $ar->entryid = $entry->id; + $ar->alias = $alias; + $DB->insert_record('glossary_alias', $ar); + } + } + + if (array_key_exists('tags', $record)) { + $tags = is_array($record['tags']) ? $record['tags'] : preg_split('/,/', $record['tags']); + + core_tag_tag::set_item_tags('mod_glossary', 'glossary_entries', $entry->id, + context_module::instance($glossary->cmid), $tags); + } + + return $entry; + } + + /** + * Create an entry. + * + * @param array $data Data to create the entry record. + * In addition to columns in the entry table, the following attributes are supported: + * - glossaryid (required): Id of the glossary where the entry will be created. + * - categoryids: Array of ids for the categories this entry belongs to. + * + * @return stdClass Entry record. + */ + public function create_entry(array $data): stdClass { global $DB, $USER, $CFG; + + // Prepare glossary. + $coursemodule = get_coursemodule_from_instance('glossary', $data['glossaryid']); + $glossary = $DB->get_record('glossary', ['id' => $data['glossaryid']], '*', MUST_EXIST); + $glossary->cmid = $coursemodule->id; + + unset($data['glossaryid']); + + // Prepare category ids. + $categoryids = $data['categoryids'] ?? []; + + unset($data['categoryids']); + + // Create entry. $this->entrycount++; $now = time(); - $record = (array)$record + array( + $record = $data + [ 'glossaryid' => $glossary->id, 'timecreated' => $now, 'timemodified' => $now, @@ -122,7 +170,7 @@ class mod_glossary_generator extends testing_module_generator { 'usedynalink' => $CFG->glossary_linkentries, 'casesensitive' => $CFG->glossary_casesensitive, 'fullmatch' => $CFG->glossary_fullmatch - ); + ]; if (!isset($record['teacherentry']) || !isset($record['approved'])) { $context = context_module::instance($glossary->cmid); if (!isset($record['teacherentry'])) { @@ -136,20 +184,8 @@ class mod_glossary_generator extends testing_module_generator { $id = $DB->insert_record('glossary_entries', $record); - if ($aliases) { - foreach ($aliases as $alias) { - $ar = new stdClass(); - $ar->entryid = $id; - $ar->alias = $alias; - $DB->insert_record('glossary_alias', $ar); - } - } - - if (array_key_exists('tags', $record)) { - $tags = is_array($record['tags']) ? $record['tags'] : preg_split('/,/', $record['tags']); - - core_tag_tag::set_item_tags('mod_glossary', 'glossary_entries', $id, - context_module::instance($glossary->cmid), $tags); + foreach ($categoryids as $categoryid) { + $DB->insert_record('glossary_entries_categories', ['entryid' => $id, 'categoryid' => $categoryid]); } return $DB->get_record('glossary_entries', array('id' => $id), '*', MUST_EXIST);