MDL-67673 phpunit: Move tests to use new phpunit_dataset
[moodle.git] / lib / phpunit / tests / advanced_test.php
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/>.
17 /**
18  * PHPUnit integration tests
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  */
26 defined('MOODLE_INTERNAL') || die();
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  */
37 class core_phpunit_advanced_testcase extends advanced_testcase {
39     public function test_debugging() {
40         global $CFG;
41         $this->resetAfterTest();
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();
51         debugging('a');
52         debugging('b', DEBUG_MINIMAL);
53         debugging('c', DEBUG_DEVELOPER);
54         $debuggings = $this->getDebuggingMessages();
55         $this->assertCount(3, $debuggings);
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);
63         $this->resetDebugging();
64         $this->assertDebuggingNotCalled();
65         $debuggings = $this->getDebuggingMessages();
66         $this->assertCount(0, $debuggings);
68         set_debugging(DEBUG_NONE);
69         debugging('hokus');
70         $this->assertDebuggingNotCalled();
71         set_debugging(DEBUG_DEVELOPER);
72     }
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     }
84     public function test_set_user() {
85         global $USER, $DB, $SESSION;
87         $this->resetAfterTest();
89         $this->assertEquals(0, $USER->id);
90         $this->assertSame($_SESSION['USER'], $USER);
91         $this->assertSame($GLOBALS['USER'], $USER);
93         $user = $DB->get_record('user', array('id'=>2));
94         $this->assertNotEmpty($user);
95         $this->setUser($user);
96         $this->assertEquals(2, $USER->id);
97         $this->assertEquals(2, $_SESSION['USER']->id);
98         $this->assertSame($_SESSION['USER'], $USER);
99         $this->assertSame($GLOBALS['USER'], $USER);
101         $USER->id = 3;
102         $this->assertEquals(3, $USER->id);
103         $this->assertEquals(3, $_SESSION['USER']->id);
104         $this->assertSame($_SESSION['USER'], $USER);
105         $this->assertSame($GLOBALS['USER'], $USER);
107         \core\session\manager::set_user($user);
108         $this->assertEquals(2, $USER->id);
109         $this->assertEquals(2, $_SESSION['USER']->id);
110         $this->assertSame($_SESSION['USER'], $USER);
111         $this->assertSame($GLOBALS['USER'], $USER);
113         $USER = $DB->get_record('user', array('id'=>1));
114         $this->assertNotEmpty($USER);
115         $this->assertEquals(1, $USER->id);
116         $this->assertEquals(1, $_SESSION['USER']->id);
117         $this->assertSame($_SESSION['USER'], $USER);
118         $this->assertSame($GLOBALS['USER'], $USER);
120         $this->setUser(null);
121         $this->assertEquals(0, $USER->id);
122         $this->assertSame($_SESSION['USER'], $USER);
123         $this->assertSame($GLOBALS['USER'], $USER);
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);
129     }
131     public function test_set_admin_user() {
132         global $USER;
134         $this->resetAfterTest();
136         $this->setAdminUser();
137         $this->assertEquals($USER->id, 2);
138         $this->assertTrue(is_siteadmin());
139     }
141     public function test_set_guest_user() {
142         global $USER;
144         $this->resetAfterTest();
146         $this->setGuestUser();
147         $this->assertEquals($USER->id, 1);
148         $this->assertTrue(isguestuser());
149     }
151     public function test_database_reset() {
152         global $DB;
154         $this->resetAfterTest();
156         $this->preventResetByRollback();
158         $this->assertEquals(1, $DB->count_records('course')); // Only frontpage in new site.
160         // This is weird table - id is NOT a sequence here.
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);
166         $this->assertEquals(0, $DB->count_records('user_preferences'));
167         $originaldisplayid = $DB->insert_record('user_preferences', array('userid'=>2, 'name'=> 'phpunittest', 'value'=>'x'));
168         $this->assertEquals(1, $DB->count_records('user_preferences'));
170         $numcourses = $DB->count_records('course');
171         $course = $this->getDataGenerator()->create_course();
172         $this->assertEquals($numcourses + 1, $DB->count_records('course'));
174         $this->assertEquals(2, $DB->count_records('user'));
175         $DB->delete_records('user', array('id'=>1));
176         $this->assertEquals(1, $DB->count_records('user'));
178         $this->resetAllData();
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.
183         $numcourses = $DB->count_records('course');
184         $course = $this->getDataGenerator()->create_course();
185         $this->assertEquals($numcourses + 1, $DB->count_records('course'));
187         $displayid = $DB->insert_record('user_preferences', array('userid'=>2, 'name'=> 'phpunittest', 'value'=>'x'));
188         $this->assertEquals($originaldisplayid, $displayid);
190         $this->assertEquals(2, $DB->count_records('user'));
191         $DB->delete_records('user', array('id'=>2));
192         $user = $this->getDataGenerator()->create_user();
193         $this->assertEquals(2, $DB->count_records('user'));
194         $this->assertGreaterThan(2, $user->id);
196         $this->resetAllData();
198         $numcourses = $DB->count_records('course');
199         $course = $this->getDataGenerator()->create_course();
200         $this->assertEquals($numcourses + 1, $DB->count_records('course'));
202         $this->assertEquals(2, $DB->count_records('user'));
203         $DB->delete_records('user', array('id'=>2));
205         $this->resetAllData();
207         $numcourses = $DB->count_records('course');
208         $course = $this->getDataGenerator()->create_course();
209         $this->assertEquals($numcourses + 1, $DB->count_records('course'));
211         $this->assertEquals(2, $DB->count_records('user'));
212     }
214     public function test_change_detection() {
215         global $DB, $CFG, $COURSE, $SITE, $USER;
217         $this->preventResetByRollback();
218         self::resetAllData(true);
220         // Database change.
221         $this->assertEquals(1, $DB->get_field('user', 'confirmed', array('id'=>2)));
222         $DB->set_field('user', 'confirmed', 0, array('id'=>2));
223         try {
224             self::resetAllData(true);
225         } catch (Exception $e) {
226             $this->assertInstanceOf('PHPUnit\Framework\Error\Warning', $e);
227         }
228         $this->assertEquals(1, $DB->get_field('user', 'confirmed', array('id'=>2)));
230         // Config change.
231         $CFG->xx = 'yy';
232         unset($CFG->admin);
233         $CFG->rolesactive = 0;
234         try {
235             self::resetAllData(true);
236         } catch (Exception $e) {
237             $this->assertInstanceOf('PHPUnit\Framework\Error\Warning', $e);
238             $this->assertStringContainsString('xx', $e->getMessage());
239             $this->assertStringContainsString('admin', $e->getMessage());
240             $this->assertStringContainsString('rolesactive', $e->getMessage());
241         }
242         $this->assertFalse(isset($CFG->xx));
243         $this->assertTrue(isset($CFG->admin));
244         $this->assertEquals(1, $CFG->rolesactive);
246         // _GET change.
247         $_GET['__somethingthatwillnotnormallybepresent__'] = 'yy';
248         self::resetAllData(true);
250         $this->assertEquals(array(), $_GET);
252         // _POST change.
253         $_POST['__somethingthatwillnotnormallybepresent2__'] = 'yy';
254         self::resetAllData(true);
255         $this->assertEquals(array(), $_POST);
257         // _FILES change.
258         $_FILES['__somethingthatwillnotnormallybepresent3__'] = 'yy';
259         self::resetAllData(true);
260         $this->assertEquals(array(), $_FILES);
262         // _REQUEST change.
263         $_REQUEST['__somethingthatwillnotnormallybepresent4__'] = 'yy';
264         self::resetAllData(true);
265         $this->assertEquals(array(), $_REQUEST);
267         // Silent changes.
268         $_SERVER['xx'] = 'yy';
269         self::resetAllData(true);
270         $this->assertFalse(isset($_SERVER['xx']));
272         // COURSE change.
273         $SITE->id = 10;
274         $COURSE = new stdClass();
275         $COURSE->id = 7;
276         try {
277             self::resetAllData(true);
278         } catch (Exception $e) {
279             $this->assertInstanceOf('PHPUnit\Framework\Error\Warning', $e);
280             $this->assertEquals(1, $SITE->id);
281             $this->assertSame($SITE, $COURSE);
282             $this->assertSame($SITE, $COURSE);
283         }
285         // USER change.
286         $this->setUser(2);
287         try {
288             self::resetAllData(true);
289         } catch (Exception $e) {
290             $this->assertInstanceOf('PHPUnit\Framework\Error\Warning', $e);
291             $this->assertEquals(0, $USER->id);
292         }
293     }
295     public function test_getDataGenerator() {
296         $generator = $this->getDataGenerator();
297         $this->assertInstanceOf('testing_data_generator', $generator);
298     }
300     public function test_database_mock1() {
301         global $DB;
303         try {
304             $DB->get_record('pokus', array());
305             $this->fail('Exception expected when accessing non existent table');
306         } catch (moodle_exception $e) {
307             $this->assertInstanceOf('dml_exception', $e);
308         }
309         $DB = $this->createMock(get_class($DB));
310         $this->assertNull($DB->get_record('pokus', array()));
311         // Rest continues after reset.
312     }
314     public function test_database_mock2() {
315         global $DB;
317         // Now the database should be back to normal.
318         $this->assertFalse($DB->get_record('user', array('id'=>9999)));
319     }
321     public function test_load_data_dataset_xml() {
322         global $DB;
324         $this->resetAfterTest();
326         $this->assertFalse($DB->record_exists('user', array('id' => 5)));
327         $this->assertFalse($DB->record_exists('user', array('id' => 7)));
328         $dataset = $this->createXMLDataSet(__DIR__.'/fixtures/sample_dataset.xml');
329         $this->loadDataSet($dataset);
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);
337     }
339     public function test_load_dataset_csv() {
340         global $DB;
342         $this->resetAfterTest();
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'));
347         $this->loadDataSet($dataset);
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')));
351     }
353     public function test_load_dataset_array() {
354         global $DB;
356         $this->resetAfterTest();
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         );
366         $this->assertFalse($DB->record_exists('user', array('email' => 'top@example.com')));
367         $this->assertFalse($DB->record_exists('user', array('email' => 'low@example.com')));
368         $dataset = $this->createArrayDataSet($data);
369         $this->loadDataSet($dataset);
370         $this->assertTrue($DB->record_exists('user', array('email' => 'top@example.com')));
371         $this->assertTrue($DB->record_exists('user', array('email' => 'low@example.com')));
373         $data = array(
374             'user' => array(
375                 array('username' => 'noidea', 'email' => 'noidea@example.com'),
376                 array('username' => 'onemore', 'email' => 'onemore@example.com'),
377             ),
378         );
379         $dataset = $this->createArrayDataSet($data);
380         $this->loadDataSet($dataset);
381         $this->assertTrue($DB->record_exists('user', array('username' => 'noidea')));
382         $this->assertTrue($DB->record_exists('user', array('username' => 'onemore')));
383     }
385     public function test_assert_time_current() {
386         $this->assertTimeCurrent(time());
388         $this->setCurrentTimeStart();
389         $this->assertTimeCurrent(time());
390         $this->waitForSecond();
391         $this->assertTimeCurrent(time());
392         $this->assertTimeCurrent(time()-1);
394         try {
395             $this->setCurrentTimeStart();
396             $this->assertTimeCurrent(time()+10);
397             $this->fail('Failed assert expected');
398         } catch (Exception $e) {
399             $this->assertInstanceOf('PHPUnit\Framework\ExpectationFailedException', $e);
400         }
402         try {
403             $this->setCurrentTimeStart();
404             $this->assertTimeCurrent(time()-10);
405             $this->fail('Failed assert expected');
406         } catch (Exception $e) {
407             $this->assertInstanceOf('PHPUnit\Framework\ExpectationFailedException', $e);
408         }
409     }
411     public function test_message_processors_reset() {
412         global $DB;
414         $this->resetAfterTest(true);
416         // Get all processors first.
417         $processors1 = get_message_processors();
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);
425         $processors2 = get_message_processors();
427         // Assert that new processor still haven't been added to the list.
428         $this->assertSame($processors1, $processors2);
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     }
438     public function test_message_redirection() {
439         $this->preventResetByRollback(); // Messaging is not compatible with transactions...
440         $this->resetAfterTest(false);
442         $user1 = $this->getDataGenerator()->create_user();
443         $user2 = $this->getDataGenerator()->create_user();
445         // Any core message will do here.
446         $message1 = new \core\message\message();
447         $message1->courseid          = 1;
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';
457         $message1->notification      = 0;
459         $message2 = new \core\message\message();
460         $message2->courseid          = 1;
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';
470         $message2->notification      = 0;
472         // There should be debugging message without redirection.
473         $mailsink = $this->redirectEmails();
474         $mailsink->close();
475         message_send($message1);
476         $this->assertDebuggingCalled(null, null, 'message_send() must print debug message that messaging is disabled in phpunit tests.');
478         // Sink should catch messages.
479         $sink = $this->redirectMessages();
480         $mid1 = message_send($message1);
481         $mid2 = message_send($message2);
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);
488         $messages = $sink->get_messages();
489         $this->assertIsArray($messages);
490         $this->assertCount(2, $messages);
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);
500         // Test resetting.
501         $sink->clear();
502         $messages = $sink->get_messages();
503         $this->assertIsArray($messages);
504         $this->assertCount(0, $messages);
506         message_send($message1);
507         $messages = $sink->get_messages();
508         $this->assertIsArray($messages);
509         $this->assertCount(1, $messages);
511         // Test closing.
512         $sink->close();
513         $messages = $sink->get_messages();
514         $this->assertIsArray($messages);
515         $this->assertCount(1, $messages, 'Messages in sink are supposed to stay there after close');
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.');
521         // Test invalid names and components.
523         $sink = $this->redirectMessages();
525         $message3 = new \core\message\message();
526         $message3->courseid          = 1;
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';
536         $message3->notification      = 0;
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);
542         $message3->component = 'moodle';
543         $message3->name      = 'yyyyyy';
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);
549         message_send($message1);
550         $this->assertEquals(1, $sink->count());
552         // Test if sink can be carried over to next test.
553         $this->assertTrue(phpunit_util::is_redirecting_messages());
554         return $sink;
555     }
557     /**
558      * @depends test_message_redirection
559      */
560     public function test_message_redirection_noreset($sink) {
561         if ($this->isInIsolation()) {
562             $this->markTestSkipped('State cannot be carried over between tests in isolated tests');
563         }
565         $this->preventResetByRollback(); // Messaging is not compatible with transactions...
566         $this->resetAfterTest();
568         $this->assertTrue(phpunit_util::is_redirecting_messages());
569         $this->assertEquals(1, $sink->count());
571         $message = new \core\message\message();
572         $message->courseid          = 1;
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';
582         $message->notification      = 0;
584         message_send($message);
585         $this->assertEquals(2, $sink->count());
586     }
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     }
595     public function test_set_timezone() {
596         global $CFG;
597         $this->resetAfterTest();
599         $this->assertSame('Australia/Perth', $CFG->timezone);
600         $this->assertSame('Australia/Perth', date_default_timezone_get());
602         $this->setTimezone('Pacific/Auckland', 'Europe/Prague');
603         $this->assertSame('Pacific/Auckland', $CFG->timezone);
604         $this->assertSame('Pacific/Auckland', date_default_timezone_get());
606         $this->setTimezone('99', 'Europe/Prague');
607         $this->assertSame('99', $CFG->timezone);
608         $this->assertSame('Europe/Prague', date_default_timezone_get());
610         $this->setTimezone('xxx', 'Europe/Prague');
611         $this->assertSame('xxx', $CFG->timezone);
612         $this->assertSame('Europe/Prague', date_default_timezone_get());
614         $this->setTimezone();
615         $this->assertSame('Australia/Perth', $CFG->timezone);
616         $this->assertSame('Australia/Perth', date_default_timezone_get());
618         try {
619             $this->setTimezone('Pacific/Auckland', '');
620         } catch (Exception $e) {
621             $this->assertInstanceOf('PHPUnit\Framework\Error\Warning', $e);
622         }
624         try {
625             $this->setTimezone('Pacific/Auckland', 'xxxx');
626         } catch (Exception $e) {
627             $this->assertInstanceOf('PHPUnit\Framework\Error\Warning', $e);
628         }
630         try {
631             $this->setTimezone('Pacific/Auckland', null);
632         } catch (Exception $e) {
633             $this->assertInstanceOf('PHPUnit\Framework\Error\Warning', $e);
634         }
636     }
638     public function test_locale_reset() {
639         global $CFG;
641         $this->resetAfterTest();
643         // If this fails self::resetAllData(); must be updated.
644         $this->assertSame('en_AU.UTF-8', get_string('locale', 'langconfig'));
645         $this->assertSame('English_Australia.1252', get_string('localewin', 'langconfig'));
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         }
655         try {
656             self::resetAllData(true);
657         } catch (Exception $e) {
658             $this->assertInstanceOf('PHPUnit\Framework\Error\Warning', $e);
659         }
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         }
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         }
675         self::resetAllData(false);
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     }
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.';
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.');
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.');
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     }