diff --git a/tests/behat/behat_mod_questionnaire.php b/tests/behat/behat_mod_questionnaire.php index c00ced7e..e8d83ff9 100644 --- a/tests/behat/behat_mod_questionnaire.php +++ b/tests/behat/behat_mod_questionnaire.php @@ -471,45 +471,53 @@ protected function get_cm_by_questionnaire_name(string $name): stdClass { * * The paths should be relative to moodle codebase. * - * @When /^I upload "(?P(?:[^"]|\\")*)" to questionnaire filemanager$/ + * @When /^I upload "(?P(?:[^"]|\\")*)" to questionnaire "(?P(?:[^"]|\\")*)" 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([])); } /** - * 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_filemanager($question) { + return $this->find( + 'xpath', + "//p[contains(.,'" . $question . "')]//parent::div[@class='no-overflow']//parent::div[@class='qn-question']//following::div[@class='qn-answer']//div[contains(concat(' ', normalize-space(@class), ' '), ' filemanager ')]" + ); +/* // If no file picker label is mentioned take the first file picker from the page. return $this->find( 'xpath', '//div[contains(concat(" ", normalize-space(@class), " "), " filemanager ")]' ); +*/ } /** * 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_filemanager($question); // Opening the select repository window and selecting the upload repository. $this->open_add_file_window($filemanagernode, get_string('pluginname', 'repository_upload')); @@ -576,40 +584,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. * diff --git a/tests/behat/file_question.feature b/tests/behat/file_question.feature index 0ff43934..855d8350 100644 --- a/tests/behat/file_question.feature +++ b/tests/behat/file_question.feature @@ -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" @@ -42,3 +46,35 @@ 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 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" diff --git a/tests/fixtures/testfilequestion2.pdf b/tests/fixtures/testfilequestion2.pdf new file mode 100644 index 00000000..90588c30 Binary files /dev/null and b/tests/fixtures/testfilequestion2.pdf differ