Proyectos de Subversion Moodle

Rev

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

Rev 1 Rev 1441
Línea 30... Línea 30...
30
 * @category   test
30
 * @category   test
31
 * @copyright  2021 Sara Arjona (sara@moodle.com)
31
 * @copyright  2021 Sara Arjona (sara@moodle.com)
32
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
32
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33
 * @coversDefaultClass \core_courseformat\external\get_state
33
 * @coversDefaultClass \core_courseformat\external\get_state
34
 */
34
 */
35
class get_state_test extends \externallib_advanced_testcase {
35
final class get_state_test extends \externallib_advanced_testcase {
Línea 36... Línea 36...
36
 
36
 
37
    /** @var array Sections in the testing course. */
37
    /** @var array Sections in the testing course. */
Línea 38... Línea 38...
38
    private $sections;
38
    private $sections;
Línea 53... Línea 53...
53
 
53
 
54
    /**
54
    /**
55
     * Setup testcase.
55
     * Setup testcase.
56
     */
56
     */
-
 
57
    public function setUp(): void {
57
    public function setUp(): void {
58
        parent::setUp();
Línea 58... Línea 59...
58
        $this->resetAfterTest();
59
        $this->resetAfterTest();
59
 
60
 
60
        $this->sections = [];
61
        $this->sections = [];
Línea 65... Línea 66...
65
     * Test tearDown.
66
     * Test tearDown.
66
     */
67
     */
67
    public function tearDown(): void {
68
    public function tearDown(): void {
68
        unset($this->sections);
69
        unset($this->sections);
69
        unset($this->activities);
70
        unset($this->activities);
-
 
71
        parent::tearDown();
70
    }
72
    }
Línea 71... Línea 73...
71
 
73
 
72
    /**
74
    /**
73
     * Test the behaviour of get_state::execute().
75
     * Test the behaviour of get_state::execute().
Línea 82... Línea 84...
82
    public function test_get_state(string $role, string $format = 'topics', ?string $expectedexception = null): void {
84
    public function test_get_state(string $role, string $format = 'topics', ?string $expectedexception = null): void {
83
        $this->resetAfterTest();
85
        $this->resetAfterTest();
Línea 84... Línea 86...
84
 
86
 
85
        // Create a course.
87
        // Create a course.
86
        $numsections = 6;
-
 
-
 
88
        $numsections = 6;
87
        $visiblesections = $numsections + 1; // Include topic 0.
89
 
88
        $course = $this->getDataGenerator()->create_course(['numsections' => $numsections, 'format' => $format]);
90
        $course = $this->getDataGenerator()->create_course(['numsections' => $numsections, 'format' => $format]);
89
        $hiddensections = [4, 6];
91
        $hiddensections = [4, 6];
90
        foreach ($hiddensections as $section) {
92
        foreach ($hiddensections as $section) {
91
            set_section_visible($course->id, $section, 0);
93
            set_section_visible($course->id, $section, 0);
Línea 95... Línea 97...
95
        $isadmin = ($role == 'admin');
97
        $isadmin = ($role == 'admin');
96
        $canedit = $isadmin || ($role == 'editingteacher');
98
        $canedit = $isadmin || ($role == 'editingteacher');
97
        if ($isadmin) {
99
        if ($isadmin) {
98
            $this->setAdminUser();
100
            $this->setAdminUser();
99
        } else {
101
        } else {
100
            if (!$canedit) {
-
 
101
                // User won't see the hidden sections. Remove them from the total.
-
 
102
                $visiblesections = $visiblesections - count($hiddensections);
-
 
103
            }
-
 
104
            $user = $this->getDataGenerator()->create_user();
102
            $user = $this->getDataGenerator()->create_user();
105
            if ($role != 'unenroled') {
103
            if ($role != 'unenroled') {
106
                $this->getDataGenerator()->enrol_user($user->id, $course->id, $role);
104
                $this->getDataGenerator()->enrol_user($user->id, $course->id, $role);
107
            }
105
            }
108
            $this->setUser($user);
106
            $this->setUser($user);
109
        }
107
        }
-
 
108
        $visiblesections = $numsections + 1; // We include topic 0.
-
 
109
        if (!$canedit) {
-
 
110
            // User won't see the hidden sections. Remove them from the total.
-
 
111
            $visiblesections = $visiblesections - count($hiddensections);
-
 
112
        }
-
 
113
        if ($format == 'social') {
-
 
114
            $visiblesections = 1; // But Social format has one section visible.
-
 
115
        }
Línea 110... Línea 116...
110
 
116
 
111
        // Social course format automatically creates a forum activity.
117
        // Social course format automatically creates a forum activity.
112
        if (course_get_format($course)->get_format() === 'social') {
118
        if (course_get_format($course)->get_format() === 'social') {
Línea 121... Línea 127...
121
 
127
 
122
            // Assign the activity cm to the activities array.
128
            // Assign the activity cm to the activities array.
123
            $this->activities[$activitycm->id] = $activitycm;
129
            $this->activities[$activitycm->id] = $activitycm;
124
        } else {
130
        } else {
-
 
131
            // Add some activities to the course.
125
            // Add some activities to the course.
132
            $this->create_activity($course->id, 'qbank', 0, true, $canedit);
126
            $this->create_activity($course->id, 'page', 1, true, $canedit);
133
            $this->create_activity($course->id, 'page', 1, true, $canedit);
127
            $this->create_activity($course->id, 'forum', 1, true, $canedit);
134
            $this->create_activity($course->id, 'forum', 1, true, $canedit);
128
            $this->create_activity($course->id, 'book', 1, false, $canedit);
135
            $this->create_activity($course->id, 'book', 1, false, $canedit);
129
            $this->create_activity($course->id, 'assign', 2, false, $canedit);
136
            $this->create_activity($course->id, 'assign', 2, false, $canedit);
Línea 168... Línea 175...
168
            // Check course modules list for this section is the expected.
175
            // Check course modules list for this section is the expected.
169
            if (array_key_exists($section->number, $this->sections)) {
176
            if (array_key_exists($section->number, $this->sections)) {
170
                $this->assertEquals($this->sections[$section->number], $section->cmlist);
177
                $this->assertEquals($this->sections[$section->number], $section->cmlist);
171
            }
178
            }
172
        }
179
        }
173
        // Check course modules information.
180
        // Check course modules information,
-
 
181
        // must not contain modules that cannot be displayed to the course page under any circumstances.
174
        foreach ($result->cm as $cm) {
182
        foreach ($result->cm as $cm) {
175
            $this->assertEquals($this->activities[$cm->id]->name, $cm->name);
183
            $this->assertEquals($this->activities[$cm->id]->name, $cm->name);
176
            $this->assertEquals((bool) $this->activities[$cm->id]->visible, $cm->visible);
184
            $this->assertEquals((bool) $this->activities[$cm->id]->visible, $cm->visible);
-
 
185
            $this->assertNotEquals('qbank', $this->activities[$cm->id]->modname);
177
        }
186
        }
178
    }
187
    }
Línea 179... Línea 188...
179
 
188
 
180
    /**
189
    /**
181
     * Data provider for test_get_state().
190
     * Data provider for test_get_state().
182
     *
191
     *
183
     * @return array
192
     * @return array
184
     */
193
     */
185
    public function get_state_provider(): array {
194
    public static function get_state_provider(): array {
186
        return [
195
        return [
187
            // ROLES. Testing behaviour depending on the user role calling the method.
196
            // ROLES. Testing behaviour depending on the user role calling the method.
188
            'Admin user should work' => [
197
            'Admin user should work' => [
189
                'role' => 'admin',
198
                'role' => 'admin',
Línea 273... Línea 282...
273
            ['section' => $section, 'visible' => $visible]
282
            ['section' => $section, 'visible' => $visible]
274
        );
283
        );
Línea 275... Línea 284...
275
 
284
 
Línea 276... Línea 285...
276
        list(, $activitycm) = get_course_and_cm_from_instance($activity->id, $type);
285
        list(, $activitycm) = get_course_and_cm_from_instance($activity->id, $type);
277
 
286
 
278
        if ($visible || $canedit) {
287
        if (($visible || $canedit) && $activitycm->is_of_type_that_can_display()) {
279
            $this->activities[$activitycm->id] = $activitycm;
288
            $this->activities[$activitycm->id] = $activitycm;
280
            $this->sections[$section][] = $activitycm->id;
289
            $this->sections[$section][] = $activitycm->id;
281
        }
290
        }