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 PHPUnit\Framework\TestCase; |
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
|
|
|
if ( strpos( $plugin_path, 'test-package.php' ) ) { |
25
|
|
|
return 'http://www.example.com/wp-content/plugins/jetpack/packages/test-package/' . $path; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
return 'http://www.example.com//wp-content/plugins/jetpack/' . $path; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Enqueue a script. |
33
|
|
|
* |
34
|
|
|
* Registers the script if $src provided (does NOT overwrite), and enqueues it. |
35
|
|
|
* |
36
|
|
|
* @param string $handle Name of the script. Should be unique. |
37
|
|
|
* @param string $src Full URL of the script, or path of the script relative to the WordPress root directory. |
38
|
|
|
* Default empty. |
39
|
|
|
* @param string[] $deps Optional. An array of registered script handles this script depends on. Default empty array. |
40
|
|
|
* @param string|bool|null $ver Optional. String specifying script version number, if it has one, which is added to the URL |
41
|
|
|
* as a query string for cache busting purposes. If version is set to false, a version |
42
|
|
|
* number is automatically added equal to current installed WordPress version. |
43
|
|
|
* If set to null, no version is added. |
44
|
|
|
* @param bool $in_footer Optional. Whether to enqueue the script before </body> instead of in the <head>. |
45
|
|
|
* Default 'false'. |
46
|
|
|
*/ |
47
|
|
|
function wp_enqueue_script( $handle, $src = '', $deps = array(), $ver = false, $in_footer = false ) { |
48
|
|
|
$GLOBALS['_was_called_wp_enqueue_script'][] = array( $handle, $src, $deps, $ver, $in_footer ); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* A wrapper for PHP's parse_url() |
53
|
|
|
* |
54
|
|
|
* @param string $url The URL to parse. |
55
|
|
|
* @param int $component The specific component to retrieve. Use one of the PHP |
56
|
|
|
* predefined constants to specify which one. |
57
|
|
|
* Defaults to -1 (= return all parts as an array). |
58
|
|
|
*/ |
59
|
|
|
function wp_parse_url( $url, $component = -1 ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
60
|
|
|
return parse_url( $url ); // phpcs:ignore WordPress.WP.AlternativeFunctions.parse_url_parse_url |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Assets test suite. |
65
|
|
|
*/ |
66
|
|
|
class AssetsTest extends TestCase { |
67
|
|
|
use \Yoast\PHPUnitPolyfills\Polyfills\AssertStringContains; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Test setup. |
71
|
|
|
* |
72
|
|
|
* @before |
73
|
|
|
*/ |
74
|
|
|
public function set_up() { |
75
|
|
|
Monkey\setUp(); |
76
|
|
|
$plugin_file = dirname( dirname( dirname( dirname( __DIR__ ) ) ) ) . '/jetpack.php'; |
77
|
|
|
Jetpack_Constants::set_constant( 'JETPACK__PLUGIN_FILE', $plugin_file ); |
78
|
|
|
|
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Run after every test. |
83
|
|
|
* |
84
|
|
|
* @after |
85
|
|
|
*/ |
86
|
|
|
public function tear_down() { |
87
|
|
|
Monkey\tearDown(); |
88
|
|
|
$GLOBALS['_was_called_wp_enqueue_script'] = array(); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Test get_file_url_for_environment |
93
|
|
|
* |
94
|
|
|
* @author ebinnion goldsounds |
95
|
|
|
* @dataProvider get_file_url_for_environment_data_provider |
96
|
|
|
* |
97
|
|
|
* @param string $min_path minified path. |
98
|
|
|
* @param string $non_min_path non-minified path. |
99
|
|
|
* @param bool $is_script_debug Is SCRIPT_DEBUG enabled. |
100
|
|
|
* @param string $expected Expected result. |
101
|
|
|
* @param string $not_expected Non expected result. |
102
|
|
|
*/ |
103
|
|
View Code Duplication |
public function test_get_file_url_for_environment( $min_path, $non_min_path, $is_script_debug, $expected, $not_expected ) { |
104
|
|
|
Constants::set_constant( 'SCRIPT_DEBUG', $is_script_debug ); |
|
|
|
|
105
|
|
|
$file_url = Assets::get_file_url_for_environment( $min_path, $non_min_path ); |
106
|
|
|
|
107
|
|
|
// note the double-$$ here, $(non_)min_path is referenced by var name. |
108
|
|
|
$this->assertStringContainsString( $$expected, $file_url ); |
109
|
|
|
$this->assertStringNotContainsString( $$not_expected, $file_url ); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Test that get_file_url_for_environment returns a full URL when given a full URL |
114
|
|
|
* |
115
|
|
|
* @author jeherve |
116
|
|
|
* @dataProvider get_file_url_for_environment_full_urls_data_provider |
117
|
|
|
* |
118
|
|
|
* @param string $url Full URL we want to enqueue. |
119
|
|
|
*/ |
120
|
|
|
public function test_get_file_url_for_environment_full_url( $url ) { |
121
|
|
|
$file_url = Assets::get_file_url_for_environment( $url, $url ); |
122
|
|
|
|
123
|
|
|
$this->assertEquals( $url, $file_url ); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Test that get_file_url_for_environment returns a full package asset url when package path is provided. |
128
|
|
|
* |
129
|
|
|
* @param string $min_path minified path. |
130
|
|
|
* @param string $non_min_path non-minified path. |
131
|
|
|
* @param string $package_path Package path. |
132
|
|
|
* @param bool $is_script_debug Is SCRIPT_DEBUG enabled. |
133
|
|
|
* @param string $expected Expected result. |
134
|
|
|
* @param string $not_expected Non expected result. |
135
|
|
|
* |
136
|
|
|
* @author davidlonjon |
137
|
|
|
* @dataProvider get_file_url_for_environment_package_path_data_provider |
138
|
|
|
*/ |
139
|
|
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 ) { |
140
|
|
|
Constants::set_constant( 'SCRIPT_DEBUG', $is_script_debug ); |
|
|
|
|
141
|
|
|
$file_url = Assets::get_file_url_for_environment( $min_path, $non_min_path, $package_path ); |
142
|
|
|
|
143
|
|
|
$this->assertStringContainsString( $expected, $file_url ); |
144
|
|
|
$this->assertStringNotContainsString( $not_expected, $file_url ); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Tests ability for a filter to map specific URLs. |
149
|
|
|
* |
150
|
|
|
* @author kraftbj |
151
|
|
|
* @see p58i-8nS-p2 |
152
|
|
|
*/ |
153
|
|
|
public function test_get_file_url_for_environment_with_filter() { |
154
|
|
|
Filters\expectApplied( 'jetpack_get_file_for_environment' )->once()->andReturn( 'special-test.js' ); |
155
|
|
|
|
156
|
|
|
$file_url = Assets::get_file_url_for_environment( 'test.min.js', 'test.js' ); |
157
|
|
|
|
158
|
|
|
$this->assertStringContainsString( 'special-test.js', $file_url ); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Possible values for test_get_file_url_for_environment. |
163
|
|
|
*/ |
164
|
|
|
public function get_file_url_for_environment_data_provider() { |
165
|
|
|
return array( |
166
|
|
|
'script-debug-true' => array( |
167
|
|
|
'_inc/build/shortcodes/js/recipes.js', |
168
|
|
|
'modules/shortcodes/js/recipes.js', |
169
|
|
|
true, |
170
|
|
|
'non_min_path', |
171
|
|
|
'min_path', |
172
|
|
|
), |
173
|
|
|
'script-debug-false' => array( |
174
|
|
|
'_inc/build/shortcodes/js/recipes.js', |
175
|
|
|
'modules/shortcodes/js/recipes.js', |
176
|
|
|
false, |
177
|
|
|
'min_path', |
178
|
|
|
'non_min_path', |
179
|
|
|
), |
180
|
|
|
); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Possible values for test_get_file_url_for_environment. |
185
|
|
|
*/ |
186
|
|
|
public function get_file_url_for_environment_full_urls_data_provider() { |
187
|
|
|
return array( |
188
|
|
|
'full_url' => array( 'https://jetpack.com/scripts/test.js' ), |
189
|
|
|
'protocol_relative' => array( '//jetpack.com/styles/test.css' ), |
190
|
|
|
); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Possible values for test_get_file_url_for_environment. |
195
|
|
|
*/ |
196
|
|
|
public function get_file_url_for_environment_package_path_data_provider() { |
197
|
|
|
$min_path = 'src/js/test.min.js'; |
198
|
|
|
$non_min_path = 'src/js/test.js'; |
199
|
|
|
$package_path = '/var/html/wp-content/plugins/jetpack/packages/test-package/test-package.php'; |
200
|
|
|
|
201
|
|
|
return array( |
202
|
|
|
'script-debug-true' => array( |
203
|
|
|
$min_path, |
204
|
|
|
$non_min_path, |
205
|
|
|
$package_path, |
206
|
|
|
true, |
207
|
|
|
'wp-content/plugins/jetpack/packages/test-package/' . $non_min_path, |
208
|
|
|
'wp-content/plugins/jetpack/packages/test-package/' . $min_path, |
209
|
|
|
|
210
|
|
|
), |
211
|
|
|
'script-debug-false' => array( |
212
|
|
|
$min_path, |
213
|
|
|
$non_min_path, |
214
|
|
|
$package_path, |
215
|
|
|
false, |
216
|
|
|
'wp-content/plugins/jetpack/packages/test-package/' . $min_path, |
217
|
|
|
'wp-content/plugins/jetpack/packages/test-package/' . $non_min_path, |
218
|
|
|
), |
219
|
|
|
); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Test that enqueue_async_script calls adds the script_loader_tag filter |
224
|
|
|
*/ |
225
|
|
|
public function test_enqueue_async_script_adds_script_loader_tag_filter() { |
226
|
|
|
Assets::enqueue_async_script( 'handle', 'minpath.js', 'path.js', array(), '123', true ); |
|
|
|
|
227
|
|
|
$asset_instance = Assets::instance(); |
228
|
|
|
self::assertEquals( 10, (int) has_filter( 'script_loader_tag', array( $asset_instance, 'script_add_async' ) ) ); |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* Test that enqueue_async_script calls wp_enqueue_script |
233
|
|
|
*/ |
234
|
|
|
public function test_enqueue_async_script_calls_wp_enqueue_script() { |
235
|
|
|
Assets::enqueue_async_script( 'handle', '/minpath.js', '/path.js', array(), '123', true ); |
|
|
|
|
236
|
|
|
$this->assertEquals( |
237
|
|
|
$GLOBALS['_was_called_wp_enqueue_script'], |
238
|
|
|
array( array( 'handle', Assets::get_file_url_for_environment( '/minpath.js', '/path.js' ), array(), '123', true ) ) |
239
|
|
|
); |
240
|
|
|
} |
241
|
|
|
} |
242
|
|
|
|
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: