Completed
Push — update/simplify_assets_depende... ( 5a4931 )
by
unknown
40:08 queued 31:50
created

test-assets.php ➔ wp_parse_url()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 3
rs 10
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
/**
11
 * Assets test suite.
12
 */
13
class AssetsTest extends \WorDBless\BaseTestCase {
14
15
	/**
16
	 * Test get_file_url_for_environment
17
	 *
18
	 * @author ebinnion goldsounds
19
	 * @dataProvider get_file_url_for_environment_data_provider
20
	 *
21
	 * @param string $min_path        minified path.
22
	 * @param string $non_min_path    non-minified path.
23
	 * @param bool   $is_script_debug Is SCRIPT_DEBUG enabled.
24
	 * @param string $expected        Expected result.
25
	 * @param string $not_expected    Non expected result.
26
	 */
27 View Code Duplication
	public function test_get_file_url_for_environment( $min_path, $non_min_path, $is_script_debug, $expected, $not_expected ) {
28
		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...
29
		$file_url = Assets::get_file_url_for_environment( $min_path, $non_min_path );
30
31
		// note the double-$$ here, $(non_)min_path is referenced by var name.
32
		$this->assertContains( $$expected, $file_url );
33
		$this->assertNotContains( $$not_expected, $file_url );
34
	}
35
36
	/**
37
	 * Test that get_file_url_for_environment returns a full URL when given a full URL
38
	 *
39
	 * @author jeherve
40
	 * @dataProvider get_file_url_for_environment_full_urls_data_provider
41
	 *
42
	 * @param string $url Full URL we want to enqueue.
43
	 */
44
	public function test_get_file_url_for_environment_full_url( $url ) {
45
		$file_url = Assets::get_file_url_for_environment( $url, $url );
46
47
		$this->assertEquals( $url, $file_url );
48
	}
49
50
	/**
51
	 * Test that get_file_url_for_environment returns a full package asset url when package path is provided.
52
	 *
53
	 * @param string $min_path        minified path.
54
	 * @param string $non_min_path    non-minified path.
55
	 * @param string $package_path    Package path.
56
	 * @param bool   $is_script_debug Is SCRIPT_DEBUG enabled.
57
	 * @param string $expected        Expected result.
58
	 * @param string $not_expected    Non expected result.
59
	 *
60
	 * @author       davidlonjon
61
	 * @dataProvider get_file_url_for_environment_package_path_data_provider
62
	 */
63 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 ) {
64
		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...
65
		$file_url = Assets::get_file_url_for_environment( $min_path, $non_min_path, $package_path );
66
67
		$this->assertContains( $expected, $file_url );
68
		$this->assertNotContains( $not_expected, $file_url );
69
	}
70
71
	/**
72
	 * Tests ability for a filter to map specific URLs.
73
	 *
74
	 * @author kraftbj
75
	 * @see p58i-8nS-p2
76
	 */
77
	public function test_get_file_url_for_environment_with_filter() {
78
		add_filter(
79
			'jetpack_get_file_for_environment',
80
			function() {
81
				return 'special-test.js';
82
			}
83
		);
84
85
		$file_url = Assets::get_file_url_for_environment( 'test.min.js', 'test.js' );
86
87
		$this->assertContains( 'special-test.js', $file_url );
88
	}
89
90
	/**
91
	 * Possible values for test_get_file_url_for_environment.
92
	 */
93
	public function get_file_url_for_environment_data_provider() {
94
		return array(
95
			'script-debug-true'  => array(
96
				'_inc/build/shortcodes/js/recipes.js',
97
				'modules/shortcodes/js/recipes.js',
98
				true,
99
				'non_min_path',
100
				'min_path',
101
			),
102
			'script-debug-false' => array(
103
				'_inc/build/shortcodes/js/recipes.js',
104
				'modules/shortcodes/js/recipes.js',
105
				false,
106
				'min_path',
107
				'non_min_path',
108
			),
109
		);
110
	}
111
112
	/**
113
	 * Possible values for test_get_file_url_for_environment.
114
	 */
115
	public function get_file_url_for_environment_full_urls_data_provider() {
116
		return array(
117
			'full_url'          => array( 'https://jetpack.com/scripts/test.js' ),
118
			'protocol_relative' => array( '//jetpack.com/styles/test.css' ),
119
		);
120
	}
121
122
	/**
123
	 * Possible values for test_get_file_url_for_environment.
124
	 */
125
	public function get_file_url_for_environment_package_path_data_provider() {
126
		$min_path     = 'src/js/test.min.js';
127
		$non_min_path = 'src/js/test.js';
128
		$package_path = '/var/html/wp-content/plugins/jetpack/packages/test-package/test-package.php';
129
130
		return array(
131
			'script-debug-true'  => array(
132
				$min_path,
133
				$non_min_path,
134
				$package_path,
135
				true,
136
				'wp-content/plugins/jetpack/packages/test-package/' . $non_min_path,
137
				'wp-content/plugins/jetpack/packages/test-package/' . $min_path,
138
139
			),
140
			'script-debug-false' => array(
141
				$min_path,
142
				$non_min_path,
143
				$package_path,
144
				false,
145
				'wp-content/plugins/jetpack/packages/test-package/' . $min_path,
146
				'wp-content/plugins/jetpack/packages/test-package/' . $non_min_path,
147
			),
148
		);
149
	}
150
151
	/**
152
	 * Test that enqueue_async_script calls adds the script_loader_tag filter
153
	 */
154
	public function test_enqueue_async_script_adds_script_loader_tag_filter() {
155
		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...
156
		$asset_instance = Assets::instance();
157
		self::assertEquals( 10, has_filter( 'script_loader_tag', array( $asset_instance, 'script_add_async' ) ) );
158
	}
159
160
	/**
161
	 * Test that enqueue_async_script calls wp_enqueue_script
162
	 */
163
	public function test_enqueue_async_script_calls_wp_enqueue_script() {
164
		Assets::enqueue_async_script( 'test-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...
165
166
		$this->assertTrue( wp_script_is( 'test-handle', 'enqueued' ) );
167
168
		$enqueued_script = wp_scripts()->query( 'test-handle', 'registered' );
169
170
		$this->assertEquals( Assets::get_file_url_for_environment( '/minpath.js', '/path.js' ), $enqueued_script->src );
171
		$this->assertEquals( '123', $enqueued_script->ver );
172
173
	}
174
}
175