moodle/repository/upload/tests/behat/behat_repository_upload.php
David Monllao c1faf86bd1 MDL-42625 behat: Make behat pacient
* When looking for texts inside the page or inside
  other containers we should wait until the elements
  are visible.
* Same when expanding tree nodes.
* Normalizing loops to spin() function using
  behat_base::TIMEOUT and behat_base::EXTENDED_TIMEOUT,
  leaving TIMEOUT for DOM load processes and
  EXTENDED_TIMEOUT for long processes that involves JS
  too.
* Add page load waits between actions that involves
  reloading the page.
2013-12-09 11:08:42 +08:00

174 lines
7.5 KiB
PHP

<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Steps definitions for the upload repository type.
*
* @package repository_upload
* @category test
* @copyright 2013 David Monllaó
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
// NOTE: no MOODLE_INTERNAL test here, this file may be required by behat before including /config.php.
require_once(__DIR__ . '/../../../../lib/behat/behat_files.php');
use Behat\Mink\Exception\ExpectationException as ExpectationException,
Behat\Gherkin\Node\TableNode as TableNode;
/**
* Steps definitions to deal with the upload repository.
*
* Extends behat_files rather than behat_base as is file-related.
*
* @package repository_upload
* @category test
* @copyright 2013 David Monllaó
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class behat_repository_upload extends behat_files {
/**
* Uploads a file to the specified filemanager leaving other fields in upload form default. The paths should be relative to moodle codebase.
*
* @When /^I upload "(?P<filepath_string>(?:[^"]|\\")*)" file to "(?P<filemanager_field_string>(?:[^"]|\\")*)" filemanager$/
* @throws ExpectationException Thrown by behat_base::find
* @param string $filepath
* @param string $filemanagerelement
*/
public function i_upload_file_to_filemanager($filepath, $filemanagerelement) {
$this->upload_file_to_filemanager($filepath, $filemanagerelement, new TableNode(), false);
}
/**
* 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.
*
* @When /^I upload and overwrite "(?P<filepath_string>(?:[^"]|\\")*)" file to "(?P<filemanager_field_string>(?:[^"]|\\")*)" filemanager$/
* @throws ExpectationException Thrown by behat_base::find
* @param string $filepath
* @param string $filemanagerelement
*/
public function i_upload_and_overwrite_file_to_filemanager($filepath, $filemanagerelement) {
$this->upload_file_to_filemanager($filepath, $filemanagerelement, new TableNode(),
get_string('overwrite', 'repository'));
}
/**
* Uploads a file to the specified filemanager and confirms to overwrite an existing file. The paths should be relative to moodle codebase.
*
* @When /^I upload "(?P<filepath_string>(?:[^"]|\\")*)" file to "(?P<filemanager_field_string>(?:[^"]|\\")*)" filemanager as:$/
* @throws ExpectationException Thrown by behat_base::find
* @param string $filepath
* @param string $filemanagerelement
* @param TableNode $data Data to fill in upload form
*/
public function i_upload_file_to_filemanager_as($filepath, $filemanagerelement, TableNode $data) {
$this->upload_file_to_filemanager($filepath, $filemanagerelement, $data, false);
}
/**
* Uploads a file to the specified filemanager. The paths should be relative to moodle codebase.
*
* @When /^I upload and overwrite "(?P<filepath_string>(?:[^"]|\\")*)" file to "(?P<filemanager_field_string>(?:[^"]|\\")*)" filemanager as:$/
* @throws ExpectationException Thrown by behat_base::find
* @param string $filepath
* @param string $filemanagerelement
* @param TableNode $data Data to fill in upload form
*/
public function i_upload_and_overwrite_file_to_filemanager_as($filepath, $filemanagerelement, TableNode $data) {
$this->upload_file_to_filemanager($filepath, $filemanagerelement, $data,
get_string('overwrite', 'repository'));
}
/**
* Uploads a file to filemanager
*
* @throws ExpectationException Thrown by behat_base::find
* @param string $filepath
* @param string $filemanagerelement
* @param TableNode $data Data to fill in upload form
* @param false|string $overwriteaction false if we don't expect that file with the same name already exists,
* or button text in overwrite dialogue ("Overwrite", "Rename to ...", "Cancel")
*/
protected function upload_file_to_filemanager($filepath, $filemanagerelement, TableNode $data, $overwriteaction = false) {
global $CFG;
$filemanagernode = $this->get_filepicker_node($filemanagerelement);
// Opening the select repository window and selecting the upload repository.
$this->open_add_file_window($filemanagernode, get_string('pluginname', 'repository_upload'));
// Ensure all the form is ready.
$noformexception = new ExpectationException('The upload file form is not ready', $this->getSession());
$this->find(
'xpath',
"//div[contains(concat(' ', normalize-space(@class), ' '), ' file-picker ')]" .
"[contains(concat(' ', normalize-space(@class), ' '), ' repository_upload ')]" .
"/descendant::div[@class='fp-content']" .
"/descendant::div[contains(concat(' ', normalize-space(@class), ' '), ' fp-upload-form ')]" .
"/descendant::form",
$noformexception
);
// After this we have the elements we want to interact with.
// Form elements to interact with.
$file = $this->find_file('repo_upload_file');
// Attaching specified file to the node.
// Replace 'admin/' if it is in start of path with $CFG->admin .
$pos = strpos($filepath, 'admin/');
if ($pos === 0) {
$filepath = $CFG->admin . DIRECTORY_SEPARATOR . substr($filepath, 6);
}
$filepath = str_replace('/', DIRECTORY_SEPARATOR, $filepath);
$fileabsolutepath = $CFG->dirroot . DIRECTORY_SEPARATOR . $filepath;
$file->attachFile($fileabsolutepath);
// Fill the form in Upload window.
$datahash = $data->getRowsHash();
// The action depends on the field type.
foreach ($datahash as $locator => $value) {
// Getting the node element pointed by the label.
$fieldnode = $this->find_field($locator);
// Gets the field type from a parent node.
$field = behat_field_manager::get_form_field($fieldnode, $this->getSession());
// Delegates to the field class.
$field->set_value($value);
}
// Submit the file.
$submit = $this->find_button(get_string('upload', 'repository'));
$submit->press();
// We wait for all the JS to finish as it is performing an action.
$this->getSession()->wait(self::TIMEOUT, self::PAGE_READY_JS);
if ($overwriteaction !== false) {
$overwritebutton = $this->find_button($overwriteaction);
$this->ensure_node_is_visible($overwritebutton);
$overwritebutton->click();
// We wait for all the JS to finish.
$this->getSession()->wait(self::TIMEOUT, self::PAGE_READY_JS);
}
}
}