Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401] [Versions 400 and 401]

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * Steps definitions related with the database activity.
  19   *
  20   * @package    mod_data
  21   * @category   test
  22   * @copyright  2014 David MonllaĆ³
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  // NOTE: no MOODLE_INTERNAL test here, this file may be required by behat before including /config.php.
  27  
  28  require_once (__DIR__ . '/../../../../lib/behat/behat_base.php');
  29  
  30  use Behat\Gherkin\Node\TableNode as TableNode;
  31  /**
  32   * Database-related steps definitions.
  33   *
  34   * @package    mod_data
  35   * @category   test
  36   * @copyright  2014 David MonllaĆ³
  37   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  38   */
  39  class behat_mod_data extends behat_base {
  40  
  41      /**
  42       * Adds a new field to a database
  43       *
  44       * @Given /^I add a "(?P<fieldtype_string>(?:[^"]|\\")*)" field to "(?P<activityname_string>(?:[^"]|\\")*)" database and I fill the form with:$/
  45       *
  46       * @param string $fieldtype
  47       * @param string $activityname
  48       * @param TableNode $fielddata
  49       */
  50      public function i_add_a_field_to_database_and_i_fill_the_form_with($fieldtype, $activityname, TableNode $fielddata) {
  51          $this->execute('behat_navigation::i_am_on_page_instance', [$this->escape($activityname), 'data activity']);
  52  
  53          $fieldsstr = get_string('fields', 'mod_data');
  54  
  55          $this->execute("behat_navigation::i_navigate_to_in_current_page_administration", $fieldsstr);
  56          $this->execute('behat_general::i_click_on', [get_string('newfield', 'mod_data'), "button"]);
  57          $this->execute('behat_general::i_click_on_in_the',
  58              [$this->escape($fieldtype), "link", "#action_bar", "css_element"]
  59          );
  60  
  61          if (!$this->running_javascript()) {
  62              $this->execute('behat_general::i_click_on_in_the',
  63                  array(get_string('go'), "button", ".fieldadd", "css_element")
  64              );
  65          }
  66  
  67          $this->execute("behat_forms::i_set_the_following_fields_to_these_values", $fielddata);
  68          $this->execute('behat_forms::press_button', get_string('save'));
  69      }
  70  
  71      /**
  72       * Adds an entry to a database.
  73       *
  74       * @Given /^I add an entry to "(?P<activityname_string>(?:[^"]|\\")*)" database with:$/
  75       *
  76       * @param string $activityname
  77       * @param TableNode $entrydata
  78       */
  79      public function i_add_an_entry_to_database_with($activityname, TableNode $entrydata) {
  80          $this->execute('behat_navigation::i_am_on_page_instance', [$this->escape($activityname), 'mod_data > add entry']);
  81          $this->execute("behat_forms::i_set_the_following_fields_to_these_values", $entrydata);
  82      }
  83  
  84      /**
  85       * Convert page names to URLs for steps like 'When I am on the "[identifier]" "[page type]" page'.
  86       *
  87       * Recognised page names are:
  88       * | pagetype  | name meaning  | description                  |
  89       * | Add entry | Database name | Add an entry page (view.php) |
  90       *
  91       * @param string $type identifies which type of page this is, e.g. 'Add entry'.
  92       * @param string $identifier identifies the particular page, e.g. 'My database name'.
  93       * @return moodle_url the corresponding URL.
  94       * @throws Exception with a meaningful error message if the specified page cannot be found.
  95       */
  96      protected function resolve_page_instance_url(string $type, string $identifier): moodle_url {
  97          global $DB;
  98  
  99          switch (strtolower($type)) {
 100              case 'add entry':
 101                  return new moodle_url('/mod/data/edit.php', [
 102                      'd' => $this->get_cm_by_activity_name('data', $identifier)->instance,
 103                  ]);
 104  
 105              default:
 106                  throw new Exception("Unrecognised page type '{$type}'");
 107          }
 108      }
 109  }