[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/admin/tests/behat/ -> behat_admin.php (source)

   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\Behat\Context\Step\Given as Given,
  32      Behat\Gherkin\Node\TableNode as TableNode,
  33      Behat\Mink\Exception\ElementNotFoundException as ElementNotFoundException;
  34  
  35  /**
  36   * Site administration level steps definitions.
  37   *
  38   * @package    core_admin
  39   * @category   test
  40   * @copyright  2013 David Monllaó
  41   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  42   */
  43  class behat_admin extends behat_base {
  44  
  45      /**
  46       * Sets the specified site settings. A table with | Setting label | value | is expected.
  47       *
  48       * @Given /^I set the following administration settings values:$/
  49       * @param TableNode $table
  50       */
  51      public function i_set_the_following_administration_settings_values(TableNode $table) {
  52  
  53          if (!$data = $table->getRowsHash()) {
  54              return;
  55          }
  56  
  57          foreach ($data as $label => $value) {
  58  
  59              // We expect admin block to be visible, otherwise go to homepage.
  60              if (!$this->getSession()->getPage()->find('css', '.block_settings')) {
  61                  $this->getSession()->visit($this->locate_path('/'));
  62                  $this->wait(self::TIMEOUT * 1000, self::PAGE_READY_JS);
  63              }
  64  
  65              // Search by label.
  66              $searchbox = $this->find_field(get_string('searchinsettings', 'admin'));
  67              $searchbox->setValue($label);
  68              $submitsearch = $this->find('css', 'form.adminsearchform input[type=submit]');
  69              $submitsearch->press();
  70  
  71              $this->wait(self::TIMEOUT * 1000, self::PAGE_READY_JS);
  72  
  73              // Admin settings does not use the same DOM structure than other moodle forms
  74              // but we also need to use lib/behat/form_field/* to deal with the different moodle form elements.
  75              $exception = new ElementNotFoundException($this->getSession(), '"' . $label . '" administration setting ');
  76  
  77              // The argument should be converted to an xpath literal.
  78              $label = $this->getSession()->getSelectorsHandler()->xpathLiteral($label);
  79  
  80              // Single element settings.
  81              try {
  82                  $fieldxpath = "//*[self::input | self::textarea | self::select][not(./@type = 'submit' or ./@type = 'image' or ./@type = 'hidden')]" .
  83                      "[@id=//label[contains(normalize-space(.), $label)]/@for or " .
  84                      "@id=//span[contains(normalize-space(.), $label)]/preceding-sibling::label[1]/@for]";
  85                  $fieldnode = $this->find('xpath', $fieldxpath, $exception);
  86  
  87                  $formfieldtypenode = $this->find('xpath', $fieldxpath . "/ancestor::div[@class='form-setting']" .
  88                      "/child::div[contains(concat(' ', @class, ' '),  ' form-')]/child::*/parent::div");
  89  
  90              } catch (ElementNotFoundException $e) {
  91  
  92                  // Multi element settings, interacting only the first one.
  93                  $fieldxpath = "//*[label[.= $label]|span[.= $label]]/ancestor::div[contains(concat(' ', normalize-space(@class), ' '), ' form-item ')]" .
  94                      "/descendant::div[@class='form-group']/descendant::*[self::input | self::textarea | self::select][not(./@type = 'submit' or ./@type = 'image' or ./@type = 'hidden')]";
  95                  $fieldnode = $this->find('xpath', $fieldxpath);
  96  
  97                  // It is the same one that contains the type.
  98                  $formfieldtypenode = $fieldnode;
  99              }
 100  
 101              // Getting the class which contains the field type.
 102              $classes = explode(' ', $formfieldtypenode->getAttribute('class'));
 103              foreach ($classes as $class) {
 104                  if (substr($class, 0, 5) == 'form-') {
 105                      $type = substr($class, 5);
 106                  }
 107              }
 108  
 109              // Instantiating the appropiate field type.
 110              $field = behat_field_manager::get_field_instance($type, $fieldnode, $this->getSession());
 111              $field->set_value($value);
 112  
 113              $this->find_button(get_string('savechanges'))->press();
 114          }
 115      }
 116  
 117      /**
 118       * Waits with the provided params if we are running a JS session.
 119       *
 120       * @param int $timeout
 121       * @param string $javascript
 122       * @return void
 123       */
 124      protected function wait($timeout, $javascript = false) {
 125          if ($this->running_javascript()) {
 126              $this->getSession()->wait($timeout, $javascript);
 127          }
 128      }
 129  }


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1