MDL-67673 phpunit: Move tests to use new phpunit_dataset
[moodle.git] / lib / phpunit / tests / advanced_test.php
CommitLineData
5bd40408
PS
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
7aea08e1 17/**
08ad3257 18 * PHPUnit integration tests
7aea08e1
SH
19 *
20 * @package core
21 * @category phpunit
22 * @copyright 2012 Petr Skoda {@link http://skodak.org}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
5bd40408
PS
26defined('MOODLE_INTERNAL') || die();
27
4b027431 28
458b3386
PS
29/**
30 * Test advanced_testcase extra features.
31 *
32 * @package core
33 * @category phpunit
34 * @copyright 2012 Petr Skoda {@link http://skodak.org}
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 */
37class core_phpunit_advanced_testcase extends advanced_testcase {
38
ef5b5e05
PS
39 public function test_debugging() {
40 global $CFG;
41 $this->resetAfterTest();
42
43 debugging('hokus');
44 $this->assertDebuggingCalled();
45 debugging('pokus');
46 $this->assertDebuggingCalled('pokus');
47 debugging('pokus', DEBUG_MINIMAL);
48 $this->assertDebuggingCalled('pokus', DEBUG_MINIMAL);
49 $this->assertDebuggingNotCalled();
50
51 debugging('a');
52 debugging('b', DEBUG_MINIMAL);
53 debugging('c', DEBUG_DEVELOPER);
54 $debuggings = $this->getDebuggingMessages();
3405d82c 55 $this->assertCount(3, $debuggings);
ef5b5e05
PS
56 $this->assertSame('a', $debuggings[0]->message);
57 $this->assertSame(DEBUG_NORMAL, $debuggings[0]->level);
58 $this->assertSame('b', $debuggings[1]->message);
59 $this->assertSame(DEBUG_MINIMAL, $debuggings[1]->level);
60 $this->assertSame('c', $debuggings[2]->message);
61 $this->assertSame(DEBUG_DEVELOPER, $debuggings[2]->level);
62
63 $this->resetDebugging();
64 $this->assertDebuggingNotCalled();
65 $debuggings = $this->getDebuggingMessages();
3405d82c 66 $this->assertCount(0, $debuggings);
ef5b5e05 67
96f81ea3 68 set_debugging(DEBUG_NONE);
ef5b5e05
PS
69 debugging('hokus');
70 $this->assertDebuggingNotCalled();
96f81ea3 71 set_debugging(DEBUG_DEVELOPER);
ef5b5e05
PS
72 }
73
ef151737
RS
74 /**
75 * @test
76 *
77 * Annotations are a valid PHPUnit method for running tests. Debugging needs to support them.
78 */
79 public function debugging_called_with_annotation() {
80 debugging('pokus', DEBUG_MINIMAL);
81 $this->assertDebuggingCalled('pokus', DEBUG_MINIMAL);
82 }
83
458b3386 84 public function test_set_user() {
faea2413
RT
85 global $USER, $DB, $SESSION;
86
87 $this->resetAfterTest();
458b3386
PS
88
89 $this->assertEquals(0, $USER->id);
90 $this->assertSame($_SESSION['USER'], $USER);
2e00d01d 91 $this->assertSame($GLOBALS['USER'], $USER);
458b3386
PS
92
93 $user = $DB->get_record('user', array('id'=>2));
d59ef9c5 94 $this->assertNotEmpty($user);
458b3386
PS
95 $this->setUser($user);
96 $this->assertEquals(2, $USER->id);
97 $this->assertEquals(2, $_SESSION['USER']->id);
98 $this->assertSame($_SESSION['USER'], $USER);
2e00d01d 99 $this->assertSame($GLOBALS['USER'], $USER);
458b3386
PS
100
101 $USER->id = 3;
102 $this->assertEquals(3, $USER->id);
103 $this->assertEquals(3, $_SESSION['USER']->id);
104 $this->assertSame($_SESSION['USER'], $USER);
2e00d01d 105 $this->assertSame($GLOBALS['USER'], $USER);
458b3386 106
d79d5ac2 107 \core\session\manager::set_user($user);
458b3386
PS
108 $this->assertEquals(2, $USER->id);
109 $this->assertEquals(2, $_SESSION['USER']->id);
110 $this->assertSame($_SESSION['USER'], $USER);
2e00d01d 111 $this->assertSame($GLOBALS['USER'], $USER);
458b3386
PS
112
113 $USER = $DB->get_record('user', array('id'=>1));
d59ef9c5 114 $this->assertNotEmpty($USER);
458b3386
PS
115 $this->assertEquals(1, $USER->id);
116 $this->assertEquals(1, $_SESSION['USER']->id);
117 $this->assertSame($_SESSION['USER'], $USER);
2e00d01d 118 $this->assertSame($GLOBALS['USER'], $USER);
458b3386
PS
119
120 $this->setUser(null);
121 $this->assertEquals(0, $USER->id);
122 $this->assertSame($_SESSION['USER'], $USER);
2e00d01d 123 $this->assertSame($GLOBALS['USER'], $USER);
faea2413
RT
124
125 // Ensure session is reset after setUser, as it may contain extra info.
126 $SESSION->sometestvalue = true;
127 $this->setUser($user);
128 $this->assertObjectNotHasAttribute('sometestvalue', $SESSION);
458b3386 129 }
3c4c5189 130
d59ef9c5
PS
131 public function test_set_admin_user() {
132 global $USER;
133
3405d82c 134 $this->resetAfterTest();
d59ef9c5
PS
135
136 $this->setAdminUser();
137 $this->assertEquals($USER->id, 2);
138 $this->assertTrue(is_siteadmin());
139 }
140
141 public function test_set_guest_user() {
142 global $USER;
143
3405d82c 144 $this->resetAfterTest();
d59ef9c5
PS
145
146 $this->setGuestUser();
147 $this->assertEquals($USER->id, 1);
148 $this->assertTrue(isguestuser());
149 }
150
a0c5affe 151 public function test_database_reset() {
8b5413cc
PS
152 global $DB;
153
3405d82c 154 $this->resetAfterTest();
8b5413cc
PS
155
156 $this->preventResetByRollback();
157
3405d82c 158 $this->assertEquals(1, $DB->count_records('course')); // Only frontpage in new site.
8b5413cc 159
3405d82c 160 // This is weird table - id is NOT a sequence here.
8b5413cc
PS
161 $this->assertEquals(0, $DB->count_records('context_temp'));
162 $DB->import_record('context_temp', array('id'=>5, 'path'=>'/1/2', 'depth'=>2));
163 $record = $DB->get_record('context_temp', array());
164 $this->assertEquals(5, $record->id);
165
2c3d9868
DP
166 $this->assertEquals(0, $DB->count_records('user_preferences'));
167 $originaldisplayid = $DB->insert_record('user_preferences', array('userid'=>2, 'name'=> 'phpunittest', 'value'=>'x'));
d2c58b95 168 $this->assertEquals(1, $DB->count_records('user_preferences'));
8b5413cc 169
d2c58b95 170 $numcourses = $DB->count_records('course');
8b5413cc 171 $course = $this->getDataGenerator()->create_course();
d2c58b95 172 $this->assertEquals($numcourses + 1, $DB->count_records('course'));
8b5413cc
PS
173
174 $this->assertEquals(2, $DB->count_records('user'));
175 $DB->delete_records('user', array('id'=>1));
176 $this->assertEquals(1, $DB->count_records('user'));
177
8b5413cc
PS
178 $this->resetAllData();
179
3405d82c
PS
180 $this->assertEquals(1, $DB->count_records('course')); // Only frontpage in new site.
181 $this->assertEquals(0, $DB->count_records('context_temp')); // Only frontpage in new site.
d2c58b95
RS
182
183 $numcourses = $DB->count_records('course');
8b5413cc 184 $course = $this->getDataGenerator()->create_course();
d2c58b95 185 $this->assertEquals($numcourses + 1, $DB->count_records('course'));
8b5413cc 186
2c3d9868 187 $displayid = $DB->insert_record('user_preferences', array('userid'=>2, 'name'=> 'phpunittest', 'value'=>'x'));
8b5413cc
PS
188 $this->assertEquals($originaldisplayid, $displayid);
189
190 $this->assertEquals(2, $DB->count_records('user'));
191 $DB->delete_records('user', array('id'=>2));
192 $user = $this->getDataGenerator()->create_user();
d2c58b95
RS
193 $this->assertEquals(2, $DB->count_records('user'));
194 $this->assertGreaterThan(2, $user->id);
8b5413cc 195
8b5413cc
PS
196 $this->resetAllData();
197
d2c58b95 198 $numcourses = $DB->count_records('course');
8b5413cc 199 $course = $this->getDataGenerator()->create_course();
d2c58b95 200 $this->assertEquals($numcourses + 1, $DB->count_records('course'));
8b5413cc
PS
201
202 $this->assertEquals(2, $DB->count_records('user'));
203 $DB->delete_records('user', array('id'=>2));
204
8b5413cc
PS
205 $this->resetAllData();
206
d2c58b95 207 $numcourses = $DB->count_records('course');
8b5413cc 208 $course = $this->getDataGenerator()->create_course();
d2c58b95 209 $this->assertEquals($numcourses + 1, $DB->count_records('course'));
8b5413cc
PS
210
211 $this->assertEquals(2, $DB->count_records('user'));
212 }
213
a0c5affe
PS
214 public function test_change_detection() {
215 global $DB, $CFG, $COURSE, $SITE, $USER;
216
217 $this->preventResetByRollback();
5ecf8e8f 218 self::resetAllData(true);
a0c5affe 219
3405d82c 220 // Database change.
a0c5affe
PS
221 $this->assertEquals(1, $DB->get_field('user', 'confirmed', array('id'=>2)));
222 $DB->set_field('user', 'confirmed', 0, array('id'=>2));
223 try {
5ecf8e8f 224 self::resetAllData(true);
a0c5affe 225 } catch (Exception $e) {
801a372d 226 $this->assertInstanceOf('PHPUnit\Framework\Error\Warning', $e);
a0c5affe
PS
227 }
228 $this->assertEquals(1, $DB->get_field('user', 'confirmed', array('id'=>2)));
229
3405d82c 230 // Config change.
a0c5affe
PS
231 $CFG->xx = 'yy';
232 unset($CFG->admin);
233 $CFG->rolesactive = 0;
234 try {
5ecf8e8f 235 self::resetAllData(true);
a0c5affe 236 } catch (Exception $e) {
801a372d 237 $this->assertInstanceOf('PHPUnit\Framework\Error\Warning', $e);
35bc26b5
EL
238 $this->assertStringContainsString('xx', $e->getMessage());
239 $this->assertStringContainsString('admin', $e->getMessage());
240 $this->assertStringContainsString('rolesactive', $e->getMessage());
a0c5affe
PS
241 }
242 $this->assertFalse(isset($CFG->xx));
243 $this->assertTrue(isset($CFG->admin));
244 $this->assertEquals(1, $CFG->rolesactive);
245
596ea56f
JP
246 // _GET change.
247 $_GET['__somethingthatwillnotnormallybepresent__'] = 'yy';
5ecf8e8f 248 self::resetAllData(true);
596ea56f
JP
249
250 $this->assertEquals(array(), $_GET);
251
596ea56f
JP
252 // _POST change.
253 $_POST['__somethingthatwillnotnormallybepresent2__'] = 'yy';
5ecf8e8f 254 self::resetAllData(true);
596ea56f
JP
255 $this->assertEquals(array(), $_POST);
256
257 // _FILES change.
258 $_FILES['__somethingthatwillnotnormallybepresent3__'] = 'yy';
5ecf8e8f 259 self::resetAllData(true);
596ea56f
JP
260 $this->assertEquals(array(), $_FILES);
261
262 // _REQUEST change.
263 $_REQUEST['__somethingthatwillnotnormallybepresent4__'] = 'yy';
5ecf8e8f 264 self::resetAllData(true);
596ea56f
JP
265 $this->assertEquals(array(), $_REQUEST);
266
3405d82c 267 // Silent changes.
a0c5affe 268 $_SERVER['xx'] = 'yy';
5ecf8e8f 269 self::resetAllData(true);
a0c5affe
PS
270 $this->assertFalse(isset($_SERVER['xx']));
271
3405d82c 272 // COURSE change.
a0c5affe
PS
273 $SITE->id = 10;
274 $COURSE = new stdClass();
275 $COURSE->id = 7;
276 try {
5ecf8e8f 277 self::resetAllData(true);
a0c5affe 278 } catch (Exception $e) {
801a372d 279 $this->assertInstanceOf('PHPUnit\Framework\Error\Warning', $e);
a0c5affe
PS
280 $this->assertEquals(1, $SITE->id);
281 $this->assertSame($SITE, $COURSE);
282 $this->assertSame($SITE, $COURSE);
283 }
284
3405d82c 285 // USER change.
a0c5affe
PS
286 $this->setUser(2);
287 try {
5ecf8e8f 288 self::resetAllData(true);
a0c5affe 289 } catch (Exception $e) {
801a372d 290 $this->assertInstanceOf('PHPUnit\Framework\Error\Warning', $e);
a0c5affe
PS
291 $this->assertEquals(0, $USER->id);
292 }
293 }
294
3c4c5189
PS
295 public function test_getDataGenerator() {
296 $generator = $this->getDataGenerator();
5c3c2c81 297 $this->assertInstanceOf('testing_data_generator', $generator);
3c4c5189 298 }
95dcf965
PS
299
300 public function test_database_mock1() {
301 global $DB;
302
303 try {
304 $DB->get_record('pokus', array());
305 $this->fail('Exception expected when accessing non existent table');
3405d82c
PS
306 } catch (moodle_exception $e) {
307 $this->assertInstanceOf('dml_exception', $e);
95dcf965 308 }
52f3e060 309 $DB = $this->createMock(get_class($DB));
95dcf965 310 $this->assertNull($DB->get_record('pokus', array()));
3405d82c 311 // Rest continues after reset.
95dcf965
PS
312 }
313
314 public function test_database_mock2() {
315 global $DB;
316
3405d82c 317 // Now the database should be back to normal.
95dcf965
PS
318 $this->assertFalse($DB->get_record('user', array('id'=>9999)));
319 }
e396e8ed 320
1f13dff1 321 public function test_load_data_dataset_xml() {
e396e8ed
PS
322 global $DB;
323
3405d82c 324 $this->resetAfterTest();
e396e8ed 325
1f13dff1
EL
326 $this->assertFalse($DB->record_exists('user', array('id' => 5)));
327 $this->assertFalse($DB->record_exists('user', array('id' => 7)));
e396e8ed
PS
328 $dataset = $this->createXMLDataSet(__DIR__.'/fixtures/sample_dataset.xml');
329 $this->loadDataSet($dataset);
1f13dff1
EL
330 $this->assertTrue($DB->record_exists('user', array('id' => 5)));
331 $this->assertTrue($DB->record_exists('user', array('id' => 7)));
332 $user5 = $DB->get_record('user', array('id' => 5));
333 $user7 = $DB->get_record('user', array('id' => 7));
334 $this->assertSame('bozka.novakova', $user5->username);
335 $this->assertSame('pepa.novak', $user7->username);
336
337 }
338
339 public function test_load_dataset_csv() {
340 global $DB;
341
342 $this->resetAfterTest();
343
344 $this->assertFalse($DB->record_exists('user', array('id' => 8)));
345 $this->assertFalse($DB->record_exists('user', array('id' => 9)));
346 $dataset = $this->createCsvDataSet(array('user' => __DIR__.'/fixtures/sample_dataset.csv'));
e396e8ed 347 $this->loadDataSet($dataset);
1f13dff1
EL
348 $this->assertEquals(5, $DB->get_field('user', 'id', array('username' => 'bozka.novakova')));
349 $this->assertEquals(7, $DB->get_field('user', 'id', array('username' => 'pepa.novak')));
350
351 }
352
353 public function test_load_dataset_array() {
354 global $DB;
355
356 $this->resetAfterTest();
e396e8ed
PS
357
358 $data = array(
359 'user' => array(
360 array('username', 'email'),
361 array('top.secret', 'top@example.com'),
362 array('low.secret', 'low@example.com'),
363 ),
364 );
1f13dff1
EL
365
366 $this->assertFalse($DB->record_exists('user', array('email' => 'top@example.com')));
367 $this->assertFalse($DB->record_exists('user', array('email' => 'low@example.com')));
e396e8ed
PS
368 $dataset = $this->createArrayDataSet($data);
369 $this->loadDataSet($dataset);
1f13dff1
EL
370 $this->assertTrue($DB->record_exists('user', array('email' => 'top@example.com')));
371 $this->assertTrue($DB->record_exists('user', array('email' => 'low@example.com')));
e396e8ed
PS
372
373 $data = array(
374 'user' => array(
1f13dff1
EL
375 array('username' => 'noidea', 'email' => 'noidea@example.com'),
376 array('username' => 'onemore', 'email' => 'onemore@example.com'),
e396e8ed
PS
377 ),
378 );
379 $dataset = $this->createArrayDataSet($data);
380 $this->loadDataSet($dataset);
1f13dff1
EL
381 $this->assertTrue($DB->record_exists('user', array('username' => 'noidea')));
382 $this->assertTrue($DB->record_exists('user', array('username' => 'onemore')));
e396e8ed 383 }
4c9e03f0 384
2a67e105
PS
385 public function test_assert_time_current() {
386 $this->assertTimeCurrent(time());
387
388 $this->setCurrentTimeStart();
389 $this->assertTimeCurrent(time());
74ee9d29 390 $this->waitForSecond();
2a67e105
PS
391 $this->assertTimeCurrent(time());
392 $this->assertTimeCurrent(time()-1);
393
394 try {
395 $this->setCurrentTimeStart();
396 $this->assertTimeCurrent(time()+10);
397 $this->fail('Failed assert expected');
398 } catch (Exception $e) {
801a372d 399 $this->assertInstanceOf('PHPUnit\Framework\ExpectationFailedException', $e);
2a67e105
PS
400 }
401
402 try {
403 $this->setCurrentTimeStart();
404 $this->assertTimeCurrent(time()-10);
405 $this->fail('Failed assert expected');
406 } catch (Exception $e) {
801a372d 407 $this->assertInstanceOf('PHPUnit\Framework\ExpectationFailedException', $e);
2a67e105
PS
408 }
409 }
410
ed23ad31
YB
411 public function test_message_processors_reset() {
412 global $DB;
413
414 $this->resetAfterTest(true);
415
416 // Get all processors first.
417 $processors1 = get_message_processors();
418
419 // Add a new message processor and get all processors again.
420 $processor = new stdClass();
421 $processor->name = 'test_processor';
422 $processor->enabled = 1;
423 $DB->insert_record('message_processors', $processor);
424
425 $processors2 = get_message_processors();
426
427 // Assert that new processor still haven't been added to the list.
428 $this->assertSame($processors1, $processors2);
429
430 // Reset message processors data.
431 $processors3 = get_message_processors(false, true);
432 // Now, list of processors should not be the same any more,
433 // And we should have one more message processor in the list.
434 $this->assertNotSame($processors1, $processors3);
435 $this->assertEquals(count($processors1) + 1, count($processors3));
436 }
437
4c9e03f0 438 public function test_message_redirection() {
4c9e03f0
PS
439 $this->preventResetByRollback(); // Messaging is not compatible with transactions...
440 $this->resetAfterTest(false);
441
442 $user1 = $this->getDataGenerator()->create_user();
443 $user2 = $this->getDataGenerator()->create_user();
444
445 // Any core message will do here.
cc350fd9
AD
446 $message1 = new \core\message\message();
447 $message1->courseid = 1;
4c9e03f0
PS
448 $message1->component = 'moodle';
449 $message1->name = 'instantmessage';
450 $message1->userfrom = $user1;
451 $message1->userto = $user2;
452 $message1->subject = 'message subject 1';
453 $message1->fullmessage = 'message body';
454 $message1->fullmessageformat = FORMAT_MARKDOWN;
455 $message1->fullmessagehtml = '<p>message body</p>';
456 $message1->smallmessage = 'small message';
5e12b369 457 $message1->notification = 0;
4c9e03f0 458
cc350fd9
AD
459 $message2 = new \core\message\message();
460 $message2->courseid = 1;
4c9e03f0
PS
461 $message2->component = 'moodle';
462 $message2->name = 'instantmessage';
463 $message2->userfrom = $user2;
464 $message2->userto = $user1;
465 $message2->subject = 'message subject 2';
466 $message2->fullmessage = 'message body';
467 $message2->fullmessageformat = FORMAT_MARKDOWN;
468 $message2->fullmessagehtml = '<p>message body</p>';
469 $message2->smallmessage = 'small message';
5e12b369 470 $message2->notification = 0;
4c9e03f0
PS
471
472 // There should be debugging message without redirection.
fc05c1b8
AN
473 $mailsink = $this->redirectEmails();
474 $mailsink->close();
4c9e03f0
PS
475 message_send($message1);
476 $this->assertDebuggingCalled(null, null, 'message_send() must print debug message that messaging is disabled in phpunit tests.');
477
3405d82c 478 // Sink should catch messages.
4c9e03f0
PS
479 $sink = $this->redirectMessages();
480 $mid1 = message_send($message1);
481 $mid2 = message_send($message2);
482
483 $this->assertDebuggingNotCalled('message redirection must prevent debug messages from the message_send()');
484 $this->assertEquals(2, $sink->count());
485 $this->assertGreaterThanOrEqual(1, $mid1);
486 $this->assertGreaterThanOrEqual($mid1, $mid2);
487
488 $messages = $sink->get_messages();
a6674bf8 489 $this->assertIsArray($messages);
3405d82c 490 $this->assertCount(2, $messages);
4c9e03f0
PS
491 $this->assertEquals($mid1, $messages[0]->id);
492 $this->assertEquals($message1->userto->id, $messages[0]->useridto);
493 $this->assertEquals($message1->userfrom->id, $messages[0]->useridfrom);
494 $this->assertEquals($message1->smallmessage, $messages[0]->smallmessage);
495 $this->assertEquals($mid2, $messages[1]->id);
496 $this->assertEquals($message2->userto->id, $messages[1]->useridto);
497 $this->assertEquals($message2->userfrom->id, $messages[1]->useridfrom);
498 $this->assertEquals($message2->smallmessage, $messages[1]->smallmessage);
499
500 // Test resetting.
501 $sink->clear();
502 $messages = $sink->get_messages();
a6674bf8 503 $this->assertIsArray($messages);
3405d82c 504 $this->assertCount(0, $messages);
4c9e03f0
PS
505
506 message_send($message1);
507 $messages = $sink->get_messages();
a6674bf8 508 $this->assertIsArray($messages);
3405d82c 509 $this->assertCount(1, $messages);
4c9e03f0
PS
510
511 // Test closing.
512 $sink->close();
513 $messages = $sink->get_messages();
a6674bf8 514 $this->assertIsArray($messages);
3405d82c 515 $this->assertCount(1, $messages, 'Messages in sink are supposed to stay there after close');
4c9e03f0
PS
516
517 // Test debugging is enabled again.
518 message_send($message1);
519 $this->assertDebuggingCalled(null, null, 'message_send() must print debug message that messaging is disabled in phpunit tests.');
520
521 // Test invalid names and components.
522
523 $sink = $this->redirectMessages();
524
cc350fd9
AD
525 $message3 = new \core\message\message();
526 $message3->courseid = 1;
4c9e03f0
PS
527 $message3->component = 'xxxx_yyyyy';
528 $message3->name = 'instantmessage';
529 $message3->userfrom = $user2;
530 $message3->userto = $user1;
531 $message3->subject = 'message subject 2';
532 $message3->fullmessage = 'message body';
533 $message3->fullmessageformat = FORMAT_MARKDOWN;
534 $message3->fullmessagehtml = '<p>message body</p>';
535 $message3->smallmessage = 'small message';
5e12b369 536 $message3->notification = 0;
4c9e03f0 537
39d2c688
DM
538 $this->assertFalse(message_send($message3));
539 $this->assertDebuggingCalled('Attempt to send msg from a provider xxxx_yyyyy/instantmessage '.
540 'that is inactive or not allowed for the user id='.$user1->id);
4c9e03f0
PS
541
542 $message3->component = 'moodle';
543 $message3->name = 'yyyyyy';
39d2c688
DM
544
545 $this->assertFalse(message_send($message3));
546 $this->assertDebuggingCalled('Attempt to send msg from a provider moodle/yyyyyy '.
547 'that is inactive or not allowed for the user id='.$user1->id);
4c9e03f0
PS
548
549 message_send($message1);
550 $this->assertEquals(1, $sink->count());
551
552 // Test if sink can be carried over to next test.
553 $this->assertTrue(phpunit_util::is_redirecting_messages());
554 return $sink;
555 }
556
557 /**
558 * @depends test_message_redirection
559 */
560 public function test_message_redirection_noreset($sink) {
fc1785b0
AN
561 if ($this->isInIsolation()) {
562 $this->markTestSkipped('State cannot be carried over between tests in isolated tests');
563 }
564
4c9e03f0 565 $this->preventResetByRollback(); // Messaging is not compatible with transactions...
3405d82c 566 $this->resetAfterTest();
4c9e03f0
PS
567
568 $this->assertTrue(phpunit_util::is_redirecting_messages());
569 $this->assertEquals(1, $sink->count());
570
cc350fd9
AD
571 $message = new \core\message\message();
572 $message->courseid = 1;
4c9e03f0
PS
573 $message->component = 'moodle';
574 $message->name = 'instantmessage';
575 $message->userfrom = get_admin();
576 $message->userto = get_admin();
577 $message->subject = 'message subject 1';
578 $message->fullmessage = 'message body';
579 $message->fullmessageformat = FORMAT_MARKDOWN;
580 $message->fullmessagehtml = '<p>message body</p>';
581 $message->smallmessage = 'small message';
5e12b369 582 $message->notification = 0;
4c9e03f0
PS
583
584 message_send($message);
585 $this->assertEquals(2, $sink->count());
586 }
587
588 /**
589 * @depends test_message_redirection_noreset
590 */
591 public function test_message_redirection_reset() {
592 $this->assertFalse(phpunit_util::is_redirecting_messages(), 'Test reset must stop message redirection.');
593 }
d6e7a63d
PS
594
595 public function test_set_timezone() {
596 global $CFG;
597 $this->resetAfterTest();
598
599 $this->assertSame('Australia/Perth', $CFG->timezone);
600 $this->assertSame('Australia/Perth', date_default_timezone_get());
601
602 $this->setTimezone('Pacific/Auckland', 'Europe/Prague');
603 $this->assertSame('Pacific/Auckland', $CFG->timezone);
604 $this->assertSame('Pacific/Auckland', date_default_timezone_get());
605
606 $this->setTimezone('99', 'Europe/Prague');
607 $this->assertSame('99', $CFG->timezone);
608 $this->assertSame('Europe/Prague', date_default_timezone_get());
609
610 $this->setTimezone('xxx', 'Europe/Prague');
611 $this->assertSame('xxx', $CFG->timezone);
612 $this->assertSame('Europe/Prague', date_default_timezone_get());
613
614 $this->setTimezone();
615 $this->assertSame('Australia/Perth', $CFG->timezone);
616 $this->assertSame('Australia/Perth', date_default_timezone_get());
617
618 try {
619 $this->setTimezone('Pacific/Auckland', '');
620 } catch (Exception $e) {
801a372d 621 $this->assertInstanceOf('PHPUnit\Framework\Error\Warning', $e);
d6e7a63d
PS
622 }
623
624 try {
625 $this->setTimezone('Pacific/Auckland', 'xxxx');
626 } catch (Exception $e) {
801a372d 627 $this->assertInstanceOf('PHPUnit\Framework\Error\Warning', $e);
d6e7a63d
PS
628 }
629
630 try {
631 $this->setTimezone('Pacific/Auckland', null);
632 } catch (Exception $e) {
801a372d 633 $this->assertInstanceOf('PHPUnit\Framework\Error\Warning', $e);
d6e7a63d
PS
634 }
635
636 }
637
638 public function test_locale_reset() {
639 global $CFG;
640
641 $this->resetAfterTest();
642
5ecf8e8f 643 // If this fails self::resetAllData(); must be updated.
d6e7a63d
PS
644 $this->assertSame('en_AU.UTF-8', get_string('locale', 'langconfig'));
645 $this->assertSame('English_Australia.1252', get_string('localewin', 'langconfig'));
646
647 if ($CFG->ostype === 'WINDOWS') {
648 $this->assertSame('English_Australia.1252', setlocale(LC_TIME, 0));
649 setlocale(LC_TIME, 'English_USA.1252');
650 } else {
651 $this->assertSame('en_AU.UTF-8', setlocale(LC_TIME, 0));
652 setlocale(LC_TIME, 'en_US.UTF-8');
653 }
654
655 try {
5ecf8e8f 656 self::resetAllData(true);
d6e7a63d 657 } catch (Exception $e) {
801a372d 658 $this->assertInstanceOf('PHPUnit\Framework\Error\Warning', $e);
d6e7a63d
PS
659 }
660
661 if ($CFG->ostype === 'WINDOWS') {
662 $this->assertSame('English_Australia.1252', setlocale(LC_TIME, 0));
663 } else {
664 $this->assertSame('en_AU.UTF-8', setlocale(LC_TIME, 0));
665 }
666
667 if ($CFG->ostype === 'WINDOWS') {
668 $this->assertSame('English_Australia.1252', setlocale(LC_TIME, 0));
669 setlocale(LC_TIME, 'English_USA.1252');
670 } else {
671 $this->assertSame('en_AU.UTF-8', setlocale(LC_TIME, 0));
672 setlocale(LC_TIME, 'en_US.UTF-8');
673 }
674
5ecf8e8f 675 self::resetAllData(false);
d6e7a63d
PS
676
677 if ($CFG->ostype === 'WINDOWS') {
678 $this->assertSame('English_Australia.1252', setlocale(LC_TIME, 0));
679 } else {
680 $this->assertSame('en_AU.UTF-8', setlocale(LC_TIME, 0));
681 }
682 }
5a97f26b
PK
683
684 /**
685 * This test sets a user agent and makes sure that it is cleared when the test is reset.
686 */
687 public function test_it_resets_useragent_after_test() {
688 $this->resetAfterTest();
689 $fakeagent = 'New user agent set.';
690
691 // Sanity check: it should not be set when test begins.
692 self::assertFalse(core_useragent::get_user_agent_string(), 'It should not be set at first.');
693
694 // Set a fake useragent and check it was set properly.
695 core_useragent::instance(true, $fakeagent);
696 self::assertSame($fakeagent, core_useragent::get_user_agent_string(), 'It should be the forced agent.');
697
698 // Reset test data and ansure the useragent was cleaned.
699 self::resetAllData(false);
700 self::assertFalse(core_useragent::get_user_agent_string(), 'It should not be set again, data was reset.');
701 }
3c4c5189 702}