Completed
Push — update/jitm-pre-docs ( 5ed376 )
by Jeremy
185:12 queued 176:49
created

test_pre_connection_jitms_disabled()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

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