Proyectos de Subversion Moodle

Rev

Rev 11 | | 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
 */
1441 ariadna 27
final class state_test extends \advanced_testcase {
1 efrain 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
1441 ariadna 44
     * @covers \core_courseformat\output\local\state\course
45
     * @covers \core_courseformat\output\local\state\section
46
     * @covers \core_courseformat\output\local\state\cm
1 efrain 47
     *
48
     * @param string $format The course format of the course where the method will be executed.
49
     */
11 efrain 50
    public function test_state(string $format = 'topics'): void {
1 efrain 51
        global $PAGE;
52
 
53
        $this->resetAfterTest();
54
 
55
        // Create a course.
56
        $numsections = 6;
57
        $course = $this->getDataGenerator()->create_course(['numsections' => $numsections, 'format' => $format]);
58
        $hiddensections = [4, 6];
59
        foreach ($hiddensections as $section) {
60
            set_section_visible($course->id, $section, 0);
61
        }
62
 
63
        // Create and enrol user.
64
        $this->setAdminUser();
65
        $courseformat = course_get_format($course->id);
66
        $modinfo = $courseformat->get_modinfo();
67
        $issocialformat = $courseformat->get_format() === 'social';
68
 
69
        // Only create activities if the course format is not social.
70
        // There's no course home page (and sections) for social course format.
71
        if (!$issocialformat || $format == 'theunittest') {
72
            // Add some activities to the course.
73
            $this->getDataGenerator()->create_module('page', ['course' => $course->id], ['section' => 1,
74
                'visible' => 1]);
75
            $this->getDataGenerator()->create_module('forum', ['course' => $course->id], ['section' => 1,
76
                'visible' => 1]);
77
            $this->getDataGenerator()->create_module('assign', ['course' => $course->id], ['section' => 2,
78
                'visible' => 0]);
79
            $this->getDataGenerator()->create_module('glossary', ['course' => $course->id], ['section' => 4,
80
                'visible' => 1]);
81
            $this->getDataGenerator()->create_module('label', ['course' => $course->id], ['section' => 5,
82
                'visible' => 0]);
83
            $this->getDataGenerator()->create_module('feedback', ['course' => $course->id], ['section' => 5,
84
                'visible' => 1]);
85
        }
86
 
87
        $courseclass = $courseformat->get_output_classname('state\\course');
88
        $sectionclass = $courseformat->get_output_classname('state\\section');
89
        $cmclass = $courseformat->get_output_classname('state\\cm');
90
 
91
        // Get the proper renderer.
92
        $renderer = $courseformat->get_renderer($PAGE);
93
        $result = (object)[
94
            'course' => (object)[],
95
            'section' => [],
96
            'cm' => [],
97
        ];
98
 
99
        // General state.
100
        $coursestate = new $courseclass($courseformat);
101
        $result->course = $coursestate->export_for_template($renderer);
102
 
103
        if ($format == 'theunittest') {
104
            // These course format's hasn't the renderer file, so a debugging message will be displayed.
105
            $this->assertDebuggingCalled();
106
        }
107
 
108
        $this->assertEquals($course->id, $result->course->id);
109
        $this->assertEquals($numsections, $result->course->numsections);
110
        $this->assertFalse($result->course->editmode);
111
        $sections = $modinfo->get_section_info_all();
112
 
113
        foreach ($sections as $key => $section) {
114
            if (!$issocialformat || $format == 'theunittest') {
1441 ariadna 115
                $this->assertEquals($section->id, $result->course->sectionlist[$key]);
1 efrain 116
                if (!empty($section->uservisible)) {
117
                    $sectionstate = new $sectionclass($courseformat, $section);
118
                    $result->section[$key] = $sectionstate->export_for_template($renderer);
119
                    $this->assertEquals($section->id, $result->section[$key]->id);
120
                    $this->assertEquals($section->section, $result->section[$key]->section);
121
                    $this->assertTrue($section->visible == $result->section[$key]->visible);
122
 
123
                    if ($key === 0 || $key === 3 || $key === 6) {
124
                        $this->assertEmpty($result->section[$key]->cmlist);
125
                    } else if ($key === 1) {
126
                        $this->assertEquals(2, count($result->section[$key]->cmlist));
127
                    } else if ($key === 2 || $key === 4) {
128
                        $this->assertEquals(1, count($result->section[$key]->cmlist));
129
                    } else if ($key === 5) {
130
                        $this->assertEquals(2, count($result->section[$key]->cmlist));
131
                    }
132
                }
133
            } else {
134
                // Social course format doesn't have sections.
135
                $this->assertEmpty($result->section);
136
            }
137
        }
138
 
139
        foreach ($modinfo->cms as $key => $cm) {
140
            $section = $sections[$cm->sectionnum];
141
            $cmstate = new $cmclass($courseformat, $section, $cm);
142
            $result->cm[$key] = $cmstate->export_for_template($renderer);
143
            $this->assertEquals($cm->id, $result->cm[$key]->id);
144
            $this->assertEquals($cm->name, $result->cm[$key]->name);
145
            $this->assertTrue($cm->visible == $result->cm[$key]->visible);
146
        }
147
    }
148
 
149
    /**
150
     * Data provider for test_state().
151
     *
152
     * @return array
153
     */
1441 ariadna 154
    public static function state_provider(): array {
1 efrain 155
        return [
156
            // COURSEFORMAT. Test behaviour depending on course formats.
157
            'Single activity format' => [
158
                'format' => 'singleactivity',
159
            ],
160
            'Social format' => [
161
                'format' => 'social',
162
            ],
163
            'Weeks format' => [
164
                'format' => 'weeks',
165
            ],
166
            'The unit tests format' => [
167
                'format' => 'theunittest',
168
            ],
169
        ];
170
    }
171
}