WordPress.org

Make WordPress Core

Changeset 35762


Ignore:
Timestamp:
12/04/2015 05:45:29 AM (4 years ago)
Author:
nacin
Message:

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

fixes #34698.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/js/wp-embed.js

    r35761 r35762  
     1/**
     2 * WordPress inline HTML embed
     3 *
     4 * @since 4.4.0
     5 *
     6 * This file cannot have ampersands in it. This is to ensure
     7 * it can be embedded in older versions of WordPress.
     8 * See https://core.trac.wordpress.org/changeset/35708.
     9 */
    110(function ( window, document ) {
    211    'use strict';
  • trunk/tests/phpunit/tests/oembed/template.php

    r35472 r35762  
    254254        $this->assertTrue( wp_script_is( 'wp-embed' ) );
    255255    }
     256
     257    /**
     258     * @ticket 34698
     259     */
     260    function test_js_no_ampersands() {
     261        $this->assertNotContains( '&', file_get_contents( ABSPATH . WPINC . '/js/wp-embed.js' ) );
     262    }
     263
     264    /**
     265     * @ticket 34698
     266     *
     267     * @depends test_js_no_ampersands
     268     *
     269     * The previous test confirms that no ampersands exist in src/wp-includes/js/wp-embed.js.
     270     * However, we must also confirm that UglifyJS does not add ampersands during its
     271     * optimizations (which we tweak to avoid, but indirectly -- understandably, there's
     272     * no "don't add ampersands to my JavaScript file" option).
     273     *
     274     * So this test checks for ampersands in build/wp-includes/js/wp-embed.min.js.
     275     * In many cases, this file will not exist; in those cases, we simply skip the test.
     276     *
     277     * So when would it be run? We have Travis CI run `grunt test` which then runs, in order,
     278     * `qunit:compiled` (which runs the build) and then `phpunit`. Thus, this test will at least be
     279     * run during continuous integration.
     280     *
     281     * However, we need to verify that `qunit:compiled` runs before `phpunit`. So this test also
     282     * does a cheap check for a registered Grunt task called `test` that contains both
     283     * `qunit:compiled` and `phpunit`, in that order.
     284     *
     285     * One final failsafe: The Gruntfile.js assertion takes place before checking for the existence
     286     * of wp-embed.min.js. If the Grunt tasks are significantly refactored later, it could indicate
     287     * that wp-embed.min.js doesn't exist anymore. We wouldn't want the test to silently become one
     288     * that is always skipped, and thus useless.
     289     */
     290    function test_js_no_ampersands_in_compiled() {
     291        $gruntfile = file_get_contents( dirname( ABSPATH ) . '/Gruntfile.js' );
     292
     293        // Confirm this file *should* exist, otherwise this test will always be skipped.
     294        $test = '/grunt.registerTask\(\s*\'test\',.*\'qunit:compiled\'.*\'phpunit\'/';
     295        $this->assertTrue( (bool) preg_match( $test, $gruntfile ) );
     296
     297        $file = dirname( ABSPATH ) . '/build/' . WPINC . '/js/wp-embed.min.js';
     298        if ( ! file_exists( $file ) ) {
     299            $this->markTestSkipped( "This test is for the compiled wp-embed.min.js file." );
     300        }
     301        $this->assertNotContains( '&', file_get_contents( $file ) );
     302    }
     303
    256304}
Note: See TracChangeset for help on using the changeset viewer.