Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
// This file is part of Moodle - http://moodle.org/
3
//
4
// Moodle is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// Moodle is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
 
17
namespace core_courseformat\output\local\state;
18
 
19
/**
20
 * Tests for state classes (course, section, cm).
21
 *
22
 * @package    core
23
 * @subpackage course
24
 * @copyright  2021 Ilya Tregubov <ilya@moodle.com>
25
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26
 */
27
class state_test extends \advanced_testcase {
28
 
29
    /**
30
     * Setup to ensure that fixtures are loaded.
31
     */
32
    public static function setupBeforeClass(): void {
33
        global $CFG;
34
 
35
        require_once($CFG->dirroot . '/course/lib.php');
36
        require_once($CFG->dirroot . '/course/format/tests/fixtures/format_theunittest.php');
37
        require_once($CFG->dirroot . '/course/format/tests/fixtures/format_theunittest_output_course_format_state.php');
38
    }
39
 
40
    /**
41
     * Test the behaviour of state::export_for_template().
42
     *
43
     * @dataProvider state_provider
44
     * @covers \core_courseformat\output\local\state
45
     *
46
     * @param string $format The course format of the course where the method will be executed.
47
     */
11 efrain 48
    public function test_state(string $format = 'topics'): void {
1 efrain 49
        global $PAGE;
50
 
51
        $this->resetAfterTest();
52
 
53
        // Create a course.
54
        $numsections = 6;
55
        $course = $this->getDataGenerator()->create_course(['numsections' => $numsections, 'format' => $format]);
56
        $hiddensections = [4, 6];
57
        foreach ($hiddensections as $section) {
58
            set_section_visible($course->id, $section, 0);
59
        }
60
 
61
        // Create and enrol user.
62
        $this->setAdminUser();
63
        $courseformat = course_get_format($course->id);
64
        $modinfo = $courseformat->get_modinfo();
65
        $issocialformat = $courseformat->get_format() === 'social';
66
 
67
        // Only create activities if the course format is not social.
68
        // There's no course home page (and sections) for social course format.
69
        if (!$issocialformat || $format == 'theunittest') {
70
            // Add some activities to the course.
71
            $this->getDataGenerator()->create_module('page', ['course' => $course->id], ['section' => 1,
72
                'visible' => 1]);
73
            $this->getDataGenerator()->create_module('forum', ['course' => $course->id], ['section' => 1,
74
                'visible' => 1]);
75
            $this->getDataGenerator()->create_module('assign', ['course' => $course->id], ['section' => 2,
76
                'visible' => 0]);
77
            $this->getDataGenerator()->create_module('glossary', ['course' => $course->id], ['section' => 4,
78
                'visible' => 1]);
79
            $this->getDataGenerator()->create_module('label', ['course' => $course->id], ['section' => 5,
80
                'visible' => 0]);
81
            $this->getDataGenerator()->create_module('feedback', ['course' => $course->id], ['section' => 5,
82
                'visible' => 1]);
83
        }
84
 
85
        $courseclass = $courseformat->get_output_classname('state\\course');
86
        $sectionclass = $courseformat->get_output_classname('state\\section');
87
        $cmclass = $courseformat->get_output_classname('state\\cm');
88
 
89
        // Get the proper renderer.
90
        $renderer = $courseformat->get_renderer($PAGE);
91
        $result = (object)[
92
            'course' => (object)[],
93
            'section' => [],
94
            'cm' => [],
95
        ];
96
 
97
        // General state.
98
        $coursestate = new $courseclass($courseformat);
99
        $result->course = $coursestate->export_for_template($renderer);
100
 
101
        if ($format == 'theunittest') {
102
            // These course format's hasn't the renderer file, so a debugging message will be displayed.
103
            $this->assertDebuggingCalled();
104
        }
105
 
106
        $this->assertEquals($course->id, $result->course->id);
107
        $this->assertEquals($numsections, $result->course->numsections);
108
        $this->assertFalse($result->course->editmode);
109
        $sections = $modinfo->get_section_info_all();
110
 
111
        foreach ($sections as $key => $section) {
112
            $this->assertEquals($section->id, $result->course->sectionlist[$key]);
113
            if (!$issocialformat || $format == 'theunittest') {
114
                if (!empty($section->uservisible)) {
115
                    $sectionstate = new $sectionclass($courseformat, $section);
116
                    $result->section[$key] = $sectionstate->export_for_template($renderer);
117
                    $this->assertEquals($section->id, $result->section[$key]->id);
118
                    $this->assertEquals($section->section, $result->section[$key]->section);
119
                    $this->assertTrue($section->visible == $result->section[$key]->visible);
120
 
121
                    if ($key === 0 || $key === 3 || $key === 6) {
122
                        $this->assertEmpty($result->section[$key]->cmlist);
123
                    } else if ($key === 1) {
124
                        $this->assertEquals(2, count($result->section[$key]->cmlist));
125
                    } else if ($key === 2 || $key === 4) {
126
                        $this->assertEquals(1, count($result->section[$key]->cmlist));
127
                    } else if ($key === 5) {
128
                        $this->assertEquals(2, count($result->section[$key]->cmlist));
129
                    }
130
                }
131
            } else {
132
                // Social course format doesn't have sections.
133
                $this->assertEmpty($result->section);
134
            }
135
        }
136
 
137
        foreach ($modinfo->cms as $key => $cm) {
138
            $section = $sections[$cm->sectionnum];
139
            $cmstate = new $cmclass($courseformat, $section, $cm);
140
            $result->cm[$key] = $cmstate->export_for_template($renderer);
141
            $this->assertEquals($cm->id, $result->cm[$key]->id);
142
            $this->assertEquals($cm->name, $result->cm[$key]->name);
143
            $this->assertTrue($cm->visible == $result->cm[$key]->visible);
144
        }
145
    }
146
 
147
    /**
148
     * Data provider for test_state().
149
     *
150
     * @return array
151
     */
152
    public function state_provider(): array {
153
        return [
154
            // COURSEFORMAT. Test behaviour depending on course formats.
155
            'Single activity format' => [
156
                'format' => 'singleactivity',
157
            ],
158
            'Social format' => [
159
                'format' => 'social',
160
            ],
161
            'Weeks format' => [
162
                'format' => 'weeks',
163
            ],
164
            'The unit tests format' => [
165
                'format' => 'theunittest',
166
            ],
167
        ];
168
    }
169
}