| | 188 | * Testing `wp_script_get_data` with data added through `wp_localize_script`. |
| | 189 | * |
| | 190 | * @ticket 40485 |
| | 191 | */ |
| | 192 | function test_wp_script_get_data_with_localized_data() { |
| | 193 | $data = array( |
| | 194 | 'key' => 'value', |
| | 195 | 'key2' => 'value', |
| | 196 | ); |
| | 197 | $l10n = array(); |
| | 198 | |
| | 199 | wp_enqueue_script( 'test-get-data', 'example.com', array(), null ); |
| | 200 | wp_localize_script( 'test-get-data', 'localized_data', $data ); |
| | 201 | |
| | 202 | // Simulate preparing data for localization. |
| | 203 | foreach ( (array) $data as $key => $value ) { |
| | 204 | if ( ! is_scalar( $value ) ) |
| | 205 | continue; |
| | 206 | |
| | 207 | $l10n[ $key ] = html_entity_decode( (string) $value, ENT_QUOTES, 'UTF-8' ); |
| | 208 | } |
| | 209 | |
| | 210 | $localized_data = 'var localized_data = ' . wp_json_encode( $data ) . ';'; |
| | 211 | |
| | 212 | $this->assertEquals( $localized_data, wp_script_get_data( 'test-get-data', 'data' ) ); |
| | 213 | } |
| | 214 | |
| | 215 | /** |
| | 216 | * Testing `wp_script_get_data` for retrieving the conditional data key. |
| | 217 | * |
| | 218 | * @ticket 40485 |
| | 219 | */ |
| | 220 | function test_wp_script_get_data_with_conditional_key() { |
| | 221 | // Enqueue & add an invalid key |
| | 222 | wp_enqueue_script( 'test-get-date-conditional', 'example.com', array(), null ); |
| | 223 | wp_script_add_data( 'test-get-date-conditional', 'conditional', 'lt IE 9' ); |
| | 224 | |
| | 225 | $this->assertEquals( 'lt IE 9', wp_script_get_data( 'test-get-date-conditional', 'conditional' ) ); |
| | 226 | } |
| | 227 | |
| | 228 | /** |