Completed
Push — update/build-to-prepare-for-wp... ( a960c5...31a28f )
by
unknown
156:22 queued 147:01
created

AssetsTest::get_resources_urls()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 39
rs 9.296
c 0
b 0
f 0
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 Automattic\Jetpack\Constants as Jetpack_Constants;
11
use Brain\Monkey;
12
use Brain\Monkey\Filters;
13
use Brain\Monkey\Functions;
14
use PHPUnit\Framework\TestCase;
15
16
/**
17
 * Retrieves a URL within the plugins or mu-plugins directory.
18
 *
19
 * @param string $path        Extra path appended to the end of the URL, including the relative directory if $plugin is supplied.
20
 * @param string $plugin_path A full path to a file inside a plugin or mu-plugin.
21
 *                            The URL will be relative to its directory.
22
 *                            Typically this is done by passing __FILE__ as the argument.
23
 */
24
function plugins_url( $path, $plugin_path ) {
25
	if ( strpos( $plugin_path, 'test-package.php' ) ) {
26
		return 'http://www.example.com/wp-content/plugins/jetpack/packages/test-package/' . $path;
27
	}
28
29
	return 'http://www.example.com//wp-content/plugins/jetpack/' . $path;
30
}
31
32
/**
33
 * Enqueue a script.
34
 *
35
 * Registers the script if $src provided (does NOT overwrite), and enqueues it.
36
 *
37
 * @param string           $handle    Name of the script. Should be unique.
38
 * @param string           $src       Full URL of the script, or path of the script relative to the WordPress root directory.
39
 *                                    Default empty.
40
 * @param string[]         $deps      Optional. An array of registered script handles this script depends on. Default empty array.
41
 * @param string|bool|null $ver       Optional. String specifying script version number, if it has one, which is added to the URL
42
 *                                    as a query string for cache busting purposes. If version is set to false, a version
43
 *                                    number is automatically added equal to current installed WordPress version.
44
 *                                    If set to null, no version is added.
45
 * @param bool             $in_footer Optional. Whether to enqueue the script before </body> instead of in the <head>.
46
 *                                    Default 'false'.
47
 */
48
function wp_enqueue_script( $handle, $src = '', $deps = array(), $ver = false, $in_footer = false ) {
49
	$GLOBALS['_was_called_wp_enqueue_script'][] = array( $handle, $src, $deps, $ver, $in_footer );
50
}
51
52
/**
53
 * A wrapper for PHP's parse_url()
54
 *
55
 * @param string $url       The URL to parse.
56
 * @param int    $component The specific component to retrieve. Use one of the PHP
57
 *                          predefined constants to specify which one.
58
 *                          Defaults to -1 (= return all parts as an array).
59
 */
60
function wp_parse_url( $url, $component = -1 ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
61
	return parse_url( $url, $component ); // phpcs:ignore WordPress.WP.AlternativeFunctions.parse_url_parse_url
62
}
63
64
/**
65
 * Assets test suite.
66
 */
