Make WordPress Core


Ignore:
Timestamp:
08/25/2024 11:47:01 PM (2 years ago)
Author:
peterwilsoncc
Message:

Script Loader: Refactor Etag generation for concatenated assets.

Move Etag HTTP header generation in load-scripts.php and load-styles.php to WP_Dependencies.

Introduces the method WP_Dependencies::get_etag() and associated unit tests.

Follow up to [57943].

Props vrajadas, martinkrcho, mukesh27.
Fixes #61485.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/dependencies.php

    r56548 r58935  
    149149                $this->assertContains( 'one', $dep->queue );
    150150        }
     151
     152        /**
     153         * Data provider for test_get_etag.
     154         *
     155         * @return array[]
     156         */
     157        public function data_provider_get_etag() {
     158                return array(
     159                        'should accept one dependency'              => array(
     160                                'load'               => array(
     161                                        'abcd' => '1.0.2',
     162                                ),
     163                                'hash_source_string' => 'WP:6.7;abcd:1.0.2;',
     164                                'expected'           => 'W/"8145d7e3c41d5a9cc2bccba4afa861fc"',
     165                        ),
     166                        'should accept empty array of dependencies' => array(
     167                                'load'               => array(),
     168                                'hash_source_string' => 'WP:6.7;',
     169                                'expected'           => 'W/"7ee896c19250a3d174f11469a4ad0b1e"',
     170                        ),
     171                );
     172        }
     173
     174        /**
     175         * Tests get_etag method for WP_Scripts.
     176         *
     177         * @ticket 58433
     178         * @ticket 61485
     179         *
     180         * @covers WP_Dependencies::get_etag
     181         *
     182         * @dataProvider data_provider_get_etag
     183         *
     184         * @param array  $load               List of scripts to load.
     185         * @param string $hash_source_string Hash source string.
     186         * @param string $expected           Expected etag.
     187         */
     188        public function test_get_etag_scripts( $load, $hash_source_string, $expected ) {
     189                global $wp_version;
     190                // Modify global to avoid tests needing to change with each new version of WordPress.
     191                $original_wp_version = $wp_version;
     192                $wp_version          = '6.7';
     193                $instance            = wp_scripts();
     194
     195                foreach ( $load as $handle => $ver ) {
     196                        // The src should not be empty.
     197                        wp_enqueue_script( $handle, 'https://example.org', array(), $ver );
     198                }
     199
     200                $result = $instance->get_etag( array_keys( $load ) );
     201
     202                // Restore global prior to making assertions.
     203                $wp_version = $original_wp_version;
     204
     205                $this->assertSame( $expected, $result, "Expected MD hash: $expected for $hash_source_string, but got: $result." );
     206        }
     207
     208        /**
     209         * Tests get_etag method for WP_Styles.
     210         *
     211         * @ticket 58433
     212         * @ticket 61485
     213         *
     214         * @covers WP_Dependencies::get_etag
     215         *
     216         * @dataProvider data_provider_get_etag
     217         *
     218         * @param array  $load               List of styles to load.
     219         * @param string $hash_source_string Hash source string.
     220         * @param string $expected           Expected etag.
     221         */
     222        public function test_get_etag_styles( $load, $hash_source_string, $expected ) {
     223                global $wp_version;
     224                // Modify global to avoid tests needing to change with each new version of WordPress.
     225                $original_wp_version = $wp_version;
     226                $wp_version          = '6.7';
     227                $instance            = wp_scripts();
     228
     229                foreach ( $load as $handle => $ver ) {
     230                        // The src should not be empty.
     231                        wp_enqueue_style( $handle, 'https://example.cdn', array(), $ver );
     232                }
     233
     234                $result = $instance->get_etag( array_keys( $load ) );
     235
     236                // Restore global prior to making assertions.
     237                $wp_version = $original_wp_version;
     238
     239                $this->assertSame( $expected, $result, "Expected MD hash: $expected for $hash_source_string, but got: $result." );
     240        }
    151241}
Note: See TracChangeset for help on using the changeset viewer.