-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathclass-block-fixture-test.php
45 lines (35 loc) · 1.45 KB
/
class-block-fixture-test.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<?php
/**
* Test block fixtures in PHP.
*
* @package Gutenberg
*/
class Block_Fixture_Test extends WP_UnitTestCase {
/**
* Tests that running the serialised block content through KSES doesn't cause the
* HTML to change.
*
* @dataProvider data_block_fixtures
*/
function test_kses_doesnt_change_fixtures( $block, $filename ) {
// KSES doesn't allow data: URLs, so we need to replace any of them in fixtures.
$block = preg_replace( "/src=['\"]data:[^'\"]+['\"]/", 'src="https://wordpress.org/foo.jpg"', $block );
$block = preg_replace( "/href=['\"]data:[^'\"]+['\"]/", 'href="https://wordpress.org/foo.jpg"', $block );
$block = preg_replace( '/url\(data:[^)]+\)/', 'url(https://wordpress.org/foo.jpg)', $block );
$kses_block = wp_kses_post( $block );
// KSES adds a space at the end of self-closing tags, add it to the original to match.
$block = preg_replace( '|([^ ])/>|', '$1 />', $block );
// KSES removes the last semi-colon from style attributes, remove it from the original to match.
$block = preg_replace( '/style="([^"]*);"/', 'style="$1"', $block );
$this->assertSame( $block, $kses_block, "Failed to match $filename" );
}
function data_block_fixtures() {
$data = array();
foreach ( glob( gutenberg_dir_path() . 'test/integration/fixtures/blocks/*.serialized.html' ) as $path ) {
$filename = basename( $path );
$block = file_get_contents( $path );
$data[] = array( $block, $filename );
}
return $data;
}
}