Repository navigation
Expand file tree
/
Copy pathclass-block-fixture-test.php
More file actions
55 lines (45 loc) · 1.77 KB
/
Copy pathclass-block-fixture-test.php
File metadata and controls
55 lines (45 loc) · 1.77 KB
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
46
47
48
49
50
51
52
53
54
55
<?php
/**
* Test block fixtures in PHP.
*
* @package gutenberg
*/
class Block_Fixture_Test extends WP_UnitTestCase {
public function replace_backslash( $input ) {
return preg_replace_callback(
'/<!-- wp:([^ ]+) ({.*?}) -->/',
function ( $matches ) {
$json = str_replace( '\\u005c', '\\\\', $matches[2] );
return "<!-- wp:{$matches[1]} {$json} -->";
},
$input
);
}
/**
* Tests that running the serialised block content through KSES doesn't cause the
* HTML to change.
*
* @dataProvider data_block_fixtures
*/
public 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 );
// Account for a wp-env override using a port other than 8889 for the tests environment.
$block = preg_replace( '#http://localhost:\d+/#', home_url( '/' ), $block );
$kses_block = wp_kses_post( $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->assertEqualHTML( $block, $kses_block, '<body>', "Failed to match $filename" );
}
public 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;
}
}