Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 1
<?php
2
// This file is part of Moodle - https://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 <https://www.gnu.org/licenses/>.
16
 
17
namespace core\hook;
18
 
19
/**
20
 * Test hook for external routing
21
 *
22
 * @coversDefaultClass \core_course\hook\before_course_viewed
23
 *
24
 * @package    core
25
 * @copyright  2024 Jacob Viertel
26
 * @license   https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27
 */
28
final class before_course_viewed_test extends \advanced_testcase {
29
    /**
30
     * Test hook description.
31
     * @covers ::get_hook_description
32
     */
33
    public function test_hook_description(): void {
34
        $description = \core_course\hook\before_course_viewed::get_hook_description();
35
        $this->assertIsString($description);
36
        $this->assertSame('Hook dispatched just before viewing a course in course/view.php.', $description);
37
    }
38
 
39
    /**
40
     * Test hook tags.
41
     * @covers ::get_hook_tags
42
     */
43
    public function test_hook_tags(): void {
44
        $tags = \core_course\hook\before_course_viewed::get_hook_tags();
45
        $this->assertIsArray($tags);
46
        $this->assertContains('course', $tags);
47
        $this->assertContains('view', $tags);
48
        $this->assertContains('routing', $tags);
49
        $this->assertContains('navigation', $tags);
50
    }
51
 
52
    /**
53
     * Test hook initialization with course data.
54
     * @covers ::__construct
55
     */
56
    public function test_hook_initialization(): void {
57
        $course = new \stdClass();
58
        $course->id = 1;
59
        $course->fullname = 'Test Course';
60
 
61
        $hook = new \core_course\hook\before_course_viewed($course);
62
 
63
        $this->assertInstanceOf(\core_course\hook\before_course_viewed::class, $hook);
64
        $this->assertSame($course, $hook->course);
65
    }
66
 
67
    /**
68
     * Test hook dispatch and propagation.
69
     * @covers \core\hook\manager::dispatch
70
     */
71
    public function test_hook_dispatch(): void {
72
        $course = new \stdClass();
73
        $course->id = 1;
74
        $course->fullname = 'Test Course';
75
 
76
        $hook = new \core_course\hook\before_course_viewed($course);
77
 
78
        $count = 0;
79
        $receivedhook = null;
80
        $testcallback = function (\core_course\hook\before_course_viewed $hook) use (&$receivedhook, &$count): void {
81
            $count++;
82
            $receivedhook = $hook;
83
        };
84
        $this->redirectHook(\core_course\hook\before_course_viewed::class, $testcallback);
85
 
86
        \core\hook\manager::get_instance()->dispatch($hook);
87
 
88
        $this->assertSame(1, $count);
89
        $this->assertSame($hook, $receivedhook);
90
    }
91
}