Proyectos de Subversion Moodle

Rev

Rev 11 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 11 Rev 1441
Línea 33... Línea 33...
33
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34
 * @author    Petr Skoda <petr.skoda@totaralms.com>
34
 * @author    Petr Skoda <petr.skoda@totaralms.com>
35
 * @covers \core_date
35
 * @covers \core_date
36
 * @coversDefaultClass \core_date
36
 * @coversDefaultClass \core_date
37
 */
37
 */
38
class date_test extends advanced_testcase {
38
final class date_test extends advanced_testcase {
39
    /**
39
    /**
40
     * @covers ::get_default_php_timezone
40
     * @covers ::get_default_php_timezone
41
     */
41
     */
42
    public function test_get_default_php_timezone(): void {
42
    public function test_get_default_php_timezone(): void {
43
        $this->resetAfterTest();
43
        $this->resetAfterTest();
Línea 124... Línea 124...
124
 
124
 
125
    /**
125
    /**
126
     * @covers ::normalise_timezone
126
     * @covers ::normalise_timezone
127
     */
127
     */
128
    public function test_windows_conversion(): void {
128
    public function test_windows_conversion(): void {
Línea 129... Línea 129...
129
        $file = __DIR__ . '/fixtures/timezonewindows.xml';
129
        $file = self::get_fixture_path('core', 'timezonewindows.xml');
130
 
130
 
Línea 131... Línea 131...
131
        $contents = file_get_contents($file);
131
        $contents = file_get_contents($file);
Línea 306... Línea 306...
306
        $this->setTimezone('Pacific/Auckland', 'Pacific/Auckland');
306
        $this->setTimezone('Pacific/Auckland', 'Pacific/Auckland');
307
        unset($CFG->timezone);
307
        unset($CFG->timezone);
308
        $this->assertSame('Pacific/Auckland', core_date::get_server_timezone());
308
        $this->assertSame('Pacific/Auckland', core_date::get_server_timezone());
Línea 309... Línea 309...
309
 
309
 
-
 
310
        // Admin should fix the settings.
-
 
311
        set_error_handler(function ($errno, $errstr): void {
-
 
312
            $this->assertStringContainsString('Unknown or bad timezone', $errstr);
-
 
313
            restore_error_handler();
310
        // Admin should fix the settings.
314
        }, E_ALL);
-
 
315
        $this->setTimezone('xxx/zzzz', 'Europe/Prague');
-
 
316
        set_error_handler(function ($errno, $errstr): void {
-
 
317
            $this->assertStringContainsString('Unknown or bad timezone', $errstr);
-
 
318
            restore_error_handler();
311
        $this->setTimezone('xxx/zzzz', 'Europe/Prague');
319
        }, E_ALL);
312
        $this->assertSame('Europe/Prague', core_date::get_server_timezone());
320
        $this->assertSame('Europe/Prague', core_date::get_server_timezone());
Línea 313... Línea 321...
313
    }
321
    }
314
 
322
 
Línea 384... Línea 392...
384
        date_default_timezone_set('UTC');
392
        date_default_timezone_set('UTC');
385
        core_date::set_default_server_timezone();
393
        core_date::set_default_server_timezone();
386
        $this->assertSame('Etc/GMT-1', date_default_timezone_get());
394
        $this->assertSame('Etc/GMT-1', date_default_timezone_get());
387
    }
395
    }
Línea 388... Línea 396...
388
 
396
 
389
    public function legacyUserTimezoneProvider() {
397
    public static function legacyUserTimezoneProvider(): array {
390
        return [
398
        return [
391
            ['', 'Australia/Perth'],            // Fallback on default timezone.
399
            ['', 'Australia/Perth'],            // Fallback on default timezone.
392
            ['-13.0', 'Australia/Perth'],       // Fallback on default timezone.
400
            ['-13.0', 'Australia/Perth'],       // Fallback on default timezone.
393
            ['-12.5', 'Etc/GMT+12'],
401
            ['-12.5', 'Etc/GMT+12'],
Línea 488... Línea 496...
488
        $USER->timezone = 'abc/def';
496
        $USER->timezone = 'abc/def';
489
        $CFG->forcetimezone = '99';
497
        $CFG->forcetimezone = '99';
490
        $this->assertSame('Europe/Prague', core_date::get_user_timezone(null));
498
        $this->assertSame('Europe/Prague', core_date::get_user_timezone(null));
491
        $this->assertSame('Europe/Prague', core_date::get_user_timezone());
499
        $this->assertSame('Europe/Prague', core_date::get_user_timezone());
Línea -... Línea 500...
-
 
500
 
-
 
501
        $invokable = self::get_invokable();
492
 
502
        set_error_handler($invokable, E_ALL);
-
 
503
        $this->setTimezone('xxx/yyy', 'Europe/London');
493
        $this->setTimezone('xxx/yyy', 'Europe/London');
504
        $this->assertCount(1, $invokable->get_invocations());
494
        $USER->timezone = 'abc/def';
505
        $USER->timezone = 'abc/def';
495
        $CFG->forcetimezone = 'Europe/Berlin';
506
        $CFG->forcetimezone = 'Europe/Berlin';
496
        $this->assertSame('Europe/Berlin', core_date::get_user_timezone(null));
507
        $this->assertSame('Europe/Berlin', core_date::get_user_timezone(null));
-
 
508
        $this->assertSame('Europe/Berlin', core_date::get_user_timezone());
-
 
509
        $this->assertCount(1, $invokable->get_invocations());
Línea -... Línea 510...
-
 
510
        restore_error_handler();
-
 
511
 
497
        $this->assertSame('Europe/Berlin', core_date::get_user_timezone());
512
        $invokable = self::get_invokable();
-
 
513
        set_error_handler($invokable, E_ALL);
-
 
514
        $this->setTimezone('xxx/yyy', 'Europe/London');
-
 
515
        $this->assertCount(1, $invokable->get_invocations());
498
 
516
        restore_error_handler();
499
        $this->setTimezone('xxx/yyy', 'Europe/London');
517
 
-
 
518
        $USER->timezone = 'abc/def';
-
 
519
        $CFG->forcetimezone = 99;
500
        $USER->timezone = 'abc/def';
520
        $invokable = self::get_invokable();
-
 
521
        set_error_handler($invokable, E_ALL);
-
 
522
        $this->assertSame('Europe/London', core_date::get_user_timezone(null));
-
 
523
        $this->assertCount(1, $invokable->get_invocations());
-
 
524
        restore_error_handler();
-
 
525
 
501
        $CFG->forcetimezone = 99;
526
        $invokable = self::get_invokable();
-
 
527
        set_error_handler($invokable, E_ALL);
-
 
528
        $this->assertSame('Europe/London', core_date::get_user_timezone());
Línea 502... Línea 529...
502
        $this->assertSame('Europe/London', core_date::get_user_timezone(null));
529
        $this->assertCount(1, $invokable->get_invocations());
503
        $this->assertSame('Europe/London', core_date::get_user_timezone());
530
        restore_error_handler();
Línea 504... Línea 531...
504
 
531
 
Línea 570... Línea 597...
570
        $this->assertSame('Europe/Prague', core_date::get_user_timezone('xxx/zzz'));
597
        $this->assertSame('Europe/Prague', core_date::get_user_timezone('xxx/zzz'));
571
        $USER->timezone = 99;
598
        $USER->timezone = 99;
572
        $this->assertSame('Europe/London', core_date::get_user_timezone('Europe/London'));
599
        $this->assertSame('Europe/London', core_date::get_user_timezone('Europe/London'));
573
        $this->assertSame('Europe/Prague', core_date::get_user_timezone('xxx/zzz'));
600
        $this->assertSame('Europe/Prague', core_date::get_user_timezone('xxx/zzz'));
574
        $USER->timezone = 'xxz/zzz';
601
        $USER->timezone = 'xxz/zzz';
-
 
602
 
-
 
603
        $invokable = self::get_invokable();
-
 
604
        set_error_handler($invokable, E_ALL);
575
        $this->assertSame('Europe/Prague', core_date::get_user_timezone('99'));
605
        $this->assertSame('Europe/Prague', core_date::get_user_timezone('99'));
-
 
606
        $this->assertCount(0, $invokable->get_invocations());
-
 
607
        restore_error_handler();
Línea -... Línea 608...
-
 
608
 
-
 
609
        $invokable = self::get_invokable();
576
 
610
        set_error_handler($invokable, E_ALL);
-
 
611
        $this->setTimezone('xxx', 'Europe/Prague');
-
 
612
        $this->assertCount(1, $invokable->get_invocations());
-
 
613
        restore_error_handler();
577
        $this->setTimezone('xxx', 'Europe/Prague');
614
 
578
        $CFG->forcetimezone = '99';
615
        $CFG->forcetimezone = '99';
-
 
616
        $USER->timezone = 'xxx';
-
 
617
        $invokable = self::get_invokable();
579
        $USER->timezone = 'xxx';
618
        set_error_handler($invokable, E_ALL);
-
 
619
        $this->assertSame('Europe/Prague', core_date::get_user_timezone('99'));
580
        $this->assertSame('Europe/Prague', core_date::get_user_timezone('99'));
620
        $this->assertCount(1, $invokable->get_invocations());
-
 
621
        $this->assertSame('Europe/Prague', core_date::get_user_timezone(99));
581
        $this->assertSame('Europe/Prague', core_date::get_user_timezone(99));
622
        $this->assertCount(2, $invokable->get_invocations());
-
 
623
        $this->assertSame('Etc/GMT-1', core_date::get_user_timezone(1));
-
 
624
        $this->assertCount(2, $invokable->get_invocations());
Línea 582... Línea 625...
582
        $this->assertSame('Etc/GMT-1', core_date::get_user_timezone(1));
625
        restore_error_handler();
583
 
626
 
584
        $this->setTimezone('Europe/Prague');
627
        $this->setTimezone('Europe/Prague');
585
        $CFG->forcetimezone = 'Pacific/Auckland';
628
        $CFG->forcetimezone = 'Pacific/Auckland';
Línea 591... Línea 634...
591
 
634
 
592
        $this->setTimezone('UTC');
635
        $this->setTimezone('UTC');
593
        $USER->timezone = 'Europe/London';
636
        $USER->timezone = 'Europe/London';
594
        $CFG->forcetimezone = 99;
637
        $CFG->forcetimezone = 99;
-
 
638
        $tz = new DateTimeZone('Pacific/Auckland');
-
 
639
        $invokable = self::get_invokable();
595
        $tz = new DateTimeZone('Pacific/Auckland');
640
        set_error_handler($invokable, E_ALL);
-
 
641
        $this->assertSame('Pacific/Auckland', core_date::get_user_timezone($tz));
-
 
642
        $this->assertCount(0, $invokable->get_invocations());
596
        $this->assertSame('Pacific/Auckland', core_date::get_user_timezone($tz));
643
        restore_error_handler();
Línea 597... Línea 644...
597
    }
644
    }
598
 
645
 
599
    /**
646
    /**
Línea 625... Línea 672...
625
            'string_c' => [
672
            'string_c' => [
626
                "1708405742",
673
                "1708405742",
627
                "%c",
674
                "%c",
628
                "20 February 2024 at 1:09 pm",
675
                "20 February 2024 at 1:09 pm",
629
            ],
676
            ],
-
 
677
            'Month Year only' => [
-
 
678
                "1708405742",
-
 
679
                "%B %Y",
-
 
680
                "February 2024",
-
 
681
            ],
-
 
682
            'Abbreviated Month Year only' => [
-
 
683
                "1708405742",
-
 
684
                "%b %Y",
-
 
685
                "Feb 2024",
-
 
686
            ],
-
 
687
            'DD Month Year' => [
-
 
688
                "1708405742",
-
 
689
                "%d %B %Y",
-
 
690
                "20 February 2024",
-
 
691
            ],
-
 
692
            'D Month Year' => [
-
 
693
                "1708405742",
-
 
694
                "%e %B %Y",
-
 
695
                "20 February 2024",
-
 
696
            ],
-
 
697
            'Abbreviated DD Month Year' => [
-
 
698
                "1708405742",
-
 
699
                "%d %b %Y",
-
 
700
                "20 Feb 2024",
-
 
701
            ],
-
 
702
            'Abbreviated D Month Year' => [
-
 
703
                "1708405742",
-
 
704
                "%e %b %Y",
-
 
705
                "20 Feb 2024",
-
 
706
            ],
630
            'numeric_c' => [
707
            'numeric_c' => [
631
                1708405742,
708
                1708405742,
632
                "%c",
709
                "%c",
633
                "20 February 2024 at 1:09 pm",
710
                "20 February 2024 at 1:09 pm",
634
            ],
711
            ],
Línea 664... Línea 741...
664
     * @param string $expected The output generated by strftime
741
     * @param string $expected The output generated by strftime
665
     */
742
     */
666
    public function test_strftime(mixed $input, string $format, string $expected): void {
743
    public function test_strftime(mixed $input, string $format, string $expected): void {
667
        $this->assertEqualsIgnoringWhitespace($expected, core_date::strftime($format, $input));
744
        $this->assertEqualsIgnoringWhitespace($expected, core_date::strftime($format, $input));
668
    }
745
    }
-
 
746
 
-
 
747
    /**
-
 
748
     * Data provider for ::test_strftime_locale.
-
 
749
     *
-
 
750
     * @return array[]
-
 
751
     */
-
 
752
    public static function get_strftime_locale_provider(): array {
-
 
753
        return [
-
 
754
            'Month Year only' => [
-
 
755
                "1728487000",
-
 
756
                'ru_RU.UTF-8',
-
 
757
                "%B %Y",
-
 
758
                "октябрь 2024",
-
 
759
            ],
-
 
760
            'DD Month Year' => [
-
 
761
                "1728487000",
-
 
762
                'ru_RU.UTF-8',
-
 
763
                "%d %B %Y",
-
 
764
                "09 октября 2024",
-
 
765
            ],
-
 
766
            'D Month Year' => [
-
 
767
                "1728487000",
-
 
768
                'ru_RU.UTF-8',
-
 
769
                "%e %B %Y",
-
 
770
                " 9 октября 2024",
-
 
771
            ],
-
 
772
            'Abbreviated Month Year only' => [
-
 
773
                "1728487000",
-
 
774
                'ru_RU.UTF-8',
-
 
775
                "%b %Y",
-
 
776
                "окт. 2024",
-
 
777
            ],
-
 
778
            'Abbreviated DD Month Year' => [
-
 
779
                "1728487000",
-
 
780
                'ru_RU.UTF-8',
-
 
781
                "%d %b %Y",
-
 
782
                "09 окт. 2024",
-
 
783
            ],
-
 
784
            'Abbreviated D Month Year' => [
-
 
785
                "1728487000",
-
 
786
                'ru_RU.UTF-8',
-
 
787
                "%e %b %Y",
-
 
788
                " 9 окт. 2024",
-
 
789
            ],
-
 
790
            'Invalid locale' => [
-
 
791
                1728487000,
-
 
792
                'xx',
-
 
793
                '%B %Y',
-
 
794
                'October 2024',
-
 
795
            ],
-
 
796
        ];
-
 
797
    }
-
 
798
 
-
 
799
    /**
-
 
800
     * Test \core_date::strftime function with alternate languages.
-
 
801
     *
-
 
802
     * @dataProvider get_strftime_locale_provider
-
 
803
     * @param mixed $input Input passed to strftime
-
 
804
     * @param string $locale The locale
-
 
805
     * @param string $format The date format to pass to strftime, falls back to '%c' if null
-
 
806
     * @param string $expected The output generated by strftime
-
 
807
     */
-
 
808
    public function test_strftime_locale(
-
 
809
        mixed $input,
-
 
810
        string $locale,
-
 
811
        string $format,
-
 
812
        string $expected,
-
 
813
    ): void {
-
 
814
        $this->assertEqualsIgnoringWhitespace(
-
 
815
            $expected,
-
 
816
            core_date::strftime($format, $input, $locale));
-
 
817
    }
669
}
818
}