Ignore:
Timestamp:
May 2, 2016, 12:09:23 PM (4 years ago)
Author:
jrpelegrina
Message:

Updated to moodle 3.0.3

Location:
moodle/trunk/fuentes/enrol/meta/tests
Files:
2 added
1 edited

Legend:

Unmodified
Added
Removed
  • moodle/trunk/fuentes/enrol/meta/tests/plugin_test.php

    r136 r1331  
    441441    }
    442442
     443    public function test_add_to_group() {
     444        global $CFG, $DB;
     445
     446        require_once($CFG->dirroot.'/group/lib.php');
     447
     448        $this->resetAfterTest(true);
     449
     450        $metalplugin = enrol_get_plugin('meta');
     451
     452        $user1 = $this->getDataGenerator()->create_user();
     453        $user4 = $this->getDataGenerator()->create_user();
     454
     455        $course1 = $this->getDataGenerator()->create_course();
     456        $course2 = $this->getDataGenerator()->create_course();
     457        $course3 = $this->getDataGenerator()->create_course();
     458        $manualenrol1 = $DB->get_record('enrol', array('courseid' => $course1->id, 'enrol' => 'manual'), '*', MUST_EXIST);
     459        $manualenrol2 = $DB->get_record('enrol', array('courseid' => $course2->id, 'enrol' => 'manual'), '*', MUST_EXIST);
     460
     461        $student = $DB->get_record('role', array('shortname' => 'student'));
     462        $teacher = $DB->get_record('role', array('shortname' => 'teacher'));
     463
     464        $id = groups_create_group((object)array('name' => 'Group 1 in course 3', 'courseid' => $course3->id));
     465        $group31 = $DB->get_record('groups', array('id' => $id), '*', MUST_EXIST);
     466        $id = groups_create_group((object)array('name' => 'Group 2 in course 4', 'courseid' => $course3->id));
     467        $group32 = $DB->get_record('groups', array('id' => $id), '*', MUST_EXIST);
     468
     469        $this->enable_plugin();
     470
     471        $e1 = $metalplugin->add_instance($course3, array('customint1' => $course1->id, 'customint2' => $group31->id));
     472        $e2 = $metalplugin->add_instance($course3, array('customint1' => $course2->id, 'customint2' => $group32->id));
     473
     474        $this->getDataGenerator()->enrol_user($user1->id, $course1->id, $student->id);
     475        $this->getDataGenerator()->enrol_user($user4->id, $course1->id, $teacher->id);
     476
     477        $this->getDataGenerator()->enrol_user($user1->id, $course2->id, $student->id);
     478
     479        // Now make sure users are in the correct groups.
     480        $this->assertTrue(groups_is_member($group31->id, $user1->id));
     481        $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $group31->id, 'userid' => $user1->id,
     482            'component' => 'enrol_meta', 'itemid' => $e1)));
     483        $this->assertTrue(groups_is_member($group32->id, $user1->id));
     484        $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $group32->id, 'userid' => $user1->id,
     485            'component' => 'enrol_meta', 'itemid' => $e2)));
     486
     487        $this->assertTrue(groups_is_member($group31->id, $user4->id));
     488        $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $group31->id, 'userid' => $user4->id,
     489            'component' => 'enrol_meta', 'itemid' => $e1)));
     490
     491        // Make sure everything is the same after sync.
     492        enrol_meta_sync(null, false);
     493        $this->assertTrue(groups_is_member($group31->id, $user1->id));
     494        $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $group31->id, 'userid' => $user1->id,
     495            'component' => 'enrol_meta', 'itemid' => $e1)));
     496        $this->assertTrue(groups_is_member($group32->id, $user1->id));
     497        $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $group32->id, 'userid' => $user1->id,
     498            'component' => 'enrol_meta', 'itemid' => $e2)));
     499
     500        $this->assertTrue(groups_is_member($group31->id, $user4->id));
     501        $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $group31->id, 'userid' => $user4->id,
     502            'component' => 'enrol_meta', 'itemid' => $e1)));
     503
     504        set_config('unenrolaction', ENROL_EXT_REMOVED_UNENROL, 'enrol_meta');
     505
     506        // When user 1 is unenrolled from course1, he is removed from group31 but still present in group32.
     507        enrol_get_plugin('manual')->unenrol_user($manualenrol1, $user1->id);
     508        $this->assertFalse(groups_is_member($group31->id, $user1->id));
     509        $this->assertTrue(groups_is_member($group32->id, $user1->id));
     510        $this->assertTrue(is_enrolled(context_course::instance($course3->id), $user1, '', true)); // He still has active enrolment.
     511        // And the same after sync.
     512        enrol_meta_sync(null, false);
     513        $this->assertFalse(groups_is_member($group31->id, $user1->id));
     514        $this->assertTrue(groups_is_member($group32->id, $user1->id));
     515        $this->assertTrue(is_enrolled(context_course::instance($course3->id), $user1, '', true));
     516
     517        // Unenroll user1 from course2 and make sure he is completely unenrolled from course3.
     518        enrol_get_plugin('manual')->unenrol_user($manualenrol2, $user1->id);
     519        $this->assertFalse(groups_is_member($group32->id, $user1->id));
     520        $this->assertFalse(is_enrolled(context_course::instance($course3->id), $user1));
     521
     522        set_config('unenrolaction', ENROL_EXT_REMOVED_SUSPENDNOROLES, 'enrol_meta');
     523
     524        // When user is unenrolled in this case, he is still a member of a group (but enrolment is suspended).
     525        enrol_get_plugin('manual')->unenrol_user($manualenrol1, $user4->id);
     526        $this->assertTrue(groups_is_member($group31->id, $user4->id));
     527        $this->assertTrue(is_enrolled(context_course::instance($course3->id), $user4));
     528        $this->assertFalse(is_enrolled(context_course::instance($course3->id), $user4, '', true));
     529        enrol_meta_sync(null, false);
     530        $this->assertTrue(groups_is_member($group31->id, $user4->id));
     531        $this->assertTrue(is_enrolled(context_course::instance($course3->id), $user4));
     532        $this->assertFalse(is_enrolled(context_course::instance($course3->id), $user4, '', true));
     533    }
     534
     535    /**
     536     * Enrol users from another course into a course where one of the members is already enrolled
     537     * and is a member of the same group.
     538     */
     539    public function test_add_to_group_with_member() {
     540        global $CFG, $DB;
     541
     542        require_once($CFG->dirroot.'/group/lib.php');
     543
     544        $this->resetAfterTest(true);
     545
     546        $metalplugin = enrol_get_plugin('meta');
     547
     548        $user1 = $this->getDataGenerator()->create_user();
     549        $user2 = $this->getDataGenerator()->create_user();
     550
     551        $course1 = $this->getDataGenerator()->create_course();
     552        $course2 = $this->getDataGenerator()->create_course();
     553        $manualenrol1 = $DB->get_record('enrol', array('courseid' => $course1->id, 'enrol' => 'manual'), '*', MUST_EXIST);
     554        $manualenrol2 = $DB->get_record('enrol', array('courseid' => $course2->id, 'enrol' => 'manual'), '*', MUST_EXIST);
     555
     556        $student = $DB->get_record('role', array('shortname' => 'student'));
     557
     558        $groupid = groups_create_group((object)array('name' => 'Grp', 'courseid' => $course2->id));
     559
     560        $this->enable_plugin();
     561        set_config('unenrolaction', ENROL_EXT_REMOVED_UNENROL, 'enrol_meta');
     562
     563        // Manually enrol user1 to course2 and add him to group.
     564        // Manually enrol user2 to course2 but do not add him to the group.
     565        enrol_get_plugin('manual')->enrol_user($manualenrol2, $user1->id, $student->id);
     566        groups_add_member($groupid, $user1->id);
     567        enrol_get_plugin('manual')->enrol_user($manualenrol2, $user2->id, $student->id);
     568        $this->assertTrue(groups_is_member($groupid, $user1->id));
     569        $this->assertFalse(groups_is_member($groupid, $user2->id));
     570
     571        // Add instance of meta enrolment in course2 linking to course1 and enrol both users in course1.
     572        $metalplugin->add_instance($course2, array('customint1' => $course1->id, 'customint2' => $groupid));
     573
     574        enrol_get_plugin('manual')->enrol_user($manualenrol1, $user1->id, $student->id);
     575        enrol_get_plugin('manual')->enrol_user($manualenrol1, $user2->id, $student->id);
     576
     577        // Both users now should be members of the group.
     578        $this->assertTrue(groups_is_member($groupid, $user1->id));
     579        $this->assertTrue(groups_is_member($groupid, $user2->id));
     580
     581        // Ununerol both users from course1.
     582        enrol_get_plugin('manual')->unenrol_user($manualenrol1, $user1->id);
     583        enrol_get_plugin('manual')->unenrol_user($manualenrol1, $user2->id);
     584
     585        // User1 should still be member of the group because he was added there manually. User2 should no longer be there.
     586        $this->assertTrue(groups_is_member($groupid, $user1->id));
     587        $this->assertFalse(groups_is_member($groupid, $user2->id));
     588
     589        // Assert that everything is the same after sync.
     590        enrol_meta_sync();
     591
     592        $this->assertTrue(groups_is_member($groupid, $user1->id));
     593        $this->assertFalse(groups_is_member($groupid, $user2->id));
     594
     595    }
     596
    443597    /**
    444598     * Test user_enrolment_created event.
     
    553707        $this->assertEventContextNotUsed($event);
    554708    }
     709
     710    /**
     711     * Test that a new group with the name of the course is created.
     712     */
     713    public function test_enrol_meta_create_new_group() {
     714        global $DB;
     715        $this->resetAfterTest();
     716        // Create two courses.
     717        $course = $this->getDataGenerator()->create_course(array('fullname' => 'Mathematics'));
     718        $course2 = $this->getDataGenerator()->create_course(array('fullname' => 'Physics'));
     719        $metacourse = $this->getDataGenerator()->create_course(array('fullname' => 'All sciences'));
     720        // Run the function.
     721        $groupid = enrol_meta_create_new_group($metacourse->id, $course->id);
     722        // Check the results.
     723        $group = $DB->get_record('groups', array('id' => $groupid));
     724        // The group name should match the course name.
     725        $this->assertEquals('Mathematics course', $group->name);
     726        // Group course id should match the course id.
     727        $this->assertEquals($metacourse->id, $group->courseid);
     728
     729        // Create a group that will have the same name as the course.
     730        $groupdata = new stdClass();
     731        $groupdata->courseid = $metacourse->id;
     732        $groupdata->name = 'Physics course';
     733        groups_create_group($groupdata);
     734        // Create a group for the course 2 in metacourse.
     735        $groupid = enrol_meta_create_new_group($metacourse->id, $course2->id);
     736        $groupinfo = $DB->get_record('groups', array('id' => $groupid));
     737        // Check that the group name has been changed.
     738        $this->assertEquals('Physics course (2)', $groupinfo->name);
     739
     740        // Create a group for the course 2 in metacourse.
     741        $groupid = enrol_meta_create_new_group($metacourse->id, $course2->id);
     742        $groupinfo = $DB->get_record('groups', array('id' => $groupid));
     743        // Check that the group name has been changed.
     744        $this->assertEquals('Physics course (3)', $groupinfo->name);
     745    }
     746
     747    /**
     748     * Test that enrolment timestart-timeend is respected in meta course.
     749     */
     750    public function test_timeend() {
     751        global $CFG, $DB;
     752
     753        $this->resetAfterTest(true);
     754
     755        $timeinfuture = time() + DAYSECS;
     756        $timeinpast = time() - DAYSECS;
     757
     758        $metalplugin = enrol_get_plugin('meta');
     759        $manplugin = enrol_get_plugin('manual');
     760
     761        $user1 = $this->getDataGenerator()->create_user();
     762        $user2 = $this->getDataGenerator()->create_user();
     763        $user3 = $this->getDataGenerator()->create_user();
     764        $user4 = $this->getDataGenerator()->create_user();
     765        $user5 = $this->getDataGenerator()->create_user();
     766
     767        $course1 = $this->getDataGenerator()->create_course();
     768        $course2 = $this->getDataGenerator()->create_course();
     769        $course3 = $this->getDataGenerator()->create_course();
     770        $manual1 = $DB->get_record('enrol', array('courseid' => $course1->id, 'enrol' => 'manual'), '*', MUST_EXIST);
     771
     772        $student = $DB->get_record('role', array('shortname' => 'student'));
     773
     774        $this->enable_plugin();
     775
     776        // Create instance of enrol_meta in course2 when there are no enrolments present.
     777        $meta2id = $metalplugin->add_instance($course2, array('customint1' => $course1->id));
     778
     779        $expectedenrolments = array(
     780            $user1->id => array(0, 0, ENROL_USER_ACTIVE),
     781            $user2->id => array($timeinpast, 0, ENROL_USER_ACTIVE),
     782            $user3->id => array(0, $timeinfuture, ENROL_USER_ACTIVE),
     783            $user4->id => array($timeinpast, $timeinfuture, ENROL_USER_ACTIVE),
     784            $user5->id => array(0, 0, ENROL_USER_SUSPENDED),
     785        );
     786        foreach ($expectedenrolments as $userid => $data) {
     787            $expectedenrolments[$userid] = (object)(array('userid' => $userid) +
     788                    array_combine(array('timestart', 'timeend', 'status'), $data));
     789        }
     790
     791        // Enrol users manually in course 1.
     792        foreach ($expectedenrolments as $e) {
     793            $manplugin->enrol_user($manual1, $e->userid, $student->id, $e->timestart, $e->timeend, $e->status);
     794        }
     795
     796        $enrolments = $DB->get_records('user_enrolments', array('enrolid' => $manual1->id), 'userid', 'userid, timestart, timeend, status');
     797        $this->assertEquals($expectedenrolments, $enrolments);
     798
     799        // Make sure that the same enrolments are now present in course2 under meta enrolment.
     800        $enrolments = $DB->get_records('user_enrolments', array('enrolid' => $meta2id), '', 'userid, timestart, timeend, status');
     801        $this->assertEquals($expectedenrolments, $enrolments);
     802
     803        // Create instance of enrol_meta in course3 and run sync.
     804        $meta3id = $metalplugin->add_instance($course3, array('customint1' => $course1->id));
     805        enrol_meta_sync($course3->id);
     806
     807        // Make sure that the same enrolments are now present in course3 under meta enrolment.
     808        $enrolments = $DB->get_records('user_enrolments', array('enrolid' => $meta3id), '', 'userid, timestart, timeend, status');
     809        $this->assertEquals($expectedenrolments, $enrolments);
     810
     811        // Update some of the manual enrolments.
     812        $expectedenrolments[$user2->id]->timestart = $timeinpast - 60;
     813        $expectedenrolments[$user3->id]->timeend = $timeinfuture + 60;
     814        $expectedenrolments[$user4->id]->status = ENROL_USER_SUSPENDED;
     815        $expectedenrolments[$user5->id]->status = ENROL_USER_ACTIVE;
     816        foreach ($expectedenrolments as $e) {
     817            $manplugin->update_user_enrol($manual1, $e->userid, $e->status, $e->timestart, $e->timeend);
     818        }
     819
     820        // Make sure meta courses are also updated.
     821        $enrolments = $DB->get_records('user_enrolments', array('enrolid' => $meta2id), '', 'userid, timestart, timeend, status');
     822        $this->assertEquals($expectedenrolments, $enrolments);
     823        $enrolments = $DB->get_records('user_enrolments', array('enrolid' => $meta3id), '', 'userid, timestart, timeend, status');
     824        $this->assertEquals($expectedenrolments, $enrolments);
     825
     826        // Test meta sync. Imagine events are not working.
     827        $sink = $this->redirectEvents();
     828        $expectedenrolments[$user2->id]->timestart = $timeinpast;
     829        $expectedenrolments[$user3->id]->timeend = $timeinfuture;
     830        $expectedenrolments[$user4->id]->status = ENROL_USER_ACTIVE;
     831        $expectedenrolments[$user5->id]->status = ENROL_USER_SUSPENDED;
     832        foreach ($expectedenrolments as $e) {
     833            $manplugin->update_user_enrol($manual1, $e->userid, $e->status, $e->timestart, $e->timeend);
     834        }
     835
     836        // Make sure meta courses are updated only for the course that was synced.
     837        enrol_meta_sync($course3->id);
     838
     839        $enrolments = $DB->get_records('user_enrolments', array('enrolid' => $meta2id), '', 'userid, timestart, timeend, status');
     840        $this->assertNotEquals($expectedenrolments, $enrolments);
     841
     842        $enrolments = $DB->get_records('user_enrolments', array('enrolid' => $meta3id), '', 'userid, timestart, timeend, status');
     843        $this->assertEquals($expectedenrolments, $enrolments);
     844
     845        $sink->close();
     846
     847        // Disable manual enrolment in course1 and make sure all user enrolments in course2 are suspended.
     848        $manplugin->update_status($manual1, ENROL_INSTANCE_DISABLED);
     849        $allsuspendedenrolemnts = array_combine(array_keys($expectedenrolments), array_fill(0, 5, ENROL_USER_SUSPENDED));
     850        $enrolmentstatuses = $DB->get_records_menu('user_enrolments', array('enrolid' => $meta2id), '', 'userid, status');
     851        $this->assertEquals($allsuspendedenrolemnts, $enrolmentstatuses);
     852
     853        $manplugin->update_status($manual1, ENROL_INSTANCE_ENABLED);
     854        $enrolments = $DB->get_records('user_enrolments', array('enrolid' => $meta2id), '', 'userid, timestart, timeend, status');
     855        $this->assertEquals($expectedenrolments, $enrolments);
     856
     857        // Disable events and repeat the same for course3 (testing sync):
     858        $sink = $this->redirectEvents();
     859        $manplugin->update_status($manual1, ENROL_INSTANCE_DISABLED);
     860        enrol_meta_sync($course3->id);
     861        $enrolmentstatuses = $DB->get_records_menu('user_enrolments', array('enrolid' => $meta3id), '', 'userid, status');
     862        $this->assertEquals($allsuspendedenrolemnts, $enrolmentstatuses);
     863
     864        $manplugin->update_status($manual1, ENROL_INSTANCE_ENABLED);
     865        enrol_meta_sync($course3->id);
     866        $enrolments = $DB->get_records('user_enrolments', array('enrolid' => $meta3id), '', 'userid, timestart, timeend, status');
     867        $this->assertEquals($expectedenrolments, $enrolments);
     868        $sink->close();
     869    }
    555870}
Note: See TracChangeset for help on using the changeset viewer.