Skip to content

Commit 500a30d

Browse files
committed
Make test_decorate_shortcode_and_filter_source less brittle
1 parent 19d6db0 commit 500a30d

File tree

1 file changed

+75
-102
lines changed

1 file changed

+75
-102
lines changed

tests/php/validation/test-class-amp-validation-manager.php

+75-102
Original file line numberDiff line numberDiff line change
@@ -1849,83 +1849,57 @@ public function test_decorate_shortcode_and_filter_source() {
18491849
'name' => 'wp-includes',
18501850
'function' => 'WP_Embed::autoembed',
18511851
],
1852-
];
1853-
1854-
if ( function_exists( 'gutenberg_apply_block_hooks_to_post_content' ) && has_filter( 'the_content', 'gutenberg_apply_block_hooks_to_post_content' ) ) {
1855-
$sources[] = [
1856-
'type' => 'plugin',
1857-
'name' => 'gutenberg',
1858-
'file' => 'lib/compat/wordpress-6.8/blocks.php',
1859-
'function' => 'gutenberg_apply_block_hooks_to_post_content',
1860-
];
1861-
}
1862-
1863-
$sources = array_merge(
1864-
$sources,
18651852
[
1866-
[
1867-
'type' => 'core',
1868-
'name' => 'wp-includes',
1869-
'function' => 'do_blocks',
1870-
],
1871-
[
1872-
'type' => 'core',
1873-
'name' => 'wp-includes',
1874-
'function' => 'wptexturize',
1875-
],
1876-
[
1877-
'type' => 'core',
1878-
'name' => 'wp-includes',
1879-
'function' => 'wpautop',
1880-
],
1881-
[
1882-
'type' => 'core',
1883-
'name' => 'wp-includes',
1884-
'function' => 'shortcode_unautop',
1885-
],
1886-
[
1887-
'type' => 'core',
1888-
'name' => 'wp-includes',
1889-
'function' => 'prepend_attachment',
1890-
],
1891-
[
1892-
'type' => 'core',
1893-
'name' => 'wp-includes',
1894-
'function' => 'wp_replace_insecure_home_url',
1895-
],
1896-
[
1897-
'type' => 'core',
1898-
'name' => 'wp-includes',
1899-
'function' => 'capital_P_dangit',
1900-
],
1901-
[
1902-
'type' => 'core',
1903-
'name' => 'wp-includes',
1904-
'function' => 'do_shortcode',
1905-
],
1906-
[
1907-
'type' => 'core',
1908-
'name' => 'wp-includes',
1909-
'function' => 'wp_filter_content_tags',
1910-
],
1911-
[
1912-
'type' => 'core',
1913-
'name' => 'wp-includes',
1914-
'function' => 'convert_smilies',
1915-
],
1916-
]
1917-
);
1918-
1919-
if ( function_exists( 'apply_block_hooks_to_content' ) && has_filter( 'the_content', 'apply_block_hooks_to_content' ) ) {
1920-
array_unshift(
1921-
$sources,
1922-
[
1923-
'type' => 'core',
1924-
'name' => 'wp-includes',
1925-
'function' => 'apply_block_hooks_to_content',
1926-
]
1927-
);
1928-
}
1853+
'type' => 'core',
1854+
'name' => 'wp-includes',
1855+
'function' => 'do_blocks',
1856+
],
1857+
[
1858+
'type' => 'core',
1859+
'name' => 'wp-includes',
1860+
'function' => 'wptexturize',
1861+
],
1862+
[
1863+
'type' => 'core',
1864+
'name' => 'wp-includes',
1865+
'function' => 'wpautop',
1866+
],
1867+
[
1868+
'type' => 'core',
1869+
'name' => 'wp-includes',
1870+
'function' => 'shortcode_unautop',
1871+
],
1872+
[
1873+
'type' => 'core',
1874+
'name' => 'wp-includes',
1875+
'function' => 'prepend_attachment',
1876+
],
1877+
[
1878+
'type' => 'core',
1879+
'name' => 'wp-includes',
1880+
'function' => 'wp_replace_insecure_home_url',
1881+
],
1882+
[
1883+
'type' => 'core',
1884+
'name' => 'wp-includes',
1885+
'function' => 'capital_P_dangit',
1886+
],
1887+
[
1888+
'type' => 'core',
1889+
'name' => 'wp-includes',
1890+
'function' => 'do_shortcode',
1891+
],
1892+
[
1893+
'type' => 'core',
1894+
'name' => 'wp-includes',
1895+
'function' => 'wp_filter_content_tags',
1896+
],
1897+
[
1898+
'type' => 'core',
1899+
'name' => 'wp-includes',
1900+
'function' => 'convert_smilies',
1901+
],
1902+
];
19291903

19301904
foreach ( $sources as &$source ) {
19311905
$function = $source['function'];
@@ -1948,35 +1922,34 @@ public function test_decorate_shortcode_and_filter_source() {
19481922
$source['function'] = $function;
19491923
}
19501924

1951-
$source_json = wp_json_encode(
1952-
[
1953-
'hook' => 'the_content',
1954-
'filter' => true,
1955-
'post_id' => get_the_ID(),
1956-
'post_type' => get_post_type(),
1957-
'sources' => $sources,
1958-
]
1959-
);
1960-
19611925
$shortcode_fallback_reflection = new ReflectionFunction( $shortcode_fallback );
19621926

1963-
$expected_content = implode(
1964-
'',
1965-
[
1966-
"<!--amp-source-stack $source_json-->",
1967-
sprintf(
1968-
'<p>before<!--amp-source-stack {"type":"plugin","name":"amp","file":%1$s,"line":%2$s,"function":"{closure}","shortcode":"test"}--><b>test</b><!--/amp-source-stack {"type":"plugin","name":"amp","file":%1$s,"line":%2$s,"function":"{closure}","shortcode":"test"}-->after</p>' . "\n",
1969-
wp_json_encode( substr( $shortcode_fallback_reflection->getFileName(), strlen( AMP__DIR__ ) + 1 ) ),
1970-
$shortcode_fallback_reflection->getStartLine()
1971-
),
1972-
"<!--/amp-source-stack $source_json-->",
1973-
]
1974-
);
1927+
$this->assertTrue( (bool) preg_match( '/^<!--amp-source-stack (\{.+?})-->/', $filtered_content, $matches ) );
1928+
$json = $matches[1];
1929+
$source_stack = json_decode( $json, true );
1930+
$this->assertIsArray( $source_stack );
1931+
$this->assertStringEndsWith( "<!--/amp-source-stack $json-->", $filtered_content );
1932+
1933+
$expected_props = [
1934+
'hook' => 'the_content',
1935+
'filter' => true,
1936+
'post_id' => get_the_ID(),
1937+
'post_type' => get_post_type(),
1938+
];
1939+
foreach ( $expected_props as $key => $value ) {
1940+
$this->assertEquals( $value, $source_stack[ $key ] );
1941+
}
19751942

1976-
$this->assertEquals(
1977-
preg_split( '/(?=<)/', $expected_content ),
1978-
preg_split( '/(?=<)/', $filtered_content )
1979-
);
1943+
foreach ( $sources as $expected_source ) {
1944+
$found = false;
1945+
foreach ( $source_stack['sources'] as $actual_source ) {
1946+
if ( count( array_diff( $expected_source, $actual_source ) ) === 0 ) {
1947+
$found = true;
1948+
break;
1949+
}
1950+
}
1951+
$this->assertTrue( $found, 'Unable to lcoate source: ' . wp_json_encode( $source ) );
1952+
}
19801953
}
19811954

19821955
/**

0 commit comments

Comments
 (0)