|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Tests for the wp_timezone_choice() function. |
| 5 | + * |
| 6 | + * @group functions |
| 7 | + * |
| 8 | + * @covers ::wp_timezone_choice |
| 9 | + */ |
| 10 | +class Tests_Functions_WpTimezoneChoice extends WP_UnitTestCase { |
| 11 | + |
| 12 | + /** |
| 13 | + * Restores the current locale after each test runs. |
| 14 | + */ |
| 15 | + public function tear_down(): void { |
| 16 | + restore_current_locale(); |
| 17 | + parent::tear_down(); |
| 18 | + } |
| 19 | + |
| 20 | + /** |
| 21 | + * Tests default values. |
| 22 | + * |
| 23 | + * @ticket 59941 |
| 24 | + * @dataProvider data_wp_timezone_choice |
| 25 | + * |
| 26 | + * @param string $expected Expected string HTML fragment. |
| 27 | + */ |
| 28 | + public function test_wp_timezone_choice( string $expected ): void { |
| 29 | + $timezone_list = wp_timezone_choice( '' ); |
| 30 | + $this->assertStringContainsString( $expected, $timezone_list ); |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * Data provider for test_wp_timezone_choice(). |
| 35 | + * |
| 36 | + * @return array<string, array{ 0: string }> |
| 37 | + */ |
| 38 | + public function data_wp_timezone_choice(): array { |
| 39 | + return array( |
| 40 | + 'placeholder option' => array( '<option selected="selected" value="">Select a city</option>' ), |
| 41 | + 'city in Americas' => array( '<option value="America/Los_Angeles" dir="auto">Los Angeles</option>' ), |
| 42 | + 'deprecated timezone' => array( '<option value="Pacific/Honolulu" dir="auto">Honolulu</option>' ), |
| 43 | + 'manual offset example' => array( '<option value="UTC-8" dir="auto">UTC-8</option>' ), |
| 44 | + 'UTC option' => array( '<option value="UTC" dir="auto">UTC</option>' ), |
| 45 | + 'continent example' => array( '<option value="Africa/Johannesburg" dir="auto">Johannesburg</option>' ), |
| 46 | + 'city example' => array( '<option value="Asia/Kuala_Lumpur" dir="auto">Kuala Lumpur</option>' ), |
| 47 | + 'city with sub-city' => array( '<option value="America/Argentina/Buenos_Aires" dir="auto">Argentina - Buenos Aires</option>' ), |
| 48 | + 'translated city name appears' => array( '<option value="Pacific/Port_Moresby" dir="auto">Port Moresby</option>' ), |
| 49 | + ); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Tests zones are selected from the list. |
| 54 | + * |
| 55 | + * @ticket 59941 |
| 56 | + * @dataProvider data_wp_timezone_choice_selected |
| 57 | + * |
| 58 | + * @param string $selected_zone The timezone to select. |
| 59 | + * @param string $expected Expected string HTML fragment. |
| 60 | + */ |
| 61 | + public function test_wp_timezone_choice_selected( string $selected_zone, string $expected ): void { |
| 62 | + $actual = wp_timezone_choice( $selected_zone ); |
| 63 | + $this->assertStringContainsString( $expected, $actual ); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Data provider for test_wp_timezone_choice_selected(). |
| 68 | + * |
| 69 | + * @return array<string, array{ 0: string }> |
| 70 | + */ |
| 71 | + public function data_wp_timezone_choice_selected(): array { |
| 72 | + return array( |
| 73 | + 'city from the list' => array( |
| 74 | + 'America/Los_Angeles', |
| 75 | + '<option selected="selected" value="America/Los_Angeles" dir="auto">Los Angeles</option>', |
| 76 | + ), |
| 77 | + 'deprecated but valid timezone string' => array( |
| 78 | + 'Pacific/Auckland', |
| 79 | + '<option selected="selected" value="Pacific/Auckland" dir="auto">Auckland</option>', |
| 80 | + ), |
| 81 | + 'UTC' => array( |
| 82 | + 'UTC', |
| 83 | + '<option selected="selected" value="UTC" dir="auto">UTC</option>', |
| 84 | + ), |
| 85 | + 'manual UTC offset' => array( |
| 86 | + 'UTC+10', |
| 87 | + '<option selected="selected" value="UTC+10" dir="auto">UTC+10</option>', |
| 88 | + ), |
| 89 | + ); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Tests passing in the locale. |
| 94 | + * |
| 95 | + * @ticket 59941 |
| 96 | + * @dataProvider data_wp_timezone_choice_es |
| 97 | + * |
| 98 | + * @param string $expected Expected string HTML fragment. |
| 99 | + */ |
| 100 | + public function test_wp_timezone_choice_es( string $expected ): void { |
| 101 | + $timezone_list = wp_timezone_choice( '', 'es_ES' ); |
| 102 | + $this->assertStringContainsString( $expected, $timezone_list ); |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Data provider for test_wp_timezone_choice_es(). |
| 107 | + * |
| 108 | + * @return array<string, array{ 0: string }> |
| 109 | + */ |
| 110 | + public function data_wp_timezone_choice_es(): array { |
| 111 | + return array( |
| 112 | + 'placeholder remains in English (no translation override passed)' => array( '<option selected="selected" value="">Select a city</option>' ), |
| 113 | + 'spanish city translation' => array( '<option value="Pacific/Port_Moresby" dir="auto">Puerto Moresby</option>' ), |
| 114 | + 'spanish optgroup Arctic' => array( '<optgroup label="Ártico" dir="auto">' ), |
| 115 | + 'spanish optgroup Manual Offsets untranslated' => array( '<optgroup label="Manual Offsets" dir="auto">' ), |
| 116 | + ); |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Tests setting the locale globally. |
| 121 | + * |
| 122 | + * @ticket 59941 |
| 123 | + * @dataProvider data_wp_timezone_choice_es_set |
| 124 | + * |
| 125 | + * @param string $expected Expected string HTML fragment. |
| 126 | + */ |
| 127 | + public function test_wp_timezone_choice_es_set( string $expected ): void { |
| 128 | + switch_to_locale( 'es_ES' ); |
| 129 | + $timezone_list = wp_timezone_choice( '' ); |
| 130 | + $this->assertStringContainsString( $expected, $timezone_list ); |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * Data provider for test_wp_timezone_choice_es_set(). |
| 135 | + * |
| 136 | + * @return array<string, array{ 0: string }> |
| 137 | + */ |
| 138 | + public static function data_wp_timezone_choice_es_set(): array { |
| 139 | + return array( |
| 140 | + 'placeholder in Spanish' => array( '<option selected="selected" value="">Elige una ciudad</option>' ), |
| 141 | + 'spanish city translation' => array( '<option value="Pacific/Port_Moresby" dir="auto">Puerto Moresby</option>' ), |
| 142 | + 'spanish optgroup Arctic' => array( '<optgroup label="Ártico" dir="auto">' ), |
| 143 | + 'spanish optgroup Manual Offsets' => array( '<optgroup label="Compensaciones manuales" dir="auto">' ), |
| 144 | + ); |
| 145 | + } |
| 146 | +} |
0 commit comments