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 tool_usertours;
18
 
19
/**
20
 * Tests for helper.
21
 *
22
 * @package    tool_usertours
23
 * @category   test
24
 * @copyright  2022 Huong Nguyen <huongnv13@gmail.com>
25
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26
 * @covers \tool_usertours\helper
27
 * @covers \tool_usertours\hook\before_serverside_filter_fetch
28
 * @covers \tool_usertours\hook\before_clientside_filter_fetch
29
 */
30
final class helper_test extends \advanced_testcase {
31
    /**
32
     * Data Provider for get_string_from_input.
33
     *
34
     * @return array
35
     */
36
    public static function get_string_from_input_provider(): array {
37
        return [
38
            'Text'  => [
39
                'example',
40
                'example',
41
            ],
42
            'Text which looks like a langstring' => [
43
                'example,fakecomponent',
44
                'example,fakecomponent',
45
            ],
46
            'Text which is a langstring' => [
47
                'administration,core',
48
                'Administration',
49
            ],
50
            'Text which is a langstring but uses "moodle" instead of "core"' => [
51
                'administration,moodle',
52
                'Administration',
53
            ],
54
            'Text which is a langstring, but with extra whitespace' => [
55
                '  administration,moodle  ',
56
                'Administration',
57
            ],
58
            'Looks like a langstring, but has incorrect space around comma' => [
59
                'administration , moodle',
60
                'administration , moodle',
61
            ],
62
        ];
63
    }
64
 
65
    /**
66
     * Ensure that the get_string_from_input function returns langstring strings correctly.
67
     *
68
     * @dataProvider get_string_from_input_provider
69
     * @param string $string The string to test
70
     * @param string $expected The expected result
71
     */
72
    public function test_get_string_from_input($string, $expected): void {
73
        $this->assertEquals($expected, helper::get_string_from_input($string));
74
    }
75
 
76
    public function test_get_all_filters(): void {
77
        $filters = helper::get_all_filters();
78
        $this->assertIsArray($filters);
79
 
80
        array_map(
81
            function ($filter) {
82
                $this->assertIsString($filter);
83
                $this->assertTrue(class_exists($filter));
84
                $this->assertTrue(is_a($filter, \tool_usertours\local\filter\base::class, true));
85
                $rc = new \ReflectionClass($filter);
86
                $this->assertTrue($rc->isInstantiable());
87
            },
88
            $filters,
89
        );
90
 
91
        $this->assertNotContains(\tool_usertours\test\hook\serverside_filter_fixture::class, $filters);
92
        $this->assertNotContains(\tool_usertours\test\hook\clientside_filter_fixture::class, $filters);
93
        $this->assertContains(\tool_usertours\local\filter\accessdate::class, $filters);
94
        $this->assertContains(\tool_usertours\local\clientside_filter\cssselector::class, $filters);
95
 
96
        $filters = helper::get_all_clientside_filters();
97
        array_map(
98
            function ($filter) {
99
                $this->assertIsString($filter);
100
            },
101
            $filters,
102
        );
103
    }
104
 
105
    public function test_get_invalid_server_filter(): void {
106
        \core\di::set(
107
            \core\hook\manager::class,
108
            \core\hook\manager::phpunit_get_instance([
109
                'test_plugin1' => __DIR__ . '/fixtures/invalid_serverside_hook_fixture.php',
110
            ]),
111
        );
112
 
113
        $this->expectException(\coding_exception::class);
114
        helper::get_all_filters();
115
    }
116
 
117
    public function test_clientside_filter_for_serverside_hook(): void {
118
        \core\di::set(
119
            \core\hook\manager::class,
120
            \core\hook\manager::phpunit_get_instance([
121
                'test_plugin1' => __DIR__ . '/fixtures/clientside_filter_for_serverside_hook.php',
122
            ]),
123
        );
124
 
125
        $this->expectException(\coding_exception::class);
126
        helper::get_all_filters();
127
    }
128
 
129
    public function test_serverside_filter_for_clientside_hook(): void {
130
        \core\di::set(
131
            \core\hook\manager::class,
132
            \core\hook\manager::phpunit_get_instance([
133
                'test_plugin1' => __DIR__ . '/fixtures/serverside_filter_for_clientside_hook.php',
134
            ]),
135
        );
136
 
137
        $this->expectException(\coding_exception::class);
138
        helper::get_all_clientside_filters();
139
    }
140
 
141
    public function test_filter_hooks(): void {
142
        \core\di::set(
143
            \core\hook\manager::class,
144
            \core\hook\manager::phpunit_get_instance([
145
                'test_plugin1' => __DIR__ . '/fixtures/hook_fixtures.php',
146
            ]),
147
        );
148
 
149
        $filters = helper::get_all_filters();
150
        $this->assertIsArray($filters);
151
 
152
        // Check the modifications from the serverside hook.
153
        $this->assertContains(\tool_usertours\test\hook\serverside_filter_fixture::class, $filters);
154
        $this->assertNotContains(\tool_usertours\test\hook\another_clientside_filter_fixture::class, $filters);
155
        $this->assertNotContains(\tool_usertours\local\filter\accessdate::class, $filters);
156
 
157
        // Check the modifications from the clientside hook.
158
        $this->assertContains(\tool_usertours\test\hook\clientside_filter_fixture::class, $filters);
159
        $this->assertNotContains(\tool_usertours\test\hook\another_serverside_filter_fixture::class, $filters);
160
        $this->assertNotContains(\tool_usertours\local\clientside_filter\cssselector::class, $filters);
161
 
162
        array_map(
163
            function ($filter) {
164
                $this->assertIsString($filter);
165
                $this->assertTrue(class_exists($filter));
166
                $this->assertTrue(is_a($filter, \tool_usertours\local\filter\base::class, true));
167
                $rc = new \ReflectionClass($filter);
168
                $this->assertTrue($rc->isInstantiable());
169
            },
170
            $filters,
171
        );
172
    }
173
 
174
    public function test_get_clientside_filter_module_names(): void {
175
        \core\di::set(
176
            \core\hook\manager::class,
177
            \core\hook\manager::phpunit_get_instance([
178
                'test_plugin1' => __DIR__ . '/fixtures/invalid_clientside_hook_fixture.php',
179
            ]),
180
        );
181
 
182
        $filters = helper::get_all_clientside_filters();
183
 
184
        $this->expectException(\coding_exception::class);
185
        $this->expectExceptionMessageMatches('/Could not determine component/');
186
        helper::get_clientside_filter_module_names($filters);
187
    }
188
}