Proyectos de Subversion Moodle

Rev

Ir a la última revisión | | 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;
18
 
19
use phpunit_util;
20
 
21
/**
22
 * Test basic_testcase extra features and PHPUnit Moodle integration.
23
 *
24
 * @package    core
25
 * @category   test
26
 * @copyright  2012 Petr Skoda {@link http://skodak.org}
27
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28
 */
29
class basic_test extends \basic_testcase {
30
    protected $testassertexecuted = false;
31
 
32
    protected function setUp(): void {
33
        parent::setUp();
34
        if ($this->getName() === 'test_setup_assert') {
35
            $this->assertTrue(true);
36
            $this->testassertexecuted = true;
37
            return;
38
        }
39
    }
40
 
41
    /**
42
     * Tests that bootstrapping has occurred correctly
43
     * @return void
44
     */
45
    public function test_bootstrap() {
46
        global $CFG;
47
 
48
        // The httpswwwroot has been deprecated, we keep it as an alias for backwards compatibility with plugins only.
49
        $this->assertTrue(isset($CFG->httpswwwroot));
50
        $this->assertEquals($CFG->httpswwwroot, $CFG->wwwroot);
51
        $this->assertEquals($CFG->prefix, $CFG->phpunit_prefix);
52
    }
53
 
54
    /**
55
     * This is just a verification if I understand the PHPUnit assert docs right --skodak
56
     * @return void
57
     */
58
    public function test_assert_behaviour() {
59
        // Arrays.
60
        $a = array('a', 'b', 'c');
61
        $b = array('a', 'c', 'b');
62
        $c = array('a', 'b', 'c');
63
        $d = array('a', 'b', 'C');
64
        $this->assertNotEquals($a, $b);
65
        $this->assertNotEquals($a, $d);
66
        $this->assertEquals($a, $c);
67
        $this->assertEqualsCanonicalizing($a, $b);
68
 
69
        // Objects.
70
        $a = new \stdClass();
71
        $a->x = 'x';
72
        $a->y = 'y';
73
        $b = new \stdClass(); // Switched order.
74
        $b->y = 'y';
75
        $b->x = 'x';
76
        $c = $a;
77
        $d = new \stdClass();
78
        $d->x = 'x';
79
        $d->y = 'y';
80
        $d->z = 'z';
81
        $this->assertEquals($a, $b);
82
        $this->assertNotSame($a, $b);
83
        $this->assertEquals($a, $c);
84
        $this->assertSame($a, $c);
85
        $this->assertNotEquals($a, $d);
86
 
87
        // String comparison.
88
        $this->assertEquals(1, '1');
89
        $this->assertEquals(null, '');
90
 
91
        $this->assertNotEquals(0, '');
92
        $this->assertNotEquals(null, '0');
93
        $this->assertNotEquals(array(), '');
94
 
95
        // Other comparison.
96
        $this->assertEquals(null, null);
97
        $this->assertEquals(false, null);
98
        $this->assertEquals(0, null);
99
 
100
        // Emptiness.
101
        $this->assertEmpty(0);
102
        $this->assertEmpty(0.0);
103
        $this->assertEmpty('');
104
        $this->assertEmpty('0');
105
        $this->assertEmpty(false);
106
        $this->assertEmpty(null);
107
        $this->assertEmpty(array());
108
 
109
        $this->assertNotEmpty(1);
110
        $this->assertNotEmpty(0.1);
111
        $this->assertNotEmpty(-1);
112
        $this->assertNotEmpty(' ');
113
        $this->assertNotEmpty('0 ');
114
        $this->assertNotEmpty(true);
115
        $this->assertNotEmpty(array(null));
116
        $this->assertNotEmpty(new \stdClass());
117
    }
118
 
119
    /**
120
     * Make sure there are no sloppy Windows line endings
121
     * that would break our tests.
122
     */
123
    public function test_lineendings() {
124
        $string = <<<STRING
125
a
126
b
127
STRING;
128
        $this->assertSame("a\nb", $string, 'Make sure all project files are checked out with unix line endings.');
129
 
130
    }
131
 
132
    /**
133
     * Make sure asserts in setUp() do not create problems.
134
     */
135
    public function test_setup_assert() {
136
        $this->assertTrue($this->testassertexecuted);
137
        $this->testassertexecuted = false;
138
    }
139
 
140
    /**
141
     * Test assert Tag
142
     */
143
    public function test_assert_tag() {
144
        // This should succeed.
145
        self::assertTag(['id' => 'testid'], "<div><span id='testid'></span></div>");
146
        $this->expectException(\PHPUnit\Framework\ExpectationFailedException::class);
147
        self::assertTag(['id' => 'testid'], "<div><div>");
148
    }
149
 
150
    /**
151
     * Tests for assertEqualsIgnoringWhitespace.
152
     *
153
     * @param string $expected
154
     * @param string $actual
155
     * @param bool $expectationvalid
156
     * @dataProvider equals_ignoring_whitespace_provider
157
     */
158
    public function test_assertEqualsIgnoringWhitespace( // phpcs:ignore
159
        string $expected,
160
        string $actual,
161
        bool $expectationvalid,
162
    ): void {
163
        if (!$expectationvalid) {
164
            $this->expectException(\PHPUnit\Framework\ExpectationFailedException::class);
165
        }
166
        self::assertEqualsIgnoringWhitespace($expected, $actual);
167
    }
168
 
169
    /**
170
     * Data provider for assertEqualsIgnoringWhitespace tests
171
     *
172
     * @return array
173
     */
174
    public static function equals_ignoring_whitespace_provider(): array {
175
        return [
176
            'equal' => ['a b c', 'a b c', true],
177
            'equal with whitespace' => ["a b c", "a\nb c", true],
178
            'equal with extra whitespace' => ["a b c", "a\nb  c", true],
179
            'whitespace missing' => ["ab c", "a\nb  c", false],
180
            'not equal' => ['a b c', 'a b d', false],
181
            'various space types' => [
182
                implode(' ', [
183
                    '20', // Regular space.
184
                    "a0", // No-Break Space (NBSP).
185
                    "80", // Ogham Space Mark.
186
                    "0", // En Quad.
187
                    "1", // Em Quad.
188
                    "2", // En Space.
189
                    "3", // Em Space.
190
                    "4", // Three-Per-Em Space.
191
                    "5", // Four-Per-Em Space.
192
                    "6", // Six-Per-Em Space.
193
                    "7", // Figure Space.
194
                    "8", // Punctuation Space.
195
                    "9", // Thin Space.
196
                    "0a", // Hair Space.
197
                    "2f", // Narrow No-Break Space (NNBSP).
198
                    "5f", // Medium Mathematical Space.
199
                    "3000", // Ideographic Space.
200
                    ".",
201
                ]),
202
                implode('', [
203
                    // All space chars taken from https://www.compart.com/en/unicode/category/Zs.
204
                    "20\u{0020}", // Regular space.
205
                    "a0\u{00a0}", // No-Break Space (NBSP).
206
                    "80\u{1680}", // Ogham Space Mark.
207
                    "0\u{2000}", // En Quad.
208
                    "1\u{2001}", // Em Quad.
209
                    "2\u{2002}", // En Space.
210
                    "3\u{2003}", // Em Space.
211
                    "4\u{2004}", // Three-Per-Em Space.
212
                    "5\u{2005}", // Four-Per-Em Space.
213
                    "6\u{2006}", // Six-Per-Em Space.
214
                    "7\u{2007}", // Figure Space.
215
                    "8\u{2008}", // Punctuation Space.
216
                    "9\u{2009}", // Thin Space.
217
                    "0a\u{200a}", // Hair Space.
218
                    "2f\u{202f}", // Narrow No-Break Space (NNBSP).
219
                    "5f\u{205f}", // Medium Mathematical Space.
220
                    "3000\u{3000}", // Ideographic Space.
221
                    ".",
222
                ]),
223
                true,
224
            ],
225
        ];
226
    }
227
 
228
    /**
229
     * Test that a database modification is detected.
230
     *
231
     * @runInSeparateProcess
232
     * @covers \phpunit_util
233
     */
234
    public function test_db_modification(): void {
235
        global $DB;
236
        $DB->set_field('user', 'confirmed', 1, ['id' => -1]);
237
 
238
        // Let's convert the user warnings into an assert-able exception.
239
        set_error_handler(
240
            static function ($errno, $errstr) {
241
                restore_error_handler();
242
                throw new \Exception($errstr, $errno);
243
            },
244
            E_USER_WARNING // Or any other specific E_ that we want to assert.
245
        );
246
 
247
        $this->expectException(\Exception::class);
248
        $this->expectExceptionMessage('Warning: unexpected database modification');
249
        phpunit_util::reset_all_data(true);
250
    }
251
 
252
    /**
253
     * Test that a $CFG modification is detected.
254
     *
255
     * @runInSeparateProcess
256
     * @covers \phpunit_util
257
     */
258
    public function test_cfg_modification(): void {
259
        global $CFG;
260
        $CFG->xx = 'yy';
261
        unset($CFG->admin);
262
        $CFG->rolesactive = 0;
263
 
264
        // Let's convert the user warnings into an assert-able exception.
265
        set_error_handler(
266
            static function ($errno, $errstr) {
267
                restore_error_handler();
268
                throw new \Exception($errstr, $errno);
269
            },
270
            E_USER_WARNING // Or any other specific E_ that we want to assert.
271
        );
272
 
273
        $this->expectException(\Exception::class);
274
        $this->expectExceptionMessageMatches('/rolesactive.*xx value.*removal.*admin/ms'); // 3 messages matched.
275
        phpunit_util::reset_all_data(true);
276
    }
277
 
278
    /**
279
     * Test that a $USER modification is detected.
280
     *
281
     * @runInSeparateProcess
282
     * @covers \phpunit_util
283
     */
284
    public function test_user_modification(): void {
285
        global $USER;
286
        $USER->id = 10;
287
 
288
        // Let's convert the user warnings into an assert-able exception.
289
        set_error_handler(
290
            static function ($errno, $errstr) {
291
                restore_error_handler();
292
                throw new \Exception($errstr, $errno);
293
            },
294
            E_USER_WARNING // Or any other specific E_ that we want to assert.
295
        );
296
 
297
        $this->expectException(\Exception::class);
298
        $this->expectExceptionMessage('Warning: unexpected change of $USER');
299
        phpunit_util::reset_all_data(true);
300
    }
301
 
302
    /**
303
     * Test that a $COURSE modification is detected.
304
     *
305
     * @runInSeparateProcess
306
     * @covers \phpunit_util
307
     */
308
    public function test_course_modification(): void {
309
        global $COURSE;
310
        $COURSE->id = 10;
311
 
312
        // Let's convert the user warnings into an assert-able exception.
313
        set_error_handler(
314
            static function ($errno, $errstr) {
315
                restore_error_handler();
316
                throw new \Exception($errstr, $errno);
317
            },
318
            E_USER_WARNING // Or any other specific E_ that we want to assert.
319
        );
320
 
321
        $this->expectException(\Exception::class);
322
        $this->expectExceptionMessage('Warning: unexpected change of $COURSE');
323
        phpunit_util::reset_all_data(true);
324
    }
325
 
326
    /**
327
     * Test that all modifications are detected together.
328
     *
329
     * @runInSeparateProcess
330
     * @covers \phpunit_util
331
     */
332
    public function test_all_modifications(): void {
333
        global $DB, $CFG, $USER, $COURSE;
334
        $DB->set_field('user', 'confirmed', 1, ['id' => -1]);
335
        $CFG->xx = 'yy';
336
        unset($CFG->admin);
337
        $CFG->rolesactive = 0;
338
        $USER->id = 10;
339
        $COURSE->id = 10;
340
 
341
        // Let's convert the user warnings into an assert-able exception.
342
        set_error_handler(
343
            static function ($errno, $errstr) {
344
                restore_error_handler();
345
                throw new \Exception($errstr, $errno);
346
            },
347
            E_USER_WARNING // Or any other specific E_ that we want to assert.
348
        );
349
 
350
        $this->expectException(\Exception::class);
351
        $this->expectExceptionMessageMatches('/resetting.*rolesactive.*new.*removal.*USER.*COURSE/ms'); // 6 messages matched.
352
        phpunit_util::reset_all_data(true);
353
    }
354
 
355
    /**
356
     * Test that an open transaction are managed ok by the reset code (silently rolled back).
357
     *
358
     * @runInSeparateProcess
359
     * @covers \phpunit_util
360
     */
361
    public function test_transaction_problem(): void {
362
        global $DB, $COURSE;
363
        $originalname = $DB->get_field('course', 'fullname', ['id' => $COURSE->id]); // Normally "PHPUnit test site".
364
        $changedname = 'Ongoing transaction test site';
365
 
366
        // Start a transaction and make some database changes.
367
        $DB->start_delegated_transaction();
368
        $DB->set_field('course', 'fullname', $changedname, ['id' => $COURSE->id]);
369
 
370
        // Assert that the transaction is open and the changes were made.
371
        $this->assertTrue($DB->is_transaction_started());
372
        $this->assertEquals($changedname, $DB->get_field('course', 'fullname', ['id' => $COURSE->id]));
373
 
374
        phpunit_util::reset_all_data(false); // We don't want to detect/warn on database changes for this test.
375
 
376
        // Assert that the transaction is now closed and the changes were rolled back.
377
        $this->assertFalse($DB->is_transaction_started());
378
        $this->assertEquals($originalname, $DB->get_field('course', 'fullname', ['id' => $COURSE->id]));
379
    }
380
}