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 format_weeks;
18
 
19
use core_external\external_api;
20
 
21
defined('MOODLE_INTERNAL') || die();
22
 
23
global $CFG;
24
require_once($CFG->dirroot . '/course/lib.php');
25
 
26
/**
27
 * format_weeks related unit tests
28
 *
29
 * @package    format_weeks
30
 * @copyright  2015 Marina Glancy
31
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
32
 * @covers     \format_weeks
33
 */
34
class format_weeks_test extends \advanced_testcase {
35
 
36
    /**
37
     * Tests for format_weeks::get_section_name method with default section names.
38
     */
11 efrain 39
    public function test_get_section_name(): void {
1 efrain 40
        global $DB;
41
        $this->resetAfterTest(true);
42
 
43
        // Generate a course with 5 sections.
44
        $generator = $this->getDataGenerator();
45
        $numsections = 5;
46
        $course = $generator->create_course(array('numsections' => $numsections, 'format' => 'weeks'),
47
            array('createsections' => true));
48
 
49
        // Get section names for course.
50
        $coursesections = $DB->get_records('course_sections', array('course' => $course->id));
51
 
52
        // Test get_section_name with default section names.
53
        $courseformat = course_get_format($course);
54
        foreach ($coursesections as $section) {
55
            // Assert that with unmodified section names, get_section_name returns the same result as get_default_section_name.
56
            $this->assertEquals($courseformat->get_default_section_name($section), $courseformat->get_section_name($section));
57
        }
58
    }
59
 
60
    /**
61
     * Tests for format_weeks::get_section_name method with modified section names.
62
     */
11 efrain 63
    public function test_get_section_name_customised(): void {
1 efrain 64
        global $DB;
65
        $this->resetAfterTest(true);
66
 
67
        // Generate a course with 5 sections.
68
        $generator = $this->getDataGenerator();
69
        $numsections = 5;
70
        $course = $generator->create_course(array('numsections' => $numsections, 'format' => 'weeks'),
71
            array('createsections' => true));
72
 
73
        // Get section names for course.
74
        $coursesections = $DB->get_records('course_sections', array('course' => $course->id));
75
 
76
        // Modify section names.
77
        $customname = "Custom Section";
78
        foreach ($coursesections as $section) {
79
            $section->name = "$customname $section->section";
80
            $DB->update_record('course_sections', $section);
81
        }
82
 
83
        // Requery updated section names then test get_section_name.
84
        $coursesections = $DB->get_records('course_sections', array('course' => $course->id));
85
        $courseformat = course_get_format($course);
86
        foreach ($coursesections as $section) {
87
            // Assert that with modified section names, get_section_name returns the modified section name.
88
            $this->assertEquals($section->name, $courseformat->get_section_name($section));
89
        }
90
    }
91
 
92
    /**
93
     * Tests for format_weeks::get_default_section_name.
94
     */
11 efrain 95
    public function test_get_default_section_name(): void {
1 efrain 96
        global $DB;
97
        $this->resetAfterTest(true);
98
 
99
        // Generate a course with 5 sections.
100
        $generator = $this->getDataGenerator();
101
        $numsections = 5;
102
        $course = $generator->create_course(array('numsections' => $numsections, 'format' => 'weeks'),
103
            array('createsections' => true));
104
 
105
        // Get section names for course.
106
        $coursesections = $DB->get_records('course_sections', array('course' => $course->id));
107
 
108
        // Test get_default_section_name with default section names.
109
        $courseformat = course_get_format($course);
110
        foreach ($coursesections as $section) {
111
            if ($section->section == 0) {
112
                $sectionname = get_string('section0name', 'format_weeks');
113
                $this->assertEquals($sectionname, $courseformat->get_default_section_name($section));
114
            } else {
115
                $dates = $courseformat->get_section_dates($section);
116
                $dates->end = ($dates->end - 86400);
117
                $dateformat = get_string('strftimedateshort');
118
                $weekday = userdate($dates->start, $dateformat);
119
                $endweekday = userdate($dates->end, $dateformat);
120
                $sectionname = $weekday.' - '.$endweekday;
121
 
122
                $this->assertEquals($sectionname, $courseformat->get_default_section_name($section));
123
            }
124
        }
125
    }
126
 
127
    /**
128
     * Test web service updating section name
129
     */
11 efrain 130
    public function test_update_inplace_editable(): void {
1 efrain 131
        global $CFG, $DB, $PAGE;
132
        require_once($CFG->dirroot . '/lib/external/externallib.php');
133
 
134
        $this->resetAfterTest();
135
        $user = $this->getDataGenerator()->create_user();
136
        $this->setUser($user);
137
        $course = $this->getDataGenerator()->create_course(array('numsections' => 5, 'format' => 'weeks'),
138
            array('createsections' => true));
139
        $section = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 2));
140
 
141
        // Call webservice without necessary permissions.
142
        try {
143
            \core_external::update_inplace_editable('format_weeks', 'sectionname', $section->id, 'New section name');
144
            $this->fail('Exception expected');
145
        } catch (\moodle_exception $e) {
146
            $this->assertEquals('Course or activity not accessible. (Not enrolled)',
147
                    $e->getMessage());
148
        }
149
 
150
        // Change to teacher and make sure that section name can be updated using web service update_inplace_editable().
151
        $teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher'));
152
        $this->getDataGenerator()->enrol_user($user->id, $course->id, $teacherrole->id);
153
 
154
        $res = \core_external::update_inplace_editable('format_weeks', 'sectionname', $section->id, 'New section name');
155
        $res = external_api::clean_returnvalue(\core_external::update_inplace_editable_returns(), $res);
