| 1431 | |
| 1432 | /** |
| 1433 | * @ticket 52534 |
| 1434 | * @covers ::wp_localize_script |
| 1435 | * |
| 1436 | * @dataProvider data_wp_localize_script_data_formats |
| 1437 | * |
| 1438 | * @param mixed $l10n_data Localization data passed to wp_localize_script(). |
| 1439 | * @param string $expected Expected transformation of localization data. |
| 1440 | * @param string $unsupported Optional. Whether the data format is unsupported. Default false. |
| 1441 | */ |
| 1442 | function test_wp_localize_script_data_formats( $l10n_data, $expected, $unsupported = false ) { |
| 1443 | if ( $unsupported ) { |
| 1444 | $this->setExpectedIncorrectUsage( 'WP_Scripts::localize' ); |
| 1445 | } |
| 1446 | |
| 1447 | wp_enqueue_script( 'test-example', 'example.com', array(), null ); |
| 1448 | wp_localize_script( 'test-example', 'testExample', $l10n_data ); |
| 1449 | |
| 1450 | $expected = "<script type='text/javascript' id='test-example-js-extra'>\n/* <![CDATA[ */\nvar testExample = {$expected};\n/* ]]> */\n</script>\n"; |
| 1451 | $expected .= "<script type='text/javascript' src='http://example.com' id='test-example-js'></script>\n"; |
| 1452 | |
| 1453 | $this->assertSame( $expected, get_echo( 'wp_print_scripts' ) ); |
| 1454 | |
| 1455 | } |
| 1456 | |
| 1457 | /** |
| 1458 | * Data provider for test_wp_localize_script_data_formats(). |
| 1459 | * |
| 1460 | * @return array[] { |
| 1461 | * Array of arguments for test. |
| 1462 | * |
| 1463 | * @type mixed $l10n_data Localization data passed to wp_localize_script(). |
| 1464 | * @type string $expected Expected transformation of localization data. |
| 1465 | * @type string $unsupported Optional. Whether the data format is unsupported. |
| 1466 | * } |
| 1467 | */ |
| 1468 | function data_wp_localize_script_data_formats() { |
| 1469 | return array( |
| 1470 | array( array( 'foo' => 'bar' ), '{"foo":"bar"}' ), |
| 1471 | array( 'string', '"string"', true ), |
| 1472 | array( 1, '"1"', true ), |
| 1473 | array( array( 'foo' => array( 'bar' => 'foobar' ) ), '{"foo":{"bar":"foobar"}}' ), |
| 1474 | array( array( 'foo' => 6.6 ), '{"foo":"6.6"}' ), |
| 1475 | array( array( 'foo' => 6 ), '{"foo":"6"}' ), |
| 1476 | ); |
| 1477 | } |