Proyectos de Subversion Moodle

Rev

Rev 11 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 11 Rev 1441
Línea 34... Línea 34...
34
 * @package    core
34
 * @package    core
35
 * @category   phpunit
35
 * @category   phpunit
36
 * @copyright  2012 Petr Skoda {@link http://skodak.org}
36
 * @copyright  2012 Petr Skoda {@link http://skodak.org}
37
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38
 */
38
 */
39
class enrollib_test extends advanced_testcase {
39
final class enrollib_test extends advanced_testcase {
Línea 40... Línea 40...
40
 
40
 
41
    public function test_enrol_get_all_users_courses(): void {
41
    public function test_enrol_get_all_users_courses(): void {
Línea 42... Línea 42...
42
        global $DB, $CFG;
42
        global $DB, $CFG;
Línea -... Línea 43...
-
 
43
 
-
 
44
        $this->resetAfterTest();
-
 
45
 
43
 
46
        // Use a mock incrementing clock to ensure deterministic and testable time values.
44
        $this->resetAfterTest();
47
        $clock = $this->mock_clock_with_incrementing(1750000000);
45
 
48
 
46
        $studentrole = $DB->get_record('role', array('shortname'=>'student'));
49
        $studentrole = $DB->get_record('role', array('shortname'=>'student'));
Línea 94... Línea 97...
94
        $manual->enrol_user($maninstance1, $user4->id, $teacherrole->id, 0, 0, ENROL_USER_SUSPENDED);
97
        $manual->enrol_user($maninstance1, $user4->id, $teacherrole->id, 0, 0, ENROL_USER_SUSPENDED);
95
        $manual->enrol_user($maninstance1, $admin->id, $studentrole->id);
98
        $manual->enrol_user($maninstance1, $admin->id, $studentrole->id);
Línea 96... Línea 99...
96
 
99
 
97
        $manual->enrol_user($maninstance2, $user1->id);
100
        $manual->enrol_user($maninstance2, $user1->id);
98
        $manual->enrol_user($maninstance2, $user2->id);
101
        $manual->enrol_user($maninstance2, $user2->id);
Línea 99... Línea 102...
99
        $manual->enrol_user($maninstance2, $user3->id, 0, 1, time()+(60*60));
102
        $manual->enrol_user($maninstance2, $user3->id, 0, 1, $clock->time() + HOURSECS);
100
 
103
 
101
        $manual->enrol_user($maninstance3, $user1->id);
104
        $manual->enrol_user($maninstance3, $user1->id);
102
        $manual->enrol_user($maninstance3, $user2->id);
105
        $manual->enrol_user($maninstance3, $user2->id);
Línea -... Línea 106...
-
 
106
        $manual->enrol_user($maninstance3, $user3->id, 0, 1, $clock->time() - HOURSECS);
Línea 103... Línea 107...
103
        $manual->enrol_user($maninstance3, $user3->id, 0, 1, time()-(60*60));
107
        $manual->enrol_user($maninstance3, $user4->id, 0, 0, 0, ENROL_USER_SUSPENDED);
104
        $manual->enrol_user($maninstance3, $user4->id, 0, 0, 0, ENROL_USER_SUSPENDED);
108
 
Línea 105... Línea 109...
105
 
109
        $manual->enrol_user($maninstance4, $user5->id, 0, $clock->time());
Línea 150... Línea 154...
150
 
154
 
151
        $courses = enrol_get_all_users_courses($user4->id, true);
155
        $courses = enrol_get_all_users_courses($user4->id, true);
152
        $this->assertCount(0, $courses);
156
        $this->assertCount(0, $courses);
Línea -... Línea 157...
-
 
157
        $this->assertEquals(array(), array_keys($courses));
-
 
158
 
-
 
159
        $courses = enrol_get_all_users_courses($user5->id, true);
-
 
160
        $this->assertCount(1, $courses);
153
        $this->assertEquals(array(), array_keys($courses));
161
        $this->assertEquals([$course4->id], array_keys($courses));
Línea 154... Línea 162...
154
 
162
 
155
        // Make sure sorting and columns work.
163
        // Make sure sorting and columns work.
Línea 379... Línea 387...
379
    /**
387
    /**
380
     * Data provider for test_enrol_course_delete_with_userid().
388
     * Data provider for test_enrol_course_delete_with_userid().
381
     *
389
     *
382
     * @return array
390
     * @return array
383
     */
391
     */
384
    public function enrol_course_delete_with_userid_provider() {
392
    public static function enrol_course_delete_with_userid_provider(): array {
385
        return [
393
        return [
386
            'The teacher can un-enrol users in a course' =>
394
            'The teacher can un-enrol users in a course' =>
387
                [
395
                [
388
                    'excludedcapabilities' => [],
396
                    'excludedcapabilities' => [],
389
                    'results' => [
397
                    'expected' => [
390
                        // Whether certain enrolment related data still exists in the course after the deletion.
398
                        // Whether certain enrolment related data still exists in the course after the deletion.
391
                        // When the user has the capabilities to un-enrol users and the enrolment plugins allow manual
399
                        // When the user has the capabilities to un-enrol users and the enrolment plugins allow manual
392
                        // unenerolment than all course enrolment data should be removed.
400
                        // unenerolment than all course enrolment data should be removed.
393
                        'Manual course enrolment instance exists' => false,
401
                        'Manual course enrolment instance exists' => false,
394
                        'Self course enrolment instance exists' => false,
402
                        'Self course enrolment instance exists' => false,
Línea 404... Línea 412...
404
                [
412
                [
405
                    'excludedcapabilities' => [
413
                    'excludedcapabilities' => [
406
                        // Exclude the following capabilities for the editing teacher.
414
                        // Exclude the following capabilities for the editing teacher.
407
                        'enrol/self:unenrol'
415
                        'enrol/self:unenrol'
408
                    ],
416
                    ],
409
                    'results' => [
417
                    'expected' => [
410
                        // When the user does not have the capabilities to un-enrol self enrolled users, the data
418
                        // When the user does not have the capabilities to un-enrol self enrolled users, the data
411
                        // related to this enrolment method should not be removed. Everything else should be removed.
419
                        // related to this enrolment method should not be removed. Everything else should be removed.
412
                        'Manual course enrolment instance exists' => false,
420
                        'Manual course enrolment instance exists' => false,
413
                        'Self course enrolment instance exists' => true,
421
                        'Self course enrolment instance exists' => true,
414
                        'User 1 course enrolment exists' => false,
422
                        'User 1 course enrolment exists' => false,
Línea 424... Línea 432...
424
                    'excludedcapabilities' => [
432
                    'excludedcapabilities' => [
425
                        // Exclude the following capabilities for the editing teacher.
433
                        // Exclude the following capabilities for the editing teacher.
426
                        'enrol/manual:unenrol',
434
                        'enrol/manual:unenrol',
427
                        'enrol/self:unenrol'
435
                        'enrol/self:unenrol'
428
                    ],
436
                    ],
429
                    'results' => [
437
                    'expected' => [
430
                        // When the user does not have the capabilities to un-enrol self and manually enrolled users,
438
                        // When the user does not have the capabilities to un-enrol self and manually enrolled users,
431
                        // the data related to these enrolment methods should not be removed.
439
                        // the data related to these enrolment methods should not be removed.
432
                        'Manual course enrolment instance exists' => true,
440
                        'Manual course enrolment instance exists' => true,
433
                        'Self course enrolment instance exists' => true,
441
                        'Self course enrolment instance exists' => true,
434
                        'User 1 course enrolment exists' => true,
442
                        'User 1 course enrolment exists' => true,
Línea 1026... Línea 1034...
1026
    /**
1034
    /**
1027
     * Data provider for {@see test_enrol_get_my_courses_by_time}
1035
     * Data provider for {@see test_enrol_get_my_courses_by_time}
1028
     *
1036
     *
1029
     * @return array
1037
     * @return array
1030
     */
1038
     */
1031
    public function enrol_get_my_courses_by_time_provider(): array {
1039
    public static function enrol_get_my_courses_by_time_provider(): array {
1032
        return [
1040
        return [
1033
            'No start or end time' =>
1041
            'No start or end time' =>
1034
                [null, null, true],
1042
                [null, null, true],
1035
            'Start time now, no end time' =>
1043
            'Start time now, no end time' =>
1036
                [0, null, true],
1044
                [0, null, true],
Línea 1188... Línea 1196...
1188
    }
1196
    }
Línea 1189... Línea 1197...
1189
 
1197
 
1190
    /**
1198
    /**
1191
     * Test cases for the test_enrol_get_my_courses_sort_by_last_access test.
1199
     * Test cases for the test_enrol_get_my_courses_sort_by_last_access test.
1192
     */
1200
     */
1193
    public function get_enrol_get_my_courses_sort_by_last_access_test_cases() {
1201
    public static function get_enrol_get_my_courses_sort_by_last_access_test_cases(): array {
Línea 1194... Línea 1202...
1194
        $now = time();
1202
        $now = time();
1195
 
1203
 
1196
        $enrolledcoursesdata = [
1204
        $enrolledcoursesdata = [
Línea 1291... Línea 1299...
1291
    }
1299
    }
Línea 1292... Línea 1300...
1292
 
1300
 
1293
    /**
1301
    /**
1294
     * Test the get_enrolled_courses_by_timeline_classification function.
1302
     * Test the get_enrolled_courses_by_timeline_classification function.
1295
     *
1303
     *
1296
     * @dataProvider get_enrol_get_my_courses_sort_by_last_access_test_cases()
1304
     * @dataProvider get_enrol_get_my_courses_sort_by_last_access_test_cases
1297
     * @param array $enrolledcoursesdata Courses to create and enrol the user in
1305
     * @param array $enrolledcoursesdata Courses to create and enrol the user in
1298
     * @param array $unenrolledcoursesdata Courses to create nut not enrol the user in
1306
     * @param array $unenrolledcoursesdata Courses to create nut not enrol the user in
1299
     * @param string $sort Sort string for the enrol function
1307
     * @param string $sort Sort string for the enrol function
1300
     * @param int $limit Maximum number of results
1308
     * @param int $limit Maximum number of results
Línea 1441... Línea 1449...
1441
    }
1449
    }
Línea 1442... Línea 1450...
1442
 
1450
 
1443
    /**
1451
    /**
1444
     * Test get_enrolled_with_capabilities_join cannotmatchanyrows attribute.
1452
     * Test get_enrolled_with_capabilities_join cannotmatchanyrows attribute.
1445
     *
1453
     *
1446
     * @dataProvider get_enrolled_with_capabilities_join_cannotmatchanyrows_data()
1454
     * @dataProvider get_enrolled_with_capabilities_join_cannotmatchanyrows_data
1447
     * @param string $capability the tested capability
1455
     * @param string $capability the tested capability
1448
     * @param bool $useprohibit if the capability must be assigned to prohibit
1456
     * @param bool $useprohibit if the capability must be assigned to prohibit
1449
     * @param int $expectedmatch expected cannotmatchanyrows value
1457
     * @param int $expectedmatch expected cannotmatchanyrows value
1450
     * @param int $expectedcount expceted count value
1458
     * @param int $expectedcount expceted count value
Línea 1488... Línea 1496...
1488
    /**
1496
    /**
1489
     * Data provider for test_get_enrolled_with_capabilities_join_cannotmatchanyrows
1497
     * Data provider for test_get_enrolled_with_capabilities_join_cannotmatchanyrows
1490
     *
1498
     *
1491
     * @return @array of testing scenarios
1499
     * @return @array of testing scenarios
1492
     */
1500
     */
1493
    public function get_enrolled_with_capabilities_join_cannotmatchanyrows_data() {
1501
    public static function get_enrolled_with_capabilities_join_cannotmatchanyrows_data(): array {
1494
        return [
1502
        return [
1495
            'no prohibits, no capability' => [
1503
            'no prohibits, no capability' => [
1496
                'capability' => '',
1504
                'capability' => '',
1497
                'useprohibit' => false,
1505
                'useprohibit' => false,
1498
                'expectedmatch' => 0,
1506
                'expectedmatch' => 0,
Línea 1528... Línea 1536...
1528
 
1536
 
1529
    /**
1537
    /**
1530
     * Data provided for test_enrol_check_plugins_with_empty_config_value test.
1538
     * Data provided for test_enrol_check_plugins_with_empty_config_value test.
1531
     * @return array
1539
     * @return array
1532
     */
1540
     */
1533
    public function empty_config_data_provider(): array {
1541
    public static function empty_config_data_provider(): array {
1534
        return [
1542
        return [
1535
            [0],
1543
            [0],
1536
            ["0"],
1544
            ["0"],
1537
            [false],
1545
            [false],
Línea 1737... Línea 1745...
1737
    }
1745
    }
Línea 1738... Línea 1746...
1738
 
1746
 
1739
    /**
1747
    /**
1740
     * Test the behaviour of validate_enrol_plugin_data().
1748
     * Test the behaviour of validate_enrol_plugin_data().
1741
     *
1749
     *
1742
     * @covers ::validate_enrol_plugin_data
1750
     * @covers \enrol_plugin::validate_enrol_plugin_data
1743
     */
1751
     */
1744
    public function test_validate_enrol_plugin_data(): void {
1752
    public function test_validate_enrol_plugin_data(): void {
Línea 1745... Línea 1753...
1745
        $this->resetAfterTest();
1753
        $this->resetAfterTest();
Línea 1759... Línea 1767...
1759
    }
1767
    }
Línea 1760... Línea 1768...
1760
 
1768
 
1761
    /**
1769
    /**
1762
     * Test the behaviour of update_enrol_plugin_data().
1770
     * Test the behaviour of update_enrol_plugin_data().
1763
     *
1771
     *
1764
     * @covers ::update_enrol_plugin_data
1772
     * @covers \enrol_plugin::update_enrol_plugin_data
1765
     */
1773
     */
1766
    public function test_update_enrol_plugin_data(): void {
1774
    public function test_update_enrol_plugin_data(): void {
1767
        global $DB;
1775
        global $DB;
1768
        $this->resetAfterTest();
1776
        $this->resetAfterTest();
Línea 1825... Línea 1833...
1825
        $expectedinstance->enrolperiod = 7200;
1833
        $expectedinstance->enrolperiod = 7200;
1826
        $expectedinstance->enrolenddate = $expectedinstance->enrolstartdate + $expectedinstance->enrolperiod;
1834
        $expectedinstance->enrolenddate = $expectedinstance->enrolstartdate + $expectedinstance->enrolperiod;
1827
        $modifiedinstance = $manualplugin->update_enrol_plugin_data($course->id, $enrolmentdata, $instance);
1835
        $modifiedinstance = $manualplugin->update_enrol_plugin_data($course->id, $enrolmentdata, $instance);
1828
        $this->assertEquals($expectedinstance, $modifiedinstance);
1836
        $this->assertEquals($expectedinstance, $modifiedinstance);
1829
    }
1837
    }
-
 
1838
 
-
 
1839
    /**
-
 
1840
     * Test case for checking the email greetings in various user notification emails.
-
 
1841
     *
-
 
1842
     * @covers \enrol_plugin::send_course_welcome_message_to_user
-
 
1843
     */
-
 
1844
    public function test_email_greetings(): void {
-
 
1845
        global $DB;
-
 
1846
        $this->resetAfterTest();
-
 
1847
 
-
 
1848
        // Create course.
-
 
1849
        $course = $this->getDataGenerator()->create_course([
-
 
1850
            'fullname' => 'Course 1',
-
 
1851
            'shortname' => 'C1',
-
 
1852
        ]);
-
 
1853
        // Create user.
-
 
1854
        $student = $this->getDataGenerator()->create_user();
-
 
1855
        // Get manual plugin.
-
 
1856
        $manualplugin = enrol_get_plugin('manual');
-
 
1857
        $maninstance = $DB->get_record(
-
 
1858
            'enrol',
-
 
1859
            ['courseid' => $course->id, 'enrol' => 'manual'],
-
 
1860
            '*',
-
 
1861
            MUST_EXIST,
-
 
1862
        );
-
 
1863
 
-
 
1864
        $messagesink = $this->redirectMessages();
-
 
1865
        $manualplugin->send_course_welcome_message_to_user(
-
 
1866
            instance: $maninstance,
-
 
1867
            userid: $student->id,
-
 
1868
            sendoption: ENROL_SEND_EMAIL_FROM_NOREPLY,
-
 
1869
            message: '',
-
 
1870
        );
-
 
1871
        $messages = $messagesink->get_messages_by_component_and_type(
-
 
1872
            'moodle',
-
 
1873
            'enrolcoursewelcomemessage',
-
 
1874
        );
-
 
1875
        $this->assertNotEmpty($messages);
-
 
1876
        $message = reset($messages);
-
 
1877
        $this->assertStringContainsString('Hi ' . $student->firstname, quoted_printable_decode($message->fullmessage));
-
 
1878
    }
1830
}
1879
}