Making WordPress.org

Ticket #1850: meta-1850.2.patch

File meta-1850.2.patch, 3.2 KB (added by SergeyBiryukov, 8 years ago)
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/api/routes/class-locale-banner.php

     
    6868                $current_locale = get_locale();
    6969
    7070                // Build a list of WordPress locales which we'll suggest to the user.
    71                 $suggest_locales = array_values( array_intersect( $translated_locales, $locales_from_header ) );
     71                $suggest_locales = array_values( array_intersect( $locales_from_header, $translated_locales ) );
    7272                $current_locale_is_suggested = in_array( $current_locale, $suggest_locales );
    7373                $current_locale_is_translated = in_array( $current_locale, $translated_locales );
    7474
     
    262262                }
    263263
    264264                foreach ( $available_locales as $locale ) {
    265                         list( $lang, ) = preg_split( '/[_-]/', $locale );
    266                         if ( $lang ) {
     265                        list( $locale_lang, ) = preg_split( '/[_-]/', $locale );
     266                        if ( $lang === $locale_lang ) {
    267267                                return $locale;
    268268                        }
    269269                }
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/tests/phpunit/tests/api/locale-banner.php

     
     1<?php
     2
     3/**
     4 *
     5 * @group api
     6 */
     7class 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}