From 1ff279717ea249bd1b958dd74a3ad3a4d65bf904 Mon Sep 17 00:00:00 2001 From: Paul Holden Date: Fri, 31 Mar 2023 14:11:31 +0100 Subject: [PATCH] MDL-73648 badges: add Behat generator class for creating badges. --- badges/tests/behat/backpack.feature | 81 ++++------ badges/tests/behat/badge_navigation.feature | 60 ++++---- badges/tests/behat/criteria_activity.feature | 18 +-- badges/tests/behat/criteria_cohort.feature | 144 ++++++++++-------- .../tests/behat/criteria_competency.feature | 47 +++--- badges/tests/behat/criteria_profile.feature | 14 +- .../generator/behat_core_badges_generator.php | 102 +++++++++++++ badges/tests/generator/lib.php | 92 ++++++++++- 8 files changed, 367 insertions(+), 191 deletions(-) create mode 100644 badges/tests/generator/behat_core_badges_generator.php diff --git a/badges/tests/behat/backpack.feature b/badges/tests/behat/backpack.feature index 3f584b6d1f0..17f1ee4eda6 100644 --- a/badges/tests/behat/backpack.feature +++ b/badges/tests/behat/backpack.feature @@ -1,4 +1,4 @@ -@core @core_badges @_file_upload +@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 @@ -37,36 +37,21 @@ Feature: Backpack badges @javascript Scenario: Verify backback settings - 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 I navigate to "Badges > Add a new badge" in site administration - And I set the following fields to these values: - | Name | Test badge verify backpack | - | 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" - 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 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 1 (student1@example.com)" - And I press "Award badge" - And I log out - When I am on homepage - And I log in as "student1" + 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 "https://dc.imsglobal.org" @@ -82,26 +67,20 @@ Feature: Backpack badges 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 I navigate to "Badges > Add a new badge" in site administration - And I set the following fields to these values: - | Name | Test badge verify backpack | - | 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" - 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 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 1 (student1@example.com)" - And I press "Award badge" - And I log out + 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 | diff --git a/badges/tests/behat/badge_navigation.feature b/badges/tests/behat/badge_navigation.feature index c14b2689556..12feba6b73c 100644 --- a/badges/tests/behat/badge_navigation.feature +++ b/badges/tests/behat/badge_navigation.feature @@ -1,4 +1,4 @@ -@core @core_badges @_file_upload @javascript +@core @core_badges @javascript Feature: Test tertiary navigation as various users Background: @@ -15,38 +15,32 @@ Feature: Test tertiary navigation as various users | teacher | C1 | editingteacher | | student1 | C1 | student | | nonediting | C1 | teacher | - # Create system badge and define a criterion. - And I log in as "admin" - 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 | Testing course badge | - | Version | 1.1 | - | Language | Catalan | - | 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 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 am on site homepage - And I navigate to "Badges > Add a new badge" in site administration - And I set the following fields to these values: - | Name | Testing site badge | - | Version | 1.1 | - | Language | Catalan | - | Description | Testing site 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" + # 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 "" diff --git a/badges/tests/behat/criteria_activity.feature b/badges/tests/behat/criteria_activity.feature index df981116cb7..b4ecdb1226d 100644 --- a/badges/tests/behat/criteria_activity.feature +++ b/badges/tests/behat/criteria_activity.feature @@ -1,4 +1,4 @@ -@mod @mod_quiz @core @core_badges @_file_upload @javascript +@mod @mod_quiz @core @core_badges @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 @@ -38,15 +38,13 @@ Feature: Award badges based on activity completion And user "student2" has attempted "Test quiz name" with responses: | slot | response | | 1 | False | - And 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 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 | Scenario: Student earns a badge using activity completion, but does not get passing grade Given I am on the "Course 1" course page logged in as teacher1 diff --git a/badges/tests/behat/criteria_cohort.feature b/badges/tests/behat/criteria_cohort.feature index 1a8ec4d4cd6..05112cfafce 100644 --- a/badges/tests/behat/criteria_cohort.feature +++ b/badges/tests/behat/criteria_cohort.feature @@ -1,4 +1,4 @@ -@core @core_badges @_file_upload +@core @core_badges Feature: Award badges based on cohort In order to award badges to users based on their cohort membership As an admin @@ -18,13 +18,15 @@ Feature: Award badges based on cohort | 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 > 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 navigate to "Badges > Manage badges" in site administration + And I click on "Edit" "link" in the "Site Badge" "table_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" @@ -53,13 +55,15 @@ Feature: Award badges based on cohort | 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 > 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 navigate to "Badges > Manage badges" in site administration + And I click on "Edit" "link" in the "Site Badge" "table_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" @@ -93,13 +97,15 @@ Feature: Award badges based on cohort | 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 > 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 navigate to "Badges > Manage badges" in site administration + And I click on "Edit" "link" in the "Site Badge" "table_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" @@ -129,13 +135,15 @@ Feature: Award badges based on cohort | 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 > 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 navigate to "Badges > Manage badges" in site administration + And I click on "Edit" "link" in the "Site Badge" "table_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" @@ -180,13 +188,15 @@ Feature: Award badges based on 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 > 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 navigate to "Badges > Manage badges" in site administration + And I click on "Edit" "link" in the "Site Badge" "table_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" @@ -237,13 +247,15 @@ Feature: Award badges based on cohort | 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 > 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 navigate to "Badges > Manage badges" in site administration + And I click on "Edit" "link" in the "Site Badge" "table_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" @@ -294,13 +306,15 @@ Feature: Award badges based on cohort | 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 > 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 navigate to "Badges > Manage badges" in site administration + And I click on "Edit" "link" in the "Site Badge" "table_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" @@ -352,25 +366,23 @@ Feature: Award badges based on 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 > Add a new badge" in site administration - And I set the following fields to these values: - | Name | Site Badge 1 | - | Description | Site badge description | - And I upload "badges/tests/behat/badge.png" file to "Image" filemanager - And I press "Create badge" + And I navigate to "Badges > Manage badges" in site administration + And I click on "Edit" "link" in the "Site Badge 1" "table_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 > Add a new badge" in site administration - And I set the following fields to these values: - | Name | Site Badge 2 | - | Description | Site badge description | - And I upload "badges/tests/behat/badge.png" file to "Image" filemanager - And I press "Create badge" + And I navigate to "Badges > Manage badges" in site administration + And I click on "Edit" "link" in the "Site Badge 2" "table_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" @@ -411,13 +423,14 @@ Feature: Award badges based on cohort | 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 > Add a new badge" in site administration - And I set the following fields to these values: - | Name | Site Badge 1 | - | Description | Site badge description | - And I upload "badges/tests/behat/badge.png" file to "Image" filemanager - And I press "Create badge" + And I navigate to "Badges > Manage badges" in site administration + And I click on "Edit" "link" in the "Site Badge 1" "table_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" @@ -426,12 +439,9 @@ Feature: Award badges based on cohort And I press "Enable access" When I press "Continue" And I should see "Recipients (1)" - And I navigate to "Badges > Add a new badge" in site administration - And I set the following fields to these values: - | Name | Site Badge 2 | - | Description | Site badge description | - And I upload "badges/tests/behat/badge.png" file to "Image" filemanager - And I press "Create badge" + And I navigate to "Badges > Manage badges" in site administration + And I click on "Edit" "link" in the "Site Badge 2" "table_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" diff --git a/badges/tests/behat/criteria_competency.feature b/badges/tests/behat/criteria_competency.feature index 5e588e08bf6..35a7a119419 100644 --- a/badges/tests/behat/criteria_competency.feature +++ b/badges/tests/behat/criteria_competency.feature @@ -1,4 +1,4 @@ -@core @core_badges @_file_upload +@core @core_badges Feature: Award badges based on competency completion In order to award badges to users based on competency completion As an admin @@ -25,6 +25,13 @@ Feature: Award badges based on competency completion @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 @@ -39,12 +46,10 @@ Feature: Award badges based on competency completion # 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 > 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 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 @@ -72,6 +77,11 @@ Feature: Award badges based on competency completion @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 @@ -83,12 +93,9 @@ Feature: Award badges based on competency completion 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 > 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 navigate to "Badges > Manage badges" in site administration + And I click on "Edit" "link" in the "Site Badge" "table_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" @@ -123,6 +130,11 @@ Feature: Award badges based on competency completion @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 @@ -134,12 +146,9 @@ Feature: Award badges based on competency completion 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 > 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 navigate to "Badges > Manage badges" in site administration + And I click on "Edit" "link" in the "Site Badge" "table_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" diff --git a/badges/tests/behat/criteria_profile.feature b/badges/tests/behat/criteria_profile.feature index 93be0eddaab..bfbb064626b 100644 --- a/badges/tests/behat/criteria_profile.feature +++ b/badges/tests/behat/criteria_profile.feature @@ -9,13 +9,15 @@ Feature: Award badges based on user profile field 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 > 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 navigate to "Badges > Manage badges" in site administration + And I click on "Edit" "link" in the "Site Badge" "table_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" diff --git a/badges/tests/generator/behat_core_badges_generator.php b/badges/tests/generator/behat_core_badges_generator.php new file mode 100644 index 00000000000..eeb13fb3e56 --- /dev/null +++ b/badges/tests/generator/behat_core_badges_generator.php @@ -0,0 +1,102 @@ +. + +declare(strict_types=1); + +/** + * Badges test generator for Behat + * + * @package core_badges + * @copyright 2023 Paul Holden + * @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', + ], + '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; + } +} diff --git a/badges/tests/generator/lib.php b/badges/tests/generator/lib.php index 6f990c189b2..242b5d46a55 100644 --- a/badges/tests/generator/lib.php +++ b/badges/tests/generator/lib.php @@ -35,12 +35,17 @@ class core_badges_generator extends component_generator_base { /** * Create badge * - * TODO: MDL-73648 Use from Behat too - * * @param array|stdClass $record + * @return badge */ public function create_badge($record): badge { - global $DB, $USER; + global $CFG, $DB, $USER; + + $record = (array) $record; + + // Save badge image path for later. + $badgeimage = $record['image'] ?? ''; + unset($record['image']); $record = (object) array_merge([ 'name' => 'Test badge', @@ -67,10 +72,87 @@ class core_badges_generator extends component_generator_base { 'imageauthoremail' => 'author@example.com', 'imageauthorurl' => 'http://image.example.com/', 'imagecaption' => 'Image caption' - ], (array) $record); + ], $record); $record->id = $DB->insert_record('badge', $record); + $badge = new badge($record->id); - return 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()); + } + + 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); } }