Skip to content

Commit

Permalink
MDL-76499 behat: Resource Behat tests optimised
Browse files Browse the repository at this point in the history
Replace steps that manually add resource instances via the UI and use
Behat generators. This improves the speed of the Behat test runs.

Co-authored-by: David Woloszyn <david.woloszyn@moodle.com>
  • Loading branch information
andelacruz and davewoloszyn committed Feb 6, 2023
1 parent cdcfb84 commit 31f13cd
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 58 deletions.
5 changes: 2 additions & 3 deletions files/tests/behat/add_custom_file_type.feature
Expand Up @@ -26,9 +26,8 @@ Feature: Add a new custom file type
| Custom description | Moodle rules |
And I press "Save changes"
And I should see "application/x-moodle-rules"
And I log out
And I log in as "teacher1"
And I am on "Course 1" course homepage with editing mode on
And I am on the "Course 1" course page logged in as teacher1
And I turn editing mode on
When I add a "File" to section "1" and I fill the form with:
| Name | Test file |
| Select files | files/tests/fixtures/custom_filetype.mdlr |
Expand Down
21 changes: 9 additions & 12 deletions mod/resource/tests/behat/display_resource.feature
Expand Up @@ -16,34 +16,32 @@ Feature: Teacher can specify different display options for the resource
| user | course | role |
| teacher1 | C1 | editingteacher |
| student1 | C1 | student |
And the following "activities" exist:
| activity | course | name | defaultfilename | uploaded |
| resource | C1 | Myfile | mod/resource/tests/fixtures/samplefile.txt | 1 |
And I log in as "teacher1"
And I am on "Course 1" course homepage with editing mode on

@javascript
Scenario: Specifying no additional display options for a file resource
When I add a "File" to section "1"
When I am on the "Myfile" "resource activity editing" page
And I set the following fields to these values:
| Name | Myfile |
| Show size | 0 |
| Show type | 0 |
| Show upload/modified date | 0 |
And I upload "mod/resource/tests/fixtures/samplefile.txt" file to "Select files" filemanager
| Show upload/modified date | 0 |
| showsize | 0 |
| showtype | 0 |
And I press "Save and display"
Then ".resourcedetails" "css_element" should not exist
And I am on "Course 1" course homepage
And ".activity.resource .resourcelinkdetails" "css_element" should not exist
And I log out

@javascript
Scenario Outline: Specifying different display options for a file resource
When I add a "File" to section "1"
When I am on the "Myfile" "resource activity editing" page
And I set the following fields to these values:
| Name | Myfile |
| Display | Open |
| display | 5 |
| Show size | <showsize> |
| Show type | <showtype> |
| Show upload/modified date | <showdate> |
And I upload "mod/resource/tests/fixtures/samplefile.txt" file to "Select files" filemanager
And I press "Save and display"
Then I <seesize> see "6 bytes" in the ".resourcedetails" "css_element"
And I <seetype> see "Text file" in the ".resourcedetails" "css_element"
Expand All @@ -52,7 +50,6 @@ Feature: Teacher can specify different display options for the resource
And I <seesize> see "6 bytes" in the ".activity.resource .resourcelinkdetails" "css_element"
And I <seetype> see "Text file" in the ".activity.resource .resourcelinkdetails" "css_element"
And I <seedate> see "Uploaded" in the ".activity.resource .resourcelinkdetails" "css_element"
And I log out

Examples:
| showsize | showtype | showdate | seesize | seetype | seedate |
Expand Down
64 changes: 23 additions & 41 deletions mod/resource/tests/behat/resource_activity_completion.feature
Expand Up @@ -18,8 +18,7 @@ Feature: View activity completion information for file resources
| teacher1 | C1 | editingteacher |
And the following config values are set as admin:
| displayoptions | 0,1,2,3,4,5,6 | resource |
And I log in as "teacher1"
And I am on "Course 1" course homepage
And I am on the "Course 1" course page logged in as teacher1
And I navigate to "Edit settings" in current page administration
And I expand all fieldsets
And I set the following fields to these values:
Expand All @@ -29,61 +28,49 @@ Feature: View activity completion information for file resources

@javascript
Scenario Outline: The manual completion button will be shown on the course page for Open, In pop-up, New window and Force download display mode if Show activity completion conditions is set to No
Given I am on "Course 1" course homepage with editing mode on
And I add a "File" to section "1"
And I set the following fields to these values:
| Name | Myfile |
| id_display | <display> |
| Show size | 0 |
| Show type | 0 |
| Show upload/modified date | 0 |
| Completion tracking | Students can manually mark the activity as completed |
And I upload "mod/resource/tests/fixtures/samplefile.txt" file to "Select files" filemanager
And I press "Save and return to course"
Given the following "activities" exist:
| activity | course | name | display | showsize | showtype | showdate | completion | defaultfilename | popupwidth | popupheight | uploaded |
| resource | C1 | Myfile | <display> | 0 | 0 | 0 | 1 | mod/resource/tests/fixtures/samplefile.txt | 620 | 450 | 1 |
And I am on "Course 1" course homepage with editing mode on
# Teacher view.
And the manual completion button for "Myfile" should exist
And the manual completion button for "Myfile" should be disabled
And I log out
# Student view.
When I log in as "student1"
And I am on "Course 1" course homepage
When I am on the "Course 1" course page logged in as student1
Then the manual completion button for "Myfile" should exist
And the manual completion button of "Myfile" is displayed as "Mark as done"
And I toggle the manual completion state of "Myfile"
And the manual completion button of "Myfile" is displayed as "Done"

