Completed
Push — fix/full-url-woo-analytics ( f2ba6b )
by Jeremy
07:31
created

test-assets.php ➔ wp_enqueue_script()   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 5
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
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
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...
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
	 */
108
	public function test_get_file_url_for_environment_full_url() {
109
		$path     = 'https://jetpack.com';
110
		$file_url = Assets::get_file_url_for_environment( $path, $path );
111
112
		$this->assertEquals( $path, $file_url );
113
	}
114
115
	/**
116
	 * Tests ability for a filter to map specific URLs.
117
	 *
118
	 * @author kraftbj
119
	 * @see p58i-8nS-p2
120
	 */
121
	public function test_get_file_url_for_environment_with_filter() {
122
		Filters\expectApplied( 'jetpack_get_file_for_environment' )->once()->andReturn( 'special-test.js' );
123
124
		$file_url = Assets::get_file_url_for_environment( 'test.min.js', 'test.js' );
125
126
		$this->assertContains( 'special-test.js', $file_url );
127
	}
128
129
	/**
130
	 * Possible values for test_get_file_url_for_environment.
131
	 */
132
	public function get_file_url_for_environment_data_provider() {
133
		return array(
134
			'script-debug-true'  => array(
135
				'_inc/build/shortcodes/js/instagram.js',
136
				'modules/shortcodes/js/instagram.js',
137
				true,
138
				'non_min_path',
139
				'min_path',
140
			),
141
			'script-debug-false' => array(
142
				'_inc/build/shortcodes/js/instagram.js',
143
				'modules/shortcodes/js/instagram.js',
144
				false,
145
				'min_path',
146
				'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::assertTrue( 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( '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
		$this->assertEquals(
166
			$GLOBALS['_was_called_wp_enqueue_script'],
167
			array( array( 'handle', Assets::get_file_url_for_environment( '/minpath.js', '/path.js' ), array(), '123', true ) )
168
		);
169
	}
170
}
171