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
@@ -0,0 +1,128 @@
@tool @tool_uploadcourse @_file_upload
Feature: An admin can create courses with cohort enrolments using a CSV file
In order to create courses using a CSV file with cohort enrolment
As an admin
I need to be able to upload a CSV file and navigate through the import process
Background:
Given the following "categories" exist:
| name | category | idnumber |
| Cat 0 | 0 | CAT0 |
| Cat 1 | CAT0 | CAT1 |
| Cat 1 | CAT0 | CAT2 |
And the following "cohorts" exist:
| name | idnumber | contextlevel | reference | visible |
| Cohort 1 | CV1 | Category | CAT1 | 1 |
| Cohort 2 | CV2 | Category | CAT2 | 1 |
| Cohort 3 | CV3 | Category | CAT2 | 1 |
| Cohort 4 | CV4 | Category | CAT1 | 1 |
| Cohort 5 | CV5 | Category | CAT1 | 1 |
And the following "courses" exist:
| fullname | shortname | category |
| Course 1 | C1 | CAT1 |
And I log in as "admin"
And I navigate to "Courses > Upload courses" in site administration
And I set the field "Upload mode" to "Create new courses, or update existing ones"
And I set the field "Update mode" to "Update with CSV data only"
@javascript
Scenario: Upload cohort enrolment if plugin is disabled
Given the following config values are set as admin:
| enrol_plugins_enabled | manual,guest,self |
And I upload "admin/tool/uploadcourse/tests/fixtures/enrolment_cohort.csv" file to "File" filemanager
When I click on "Preview" "button"
Then I should see "Cohort sync enrol plugin is disabled"
@javascript
Scenario: Validation of cohorts for uploaded courses
Given I upload "admin/tool/uploadcourse/tests/fixtures/enrolment_cohort.csv" file to "File" filemanager
And I click on "Preview" "button"
And I should see "Unknown cohort (Not exist)!"
And I should see "Cohort CV3 not allowed in this context."
When I click on "Upload courses" "button"
And I should see "Unknown cohort (Not exist)!"
And I should see "Cohort CV3 not allowed in this context."
And I should see "Cohort CV4 not allowed in this context."
And I should see "Invalid role names: student1"
And I should see "Courses created: 2"
And I should see "Courses updated: 0"
And I should see "Courses errors: 4"
And I am on the "Course 1" "enrolment methods" page
Then I should not see "Cohort sync (Cohort 3 - Student)"
And I am on the "Course 2" "enrolment methods" page
And I should not see "Cohort sync (Cohort 4 - Student)"
And I am on the "Course 3" "enrolment methods" page
And I should see "Cohort sync (Cohort 5 - Student)"
And I click on "Edit" "link" in the "Cohort 5" "table_row"
And the field "Add to group" matches value "None"
And I navigate to "Courses > Upload courses" in site administration
And I set the field "Upload mode" to "Create new courses, or update existing ones"
And I set the field "Update mode" to "Update with CSV data only"
And I upload "admin/tool/uploadcourse/tests/fixtures/enrolment_cohort_missing_fields.csv" file to "File" filemanager
And I click on "Preview" "button"
And I should see "Missing value for mandatory fields: cohortidnumber, role"
And "Upload courses" "button" should exist
@javascript
Scenario: Validation of groups for uploaded courses with cohort enrolments
Given the following "groups" exist:
| name | course | idnumber |
| group1 | C1 | G1 |
# Test that groupname can't be set when addtogroup is used.
And I upload "admin/tool/uploadcourse/tests/fixtures/enrolment_cohort_addtogroup_groupname.csv" file to "File" filemanager
And I click on "Preview" "button"
And I should see "You cannot specify groupname when addtogroup is set."
# Test creating a new group when uploading a course.
And I navigate to "Courses > Upload courses" in site administration
And I set the field "Upload mode" to "Create new courses, or update existing ones"
And I set the field "Update mode" to "Update with CSV data only"
And I upload "admin/tool/uploadcourse/tests/fixtures/enrolment_cohort_addtogroup.csv" file to "File" filemanager
And I click on "Preview" "button"
And I click on "Upload courses" "button"
And I should see "Courses created: 2"
And I should see "Courses errors: 0"
And I am on the "Course 2" "enrolment methods" page
And I should see "Cohort sync (Cohort 2 - Student)"
And I click on "Edit" "link" in the "Cohort 2" "table_row"
And the field "Add to group" matches value "Cohort 2 cohort"
And I am on the "Course 2" "groups" page
And I should see "Cohort 2 cohort"
And I am on the "Course 3" "enrolment methods" page
And I should see "Cohort sync (Cohort 1 - Student)"
And I click on "Edit" "link" in the "Cohort 1" "table_row"
And the field "Add to group" matches value "None"
And I am on the "Course 3" "groups" page
And I should not see "Cohort 1 cohort"
# Test assigning to an existing group when uploading a course.
And I navigate to "Courses > Upload courses" in site administration
And I set the field "Upload mode" to "Create new courses, or update existing ones"
And I set the field "Update mode" to "Update with CSV data only"
And I upload "admin/tool/uploadcourse/tests/fixtures/enrolment_cohort_groups.csv" file to "File" filemanager
And I click on "Preview" "button"
And I should see "Error, invalid group notexist"
When I click on "Upload courses" "button"
And I should see "Error, invalid group notexist"
And I should see "Courses updated: 1"
And I should see "Courses errors: 1"
And I am on the "Course 1" "enrolment methods" page
Then I should see "Cohort sync (Cohort 1 - Student)"
And I click on "Edit" "link" in the "Cohort 1" "table_row"
And the field "Add to group" matches value "group1"
And I am on the "Course 1" "groups" page
And I should see "group1"
@javascript
Scenario: Upload multiple enrolment methods of same type to the same course
Given I upload "admin/tool/uploadcourse/tests/fixtures/enrolment_cohort_multiple.csv" file to "File" filemanager
And I click on "Preview" "button"
When I click on "Upload courses" "button"
And I should see "Courses updated: 2"
And I am on the "Course 1" "enrolment methods" page
Then I should see "Cohort sync (Cohort 1 - Student)"
And I should see "Cohort sync (Cohort 4 - Non-editing teacher)"
And I click on "Edit" "link" in the "Cohort 1" "table_row"
And the field "Assign role" matches value "Student"
And I press "Cancel"
And I click on "Edit" "link" in the "Cohort 4" "table_row"
And the field "Assign role" matches value "Non-editing teacher"
@@ -0,0 +1,183 @@
@tool @tool_uploadcourse @_file_upload
Feature: An admin can create courses using a CSV file
In order to create courses using a CSV file
As an admin
I need to be able to upload a CSV file and navigate through the import process
Background:
Given the following "courses" exist:
| fullname | shortname | category |
| First course | C1 | 0 |
And I log in as "admin"
And I navigate to "Courses > Upload courses" in site administration
@javascript
Scenario: Creation of unexisting courses
Given I upload "admin/tool/uploadcourse/tests/fixtures/courses.csv" file to "File" filemanager
And I click on "Preview" "button"
When I click on "Upload courses" "button"
Then I should see "The course exists and update is not allowed"
And I should see "Course created"
And I should see "Courses total: 3"
And I should see "Courses created: 2"
And I should see "Courses errors: 1"
And I am on site homepage
And I should see "Course 2"
And I should see "Course 3"
@javascript
Scenario: Creation of existing courses
Given I upload "admin/tool/uploadcourse/tests/fixtures/courses.csv" file to "File" filemanager
And I set the field "Upload mode" to "Create all, increment shortname if needed"
And I click on "Preview" "button"
When I click on "Upload courses" "button"
Then I should see "Course created"
And I should see "Course shortname incremented C1 -> C2"
And I should see "Course shortname incremented C2 -> C3"
And I should see "Course shortname incremented C3 -> C4"
And I should see "Courses total: 3"
And I should see "Courses created: 3"
And I should see "Courses errors: 0"
And I am on site homepage
And I should see "Course 1"
And I should see "Course 2"
And I should see "Course 3"
@javascript
Scenario: Creation of new courses with custom fields
Given the following "custom field categories" exist:
| name | component | area | itemid |
| Other | core_course | course | 0 |
And the following "custom fields" exist:
| name | category | type | shortname | configdata |
| Field 1 | Other | checkbox | checkbox | |
| Field 2 | Other | date | date | |
| Field 3 | Other | select | select | {"options":"a\nb\nc"} |
| Field 4 | Other | text | text | |
| Field 5 | Other | textarea | textarea | |
When I upload "admin/tool/uploadcourse/tests/fixtures/courses_custom_fields.csv" file to "File" filemanager
And I set the field "Upload mode" to "Create new courses only, skip existing ones"
And I click on "Preview" "button"
And I click on "Upload courses" "button"
Then I should see "Course created"
And I should see "Courses created: 1"
And I am on site homepage
And I should see "Course fields 1"
And I should see "Field 1: Yes"
And I should see "Field 2: Tuesday, 1 October 2019, 2:00"
And I should see "Field 3: b"
And I should see "Field 4: Hello"
And I should see "Field 5: Goodbye"
@javascript
Scenario: Creation of new courses with custom fields using defaults
Given the following "custom field categories" exist:
| name | component | area | itemid |
| Other | core_course | course | 0 |
And the following "custom fields" exist:
| name | category | type | shortname | configdata |
| Field 1 | Other | checkbox | checkbox | {"checkbydefault":1} |
| Field 2 | Other | date | date | {"includetime":0} |
| Field 3 | Other | select | select | {"options":"a\nb\nc","defaultvalue":"b"} |
| Field 4 | Other | text | text | {"defaultvalue":"Hello"} |
| Field 5 | Other | textarea | textarea | {"defaultvalue":"Some text","defaultvalueformat":1} |
When I upload "admin/tool/uploadcourse/tests/fixtures/courses.csv" file to "File" filemanager
And I set the field "Upload mode" to "Create all, increment shortname if needed"
And I click on "Preview" "button"
And I expand all fieldsets
And the field "Field 1" matches value "1"
And the field "Field 3" matches value "b"
And the field "Field 4" matches value "Hello"
And the field "Field 5" matches value "Some text"
# We have to enable the date field manually.
And I set the following fields to these values:
| customfield_date[enabled] | 1 |
| customfield_date[day] | 1 |
| customfield_date[month] | June |
| customfield_date[year] | 2020 |
And I click on "Upload courses" "button"
Then I should see "Course created"
And I should see "Courses created: 3"
And I am on site homepage
And I should see "Course 1"
And I should see "Field 1: Yes"
And I should see "Field 2: 1 June 2020"
And I should see "Field 3: b"
And I should see "Field 4: Hello"
And I should see "Field 5: Some text"
@javascript
Scenario: Validation of role for uploaded courses
Given I navigate to "Users > Permissions > Define roles" in site administration
And I click on "Add a new role" "button"
And I click on "Continue" "button"
And I set the following fields to these values:
| Short name | notallowed |
| Custom full name | notallowed |
| contextlevel80 | 1 |
And I click on "Create this role" "button"
And I navigate to "Courses > Upload courses" in site administration
And I upload "admin/tool/uploadcourse/tests/fixtures/enrolment_role.csv" file to "File" filemanager
And I click on "Preview" "button"
And I should see "Invalid role names: notexist"
And I should see "Role notallowed not allowed in this context."
When I click on "Upload courses" "button"
And I should see "Course created"
And I should see "Courses total: 3"
And I should see "Courses created: 1"
And I should see "Courses errors: 2"
And I should see "Invalid role names: notexist"
And I should see "Role notallowed not allowed in this context."
And I am on site homepage
And I should see "coursez"
@javascript
Scenario: Unsupported enrol methods are not created
Given the following config values are set as admin:
| enrol_plugins_enabled | manual,guest,lti |
And I set the field "Upload mode" to "Create new courses, or update existing ones"
And I set the field "Update mode" to "Update with CSV data only"
And I upload "admin/tool/uploadcourse/tests/fixtures/unsupported_enrol_method.csv" file to "File" filemanager
And I click on "Preview" "button"
When I click on "Upload courses" "button"
Then I should see "Course created"
And I should see "Enrolment method 'enrol_lti_plugin' is not supported in csv upload"
And I am on the "C2" "enrolment methods" page
And I should see "manualtest"
And I should not see "ltitest"
@javascript
Scenario: Manager can use upload course tool in course category
Given the following "users" exist:
| username | firstname | lastname | email |
| user1 | User | 1 | user1@example.com |
And the following "categories" exist:
| name | category | idnumber |
| Cat 1 | 0 | CAT1 |
| Cat 2 | 0 | CAT2 |
| Cat 3 | CAT1 | CAT3 |
And the following "role assigns" exist:
| user | role | contextlevel | reference |
| user1 | manager | Category | CAT1 |
When I log in as "user1"
And I am on course index
And I follow "Cat 1"
And I navigate to "Upload courses" in current page administration
And I upload "admin/tool/uploadcourse/tests/fixtures/courses_manager1.csv" file to "File" filemanager
And I click on "Preview" "button"
Then I should see "The course exists and update is not allowed" in the "C1" "table_row"
And I should see "No permission to upload courses in category: Cat 2" in the "C2" "table_row"
And I set the field "Course category" to "Cat 1 / Cat 3"
And I click on "Upload courses" "button"
And I should see "Course created"
And I should see "Courses total: 5"
And I should see "Courses created: 3"
And I should see "Courses errors: 2"
And I am on course index
And I follow "Cat 1"
And I should see "Course 4"
And I follow "Cat 3"
And I should see "Course 5"
# Course 3 did not have category specified in CSV file and it was uploaded to the current category.
And I should see "Course 3"
And I log out
@@ -0,0 +1,156 @@
@tool @tool_uploadcourse @_file_upload
Feature: An admin can update courses enrolments using a CSV file
In order to update courses enrolments using a CSV file
As an admin
I need to be able to upload a CSV file with enrolment methods for the courses
Background:
Given the following "courses" exist:
| fullname | shortname | category |
| Course 1 | C1 | 0 |
And I log in as "admin"
@javascript
Scenario: Creating enrolment method by enable it
Given I am on the "Course 1" "enrolment methods" page
And I click on "Delete" "link" in the "Guest access" "table_row"
And I click on "Continue" "button"
And I should not see "Guest access" in the "generaltable" "table"
And I navigate to "Courses > Upload courses" in site administration
And I upload "admin/tool/uploadcourse/tests/fixtures/enrolment_enable.csv" file to "File" filemanager
And I set the field "Upload mode" to "Only update existing courses"
And I set the field "Update mode" to "Update with CSV data only"
And I set the field "Allow deletes" to "Yes"
And I click on "Preview" "button"
When I click on "Upload courses" "button"
Then I should see "Course updated"
And I am on the "Course 1" "enrolment methods" page
And "Disable" "icon" should exist in the "Guest access" "table_row"
@javascript
Scenario: Creating enrolment method by disabling it
Given I am on the "Course 1" "enrolment methods" page
And I click on "Delete" "link" in the "Guest access" "table_row"
And I click on "Continue" "button"
And I should not see "Guest access" in the "generaltable" "table"
And I navigate to "Courses > Upload courses" in site administration
And I upload "admin/tool/uploadcourse/tests/fixtures/enrolment_disable.csv" file to "File" filemanager
And I set the field "Upload mode" to "Only update existing courses"
And I set the field "Update mode" to "Update with CSV data only"
And I set the field "Allow deletes" to "Yes"
And I click on "Preview" "button"
When I click on "Upload courses" "button"
Then I should see "Course updated"
And I am on the "Course 1" "enrolment methods" page
And "Enable" "icon" should exist in the "Guest access" "table_row"
@javascript
Scenario: Enabling enrolment method
Given I navigate to "Courses > Upload courses" in site administration
And I upload "admin/tool/uploadcourse/tests/fixtures/enrolment_enable.csv" file to "File" filemanager
And I set the field "Upload mode" to "Only update existing courses"
And I set the field "Update mode" to "Update with CSV data only"
And I set the field "Allow deletes" to "Yes"
And I click on "Preview" "button"
When I click on "Upload courses" "button"
Then I should see "Course updated"
And I am on the "Course 1" "enrolment methods" page
And "Disable" "icon" should exist in the "Guest access" "table_row"
@javascript
Scenario: Disable an enrolment method
Given I am on the "Course 1" "enrolment methods" page
And I click on "Enable" "link" in the "Guest access" "table_row"
And "Disable" "icon" should exist in the "Guest access" "table_row"
And I navigate to "Courses > Upload courses" in site administration
And I upload "admin/tool/uploadcourse/tests/fixtures/enrolment_disable.csv" file to "File" filemanager
And I set the field "Upload mode" to "Only update existing courses"
And I set the field "Update mode" to "Update with CSV data only"
And I set the field "Allow deletes" to "Yes"
And I click on "Preview" "button"
When I click on "Upload courses" "button"
Then I should see "Course updated"
And I am on the "Course 1" "enrolment methods" page
And "Enable" "icon" should exist in the "Guest access" "table_row"
@javascript
Scenario: Delete an enrolment method
Given I navigate to "Courses > Upload courses" in site administration
And I upload "admin/tool/uploadcourse/tests/fixtures/enrolment_delete.csv" file to "File" filemanager
And I set the field "Upload mode" to "Only update existing courses"
And I set the field "Update mode" to "Update with CSV data only"
And I set the field "Allow deletes" to "Yes"
And I click on "Preview" "button"
When I click on "Upload courses" "button"
Then I should see "Course updated"
And I am on the "Course 1" "enrolment methods" page
And I should not see "Guest access" in the "generaltable" "table"
@javascript
Scenario: Delete an unexistent enrolment method (nothing should change)
Given I am on the "Course 1" "enrolment methods" page
And I click on "Delete" "link" in the "Guest access" "table_row"
And I click on "Continue" "button"
And I should not see "Guest access" in the "generaltable" "table"
And I navigate to "Courses > Upload courses" in site administration
And I upload "admin/tool/uploadcourse/tests/fixtures/enrolment_delete.csv" file to "File" filemanager
And I set the field "Upload mode" to "Only update existing courses"
And I set the field "Update mode" to "Update with CSV data only"
And I set the field "Allow deletes" to "Yes"
And I click on "Preview" "button"
When I click on "Upload courses" "button"
Then I should see "Course updated"
And I am on the "Course 1" "enrolment methods" page
And I should not see "Guest access" in the "generaltable" "table"
@javascript
Scenario: Re-upload a file using CSV data only after deleting the enrolments method
Given I navigate to "Plugins > Enrolments > Manage enrol plugins" in site administration
And I click on "Enable" "link" in the "Course meta link" "table_row"
And the following "cohort" exists:
| name | Cohort1 |
| idnumber | Cohort1 |
And the following "category" exists:
| name | Cat 1 |
| category | 0 |
| idnumber | CAT1 |
And I navigate to "Courses > Upload courses" in site administration
And I upload "admin/tool/uploadcourse/tests/fixtures/enrolment_multiple.csv" file to "File" filemanager
And I click on "Preview" "button"
And I click on "Upload courses" "button"
And I am on the "Course 2" "enrolment methods" page
And I should see "Self enrolment (Student)" in the "generaltable" "table"
And I should see "Cohort sync (Cohort1 - Student)" in the "generaltable" "table"
And I should see "Guest access" in the "generaltable" "table"
And I should see "Manual enrolments" in the "generaltable" "table"
And I should see "Course meta link (Course 1)" in the "generaltable" "table"
And I navigate to "Courses > Upload courses" in site administration
# Delete all enrolment methods.
And I upload "admin/tool/uploadcourse/tests/fixtures/enrolment_multiple_delete.csv" file to "File" filemanager
And I set the field "Upload mode" to "Only update existing courses"
And I set the field "Update mode" to "Update with CSV data only"
And I set the field "Allow deletes" to "Yes"
And I click on "Preview" "button"
And I click on "Upload courses" "button"
And I should see "Course updated"
And I am on the "Course 2" "enrolment methods" page
And I should not see "Self enrolment (Student)" in the "generaltable" "table"
And I should not see "Cohort sync (Cohort1 - Student)" in the "generaltable" "table"
And I should not see "Guest access" in the "generaltable" "table"
And I should not see "Manual enrolments" in the "generaltable" "table"
And I should not see "Course meta link (Course 1)" in the "generaltable" "table"
# Re-upload again the CSV file, to add again the enrolment methods.
And I navigate to "Courses > Upload courses" in site administration
When I upload "admin/tool/uploadcourse/tests/fixtures/enrolment_multiple.csv" file to "File" filemanager
And I set the field "Upload mode" to "Only update existing courses"
And I set the field "Update mode" to "Update with CSV data only"
And I set the field "Allow deletes" to "Yes"
And I click on "Preview" "button"
And I click on "Upload courses" "button"
And I am on the "Course 2" "enrolment methods" page
Then I should see "Self enrolment (Student)" in the "generaltable" "table"
And I should see "Cohort sync (Cohort1 - Student)" in the "generaltable" "table"
And I should see "Guest access" in the "generaltable" "table"
And I should see "Manual enrolments" in the "generaltable" "table"
And I should see "Course meta link (Course 1)" in the "generaltable" "table"
And I navigate to "Courses > Upload courses" in site administration
@@ -0,0 +1,104 @@
@tool @tool_uploadcourse @_file_upload
Feature: An admin can create courses with guest enrolments using a CSV file
In order to create courses using a CSV file with guest enrolment
As an admin
I need to be able to upload a CSV file and navigate through the import process
Background:
Given the following "categories" exist:
| name | category | idnumber |
| Cat 0 | 0 | CAT0 |
| Cat 1 | CAT0 | CAT1 |
And the following "courses" exist:
| fullname | shortname | category |
| Course 1 | C1 | CAT1 |
And I log in as "admin"
And I navigate to "Courses > Upload courses" in site administration
And I set the field "Upload mode" to "Create new courses, or update existing ones"
And I set the field "Update mode" to "Update with CSV data only"
@javascript
Scenario: Validation of password for uploaded courses with guest enrolments
# usepasswordpolicy is not set.
Given I upload "admin/tool/uploadcourse/tests/fixtures/enrolment_guest.csv" file to "File" filemanager
And I click on "Preview" "button"
And I click on "Upload courses" "button"
And I should see "Courses created: 2"
And I should see "Courses updated: 1"
And I should see "Courses errors: 0"
And I am on the "Course 1" "enrolment methods" page
And I click on "Edit" "link" in the "Guest access" "table_row"
And the field "Password" matches value "test"
And I press "Cancel"
And I click on "Delete" "link" in the "Guest access" "table_row"
And I press "Continue"
And I am on the "Course 2" "enrolment methods" page
And I click on "Edit" "link" in the "Guest access" "table_row"
And the field "Password" matches value ""
And I press "Cancel"
And I click on "Delete" "link" in the "Guest access" "table_row"
And I press "Continue"
And I am on the "Course 3" "enrolment methods" page
And I click on "Edit" "link" in the "Guest access" "table_row"
And the field "Password" matches value "Test123@"
And I press "Cancel"
And I click on "Delete" "link" in the "Guest access" "table_row"
And I press "Continue"
# Policy is used, but password not required so it will not be generated if omitted.
And the following config values are set as admin:
| config | value | plugin |
| usepasswordpolicy | 1 | enrol_guest |
And I navigate to "Courses > Upload courses" in site administration
And I set the field "Upload mode" to "Create new courses, or update existing ones"
And I set the field "Update mode" to "Update with CSV data only"
And I upload "admin/tool/uploadcourse/tests/fixtures/enrolment_guest.csv" file to "File" filemanager
And I click on "Preview" "button"
And I should see "Passwords must be at least 8 characters long."
And I should see "Passwords must have at least 1 digit(s)."
And I should see "Passwords must have at least 1 upper case letter(s)."
And I should see "The password must have at least 1 special character(s) such as *, -, or #."
And I click on "Upload courses" "button"
And I should see "Courses created: 0"
And I should see "Courses updated: 2"
And I should see "Courses errors: 1"
And I am on the "Course 1" "enrolment methods" page
And "Guest access" "table_row" should not exist
And I am on the "Course 2" "enrolment methods" page
And I click on "Edit" "link" in the "Guest access" "table_row"
And the field "Password" matches value ""
And I press "Cancel"
And I click on "Delete" "link" in the "Guest access" "table_row"
And I press "Continue"
And I am on the "Course 3" "enrolment methods" page
And I click on "Edit" "link" in the "Guest access" "table_row"
And the field "Password" matches value "Test123@"
And I press "Cancel"
And I click on "Delete" "link" in the "Guest access" "table_row"
And I press "Continue"
# Policy is used and password not required so it will be generated if omitted.
And the following config values are set as admin:
| config | value | plugin |
| requirepassword | 1 | enrol_guest |
And I navigate to "Courses > Upload courses" in site administration
And I set the field "Upload mode" to "Create new courses, or update existing ones"
And I set the field "Update mode" to "Update with CSV data only"
And I upload "admin/tool/uploadcourse/tests/fixtures/enrolment_guest.csv" file to "File" filemanager
And I click on "Preview" "button"
And I should see "Passwords must be at least 8 characters long."
And I should see "Passwords must have at least 1 digit(s)."
And I should see "Passwords must have at least 1 upper case letter(s)."
And I should see "The password must have at least 1 special character(s) such as *, -, or #."
And I click on "Upload courses" "button"
And I should see "Courses created: 0"
And I should see "Courses updated: 2"
And I should see "Courses errors: 1"
And I am on the "Course 1" "enrolment methods" page
And "Guest access" "table_row" should not exist
And I am on the "Course 2" "enrolment methods" page
And I click on "Edit" "link" in the "Guest access" "table_row"
And the field "Password" does not match value ""
And I am on the "Course 3" "enrolment methods" page
And I click on "Edit" "link" in the "Guest access" "table_row"
And the field "Password" matches value "Test123@"
@@ -0,0 +1,88 @@
@tool @tool_uploadcourse @_file_upload
Feature: An admin can create courses with meta enrolments using a CSV file
In order to create courses using a CSV file with meta enrolment
As an admin
I need to be able to upload a CSV file and navigate through the import process
Background:
Given the following "categories" exist:
| name | category | idnumber |
| Cat 0 | 0 | CAT0 |
| Cat 1 | CAT0 | CAT1 |
| Cat 1 | CAT0 | CAT2 |
And the following "courses" exist:
| fullname | shortname | category |
| Course 1 | C1 | CAT1 |
| Course 2 | C2 | CAT1 |
And I log in as "admin"
And I navigate to "Plugins > Enrolments > Manage enrol plugins" in site administration
And I click on "Enable" "link" in the "Course meta link" "table_row"
And I navigate to "Courses > Upload courses" in site administration
And I set the field "Upload mode" to "Create new courses, or update existing ones"
And I set the field "Update mode" to "Update with CSV data only"
@javascript
Scenario: Validation of meta link course shortname for uploaded courses
Given I upload "admin/tool/uploadcourse/tests/fixtures/enrolment_meta.csv" file to "File" filemanager
And I click on "Preview" "button"
And I should see "Unknown meta course shortname"
And I should see "You can't add a meta link to the same course."
When I click on "Upload courses" "button"
And I should see "Unknown meta course shortname"
And I should see "You can't add a meta link to the same course."
And I should see "Courses created: 1"
And I should see "Courses updated: 0"
And I should see "Courses errors: 2"
And I am on the "Course 4" "enrolment methods" page
Then I should see "Course meta link (Course 1)"
And I click on "Edit" "link" in the "Course meta link" "table_row"
And the field "Add to group" matches value "None"
And I am on the "Course 1" "enrolment methods" page
And I should not see "Course meta link (Course 1)"
@javascript
Scenario: Validation of groups for uploaded courses with meta enrolments
Given the following "groups" exist:
| name | course | idnumber |
| group1 | C2 | G1 |
And I upload "admin/tool/uploadcourse/tests/fixtures/enrolment_meta_addtogroup_groupname.csv" file to "File" filemanager
And I click on "Preview" "button"
And I should see "You cannot specify groupname when addtogroup is set."
And I navigate to "Courses > Upload courses" in site administration
And I set the field "Upload mode" to "Create new courses, or update existing ones"
And I set the field "Update mode" to "Update with CSV data only"
And I upload "admin/tool/uploadcourse/tests/fixtures/enrolment_meta_addtogroup.csv" file to "File" filemanager
And I click on "Preview" "button"
And I click on "Upload courses" "button"
And I should see "Courses created: 2"
And I should see "Courses errors: 0"
And I am on the "Course 3" "enrolment methods" page
And I should see "Course meta link (Course 1)"
And I click on "Edit" "link" in the "Course meta link" "table_row"
And the field "Add to group" matches value "Course 1 course"
And I am on the "Course 3" "groups" page
And I should see "Course 1 course"
And I am on the "Course 4" "enrolment methods" page
And I should see "Course meta link (Course 1)"
And I click on "Edit" "link" in the "Course meta link" "table_row"
And the field "Add to group" matches value "None"
And I am on the "Course 4" "groups" page
And I should not see "Course 1 course"
And I navigate to "Courses > Upload courses" in site administration
And I set the field "Upload mode" to "Create new courses, or update existing ones"
And I set the field "Update mode" to "Update with CSV data only"
And I upload "admin/tool/uploadcourse/tests/fixtures/enrolment_meta_groups.csv" file to "File" filemanager
And I click on "Preview" "button"
And I should see "Error, invalid group notexist"
When I click on "Upload courses" "button"
And I should see "Error, invalid group notexist"
And I should see "Courses updated: 1"
And I should see "Courses errors: 1"
And I am on the "Course 2" "enrolment methods" page
Then I should see "Course meta link (Course 1)"
And I click on "Edit" "link" in the "Course meta link" "table_row"
And the field "Add to group" matches value "group1"
And I am on the "Course 2" "groups" page
And I should see "group1"
@@ -0,0 +1,122 @@
@tool @tool_uploadcourse @_file_upload
Feature: An admin can create courses with self enrolments using a CSV file
In order to create courses using a CSV file with self enrolment
As an admin
I need to be able to upload a CSV file and navigate through the import process
Background:
Given the following "categories" exist:
| name | category | idnumber |
| Cat 0 | 0 | CAT0 |
| Cat 1 | CAT0 | CAT1 |
And the following "courses" exist:
| fullname | shortname | category |
| Course 1 | C1 | CAT1 |
And the following "groups" exist:
| name | course | idnumber | enrolmentkey |
| group1 | C1 | G1 | test |
And I log in as "admin"
And I navigate to "Courses > Upload courses" in site administration
And I set the field "Upload mode" to "Create new courses, or update existing ones"
And I set the field "Update mode" to "Update with CSV data only"
@javascript
Scenario: Validation of password for uploaded courses with self enrolments
# usepasswordpolicy is not set.
Given I upload "admin/tool/uploadcourse/tests/fixtures/enrolment_self.csv" file to "File" filemanager
And I click on "Preview" "button"
And I click on "Upload courses" "button"
And I should see "Courses created: 2"
And I should see "Courses updated: 1"
And I should see "Courses errors: 0"
And I am on the "Course 1" "enrolment methods" page
And I click on "Edit" "link" in the "Self enrolment" "table_row"
And the field "Enrolment key" matches value "test"
And I press "Cancel"
And I click on "Delete" "link" in the "Self enrolment" "table_row"
And I press "Continue"
And I am on the "Course 2" "enrolment methods" page
And I click on "Edit" "link" in the "Self enrolment" "table_row"
And the field "Enrolment key" matches value ""
And I press "Cancel"
And I click on "Delete" "link" in the "Self enrolment" "table_row"
And I press "Continue"
And I am on the "Course 3" "enrolment methods" page
And I click on "Edit" "link" in the "Self enrolment" "table_row"
And the field "Enrolment key" matches value "Test123@"
And I press "Cancel"
And I click on "Delete" "link" in the "Self enrolment" "table_row"
And I press "Continue"
# Policy is used, but password not required so it will not be generated if omitted.
And the following config values are set as admin:
| config | value | plugin |
| usepasswordpolicy | 1 | enrol_self |
And I navigate to "Courses > Upload courses" in site administration
And I set the field "Upload mode" to "Create new courses, or update existing ones"
And I set the field "Update mode" to "Update with CSV data only"
And I upload "admin/tool/uploadcourse/tests/fixtures/enrolment_self.csv" file to "File" filemanager
And I click on "Preview" "button"
And I should see "Passwords must be at least 8 characters long."
And I should see "Passwords must have at least 1 digit(s)."
And I should see "Passwords must have at least 1 upper case letter(s)."
And I should see "The password must have at least 1 special character(s) such as *, -, or #."
And I click on "Upload courses" "button"
And I should see "Courses created: 0"
And I should see "Courses updated: 2"
And I should see "Courses errors: 1"
And I am on the "Course 1" "enrolment methods" page
And "Self enrolment (Student)" "table_row" should not exist
And I am on the "Course 2" "enrolment methods" page
And I click on "Edit" "link" in the "Self enrolment" "table_row"
And the field "Enrolment key" matches value ""
And I press "Cancel"
And I click on "Delete" "link" in the "Self enrolment" "table_row"
And I press "Continue"
And I am on the "Course 3" "enrolment methods" page
And I click on "Edit" "link" in the "Self enrolment" "table_row"
And the field "Enrolment key" matches value "Test123@"
And I press "Cancel"
And I click on "Delete" "link" in the "Self enrolment" "table_row"
And I press "Continue"
# Policy is used and password not required so it will be generated if omitted.
And the following config values are set as admin:
| config | value | plugin |
| requirepassword | 1 | enrol_self |
And I navigate to "Courses > Upload courses" in site administration
And I set the field "Upload mode" to "Create new courses, or update existing ones"
And I set the field "Update mode" to "Update with CSV data only"
And I upload "admin/tool/uploadcourse/tests/fixtures/enrolment_self.csv" file to "File" filemanager
And I click on "Preview" "button"
And I should see "Passwords must be at least 8 characters long."
And I should see "Passwords must have at least 1 digit(s)."
And I should see "Passwords must have at least 1 upper case letter(s)."
And I should see "The password must have at least 1 special character(s) such as *, -, or #."
And I click on "Upload courses" "button"
And I should see "Courses created: 0"
And I should see "Courses updated: 2"
And I should see "Courses errors: 1"
And I am on the "Course 1" "enrolment methods" page
And "Self enrolment (Student)" "table_row" should not exist
And I am on the "Course 2" "enrolment methods" page
And I click on "Edit" "link" in the "Self enrolment" "table_row"
And the field "Enrolment key" does not match value ""
And I am on the "Course 3" "enrolment methods" page
And I click on "Edit" "link" in the "Self enrolment" "table_row"
And the field "Enrolment key" matches value "Test123@"
# Use group enrolment keys is set to yes, so the password must be different from group enrolment key.
And I am on the "Course 1" "enrolment methods" page
And I add "Self enrolment" enrolment method in "Course 1" with:
| Enrolment key | Abcejfd12@ |
| Use group enrolment keys | Yes |
And the following config values are set as admin:
| config | value | plugin |
| usepasswordpolicy | 0 | enrol_self |
And I navigate to "Courses > Upload courses" in site administration
And I set the field "Upload mode" to "Create new courses, or update existing ones"
And I set the field "Update mode" to "Update with CSV data only"
And I upload "admin/tool/uploadcourse/tests/fixtures/enrolment_self.csv" file to "File" filemanager
And I click on "Preview" "button"
And I should see "This enrolment key is already used as a group enrolment key."
@@ -0,0 +1,136 @@
@tool @tool_uploadcourse @_file_upload
Feature: An admin can update courses using a CSV file
In order to update courses using a CSV file
As an admin
I need to be able to upload a CSV file and navigate through the import process
Background:
Given the following "courses" exist:
| fullname | shortname | category |
| Some random name | C1 | 0 |
| Another course | CF1 | 0 |
And I log in as "admin"
And I navigate to "Courses > Upload courses" in site administration
@javascript
Scenario: Updating a course fullname
Given I upload "admin/tool/uploadcourse/tests/fixtures/courses.csv" file to "File" filemanager
And I set the field "Upload mode" to "Only update existing courses"
And I set the field "Update mode" to "Update with CSV data only"
And I click on "Preview" "button"
When I click on "Upload courses" "button"
Then I should see "Course updated"
And I should see "The course does not exist and creating course is not allowed"
And I should see "Courses total: 3"
And I should see "Courses updated: 1"
And I should see "Courses created: 0"
And I should see "Courses errors: 2"
And I am on site homepage
And I should see "Course 1"
And I should not see "Course 2"
And I should not see "Course 3"
@javascript
Scenario: Updating a course with custom fields
Given the following "custom field categories" exist:
| name | component | area | itemid |
| Other | core_course | course | 0 |
And the following "custom fields" exist:
| name | category | type | shortname | configdata |
| Field 1 | Other | checkbox | checkbox | |
| Field 2 | Other | date | date | |
| Field 3 | Other | select | select | {"options":"a\nb\nc"} |
| Field 4 | Other | text | text | |
| Field 5 | Other | textarea | textarea | |
When I upload "admin/tool/uploadcourse/tests/fixtures/courses_custom_fields.csv" file to "File" filemanager
And I set the following fields to these values:
| Upload mode | Only update existing courses |
| Update mode | Update with CSV data only |
And I click on "Preview" "button"
And I click on "Upload courses" "button"
Then I should see "Course updated"
And I should see "Courses updated: 1"
And I am on site homepage
And I should see "Course fields 1"
And I should see "Field 1: Yes"
And I should see "Field 2: Tuesday, 1 October 2019, 2:00"
And I should see "Field 3: b"
And I should see "Field 4: Hello"
And I should see "Field 5: Goodbye"
@javascript
Scenario: Unsupported enrol methods are not updated
Given the following config values are set as admin:
| enrol_plugins_enabled | manual,lti |
And the following "courses" exist:
| fullname | shortname | category |
| Course 2 | C2 | 0 |
And I am on the "C2" "enrolment methods" page
When I select "Publish as LTI tool" from the "Add method" singleselect
And the following fields match these values:
| LTI version | LTI Advantage |
And I set the following fields to these values:
| Custom instance name | Published course |
| Tool to be published | Course |
And I press "Add method"
And I should see "Published course"
And I navigate to "Courses > Upload courses" in site administration
And I set the field "Upload mode" to "Create new courses, or update existing ones"
And I set the field "Update mode" to "Update with CSV data only"
And I upload "admin/tool/uploadcourse/tests/fixtures/unsupported_enrol_method.csv" file to "File" filemanager
And I click on "Preview" "button"
And I click on "Upload courses" "button"
Then I should see "Enrolment method 'enrol_lti_plugin' is not supported in csv upload"
And I should see "Courses errors: 1"
And I am on the "C2" "enrolment methods" page
And I should see "manualtest"
And I should not see "Manual enrolments"
And I should see "Published course"
And I should not see "ltitest"
@javascript
Scenario: Manager can use upload course tool to update courses in course category
Given the following "users" exist:
| username | firstname | lastname | email |
| user1 | User | 1 | user1@example.com |
And the following "categories" exist:
| name | category | idnumber |
| Cat 1 | 0 | CAT1 |
| Cat 2 | 0 | CAT2 |
| Cat 3 | CAT1 | CAT3 |
And the following "courses" exist:
| fullname | shortname | category |
| Course 1 | C01 | CAT1 |
| Course 2 | C02 | CAT2 |
| Course 3 | C03 | CAT3 |
| Course 4 | C04 | CAT3 |
And the following "role assigns" exist:
| user | role | contextlevel | reference |
| user1 | manager | Category | CAT1 |
When I log in as "user1"
And I am on course index
And I follow "Cat 1"
And I navigate to "Upload courses" in current page administration
And I upload "admin/tool/uploadcourse/tests/fixtures/courses_manager2.csv" file to "File" filemanager
And I set the field "Upload mode" to "Only update existing courses"
And I set the field "Update mode" to "Update with CSV data only"
And I click on "Preview" "button"
# Course C01 is in "our" category but can not be moved to Cat 2 because current user can not manage it.
Then I should see "No permission to upload courses in category: Cat 2" in the "C01" "table_row"
# Course C02 can not be updated (no capability in "Cat 2" context).
And I should see "A course with this short name exists but you don't have permission to use the upload courses functionality to update it." in the "C02" "table_row"
# Course with short name "C05" does not exist.
And I should see "The course does not exist and creating course is not allowed" in the "C05" "table_row"
And I click on "Upload courses" "button"
And I should see "Course updated"
And I should see "Courses total: 5"
And I should see "Courses updated: 2"
And I should see "Courses errors: 3"
And I am on course index
And I follow "Cat 1"
# Course C04 was moved from Cat 3 to Cat 1.
And I should see "Course 4"
And I should see "Course 1"
And I follow "Cat 3"
And I should see "Course 3"
And I log out
File diff suppressed because it is too large Load Diff
Binary file not shown.
+4
View File
@@ -0,0 +1,4 @@
shortname,fullname,summary,category,idnumber
C1,Course 1,Summary 1,1,ID1
C2,Course 2,Summary 2,1,ID2
C3,Course 3,Summary 3,1,ID3
1 shortname fullname summary category idnumber
2 C1 Course 1 Summary 1 1 ID1
3 C2 Course 2 Summary 2 1 ID2
4 C3 Course 3 Summary 3 1 ID3
@@ -0,0 +1,2 @@
shortname,fullname,summary,category,customfield_checkbox,customfield_date,customfield_select,customfield_text,customfield_textarea
CF1,Course fields 1,Testing course fields,1,1,2019-10-01 14:00,b,Hello,Goodbye
1 shortname fullname summary category customfield_checkbox customfield_date customfield_select customfield_text customfield_textarea
2 CF1 Course fields 1 Testing course fields 1 1 2019-10-01 14:00 b Hello Goodbye
@@ -0,0 +1,6 @@
shortname,fullname,summary,idnumber,category_idnumber
C1,Course 1,Summary 1,ID1,CAT2
C2,Course 2,Summary 2,ID2,CAT2
C3,Course 3,Summary 3,ID3,
C4,Course 4,Summary 4,ID4,CAT1
C5,Course 5,Summary 5,ID5,CAT3
1 shortname fullname summary idnumber category_idnumber
2 C1 Course 1 Summary 1 ID1 CAT2
3 C2 Course 2 Summary 2 ID2 CAT2
4 C3 Course 3 Summary 3 ID3
5 C4 Course 4 Summary 4 ID4 CAT1
6 C5 Course 5 Summary 5 ID5 CAT3
@@ -0,0 +1,6 @@
shortname,category_idnumber
C01,CAT2
C02,CAT2
C03,
C04,CAT1
C05,CAT3
1 shortname category_idnumber
2 C01 CAT2
3 C02 CAT2
4 C03
5 C04 CAT1
6 C05 CAT3
@@ -0,0 +1,6 @@
shortname,fullname,category_idnumber,enrolment_1,enrolment_1_role,enrolment_1_cohortidnumber
C1,Course 1,CAT1,cohort,student,Not exist
C1,Course 1,CAT1,cohort,student,CV3
C2,Course 2,CAT2,cohort,student,CV4
C3,Course 3,CAT1,cohort,student,CV5
C4,Course 3,CAT1,cohort,student1,CV6
1 shortname fullname category_idnumber enrolment_1 enrolment_1_role enrolment_1_cohortidnumber
2 C1 Course 1 CAT1 cohort student Not exist
3 C1 Course 1 CAT1 cohort student CV3
4 C2 Course 2 CAT2 cohort student CV4
5 C3 Course 3 CAT1 cohort student CV5
6 C4 Course 3 CAT1 cohort student1 CV6
@@ -0,0 +1,3 @@
shortname,fullname,category_idnumber,enrolment_1,enrolment_1_role,enrolment_1_cohortidnumber,enrolment_1_addtogroup
C2,Course 2,CAT2,cohort,student,CV2,1
C3,Course 3,CAT1,cohort,student,CV1,0
1 shortname fullname category_idnumber enrolment_1 enrolment_1_role enrolment_1_cohortidnumber enrolment_1_addtogroup
2 C2 Course 2 CAT2 cohort student CV2 1
3 C3 Course 3 CAT1 cohort student CV1 0
@@ -0,0 +1,2 @@
shortname,fullname,category_idnumber,enrolment_1,enrolment_1_role,enrolment_1_cohortidnumber,enrolment_1_addtogroup,enrolment_1_groupname
C1,Course 1,CAT1,cohort,student,CV1,0,group1
1 shortname fullname category_idnumber enrolment_1 enrolment_1_role enrolment_1_cohortidnumber enrolment_1_addtogroup enrolment_1_groupname
2 C1 Course 1 CAT1 cohort student CV1 0 group1
@@ -0,0 +1,3 @@
shortname,fullname,category_idnumber,enrolment_1,enrolment_1_role,enrolment_1_cohortidnumber,enrolment_1_groupname
C1,Course 1,CAT1,cohort,student,CV1,notexist
C1,Course 1,CAT1,cohort,student,CV1,group1
1 shortname fullname category_idnumber enrolment_1 enrolment_1_role enrolment_1_cohortidnumber enrolment_1_groupname
2 C1 Course 1 CAT1 cohort student CV1 notexist
3 C1 Course 1 CAT1 cohort student CV1 group1
@@ -0,0 +1,2 @@
shortname,fullname,category_idnumber,enrolment_1
C1,Course 1,CAT1,cohort
1 shortname fullname category_idnumber enrolment_1
2 C1 Course 1 CAT1 cohort
@@ -0,0 +1,3 @@
shortname,fullname,category_idnumber,enrolment_1,enrolment_1_role,enrolment_1_cohortidnumber
C1,Course 1,CAT1,cohort,student,CV1
C1,Course 1,CAT1,cohort,teacher,CV4
1 shortname fullname category_idnumber enrolment_1 enrolment_1_role enrolment_1_cohortidnumber
2 C1 Course 1 CAT1 cohort student CV1
3 C1 Course 1 CAT1 cohort teacher CV4
@@ -0,0 +1,2 @@
shortname,category,enrolment_1,enrolment_1_delete
C1,1,guest,1
1 shortname category enrolment_1 enrolment_1_delete
2 C1 1 guest 1
@@ -0,0 +1,2 @@
shortname,category,enrolment_1,enrolment_1_disable
C1,1,guest,1
1 shortname category enrolment_1 enrolment_1_disable
2 C1 1 guest 1
@@ -0,0 +1,2 @@
shortname,category,enrolment_1,enrolment_1_disable
C1,1,guest,0
1 shortname category enrolment_1 enrolment_1_disable
2 C1 1 guest 0
@@ -0,0 +1,4 @@
shortname,fullname,category_idnumber,enrolment_1,enrolment_1_role,enrolment_1_password
C1,Course 1,CAT1,guest,student,test
C2,Course 2,CAT1,guest,student,
C3,Course 3,CAT1,guest,student,Test123@
1 shortname fullname category_idnumber enrolment_1 enrolment_1_role enrolment_1_password
2 C1 Course 1 CAT1 guest student test
3 C2 Course 2 CAT1 guest student
4 C3 Course 3 CAT1 guest student Test123@
@@ -0,0 +1,4 @@
shortname,fullname,category_idnumber,enrolment_1,enrolment_1_metacoursename
C3,Course 3,CAT1,meta,Not exist
C4,Course 4,CAT2,meta,C1
C1,Course 1,CAT1,meta,C1
1 shortname fullname category_idnumber enrolment_1 enrolment_1_metacoursename
2 C3 Course 3 CAT1 meta Not exist
3 C4 Course 4 CAT2 meta C1
4 C1 Course 1 CAT1 meta C1
@@ -0,0 +1,3 @@
shortname,fullname,category_idnumber,enrolment_1,enrolment_1_metacoursename,enrolment_1_addtogroup
C3,Course 3,CAT2,meta,C1,1
C4,Course 4,CAT1,meta,C1,0
1 shortname fullname category_idnumber enrolment_1 enrolment_1_metacoursename enrolment_1_addtogroup
2 C3 Course 3 CAT2 meta C1 1
3 C4 Course 4 CAT1 meta C1 0
@@ -0,0 +1,2 @@
shortname,fullname,category_idnumber,enrolment_1,enrolment_1_metacoursename,enrolment_1_addtogroup,enrolment_1_groupname
C2,Course 2,CAT1,meta,C1,0,group1
1 shortname fullname category_idnumber enrolment_1 enrolment_1_metacoursename enrolment_1_addtogroup enrolment_1_groupname
2 C2 Course 2 CAT1 meta C1 0 group1
@@ -0,0 +1,3 @@
shortname,fullname,category_idnumber,enrolment_1,enrolment_1_metacoursename,enrolment_1_groupname
C2,Course 2,CAT1,meta,C1,notexist
C2,Course 2,CAT1,meta,C1,group1
1 shortname fullname category_idnumber enrolment_1 enrolment_1_metacoursename enrolment_1_groupname
2 C2 Course 2 CAT1 meta C1 notexist
3 C2 Course 2 CAT1 meta C1 group1
@@ -0,0 +1,2 @@
shortname,fullname,category_idnumber,enrolment_1,enrolment_1_role,enrolment_2,enrolment_2_role,enrolment_2_cohortidnumber,enrolment_3,enrolment_4,enrolment_4_role,enrolment_5,enrolment_5_metacoursename
C2,Course 2,CAT1,self,student,cohort,student,Cohort1,guest,manual,student,meta,C1
1 shortname fullname category_idnumber enrolment_1 enrolment_1_role enrolment_2 enrolment_2_role enrolment_2_cohortidnumber enrolment_3 enrolment_4 enrolment_4_role enrolment_5 enrolment_5_metacoursename
2 C2 Course 2 CAT1 self student cohort student Cohort1 guest manual student meta C1
@@ -0,0 +1,2 @@
shortname,fullname,category_idnumber,enrolment_1,enrolment_1_delete,enrolment_2,enrolment_2_role,enrolment_2_cohortidnumber,enrolment_2_delete,enrolment_3,enrolment_3_delete,enrolment_4,enrolment_4_delete,enrolment_5,enrolment_5_metacoursename,enrolment_5_delete
C2,Course 2,CAT1,self,1,cohort,student,Cohort1,1,guest,1,manual,1,meta,C1,1
1 shortname fullname category_idnumber enrolment_1 enrolment_1_delete enrolment_2 enrolment_2_role enrolment_2_cohortidnumber enrolment_2_delete enrolment_3 enrolment_3_delete enrolment_4 enrolment_4_delete enrolment_5 enrolment_5_metacoursename enrolment_5_delete
2 C2 Course 2 CAT1 self 1 cohort student Cohort1 1 guest 1 manual 1 meta C1 1
@@ -0,0 +1,4 @@
shortname,fullname,category,enrolment_1,enrolment_1_role
CX,coursex,1,manual,notexist
CY,coursey,1,manual,notallowed
CZ,coursez,1,manual,student
1 shortname fullname category enrolment_1 enrolment_1_role
2 CX coursex 1 manual notexist
3 CY coursey 1 manual notallowed
4 CZ coursez 1 manual student
@@ -0,0 +1,4 @@
shortname,fullname,category_idnumber,enrolment_1,enrolment_1_role,enrolment_1_password
C1,Course 1,CAT1,self,student,test
C2,Course 2,CAT1,self,student,
C3,Course 3,CAT1,self,student,Test123@
1 shortname fullname category_idnumber enrolment_1 enrolment_1_role enrolment_1_password
2 C1 Course 1 CAT1 self student test
3 C2 Course 2 CAT1 self student
4 C3 Course 3 CAT1 self student Test123@
@@ -0,0 +1,3 @@
shortname,fullname,category_idnumber,enrolment_1,enrolment_1_role,enrolment_1_name
C2,Course 2,0,manual,student,manualtest
C2,Course 2,0,lti,student,ltitest
1 shortname fullname category_idnumber enrolment_1 enrolment_1_role enrolment_1_name
2 C2 Course 2 0 manual student manualtest
3 C2 Course 2 0 lti student ltitest
@@ -0,0 +1,497 @@
<?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 tool_uploadcourse;
use tool_uploadcourse_helper;
/**
* Helper test case.
*
* @package tool_uploadcourse
* @copyright 2013 Frédéric Massart
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class helper_test extends \advanced_testcase {
public function test_generate_shortname(): void {
$data = (object) array('fullname' => 'Ah bh Ch 01 02 03', 'idnumber' => 'ID123');
$this->assertSame($data->fullname, tool_uploadcourse_helper::generate_shortname($data, '%f'));
$this->assertSame($data->idnumber, tool_uploadcourse_helper::generate_shortname($data, '%i'));
$this->assertSame('Ah Bh Ch', tool_uploadcourse_helper::generate_shortname($data, '%~8f'));
$this->assertSame('AH BH CH', tool_uploadcourse_helper::generate_shortname($data, '%+8f'));
$this->assertSame('id123', tool_uploadcourse_helper::generate_shortname($data, '%-i'));
$this->assertSame('[Ah bh Ch] = ID123', tool_uploadcourse_helper::generate_shortname($data, '[%8f] = %i'));
$this->assertSame('0', tool_uploadcourse_helper::generate_shortname($data, '0'));
$this->assertSame('%unknown', tool_uploadcourse_helper::generate_shortname($data, '%unknown'));
$this->assertNull(tool_uploadcourse_helper::generate_shortname($data, ''));
$this->assertNull(tool_uploadcourse_helper::generate_shortname(array(), '%f'));
}
public function test_get_course_formats(): void {
$result = tool_uploadcourse_helper::get_course_formats();
$this->assertSame(array_keys(\core_component::get_plugin_list('format')), $result);
// Should be similar as first result, as cached.
$this->assertSame($result, tool_uploadcourse_helper::get_course_formats());
}
public function test_get_enrolment_data(): void {
$this->resetAfterTest(true);
$data = array(
'enrolment_1' => 'unknown',
'enrolment_1_foo' => '1',
'enrolment_1_bar' => '2',
'enrolment_2' => 'self',
'enrolment_2_delete' => '1',
'enrolment_2_foo' => 'a',
'enrolment_2_bar' => '1',
'enrolment_3' => 'manual',
'enrolment_3_disable' => '2',
'enrolment_3_foo' => 'b',
'enrolment_3_bar' => '2',
'enrolment_4' => 'database',
'enrolment_4_foo' => 'x',
'enrolment_4_bar' => '3',
'enrolment_5_test3' => 'test3',
'enrolment_5_test2' => 'test2',
'enrolment_5_test1' => 'test1',
'enrolment_5' => 'flatfile',
);
$expected = array(
'self' => array(
'delete' => '1',
'foo' => 'a',
'bar' => '1',
),
'manual' => array(
'disable' => '2',
'foo' => 'b',
'bar' => '2',
),
'database' => array(
'foo' => 'x',
'bar' => '3',
),
'flatfile' => array(
'test3' => 'test3',
'test2' => 'test2',
'test1' => 'test1',
)
);
$this->assertSame(tool_uploadcourse_helper::get_enrolment_data($data), $expected);
}
public function test_get_enrolment_plugins(): void {
$this->resetAfterTest(true);
$actual = tool_uploadcourse_helper::get_enrolment_plugins();
$this->assertSame(array_keys(enrol_get_plugins(false)), array_keys($actual));
// This should be identical as cached.
$secondactual = tool_uploadcourse_helper::get_enrolment_plugins();
$this->assertEquals($actual, $secondactual);
}
public function test_get_restore_content_dir(): void {
global $CFG;
require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php');
$this->resetAfterTest(true);
$this->setAdminUser();
$c1 = $this->getDataGenerator()->create_course();
$c2 = $this->getDataGenerator()->create_course((object) array('shortname' => 'Yay'));
// Creating backup file.
$bc = new \backup_controller(\backup::TYPE_1COURSE, $c1->id, \backup::FORMAT_MOODLE,
\backup::INTERACTIVE_NO, \backup::MODE_GENERAL, 2);
$bc->execute_plan();
$result = $bc->get_results();
$this->assertTrue(isset($result['backup_destination']));
$c1backupfile = $result['backup_destination']->copy_content_to_temp();
$bc->destroy();
// Creating backup file.
$bc = new \backup_controller(\backup::TYPE_1COURSE, $c2->id, \backup::FORMAT_MOODLE,
\backup::INTERACTIVE_NO, \backup::MODE_GENERAL, 2);
$bc->execute_plan();
$result = $bc->get_results();
$this->assertTrue(isset($result['backup_destination']));
$c2backupfile = $result['backup_destination']->copy_content_to_temp();
$bc->destroy();
$oldcfg = isset($CFG->keeptempdirectoriesonbackup) ? $CFG->keeptempdirectoriesonbackup : false;
$CFG->keeptempdirectoriesonbackup = true;
// Checking restore dir.
$dir = tool_uploadcourse_helper::get_restore_content_dir($c1backupfile, null);
$bcinfo = \backup_general_helper::get_backup_information($dir);
$this->assertEquals($bcinfo->original_course_id, $c1->id);
$this->assertEquals($bcinfo->original_course_fullname, $c1->fullname);
// Do it again, it should be the same directory.
$dir2 = tool_uploadcourse_helper::get_restore_content_dir($c1backupfile, null);
$this->assertEquals($dir, $dir2);
// Get the second course.
$dir = tool_uploadcourse_helper::get_restore_content_dir($c2backupfile, null);
$bcinfo = \backup_general_helper::get_backup_information($dir);
$this->assertEquals($bcinfo->original_course_id, $c2->id);
$this->assertEquals($bcinfo->original_course_fullname, $c2->fullname);
// Checking with a shortname.
$dir = tool_uploadcourse_helper::get_restore_content_dir(null, $c1->shortname);
$bcinfo = \backup_general_helper::get_backup_information($dir);
$this->assertEquals($bcinfo->original_course_id, $c1->id);
$this->assertEquals($bcinfo->original_course_fullname, $c1->fullname);
// Do it again, it should be the same directory.
$dir2 = tool_uploadcourse_helper::get_restore_content_dir(null, $c1->shortname);
$this->assertEquals($dir, $dir2);
// Get the second course.
$dir = tool_uploadcourse_helper::get_restore_content_dir(null, $c2->shortname);
$bcinfo = \backup_general_helper::get_backup_information($dir);
$this->assertEquals($bcinfo->original_course_id, $c2->id);
$this->assertEquals($bcinfo->original_course_fullname, $c2->fullname);
// Get a course that does not exist.
$errors = array();
$dir = tool_uploadcourse_helper::get_restore_content_dir(null, 'DoesNotExist', $errors);
$this->assertFalse($dir);
$this->assertArrayHasKey('coursetorestorefromdoesnotexist', $errors);
// Trying again without caching. $CFG->keeptempdirectoriesonbackup is required for caching.
$CFG->keeptempdirectoriesonbackup = false;
// Checking restore dir.
$dir = tool_uploadcourse_helper::get_restore_content_dir($c1backupfile, null);
$dir2 = tool_uploadcourse_helper::get_restore_content_dir($c1backupfile, null);
$this->assertNotEquals($dir, $dir2);
// Checking with a shortname.
$dir = tool_uploadcourse_helper::get_restore_content_dir(null, $c1->shortname);
$dir2 = tool_uploadcourse_helper::get_restore_content_dir(null, $c1->shortname);
$this->assertNotEquals($dir, $dir2);
// Get a course that does not exist.
$errors = array();
$dir = tool_uploadcourse_helper::get_restore_content_dir(null, 'DoesNotExist', $errors);
$this->assertFalse($dir);
$this->assertArrayHasKey('coursetorestorefromdoesnotexist', $errors);
$dir2 = tool_uploadcourse_helper::get_restore_content_dir(null, 'DoesNotExist', $errors);
$this->assertEquals($dir, $dir2);
$CFG->keeptempdirectoriesonbackup = $oldcfg;
}
public function test_get_role_ids(): void {
$this->getDataGenerator();
// Mimic function result.
$expected = array();
$roles = get_all_roles();
foreach ($roles as $role) {
$expected[$role->shortname] = $role->id;
}
$actual = tool_uploadcourse_helper::get_role_ids();
$this->assertSame($actual, $expected);
// Check cache.
$this->assertSame($actual, tool_uploadcourse_helper::get_role_ids());
}
public function test_get_role_names(): void {
$this->resetAfterTest(true);
create_role('Villain', 'villain', 'The bad guys');
$data = array(
'role_student' => 'Padawan',
'role_teacher' => 'Guardian',
'role_editingteacher' => 'Knight',
'role_manager' => 'Master',
'role_villain' => 'Jabba the Hutt',
'role_android' => 'R2D2',
);
// Get the role IDs, but need to force the cache reset as a new role is defined.
$roleids = tool_uploadcourse_helper::get_role_ids(true);
$expected = array(
'role_' . $roleids['student'] => 'Padawan',
'role_' . $roleids['teacher'] => 'Guardian',
'role_' . $roleids['editingteacher'] => 'Knight',
'role_' . $roleids['manager'] => 'Master',
'role_' . $roleids['villain'] => 'Jabba the Hutt',
);
$errors = array();
$actual = tool_uploadcourse_helper::get_role_names($data, $errors);
$this->assertSame($actual, $expected);
$this->assertArrayHasKey('invalidroles', $errors);
}
/**
* Test custom field data processing
*/
public function test_get_custom_course_field_data(): void {
global $DB;
$this->resetAfterTest();
// Create all the fields!
$category = $this->get_customfield_generator()->create_category();
$checkboxfield = $this->create_custom_field($category, 'checkbox', 'mycheckbox');
$datefield = $this->create_custom_field($category, 'date', 'mydate');
$selectfield = $this->create_custom_field($category, 'select', 'myselect', ['options' => "Red\nGreen\nBlue"]);
$textfield = $this->create_custom_field($category, 'text', 'mytext', ['locked' => 1]);
$textareafield = $this->create_custom_field($category, 'textarea', 'mytextarea');
$fields = tool_uploadcourse_helper::get_custom_course_fields();
$this->assertCount(5, $fields);
$this->assertArrayHasKey($checkboxfield->get('shortname'), $fields);
$this->assertInstanceOf(\customfield_checkbox\field_controller::class, $fields[$checkboxfield->get('shortname')]);
$this->assertArrayHasKey($datefield->get('shortname'), $fields);
$this->assertInstanceOf(\customfield_date\field_controller::class, $fields[$datefield->get('shortname')]);
$this->assertArrayHasKey($selectfield->get('shortname'), $fields);
$this->assertInstanceOf(\customfield_select\field_controller::class, $fields[$selectfield->get('shortname')]);
$this->assertArrayHasKey($textfield->get('shortname'), $fields);
$this->assertInstanceOf(\customfield_text\field_controller::class, $fields[$textfield->get('shortname')]);
$this->assertArrayHasKey($textareafield->get('shortname'), $fields);
$this->assertInstanceOf(\customfield_textarea\field_controller::class, $fields[$textareafield->get('shortname')]);
$data = [
'customfield_mycheckbox' => '1',
'customfield_mydate' => '2019-10-01',
'customfield_myselect' => 'Green',
'customfield_mytext' => 'Hello',
'customfield_myunknownfield' => 'Goodbye',
];
$expected = [
'customfield_mycheckbox' => '1',
'customfield_mydate' => strtotime('2019-10-01'),
'customfield_myselect' => 2,
'customfield_mytext' => 'Hello',
];
$course = $this->getDataGenerator()->create_course();
$user = $this->getDataGenerator()->create_and_enrol($course, 'manager');
$this->setUser($user);
$context = \context_course::instance($course->id);
$this->assertEquals($expected, tool_uploadcourse_helper::get_custom_course_field_data($data, [], $context));
// Now add our custom textarea field (separately because the value of it's 'itemid' element is unknown).
$data['customfield_mytextarea'] = 'Something';
$fields = tool_uploadcourse_helper::get_custom_course_field_data($data, [], $context);
$this->assertArrayHasKey('customfield_mytextarea_editor', $fields);
$this->assertArrayHasKey('text', $fields['customfield_mytextarea_editor']);
$this->assertEquals('Something', $fields['customfield_mytextarea_editor']['text']);
// Now prohibit the capability to change locked fields for the manager role.
$managerrole = $DB->get_record('role', ['shortname' => 'manager']);
role_change_permission($managerrole->id, $context, 'moodle/course:changelockedcustomfields', CAP_PROHIBIT);
// The locked 'mytext' custom field should not be returned.
$fields = tool_uploadcourse_helper::get_custom_course_field_data($data, [], $context);
$this->assertCount(4, $fields);
$this->assertArrayNotHasKey('customfield_mytext', $fields);
}
public function test_increment_idnumber(): void {
$this->resetAfterTest(true);
$c1 = $this->getDataGenerator()->create_course(array('idnumber' => 'C1'));
$c2 = $this->getDataGenerator()->create_course(array('idnumber' => 'C2'));
$c3 = $this->getDataGenerator()->create_course(array('idnumber' => 'Yo'));
$this->assertEquals('C3', tool_uploadcourse_helper::increment_idnumber('C1'));
$this->assertEquals('Yo_2', tool_uploadcourse_helper::increment_idnumber('Yo'));
$this->assertEquals('DoesNotExist', tool_uploadcourse_helper::increment_idnumber('DoesNotExist'));
}
public function test_increment_shortname(): void {
$this->resetAfterTest(true);
$c1 = $this->getDataGenerator()->create_course(array('shortname' => 'C1'));
$c2 = $this->getDataGenerator()->create_course(array('shortname' => 'C2'));
$c3 = $this->getDataGenerator()->create_course(array('shortname' => 'Yo'));
// FYI: increment_shortname assumes that the course exists, and so increment the shortname immediately.
$this->assertEquals('C3', tool_uploadcourse_helper::increment_shortname('C1'));
$this->assertEquals('Yo_2', tool_uploadcourse_helper::increment_shortname('Yo'));
$this->assertEquals('DoesNotExist_2', tool_uploadcourse_helper::increment_shortname('DoesNotExist'));
}
public function test_resolve_category(): void {
$this->resetAfterTest(true);
$c1 = $this->getDataGenerator()->create_category(array('name' => 'First level'));
$c2 = $this->getDataGenerator()->create_category(array('name' => 'Second level', 'parent' => $c1->id));
$c3 = $this->getDataGenerator()->create_category(array('idnumber' => 'C3'));
$data = array(
'category' => $c1->id,
'category_path' => $c1->name . ' / ' . $c2->name,
'category_idnumber' => $c3->idnumber,
);
$this->assertEquals($c1->id, tool_uploadcourse_helper::resolve_category($data));
unset($data['category']);
$this->assertEquals($c3->id, tool_uploadcourse_helper::resolve_category($data));
unset($data['category_idnumber']);
$this->assertEquals($c2->id, tool_uploadcourse_helper::resolve_category($data));
// Adding unexisting data.
$errors = array();
$data['category_idnumber'] = 1234;
$this->assertEquals($c2->id, tool_uploadcourse_helper::resolve_category($data, $errors));
$this->assertArrayHasKey('couldnotresolvecatgorybyidnumber', $errors);
$errors = array();
$data['category'] = 1234;
$this->assertEquals($c2->id, tool_uploadcourse_helper::resolve_category($data, $errors));
$this->assertArrayHasKey('couldnotresolvecatgorybyid', $errors);
$errors = array();
$data['category_path'] = 'Not exist';
$this->assertEmpty(tool_uploadcourse_helper::resolve_category($data, $errors));
$this->assertArrayHasKey('couldnotresolvecatgorybypath', $errors);
}
public function test_resolve_category_by_idnumber(): void {
$this->resetAfterTest(true);
$c1 = $this->getDataGenerator()->create_category(array('idnumber' => 'C1'));
$c2 = $this->getDataGenerator()->create_category(array('idnumber' => 'C2'));
$c3 = $this->getDataGenerator()->create_category(['idnumber' => 'C3', 'visible' => false]);
// Doubled for cache check.
$this->assertEquals($c1->id, tool_uploadcourse_helper::resolve_category_by_idnumber('C1'));
$this->assertEquals($c1->id, tool_uploadcourse_helper::resolve_category_by_idnumber('C1'));
$this->assertEquals($c2->id, tool_uploadcourse_helper::resolve_category_by_idnumber('C2'));
$this->assertEquals($c2->id, tool_uploadcourse_helper::resolve_category_by_idnumber('C2'));
$this->assertEmpty(tool_uploadcourse_helper::resolve_category_by_idnumber('C3'));
$this->assertEmpty(tool_uploadcourse_helper::resolve_category_by_idnumber('C3'));
$this->assertEmpty(tool_uploadcourse_helper::resolve_category_by_idnumber('DoesNotExist'));
$this->assertEmpty(tool_uploadcourse_helper::resolve_category_by_idnumber('DoesNotExist'));
}
public function test_resolve_category_by_path(): void {
$this->resetAfterTest(true);
$cat1 = $this->getDataGenerator()->create_category(array('name' => 'Cat 1'));
$cat1_1 = $this->getDataGenerator()->create_category(array('name' => 'Cat 1.1', 'parent' => $cat1->id));
$cat1_1_1 = $this->getDataGenerator()->create_category(array('name' => 'Cat 1.1.1', 'parent' => $cat1_1->id));
$cat1_1_2 = $this->getDataGenerator()->create_category(array('name' => 'Cat 1.1.2', 'parent' => $cat1_1->id));
$cat1_2 = $this->getDataGenerator()->create_category(array('name' => 'Cat 1.2', 'parent' => $cat1->id));
$cat2 = $this->getDataGenerator()->create_category(array('name' => 'Cat 2'));
$cat2_1 = $this->getDataGenerator()->create_category(array('name' => 'Cat 2.1', 'parent' => $cat2->id, 'visible' => false));
$cat2_1_1 = $this->getDataGenerator()->create_category(array('name' => 'Cat 2.1.1', 'parent' => $cat2_1->id));
$cat2_1_2 = $this->getDataGenerator()->create_category(array('name' => 'Cat 2.1.2', 'parent' => $cat2_1->id));
$cat2_2 = $this->getDataGenerator()->create_category(array('name' => 'Cat 2.2', 'parent' => $cat2->id));
$cat3 = $this->getDataGenerator()->create_category(array('name' => 'Cat 3'));
$cat3_1 = $this->getDataGenerator()->create_category(array('name' => 'Cat 3.1 Doubled', 'parent' => $cat3->id));
$cat3_1b = $this->getDataGenerator()->create_category(array('name' => 'Cat 3.1 Doubled', 'parent' => $cat3->id));
$cat3_1_1 = $this->getDataGenerator()->create_category(array('name' => 'Cat 3.1.1', 'parent' => $cat3_1->id));
$cat3_fakedouble = $this->getDataGenerator()->create_category(array('name' => 'Cat 3.1.1', 'parent' => $cat3->id));
// Existing categories. Doubled for cache testing.
$path = array('Cat 1');
$this->assertEquals($cat1->id, tool_uploadcourse_helper::resolve_category_by_path($path));
$this->assertEquals($cat1->id, tool_uploadcourse_helper::resolve_category_by_path($path));
$path = array('Cat 1', 'Cat 1.1', 'Cat 1.1.2');
$this->assertEquals($cat1_1_2->id, tool_uploadcourse_helper::resolve_category_by_path($path));
$this->assertEquals($cat1_1_2->id, tool_uploadcourse_helper::resolve_category_by_path($path));
$path = array('Cat 1', 'Cat 1.2');
$this->assertEquals($cat1_2->id, tool_uploadcourse_helper::resolve_category_by_path($path));
$this->assertEquals($cat1_2->id, tool_uploadcourse_helper::resolve_category_by_path($path));
$path = array('Cat 2');
$this->assertEquals($cat2->id, tool_uploadcourse_helper::resolve_category_by_path($path));
$this->assertEquals($cat2->id, tool_uploadcourse_helper::resolve_category_by_path($path));
// Hidden category.
$path = array('Cat 2', 'Cat 2.1');
$this->assertEquals($cat2_1->id, tool_uploadcourse_helper::resolve_category_by_path($path));
$this->assertEquals($cat2_1->id, tool_uploadcourse_helper::resolve_category_by_path($path));
// Hidden parent.
$path = array('Cat 2', 'Cat 2.1', 'Cat 2.1.2');
$this->assertEmpty(tool_uploadcourse_helper::resolve_category_by_path($path));
$this->assertEmpty(tool_uploadcourse_helper::resolve_category_by_path($path));
// Does not exist.
$path = array('No cat 3', 'Cat 1.2');
$this->assertEmpty(tool_uploadcourse_helper::resolve_category_by_path($path));
$this->assertEmpty(tool_uploadcourse_helper::resolve_category_by_path($path));
$path = array('Cat 2', 'Cat 2.x');
$this->assertEmpty(tool_uploadcourse_helper::resolve_category_by_path($path));
$this->assertEmpty(tool_uploadcourse_helper::resolve_category_by_path($path));
// Name conflict.
$path = array('Cat 3', 'Cat 3.1 Doubled');
$this->assertEmpty(tool_uploadcourse_helper::resolve_category_by_path($path));
$this->assertEmpty(tool_uploadcourse_helper::resolve_category_by_path($path));
$path = array('Cat 3', 'Cat 3.1 Doubled', 'Cat 3.1.1');
$this->assertEmpty(tool_uploadcourse_helper::resolve_category_by_path($path));
$this->assertEmpty(tool_uploadcourse_helper::resolve_category_by_path($path));
$path = array('Cat 3', 'Cat 3.1.1');
$this->assertEquals($cat3_fakedouble->id, tool_uploadcourse_helper::resolve_category_by_path($path));
$this->assertEquals($cat3_fakedouble->id, tool_uploadcourse_helper::resolve_category_by_path($path));
}
/**
* Get custom field plugin generator
*
* @return core_customfield_generator
*/
protected function get_customfield_generator(): \core_customfield_generator {
return $this->getDataGenerator()->get_plugin_generator('core_customfield');
}
/**
* Helper method to create custom course field
*
* @param \core_customfield\category_controller $category
* @param string $type
* @param string $shortname
* @param array $configdata
* @return \core_customfield\field_controller
*/
protected function create_custom_field(\core_customfield\category_controller $category, string $type, string $shortname,
array $configdata = []): \core_customfield\field_controller {
return $this->get_customfield_generator()->create_field([
'categoryid' => $category->get('id'),
'type' => $type,
'shortname' => $shortname,
'configdata' => $configdata,
]);
}
}
@@ -0,0 +1,217 @@
<?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 tool_uploadcourse;
use tool_uploadcourse_processor;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->libdir . '/csvlib.class.php');
/**
* Processor test case.
*
* @package tool_uploadcourse
* @copyright 2013 Frédéric Massart
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class processor_test extends \advanced_testcase {
public function test_basic(): void {
global $DB;
$this->resetAfterTest(true);
$this->setAdminUser();
$content = array(
"shortname,fullname,summary",
"c1,Course 1,Course 1 summary",
"c2,Course 2,Course 2 summary",
);
$content = implode("\n", $content);
$iid = \csv_import_reader::get_new_iid('uploadcourse');
$cir = new \csv_import_reader($iid, 'uploadcourse');
$cir->load_csv_content($content, 'utf-8', 'comma');
$cir->init();
$options = array('mode' => tool_uploadcourse_processor::MODE_CREATE_ALL);
$defaults = array('category' => '1');
$p = new tool_uploadcourse_processor($cir, $options, $defaults);
$this->assertFalse($DB->record_exists('course', array('shortname' => 'c1')));
$this->assertFalse($DB->record_exists('course', array('shortname' => 'c2')));
$p->execute();
$this->assertTrue($DB->record_exists('course', array('shortname' => 'c1')));
$this->assertTrue($DB->record_exists('course', array('shortname' => 'c2')));
}
public function test_restore_template_course(): void {
global $DB;
$this->resetAfterTest(true);
$this->setAdminUser();
$c1 = $this->getDataGenerator()->create_course();
$c1f1 = $this->getDataGenerator()->create_module('forum', array('course' => $c1->id));
$content = array(
"shortname,fullname,summary",
"c2,Course 2,Course 2 summary",
);
$content = implode("\n", $content);
$iid = \csv_import_reader::get_new_iid('uploadcourse');
$cir = new \csv_import_reader($iid, 'uploadcourse');
$cir->load_csv_content($content, 'utf-8', 'comma');
$cir->init();
$options = array('mode' => tool_uploadcourse_processor::MODE_CREATE_NEW, 'templatecourse' => $c1->shortname);
$defaults = array('category' => '1');
$p = new tool_uploadcourse_processor($cir, $options, $defaults);
$this->assertFalse($DB->record_exists('course', array('shortname' => 'c2')));
$p->execute();
$c2 = $DB->get_record('course', array('shortname' => 'c2'));
$modinfo = get_fast_modinfo($c2);
$found = false;
foreach ($modinfo->get_cms() as $cmid => $cm) {
if ($cm->modname == 'forum' && $cm->name == $c1f1->name) {
$found = true;
break;
}
}
$this->assertTrue($found);
}
public function test_restore_restore_file(): void {
global $DB;
$this->resetAfterTest(true);
$this->setAdminUser();
$content = array(
"shortname,fullname,summary",
"c1,Course 1,Course 1 summary",
);
$content = implode("\n", $content);
$iid = \csv_import_reader::get_new_iid('uploadcourse');
$cir = new \csv_import_reader($iid, 'uploadcourse');
$cir->load_csv_content($content, 'utf-8', 'comma');
$cir->init();
$options = array(
'mode' => tool_uploadcourse_processor::MODE_CREATE_NEW,
'restorefile' => __DIR__ . '/fixtures/backup.mbz',
'templatecourse' => 'DoesNotExist' // Restorefile takes priority.
);
$defaults = array('category' => '1');
$p = new tool_uploadcourse_processor($cir, $options, $defaults);
$this->assertFalse($DB->record_exists('course', array('shortname' => 'c1')));
$p->execute();
$c1 = $DB->get_record('course', array('shortname' => 'c1'));
$modinfo = get_fast_modinfo($c1);
$found = false;
foreach ($modinfo->get_cms() as $cmid => $cm) {
if ($cm->modname == 'glossary' && $cm->name == 'Imported Glossary') {
$found = true;
break;
}
}
$this->assertTrue($found);
}
public function test_shortname_template(): void {
global $DB;
$this->resetAfterTest(true);
$this->setAdminUser();
$content = array(
"shortname,fullname,summary,idnumber",
",Course 1,C1 Summary,ID123",
);
$content = implode("\n", $content);
$iid = \csv_import_reader::get_new_iid('uploadcourse');
$cir = new \csv_import_reader($iid, 'uploadcourse');
$cir->load_csv_content($content, 'utf-8', 'comma');
$cir->init();
$options = array('mode' => tool_uploadcourse_processor::MODE_CREATE_NEW, 'shortnametemplate' => '%i: %f');
$defaults = array('category' => '1');
$p = new tool_uploadcourse_processor($cir, $options, $defaults);
$this->assertFalse($DB->record_exists('course', array('idnumber' => 'ID123')));
$p->execute();
$this->assertTrue($DB->record_exists('course', array('idnumber' => 'ID123')));
$c = $DB->get_record('course', array('idnumber' => 'ID123'));
$this->assertEquals('ID123: Course 1', $c->shortname);
}
public function test_empty_csv(): void {
$this->resetAfterTest(true);
$content = array();
$content = implode("\n", $content);
$iid = \csv_import_reader::get_new_iid('uploadcourse');
$cir = new \csv_import_reader($iid, 'uploadcourse');
$cir->load_csv_content($content, 'utf-8', 'comma');
$cir->init();
$options = array('mode' => tool_uploadcourse_processor::MODE_CREATE_NEW);
$this->expectException(\moodle_exception::class);
$p = new tool_uploadcourse_processor($cir, $options, array());
}
public function test_not_enough_columns(): void {
$this->resetAfterTest(true);
$content = array(
"shortname",
"c1",
);
$content = implode("\n", $content);
$iid = \csv_import_reader::get_new_iid('uploadcourse');
$cir = new \csv_import_reader($iid, 'uploadcourse');
$cir->load_csv_content($content, 'utf-8', 'comma');
$cir->init();
$options = array('mode' => tool_uploadcourse_processor::MODE_CREATE_NEW);
$this->expectException(\moodle_exception::class);
$p = new tool_uploadcourse_processor($cir, $options, array());
}
public function test_preview(): void {
global $DB;
$this->resetAfterTest(true);
$content = array(
"shortname,fullname,summary",
"c1,Course 1,Course 1 summary",
"c2,Course 2,Course 2 summary",
);
$content = implode("\n", $content);
$iid = \csv_import_reader::get_new_iid('uploadcourse');
$cir = new \csv_import_reader($iid, 'uploadcourse');
$cir->load_csv_content($content, 'utf-8', 'comma');
$cir->init();
$options = array('mode' => tool_uploadcourse_processor::MODE_CREATE_ALL);
$defaults = array('category' => '1');
$p = new tool_uploadcourse_processor($cir, $options, $defaults);
// Nothing special to expect here, just make sure no exceptions are thrown.
$p->preview();
}
}