Completed
Push — update/minimum-wp-version-55 ( e2aaab...409fc3 )
by Jeremy
36:49 queued 28:04
created

Test_Jetpack_JITM::get_enqueued_script()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php  // phpcs:disable
2
3
namespace Automattic\Jetpack;
4
5
use Automattic\Jetpack\JITMS\JITM;
6
use Automattic\Jetpack\JITMS\Pre_Connection_JITM;
7
use phpmock\functions\FunctionProvider;
8
use phpmock\Mock;
9
use phpmock\MockBuilder;
10
use PHPUnit\Framework\TestCase;
11
12
class Test_Jetpack_JITM extends TestCase {
13
	public function setUp() {
14
		$this->mock_add_get_current_screen();
15
		$this->mock_add_action();
16
		$this->mock_do_action();
17
		$this->mock_wp_enqueue_script();
18
		//$this->mock_wp_enqueue_script();
19
		$this->mock_filters_deprecated();
20
21
		// input/output of these functions doesn't matter right now, they just need to exist
22
		$this->mock_empty_function( 'wp_register_style' );
23
		$this->mock_empty_function( 'plugins_url' );
24
		$this->mock_empty_function( 'wp_style_add_data' );
25
		$this->mock_empty_function( 'wp_enqueue_style' );
26
		$this->mock_empty_function( 'wp_localize_script' );
27
		$this->mock_empty_function( 'esc_url_raw' );
28
		$this->mock_empty_function( 'rest_url' );
29
		$this->mock_empty_function( 'esc_html__' );
30
		$this->mock_empty_function( 'wp_create_nonce' );
31
32
		$this->mock_production_environment();
33
	}
34
35
	public function tearDown() {
36
		Mock::disableAll();
37
		\Mockery::close();
38
		$this->clear_added_actions();
39
		$this->clear_enqueued_scripts();
40
	}
41
42 View Code Duplication
	public function test_jitm_disabled_by_filter() {
43
		$this->mock_filters( array(
44
			array( 'jetpack_just_in_time_msgs', false, false ),
45
		), "Automattic\Jetpack\JITMS" );
46
47
		// Used for Status->is_offline_mode().
48
		$this->mock_filters( array(
49
			array( 'jetpack_just_in_time_msgs', false, false ),
50
		), "Automattic\Jetpack" );
51
		$this->mock_site_url();
52
53
		$jitm = new JITM();
54
		$this->assertFalse( $jitm->register() );
55
56
		$this->clear_mock_filters();
57
	}
58
59 View Code Duplication
	public function test_jitm_enabled_by_default() {
60
		$this->mock_filters( array(
61
			array( 'jetpack_just_in_time_msgs', false, true ),
62
		), "Automattic\Jetpack\JITMS" );
63
64
		// Used for Status->is_offline_mode().
65
		$this->mock_filters( array(
66
			array( 'jetpack_just_in_time_msgs', false, true ),
67
		), "Automattic\Jetpack" );
68
		$this->mock_site_url();
69
70
		$jitm = new JITM();
71
		$this->assertTrue( $jitm->register() );
72
73
		$this->clear_mock_filters();
74
	}
75
76
	/**
77
	 * Pre-connection JITMs are disabled by default,
78
	 * unless a filter is used.
79
	 */
80
	public function test_pre_connection_jitms_disabled() {
81
		$this->mock_filters( array(
82
			array( 'jetpack_pre_connection_prompt_helpers', false, false ),
83
		), "Automattic\Jetpack\JITMS" );
84
85
		$jitm = new Pre_Connection_JITM();
86
		$this->assertEmpty( $jitm->get_messages( '/wp:edit-post:admin_notices/', '', false ) );
87
88
		$this->clear_mock_filters();
89
	}
90
91
	/**
92
	 * This is an example of a test which uses Mockery to tests a class static method.
93
	 *
94
	 * It requires the runInSeparateProcess tag so that the class isn't already autoloaded.
95
	 *
96
	 * @runInSeparateProcess
97
	 */
98
	public function test_prepare_jitms_enqueues_assets() {
99
		$mockAssets = \Mockery::mock('alias:Automattic\Jetpack\Assets');
100
101
		// mock the static method and return a dummy value
102
		$mockAssets
103
			->shouldReceive('get_file_url_for_environment')
104
			->andReturn('the_file_url');
105
106
		$jitm = new JITM();
107
		$screen = (object) array( 'id' => 'jetpack_foo' ); // fake screen object
108
		$jitm->prepare_jitms( $screen );
109
110
		// this should enqueue a jetpack-jitm-new script
111
		do_action( 'admin_enqueue_scripts' );
112
113
		// assert our script was enqueued with the right value
114
		$script = $this->get_enqueued_script( 'jetpack-jitm-new' );
115
116
		$this->assertEquals( 'the_file_url', $script['src'] );
117
	}
118
119
	/*
120
	public function test_prepare_jitms_does_not_show_on_some_screens() {
121
		$jitm = new JITM();
122
		$screen = new \stdClass();
123
		$screen->id = 'jetpack_page_stats';
124
		$jitm->prepare_jitms( $screen );
125
	}
126
	*/
127
128 View Code Duplication
	protected function mock_site_url() {
129
		$builder = new MockBuilder();
130
		$builder->setNamespace( "Automattic\Jetpack" )
131
			->setName( 'site_url' )
132
			->setFunction( function() {
133
				return "unit-test";
134
			} );
135
		$builder->build()->enable();
136
	}
137
138
	protected function mock_filters( $filters, $namespace ) {
139
		$this->mocked_filters = $filters;
140
		$builder = new MockBuilder();
141
		$builder->setNamespace( $namespace )
142
			->setName( 'apply_filters' )
143
			->setFunction(
144
				function( ...$current_args ) {
145
					foreach ( $this->mocked_filters as $filter ) {
146
						if ( array_slice( $filter, 0, -1 ) === $current_args ) {
147
							return array_pop( $filter );
148
						}
149
					}
150
				}
151
			);
152
		$this->apply_filters_mock = $builder->build();
153
		$this->apply_filters_mock->enable();
154
	}
155
156
	protected function mock_filters_deprecated() {
157
		$builder = new MockBuilder();
158
		$builder->setNamespace( 'Automattic\Jetpack' )
159
		        ->setName( 'apply_filters_deprecated' )
160
		        ->setFunction(
161
			        function( ...$args ) {
162
				        return $args[1][0]; // Return the 2nd argument's first array item.
163
			        }
164
		        );
165
		$this->apply_filters_deprecated_mock = $builder->build();
166
		$this->apply_filters_deprecated_mock->enable();
167
	}
168
169
	protected function clear_mock_filters() {
170
		$this->apply_filters_mock->disable();
171
		unset( $this->mocked_filters );
172
	}
173
174 View Code Duplication
	protected function mock_add_get_current_screen() {
175
		$builder = new MockBuilder();
176
		$builder->setNamespace( "Automattic\Jetpack\JITMS" )
177
			->setName( 'get_current_screen' )
178
			->setFunction( function() {
179
				return new \stdClass;
180
			} );
181
		$builder->build()->enable();
182
	}
183
184
	protected function mock_add_action() {
185
		$builder = new MockBuilder();
186
		$builder->setNamespace( "Automattic\Jetpack\JITMS" )
187
			->setName( 'add_action' )
188
			->setFunction( function( $name, $callable ) {
189
				global $actions;
190
191
				if ( is_null( $actions ) ) {
192
					$actions = array();
193
				}
194
195
				// don't worry about precedence for now
196
				if ( ! isset( $actions[$name] ) ) {
197
					$actions[$name] = array();
198
				}
199
200
				$actions[$name][] = $callable;
201
			} );
202
		$builder->build()->enable();
203
	}
204
205
	protected function mock_do_action() {
206
		$builder = new MockBuilder();
207
		$builder->setNamespace( __NAMESPACE__ )
208
			->setName( 'do_action' )
209
			->setFunction( function( ...$args ) {
210
				global $actions;
211
				$name = array_shift( $args );
212
213
				if ( is_null( $actions ) ) {
214
					$actions = array();
215
				}
216
217
				// don't worry about precedence for now
218
				if ( ! isset( $actions[$name] ) ) {
219
					$actions[$name] = array();
220
				}
221
222
				foreach( $actions[$name] as $callable ) {
223
					call_user_func_array( $callable, $args );
224
				}
225
			} );
226
		$builder->build()->enable();
227
	}
228
229
	protected function mock_wp_enqueue_script() {
230
		$builder = new MockBuilder();
231
		$builder->setNamespace( "Automattic\Jetpack\JITMS" )
232
			->setName( 'wp_enqueue_script' )
233
			->setFunction( function( $handle, $src = '', $deps = array(), $ver = false, $in_footer = false ) {
234
				global $wp_scripts;
235
236
				if ( is_null( $wp_scripts ) ) {
237
					$wp_scripts = array();
238
				}
239
240
				$wp_scripts[$handle] = compact( 'src', 'deps', 'ver', 'in_footer' );
241
			} );
242
		$builder->build()->enable();
243
	}
244
245
	protected function get_enqueued_script( $handle ) {
246
		global $wp_scripts;
247
		return isset( $wp_scripts[$handle] ) ? $wp_scripts[$handle] : null;
248
	}
249
250
	protected function clear_added_actions() {
251
		global $actions;
252
		$actions = array();
253
	}
254
255
	protected function clear_enqueued_scripts() {
256
		global $wp_scripts;
257
		$wp_scripts = array();
258
	}
259
260
	protected function mock_empty_function( $name ) {
261
		$builder = new MockBuilder();
262
		$builder->setNamespace( "Automattic\Jetpack\JITMS" )
263
			->setName( $name )
264
			->setFunction( function() use ( $name ) {
265
				// echo "Called $name with " . print_r( func_get_args(),1 ) . "\n";
266
			} );
267
		$builder->build()->enable();
268
	}
269
270
	protected function mock_production_environment() {
271
		$builder = new MockBuilder();
272
		$builder->setNamespace( "Automattic\Jetpack" )
273
			->setName( 'wp_get_environment_type' )
274
			->setFunction( function() {
275
				return '';
276
			} );
277
		$builder->build()->enable();
278
	}
279
}
280