diff --git a/src/wp-includes/js/wp-embed.js b/src/wp-includes/js/wp-embed.js index 985f0b801d..660558e03b 100644 --- a/src/wp-includes/js/wp-embed.js +++ b/src/wp-includes/js/wp-embed.js @@ -1,3 +1,12 @@ +/** + * WordPress inline HTML embed + * + * @since 4.4.0 + * + * This file cannot have ampersands in it. This is to ensure + * it can be embedded in older versions of WordPress. + * See https://core.trac.wordpress.org/changeset/35708. + */ (function ( window, document ) { 'use strict'; diff --git a/tests/phpunit/tests/oembed/template.php b/tests/phpunit/tests/oembed/template.php index ca07035b23..9121670185 100644 --- a/tests/phpunit/tests/oembed/template.php +++ b/tests/phpunit/tests/oembed/template.php @@ -253,4 +253,52 @@ class Tests_Embed_Template extends WP_UnitTestCase { $this->assertTrue( wp_script_is( 'wp-embed' ) ); } + + /** + * @ticket 34698 + */ + function test_js_no_ampersands() { + $this->assertNotContains( '&', file_get_contents( ABSPATH . WPINC . '/js/wp-embed.js' ) ); + } + + /** + * @ticket 34698 + * + * @depends test_js_no_ampersands + * + * The previous test confirms that no ampersands exist in src/wp-includes/js/wp-embed.js. + * However, we must also confirm that UglifyJS does not add ampersands during its + * optimizations (which we tweak to avoid, but indirectly -- understandably, there's + * no "don't add ampersands to my JavaScript file" option). + * + * So this test checks for ampersands in build/wp-includes/js/wp-embed.min.js. + * In many cases, this file will not exist; in those cases, we simply skip the test. + * + * So when would it be run? We have Travis CI run `grunt test` which then runs, in order, + * `qunit:compiled` (which runs the build) and then `phpunit`. Thus, this test will at least be + * run during continuous integration. + * + * However, we need to verify that `qunit:compiled` runs before `phpunit`. So this test also + * does a cheap check for a registered Grunt task called `test` that contains both + * `qunit:compiled` and `phpunit`, in that order. + * + * One final failsafe: The Gruntfile.js assertion takes place before checking for the existence + * of wp-embed.min.js. If the Grunt tasks are significantly refactored later, it could indicate + * that wp-embed.min.js doesn't exist anymore. We wouldn't want the test to silently become one + * that is always skipped, and thus useless. + */ + function test_js_no_ampersands_in_compiled() { + $gruntfile = file_get_contents( dirname( ABSPATH ) . '/Gruntfile.js' ); + + // Confirm this file *should* exist, otherwise this test will always be skipped. + $test = '/grunt.registerTask\(\s*\'test\',.*\'qunit:compiled\'.*\'phpunit\'/'; + $this->assertTrue( (bool) preg_match( $test, $gruntfile ) ); + + $file = dirname( ABSPATH ) . '/build/' . WPINC . '/js/wp-embed.min.js'; + if ( ! file_exists( $file ) ) { + $this->markTestSkipped( "This test is for the compiled wp-embed.min.js file." ); + } + $this->assertNotContains( '&', file_get_contents( $file ) ); + } + }