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