5cef89d7d478553d64885d51a0fbd54245ca7aae
[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_dataset() {
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('john.doe', $user5->username);
335         $this->assertSame('jane.doe', $user7->username);
337         $dataset = $this->createCsvDataSet(array('user'=>__DIR__.'/fixtures/sample_dataset.csv'));
338         $this->loadDataSet($dataset);
339         $this->assertEquals(8, $DB->get_field('user', 'id', array('username'=>'pepa.novak')));
340         $this->assertEquals(9, $DB->get_field('user', 'id', array('username'=>'bozka.novakova')));
342         $data = array(
343             'user' => array(
344                 array('username', 'email'),
345                 array('top.secret', 'top@example.com'),
346                 array('low.secret', 'low@example.com'),
347             ),
348         );
349         $dataset = $this->createArrayDataSet($data);
350         $this->loadDataSet($dataset);
351         $this->assertTrue($DB->record_exists('user', array('email'=>'top@example.com')));
352         $this->assertTrue($DB->record_exists('user', array('email'=>'low@example.com')));
354         $data = array(
355             'user' => array(
356                 array('username'=>'noidea', 'email'=>'noidea@example.com'),
357                 array('username'=>'onemore', 'email'=>'onemore@example.com'),
358             ),
359         );
360         $dataset = $this->createArrayDataSet($data);
361         $this->loadDataSet($dataset);
362         $this->assertTrue($DB->record_exists('user', array('username'=>'noidea')));
363         $this->assertTrue($DB->record_exists('user', array('username'=>'onemore')));
364     }
366     public function test_assert_time_current() {
367         $this->assertTimeCurrent(time());
369         $this->setCurrentTimeStart();
370         $this->assertTimeCurrent(time());
371         $this->waitForSecond();
372         $this->assertTimeCurrent(time());
373         $this->assertTimeCurrent(time()-1);
375         try {
376             $this->setCurrentTimeStart();
377             $this->assertTimeCurrent(time()+10);
378             $this->fail('Failed assert expected');
379         } catch (Exception $e) {
380             $this->assertInstanceOf('PHPUnit\Framework\ExpectationFailedException', $e);
381         }
383         try {
384             $this->setCurrentTimeStart();
385             $this->assertTimeCurrent(time()-10);
386             $this->fail('Failed assert expected');
387         } catch (Exception $e) {
388             $this->assertInstanceOf('PHPUnit\Framework\ExpectationFailedException', $e);
389         }
390     }
392     public function test_message_processors_reset() {
393         global $DB;
395         $this->resetAfterTest(true);
397         // Get all processors first.
398         $processors1 = get_message_processors();
400         // Add a new message processor and get all processors again.
401         $processor = new stdClass();
402         $processor->name = 'test_processor';
403         $processor->enabled = 1;
404         $DB->insert_record('message_processors', $processor);
406         $processors2 = get_message_processors();
408         // Assert that new processor still haven't been added to the list.
409         $this->assertSame($processors1, $processors2);
411         // Reset message processors data.
412         $processors3 = get_message_processors(false, true);
413         // Now, list of processors should not be the same any more,
414         // And we should have one more message processor in the list.
415         $this->assertNotSame($processors1, $processors3);
416         $this->assertEquals(count($processors1) + 1, count($processors3));
417     }
419     public function test_message_redirection() {
420         $this->preventResetByRollback(); // Messaging is not compatible with transactions...
421         $this->resetAfterTest(false);
423         $user1 = $this->getDataGenerator()->create_user();
424         $user2 = $this->getDataGenerator()->create_user();
426         // Any core message will do here.
427         $message1 = new \core\message\message();
428         $message1->courseid          = 1;
429         $message1->component         = 'moodle';
430         $message1->name              = 'instantmessage';
431         $message1->userfrom          = $user1;
432         $message1->userto            = $user2;
433         $message1->subject           = 'message subject 1';
434         $message1->fullmessage       = 'message body';
435         $message1->fullmessageformat = FORMAT_MARKDOWN;
436         $message1->fullmessagehtml   = '<p>message body</p>';
437         $message1->smallmessage      = 'small message';
438         $message1->notification      = 0;
440         $message2 = new \core\message\message();
441         $message2->courseid          = 1;
442         $message2->component         = 'moodle';
443         $message2->name              = 'instantmessage';
444         $message2->userfrom          = $user2;
445         $message2->userto            = $user1;
446         $message2->subject           = 'message subject 2';
447         $message2->fullmessage       = 'message body';
448         $message2->fullmessageformat = FORMAT_MARKDOWN;
449         $message2->fullmessagehtml   = '<p>message body</p>';
450         $message2->smallmessage      = 'small message';
451         $message2->notification      = 0;
453         // There should be debugging message without redirection.
454         $mailsink = $this->redirectEmails();
455         $mailsink->close();
456         message_send($message1);
457         $this->assertDebuggingCalled(null, null, 'message_send() must print debug message that messaging is disabled in phpunit tests.');
459         // Sink should catch messages.
460         $sink = $this->redirectMessages();
461         $mid1 = message_send($message1);
462         $mid2 = message_send($message2);
464         $this->assertDebuggingNotCalled('message redirection must prevent debug messages from the message_send()');
465         $this->assertEquals(2, $sink->count());
466         $this->assertGreaterThanOrEqual(1, $mid1);
467         $this->assertGreaterThanOrEqual($mid1, $mid2);
469         $messages = $sink->get_messages();
470         $this->assertIsArray($messages);
471         $this->assertCount(2, $messages);
472         $this->assertEquals($mid1, $messages[0]->id);
473         $this->assertEquals($message1->userto->id, $messages[0]->useridto);
474         $this->assertEquals($message1->userfrom->id, $messages[0]->useridfrom);
475         $this->assertEquals($message1->smallmessage, $messages[0]->smallmessage);
476         $this->assertEquals($mid2, $messages[1]->id);
477         $this->assertEquals($message2->userto->id, $messages[1]->useridto);
478         $this->assertEquals($message2->userfrom->id, $messages[1]->useridfrom);
479         $this->assertEquals($message2->smallmessage, $messages[1]->smallmessage);
481         // Test resetting.
482         $sink->clear();
483         $messages = $sink->get_messages();
484         $this->assertIsArray($messages);
485         $this->assertCount(0, $messages);
487         message_send($message1);
488         $messages = $sink->get_messages();
489         $this->assertIsArray($messages);
490         $this->assertCount(1, $messages);
492         // Test closing.
493         $sink->close();
494         $messages = $sink->get_messages();
495         $this->assertIsArray($messages);
496         $this->assertCount(1, $messages, 'Messages in sink are supposed to stay there after close');
498         // Test debugging is enabled again.
499         message_send($message1);
500         $this->assertDebuggingCalled(null, null, 'message_send() must print debug message that messaging is disabled in phpunit tests.');
502         // Test invalid names and components.
504         $sink = $this->redirectMessages();
506         $message3 = new \core\message\message();
507         $message3->courseid          = 1;
508         $message3->component         = 'xxxx_yyyyy';
509         $message3->name              = 'instantmessage';
510         $message3->userfrom          = $user2;
511         $message3->userto            = $user1;
512         $message3->subject           = 'message subject 2';
513         $message3->fullmessage       = 'message body';
514         $message3->fullmessageformat = FORMAT_MARKDOWN;
515         $message3->fullmessagehtml   = '<p>message body</p>';
516         $message3->smallmessage      = 'small message';
517         $message3->notification      = 0;
519         $this->assertFalse(message_send($message3));
520         $this->assertDebuggingCalled('Attempt to send msg from a provider xxxx_yyyyy/instantmessage '.
521             'that is inactive or not allowed for the user id='.$user1->id);
523         $message3->component = 'moodle';
524         $message3->name      = 'yyyyyy';
526         $this->assertFalse(message_send($message3));
527         $this->assertDebuggingCalled('Attempt to send msg from a provider moodle/yyyyyy '.
528             'that is inactive or not allowed for the user id='.$user1->id);
530         message_send($message1);
531         $this->assertEquals(1, $sink->count());
533         // Test if sink can be carried over to next test.
534         $this->assertTrue(phpunit_util::is_redirecting_messages());
535         return $sink;
536     }
538     /**
539      * @depends test_message_redirection
540      */
541     public function test_message_redirection_noreset($sink) {
542         if ($this->isInIsolation()) {
543             $this->markTestSkipped('State cannot be carried over between tests in isolated tests');
544         }
546         $this->preventResetByRollback(); // Messaging is not compatible with transactions...
547         $this->resetAfterTest();
549         $this->assertTrue(phpunit_util::is_redirecting_messages());
550         $this->assertEquals(1, $sink->count());
552         $message = new \core\message\message();
553         $message->courseid          = 1;
554         $message->component         = 'moodle';
555         $message->name              = 'instantmessage';
556         $message->userfrom          = get_admin();
557         $message->userto            = get_admin();
558         $message->subject           = 'message subject 1';
559         $message->fullmessage       = 'message body';
560         $message->fullmessageformat = FORMAT_MARKDOWN;
561         $message->fullmessagehtml   = '<p>message body</p>';
562         $message->smallmessage      = 'small message';
563         $message->notification      = 0;
565         message_send($message);
566         $this->assertEquals(2, $sink->count());
567     }
569     /**
570      * @depends test_message_redirection_noreset
571      */
572     public function test_message_redirection_reset() {
573         $this->assertFalse(phpunit_util::is_redirecting_messages(), 'Test reset must stop message redirection.');
574     }
576     public function test_set_timezone() {
577         global $CFG;
578         $this->resetAfterTest();
580         $this->assertSame('Australia/Perth', $CFG->timezone);
581         $this->assertSame('Australia/Perth', date_default_timezone_get());
583         $this->setTimezone('Pacific/Auckland', 'Europe/Prague');
584         $this->assertSame('Pacific/Auckland', $CFG->timezone);
585         $this->assertSame('Pacific/Auckland', date_default_timezone_get());
587         $this->setTimezone('99', 'Europe/Prague');
588         $this->assertSame('99', $CFG->timezone);
589         $this->assertSame('Europe/Prague', date_default_timezone_get());
591         $this->setTimezone('xxx', 'Europe/Prague');
592         $this->assertSame('xxx', $CFG->timezone);
593         $this->assertSame('Europe/Prague', date_default_timezone_get());
595         $this->setTimezone();
596         $this->assertSame('Australia/Perth', $CFG->timezone);
597         $this->assertSame('Australia/Perth', date_default_timezone_get());
599         try {
600             $this->setTimezone('Pacific/Auckland', '');
601         } catch (Exception $e) {
602             $this->assertInstanceOf('PHPUnit\Framework\Error\Warning', $e);
603         }
605         try {
606             $this->setTimezone('Pacific/Auckland', 'xxxx');
607         } catch (Exception $e) {
608             $this->assertInstanceOf('PHPUnit\Framework\Error\Warning', $e);
609         }
611         try {
612             $this->setTimezone('Pacific/Auckland', null);
613         } catch (Exception $e) {
614             $this->assertInstanceOf('PHPUnit\Framework\Error\Warning', $e);
615         }
617     }
619     public function test_locale_reset() {
620         global $CFG;
622         $this->resetAfterTest();
624         // If this fails self::resetAllData(); must be updated.
625         $this->assertSame('en_AU.UTF-8', get_string('locale', 'langconfig'));
626         $this->assertSame('English_Australia.1252', get_string('localewin', 'langconfig'));
628         if ($CFG->ostype === 'WINDOWS') {
629             $this->assertSame('English_Australia.1252', setlocale(LC_TIME, 0));
630             setlocale(LC_TIME, 'English_USA.1252');
631         } else {
632             $this->assertSame('en_AU.UTF-8', setlocale(LC_TIME, 0));
633             setlocale(LC_TIME, 'en_US.UTF-8');
634         }
636         try {
637             self::resetAllData(true);
638         } catch (Exception $e) {
639             $this->assertInstanceOf('PHPUnit\Framework\Error\Warning', $e);
640         }
642         if ($CFG->ostype === 'WINDOWS') {
643             $this->assertSame('English_Australia.1252', setlocale(LC_TIME, 0));
644         } else {
645             $this->assertSame('en_AU.UTF-8', setlocale(LC_TIME, 0));
646         }
648         if ($CFG->ostype === 'WINDOWS') {
649             $this->assertSame('English_Australia.1252', setlocale(LC_TIME, 0));
650             setlocale(LC_TIME, 'English_USA.1252');
651         } else {
652             $this->assertSame('en_AU.UTF-8', setlocale(LC_TIME, 0));
653             setlocale(LC_TIME, 'en_US.UTF-8');
654         }
656         self::resetAllData(false);
658         if ($CFG->ostype === 'WINDOWS') {
659             $this->assertSame('English_Australia.1252', setlocale(LC_TIME, 0));
660         } else {
661             $this->assertSame('en_AU.UTF-8', setlocale(LC_TIME, 0));
662         }
663     }
665     /**
666      * This test sets a user agent and makes sure that it is cleared when the test is reset.
667      */
668     public function test_it_resets_useragent_after_test() {
669         $this->resetAfterTest();
670         $fakeagent = 'New user agent set.';
672         // Sanity check: it should not be set when test begins.
673         self::assertFalse(core_useragent::get_user_agent_string(), 'It should not be set at first.');
675         // Set a fake useragent and check it was set properly.
676         core_useragent::instance(true, $fakeagent);
677         self::assertSame($fakeagent, core_useragent::get_user_agent_string(), 'It should be the forced agent.');
679         // Reset test data and ansure the useragent was cleaned.
680         self::resetAllData(false);
681         self::assertFalse(core_useragent::get_user_agent_string(), 'It should not be set again, data was reset.');
682     }