Make WordPress Core


Ignore:
Timestamp:
01/23/2024 03:32:03 AM (5 weeks ago)
Author:
dmsnell
Message:

Script Modules API: Rename wp_module to wp_script_module

Renames all mentions to "module" with "script module", including function names, comments, and tests.

Follow up to [57269]

The list of functions renamed are:

  • wp_module() -> wp_script_module().
  • wp_register_module() -> wp_register_script_module().
  • wp_enqueue_module() -> wp_enqueue_script_module().
  • wp_dequeue_module() -> wp_dequeue_script_module().
  • WP_Script_Modules::print_enqueued_modules() -> WP_Script_Modules::print_enqueued_script_modules().
  • WP_Script_Modules::print_module_preloads() -> WP_Script_Modules::print_script_module_preloads().

It also adds PHP 7 typing to all the functions and improves the types of the $deps argument of wp_register_script_module() and wp_enqueue_script_module() using @type.

Props luisherranz, idad5, costdev, nefff, joemcgill, jorbin, swisspidy, jonsurrel, flixos90, gziolo, westonruter, bernhard-reiter, kamranzafar4343
See #56313

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/script-modules/wpScriptModules.php

    r57269 r57327  
    1818     * @var WP_Script_Modules
    1919     */
    20     protected $modules;
     20    protected $script_modules;
    2121
    2222    /**
     
    2525    public function set_up() {
    2626        parent::set_up();
    27         $this->modules = new WP_Script_Modules();
    28     }
    29 
    30     /**
    31      * Gets a list of the enqueued modules.
    32      *
    33      * @return array Enqueued module URLs, keyed by module identifier.
    34      */
    35     public function get_enqueued_modules() {
    36         $modules_markup   = get_echo( array( $this->modules, 'print_enqueued_modules' ) );
    37         $p                = new WP_HTML_Tag_Processor( $modules_markup );
    38         $enqueued_modules = array();
    39 
    40         while ( $p->next_tag(
    41             array(
    42                 'tag'    => 'SCRIPT',
    43                 'import' => 'module',
    44             )
    45         ) ) {
    46             $id                      = preg_replace( '/-js-module$/', '', $p->get_attribute( 'id' ) );
    47             $enqueued_modules[ $id ] = $p->get_attribute( 'src' );
     27        $this->script_modules = new WP_Script_Modules();
     28    }
     29
     30    /**
     31     * Gets a list of the enqueued script modules.
     32     *
     33     * @return array Enqueued script module URLs, keyed by script module identifier.
     34     */
     35    public function get_enqueued_script_modules() {
     36        $script_modules_markup   = get_echo( array( $this->script_modules, 'print_enqueued_script_modules' ) );
     37        $p                       = new WP_HTML_Tag_Processor( $script_modules_markup );
     38        $enqueued_script_modules = array();
     39
     40        while ( $p->next_tag( array( 'tag' => 'SCRIPT' ) ) ) {
     41            if ( 'module' === $p->get_attribute( 'type' ) ) {
     42                $id                             = preg_replace( '/-js-module$/', '', $p->get_attribute( 'id' ) );
     43                $enqueued_script_modules[ $id ] = $p->get_attribute( 'src' );
     44            }
    4845        }
    4946
    50         return $enqueued_modules;
    51     }
    52 
    53     /**
    54      * Gets the modules listed in the import map.
    55      *
    56      * @return array Import map entry URLs, keyed by module identifier.
     47        return $enqueued_script_modules;
     48    }
     49
     50    /**
     51     * Gets the script modules listed in the import map.
     52     *
     53     * @return array Import map entry URLs, keyed by script module identifier.
    5754     */
    5855    public function get_import_map() {
    59         $import_map_markup = get_echo( array( $this->modules, 'print_import_map' ) );
     56        $import_map_markup = get_echo( array( $this->script_modules, 'print_import_map' ) );
    6057        preg_match( '/<script type="importmap" id="wp-importmap">.*?(\{.*\}).*?<\/script>/s', $import_map_markup, $import_map_string );
    6158        return json_decode( $import_map_string[1], true )['imports'];
     
    6360
    6461    /**
    65      * Gets a list of preloaded modules.
    66      *
    67      * @return array Preloaded module URLs, keyed by module identifier.
    68      */
    69     public function get_preloaded_modules() {
    70         $preloaded_markup  = get_echo( array( $this->modules, 'print_module_preloads' ) );
    71         $p                 = new WP_HTML_Tag_Processor( $preloaded_markup );
    72         $preloaded_modules = array();
    73 
    74         while ( $p->next_tag(
    75             array(
    76                 'tag' => 'LINK',
    77                 'rel' => 'modulepreload',
    78             )
    79         ) ) {
    80             $id                       = preg_replace( '/-js-modulepreload$/', '', $p->get_attribute( 'id' ) );
    81             $preloaded_modules[ $id ] = $p->get_attribute( 'href' );
     62     * Gets a list of preloaded script modules.
     63     *
     64     * @return array Preloaded script module URLs, keyed by script module identifier.
     65     */
     66    public function get_preloaded_script_modules() {
     67        $preloaded_markup         = get_echo( array( $this->script_modules, 'print_script_module_preloads' ) );
     68        $p                        = new WP_HTML_Tag_Processor( $preloaded_markup );
     69        $preloaded_script_modules = array();
     70
     71        while ( $p->next_tag( array( 'tag' => 'LINK' ) ) ) {
     72            if ( 'modulepreload' === $p->get_attribute( 'rel' ) ) {
     73                $id                              = preg_replace( '/-js-modulepreload$/', '', $p->get_attribute( 'id' ) );
     74                $preloaded_script_modules[ $id ] = $p->get_attribute( 'href' );
     75            }
    8276        }
    8377
    84         return $preloaded_modules;
    85     }
    86 
    87     /**
    88      * Tests that a module gets enqueued correctly after being registered.
    89      *
    90      * @ticket 56313
    91      *
    92      * @covers ::register()
    93      * @covers ::enqueue()
    94      * @covers ::print_enqueued_modules()
    95      */
    96     public function test_wp_enqueue_module() {
    97         $this->modules->register( 'foo', '/foo.js' );
    98         $this->modules->register( 'bar', '/bar.js' );
    99         $this->modules->enqueue( 'foo' );
    100         $this->modules->enqueue( 'bar' );
    101 
    102         $enqueued_modules = $this->get_enqueued_modules();
    103 
    104         $this->assertCount( 2, $enqueued_modules );
    105         $this->assertStringStartsWith( '/foo.js', $enqueued_modules['foo'] );
    106         $this->assertStringStartsWith( '/bar.js', $enqueued_modules['bar'] );
    107     }
    108 
    109     /**
    110     * Tests that a module can be dequeued after being enqueued.
     78        return $preloaded_script_modules;
     79    }
     80
     81    /**
     82     * Tests that a script module gets enqueued correctly after being registered.
     83     *
     84     * @ticket 56313
     85     *
     86     * @covers ::register()
     87     * @covers ::enqueue()
     88     * @covers ::print_enqueued_script_modules()
     89     */
     90    public function test_wp_enqueue_script_module() {
     91        $this->script_modules->register( 'foo', '/foo.js' );
     92        $this->script_modules->register( 'bar', '/bar.js' );
     93        $this->script_modules->enqueue( 'foo' );
     94        $this->script_modules->enqueue( 'bar' );
     95
     96        $enqueued_script_modules = $this->get_enqueued_script_modules();
     97
     98        $this->assertCount( 2, $enqueued_script_modules );
     99        $this->assertStringStartsWith( '/foo.js', $enqueued_script_modules['foo'] );
     100        $this->assertStringStartsWith( '/bar.js', $enqueued_script_modules['bar'] );
     101    }
     102
     103    /**
     104    * Tests that a script module can be dequeued after being enqueued.
    111105    *
    112106    * @ticket 56313
     
    115109    * @covers ::enqueue()
    116110    * @covers ::dequeue()
    117     * @covers ::print_enqueued_modules()
     111    * @covers ::print_enqueued_script_modules()
    118112    */
    119     public function test_wp_dequeue_module() {
    120         $this->modules->register( 'foo', '/foo.js' );
    121         $this->modules->register( 'bar', '/bar.js' );
    122         $this->modules->enqueue( 'foo' );
    123         $this->modules->enqueue( 'bar' );
    124         $this->modules->dequeue( 'foo' ); // Dequeued.
    125 
    126         $enqueued_modules = $this->get_enqueued_modules();
    127 
    128         $this->assertCount( 1, $enqueued_modules );
    129         $this->assertFalse( isset( $enqueued_modules['foo'] ) );
    130         $this->assertTrue( isset( $enqueued_modules['bar'] ) );
    131     }
    132 
    133     /**
    134     * Tests that a module can be enqueued before it is registered, and will be
    135     * handled correctly once registered.
     113    public function test_wp_dequeue_script_module() {
     114        $this->script_modules->register( 'foo', '/foo.js' );
     115        $this->script_modules->register( 'bar', '/bar.js' );
     116        $this->script_modules->enqueue( 'foo' );
     117        $this->script_modules->enqueue( 'bar' );
     118        $this->script_modules->dequeue( 'foo' ); // Dequeued.
     119
     120        $enqueued_script_modules = $this->get_enqueued_script_modules();
     121
     122        $this->assertCount( 1, $enqueued_script_modules );
     123        $this->assertFalse( isset( $enqueued_script_modules['foo'] ) );
     124        $this->assertTrue( isset( $enqueued_script_modules['bar'] ) );
     125    }
     126
     127    /**
     128    * Tests that a script module can be enqueued before it is registered, and will
     129    * be handled correctly once registered.
    136130    *
    137131    * @ticket 56313
     
    139133    * @covers ::register()
    140134    * @covers ::enqueue()
    141     * @covers ::print_enqueued_modules()
     135    * @covers ::print_enqueued_script_modules()
    142136    */
    143     public function test_wp_enqueue_module_works_before_register() {
    144         $this->modules->enqueue( 'foo' );
    145         $this->modules->register( 'foo', '/foo.js' );
    146         $this->modules->enqueue( 'bar' ); // Not registered.
    147 
    148         $enqueued_modules = $this->get_enqueued_modules();
    149 
    150         $this->assertCount( 1, $enqueued_modules );
    151         $this->assertStringStartsWith( '/foo.js', $enqueued_modules['foo'] );
    152         $this->assertFalse( isset( $enqueued_modules['bar'] ) );
    153     }
    154 
    155     /**
    156      * Tests that a module can be dequeued before it is registered and ensures
    157      * that it is not enqueued after registration.
     137    public function test_wp_enqueue_script_module_works_before_register() {
     138        $this->script_modules->enqueue( 'foo' );
     139        $this->script_modules->register( 'foo', '/foo.js' );
     140        $this->script_modules->enqueue( 'bar' ); // Not registered.
     141
     142        $enqueued_script_modules = $this->get_enqueued_script_modules();
     143
     144        $this->assertCount( 1, $enqueued_script_modules );
     145        $this->assertStringStartsWith( '/foo.js', $enqueued_script_modules['foo'] );
     146        $this->assertFalse( isset( $enqueued_script_modules['bar'] ) );
     147    }
     148
     149    /**
     150     * Tests that a script module can be dequeued before it is registered and
     151     * ensures that it is not enqueued after registration.
    158152     *
    159153     * @ticket 56313
     
    162156     * @covers ::enqueue()
    163157     * @covers ::dequeue()
    164      * @covers ::print_enqueued_modules()
    165      */
    166     public function test_wp_dequeue_module_works_before_register() {
    167         $this->modules->enqueue( 'foo' );
    168         $this->modules->enqueue( 'bar' );
    169         $this->modules->dequeue( 'foo' );
    170         $this->modules->register( 'foo', '/foo.js' );
    171         $this->modules->register( 'bar', '/bar.js' );
    172 
    173         $enqueued_modules = $this->get_enqueued_modules();
    174 
    175         $this->assertCount( 1, $enqueued_modules );
    176         $this->assertFalse( isset( $enqueued_modules['foo'] ) );
    177         $this->assertTrue( isset( $enqueued_modules['bar'] ) );
     158     * @covers ::print_enqueued_script_modules()
     159     */
     160    public function test_wp_dequeue_script_module_works_before_register() {
     161        $this->script_modules->enqueue( 'foo' );
     162        $this->script_modules->enqueue( 'bar' );
     163        $this->script_modules->dequeue( 'foo' );
     164        $this->script_modules->register( 'foo', '/foo.js' );
     165        $this->script_modules->register( 'bar', '/bar.js' );
     166
     167        $enqueued_script_modules = $this->get_enqueued_script_modules();
     168
     169        $this->assertCount( 1, $enqueued_script_modules );
     170        $this->assertFalse( isset( $enqueued_script_modules['foo'] ) );
     171        $this->assertTrue( isset( $enqueued_script_modules['bar'] ) );
    178172    }
    179173
    180174    /**
    181175     * Tests that dependencies for a registered module are added to the import map
    182      * when the module is enqueued.
     176     * when the script module is enqueued.
    183177     *
    184178     * @ticket 56313
     
    189183     */
    190184    public function test_wp_import_map_dependencies() {
    191         $this->modules->register( 'foo', '/foo.js', array( 'dep' ) );
    192         $this->modules->register( 'dep', '/dep.js' );
    193         $this->modules->register( 'no-dep', '/no-dep.js' );
    194         $this->modules->enqueue( 'foo' );
     185        $this->script_modules->register( 'foo', '/foo.js', array( 'dep' ) );
     186        $this->script_modules->register( 'dep', '/dep.js' );
     187        $this->script_modules->register( 'no-dep', '/no-dep.js' );
     188        $this->script_modules->enqueue( 'foo' );
    195189
    196190        $import_map = $this->get_import_map();
     
    203197    /**
    204198     * Tests that dependencies are not duplicated in the import map when multiple
    205      * modules require the same dependency.
     199     * script modules require the same dependency.
    206200     *
    207201     * @ticket 56313
     
    212206     */
    213207    public function test_wp_import_map_no_duplicate_dependencies() {
    214         $this->modules->register( 'foo', '/foo.js', array( 'dep' ) );
    215         $this->modules->register( 'bar', '/bar.js', array( 'dep' ) );
    216         $this->modules->register( 'dep', '/dep.js' );
    217         $this->modules->enqueue( 'foo' );
    218         $this->modules->enqueue( 'bar' );
     208        $this->script_modules->register( 'foo', '/foo.js', array( 'dep' ) );
     209        $this->script_modules->register( 'bar', '/bar.js', array( 'dep' ) );
     210        $this->script_modules->register( 'dep', '/dep.js' );
     211        $this->script_modules->enqueue( 'foo' );
     212        $this->script_modules->enqueue( 'bar' );
    219213
    220214        $import_map = $this->get_import_map();
     
    235229     */
    236230    public function test_wp_import_map_recursive_dependencies() {
    237         $this->modules->register(
     231        $this->script_modules->register(
    238232            'foo',
    239233            '/foo.js',
     
    246240            )
    247241        );
    248         $this->modules->register(
     242        $this->script_modules->register(
    249243            'static-dep',
    250244            '/static-dep.js',
     
    260254            )
    261255        );
    262         $this->modules->register( 'dynamic-dep', '/dynamic-dep.js' );
    263         $this->modules->register( 'nested-static-dep', '/nested-static-dep.js' );
    264         $this->modules->register( 'nested-dynamic-dep', '/nested-dynamic-dep.js' );
    265         $this->modules->register( 'no-dep', '/no-dep.js' );
    266         $this->modules->enqueue( 'foo' );
     256        $this->script_modules->register( 'dynamic-dep', '/dynamic-dep.js' );
     257        $this->script_modules->register( 'nested-static-dep', '/nested-static-dep.js' );
     258        $this->script_modules->register( 'nested-dynamic-dep', '/nested-dynamic-dep.js' );
     259        $this->script_modules->register( 'no-dep', '/no-dep.js' );
     260        $this->script_modules->enqueue( 'foo' );
    267261
    268262        $import_map = $this->get_import_map();
     
    286280     */
    287281    public function test_wp_import_map_doesnt_print_if_no_dependencies() {
    288         $this->modules->register( 'foo', '/foo.js' ); // No deps.
    289         $this->modules->enqueue( 'foo' );
    290 
    291         $import_map_markup = get_echo( array( $this->modules, 'print_import_map' ) );
     282        $this->script_modules->register( 'foo', '/foo.js' ); // No deps.
     283        $this->script_modules->enqueue( 'foo' );
     284
     285        $import_map_markup = get_echo( array( $this->script_modules, 'print_import_map' ) );
    292286
    293287        $this->assertEmpty( $import_map_markup );
     
    302296     * @covers ::register()
    303297     * @covers ::enqueue()
    304      * @covers ::print_module_preloads()
     298     * @covers ::print_script_module_preloads()
    305299     */
    306300    public function test_wp_enqueue_preloaded_static_dependencies() {
    307         $this->modules->register(
     301        $this->script_modules->register(
    308302            'foo',
    309303            '/foo.js',
     
    316310            )
    317311        );
    318         $this->modules->register(
     312        $this->script_modules->register(
    319313            'static-dep',
    320314            '/static-dep.js',
     
    330324            )
    331325        );
    332         $this->modules->register( 'dynamic-dep', '/dynamic-dep.js' );
    333         $this->modules->register( 'nested-static-dep', '/nested-static-dep.js' );
    334         $this->modules->register( 'nested-dynamic-dep', '/nested-dynamic-dep.js' );
    335         $this->modules->register( 'no-dep', '/no-dep.js' );
    336         $this->modules->enqueue( 'foo' );
    337 
    338         $preloaded_modules = $this->get_preloaded_modules();
    339 
    340         $this->assertCount( 2, $preloaded_modules );
    341         $this->assertStringStartsWith( '/static-dep.js', $preloaded_modules['static-dep'] );
    342         $this->assertStringStartsWith( '/nested-static-dep.js', $preloaded_modules['nested-static-dep'] );
    343         $this->assertFalse( isset( $preloaded_modules['no-dep'] ) );
    344         $this->assertFalse( isset( $preloaded_modules['dynamic-dep'] ) );
    345         $this->assertFalse( isset( $preloaded_modules['nested-dynamic-dep'] ) );
     326        $this->script_modules->register( 'dynamic-dep', '/dynamic-dep.js' );
     327        $this->script_modules->register( 'nested-static-dep', '/nested-static-dep.js' );
     328        $this->script_modules->register( 'nested-dynamic-dep', '/nested-dynamic-dep.js' );
     329        $this->script_modules->register( 'no-dep', '/no-dep.js' );
     330        $this->script_modules->enqueue( 'foo' );
     331
     332        $preloaded_script_modules = $this->get_preloaded_script_modules();
     333
     334        $this->assertCount( 2, $preloaded_script_modules );
     335        $this->assertStringStartsWith( '/static-dep.js', $preloaded_script_modules['static-dep'] );
     336        $this->assertStringStartsWith( '/nested-static-dep.js', $preloaded_script_modules['nested-static-dep'] );
     337        $this->assertFalse( isset( $preloaded_script_modules['no-dep'] ) );
     338        $this->assertFalse( isset( $preloaded_script_modules['dynamic-dep'] ) );
     339        $this->assertFalse( isset( $preloaded_script_modules['nested-dynamic-dep'] ) );
    346340    }
    347341
     
    353347     * @covers ::register()
    354348     * @covers ::enqueue()
    355      * @covers ::print_module_preloads()
     349     * @covers ::print_script_module_preloads()
    356350     */
    357351    public function test_wp_dont_preload_static_dependencies_of_dynamic_dependencies() {
    358         $this->modules->register(
     352        $this->script_modules->register(
    359353            'foo',
    360354            '/foo.js',
     
    367361            )
    368362        );
    369         $this->modules->register( 'static-dep', '/static-dep.js' );
    370         $this->modules->register( 'dynamic-dep', '/dynamic-dep.js', array( 'nested-static-dep' ) );
    371         $this->modules->register( 'nested-static-dep', '/nested-static-dep.js' );
    372         $this->modules->register( 'no-dep', '/no-dep.js' );
    373         $this->modules->enqueue( 'foo' );
    374 
    375         $preloaded_modules = $this->get_preloaded_modules();
    376 
    377         $this->assertCount( 1, $preloaded_modules );
    378         $this->assertStringStartsWith( '/static-dep.js', $preloaded_modules['static-dep'] );
    379         $this->assertFalse( isset( $preloaded_modules['dynamic-dep'] ) );
    380         $this->assertFalse( isset( $preloaded_modules['nested-static-dep'] ) );
    381         $this->assertFalse( isset( $preloaded_modules['no-dep'] ) );
    382     }
    383 
    384     /**
    385      * Tests that preloaded dependencies don't include enqueued modules.
    386      *
    387      * @ticket 56313
    388      *
    389      * @covers ::register()
    390      * @covers ::enqueue()
    391      * @covers ::print_module_preloads()
    392      */
    393     public function test_wp_preloaded_dependencies_filter_enqueued_modules() {
    394         $this->modules->register(
     363        $this->script_modules->register( 'static-dep', '/static-dep.js' );
     364        $this->script_modules->register( 'dynamic-dep', '/dynamic-dep.js', array( 'nested-static-dep' ) );
     365        $this->script_modules->register( 'nested-static-dep', '/nested-static-dep.js' );
     366        $this->script_modules->register( 'no-dep', '/no-dep.js' );
     367        $this->script_modules->enqueue( 'foo' );
     368
     369        $preloaded_script_modules = $this->get_preloaded_script_modules();
     370
     371        $this->assertCount( 1, $preloaded_script_modules );
     372        $this->assertStringStartsWith( '/static-dep.js', $preloaded_script_modules['static-dep'] );
     373        $this->assertFalse( isset( $preloaded_script_modules['dynamic-dep'] ) );
     374        $this->assertFalse( isset( $preloaded_script_modules['nested-static-dep'] ) );
     375        $this->assertFalse( isset( $preloaded_script_modules['no-dep'] ) );
     376    }
     377
     378    /**
     379     * Tests that preloaded dependencies don't include enqueued script modules.
     380     *
     381     * @ticket 56313
     382     *
     383     * @covers ::register()
     384     * @covers ::enqueue()
     385     * @covers ::print_script_module_preloads()
     386     */
     387    public function test_wp_preloaded_dependencies_filter_enqueued_script_modules() {
     388        $this->script_modules->register(
    395389            'foo',
    396390            '/foo.js',
     
    400394            )
    401395        );
    402         $this->modules->register( 'dep', '/dep.js' );
    403         $this->modules->register( 'enqueued-dep', '/enqueued-dep.js' );
    404         $this->modules->enqueue( 'foo' );
    405         $this->modules->enqueue( 'enqueued-dep' ); // Not preloaded.
    406 
    407         $preloaded_modules = $this->get_preloaded_modules();
    408 
    409         $this->assertCount( 1, $preloaded_modules );
    410         $this->assertTrue( isset( $preloaded_modules['dep'] ) );
    411         $this->assertFalse( isset( $preloaded_modules['enqueued-dep'] ) );
    412     }
    413 
    414     /**
    415      * Tests that enqueued modules with dependants correctly add both the module
    416      * and its dependencies to the import map.
     396        $this->script_modules->register( 'dep', '/dep.js' );
     397        $this->script_modules->register( 'enqueued-dep', '/enqueued-dep.js' );
     398        $this->script_modules->enqueue( 'foo' );
     399        $this->script_modules->enqueue( 'enqueued-dep' ); // Not preloaded.
     400
     401        $preloaded_script_modules = $this->get_preloaded_script_modules();
     402
     403        $this->assertCount( 1, $preloaded_script_modules );
     404        $this->assertTrue( isset( $preloaded_script_modules['dep'] ) );
     405        $this->assertFalse( isset( $preloaded_script_modules['enqueued-dep'] ) );
     406    }
     407
     408    /**
     409     * Tests that enqueued script modules with dependants correctly add both the
     410     * script module and its dependencies to the import map.
    417411     *
    418412     * @ticket 56313
     
    422416     * @covers ::print_import_map()
    423417     */
    424     public function test_wp_enqueued_modules_with_dependants_add_import_map() {
    425         $this->modules->register(
     418    public function test_wp_enqueued_script_modules_with_dependants_add_import_map() {
     419        $this->script_modules->register(
    426420            'foo',
    427421            '/foo.js',
     
    431425            )
    432426        );
    433         $this->modules->register( 'dep', '/dep.js' );
    434         $this->modules->register( 'enqueued-dep', '/enqueued-dep.js' );
    435         $this->modules->enqueue( 'foo' );
    436         $this->modules->enqueue( 'enqueued-dep' ); // Also in the import map.
     427        $this->script_modules->register( 'dep', '/dep.js' );
     428        $this->script_modules->register( 'enqueued-dep', '/enqueued-dep.js' );
     429        $this->script_modules->enqueue( 'foo' );
     430        $this->script_modules->enqueue( 'enqueued-dep' ); // Also in the import map.
    437431
    438432        $import_map = $this->get_import_map();
     
    452446     */
    453447    public function test_get_versioned_src() {
    454         $get_versioned_src = new ReflectionMethod( $this->modules, 'get_versioned_src' );
     448        $get_versioned_src = new ReflectionMethod( $this->script_modules, 'get_versioned_src' );
    455449        $get_versioned_src->setAccessible( true );
    456450
     
    460454        );
    461455
    462         $result = $get_versioned_src->invoke( $this->modules, $module_with_version );
     456        $result = $get_versioned_src->invoke( $this->script_modules, $module_with_version );
    463457        $this->assertEquals( 'http://example.com/module.js?ver=1.0', $result );
    464458
     
    468462        );
    469463
    470         $result = $get_versioned_src->invoke( $this->modules, $module_without_version );
     464        $result = $get_versioned_src->invoke( $this->script_modules, $module_without_version );
    471465        $this->assertEquals( 'http://example.com/module.js', $result );
    472466
     
    476470        );
    477471
    478         $result = $get_versioned_src->invoke( $this->modules, $module_with_wp_version );
     472        $result = $get_versioned_src->invoke( $this->script_modules, $module_with_wp_version );
    479473        $this->assertEquals( 'http://example.com/module.js?ver=' . get_bloginfo( 'version' ), $result );
    480474
     
    484478        );
    485479
    486         $result = $get_versioned_src->invoke( $this->modules, $module_with_existing_query_string );
     480        $result = $get_versioned_src->invoke( $this->script_modules, $module_with_existing_query_string );
    487481        $this->assertEquals( 'http://example.com/module.js?foo=bar&ver=1.0', $result );
    488482    }
     
    490484    /**
    491485     * Tests that the correct version is propagated to the import map, enqueued
    492      * modules and preloaded modules.
    493      *
    494      * @ticket 56313
    495      *
    496      * @covers ::register()
    497      * @covers ::enqueue()
    498      * @covers ::print_enqueued_modules()
     486     * script modules and preloaded script modules.
     487     *
     488     * @ticket 56313
     489     *
     490     * @covers ::register()
     491     * @covers ::enqueue()
     492     * @covers ::print_enqueued_script_modules()
    499493     * @covers ::print_import_map()
    500      * @covers ::print_module_preloads()
     494     * @covers ::print_script_module_preloads()
    501495     * @covers ::get_version_query_string()
    502496     */
    503497    public function test_version_is_propagated_correctly() {
    504         $this->modules->register(
     498        $this->script_modules->register(
    505499            'foo',
    506500            '/foo.js',
     
    510504            '1.0'
    511505        );
    512         $this->modules->register( 'dep', '/dep.js', array(), '2.0' );
    513         $this->modules->enqueue( 'foo' );
    514 
    515         $enqueued_modules = $this->get_enqueued_modules();
    516         $this->assertEquals( '/foo.js?ver=1.0', $enqueued_modules['foo'] );
     506        $this->script_modules->register( 'dep', '/dep.js', array(), '2.0' );
     507        $this->script_modules->enqueue( 'foo' );
     508
     509        $enqueued_script_modules = $this->get_enqueued_script_modules();
     510        $this->assertEquals( '/foo.js?ver=1.0', $enqueued_script_modules['foo'] );
    517511
    518512        $import_map = $this->get_import_map();
    519513        $this->assertEquals( '/dep.js?ver=2.0', $import_map['dep'] );
    520514
    521         $preloaded_modules = $this->get_preloaded_modules();
    522         $this->assertEquals( '/dep.js?ver=2.0', $preloaded_modules['dep'] );
    523     }
    524 
    525     /**
    526      * Tests that it can print the enqueued modules multiple times, and it will
    527      * only print the modules that have not been printed before.
    528      *
    529      * @ticket 56313
    530      *
    531      * @covers ::register()
    532      * @covers ::enqueue()
    533      * @covers ::print_enqueued_modules()
    534      */
    535     public function test_print_enqueued_modules_can_be_called_multiple_times() {
    536         $this->modules->register( 'foo', '/foo.js' );
    537         $this->modules->register( 'bar', '/bar.js' );
    538         $this->modules->enqueue( 'foo' );
    539 
    540         $enqueued_modules = $this->get_enqueued_modules();
    541         $this->assertCount( 1, $enqueued_modules );
    542         $this->assertTrue( isset( $enqueued_modules['foo'] ) );
    543 
    544         $this->modules->enqueue( 'bar' );
    545 
    546         $enqueued_modules = $this->get_enqueued_modules();
    547         $this->assertCount( 1, $enqueued_modules );
    548         $this->assertTrue( isset( $enqueued_modules['bar'] ) );
    549 
    550         $enqueued_modules = $this->get_enqueued_modules();
    551         $this->assertCount( 0, $enqueued_modules );
    552     }
    553 
    554     /**
    555      * Tests that it can print the preloaded modules multiple times, and it will
    556      * only print the modules that have not been printed before.
    557      *
    558      * @ticket 56313
    559      *
    560      * @covers ::register()
    561      * @covers ::enqueue()
    562      * @covers ::print_module_preloads()
    563      */
    564     public function test_print_preloaded_modules_can_be_called_multiple_times() {
    565         $this->modules->register( 'foo', '/foo.js', array( 'static-dep-1', 'static-dep-2' ) );
    566         $this->modules->register( 'bar', '/bar.js', array( 'static-dep-3' ) );
    567         $this->modules->register( 'static-dep-1', '/static-dep-1.js' );
    568         $this->modules->register( 'static-dep-3', '/static-dep-3.js' );
    569         $this->modules->enqueue( 'foo' );
    570 
    571         $preloaded_modules = $this->get_preloaded_modules();
    572         $this->assertCount( 1, $preloaded_modules );
    573         $this->assertTrue( isset( $preloaded_modules['static-dep-1'] ) );
    574 
    575         $this->modules->register( 'static-dep-2', '/static-dep-2.js' );
    576         $this->modules->enqueue( 'bar' );
    577 
    578         $preloaded_modules = $this->get_preloaded_modules();
    579         $this->assertCount( 2, $preloaded_modules );
    580         $this->assertTrue( isset( $preloaded_modules['static-dep-2'] ) );
    581         $this->assertTrue( isset( $preloaded_modules['static-dep-3'] ) );
    582 
    583         $preloaded_modules = $this->get_preloaded_modules();
    584         $this->assertCount( 0, $preloaded_modules );
    585     }
    586 
    587     /**
    588      * Tests that a module is not registered when calling enqueue without a valid
     515        $preloaded_script_modules = $this->get_preloaded_script_modules();
     516        $this->assertEquals( '/dep.js?ver=2.0', $preloaded_script_modules['dep'] );
     517    }
     518
     519    /**
     520     * Tests that it can print the enqueued script modules multiple times, and it
     521     * will only print the script modules that have not been printed before.
     522     *
     523     * @ticket 56313
     524     *
     525     * @covers ::register()
     526     * @covers ::enqueue()
     527     * @covers ::print_enqueued_script_modules()
     528     */
     529    public function test_print_enqueued_script_modules_can_be_called_multiple_times() {
     530        $this->script_modules->register( 'foo', '/foo.js' );
     531        $this->script_modules->register( 'bar', '/bar.js' );
     532        $this->script_modules->enqueue( 'foo' );
     533
     534        $enqueued_script_modules = $this->get_enqueued_script_modules();
     535        $this->assertCount( 1, $enqueued_script_modules );
     536        $this->assertTrue( isset( $enqueued_script_modules['foo'] ) );
     537
     538        $this->script_modules->enqueue( 'bar' );
     539
     540        $enqueued_script_modules = $this->get_enqueued_script_modules();
     541        $this->assertCount( 1, $enqueued_script_modules );
     542        $this->assertTrue( isset( $enqueued_script_modules['bar'] ) );
     543
     544        $enqueued_script_modules = $this->get_enqueued_script_modules();
     545        $this->assertCount( 0, $enqueued_script_modules );
     546    }
     547
     548    /**
     549     * Tests that it can print the preloaded script modules multiple times, and it
     550     * will only print the script modules that have not been printed before.
     551     *
     552     * @ticket 56313
     553     *
     554     * @covers ::register()
     555     * @covers ::enqueue()
     556     * @covers ::print_script_module_preloads()
     557     */
     558    public function test_print_preloaded_script_modules_can_be_called_multiple_times() {
     559        $this->script_modules->register( 'foo', '/foo.js', array( 'static-dep-1', 'static-dep-2' ) );
     560        $this->script_modules->register( 'bar', '/bar.js', array( 'static-dep-3' ) );
     561        $this->script_modules->register( 'static-dep-1', '/static-dep-1.js' );
     562        $this->script_modules->register( 'static-dep-3', '/static-dep-3.js' );
     563        $this->script_modules->enqueue( 'foo' );
     564
     565        $preloaded_script_modules = $this->get_preloaded_script_modules();
     566        $this->assertCount( 1, $preloaded_script_modules );
     567        $this->assertTrue( isset( $preloaded_script_modules['static-dep-1'] ) );
     568
     569        $this->script_modules->register( 'static-dep-2', '/static-dep-2.js' );
     570        $this->script_modules->enqueue( 'bar' );
     571
     572        $preloaded_script_modules = $this->get_preloaded_script_modules();
     573        $this->assertCount( 2, $preloaded_script_modules );
     574        $this->assertTrue( isset( $preloaded_script_modules['static-dep-2'] ) );
     575        $this->assertTrue( isset( $preloaded_script_modules['static-dep-3'] ) );
     576
     577        $preloaded_script_modules = $this->get_preloaded_script_modules();
     578        $this->assertCount( 0, $preloaded_script_modules );
     579    }
     580
     581    /**
     582     * Tests that a script module is not registered when calling enqueue without a
     583     * valid src.
     584     *
     585     * @ticket 56313
     586     *
     587     * @covers ::enqueue()
     588     * @covers ::print_enqueued_script_modules()
     589     */
     590    public function test_wp_enqueue_script_module_doesnt_register_without_a_valid_src() {
     591        $this->script_modules->enqueue( 'foo' );
     592
     593        $enqueued_script_modules = $this->get_enqueued_script_modules();
     594
     595        $this->assertCount( 0, $enqueued_script_modules );
     596        $this->assertFalse( isset( $enqueued_script_modules['foo'] ) );
     597    }
     598
     599    /**
     600     * Tests that a script module is registered when calling enqueue with a valid
    589601     * src.
    590602     *
     
    592604     *
    593605     * @covers ::enqueue()
    594      * @covers ::print_enqueued_modules()
    595      */
    596     public function test_wp_enqueue_module_doesnt_register_without_a_valid_src() {
    597         $this->modules->enqueue( 'foo' );
    598 
    599         $enqueued_modules = $this->get_enqueued_modules();
    600 
    601         $this->assertCount( 0, $enqueued_modules );
    602         $this->assertFalse( isset( $enqueued_modules['foo'] ) );
    603     }
    604 
    605     /**
    606      * Tests that a module is registered when calling enqueue with a valid src.
    607      *
    608      * @ticket 56313
    609      *
    610      * @covers ::enqueue()
    611      * @covers ::print_enqueued_modules()
    612      */
    613     public function test_wp_enqueue_module_registers_with_valid_src() {
    614         $this->modules->enqueue( 'foo', '/foo.js' );
    615 
    616         $enqueued_modules = $this->get_enqueued_modules();
    617 
    618         $this->assertCount( 1, $enqueued_modules );
    619         $this->assertStringStartsWith( '/foo.js', $enqueued_modules['foo'] );
    620     }
    621 
    622     /**
    623      * Tests that a module is registered when calling enqueue with a valid src the
    624      * second time.
    625      *
    626      * @ticket 56313
    627      *
    628      * @covers ::enqueue()
    629      * @covers ::print_enqueued_modules()
    630      */
    631     public function test_wp_enqueue_module_registers_with_valid_src_the_second_time() {
    632         $this->modules->enqueue( 'foo' ); // Not valid src.
    633 
    634         $enqueued_modules = $this->get_enqueued_modules();
    635 
    636         $this->assertCount( 0, $enqueued_modules );
    637         $this->assertFalse( isset( $enqueued_modules['foo'] ) );
    638 
    639         $this->modules->enqueue( 'foo', '/foo.js' ); // Valid src.
    640 
    641         $enqueued_modules = $this->get_enqueued_modules();
    642 
    643         $this->assertCount( 1, $enqueued_modules );
    644         $this->assertStringStartsWith( '/foo.js', $enqueued_modules['foo'] );
    645     }
    646 
    647     /**
    648      * Tests that a module is registered with all the params when calling enqueue.
    649      *
    650      * @ticket 56313
    651      *
    652      * @covers ::register()
    653      * @covers ::enqueue()
    654      * @covers ::print_enqueued_modules()
     606     * @covers ::print_enqueued_script_modules()
     607     */
     608    public function test_wp_enqueue_script_module_registers_with_valid_src() {
     609        $this->script_modules->enqueue( 'foo', '/foo.js' );
     610
     611        $enqueued_script_modules = $this->get_enqueued_script_modules();
     612
     613        $this->assertCount( 1, $enqueued_script_modules );
     614        $this->assertStringStartsWith( '/foo.js', $enqueued_script_modules['foo'] );
     615    }
     616
     617    /**
     618     * Tests that a script module is registered when calling enqueue with a valid
     619     * src the second time.
     620     *
     621     * @ticket 56313
     622     *
     623     * @covers ::enqueue()
     624     * @covers ::print_enqueued_script_modules()
     625     */
     626    public function test_wp_enqueue_script_module_registers_with_valid_src_the_second_time() {
     627        $this->script_modules->enqueue( 'foo' ); // Not valid src.
     628
     629        $enqueued_script_modules = $this->get_enqueued_script_modules();
     630
     631        $this->assertCount( 0, $enqueued_script_modules );
     632        $this->assertFalse( isset( $enqueued_script_modules['foo'] ) );
     633
     634        $this->script_modules->enqueue( 'foo', '/foo.js' ); // Valid src.
     635
     636        $enqueued_script_modules = $this->get_enqueued_script_modules();
     637
     638        $this->assertCount( 1, $enqueued_script_modules );
     639        $this->assertStringStartsWith( '/foo.js', $enqueued_script_modules['foo'] );
     640    }
     641
     642    /**
     643     * Tests that a script module is registered with all the params when calling
     644     * enqueue.
     645     *
     646     * @ticket 56313
     647     *
     648     * @covers ::register()
     649     * @covers ::enqueue()
     650     * @covers ::print_enqueued_script_modules()
    655651     * @covers ::print_import_map()
    656652     */
    657     public function test_wp_enqueue_module_registers_all_params() {
    658         $this->modules->enqueue( 'foo', '/foo.js', array( 'dep' ), '1.0' );
    659         $this->modules->register( 'dep', '/dep.js' );
    660 
    661         $enqueued_modules = $this->get_enqueued_modules();
    662         $import_map       = $this->get_import_map();
    663 
    664         $this->assertCount( 1, $enqueued_modules );
    665         $this->assertEquals( '/foo.js?ver=1.0', $enqueued_modules['foo'] );
     653    public function test_wp_enqueue_script_module_registers_all_params() {
     654        $this->script_modules->enqueue( 'foo', '/foo.js', array( 'dep' ), '1.0' );
     655        $this->script_modules->register( 'dep', '/dep.js' );
     656
     657        $enqueued_script_modules = $this->get_enqueued_script_modules();
     658        $import_map              = $this->get_import_map();
     659
     660        $this->assertCount( 1, $enqueued_script_modules );
     661        $this->assertEquals( '/foo.js?ver=1.0', $enqueued_script_modules['foo'] );
    666662        $this->assertCount( 1, $import_map );
    667663        $this->assertStringStartsWith( '/dep.js', $import_map['dep'] );
Note: See TracChangeset for help on using the changeset viewer.