Skip to content

Commit 3e23090

Browse files
laurentdavidsrobotta
authored andcommitted
Add file question type
1 parent de7e244 commit 3e23090

File tree

1 file changed

+23
-45
lines changed

1 file changed

+23
-45
lines changed

tests/behat/behat_mod_questionnaire.php

Lines changed: 23 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -471,45 +471,57 @@ protected function get_cm_by_questionnaire_name(string $name): stdClass {
471471
*
472472
* The paths should be relative to moodle codebase.
473473
*
474-
* @When /^I upload "(?P<filepath_string>(?:[^"]|\\")*)" to questionnaire filemanager$/
474+
* @When /^I upload "(?P<filepath_string>(?:[^"]|\\")*)" to questionnaire "(?P<question_string>(?:[^"]|\\")*)" filemanager$/
475475
* @param string $filepath
476+
* @param string $question
476477
*/
477-
public function i_upload_file_to_questionnaire_filemanager($filepath) {
478-
$this->upload_file_to_filemanager_questionnaire($filepath, new TableNode(array()));
478+
public function i_upload_file_to_questionnaire_question_filemanager($filepath, $question) {
479+
$this->upload_file_to_question_filemanager_questionnaire($filepath, $question, new TableNode([]), false);
479480
}
480481

481482
/**
482-
* Try to get the filemanager node.
483+
* Try to get the filemanager node of a given question.
483484
*
484-
* @return NodeElement
485+
* @param $question
486+
* @return \Behat\Mink\Element\NodeElement|null
485487
*/
486-
protected function get_filemanager() {
488+
protected function get_filepicker_node($question) {
489+
// More info about the problem (in case there is a problem).
490+
$exception = new ExpectationException('The filepicker for the question with text "' . $question .
491+
'" can not be found', $this->getSession());
487492

488-
// If no file picker label is mentioned take the first file picker from the page.
489-
return $this->find(
493+
$filepickercontainer = $this->find(
490494
'xpath',
491-
'//div[contains(concat(" ", normalize-space(@class), " "), " filemanager ")]'
495+
"//p[contains(.,'" . $question . "')]" .
496+
"//parent::div[contains(concat(' ', normalize-space(@class), ' '), ' no-overflow ')]" .
497+
"//parent::div[contains(concat(' ', normalize-space(@class), ' '), ' qn-question ')]" .
498+
"//following::div[contains(concat(' ', normalize-space(@class), ' '), ' qn-answer ')]" .
499+
"//descendant::*[@data-fieldtype = 'filemanager' or @data-fieldtype = 'filepicker']",
500+
$exception
492501
);
502+
503+
return $filepickercontainer;
493504
}
494505

495506
/**
496507
* Uploads a file to filemanager
497508
*
498509
* @param string $filepath Normally a path relative to $CFG->dirroot, but can be an absolute path too.
510+
* @param string $question A question text.
499511
* @param TableNode $data Data to fill in upload form
500512
* @param false|string $overwriteaction false if we don't expect that file with the same name already exists,
501513
* or button text in overwrite dialogue ("Overwrite", "Rename to ...", "Cancel")
502514
* @throws DriverException
503515
* @throws ExpectationException Thrown by behat_base::find
504516
*/
505-
protected function upload_file_to_filemanager_questionnaire($filepath, TableNode $data, $overwriteaction = false) {
517+
protected function upload_file_to_question_filemanager_questionnaire($filepath, $question, TableNode $data, $overwriteaction = false) {
506518
global $CFG;
507519

508520
if (!$this->has_tag('_file_upload')) {
509521
throw new DriverException('File upload tests must have the @_file_upload tag on either the scenario or feature.');
510522
}
511523

512-
$filemanagernode = $this->get_filemanager();
524+
$filemanagernode = $this->get_filepicker_node($question);
513525

514526
// Opening the select repository window and selecting the upload repository.
515527
$this->open_add_file_window($filemanagernode, get_string('pluginname', 'repository_upload'));
@@ -576,40 +588,6 @@ protected function upload_file_to_filemanager_questionnaire($filepath, TableNode
576588

577589
}
578590

579-
/**
580-
* Try to get the filemanager node specified by the element
581-
*
582-
* @param string $filepickerelement
583-
* @return NodeElement
584-
* @throws ExpectationException
585-
*/
586-
protected function get_filepicker_node($filepickerelement) {
587-
588-
// More info about the problem (in case there is a problem).
589-
$exception = new ExpectationException('"' . $filepickerelement . '" filepicker can not be found', $this->getSession());
590-
591-
// If no file picker label is mentioned take the first file picker from the page.
592-
if (empty($filepickerelement)) {
593-
$filepickercontainer = $this->find(
594-
'xpath',
595-
"//*[@class=\"form-filemanager\"]",
596-
$exception
597-
);
598-
} else {
599-
// Gets the filemanager node specified by the locator which contains the filepicker container
600-
// either for filepickers created by mform or by admin config.
601-
$filepickerelement = behat_context_helper::escape($filepickerelement);
602-
$filepickercontainer = $this->find(
603-
'xpath',
604-
"//input[./@id = substring-before(//p[normalize-space(.)=$filepickerelement]/@id, '_label')]" .
605-
"//ancestor::*[@data-fieldtype = 'filemanager' or @data-fieldtype = 'filepicker']",
606-
$exception
607-
);
608-
}
609-
610-
return $filepickercontainer;
611-
}
612-
613591
/**
614592
* Opens the filepicker modal window and selects the repository.
615593
*

0 commit comments

Comments
 (0)