| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * |
| 5 | * @group api |
| 6 | */ |
| 7 | class Tests_API_Locale_Banner extends WP_UnitTestCase { |
| 8 | |
| 9 | /** |
| 10 | * @dataProvider data_locale_banner |
| 11 | * |
| 12 | * @type string $header 'Accept-Language' header value. |
| 13 | * @type array $expected_sites Rosetta sites that should be present in the banner text. |
| 14 | */ |
| 15 | function test_locale_banner( $header, $expected_sites ) { |
| 16 | $response = wp_remote_post( 'https://wordpress.org/plugins-wp/wp-json/plugins/v1/locale-banner', array( |
| 17 | 'headers' => array( |
| 18 | 'Accept-Language' => $header |
| 19 | ), |
| 20 | ) ); |
| 21 | |
| 22 | $data = json_decode( wp_remote_retrieve_body( $response ), true ); |
| 23 | |
| 24 | $this->assertArrayHasKey( 'suggest_string', $data ); |
| 25 | $this->assertArrayHasKey( 'translated', $data ); |
| 26 | $this->assertInternalType( 'array', $data['translated'] ); |
| 27 | |
| 28 | preg_match_all( '#[a-z-]+.wordpress.org#', $data['suggest_string'], $sites ); |
| 29 | |
| 30 | $this->assertNotEmpty( $sites ); |
| 31 | $this->assertEquals( $expected_sites, $sites[0] ); |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Data provider for test_locale_banner(). |
| 36 | * |
| 37 | * @return array { |
| 38 | * @type array { |
| 39 | * @type string $header 'Accept-Language' header value. |
| 40 | * @type array $expected_sites Rosetta sites that should be present in the banner text. |
| 41 | * } |
| 42 | * } |
| 43 | */ |
| 44 | function data_locale_banner() { |
| 45 | return array( |
| 46 | array( |
| 47 | 'en-US,en;q=0.8,ru;q=0.6,cs;q=0.4', |
| 48 | array( 'ru.wordpress.org', 'cs.wordpress.org' ), |
| 49 | ), |
| 50 | array( |
| 51 | 'en-US,en;q=0.8,da;q=0.6,nb;q=0.4,sv;q=0.2', |
| 52 | array( 'da.wordpress.org', 'nb.wordpress.org', 'sv.wordpress.org' ), |
| 53 | ), |
| 54 | ); |
| 55 | } |
| 56 | |
| 57 | } |