|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Automattic\Jetpack; |
|
4
|
|
|
|
|
5
|
|
|
use PHPUnit\Framework\TestCase; |
|
6
|
|
|
use Automattic\Jetpack\Constants as Jetpack_Constants; |
|
7
|
|
|
use Brain\Monkey; |
|
8
|
|
|
use Brain\Monkey\Filters; |
|
9
|
|
|
|
|
10
|
|
|
function plugins_url( $path, $plugin_path ) { |
|
11
|
|
|
return $plugin_path . $path; |
|
12
|
|
|
} |
|
13
|
|
|
|
|
14
|
|
|
function wp_enqueue_script( $handle, $src = '', $deps = array(), $ver = false, $in_footer = false ) { |
|
15
|
|
|
$GLOBALS['_was_called_wp_enqueue_script'][] = array( $handle, $src, $deps, $ver, $in_footer ); |
|
16
|
|
|
} |
|
17
|
|
|
|
|
18
|
|
|
class AssetsTest extends TestCase { |
|
19
|
|
|
|
|
20
|
|
|
public function setUp() { |
|
21
|
|
|
Monkey\setUp(); |
|
22
|
|
|
$plugin_file = dirname( dirname( dirname( dirname( __DIR__ ) ) ) ) . '/jetpack.php'; |
|
23
|
|
|
Jetpack_Constants::set_constant( 'JETPACK__PLUGIN_FILE', $plugin_file ); |
|
24
|
|
|
|
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Run after every test. |
|
29
|
|
|
*/ |
|
30
|
|
|
public function tearDown() { |
|
31
|
|
|
Monkey\tearDown(); |
|
32
|
|
|
$GLOBALS['_was_called_wp_enqueue_script'] = array(); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @author ebinnion goldsounds |
|
37
|
|
|
* @dataProvider get_file_url_for_environment_data_provider |
|
38
|
|
|
*/ |
|
39
|
|
|
function test_get_file_url_for_environment( $min_path, $non_min_path, $is_script_debug, $expected, $not_expected ) { |
|
40
|
|
|
Constants::set_constant( 'SCRIPT_DEBUG', $is_script_debug ); |
|
41
|
|
|
$file_url = Assets::get_file_url_for_environment( $min_path, $non_min_path ); |
|
42
|
|
|
|
|
43
|
|
|
// note the double-$$ here, $(non_)min_path is referenced by var name |
|
44
|
|
|
$this->assertContains( $$expected, $file_url ); |
|
45
|
|
|
$this->assertNotContains( $$not_expected, $file_url ); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Tests ability for a filter to map specific URLs. |
|
50
|
|
|
* |
|
51
|
|
|
* @author kraftbj |
|
52
|
|
|
* @see p58i-8nS-p2 |
|
53
|
|
|
*/ |
|
54
|
|
|
public function test_get_file_url_for_environment_with_filter() { |
|
55
|
|
|
Filters\expectApplied( 'jetpack_get_file_for_environment' )->once()->andReturn( 'special-test.js' ); |
|
56
|
|
|
|
|
57
|
|
|
$file_url = Assets::get_file_url_for_environment( 'test.min.js', 'test.js' ); |
|
58
|
|
|
|
|
59
|
|
|
$this->assertContains( 'special-test.js', $file_url ); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
function get_file_url_for_environment_data_provider() { |
|
63
|
|
|
return array( |
|
64
|
|
|
'script-debug-true' => array( |
|
65
|
|
|
'_inc/build/shortcodes/js/instagram.js', |
|
66
|
|
|
'modules/shortcodes/js/instagram.js', |
|
67
|
|
|
true, |
|
68
|
|
|
'non_min_path', |
|
69
|
|
|
'min_path', |
|
70
|
|
|
), |
|
71
|
|
|
'script-debug-false' => array( |
|
72
|
|
|
'_inc/build/shortcodes/js/instagram.js', |
|
73
|
|
|
'modules/shortcodes/js/instagram.js', |
|
74
|
|
|
false, |
|
75
|
|
|
'min_path', |
|
76
|
|
|
'non_min_path', |
|
77
|
|
|
), |
|
78
|
|
|
); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Test that enqueue_async_script calls adds the script_loader_tag filter |
|
83
|
|
|
*/ |
|
84
|
|
|
public function test_enqueue_async_script_adds_script_loader_tag_filter() { |
|
85
|
|
|
Assets::enqueue_async_script( 'handle', 'minpath.js', 'path.js', array(), '123', true ); |
|
|
|
|
|
|
86
|
|
|
$asset_instance = Assets::instance(); |
|
87
|
|
|
self::assertTrue( has_filter( 'script_loader_tag', array( $asset_instance, 'script_add_async' ) ) ); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Test that enqueue_async_script calls wp_enqueue_script |
|
92
|
|
|
*/ |
|
93
|
|
|
public function test_enqueue_async_script_calls_wp_enqueue_script() { |
|
94
|
|
|
Assets::enqueue_async_script( 'handle', '/minpath.js', '/path.js', array(), '123', true ); |
|
|
|
|
|
|
95
|
|
|
$this->assertEquals( |
|
96
|
|
|
$GLOBALS['_was_called_wp_enqueue_script'], |
|
97
|
|
|
array( array( 'handle', Assets::get_file_url_for_environment( '/minpath.js', '/path.js' ), array(), '123', true ) ) |
|
98
|
|
|
); |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|
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: