From fb3bbeedf8de2581aa35bc9f8d678514ae234da4 Mon Sep 17 00:00:00 2001 From: Dan Marsden Date: Mon, 13 Sep 2021 12:01:45 +1200 Subject: [PATCH] Fix behat tests. --- tests/behat/attendance_mod.feature | 35 +++++------ tests/behat/behat_mod_attendance.php | 84 +++++++++++++++++++++++++++ tests/behat/calendar_features.feature | 26 ++++----- tests/behat/defaultstatus.feature | 3 +- tests/behat/extra_features.feature | 60 ++++++++++--------- tests/behat/preferences.feature | 23 ++++---- tests/behat/report.feature | 40 +++++-------- 7 files changed, 167 insertions(+), 104 deletions(-) create mode 100644 tests/behat/behat_mod_attendance.php diff --git a/tests/behat/attendance_mod.feature b/tests/behat/attendance_mod.feature index 665ffce..834eddb 100644 --- a/tests/behat/attendance_mod.feature +++ b/tests/behat/attendance_mod.feature @@ -1,4 +1,4 @@ -@javascript @mod @uon @mod_attendance +@mod @mod_attendance Feature: Teachers and Students can record session attendance In order to record session attendance As a student @@ -37,10 +37,9 @@ Feature: Teachers and Students can record session attendance And I should see "Attendance" And I log out + @javascript Scenario: Students can mark their own attendance and teacher can hide specific status from students. - Given I log in as "teacher1" - And I am on "Course 1" course homepage - And I follow "Attendance" + Given I am on the "Attendance" "mod_attendance > View" page logged in as "teacher1" And I follow "Add" And I set the field "Allow students to record own attendance" to "1" And I set the following fields to these values: @@ -49,22 +48,16 @@ Feature: Teachers and Students can record session attendance | id_sestime_endminute | 55 | And I click on "id_submitbutton" "button" And I log out - And I log in as "student1" - And I am on "Course 1" course homepage - And I follow "Attendance" + And I am on the "Attendance" "mod_attendance > View" page logged in as "student1" And I follow "Submit attendance" And I should see "Excused" And I log out - And I log in as "teacher1" - And I am on "Course 1" course homepage - And I follow "Attendance" + And I am on the "Attendance" "mod_attendance > View" page logged in as "teacher1" And I follow "Status set" And I set the field with xpath "//*[@id='preferencesform']/table/tbody/tr[3]/td[5]/input" to "0" And I press "Update" And I log out - And I log in as "student1" - And I am on "Course 1" course homepage - And I follow "Attendance" + And I am on the "Attendance" "mod_attendance > View" page logged in as "student1" And I follow "Submit attendance" And I should not see "Excused" And I set the field "Present" to "1" @@ -78,10 +71,9 @@ Feature: Teachers and Students can record session attendance And I click on "Get these logs" "button" Then "Attendance taken by student" "link" should exist + @javascript Scenario: Teachers can view below % report and send a message - Given I log in as "teacher1" - And I am on "Course 1" course homepage - And I follow "Attendance" + Given I am on the "Attendance" "mod_attendance > View" page logged in as "teacher1" And I follow "Add" And I set the following fields to these values: | id_sestime_starthour | 01 | @@ -99,6 +91,7 @@ Feature: Teachers and Students can record session attendance And I click on "Get these logs" "button" Then "Attendance report viewed" "link" should exist + @javascript Scenario: Export report includes id number, department and institution Given I log in as "admin" And I navigate to "Users > Permissions > User policies" in site administration @@ -106,9 +99,7 @@ Feature: Teachers and Students can record session attendance | showuseridentity | idnumber,email,phone1,phone2,department,institution | And I log out - And I log in as "teacher1" - And I am on "Course 1" course homepage - And I follow "Attendance" + And I am on the "Attendance" "mod_attendance > View" page logged in as "teacher1" And I follow "Add" And I set the following fields to these values: | id_sestime_starthour | 01 | @@ -119,6 +110,7 @@ Feature: Teachers and Students can record session attendance And the field "id_ident_institution" matches value "1" And the field "id_ident_department" matches value "1" + @javascript Scenario: Test enabling custom user profile field # Add custom field. Given I log in as "admin" @@ -133,6 +125,7 @@ Feature: Teachers and Students can record session attendance And I navigate to "Plugins > Activity modules > Attendance" in site administration And the "Export custom user profile fields" select box should contain "Super field" + @javascript Scenario: Test adding custom user profile # Add custom field. Given I log in as "admin" @@ -148,9 +141,7 @@ Feature: Teachers and Students can record session attendance | customexportfields | superfield | attendance | And I log out - And I log in as "teacher1" - And I am on "Course 1" course homepage - And I follow "Attendance" + And I am on the "Attendance" "mod_attendance > View" page logged in as "teacher1" And I follow "Add" And I set the following fields to these values: | id_sestime_starthour | 01 | diff --git a/tests/behat/behat_mod_attendance.php b/tests/behat/behat_mod_attendance.php new file mode 100644 index 0000000..e801caf --- /dev/null +++ b/tests/behat/behat_mod_attendance.php @@ -0,0 +1,84 @@ +. + +/** + * Steps definitions related to mod_attendance + * + * @package mod_attendance + * @copyright 2021 Dan Marsden + * @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_base.php'); + +use Behat\Gherkin\Node\TableNode as TableNode; + +use Behat\Mink\Exception\ExpectationException as ExpectationException; + +/** + * Steps definitions related to mod_attendance. + * + * @copyright 2021 Dan Marsden + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class behat_mod_attendance extends behat_question_base { + /** + * Convert page names to URLs for steps like 'When I am on the "[identifier]" "[page type]" page'. + * + * Recognised page names are: + * | pagetype | name meaning | description | + * | View | attendance name | The attendance info page (view.php) | + * + * @param string $type identifies which type of page this is, e.g. 'Attempt review'. + * @param string $identifier identifies the particular page, e.g. 'Test attendance > student > Attempt 1'. + * @return moodle_url the corresponding URL. + * @throws Exception with a meaningful error message if the specified page cannot be found. + */ + protected function resolve_page_instance_url(string $type, string $identifier): moodle_url { + switch (strtolower($type)) { + case 'view': + return new moodle_url('/mod/attendance/view.php', + ['id' => $this->get_cm_by_attendance_name($identifier)->id]); + + default: + throw new Exception('Unrecognised attendance page type "' . $type . '."'); + } + } + + /** + * Get a attendance by name. + * + * @param string $name attendance name. + * @return stdClass the corresponding DB row. + */ + protected function get_attendance_by_name(string $name): stdClass { + global $DB; + return $DB->get_record('attendance', array('name' => $name), '*', MUST_EXIST); + } + + /** + * Get a quiz cmid from the quiz name. + * + * @param string $name quiz name. + * @return stdClass cm from get_coursemodule_from_instance. + */ + protected function get_cm_by_attendance_name(string $name): stdClass { + $attendance = $this->get_attendance_by_name($name); + return get_coursemodule_from_instance('attendance', $attendance->id, $attendance->course); + } +} diff --git a/tests/behat/calendar_features.feature b/tests/behat/calendar_features.feature index 5e97214..b04507c 100644 --- a/tests/behat/calendar_features.feature +++ b/tests/behat/calendar_features.feature @@ -1,4 +1,4 @@ -@mod @mod_attendance @javascript +@mod @mod_attendance Feature: Test the calendar related features in the attendance module Background: @@ -13,18 +13,18 @@ Feature: Test the calendar related features in the attendance module | course | user | role | timestart | | C1 | student1 | student | ##yesterday## | | C1 | teacher1 | editingteacher | ##yesterday## | - + And the following "activity" exists: + | activity | attendance | + | course | C1 | + | idnumber | 00001 | + | name | Test attendance | And I log in as "teacher1" - And I am on "Course 1" course homepage with editing mode on - And I add the "Upcoming events" block - And I add a "Attendance" to section "1" and I fill the form with: - | Name | Test attendance | - And I log out + @javascript Scenario: Calendar events can be created automatically with sessions creation - Given I log in as "teacher1" - And I am on "Course 1" course homepage - And I follow "Test attendance" + Given I am on "Course 1" course homepage with editing mode on + And I add the "Calendar" block + And I am on the "Test attendance" "mod_attendance > View" page And I follow "Add session" And I set the following fields to these values: | id_sestime_starthour | 23 | @@ -33,9 +33,5 @@ Feature: Test the calendar related features in the attendance module | id_sestime_endminute | 55 | And I click on "id_submitbutton" "button" And I am on "Course 1" course homepage - And I follow "Go to calendar" - And I should see "Test attendance" - And I log out - And I log in as "student1" - And I follow "Go to calendar" + And I click on "Full calendar" "link" Then I should see "Test attendance" diff --git a/tests/behat/defaultstatus.feature b/tests/behat/defaultstatus.feature index 5af3534..918f13a 100644 --- a/tests/behat/defaultstatus.feature +++ b/tests/behat/defaultstatus.feature @@ -21,10 +21,11 @@ Feature: Admin can set default status set for use in new attendance @javascript Scenario: Modified default status set added to new attendance + Given I log in as "teacher1" And I am on "Course 1" course homepage with editing mode on And I add a "Attendance" to section "1" and I fill the form with: | Name | Attendance1 | - And I follow "Attendance1" + And I am on the "Attendance1" "mod_attendance > View" page And I follow "Status set" Then the field with xpath "//*[@id='preferencesform']/table/tbody/tr[2]/td[3]/input" matches value "customstatusdescription" diff --git a/tests/behat/extra_features.feature b/tests/behat/extra_features.feature index 75535bc..ee85fcd 100644 --- a/tests/behat/extra_features.feature +++ b/tests/behat/extra_features.feature @@ -1,10 +1,11 @@ -@mod @mod_attendance @javascript +@mod @mod_attendance Feature: Test the various new features in the attendance module Background: Given the following "courses" exist: | fullname | shortname | summary | category | timecreated | timemodified | | Course 1 | C1 | Prove the attendance activity works | 0 | ##yesterday## | ##yesterday## | + | Course 2 | C2 | Prove the attendance activity works | 0 | ##yesterday## | ##yesterday## | And the following "users" exist: | username | firstname | lastname | email | | teacher1 | Teacher | 1 | teacher1@example.com | @@ -19,19 +20,25 @@ Feature: Test the various new features in the attendance module | C1 | student1 | student | ##yesterday## | | C1 | student2 | student | ##yesterday## | | C1 | student3 | student | ##yesterday## | - - And I log in as "teacher1" - And I am on "Course 1" course homepage with editing mode on - And I add a "Attendance" to section "1" and I fill the form with: - | Name | Test attendance | - And I add a "Attendance" to section "1" and I fill the form with: - | Name | Test2 attendance | - And I log out - + | C2 | teacher1 | editingteacher | ##yesterday## | + | C2 | student1 | student | ##yesterday## | + | C2 | student2 | student | ##yesterday## | + | C2 | student3 | student | ##yesterday## | + And the following "activity" exists: + | activity | attendance | + | course | C1 | + | idnumber | 00001 | + | name | Test attendance | + + And the following "activity" exists: + | activity | attendance | + | course | C2 | + | idnumber | 00002 | + | name | Test2attendance | + + @javascript Scenario: A teacher can create and update temporary users - Given I log in as "teacher1" - And I am on "Course 1" course homepage - And I follow "Test attendance" + Given I am on the "Test attendance" "mod_attendance > View" page logged in as "teacher1" And I follow "Temporary users" When I set the following fields to these values: @@ -60,10 +67,9 @@ Feature: Test the various new features in the attendance module Then I should not see "Temporary user 1" And I should see "Temporary user 2" + @javascript Scenario: A teacher can take attendance for temporary users - Given I log in as "teacher1" - And I am on "Course 1" course homepage - And I follow "Test attendance" + Given I am on the "Test attendance" "mod_attendance > View" page logged in as "teacher1" And I follow "Temporary users" And I set the following fields to these values: | Full name | Temporary user 1 | @@ -99,7 +105,7 @@ Feature: Test the various new features in the attendance module And I should see "Absent" # Merge user. - When I follow "Test attendance" + And I am on the "Test attendance" "mod_attendance > View" page And I follow "Temporary users" And I click on "Merge user" "link" in the "Temporary user 2" "table_row" And I set the field "Participant" to "Student 3" @@ -112,6 +118,7 @@ Feature: Test the various new features in the attendance module And "A" "text" should exist in the "Student 3" "table_row" Then I should not see "Temporary user 2" + @javascript Scenario: A teacher can select a subset of users for export Given the following "groups" exist: | course | name | idnumber | @@ -124,9 +131,7 @@ Feature: Test the various new features in the attendance module | Group2 | student2 | | Group2 | student3 | - And I log in as "teacher1" - And I am on "Course 1" course homepage - And I follow "Test attendance" + And I am on the "Test attendance" "mod_attendance > View" page logged in as "teacher1" And I follow "Add" And I set the following fields to these values: | id_addmultiply | 0 | @@ -146,10 +151,9 @@ Feature: Test the various new features in the attendance module And the "Users to export" select box should not contain "Student 1" # Ideally the download would be tested here, but that is difficult to configure. + @javascript Scenario: A teacher can create and use multiple status lists - Given I log in as "teacher1" - And I am on "Course 1" course homepage - And I follow "Test attendance" + Given I am on the "Test attendance" "mod_attendance > View" page logged in as "teacher1" And I follow "Status set" And I set the field "jump" to "New set of statuses" And I set the field with xpath "//*[@id='preferencesform']/table/tbody/tr[1]/td[2]/input" to "G" @@ -195,10 +199,9 @@ Feature: Test the various new features in the attendance module And "Set status to «OK»" "link" should exist And "Set status to «Bad»" "link" should exist + @javascript Scenario: A teacher can use the radio buttons to set attendance values for all users - Given I log in as "teacher1" - And I am on "Course 1" course homepage - And I follow "Test attendance" + Given I am on the "Test attendance" "mod_attendance > View" page logged in as "teacher1" And I follow "Add" And I set the following fields to these values: | id_addmultiply | 0 | @@ -212,10 +215,9 @@ Feature: Test the various new features in the attendance module And "L" "text" should exist in the "Student 2" "table_row" And "L" "text" should exist in the "Student 3" "table_row" + @javascript Scenario: A teacher can use the radio buttons to set attendance values for unselected users - Given I log in as "teacher1" - And I am on "Course 1" course homepage - And I follow "Test2 attendance" + Given I am on the "Test2attendance" "mod_attendance > View" page logged in as "teacher1" And I follow "Add" And I set the following fields to these values: | id_addmultiply | 0 | diff --git a/tests/behat/preferences.feature b/tests/behat/preferences.feature index ae529db..3b119f6 100644 --- a/tests/behat/preferences.feature +++ b/tests/behat/preferences.feature @@ -1,4 +1,4 @@ -@mod @uon @mod_attendance @mod_attendance_preferences +@mod @mod_attendance @mod_attendance_preferences Feature: Teachers can't change status variables to have empty acronyms or descriptions In order to update status variables As a teacher @@ -17,28 +17,29 @@ Feature: Teachers can't change status variables to have empty acronyms or descri | C1 | student1 | student | ##yesterday## | | C1 | teacher1 | editingteacher | ##yesterday## | - And I log in as "teacher1" - And I am on "Course 1" course homepage with editing mode on - And I add a "Attendance" to section "1" and I fill the form with: - | Name | Attendancepreftest | - And I follow "Attendancepreftest" - And I follow "Status set" + And the following "activity" exists: + | activity | attendance | + | course | C1 | + | idnumber | 00001 | + | name | Attendancepreftest | @javascript Scenario: Teachers can add status variables + Given I am on the "Attendancepreftest" "mod_attendance > View" page logged in as "teacher1" + And I follow "Status set" # Set the second status acronym to be empty - Given I set the field with xpath "//*[@id='preferencesform']/table/tbody/tr[2]/td[2]/input" to "" + And I set the field with xpath "//*[@id='preferencesform']/table/tbody/tr[2]/td[2]/input" to "" # Set the second status description to be empty And I set the field with xpath "//*[@id='preferencesform']/table/tbody/tr[2]/td[3]/input" to "" # Set the second status grade to be empty And I set the field with xpath "//*[@id='preferencesform']/table/tbody/tr[2]/td[4]/input" to "" - When I click on "Update" "button" in the "#preferencesform" "css_element" - Then I should see "Empty acronyms are not allowed" in the "//*[@id='preferencesform']/table/tbody/tr[2]/td[2]/p" "xpath_element" + And I click on "Update" "button" in the "#preferencesform" "css_element" + And I should see "Empty acronyms are not allowed" in the "//*[@id='preferencesform']/table/tbody/tr[2]/td[2]/p" "xpath_element" And I should see "Empty descriptions are not allowed" in the "//*[@id='preferencesform']/table/tbody/tr[2]/td[3]/p" "xpath_element" And I click on "Update" "button" in the "#preferencesform" "css_element" # Set the first status acronym to be empty - Given I set the field with xpath "//*[@id='preferencesform']/table/tbody/tr[1]/td[2]/input" to "" + And I set the field with xpath "//*[@id='preferencesform']/table/tbody/tr[1]/td[2]/input" to "" # Set the first status description to be empty And I set the field with xpath "//*[@id='preferencesform']/table/tbody/tr[1]/td[3]/input" to "" # Set the first status grade to be empty diff --git a/tests/behat/report.feature b/tests/behat/report.feature index b9da498..e4a188d 100644 --- a/tests/behat/report.feature +++ b/tests/behat/report.feature @@ -1,4 +1,4 @@ -@javascript @mod @uon @mod_attendance +@javascript @mod @mod_attendance Feature: Visiting reports As a teacher I visit the reports @@ -17,11 +17,12 @@ Feature: Visiting reports And the following config values are set as admin: | enablewarnings | 1 | attendance | - And I log in as "teacher1" - And I am on "Course 1" course homepage with editing mode on - And I add a "Attendance" to section "1" and I fill the form with: - | Name | Attendance | - And I follow "Attendance" + And the following "activity" exists: + | activity | attendance | + | course | C1 | + | idnumber | 00001 | + | name | Attendance | + And I am on the "Attendance" "mod_attendance > View" page logged in as "teacher1" And I follow "Add session" And I set the following fields to these values: | id_sestime_starthour | 01 | @@ -36,9 +37,7 @@ Feature: Visiting reports And I log out Scenario: Teacher takes attendance - Given I log in as "teacher1" - And I am on "Course 1" course homepage - And I follow "Attendance" + Given I am on the "Attendance" "mod_attendance > View" page logged in as "teacher1" And I navigate to "Edit settings" in current page administration Then I set the following fields to these values: | id_grade_modgrade_type | Point | @@ -76,18 +75,14 @@ Feature: Visiting reports And the following "group members" exist: | group | user | | Group1 | student1 | - - When I log in as "teacher1" - And I am on "Course 1" course homepage - And I follow "Attendance" + And I am on the "Attendance" "mod_attendance > View" page logged in as "teacher1" And I navigate to "Edit settings" in current page administration And I set the following fields to these values: | id_grade_modgrade_type | Point | | id_grade_modgrade_point | 50 | | id_groupmode | Visible groups | And I press "Save and display" - - When I follow "Attendance" + And I am on the "Attendance" "mod_attendance > View" page Then I click on "Take attendance" "link" in the "1AM - 2AM" "table_row" # Excused And I click on "td.cell.c4 input" "css_element" in the "Student 1" "table_row" @@ -119,15 +114,13 @@ Feature: Visiting reports And I log out Scenario: Teacher visit summary report and absentee report - Given I log in as "teacher1" - And I am on "Course 1" course homepage - And I follow "Attendance" + Given I am on the "Attendance" "mod_attendance > View" page logged in as "teacher1" And I navigate to "Edit settings" in current page administration And I set the following fields to these values: | id_grade_modgrade_type | Point | | id_grade_modgrade_point | 50 | And I press "Save and display" - + And I am on the "Attendance" "mod_attendance > View" page When I click on "Take attendance" "link" in the "1AM - 2AM" "table_row" # Late And I click on "td.cell.c4 input" "css_element" in the "Student 1" "table_row" @@ -166,9 +159,7 @@ Feature: Visiting reports And I log out Scenario: Student visit user report - Given I log in as "teacher1" - And I am on "Course 1" course homepage - And I follow "Attendance" + Given I am on the "Attendance" "mod_attendance > View" page logged in as "teacher1" And I navigate to "Edit settings" in current page administration Then I set the following fields to these values: | id_grade_modgrade_type | Point | @@ -198,10 +189,7 @@ Feature: Visiting reports And I click on "id_submitbutton" "button" Then I log out - - When I log in as "student1" - And I am on "Course 1" course homepage - And I follow "Attendance" + Given I am on the "Attendance" "mod_attendance > View" page logged in as "student1" And I click on "All" "link" in the ".attfiltercontrols" "css_element" Then "2" "text" should exist in the "Taken sessions" "table_row"