. /** * 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(?:[^"]|\\")*)" file to "(?P(?:[^"]|\\")*)" 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(?:[^"]|\\")*)" file to "(?P(?:[^"]|\\")*)" 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(?:[^"]|\\")*)" file to "(?P(?:[^"]|\\")*)" 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(?:[^"]|\\")*)" file to "(?P(?:[^"]|\\")*)" 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) { $field = behat_field_manager::get_form_field_from_label($locator, $this); // 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); } } }