- Timestamp:
- 01/23/2024 03:32:03 AM (8 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tests/phpunit/tests/script-modules/wpScriptModules.php
r57269 r57327 18 18 * @var WP_Script_Modules 19 19 */ 20 protected $ modules;20 protected $script_modules; 21 21 22 22 /** … … 25 25 public function set_up() { 26 26 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 } 48 45 } 49 46 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. 57 54 */ 58 55 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' ) ); 60 57 preg_match( '/<script type="importmap" id="wp-importmap">.*?(\{.*\}).*?<\/script>/s', $import_map_markup, $import_map_string ); 61 58 return json_decode( $import_map_string[1], true )['imports']; … … 63 60 64 61 /** 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 } 82 76 } 83 77 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. 111 105 * 112 106 * @ticket 56313 … … 115 109 * @covers ::enqueue() 116 110 * @covers ::dequeue() 117 * @covers ::print_enqueued_ modules()111 * @covers ::print_enqueued_script_modules() 118 112 */ 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 be135 * 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. 136 130 * 137 131 * @ticket 56313 … … 139 133 * @covers ::register() 140 134 * @covers ::enqueue() 141 * @covers ::print_enqueued_ modules()135 * @covers ::print_enqueued_script_modules() 142 136 */ 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 ensures157 * 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. 158 152 * 159 153 * @ticket 56313 … … 162 156 * @covers ::enqueue() 163 157 * @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'] ) ); 178 172 } 179 173 180 174 /** 181 175 * 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. 183 177 * 184 178 * @ticket 56313 … … 189 183 */ 190 184 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' ); 195 189 196 190 $import_map = $this->get_import_map(); … … 203 197 /** 204 198 * 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. 206 200 * 207 201 * @ticket 56313 … … 212 206 */ 213 207 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' ); 219 213 220 214 $import_map = $this->get_import_map(); … … 235 229 */ 236 230 public function test_wp_import_map_recursive_dependencies() { 237 $this-> modules->register(231 $this->script_modules->register( 238 232 'foo', 239 233 '/foo.js', … … 246 240 ) 247 241 ); 248 $this-> modules->register(242 $this->script_modules->register( 249 243 'static-dep', 250 244 '/static-dep.js', … … 260 254 ) 261 255 ); 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' ); 267 261 268 262 $import_map = $this->get_import_map(); … … 286 280 */ 287 281 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' ) ); 292 286 293 287 $this->assertEmpty( $import_map_markup ); … … 302 296 * @covers ::register() 303 297 * @covers ::enqueue() 304 * @covers ::print_ module_preloads()298 * @covers ::print_script_module_preloads() 305 299 */ 306 300 public function test_wp_enqueue_preloaded_static_dependencies() { 307 $this-> modules->register(301 $this->script_modules->register( 308 302 'foo', 309 303 '/foo.js', … … 316 310 ) 317 311 ); 318 $this-> modules->register(312 $this->script_modules->register( 319 313 'static-dep', 320 314 '/static-dep.js', … … 330 324 ) 331 325 ); 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'] ) ); 346 340 } 347 341 … … 353 347 * @covers ::register() 354 348 * @covers ::enqueue() 355 * @covers ::print_ module_preloads()349 * @covers ::print_script_module_preloads() 356 350 */ 357 351 public function test_wp_dont_preload_static_dependencies_of_dynamic_dependencies() { 358 $this-> modules->register(352 $this->script_modules->register( 359 353 'foo', 360 354 '/foo.js', … … 367 361 ) 368 362 ); 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( 395 389 'foo', 396 390 '/foo.js', … … 400 394 ) 401 395 ); 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 module416 * 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. 417 411 * 418 412 * @ticket 56313 … … 422 416 * @covers ::print_import_map() 423 417 */ 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( 426 420 'foo', 427 421 '/foo.js', … … 431 425 ) 432 426 ); 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. 437 431 438 432 $import_map = $this->get_import_map(); … … 452 446 */ 453 447 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' ); 455 449 $get_versioned_src->setAccessible( true ); 456 450 … … 460 454 ); 461 455 462 $result = $get_versioned_src->invoke( $this-> modules, $module_with_version );456 $result = $get_versioned_src->invoke( $this->script_modules, $module_with_version ); 463 457 $this->assertEquals( 'http://example.com/module.js?ver=1.0', $result ); 464 458 … … 468 462 ); 469 463 470 $result = $get_versioned_src->invoke( $this-> modules, $module_without_version );464 $result = $get_versioned_src->invoke( $this->script_modules, $module_without_version ); 471 465 $this->assertEquals( 'http://example.com/module.js', $result ); 472 466 … … 476 470 ); 477 471 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 ); 479 473 $this->assertEquals( 'http://example.com/module.js?ver=' . get_bloginfo( 'version' ), $result ); 480 474 … … 484 478 ); 485 479 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 ); 487 481 $this->assertEquals( 'http://example.com/module.js?foo=bar&ver=1.0', $result ); 488 482 } … … 490 484 /** 491 485 * Tests that the correct version is propagated to the import map, enqueued 492 * modules and preloadedmodules.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() 499 493 * @covers ::print_import_map() 500 * @covers ::print_ module_preloads()494 * @covers ::print_script_module_preloads() 501 495 * @covers ::get_version_query_string() 502 496 */ 503 497 public function test_version_is_propagated_correctly() { 504 $this-> modules->register(498 $this->script_modules->register( 505 499 'foo', 506 500 '/foo.js', … … 510 504 '1.0' 511 505 ); 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'] ); 517 511 518 512 $import_map = $this->get_import_map(); 519 513 $this->assertEquals( '/dep.js?ver=2.0', $import_map['dep'] ); 520 514 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 589 601 * src. 590 602 * … … 592 604 * 593 605 * @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() 655 651 * @covers ::print_import_map() 656 652 */ 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'] ); 666 662 $this->assertCount( 1, $import_map ); 667 663 $this->assertStringStartsWith( '/dep.js', $import_map['dep'] );
Note: See TracChangeset
for help on using the changeset viewer.