Make WordPress Core

Ticket #45528: 45528.5.patch

File 45528.5.patch, 4.6 KB (added by akirk, 4 years ago)

Improve unit test

  • src/wp-includes/l10n.php

     
    924924
    925925        $obj = $wp_scripts->registered[ $handle ];
    926926
     927        $src = $obj->src;
     928        if ( ! preg_match( '|^(https?:)?//|', $src ) && ! ( $wp_scripts->content_url && 0 === strpos( $src, $wp_scripts->content_url ) ) ) {
     929                $src = $wp_scripts->base_url . $src;
     930        }
    927931        /** This filter is documented in wp-includes/class.wp-scripts.php */
    928         $src = esc_url( apply_filters( 'script_loader_src', $obj->src, $handle ) );
     932        $src = esc_url( apply_filters( 'script_loader_src', $src, $handle ) );
    929933
    930934        $relative       = false;
    931935        $languages_path = WP_LANG_DIR;
     
    937941        // If the host is the same or it's a relative URL.
    938942        if (
    939943                strpos( $src_url['path'], $content_url['path'] ) === 0 &&
    940                 ( ! isset( $src_url['host'] ) || $src_url['host'] !== $content_url['host'] )
     944                ( ! isset( $src_url['host'] ) || $src_url['host'] === $content_url['host'] )
    941945        ) {
    942946                // Make the src relative the specific plugin or theme.
    943                 $relative = trim( substr( $src, strlen( $content_url['path'] ) ), '/' );
     947                $relative = trim( substr( $src_url['path'], strlen( $content_url['path'] ) ), '/' );
    944948                $relative = explode( '/', $relative );
    945949
    946950                $languages_path = WP_LANG_DIR . '/' . $relative[0];
     
    947951
    948952                $relative = array_slice( $relative, 2 );
    949953                $relative = implode( '/', $relative );
    950         } elseif ( ! isset( $src_url['host'] ) || $src_url['host'] !== $site_url['host'] ) {
     954        } elseif ( ! isset( $src_url['host'] ) || $src_url['host'] === $site_url['host'] ) {
    951955                if ( ! isset( $site_url['path'] ) ) {
    952956                        $relative = trim( $src_url['path'], '/' );
    953                 } elseif ( ( strpos( $src_url['path'], $site_url['path'] ) === 0 ) ) {
     957                } elseif ( ( strpos( $src_url['path'], trailingslashit( $site_url['path'] ) ) === 0 ) ) {
    954958                        // Make the src relative to the WP root.
    955                         $relative = substr( $src, strlen( $site_url['path'] ) );
     959                        $relative = substr( $src_url['path'], strlen( $site_url['path'] ) );
    956960                        $relative = trim( $relative, '/' );
    957961                }
     962        } elseif ( isset( $src_url['host'] ) && $src_url['host'] !== $site_url['host'] && false !== strpos( $src_url['path'], '/wp-includes/' ) ) {
     963                // This handles CDNs that mirror all static assets.
     964                $relative = substr( $src_url['path'], 1 + strpos( $src_url['path'], '/wp-includes/' ) );
    958965        }
    959966
    960967        // If the source is not from WP.
  • tests/phpunit/tests/l10n/loadScriptTextdomain.php

     
     1<?php
     2
     3/**
     4 * @group l10n
     5 * @group i18n
     6 */
     7class Tests_L10n_loadScriptTextdomain extends WP_UnitTestCase {
     8        public function siteurl_example_org() {
     9                return 'http://example.org/';
     10        }
     11
     12        public function siteurl_example_org_wp() {
     13                return 'http://example.org/wp/';
     14        }
     15
     16        /**
     17         * @ticket 45528
     18         */
     19        public function test_resolve_relative_path() {
     20                $json_translations = file_get_contents( DIR_TESTDATA . '/languages/en_US-813e104eb47e13dd4cc5af844c618754.json' );
     21
     22                // Test for a WordPress install in root.
     23                add_filter( 'pre_option_siteurl', array( $this, 'siteurl_example_org' ) );
     24
     25                wp_enqueue_script( 'test-example-root', '/wp-includes/js/script.js', array(), null );
     26                $this->assertEquals( $json_translations, load_script_textdomain( 'test-example-root', 'default', DIR_TESTDATA . '/languages' ) );
     27
     28                // Assets on a CDN.
     29                wp_enqueue_script( 'test-example-cdn', 'https://c0.wp.com/c/5.0.1/wp-includes/js/script.js', array(), null );
     30                $this->assertEquals( $json_translations, load_script_textdomain( 'test-example-cdn', 'default', DIR_TESTDATA . '/languages' ) );
     31
     32                // Test for WordPress installs in a subdirectory.
     33                remove_filter( 'option_get_siteurl', array( $this, 'siteurl_example_org' ) );
     34                add_filter( 'pre_option_siteurl', array( $this, 'siteurl_example_org_wp' ) );
     35
     36                wp_enqueue_script( 'test-example-subdir', '/wp/wp-includes/js/script.js', array(), null );
     37                $this->assertEquals( $json_translations, load_script_textdomain( 'test-example-subdir', 'default', DIR_TESTDATA . '/languages' ) );
     38
     39                // Assets on a CDN.
     40                wp_enqueue_script( 'test-example-cdn-subdir', 'https://c0.wp.com/c/5.0.1/wp-includes/js/script.js', array(), null );
     41                $this->assertEquals( $json_translations, load_script_textdomain( 'test-example-cdn-subdir', 'default', DIR_TESTDATA . '/languages' ) );
     42
     43                remove_filter( 'option_get_siteurl', array( $this, 'siteurl_example_org_wp' ) );
     44        }
     45}