[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/lib/behat/ -> behat_files.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   * Files interactions with behat.
  19   *
  20   * Note that steps definitions files can not extend other steps definitions files, so
  21   * steps definitions which makes use of file attachments or filepicker should
  22   * extend behat_files instead of behat_base.
  23   *
  24   * @package    core
  25   * @category   test
  26   * @copyright  2013 David Monllaó
  27   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  28   */
  29  
  30  // NOTE: no MOODLE_INTERNAL test here, this file may be required by behat before including /config.php.
  31  
  32  require_once (__DIR__ . '/behat_base.php');
  33  
  34  use Behat\Mink\Exception\ExpectationException as ExpectationException,
  35      Behat\Mink\Element\NodeElement as NodeElement;
  36  
  37  /**
  38   * Files-related actions.
  39   *
  40   * Steps definitions related with filepicker or repositories should extend
  41   * this class instead of behat_base as it provides useful methods to deal
  42   * with the common filepicker issues.
  43   *
  44   * @package    core
  45   * @category   test
  46   * @copyright  2013 David Monllaó
  47   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  48   */
  49  class behat_files extends behat_base {
  50  
  51      /**
  52       * Gets the NodeElement for filepicker of filemanager moodleform element.
  53       *
  54       * The filepicker/filemanager element label is pointing to a hidden input which is
  55       * not recognized as a named selector, as it is hidden...
  56       *
  57       * @throws ExpectationException Thrown by behat_base::find
  58       * @param string $filepickerelement The filepicker form field label
  59       * @return NodeElement The hidden element node.
  60       */
  61      protected function get_filepicker_node($filepickerelement) {
  62  
  63          // More info about the problem (in case there is a problem).
  64          $exception = new ExpectationException('"' . $filepickerelement . '" filepicker can not be found', $this->getSession());
  65  
  66          // If no file picker label is mentioned take the first file picker from the page.
  67          if (empty($filepickerelement)) {
  68              $filepickercontainer = $this->find(
  69                  'xpath',
  70                  "//*[@class=\"form-filemanager\"]",
  71                  $exception
  72              );
  73          } else {
  74              // Gets the ffilemanager node specified by the locator which contains the filepicker container.
  75              $filepickerelement = behat_context_helper::escape($filepickerelement);
  76              $filepickercontainer = $this->find(
  77                  'xpath',
  78                  "//input[./@id = //label[normalize-space(.)=$filepickerelement]/@for]" .
  79                      "//ancestor::div[contains(concat(' ', normalize-space(@class), ' '), ' ffilemanager ') or " .
  80                      "contains(concat(' ', normalize-space(@class), ' '), ' ffilepicker ')]",
  81                  $exception
  82              );
  83          }
  84  
  85          return $filepickercontainer;
  86      }
  87  
  88      /**
  89       * Performs $action on a filemanager container element (file or folder).
  90       *
  91       * It works together with open_element_contextual_menu
  92       * as this method needs the contextual menu to be opened.
  93       *
  94       * @throws ExpectationException Thrown by behat_base::find
  95       * @param string $action
  96       * @param ExpectationException $exception
  97       * @return void
  98       */
  99      protected function perform_on_element($action, ExpectationException $exception) {
 100  
 101          // Finds the button inside the DOM, is a modal window, so should be unique.
 102          $classname = 'fp-file-' . $action;
 103          $button = $this->find('css', '.moodle-dialogue-focused button.' . $classname, $exception);
 104  
 105          $this->ensure_node_is_visible($button);
 106          $button->click();
 107      }
 108  
 109      /**
 110       * Opens the contextual menu of a folder or a file.
 111       *
 112       * Works both in filemanager elements and when dealing with repository
 113       * elements inside filepicker modal window.
 114       *
 115       * @throws ExpectationException Thrown by behat_base::find
 116       * @param string $name The name of the folder/file
 117       * @param string $filemanagerelement The filemanager form element locator, the repository items are in filepicker modal window if false
 118       * @return void
 119       */
 120      protected function open_element_contextual_menu($name, $filemanagerelement = false) {
 121  
 122          // If a filemanager is specified we restrict the search to the descendants of this particular filemanager form element.
 123          $containernode = false;
 124          $exceptionmsg = '"'.$name.'" element can not be found';
 125          if ($filemanagerelement) {
 126              $containernode = $this->get_filepicker_node($filemanagerelement);
 127              $exceptionmsg = 'The "'.$filemanagerelement.'" filemanager ' . $exceptionmsg;
 128              $locatorprefix = "//div[@class='fp-content']";
 129          } else {
 130              $locatorprefix = "//div[contains(concat(' ', normalize-space(@class), ' '), ' fp-repo-items ')]//descendant::div[@class='fp-content']";
 131          }
 132  
 133          $exception = new ExpectationException($exceptionmsg, $this->getSession());
 134  
 135          // Avoid quote-related problems.
 136          $name = behat_context_helper::escape($name);
 137  
 138          // Get a filepicker/filemanager element (folder or file).
 139          try {
 140  
 141              // First we look at the folder as we need to click on the contextual menu otherwise it would be opened.
 142              $node = $this->find(
 143                  'xpath',
 144                  $locatorprefix .
 145                      "//descendant::*[self::div | self::a][contains(concat(' ', normalize-space(@class), ' '), ' fp-file ')]" .
 146                      "[contains(concat(' ', normalize-space(@class), ' '), ' fp-folder ')]" .
 147                      "[normalize-space(.)=$name]" .
 148                      "//descendant::a[contains(concat(' ', normalize-space(@class), ' '), ' fp-contextmenu ')]",
 149                  $exception,
 150                  $containernode
 151              );
 152  
 153          } catch (ExpectationException $e) {
 154  
 155              // Here the contextual menu is hidden, we click on the thumbnail.
 156              $node = $this->find(
 157                  'xpath',
 158                  $locatorprefix .
 159                  "//descendant::*[self::div | self::a][contains(concat(' ', normalize-space(@class), ' '), ' fp-file ')]" .
 160                  "[normalize-space(.)=$name]" .
 161                  "//descendant::div[contains(concat(' ', normalize-space(@class), ' '), ' fp-filename-field ')]",
 162                  false,
 163                  $containernode
 164              );
 165          }
 166  
 167          // Click opens the contextual menu when clicking on files.
 168          $this->ensure_node_is_visible($node);
 169          $node->click();
 170      }
 171  
 172      /**
 173       * Opens the filepicker modal window and selects the repository.
 174       *
 175       * @throws ExpectationException Thrown by behat_base::find
 176       * @param NodeElement $filemanagernode The filemanager or filepicker form element DOM node.
 177       * @param mixed $repositoryname The repo name.
 178       * @return void
 179       */
 180      protected function open_add_file_window($filemanagernode, $repositoryname) {
 181  
 182          $exception = new ExpectationException('No files can be added to the specified filemanager', $this->getSession());
 183  
 184          // We should deal with single-file and multiple-file filemanagers,
 185          // catching the exception thrown by behat_base::find() in case is not multiple
 186          try {
 187              // Looking for the add button inside the specified filemanager.
 188              $add = $this->find('css', 'div.fp-btn-add a', $exception, $filemanagernode);
 189          } catch (Exception $e) {
 190              // Otherwise should be a single-file filepicker form element.
 191              $add = $this->find('css', 'input.fp-btn-choose', $exception, $filemanagernode);
 192          }
 193          $this->ensure_node_is_visible($add);
 194          $add->click();
 195  
 196          // Wait for the default repository (if any) to load. This checks that
 197          // the relevant div exists and that it does not include the loading image.
 198          $this->ensure_element_exists(
 199                  "//div[contains(concat(' ', normalize-space(@class), ' '), ' file-picker ')]" .
 200                  "//div[contains(concat(' ', normalize-space(@class), ' '), ' fp-content ')]" .
 201                  "[not(descendant::div[contains(concat(' ', normalize-space(@class), ' '), ' fp-content-loading ')])]",
 202                  'xpath_element');
 203  
 204          // Getting the repository link and opening it.
 205          $repoexception = new ExpectationException('The "' . $repositoryname . '" repository has not been found', $this->getSession());
 206  
 207          // Avoid problems with both double and single quotes in the same string.
 208          $repositoryname = behat_context_helper::escape($repositoryname);
 209  
 210          // Here we don't need to look inside the selected element because there can only be one modal window.
 211          $repositorylink = $this->find(
 212              'xpath',
 213              "//div[contains(concat(' ', normalize-space(@class), ' '), ' fp-repo-area ')]" .
 214                  "//descendant::span[contains(concat(' ', normalize-space(@class), ' '), ' fp-repo-name ')]" .
 215                  "[normalize-space(.)=$repositoryname]",
 216              $repoexception
 217          );
 218  
 219          // Selecting the repo.
 220          $this->ensure_node_is_visible($repositorylink);
 221          if (!$repositorylink->getParent()->getParent()->hasClass('active')) {
 222              // If the repository link is active, then the repository is already loaded.
 223              // Clicking it while it's active causes issues, so only click it when it isn't (see MDL-51014).
 224              $repositorylink->click();
 225          }
 226      }
 227  
 228      /**
 229       * Waits until the file manager modal windows are closed.
 230       *
 231       * This method is not used by any of our step definitions,
 232       * keeping it here for users already using it.
 233       *
 234       * @throws ExpectationException
 235       * @return void
 236       */
 237      protected function wait_until_return_to_form() {
 238  
 239          $exception = new ExpectationException('The file manager is taking too much time to finish the current action', $this->getSession());
 240  
 241           $this->find(
 242               'xpath',
 243               "//div[contains(concat(' ', @class, ' '), ' moodle-dialogue-lightbox ')][contains(@style, 'display: none;')]",
 244               $exception
 245           );
 246      }
 247  
 248      /**
 249       * Checks that the file manager contents are not being updated.
 250       *
 251       * This method is not used by any of our step definitions,
 252       * keeping it here for users already using it.
 253       *
 254       * @throws ExpectationException
 255       * @param NodeElement $filepickernode The file manager DOM node
 256       * @return void
 257       */
 258      protected function wait_until_contents_are_updated($filepickernode) {
 259  
 260          $exception = new ExpectationException(
 261              'The file manager contents are requiring too much time to be updated',
 262              $this->getSession()
 263          );
 264  
 265          // Looks for the loading image not being displayed. For single-file filepickers is
 266          // only used when accessing the filepicker, there is no filemanager-loading after selecting the file.
 267          $this->find(
 268              'xpath',
 269              "//div[contains(concat(' ', normalize-space(@class), ' '), ' filemanager ')]" .
 270                  "[not(contains(concat(' ', normalize-space(@class), ' '), ' fm-updating '))]" .
 271              "|" .
 272              "//div[contains(concat(' ', normalize-space(@class), ' '), ' filemanager-loading ')]" .
 273                  "[contains(@style, 'display: none;')]",
 274              $exception,
 275              $filepickernode
 276          );
 277      }
 278  
 279  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1