Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403]

   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          // Open "Fields" tab if it is not already open.
  54          $fieldsstr = get_string('fields', 'mod_data');
  55          $xpath = '//ul[contains(@class,\'nav-tabs\')]//*[contains(@class,\'active\') and contains(normalize-space(.), \'' .
  56              $fieldsstr . '\')]';
  57          if (!$this->getSession()->getPage()->findAll('xpath', $xpath)) {
  58              $this->execute("behat_general::i_click_on_in_the", array($fieldsstr, 'link', '.nav-tabs', 'css_element'));
  59          }
  60  
  61          $this->execute('behat_forms::i_set_the_field_to', array('newtype', $this->escape($fieldtype)));
  62  
  63          if (!$this->running_javascript()) {
  64              $this->execute('behat_general::i_click_on_in_the',
  65                  array(get_string('go'), "button", ".fieldadd", "css_element")
  66              );
  67          }
  68  
  69          $this->execute("behat_forms::i_set_the_following_fields_to_these_values", $fielddata);
  70          $this->execute('behat_forms::press_button', get_string('add'));
  71      }
  72  
  73      /**
  74       * Adds an entry to a database.
  75       *
  76       * @Given /^I add an entry to "(?P<activityname_string>(?:[^"]|\\")*)" database with:$/
  77       *
  78       * @param string $activityname
  79       * @param TableNode $entrydata
  80       */
  81      public function i_add_an_entry_to_database_with($activityname, TableNode $entrydata) {
  82          $this->execute('behat_navigation::i_am_on_page_instance', [$this->escape($activityname), 'mod_data > add entry']);
  83          $this->execute("behat_forms::i_set_the_following_fields_to_these_values", $entrydata);
  84      }
  85  
  86      /**
  87       * Convert page names to URLs for steps like 'When I am on the "[identifier]" "[page type]" page'.
  88       *
  89       * Recognised page names are:
  90       * | pagetype  | name meaning  | description                  |
  91       * | Add entry | Database name | Add an entry page (view.php) |
  92       *
  93       * @param string $type identifies which type of page this is, e.g. 'Add entry'.
  94       * @param string $identifier identifies the particular page, e.g. 'My database name'.
  95       * @return moodle_url the corresponding URL.
  96       * @throws Exception with a meaningful error message if the specified page cannot be found.
  97       */
  98      protected function resolve_page_instance_url(string $type, string $identifier): moodle_url {
  99          global $DB;
 100  
 101          switch (strtolower($type)) {
 102              case 'add entry':
 103                  return new moodle_url('/mod/data/edit.php', [
 104                      'd' => $this->get_cm_by_activity_name('data', $identifier)->instance,
 105                  ]);
 106  
 107              default:
 108                  throw new Exception("Unrecognised page type '{$type}'");
 109          }
 110      }
 111  }