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 by default by the `jetpack_pre_connection_prompt_helpers` filter's default value. |
68
|
|
|
*/ |
69
|
|
View Code Duplication |
public function test_get_messages_prompt_helpers_default() { |
70
|
|
|
Filters\expectApplied( 'jetpack_pre_connection_prompt_helpers' ) |
71
|
|
|
->once() |
72
|
|
|
->with( false ); |
73
|
|
|
|
74
|
|
|
Functions\expect( 'current_user_can' ) |
75
|
|
|
->atMost() |
76
|
|
|
->once() |
77
|
|
|
->andReturn( true ); |
78
|
|
|
|
79
|
|
|
Filters\expectApplied( 'jetpack_pre_connection_jitms' ) |
80
|
|
|
->atMost() |
81
|
|
|
->once() |
82
|
|
|
->with( array() ) |
83
|
|
|
->andReturn( $this->test_jitms ); |
84
|
|
|
|
85
|
|
|
$this->assertEmpty( $this->jitm_instance->get_messages( '/wp:plugins:admin_notices/', '', false ) ); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* The pre-connection JITMs are disabled when the current user does not have the 'install_plugins' capability. |
90
|
|
|
*/ |
91
|
|
View Code Duplication |
public function test_get_messages_user_cannot_install_plugins() { |
92
|
|
|
Filters\expectApplied( 'jetpack_pre_connection_prompt_helpers' ) |
93
|
|
|
->atMost() |
94
|
|
|
->once() |
95
|
|
|
->with( false ) |
96
|
|
|
->andReturns( true ); |
97
|
|
|
|
98
|
|
|
Functions\expect( 'current_user_can' ) |
99
|
|
|
->once() |
100
|
|
|
->andReturn( false ); |
101
|
|
|
|
102
|
|
|
Filters\expectApplied( 'jetpack_pre_connection_jitms' ) |
103
|
|
|
->atMost() |
104
|
|
|
->once() |
105
|
|
|
->with( array() ) |
106
|
|
|
->andReturn( $this->test_jitms ); |
107
|
|
|
|
108
|
|
|
$this->assertEmpty( $this->jitm_instance->get_messages( '/wp:plugins:admin_notices/', '', false ) ); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* The pre-connection JITMs are empty by default. The default value of the 'jetpack_pre_connection_jitms' filter is |
113
|
|
|
* an empty array. |
114
|
|
|
*/ |
115
|
|
View Code Duplication |
public function test_get_messages_jitms_filter_default() { |
116
|
|
|
Filters\expectApplied( 'jetpack_pre_connection_prompt_helpers' ) |
117
|
|
|
->atMost() |
118
|
|
|
->once() |
119
|
|
|
->with( false ) |
120
|
|
|
->andReturns( true ); |
121
|
|
|
|
122
|
|
|
Functions\expect( 'current_user_can' ) |
123
|
|
|
->atMost() |
124
|
|
|
->once() |
125
|
|
|
->andReturn( true ); |
126
|
|
|
|
127
|
|
|
Filters\expectApplied( 'jetpack_pre_connection_jitms' ) |
128
|
|
|
->once() |
129
|
|
|
->with( array() ); |
130
|
|
|
|
131
|
|
|
$this->assertEmpty( $this->jitm_instance->get_messages( '/wp:plugins:admin_notices/', '', false ) ); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* The Pre_Connection_JITM::get_messages method returns an empty array when the the 'jetpack_pre_connection_jitms' filter |
136
|
|
|
* returns anything other than an array. |
137
|
|
|
*/ |
138
|
|
View Code Duplication |
public function test_get_messages_filter_returns_string() { |
139
|
|
|
Filters\expectApplied( 'jetpack_pre_connection_prompt_helpers' ) |
140
|
|
|
->atMost() |
141
|
|
|
->once() |
142
|
|
|
->with( false ) |
143
|
|
|
->andReturns( true ); |
144
|
|
|
|
145
|
|
|
Functions\expect( 'current_user_can' ) |
146
|
|
|
->atMost() |
147
|
|
|
->once() |
148
|
|
|
->andReturn( true ); |
149
|
|
|
|
150
|
|
|
Filters\expectApplied( 'jetpack_pre_connection_jitms' ) |
151
|
|
|
->once() |
152
|
|
|
->with( array() ) |
153
|
|
|
->andReturn( 'a string intead of an array' ); |
154
|
|
|
|
155
|
|
|
$this->assertEmpty( $this->jitm_instance->get_messages( '/wp:plugins:admin_notices/', '', false ) ); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* The pre-connection JITMs are added using the `jetpack_pre_connection_jitms` filter. |
160
|
|
|
*/ |
161
|
|
View Code Duplication |
public function test_get_messages_return_message() { |
162
|
|
|
$this->set_prompt_helpers_and_user_cap_conditions(); |
163
|
|
|
|
164
|
|
|
Filters\expectApplied( 'jetpack_pre_connection_jitms' ) |
165
|
|
|
->once() |
166
|
|
|
->with( array() ) |
167
|
|
|
->andReturn( $this->test_jitms ); |
168
|
|
|
|
169
|
|
|
$messages = $this->jitm_instance->get_messages( '/wp:plugins:admin_notices/', '', false ); |
170
|
|
|
$this->assertSame( $this->test_jitms[0]['id'], $messages[0]->id ); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* A pre-connection JITM is only displayed if its message_path value matches the message path |
175
|
|
|
* passed to Pre_Connection_JITM::get_messages. In this test, the test JITM's path does not match the |
176
|
|
|
* tested path. |
177
|
|
|
*/ |
178
|
|
|
public function test_get_messages_unmatched_message_path() { |
179
|
|
|
$this->set_prompt_helpers_and_user_cap_conditions(); |
180
|
|
|
|
181
|
|
|
Filters\expectApplied( 'jetpack_pre_connection_jitms' ) |
182
|
|
|
->once() |
183
|
|
|
->with( array() ) |
184
|
|
|
->andReturn( $this->test_jitms ); |
185
|
|
|
|
186
|
|
|
$this->assertEmpty( $this->jitm_instance->get_messages( '/wp:edit-comments:admin_notices/', '', false ) ); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* The pre-connection JITM is not displayed if the message array is missing a required key. In this test, the JITM is |
191
|
|
|
* missing the message_path key. |
192
|
|
|
*/ |
193
|
|
View Code Duplication |
public function test_get_messages_missing_key() { |
194
|
|
|
$this->set_prompt_helpers_and_user_cap_conditions(); |
195
|
|
|
|
196
|
|
|
unset( $this->test_jitms[0]['message_path'] ); |
197
|
|
|
|
198
|
|
|
Filters\expectApplied( 'jetpack_pre_connection_jitms' ) |
199
|
|
|
->once() |
200
|
|
|
->with( array() ) |
201
|
|
|
->andReturn( $this->test_jitms ); |
202
|
|
|
|
203
|
|
|
$this->assertEmpty( $this->jitm_instance->get_messages( '/wp:plugins:admin_notices/', '', false ) ); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* A pre-connection JITM is displayed if it has unexpected keys. |
208
|
|
|
*/ |
209
|
|
|
public function test_get_messages_extra_key() { |
210
|
|
|
$this->set_prompt_helpers_and_user_cap_conditions(); |
211
|
|
|
|
212
|
|
|
$this->test_jitms[0]['extra_key'] = 'extra jitm key'; |
213
|
|
|
|
214
|
|
|
Filters\expectApplied( 'jetpack_pre_connection_jitms' ) |
215
|
|
|
->once() |
216
|
|
|
->with( array() ) |
217
|
|
|
->andReturn( $this->test_jitms ); |
218
|
|
|
|
219
|
|
|
$messages = $this->jitm_instance->get_messages( '/wp:plugins:admin_notices/', '', false ); |
220
|
|
|
$this->assertSame( $this->test_jitms[0]['id'], $messages[0]->id ); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
private function set_prompt_helpers_and_user_cap_conditions() { |
224
|
|
|
Filters\expectApplied( 'jetpack_pre_connection_prompt_helpers' ) |
225
|
|
|
->once() |
226
|
|
|
->with( false ) |
227
|
|
|
->andReturns( true ); |
228
|
|
|
|
229
|
|
|
Functions\expect( 'current_user_can' ) |
230
|
|
|
->once() |
231
|
|
|
->andReturn( true ); |
232
|
|
|
} |
233
|
|
|
} |
234
|
|
|
|