@@ -1312,10 +1312,14 @@ public function assert_queried_element_exists( DOMXPath $xpath, $query ) {
13121312 */
13131313 public function assert_dev_mode_is_on_queried_element ( DOMXPath $ xpath , $ query ) {
13141314 $ element = $ xpath ->query ( $ query )->item ( 0 );
1315- $ this ->assertInstanceOf ( 'DOMElement ' , $ element , 'Expected element for query: ' . $ query );
1315+ $ this ->assertInstanceOf (
1316+ 'DOMElement ' ,
1317+ $ element ,
1318+ 'Expected element for query: ' . $ query . "\nDocument: " . $ xpath ->document ->saveHTML ()
1319+ );
13161320 $ this ->assertTrue (
13171321 $ element ->hasAttribute ( AMP_Rule_Spec::DEV_MODE_ATTRIBUTE ),
1318- 'Expected dev mode to be enabled on element for query: ' . $ query . "\nDocument: " . $ element -> ownerDocument ->saveHTML ()
1322+ 'Expected dev mode to be enabled on element for query: ' . $ query . "\nDocument: " . $ xpath -> document ->saveHTML ()
13191323 );
13201324 }
13211325
@@ -1327,8 +1331,15 @@ public function assert_dev_mode_is_on_queried_element( DOMXPath $xpath, $query )
13271331 */
13281332 public function assert_dev_mode_is_not_on_queried_element ( DOMXPath $ xpath , $ query ) {
13291333 $ element = $ xpath ->query ( $ query )->item ( 0 );
1330- $ this ->assertInstanceOf ( 'DOMElement ' , $ element , 'Expected element for query: ' . $ query );
1331- $ this ->assertFalse ( $ element ->hasAttribute ( AMP_Rule_Spec::DEV_MODE_ATTRIBUTE ), 'Expected dev mode to not be enabled on element for query: ' . $ query );
1334+ $ this ->assertInstanceOf (
1335+ 'DOMElement ' ,
1336+ $ element ,
1337+ 'Expected element for query: ' . $ query . "\nDocument: " . $ xpath ->document ->saveHTML ()
1338+ );
1339+ $ this ->assertFalse (
1340+ $ element ->hasAttribute ( AMP_Rule_Spec::DEV_MODE_ATTRIBUTE ),
1341+ 'Expected dev mode to not be enabled on element for query: ' . $ query . "\nDocument: " . $ xpath ->document ->saveHTML ()
1342+ );
13321343 }
13331344
13341345 /**
@@ -1341,10 +1352,12 @@ public function get_data_to_test_filtering_admin_bar_style_loader_tag_data() {
13411352 'admin_bar_exclusively_dependent ' => [
13421353 static function () {
13431354 wp_enqueue_style ( 'example-admin-bar ' , 'https://example.com/example-admin-bar.css ' , [ 'admin-bar ' ], '0.1 ' );
1355+ wp_add_inline_style ( 'example-admin-bar ' , '#wpadminbar:after { content: "Hey admin!" } ' );
13441356 },
13451357 function ( DOMXPath $ xpath ) {
13461358 $ this ->assert_dev_mode_is_on_queried_element ( $ xpath , '//link[ @id = "example-admin-bar-css" ] ' );
13471359 $ this ->assert_dev_mode_is_on_queried_element ( $ xpath , '//link[ @id = "dashicons-css" ] ' );
1360+ $ this ->assert_dev_mode_is_on_queried_element ( $ xpath , '//style[ @id = "example-admin-bar-inline-css" ] ' );
13481361 },
13491362 ],
13501363
@@ -1398,6 +1411,22 @@ function ( DOMXPath $xpath ) {
13981411 $ this ->assert_dev_mode_is_on_queried_element ( $ xpath , '//link[ @id = "dashicons-css" ] ' );
13991412 },
14001413 ],
1414+
1415+ 'styles_have_dev_mode_when_flagged ' => [
1416+ static function () {
1417+ wp_enqueue_style ( 'custom-style ' , 'https://example.com/custom-style.css ' , [], '1.0 ' , false );
1418+ wp_add_inline_style ( 'custom-style ' , '/* inline-custom-style */ ' );
1419+ wp_style_add_data ( 'custom-style ' , 'ampdevmode ' , true );
1420+
1421+ wp_enqueue_style ( 'excluded-style ' , 'https://example.com/excluded-style.js ' , [], '1.0 ' , false );
1422+ },
1423+ function ( DOMXPath $ xpath ) {
1424+ $ this ->assert_dev_mode_is_on_queried_element ( $ xpath , '//link[ contains( @href, "/custom-style" ) ] ' );
1425+ $ this ->assert_dev_mode_is_on_queried_element ( $ xpath , '//style[ @id = "custom-style-inline-css" ] ' );
1426+ $ this ->assert_dev_mode_is_on_queried_element ( $ xpath , '//style[ contains( text(), "inline-custom-style" ) ] ' );
1427+ $ this ->assert_dev_mode_is_not_on_queried_element ( $ xpath , '//link[ contains( @href, "/excluded-style" ) ] ' );
1428+ },
1429+ ],
14011430 ];
14021431 }
14031432
@@ -1423,9 +1452,15 @@ public function test_filter_admin_bar_style_loader_tag( $setup_callback, $assert
14231452 echo '</head><body></body></html> ' ;
14241453 $ output = ob_get_clean ();
14251454
1426- $ dom = new DOMDocument ();
1455+ $ dom = new Document ();
14271456 $ dom ->loadHTML ( $ output );
14281457
1458+ AMP_Content_Sanitizer::sanitize_document (
1459+ $ dom ,
1460+ wp_array_slice_assoc ( amp_get_content_sanitizers (), [ 'AMP_Dev_Mode_Sanitizer ' ] ),
1461+ [ 'use_document_element ' => true ]
1462+ );
1463+
14291464 $ assert_callback ( new DOMXPath ( $ dom ) );
14301465 }
14311466
@@ -1440,13 +1475,15 @@ public function get_data_to_test_filtering_admin_bar_script_loader_tag_data() {
14401475 static function () {
14411476 wp_enqueue_script ( 'admin-bar ' );
14421477 wp_enqueue_script ( 'example-admin-bar ' , 'https://example.com/example-admin-bar.js ' , [ 'admin-bar ' ], '0.1 ' , false );
1443- wp_add_inline_script ( 'example-admin-bar ' , '/* inline-example-admin-bar */ ' );
1478+ wp_add_inline_script ( 'example-admin-bar ' , '/* inline-example-admin-bar-before */ ' , 'before ' );
1479+ wp_add_inline_script ( 'example-admin-bar ' , '/* inline-example-admin-bar-after */ ' , 'after ' );
14441480 wp_localize_script ( 'example-admin-bar ' , 'exampleAdminBar ' , [ 'hello ' => 'world ' ] );
14451481 },
14461482 function ( DOMXPath $ xpath ) {
14471483 $ this ->assert_dev_mode_is_on_queried_element ( $ xpath , '//script[ contains( @src, "/example-admin-bar" ) ] ' );
14481484 $ this ->assert_dev_mode_is_on_queried_element ( $ xpath , '//script[ contains( @src, "/admin-bar" ) ] ' );
1449- $ this ->assert_dev_mode_is_on_queried_element ( $ xpath , '//script[ contains( text(), "inline-example-admin-bar" ) ] ' );
1485+ $ this ->assert_dev_mode_is_on_queried_element ( $ xpath , '//script[ contains( text(), "inline-example-admin-bar-before" ) ] ' );
1486+ $ this ->assert_dev_mode_is_on_queried_element ( $ xpath , '//script[ contains( text(), "inline-example-admin-bar-after" ) ] ' );
14501487 $ this ->assert_dev_mode_is_on_queried_element ( $ xpath , '//script[ contains( text(), "exampleAdminBar" ) ] ' );
14511488 if ( wp_script_is ( 'hoverintent-js ' , 'registered ' ) ) {
14521489 $ this ->assert_dev_mode_is_on_queried_element ( $ xpath , '//script[ contains( @src, "/hoverintent-js" ) ] ' );
@@ -1502,6 +1539,25 @@ function ( DOMXPath $xpath ) {
15021539 $ this ->assert_dev_mode_is_not_on_queried_element ( $ xpath , '//script[ contains( @src, "/hoverintent-js" ) ] ' );
15031540 },
15041541 ],
1542+
1543+ 'scripts_have_dev_mode_when_flagged ' => [
1544+ static function () {
1545+ wp_enqueue_script ( 'custom-script ' , 'https://example.com/custom-script.js ' , [], '1.0 ' , false );
1546+ wp_add_inline_script ( 'custom-script ' , '/* inline-custom-script-before */ ' , 'before ' );
1547+ wp_add_inline_script ( 'custom-script ' , '/* inline-custom-script-after */ ' , 'after ' );
1548+ wp_localize_script ( 'custom-script ' , 'customScript ' , [ 'hello ' => 'world ' ] );
1549+ wp_script_add_data ( 'custom-script ' , 'ampdevmode ' , true );
1550+
1551+ wp_enqueue_script ( 'excluded-script ' , 'https://example.com/excluded-script.js ' , [], '1.0 ' , false );
1552+ },
1553+ function ( DOMXPath $ xpath ) {
1554+ $ this ->assert_dev_mode_is_on_queried_element ( $ xpath , '//script[ contains( @src, "/custom-script" ) ] ' );
1555+ $ this ->assert_dev_mode_is_on_queried_element ( $ xpath , '//script[ contains( text(), "inline-custom-script-before" ) ] ' );
1556+ $ this ->assert_dev_mode_is_on_queried_element ( $ xpath , '//script[ contains( text(), "inline-custom-script-after" ) ] ' );
1557+ $ this ->assert_dev_mode_is_on_queried_element ( $ xpath , '//script[ contains( text(), "customScript" ) ] ' );
1558+ $ this ->assert_dev_mode_is_not_on_queried_element ( $ xpath , '//script[ contains( @src, "/excluded-script" ) ] ' );
1559+ },
1560+ ],
15051561 ];
15061562 }
15071563
0 commit comments