Completed
Push — update/nested-upgrade-nudges ( e1e842...baea19 )
by
unknown
132:36 queued 123:22
created

Test_Pre_Connection_JITM::tear_down()   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 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php  // phpcs:disable
2
3
namespace Automattic\Jetpack;
4
5
use Automattic\Jetpack\JITMS\Pre_Connection_JITM;
6
use Brain\Monkey;
7
use Brain\Monkey\Filters;
8
use Brain\Monkey\Functions;
9
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
10
use PHPUnit\Framework\TestCase;
11
12
class Test_Pre_Connection_JITM extends TestCase {
13
	use MockeryPHPUnitIntegration;
14
15
	/**
16
	 * An array containing a test pre-connection JITM.
17
	 *
18
	 * @var array
19
	 */
20
	private $test_jitms;
21
22
	/**
23
	 * The Pre_Connection_JITM instance.
24
	 *
25
	 * @var Pre_Connection_JITM
26
	 */
27
	private $jitm_instance;
28
29
	/**
30
	 * Set up.
31
	 *
32
	 * @before
33
	 */
34
	public function set_up() {
35
		Monkey\setUp();
36
37
		Functions\when( 'get_current_screen' )->justReturn( new \stdClass() );
38
		Functions\when( 'site_url' )->justReturn( 'unit-test' );
39
		Functions\when( 'wp_get_environment_type' )->justReturn( '' );
40
		Functions\when( 'get_option' )->justReturn( '' );
41
		Functions\when( '__' )->returnArg();
42
43
		$this->test_jitms = array(
44
			array(
45
				'id'             => 'test-jitm',
46
				'message_path'   => '/wp:plugins:admin_notices/',
47
				'message'        => __( 'A test message.', 'jetpack' ),
48
				'description'    => __( 'A test description.', 'jetpack' ),
49
				'button_link'    => 'a/test/url',
50
				'button_caption' => __( 'Test button text', 'jetpack' ),
51
			),
52
		);
53
54
		$this->jitm_instance = new Pre_Connection_JITM();
55
	}
56
57
	/**
58
	 * Tear down.
59
	 *
60
	 * @after
61
	 */
62
	public function tear_down() {
63
		Monkey\tearDown();
64
	}
65
66
	/**
67
	 * The pre-connection JITMs are disabled when the current user does not have the 'install_plugins' capability.
68
	 */
69 View Code Duplication
	public function test_get_messages_user_cannot_install_plugins() {
70
		Functions\expect( 'current_user_can' )
71
			->once()
72
			->andReturn( false );
73
74
		Filters\expectApplied( 'jetpack_pre_connection_jitms' )
75
			->atMost()
76
			->once()
77
			->with( array() )
78
			->andReturn( $this->test_jitms );
79
80
		$this->assertEmpty( $this->jitm_instance->get_messages( '/wp:plugins:admin_notices/', '', false ) );
81
	}
82
83
	/**
84
	 * The pre-connection JITMs are empty by default. The default value of the 'jetpack_pre_connection_jitms' filter is
85
	 * an empty array.
86
	 */
87 View Code Duplication
	public function test_get_messages_jitms_filter_default() {
88
		Functions\expect( 'current_user_can' )
89
			->atMost()
90
			->once()
91
			->andReturn( true );
92
93
		Filters\expectApplied( 'jetpack_pre_connection_jitms' )
94
			->once()
95
			->with( array() );
96
97
		$this->assertEmpty( $this->jitm_instance->get_messages( '/wp:plugins:admin_notices/', '', false ) );
98
	}
99
100
	/**
101
	 * The Pre_Connection_JITM::get_messages method returns an empty array when the the 'jetpack_pre_connection_jitms' filter
102
	 * returns anything other than an array.
103
	 */
104 View Code Duplication
	public function test_get_messages_filter_returns_string() {
105
		Functions\expect( 'current_user_can' )
106
			->atMost()
107
			->once()
108
			->andReturn( true );
109
110
		Filters\expectApplied( 'jetpack_pre_connection_jitms' )
111
			->once()
112
			->with( array() )
113
			->andReturn( 'a string intead of an array' );
114
115
		$this->assertEmpty( $this->jitm_instance->get_messages( '/wp:plugins:admin_notices/', '', false ) );
116
	}
117
118
	/**
119
	 * The pre-connection JITMs are added using the `jetpack_pre_connection_jitms` filter.
120
	 */
121 View Code Duplication
	public function test_get_messages_return_message() {
122
		$this->set_user_cap_conditions();
123
124
		Filters\expectApplied( 'jetpack_pre_connection_jitms' )
125
			->once()
126
			->with( array() )
127
			->andReturn( $this->test_jitms );
128
129
		$messages = $this->jitm_instance->get_messages( '/wp:plugins:admin_notices/', '', false );
130
		$this->assertSame( $this->test_jitms[0]['id'], $messages[0]->id );
131
	}
132
133
	/**
134
	 * A pre-connection JITM is only displayed if its message_path value matches the message path
135
	 * passed to Pre_Connection_JITM::get_messages. In this test, the test JITM's path does not match the
136
	 * tested path.
137
	 */
138
	public function test_get_messages_unmatched_message_path() {
139
		$this->set_user_cap_conditions();
140
141
		Filters\expectApplied( 'jetpack_pre_connection_jitms' )
142
			->once()
143
			->with( array() )
144
			->andReturn( $this->test_jitms );
145
146
		$this->assertEmpty( $this->jitm_instance->get_messages( '/wp:edit-comments:admin_notices/', '', false ) );
147
	}
148
149
	/**
150
	 * The pre-connection JITM is not displayed if the message array is missing a required key. In this test, the JITM is
151
	 * missing the message_path key.
152
	 */
153 View Code Duplication
	public function test_get_messages_missing_key() {
154
		$this->set_user_cap_conditions();
155
156
		unset( $this->test_jitms[0]['message_path'] );
157
158
		Filters\expectApplied( 'jetpack_pre_connection_jitms' )
159
			->once()
160
			->with( array() )
161
			->andReturn( $this->test_jitms );
162
163
		$this->assertEmpty( $this->jitm_instance->get_messages( '/wp:plugins:admin_notices/', '', false ) );
164
	}
165
166
	/**
167
	 * A pre-connection JITM is displayed if it has unexpected keys.
168
	 */
169
	public function test_get_messages_extra_key() {
170
		$this->set_user_cap_conditions();
171
172
		$this->test_jitms[0]['extra_key'] = 'extra jitm key';
173
174
		Filters\expectApplied( 'jetpack_pre_connection_jitms' )
175
			->once()
176
			->with( array() )
177
			->andReturn( $this->test_jitms );
178
179
		$messages = $this->jitm_instance->get_messages( '/wp:plugins:admin_notices/', '', false );
180
		$this->assertSame( $this->test_jitms[0]['id'], $messages[0]->id );
181
	}
182
183
	/**
184
	 * Test the pre-connection JITM icon values.
185
	 *
186
	 * @param string|null $message_icon_value The message's icon value or null if it's doesn't have one.
187
	 * @param string      $expected_icon      The expected icon value input for the generate_icon method.
188
	 *
189
	 * @dataProvider data_provider_test_message_icon_values
190
	 */
191
	public function test_message_icon_values( $message_icon_value, $expected_icon ) {
192
		$this->set_user_cap_conditions();
193
194
		if ( null !== $message_icon_value ) {
195
			$this->test_jitms[0]['icon'] = $message_icon_value;
196
		}
197
198
		Filters\expectApplied( 'jetpack_pre_connection_jitms' )
199
			->once()
200
			->with( array() )
201
			->andReturn( $this->test_jitms );
202
203
		$jitm = \Mockery::mock( Pre_Connection_JITM::class )->makePartial();
204
		$jitm
205
			->expects( 'generate_icon' )
206
			->once()
207
			->with( $expected_icon, false );
208
209
		$jitm->get_messages( '/wp:plugins:admin_notices/', '', false );
210
	}
211
212
	/**
213
	 * Data provider for the test_message_icon_values method.
214
	 *
215
	 * @return array The test data. The structure of each test data element is:
216
	 *     { test data name } =>
217
	 *         array(
218
	 *             { the message's icon value, null if it doesn't have one },
219
	 *             { the expected icon value input for the generate_icon method },
220
	 *         )
221
	 */
222
	public function data_provider_test_message_icon_values() {
223
		return array(
224
			'default icon' => array(
225
				null,
226
				'jetpack',
227
			),
228
			'supported icon' => array(
229
				'woocommerce',
230
				'woocommerce',
231
			),
232
			'empty string' => array(
233
				'',
234
				'',
235
			),
236
			'unsupported icon' => array(
237
				'not_supported',
238
				'jetpack',
239
			),
240
		);
241
	}
242
243
	private function set_user_cap_conditions() {
244
		Functions\expect( 'current_user_can' )
245
			->once()
246
			->andReturn( true );
247
	}
248
}
249