Examples:
| display |
| Open |
| In pop-up |
| Force download |
| New window |
| display |
| 5 |
| 6 |
| 4 |
| 3 |

@javascript
Scenario: The manual completion button will be shown on the activity page and course page if Show activity completion conditions is set to Yes
Given I am on "Course 1" course homepage
Given the following "activities" exist:
| activity | course | name | display | defaultfilename | uploaded |
| resource | C1 | Myfile | 1 | mod/resource/tests/fixtures/samplefile.txt | 1 |
And I am on "Course 1" course homepage with editing mode on
And I navigate to "Edit settings" in current page administration
And I expand all fieldsets
And I set the field "Show activity completion conditions" to "Yes"
And I press "Save and display"
And I am on "Course 1" course homepage with editing mode on
And I add a "File" to section "1"
And I am on the "Myfile" "resource activity editing" page
And I set the following fields to these values:
| Name | Myfile |
| id_display | Embed |
| Completion tracking | Students can manually mark the activity as completed |
And I upload "mod/resource/tests/fixtures/samplefile.txt" file to "Select files" filemanager
And I click on "Save and return to course" "button"
# Teacher view.
And the manual completion button for "Myfile" should exist
And the manual completion button for "Myfile" should be disabled
And I am on the "Myfile" "resource activity" page
And the manual completion button for "Myfile" should exist
And the manual completion button for "Myfile" should be disabled
And I log out
# Student view.
When I log in as "student1"
And I am on "Course 1" course homepage
When I am on the "Course 1" course page logged in as student1
Then the manual completion button for "Myfile" should exist
And I am on the "Myfile" "resource activity" page
And the manual completion button of "Myfile" is displayed as "Mark as done"
Expand All @@ -92,26 +79,21 @@ Feature: View activity completion information for file resources

@javascript
Scenario: View automatic completion items
Given I navigate to "Edit settings" in current page administration
Given the following "activities" exist:
| activity | course | name | display | defaultfilename | uploaded |
| resource | C1 | Myfile | 1 | mod/resource/tests/fixtures/samplefile.txt | 1 |
And I navigate to "Edit settings" in current page administration
And I expand all fieldsets
And I set the field "Show activity completion conditions" to "Yes"
And I press "Save and display"
And I am on "Course 1" course homepage with editing mode on
And I add a "File" to section "1"
And I am on the "Myfile" "resource activity editing" page
And I set the following fields to these values:
| Name | Myfile |
| id_display | Embed |
| Completion tracking | Show activity as complete when conditions are met |
| Require view | 1 |
And I upload "mod/resource/tests/fixtures/samplefile.txt" file to "Select files" filemanager
And I press "Save and display"
And I am on "Course 1" course homepage
# Teacher view.
And I am on the "Myfile" "resource activity" page
And "Myfile" should have the "View" completion condition
And I log out
# Student view.
When I log in as "student1"
And I am on "Course 1" course homepage
And I am on the "Myfile" "resource activity" page
When I am on the "Myfile" "resource activity" page logged in as student1
Then the "View" completion condition of "Myfile" is displayed as "done"
15 changes: 13 additions & 2 deletions mod/resource/tests/generator/lib.php
Expand Up @@ -65,6 +65,9 @@ public function create_instance($record = null, array $options = null) {
if (!isset($record->showtype)) {
$record->showtype = 0;
}
if (!isset($record->uploaded)) {
$record->uploaded = 0;
}

// The 'files' value corresponds to the draft file area ID. If not
// specified, create a default file.
Expand All @@ -74,16 +77,24 @@ public function create_instance($record = null, array $options = null) {
}
$usercontext = context_user::instance($USER->id);
$filename = $record->defaultfilename ?? 'resource' . ($this->instancecount + 1) . '.txt';
// Set filepath depending on filename.
$filepath = (isset($record->defaultfilename)) ? $CFG->dirroot . '/' : '/';

// Pick a random context id for specified user.
$record->files = file_get_unused_draft_itemid();

// Add actual file there.
$filerecord = ['component' => 'user', 'filearea' => 'draft',
'contextid' => $usercontext->id, 'itemid' => $record->files,
'filename' => $filename, 'filepath' => '/'];
'filename' => basename($filename), 'filepath' => $filepath];
$fs = get_file_storage();
$fs->create_file_from_string($filerecord, 'Test resource ' . $filename . ' file');
if ($record->uploaded == 1) {
// Create file using pathname (defaultfilename) set.
$fs->create_file_from_pathname($filerecord, $filepath . $filename);
} else {
// If defaultfilename is not set, create file from string "resource 1.txt".
$fs->create_file_from_string($filerecord, 'Test resource ' . $filename . ' file');
}
}

// Do work to actually add the instance.
Expand Down

0 comments on commit 31f13cd

Please sign in to comment.