first commit

This commit is contained in:
CHIEFSOFT\ameye
2024-09-30 18:11:26 -04:00
commit e592ca6823
27270 changed files with 5002257 additions and 0 deletions
File diff suppressed because it is too large Load Diff
+208
View File
@@ -0,0 +1,208 @@
@core @core_badges
Feature: Add badges to the system
In order to give badges to users for their achievements
As an admin
I need to manage badges in the system
Background:
Given I am on homepage
And I log in as "admin"
@javascript
Scenario: Accessing the badges
And I turn editing mode on
And the following config values are set as admin:
| unaddableblocks | | theme_boost|
# TODO MDL-57120 site "Badges" link not accessible without navigation block.
And I add the "Navigation" block if not present
And I click on "Site pages" "list_item" in the "Navigation" "block"
Given I click on "Site badges" "link" in the "Navigation" "block"
Then I should see "There are no matching badges available for users to earn"
@javascript @_file_upload
Scenario: Add a badge
Given I navigate to "Badges > Badges settings" in site administration
And I set the field "Badge issuer name" to "Test Badge Site"
And I set the field "Badge issuer email address" to "testuser@example.com"
And I press "Save changes"
And I navigate to "Badges > Add a new badge" in site administration
And I set the following fields to these values:
| Name | Test badge with 'apostrophe' and other friends (<>&@#) |
| Version | v1 |
| Language | English |
| Description | Test badge description |
| Image author | http://author.example.com |
| Image caption | Test caption image |
| Tags | Math, Physics |
And I upload "badges/tests/behat/badge.png" file to "Image" filemanager
When I press "Create badge"
Then I should see "Edit details"
And I should see "Test badge with 'apostrophe' and other friends (&@#)"
And I should see "Endorsement"
And I should see "Related badges (0)"
And I should see "Alignments (0)"
And I should not see "Create badge"
And I should not see "Issuer details"
And I select "Overview" from the "jump" singleselect
And I should see "Issuer details"
And I should see "Test Badge Site"
And I should see "testuser@example.com"
And I should see "Tags"
And I should see "Math"
And I should see "Physics"
And I navigate to "Badges > Manage badges" in site administration
And I should not see "There are no matching badges available for users to earn"
@javascript @_file_upload
Scenario: Add a badge related
Given I navigate to "Badges > Add a new badge" in site administration
And I set the following fields to these values:
| Name | Test Badge 1 |
| Version | v1 |
| Language | French |
| Description | Test badge related description |
| Image author | http://author.example.com |
| Image caption | Test caption image |
And I upload "badges/tests/behat/badge.png" file to "Image" filemanager
And I press "Create badge"
And I wait until the page is ready
And I navigate to "Badges > Manage badges" in site administration
And I press "Add a new badge"
And I set the following fields to these values:
| Name | Test Badge 2 |
| Version | v2 |
| Language | English |
| Description | Test badge description |
| Image author | http://author.example.com |
| Image caption | Test caption image |
And I upload "badges/tests/behat/badge.png" file to "Image" filemanager
And I press "Create badge"
And I select "Related badges (0)" from the "jump" singleselect
And I should see "This badge does not have any related badges."
And I press "Add related badge"
And I follow "Related badges"
And I wait until the page is ready
And I follow "Related badges"
And I set the field "relatedbadgeids[]" to "Test Badge 1 (version: v1, language: French, Site badges)"
When I press "Save changes"
Then I should see "Related badges (1)"
@javascript @_file_upload
Scenario: Endorsement for Badge
Given I navigate to "Badges > Add a new badge" in site administration
And I set the following fields to these values:
| Name | Test Badge Enrolment |
| Version | v1 |
| Language | English |
| Description | Test badge description |
| Image author | http://author.example.com |
| Image caption | Test caption image |
And I upload "badges/tests/behat/badge.png" file to "Image" filemanager
When I press "Create badge"
Then I should see "Edit details"
And I should see "Endorsement"
And I select "Endorsement" from the "jump" singleselect
And I set the following fields to these values:
| Endorser name | Endorser |
| Email | endorsement@example.com |
| Issuer URL | http://example.com |
| Claim URL | http://claimurl.example.com |
| Endorsement comment | Test Endorsement comment |
And I press "Save changes"
Then I should see "Changes saved"
@javascript @_file_upload
Scenario: Alignments for Badge
Given I navigate to "Badges > Add a new badge" in site administration
And I set the following fields to these values:
| Name | Test Badge |
| Version | v1 |
| Language | English |
| Description | Test badge description |
| Image author | http://author.example.com |
| Image caption | Test caption image |
And I upload "badges/tests/behat/badge.png" file to "Image" filemanager
When I press "Create badge"
Then I should see "Test Badge"
And I should see "Endorsement"
And I select "Alignments (0)" from the "jump" singleselect
And I should see "This badge does not have any external skills or standards specified."
And I press "Add external skill or standard"
And I set the following fields to these values:
| Name | Test Badge Alignments |
| URL | https://alignments.example.com |
| Description | Test Badge Alignments description |
When I press "Save changes"
And I should see "Alignments (1)"
@javascript @_file_upload
Scenario: Add a badge from Site badges section
Given I turn editing mode on
And the following config values are set as admin:
| unaddableblocks | | theme_boost|
# TODO MDL-57120 site "Badges" link not accessible without navigation block.
And I add the "Navigation" block if not present
When I click on "Site pages" "list_item" in the "Navigation" "block"
And I click on "Site badges" "link" in the "Navigation" "block"
Then I should see "Add a new badge"
# Add a badge.
When I press "Add a new badge"
And I set the following fields to these values:
| Name | Test badge with 'apostrophe' and other friends (<>&@#) 2 |
| Version | v1 |
| Language | English |
| Description | Test badge description |
| Image author | http://author.example.com |
| Image caption | Test caption image |
And I upload "badges/tests/behat/badge.png" file to "Image" filemanager
And I press "Create badge"
Then I should see "Edit details"
And I should see "Test badge with 'apostrophe' and other friends (&@#) 2"
And I should see "Endorsement"
And I should see "Related badges (0)"
And I should see "Alignments (0)"
And I should not see "Create badge"
And I navigate to "Badges > Manage badges" in site administration
And I should not see "There are no matching badges available for users to earn"
# See buttons from the "Site badges" page.
And I am on homepage
When I click on "Site pages" "list_item" in the "Navigation" "block"
And I click on "Site badges" "link" in the "Navigation" "block"
Then I should see "Manage badges"
And I should see "Add a new badge"
@javascript @_file_upload
Scenario: Edit a badge
Given I navigate to "Badges > Badges settings" in site administration
And I set the field "Badge issuer name" to "Test Badge Site"
And I set the field "Badge issuer email address" to "testuser@example.com"
And I press "Save changes"
And I navigate to "Badges > Add a new badge" in site administration
And I set the following fields to these values:
| Name | Test badge with 'apostrophe' and other friends (<>&@#) |
| Version | firstversion |
| Language | English |
| Description | Test badge description |
| Image author | http://author.example.com |
| Image caption | Test caption image |
| Tags | Math, Physics |
And I upload "badges/tests/behat/badge.png" file to "Image" filemanager
And I press "Create badge"
When I select "Edit details" from the "jump" singleselect
And I should see "Test badge with 'apostrophe' and other friends (&@#)"
And I should not see "Issuer details"
And I should see "Math"
And I should see "Physics"
And I set the following fields to these values:
| Name | Test badge renamed |
| Version | secondversion |
| Tags | Math, History |
And I press "Save changes"
And I select "Overview" from the "jump" singleselect
Then I should not see "Test badge with 'apostrophe' and other friends (&@#)"
And I should not see "firstversion"
And I should not see "Math, Physics"
And I should see "Test badge renamed"
And I should see "secondversion"
And I should see "Math"
And I should see "History"
+381
View File
@@ -0,0 +1,381 @@
@core @core_badges @_file_upload
Feature: Award badges
In order to award badges to users for their achievements
As an admin
I need to add criteria to badges in the system
Background:
Given the following "courses" exist:
| fullname | shortname | format | enablecompletion |
| Course 1 | C1 | topics | 1 |
And the following "users" exist:
| username | firstname | lastname | email |
| teacher1 | Teacher | 1 | teacher1@example.com |
| student1 | Student | 1 | student1@example.com |
| student2 | Student | 2 | student2@example.com |
And the following "course enrolments" exist:
| user | course | role |
| teacher1 | C1 | editingteacher |
| student1 | C1 | student |
| student2 | C1 | student |
And the following "activity" exists:
| activity | assign |
| course | C1 |
| section | 1 |
| name | Test assignment name |
| intro | Submit your online text |
| completion | 1 |
| assignsubmission_onlinetext_enabled | 1 |
@javascript
Scenario: Award badge on other badges as criteria
Given I log in as "teacher1"
And I am on "Course 1" course homepage
# Create course badge 1.
And I navigate to "Badges > Add a new badge" in current page administration
And I set the following fields to these values:
| Name | Course Badge 1 |
| Description | Course badge 1 description |
And I upload "badges/tests/behat/badge.png" file to "Image" filemanager
And I press "Create badge"
And I set the field "type" to "Manual issue by role"
And I expand all fieldsets
# Set to ANY of the roles awards badge.
And I set the field "Teacher" to "1"
And I set the field "Any of the selected roles awards the badge" to "1"
And I press "Save"
And I press "Enable access"
And I press "Continue"
# Badge #2
And I am on "Course 1" course homepage
And I navigate to "Badges > Add a new badge" in current page administration
And I set the following fields to these values:
| Name | Course Badge 2 |
| Description | Course badge 2 description |
And I upload "badges/tests/behat/badge.png" file to "Image" filemanager
And I press "Create badge"
# Set "course badge 1" as criteria
And I set the field "type" to "Awarded badges"
And I set the field "id_badge_badges" to "Course Badge 1"
And I press "Save"
And I press "Enable access"
And I press "Continue"
And I am on "Course 1" course homepage
And I navigate to "Badges > Manage badges" in current page administration
And I follow "Course Badge 1"
And I select "Recipients (0)" from the "jump" singleselect
And I press "Award badge"
# Award course badge 1 to student 1.
And I set the field "potentialrecipients[]" to "Student 1 (student1@example.com)"
When I press "Award badge"
And I am on "Course 1" course homepage
And I navigate to "Badges > Manage badges" in current page administration
And I follow "Course Badge 1"
And I select "Recipients (1)" from the "jump" singleselect
Then I should see "Recipients (1)"
And I log out
# Student 1 should have both badges.
And I log in as "student1"
And I follow "Profile" in the user menu
When I click on "Course 1" "link" in the "region-main" "region"
Then I should see "Course Badge 1"
And I should see "Course Badge 2"
# Student 1 should have both badges also in the Badges navigation section.
When I follow "Badges"
Then I should see "Course Badge 1"
And I should see "Course Badge 2"
And I should not see "Manage badges"
And I should not see "Add a new badge"
And I log out
# Teacher 1 should have access to manage/create badges in the Badges navigation section.
When I log in as "teacher1"
And I am on "Course 1" course homepage
And I navigate to "Badges > Manage badges" in current page administration
Then I should see "Course Badge 1"
And I should see "Course Badge 2"
And I should see "Manage badges"
And I should see "Add a new badge"
# Teacher 1 should NOT have access to manage/create site badges in the Site badges section.
When I am on homepage
And I turn editing mode on
And the following config values are set as admin:
| unaddableblocks | | theme_boost|
# TODO MDL-57120 site "Badges" link not accessible without navigation block.
And I add the "Navigation" block if not present
And I click on "Site pages" "list_item" in the "Navigation" "block"
And I click on "Site badges" "link" in the "Navigation" "block"
Then I should see "There are no matching badges available for users to earn"
And I should not see "Manage badges"
And I should not see "Add a new badge"
@javascript
Scenario: Award profile badge
Given I log in as "admin"
And I navigate to "Badges > Add a new badge" in site administration
And I set the following fields to these values:
| Name | Profile Badge |
| Description | Test badge description |
And I upload "badges/tests/behat/badge.png" file to "Image" filemanager
And I press "Create badge"
And I set the field "type" to "Profile completion"
And I expand all fieldsets
And I set the field "First name" to "1"
And I set the field "Email address" to "1"
And I set the field "Phone" to "1"
And I set the field "id_description" to "Criterion description"
When I press "Save"
Then I should see "Profile completion"
And I should see "First name"
And I should see "Email address"
And I should see "Phone"
And I should see "Criterion description"
And I should not see "Criteria for this badge have not been set up yet."
And I press "Enable access"
And I press "Continue"
And I open my profile in edit mode
And I expand all fieldsets
And I set the field "Phone" to "123456789"
And I press "Update profile"
And I follow "Profile" in the user menu
Then I should see "Profile Badge"
@javascript
Scenario: Award site badge
Given I log in as "admin"
And I navigate to "Badges > Add a new badge" in site administration
And I set the following fields to these values:
| Name | Site Badge |
| Description | Site badge description |
And I upload "badges/tests/behat/badge.png" file to "Image" filemanager
And I press "Create badge"
And I set the field "type" to "Manual issue by role"
And I set the field "Teacher" to "1"
And I press "Save"
And I press "Enable access"
And I press "Continue"
And I select "Recipients (0)" from the "jump" singleselect
And I press "Award badge"
And I set the field "potentialrecipients[]" to "Teacher 1 (teacher1@example.com)"
And I press "Award badge"
And I set the field "potentialrecipients[]" to "Student 1 (student1@example.com)"
And I press "Award badge"
And I navigate to "Badges > Manage badges" in site administration
When I follow "Site Badge"
Then I should see "Recipients (2)"
And I log out
And I log in as "student1"
And I follow "Profile" in the user menu
Then I should see "Site Badge"
@javascript
Scenario: Award course badge
Given I log in as "teacher1"
And I am on "Course 1" course homepage
And I navigate to "Badges > Add a new badge" in current page administration
And I set the following fields to these values:
| Name | Course Badge |
| Description | Course badge description |
And I upload "badges/tests/behat/badge.png" file to "Image" filemanager
And I press "Create badge"
And I set the field "type" to "Manual issue by role"
And I set the field "Teacher" to "1"
And I press "Save"
And I press "Enable access"
And I press "Continue"
And I select "Recipients (0)" from the "jump" singleselect
And I press "Award badge"
And I set the field "potentialrecipients[]" to "Student 2 (student2@example.com)"
And I press "Award badge"
And I set the field "potentialrecipients[]" to "Student 1 (student1@example.com)"
When I press "Award badge"
And I am on "Course 1" course homepage
And I navigate to "Badges > Manage badges" in current page administration
And I follow "Course Badge"
Then I should see "Recipients (2)"
And I log out
And I log in as "student1"
And I follow "Profile" in the user menu
And I click on "Course 1" "link" in the "region-main" "region"
And I click on "Course Badge" "link"
And I should see "Course: Course 1"
@javascript
Scenario: Award badge on activity completion
Given I log in as "teacher1"
And I am on "Course 1" course homepage
And I change window size to "large"
And I navigate to "Badges > Add a new badge" in current page administration
And I set the following fields to these values:
| Name | Course Badge |
| Description | Course badge description |
And I upload "badges/tests/behat/badge.png" file to "Image" filemanager
And I press "Create badge"
And I set the field "type" to "Activity completion"
And I set the field "Test assignment name" to "1"
And I press "Save"
And I press "Enable access"
When I press "Continue"
And I log out
And I log in as "student1"
And I follow "Profile" in the user menu
And I click on "Course 1" "link" in the "region-main" "region"
Then I should not see "badges"
And I am on "Course 1" course homepage
And I toggle the manual completion state of "Test assignment name"
And I follow "Profile" in the user menu
And I click on "Course 1" "link" in the "region-main" "region"
Then I should see "Course Badge"
@javascript
Scenario: Award badge on course completion
Given the following "activity" exists:
| activity | assign |
| course | C1 |
| name | Music history |
| section | 1 |
| completion | 2 |
| completionview | 1 |
And I log in as "teacher1"
And I am on "Course 1" course homepage
And I navigate to "Course completion" in current page administration
And I set the field "id_overall_aggregation" to "2"
And I click on "Condition: Activity completion" "link"
And I set the field "Assignment - Music history" to "1"
And I press "Save changes"
And I am on "Course 1" course homepage
And I navigate to "Badges > Add a new badge" in current page administration
And I set the following fields to these values:
| Name | Course Badge |
| Description | Course badge description |
And I upload "badges/tests/behat/badge.png" file to "Image" filemanager
And I press "Create badge"
And I set the field "type" to "Course completion"
And I set the field with xpath ".//*[contains(., 'Minimum grade required')]/ancestor::*[contains(concat(' ', @class, ' '), ' fitem ')]//input[1]" to "0"
And I press "Save"
And I press "Enable access"
When I press "Continue"
And I log out
And I log in as "student1"
And I follow "Profile" in the user menu
And I click on "Course 1" "link" in the "region-main" "region"
Then I should not see "badges"
When I am on the "Music history" "assign activity" page
And I log out
# Completion cron won't mark the whole course completed unless the
# individual criteria was marked completed more than a second ago. So
# run it twice, first to mark the criteria and second for the course.
And I run the scheduled task "core\task\completion_regular_task"
And I wait "1" seconds
And I run the scheduled task "core\task\completion_regular_task"
# The student should now see their badge.
And I log in as "student1"
And I follow "Profile" in the user menu
And I click on "Course 1" "link" in the "region-main" "region"
Then I should see "Course Badge"
@javascript
Scenario: All of the selected roles can award badges
Given I log in as "teacher1"
And I am on "Course 1" course homepage
# Create course badge 1.
And I navigate to "Badges > Add a new badge" in current page administration
And I set the following fields to these values:
| Name | Course Badge 1 |
| Description | Course badge description |
And I upload "badges/tests/behat/badge.png" file to "Image" filemanager
And I press "Create badge"
And I set the field "type" to "Manual issue by role"
And I expand all fieldsets
# Set to ANY of the roles awards badge.
And I set the field "Teacher" to "1"
And I set the field "Any of the selected roles awards the badge" to "1"
And I press "Save"
And I press "Enable access"
And I press "Continue"
And I select "Recipients (0)" from the "jump" singleselect
And I press "Award badge"
# Award course badge 1 to student 1.
And I set the field "potentialrecipients[]" to "Student 1 (student1@example.com)"
When I press "Award badge"
And I am on "Course 1" course homepage
And I navigate to "Badges > Manage badges" in current page administration
And I follow "Course Badge 1"
And I select "Recipients (1)" from the "jump" singleselect
Then I should see "Recipients (1)"
# Add course badge 2.
And I am on "Course 1" course homepage
And I navigate to "Badges > Add a new badge" in current page administration
And I set the following fields to these values:
| Name | Course Badge 2 |
| Description | Course badge description |
And I upload "badges/tests/behat/badge.png" file to "Image" filemanager
And I press "Create badge"
And I set the field "type" to "Manual issue by role"
And I expand all fieldsets
# Set to ALL of the selected roles award badge.
And I set the field "Teacher" to "1"
And I set the field "All of the selected roles award the badge" to "1"
And I press "Save"
And I press "Enable access"
And I press "Continue"
And I select "Recipients (0)" from the "jump" singleselect
And I press "Award badge"
# Award course badge 2 to student 2.
And I set the field "potentialrecipients[]" to "Student 2 (student2@example.com)"
When I press "Award badge"
And I am on "Course 1" course homepage
And I navigate to "Badges > Manage badges" in current page administration
And I follow "Course Badge 2"
And I select "Recipients (1)" from the "jump" singleselect
Then I should see "Recipients (1)"
And I log out
And I trigger cron
# Student 1 should have just course badge 1.
And I log in as "student1"
And I follow "Profile" in the user menu
When I click on "Course 1" "link" in the "region-main" "region"
Then I should see "Course Badge 1"
And I should not see "Course Badge 2"
And I log out
# Student 2 should have just course badge 2.
And I log in as "student2"
And I follow "Profile" in the user menu
When I click on "Course 1" "link" in the "region-main" "region"
Then I should see "Course Badge 2"
Then I should not see "Course Badge 1"
@javascript
Scenario: Revoke badge
Given I log in as "teacher1"
And I am on "Course 1" course homepage
And I navigate to "Badges > Add a new badge" in current page administration
And I set the following fields to these values:
| Name | Course Badge |
| Description | Course badge description |
And I upload "badges/tests/behat/badge.png" file to "Image" filemanager
And I press "Create badge"
And I set the field "type" to "Manual issue by role"
And I set the field "Teacher" to "1"
And I press "Save"
And I press "Enable access"
And I press "Continue"
And I select "Recipients (0)" from the "jump" singleselect
And I press "Award badge"
And I set the field "potentialrecipients[]" to "Student 2 (student2@example.com)"
And I press "Award badge"
And I set the field "potentialrecipients[]" to "Student 1 (student1@example.com)"
When I press "Award badge"
And I am on "Course 1" course homepage
And I navigate to "Badges > Manage badges" in current page administration
And I follow "Course Badge"
Then I should see "Recipients (2)"
And I select "Recipients (2)" from the "jump" singleselect
And I press "Award badge"
And I set the field "existingrecipients[]" to "Student 2 (student2@example.com)"
And I press "Revoke badge"
And I set the field "existingrecipients[]" to "Student 1 (student1@example.com)"
When I press "Revoke badge"
And I am on "Course 1" course homepage
And I navigate to "Badges > Manage badges" in current page administration
And I follow "Course Badge"
Then I should see "Recipients (0)"
@@ -0,0 +1,215 @@
@core @core_badges @_file_upload
Feature: Award badges with separate groups
In order to award badges to users for their achievements
As a teacher
I need to award badges only to users in the groups I have access
Background:
Given the following "users" exist:
| username | firstname | lastname | email |
| teacher1 | Teacher | 1 | teacher1@example.com |
| teacher2 | Teacher | 2 | teacher2@example.com |
| student1 | Student | 1 | student1@example.com |
| student2 | Student | 2 | student2@example.com |
And the following "courses" exist:
| fullname | shortname | category | groupmode |
| Course 1 | C1 | 0 | 1 |
And the following "course enrolments" exist:
| user | course | role |
| teacher1 | C1 | editingteacher |
| teacher2 | C1 | teacher |
| student1 | C1 | student |
| student2 | C1 | student |
And the following "groups" exist:
| name | course | idnumber |
| Class A | C1 | CA |
| Class B | C1 | CB |
And the following "group members" exist:
| user | group |
| student1 | CB |
| teacher1 | CB |
| student2 | CA |
| teacher2 | CA |
And I am on the "Course 1" "course editing" page logged in as "teacher1"
And I expand all fieldsets
And I set the field "Group mode" to "Separate groups"
And I press "Save and display"
And I navigate to "Badges > Add a new badge" in current page administration
And I set the following fields to these values:
| Name | Course Badge |
| Description | Course badge description |
And I upload "badges/tests/behat/badge.png" file to "Image" filemanager
And I press "Create badge"
And I set the field "type" to "Manual issue by role"
And I expand all fieldsets
And I set the field "Teacher" to "1"
And I set the field "Non-editing teacher" to "1"
# Set to ANY of the roles awards badge.
And I set the field "Any of the selected roles awards the badge" to "1"
And I press "Save"
And I press "Enable access"
And I press "Continue"
And I log out
@javascript
Scenario: Award course badge as non-editing teacher with only one group
When I log in as "teacher2"
And I am on "Course 1" course homepage
And I navigate to "Badges > Manage badges" in current page administration
And I follow "Course Badge"
And I press "Award badge"
And I set the field "role" to "Non-editing teacher"
# Teacher 2 should see a "Separate groups" label with the group he is in
Then I should see "Separate groups: Class A"
# Teacher 2 should only see the users who belong to the same group as he does
And I should see "Student 2"
And I should not see "Student 1"
# Non-editing teacher can award the badge
And I set the field "potentialrecipients[]" to "Student 2 (student2@example.com)"
And I press "Award badge"
And I am on "Course 1" course homepage
And I navigate to "Badges > Manage badges" in current page administration
And I follow "Course Badge"
And I should see "Recipients (1)"
And I log out
And I log in as "student2"
And I follow "Profile" in the user menu
And I click on "Course 1" "link" in the "region-main" "region"
And I should see "Course Badge"
And I log out
@javascript
Scenario: Award course badge as non-editing teacher with more than one group
Given I log in as "teacher1"
And I am on the "Course 1" "groups" page
And I set the field "groups" to "Class B (2)"
And I press "Add/remove users"
And I set the field "addselect" to "Teacher 2 (teacher2@example.com)"
And I press "Add"
And I log out
When I log in as "teacher2"
And I am on "Course 1" course homepage
And I navigate to "Badges > Manage badges" in current page administration
And I follow "Course Badge"
And I press "Award badge"
And I set the field "role" to "Non-editing teacher"
# Teacher 2 should see a "Separate groups" label and a dropdown menu with the groups he belongs to
And I set the field "Separate groups" to "Class A"
Then I should see "Student 2"
And I should not see "Student 1"
And I set the field "Separate groups" to "Class B"
And I should see "Student 1"
And I should not see "Student 2"
And I log out
@javascript
Scenario: Award course badge as non-editing teacher without any group
Given I log in as "teacher1"
And I am on the "Course 1" "groups" page
And I set the field "groups" to "Class A (2)"
And I press "Add/remove users"
And I set the field "removeselect" to "Teacher 2 (teacher2@example.com)"
And I press "Remove"
And I press "Back to groups"
And I log out
When I log in as "teacher2"
And I am on "Course 1" course homepage
And I navigate to "Badges > Manage badges" in current page administration
And I follow "Course Badge"
And I press "Award badge"
# Teacher 2 shouldn't be able to go further
Then I should see "Sorry, but you need to be part of a group to see this page."
@javascript
Scenario: Editing teacher can award badge to members of separate groups
Given I am on the "Course 1" course page logged in as teacher1
And I navigate to "Badges > Manage badges" in current page administration
And I follow "Course Badge"
And I press "Award badge"
When I set the field "role" to "Teacher"
# Confirm that editing teacher sees a separate groups dropdown menu.
Then "Separate groups" "select" should exist
And I should see "All participants" in the "Separate groups" "select"
# Confirm that all participants are displayed when All participants is selected.
And I should see "Student 1" in the "potentialrecipients[]" "select"
And I should see "Student 2" in the "potentialrecipients[]" "select"
And I should see "Teacher 1" in the "potentialrecipients[]" "select"
And I should see "Teacher 2" in the "potentialrecipients[]" "select"
And I set the field "Separate groups" to "Class A"
# Confirm that only members of selected group are displayed
And I should not see "Student 1" in the "potentialrecipients[]" "select"
And I should not see "Teacher 1" in the "potentialrecipients[]" "select"
And I should see "Student 2" in the "potentialrecipients[]" "select"
And I should see "Teacher 2" in the "potentialrecipients[]" "select"
And I set the field "Separate groups" to "Class B"
And I should not see "Student 2" in the "potentialrecipients[]" "select"
And I should not see "Teacher 2" in the "potentialrecipients[]" "select"
And I should see "Student 1" in the "potentialrecipients[]" "select"
And I should see "Teacher 1" in the "potentialrecipients[]" "select"
@javascript
Scenario Outline: Teacher can award badge to members of visible groups
Given I am on the "Course 1" "course editing" page logged in as teacher1
And I expand all fieldsets
# Set the group mode to visible groups.
And I set the field "Group mode" to "Visible groups"
And I press "Save and display"
When I am on the "Course 1" course page logged in as <loggedinuser>
And I navigate to "Badges > Manage badges" in current page administration
And I follow "Course Badge"
And I press "Award badge"
And I set the field "role" to "<awarder>"
# Confirm that teachers see a visible groups dropdown menu.
Then "Visible groups" "select" should exist
# Confirm that My groups option group exists.
And "optgroup[label='My groups']" "css_element" should exist in the "select[name='group']" "css_element"
# Confirm that Other groups option group exists.
And "optgroup[label='Other groups']" "css_element" should exist in the "select[name='group']" "css_element"
# Confirm that all participants are displayed when All participants is selected.
And I set the field "Visible groups" to "All participants"
And I should see "Student 1" in the "potentialrecipients[]" "select"
And I should see "Student 2" in the "potentialrecipients[]" "select"
And I should see "Teacher 1" in the "potentialrecipients[]" "select"
And I should see "Teacher 2" in the "potentialrecipients[]" "select"
# Confirm that only members of selected group are displayed.
And I set the field "Visible groups" to "Class A"
And I should not see "Student 1" in the "potentialrecipients[]" "select"
And I should not see "Teacher 1" in the "potentialrecipients[]" "select"
And I should see "Student 2" in the "potentialrecipients[]" "select"
And I should see "Teacher 2" in the "potentialrecipients[]" "select"
And I set the field "Visible groups" to "Class B"
And I should not see "Student 2" in the "potentialrecipients[]" "select"
And I should not see "Teacher 2" in the "potentialrecipients[]" "select"
And I should see "Student 1" in the "potentialrecipients[]" "select"
And I should see "Teacher 1" in the "potentialrecipients[]" "select"
Examples:
| loggedinuser | awarder |
| teacher1 | Teacher |
| teacher2 | Non-editing teacher |
@javascript
Scenario Outline: Teacher can award badge to members when group mode is set to no groups
Given I am on the "Course 1" "course editing" page logged in as teacher1
And I expand all fieldsets
# Set the group mode to no groups.
And I set the field "Group mode" to "No groups"
And I press "Save and display"
When I am on the "Course 1" course page logged in as <loggedinuser>
And I navigate to "Badges > Manage badges" in current page administration
And I follow "Course Badge"
And I press "Award badge"
And I set the field "role" to "<awarder>"
# Confirm that no group dropdowns don't exist.
Then "Separate groups" "select" should not exist
And "Visible groups" "select" should not exist
# Confirm all participants are displayed.
And I should see "Student 1" in the "potentialrecipients[]" "select"
And I should see "Student 2" in the "potentialrecipients[]" "select"
And I should see "Teacher 1" in the "potentialrecipients[]" "select"
And I should see "Teacher 2" in the "potentialrecipients[]" "select"
Examples:
| loggedinuser | awarder |
| teacher1 | Teacher |
| teacher2 | Non-editing teacher |
+204
View File
@@ -0,0 +1,204 @@
@core @core_badges
Feature: Backpack badges
Test the settings to add/update a backpack for a site and user.
I need to verify display backpack in the my profile
Background:
Given the following "badge external backpacks" exist:
| backpackapiurl | backpackweburl | apiversion | sortorder |
| https://dc.imsglobal.org/obchost/ims/ob/v2p1 | https://dc.imsglobal.org | 2.1 | 2 |
| https://test.com/ | https://test.com/ | 2 | 3 |
And the following "users" exist:
| username | firstname | lastname | email |
| student1 | Student | 1 | student1@example.com |
And I log in as "admin"
And I navigate to "Badges > Badges settings" in site administration
And I set the field "Badge issuer name" to "Test Badge Site"
And I set the field "Badge issuer email address" to "testuser@example.com"
And I log out
@javascript
Scenario: If external backpack connection is disabled, backpack settings should not be displayed
Given I am on homepage
And I log in as "admin"
And I navigate to "Badges > Badges settings" in site administration
And I set the following fields to these values:
| External backpack connection | 0 |
And I press "Save changes"
When I navigate to "Badges" in site administration
Then I should not see "Manage backpacks"
And I navigate to "Badges > Badges settings" in site administration
And I set the following fields to these values:
| External backpack connection | 1 |
And I press "Save changes"
And I am on homepage
And I navigate to "Badges" in site administration
And I should see "Manage backpacks"
@javascript
Scenario: Verify backback settings
Given the following "core_badges > Badge" exists:
| name | Test badge verify backpack |
| version | 1 |
| language | en |
| description | Test badge description |
| image | badges/tests/behat/badge.png |
| imageauthorurl | http://author.example.com |
| imagecaption | Test caption image |
And the following "core_badges > Criteria" exists:
| badge | Test badge verify backpack |
| role | editingteacher |
And the following "core_badges > Issued badge" exists:
| badge | Test badge verify backpack |
| user | student1 |
When I log in as "student1"
And I follow "Preferences" in the user menu
And I follow "Backpack settings"
Then I should see "Choose..." in the "Backpack provider" "select"
@javascript
Scenario: User has been connected backpack
Given I am on homepage
And I log in as "admin"
And I navigate to "Badges > Badges settings" in site administration
And I set the following fields to these values:
| External backpack connection | 1 |
And I press "Save changes"
And I navigate to "Badges > Manage backpacks" in site administration
And I click on "Move up" "link" in the "https://dc.imsglobal.org" "table_row"
And the following "core_badges > Badge" exists:
| name | Test badge verify backpack |
| version | 1 |
| language | en |
| description | Test badge description |
| image | badges/tests/behat/badge.png |
| imageauthorurl | http://author.example.com |
| imagecaption | Test caption image |
And the following "core_badges > Criteria" exists:
| badge | Test badge verify backpack |
| role | editingteacher |
And the following "core_badges > Issued badge" exists:
| badge | Test badge verify backpack |
| user | student1 |
And the following "setup backpack connected" exist:
| user | externalbackpack |
| student1 | https://dc.imsglobal.org |
When I log in as "student1"
And I follow "Preferences" in the user menu
And I follow "Backpack settings"
Then I should see "Connected"
And I follow "Preferences" in the user menu
And I follow "Manage badges"
And I should see "Test badge verify backpack"
And "Add to backpack" "link" should exist
@javascript
Scenario: Add a new site backpack
Given I am on homepage
And I log in as "admin"
And I navigate to "Badges > Manage backpacks" in site administration
When I press "Add a new backpack"
And I set the field "apiversion" to "2"
And I set the field "backpackweburl" to "aaa"
And I press "Save changes"
And I should see "Invalid URL"
And I set the field "backpackweburl" to "http://backpackweburl.cat"
And I press "Save changes"
And I should see "You must supply a value here"
And I set the field "backpackapiurl" to "http://backpackapiurl.cat"
And I press "Save changes"
Then I should see "http://backpackweburl.cat"
And "Delete" "icon" should exist in the "http://backpackweburl.cat" "table_row"
And "Edit settings" "icon" should exist in the "http://backpackweburl.cat" "table_row"
@javascript
Scenario: Remove a site backpack
Given I am on homepage
And I log in as "admin"
And I navigate to "Badges > Manage backpacks" in site administration
When I click on "Delete" "link" in the "https://dc.imsglobal.org" "table_row"
And I should see "Delete site backpack 'https://dc.imsglobal.org'?"
And I click on "Delete" "button" in the "Delete site backpack" "dialogue"
Then I should see "The site backpack has been deleted."
And I should not see "https://dc.imsglobal.org"
And "Delete" "button" should not be visible
@javascript
Scenario: Move up and down site backpack
Given I am on homepage
And I log in as "admin"
And I navigate to "Badges > Manage backpacks" in site administration
And "Move up" "icon" should exist in the "https://dc.imsglobal.org" "table_row"
And "Move down" "icon" should exist in the "https://dc.imsglobal.org" "table_row"
When I click on "Move up" "link" in the "https://dc.imsglobal.org" "table_row"
Then "Move up" "icon" should not exist in the "https://dc.imsglobal.org" "table_row"
And "Move down" "icon" should exist in the "https://dc.imsglobal.org" "table_row"
And I click on "Move down" "link" in the "https://dc.imsglobal.org" "table_row"
And I click on "Move down" "link" in the "https://dc.imsglobal.org" "table_row"
And "Move up" "icon" should exist in the "https://dc.imsglobal.org" "table_row"
And "Move down" "icon" should not exist in the "https://dc.imsglobal.org" "table_row"
@javascript
Scenario: Add a new site backpack with authentication details checkbox
Given I am on homepage
And I log in as "admin"
And I navigate to "Badges > Manage backpacks" in site administration
When I press "Add a new backpack"
And I set the field "apiversion" to "2.1"
And I set the field "backpackweburl" to "http://backpackweburl.cat"
And I should not see "Backpack API URL"
Then "Include authentication details with the backpack" "checkbox" should not be visible
And I should not see "Badge issuer email address"
And I should not see "Badge issuer password"
And I set the field "apiversion" to "1"
And "Include authentication details with the backpack" "checkbox" should be visible
And I click on "includeauthdetails" "checkbox"
And I should see "Badge issuer email address"
And I should not see "Badge issuer password"
And I set the field "apiversion" to "2"
And "Include authentication details with the backpack" "checkbox" should be visible
And I should see "Badge issuer email address"
And I should see "Badge issuer password"
And I set the field "backpackemail" to "test@test.com"
And I set the field "password" to "123456"
And I set the field "backpackapiurl" to "http://backpackapiurl.cat"
And I press "Save changes"
And I click on "Edit" "link" in the "http://backpackweburl.cat" "table_row"
And the field "Include authentication details with the backpack" matches value "1"
And I click on "includeauthdetails" "checkbox"
And I press "Save changes"
And I click on "Edit" "link" in the "http://backpackweburl.cat" "table_row"
And the field "Include authentication details with the backpack" matches value "0"
And I click on "includeauthdetails" "checkbox"
And I should not see "test@test.com"
@javascript
Scenario: View backpack form as a student
Given I log in as "student1"
And I follow "Preferences" in the user menu
And I follow "Backpack settings"
When I set the field "externalbackpackid" to "https://dc.imsglobal.org"
Then I should not see "Log in to your backpack"
And I should not see "Email address"
And I should not see "Password"
But I set the field "externalbackpackid" to "https://test.com/"
And I should see "Log in to your backpack"
And I should see "Email address"
And I should see "Password"
@javascript
Scenario: Check backpack form validation as a student
Given I log in as "student1"
And I follow "Preferences" in the user menu
And I follow "Backpack settings"
When I click on "Connect to backpack" "button"
Then I should see "Backpack provider can't be blank"
And I set the field "externalbackpackid" to "https://test.com/"
And I set the field "password" to ""
When I click on "Connect to backpack" "button"
Then I should see "Password can't be blank"
And I should not see "Email address can't be blank"
And I set the field "backpackemail" to ""
And I click on "Connect to backpack" "button"
And I should see "Email address can't be blank"
And I should see "Password can't be blank"
Binary file not shown.

After

Width:  |  Height:  |  Size: 2.1 KiB

+152
View File
@@ -0,0 +1,152 @@
@core @core_badges @javascript
Feature: Test tertiary navigation as various users
Background:
Given the following "users" exist:
| username | firstname | lastname | email |
| student1 | Student | 1 | student1@example.com |
| teacher | Teacher | 1 | teacher@example.com |
| nonediting | Nonediting | 1 | nonediting@example.com |
And the following "courses" exist:
| fullname | shortname | format | enablecompletion |
| Course 1 | C1 | topics | 1 |
And the following "course enrolments" exist:
| user | course | role |
| teacher | C1 | editingteacher |
| student1 | C1 | student |
| nonediting | C1 | teacher |
# Create a course badge in our test course.
And the following "core_badges > Badge" exists:
| name | Testing course badge |
| status | inactive |
| type | 2 |
| course | C1 |
| version | 1.1 |
| language | ca |
| description | Testing course badge description |
| image | badges/tests/behat/badge.png |
| imageauthorurl | http://author.example.com |
| imagecaption | Test caption image |
# Create a site badge.
And the following "core_badges > Badge" exists:
| name | Testing site badge |
| status | inactive |
| version | 1.1 |
| language | ca |
| description | Testing site badge description |
| image | badges/tests/behat/badge.png |
| imageauthorurl | http://author.example.com |
| imagecaption | Test caption image |
And the following "core_badges > Criterias" exist:
| badge | role |
| Testing course badge | editingteacher |
| Testing site badge | editingteacher |
Scenario Outline: Check navigation as different users in a course context
Given I log in as "<user>"
And I am on "Course 1" course homepage
When I navigate to "Badges" in current page administration
Then "Manage badges" "button" should exist
And "Add a new badge" "button" <createbutton>
And I should see "<activetab>" is active in secondary navigation
And I click on "Manage badges" "button"
And "Manage badges" "button" should not exist
And "Back" "button" should exist
And "Add a new badge" "button" <createbutton>
And I should see "<activetab>" is active in secondary navigation
And I click on "Back" "button"
And "Back" "button" should not exist
And "Manage badges" "button" should exist
And "Add a new badge" "button" <createbutton>
And I should see "<activetab>" is active in secondary navigation
And I click on "Manage badges" "button"
And I click on "Testing course badge" "link"
And "Manage badges" "button" should not exist
And "Add a new badge" "button" should not exist
And "Back" "button" should exist
And I should see "<activetab>" is active in secondary navigation
And I click on "Back" "button"
And "Back" "button" should exist
And "Manage badges" "button" should not exist
And "Add a new badge" "button" <createbutton>
And I should see "<activetab>" is active in secondary navigation
Examples:
| user | createbutton | activetab |
| admin | should exist | More |
| teacher | should exist | More |
| nonediting | should not exist | Badges |
Scenario: Check navigation as a student
Given I am on the "C1" "Course" page logged in as "student1"
And "Badges" "link" should not exist in current page administration
And I log out
# Enable one badge.
When I am on the "C1" "Course" page logged in as "admin"
And I navigate to "Badges" in current page administration
And I click on "Manage badges" "button"
And I press "Enable access" action in the "Testing course badge" report row
And I press "Continue"
And I log out
# Now student should see the Badges link.
And I am on the "C1" "Course" page logged in as "student1"
Then "Badges" "link" should exist in current page administration
And I navigate to "Badges" in current page administration
And "Back" "button" should not exist
And "Manage badges" "button" should not exist
And "Add a new badge" "button" should not exist
And I should see "Badges" is active in secondary navigation
Scenario: Check navigation as an admin in a site context
Given I log in as "admin"
When I navigate to "Badges > Manage badges" in site administration
Then "Manage badges" "button" should not exist
And "Add a new badge" "button" should exist
And I should see "General" is active in secondary navigation
And I click on "Testing site badge" "link"
And "Manage badges" "button" should not exist
And "Add a new badge" "button" should not exist
And "Back" "button" should exist
And I should see "General" is active in secondary navigation
And I click on "Back" "button"
And "Back" "button" should not exist
And "Manage badges" "button" should not exist
And "Add a new badge" "button" should exist
Scenario Outline: Check secondary navigation highlights after tertiary nav jumps in site admin
Given I log in as "admin"
And I navigate to "Badges > Manage badges" in site administration
And I click on "Testing site badge" "link"
When I select "<option>" from the "jump" singleselect
Then I should see "General" is active in secondary navigation
Examples:
| option |
| Overview |
| Edit details |
| Criteria |
| Message |
| Recipients (0) |
| Endorsement |
| Related badges (0) |
| Alignments (0) |
Scenario Outline: Check secondary navigation highlights after tertiary nav jumps in course as admin
Given I log in as "admin"
And I am on "Course 1" course homepage
And I navigate to "Badges" in current page administration
And I click on "Manage badges" "button"
And I click on "Testing course badge" "link"
When I select "<option>" from the "jump" singleselect
Then I should see "More" is active in secondary navigation
Examples:
| option |
| Overview |
| Edit details |
| Criteria |
| Message |
| Recipients (0) |
| Endorsement |
| Related badges (0) |
| Alignments (0) |
@@ -0,0 +1,127 @@
@mod @mod_quiz @core @core_badges @core_completion @javascript
Feature: Award badges based on activity completion
In order to ensure a student has learned the material before being marked complete
As a teacher
I need to configure an activity to grant a badge only when the student achieves a passing grade upon completion.
Background:
Given the following "users" exist:
| username | firstname | lastname | email |
| student1 | Student | 1 | student1@example.com |
| student2 | Student | 2 | student2@example.com |
| teacher1 | Teacher | 1 | teacher1@example.com |
And the following "courses" exist:
| fullname | shortname | category | enablecompletion |
| Course 1 | C1 | 0 | 1 |
And the following "course enrolments" exist:
| user | course | role |
| teacher1 | C1 | editingteacher |
| student1 | C1 | student |
| student2 | C1 | student |
And the following "question categories" exist:
| contextlevel | reference | name |
| Course | C1 | Test questions |
And the following "questions" exist:
| questioncategory | qtype | name | questiontext |
| Test questions | truefalse | First question | Answer the first question |
And the following "activities" exist:
| activity | name | course | idnumber | attempts | gradepass | completion | completionpassgrade | completionusegrade |
| quiz | Test quiz name 1 | C1 | quiz1 | 2 | 5.00 | 2 | 1 | 1 |
| quiz | Test quiz name 2 | C1 | quiz2 | 2 | 5.00 | 2 | 0 | 1 |
And quiz "Test quiz name 1" contains the following questions:
| question | page |
| First question | 1 |
And quiz "Test quiz name 2" contains the following questions:
| question | page |
| First question | 1 |
And the following "core_badges > Badge" exists:
| name | Course Badge 1 |
| status | 0 |
| type | 2 |
| course | C1 |
| description | Course badge 1 description |
| image | badges/tests/behat/badge.png |
And the following "core_badges > Badge" exists:
| name | Course Badge 2 |
| status | 0 |
| type | 2 |
| course | C1 |
| description | Course badge 2 description |
| image | badges/tests/behat/badge.png |
And I am on the "Course 1" course page logged in as teacher1
Scenario: Student does not earn a badge using activity completion when does not get passing grade
Given I navigate to "Badges" in current page administration
And I press "Manage badges"
And I follow "Course Badge 1"
And I select "Criteria" from the "jump" singleselect
And I set the field "type" to "Activity completion"
And I set the field "Quiz - Test quiz name 1" to "1"
And I press "Save"
And I press "Enable access"
And I press "Continue"
And I should see "Recipients (0)"
# Pass grade for student1. Activity is considered complete because student1 got a passing grade.
And user "student1" has attempted "Test quiz name 1" with responses:
| slot | response |
| 1 | True |
# Fail grade for student2. Activity is considered incomplete because student2 got a failing grade.
And user "student2" has attempted "Test quiz name 1" with responses:
| slot | response |
| 1 | False |
And I navigate to "Badges > Manage badges" in current page administration
And I follow "Course Badge 1"
Then I should see "Recipients (1)"
And I select "Recipients (1)" from the "jump" singleselect
And I should see "Student 1"
And I should not see "Student 2"
Scenario: Students with any grades in an activity will receive a badge if the completion condition is set to receive any grade
Given I navigate to "Badges" in current page administration
And I press "Manage badges"
And I follow "Course Badge 2"
And I select "Criteria" from the "jump" singleselect
And I set the field "type" to "Activity completion"
And I set the field "Quiz - Test quiz name 2" to "1"
And I press "Save"
And I press "Enable access"
And I press "Continue"
# Pass grade for student1.
And user "student1" has attempted "Test quiz name 2" with responses:
| slot | response |
| 1 | True |
# Fail grade for student2. Activity is considered complete even if student2 got a failing grade.
And user "student2" has attempted "Test quiz name 2" with responses:
| slot | response |
| 1 | False |
And I navigate to "Badges > Manage badges" in current page administration
And I follow "Course Badge 2"
Then I should see "Recipients (2)"
And I select "Recipients (2)" from the "jump" singleselect
And I should see "Student 1"
And I should see "Student 2"
Scenario: Previously graded pass/fail students should earn a badge after enabling a badge
# Pass grade for student1.
Given user "student1" has attempted "Test quiz name 1" with responses:
| slot | response |
| 1 | True |
# Fail grade for student2.
And user "student2" has attempted "Test quiz name 1" with responses:
| slot | response |
| 1 | False |
And I navigate to "Badges" in current page administration
And I press "Manage badges"
And I follow "Course Badge 1"
And I select "Criteria" from the "jump" singleselect
And I set the field "type" to "Activity completion"
And I set the field "Quiz - Test quiz name 1" to "1"
And I press "Save"
# Enable badge access once students have completed the activity.
When I press "Enable access"
And I press "Continue"
# Only student1 should earn the badge because student2 did not pass the quiz.
Then I should see "Recipients (1)"
And I select "Recipients (1)" from the "jump" singleselect
And I should see "Student 1"
And I should not see "Student 2"
+467
View File
@@ -0,0 +1,467 @@
@core @core_badges
Feature: Award badges based on cohort
In order to award badges to users based on their cohort membership
As an admin
I need to add cohort criteria to badges in the system
@javascript
Scenario: Award cohort membership badge for a member of a single cohort.
Given the following "cohorts" exist:
| name | idnumber |
| One Cohort | CH1 |
| Two Cohort | CH2 |
And the following "users" exist:
| username | firstname | lastname | email |
| user1 | First | User | first@example.com |
| user2 | Second | User | second@example.com |
And the following "cohort members" exist:
| user | cohort |
| user1 | CH1 |
| user2 | CH2 |
And the following "core_badges > Badge" exists:
| name | Site Badge |
| status | 0 |
| description | Site badge description |
| image | badges/tests/behat/badge.png |
And I log in as "admin"
And I navigate to "Badges > Manage badges" in site administration
And I press "Edit" action in the "Site Badge" report row
And I select "Criteria" from the "jump" singleselect
And I set the field "type" to "Cohort membership"
And I set the field "id_cohort_cohorts" to "One Cohort"
And I press "Save"
And I press "Enable access"
When I press "Continue"
Then I should see "Recipients (1)"
And I log out
And I log in as "user1"
And I follow "Profile" in the user menu
And I should see "Site Badge"
@javascript
Scenario: Award cohort membership badge for a member of all required cohorts.
Given the following "cohorts" exist:
| name | idnumber |
| One Cohort | CH1 |
| Two Cohort | CH2 |
| Three Cohort | CH3 |
And the following "users" exist:
| username | firstname | lastname | email |
| user1 | First | User | first@example.com |
| user2 | Second | User | second@example.com |
And the following "cohort members" exist:
| user | cohort |
| user1 | CH1 |
| user1 | CH2 |
| user2 | CH1 |
| user2 | CH3 |
And the following "core_badges > Badge" exists:
| name | Site Badge |
| status | 0 |
| description | Site badge description |
| image | badges/tests/behat/badge.png |
And I log in as "admin"
And I navigate to "Badges > Manage badges" in site administration
And I press "Edit" action in the "Site Badge" report row
And I select "Criteria" from the "jump" singleselect
And I set the field "type" to "Cohort membership"
And I expand all fieldsets
And I set the field "id_cohort_cohorts" to "One Cohort,Two Cohort"
And I set the field "id_agg_1" to "1"
And I press "Save"
When I press "Enable access"
And I press "Continue"
Then I should see "Recipients (1)"
And I log out
And I log in as "user1"
And I follow "Profile" in the user menu
And I should see "Site Badge"
@javascript
Scenario: Award cohort membership badge for a member of any required cohorts.
Given the following "cohorts" exist:
| name | idnumber |
| One Cohort | CH1 |
| Two Cohort | CH2 |
| Three Cohort | CH3 |
And the following "users" exist:
| username | firstname | lastname | email |
| user1 | First | User | first@example.com |
| user2 | Second | User | second@example.com |
| user3 | third | User | third@example.com |
And the following "cohort members" exist:
| user | cohort |
| user1 | CH1 |
| user1 | CH2 |
| user2 | CH1 |
| user2 | CH3 |
| user3 | CH2 |
| user3 | CH3 |
And the following "core_badges > Badge" exists:
| name | Site Badge |
| status | 0 |
| description | Site badge description |
| image | badges/tests/behat/badge.png |
And I log in as "admin"
And I navigate to "Badges > Manage badges" in site administration
And I press "Edit" action in the "Site Badge" report row
And I select "Criteria" from the "jump" singleselect
And I set the field "type" to "Cohort membership"
And I set the field "id_cohort_cohorts" to "One Cohort"
And I press "Save"
When I press "Enable access"
And I press "Continue"
Then I should see "Recipients (2)"
And I log out
And I log in as "user1"
And I follow "Profile" in the user menu
And I should see "Site Badge"
And I log out
And I log in as "user2"
And I follow "Profile" in the user menu
And I should see "Site Badge"
@javascript
Scenario: Award badge based on a single cohort membership and other criteria.
Given the following "cohorts" exist:
| name | idnumber |
| One Cohort | CH1 |
| Two Cohort | CH2 |
And the following "users" exist:
| username | firstname | lastname | email |
| user1 | First | User | first@example.com |
| user2 | Second | User | second@example.com |
And the following "cohort members" exist:
| user | cohort |
| user1 | CH1 |
| user2 | CH2 |
And the following "core_badges > Badge" exists:
| name | Site Badge |
| status | 0 |
| description | Site badge description |
| image | badges/tests/behat/badge.png |
And I log in as "admin"
And I navigate to "Badges > Manage badges" in site administration
And I press "Edit" action in the "Site Badge" report row
And I select "Criteria" from the "jump" singleselect
And I set the field "type" to "Cohort membership"
And I set the field "id_cohort_cohorts" to "One Cohort"
And I press "Save"
And I set the field "type" to "Manual issue by role"
And I expand all fieldsets
And I set the field "Manager" to "1"
And I set the field "Any of the selected roles awards the badge" to "1"
And I press "Save"
When I press "Enable access"
And I press "Continue"
And I select "Recipients (0)" from the "jump" singleselect
And I press "Award badge"
And I set the field "potentialrecipients[]" to "First User (first@example.com)"
And I press "Award badge"
And I set the field "potentialrecipients[]" to "Second User (second@example.com)"
And I press "Award badge"
And I navigate to "Badges > Manage badges" in site administration
And I follow "Site Badge"
Then I should see "Recipients (1)"
And I log out
And I log in as "user1"
And I follow "Profile" in the user menu
And I should see "Site Badge"
And I log out
And I log in as "user2"
And I follow "Profile" in the user menu
And I should not see "Site Badge"
@javascript
Scenario: Award badge based on a single cohort membership or other criteria.
Given the following "cohorts" exist:
| name | idnumber |
| One Cohort | CH1 |
| Two Cohort | CH2 |
And the following "users" exist:
| username | firstname | lastname | email |
| user1 | First | User | first@example.com |
| user2 | Second | User | second@example.com |
| user3 | Third | User | third@example.com |
And the following "cohort members" exist:
| user | cohort |
| user1 | CH1 |
| user2 | CH2 |
| user3 | CH2 |
And the following "core_badges > Badge" exists:
| name | Site Badge |
| status | 0 |
| description | Site badge description |
| image | badges/tests/behat/badge.png |
And I log in as "admin"
And I navigate to "Badges > Manage badges" in site administration
And I press "Edit" action in the "Site Badge" report row
And I select "Criteria" from the "jump" singleselect
And I set the field "type" to "Cohort membership"
And I set the field "id_cohort_cohorts" to "One Cohort"
And I press "Save"
And I set the field "type" to "Manual issue by role"
And I expand all fieldsets
And I set the field "Manager" to "1"
And I set the field "Any of the selected roles awards the badge" to "1"
And I press "Save"
And I set the field "update" to "Any"
When I press "Enable access"
And I press "Continue"
And I select "Recipients (1)" from the "jump" singleselect
And I press "Award badge"
And I set the field "potentialrecipients[]" to "First User (first@example.com)"
And I press "Award badge"
And I set the field "potentialrecipients[]" to "Second User (second@example.com)"
And I press "Award badge"
And I navigate to "Badges > Manage badges" in site administration
And I follow "Site Badge"
Then I should see "Recipients (2)"
And I log out
And I log in as "user1"
And I follow "Profile" in the user menu
And I should see "Site Badge"
And I log out
And I log in as "user2"
And I follow "Profile" in the user menu
And I should see "Site Badge"
And I log out
And I log in as "user3"
And I follow "Profile" in the user menu
And I should not see "Site Badge"
@javascript
Scenario: Award badge based on a multiple cohort membership or other criteria.
Given the following "cohorts" exist:
| name | idnumber |
| One Cohort | CH1 |
| Two Cohort | CH2 |
And the following "users" exist:
| username | firstname | lastname | email |
| user1 | First | User | first@example.com |
| user2 | Second | User | second@example.com |
| user3 | third | User | third@example.com |
And the following "cohort members" exist:
| user | cohort |
| user1 | CH1 |
| user1 | CH2 |
| user2 | CH2 |
| user2 | CH2 |
And the following "core_badges > Badge" exists:
| name | Site Badge |
| status | 0 |
| description | Site badge description |
| image | badges/tests/behat/badge.png |
And I log in as "admin"
And I navigate to "Badges > Manage badges" in site administration
And I press "Edit" action in the "Site Badge" report row
And I select "Criteria" from the "jump" singleselect
And I set the field "type" to "Cohort membership"
And I set the field "id_cohort_cohorts" to "One Cohort"
And I press "Save"
And I set the field "type" to "Manual issue by role"
And I expand all fieldsets
And I set the field "Manager" to "1"
And I set the field "Any of the selected roles awards the badge" to "1"
And I press "Save"
And I set the field "update" to "Any"
When I press "Enable access"
And I press "Continue"
And I select "Recipients (1)" from the "jump" singleselect
And I press "Award badge"
And I set the field "potentialrecipients[]" to "First User (first@example.com)"
And I press "Award badge"
And I set the field "potentialrecipients[]" to "Second User (second@example.com)"
And I press "Award badge"
And I navigate to "Badges > Manage badges" in site administration
And I follow "Site Badge"
Then I should see "Recipients (2)"
And I log out
And I log in as "user1"
And I follow "Profile" in the user menu
And I should see "Site Badge"
And I log out
And I log in as "user2"
And I follow "Profile" in the user menu
And I should see "Site Badge"
And I log out
And I log in as "user3"
And I follow "Profile" in the user menu
And I should not see "Site Badge"
@javascript
Scenario: Award badge based on a multiple cohort membership and other criteria.
Given the following "cohorts" exist:
| name | idnumber |
| One Cohort | CH1 |
| Two Cohort | CH2 |
And the following "users" exist:
| username | firstname | lastname | email |
| user1 | First | User | first@example.com |
| user2 | Second | User | second@example.com |
| user3 | Third | User | third@example.com |
And the following "cohort members" exist:
| user | cohort |
| user1 | CH1 |
| user1 | CH2 |
| user2 | CH1 |
| user3 | CH2 |
And the following "core_badges > Badge" exists:
| name | Site Badge |
| status | 0 |
| description | Site badge description |
| image | badges/tests/behat/badge.png |
And I log in as "admin"
And I navigate to "Badges > Manage badges" in site administration
And I press "Edit" action in the "Site Badge" report row
And I select "Criteria" from the "jump" singleselect
And I set the field "type" to "Cohort membership"
And I expand all fieldsets
And I set the field "id_cohort_cohorts" to "One Cohort,Two Cohort"
And I set the field "id_agg_1" to "1"
And I press "Save"
And I set the field "type" to "Manual issue by role"
And I expand all fieldsets
And I set the field "Manager" to "1"
And I set the field "Any of the selected roles awards the badge" to "1"
And I press "Save"
And I set the field "update" to "All"
When I press "Enable access"
And I press "Continue"
And I select "Recipients (0)" from the "jump" singleselect
And I press "Award badge"
And I set the field "potentialrecipients[]" to "First User (first@example.com)"
And I press "Award badge"
And I set the field "potentialrecipients[]" to "Second User (second@example.com)"
And I press "Award badge"
And I navigate to "Badges > Manage badges" in site administration
And I follow "Site Badge"
Then I should see "Recipients (1)"
And I log out
And I log in as "user1"
And I follow "Profile" in the user menu
And I should see "Site Badge"
And I log out
And I log in as "user2"
And I follow "Profile" in the user menu
And I should not see "Site Badge"
And I log out
And I log in as "user3"
And I follow "Profile" in the user menu
And I should not see "Site Badge"
@javascript
Scenario: Award multiple badges based on single cohort membership
Given the following "cohorts" exist:
| name | idnumber |
| One Cohort | CH1 |
| Two Cohort | CH2 |
And the following "users" exist:
| username | firstname | lastname | email |
| user1 | First | User | first@example.com |
| user2 | Second | User | second@example.com |
| user3 | Third | User | third@example.com |
And the following "cohort members" exist:
| user | cohort |
| user1 | CH1 |
| user1 | CH2 |
| user2 | CH2 |
And the following "core_badges > Badges" exist:
| name | status | description | image |
| Site Badge 1 | 0 | Site badge description | badges/tests/behat/badge.png |
| Site Badge 2 | 0 | Site badge description | badges/tests/behat/badge.png |
And I log in as "admin"
And I navigate to "Badges > Manage badges" in site administration
And I press "Edit" action in the "Site Badge" report row
And I select "Criteria" from the "jump" singleselect
And I set the field "type" to "Cohort membership"
And I set the field "id_cohort_cohorts" to "One Cohort"
And I press "Save"
And I press "Enable access"
When I press "Continue"
And I should see "Recipients (1)"
And I navigate to "Badges > Manage badges" in site administration
And I press "Edit" action in the "Site Badge 2" report row
And I select "Criteria" from the "jump" singleselect
And I set the field "type" to "Cohort membership"
And I set the field "id_cohort_cohorts" to "Two Cohort"
And I press "Save"
And I press "Enable access"
And I press "Continue"
Then I should see "Recipients (2)"
And I log out
And I log in as "user1"
And I follow "Profile" in the user menu
And I should see "Site Badge 1"
And I should see "Site Badge 2"
And I log out
And I log in as "user2"
And I follow "Profile" in the user menu
And I should not see "Site Badge 1"
And I should see "Site Badge 2"
And I log out
And I log in as "user3"
And I follow "Profile" in the user menu
And I should not see "Site Badge 1"
And I should not see "Site Badge 2"
@javascript
Scenario: Award multiple badges based on multiple cohort memberships
Given the following "cohorts" exist:
| name | idnumber |
| One Cohort | CH1 |
| Two Cohort | CH2 |
| Three Cohort | CH3 |
And the following "users" exist:
| username | firstname | lastname | email |
| user1 | First | User | first@example.com |
| user2 | Second | User | second@example.com |
| user3 | Third | User | third@example.com |
And the following "cohort members" exist:
| user | cohort |
| user1 | CH1 |
| user1 | CH2 |
| user2 | CH2 |
| user2 | CH3 |
And the following "core_badges > Badges" exist:
| name | status | description | image |
| Site Badge 1 | 0 | Site badge description | badges/tests/behat/badge.png |
| Site Badge 2 | 0 | Site badge description | badges/tests/behat/badge.png |
And I log in as "admin"
And I navigate to "Badges > Manage badges" in site administration
And I press "Edit" action in the "Site Badge" report row
And I select "Criteria" from the "jump" singleselect
And I set the field "type" to "Cohort membership"
And I expand all fieldsets
And I set the field "id_cohort_cohorts" to "One Cohort,Two Cohort"
And I set the field "id_agg_1" to "1"
And I press "Save"
And I press "Enable access"
When I press "Continue"
And I should see "Recipients (1)"
And I navigate to "Badges > Manage badges" in site administration
And I press "Edit" action in the "Site Badge 2" report row
And I select "Criteria" from the "jump" singleselect
And I set the field "type" to "Cohort membership"
And I expand all fieldsets
And I set the field "id_cohort_cohorts" to "Three Cohort,Two Cohort"
And I set the field "id_agg_1" to "1"
And I press "Save"
And I press "Enable access"
And I press "Continue"
And I should see "Recipients (1)"
And I log out
And I log in as "user1"
And I follow "Profile" in the user menu
And I should see "Site Badge 1"
And I should not see "Site Badge 2"
And I log out
And I log in as "user2"
And I follow "Profile" in the user menu
And I should not see "Site Badge 1"
And I should see "Site Badge 2"
And I log out
And I log in as "user3"
And I follow "Profile" in the user menu
And I should not see "Site Badge 1"
And I should not see "Site Badge 2"
@@ -0,0 +1,200 @@
@core @core_badges
Feature: Award badges based on competency completion
In order to award badges to users based on competency completion
As an admin
I need to add competency completion criteria to badges in the system
Background: Setup the competency framework and the course
Given the following "users" exist:
| username | firstname | lastname | email |
| user1 | First | User | first@example.com |
And the following "courses" exist:
| fullname | shortname | category |
| Course 1 | C1 | 0 |
And the following "course enrolments" exist:
| user | course | role |
| user1 | C1 | student |
And the following lp "frameworks" exist:
| shortname | idnumber |
| Framework 1 | sc-y-2 |
And the following lp "competencies" exist:
| shortname | framework |
| comp1 | sc-y-2 |
| comp2 | sc-y-2 |
And I log in as "admin"
@javascript
Scenario: Award badge for completing a competency in a course
Given the following "core_badges > Badge" exists:
| name | Course Badge |
| status | 0 |
| type | 2 |
| course | C1 |
| description | Course badge description |
| image | badges/tests/behat/badge.png |
# Add a competency to the course
When I am on "Course 1" course homepage
And I navigate to "Competencies > Add competencies to course" in current page administration
And "Competency picker" "dialogue" should be visible
And I select "comp1" of the competency tree
And I click on "Add" "button" in the "Competency picker" "dialogue"
And I wait until the page is ready
And I click on "Edit" "link" in the "[data-region='configurecoursecompetencies']" "css_element"
And I wait until the page is ready
And I click on "Rating a competency only updates the competency in this course" "text"
And I click on "Save changes" "button" in the "Configure course competencies" "dialogue"
# Add a badge to the course
And I am on "Course 1" course homepage
And I change window size to "large"
And I navigate to "Badges" in current page administration
And I press "Manage badges"
And I follow "Course Badge"
And I select "Criteria" from the "jump" singleselect
# Set the competency as a criteria for the badge
And I set the field "type" to "Competencies"
When I open the autocomplete suggestions list
And I click on "ul[class='form-autocomplete-suggestions'] li" "css_element"
And I wait until the page is ready
And I press "Save"
And I wait until the page is ready
# Enable the badge
And I press "Enable access"
And I press "Continue"
# Rate the competency in the course
And I am on "Course 1" course homepage
And I navigate to "Competencies" in current page administration
And I click on "comp1" "link" in the "[data-region='coursecompetencies']" "css_element"
And I press "Rate"
And I set the following fields to these values:
| Rating | C |
And I click on "Rate" "button" in the "Rate" "dialogue"
And I should see "The competency rating was manually set in the course"
And I log out
# See if we got the badge
Then I log in as "user1"
And I follow "Profile" in the user menu
And I should see "Course Badge"
@javascript
Scenario: Award badge for completing a competency in the site
Given the following "core_badges > Badge" exists:
| name | Site Badge |
| status | 0 |
| description | Site badge description |
| image | badges/tests/behat/badge.png |
# Add a competency to the course
When I am on "Course 1" course homepage
And I navigate to "Competencies > Add competencies to course" in current page administration
And "Competency picker" "dialogue" should be visible
And I select "comp1" of the competency tree
And I click on "Add" "button" in the "Competency picker" "dialogue"
And I press "Add competencies to course"
And "Competency picker" "dialogue" should be visible
And I select "comp2" of the competency tree
And I click on "Add" "button" in the "Competency picker" "dialogue"
# Add a badge to the site
And I navigate to "Badges > Manage badges" in site administration
And I press "Edit" action in the "Site Badge" report row
And I select "Criteria" from the "jump" singleselect
# Set the competency as a criteria for the badge
And I set the field "type" to "Competencies"
And I press "Add competency"
And "Competency picker" "dialogue" should be visible
And I select "comp1" of the competency tree
And I click on "Add" "button" in the "Competency picker" "dialogue"
And I wait until the page is ready
And I press "Add competency"
And "Competency picker" "dialogue" should be visible
And I select "comp2" of the competency tree
And I click on "Add" "button" in the "Competency picker" "dialogue"
And I wait until the page is ready
And I press "Save"
# Enable the badge
And I wait until the page is ready
And I press "Enable access"
And I press "Continue"
# Rate the competency in the course
And I am on "Course 1" course homepage
And I navigate to "Competencies" in current page administration
And I click on "comp1" "link" in the "[data-region='coursecompetencies']" "css_element"
And I press "Rate"
And I set the following fields to these values:
| Rating | C |
And I click on "Rate" "button" in the "Rate" "dialogue"
And I should see "The competency rating was manually set in the course"
And I log out
# See if we got the badge
Then I log in as "user1"
And I follow "Profile" in the user menu
And I should see "Site Badge"
@javascript
Scenario: Award badge for completing all competencies in the site
Given the following "core_badges > Badge" exists:
| name | Site Badge |
| status | 0 |
| description | Site badge description |
| image | badges/tests/behat/badge.png |
# Add a competency to the course
When I am on "Course 1" course homepage
And I navigate to "Competencies > Add competencies to course" in current page administration
And "Competency picker" "dialogue" should be visible
And I select "comp1" of the competency tree
And I click on "Add" "button" in the "Competency picker" "dialogue"
And I press "Add competencies to course"
And "Competency picker" "dialogue" should be visible
And I select "comp2" of the competency tree
And I click on "Add" "button" in the "Competency picker" "dialogue"
# Add a badge to the site
And I navigate to "Badges > Manage badges" in site administration
And I press "Edit" action in the "Site Badge" report row
And I select "Criteria" from the "jump" singleselect
# Set the competency as a criteria for the badge
And I set the field "type" to "Competencies"
And I press "Add competency"
And "Competency picker" "dialogue" should be visible
And I select "comp1" of the competency tree
And I click on "Add" "button" in the "Competency picker" "dialogue"
And I wait until the page is ready
And I press "Add competency"
And "Competency picker" "dialogue" should be visible
And I select "comp2" of the competency tree
And I click on "Add" "button" in the "Competency picker" "dialogue"
And I wait until the page is ready
And I click on "This criterion is complete when" "link"
And I click on "All of the selected competencies have been completed" "radio"
And I press "Save"
# Enable the badge
And I wait until the page is ready
And I press "Enable access"
And I press "Continue"
# Rate the competency in the course
And I am on "Course 1" course homepage
And I navigate to "Competencies" in current page administration
And I click on "comp1" "link" in the "[data-region='coursecompetencies']" "css_element"
And I press "Rate"
And I set the following fields to these values:
| Rating | C |
And I click on "Rate" "button" in the "Rate" "dialogue"
And I should see "The competency rating was manually set in the course"
And I log out
# We should not get the badge yet.
Then I log in as "user1"
And I follow "Profile" in the user menu
And I should not see "Site Badge"
And I log out
# Rate the other competency.
And I log in as "admin"
And I am on "Course 1" course homepage
And I navigate to "Competencies" in current page administration
And I click on "comp2" "link" in the "[data-region='coursecompetencies']" "css_element"
And I press "Rate"
And I set the following fields to these values:
| Rating | C |
And I click on "Rate" "button" in the "Rate" "dialogue"
And I should see "The competency rating was manually set in the course"
And I log out
# See if we got the badge now.
Then I log in as "user1"
And I follow "Profile" in the user menu
And I should see "Site Badge"
@@ -0,0 +1,32 @@
@core @core_badges @_file_upload
Feature: Award badges based on user profile field
In order to award badges to users based on completion of their user profile
As an admin
I need to add profile completion criteria to badges in the system
@javascript
Scenario: Award badge for a uploading a profile picture.
Given the following "users" exist:
| username | firstname | lastname | email |
| user1 | First | User | first@example.com |
And the following "core_badges > Badge" exists:
| name | Site Badge |
| status | 0 |
| description | Site badge description |
| image | badges/tests/behat/badge.png |
And I log in as "admin"
And I navigate to "Badges > Manage badges" in site administration
And I press "Edit" action in the "Site Badge" report row
And I select "Criteria" from the "jump" singleselect
And I set the field "type" to "Profile completion"
And I set the field "id_field_picture" to "1"
And I press "Save"
And I press "Enable access"
And I press "Continue"
And I log out
When I log in as "user1"
And I follow "Profile" in the user menu
And I click on "Edit profile" "link" in the "region-main" "region"
And I upload "badges/tests/behat/badge.png" file to "New picture" filemanager
And I press "Update profile"
Then I should see "Site Badge"
@@ -0,0 +1,51 @@
@core @core_badges
Feature: Delete course badge already awarded
As a teacher
I can delete awarded course badge
Background:
Given the following "users" exist:
| username | firstname | lastname | email |
| teacher1 | Teacher | 1 | teacher1@example.com |
| student1 | Student | 1 | student1@example.com |
And the following "courses" exist:
| fullname | shortname |
| Course 1 | C1 |
And the following "course enrolments" exist:
| user | course | role |
| teacher1 | C1 | editingteacher |
| student1 | C1 | student |
@javascript
Scenario Outline: Delete course badge already awarded to student
Given the following "core_badges > Badges" exist:
| name | course | description | image | status | type |
| <badgename> | C1 | <badgename> description | badges/tests/behat/badge.png | active | 2 |
And the following "core_badges > Criterias" exist:
| badge | role |
| <badgename> | editingteacher |
And the following "core_badges > Issued badges" exist:
| badge | user |
| <badgename> | student1 |
When I am on the "Course 1" "enrolled users" page logged in as "teacher1"
And I click on "Student 1" "link"
Then I should see "<badgename>"
And I am on the "Course 1" course page
# Navigate to Manage Badges page in order to delete the badge
And I navigate to "Badges > Manage badges" in current page administration
# Delete the badge
And I press "Delete" action in the "<badgename>" report row
And I press "<deleteoption>"
And I am on the "Course 1" "enrolled users" page
And I click on "Student 1" "link"
# Confirm that Badge 1 is retained as awarded badge but Badge 2 is not
And I <visibility> see "<badgename>"
And I am on the "Course 1" course page
# Navigate to Badges page to confirm that no badges exist, hence, Manage badges would not exist
And I navigate to "Badges" in current page administration
# Confirm that badges are sucessfully deleted
And I should see "There are no matching badges available for users to earn"
Examples:
| badgename | deleteoption | visibility |
| Badge 1 | Delete and keep existing issued badges | should |
| Badge 2 | Delete and remove existing issued badges | should not |
+117
View File
@@ -0,0 +1,117 @@
@core @core_badges @javascript
Feature: Manage badges
In order to manage badges in the system
As an admin
I need to be able to edit, copy, enable/disable access, delete and award badges
Background:
Given the following "core_badges > Badge" exists:
| name | Badge #1 |
| status | 0 |
| version | 1.0 |
| language | en |
| description | Test badge description |
| image | badges/tests/behat/badge.png |
| imageauthorurl | http://author.example.com |
| imagecaption | Test caption image |
Scenario: Copy a badge
Given I log in as "admin"
And I navigate to "Badges > Manage badges" in site administration
And I press "Copy" action in the "Badge #1" report row
And I should see "Copy of Badge #1"
And I press "Save changes"
And I click on "Back" "button"
Then the following should exist in the "reportbuilder-table" table:
| Name | Version | Badge status |
| Badge #1 | 1.0 | Not available |
| Copy of Badge #1 | 1.0 | Not available |
Scenario: Edit a badge
Given I log in as "admin"
And I navigate to "Badges > Manage badges" in site administration
And I press "Edit" action in the "Badge #1" report row
And I set the following fields to these values:
| Name | New Badge #1 |
| Version | 1.1 |
And I press "Save changes"
And I click on "Back" "button"
Then the following should exist in the "reportbuilder-table" table:
| Name | Version | Badge status |
| New Badge #1 | 1.1 | Not available |
Scenario: Delete a badge
Given I log in as "admin"
And I navigate to "Badges > Manage badges" in site administration
And I press "Delete" action in the "Badge #1" report row
And I press "Delete and remove existing issued badges"
Then I should see "There are no matching badges available for users to earn"
Scenario Outline: Filter managed badges
Given the following "core_badges > Badges" exist:
| name | status | version | image |
| Badge #2 | 1 | 2.0 | badges/tests/behat/badge.png |
And I log in as "admin"
When I navigate to "Badges > Manage badges" in site administration
And I click on "Filters" "button"
And I set the following fields in the "<filter>" "core_reportbuilder > Filter" to these values:
| <filter> operator | Is equal to |
| <filter> value | <value> |
And I click on "Apply" "button" in the "[data-region='report-filters']" "css_element"
Then I should see "Filters applied"
And I should see "Badge #1" in the "Badges" "table"
And I should not see "Badge #2" in the "Badges" "table"
Examples:
| filter | value |
| Name | Badge #1 |
| Version | 1.0 |
| Badge status | Not available |
Scenario: Enable and disable access to a badge
Given I log in as "admin"
And I navigate to "Badges > Manage badges" in site administration
And I press "Edit" action in the "Badge #1" report row
And I select "Criteria" from the "jump" singleselect
And I set the field "type" to "Manual issue by role"
And I set the field "Manager" to "1"
And I press "Save"
And I navigate to "Badges > Manage badges" in site administration
And I press "Enable access" action in the "Badge #1" report row
And I should see "Changes in badge access"
And I press "Continue"
And I should see "Access to the badges was successfully enabled"
Then the following should exist in the "reportbuilder-table" table:
| Name | Badge status |
| Badge #1 | Available |
And I press "Disable access" action in the "Badge #1" report row
And I should see "Access to the badges was successfully disabled"
And the following should exist in the "reportbuilder-table" table:
| Name | Badge status |
| Badge #1 | Not available |
Scenario: Award a badge
Given the following "users" exist:
| username | firstname | lastname | email |
| user1 | User | One | user1@example.com |
When I log in as "admin"
And I navigate to "Badges > Manage badges" in site administration
And I press "Edit" action in the "Badge #1" report row
And I select "Criteria" from the "jump" singleselect
And I set the field "type" to "Manual issue by role"
And I set the field "Manager" to "1"
And I press "Save"
And I navigate to "Badges > Manage badges" in site administration
And I press "Enable access" action in the "Badge #1" report row
And I press "Continue"
And I press "Award badge" action in the "Badge #1" report row
And I set the field "potentialrecipients[]" to "Admin User (moodle@example.com),User One (user1@example.com)"
And I press "Award badge"
And I navigate to "Badges > Manage badges" in site administration
Then the following should exist in the "Badges" table:
| Name | Badge status | Recipients |
| Badge #1 | Available | 2 |
And I click on "2" "link" in the "Badge #1" "table_row"
And the following should exist in the "Recipients" table:
| -1- |
| Admin User |
| User One |
@@ -0,0 +1,105 @@
@core @core_badges @_file_upload @javascript
Feature: Manage badges is not shown when there are no existing badges.
Scenario: Check navigation at site level with no badges
Given I log in as "admin"
When I navigate to "Badges > Manage badges" in site administration
And I should see "There are no matching badges available for users to earn"
Then "Manage badges" "button" should not exist
Scenario: Check navigation at course level with no badges
Given the following "users" exist:
| username | firstname | lastname | email |
| teacher | Teacher | 1 | teacher@example.com |
And the following "courses" exist:
| fullname | shortname | format | enablecompletion |
| Course 1 | C1 | topics | 1 |
And the following "course enrolments" exist:
| user | course | role |
| teacher | C1 | editingteacher |
And I log in as "teacher"
And I am on "Course 1" course homepage
When I navigate to "Badges" in current page administration
Then "Manage badges" "button" should not exist
And I click on "Add a new badge" "button"
And I set the following fields to these values:
| Name | Testing course badge |
| Version | 1.1 |
| Language | Basque |
| Description | Testing course badge description |
| Image author | http://author.example.com |
| Image caption | Test caption image |
And I upload "badges/tests/behat/badge.png" file to "Image" filemanager
And I click on "Create badge" "button"
And I click on "Back" "button"
And I should see "Testing course badge"
And I click on "Back" "button"
And "Manage badges" "button" should exist
# Badge is not enabled so is not listed.
And I should not see "Testing course badge"
And I click on "Manage badges" "button"
And I press "Edit" action in the "Testing course badge" report row
And I click on "Add criteria" "button"
And I set the field "type" to "Manual issue by role"
And I expand all fieldsets
And I set the field "Teacher" to "1"
And I click on "Save" "button"
And I click on "Back" "button"
And I should see "Testing course badge"
And I click on "Back" "button"
And "Manage badges" "button" should exist
# Badge is not enabled yet so is not listed.
And I should not see "Testing course badge"
And I click on "Manage badges" "button"
And I press "Enable access" action in the "Testing course badge" report row
And I click on "Continue" "button"
And I should see "Testing course badge"
And I click on "Back" "button"
And "Manage badges" "button" should exist
# Badge is already enabled so is listed.
And I should see "Testing course badge"
Scenario: Check navigation at course level with no badges as a student
# Create a badge, but leave it not enabled for now.
Given the following "users" exist:
| username | firstname | lastname | email |
| student1 | Student | 1 | student1@example.com |
And the following "courses" exist:
| fullname | shortname | format | enablecompletion |
| Course 1 | C1 | topics | 1 |
And the following "course enrolments" exist:
| user | course | role |
| student1 | C1 | student |
And I am on the "C1" "Course" page logged in as "admin"
And I navigate to "Badges > Add a new badge" in current page administration
And I set the following fields to these values:
| Name | Testing course badge |
| Version | 1.0 |
| Language | Catalan |
| Description | Testing course badge description |
And I upload "badges/tests/behat/badge.png" file to "Image" filemanager
And I press "Create badge"
And I set the field "type" to "Manual issue by role"
And I expand all fieldsets
And I set the field "Teacher" to "1"
And I press "Save"
And I log out
When I am on the "C1" "Course" page logged in as "student1"
Then "Badges" "link" should not exist in current page administration
And I log out
# Enable the badge.
And I am on the "C1" "Course" page logged in as "admin"
And I navigate to "Badges" in current page administration
And I click on "Manage badges" "button"
And I press "Enable access" action in the "Testing course badge" report row
And I press "Continue"
And I log out
# Now student should see the Badges link.
And I am on the "C1" "Course" page logged in as "student1"
And I follow "Badges"
And "Manage badges" "button" should not exist
And "Add a new badge" "button" should not exist
And I should not see "There are no matching badges available for users to earn"
And the following should exist in the "Course badges" table:
| Name | Description | Criteria |
| Testing course badge | Testing course badge description | Awarded by: Teacher |
@@ -0,0 +1,47 @@
@core @core_badges
Feature: Test role visibility for the badge administration page
In order to control access
As an admin
I need to control which roles can see each other
Background: Add a bunch of users
Given the following "courses" exist:
| fullname | shortname |
| Course 1 | C1 |
And the following "users" exist:
| username | firstname | lastname | email |
| teacher1 | Teacher | 1 | teacher1@example.com |
| manager1 | Manager | 1 | manager1@example.com |
And the following "course enrolments" exist:
| user | course | role |
| teacher1 | C1 | editingteacher |
| manager1 | C1 | manager |
@javascript @_file_upload
Scenario: Check the default roles are visible
Given I log in as "manager1"
And I am on "Course 1" course homepage
And I navigate to "Badges > Add a new badge" in current page administration
And I set the following fields to these values:
| Name | Course Badge |
| Description | Course badge description |
And I upload "badges/tests/behat/badge.png" file to "Image" filemanager
And I press "Create badge"
And I set the field "type" to "Manual issue by role"
Then I should see "Teacher"
And I should see "Manager"
@javascript @_file_upload
Scenario: Check hidden roles are not visible
Given I log in as "teacher1"
And I am on "Course 1" course homepage
And I navigate to "Badges > Add a new badge" in current page administration
And I set the following fields to these values:
| Name | Course Badge |
| Description | Course badge description |
And I upload "badges/tests/behat/badge.png" file to "Image" filemanager
And I press "Create badge"
And I set the following fields to these values:
| Add badge criteria | Manual issue by role |
Then I should see "Teacher"
And I should not see "Manager"
+141
View File
@@ -0,0 +1,141 @@
@core @core_badges @_file_upload @javascript
Feature: Display badges
In order to access to badges information
As a user
I need to view badges data awarded to users
Background:
Given the following "users" exist:
| username | firstname | lastname | email |
| student1 | Student | 1 | student1@example.com |
# Create system badge and define a criterion.
And I log in as "admin"
And I navigate to "Badges > Add a new badge" in site administration
And I set the following fields to these values:
| Name | Testing system badge |
| Version | 1.1 |
| Language | Catalan |
| Description | Testing system badge description |
| Image author | http://author.example.com |
| Image caption | Test caption image |
And I upload "badges/tests/behat/badge.png" file to "Image" filemanager
And I press "Create badge"
And I set the field "type" to "Manual issue by role"
And I expand all fieldsets
And I set the field "Teacher" to "1"
And I press "Save"
Scenario: Display badge without expired date
# Enable the badge.
Given I press "Enable access"
And I press "Continue"
# Award badge to student1.
And I select "Recipients (0)" from the "jump" singleselect
And I press "Award badge"
And I set the field "potentialrecipients[]" to "Student 1 (student1@example.com)"
And I press "Award badge"
# Check badge details are displayed.
And I navigate to "Badges > Manage badges" in site administration
And I follow "Testing system badge"
And I select "Recipients (1)" from the "jump" singleselect
And I press "View issued badge" action in the "Student 1" report row
Then I should see "Awarded to Student 1"
And I should see "This badge has to be awarded by a user with the following role:"
And I should not see "Expired"
And I should not see "Expires"
And I follow "More details"
And I should see "Catalan"
And I should see "1.1"
Scenario: Display badge with ALL criteria
# Add another criterion and enable the badge.
Given I set the field "type" to "Profile completion"
And I set the field "id_field_firstname" to "1"
And I press "Save"
And I press "Enable access"
And I press "Continue"
# Award badge to student1.
And I select "Recipients (0)" from the "jump" singleselect
And I press "Award badge"
And I set the field "potentialrecipients[]" to "Student 1 (student1@example.com)"
And I press "Award badge"
# Check badge details are displayed.
And I navigate to "Badges > Manage badges" in site administration
And I follow "Testing system badge"
And I select "Recipients (1)" from the "jump" singleselect
And I press "View issued badge" action in the "Student 1" report row
Then I should see "Awarded to Student 1"
And I should see "Complete ALL of the listed requirements."
And I should see "This badge has to be awarded by a user with the following role:"
And I should see "The following user profile field has to be completed:"
And I should not see "Expired"
And I should not see "Expires"
And I follow "More details"
And I should see "Catalan"
And I should see "1.1"
Scenario: Display badge with ANY criteria
# Add another criterion and enable the badge.
Given I set the field "type" to "Profile completion"
And I set the field "id_field_firstname" to "1"
And I press "Save"
And I set the field "update" to "2"
And I press "Enable access"
And I press "Continue"
# Check badge details are displayed.
And I select "Recipients (2)" from the "jump" singleselect
And I press "View issued badge" action in the "Student 1" report row
Then I should see "Awarded to Student 1"
And I should see "Complete ANY of the listed requirements."
And I should see "This badge has to be awarded by a user with the following role:"
And I should see "The following user profile field has to be completed:"
And I should not see "Expired"
And I should not see "Expires"
And I follow "More details"
And I should see "Catalan"
And I should see "1.1"
Scenario: Display badge with expiration date but not expired yet
# Set expired date to badge (future date).
Given I select "Edit details" from the "jump" singleselect
When I click on "Relative date" "radio"
And I set the field "expireperiod[number]" to "1"
And I press "Save changes"
And I press "Enable access"
And I press "Continue"
# Award badge to student1.
And I select "Recipients (0)" from the "jump" singleselect
And I press "Award badge"
And I set the field "potentialrecipients[]" to "Student 1 (student1@example.com)"
And I press "Award badge"
# Check "Expires" date is displayed.
And I navigate to "Badges > Manage badges" in site administration
And I follow "Testing system badge"
And I select "Recipients (1)" from the "jump" singleselect
And I press "View issued badge" action in the "Student 1" report row
Then I should see "Expires"
And I should not see "Expired"
Scenario: Display expired badge
# Set expired date to badge (relative date 1 seconds after the date of issue it).
Given I select "Edit details" from the "jump" singleselect
When I click on "Relative date" "radio"
And I set the field "expireperiod[timeunit]" to "1"
And I set the field "expireperiod[number]" to "1"
And I press "Save changes"
And I press "Enable access"
And I press "Continue"
# Award badge to student1.
And I select "Recipients (0)" from the "jump" singleselect
And I press "Award badge"
And I set the field "potentialrecipients[]" to "Student 1 (student1@example.com)"
And I press "Award badge"
# Wait 1 second to guarantee the badge is expired.
And I wait "1" seconds
# Check "Expired" date is displayed.
And I navigate to "Badges > Manage badges" in site administration
And I follow "Testing system badge"
And I select "Recipients (1)" from the "jump" singleselect
And I press "View issued badge" action in the "Student 1" report row
Then I should see "Expired"
And I should not see "Expires"
+375
View File
@@ -0,0 +1,375 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Badge events tests.
*
* @package core_badges
* @copyright 2015 onwards Simey Lameze <simey@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->dirroot . '/badges/tests/badgeslib_test.php');
/**
* Badge events tests class.
*
* @package core_badges
* @copyright 2015 onwards Simey Lameze <simey@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class events_test extends badgeslib_test {
/**
* Test badge awarded event.
*/
public function test_badge_awarded(): void {
$systemcontext = context_system::instance();
$sink = $this->redirectEvents();
$badge = new badge($this->badgeid);
$badge->issue($this->user->id, true);
$badge->is_issued($this->user->id);
$events = $sink->get_events();
$this->assertCount(1, $events);
$event = reset($events);
$this->assertInstanceOf('\core\event\badge_awarded', $event);
$this->assertEquals($this->badgeid, $event->objectid);
$this->assertEquals($this->user->id, $event->relateduserid);
$this->assertEquals($systemcontext, $event->get_context());
$sink->close();
}
/**
* Test the badge created event.
*
* There is no external API for creating a badge, so the unit test will simply
* create and trigger the event and ensure data is returned as expected.
*/
public function test_badge_created(): void {
$badge = new badge($this->badgeid);
// Trigger an event: badge created.
$eventparams = array(
'userid' => $badge->usercreated,
'objectid' => $badge->id,
'context' => $badge->get_context(),
);
$event = \core\event\badge_created::create($eventparams);
// Trigger and capture the event.
$sink = $this->redirectEvents();
$event->trigger();
$events = $sink->get_events();
$event = reset($events);
// Check that the event data is valid.
$this->assertInstanceOf('\core\event\badge_created', $event);
$this->assertEquals($badge->usercreated, $event->userid);
$this->assertEquals($badge->id, $event->objectid);
$this->assertDebuggingNotCalled();
$sink->close();
}
/**
* Test the badge archived event.
*
*/
public function test_badge_archived(): void {
$badge = new badge($this->badgeid);
$sink = $this->redirectEvents();
// Trigger and capture the event.
$badge->delete(true);
$events = $sink->get_events();
$this->assertCount(2, $events);
$event = $events[1];
// Check that the event data is valid.
$this->assertInstanceOf('\core\event\badge_archived', $event);
$this->assertEquals($badge->id, $event->objectid);
$this->assertDebuggingNotCalled();
$sink->close();
}
/**
* Test the badge updated event.
*
*/
public function test_badge_updated(): void {
$badge = new badge($this->badgeid);
$sink = $this->redirectEvents();
// Trigger and capture the event.
$badge->save();
$events = $sink->get_events();
$event = reset($events);
$this->assertCount(1, $events);
// Check that the event data is valid.
$this->assertInstanceOf('\core\event\badge_updated', $event);
$this->assertEquals($badge->id, $event->objectid);
$this->assertDebuggingNotCalled();
$sink->close();
}
/**
* Test the badge deleted event.
*/
public function test_badge_deleted(): void {
$badge = new badge($this->badgeid);
$sink = $this->redirectEvents();
// Trigger and capture the event.
$badge->delete(false);
$events = $sink->get_events();
$event = reset($events);
$this->assertCount(1, $events);
// Check that the event data is valid.
$this->assertInstanceOf('\core\event\badge_deleted', $event);
$this->assertEquals($badge->id, $event->objectid);
$this->assertDebuggingNotCalled();
$sink->close();
}
/**
* Test the badge duplicated event.
*
*/
public function test_badge_duplicated(): void {
$badge = new badge($this->badgeid);
$sink = $this->redirectEvents();
// Trigger and capture the event.
$newid = $badge->make_clone();
$events = $sink->get_events();
$event = reset($events);
$this->assertCount(1, $events);
// Check that the event data is valid.
$this->assertInstanceOf('\core\event\badge_duplicated', $event);
$this->assertEquals($newid, $event->objectid);
$this->assertDebuggingNotCalled();
$sink->close();
}
/**
* Test the badge disabled event.
*
*/
public function test_badge_disabled(): void {
$badge = new badge($this->badgeid);
$sink = $this->redirectEvents();
// Trigger and capture the event.
$badge->set_status(BADGE_STATUS_INACTIVE);
$events = $sink->get_events();
$event = reset($events);
$this->assertCount(2, $events);
$event = $events[1];
// Check that the event data is valid.
$this->assertInstanceOf('\core\event\badge_disabled', $event);
$this->assertEquals($badge->id, $event->objectid);
$this->assertDebuggingNotCalled();
$sink->close();
}
/**
* Test the badge enabled event.
*
*/
public function test_badge_enabled(): void {
$badge = new badge($this->badgeid);
$sink = $this->redirectEvents();
// Trigger and capture the event.
$badge->set_status(BADGE_STATUS_ACTIVE);
$events = $sink->get_events();
$event = reset($events);
$this->assertCount(2, $events);
$event = $events[1];
// Check that the event data is valid.
$this->assertInstanceOf('\core\event\badge_enabled', $event);
$this->assertEquals($badge->id, $event->objectid);
$this->assertDebuggingNotCalled();
$sink->close();
}
/**
* Test the badge criteria created event.
*
* There is no external API for this, so the unit test will simply
* create and trigger the event and ensure data is returned as expected.
*/
public function test_badge_criteria_created(): void {
$badge = new badge($this->badgeid);
// Trigger and capture the event.
$sink = $this->redirectEvents();
$criteriaoverall = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_OVERALL, 'badgeid' => $badge->id));
$criteriaoverall->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ALL));
$criteriaprofile = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_PROFILE, 'badgeid' => $badge->id));
$params = array('agg' => BADGE_CRITERIA_AGGREGATION_ALL, 'field_address' => 'address');
$criteriaprofile->save($params);
$events = $sink->get_events();
$event = reset($events);
// Check that the event data is valid.
$this->assertCount(1, $events);
$this->assertInstanceOf('\core\event\badge_criteria_created', $event);
$this->assertEquals($criteriaprofile->id, $event->objectid);
$this->assertEquals($criteriaprofile->badgeid, $event->other['badgeid']);
$this->assertDebuggingNotCalled();
$sink->close();
}
/**
* Test the badge criteria updated event.
*
* There is no external API for this, so the unit test will simply
* create and trigger the event and ensure data is returned as expected.
*/
public function test_badge_criteria_updated(): void {
$criteriaoverall = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_OVERALL, 'badgeid' => $this->badgeid));
$criteriaoverall->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ALL));
$criteriaprofile = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_PROFILE, 'badgeid' => $this->badgeid));
$params = array('agg' => BADGE_CRITERIA_AGGREGATION_ALL, 'field_address' => 'address');
$criteriaprofile->save($params);
$badge = new badge($this->badgeid);
// Trigger and capture the event.
$sink = $this->redirectEvents();
$criteria = $badge->criteria[BADGE_CRITERIA_TYPE_PROFILE];
$params2 = array('agg' => BADGE_CRITERIA_AGGREGATION_ALL, 'field_address' => 'address', 'id' => $criteria->id);
$criteria->save((array)$params2);
$events = $sink->get_events();
$event = reset($events);
// Check that the event data is valid.
$this->assertCount(1, $events);
$this->assertInstanceOf('\core\event\badge_criteria_updated', $event);
$this->assertEquals($criteria->id, $event->objectid);
$this->assertEquals($this->badgeid, $event->other['badgeid']);
$this->assertDebuggingNotCalled();
$sink->close();
}
/**
* Test the badge criteria deleted event.
*
* There is no external API for this, so the unit test will simply
* create and trigger the event and ensure data is returned as expected.
*/
public function test_badge_criteria_deleted(): void {
$criteriaoverall = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_OVERALL, 'badgeid' => $this->badgeid));
$criteriaoverall->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ALL));
$badge = new badge($this->badgeid);
// Trigger and capture the event.
$sink = $this->redirectEvents();
$badge->criteria[BADGE_CRITERIA_TYPE_OVERALL]->delete();
$events = $sink->get_events();
$event = reset($events);
// Check that the event data is valid.
$this->assertCount(1, $events);
$this->assertInstanceOf('\core\event\badge_criteria_deleted', $event);
$this->assertEquals($criteriaoverall->badgeid, $event->other['badgeid']);
$this->assertDebuggingNotCalled();
$sink->close();
}
/**
* Test the badge viewed event.
*
* There is no external API for viewing a badge, so the unit test will simply
* create and trigger the event and ensure data is returned as expected.
*/
public function test_badge_viewed(): void {
$badge = new badge($this->badgeid);
// Trigger an event: badge viewed.
$other = array('badgeid' => $badge->id, 'badgehash' => '12345678');
$eventparams = array(
'context' => $badge->get_context(),
'other' => $other,
);
$event = \core\event\badge_viewed::create($eventparams);
// Trigger and capture the event.
$sink = $this->redirectEvents();
$event->trigger();
$events = $sink->get_events();
$event = reset($events);
// Check that the event data is valid.
$this->assertInstanceOf('\core\event\badge_viewed', $event);
$this->assertEquals('12345678', $event->other['badgehash']);
$this->assertEquals($badge->id, $event->other['badgeid']);
$this->assertDebuggingNotCalled();
$sink->close();
}
/**
* Test the badge listing viewed event.
*
* There is no external API for viewing a badge, so the unit test will simply
* create and trigger the event and ensure data is returned as expected.
*/
public function test_badge_listing_viewed(): void {
// Trigger an event: badge listing viewed.
$context = context_system::instance();
$eventparams = array(
'context' => $context,
'other' => array('badgetype' => BADGE_TYPE_SITE)
);
$event = \core\event\badge_listing_viewed::create($eventparams);
// Trigger and capture the event.
$sink = $this->redirectEvents();
$event->trigger();
$events = $sink->get_events();
$event = reset($events);
// Check that the event data is valid.
$this->assertInstanceOf('\core\event\badge_listing_viewed', $event);
$this->assertEquals(BADGE_TYPE_SITE, $event->other['badgetype']);
$this->assertDebuggingNotCalled();
$sink->close();
}
}
+283
View File
@@ -0,0 +1,283 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Badges external functions tests.
*
* @package core_badges
* @category external
* @copyright 2016 Juan Leyva <juan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since Moodle 3.1
*/
namespace core_badges\external;
use core_badges_external;
use core_external\external_api;
use core_external\external_settings;
use externallib_advanced_testcase;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->dirroot . '/webservice/tests/helpers.php');
require_once($CFG->libdir . '/badgeslib.php');
/**
* Badges external functions tests
*
* @package core_badges
* @category external
* @copyright 2016 Juan Leyva <juan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since Moodle 3.1
*/
class external_test extends externallib_advanced_testcase {
/** @var stdClass $course */
private $course;
/** @var stdClass $student */
private $student;
/** @var stdClass $teacher */
private $teacher;
/**
* Set up for every test
*/
public function setUp(): void {
global $DB;
$this->resetAfterTest();
$this->setAdminUser();
// Setup test data.
$this->course = $this->getDataGenerator()->create_course();
// Create users and enrolments.
$this->student = $this->getDataGenerator()->create_and_enrol($this->course, 'student');
$this->teacher = $this->getDataGenerator()->create_and_enrol($this->course, 'editingteacher');
// Mock up a site badge.
$now = time();
$badge = new \stdClass();
$badge->id = null;
$badge->name = "Test badge site";
$badge->description = "Testing badges site";
$badge->timecreated = $now - 12;
$badge->timemodified = $now - 12;
$badge->usercreated = $this->teacher->id;
$badge->usermodified = $this->teacher->id;
$badge->issuername = "Test issuer";
$badge->issuerurl = "http://issuer-url.domain.co.nz";
$badge->issuercontact = "issuer@example.com";
$badge->expiredate = null;
$badge->expireperiod = null;
$badge->type = BADGE_TYPE_SITE;
$badge->courseid = null;
$badge->messagesubject = "Test message subject for badge";
$badge->message = "Test message body for badge";
$badge->attachment = 1;
$badge->notification = 0;
$badge->status = BADGE_STATUS_ACTIVE;
$badge->version = '1';
$badge->language = 'en';
$badge->imageauthorname = 'Image author';
$badge->imageauthoremail = 'imageauthor@example.com';
$badge->imageauthorurl = 'http://image-author-url.domain.co.nz';
$badge->imagecaption = 'Caption';
$badgeid = $DB->insert_record('badge', $badge, true);
$badge = new \badge($badgeid);
$badge->issue($this->student->id, true);
// Hack the database to adjust the time each badge was issued.
$DB->set_field('badge_issued', 'dateissued', $now - 11, array('userid' => $this->student->id, 'badgeid' => $badgeid));
// Add an endorsement for the badge.
$endorsement = new \stdClass();
$endorsement->badgeid = $badgeid;
$endorsement->issuername = 'Issuer name';
$endorsement->issuerurl = 'http://endorsement-issuer-url.domain.co.nz';
$endorsement->issueremail = 'endorsementissuer@example.com';
$endorsement->claimid = 'http://claim-url.domain.co.nz';
$endorsement->claimcomment = 'Claim comment';
$endorsement->dateissued = $now;
$badge->save_endorsement($endorsement);
// Add 2 alignments.
$alignment = new \stdClass();
$alignment->badgeid = $badgeid;
$alignment->targetname = 'Alignment 1';
$alignment->targeturl = 'http://a1-target-url.domain.co.nz';
$alignment->targetdescription = 'A1 target description';
$alignment->targetframework = 'A1 framework';
$alignment->targetcode = 'A1 code';
$badge->save_alignment($alignment);
$alignment->targetname = 'Alignment 2';
$alignment->targeturl = 'http://a2-target-url.domain.co.nz';
$alignment->targetdescription = 'A2 target description';
$alignment->targetframework = 'A2 framework';
$alignment->targetcode = 'A2 code';
$badge->save_alignment($alignment);
// Now a course badge.
$badge->id = null;
$badge->name = "Test badge course";
$badge->description = "Testing badges course";
$badge->type = BADGE_TYPE_COURSE;
$badge->courseid = $this->course->id;
$coursebadgeid = $DB->insert_record('badge', $badge, true);
$badge = new \badge($coursebadgeid);
$badge->issue($this->student->id, true);
// Hack the database to adjust the time each badge was issued.
$DB->set_field('badge_issued', 'dateissued', $now - 10, array('userid' => $this->student->id, 'badgeid' => $coursebadgeid));
// Make the site badge a related badge.
$badge->add_related_badges(array($badgeid));
}
/**
* Test get user badges.
* These is a basic test since the badges_get_my_user_badges used by the external function already has unit tests.
*/
public function test_get_my_user_badges(): void {
$this->setUser($this->student);
$badges = (array) badges_get_user_badges($this->student->id);
$expectedbadges = array();
$coursebadge = null;
foreach ($badges as $badge) {
$context = ($badge->type == BADGE_TYPE_SITE) ? \context_system::instance() : \context_course::instance($badge->courseid);
$badge->badgeurl = \moodle_url::make_webservice_pluginfile_url($context->id, 'badges', 'badgeimage', $badge->id, '/',
'f3')->out(false);
// Get the endorsement, alignments and related badges.
$badgeinstance = new \badge($badge->id);
$endorsement = $badgeinstance->get_endorsement();
$alignments = $badgeinstance->get_alignments();
$relatedbadges = $badgeinstance->get_related_badges();
$badge->alignment = array();
$badge->relatedbadges = array();
if ($endorsement) {
$badge->endorsement = (array) $endorsement;
}
if (!empty($alignments)) {
foreach ($alignments as $alignment) {
// Students cannot see some fields of the alignments.
unset($alignment->targetname);
unset($alignment->targeturl);
unset($alignment->targetdescription);
unset($alignment->targetframework);
unset($alignment->targetcode);
$badge->alignment[] = (array) $alignment;
}
}
if (!empty($relatedbadges)) {
foreach ($relatedbadges as $relatedbadge) {
// Students cannot see some fields of the related badges.
unset($relatedbadge->version);
unset($relatedbadge->language);
unset($relatedbadge->type);
$badge->relatedbadges[] = (array) $relatedbadge;
}
}
$expectedbadges[] = (array) $badge;
if (isset($badge->courseid)) {
// Save the course badge to be able to compare it in our tests.
$coursebadge = (array) $badge;
}
}
$result = core_badges_external::get_user_badges();
$result = external_api::clean_returnvalue(core_badges_external::get_user_badges_returns(), $result);
$this->assertEquals($expectedbadges, $result['badges']);
// Pagination and filtering.
$result = core_badges_external::get_user_badges(0, $this->course->id, 0, 1, '', true);
$result = external_api::clean_returnvalue(core_badges_external::get_user_badges_returns(), $result);
$this->assertCount(1, $result['badges']);
$this->assertEquals($coursebadge, $result['badges'][0]);
}
/**
* Test get user badges.
*/
public function test_get_other_user_badges(): void {
$this->setUser($this->teacher);
$result = core_badges_external::get_user_badges($this->student->id);
$result = external_api::clean_returnvalue(core_badges_external::get_user_badges_returns(), $result);
$this->assertCount(2, $result['badges']);
// Check that we don't have permissions for view the complete information for site badges.
foreach ($result['badges'] as $badge) {
if (isset($badge['type']) and $badge['type'] == BADGE_TYPE_COURSE) {
$this->assertTrue(isset($badge['message']));
// Check that we have permissions to see all the data in alignments and related badges.
foreach ($badge['alignment'] as $alignment) {
$this->assertTrue(isset($alignment['targetdescription']));
}
foreach ($badge['relatedbadges'] as $relatedbadge) {
$this->assertTrue(isset($relatedbadge['type']));
}
} else {
$this->assertFalse(isset($badge['message']));
}
}
}
/**
* Test get_user_badges where issuername contains text to be filtered
*/
public function test_get_user_badges_filter_issuername(): void {
global $DB;
filter_set_global_state('multilang', TEXTFILTER_ON);
filter_set_applies_to_strings('multilang', true);
external_settings::get_instance()->set_filter(true);
// Update issuer name of test badge.
$issuername = '<span class="multilang" lang="en">Issuer (en)</span><span class="multilang" lang="es">Issuer (es)</span>';
$DB->set_field('badge', 'issuername', $issuername, ['name' => 'Test badge site']);
// Retrieve student badges.
$result = core_badges_external::get_user_badges($this->student->id);
$result = external_api::clean_returnvalue(core_badges_external::get_user_badges_returns(), $result);
// Site badge will be last, because it has the earlier issued date.
$badge = end($result['badges']);
$this->assertEquals('Issuer (en)', $badge['issuername']);
}
}
+247
View File
@@ -0,0 +1,247 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace core_badges\external;
use externallib_advanced_testcase;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->dirroot . '/webservice/tests/helpers.php');
require_once($CFG->libdir . '/badgeslib.php');
/**
* Tests for external function get_user_badge_by_hash.
*
* @package core_badges
* @category external
* @copyright 2023 Rodrigo Mady <rodrigo.mady@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since Moodle 4.3
* @coversDefaultClass \core_badges\external\get_user_badge_by_hash
*/
class get_user_badge_by_hash_test extends externallib_advanced_testcase {
/**
* Prepare the test.
*
* @return array
*/
private function prepare_test_data(): array {
global $DB;
$this->resetAfterTest();
$this->setAdminUser();
// Setup test data.
$course = $this->getDataGenerator()->create_course();
// Create users and enrolments.
$student1 = $this->getDataGenerator()->create_and_enrol($course);
$student2 = $this->getDataGenerator()->create_and_enrol($course);
$teacher = $this->getDataGenerator()->create_and_enrol($course, 'editingteacher');
// Mock up a site badge.
$now = time();
$badge = new \stdClass();
$badge->id = null;
$badge->name = "Test badge site";
$badge->description = "Testing badges site";
$badge->timecreated = $now;
$badge->timemodified = $now;
$badge->usercreated = (int) $teacher->id;
$badge->usermodified = (int) $teacher->id;
$badge->expiredate = null;
$badge->expireperiod = null;
$badge->type = BADGE_TYPE_SITE;
$badge->courseid = null;
$badge->messagesubject = "Test message subject for badge";
$badge->message = "Test message body for badge";
$badge->attachment = 1;
$badge->notification = 0;
$badge->status = BADGE_STATUS_ACTIVE;
$badge->version = '1';
$badge->language = 'en';
$badge->imageauthorname = 'Image author';
$badge->imageauthoremail = 'imageauthor@example.com';
$badge->imageauthorurl = 'http://image-author-url.domain.co.nz';
$badge->imagecaption = 'Caption';
$badgeid = $DB->insert_record('badge', $badge, true);
$badge->id = $badgeid;
$sitebadge = new \badge($badgeid);
$sitebadge->issue($student1->id, true);
$siteissuedbadge = $DB->get_record('badge_issued', [ 'badgeid' => $badge->id ]);
$badge->issuername = $sitebadge->issuername;
$badge->issuercontact = $sitebadge->issuercontact;
$badge->issuerurl = $sitebadge->issuerurl;
$badge->nextcron = $sitebadge->nextcron;
$badge->issuedid = (int) $siteissuedbadge->id;
$badge->uniquehash = $siteissuedbadge->uniquehash;
$badge->dateissued = (int) $siteissuedbadge->dateissued;
$badge->dateexpire = $siteissuedbadge->dateexpire;
$badge->visible = (int) $siteissuedbadge->visible;
$badge->email = $student1->email;
$context = \context_system::instance();
$badge->badgeurl = \moodle_url::make_webservice_pluginfile_url($context->id, 'badges', 'badgeimage', $badge->id, '/',
'f3')->out(false);
$badge->status = BADGE_STATUS_ACTIVE_LOCKED;
// Add an endorsement for the badge.
$endorsement = new \stdClass();
$endorsement->badgeid = $badgeid;
$endorsement->issuername = 'Issuer name';
$endorsement->issuerurl = 'http://endorsement-issuer-url.domain.co.nz';
$endorsement->issueremail = 'endorsementissuer@example.com';
$endorsement->claimid = 'http://claim-url.domain.co.nz';
$endorsement->claimcomment = 'Claim comment';
$endorsement->dateissued = $now;
$endorsement->id = $sitebadge->save_endorsement($endorsement);
$badge->endorsement = (array) $endorsement;
// Add 2 alignments.
$alignment = new \stdClass();
$alignment->badgeid = $badgeid;
$alignment->id = $sitebadge->save_alignment($alignment);
$badge->alignment[] = (array) $alignment;
$alignment->id = $sitebadge->save_alignment($alignment);
$badge->alignment[] = (array) $alignment;
$badge->relatedbadges = [];
$usersitebadge[] = (array) $badge;
// Now a course badge.
$badge->id = null;
$badge->name = "Test badge course";
$badge->description = "Testing badges course";
$badge->type = BADGE_TYPE_COURSE;
$badge->courseid = (int) $course->id;
$badge->id = $DB->insert_record('badge', $badge, true);
$coursebadge = new \badge($badge->id );
$coursebadge->issue($student1->id, true);
$courseissuedbadge = $DB->get_record('badge_issued', [ 'badgeid' => $badge->id ]);
$badge->issuername = $coursebadge->issuername;
$badge->issuercontact = $coursebadge->issuercontact;
$badge->issuerurl = $coursebadge->issuerurl;
$badge->nextcron = $coursebadge->nextcron;
$badge->issuedid = (int) $courseissuedbadge->id;
$badge->uniquehash = $courseissuedbadge->uniquehash;
$badge->dateissued = (int) $courseissuedbadge->dateissued;
$badge->dateexpire = $courseissuedbadge->dateexpire;
$badge->visible = (int) $courseissuedbadge->visible;
$badge->email = $student1->email;
$context = \context_course::instance($badge->courseid);
$badge->badgeurl = \moodle_url::make_webservice_pluginfile_url($context->id, 'badges', 'badgeimage', $badge->id , '/',
'f3')->out(false);
unset($badge->endorsement);
$badge->alignment = [];
$usercoursebadge[] = (array) $badge;
// Make the site badge a related badge.
$sitebadge->add_related_badges([$badge->id]);
$usersitebadge[0]['relatedbadges'][0] = [
'id' => (int) $coursebadge->id,
'name' => $coursebadge->name
];
$usercoursebadge[0]['relatedbadges'][0] = [
'id' => (int) $sitebadge->id,
'name' => $sitebadge->name
];
return [
'coursebadge' => $usercoursebadge,
'sitebadge' => $usersitebadge,
'student1' => $student1,
'student2' => $student2
];
}
/**
* Test get user badge by hash.
* These are a basic tests since the badges_get_my_user_badges used by the external function already has unit tests.
* @covers ::execute
*/
public function test_get_user_badge_by_hash(): void {
$data = $this->prepare_test_data();
$this->setUser($data['student1']);
// Site badge.
$result = get_user_badge_by_hash::execute($data['sitebadge'][0]['uniquehash']);
$result = \core_external\external_api::clean_returnvalue(get_user_badge_by_hash::execute_returns(), $result);
$this->assertEquals($data['sitebadge'][0]['uniquehash'], $result['badge'][0]['uniquehash']);
$this->assertEmpty($result['warnings']);
// Course badge.
$result = get_user_badge_by_hash::execute($data['coursebadge'][0]['uniquehash']);
$result = \core_external\external_api::clean_returnvalue(get_user_badge_by_hash::execute_returns(), $result);
$this->assertEquals($data['coursebadge'][0]['uniquehash'], $result['badge'][0]['uniquehash']);
$this->assertEmpty($result['warnings']);
// Wrong hash.
$result = get_user_badge_by_hash::execute('1234');
$result = \core_external\external_api::clean_returnvalue(get_user_badge_by_hash::execute_returns(), $result);
$this->assertEmpty($result['badge']);
$this->assertNotEmpty($result['warnings']);
$this->assertEquals('badgeawardnotfound', $result['warnings'][0]['warningcode']);
}
/**
* Test get user badge by hash with restrictions.
* @covers ::execute
*/
public function test_get_user_badge_by_hash_with_restrictions(): void {
$data = $this->prepare_test_data();
$this->setUser($data['student2']);
// Site badge.
$result = get_user_badge_by_hash::execute($data['sitebadge'][0]['uniquehash']);
$result = \core_external\external_api::clean_returnvalue(get_user_badge_by_hash::execute_returns(), $result);
$this->assertNotEmpty($result['badge']);
$this->assertEmpty($result['warnings']);
// Check that we don't have permissions for view the complete information for site badges.
if (isset($result['badge'][0]['type']) && $result['badge'][0]['type'] == BADGE_TYPE_SITE) {
$this->assertFalse(isset($result['badge'][0]['message']));
// Check that we have permissions to see all the data in alignments and related badges.
foreach ($result['badge'][0]['alignment'] as $alignment) {
$this->assertTrue(isset($alignment['id']));
}
foreach ($result['badge'][0]['relatedbadges'] as $relatedbadge) {
$this->assertTrue(isset($relatedbadge['id']));
}
} else {
$this->assertTrue(isset($result['badge'][0]['message']));
}
// Course badge.
$result = get_user_badge_by_hash::execute($data['coursebadge'][0]['uniquehash']);
$result = \core_external\external_api::clean_returnvalue(get_user_badge_by_hash::execute_returns(), $result);
$this->assertNotEmpty($result['badge']);
$this->assertEmpty($result['warnings']);
// Check that we don't have permissions for view the complete information for course badges.
if (isset($result['badge'][0]['type']) && $result['badge'][0]['type'] == BADGE_TYPE_COURSE) {
$this->assertFalse(isset($result['badge'][0]['message']));
} else {
$this->assertTrue(isset($result['badge'][0]['message']));
}
}
}
@@ -0,0 +1,103 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
declare(strict_types=1);
/**
* Badges test generator for Behat
*
* @package core_badges
* @copyright 2023 Paul Holden <paulh@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class behat_core_badges_generator extends behat_generator_base {
/**
* Get a list of the entities that can be created for this component
*
* @return array[]
*/
protected function get_creatable_entities(): array {
return [
'Badges' => [
'singular' => 'Badge',
'datagenerator' => 'badge',
'required' => [
'name',
'image',
],
'switchids' => [
'course' => 'courseid',
],
],
'Criterias' => [
'singular' => 'Criteria',
'datagenerator' => 'criteria',
'required' => [
'badge',
'role',
],
'switchids' => [
'badge' => 'badgeid',
'role' => 'roleid',
],
],
'Issued badges' => [
'singular' => 'Issued badge',
'datagenerator' => 'issued_badge',
'required' => [
'badge',
'user',
],
'switchids' => [
'badge' => 'badgeid',
'user' => 'userid',
],
],
];
}
/**
* Look up badge ID from given name
*
* @param string $name
* @return int
*/
protected function get_badge_id(string $name): int {
global $DB;
return (int) $DB->get_field('badge', 'id', ['name' => $name], MUST_EXIST);
}
/**
* Pre-process badge entity
*
* @param array $badge
* @return array
*/
protected function preprocess_badge(array $badge): array {
global $CFG;
require_once("{$CFG->libdir}/badgeslib.php");
// Allow text status' that correspond to badge constants.
if (array_key_exists('status', $badge) && !is_numeric($badge['status'])) {
$badge['status'] = constant('BADGE_STATUS_' . strtoupper($badge['status']));
}
return $badge;
}
}
+167
View File
@@ -0,0 +1,167 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
declare(strict_types=1);
use core_badges\badge;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once("{$CFG->libdir}/badgeslib.php");
/**
* Badges test generator
*
* @package core_badges
* @copyright 2022 Paul Holden <paulh@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class core_badges_generator extends component_generator_base {
/**
* Create badge
*
* @param array|stdClass $record
* @return badge
*/
public function create_badge($record): badge {
global $CFG, $DB, $USER;
$record = (array) $record;
// Save badge image/tags for later.
$badgeimage = $record['image'] ?? '';
$badgetags = $record['tags'] ?? '';
unset($record['image'], $record['tags']);
$record = (object) array_merge([
'name' => 'Test badge',
'description' => 'Testing badges',
'timecreated' => time(),
'timemodified' => time(),
'usercreated' => $USER->id,
'usermodified' => $USER->id,
'issuername' => 'Test issuer',
'issuerurl' => 'http://issuer-url.domain.co.nz',
'issuercontact' => 'issuer@example.com',
'expiredate' => null,
'expireperiod' => null,
'type' => BADGE_TYPE_SITE,
'courseid' => null,
'messagesubject' => 'Test message subject',
'message' => 'Test message body',
'attachment' => 1,
'notification' => 0,
'status' => BADGE_STATUS_ACTIVE,
'version' => OPEN_BADGES_V2,
'language' => 'en',
'imageauthorname' => 'Image author',
'imageauthoremail' => 'author@example.com',
'imageauthorurl' => 'http://image.example.com/',
'imagecaption' => 'Image caption'
], $record);
$record->id = $DB->insert_record('badge', $record);
$badge = new badge($record->id);
// Process badge image (if supplied).
if ($badgeimage !== '') {
$file = get_file_storage()->create_file_from_pathname([
'contextid' => context_user::instance($USER->id)->id,
'userid' => $USER->id,
'component' => 'user',
'filearea' => 'draft',
'itemid' => file_get_unused_draft_itemid(),
'filepath' => '/',
'filename' => basename($badgeimage),
], "{$CFG->dirroot}/$badgeimage");
// Copy image to temp file, as it'll be deleted by the following call.
badges_process_badge_image($badge, $file->copy_content_to_temp());
}
// Process badge tags (if supplied).
if ($badgetags !== '') {
if (!is_array($badgetags)) {
$badgetags = preg_split('/\s*,\s*/', $badgetags, -1, PREG_SPLIT_NO_EMPTY);
}
core_tag_tag::set_item_tags('core_badges', 'badge', $badge->id, $badge->get_context(), $badgetags);
}
return $badge;
}
/**
* Create badge criteria
*
* Note that only manual criteria issues by role is currently supported
*
* @param array|stdClass $record
* @throws coding_exception
*/
public function create_criteria($record): void {
$record = (array) $record;
if (!array_key_exists('badgeid', $record)) {
throw new coding_exception('Record must contain \'badgeid\' property');
}
if (!array_key_exists('roleid', $record)) {
throw new coding_exception('Record must contain \'roleid\' property');
}
$badge = new badge($record['badgeid']);
// Create the overall criteria.
if (count($badge->criteria) === 0) {
award_criteria::build([
'badgeid' => $badge->id,
'criteriatype' => BADGE_CRITERIA_TYPE_OVERALL,
])->save([
'agg' => BADGE_CRITERIA_AGGREGATION_ALL,
]);
}
// Create the manual criteria.
award_criteria::build([
'badgeid' => $badge->id,
'criteriatype' => BADGE_CRITERIA_TYPE_MANUAL,
])->save([
'role_' . $record['roleid'] => $record['roleid'],
'description' => $record['description'] ?? '',
]);
}
/**
* Create issued badge to a user
*
* @param array|stdClass $record
* @throws coding_exception
*/
public function create_issued_badge($record): void {
$record = (array) $record;
if (!array_key_exists('badgeid', $record)) {
throw new coding_exception('Record must contain \'badgeid\' property');
}
if (!array_key_exists('userid', $record)) {
throw new coding_exception('Record must contain \'userid\' property');
}
$badge = new badge($record['badgeid']);
$badge->issue($record['userid'], true);
}
}
@@ -0,0 +1,140 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace core_badges\output;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->libdir . '/badgeslib.php');
/**
* Class manage_badge_action_bar_test
*
* Unit test for the badges tertiary navigation
*
* @coversDefaultClass \core_badges\output\manage_badge_action_bar
* @package core_badges
* @copyright 2021 onwards Peter Dias
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class manage_badge_action_bar_test extends \advanced_testcase {
/**
* Data provider for test_generate_badge_navigation
*
* @return array
*/
public function generate_badge_navigation_provider(): array {
return [
"Test tertiary nav as an editing teacher" => [
"editingteacher", [
'Overview',
'Edit details',
'Criteria',
'Message',
'Recipients (0)',
'Endorsement',
'Related badges (0)',
'Alignments (0)'
],
],
"Test tertiary nav as an non-editing teacher" => [
"teacher", [
'Overview',
'Recipients (0)'
],
],
"Test tertiary nav as an admin" => [
"admin", [
'Overview',
'Edit details',
'Criteria',
'Message',
'Recipients (0)',
'Endorsement',
'Related badges (0)',
'Alignments (0)'
]
],
"Test tertiary nav as a student" => [
"student", [
'Overview'
]
]
];
}
/**
* Test the generate_badge_navigation function
*
* @dataProvider generate_badge_navigation_provider
* @param string $role
* @param array $expected
* @covers ::generate_badge_navigation
*/
public function test_generate_badge_navigation(string $role, array $expected): void {
global $DB;
$this->resetAfterTest();
$course = $this->getDataGenerator()->create_course();
$teacher = self::getDataGenerator()->create_and_enrol($course, 'editingteacher');
if ($role != 'admin') {
$user = $this->getDataGenerator()->create_and_enrol($course, $role);
$this->setUser($user);
} else {
$this->setAdminUser();
}
// Mock up a course badge.
$now = time();
$badge = new \stdClass();
$badge->id = null;
$badge->name = "Test badge course";
$badge->description = "Testing badges course";
$badge->type = BADGE_TYPE_COURSE;
$badge->courseid = $course->id;
$badge->timecreated = $now - 12;
$badge->timemodified = $now - 12;
$badge->usercreated = $teacher->id;
$badge->usermodified = $teacher->id;
$badge->issuername = "Test issuer";
$badge->issuerurl = "http://issuer-url.domain.co.nz";
$badge->issuercontact = "issuer@example.com";
$badge->expiredate = null;
$badge->expireperiod = null;
$badge->messagesubject = "Test message subject for badge";
$badge->message = "Test message body for badge";
$badge->attachment = 1;
$badge->notification = 0;
$badge->status = BADGE_STATUS_ACTIVE;
$badge->version = '1';
$badge->language = 'en';
$badge->imageauthorname = 'Image author';
$badge->imageauthoremail = 'imageauthor@example.com';
$badge->imageauthorurl = 'http://image-author-url.domain.co.nz';
$badge->imagecaption = 'Caption';
$coursebadgeid = $DB->insert_record('badge', $badge, true);
$badge = new \core_badges\badge($coursebadgeid);
$context = \context_course::instance($course->id);
$page = new \moodle_page();
$page->set_context($context);
$actionbar = new manage_badge_action_bar($badge, $page);
$rc = new \ReflectionClass(manage_badge_action_bar::class);
$rcm = $rc->getMethod('generate_badge_navigation');
$content = $rcm->invoke($actionbar);
$this->assertEquals($expected, array_values($content));
}
}
+815
View File
@@ -0,0 +1,815 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Data provider tests.
*
* @package core_badges
* @category test
* @copyright 2018 Frédéric Massart
* @author Frédéric Massart <fred@branchup.tech>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_badges\privacy;
defined('MOODLE_INTERNAL') || die();
global $CFG;
use core_privacy\tests\provider_testcase;
use core_privacy\local\request\approved_contextlist;
use core_privacy\local\request\transform;
use core_privacy\local\request\writer;
use core_badges\privacy\provider;
use core_privacy\local\request\approved_userlist;
use core_badges\helper;
require_once($CFG->libdir . '/badgeslib.php');
/**
* Data provider testcase class.
*
* @package core_badges
* @category test
* @copyright 2018 Frédéric Massart
* @author Frédéric Massart <fred@branchup.tech>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class provider_test extends provider_testcase {
public function setUp(): void {
$this->resetAfterTest();
}
public function test_get_contexts_for_userid_for_badge_editing(): void {
$dg = $this->getDataGenerator();
$u1 = $dg->create_user();
$u2 = $dg->create_user();
$u3 = $dg->create_user();
$u4 = $dg->create_user();
$u5 = $dg->create_user();
$c1 = $dg->create_course();
$c2 = $dg->create_course();
$sysctx = \context_system::instance();
$c1ctx = \context_course::instance($c1->id);
$c2ctx = \context_course::instance($c2->id);
// Assert that we find contexts where we created/modified a badge.
$this->create_badge(['usercreated' => $u1->id, 'usermodified' => $u5->id]);
$this->create_badge(['usercreated' => $u2->id, 'type' => BADGE_TYPE_COURSE, 'courseid' => $c1->id]);
$this->create_badge(['usermodified' => $u3->id]);
$this->create_badge(['usermodified' => $u4->id, 'type' => BADGE_TYPE_COURSE, 'courseid' => $c2->id,
'usercreated' => $u5->id]);
$contexts = provider::get_contexts_for_userid($u1->id)->get_contextids();
$this->assertCount(1, $contexts);
$this->assertEquals($sysctx->id, $contexts[0]);
$contexts = provider::get_contexts_for_userid($u2->id)->get_contextids();
$this->assertCount(1, $contexts);
$this->assertEquals($c1ctx->id, $contexts[0]);
$contexts = provider::get_contexts_for_userid($u3->id)->get_contextids();
$this->assertCount(1, $contexts);
$this->assertEquals($sysctx->id, $contexts[0]);
$contexts = provider::get_contexts_for_userid($u4->id)->get_contextids();
$this->assertCount(1, $contexts);
$this->assertEquals($c2ctx->id, $contexts[0]);
$contexts = provider::get_contexts_for_userid($u5->id)->get_contextids();
$this->assertCount(2, $contexts);
$this->assertTrue(in_array($sysctx->id, $contexts));
$this->assertTrue(in_array($c2ctx->id, $contexts));
}
public function test_get_contexts_for_userid_for_manual_award(): void {
global $DB;
$dg = $this->getDataGenerator();
$u1 = $dg->create_user();
$u2 = $dg->create_user();
$u3 = $dg->create_user();
$u4 = $dg->create_user();
$c1 = $dg->create_course();
$sysctx = \context_system::instance();
$c1ctx = \context_course::instance($c1->id);
$u1ctx = \context_user::instance($u1->id);
$u2ctx = \context_user::instance($u2->id);
$u3ctx = \context_user::instance($u3->id);
$u4ctx = \context_user::instance($u4->id);
$b1 = $this->create_badge();
$b2 = $this->create_badge(['type' => BADGE_TYPE_COURSE, 'courseid' => $c1->id]);
$this->create_manual_award(['recipientid' => $u4->id, 'issuerid' => $u1->id, 'badgeid' => $b1->id]);
$this->create_manual_award(['recipientid' => $u3->id, 'issuerid' => $u2->id, 'badgeid' => $b1->id]);
$this->create_manual_award(['recipientid' => $u3->id, 'issuerid' => $u2->id, 'badgeid' => $b2->id]);
$contexts = provider::get_contexts_for_userid($u1->id)->get_contextids();
$this->assertCount(1, $contexts);
$this->assertEquals($u4ctx->id, $contexts[0]);
$contexts = provider::get_contexts_for_userid($u2->id)->get_contextids();
$this->assertCount(1, $contexts);
$this->assertEquals($u3ctx->id, $contexts[0]);
}
public function test_get_contexts_for_userid_for_my_stuff(): void {
global $DB;
$dg = $this->getDataGenerator();
$u1 = $dg->create_user();
$u2 = $dg->create_user();
$u3 = $dg->create_user();
$u4 = $dg->create_user();
$c1 = $dg->create_course();
$sysctx = \context_system::instance();
$c1ctx = \context_course::instance($c1->id);
$u1ctx = \context_user::instance($u1->id);
$u2ctx = \context_user::instance($u2->id);
$u3ctx = \context_user::instance($u3->id);
$u4ctx = \context_user::instance($u4->id);
$b1 = $this->create_badge();
$b2 = $this->create_badge(['type' => BADGE_TYPE_COURSE, 'courseid' => $c1->id]);
helper::create_fake_backpack(['userid' => $u1->id]);
$this->create_manual_award(['recipientid' => $u2->id, 'badgeid' => $b1->id]);
$this->create_issued(['badgeid' => $b2->id, 'userid' => $u3->id]);
$crit = $this->create_criteria_manual($b1->id);
$crit->mark_complete($u4->id);
$contexts = provider::get_contexts_for_userid($u1->id)->get_contextids();
$this->assertCount(1, $contexts);
$this->assertEquals($u1ctx->id, $contexts[0]);
$contexts = provider::get_contexts_for_userid($u2->id)->get_contextids();
$this->assertCount(1, $contexts);
$this->assertEquals($u2ctx->id, $contexts[0]);
$contexts = provider::get_contexts_for_userid($u3->id)->get_contextids();
$this->assertCount(1, $contexts);
$this->assertEquals($u3ctx->id, $contexts[0]);
$contexts = provider::get_contexts_for_userid($u4->id)->get_contextids();
$this->assertCount(1, $contexts);
$this->assertEquals($u4ctx->id, $contexts[0]);
}
public function test_delete_data_for_user(): void {
global $DB;
$dg = $this->getDataGenerator();
$u1 = $dg->create_user();
$u2 = $dg->create_user();
$c1 = $dg->create_course();
$sysctx = \context_system::instance();
$c1ctx = \context_course::instance($c1->id);
$u1ctx = \context_user::instance($u1->id);
$u2ctx = \context_user::instance($u2->id);
$b1 = $this->create_badge(['usercreated' => $u1->id, 'usermodified' => $u2->id]);
$b2 = $this->create_badge(['usercreated' => $u2->id, 'usermodified' => $u1->id,
'type' => BADGE_TYPE_COURSE, 'courseid' => $c1->id]);
helper::create_fake_backpack(['userid' => $u1->id]);
helper::create_fake_backpack(['userid' => $u2->id]);
$this->create_manual_award(['recipientid' => $u1->id, 'badgeid' => $b1->id]);
$this->create_manual_award(['recipientid' => $u2->id, 'badgeid' => $b1->id, 'issuerid' => $u1->id]);
$this->create_issued(['badgeid' => $b2->id, 'userid' => $u1->id]);
$this->create_issued(['badgeid' => $b2->id, 'userid' => $u2->id]);
$crit = $this->create_criteria_manual($b1->id);
$crit->mark_complete($u2->id);
$crit = $this->create_criteria_manual($b2->id);
$crit->mark_complete($u1->id);
$this->assertTrue($DB->record_exists('badge_backpack', ['userid' => $u1->id]));
$this->assertTrue($DB->record_exists('badge_backpack', ['userid' => $u2->id]));
$this->assertTrue($DB->record_exists('badge', ['usercreated' => $u1->id]));
$this->assertTrue($DB->record_exists('badge', ['usermodified' => $u1->id]));
$this->assertTrue($DB->record_exists('badge', ['usercreated' => $u2->id]));
$this->assertTrue($DB->record_exists('badge', ['usermodified' => $u2->id]));
$this->assertTrue($DB->record_exists('badge_manual_award', ['recipientid' => $u1->id]));
$this->assertTrue($DB->record_exists('badge_manual_award', ['recipientid' => $u2->id]));
$this->assertTrue($DB->record_exists('badge_issued', ['userid' => $u1->id]));
$this->assertTrue($DB->record_exists('badge_issued', ['userid' => $u2->id]));
$this->assertTrue($DB->record_exists('badge_criteria_met', ['userid' => $u1->id]));
$this->assertTrue($DB->record_exists('badge_criteria_met', ['userid' => $u2->id]));
provider::delete_data_for_user(new approved_contextlist($u1, 'core_badges', [$sysctx->id, $c1ctx->id,
$u1ctx->id, $u2ctx->id]));
$this->assertTrue($DB->record_exists('badge', ['usercreated' => $u1->id]));
$this->assertTrue($DB->record_exists('badge', ['usermodified' => $u1->id]));
$this->assertTrue($DB->record_exists('badge', ['usercreated' => $u2->id]));
$this->assertTrue($DB->record_exists('badge', ['usermodified' => $u2->id]));
$this->assertFalse($DB->record_exists('badge_backpack', ['userid' => $u1->id]));
$this->assertTrue($DB->record_exists('badge_backpack', ['userid' => $u2->id]));
$this->assertFalse($DB->record_exists('badge_manual_award', ['recipientid' => $u1->id]));
$this->assertTrue($DB->record_exists('badge_manual_award', ['recipientid' => $u2->id]));
$this->assertFalse($DB->record_exists('badge_issued', ['userid' => $u1->id]));
$this->assertTrue($DB->record_exists('badge_issued', ['userid' => $u2->id]));
$this->assertFalse($DB->record_exists('badge_criteria_met', ['userid' => $u1->id]));
$this->assertTrue($DB->record_exists('badge_criteria_met', ['userid' => $u2->id]));
}
public function test_delete_data_for_all_users_in_context(): void {
global $DB;
$dg = $this->getDataGenerator();
$u1 = $dg->create_user();
$u2 = $dg->create_user();
$c1 = $dg->create_course();
$sysctx = \context_system::instance();
$c1ctx = \context_course::instance($c1->id);
$u1ctx = \context_user::instance($u1->id);
$u2ctx = \context_user::instance($u2->id);
$b1 = $this->create_badge(['usercreated' => $u1->id, 'usermodified' => $u2->id]);
$b2 = $this->create_badge(['usercreated' => $u2->id, 'usermodified' => $u1->id,
'type' => BADGE_TYPE_COURSE, 'courseid' => $c1->id]);
helper::create_fake_backpack(['userid' => $u1->id]);
helper::create_fake_backpack(['userid' => $u2->id]);
$this->create_manual_award(['recipientid' => $u1->id, 'badgeid' => $b1->id]);
$this->create_manual_award(['recipientid' => $u2->id, 'badgeid' => $b1->id, 'issuerid' => $u1->id]);
$this->create_issued(['badgeid' => $b2->id, 'userid' => $u1->id]);
$this->create_issued(['badgeid' => $b2->id, 'userid' => $u2->id]);
$crit = $this->create_criteria_manual($b1->id);
$crit->mark_complete($u2->id);
$crit = $this->create_criteria_manual($b2->id);
$crit->mark_complete($u1->id);
$assertnochange = function() use ($DB, $u1, $u2) {
$this->assertTrue($DB->record_exists('badge_backpack', ['userid' => $u1->id]));
$this->assertTrue($DB->record_exists('badge_backpack', ['userid' => $u2->id]));
$this->assertTrue($DB->record_exists('badge', ['usercreated' => $u1->id]));
$this->assertTrue($DB->record_exists('badge', ['usermodified' => $u1->id]));
$this->assertTrue($DB->record_exists('badge', ['usercreated' => $u2->id]));
$this->assertTrue($DB->record_exists('badge', ['usermodified' => $u2->id]));
$this->assertTrue($DB->record_exists('badge_manual_award', ['recipientid' => $u1->id]));
$this->assertTrue($DB->record_exists('badge_manual_award', ['recipientid' => $u2->id]));
$this->assertTrue($DB->record_exists('badge_issued', ['userid' => $u1->id]));
$this->assertTrue($DB->record_exists('badge_issued', ['userid' => $u2->id]));
$this->assertTrue($DB->record_exists('badge_criteria_met', ['userid' => $u1->id]));
$this->assertTrue($DB->record_exists('badge_criteria_met', ['userid' => $u2->id]));
};
$assertnochange();
provider::delete_data_for_all_users_in_context($sysctx);
$assertnochange();
provider::delete_data_for_all_users_in_context($c1ctx);
$assertnochange();
provider::delete_data_for_all_users_in_context($u1ctx);
$this->assertTrue($DB->record_exists('badge', ['usercreated' => $u1->id]));
$this->assertTrue($DB->record_exists('badge', ['usermodified' => $u1->id]));
$this->assertTrue($DB->record_exists('badge', ['usercreated' => $u2->id]));
$this->assertTrue($DB->record_exists('badge', ['usermodified' => $u2->id]));
$this->assertFalse($DB->record_exists('badge_backpack', ['userid' => $u1->id]));
$this->assertTrue($DB->record_exists('badge_backpack', ['userid' => $u2->id]));
$this->assertFalse($DB->record_exists('badge_manual_award', ['recipientid' => $u1->id]));
$this->assertTrue($DB->record_exists('badge_manual_award', ['recipientid' => $u2->id]));
$this->assertFalse($DB->record_exists('badge_issued', ['userid' => $u1->id]));
$this->assertTrue($DB->record_exists('badge_issued', ['userid' => $u2->id]));
$this->assertFalse($DB->record_exists('badge_criteria_met', ['userid' => $u1->id]));
$this->assertTrue($DB->record_exists('badge_criteria_met', ['userid' => $u2->id]));
}
public function test_export_data_for_user(): void {
global $DB;
$yes = transform::yesno(true);
$no = transform::yesno(false);
$dg = $this->getDataGenerator();
$u1 = $dg->create_user();
$u2 = $dg->create_user();
$u3 = $dg->create_user();
$c1 = $dg->create_course();
$sysctx = \context_system::instance();
$c1ctx = \context_course::instance($c1->id);
$u1ctx = \context_user::instance($u1->id);
$u2ctx = \context_user::instance($u2->id);
$b1 = $this->create_badge(['usercreated' => $u3->id]);
$this->endorse_badge(['badgeid' => $b1->id]);
$this->align_badge(['badgeid' => $b1->id], ' (1)');
$this->align_badge(['badgeid' => $b1->id], ' (2)');
$b2 = $this->create_badge(['type' => BADGE_TYPE_COURSE, 'courseid' => $c1->id, 'usermodified' => $u3->id]);
$this->relate_badge($b1->id, $b2->id);
$b3 = $this->create_badge();
$b3crit = $this->create_criteria_manual($b3->id);
$b4 = $this->create_badge();
// Create things for user 2, to check it's not exported it.
$this->create_issued(['badgeid' => $b4->id, 'userid' => $u2->id]);
helper::create_fake_backpack(['userid' => $u2->id, 'email' => $u2->email]);
$this->create_manual_award(['badgeid' => $b1->id, 'recipientid' => $u2->id, 'issuerid' => $u3->id]);
// Create a set of stuff for u1.
$this->create_issued(['badgeid' => $b1->id, 'userid' => $u1->id, 'uniquehash' => 'yoohoo']);
$this->create_manual_award(['badgeid' => $b2->id, 'recipientid' => $u1->id, 'issuerid' => $u3->id]);
$b3crit->mark_complete($u1->id);
helper::create_fake_backpack(['userid' => $u1->id, 'email' => $u1->email]);
// Check u1.
writer::reset();
provider::export_user_data(new approved_contextlist($u1, 'core_badges', [$u1ctx->id, $u2ctx->id,
$sysctx->id, $c1ctx->id]));
$this->assertFalse(writer::with_context($u2ctx)->has_any_data());
$this->assertFalse(writer::with_context($sysctx)->has_any_data());
$this->assertFalse(writer::with_context($c1ctx)->has_any_data());
$path = [get_string('badges', 'core_badges'), "{$b1->name} ({$b1->id})"];
$data = writer::with_context($u1ctx)->get_data($path);
$this->assertEquals($b1->name, $data->name);
$this->assertEquals($b1->version, $data->version);
$this->assertEquals($b1->language, $data->language);
$this->assertEquals($b1->imageauthorname, $data->imageauthorname);
$this->assertEquals($b1->imageauthoremail, $data->imageauthoremail);
$this->assertEquals($b1->imageauthorurl, $data->imageauthorurl);
$this->assertEquals($b1->imagecaption, $data->imagecaption);
$this->assertNotEmpty($data->issued);
$this->assertEmpty($data->manual_award);
$this->assertEmpty($data->criteria_met);
$this->assertFalse(isset($data->course));
$this->assertEquals('yoohoo', $data->issued['unique_hash']);
$this->assertNull($data->issued['expires_on']);
$this->assertNotEmpty($data->endorsement);
$this->assertNotEmpty($data->endorsement['issuername']);
$this->assertNotEmpty($data->endorsement['issuerurl']);
$this->assertNotEmpty($data->endorsement['issueremail']);
$this->assertNotEmpty($data->endorsement['claimid']);
$this->assertNotEmpty($data->endorsement['claimcomment']);
$this->assertNotEmpty($data->endorsement['dateissued']);
$this->assertNotEmpty($data->related_badge);
$this->assertNotEmpty($data->related_badge[0]);
$this->assertEquals($data->related_badge[0]['badgeid'], $b2->id);
$this->assertEquals($data->related_badge[0]['badgename'], $b2->name);
$this->assertNotEmpty($data->alignment);
$this->assertNotEmpty($data->alignment[0]);
$this->assertNotEmpty($data->alignment[0]['targetname']);
$this->assertNotEmpty($data->alignment[0]['targeturl']);
$this->assertNotEmpty($data->alignment[0]['targetdescription']);
$this->assertNotEmpty($data->alignment[0]['targetframework']);
$this->assertNotEmpty($data->alignment[0]['targetcode']);
$this->assertNotEmpty($data->alignment[1]);
$this->assertNotEmpty($data->alignment[1]['targetname']);
$this->assertNotEmpty($data->alignment[1]['targeturl']);
$this->assertNotEmpty($data->alignment[1]['targetdescription']);
$this->assertNotEmpty($data->alignment[1]['targetframework']);
$this->assertNotEmpty($data->alignment[1]['targetcode']);
$path = [get_string('badges', 'core_badges'), "{$b2->name} ({$b2->id})"];
$data = writer::with_context($u1ctx)->get_data($path);
$this->assertEquals($b2->name, $data->name);
$this->assertEmpty($data->issued);
$this->assertNotEmpty($data->manual_award);
$this->assertEmpty($data->criteria_met);
$this->assertEquals($c1->fullname, $data->course);
$this->assertEquals($u3->id, $data->manual_award['issuer']);
$path = [get_string('badges', 'core_badges'), "{$b3->name} ({$b3->id})"];
$data = writer::with_context($u1ctx)->get_data($path);
$this->assertEquals($b3->name, $data->name);
$this->assertEmpty($data->issued);
$this->assertEmpty($data->manual_award);
$this->assertNotEmpty($data->criteria_met);
$this->assertNotFalse(strpos($data->criteria_met[0], get_string('criteria_descr_2', 'core_badges', 'ALL')));
$path = [get_string('badges', 'core_badges')];
$data = writer::with_context($u1ctx)->get_related_data($path, 'backpacks');
$this->assertCount(1, $data->backpacks);
$this->assertEquals($u1->email, $data->backpacks[0]['email']);
// Confirm we do not have u2.
$path = [get_string('badges', 'core_badges'), "{$b4->name} ({$b4->id})"];
$data = writer::with_context($u1ctx)->get_data($path);
$this->assertEmpty($data);
$data = writer::with_context($u2ctx)->get_data($path);
$this->assertEmpty($data);
// Export for u3.
writer::reset();
$path = [get_string('badges', 'core_badges')];
provider::export_user_data(new approved_contextlist($u3, 'core_badges', [$u1ctx->id, $u2ctx->id,
$sysctx->id, $c1ctx->id]));
$data = writer::with_context($u2ctx)->get_related_data($path, 'manual_awards');
$this->assertCount(1, $data->badges);
$this->assertEquals($b1->name, $data->badges[0]['name']);
$this->assertEquals($yes, $data->badges[0]['issued_by_you']);
$this->assertEquals('Manager', $data->badges[0]['issuer_role']);
$data = writer::with_context($sysctx)->get_data($path);
$this->assertCount(1, $data->badges);
$this->assertEquals($b1->name, $data->badges[0]['name']);
$this->assertEquals($yes, $data->badges[0]['created_by_you']);
$this->assertEquals($no, $data->badges[0]['modified_by_you']);
$data = writer::with_context($c1ctx)->get_data($path);
$this->assertCount(1, $data->badges);
$this->assertEquals($b2->name, $data->badges[0]['name']);
$this->assertEquals($no, $data->badges[0]['created_by_you']);
$this->assertEquals($yes, $data->badges[0]['modified_by_you']);
$data = writer::with_context($u1ctx)->get_related_data($path, 'manual_awards');
$this->assertCount(1, $data->badges);
$this->assertEquals($b3->name, $data->badges[0]['name']);
$this->assertEquals($yes, $data->badges[0]['issued_by_you']);
$this->assertEquals('Manager', $data->badges[0]['issuer_role']);
}
/**
* Test that only users within a user, system and course context are fetched.
*/
public function test_get_users_in_context(): void {
$component = 'core_badges';
// Create course1.
$course1 = $this->getDataGenerator()->create_course();
$coursecontext1 = \context_course::instance($course1->id);
// Create course2.
$course2 = $this->getDataGenerator()->create_course();
$coursecontext2 = \context_course::instance($course2->id);
// Create user1.
$user1 = $this->getDataGenerator()->create_user();
$usercontext1 = \context_user::instance($user1->id);
// Create user2.
$user2 = $this->getDataGenerator()->create_user();
$usercontext2 = \context_user::instance($user2->id);
// Create user3.
$user3 = $this->getDataGenerator()->create_user();
$usercontext3 = \context_user::instance($user3->id);
// The list of users in usercontext1 should not return anything yet (related data still haven't been created).
$userlist1 = new \core_privacy\local\request\userlist($usercontext1, $component);
provider::get_users_in_context($userlist1);
$this->assertCount(0, $userlist1);
// The list of users in coursecontext1 should not return anything yet (related data still haven't been created).
$userlist2 = new \core_privacy\local\request\userlist($coursecontext1, $component);
provider::get_users_in_context($userlist2);
$this->assertCount(0, $userlist2);
// The list of users in systemcontext should not return anything yet (related data still haven't been created).
$systemcontext = \context_system::instance();
$userlist3 = new \core_privacy\local\request\userlist($systemcontext, $component);
provider::get_users_in_context($userlist3);
$this->assertCount(0, $userlist3);
// Assert that we find contexts where we created/modified a badge.
$this->create_badge(['usercreated' => $user1->id, 'usermodified' => $user2->id]);
$badge1 = $this->create_badge(['usercreated' => $user2->id, 'type' => BADGE_TYPE_COURSE, 'courseid' => $course1->id]);
$badge2 = $this->create_badge(['usercreated' => $user3->id, 'usermodified' => $user1->id]);
$this->create_manual_award(['recipientid' => $user2->id, 'issuerid' => $user1->id, 'badgeid' => $badge1->id]);
$this->create_manual_award(['recipientid' => $user3->id, 'issuerid' => $user2->id, 'badgeid' => $badge1->id]);
$this->create_manual_award(['recipientid' => $user1->id, 'issuerid' => $user2->id, 'badgeid' => $badge2->id]);
helper::create_fake_backpack(['userid' => $user2->id]);
$this->create_issued(['badgeid' => $badge2->id, 'userid' => $user3->id]);
$crit = $this->create_criteria_manual($badge1->id);
$crit->mark_complete($user3->id);
// The list of users for user context should return user1 and user2.
provider::get_users_in_context($userlist1);
$this->assertCount(2, $userlist1);
$this->assertTrue(in_array($user1->id, $userlist1->get_userids()));
$this->assertTrue(in_array($user2->id, $userlist1->get_userids()));
// The list of users for course context should return user2.
provider::get_users_in_context($userlist2);
$this->assertCount(1, $userlist2);
$this->assertTrue(in_array($user2->id, $userlist2->get_userids()));
// The list of users for system context should return user1, user2 and user3.
provider::get_users_in_context($userlist3);
$this->assertCount(3, $userlist3);
$this->assertTrue(in_array($user1->id, $userlist3->get_userids()));
$this->assertTrue(in_array($user2->id, $userlist3->get_userids()));
$this->assertTrue(in_array($user3->id, $userlist3->get_userids()));
}
/**
* Test that data for users in approved userlist is deleted.
*/
public function test_delete_data_for_users(): void {
$component = 'core_badges';
// Create course1.
$course1 = $this->getDataGenerator()->create_course();
$coursecontext1 = \context_course::instance($course1->id);
// Create course2.
$course2 = $this->getDataGenerator()->create_course();
$coursecontext2 = \context_course::instance($course2->id);
// Create user1.
$user1 = $this->getDataGenerator()->create_user();
$usercontext1 = \context_user::instance($user1->id);
// Create user2.
$user2 = $this->getDataGenerator()->create_user();
$usercontext2 = \context_user::instance($user2->id);
// Create user3.
$user3 = $this->getDataGenerator()->create_user();
$usercontext3 = \context_user::instance($user3->id);
$this->create_badge(['usercreated' => $user1->id, 'usermodified' => $user2->id]);
$badge1 = $this->create_badge(['usercreated' => $user2->id, 'type' => BADGE_TYPE_COURSE, 'courseid' => $course1->id]);
$badge2 = $this->create_badge(['usercreated' => $user3->id, 'type' => BADGE_TYPE_COURSE, 'courseid' => $course2->id,
'usermodified' => $user1->id]);
$this->create_manual_award(['recipientid' => $user2->id, 'issuerid' => $user1->id, 'badgeid' => $badge1->id]);
$this->create_manual_award(['recipientid' => $user3->id, 'issuerid' => $user2->id, 'badgeid' => $badge1->id]);
$this->create_manual_award(['recipientid' => $user1->id, 'issuerid' => $user2->id, 'badgeid' => $badge2->id]);
helper::create_fake_backpack(['userid' => $user2->id]);
$this->create_issued(['badgeid' => $badge2->id, 'userid' => $user3->id]);
$crit = $this->create_criteria_manual($badge1->id);
$crit->mark_complete($user3->id);
// The list of users for usercontext2 context should return users.
$userlist1 = new \core_privacy\local\request\userlist($usercontext2, $component);
provider::get_users_in_context($userlist1);
$this->assertCount(2, $userlist1);
$this->assertTrue(in_array($user1->id, $userlist1->get_userids()));
$this->assertTrue(in_array($user2->id, $userlist1->get_userids()));
// The list of users for coursecontext2 context should return users.
$userlist2 = new \core_privacy\local\request\userlist($coursecontext2, $component);
provider::get_users_in_context($userlist2);
$this->assertCount(2, $userlist2);
$this->assertTrue(in_array($user1->id, $userlist2->get_userids()));
$this->assertTrue(in_array($user3->id, $userlist2->get_userids()));
// The list of users for system context should return users.
$systemcontext = \context_system::instance();
$userlist3 = new \core_privacy\local\request\userlist($systemcontext, $component);
provider::get_users_in_context($userlist3);
$this->assertCount(2, $userlist3);
$this->assertTrue(in_array($user1->id, $userlist3->get_userids()));
$this->assertTrue(in_array($user2->id, $userlist3->get_userids()));
// Delete the data for user1 in usercontext2.
$approvedlist = new approved_userlist($usercontext2, $component, [$user1->id]);
// Delete using delete_data_for_user. No data for users in usercontext2 should be removed.
provider::delete_data_for_users($approvedlist);
// The list of users for usercontext2 context should still return user1, user2.
$userlist1 = new \core_privacy\local\request\userlist($usercontext2, $component);
provider::get_users_in_context($userlist1);
$this->assertCount(2, $userlist1);
$this->assertTrue(in_array($user1->id, $userlist1->get_userids()));
$this->assertTrue(in_array($user2->id, $userlist1->get_userids()));
// Delete the data for user2 in usercontext2.
$approvedlist = new approved_userlist($usercontext2, $component, [$user2->id]);
// Delete using delete_data_for_user. The user data in usercontext2 should be removed.
provider::delete_data_for_users($approvedlist);
// The list of users for usercontext2 context should not return any users.
$userlist1 = new \core_privacy\local\request\userlist($usercontext2, $component);
provider::get_users_in_context($userlist1);
$this->assertCount(0, $userlist1);
// The list of users for coursecontext2 context should return the previous users.
$userlist2 = new \core_privacy\local\request\userlist($coursecontext2, $component);
provider::get_users_in_context($userlist2);
$this->assertCount(2, $userlist2);
// The list of users for system context should return the previous users.
$systemcontext = \context_system::instance();
$userlist3 = new \core_privacy\local\request\userlist($systemcontext, $component);
provider::get_users_in_context($userlist3);
$this->assertCount(2, $userlist3);
// Make sure data is only deleted in the user context, nothing in course or system.
// Convert $userlist2 into an approved_contextlist.
$approvedlist = new approved_userlist($coursecontext2, $component, $userlist2->get_userids());
provider::delete_data_for_users($approvedlist);
// The list of users for coursecontext2 context should still return the user data.
$userlist2 = new \core_privacy\local\request\userlist($coursecontext2, $component);
provider::get_users_in_context($userlist2);
$this->assertCount(2, $userlist2);
}
/**
* Create a badge.
*
* @param array $params Parameters.
* @return object
*/
protected function create_badge(array $params = []) {
global $DB, $USER;
$record = (object) array_merge([
'name' => "Test badge with 'apostrophe' and other friends (<>&@#)",
'description' => "Testing badges",
'timecreated' => time(),
'timemodified' => time(),
'usercreated' => $USER->id,
'usermodified' => $USER->id,
'issuername' => "Test issuer",
'issuerurl' => "http://issuer-url.domain.co.nz",
'issuercontact' => "issuer@example.com",
'expiredate' => null,
'expireperiod' => null,
'type' => BADGE_TYPE_SITE,
'courseid' => null,
'messagesubject' => "Test message subject",
'message' => "Test message body",
'attachment' => 1,
'notification' => 0,
'status' => BADGE_STATUS_ACTIVE,
'version' => OPEN_BADGES_V2,
'language' => 'en',
'imageauthorname' => 'Image author',
'imageauthoremail' => 'author@example.com',
'imageauthorurl' => 'http://image.example.com/',
'imagecaption' => 'Image caption'
], $params);
$record->id = $DB->insert_record('badge', $record);
return $record;
}
/**
* Relate a badge.
*
* @param int $badgeid The badge ID.
* @param int $relatedbadgeid The related badge ID.
* @return object
*/
protected function relate_badge(int $badgeid, int $relatedbadgeid) {
global $DB;
$record = (object) [
'badgeid' => $badgeid,
'relatedbadgeid' => $relatedbadgeid
];
$record->id = $DB->insert_record('badge_related', $record);
return $record;
}
/**
* Align a badge.
*
* @param array $params Parameters.
* @return object
*/
protected function align_badge(array $params = [], $suffix = '') {
global $DB;
$record = (object) array_merge([
'badgeid' => null,
'targetname' => "Alignment name" . $suffix,
'targeturl' => "http://issuer-url.domain.co.nz",
'targetdescription' => "Description" . $suffix,
'targetframework' => "Framework" . $suffix,
'targetcode' => "Code . $suffix"
], $params);
$record->id = $DB->insert_record('badge_alignment', $record);
return $record;
}
/**
* Endorse a badge.
*
* @param array $params Parameters.
* @return object
*/
protected function endorse_badge(array $params = []) {
global $DB;
$record = (object) array_merge([
'badgeid' => null,
'issuername' => "External issuer name",
'issuerurl' => "http://issuer-url.domain.co.nz",
'issueremail' => "issuer@example.com",
'claimid' => "Claim ID",
'claimcomment' => "Claim comment",
'dateissued' => time()
], $params);
$record->id = $DB->insert_record('badge_endorsement', $record);
return $record;
}
/**
* Create a criteria of type badge.
*
* @param int $badgeid The badge ID.
* @param array $params Parameters.
* @return object
*/
protected function create_criteria_badge($badgeid, array $params = []) {
$badge = new \badge($badgeid);
if (empty($badge->criteria)) {
$overall = \award_criteria::build(['criteriatype' => BADGE_CRITERIA_TYPE_OVERALL, 'badgeid' => $badge->id]);
$overall->save(['agg' => BADGE_CRITERIA_AGGREGATION_ALL]);
}
$criteria = \award_criteria::build([
'badgeid' => $badge->id,
'criteriatype' => BADGE_CRITERIA_TYPE_BADGE,
]);
if (isset($params['badgeid'])) {
$params['badge_' . $params['badgeid']] = $params['badgeid'];
unset($params['badgeid']);
}
$criteria->save($params);
$badge = new \badge($badgeid);
return $badge->criteria[BADGE_CRITERIA_TYPE_BADGE];
}
/**
* Create a criteria of type manual.
*
* @param int $badgeid The badge ID.
* @param array $params Parameters.
* @return object
*/
protected function create_criteria_manual($badgeid, array $params = []) {
global $DB;
$badge = new \badge($badgeid);
if (empty($badge->criteria)) {
$overall = \award_criteria::build(['criteriatype' => BADGE_CRITERIA_TYPE_OVERALL, 'badgeid' => $badge->id]);
$overall->save(['agg' => BADGE_CRITERIA_AGGREGATION_ALL]);
}
$criteria = \award_criteria::build([
'badgeid' => $badge->id,
'criteriatype' => BADGE_CRITERIA_TYPE_MANUAL,
]);
$managerroleid = $DB->get_field_select('role', 'id', 'shortname = ?', ['manager'], IGNORE_MULTIPLE);
if (empty($params)) {
$params = [
'role_' . $managerroleid = $managerroleid
];
}
$criteria->save($params);
$badge = new \badge($badgeid);
return $badge->criteria[BADGE_CRITERIA_TYPE_MANUAL];
}
/**
* Create a badge issued.
*
* @param array $params Parameters.
* @return object
*/
protected function create_issued(array $params = []) {
global $DB, $USER;
$record = (object) array_merge([
'badgeid' => null,
'userid' => null,
'uniquehash' => random_string(40),
'dateissued' => time(),
'dateexpire' => null,
'visible' => 1,
'issuernotified' => null,
], $params);
$record->id = $DB->insert_record('badge_issued', $record);
return $record;
}
/**
* Create a manual award.
*
* @param array $params Parameters.
* @return object
*/
protected function create_manual_award(array $params = []) {
global $DB, $USER;
$record = (object) array_merge([
'badgeid' => null,
'recipientid' => null,
'issuerid' => $USER->id,
'issuerrole' => $DB->get_field_select('role', 'id', 'shortname = ?', ['manager'], IGNORE_MULTIPLE),
'datemet' => time()
], $params);
$record->id = $DB->insert_record('badge_manual_award', $record);
return $record;
}
}
@@ -0,0 +1,419 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
declare(strict_types=1);
namespace core_badges\reportbuilder\datasource;
use core_badges_generator;
use core_reportbuilder_generator;
use core_reportbuilder_testcase;
use core_reportbuilder\local\filters\{boolean_select, date, select, tags, text};
use core_reportbuilder\manager;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once("{$CFG->dirroot}/reportbuilder/tests/helpers.php");
require_once("{$CFG->libdir}/badgeslib.php");
/**
* Unit tests for badges datasource
*
* @package core_badges
* @covers \core_badges\reportbuilder\datasource\badges
* @copyright 2022 Paul Holden <paulh@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
final class badges_test extends core_reportbuilder_testcase {
/**
* Test default datasource
*/
public function test_datasource_default(): void {
$this->resetAfterTest();
$user1 = $this->getDataGenerator()->create_user(['firstname' => 'Alan', 'lastname' => 'Apple']);
$user2 = $this->getDataGenerator()->create_user(['firstname' => 'Barry', 'lastname' => 'Banana']);
/** @var core_badges_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_badges');
$badgeone = $generator->create_badge(['name' => 'Badge 1', 'description' => 'My first badge']);
$badgeone->issue($user1->id, true);
$badgeone->issue($user2->id, true);
// Second badge, not issued to any users.
$badgetwo = $generator->create_badge(['name' => 'Badge 2', 'description' => 'My second badge']);
/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
$report = $generator->create_report(['name' => 'Badges', 'source' => badges::class, 'default' => 1]);
$content = $this->get_custom_report_content($report->get('id'));
$this->assertCount(3, $content);
// Default columns are badge, description, user, issued time. Sorted by badge, user, issued time.
[$badgename, $badgedescription, $userfullname, $badgeissued] = array_values($content[0]);
$this->assertEquals($badgeone->name, $badgename);
$this->assertEquals($badgeone->description, $badgedescription);
$this->assertEquals(fullname($user1), $userfullname);
$this->assertNotEmpty($badgeissued);
[$badgename, $badgedescription, $userfullname, $badgeissued] = array_values($content[1]);
$this->assertEquals($badgeone->name, $badgename);
$this->assertEquals($badgeone->description, $badgedescription);
$this->assertEquals(fullname($user2), $userfullname);
$this->assertNotEmpty($badgeissued);
[$badgename, $badgedescription, $userfullname, $badgeissued] = array_values($content[2]);
$this->assertEquals($badgetwo->name, $badgename);
$this->assertEquals($badgetwo->description, $badgedescription);
$this->assertEmpty($userfullname);
$this->assertEmpty($badgeissued);
}
/**
* Test datasource columns that aren't added by default
*/
public function test_datasource_non_default_columns(): void {
global $DB;
$this->resetAfterTest();
$user1 = $this->getDataGenerator()->create_user(['firstname' => 'Alan', 'lastname' => 'Apple']);
$user2 = $this->getDataGenerator()->create_user(['firstname' => 'Barry', 'lastname' => 'Banana']);
/** @var core_badges_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_badges');
$badgeone = $generator->create_badge([
'name' => 'Badge 1',
'language' => 'de',
'expireperiod' => HOURSECS,
'tags' => ['cool'],
]);
$badgeone->issue($user1->id, true);
$badgeone->issue($user2->id, true);
// Course badge, not issued to any users.
$course = $this->getDataGenerator()->create_course();
$badgetwo = $generator->create_badge(['name' => 'Badge 2', 'type' => BADGE_TYPE_COURSE, 'courseid' => $course->id]);
// Create criteria for manually awarding by role.
$managerrole = $DB->get_field('role', 'id', ['shortname' => 'manager']);
$generator->create_criteria(['badgeid' => $badgeone->id, 'roleid' => $managerrole]);
/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
$report = $generator->create_report(['name' => 'Badges', 'source' => badges::class, 'default' => 0]);
// These two columns have been asserted previously, we're only adding them for consistent sorting.
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'badge:name', 'sortenabled' => 1]);
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:fullname', 'sortenabled' => 1]);
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'badge:namewithlink']);
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'badge:criteria']);
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'badge:image']);
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'badge:language']);
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'badge:version']);
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'badge:status']);
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'badge:expiry']);
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'tag:name']);
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'badge_issued:expire']);
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'badge_issued:visible']);
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'course:fullname']);
$content = $this->get_custom_report_content($report->get('id'));
$this->assertCount(3, $content);
$expectedbadgeonelink = \html_writer::link(new \moodle_url('/badges/overview.php',
['id' => $badgeone->id]), ($badgeone->name));
// First badge, issued to user one.
[$badgename, $fullname, $namewithlink, $criteria, $image, $language, $version, $status, $expiry, $tag, $expires,
$visible, $coursename] = array_values($content[0]);
$this->assertEquals($badgeone->name, $badgename);
$this->assertEquals(fullname($user1), $fullname);
$this->assertEquals($expectedbadgeonelink, $namewithlink);
$this->assertStringContainsString('Awarded by: Manager', $criteria);
$this->assertStringContainsString('Image caption', $image);
$this->assertEquals('German', $language);
$this->assertEquals(2, $version);
$this->assertEquals('Available (criteria locked)', $status);
$this->assertEquals('1 hour', $expiry);
$this->assertEquals('cool', $tag);
$this->assertNotEmpty($expires);
$this->assertEquals('Yes', $visible);
$this->assertEquals('PHPUnit test site', $coursename);
// First badge, issued to user two.
[$badgename, $fullname, $namewithlink, $criteria, $image, $language, $version, $status, $expiry, $tag, $expires,
$visible, $coursename] = array_values($content[1]);
$this->assertEquals($badgeone->name, $badgename);
$this->assertEquals(fullname($user2), $fullname);
$this->assertEquals($expectedbadgeonelink, $namewithlink);
$this->assertStringContainsString('Awarded by: Manager', $criteria);
$this->assertStringContainsString('Image caption', $image);
$this->assertEquals('German', $language);
$this->assertEquals(2, $version);
$this->assertEquals('Available (criteria locked)', $status);
$this->assertEquals('1 hour', $expiry);
$this->assertEquals('cool', $tag);
$this->assertNotEmpty($expires);
$this->assertEquals('Yes', $visible);
$this->assertEquals('PHPUnit test site', $coursename);
$expectedbadgetwolink = \html_writer::link(new \moodle_url('/badges/overview.php',
['id' => $badgetwo->id]), ($badgetwo->name));
// Course badge, not issues to any users.
[$badgename, $fullname, $namewithlink, $criteria, $image, $language, $version, $status, $expiry, $tag, $expires,
$visible, $coursename] = array_values($content[2]);
$this->assertEquals($badgetwo->name, $badgename);
$this->assertEmpty($fullname);
$this->assertEquals($expectedbadgetwolink, $namewithlink);
$this->assertEquals('Criteria for this badge have not been set up yet.', $criteria);
$this->assertStringContainsString('Image caption', $image);
$this->assertEquals('English', $language);
$this->assertEquals(2, $version);
$this->assertEquals('Available', $status);
$this->assertEquals('Never', $expiry);
$this->assertEmpty($tag);
$this->assertEmpty($expires);
$this->assertEmpty($visible);
$this->assertEquals($course->fullname, $coursename);
}
/**
* Test creating a report containing "expiry" as both a condition and a filter
*
* This is really testing that it's possible to do so, for a filter instance that returns SQL parameters
*/
public function test_report_expiry_condition_and_filter(): void {
$this->resetAfterTest();
/** @var core_badges_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_badges');
$badgeone = $generator->create_badge(['name' => 'Badge 1', 'expiredate' => 10]);
$badgetwo = $generator->create_badge(['name' => 'Badge 2', 'expiredate' => 20]);
$badgethree = $generator->create_badge(['name' => 'Badge 3', 'expiredate' => 30]);
/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
$report = $generator->create_report(['name' => 'Badges', 'source' => badges::class, 'default' => 0]);
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'badge:name']);
$generator->create_condition(['reportid' => $report->get('id'), 'uniqueidentifier' => 'badge:expiry']);
$generator->create_filter(['reportid' => $report->get('id'), 'uniqueidentifier' => 'badge:expiry']);
// Load report instance, set condition.
$instance = manager::get_report_from_persistent($report);
$instance->set_condition_values([
'badge:expiry_operator' => date::DATE_RANGE,
'badge:expiry_from' => 15,
]);
// Set filter.
$content = $this->get_custom_report_content($report->get('id'), 0, [
'badge:expiry_operator' => date::DATE_RANGE,
'badge:expiry_to' => 25,
]);
$this->assertEquals([
[$badgetwo->name],
], array_map('array_values', $content));
}
/**
* Data provider for {@see test_datasource_filters}
*
* @return array[]
*/
public static function datasource_filters_provider(): array {
return [
// Badge.
'Filter badge name' => ['badge:name', [
'badge:name_operator' => text::IS_EQUAL_TO,
'badge:name_value' => 'Course badge',
], true],
'Filter badge name (no match)' => ['badge:name', [
'badge:name_operator' => text::IS_EQUAL_TO,
'badge:name_value' => 'Other badge',
], false],
'Filter badge version' => ['badge:version', [
'badge:version_operator' => text::IS_EQUAL_TO,
'badge:version_value' => '2.0',
], true],
'Filter badge version (no match)' => ['badge:version', [
'badge:version_operator' => text::IS_EQUAL_TO,
'badge:version_value' => '1.0',
], false],
'Filter badge status' => ['badge:status', [
'badge:status_operator' => select::EQUAL_TO,
'badge:status_value' => BADGE_STATUS_ACTIVE_LOCKED,
], true],
'Filter badge status (no match)' => ['badge:status', [
'badge:status_operator' => select::EQUAL_TO,
'badge:status_value' => BADGE_STATUS_ACTIVE,
], false],
'Filter badge expiry' => ['badge:expiry', [
'badge:expiry_operator' => date::DATE_RANGE,
'badge:expiry_from' => 1622502000,
], true],
'Filter badge expiry (no match)' => ['badge:expiry', [
'badge:expiry_operator' => date::DATE_RANGE,
'badge:expiry_to' => 1622502000,
], false],
'Filter badge type' => ['badge:type', [
'badge:type_operator' => select::EQUAL_TO,
'badge:type_value' => BADGE_TYPE_COURSE,
], true],
'Filter badge type (no match)' => ['badge:type', [
'badge:type_operator' => select::EQUAL_TO,
'badge:type_value' => BADGE_TYPE_SITE,
], false],
// Badge tag.
'Filter tag name' => ['tag:name', [
'tag:name_operator' => tags::NOT_EMPTY,
], true],
'Filter tag name (no match)' => ['tag:name', [
'tag:name_operator' => tags::EQUAL_TO,
'tag:name_value' => [-1],
], false],
// User.
'Filter user fullname' => ['user:fullname', [
'user:fullname_operator' => text::IS_EQUAL_TO,
'user:fullname_value' => 'Zoe Zebra',
], true],
'Filter user fullname (no match)' => ['user:fullname', [
'user:fullname_operator' => text::IS_EQUAL_TO,
'user:fullname_value' => 'Alice Aardvark',
], false],
// Badge issued.
'Filter badge issued date' => ['badge_issued:issued', [
'badge_issued:issued_operator' => date::DATE_RANGE,
'badge_issued:issued_from' => 1622502000,
], true],
'Filter badge issued date (no match)' => ['badge_issued:issued', [
'badge_issued:issued_operator' => date::DATE_RANGE,
'badge_issued:issued_to' => 1622502000,
], false],
'Filter badge issued expires' => ['badge_issued:expires', [
'badge_issued:expires_operator' => date::DATE_RANGE,
'badge_issued:expires_from' => 1622502000,
], true],
'Filter badge issued expires (no match)' => ['badge_issued:expires', [
'badge_issued:expires_operator' => date::DATE_RANGE,
'badge_issued:expires_to' => 1622502000,
], false],
'Filter badge issued visible' => ['badge_issued:visible', [
'badge_issued:visible_operator' => boolean_select::CHECKED,
], true],
'Filter badge issued visible (no match)' => ['badge_issued:visible', [
'badge_issued:visible_operator' => boolean_select::NOT_CHECKED,
], false],
// Course.
'Filter course fullname' => ['course:fullname', [
'course:fullname_operator' => text::IS_EQUAL_TO,
'course:fullname_value' => 'Course 1',
], true],
'Filter course fullname (no match)' => ['course:fullname', [
'course:fullname_operator' => text::IS_EQUAL_TO,
'course:fullname_value' => 'Course 2',
], false],
];
}
/**
* Test datasource filters
*
* @param string $filtername
* @param array $filtervalues
* @param bool $expectmatch
*
* @dataProvider datasource_filters_provider
*/
public function test_datasource_filters(string $filtername, array $filtervalues, bool $expectmatch): void {
$this->resetAfterTest();
$course = $this->getDataGenerator()->create_course(['fullname' => 'Course 1']);
$user = $this->getDataGenerator()->create_and_enrol($course, 'student', ['firstname' => 'Zoe', 'lastname' => 'Zebra']);
/** @var core_badges_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_badges');
$badge = $generator->create_badge([
'name' => 'Course badge',
'version' => '2.0',
'type' => BADGE_TYPE_COURSE,
'courseid' => $course->id,
'expireperiod' => HOURSECS,
'tags' => ['cool'],
]);
$badge->issue($user->id, true);
/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
// Create report containing single username column, and given filter.
$report = $generator->create_report(['name' => 'My report', 'source' => badges::class, 'default' => 0]);
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'badge:name']);
// Add filter, set it's values.
$generator->create_filter(['reportid' => $report->get('id'), 'uniqueidentifier' => $filtername]);
$content = $this->get_custom_report_content($report->get('id'), 0, $filtervalues);
if ($expectmatch) {
$this->assertCount(1, $content);
$this->assertEquals($badge->name, reset($content[0]));
} else {
$this->assertEmpty($content);
}
}
/**
* Stress test datasource
*
* In order to execute this test PHPUNIT_LONGTEST should be defined as true in phpunit.xml or directly in config.php
*/
public function test_stress_datasource(): void {
if (!PHPUNIT_LONGTEST) {
$this->markTestSkipped('PHPUNIT_LONGTEST is not defined');
}
$this->resetAfterTest();
$course = $this->getDataGenerator()->create_course();
$user = $this->getDataGenerator()->create_and_enrol($course);
/** @var core_badges_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_badges');
$badge = $generator->create_badge(['name' => 'Course badge', 'type' => BADGE_TYPE_COURSE, 'courseid' => $course->id]);
$badge->issue($user->id, true);
$this->datasource_stress_test_columns(badges::class);
$this->datasource_stress_test_columns_aggregation(badges::class);
$this->datasource_stress_test_conditions(badges::class, 'badge:name');
}
}
@@ -0,0 +1,379 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
declare(strict_types=1);
namespace core_badges\reportbuilder\datasource;
use core_badges_generator;
use core_reportbuilder_generator;
use core_reportbuilder_testcase;
use core_reportbuilder\local\filters\{boolean_select, date, select, tags, text};
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once("{$CFG->dirroot}/reportbuilder/tests/helpers.php");
require_once("{$CFG->libdir}/badgeslib.php");
/**
* Unit tests for user badges datasource
*
* @package core_badges
* @covers \core_badges\reportbuilder\datasource\users
* @copyright 2023 Paul Holden <paulh@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class users_test extends core_reportbuilder_testcase {
/**
* Test default datasource
*/
public function test_datasource_default(): void {
$this->resetAfterTest();
$user = $this->getDataGenerator()->create_user(['firstname' => 'Zoe', 'lastname' => 'Zebra']);
/** @var core_badges_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_badges');
($badgeone = $generator->create_badge(['name' => 'Badge 1', 'description' => 'My first badge']))
->issue($user->id, true);
($badgetwo = $generator->create_badge(['name' => 'Badge 2', 'description' => 'My second badge']))
->issue($user->id, true);
/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
$report = $generator->create_report(['name' => 'Badges', 'source' => users::class, 'default' => 1]);
$content = $this->get_custom_report_content($report->get('id'));
$this->assertCount(3, $content);
// Default columns are user, badge, description, issued time. Sorted by user, badge, issued time.
[$userfullname, $badgename, $badgedescription, $badgeissued] = array_values($content[0]);
$this->assertEquals('Admin User', $userfullname);
$this->assertEmpty($badgename);
$this->assertEmpty($badgedescription);
$this->assertEmpty($badgeissued);
[$userfullname, $badgename, $badgedescription, $badgeissued] = array_values($content[1]);
$this->assertEquals(fullname($user), $userfullname);
$this->assertEquals($badgeone->name, $badgename);
$this->assertEquals($badgeone->description, $badgedescription);
$this->assertNotEmpty($badgeissued);
[$userfullname, $badgename, $badgedescription, $badgeissued] = array_values($content[2]);
$this->assertEquals(fullname($user), $userfullname);
$this->assertEquals($badgetwo->name, $badgename);
$this->assertEquals($badgetwo->description, $badgedescription);
$this->assertNotEmpty($badgeissued);
}
/**
* Test datasource columns that aren't added by default
*/
public function test_datasource_non_default_columns(): void {
global $DB;
$this->resetAfterTest();
$course = $this->getDataGenerator()->create_course();
$user = $this->getDataGenerator()->create_and_enrol($course, 'student', ['firstname' => 'Zoe', 'lastname' => 'Zebra']);
$cohort = $this->getDataGenerator()->create_cohort(['name' => 'My cohort']);
cohort_add_member($cohort->id, $user->id);
/** @var core_badges_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_badges');
$badgesite = $generator->create_badge([
'name' => 'Badge 1',
'language' => 'de',
'expireperiod' => HOURSECS,
'tags' => ['cool'],
]);
$badgecourse = $generator->create_badge([
'name' => 'Badge 2',
'type' => BADGE_TYPE_COURSE,
'courseid' => $course->id,
]);
$badgesite->issue($user->id, true);
$badgecourse->issue($user->id, true);
// Create criteria for manually awarding by role.
$managerrole = $DB->get_field('role', 'id', ['shortname' => 'manager']);
$generator->create_criteria(['badgeid' => $badgesite->id, 'roleid' => $managerrole]);
/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
$report = $generator->create_report(['name' => 'Badges', 'source' => users::class, 'default' => 0]);
// These two columns have been asserted previously, we're only adding them for consistent sorting.
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:fullname', 'sortenabled' => 1]);
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'badge:name', 'sortenabled' => 1]);
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'badge:namewithlink']);
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'badge:criteria']);
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'badge:image']);
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'badge:language']);
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'badge:version']);
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'badge:status']);
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'badge:expiry']);
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'tag:name']);
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'badge_issued:expire']);
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'badge_issued:visible']);
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'course:fullname']);
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'cohort:name']);
$content = $this->get_custom_report_content($report->get('id'));
$this->assertCount(3, $content);
// Admin user, no badge issued.
[$fullname, $badgename, $namewithlink, $criteria, $image, $language, $version, $status, $expiry, $tag, $expires,
$visible, $coursename, $cohortname] = array_values($content[0]);
$this->assertEquals('Admin User', $fullname);
$this->assertEmpty($badgename);
$this->assertEmpty($namewithlink);
$this->assertEmpty($criteria);
$this->assertEmpty($image);
$this->assertEmpty($language);
$this->assertEmpty($version);
$this->assertEmpty($status);
$this->assertEmpty($expiry);
$this->assertEmpty($tag);
$this->assertEmpty($expires);
$this->assertEmpty($visible);
$this->assertEmpty($coursename);
$this->assertEmpty($cohortname);
$expectedbadgesitelink = \html_writer::link(new \moodle_url('/badges/overview.php',
['id' => $badgesite->id]), ($badgesite->name));
// User issued site badge.
[$fullname, $badgename, $namewithlink, $criteria, $image, $language, $version, $status, $expiry, $tag, $expires,
$visible, $coursename, $cohortname] = array_values($content[1]);
$this->assertEquals(fullname($user), $fullname);
$this->assertEquals($badgesite->name, $badgename);
$this->assertEquals($expectedbadgesitelink, $namewithlink);
$this->assertStringContainsString('Awarded by: Manager', $criteria);
$this->assertStringContainsString('Image caption', $image);
$this->assertEquals('German', $language);
$this->assertEquals(2, $version);
$this->assertEquals('Available (criteria locked)', $status);
$this->assertEquals('1 hour', $expiry);
$this->assertEquals('cool', $tag);
$this->assertNotEmpty($expires);
$this->assertEquals('Yes', $visible);
$this->assertEquals('PHPUnit test site', $coursename);
$this->assertEquals($cohort->name, $cohortname);
$expectedbadgecourselink = \html_writer::link(new \moodle_url('/badges/overview.php',
['id' => $badgecourse->id]), ($badgecourse->name));
// User issued course badge.
[$fullname, $badgename, $namewithlink, $criteria, $image, $language, $version, $status, $expiry, $tag, $expires,
$visible, $coursename, $cohortname] = array_values($content[2]);
$this->assertEquals(fullname($user), $fullname);
$this->assertEquals($badgecourse->name, $badgename);
$this->assertEquals($expectedbadgecourselink, $namewithlink);
$this->assertEquals('Criteria for this badge have not been set up yet.', $criteria);
$this->assertStringContainsString('Image caption', $image);
$this->assertEquals('English', $language);
$this->assertEquals(2, $version);
$this->assertEquals('Available (criteria locked)', $status);
$this->assertEquals('Never', $expiry);
$this->assertEmpty($tag);
$this->assertEmpty($expires);
$this->assertEquals('Yes', $visible);
$this->assertEquals($course->fullname, $coursename);
$this->assertEquals($cohort->name, $cohortname);
}
/**
* Data provider for {@see test_datasource_filters}
*
* @return array[]
*/
public function datasource_filters_provider(): array {
return [
// User.
'Filter user fullname' => ['user:fullname', [
'user:fullname_operator' => text::IS_EQUAL_TO,
'user:fullname_value' => 'Zoe Zebra',
], true],
'Filter user fullname (no match)' => ['user:fullname', [
'user:fullname_operator' => text::IS_EQUAL_TO,
'user:fullname_value' => 'Alice Aardvark',
], false],
// Badge.
'Filter badge name' => ['badge:name', [
'badge:name_operator' => text::IS_EQUAL_TO,
'badge:name_value' => 'Course badge',
], true],
'Filter badge name (no match)' => ['badge:name', [
'badge:name_operator' => text::IS_EQUAL_TO,
'badge:name_value' => 'Other badge',
], false],
'Filter badge status' => ['badge:status', [
'badge:status_operator' => select::EQUAL_TO,
'badge:status_value' => BADGE_STATUS_ACTIVE_LOCKED,
], true],
'Filter badge status (no match)' => ['badge:status', [
'badge:status_operator' => select::EQUAL_TO,
'badge:status_value' => BADGE_STATUS_ACTIVE,
], false],
'Filter badge type' => ['badge:type', [
'badge:type_operator' => select::EQUAL_TO,
'badge:type_value' => BADGE_TYPE_COURSE,
], true],
'Filter badge type (no match)' => ['badge:type', [
'badge:type_operator' => select::EQUAL_TO,
'badge:type_value' => BADGE_TYPE_SITE,
], false],
// Badge tag.
'Filter tag name' => ['tag:name', [
'tag:name_operator' => tags::NOT_EMPTY,
], true],
'Filter tag name (no match)' => ['tag:name', [
'tag:name_operator' => tags::EQUAL_TO,
'tag:name_value' => [-1],
], false],
// Badge issued.
'Filter badge issued date' => ['badge_issued:issued', [
'badge_issued:issued_operator' => date::DATE_RANGE,
'badge_issued:issued_from' => 1622502000,
], true],
'Filter badge issued date (no match)' => ['badge_issued:issued', [
'badge_issued:issued_operator' => date::DATE_RANGE,
'badge_issued:issued_to' => 1622502000,
], false],
'Filter badge issued expires' => ['badge_issued:expires', [
'badge_issued:expires_operator' => date::DATE_RANGE,
'badge_issued:expires_from' => 1622502000,
], true],
'Filter badge issued expires (no match)' => ['badge_issued:expires', [
'badge_issued:expires_operator' => date::DATE_RANGE,
'badge_issued:expires_to' => 1622502000,
], false],
'Filter badge issued visible' => ['badge_issued:visible', [
'badge_issued:visible_operator' => boolean_select::CHECKED,
], true],
'Filter badge issued visible (no match)' => ['badge_issued:visible', [
'badge_issued:visible_operator' => boolean_select::NOT_CHECKED,
], false],
// Course.
'Filter course fullname' => ['course:fullname', [
'course:fullname_operator' => text::IS_EQUAL_TO,
'course:fullname_value' => 'Course 1',
], true],
'Filter course fullname (no match)' => ['course:fullname', [
'course:fullname_operator' => text::IS_EQUAL_TO,
'course:fullname_value' => 'Course 2',
], false],
// Cohort.
'Filter cohort name' => ['cohort:name', [
'cohort:name_operator' => text::IS_EQUAL_TO,
'cohort:name_value' => 'My cohort',
], true],
'Filter cohort name (no match)' => ['cohort:name', [
'cohort:name_operator' => text::IS_EQUAL_TO,
'cohort:name_value' => 'Not my cohort',
], false],
];
}
/**
* Test datasource filters
*
* @param string $filtername
* @param array $filtervalues
* @param bool $expectmatch
*
* @dataProvider datasource_filters_provider
*/
public function test_datasource_filters(string $filtername, array $filtervalues, bool $expectmatch): void {
$this->resetAfterTest();
$course = $this->getDataGenerator()->create_course(['fullname' => 'Course 1']);
$user = $this->getDataGenerator()->create_and_enrol($course, 'student', ['firstname' => 'Zoe', 'lastname' => 'Zebra']);
$cohort = $this->getDataGenerator()->create_cohort(['name' => 'My cohort']);
cohort_add_member($cohort->id, $user->id);
/** @var core_badges_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_badges');
$badge = $generator->create_badge([
'name' => 'Course badge',
'type' => BADGE_TYPE_COURSE,
'courseid' => $course->id,
'expireperiod' => HOURSECS,
'tags' => ['cool'],
]);
$badge->issue($user->id, true);
/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
// Create report containing single username column, and given filter.
$report = $generator->create_report(['name' => 'My report', 'source' => users::class, 'default' => 0]);
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:username']);
// Add filter, set it's values.
$generator->create_filter(['reportid' => $report->get('id'), 'uniqueidentifier' => $filtername]);
$content = $this->get_custom_report_content($report->get('id'), 0, $filtervalues);
if ($expectmatch) {
$this->assertCount(1, $content);
$this->assertEquals($user->username, reset($content[0]));
} else {
$this->assertEmpty($content);
}
}
/**
* Stress test datasource
*
* In order to execute this test PHPUNIT_LONGTEST should be defined as true in phpunit.xml or directly in config.php
*/
public function test_stress_datasource(): void {
if (!PHPUNIT_LONGTEST) {
$this->markTestSkipped('PHPUNIT_LONGTEST is not defined');
}
$this->resetAfterTest();
$course = $this->getDataGenerator()->create_course();
$user = $this->getDataGenerator()->create_and_enrol($course);
/** @var core_badges_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_badges');
$badge = $generator->create_badge(['name' => 'Course badge', 'type' => BADGE_TYPE_COURSE, 'courseid' => $course->id]);
$badge->issue($user->id, true);
$this->datasource_stress_test_columns(users::class);
$this->datasource_stress_test_columns_aggregation(users::class);
$this->datasource_stress_test_conditions(users::class, 'user:username');
}
}