Proyectos de Subversion Moodle

Rev

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

Rev 11 Rev 1441
Línea 21... Línea 21...
21
 * @copyright  2014 Marina Glancy
21
 * @copyright  2014 Marina Glancy
22
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 * @covers     \core_courseformat\base
23
 * @covers     \core_courseformat\base
24
 * @coversDefaultClass \core_courseformat\base
24
 * @coversDefaultClass \core_courseformat\base
25
 */
25
 */
26
class base_test extends advanced_testcase {
26
final class base_test extends advanced_testcase {
Línea 27... Línea 27...
27
 
27
 
28
    /**
28
    /**
29
     * Setup to ensure that fixtures are loaded.
29
     * Setup to ensure that fixtures are loaded.
30
     */
30
     */
Línea 38... Línea 38...
38
 
38
 
39
    /**
39
    /**
40
     * Tests the save and load functionality.
40
     * Tests the save and load functionality.
41
     *
41
     *
42
     * @author Jason den Dulk
-
 
43
     * @covers \core_courseformat
42
     * @author Jason den Dulk
44
     */
43
     */
45
    public function test_courseformat_saveandload(): void {
44
    public function test_courseformat_saveandload(): void {
Línea 46... Línea 45...
46
        $this->resetAfterTest();
45
        $this->resetAfterTest();
Línea 280... Línea 279...
280
    /**
279
    /**
281
     * Data provider for test_get_output_classname.
280
     * Data provider for test_get_output_classname.
282
     *
281
     *
283
     * @return array the testing scenarios
282
     * @return array the testing scenarios
284
     */
283
     */
285
    public function get_output_classname_provider(): array {
284
    public static function get_output_classname_provider(): array {
286
        return [
285
        return [
287
            'overridden class' => [
286
            'overridden class' => [
288
                'find' => 'state\\course',
287
                'find' => 'state\\course',
289
                'result' => 'format_theunittest\\output\\courseformat\\state\\course',
288
                'result' => 'format_theunittest\\output\\courseformat\\state\\course',
290
                'exception' => false,
289
                'exception' => false,
Línea 515... Línea 514...
515
    /**
514
    /**
516
     * Data provider for test_delete_format_data.
515
     * Data provider for test_delete_format_data.
517
     *
516
     *
518
     * @return array the testing scenarios
517
     * @return array the testing scenarios
519
     */
518
     */
520
    public function delete_format_data_provider(): array {
519
    public static function delete_format_data_provider(): array {
521
        return [
520
        return [
522
            'direct call' => [
521
            'direct call' => [
523
                'usehook' => false
522
                'usehook' => false
524
            ],
523
            ],
525
            'use hook' => [
524
            'use hook' => [
Línea 540... Línea 539...
540
 
539
 
541
        $generator = $this->getDataGenerator();
540
        $generator = $this->getDataGenerator();
542
        $course = $generator->create_course();
541
        $course = $generator->create_course();
Línea 543... Línea 542...
543
        $format = course_get_format($course);
542
        $format = course_get_format($course);
544
 
543
 
545
        $originalsection = $DB->get_record('course_sections', ['course' => $course->id, 'section' => 1], '*', MUST_EXIST);
544
        $originalsection = $DB->get_record('course_sections', ['course' => $course->id, 'section' => 0], '*', MUST_EXIST);
546
        $generator->create_module('page', ['course' => $course, 'section' => $originalsection->section]);
545
        $generator->create_module('page', ['course' => $course, 'section' => $originalsection->section]);
-
 
546
        $generator->create_module('page', ['course' => $course, 'section' => $originalsection->section]);
Línea 547... Línea 547...
547
        $generator->create_module('page', ['course' => $course, 'section' => $originalsection->section]);
547
        $generator->create_module('page', ['course' => $course, 'section' => $originalsection->section]);
548
        $generator->create_module('page', ['course' => $course, 'section' => $originalsection->section]);
548
        $generator->create_module('qbank', ['course' => $course, 'section' => $originalsection->section]);
Línea 549... Línea 549...
549
 
549
 
550
        $originalmodcount = $DB->count_records('course_modules', ['course' => $course->id, 'section' => $originalsection->id]);
550
        $originalmodcount = $DB->count_records('course_modules', ['course' => $course->id, 'section' => $originalsection->id]);
Línea 551... Línea 551...
551
        $this->assertEquals(3, $originalmodcount);
551
        $this->assertEquals(4, $originalmodcount);
Línea 562... Línea 562...
562
            }
562
            }
563
            $this->assertEquals($value, $newsection->$prop);
563
            $this->assertEquals($value, $newsection->$prop);
564
        }
564
        }
Línea 565... Línea 565...
565
 
565
 
-
 
566
        $newmodcount = $DB->count_records('course_modules', ['course' => $course->id, 'section' => $newsection->id]);
-
 
567
        $modinfo = course_modinfo::instance($course);
-
 
568
        $qbankinstances = $modinfo->get_instances_of('qbank');
566
        $newmodcount = $DB->count_records('course_modules', ['course' => $course->id, 'section' => $newsection->id]);
569
        $this->assertCount(1, $qbankinstances);
-
 
570
        $this->assertEquals($originalmodcount - 1, $newmodcount);
-
 
571
    }
-
 
572
 
-
 
573
    /**
-
 
574
     * Test duplicate_section() with delegated section
-
 
575
     * @covers     ::duplicate_section
-
 
576
     */
-
 
577
    public function test_duplicate_section_with_delegated_sections(): void {
-
 
578
        global $DB;
-
 
579
 
-
 
580
        $this->setAdminUser();
-
 
581
        $this->resetAfterTest();
-
 
582
        // Add subsection.
-
 
583
        $manager = \core_plugin_manager::resolve_plugininfo_class('mod');
-
 
584
        $manager::enable_plugin('subsection', 1);
-
 
585
        $course = $this->getDataGenerator()->create_course(['format' => 'topics', 'numsections' => 1]);
-
 
586
        $subsection1 = $this->getDataGenerator()->create_module(
-
 
587
            'subsection', ['course' => $course, 'section' => 1, 'name' => 'subsection1']);
-
 
588
        $subsection2 = $this->getDataGenerator()->create_module(
-
 
589
            'subsection', ['course' => $course, 'section' => 1, 'name' => 'subsection2']);
-
 
590
        $format = course_get_format($course);
-
 
591
 
-
 
592
        $modinfo = get_fast_modinfo($course);
-
 
593
        $sectioninfo = $modinfo->get_section_info(1, MUST_EXIST);
-
 
594
        $originalsectioncount = $DB->count_records('course_sections', ['course' => $course->id]);
-
 
595
        $this->assertEquals(4, $originalsectioncount);
-
 
596
 
-
 
597
        $originalsection = $DB->get_record('course_sections',
-
 
598
            ['course' => $course->id, 'section' => 0],
-
 
599
            '*',
-
 
600
            MUST_EXIST);
-
 
601
        $newsection = $format->duplicate_section($sectioninfo);
-
 
602
        foreach ($originalsection as $prop => $value) {
-
 
603
            if ($prop == 'id' || $prop == 'sequence' || $prop == 'section' || $prop == 'timemodified') {
-
 
604
                continue;
-
 
605
            }
-
 
606
            $this->assertEquals($value, $newsection->$prop);
-
 
607
        }
-
 
608
        $sectioncount = $DB->count_records('course_sections', ['course' => $course->id]);
567
        $this->assertEquals($originalmodcount, $newmodcount);
609
        $this->assertEquals(7, $sectioncount);
Línea 568... Línea 610...
568
    }
610
    }
569
 
611
 
570
    /**
612
    /**
Línea 603... Línea 645...
603
    /**
645
    /**
604
     * Data provider for test_get_format_string.
646
     * Data provider for test_get_format_string.
605
     *
647
     *
606
     * @return array the testing scenarios
648
     * @return array the testing scenarios
607
     */
649
     */
608
    public function get_format_string_provider(): array {
650
    public static function get_format_string_provider(): array {
609
        return [
651
        return [
610
            'Existing in format lang' => [
652
            'Existing in format lang' => [
611
                'key' => 'addsection',
653
                'key' => 'addsection',
612
                'data' => null,
654
                'data' => null,
613
                'expectedstring' => ['addsection', 'format_weeks', null],
655
                'expectedstring' => ['addsection', 'format_weeks', null],
Línea 682... Línea 724...
682
    /**
724
    /**
683
     * Data provider for test_move_section_after.
725
     * Data provider for test_move_section_after.
684
     *
726
     *
685
     * @return array the testing scenarios
727
     * @return array the testing scenarios
686
     */
728
     */
687
    public function move_section_after_provider(): array {
729
    public static function move_section_after_provider(): array {
688
        return [
730
        return [
689
            'Move top' => [
731
            'Move top' => [
690
                'movesection' => 'section3',
732
                'movesection' => 'section3',
691
                'destination' => 'section0',
733
                'destination' => 'section0',
692
                'order' => [
734
                'order' => [
Línea 785... Línea 827...
785
 
827
 
786
        if ($exception) {
828
        if ($exception) {
787
            $this->expectException(\coding_exception::class);
829
            $this->expectException(\coding_exception::class);
788
        }
830
        }
-
 
831
        $result = $format->get_non_ajax_cm_action_url($action, $cminfo);
-
 
832
        if (!$exception) {
-
 
833
            $this->assertDebuggingCalled();
789
        $result = $format->get_non_ajax_cm_action_url($action, $cminfo);
834
        }
790
        $this->assertEquals($assign0->cmid, $result->param($expectedparam));
835
        $this->assertEquals($assign0->cmid, $result->param('id'));
Línea 791... Línea 836...
791
    }
836
    }
792
 
837
 
793
    /**
838
    /**
794
     * Data provider for test_get_non_ajax_cm_action_url.
839
     * Data provider for test_get_non_ajax_cm_action_url.
795
     *
840
     *
796
     * @return array the testing scenarios
841
     * @return array the testing scenarios
797
     */
842
     */
798
    public function get_non_ajax_cm_action_url_provider(): array {
843
    public static function get_non_ajax_cm_action_url_provider(): array {
799
        return [
844
        return [
800
            'duplicate' => [
845
            'duplicate' => [
801
                'action' => 'cmDuplicate',
846
                'action' => 'cmDuplicate',
Línea 963... Línea 1008...
963
 
1008
 
964
        $course = $generator->create_course(['format' => 'testformatsections']);
1009
        $course = $generator->create_course(['format' => 'testformatsections']);
965
        $format = course_get_format($course);
1010
        $format = course_get_format($course);
966
        $this->assertTrue($format->can_sections_be_removed_from_navigation());
1011
        $this->assertTrue($format->can_sections_be_removed_from_navigation());
-
 
1012
    }
-
 
1013
 
-
 
1014
    public function test_is_section_visible(): void {
-
 
1015
        $this->resetAfterTest();
-
 
1016
 
-
 
1017
        $generator = $this->getDataGenerator();
-
 
1018
        $course = $generator->create_course(['format' => 'testformatsections'], ['hiddensections' => 1]);
-
 
1019
        course_create_sections_if_missing($course, [0, 1, 2]);
-
 
1020
 
-
 
1021
        // Students cannot view hidden sections.
-
 
1022
        $sectioninfo = get_fast_modinfo($course)->get_section_info(1);
-
 
1023
        \core_courseformat\formatactions::section($course)->update($sectioninfo, ['visible' => false]);
-
 
1024
 
-
 
1025
        $format = course_get_format($course);
-
 
1026
 
-
 
1027
        // Force max sections to 1 to detect section 2 as orphan.
-
 
1028
        $format->forcemaxsections = 1;
-
 
1029
 
-
 
1030
        $teacher = $this->getDataGenerator()->create_and_enrol($course, 'editingteacher');
-
 
1031
        $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
-
 
1032
 
-
 
1033
        $this->setUser($teacher);
-
 
1034
        $modinfoteacher = get_fast_modinfo($course, $teacher->id);
-
 
1035
        $this->assertTrue($format->is_section_visible($modinfoteacher->get_section_info(0)));
-
 
1036
        $this->assertTrue($format->is_section_visible($modinfoteacher->get_section_info(1)));
-
 
1037
        $this->assertTrue($format->is_section_visible($modinfoteacher->get_section_info(2)));
-
 
1038
 
-
 
1039
        $this->setUser($student);
-
 
1040
        $modinfostudent = get_fast_modinfo($course, $student->id);
-
 
1041
        $this->assertTrue($format->is_section_visible($modinfostudent->get_section_info(0)));
-
 
1042
        $this->assertFalse($format->is_section_visible($modinfostudent->get_section_info(1)));
-
 
1043
        $this->assertFalse($format->is_section_visible($modinfostudent->get_section_info(2)));
-
 
1044
    }
-
 
1045
 
-
 
1046
    /**
-
 
1047
     * Test for the get_generic_section_name method.
-
 
1048
     *
-
 
1049
     * @covers ::get_generic_section_name
-
 
1050
     */
-
 
1051
    public function test_get_generic_section_name(): void {
-
 
1052
        $this->resetAfterTest();
-
 
1053
 
-
 
1054
        $generator = $this->getDataGenerator();
-
 
1055
        $course1 = $generator->create_course(['format' => 'topics']);
-
 
1056
        $course2 = $generator->create_course(['format' => 'theunittest']);
-
 
1057
 
-
 
1058
        $format = course_get_format($course1);
-
 
1059
        $this->assertEquals(
-
 
1060
            get_string('sectionname', 'format_topics'),
-
 
1061
            $format->get_generic_section_name()
-
 
1062
        );
-
 
1063
 
-
 
1064
        $format = course_get_format($course2);
-
 
1065
        $this->assertEquals(
-
 
1066
            get_string('section'),
-
 
1067
            $format->get_generic_section_name()
-
 
1068
        );
-
 
1069
    }
-
 
1070
 
-
 
1071
    /**
-
 
1072
     * Test can_sections_be_removed_from_navigation().
-
 
1073
     *
-
 
1074
     * @covers ::session_cache
-
 
1075
     * @covers ::session_cache_reset
-
 
1076
     * @covers ::session_cache_reset_all
-
 
1077
     * @covers ::invalidate_all_session_caches_for_course
-
 
1078
     */
-
 
1079
    public function test_session_caches_methods(): void {
-
 
1080
        global $DB;
-
 
1081
 
-
 
1082
        $this->resetAfterTest();
-
 
1083
        $generator = $this->getDataGenerator();
-
 
1084
 
-
 
1085
        $course1 = $generator->create_course(['format' => 'topics']);
-
 
1086
        $course2 = $generator->create_course(['format' => 'topics']);
-
 
1087
 
-
 
1088
        // Force some cacherev to the course.
-
 
1089
        $course1->cacherev = 12345;
-
 
1090
        $course2->cacherev = 67890;
-
 
1091
        $DB->set_field('course', 'cacherev', $course1->cacherev, ['id' => $course1->id]);
-
 
1092
        $DB->set_field('course', 'cacherev', $course2->cacherev, ['id' => $course2->id]);
-
 
1093
 
-
 
1094
        $teacher = $generator->create_and_enrol($course1, 'editingteacher');
-
 
1095
        $generator->enrol_user($teacher->id, $course2->id, 'editingteacher');
-
 
1096
        $this->setUser($teacher);
-
 
1097
 
-
 
1098
        // The cache key uses time() as hash. To not wait a second between calls we fake an initial value.
-
 
1099
        $statecache = cache::make('core', 'courseeditorstate');
-
 
1100
        $statecache->set($course1->id, $course1->cacherev . '_11111');
-
 
1101
        $statecache->set($course2->id, $course2->cacherev . '_22222');
-
 
1102
 
-
 
1103
        $course1cachekey = \core_courseformat\base::session_cache($course1);
-
 
1104
 
-
 
1105
        // Validate the method returns the same value when called twice.
-
 
1106
        $course1cachekeyagain = \core_courseformat\base::session_cache($course1);
-
 
1107
        $this->assertEquals($course1cachekey, $course1cachekeyagain);
-
 
1108
 
-
 
1109
        // Validate other course has a diferent cache key.
-
 
1110
        $course2cachekey = \core_courseformat\base::session_cache($course2);
-
 
1111
        $this->assertNotEquals($course1cachekey, $course2cachekey);
-
 
1112
 
-
 
1113
        // Reset the specific course cache.
-
 
1114
        \core_courseformat\base::session_cache_reset($course1);
-
 
1115
 
-
 
1116
        $resetcachekey = \core_courseformat\base::session_cache($course1);
-
 
1117
        $this->assertNotEquals($course1cachekey, $resetcachekey);
-
 
1118
 
-
 
1119
        $reset2cachekey = \core_courseformat\base::session_cache($course2);
-
 
1120
        $this->assertEquals($course2cachekey, $reset2cachekey);
-
 
1121
 
-
 
1122
        // Return to the initial value.
-
 
1123
        $statecache->set($course1->id, $course1->cacherev . '_11111');
-
 
1124
        $statecache->set($course2->id, $course2->cacherev . '_22222');
-
 
1125
 
-
 
1126
        // Reset all user course caches.
-
 
1127
        \core_courseformat\base::session_cache_reset_all();
-
 
1128
 
-
 
1129
        $resetallcachekey = \core_courseformat\base::session_cache($course1);
-
 
1130
        $this->assertNotEquals($course1cachekey, $resetallcachekey);
-
 
1131
 
-
 
1132
        $resetall2cachekey = \core_courseformat\base::session_cache($course2);
-
 
1133
        $this->assertNotEquals($course2cachekey, $resetall2cachekey);
-
 
1134
 
-
 
1135
        // Return to the initial value.
-
 
1136
        $statecache->set($course1->id, $course1->cacherev . '_11111');
-
 
1137
        $statecache->set($course2->id, $course2->cacherev . '_22222');
-
 
1138
 
-
 
1139
        // Invalidate cache on course 1.
-
 
1140
        \core_courseformat\base::invalidate_all_session_caches_for_course($course1);
-
 
1141
 
-
 
1142
        $invalidatecachekey = \core_courseformat\base::session_cache($course1);
-
 
1143
        $this->assertNotEquals($course1cachekey, $invalidatecachekey);
-
 
1144
 
-
 
1145
        $invalidate2cachekey = \core_courseformat\base::session_cache($course2);
-
 
1146
        $this->assertEquals($course2cachekey, $invalidate2cachekey);
967
    }
1147
    }
Línea 968... Línea 1148...
968
}
1148
}
969
 
1149
 
970
/**
1150
/**
Línea 999... Línea 1179...
999
 * @author    Philipp Memmel
1179
 * @author    Philipp Memmel
1000
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1180
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1001
 */
1181
 */
1002
class format_testformatsections extends core_courseformat\base {
1182
class format_testformatsections extends core_courseformat\base {
1003
    /**
1183
    /**
-
 
1184
     * @var int|null $forcemaxsections The maximum number of sections.
-
 
1185
     */
-
 
1186
    public ?int $forcemaxsections = null;
-
 
1187
    /**
1004
     * Returns if this course format uses sections.
1188
     * Returns if this course format uses sections.
1005
     *
1189
     *
1006
     * @return true
1190
     * @return true
1007
     */
1191
     */
1008
    public function uses_sections() {
1192
    public function uses_sections() {
Línea 1010... Línea 1194...
1010
    }
1194
    }
Línea 1011... Línea 1195...
1011
 
1195
 
1012
    public function can_sections_be_removed_from_navigation(): bool {
1196
    public function can_sections_be_removed_from_navigation(): bool {
1013
        return true;
1197
        return true;
-
 
1198
    }
-
 
1199
 
-
 
1200
    public function get_last_section_number(): int {
-
 
1201
        if ($this->forcemaxsections !== null) {
-
 
1202
            return $this->forcemaxsections;
-
 
1203
        }
-
 
1204
        return parent::get_last_section_number();
1014
    }
1205
    }
Línea 1015... Línea 1206...
1015
}
1206
}
1016
 
1207
 
1017
/**
1208
/**