Skip to content

Commit

Permalink
File question type Behat tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
lucaboesch committed Jan 22, 2024
1 parent 1b49466 commit a790831
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 59 deletions.
67 changes: 22 additions & 45 deletions tests/behat/behat_mod_questionnaire.php
Original file line number Diff line number Diff line change
Expand Up @@ -471,45 +471,56 @@ protected function get_cm_by_questionnaire_name(string $name): stdClass {
*
* The paths should be relative to moodle codebase.
*
* @When /^I upload "(?P<filepath_string>(?:[^"]|\\")*)" to questionnaire filemanager$/
* @When /^I upload "(?P<filepath_string>(?:[^"]|\\")*)" to questionnaire "(?P<question_string>(?:[^"]|\\")*)" filemanager$/
* @param string $filepath
* @param string $question
*/
public function i_upload_file_to_questionnaire_filemanager($filepath) {
$this->upload_file_to_filemanager_questionnaire($filepath, new TableNode(array()));
public function i_upload_file_to_questionnaire_question_filemanager($filepath, $question) {
$this->upload_file_to_question_filemanager_questionnaire($filepath, $question, new TableNode([]), false);
}

/**
* Try to get the filemanager node.
* Try to get the filemanager node of a given question.
*
* @return NodeElement
* @param $question
* @return \Behat\Mink\Element\NodeElement|null
*/
protected function get_filemanager() {
protected function get_filepicker_node($question) {
// More info about the problem (in case there is a problem).
$exception = new ExpectationException('"' . $question . '" filepicker can not be found', $this->getSession());

// If no file picker label is mentioned take the first file picker from the page.
return $this->find(
$filepickercontainer = $this->find(
'xpath',
'//div[contains(concat(" ", normalize-space(@class), " "), " filemanager ")]'
"//p[contains(.,'" . $question . "')]" .
"//parent::div[contains(concat(' ', normalize-space(@class), ' '), ' no-overflow ')]" .
"//parent::div[contains(concat(' ', normalize-space(@class), ' '), ' qn-question ')]" .
"//following::div[contains(concat(' ', normalize-space(@class), ' '), ' qn-answer ')]" .
"//descendant::*[contains(concat(' ', normalize-space(@class), ' '), ' filemanager ')]",
$exception
);

return $filepickercontainer;
}

/**
* Uploads a file to filemanager
*
* @param string $filepath Normally a path relative to $CFG->dirroot, but can be an absolute path too.
* @param string $question A question text.
* @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")
* @throws DriverException
* @throws ExpectationException Thrown by behat_base::find
*/
protected function upload_file_to_filemanager_questionnaire($filepath, TableNode $data, $overwriteaction = false) {
protected function upload_file_to_question_filemanager_questionnaire($filepath, $question, TableNode $data, $overwriteaction = false) {
global $CFG;

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

$filemanagernode = $this->get_filemanager();
$filemanagernode = $this->get_filepicker_node($question);

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

}

/**
* Try to get the filemanager node specified by the element
*
* @param string $filepickerelement
* @return NodeElement
* @throws ExpectationException
*/
protected function get_filepicker_node($filepickerelement) {

// More info about the problem (in case there is a problem).
$exception = new ExpectationException('"' . $filepickerelement . '" filepicker can not be found', $this->getSession());

// If no file picker label is mentioned take the first file picker from the page.
if (empty($filepickerelement)) {
$filepickercontainer = $this->find(
'xpath',
"//*[@class=\"form-filemanager\"]",
$exception
);
} else {
// Gets the filemanager node specified by the locator which contains the filepicker container
// either for filepickers created by mform or by admin config.
$filepickerelement = behat_context_helper::escape($filepickerelement);
$filepickercontainer = $this->find(
'xpath',
"//input[./@id = substring-before(//p[normalize-space(.)=$filepickerelement]/@id, '_label')]" .
"//ancestor::*[@data-fieldtype = 'filemanager' or @data-fieldtype = 'filepicker']",
$exception
);
}

return $filepickercontainer;
}

/**
* Opens the filepicker modal window and selects the repository.
*
Expand Down
65 changes: 51 additions & 14 deletions tests/behat/file_question.feature
Original file line number Diff line number Diff line change
@@ -1,37 +1,41 @@
@mod @mod_questionnaire
Feature: In questionnaire, we can add a question requiring a file upload.
Feature: Add a question requiring a file upload in questionnaire.
In order to use this plugin
As a teacher
I need to add a a file question to a questionnaire created in my course
and a student answers to it. Then the file has to be accessible.

Background:
Given the following "users" exist:
| username | firstname | lastname | email |
| teacher1 | Teacher | 1 | teacher1@example.com |
| student1 | Student | 1 | student1@example.com |
| username | firstname | lastname | email |
| teacher1 | Teacher | 1 | teacher1@example.com |
| student1 | Student | 1 | student1@example.com |
And the following "courses" exist:
| fullname | shortname | category |
| Course 1 | C1 | 0 |
| Course 1 | C1 | 0 |
And the following "course enrolments" exist:
| user | course | role |
| teacher1 | C1 | editingteacher |
| student1 | C1 | student |
| user | course | role |
| teacher1 | C1 | editingteacher |
| student1 | C1 | student |
And the following "activities" exist:
| activity | name | description | course | idnumber | resume | navigate |
| questionnaire | Test questionnaire | Test questionnaire description | C1 | questionnaire0 | 1 | 1 |
| activity | name | description | course | idnumber | resume | navigate |
| questionnaire | Test questionnaire | Test questionnaire description | C1 | questionnaire0 | 1 | 1 |

@javascript @_file_upload
Scenario: As a teacher, I create a questionnaire in my course with a file question and a student answers to it. Then the file has to be accessible.
Scenario: Add a single file question to a questionnaire and view an answer with an uploaded file.
Given I log in as "teacher1"
When I am on the "Test questionnaire" "questionnaire activity" page
And I navigate to "Questions" in current page administration
And I should see "Add questions"
And I add a "File" question and I fill the form with:
| Question Name | File question |
| Yes | Yes |
| Question Name | File question |
| Yes | Yes |
| Question Text | Add a file as an answer |
And I log out
And I log in as "student1"
And I am on the "Test questionnaire" "questionnaire activity" page
And I navigate to "Answer the questions..." in current page administration
And I upload "mod/questionnaire/tests/fixtures/testfilequestion.pdf" to questionnaire filemanager
And I upload "mod/questionnaire/tests/fixtures/testfilequestion.pdf" to questionnaire "Add a file as an answer" filemanager
And I press "Submit questionnaire"
And I should see "Thank you for completing this Questionnaire"
And I press "Continue"
Expand All @@ -42,3 +46,36 @@ Feature: In questionnaire, we can add a question requiring a file upload.
And I am on the "Test questionnaire" "questionnaire activity" page
And I navigate to "View all responses" in current page administration
Then I should see "testfilequestion.pdf"

@javascript @_file_upload
Scenario: Add two file questions to a questionnaire and view an answer with two uploaded file.
Given I log in as "teacher1"
When I am on the "Test questionnaire" "questionnaire activity" page
And I navigate to "Questions" in current page administration
And I should see "Add questions"
And I add a "File" question and I fill the form with:
| Question Name | File question one |
| Yes | Yes |
| Question Text | Add a first file as an answer |
And I add a "File" question and I fill the form with:
| Question Name | File question two |
| Yes | Yes |
| Question Text | Add a second file as an answer |
And I log out
And I log in as "student1"
And I am on the "Test questionnaire" "questionnaire activity" page
And I navigate to "Answer the questions..." in current page administration
And I upload "mod/questionnaire/tests/fixtures/testfilequestion.pdf" to questionnaire "Add a first file as an answer" filemanager
And I wait until the page is ready
And I upload "mod/questionnaire/tests/fixtures/testfilequestion2.pdf" to questionnaire "Add a second file as an answer" filemanager
And I press "Submit questionnaire"
And I should see "Thank you for completing this Questionnaire"
And I press "Continue"
And I should see "View your response(s)"
And ".resourcecontent.resourcepdf" "css_element" should exist
And I log out
And I log in as "teacher1"
And I am on the "Test questionnaire" "questionnaire activity" page
And I navigate to "View all responses" in current page administration
Then I should see "testfilequestion.pdf"
And I should see "testfilequestion2.pdf"
Binary file added tests/fixtures/testfilequestion2.pdf
Binary file not shown.

0 comments on commit a790831

Please sign in to comment.