[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/auth/tests/behat/ -> behat_auth.php (source)

   1  <?php
   2  
   3  // This file is part of Moodle - http://moodle.org/
   4  //
   5  // Moodle is free software: you can redistribute it and/or modify
   6  // it under the terms of the GNU General Public License as published by
   7  // the Free Software Foundation, either version 3 of the License, or
   8  // (at your option) any later version.
   9  //
  10  // Moodle is distributed in the hope that it will be useful,
  11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13  // GNU General Public License for more details.
  14  //
  15  // You should have received a copy of the GNU General Public License
  16  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  17  
  18  /**
  19   * Basic authentication steps definitions.
  20   *
  21   * @package    core_auth
  22   * @category   test
  23   * @copyright  2012 David Monllaó
  24   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  25   */
  26  
  27  // NOTE: no MOODLE_INTERNAL test here, this file may be required by behat before including /config.php.
  28  
  29  require_once (__DIR__ . '/../../../lib/behat/behat_base.php');
  30  
  31  use Behat\Behat\Context\Step\Given as Given;
  32  use Behat\Behat\Context\Step\When as When;
  33  
  34  /**
  35   * Log in log out steps definitions.
  36   *
  37   * @package    core_auth
  38   * @category   test
  39   * @copyright  2012 David Monllaó
  40   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  41   */
  42  class behat_auth extends behat_base {
  43  
  44      /**
  45       * Logs in the user. There should exist a user with the same value as username and password.
  46       *
  47       * @Given /^I log in as "(?P<username_string>(?:[^"]|\\")*)"$/
  48       */
  49      public function i_log_in_as($username) {
  50  
  51          // Running this step using the API rather than a chained step because
  52          // we need to see if the 'Log in' link is available or we need to click
  53          // the dropdown to expand the navigation bar before.
  54          $this->getSession()->visit($this->locate_path('/'));
  55  
  56          // Generic steps (we will prefix them later expanding the navigation dropdown if necessary).
  57          $steps = array(
  58              new Given('I click on "' . get_string('login') . '" "link" in the ".logininfo" "css_element"'),
  59              new Given('I set the field "' . get_string('username') . '" to "' . $this->escape($username) . '"'),
  60              new Given('I set the field "' . get_string('password') . '" to "'. $this->escape($username) . '"'),
  61              new Given('I press "' . get_string('login') . '"')
  62          );
  63  
  64          // If Javascript is disabled we have enough with these steps.
  65          if (!$this->running_javascript()) {
  66              return $steps;
  67          }
  68  
  69          // Wait for the homepage to be ready.
  70          $this->getSession()->wait(self::TIMEOUT * 1000, self::PAGE_READY_JS);
  71  
  72          // If it is needed, it expands the navigation bar with the 'Log in' link.
  73          if ($clicknavbar = $this->get_expand_navbar_step()) {
  74              array_unshift($steps, $clicknavbar);
  75          }
  76  
  77          return $steps;
  78      }
  79  
  80      /**
  81       * Logs out of the system.
  82       *
  83       * @Given /^I log out$/
  84       */
  85      public function i_log_out() {
  86  
  87          $steps = array(new When('I follow "' . get_string('logout') . '"'));
  88  
  89          // No need to check anything else if we run without JS.
  90          if (!$this->running_javascript()) {
  91              return $steps;
  92          }
  93  
  94          // There is no longer any need to worry about whether the navigation
  95          // bar needs to be expanded; user_menu now lives outside the
  96          // hamburger.
  97  
  98          // However, the user menu *always* needs to be expanded.
  99          $xpath = "//div[@class='usermenu']//a[contains(concat(' ', @class, ' '), ' toggle-display ')]";
 100          array_unshift($steps, new When('I click on "'.$xpath.'" "xpath_element"'));
 101  
 102          return $steps;
 103      }
 104  
 105      /**
 106       * Returns a step to open the navigation bar if it is needed.
 107       *
 108       * The top log in and log out links are hidden when middle or small
 109       * size windows (or devices) are used. This step returns a step definition
 110       * clicking to expand the navbar if it is hidden.
 111       *
 112       * @return Given|bool A step definition or false if there is no need to show the navbar.
 113       */
 114      protected function get_expand_navbar_step() {
 115  
 116          // Checking if we need to click the navbar button to show the navigation menu, it
 117          // is hidden by default when using clean theme and a medium or small screen size.
 118  
 119          // The DOM and the JS should be all ready and loaded. Running without spinning
 120          // as this is a widely used step and we can not spend time here trying to see
 121          // a DOM node that is not always there (at the moment clean is not even the
 122          // default theme...).
 123          $navbuttonjs = "return (
 124              Y.one('.btn-navbar') &&
 125              Y.one('.btn-navbar').getComputedStyle('display') !== 'none'
 126          )";
 127  
 128          // Adding an extra click we need to show the 'Log in' link.
 129          if (!$this->getSession()->getDriver()->evaluateScript($navbuttonjs)) {
 130              return false;
 131          }
 132  
 133          return new Given('I click on ".btn-navbar" "css_element"');
 134      }
 135  }


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