Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 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 administration.
  19   *
  20   * @package   core_admin
  21   * @category  test
  22   * @copyright 2013 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  require_once (__DIR__ . '/../../../lib/behat/behat_field_manager.php');
  30  
  31  use Behat\Gherkin\Node\TableNode as TableNode,
  32      Behat\Mink\Exception\ElementNotFoundException as ElementNotFoundException;
  33  
  34  /**
  35   * Site administration level steps definitions.
  36   *
  37   * @package    core_admin
  38   * @category   test
  39   * @copyright  2013 David MonllaĆ³
  40   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  41   */
  42  class behat_admin extends behat_base {
  43  
  44      /**
  45       * Sets the specified site settings. A table with | Setting label | value | is expected.
  46       *
  47       * @Given /^I set the following administration settings values:$/
  48       * @param TableNode $table
  49       */
  50      public function i_set_the_following_administration_settings_values(TableNode $table) {
  51          if (!$data = $table->getRowsHash()) {
  52              return;
  53          }
  54  
  55          foreach ($data as $label => $value) {
  56              // Navigate straight to the search results fo rthis label.
  57              $this->execute('behat_general::i_visit', ["/admin/search.php?query=" . urlencode($label)]);
  58  
  59              // Admin settings does not use the same DOM structure than other moodle forms
  60              // but we also need to use lib/behat/form_field/* to deal with the different moodle form elements.
  61              $exception = new ElementNotFoundException($this->getSession(), '"' . $label . '" administration setting ');
  62  
  63              // The argument should be converted to an xpath literal.
  64              $label = behat_context_helper::escape($label);
  65  
  66              // Single element settings.
  67              try {
  68                  $fieldxpath = "//*[self::input | self::textarea | self::select]" .
  69                          "[not(./@type = 'submit' or ./@type = 'image' or ./@type = 'hidden')]" .
  70                          "[@id=//label[contains(normalize-space(.), $label)]/@for or " .
  71                          "@id=//span[contains(normalize-space(.), $label)]/preceding-sibling::label[1]/@for]";
  72                  $fieldnode = $this->find('xpath', $fieldxpath, $exception);
  73  
  74              } catch (ElementNotFoundException $e) {
  75                  // Multi element settings, interacting only the first one.
  76                  $fieldxpath = "//*[label[contains(., $label)]|span[contains(., $label)]]" .
  77                          "/ancestor::div[contains(concat(' ', normalize-space(@class), ' '), ' form-item ')]" .
  78                          "/descendant::div[contains(concat(' ', @class, ' '), ' form-group ')]" .
  79                          "/descendant::*[self::input | self::textarea | self::select]" .
  80                          "[not(./@type = 'submit' or ./@type = 'image' or ./@type = 'hidden')]";
  81              }
  82  
  83              $this->execute('behat_forms::i_set_the_field_with_xpath_to', [$fieldxpath, $value]);
  84              $this->execute("behat_general::i_click_on", [get_string('savechanges'), 'button']);
  85          }
  86      }
  87  
  88      /**
  89       * Sets the specified site settings. A table with | config | value | (optional)plugin | is expected.
  90       *
  91       * @Given /^the following config values are set as admin:$/
  92       * @param TableNode $table
  93       */
  94      public function the_following_config_values_are_set_as_admin(TableNode $table) {
  95  
  96          if (!$data = $table->getRowsHash()) {
  97              return;
  98          }
  99  
 100          foreach ($data as $config => $value) {
 101              // Default plugin value is null.
 102              $plugin = null;
 103  
 104              if (is_array($value)) {
 105                  $plugin = $value[1];
 106                  $value = $value[0];
 107              }
 108              set_config($config, $value, $plugin);
 109          }
 110      }
 111  
 112      /**
 113       * Waits with the provided params if we are running a JS session.
 114       *
 115       * @param int $timeout
 116       * @param string $javascript
 117       * @return void
 118       */
 119      protected function wait($timeout, $javascript = false) {
 120          if ($this->running_javascript()) {
 121              $this->getSession()->wait($timeout, $javascript);
 122          }
 123      }
 124  }