@@ -996,6 +996,8 @@ static function( $templates ) {
996
996
* @covers AMP_Theme_Support::add_hooks()
997
997
*/
998
998
public function test_add_hooks () {
999
+ add_theme_support ( 'amp ' );
1000
+ $ this ->go_to ( '/ ' );
999
1001
AMP_Theme_Support::add_hooks ();
1000
1002
$ this ->assertFalse ( has_action ( 'wp_head ' , 'wp_post_preview_js ' ) );
1001
1003
$ this ->assertFalse ( has_action ( 'wp_head ' , 'wp_oembed_add_host_js ' ) );
@@ -1016,6 +1018,8 @@ public function test_add_hooks() {
1016
1018
$ this ->assertEquals ( 10 , has_filter ( 'customize_partial_render ' , [ self ::TESTED_CLASS , 'filter_customize_partial_render ' ] ) );
1017
1019
$ this ->assertEquals ( 10 , has_action ( 'wp_footer ' , 'amp_print_analytics ' ) );
1018
1020
$ this ->assertEquals ( 10 , has_action ( 'admin_bar_init ' , [ self ::TESTED_CLASS , 'init_admin_bar ' ] ) );
1021
+ $ this ->assertEquals ( 10 , has_filter ( 'style_loader_tag ' , [ 'AMP_Theme_Support ' , 'filter_style_loader_tag_for_dev_mode ' ] ) );
1022
+ $ this ->assertEquals ( 10 , has_filter ( 'script_loader_tag ' , [ 'AMP_Theme_Support ' , 'filter_script_loader_tag_for_dev_mode ' ] ) );
1019
1023
$ priority = defined ( 'PHP_INT_MIN ' ) ? PHP_INT_MIN : ~PHP_INT_MAX ; // phpcs:ignore PHPCompatibility.Constants.NewConstants.php_int_minFound
1020
1024
$ this ->assertEquals ( $ priority , has_action ( 'template_redirect ' , [ self ::TESTED_CLASS , 'start_output_buffering ' ] ) );
1021
1025
@@ -1237,8 +1241,8 @@ public function test_filter_cancel_comment_reply_link() {
1237
1241
* Test init_admin_bar.
1238
1242
*
1239
1243
* @covers \AMP_Theme_Support::init_admin_bar()
1240
- * @covers \AMP_Theme_Support::filter_admin_bar_style_loader_tag ()
1241
- * @covers \AMP_Theme_Support::filter_admin_bar_script_loader_tag ()
1244
+ * @covers \AMP_Theme_Support::filter_style_loader_tag_for_dev_mode ()
1245
+ * @covers \AMP_Theme_Support::filter_script_loader_tag_for_dev_mode ()
1242
1246
*/
1243
1247
public function test_init_admin_bar () {
1244
1248
require_once ABSPATH . WPINC . '/class-wp-admin-bar.php ' ;
@@ -1269,8 +1273,7 @@ function() {
1269
1273
$ this ->assertEquals ( 10 , has_action ( 'wp_head ' , $ callback ) );
1270
1274
1271
1275
AMP_Theme_Support::init_admin_bar ();
1272
- $ this ->assertEquals ( 10 , has_filter ( 'style_loader_tag ' , [ 'AMP_Theme_Support ' , 'filter_admin_bar_style_loader_tag ' ] ) );
1273
- $ this ->assertEquals ( 10 , has_filter ( 'script_loader_tag ' , [ 'AMP_Theme_Support ' , 'filter_admin_bar_script_loader_tag ' ] ) );
1276
+ AMP_Theme_Support::add_hooks ();
1274
1277
$ this ->assertFalse ( has_action ( 'wp_head ' , $ callback ) );
1275
1278
ob_start ();
1276
1279
wp_print_styles ();
@@ -1310,7 +1313,10 @@ public function assert_queried_element_exists( DOMXPath $xpath, $query ) {
1310
1313
public function assert_dev_mode_is_on_queried_element ( DOMXPath $ xpath , $ query ) {
1311
1314
$ element = $ xpath ->query ( $ query )->item ( 0 );
1312
1315
$ this ->assertInstanceOf ( 'DOMElement ' , $ element , 'Expected element for query: ' . $ query );
1313
- $ this ->assertTrue ( $ element ->hasAttribute ( AMP_Rule_Spec::DEV_MODE_ATTRIBUTE ), 'Expected dev mode to be enabled on element for query: ' . $ query );
1316
+ $ this ->assertTrue (
1317
+ $ element ->hasAttribute ( AMP_Rule_Spec::DEV_MODE_ATTRIBUTE ),
1318
+ 'Expected dev mode to be enabled on element for query: ' . $ query . "\nDocument: " . $ element ->ownerDocument ->saveHTML ()
1319
+ );
1314
1320
}
1315
1321
1316
1322
/**
@@ -1326,7 +1332,7 @@ public function assert_dev_mode_is_not_on_queried_element( DOMXPath $xpath, $que
1326
1332
}
1327
1333
1328
1334
/**
1329
- * Get data to test AMP_Theme_Support::filter_admin_bar_style_loader_tag ().
1335
+ * Get data to test AMP_Theme_Support::filter_style_loader_tag_for_dev_mode ().
1330
1336
*
1331
1337
* @return array
1332
1338
*/
@@ -1396,10 +1402,10 @@ function ( DOMXPath $xpath ) {
1396
1402
}
1397
1403
1398
1404
/**
1399
- * Test filter_admin_bar_style_loader_tag .
1405
+ * Test filter_style_loader_tag_for_dev_mode .
1400
1406
*
1401
1407
* @dataProvider get_data_to_test_filtering_admin_bar_style_loader_tag_data
1402
- * @covers \AMP_Theme_Support::filter_admin_bar_style_loader_tag ()
1408
+ * @covers \AMP_Theme_Support::filter_style_loader_tag_for_dev_mode ()
1403
1409
* @covers \AMP_Theme_Support::is_exclusively_dependent()
1404
1410
*
1405
1411
* @param callable $setup_callback Setup callback.
@@ -1409,7 +1415,7 @@ public function test_filter_admin_bar_style_loader_tag( $setup_callback, $assert
1409
1415
add_theme_support ( 'amp ' );
1410
1416
$ this ->go_to ( '/ ' );
1411
1417
add_filter ( 'amp_dev_mode_enabled ' , '__return_true ' );
1412
- add_filter ( 'style_loader_tag ' , [ 'AMP_Theme_Support ' , 'filter_admin_bar_style_loader_tag ' ], 10 , 2 );
1418
+ add_filter ( 'style_loader_tag ' , [ 'AMP_Theme_Support ' , 'filter_style_loader_tag_for_dev_mode ' ], 10 , 2 );
1413
1419
$ setup_callback ();
1414
1420
ob_start ();
1415
1421
echo '<html><head> ' ;
@@ -1424,7 +1430,7 @@ public function test_filter_admin_bar_style_loader_tag( $setup_callback, $assert
1424
1430
}
1425
1431
1426
1432
/**
1427
- * Get data to test AMP_Theme_Support::filter_admin_bar_script_loader_tag ().
1433
+ * Get data to test AMP_Theme_Support::filter_script_loader_tag_for_dev_mode ().
1428
1434
*
1429
1435
* @return array
1430
1436
*/
@@ -1434,10 +1440,14 @@ public function get_data_to_test_filtering_admin_bar_script_loader_tag_data() {
1434
1440
static function () {
1435
1441
wp_enqueue_script ( 'admin-bar ' );
1436
1442
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 */ ' );
1444
+ wp_localize_script ( 'example-admin-bar ' , 'exampleAdminBar ' , [ 'hello ' => 'world ' ] );
1437
1445
},
1438
1446
function ( DOMXPath $ xpath ) {
1439
1447
$ this ->assert_dev_mode_is_on_queried_element ( $ xpath , '//script[ contains( @src, "/example-admin-bar" ) ] ' );
1440
1448
$ 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" ) ] ' );
1450
+ $ this ->assert_dev_mode_is_on_queried_element ( $ xpath , '//script[ contains( text(), "exampleAdminBar" ) ] ' );
1441
1451
if ( wp_script_is ( 'hoverintent-js ' , 'registered ' ) ) {
1442
1452
$ this ->assert_dev_mode_is_on_queried_element ( $ xpath , '//script[ contains( @src, "/hoverintent-js" ) ] ' );
1443
1453
}
@@ -1496,10 +1506,10 @@ function ( DOMXPath $xpath ) {
1496
1506
}
1497
1507
1498
1508
/**
1499
- * Test filter_admin_bar_script_loader_tag .
1509
+ * Test filter_script_loader_tag_for_dev_mode .
1500
1510
*
1501
1511
* @dataProvider get_data_to_test_filtering_admin_bar_script_loader_tag_data
1502
- * @covers \AMP_Theme_Support::filter_admin_bar_script_loader_tag ()
1512
+ * @covers \AMP_Theme_Support::filter_script_loader_tag_for_dev_mode ()
1503
1513
* @covers \AMP_Theme_Support::is_exclusively_dependent()
1504
1514
*
1505
1515
* @param callable $setup_callback Setup callback.
@@ -1509,7 +1519,7 @@ public function test_filter_admin_bar_script_loader_tag( $setup_callback, $asser
1509
1519
add_theme_support ( 'amp ' );
1510
1520
$ this ->go_to ( '/ ' );
1511
1521
add_filter ( 'amp_dev_mode_enabled ' , '__return_true ' );
1512
- add_filter ( 'script_loader_tag ' , [ 'AMP_Theme_Support ' , 'filter_admin_bar_script_loader_tag ' ], 10 , 2 );
1522
+ add_filter ( 'script_loader_tag ' , [ 'AMP_Theme_Support ' , 'filter_script_loader_tag_for_dev_mode ' ], 10 , 2 );
1513
1523
$ setup_callback ();
1514
1524
ob_start ();
1515
1525
echo '<html><head> ' ;
@@ -1519,8 +1529,14 @@ public function test_filter_admin_bar_script_loader_tag( $setup_callback, $asser
1519
1529
echo '</body></html> ' ;
1520
1530
$ output = ob_get_clean ();
1521
1531
1522
- $ dom = new DOMDocument ();
1523
- @$ dom ->loadHTML ( $ output ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
1532
+ $ dom = new Document ();
1533
+ $ dom ->loadHTML ( $ output );
1534
+
1535
+ AMP_Content_Sanitizer::sanitize_document (
1536
+ $ dom ,
1537
+ wp_array_slice_assoc ( amp_get_content_sanitizers (), [ 'AMP_Dev_Mode_Sanitizer ' ] ),
1538
+ [ 'use_document_element ' => true ]
1539
+ );
1524
1540
1525
1541
$ assert_callback ( new DOMXPath ( $ dom ) );
1526
1542
}
@@ -1529,15 +1545,18 @@ public function test_filter_admin_bar_script_loader_tag( $setup_callback, $asser
1529
1545
* Test init_admin_bar to ensure dashicons are not added to dev mode when directly enqueued.
1530
1546
*
1531
1547
* @covers \AMP_Theme_Support::init_admin_bar()
1532
- * @covers \AMP_Theme_Support::filter_admin_bar_style_loader_tag ()
1548
+ * @covers \AMP_Theme_Support::filter_style_loader_tag_for_dev_mode ()
1533
1549
*/
1534
1550
public function test_init_admin_bar_for_directly_enqueued_dashicons () {
1551
+ add_theme_support ( 'amp ' );
1552
+ $ this ->go_to ( '/ ' );
1535
1553
require_once ABSPATH . WPINC . '/class-wp-admin-bar.php ' ;
1536
1554
1537
1555
global $ wp_admin_bar ;
1538
1556
$ wp_admin_bar = new WP_Admin_Bar ();
1539
1557
$ wp_admin_bar ->initialize ();
1540
1558
AMP_Theme_Support::init_admin_bar ();
1559
+ AMP_Theme_Support::add_hooks ();
1541
1560
1542
1561
// Enqueued directly.
1543
1562
wp_enqueue_style ( 'dashicons ' );
@@ -1555,15 +1574,18 @@ public function test_init_admin_bar_for_directly_enqueued_dashicons() {
1555
1574
* Test init_admin_bar to ensure dashicons are not added to dev mode when indirectly enqueued.
1556
1575
*
1557
1576
* @covers \AMP_Theme_Support::init_admin_bar()
1558
- * @covers \AMP_Theme_Support::filter_admin_bar_style_loader_tag ()
1577
+ * @covers \AMP_Theme_Support::filter_style_loader_tag_for_dev_mode ()
1559
1578
*/
1560
1579
public function test_init_admin_bar_for_indirectly_enqueued_dashicons () {
1580
+ add_theme_support ( 'amp ' );
1581
+ $ this ->go_to ( '/ ' );
1561
1582
require_once ABSPATH . WPINC . '/class-wp-admin-bar.php ' ;
1562
1583
1563
1584
global $ wp_admin_bar ;
1564
1585
$ wp_admin_bar = new WP_Admin_Bar ();
1565
1586
$ wp_admin_bar ->initialize ();
1566
1587
AMP_Theme_Support::init_admin_bar ();
1588
+ AMP_Theme_Support::add_hooks ();
1567
1589
1568
1590
// Enqueued indirectly.
1569
1591
wp_enqueue_style ( 'my-font-pack ' , 'https://example.com/fonts ' , [ 'dashicons ' ], '0.1 ' );
0 commit comments