Proyectos de Subversion Moodle

Rev

| 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\local;
18
 
19
use stdClass;
20
 
21
/**
22
 * Section format actions class tests.
23
 *
24
 * @package    core_courseformat
25
 * @copyright  2023 Ferran Recio <ferran@moodle.com>
26
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27
 * @coversDefaultClass \core_courseformat\sectionactions
28
 */
29
class sectionactions_test extends \advanced_testcase {
30
    /**
31
     * Setup to ensure that fixtures are loaded.
32
     */
33
    public static function setUpBeforeClass(): void {
34
        global $CFG;
35
        require_once($CFG->dirroot . '/course/lib.php');
36
    }
37
 
38
    /**
39
     * Test for create_delegated method.
40
     * @covers ::create_delegated
41
     * @dataProvider create_delegated_provider
42
     * @param string $component the name of the plugin
43
     * @param int|null $itemid the id of the delegated section
44
     * @param stdClass|null $fields the fields to set on the section
45
     */
46
    public function test_create_delegated(string $component, ?int $itemid, ?stdClass $fields): void {
47
        global $DB;
48
        $this->resetAfterTest();
49
 
50
        $course = $this->getDataGenerator()->create_course(['format' => 'topics', 'numsections' => 1]);
51
 
52
        $sectionactions = new sectionactions($course);
53
        $section = $sectionactions->create_delegated($component, $itemid, $fields);
54
 
55
        $this->assertEquals($component, $section->component);
56
        $this->assertEquals($itemid, $section->itemid);
57
        if (!empty($fields)) {
58
            foreach ($fields as $field => $value) {
59
                $this->assertEquals($value, $section->$field);
60
            }
61
        }
62
    }
63
 
64
    /**
65
     * Data provider for test_create_delegated.
66
     * @return array
67
     */
68
    public static function create_delegated_provider(): array {
69
        return [
70
            'component with no itemid or fields' => [
71
                'mod_assign',
72
                null,
73
                null,
74
            ],
75
            'component with itemid but no fields' => [
76
                'mod_assign',
77
                1,
78
                null,
79
            ],
80
            'component with itemid and empty fields' => [
81
                'mod_assign',
82
                1,
83
                new stdClass(),
84
            ],
85
            'component with itemid and name field' => [
86
                'mod_assign',
87
                1,
88
                (object) ['name' => 'new name'],
89
            ],
90
            'component with no itemid but name field' => [
91
                'mod_assign',
92
                null,
93
                (object) ['name' => 'new name'],
94
            ],
95
            'component with itemid and summary' => [
96
                'mod_assign',
97
                1,
98
                (object) ['summary' => 'summary'],
99
            ],
100
            'component with itemid and summary, summaryformat ' => [
101
                'mod_assign',
102
                1,
103
                (object) ['summary' => 'summary', 'summaryformat' => 1],
104
            ],
105
            'component with itemid and section number' => [
106
                'mod_assign',
107
                1,
108
                (object) ['section' => 2],
109
            ],
110
            'component with itemid and visible 1' => [
111
                'mod_assign',
112
                1,
113
                (object) ['visible' => 1],
114
            ],
115
            'component with itemid and visible 0' => [
116
                'mod_assign',
117
                1,
118
                (object) ['visible' => 0],
119
            ],
120
        ];
121
    }
122
 
123
    /**
124
     * Test for create method.
125
     * @covers ::create
126
     * @dataProvider create_provider
127
     * @param int $sectionnum the name of the plugin
128
     * @param bool $skip if the validation should be skipped
129
     * @param bool $expectexception if the method should throw an exception
130
     * @param int $expected the expected section number
131
     */
132
    public function test_create(int $sectionnum, bool $skip, bool $expectexception, int $expected): void {
133
        global $DB;
134
        $this->resetAfterTest();
135
 
136
        $course = $this->getDataGenerator()->create_course(['format' => 'topics', 'numsections' => 1]);
137
 
138
        $sectionactions = new sectionactions($course);
139
 
140
        if ($expectexception) {
141
            $this->expectException(\dml_write_exception::class);
142
        }
143
        $section = $sectionactions->create($sectionnum, $skip);
144
 
145
        $this->assertEquals($expected, $section->section);
146
    }
147
 
148
    /**
149
     * Data provider for test_create_delegated.
150
     * @return array
151
     */
152
    public static function create_provider(): array {
153
        return [
154
            'section 1' => [
155
                'sectionnum' => 1,
156
                'skip' => false,
157
                'expectexception' => false,
158
                'expected' => 1,
159
            ],
160
            'section 2' => [
161
                'sectionnum' => 2,
162
                'skip' => false,
163
                'expectexception' => false,
164
                'expected' => 2,
165
            ],
166
            'section 3' => [
167
                'sectionnum' => 3,
168
                'skip' => false,
169
                'expectexception' => false,
170
                'expected' => 2,
171
            ],
172
            'section 4' => [
173
                'sectionnum' => 4,
174
                'skip' => false,
175
                'expectexception' => false,
176
                'expected' => 2,
177
            ],
178
            'section 1 with exception' => [
179
                'sectionnum' => 1,
180
                'skip' => true,
181
                'expectexception' => true,
182
                'expected' => 0,
183
            ],
184
            'section 2 with skip validation' => [
185
                'sectionnum' => 2,
186
                'skip' => true,
187
                'expectexception' => false,
188
                'expected' => 2,
189
            ],
190
            'section 5 with skip validation' => [
191
                'sectionnum' => 5,
192
                'skip' => true,
193
                'expectexception' => false,
194
                'expected' => 5,
195
            ],
196
        ];
197
    }
198
 
199
    /**
200
     * Test create sections when there are sections with comonent (delegated sections) in the course.
201
     * @covers ::create
202
     * @covers ::create_delegated
203
     */
204
    public function test_create_with_delegated_sections(): void {
205
        global $DB;
206
        $this->resetAfterTest();
207
 
208
        $course = $this->getDataGenerator()->create_course(
209
            ['format' => 'topics', 'numsections' => 1],
210
            ['createsections' => true],
211
        );
212
 
213
        $sectionactions = new sectionactions($course);
214
        $section = $sectionactions->create_delegated('mod_forum', 1);
215
        $this->assertEquals(2, $section->section);
216
        $delegateid = $section->id;
217
 
218
        // Regular sections are created before delegated ones.
219
        $section = $sectionactions->create(2);
220
        $this->assertEquals(2, $section->section);
221
        $regularid = $section->id;
222
 
223
        $modinfo = get_fast_modinfo($course);
224
 
225
        $section2 = $modinfo->get_section_info(2);
226
        $this->assertEquals($regularid, $section2->id);
227
        $this->assertEquals(2, $section2->section);
228
 
229
        $sectiondelegated = $modinfo->get_section_info_by_component('mod_forum', 1);
230
        $this->assertEquals($delegateid, $sectiondelegated->id);
231
        $this->assertEquals(3, $sectiondelegated->section);
232
 
233
        // New delegates should be after the current delegate sections.
234
        $section = $sectionactions->create_delegated('mod_forum', 2);
235
        $this->assertEquals(4, $section->section);
236
    }
237
 
238
    /**
239
     * Test for create_if_missing method.
240
     * @covers ::create_if_missing
241
     * @dataProvider create_if_missing_provider
242
     * @param array $sectionnums the section numbers to create
243
     * @param bool $expected the expected result
244
     */
245
    public function test_create_if_missing(array $sectionnums, bool $expected): void {
246
        global $DB;
247
        $this->resetAfterTest();
248
 
249
        $course = $this->getDataGenerator()->create_course(['format' => 'topics', 'numsections' => 2]);
250
 
251
        $sectionactions = new sectionactions($course);
252
        $result = $sectionactions->create_if_missing($sectionnums);
253
 
254
        $this->assertEquals($expected, $result);
255
 
256
        $modinfo = get_fast_modinfo($course);
257
        foreach ($sectionnums as $sectionnum) {
258
            $section = $modinfo->get_section_info($sectionnum);
259
            $this->assertEquals($sectionnum, $section->section);
260
        }
261
    }
262
 
263
    /**
264
     * Data provider for test_create_delegated.
265
     * @return array
266
     */
267
    public static function create_if_missing_provider(): array {
268
        return [
269
            'existing section' => [
270
                'sectionnum' => [1],
271
                'expected' => false,
272
            ],
273
            'unexisting section' => [
274
                'sectionnum' => [3],
275
                'expected' => true,
276
            ],
277
            'several existing sections' => [
278
                'sectionnum' => [1, 2],
279
                'expected' => false,
280
            ],
281
            'several unexisting sections' => [
282
                'sectionnum' => [3, 4],
283
                'expected' => true,
284
            ],
285
            'empty array' => [
286
                'sectionnum' => [],
287
                'expected' => false,
288
            ],
289
            'existent and unexistent sections' => [
290
                'sectionnum' => [1, 2, 3, 4],
291
                'expected' => true,
292
            ],
293
        ];
294
    }
295
 
296
    /**
297
     * Test create if missing when the course has delegated sections.
298
     * @covers ::create_if_missing
299
     * @covers ::create_delegated
300
     */
301
    public function test_create_if_missing_with_delegated_sections(): void {
302
        global $DB;
303
        $this->resetAfterTest();
304
 
305
        $course = $this->getDataGenerator()->create_course(
306
            ['format' => 'topics', 'numsections' => 1],
307
            ['createsections' => true],
308
        );
309
 
310
        $sectionactions = new sectionactions($course);
311
        $section = $sectionactions->create_delegated('mod_forum', 1);
312
        $delegateid = $section->id;
313
 
314
        $result = $sectionactions->create_if_missing([1, 2]);
315
        $this->assertTrue($result);
316
 
317
        $modinfo = get_fast_modinfo($course);
318
        $section = $modinfo->get_section_info(2);
319
        $this->assertEquals(2, $section->section);
320
        $this->assertNotEquals($delegateid, $section->id);
321
        $delegatedsection = $modinfo->get_section_info_by_id($delegateid);
322
        $this->assertEquals(3, $delegatedsection->section);
323
 
324
        $result = $sectionactions->create_if_missing([1, 2]);
325
        $this->assertFalse($result);
326
 
327
        $result = $sectionactions->create_if_missing([1, 2, 3]);
328
        $this->assertTrue($result);
329
 
330
        $modinfo = get_fast_modinfo($course);
331
        $section = $modinfo->get_section_info(3);
332
        $this->assertEquals(3, $section->section);
333
        $this->assertNotEquals($delegateid, $section->id);
334
        $delegatedsection = $modinfo->get_section_info_by_id($delegateid);
335
        $this->assertEquals(4, $delegatedsection->section);
336
 
337
        $result = $sectionactions->create_if_missing([1, 2, 3]);
338
        $this->assertFalse($result);
339
    }
340
 
341
    /**
342
     * Test for delete method.
343
     * @covers ::delete
344
     */
345
    public function test_delete(): void {
346
        global $DB;
347
        $this->resetAfterTest(true);
348
 
349
        $generator = $this->getDataGenerator();
350
 
351
        $course = $generator->create_course(
352
            ['numsections' => 6, 'format' => 'topics'],
353
            ['createsections' => true]
354
        );
355
        $assign0 = $generator->create_module('assign', ['course' => $course, 'section' => 0]);
356
        $assign1 = $generator->create_module('assign', ['course' => $course, 'section' => 1]);
357
        $assign21 = $generator->create_module('assign', ['course' => $course, 'section' => 2]);
358
        $assign22 = $generator->create_module('assign', ['course' => $course, 'section' => 2]);
359
        $assign3 = $generator->create_module('assign', ['course' => $course, 'section' => 3]);
360
        $assign5 = $generator->create_module('assign', ['course' => $course, 'section' => 5]);
361
        $assign6 = $generator->create_module('assign', ['course' => $course, 'section' => 6]);
362
 
363
        $this->setAdminUser();
364
 
365
        $sectionactions = new sectionactions($course);
366
        $sections = get_fast_modinfo($course)->get_section_info_all();
367
 
368
        // Attempt to delete 0-section.
369
        $this->assertFalse($sectionactions->delete($sections[0], true));
370
        $this->assertTrue($DB->record_exists('course_modules', ['id' => $assign0->cmid]));
371
        $this->assertEquals(6, course_get_format($course)->get_last_section_number());
372
 
373
        // Delete last section.
374
        $this->assertTrue($sectionactions->delete($sections[6], true));
375
        $this->assertFalse($DB->record_exists('course_modules', ['id' => $assign6->cmid]));
376
        $this->assertEquals(5, course_get_format($course)->get_last_section_number());
377
 
378
        // Delete empty section.
379
        $this->assertTrue($sectionactions->delete($sections[4], false));
380
        $this->assertEquals(4, course_get_format($course)->get_last_section_number());
381
 
382
        // Delete section in the middle (2).
383
        $this->assertFalse($sectionactions->delete($sections[2], false));
384
        $this->assertEquals(4, course_get_format($course)->get_last_section_number());
385
        $sections = get_fast_modinfo($course)->get_section_info_all();
386
        $this->assertTrue($sectionactions->delete($sections[2], true));
387
        $this->assertFalse($DB->record_exists('course_modules', ['id' => $assign21->cmid]));
388
        $this->assertFalse($DB->record_exists('course_modules', ['id' => $assign22->cmid]));
389
        $this->assertEquals(3, course_get_format($course)->get_last_section_number());
390
        $this->assertEquals(
391
            [
392
 
393
                1 => [$assign1->cmid],
394
                2 => [$assign3->cmid],
395
                3 => [$assign5->cmid],
396
            ],
397
            get_fast_modinfo($course)->sections
398
        );
399
 
400
        // Remove marked section.
401
        course_set_marker($course->id, 1);
402
        $this->assertTrue(course_get_format($course)->is_section_current(1));
403
        $this->assertTrue($sectionactions->delete(
404
            get_fast_modinfo($course)->get_section_info(1),
405
            true
406
        ));
407
        $this->assertFalse(course_get_format($course)->is_section_current(1));
408
    }
409
 
410
    /**
411
     * Test that triggering a course_section_deleted event works as expected.
412
     * @covers ::delete
413
     */
414
    public function test_section_deleted_event(): void {
415
        global $USER, $DB;
416
        $this->resetAfterTest();
417
        $sink = $this->redirectEvents();
418
 
419
        // Create the course with sections.
420
        $course = $this->getDataGenerator()->create_course(['numsections' => 10], ['createsections' => true]);
421
        $coursecontext = \context_course::instance($course->id);
422
 
423
        $section = get_fast_modinfo($course)->get_section_info(10);
424
        $sectionrecord = $DB->get_record('course_sections', ['id' => $section->id]);
425
 
426
        $sectionactions = new sectionactions($course);
427
        $sectionactions->delete($section);
428
 
429
        $events = $sink->get_events();
430
        $event = array_pop($events); // Delete section event.
431
        $sink->close();
432
 
433
        // Validate event data.
434
        $this->assertInstanceOf('\core\event\course_section_deleted', $event);
435
        $this->assertEquals('course_sections', $event->objecttable);
436
        $this->assertEquals($section->id, $event->objectid);
437
        $this->assertEquals($course->id, $event->courseid);
438
        $this->assertEquals($coursecontext->id, $event->contextid);
439
        $this->assertEquals($section->section, $event->other['sectionnum']);
440
        $expecteddesc = "The user with id '{$event->userid}' deleted section number '{$event->other['sectionnum']}' " .
441
            "(section name '{$event->other['sectionname']}') for the course with id '{$event->courseid}'";
442
        $this->assertEquals($expecteddesc, $event->get_description());
443
        $this->assertEquals($sectionrecord, $event->get_record_snapshot('course_sections', $event->objectid));
444
        $this->assertNull($event->get_url());
445
        $this->assertEventContextNotUsed($event);
446
    }
447
 
448
    /**
449
     * Test async section deletion hook.
450
     * @covers ::delete
451
     */
452
    public function test_async_section_deletion_hook_implemented(): void {
453
        // Async section deletion (provided section contains modules), depends on the 'true' being returned by at least one plugin
454
        // implementing the 'course_module_adhoc_deletion_recommended' hook. In core, is implemented by the course recyclebin,
455
        // which will only return true if the plugin is enabled. To make sure async deletion occurs, this test enables recyclebin.
456
        global $DB, $USER;
457
        $this->resetAfterTest(true);
458
        $this->setAdminUser();
459
 
460
        // Ensure recyclebin is enabled.
461
        set_config('coursebinenable', true, 'tool_recyclebin');
462
 
463
        // Create course, module and context.
464
        $generator = $this->getDataGenerator();
465
        $course = $generator->create_course(['numsections' => 4, 'format' => 'topics'], ['createsections' => true]);
466
        $assign0 = $generator->create_module('assign', ['course' => $course, 'section' => 2]);
467
        $assign1 = $generator->create_module('assign', ['course' => $course, 'section' => 2]);
468
        $assign2 = $generator->create_module('assign', ['course' => $course, 'section' => 2]);
469
        $assign3 = $generator->create_module('assign', ['course' => $course, 'section' => 0]);
470
 
471
        $sectionactions = new sectionactions($course);
472
 
473
        // Delete empty section. No difference from normal, synchronous behaviour.
474
        $this->assertTrue($sectionactions->delete(get_fast_modinfo($course)->get_section_info(4), false, true));
475
        $this->assertEquals(3, course_get_format($course)->get_last_section_number());
476
 
477
        // Delete a module in section 2 (using async). Need to verify this doesn't generate two tasks when we delete
478
        // the section in the next step.
479
        course_delete_module($assign2->cmid, true);
480
 
481
        // Confirm that the module is pending deletion in its current section.
482
        $section = $DB->get_record('course_sections', ['course' => $course->id, 'section' => '2']); // For event comparison.
483
        $this->assertEquals(true, $DB->record_exists('course_modules', ['id' => $assign2->cmid, 'deletioninprogress' => 1,
484
            'section' => $section->id]));
485
 
486
        // Non-empty section, no forcedelete, so no change.
487
        $this->assertFalse($sectionactions->delete(get_fast_modinfo($course)->get_section_info(2), false, true));
488
 
489
        $sink = $this->redirectEvents();
490
        $this->assertTrue($sectionactions->delete(get_fast_modinfo($course)->get_section_info(2), true, true));
491
 
492
        // Now, confirm that:
493
        // a) the section's modules have been flagged for deletion and moved to section 0 and;
494
        // b) the section has been deleted and;
495
        // c) course_section_deleted event has been fired. The course_module_deleted events will only fire once they have been
496
        // removed from section 0 via the adhoc task.
497
 
498
        // Modules should have been flagged for deletion and moved to section 0.
499
        $sectionid = $DB->get_field('course_sections', 'id', ['course' => $course->id, 'section' => 0]);
500
        $this->assertEquals(
501
            3,
502
            $DB->count_records('course_modules', ['section' => $sectionid, 'deletioninprogress' => 1])
503
        );
504
 
505
        // Confirm the section has been deleted.
506
        $this->assertEquals(2, course_get_format($course)->get_last_section_number());
507
 
508
        // Check event fired.
509
        $events = $sink->get_events();
510
        $event = array_pop($events);
511
        $sink->close();
512
        $this->assertInstanceOf('\core\event\course_section_deleted', $event);
513
        $this->assertEquals($section->id, $event->objectid);
514
        $this->assertEquals($USER->id, $event->userid);
515
        $this->assertEquals('course_sections', $event->objecttable);
516
        $this->assertEquals(null, $event->get_url());
517
        $this->assertEquals($section, $event->get_record_snapshot('course_sections', $section->id));
518
 
519
        // Now, run the adhoc task to delete the modules from section 0.
520
        $sink = $this->redirectEvents(); // To capture the events.
521
        \phpunit_util::run_all_adhoc_tasks();
522
 
523
        // Confirm the modules have been deleted.
524
        list($insql, $assignids) = $DB->get_in_or_equal([$assign0->cmid, $assign1->cmid, $assign2->cmid]);
525
        $cmcount = $DB->count_records_select('course_modules', 'id ' . $insql, $assignids);
526
        $this->assertEmpty($cmcount);
527
 
528
        // Confirm other modules in section 0 still remain.
529
        $this->assertEquals(1, $DB->count_records('course_modules', ['id' => $assign3->cmid]));
530
 
531
        // Confirm that events were generated for all 3 of the modules.
532
        $events = $sink->get_events();
533
        $sink->close();
534
        $count = 0;
535
        while (!empty($events)) {
536
            $event = array_pop($events);
537
            if ($event instanceof \core\event\course_module_deleted &&
538
                in_array($event->objectid, [$assign0->cmid, $assign1->cmid, $assign2->cmid])) {
539
                $count++;
540
            }
541
        }
542
        $this->assertEquals(3, $count);
543
    }
544
 
545
    /**
546
     * Test section update method.
547
     *
548
     * @covers ::update
549
     * @dataProvider update_provider
550
     * @param string $fieldname the name of the field to update
551
     * @param int|string $value the value to set
552
     * @param int|string $expected the expected value after the update ('=' to specify the same value as original field)
553
     * @param bool $expectexception if the method should throw an exception
554
     */
555
    public function test_update(
556
        string $fieldname,
557
        int|string $value,
558
        int|string $expected,
559
        bool $expectexception
560
    ): void {
561
        global $DB;
562
        $this->resetAfterTest();
563
 
564
        $course = $this->getDataGenerator()->create_course(
565
            ['format' => 'topics', 'numsections' => 1],
566
            ['createsections' => true]
567
        );
568
        $section = get_fast_modinfo($course)->get_section_info(1);
569
 
570
        $sectionrecord = $DB->get_record('course_sections', ['id' => $section->id]);
571
        $this->assertNotEquals($value, $sectionrecord->$fieldname);
572
        $this->assertNotEquals($value, $section->$fieldname);
573
 
574
        if ($expectexception) {
575
            $this->expectException(\moodle_exception::class);
576
        }
577
 
578
        if ($expected === '=') {
579
            $expected = $section->$fieldname;
580
        }
581
 
582
        $sectionactions = new sectionactions($course);
583
        $sectionactions->update($section, [$fieldname => $value]);
584
 
585
        $sectionrecord = $DB->get_record('course_sections', ['id' => $section->id]);
586
        $this->assertEquals($expected, $sectionrecord->$fieldname);
587
 
588
        $section = get_fast_modinfo($course)->get_section_info(1);
589
        $this->assertEquals($expected, $section->$fieldname);
590
    }
591
 
592
    /**
593
     * Data provider for test_update.
594
     * @return array
595
     */
596
    public static function update_provider(): array {
597
        return [
598
            'Id will not be updated' => [
599
                'fieldname' => 'id',
600
                'value' => -1,
601
                'expected' => '=',
602
                'expectexception' => false,
603
            ],
604
            'Course will not be updated' => [
605
                'fieldname' => 'course',
606
                'value' => -1,
607
                'expected' => '=',
608
                'expectexception' => false,
609
            ],
610
            'Section number will not be updated' => [
611
                'fieldname' => 'section',
612
                'value' => -1,
613
                'expected' => '=',
614
                'expectexception' => false,
615
            ],
616
            'Sequence will be updated' => [
617
                'fieldname' => 'name',
618
                'value' => 'new name',
619
                'expected' => 'new name',
620
                'expectexception' => false,
621
            ],
622
            'Summary can be updated' => [
623
                'fieldname' => 'summary',
624
                'value' => 'new summary',
625
                'expected' => 'new summary',
626
                'expectexception' => false,
627
            ],
628
            'Visible can be updated' => [
629
                'fieldname' => 'visible',
630
                'value' => 0,
631
                'expected' => 0,
632
                'expectexception' => false,
633
            ],
634
            'component can be updated' => [
635
                'fieldname' => 'component',
636
                'value' => 'mod_assign',
637
                'expected' => 'mod_assign',
638
                'expectexception' => false,
639
            ],
640
            'itemid can be updated' => [
641
                'fieldname' => 'itemid',
642
                'value' => 1,
643
                'expected' => 1,
644
                'expectexception' => false,
645
            ],
646
            'Long names throws and exception' => [
647
                'fieldname' => 'name',
648
                'value' => str_repeat('a', 256),
649
                'expected' => '=',
650
                'expectexception' => true,
651
            ],
652
        ];
653
    }
654
 
655
    /**
656
     * Test section update method updating several values at once.
657
     *
658
     * @covers ::update
659
     */
660
    public function test_update_multiple_fields(): void {
661
        global $DB;
662
        $this->resetAfterTest();
663
 
664
        $course = $this->getDataGenerator()->create_course(
665
            ['format' => 'topics', 'numsections' => 1],
666
            ['createsections' => true]
667
        );
668
        $section = get_fast_modinfo($course)->get_section_info(1);
669
 
670
        $sectionrecord = $DB->get_record('course_sections', ['id' => $section->id]);
671
        $this->assertEquals(1, $sectionrecord->visible);
672
        $this->assertNull($section->name);
673
 
674
        $sectionactions = new sectionactions($course);
675
        $sectionactions->update($section, ['name' => 'New name', 'visible' => 0]);
676
 
677
        $sectionrecord = $DB->get_record('course_sections', ['id' => $section->id]);
678
        $this->assertEquals('New name', $sectionrecord->name);
679
        $this->assertEquals(0, $sectionrecord->visible);
680
 
681
        $section = get_fast_modinfo($course)->get_section_info(1);
682
        $this->assertEquals('New name', $section->name);
683
        $this->assertEquals(0, $section->visible);
684
    }
685
 
686
    /**
687
     * Test updating a section trigger a course section update log event.
688
     *
689
     * @covers ::update
690
     */
691
    public function test_course_section_updated_event(): void {
692
        $this->resetAfterTest();
693
 
694
        $course = $this->getDataGenerator()->create_course(
695
            ['format' => 'topics', 'numsections' => 1],
696
            ['createsections' => true]
697
        );
698
        $section = get_fast_modinfo($course)->get_section_info(1);
699
 
700
        $sink = $this->redirectEvents();
701
 
702
        $sectionactions = new sectionactions($course);
703
        $sectionactions->update($section, ['name' => 'New name', 'visible' => 0]);
704
 
705
        $events = $sink->get_events();
706
        $event = reset($events);
707
 
708
        // Check that the event data is valid.
709
        $this->assertInstanceOf('\core\event\course_section_updated', $event);
710
        $data = $event->get_data();
711
        $this->assertEquals(\context_course::instance($course->id), $event->get_context());
712
        $this->assertEquals($section->id, $data['objectid']);
713
    }
714
 
715
    /**
716
     * Test section update change the modified date.
717
     *
718
     * @covers ::update
719
     */
720
    public function test_update_time_modified(): void {
721
        global $DB;
722
        $this->resetAfterTest();
723
 
724
        // Create the course with sections.
725
        $course = $this->getDataGenerator()->create_course(
726
            ['format' => 'topics', 'numsections' => 1],
727
            ['createsections' => true]
728
        );
729
        $section = get_fast_modinfo($course)->get_section_info(1);
730
 
731
        $sectionrecord = $DB->get_record('course_sections', ['id' => $section->id]);
732
        $oldtimemodified = $sectionrecord->timemodified;
733
 
734
        $sectionactions = new sectionactions($course);
735
 
736
        // Ensuring that the section update occurs at a different timestamp.
737
        $this->waitForSecond();
738
 
739
        // The timemodified should only be updated if the section is actually updated.
740
        $result = $sectionactions->update($section, []);
741
        $this->assertFalse($result);
742
        $sectionrecord = $DB->get_record('course_sections', ['id' => $section->id]);
743
        $this->assertEquals($oldtimemodified, $sectionrecord->timemodified);
744
 
745
        // Now update something to prove timemodified changes.
746
        $result = $sectionactions->update($section, ['name' => 'New name']);
747
        $this->assertTrue($result);
748
        $sectionrecord = $DB->get_record('course_sections', ['id' => $section->id]);
749
        $this->assertGreaterThan($oldtimemodified, $sectionrecord->timemodified);
750
    }
751
 
752
    /**
753
     * Test section updating visibility will hide or show section activities.
754
     *
755
     * @covers ::update
756
     */
757
    public function test_update_hide_section_activities(): void {
758
        global $DB;
759
        $this->resetAfterTest();
760
 
761
        // Create 4 activities (visible, visible, hidden, hidden).
762
        $course = $this->getDataGenerator()->create_course(
763
            ['format' => 'topics', 'numsections' => 1],
764
            ['createsections' => true]
765
        );
766
        $activity1 = $this->getDataGenerator()->create_module(
767
            'assign',
768
            ['course' => $course->id, 'section' => 1]
769
        );
770
        $activity2 = $this->getDataGenerator()->create_module(
771
            'assign',
772
            ['course' => $course->id, 'section' => 1]
773
        );
774
        $activity3 = $this->getDataGenerator()->create_module(
775
            'assign',
776
            ['course' => $course->id, 'section' => 1, 'visible' => 0]
777
        );
778
        $activity4 = $this->getDataGenerator()->create_module(
779
            'assign',
780
            ['course' => $course->id, 'section' => 1, 'visible' => 0]
781
        );
782
 
783
        $modinfo = get_fast_modinfo($course);
784
        $cm1 = $modinfo->get_cm($activity1->cmid);
785
        $cm2 = $modinfo->get_cm($activity2->cmid);
786
        $cm3 = $modinfo->get_cm($activity3->cmid);
787
        $cm4 = $modinfo->get_cm($activity4->cmid);
788
        $this->assertEquals(1, $cm1->visible);
789
        $this->assertEquals(1, $cm2->visible);
790
        $this->assertEquals(0, $cm3->visible);
791
        $this->assertEquals(0, $cm4->visible);
792
 
793
        $sectionactions = new sectionactions($course);
794
 
795
        // Validate hidding section hides all activities.
796
        $section = $modinfo->get_section_info(1);
797
        $sectionactions->update($section, ['visible' => 0]);
798
 
799
        $modinfo = get_fast_modinfo($course);
800
        $cm1 = $modinfo->get_cm($activity1->cmid);
801
        $cm2 = $modinfo->get_cm($activity2->cmid);
802
        $cm3 = $modinfo->get_cm($activity3->cmid);
803
        $cm4 = $modinfo->get_cm($activity4->cmid);
804
        $this->assertEquals(0, $cm1->visible);
805
        $this->assertEquals(0, $cm2->visible);
806
        $this->assertEquals(0, $cm3->visible);
807
        $this->assertEquals(0, $cm4->visible);
808
 
809
        // Validate showing the section restores the previous visibility.
810
        $section = $modinfo->get_section_info(1);
811
        $sectionactions->update($section, ['visible' => 1]);
812
 
813
        $modinfo = get_fast_modinfo($course);
814
        $cm1 = $modinfo->get_cm($activity1->cmid);
815
        $cm2 = $modinfo->get_cm($activity2->cmid);
816
        $cm3 = $modinfo->get_cm($activity3->cmid);
817
        $cm4 = $modinfo->get_cm($activity4->cmid);
818
        $this->assertEquals(1, $cm1->visible);
819
        $this->assertEquals(1, $cm2->visible);
820
        $this->assertEquals(0, $cm3->visible);
821
        $this->assertEquals(0, $cm4->visible);
822
 
823
        // Swap two activities visibility to alter visible values.
824
        set_coursemodule_visible($cm2->id, 0, 0, true);
825
        set_coursemodule_visible($cm4->id, 1, 1, true);
826
 
827
        $modinfo = get_fast_modinfo($course);
828
        $cm1 = $modinfo->get_cm($activity1->cmid);
829
        $cm2 = $modinfo->get_cm($activity2->cmid);
830
        $cm3 = $modinfo->get_cm($activity3->cmid);
831
        $cm4 = $modinfo->get_cm($activity4->cmid);
832
        $this->assertEquals(1, $cm1->visible);
833
        $this->assertEquals(0, $cm2->visible);
834
        $this->assertEquals(0, $cm3->visible);
835
        $this->assertEquals(1, $cm4->visible);
836
 
837
        // Validate hidding the section again.
838
        $section = $modinfo->get_section_info(1);
839
        $sectionactions->update($section, ['visible' => 0]);
840
 
841
        $modinfo = get_fast_modinfo($course);
842
        $cm1 = $modinfo->get_cm($activity1->cmid);
843
        $cm2 = $modinfo->get_cm($activity2->cmid);
844
        $cm3 = $modinfo->get_cm($activity3->cmid);
845
        $cm4 = $modinfo->get_cm($activity4->cmid);
846
        $this->assertEquals(0, $cm1->visible);
847
        $this->assertEquals(0, $cm2->visible);
848
        $this->assertEquals(0, $cm3->visible);
849
        $this->assertEquals(0, $cm4->visible);
850
 
851
        // Validate showing the section once more to check previous state is restored.
852
        $section = $modinfo->get_section_info(1);
853
        $sectionactions->update($section, ['visible' => 1]);
854
 
855
        $modinfo = get_fast_modinfo($course);
856
        $cm1 = $modinfo->get_cm($activity1->cmid);
857
        $cm2 = $modinfo->get_cm($activity2->cmid);
858
        $cm3 = $modinfo->get_cm($activity3->cmid);
859
        $cm4 = $modinfo->get_cm($activity4->cmid);
860
        $this->assertEquals(1, $cm1->visible);
861
        $this->assertEquals(0, $cm2->visible);
862
        $this->assertEquals(0, $cm3->visible);
863
        $this->assertEquals(1, $cm4->visible);
864
    }
865
 
866
    /**
867
     * Test that the preprocess_section_name method can alter the section rename value.
868
     *
869
     * @covers ::update
870
     * @covers ::preprocess_delegated_section_fields
871
     */
872
    public function test_preprocess_section_name(): void {
873
        global $DB, $CFG;
874
        $this->resetAfterTest();
875
 
876
        require_once($CFG->libdir . '/tests/fixtures/sectiondelegatetest.php');
877
 
878
        $course = $this->getDataGenerator()->create_course();
879
 
880
        $sectionactions = new sectionactions($course);
881
        $section = $sectionactions->create_delegated('test_component', 1);
882
 
883
        $result = $sectionactions->update($section, ['name' => 'new_name']);
884
        $this->assertTrue($result);
885
 
886
        $section = $DB->get_record('course_sections', ['id' => $section->id]);
887
        $this->assertEquals('new_name_suffix', $section->name);
888
 
889
        $sectioninfo = get_fast_modinfo($course->id)->get_section_info_by_id($section->id);
890
        $this->assertEquals('new_name_suffix', $sectioninfo->name);
891
 
892
        // Validate null name.
893
        $section = $sectionactions->create_delegated('test_component', 1, (object)['name' => 'sample']);
894
 
895
        $result = $sectionactions->update($section, ['name' => null]);
896
        $this->assertTrue($result);
897
 
898
        $section = $DB->get_record('course_sections', ['id' => $section->id]);
899
        $this->assertEquals('null_name', $section->name);
900
 
901
        $sectioninfo = get_fast_modinfo($course->id)->get_section_info_by_id($section->id);
902
        $this->assertEquals('null_name', $sectioninfo->name);
903
    }
904
}