Automattic /
jetpack
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName |
||
| 2 | /** |
||
| 3 | * Tests for Automattic\Jetpack\Assets methods |
||
| 4 | * |
||
| 5 | * @package automattic/jetpack-assets |
||
| 6 | */ |
||
| 7 | |||
| 8 | namespace Automattic\Jetpack; |
||
| 9 | |||
| 10 | use PHPUnit\Framework\TestCase; |
||
| 11 | use Automattic\Jetpack\Constants as Jetpack_Constants; |
||
| 12 | use Brain\Monkey; |
||
| 13 | use Brain\Monkey\Filters; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * Retrieves a URL within the plugins or mu-plugins directory. |
||
| 17 | * |
||
| 18 | * @param string $path Extra path appended to the end of the URL, including the relative directory if $plugin is supplied. |
||
| 19 | * @param string $plugin_path A full path to a file inside a plugin or mu-plugin. |
||
| 20 | * The URL will be relative to its directory. |
||
| 21 | * Typically this is done by passing __FILE__ as the argument. |
||
| 22 | */ |
||
| 23 | function plugins_url( $path, $plugin_path ) { |
||
| 24 | return $plugin_path . $path; |
||
| 25 | } |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Enqueue a script. |
||
| 29 | * |
||
| 30 | * Registers the script if $src provided (does NOT overwrite), and enqueues it. |
||
| 31 | * |
||
| 32 | * @param string $handle Name of the script. Should be unique. |
||
| 33 | * @param string $src Full URL of the script, or path of the script relative to the WordPress root directory. |
||
| 34 | * Default empty. |
||
| 35 | * @param string[] $deps Optional. An array of registered script handles this script depends on. Default empty array. |
||
| 36 | * @param string|bool|null $ver Optional. String specifying script version number, if it has one, which is added to the URL |
||
| 37 | * as a query string for cache busting purposes. If version is set to false, a version |
||
| 38 | * number is automatically added equal to current installed WordPress version. |
||
| 39 | * If set to null, no version is added. |
||
| 40 | * @param bool $in_footer Optional. Whether to enqueue the script before </body> instead of in the <head>. |
||
| 41 | * Default 'false'. |
||
| 42 | */ |
||
| 43 | function wp_enqueue_script( $handle, $src = '', $deps = array(), $ver = false, $in_footer = false ) { |
||
| 44 | $GLOBALS['_was_called_wp_enqueue_script'][] = array( $handle, $src, $deps, $ver, $in_footer ); |
||
| 45 | } |
||
| 46 | |||
| 47 | /** |
||
| 48 | * A wrapper for PHP's parse_url() |
||
| 49 | * |
||
| 50 | * @param string $url The URL to parse. |
||
| 51 | * @param int $component The specific component to retrieve. Use one of the PHP |
||
| 52 | * predefined constants to specify which one. |
||
| 53 | * Defaults to -1 (= return all parts as an array). |
||
| 54 | */ |
||
| 55 | function wp_parse_url( $url, $component = -1 ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
| 56 | return parse_url( $url ); // phpcs:ignore WordPress.WP.AlternativeFunctions.parse_url_parse_url |
||
| 57 | } |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Assets test suite. |
||
| 61 | */ |
||
| 62 | class AssetsTest extends TestCase { |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Test setup. |
||
| 66 | */ |
||
| 67 | public function setUp() { |
||
| 68 | Monkey\setUp(); |
||
| 69 | $plugin_file = dirname( dirname( dirname( dirname( __DIR__ ) ) ) ) . '/jetpack.php'; |
||
| 70 | Jetpack_Constants::set_constant( 'JETPACK__PLUGIN_FILE', $plugin_file ); |
||
| 71 | |||
| 72 | } |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Run after every test. |
||
| 76 | */ |
||
| 77 | public function tearDown() { |
||
| 78 | Monkey\tearDown(); |
||
| 79 | $GLOBALS['_was_called_wp_enqueue_script'] = array(); |
||
| 80 | } |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Test get_file_url_for_environment |
||
| 84 | * |
||
| 85 | * @author ebinnion goldsounds |
||
| 86 | * @dataProvider get_file_url_for_environment_data_provider |
||
| 87 | * |
||
| 88 | * @param string $min_path minified path. |
||
| 89 | * @param string $non_min_path non-minified path. |
||
| 90 | * @param bool $is_script_debug Is SCRIPT_DEBUG enabled. |
||
| 91 | * @param string $expected Expected result. |
||
| 92 | * @param string $not_expected Non expected result. |
||
| 93 | */ |
||
| 94 | public function test_get_file_url_for_environment( $min_path, $non_min_path, $is_script_debug, $expected, $not_expected ) { |
||
| 95 | Constants::set_constant( 'SCRIPT_DEBUG', $is_script_debug ); |
||
|
0 ignored issues
–
show
|
|||
| 96 | $file_url = Assets::get_file_url_for_environment( $min_path, $non_min_path ); |
||
| 97 | |||
| 98 | // note the double-$$ here, $(non_)min_path is referenced by var name. |
||
| 99 | $this->assertContains( $$expected, $file_url ); |
||
| 100 | $this->assertNotContains( $$not_expected, $file_url ); |
||
| 101 | } |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Test that get_file_url_for_environment returns a full URL when given a full URL |
||
| 105 | * |
||
| 106 | * @author jeherve |
||
| 107 | * @dataProvider get_file_url_for_environment_full_urls_data_provider |
||
| 108 | * |
||
| 109 | * @param string $url Full URL we want to enqueue. |
||
| 110 | */ |
||
| 111 | public function test_get_file_url_for_environment_full_url( $url ) { |
||
| 112 | $file_url = Assets::get_file_url_for_environment( $url, $url ); |
||
| 113 | |||
| 114 | $this->assertEquals( $url, $file_url ); |
||
| 115 | } |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Tests ability for a filter to map specific URLs. |
||
| 119 | * |
||
| 120 | * @author kraftbj |
||
| 121 | * @see p58i-8nS-p2 |
||
| 122 | */ |
||
| 123 | public function test_get_file_url_for_environment_with_filter() { |
||
| 124 | Filters\expectApplied( 'jetpack_get_file_for_environment' )->once()->andReturn( 'special-test.js' ); |
||
| 125 | |||
| 126 | $file_url = Assets::get_file_url_for_environment( 'test.min.js', 'test.js' ); |
||
| 127 | |||
| 128 | $this->assertContains( 'special-test.js', $file_url ); |
||
| 129 | } |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Possible values for test_get_file_url_for_environment. |
||
| 133 | */ |
||
| 134 | public function get_file_url_for_environment_data_provider() { |
||
| 135 | return array( |
||
| 136 | 'script-debug-true' => array( |
||
| 137 | '_inc/build/shortcodes/js/instagram.js', |
||
| 138 | 'modules/shortcodes/js/instagram.js', |
||
| 139 | true, |
||
| 140 | 'non_min_path', |
||
| 141 | 'min_path', |
||
| 142 | ), |
||
| 143 | 'script-debug-false' => array( |
||
| 144 | '_inc/build/shortcodes/js/instagram.js', |
||
| 145 | 'modules/shortcodes/js/instagram.js', |
||
| 146 | false, |
||
| 147 | 'min_path', |
||
| 148 | 'non_min_path', |
||
| 149 | ), |
||
| 150 | ); |
||
| 151 | } |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Possible values for test_get_file_url_for_environment. |
||
| 155 | */ |
||
| 156 | public function get_file_url_for_environment_full_urls_data_provider() { |
||
| 157 | return array( |
||
| 158 | 'full_url' => array( 'https://jetpack.com/scripts/test.js' ), |
||
| 159 | 'protocol_relative' => array( '//jetpack.com/styles/test.css' ), |
||
| 160 | ); |
||
| 161 | } |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Test that enqueue_async_script calls adds the script_loader_tag filter |
||
| 165 | */ |
||
| 166 | public function test_enqueue_async_script_adds_script_loader_tag_filter() { |
||
| 167 | Assets::enqueue_async_script( 'handle', 'minpath.js', 'path.js', array(), '123', true ); |
||
| 168 | $asset_instance = Assets::instance(); |
||
| 169 | self::assertTrue( has_filter( 'script_loader_tag', array( $asset_instance, 'script_add_async' ) ) ); |
||
| 170 | } |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Test that enqueue_async_script calls wp_enqueue_script |
||
| 174 | */ |
||
| 175 | public function test_enqueue_async_script_calls_wp_enqueue_script() { |
||
| 176 | Assets::enqueue_async_script( 'handle', '/minpath.js', '/path.js', array(), '123', true ); |
||
| 177 | $this->assertEquals( |
||
| 178 | $GLOBALS['_was_called_wp_enqueue_script'], |
||
| 179 | array( array( 'handle', Assets::get_file_url_for_environment( '/minpath.js', '/path.js' ), array(), '123', true ) ) |
||
| 180 | ); |
||
| 181 | } |
||
| 182 | } |
||
| 183 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: