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 Andrew Davis
36
 * @copyright  2012 Andrew Davis
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 modinfolib_test extends advanced_testcase {
39
final class modinfolib_test extends advanced_testcase {
40
    /**
40
    /**
41
     * Setup to ensure that fixtures are loaded.
41
     * Setup to ensure that fixtures are loaded.
42
     */
42
     */
43
    public static function setUpBeforeClass(): void {
43
    public static function setUpBeforeClass(): void {
44
        global $CFG;
44
        global $CFG;
45
        require_once($CFG->dirroot . '/course/lib.php');
45
        require_once($CFG->dirroot . '/course/lib.php');
46
        require_once($CFG->libdir . '/tests/fixtures/sectiondelegatetest.php');
46
        require_once($CFG->libdir . '/tests/fixtures/sectiondelegatetest.php');
-
 
47
        parent::setUpBeforeClass();
47
    }
48
    }
Línea 48... Línea 49...
48
 
49
 
49
    public function test_section_info_properties(): void {
50
    public function test_section_info_properties(): void {
Línea 411... Línea 412...
411
        global $USER, $DB;
412
        global $USER, $DB;
Línea 412... Línea 413...
412
 
413
 
413
        $this->resetAfterTest();
414
        $this->resetAfterTest();
Línea -... Línea 415...
-
 
415
        $this->setAdminUser();
-
 
416
 
414
        $this->setAdminUser();
417
        set_config('allowstealth', true);
415
 
418
 
416
        // Generate the course and some modules. Make one section hidden.
-
 
417
        $course = $this->getDataGenerator()->create_course(
419
        // Generate the course and some modules. Make one section hidden.
418
                array('format' => 'topics',
420
        $course = $this->getDataGenerator()->create_course(
419
                    'numsections' => 3),
421
                ['format' => 'topics', 'numsections' => 3],
420
                array('createsections' => true));
422
                ['createsections' => true]);
421
        $DB->execute('UPDATE {course_sections} SET visible = 0 WHERE course = ? and section = ?',
-
 
422
                array($course->id, 3));
423
        $DB->execute('UPDATE {course_sections} SET visible = 0 WHERE course = ? and section = ?',
423
        $coursecontext = context_course::instance($course->id);
424
                [$course->id, 3]);
424
        $forum0 = $this->getDataGenerator()->create_module('forum',
425
        $forum0 = $this->getDataGenerator()->create_module('forum',
425
                array('course' => $course->id), array('section' => 0));
426
                ['course' => $course->id, 'section' => 0]);
-
 
427
        $assign0 = $this->getDataGenerator()->create_module('assign',
-
 
428
                ['course' => $course->id, 'section' => 0, 'visible' => 0]);
426
        $assign0 = $this->getDataGenerator()->create_module('assign',
429
        $page0 = $this->getDataGenerator()->create_module('page',
427
                array('course' => $course->id), array('section' => 0, 'visible' => 0));
430
                ['course' => $course->id, 'section' => 0, 'visibleoncoursepage' => 0]);
428
        $forum1 = $this->getDataGenerator()->create_module('forum',
431
        $forum1 = $this->getDataGenerator()->create_module('forum',
429
                array('course' => $course->id), array('section' => 1));
432
                ['course' => $course->id, 'section' => 1]);
430
        $assign1 = $this->getDataGenerator()->create_module('assign',
433
        $assign1 = $this->getDataGenerator()->create_module('assign',
431
                array('course' => $course->id), array('section' => 1));
434
                ['course' => $course->id, 'section' => 1]);
432
        $page1 = $this->getDataGenerator()->create_module('page',
435
        $page1 = $this->getDataGenerator()->create_module('page',
433
                array('course' => $course->id), array('section' => 1));
436
                ['course' => $course->id, 'section' => 1]);
Línea 434... Línea 437...
434
        $page3 = $this->getDataGenerator()->create_module('page',
437
        $page3 = $this->getDataGenerator()->create_module('page',
Línea -... Línea 438...
-
 
438
                ['course' => $course->id, 'section' => 3]);
435
                array('course' => $course->id), array('section' => 3));
439
 
436
 
440
        $modinfo = get_fast_modinfo($course->id);
437
        $modinfo = get_fast_modinfo($course->id);
441
 
438
 
442
        $this->assertEquals(
-
 
443
                [$forum0->cmid, $assign0->cmid, $page0->cmid, $forum1->cmid, $assign1->cmid, $page1->cmid, $page3->cmid],
439
        $this->assertEquals(array($forum0->cmid, $assign0->cmid, $forum1->cmid, $assign1->cmid, $page1->cmid, $page3->cmid),
444
                array_keys($modinfo->cms));
440
                array_keys($modinfo->cms));
445
        $this->assertEquals($course->id, $modinfo->courseid);
-
 
446
        $this->assertEquals($USER->id, $modinfo->userid);
-
 
447
        $this->assertEquals([
441
        $this->assertEquals($course->id, $modinfo->courseid);
448
                0 => [$forum0->cmid, $assign0->cmid, $page0->cmid],
442
        $this->assertEquals($USER->id, $modinfo->userid);
449
                1 => [$forum1->cmid, $assign1->cmid, $page1->cmid],
443
        $this->assertEquals(array(0 => array($forum0->cmid, $assign0->cmid),
450
                3 => [$page3->cmid],
444
            1 => array($forum1->cmid, $assign1->cmid, $page1->cmid), 3 => array($page3->cmid)), $modinfo->sections);
451
            ], $modinfo->sections);
445
        $this->assertEquals(array('forum', 'assign', 'page'), array_keys($modinfo->instances));
452
        $this->assertEquals(['forum', 'assign', 'page'], array_keys($modinfo->instances));
-
 
453
        $this->assertEquals([$assign0->id, $assign1->id], array_keys($modinfo->instances['assign']));
446
        $this->assertEquals(array($assign0->id, $assign1->id), array_keys($modinfo->instances['assign']));
454
        $this->assertEquals([$forum0->id, $forum1->id], array_keys($modinfo->instances['forum']));
447
        $this->assertEquals(array($forum0->id, $forum1->id), array_keys($modinfo->instances['forum']));
455
        $this->assertEquals([$page0->id, $page1->id, $page3->id], array_keys($modinfo->instances['page']));
-
 
456
        $this->assertEquals(groups_get_user_groups($course->id), $modinfo->groups);
448
        $this->assertEquals(array($page1->id, $page3->id), array_keys($modinfo->instances['page']));
457
        $this->assertEquals([
449
        $this->assertEquals(groups_get_user_groups($course->id), $modinfo->groups);
458
                0 => [$forum0->cmid, $assign0->cmid, $page0->cmid],
450
        $this->assertEquals(array(0 => array($forum0->cmid, $assign0->cmid),
459
                1 => [$forum1->cmid, $assign1->cmid, $page1->cmid],
451
            1 => array($forum1->cmid, $assign1->cmid, $page1->cmid),
460
                3 => [$page3->cmid],
452
            3 => array($page3->cmid)), $modinfo->get_sections());
461
            ], $modinfo->get_sections());
453
        $this->assertEquals(array(0, 1, 2, 3), array_keys($modinfo->get_section_info_all()));
462
        $this->assertEquals([0, 1, 2, 3], array_keys($modinfo->get_section_info_all()));
454
        $this->assertEquals($forum0->cmid . ',' . $assign0->cmid, $modinfo->get_section_info(0)->sequence);
463
        $this->assertEquals($forum0->cmid . ',' . $assign0->cmid . ',' . $page0->cmid, $modinfo->get_section_info(0)->sequence);
455
        $this->assertEquals($forum1->cmid . ',' . $assign1->cmid . ',' . $page1->cmid, $modinfo->get_section_info(1)->sequence);
464
        $this->assertEquals($forum1->cmid . ',' . $assign1->cmid . ',' . $page1->cmid, $modinfo->get_section_info(1)->sequence);
456
        $this->assertEquals('', $modinfo->get_section_info(2)->sequence);
465
        $this->assertEquals('', $modinfo->get_section_info(2)->sequence);
457
        $this->assertEquals($page3->cmid, $modinfo->get_section_info(3)->sequence);
466
        $this->assertEquals($page3->cmid, $modinfo->get_section_info(3)->sequence);
458
        $this->assertEquals($course->id, $modinfo->get_course()->id);
467
        $this->assertEquals($course->id, $modinfo->get_course()->id);
459
        $names = array_keys($modinfo->get_used_module_names());
468
        $names = array_keys($modinfo->get_used_module_names());
460
        sort($names);
469
        sort($names);
461
        $this->assertEquals(array('assign', 'forum', 'page'), $names);
470
        $this->assertEquals(['assign', 'forum', 'page'], $names);
462
        $names = array_keys($modinfo->get_used_module_names(true));
471
        $names = array_keys($modinfo->get_used_module_names(true));
-
 
472
        sort($names);
-
 
473
        $this->assertEquals(['assign', 'forum', 'page'], $names);
-
 
474
        // Admin can see hidden modules/sections.
463
        sort($names);
475
        $this->assertTrue($modinfo->cms[$assign0->cmid]->uservisible);
Línea -... Línea 476...
-
 
476
        $this->assertTrue($modinfo->cms[$assign0->cmid]->is_visible_on_course_page());
-
 
477
        $this->assertTrue($modinfo->cms[$page0->cmid]->uservisible);
-
 
478
        $this->assertTrue($modinfo->cms[$page0->cmid]->is_visible_on_course_page());
-
 
479
        $this->assertTrue($modinfo->get_section_info(3)->uservisible);
-
 
480
 
464
        $this->assertEquals(array('assign', 'forum', 'page'), $names);
481
        $this->assertFalse($modinfo->cms[$assign0->cmid]->is_stealth());
465
        // Admin can see hidden modules/sections.
482
        $this->assertFalse($modinfo->cms[$assign0->cmid]->is_stealth());
-
 
483
        $this->assertTrue($modinfo->cms[$page0->cmid]->is_stealth());
466
        $this->assertTrue($modinfo->cms[$assign0->cmid]->uservisible);
484
        $this->assertTrue($modinfo->cms[$page3->cmid]->is_stealth());
467
        $this->assertTrue($modinfo->get_section_info(3)->uservisible);
485
 
-
 
486
        // Get modinfo for user with student role (without capability to view hidden activities/sections).
-
 
487
        $student = $this->getDataGenerator()->create_user();
468
 
488
        $this->getDataGenerator()->enrol_user($student->id, $course->id, 'student');
-
 
489
        $studentmodinfo = get_fast_modinfo($course->id, $student->id);
-
 
490
        $this->assertEquals($student->id, $studentmodinfo->userid);
-
 
491
        $this->assertTrue($studentmodinfo->cms[$forum0->cmid]->uservisible);
-
 
492
        $this->assertTrue($studentmodinfo->cms[$forum0->cmid]->is_visible_on_course_page());
-
 
493
        $this->assertFalse($studentmodinfo->cms[$assign0->cmid]->uservisible);
-
 
494
        $this->assertFalse($studentmodinfo->cms[$assign0->cmid]->is_visible_on_course_page());
-
 
495
        $this->assertTrue($studentmodinfo->cms[$page0->cmid]->uservisible);
-
 
496
        $this->assertFalse($studentmodinfo->cms[$page0->cmid]->is_visible_on_course_page());
-
 
497
        $this->assertFalse($studentmodinfo->get_section_info(3)->uservisible);
-
 
498
        $this->assertTrue($studentmodinfo->cms[$page3->cmid]->uservisible);
-
 
499
        $this->assertTrue($studentmodinfo->cms[$page3->cmid]->is_visible_on_course_page());
-
 
500
 
-
 
501
        // Get modinfo for user with teacher role (with capability to view hidden activities but not sections).
-
 
502
        $teacher = $this->getDataGenerator()->create_user();
-
 
503
        $this->getDataGenerator()->enrol_user($teacher->id, $course->id, 'teacher');
-
 
504
        $teachermodinfo = get_fast_modinfo($course->id, $teacher->id);
-
 
505
        $this->assertEquals($teacher->id, $teachermodinfo->userid);
-
 
506
        $this->assertTrue($teachermodinfo->cms[$forum0->cmid]->uservisible);
469
        // Get modinfo for non-current user (without capability to view hidden activities/sections).
507
        $this->assertTrue($teachermodinfo->cms[$forum0->cmid]->is_visible_on_course_page());
-
 
508
        $this->assertTrue($teachermodinfo->cms[$assign0->cmid]->uservisible);
-
 
509
        $this->assertTrue($teachermodinfo->cms[$assign0->cmid]->is_visible_on_course_page());
-
 
510
        $this->assertTrue($teachermodinfo->cms[$page0->cmid]->uservisible);
-
 
511
        $this->assertTrue($teachermodinfo->cms[$page0->cmid]->is_visible_on_course_page());
-
 
512
        $this->assertFalse($teachermodinfo->get_section_info(3)->uservisible);
-
 
513
        $this->assertTrue($teachermodinfo->cms[$page3->cmid]->uservisible);
-
 
514
        $this->assertTrue($teachermodinfo->cms[$page3->cmid]->is_visible_on_course_page());
-
 
515
 
-
 
516
        // Get modinfo for user with editingteacher role (with capability to view hidden activities/sections).
-
 
517
        $editingteacher = $this->getDataGenerator()->create_user();
-
 
518
        $this->getDataGenerator()->enrol_user($editingteacher->id, $course->id, 'editingteacher');
-
 
519
        $editingteachermodinfo = get_fast_modinfo($course->id, $editingteacher->id);
-
 
520
        $this->assertEquals($editingteacher->id, $editingteachermodinfo->userid);
-
 
521
        $this->assertTrue($editingteachermodinfo->cms[$forum0->cmid]->uservisible);
-
 
522
        $this->assertTrue($editingteachermodinfo->cms[$forum0->cmid]->is_visible_on_course_page());
-
 
523
        $this->assertTrue($editingteachermodinfo->cms[$assign0->cmid]->uservisible);
-
 
524
        $this->assertTrue($editingteachermodinfo->cms[$assign0->cmid]->is_visible_on_course_page());
Línea 470... Línea 525...
470
        $user = $this->getDataGenerator()->create_user();
525
        $this->assertTrue($editingteachermodinfo->cms[$page0->cmid]->uservisible);
471
        $modinfo = get_fast_modinfo($course->id, $user->id);
526
        $this->assertTrue($editingteachermodinfo->cms[$page0->cmid]->is_visible_on_course_page());
472
        $this->assertEquals($user->id, $modinfo->userid);
527
        $this->assertTrue($editingteachermodinfo->get_section_info(3)->uservisible);
473
        $this->assertFalse($modinfo->cms[$assign0->cmid]->uservisible);
528
        $this->assertTrue($editingteachermodinfo->cms[$page3->cmid]->uservisible);
Línea 1052... Línea 1107...
1052
 
1107
 
1053
        $listed = get_fast_modinfo($course)->get_section_info_all();
1108
        $listed = get_fast_modinfo($course)->get_section_info_all();
Línea 1054... Línea 1109...
1054
        $this->assertCount(4, $listed);
1109
        $this->assertCount(4, $listed);
1055
 
1110
 
1056
        // Generate some delegated sections (not listed).
1111
        // Generate some delegated sections (not listed).
Línea 1057... Línea 1112...
1057
        formatactions::section($course)->create_delegated('mod_label', 0);
1112
        formatactions::section($course)->create_delegated('test_component', 0);
Línea 1058... Línea 1113...
1058
        formatactions::section($course)->create_delegated('mod_label', 1);
1113
        formatactions::section($course)->create_delegated('test_component', 1);
Línea 1119... Línea 1174...
1119
    /**
1174
    /**
1120
     * Data provider for test_get_section_info_by_id().
1175
     * Data provider for test_get_section_info_by_id().
1121
     *
1176
     *
1122
     * @return array
1177
     * @return array
1123
     */
1178
     */
1124
    public function get_section_info_by_id_provider() {
1179
    public static function get_section_info_by_id_provider(): array {
1125
        return [
1180
        return [
1126
            'Valid section id' => [
1181
            'Valid section id' => [
1127
                'sectionnum' => 1,
1182
                'sectionnum' => 1,
1128
                'strictness' => IGNORE_MISSING,
1183
                'strictness' => IGNORE_MISSING,
1129
                'expectnull' => false,
1184
                'expectnull' => false,
Línea 1730... Línea 1785...
1730
 
1785
 
1731
        get_fast_modinfo($coursethree->id);
1786
        get_fast_modinfo($coursethree->id);
1732
        $cacherevthree = $DB->get_field('course', 'cacherev', ['id' => $coursethree->id]);
1787
        $cacherevthree = $DB->get_field('course', 'cacherev', ['id' => $coursethree->id]);
1733
        $this->assertGreaterThan($prevcacherevthree, $cacherevthree);
1788
        $this->assertGreaterThan($prevcacherevthree, $cacherevthree);
-
 
1789
    }
-
 
1790
 
-
 
1791
    /**
-
 
1792
     * Test get_sections_delegated_by_cm method
-
 
1793
     *
-
 
1794
     * @covers \course_modinfo::get_sections_delegated_by_cm
-
 
1795
     */
-
 
1796
    public function test_get_sections_delegated_by_cm(): void {
-
 
1797
        $this->resetAfterTest();
-
 
1798
 
-
 
1799
        $course = $this->getDataGenerator()->create_course(['numsections' => 1]);
-
 
1800
 
-
 
1801
        $modinfo = get_fast_modinfo($course);
-
 
1802
        $delegatedsections = $modinfo->get_sections_delegated_by_cm();
-
 
1803
        $this->assertEmpty($delegatedsections);
-
 
1804
 
-
 
1805
        // Add a section delegated by a course module.
-
 
1806
        $subsection = $this->getDataGenerator()->create_module('subsection', ['course' => $course]);
-
 
1807
        $modinfo = get_fast_modinfo($course);
-
 
1808
        $delegatedsections = $modinfo->get_sections_delegated_by_cm();
-
 
1809
        $this->assertCount(1, $delegatedsections);
-
 
1810
        $this->assertArrayHasKey($subsection->cmid, $delegatedsections);
-
 
1811
 
-
 
1812
        // Add a section delegated by a block.
-
 
1813
        formatactions::section($course)->create_delegated('block_site_main_menu', 1);
-
 
1814
        $modinfo = get_fast_modinfo($course);
-
 
1815
        $delegatedsections = $modinfo->get_sections_delegated_by_cm();
-
 
1816
        // Sections delegated by a block shouldn't be returned.
-
 
1817
        $this->assertCount(1, $delegatedsections);
-
 
1818
    }
-
 
1819
 
-
 
1820
    /**
-
 
1821
     * Test get_sections_delegated_by_cm method
-
 
1822
     *
-
 
1823
     * @covers \cm_info::get_delegated_section_info
-
 
1824
     */
-
 
1825
    public function test_get_delegated_section_info(): void {
-
 
1826
        $this->resetAfterTest();
-
 
1827
 
-
 
1828
        $course = $this->getDataGenerator()->create_course(['numsections' => 1]);
-
 
1829
 
-
 
1830
        // Add a section delegated by a course module.
-
 
1831
        $subsection = $this->getDataGenerator()->create_module('subsection', ['course' => $course]);
-
 
1832
        $otheractivity = $this->getDataGenerator()->create_module('page', ['course' => $course]);
-
 
1833
 
-
 
1834
        $modinfo = get_fast_modinfo($course);
-
 
1835
        $delegatedsections = $modinfo->get_sections_delegated_by_cm();
-
 
1836
 
-
 
1837
        $delegated = $modinfo->get_cm($subsection->cmid)->get_delegated_section_info();
-
 
1838
        $this->assertNotNull($delegated);
-
 
1839
        $this->assertEquals($delegated, $delegatedsections[$subsection->cmid]);
-
 
1840
 
-
 
1841
        $delegated = $modinfo->get_cm($otheractivity->cmid)->get_delegated_section_info();
-
 
1842
        $this->assertNull($delegated);
-
 
1843
    }
-
 
1844
 
-
 
1845
    /**
-
 
1846
     * Test get_uservisible method when the section is delegated.
-
 
1847
     *
-
 
1848
     * @covers \section_info::get_uservisible
-
 
1849
     * @dataProvider data_provider_get_uservisible_delegate
-
 
1850
     * @param string $role The role to assign to the user.
-
 
1851
     * @param bool $parentvisible The visibility of the parent section.
-
 
1852
     * @param bool $delegatedvisible The visibility of the delegated section.
-
 
1853
     * @param bool $expected The expected visibility of the delegated section.
-
 
1854
     * @return void
-
 
1855
     */
-
 
1856
    public function test_get_uservisible_delegate(
-
 
1857
        string $role,
-
 
1858
        bool $parentvisible,
-
 
1859
        bool $delegatedvisible,
-
 
1860
        bool $expected,
-
 
1861
    ): void {
-
 
1862
        $this->resetAfterTest();
-
 
1863
 
-
 
1864
        $course = $this->getDataGenerator()->create_course(['numsections' => 1]);
-
 
1865
        $subsection = $this->getDataGenerator()->create_module('subsection', ['course' => $course], ['section' => 1]);
-
 
1866
 
-
 
1867
        $student = $this->getDataGenerator()->create_and_enrol($course, $role);
-
 
1868
 
-
 
1869
        $modinfo = get_fast_modinfo($course);
-
 
1870
 
-
 
1871
        formatactions::section($course)->update(
-
 
1872
            $modinfo->get_section_info(1),
-
 
1873
            ['visible' => $parentvisible]
-
 
1874
        );
-
 
1875
 
-
 
1876
        formatactions::cm($course)->set_visibility(
-
 
1877
            $subsection->cmid,
-
 
1878
            $delegatedvisible,
-
 
1879
        );
-
 
1880
 
-
 
1881
        $this->setUser($student);
-
 
1882
        $modinfo = get_fast_modinfo($course);
-
 
1883
 
-
 
1884
        $delegatedsection = $modinfo->get_cm($subsection->cmid)->get_delegated_section_info();
-
 
1885
 
-
 
1886
        // The get_uservisible is a magic getter.
-
 
1887
        $this->assertEquals($expected, $delegatedsection->uservisible);
-
 
1888
    }
-
 
1889
 
-
 
1890
    /**
-
 
1891
     * Data provider for test_get_uservisible_delegate.
-
 
1892
     *
-
 
1893
     * @return array
-
 
1894
     */
-
 
1895
    public static function data_provider_get_uservisible_delegate(): array {
-
 
1896
        return [
-
 
1897
            'Student on a visible subsection inside a visible parent' => [
-
 
1898
                'role' => 'student',
-
 
1899
                'parentvisible' => true,
-
 
1900
                'delegatedvisible' => true,
-
 
1901
                'expected' => true,
-
 
1902
            ],
-
 
1903
            'Student on a hidden subsection inside a visible parent' => [
-
 
1904
                'role' => 'student',
-
 
1905
                'parentvisible' => true,
-
 
1906
                'delegatedvisible' => false,
-
 
1907
                'expected' => false,
-
 
1908
            ],
-
 
1909
            'Student on a visible subsection inside a hidden parent' => [
-
 
1910
                'role' => 'student',
-
 
1911
                'parentvisible' => false,
-
 
1912
                'delegatedvisible' => true,
-
 
1913
                'expected' => false,
-
 
1914
            ],
-
 
1915
            'Student on a hidden subsection inside a hidden parent' => [
-
 
1916
                'role' => 'student',
-
 
1917
                'parentvisible' => false,
-
 
1918
                'delegatedvisible' => false,
-
 
1919
                'expected' => false,
-
 
1920
            ],
-
 
1921
            'Teacher on a visible subsection inside a visible parent' => [
-
 
1922
                'role' => 'editingteacher',
-
 
1923
                'parentvisible' => true,
-
 
1924
                'delegatedvisible' => true,
-
 
1925
                'expected' => true,
-
 
1926
            ],
-
 
1927
            'Teacher on a hidden subsection inside a visible parent' => [
-
 
1928
                'role' => 'editingteacher',
-
 
1929
                'parentvisible' => true,
-
 
1930
                'delegatedvisible' => false,
-
 
1931
                'expected' => true,
-
 
1932
            ],
-
 
1933
            'Teacher on a visible subsection inside a hidden parent' => [
-
 
1934
                'role' => 'editingteacher',
-
 
1935
                'parentvisible' => false,
-
 
1936
                'delegatedvisible' => true,
-
 
1937
                'expected' => true,
-
 
1938
            ],
-
 
1939
            'Teacher on a hidden subsection inside a hidden parent' => [
-
 
1940
                'role' => 'editingteacher',
-
 
1941
                'parentvisible' => false,
-
 
1942
                'delegatedvisible' => false,
-
 
1943
                'expected' => true,
-
 
1944
            ],
-
 
1945
        ];
-
 
1946
    }
-
 
1947
 
-
 
1948
    /**
-
 
1949
     * Test get_uservisible method when the section is delegated and depending on if the plugin is enabled.
-
 
1950
     *
-
 
1951
     * @covers \section_info::get_uservisible
-
 
1952
     * @dataProvider provider_test_get_uservisible_delegate_enabled
-
 
1953
     * @param string $role The role to assign to the user.
-
 
1954
     * @param bool $enabled Whether the plugin is enabled.
-
 
1955
     * @param bool $expected The expected visibility of the delegated section.
-
 
1956
     * @return void
-
 
1957
     */
-
 
1958
    public function test_get_uservisible_delegate_enabled(
-
 
1959
        string $role,
-
 
1960
        bool $enabled,
-
 
1961
        bool $expected,
-
 
1962
    ): void {
-
 
1963
        $this->resetAfterTest();
-
 
1964
 
-
 
1965
        $course = $this->getDataGenerator()->create_course(['numsections' => 1]);
-
 
1966
        $subsection = $this->getDataGenerator()->create_module('subsection', ['course' => $course], ['section' => 1]);
-
 
1967
 
-
 
1968
        $modinfo = get_fast_modinfo($course);
-
 
1969
        $delegatedsection = $modinfo->get_cm($subsection->cmid)->get_delegated_section_info();
-
 
1970
 
-
 
1971
        $user = $this->getDataGenerator()->create_and_enrol($course, $role);
-
 
1972
 
-
 
1973
        if (!$enabled) {
-
 
1974
            $manager = \core_plugin_manager::resolve_plugininfo_class('mod');
-
 
1975
            $manager::enable_plugin('subsection', 0);
-
 
1976
            rebuild_course_cache($course->id, true);
-
 
1977
        }
-
 
1978
 
-
 
1979
        $this->setUser($user);
-
 
1980
        $modinfo = get_fast_modinfo($course);
-
 
1981
 
-
 
1982
        $delegatedsection = $modinfo->get_section_info($delegatedsection->section);
-
 
1983
 
-
 
1984
        // The get_uservisible is a magic getter.
-
 
1985
        $this->assertEquals($expected, $delegatedsection->uservisible);
-
 
1986
    }
-
 
1987
 
-
 
1988
    /**
-
 
1989
     * Data provider for test_get_uservisible_delegate_enabled.
-
 
1990
     *
-
 
1991
     * @return array
-
 
1992
     */
-
 
1993
    public static function provider_test_get_uservisible_delegate_enabled(): array {
-
 
1994
        return [
-
 
1995
            'Student with plugin enabled' => [
-
 
1996
                'role' => 'student',
-
 
1997
                'enabled' => true,
-
 
1998
                'expected' => true,
-
 
1999
            ],
-
 
2000
            'Student with plugin disabled' => [
-
 
2001
                'role' => 'student',
-
 
2002
                'enabled' => false,
-
 
2003
                'expected' => false,
-
 
2004
            ],
-
 
2005
            'Teacher with plugin enabled' => [
-
 
2006
                'role' => 'editingteacher',
-
 
2007
                'enabled' => true,
-
 
2008
                'expected' => true,
-
 
2009
            ],
-
 
2010
            'Teacher with plugin disabled' => [
-
 
2011
                'role' => 'editingteacher',
-
 
2012
                'enabled' => false,
-
 
2013
                'expected' => true,
-
 
2014
            ],
-
 
2015
        ];
-
 
2016
    }
-
 
2017
 
-
 
2018
    /**
-
 
2019
     * Test get_available method when the section is delegated.
-
 
2020
     *
-
 
2021
     * @covers \section_info::get_available
-
 
2022
     * @covers \section_info::get_uservisible
-
 
2023
     * @dataProvider data_provider_get_available_delegated
-
 
2024
     * @param string $role The role to assign to the user.
-
 
2025
     * @param bool $parentavailable The parent section is available.
-
 
2026
     * @param bool $delegatedavailable The delegated section is available..
-
 
2027
     * @param bool $expectedavailable The expected availability of the delegated section.
-
 
2028
     * @param bool $expecteduservisible The expected uservisibility of the delegated section.
-
 
2029
     * @return void
-
 
2030
     */
-
 
2031
    public function test_get_available_delegated(
-
 
2032
        string $role,
-
 
2033
        bool $parentavailable,
-
 
2034
        bool $delegatedavailable,
-
 
2035
        bool $expectedavailable,
-
 
2036
        bool $expecteduservisible,
-
 
2037
    ): void {
-
 
2038
        $this->resetAfterTest();
-
 
2039
 
-
 
2040
        // The element will be available tomorrow.
-
 
2041
        $availability = json_encode(
-
 
2042
            (object) [
-
 
2043
                'op' => '&',
-
 
2044
                'showc' => [true],
-
 
2045
                'c' => [
-
 
2046
                    [
-
 
2047
                        'type' => 'date',
-
 
2048
                        'd' => '>=',
-
 
2049
                        't' => time() + DAYSECS,
-
 
2050
                    ],
-
 
2051
                ],
-
 
2052
            ]
-
 
2053
        );
-
 
2054
 
-
 
2055
        $course = $this->getDataGenerator()->create_course(['numsections' => 1]);
-
 
2056
 
-
 
2057
        $cmparams = ['section' => 1];
-
 
2058
        if (!$delegatedavailable) {
-
 
2059
            $cmparams['availability'] = $availability;
-
 
2060
        }
-
 
2061
 
-
 
2062
        $subsection = $this->getDataGenerator()->create_module(
-
 
2063
            'subsection',
-
 
2064
            ['course' => $course],
-
 
2065
            $cmparams
-
 
2066
        );
-
 
2067
 
-
 
2068
        $student = $this->getDataGenerator()->create_and_enrol($course, $role);
-
 
2069
 
-
 
2070
        $modinfo = get_fast_modinfo($course);
-
 
2071
 
-
 
2072
        if (!$parentavailable) {
-
 
2073
            formatactions::section($course)->update(
-
 
2074
                $modinfo->get_section_info(1),
-
 
2075
                ['availability' => $availability]
-
 
2076
            );
-
 
2077
        }
-
 
2078
 
-
 
2079
        $this->setUser($student);
-
 
2080
        $modinfo = get_fast_modinfo($course);
-
 
2081
 
-
 
2082
        $delegatedsection = $modinfo->get_cm($subsection->cmid)->get_delegated_section_info();
-
 
2083
 
-
 
2084
        // All section_info getters are magic methods.
-
 
2085
        $this->assertEquals($expectedavailable, $delegatedsection->available);
-
 
2086
        $this->assertEquals($expecteduservisible, $delegatedsection->uservisible);
-
 
2087
    }
-
 
2088
 
-
 
2089
    /**
-
 
2090
     * Data provider for test_get_available_delegated.
-
 
2091
     *
-
 
2092
     * @return array
-
 
2093
     */
-
 
2094
    public static function data_provider_get_available_delegated(): array {
-
 
2095
        return [
-
 
2096
            'Student on an available subsection inside an available parent' => [
-
 
2097
                'role' => 'student',
-
 
2098
                'parentavailable' => true,
-
 
2099
                'delegatedavailable' => true,
-
 
2100
                'expectedavailable' => true,
-
 
2101
                'expecteduservisible' => true,
-
 
2102
            ],
-
 
2103
            'Student on an unavailable subsection inside an available parent' => [
-
 
2104
                'role' => 'student',
-
 
2105
                'parentavailable' => true,
-
 
2106
                'delegatedavailable' => false,
-
 
2107
                'expectedavailable' => false,
-
 
2108
                'expecteduservisible' => false,
-
 
2109
            ],
-
 
2110
            'Student on an available subsection inside an unavailable parent' => [
-
 
2111
                'role' => 'student',
-
 
2112
                'parentavailable' => false,
-
 
2113
                'delegatedavailable' => true,
-
 
2114
                'expectedavailable' => false,
-
 
2115
                'expecteduservisible' => false,
-
 
2116
            ],
-
 
2117
            'Student on an unavailable subsection inside an unavailable parent' => [
-
 
2118
                'role' => 'student',
-
 
2119
                'parentavailable' => false,
-
 
2120
                'delegatedavailable' => false,
-
 
2121
                'expectedavailable' => false,
-
 
2122
                'expecteduservisible' => false,
-
 
2123
            ],
-
 
2124
            'Teacher on an available subsection inside an available parent' => [
-
 
2125
                'role' => 'editingteacher',
-
 
2126
                'parentavailable' => true,
-
 
2127
                'delegatedavailable' => true,
-
 
2128
                'expectedavailable' => true,
-
 
2129
                'expecteduservisible' => true,
-
 
2130
            ],
-
 
2131
            'Teacher on an unavailable subsection inside an available parent' => [
-
 
2132
                'role' => 'editingteacher',
-
 
2133
                'parentavailable' => true,
-
 
2134
                'delegatedavailable' => false,
-
 
2135
                'expectedavailable' => false,
-
 
2136
                'expecteduservisible' => true,
-
 
2137
            ],
-
 
2138
            'Teacher on an available subsection inside an unavailable parent' => [
-
 
2139
                'role' => 'editingteacher',
-
 
2140
                'parentavailable' => false,
-
 
2141
                'delegatedavailable' => true,
-
 
2142
                'expectedavailable' => false,
-
 
2143
                'expecteduservisible' => true,
-
 
2144
            ],
-
 
2145
            'Teacher on an unavailable subsection inside an unavailable parent' => [
-
 
2146
                'role' => 'editingteacher',
-
 
2147
                'parentavailable' => false,
-
 
2148
                'delegatedavailable' => false,
-
 
2149
                'expectedavailable' => false,
-
 
2150
                'expecteduservisible' => true,
-
 
2151
            ],
-
 
2152
        ];
-
 
2153
    }
-
 
2154
 
-
 
2155
    /**
-
 
2156
     * Test when a section is considered orphan.
-
 
2157
     *
-
 
2158
     * @covers \section_info::is_orphan
-
 
2159
     * @return void
-
 
2160
     */
-
 
2161
    public function test_is_orphan(): void {
-
 
2162
 
-
 
2163
        $this->resetAfterTest();
-
 
2164
 
-
 
2165
        $course = $this->getDataGenerator()->create_course(['numsections' => 1]);
-
 
2166
        $subsection = $this->getDataGenerator()->create_module('subsection', ['course' => $course], ['section' => 1]);
-
 
2167
 
-
 
2168
        $modinfo = get_fast_modinfo($course);
-
 
2169
        $delegatedsection = $modinfo->get_cm($subsection->cmid)->get_delegated_section_info();
-
 
2170
 
-
 
2171
        // If mod_subsection is enabled, a subsection is not orphan.
-
 
2172
        $modinfo = get_fast_modinfo($course);
-
 
2173
        $this->assertFalse($delegatedsection->is_orphan());
-
 
2174
 
-
 
2175
        // Delegated sections without a component instance (disabled mod_subsection) is considered orphan.
-
 
2176
        $manager = \core_plugin_manager::resolve_plugininfo_class('mod');
-
 
2177
        $manager::enable_plugin('subsection', 0);
-
 
2178
        rebuild_course_cache($course->id, true);
-
 
2179
 
-
 
2180
        $modinfo = get_fast_modinfo($course);
-
 
2181
        $delegatedsection = $modinfo->get_section_info($delegatedsection->section);
-
 
2182
        $this->assertTrue($delegatedsection->is_orphan());
-
 
2183
 
-
 
2184
        // Check enabling the plugin restore the previous state.
-
 
2185
        $manager::enable_plugin('subsection', 1);
-
 
2186
        rebuild_course_cache($course->id, true);
-
 
2187
 
-
 
2188
        $modinfo = get_fast_modinfo($course);
-
 
2189
        $delegatedsection = $modinfo->get_section_info($delegatedsection->section);
-
 
2190
        $this->assertFalse($delegatedsection->is_orphan());
-
 
2191
 
-
 
2192
        // Force section limit in the course format instance.
-
 
2193
        rebuild_course_cache($course->id, true);
-
 
2194
        $modinfo = get_fast_modinfo($course);
-
 
2195
 
-
 
2196
        // Core formats does not use numsections anymore. We need to use reflection to change the value.
-
 
2197
        $format = course_get_format($course);
-
 
2198
        // Add a fake numsections format data (Force loading format data first).
-
 
2199
        $format->get_course();
-
 
2200
        $reflection = new \ReflectionObject($format);
-
 
2201
        $property = $reflection->getProperty('course');
-
 
2202
        $courseobject = $property->getValue($format);
-
 
2203
        $courseobject->numsections = 1;
-
 
2204
        $property->setValue($format, $courseobject);
-
 
2205
 
-
 
2206
        $delegatedsection = $modinfo->get_section_info($delegatedsection->section);
-
 
2207
        $this->assertTrue($delegatedsection->is_orphan());
-
 
2208
    }
-
 
2209
 
-
 
2210
    /**
-
 
2211
     * Test for section_info::get_sequence_cm_infos
-
 
2212
     *
-
 
2213
     * @covers \section_info::get_sequence_cm_infos
-
 
2214
     * @return void
-
 
2215
     */
-
 
2216
    public function test_section_get_sequence_cm_infos(): void {
-
 
2217
        $this->resetAfterTest();
-
 
2218
 
-
 
2219
        $course = $this->getDataGenerator()->create_course(['numsections' => 2]);
-
 
2220
        $cm1 = $this->getDataGenerator()->create_module('page', ['course' => $course], ['section' => 0]);
-
 
2221
        $cm2 = $this->getDataGenerator()->create_module('page', ['course' => $course], ['section' => 1]);
-
 
2222
        $cm3 = $this->getDataGenerator()->create_module('page', ['course' => $course], ['section' => 1]);
-
 
2223
        $cm4 = $this->getDataGenerator()->create_module('page', ['course' => $course], ['section' => 1]);
-
 
2224
 
-
 
2225
        $modinfo = get_fast_modinfo($course->id);
-
 
2226
 
-
 
2227
        $sectioninfo = $modinfo->get_section_info(0);
-
 
2228
        $cms = $sectioninfo->get_sequence_cm_infos();
-
 
2229
        $this->assertCount(1, $cms);
-
 
2230
        $this->assertEquals($cm1->cmid, $cms[0]->id);
-
 
2231
 
-
 
2232
        $sectioninfo = $modinfo->get_section_info(1);
-
 
2233
        $cms = $sectioninfo->get_sequence_cm_infos();
-
 
2234
        $this->assertCount(3, $cms);
-
 
2235
        $this->assertEquals($cm2->cmid, $cms[0]->id);
-
 
2236
        $this->assertEquals($cm3->cmid, $cms[1]->id);
-
 
2237
        $this->assertEquals($cm4->cmid, $cms[2]->id);
-
 
2238
 
-
 
2239
        $sectioninfo = $modinfo->get_section_info(2);
-
 
2240
        $cms = $sectioninfo->get_sequence_cm_infos();
-
 
2241
        $this->assertCount(0, $cms);
-
 
2242
    }
-
 
2243
 
-
 
2244
    /**
-
 
2245
     * Test for cm_info::get_instance_record
-
 
2246
     *
-
 
2247
     * @covers \cm_info::get_instance_record
-
 
2248
     * @return void
-
 
2249
     */
-
 
2250
    public function test_section_get_instance_record(): void {
-
 
2251
        global $DB;
-
 
2252
 
-
 
2253
        $this->resetAfterTest();
-
 
2254
 
-
 
2255
        $course = $this->getDataGenerator()->create_course(['numsections' => 2]);
-
 
2256
        $activity = $this->getDataGenerator()->create_module('page', ['course' => $course], ['section' => 0]);
-
 
2257
 
-
 
2258
        $modinfo = get_fast_modinfo($course->id);
-
 
2259
        $cminfo = $modinfo->get_cm($activity->cmid);
-
 
2260
 
-
 
2261
        $instancerecord = $DB->get_record('page', ['id' => $activity->id]);
-
 
2262
 
-
 
2263
        $instance = $cminfo->get_instance_record();
-
 
2264
        $this->assertEquals($instancerecord, $instance);
-
 
2265
 
-
 
2266
        // The instance record should be cached.
-
 
2267
        $DB->delete_records('page', ['id' => $activity->id]);
-
 
2268
 
-
 
2269
        $instance2 = $cminfo->get_instance_record();
-
 
2270
        $this->assertEquals($instancerecord, $instance);
-
 
2271
        $this->assertEquals($instance, $instance2);
-
 
2272
    }
-
 
2273
 
-
 
2274
    /**
-
 
2275
     * Test for sort_cm_array method.
-
 
2276
     */
-
 
2277
    public function test_sort_cm_array(): void {
-
 
2278
        $this->resetAfterTest();
-
 
2279
        $generator = $this->getDataGenerator();
-
 
2280
        // Create a course with 4 sections.
-
 
2281
        $course = $generator->create_course(['numsections' => 3]);
-
 
2282
        $generator->create_module('page', ['name' => 'Page s1', 'course' => $course->id, 'section' => 0]);
-
 
2283
        $generator->create_module('page', ['name' => 'Page s2', 'course' => $course->id, 'section' => 1]);
-
 
2284
        $generator->create_module('assign', ['name' => 'Assign s3', 'course' => $course->id, 'section' => 2]);
-
 
2285
        $generator->create_module('page', ['name' => 'Page s3', 'course' => $course->id, 'section' => 3]);
-
 
2286
        // Check we return all cms in order.
-
 
2287
        $cms = get_fast_modinfo($course)->get_instances_of('page');
-
 
2288
        get_fast_modinfo($course)->sort_cm_array($cms);
-
 
2289
        $this->assertCount(3, $cms);
-
 
2290
        $this->assertEquals(['Page s1', 'Page s2', 'Page s3'], array_column($cms, 'name'));
-
 
2291
 
-
 
2292
        // Generate some delegated sections (not listed).
-
 
2293
        $module = $this->getDataGenerator()->create_module('subsection', (object) ['course' => $course->id, 'section' => 1]);
-
 
2294
        $sub1 = get_fast_modinfo($course)->get_section_info_by_component('mod_subsection', $module->id);
-
 
2295
        $generator->create_module('page', ['name' => 'Page sub1', 'course' => $course->id, 'section' => $sub1->sectionnum]);
-
 
2296
        $generator->create_module('page', ['name' => 'Page sub2', 'course' => $course->id, 'section' => $sub1->sectionnum]);
-
 
2297
        $generator->create_module('assign', ['name' => 'Assign sub1', 'course' => $course->id, 'section' => $sub1->sectionnum]);
-
 
2298
 
-
 
2299
        $cms = get_fast_modinfo($course)->get_instances_of('page');
-
 
2300
        get_fast_modinfo($course)->sort_cm_array($cms);
-
 
2301
        $this->assertCount(5, $cms);
-
 
2302
        $this->assertEquals(['Page s1', 'Page s2', 'Page sub1', 'Page sub2', 'Page s3'], array_column($cms, 'name'));
-
 
2303
 
-
 
2304
        $cms = get_fast_modinfo($course)->get_instances_of('assign');
-
 
2305
        get_fast_modinfo($course)->sort_cm_array($cms);
-
 
2306
        $this->assertCount(2, $cms);
-
 
2307
        $this->assertEquals(['Assign sub1', 'Assign s3'], array_column($cms, 'name'));
-
 
2308
    }
-
 
2309
 
-
 
2310
    /**
-
 
2311
     * Test for get_instance_of method.
-
 
2312
     */
-
 
2313
    public function test_get_instance_of(): void {
-
 
2314
        $this->resetAfterTest();
-
 
2315
        $generator = $this->getDataGenerator();
-
 
2316
        // Create a course with 4 sections.
-
 
2317
        $course = $generator->create_course(['numsections' => 3]);
-
 
2318
        $generator->create_module('page', ['name' => 'Page s1', 'course' => $course->id, 'section' => 0]);
-
 
2319
        $generator->create_module('page', ['name' => 'Page s2', 'course' => $course->id, 'section' => 1]);
-
 
2320
        $generator->create_module('assign', ['name' => 'Assign s3', 'course' => $course->id, 'section' => 2]);
-
 
2321
 
-
 
2322
        $modinfo = get_fast_modinfo($course);
-
 
2323
        $pagecms = array_values($modinfo->get_instances_of('page'));
-
 
2324
        $assigncms = array_values($modinfo->get_instances_of('assign'));
-
 
2325
        $this->assertCount(2, $pagecms);
-
 
2326
        $this->assertCount(1, $assigncms);
-
 
2327
 
-
 
2328
        $this->assertEquals('Page s1', $modinfo->get_instance_of('page', $pagecms[0]->instance)->name);
-
 
2329
        $this->assertEquals('Page s2', $modinfo->get_instance_of('page', $pagecms[1]->instance)->name);
-
 
2330
        $this->assertEquals('Assign s3', $modinfo->get_instance_of('assign', $assigncms[0]->instance)->name);
-
 
2331
 
-
 
2332
        $this->assertNull($modinfo->get_instance_of('page', 99999));
-
 
2333
        $this->assertNull($modinfo->get_instance_of('assign', 99999));
-
 
2334
        $this->assertNull($modinfo->get_instance_of('nonexisting', 99999));
-
 
2335
    }
-
 
2336
 
-
 
2337
    /**
-
 
2338
     * Test for get_instance_of method when asking for a non existing module with MUST_EXIST.
-
 
2339
     */
-
 
2340
    public function test_get_instance_of_exception(): void {
-
 
2341
        $this->resetAfterTest();
-
 
2342
        $generator = $this->getDataGenerator();
-
 
2343
        // Create a course with 4 sections.
-
 
2344
        $course = $generator->create_course(['numsections' => 3]);
-
 
2345
        $generator->create_module('page', ['name' => 'Page s1', 'course' => $course->id, 'section' => 0]);
-
 
2346
        $generator->create_module('page', ['name' => 'Page s2', 'course' => $course->id, 'section' => 1]);
-
 
2347
        $generator->create_module('assign', ['name' => 'Assign s3', 'course' => $course->id, 'section' => 2]);
-
 
2348
 
-
 
2349
        $modinfo = get_fast_modinfo($course);
-
 
2350
 
-
 
2351
        $this->expectException(moodle_exception::class);
-
 
2352
        $this->expectExceptionMessage('Invalid module ID: 99999');
-
 
2353
        $modinfo->get_instance_of('page', 99999, MUST_EXIST);
1734
    }
2354
    }