| 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 | 
           /**
  | 
        
        
            | 
            | 
           20 | 
            * Tests for the standard ClockInterface implementation.
  | 
        
        
            | 
            | 
           21 | 
            *
  | 
        
        
            | 
            | 
           22 | 
            * @package    core
  | 
        
        
            | 
            | 
           23 | 
            * @category   test
  | 
        
        
            | 
            | 
           24 | 
            * @copyright  2024 Andrew Lyons <andrew@nicols.co.uk>
  | 
        
        
            | 
            | 
           25 | 
            * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  | 
        
        
            | 
            | 
           26 | 
            * @covers \core\system_clock
  | 
        
        
            | 
            | 
           27 | 
            */
  | 
        
        
            | 
            | 
           28 | 
           final class system_clock_test extends \advanced_testcase {
  | 
        
        
            | 
            | 
           29 | 
               /**
  | 
        
        
            | 
            | 
           30 | 
                * Test that the now method returns a DateTimeImmutable object.
  | 
        
        
            | 
            | 
           31 | 
                */
  | 
        
        
            | 
            | 
           32 | 
               public function test_now(): void {
  | 
        
        
            | 
            | 
           33 | 
                   $starttime = time();
  | 
        
        
            | 
            | 
           34 | 
              | 
        
        
            | 
            | 
           35 | 
                   $clock = new system_clock();
  | 
        
        
            | 
            | 
           36 | 
                   $now = $clock->now();
  | 
        
        
            | 
            | 
           37 | 
                   $this->assertInstanceOf(\DateTimeImmutable::class, $now);
  | 
        
        
            | 
            | 
           38 | 
                   $this->assertGreaterThanOrEqual($starttime, $now->getTimestamp());
  | 
        
        
            | 
            | 
           39 | 
               }
  | 
        
        
            | 
            | 
           40 | 
              | 
        
        
            | 
            | 
           41 | 
               /**
  | 
        
        
            | 
            | 
           42 | 
                * Test that the time method returns a timestamp.
  | 
        
        
            | 
            | 
           43 | 
                */
  | 
        
        
            | 
            | 
           44 | 
               public function test_time(): void {
  | 
        
        
            | 
            | 
           45 | 
                   $starttime = time();
  | 
        
        
            | 
            | 
           46 | 
              | 
        
        
            | 
            | 
           47 | 
                   $clock = new system_clock();
  | 
        
        
            | 
            | 
           48 | 
                   $time = $clock->time();
  | 
        
        
            | 
            | 
           49 | 
                   $this->assertGreaterThanOrEqual($starttime, $time);
  | 
        
        
            | 
            | 
           50 | 
               }
  | 
        
        
            | 
            | 
           51 | 
              | 
        
        
            | 
            | 
           52 | 
               /**
  | 
        
        
            | 
            | 
           53 | 
                * Test that the now method returns a DateTimeImmutable object in the server timezone.
  | 
        
        
            | 
            | 
           54 | 
                *
  | 
        
        
            | 
            | 
           55 | 
                * @dataProvider timezone_provider
  | 
        
        
            | 
            | 
           56 | 
                * @param string $timezone
  | 
        
        
            | 
            | 
           57 | 
                */
  | 
        
        
            | 
            | 
           58 | 
               public function test_now_timezone(string $timezone): void {
  | 
        
        
            | 
            | 
           59 | 
                   global $CFG;
  | 
        
        
            | 
            | 
           60 | 
                   $this->resetAfterTest();
  | 
        
        
            | 
            | 
           61 | 
              | 
        
        
            | 
            | 
           62 | 
                   $CFG->timezone = $timezone;
  | 
        
        
            | 
            | 
           63 | 
              | 
        
        
            | 
            | 
           64 | 
                   $clock = new system_clock();
  | 
        
        
            | 
            | 
           65 | 
                   $now = $clock->now();
  | 
        
        
            | 
            | 
           66 | 
                   $this->assertEquals(\core_date::normalise_timezone($CFG->timezone), $now->getTimezone()->getName());
  | 
        
        
            | 
            | 
           67 | 
               }
  | 
        
        
            | 
            | 
           68 | 
              | 
        
        
            | 
            | 
           69 | 
               /**
  | 
        
        
            | 
            | 
           70 | 
                * Data provider for the test_now_timezone method.
  | 
        
        
            | 
            | 
           71 | 
                *
  | 
        
        
            | 
            | 
           72 | 
                * @return array
  | 
        
        
            | 
            | 
           73 | 
                */
  | 
        
        
            | 
            | 
           74 | 
               public static function timezone_provider(): array {
  | 
        
        
            | 
            | 
           75 | 
                   return [
  | 
        
        
            | 
            | 
           76 | 
                       ['UTC'],
  | 
        
        
            | 
            | 
           77 | 
                       ['Europe/London'],
  | 
        
        
            | 
            | 
           78 | 
                       ['America/New_York'],
  | 
        
        
            | 
            | 
           79 | 
                   ];
  | 
        
        
            | 
            | 
           80 | 
               }
  | 
        
        
            | 
            | 
           81 | 
           }
  |