From 4d0ac716bcdbbd7c9077c820153cc186e2ef5ab4 Mon Sep 17 00:00:00 2001 From: Shamim Rezaie Date: Mon, 10 Dec 2018 15:09:57 +1100 Subject: [PATCH] MDL-53140 behat: Allow language customisation in behat --- lib/tests/behat/behat_data_generators.php | 59 +++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/lib/tests/behat/behat_data_generators.php b/lib/tests/behat/behat_data_generators.php index 51664c8d749fd..ce1584b4bdfc9 100644 --- a/lib/tests/behat/behat_data_generators.php +++ b/lib/tests/behat/behat_data_generators.php @@ -208,6 +208,10 @@ class behat_data_generators extends behat_base { 'required' => array('user', 'contact'), 'switchids' => array('user' => 'userid', 'contact' => 'contactid') ), + 'language customisations' => array( + 'datagenerator' => 'customlang', + 'required' => array('component', 'stringid', 'value'), + ), ); /** @@ -415,6 +419,61 @@ protected function process_block_instance($data) { $this->datagenerator->create_block($data['blockname'], $data, $data); } + /** + * Creates language customisation. + * + * @throws Exception + * @throws dml_exception + * @param array $data + * @return void + */ + protected function process_customlang($data) { + global $CFG, $DB, $USER; + + require_once($CFG->dirroot . '/' . $CFG->admin . '/tool/customlang/locallib.php'); + require_once($CFG->libdir . '/adminlib.php'); + + if (empty($data['component'])) { + throw new Exception('\'customlang\' requires the field \'component\' type to be specified'); + } + + if (empty($data['stringid'])) { + throw new Exception('\'customlang\' requires the field \'stringid\' to be specified'); + } + + if (!isset($data['value'])) { + throw new Exception('\'customlang\' requires the field \'value\' to be specified'); + } + + $now = time(); + + tool_customlang_utils::checkout($USER->lang); + + $record = $DB->get_record_sql("SELECT s.* + FROM {tool_customlang} s + JOIN {tool_customlang_components} c ON s.componentid = c.id + WHERE c.name = ? AND s.lang = ? AND s.stringid = ?", + array($data['component'], $USER->lang, $data['stringid'])); + + if (empty($data['value']) && !is_null($record->local)) { + $record->local = null; + $record->modified = 1; + $record->outdated = 0; + $record->timecustomized = null; + $DB->update_record('tool_customlang', $record); + tool_customlang_utils::checkin($USER->lang); + } + + if (!empty($data['value']) && $data['value'] != $record->local) { + $record->local = $data['value']; + $record->modified = 1; + $record->outdated = 0; + $record->timecustomized = $now; + $DB->update_record('tool_customlang', $record); + tool_customlang_utils::checkin($USER->lang); + } + } + /** * Adapter to enrol_user() data generator. * @throws Exception