Skip to content

Commit

Permalink
Merge branch 'MDL-78522-master' of https://github.com/ilyatregubov/mo…
Browse files Browse the repository at this point in the history
  • Loading branch information
sarjona committed Jul 13, 2023
2 parents 2c3f4d3 + a688a93 commit 075377a
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 5 deletions.
22 changes: 22 additions & 0 deletions admin/tool/uploadcourse/tests/behat/cohorts.feature
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,28 @@ Feature: An admin can create courses with cohort enrolments using a CSV file
When I click on "Preview" "button"
Then I should see "Cohort sync 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 Cohort 3 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 Cohort 3 not allowed in this context."
And I should see "Cohort Cohort 4 not allowed in this context."
And I should see "Courses created: 2"
And I should see "Courses updated: 0"
And I should see "Courses errors: 3"
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"

@javascript
Scenario: Validation of groups for uploaded courses with cohort enrolments
Given the following "groups" exist:
Expand Down
6 changes: 3 additions & 3 deletions cohort/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -384,13 +384,13 @@ function cohort_get_cohort($cohortorid, $currentcontext, $withcustomfields = fal

if ($cohort && in_array($cohort->contextid, $currentcontext->get_parent_context_ids())) {
if (!$cohort->visible) {
$cohort = false;
} else {
$cohortcontext = context::instance_by_id($cohort->contextid);
if (!has_capability('moodle/cohort:view', $cohortcontext)) {
$cohort = false;
return false;
}
}
} else {
return false;
}

if ($cohort && $withcustomfields) {
Expand Down
35 changes: 33 additions & 2 deletions cohort/tests/lib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -710,7 +710,7 @@ public function test_get_functions_return_custom_fields() {
$cohort2 = $this->getDataGenerator()->create_cohort();

// Test cohort_get_cohort.
$result = cohort_get_cohort($cohort1->id, \context_system::instance(), true);
$result = cohort_get_cohort($cohort1->id, $coursectx, true);
$this->assertObjectHasAttribute('customfields', $result);
$this->assertCount(1, $result->customfields);
$field = reset($result->customfields);
Expand All @@ -719,7 +719,7 @@ public function test_get_functions_return_custom_fields() {
$this->assertEquals('Test value 1', $field->get_value());

// Test custom fields are not returned if not needed.
$result = cohort_get_cohort($cohort1->id, \context_system::instance());
$result = cohort_get_cohort($cohort1->id, $coursectx);
$this->assertObjectNotHasAttribute('customfields', $result);

// Test cohort_get_cohorts.
Expand Down Expand Up @@ -939,4 +939,35 @@ public function test_cohort_get_custom_fields_data() {
}
}
}

/**
* Test the behaviour of cohort_get_cohort().
*
* @covers ::cohort_get_cohort
*/
public function test_cohort_get_cohort() {
$this->resetAfterTest();

$cat = $this->getDataGenerator()->create_category();
$cat1 = $this->getDataGenerator()->create_category(['parent' => $cat->id]);
$cat2 = $this->getDataGenerator()->create_category(['parent' => $cat->id]);

$course1 = $this->getDataGenerator()->create_course(['category' => $cat1->id, 'shortname' => 'ANON1']);
$course2 = $this->getDataGenerator()->create_course(['category' => $cat2->id, 'shortname' => 'ANON2']);

$cohort1 = $this->getDataGenerator()->create_cohort(['contextid' => \context_coursecat::instance($cat1->id)->id]);

$result = cohort_get_cohort($cohort1->id, \context_course::instance($course2->id));
$this->assertFalse($result);

$result = cohort_get_cohort($cohort1->id, \context_course::instance($course2->id), true);
$this->assertFalse($result);

$result = cohort_get_cohort($cohort1->id, \context_course::instance($course1->id));
$this->assertEquals($cohort1->id, $result->id);

$result = cohort_get_cohort($cohort1->id, \context_course::instance($course1->id), true);
$this->assertEquals($cohort1->id, $result->id);
}

}
64 changes: 64 additions & 0 deletions enrol/cohort/tests/lib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,41 @@ public function test_enrol_cohort_unenrolaction_suspend_only() {
$this->assertEquals($studentrole->id, $usersrole[$user4->id]->roleid);
}

/**
* Test the behaviour of validate_plugin_data_context().
*
* @covers ::validate_plugin_data_context
*/
public function test_validate_plugin_data_context() {
$this->resetAfterTest();

$cohortplugin = enrol_get_plugin('cohort');

$cat = $this->getDataGenerator()->create_category();
$cat1 = $this->getDataGenerator()->create_category(['parent' => $cat->id]);
$cat2 = $this->getDataGenerator()->create_category(['parent' => $cat->id]);

$course = $this->getDataGenerator()->create_course(['category' => $cat1->id, 'shortname' => 'ANON']);

$cohort1 = $this->getDataGenerator()->create_cohort(['contextid' => \context_coursecat::instance($cat1->id)->id]);
$cohort2 = $this->getDataGenerator()->create_cohort(['contextid' => \context_coursecat::instance($cat2->id)->id]);

$enrolmentdata = [
'customint1' => $cohort2->id,
'cohortname' => $cohort2->name,
];
$error = $cohortplugin->validate_plugin_data_context($enrolmentdata, $course->id);
$this->assertInstanceOf('lang_string', $error);
$this->assertEquals('contextcohortnotallowed', $error->get_identifier());

$enrolmentdata = [
'customint1' => $cohort1->id,
'cohortname' => $cohort1->name,
];
$error = $cohortplugin->validate_plugin_data_context($enrolmentdata, $course->id);
$this->assertNull($error);
}

/**
* Test the behaviour of fill_enrol_custom_fields().
*
Expand Down Expand Up @@ -272,9 +307,14 @@ public function test_validate_enrol_plugin_data() {

$cat = $this->getDataGenerator()->create_category();
$cat1 = $this->getDataGenerator()->create_category(['parent' => $cat->id]);
$cat2 = $this->getDataGenerator()->create_category(['parent' => $cat->id]);

$course = $this->getDataGenerator()->create_course(['category' => $cat1->id, 'shortname' => 'ANON']);

$group1 = $this->getDataGenerator()->create_group(['courseid' => $course->id, 'name' => 'Group 1']);

$cohort1 = $this->getDataGenerator()->create_cohort(['contextid' => \context_coursecat::instance($cat1->id)->id]);
$cohort2 = $this->getDataGenerator()->create_cohort(['contextid' => \context_coursecat::instance($cat2->id)->id]);

enrol::enable_plugin('cohort', false);

Expand Down Expand Up @@ -305,10 +345,34 @@ public function test_validate_enrol_plugin_data() {
$errors = $cohortplugin->validate_enrol_plugin_data($enrolmentdata, $course->id);
$this->assertArrayHasKey('erroraddtogroupgroupname', $errors);

// Cohort is not allowed on a given category context.
$enrolmentdata['cohortname'] = $cohort2->name;
$errors = $cohortplugin->validate_enrol_plugin_data($enrolmentdata, $course->id);
$this->assertArrayHasKey('contextnotallowed', $errors);

// Group does not exist.
unset($enrolmentdata['addtogroup']);
$errors = $cohortplugin->validate_enrol_plugin_data($enrolmentdata, $course->id);
$this->assertArrayHasKey('errorinvalidgroup', $errors);

// Valid data when trying to create a group.
$enrolmentdata['cohortname'] = $cohort1->name;
$enrolmentdata['addtogroup'] = 1;
unset($enrolmentdata['groupname']);
$errors = $cohortplugin->validate_enrol_plugin_data($enrolmentdata, $course->id);
$this->assertEmpty($errors);

// Valid data when trying to add to existing group.
$enrolmentdata['groupname'] = $group1->name;
unset($enrolmentdata['addtogroup']);
$errors = $cohortplugin->validate_enrol_plugin_data($enrolmentdata, $course->id);
$this->assertEmpty($errors);

// Valid data when trying without group mode.
$enrolmentdata['addtogroup'] = 0;
unset($enrolmentdata['groupname']);
$errors = $cohortplugin->validate_enrol_plugin_data($enrolmentdata, $course->id);
$this->assertEmpty($errors);
}

}

0 comments on commit 075377a

Please sign in to comment.