67
class AssetsTest extends TestCase {
68
	use \Yoast\PHPUnitPolyfills\Polyfills\AssertStringContains;
69
70
	/**
71
	 * Test setup.
72
	 *
73
	 * @before
74
	 */
75
	public function set_up() {
76
		Monkey\setUp();
77
		$plugin_file = dirname( dirname( dirname( dirname( __DIR__ ) ) ) ) . '/jetpack.php';
78
		Jetpack_Constants::set_constant( 'JETPACK__PLUGIN_FILE', $plugin_file );
79
80
	}
81
82
	/**
83
	 * Run after every test.
84
	 *
85
	 * @after
86
	 */
87
	public function tear_down() {
88
		Monkey\tearDown();
89
		$GLOBALS['_was_called_wp_enqueue_script'] = array();
90
	}
91
92
	/**
93
	 * Test get_file_url_for_environment
94
	 *
95
	 * @author ebinnion goldsounds
96
	 * @dataProvider get_file_url_for_environment_data_provider
97
	 *
98
	 * @param string $min_path        minified path.
99
	 * @param string $non_min_path    non-minified path.
100
	 * @param bool   $is_script_debug Is SCRIPT_DEBUG enabled.
101
	 * @param string $expected        Expected result.
102
	 * @param string $not_expected    Non expected result.
103
	 */
104 View Code Duplication
	public function test_get_file_url_for_environment( $min_path, $non_min_path, $is_script_debug, $expected, $not_expected ) {
105
		Constants::set_constant( 'SCRIPT_DEBUG', $is_script_debug );
0 ignored issues
show
Documentation introduced by
$is_script_debug is of type boolean, but the function expects a string.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
106
		$file_url = Assets::get_file_url_for_environment( $min_path, $non_min_path );
107
108
		// note the double-$$ here, $(non_)min_path is referenced by var name.
109
		$this->assertStringContainsString( $$expected, $file_url );
110
		$this->assertStringNotContainsString( $$not_expected, $file_url );
111
	}
112
113
	/**
114
	 * Test that get_file_url_for_environment returns a full URL when given a full URL
115
	 *
116
	 * @author jeherve
117
	 * @dataProvider get_file_url_for_environment_full_urls_data_provider
118
	 *
119
	 * @param string $url Full URL we want to enqueue.
120
	 */
121
	public function test_get_file_url_for_environment_full_url( $url ) {
122
		$file_url = Assets::get_file_url_for_environment( $url, $url );
123
124
		$this->assertEquals( $url, $file_url );
125
	}
126
127
	/**
128
	 * Test that get_file_url_for_environment returns a full package asset url when package path is provided.
129
	 *
130
	 * @param string $min_path        minified path.
131
	 * @param string $non_min_path    non-minified path.
132
	 * @param string $package_path    Package path.
133
	 * @param bool   $is_script_debug Is SCRIPT_DEBUG enabled.
134
	 * @param string $expected        Expected result.
135
	 * @param string $not_expected    Non expected result.
136
	 *
137
	 * @author       davidlonjon
138
	 * @dataProvider get_file_url_for_environment_package_path_data_provider
139
	 */
140 View Code Duplication
	public function test_get_file_url_for_environment_package_path( $min_path, $non_min_path, $package_path, $is_script_debug, $expected, $not_expected ) {
141
		Constants::set_constant( 'SCRIPT_DEBUG', $is_script_debug );
0 ignored issues
show
Documentation introduced by
$is_script_debug is of type boolean, but the function expects a string.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
142
		$file_url = Assets::get_file_url_for_environment( $min_path, $non_min_path, $package_path );
143
144
		$this->assertStringContainsString( $expected, $file_url );
145
		$this->assertStringNotContainsString( $not_expected, $file_url );
146
	}
147
148
	/**
149
	 * Tests ability for a filter to map specific URLs.
150
	 *
151
	 * @author kraftbj
152
	 * @see p58i-8nS-p2
153
	 */
154
	public function test_get_file_url_for_environment_with_filter() {
155
		Filters\expectApplied( 'jetpack_get_file_for_environment' )->once()->andReturn( 'special-test.js' );
156
157
		$file_url = Assets::get_file_url_for_environment( 'test.min.js', 'test.js' );
158
159
		$this->assertStringContainsString( 'special-test.js', $file_url );
160
	}
161
162
	/**
163
	 * Possible values for test_get_file_url_for_environment.
164
	 */
165
	public function get_file_url_for_environment_data_provider() {
166
		return array(
167
			'script-debug-true'  => array(
168
				'_inc/build/shortcodes/js/recipes.js',
169
				'modules/shortcodes/js/recipes.js',
170
				true,
171
				'non_min_path',
172
				'min_path',
173
			),
174
			'script-debug-false' => array(
175
				'_inc/build/shortcodes/js/recipes.js',
176
				'modules/shortcodes/js/recipes.js',
177
				false,
178
				'min_path',
179
				'non_min_path',
180
			),
181
		);
182
	}
183
184
	/**
185
	 * Possible values for test_get_file_url_for_environment.
186
	 */
187
	public function get_file_url_for_environment_full_urls_data_provider() {
188
		return array(
189
			'full_url'          => array( 'https://jetpack.com/scripts/test.js' ),
190
			'protocol_relative' => array( '//jetpack.com/styles/test.css' ),
191
		);
192
	}
193
194
	/**
195
	 * Possible values for test_get_file_url_for_environment.
196
	 */
197
	public function get_file_url_for_environment_package_path_data_provider() {
198
		$min_path     = 'src/js/test.min.js';
199
		$non_min_path = 'src/js/test.js';
200
		$package_path = '/var/html/wp-content/plugins/jetpack/packages/test-package/test-package.php';
201
202
		return array(
203
			'script-debug-true'  => array(
204
				$min_path,
205
				$non_min_path,
206
				$package_path,
207
				true,
208
				'wp-content/plugins/jetpack/packages/test-package/' . $non_min_path,
209
				'wp-content/plugins/jetpack/packages/test-package/' . $min_path,
210
211
			),
212
			'script-debug-false' => array(
213
				$min_path,
214
				$non_min_path,
215
				$package_path,
216
				false,
217
				'wp-content/plugins/jetpack/packages/test-package/' . $min_path,
218
				'wp-content/plugins/jetpack/packages/test-package/' . $non_min_path,
219
			),
220
		);
221
	}
222
223
	/**
224
	 * Test that enqueue_async_script calls adds the script_loader_tag filter
225
	 */
226
	public function test_enqueue_async_script_adds_script_loader_tag_filter() {
227
		Assets::enqueue_async_script( 'handle', 'minpath.js', 'path.js', array(), '123', true );
0 ignored issues
show
Documentation introduced by
'123' is of type string, but the function expects a boolean.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
228
		$asset_instance = Assets::instance();
229
		self::assertEquals( 10, (int) has_filter( 'script_loader_tag', array( $asset_instance, 'script_add_async' ) ) );
230
	}
231
232
	/**
233
	 * Test that enqueue_async_script calls wp_enqueue_script
234
	 */
235
	public function test_enqueue_async_script_calls_wp_enqueue_script() {
236
		Assets::enqueue_async_script( 'handle', '/minpath.js', '/path.js', array(), '123', true );
0 ignored issues
show
Documentation introduced by
'123' is of type string, but the function expects a boolean.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
237
		$this->assertEquals(
238
			$GLOBALS['_was_called_wp_enqueue_script'],
239
			array( array( 'handle', Assets::get_file_url_for_environment( '/minpath.js', '/path.js' ), array(), '123', true ) )
240
		);
241
	}
242
243
	/**
244
	 * Test whether static resources are properly updated to use a WordPress.com static domain.
245
	 *
246
	 * @covers Automattic\Jetpack\Status::staticize_subdomain
247
	 * @dataProvider get_resources_urls
248
	 *
249
	 * @param string $original       Source URL.
250
	 * @param string $expected_http  Expected WordPress.com Static URL when we're mocking a site using HTTP.
251
	 * @param string $expected_https Expected WordPress.com Static URL when we're mocking a site using HTTPS.
252
	 */
253
	public function test_staticize_subdomain( $original, $expected_http, $expected_https ) {
254
		Functions\when( 'is_ssl' )->justReturn( false );
255
		$static_resource = Assets::staticize_subdomain( $original );
256
		$this->assertStringContainsString( $expected_http, $static_resource );
257
258
		Functions\when( 'is_ssl' )->justReturn( true );
259
		$static_resource = Assets::staticize_subdomain( $original );
260
		$this->assertEquals( $expected_https, $static_resource );
261
	}
262
263
	/**
264
	 * Data provider to test staticize_subdomain
265
	 */
266
	public function get_resources_urls() {
267
		return array(
268
			'non_wpcom_domain'  => array(
269
				'https://example.org/thing.jpg',
270
				'https://example.org/thing.jpg',
271
				'https://example.org/thing.jpg',
272
			),
273
			'wp_in_the_name'    => array(
274
				'https://examplewp.com/thing.jpg',
275
				'https://examplewp.com/thing.jpg',
276
				'https://examplewp.com/thing.jpg',
277
			),
278
			'local_domain'      => array(
279
				'https://localhost/dir/thing.jpg',
280
				'https://localhost/dir/thing.jpg',
281
				'https://localhost/dir/thing.jpg',
282
			),
283
			'wordpresscom'      => array(
284
				'https://wordpress.com/i/blank.jpg',
285
				'.wp.com/i/blank.jpg',
286
				'https://s-ssl.wordpress.com/i/blank.jpg',
287
			),
288
			'wpcom'             => array(
289
				'https://wp.com/i/blank.jpg',
290
				'.wp.com/i/blank.jpg',
291
				'https://s-ssl.wordpress.com/i/blank.jpg',
292
			),
293
			'www_wordpresscom'  => array(
294
				'https://www.wordpress.com/i/blank.jpg',
295
				'.wp.com/i/blank.jpg',
296
				'https://s-ssl.wordpress.com/i/blank.jpg',
297
			),
298
			'http_wordpresscom' => array(
299
				'http://wordpress.com/i/blank.jpg',
300
				'.wp.com/i/blank.jpg',
301
				'https://s-ssl.wordpress.com/i/blank.jpg',
302
			),
303
		);
304
	}
305
}
306