Embeds: Enforce, via unit tests, the no-ampersand rule for wp-embed.js.

fixes #34698.


git-svn-id: https://develop.svn.wordpress.org/trunk@35762 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Andrew Nacin 2015-12-04 05:45:29 +00:00
parent dedff8fd0e
commit 12b1cc4410
2 changed files with 57 additions and 0 deletions

View File

@ -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';

View File

@ -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 ) );
}
}