[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/repository/upload/tests/behat/ -> behat_repository_upload.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 for the upload repository type.
  19   *
  20   * @package    repository_upload
  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_files.php');
  29  
  30  use Behat\Mink\Exception\ExpectationException as ExpectationException,
  31      Behat\Gherkin\Node\TableNode as TableNode;
  32  
  33  /**
  34   * Steps definitions to deal with the upload repository.
  35   *
  36   * Extends behat_files rather than behat_base as is file-related.
  37   *
  38   * @package    repository_upload
  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_repository_upload extends behat_files {
  44  
  45      /**
  46       * Uploads a file to the specified filemanager leaving other fields in upload form default. The paths should be relative to moodle codebase.
  47       *
  48       * @When /^I upload "(?P<filepath_string>(?:[^"]|\\")*)" file to "(?P<filemanager_field_string>(?:[^"]|\\")*)" filemanager$/
  49       * @throws ExpectationException Thrown by behat_base::find
  50       * @param string $filepath
  51       * @param string $filemanagerelement
  52       */
  53      public function i_upload_file_to_filemanager($filepath, $filemanagerelement) {
  54          $this->upload_file_to_filemanager($filepath, $filemanagerelement, new TableNode(), false);
  55      }
  56  
  57      /**
  58       * Uploads a file to the specified filemanager leaving other fields in upload form default and confirms to overwrite an existing file. The paths should be relative to moodle codebase.
  59       *
  60       * @When /^I upload and overwrite "(?P<filepath_string>(?:[^"]|\\")*)" file to "(?P<filemanager_field_string>(?:[^"]|\\")*)" filemanager$/
  61       * @throws ExpectationException Thrown by behat_base::find
  62       * @param string $filepath
  63       * @param string $filemanagerelement
  64       */
  65      public function i_upload_and_overwrite_file_to_filemanager($filepath, $filemanagerelement) {
  66          $this->upload_file_to_filemanager($filepath, $filemanagerelement, new TableNode(),
  67                  get_string('overwrite', 'repository'));
  68      }
  69  
  70      /**
  71       * Uploads a file to the specified filemanager and confirms to overwrite an existing file. The paths should be relative to moodle codebase.
  72       *
  73       * @When /^I upload "(?P<filepath_string>(?:[^"]|\\")*)" file to "(?P<filemanager_field_string>(?:[^"]|\\")*)" filemanager as:$/
  74       * @throws ExpectationException Thrown by behat_base::find
  75       * @param string $filepath
  76       * @param string $filemanagerelement
  77       * @param TableNode $data Data to fill in upload form
  78       */
  79      public function i_upload_file_to_filemanager_as($filepath, $filemanagerelement, TableNode $data) {
  80          $this->upload_file_to_filemanager($filepath, $filemanagerelement, $data, false);
  81      }
  82  
  83      /**
  84       * Uploads a file to the specified filemanager. The paths should be relative to moodle codebase.
  85       *
  86       * @When /^I upload and overwrite "(?P<filepath_string>(?:[^"]|\\")*)" file to "(?P<filemanager_field_string>(?:[^"]|\\")*)" filemanager as:$/
  87       * @throws ExpectationException Thrown by behat_base::find
  88       * @param string $filepath
  89       * @param string $filemanagerelement
  90       * @param TableNode $data Data to fill in upload form
  91       */
  92      public function i_upload_and_overwrite_file_to_filemanager_as($filepath, $filemanagerelement, TableNode $data) {
  93          $this->upload_file_to_filemanager($filepath, $filemanagerelement, $data,
  94                  get_string('overwrite', 'repository'));
  95      }
  96  
  97      /**
  98       * Uploads a file to filemanager
  99       *
 100       * @throws ExpectationException Thrown by behat_base::find
 101       * @param string $filepath
 102       * @param string $filemanagerelement
 103       * @param TableNode $data Data to fill in upload form
 104       * @param false|string $overwriteaction false if we don't expect that file with the same name already exists,
 105       *     or button text in overwrite dialogue ("Overwrite", "Rename to ...", "Cancel")
 106       */
 107      protected function upload_file_to_filemanager($filepath, $filemanagerelement, TableNode $data, $overwriteaction = false) {
 108          global $CFG;
 109  
 110          $filemanagernode = $this->get_filepicker_node($filemanagerelement);
 111  
 112          // Opening the select repository window and selecting the upload repository.
 113          $this->open_add_file_window($filemanagernode, get_string('pluginname', 'repository_upload'));
 114  
 115          // Ensure all the form is ready.
 116          $noformexception = new ExpectationException('The upload file form is not ready', $this->getSession());
 117          $this->find(
 118              'xpath',
 119              "//div[contains(concat(' ', normalize-space(@class), ' '), ' file-picker ')]" .
 120                  "[contains(concat(' ', normalize-space(@class), ' '), ' repository_upload ')]" .
 121                  "/descendant::div[@class='fp-content']" .
 122                  "/descendant::div[contains(concat(' ', normalize-space(@class), ' '), ' fp-upload-form ')]" .
 123                  "/descendant::form",
 124              $noformexception
 125          );
 126          // After this we have the elements we want to interact with.
 127  
 128          // Form elements to interact with.
 129          $file = $this->find_file('repo_upload_file');
 130  
 131          // Attaching specified file to the node.
 132          // Replace 'admin/' if it is in start of path with $CFG->admin .
 133          $pos = strpos($filepath, 'admin/');
 134          if ($pos === 0) {
 135              $filepath = $CFG->admin . DIRECTORY_SEPARATOR . substr($filepath, 6);
 136          }
 137          $filepath = str_replace('/', DIRECTORY_SEPARATOR, $filepath);
 138          $fileabsolutepath = $CFG->dirroot . DIRECTORY_SEPARATOR . $filepath;
 139          $file->attachFile($fileabsolutepath);
 140  
 141          // Fill the form in Upload window.
 142          $datahash = $data->getRowsHash();
 143  
 144          // The action depends on the field type.
 145          foreach ($datahash as $locator => $value) {
 146  
 147              $field = behat_field_manager::get_form_field_from_label($locator, $this);
 148  
 149              // Delegates to the field class.
 150              $field->set_value($value);
 151          }
 152  
 153          // Submit the file.
 154          $submit = $this->find_button(get_string('upload', 'repository'));
 155          $submit->press();
 156  
 157          // We wait for all the JS to finish as it is performing an action.
 158          $this->getSession()->wait(self::TIMEOUT, self::PAGE_READY_JS);
 159  
 160          if ($overwriteaction !== false) {
 161              $overwritebutton = $this->find_button($overwriteaction);
 162              $this->ensure_node_is_visible($overwritebutton);
 163              $overwritebutton->click();
 164  
 165              // We wait for all the JS to finish.
 166              $this->getSession()->wait(self::TIMEOUT, self::PAGE_READY_JS);
 167          }
 168  
 169      }
 170  
 171  }


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