156
        $this->assertEquals('New section name', $res['value']);
157
        $this->assertEquals('New section name', $DB->get_field('course_sections', 'name', array('id' => $section->id)));
158
    }
159
 
160
    /**
161
     * Test callback updating section name
162
     */
11 efrain 163
    public function test_inplace_editable(): void {
1 efrain 164
        global $CFG, $DB, $PAGE;
165
 
166
        $this->resetAfterTest();
167
        $user = $this->getDataGenerator()->create_user();
168
        $course = $this->getDataGenerator()->create_course(array('numsections' => 5, 'format' => 'weeks'),
169
            array('createsections' => true));
170
        $teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher'));
171
        $this->getDataGenerator()->enrol_user($user->id, $course->id, $teacherrole->id);
172
        $this->setUser($user);
173
 
174
        $section = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 2));
175
 
176
        // Call callback format_weeks_inplace_editable() directly.
177
        $tmpl = component_callback('format_weeks', 'inplace_editable', array('sectionname', $section->id, 'Rename me again'));
178
        $this->assertInstanceOf('core\output\inplace_editable', $tmpl);
179
        $res = $tmpl->export_for_template($PAGE->get_renderer('core'));
180
        $this->assertEquals('Rename me again', $res['value']);
181
        $this->assertEquals('Rename me again', $DB->get_field('course_sections', 'name', array('id' => $section->id)));
182
 
183
        // Try updating using callback from mismatching course format.
184
        try {
185
            $tmpl = component_callback('format_topics', 'inplace_editable', array('sectionname', $section->id, 'New name'));
186
            $this->fail('Exception expected');
187
        } catch (\moodle_exception $e) {
188
            $this->assertEquals(1, preg_match('/^Can\'t find data record in database/', $e->getMessage()));
189
        }
190
    }
191
 
192
    /**
193
     * Test get_default_course_enddate.
194
     *
195
     * @return void
196
     */
11 efrain 197
    public function test_default_course_enddate(): void {
1 efrain 198
        global $CFG, $DB, $PAGE;
199
 
200
        $this->resetAfterTest(true);
201
 
202
        require_once($CFG->dirroot . '/course/tests/fixtures/testable_course_edit_form.php');
203
 
204
        $this->setTimezone('UTC');
205
 
206
        $params = array('format' => 'weeks', 'numsections' => 5, 'startdate' => 1445644800);
207
        $course = $this->getDataGenerator()->create_course($params);
208
        $category = $DB->get_record('course_categories', array('id' => $course->category));
209
 
210
        $args = [
211
            'course' => $course,
212
            'category' => $category,
213
            'editoroptions' => [
214
                'context' => \context_course::instance($course->id),
215
                'subdirs' => 0
216
            ],
217
            'returnto' => new \moodle_url('/'),
218
            'returnurl' => new \moodle_url('/'),
219
        ];
220
 
221
        $PAGE->set_course($course);
222
        $courseform = new \testable_course_edit_form(null, $args);
223
        $courseform->definition_after_data();
224
 
225
        // Calculate the expected end date.
226
        $enddate = $params['startdate'] + (WEEKSECS * $params['numsections']);
227
 
228
        $weeksformat = course_get_format($course->id);
229
        $this->assertEquals($enddate, $weeksformat->get_default_course_enddate($courseform->get_quick_form()));
230
    }
231
 
232
    /**
233
     * Test for get_view_url().
234
     *
235
     * @covers ::get_view_url
236
     */
237
    public function test_get_view_url(): void {
238
        global $CFG;
239
        $this->resetAfterTest();
240
 
241
        // Generate a course with two sections (0 and 1) and two modules.
242
        $generator = $this->getDataGenerator();
243
        $course1 = $generator->create_course(array('format' => 'weeks'));
244
        course_create_sections_if_missing($course1, array(0, 1));
245
 
246
        $data = (object)['id' => $course1->id];
247
        $format = course_get_format($course1);
248
        $format->update_course_format_options($data);
249
 
250
        // In page.
251
        $this->assertNotEmpty($format->get_view_url(null));
252
        $this->assertNotEmpty($format->get_view_url(0));
253
        $this->assertNotEmpty($format->get_view_url(1));
254
 
255
        // Navigation.
256
        $this->assertStringContainsString('course/view.php', $format->get_view_url(0));
257
        $this->assertStringContainsString('course/view.php', $format->get_view_url(1));
258
        $this->assertStringContainsString('course/section.php', $format->get_view_url(0, ['navigation' => 1]));
259
        $this->assertStringContainsString('course/section.php', $format->get_view_url(1, ['navigation' => 1]));
260
        // When sr parameter is defined, the section.php page should be returned.
261
        $this->assertStringContainsString('course/section.php', $format->get_view_url(0, ['sr' => 1]));
262
        $this->assertStringContainsString('course/section.php', $format->get_view_url(1, ['sr' => 1]));
263
        $this->assertStringContainsString('course/section.php', $format->get_view_url(0, ['sr' => 0]));
264
        $this->assertStringContainsString('course/section.php', $format->get_view_url(1, ['sr' => 0]));
265
    }
266
 
267
    /**
268
     * Test get_required_jsfiles().
269
     *
270
     * @covers ::get_required_jsfiles
271
     */
272
    public function test_get_required_jsfiles(): void {
273
        $this->resetAfterTest();
274
 
275
        $generator = $this->getDataGenerator();
276
 
277
        $course = $generator->create_course(['format' => 'weeks']);
278
        $format = course_get_format($course);
279
        $this->assertEmpty($format->get_required_jsfiles());
280
    }
281
}