|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Tests the TOS package. |
|
4
|
|
|
* |
|
5
|
|
|
* @package automattic/jetpack-licensing |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace Automattic\Jetpack; |
|
9
|
|
|
|
|
10
|
|
|
use Automattic\Jetpack\Connection\Manager as Connection_Manager; |
|
11
|
|
|
use Jetpack_IXR_ClientMulticall; |
|
12
|
|
|
use stdClass; |
|
13
|
|
|
use WorDBless\BaseTestCase; |
|
14
|
|
|
use WP_Error; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Class Test_Licensing |
|
18
|
|
|
* |
|
19
|
|
|
* @package Automattic\Jetpack |
|
20
|
|
|
*/ |
|
21
|
|
|
class Test_Licensing extends BaseTestCase { |
|
22
|
|
|
/** |
|
23
|
|
|
* Test last_error(). |
|
24
|
|
|
*/ |
|
25
|
|
|
public function test_last_error() { |
|
26
|
|
|
$licensing = new Licensing(); |
|
27
|
|
|
|
|
28
|
|
|
delete_option( 'jetpack_options' ); |
|
29
|
|
|
$this->assertSame( '', $licensing->last_error() ); |
|
30
|
|
|
|
|
31
|
|
|
update_option( 'jetpack_options', array() ); |
|
32
|
|
|
$this->assertSame( '', $licensing->last_error() ); |
|
33
|
|
|
|
|
34
|
|
|
update_option( 'jetpack_options', array( 'licensing_error' => '' ) ); |
|
35
|
|
|
$this->assertSame( '', $licensing->last_error() ); |
|
36
|
|
|
|
|
37
|
|
|
update_option( 'jetpack_options', array( 'licensing_error' => 'foo' ) ); |
|
38
|
|
|
$this->assertSame( 'foo', $licensing->last_error() ); |
|
39
|
|
|
|
|
40
|
|
|
delete_option( 'jetpack_options' ); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Test log_error(). |
|
45
|
|
|
*/ |
|
46
|
|
|
public function test_log_error() { |
|
47
|
|
|
$licensing = new Licensing(); |
|
48
|
|
|
|
|
49
|
|
|
delete_option( 'jetpack_options' ); |
|
50
|
|
|
$this->assertSame( '', $licensing->last_error() ); |
|
51
|
|
|
|
|
52
|
|
|
$licensing->log_error( '' ); |
|
53
|
|
|
$this->assertSame( '', $licensing->last_error() ); |
|
54
|
|
|
|
|
55
|
|
|
$licensing->log_error( 'foo' ); |
|
56
|
|
|
$this->assertSame( 'foo', $licensing->last_error() ); |
|
57
|
|
|
|
|
58
|
|
|
$licensing->log_error( str_repeat( 'a', 2048 ) ); |
|
59
|
|
|
$this->assertSame( str_repeat( 'a', 1024 ), $licensing->last_error() ); |
|
60
|
|
|
|
|
61
|
|
|
delete_option( 'jetpack_options' ); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Test stored_licenses(). |
|
66
|
|
|
*/ |
|
67
|
|
|
public function test_stored_licenses() { |
|
68
|
|
|
$licensing = new Licensing(); |
|
69
|
|
|
|
|
70
|
|
|
delete_option( Licensing::LICENSES_OPTION_NAME ); |
|
71
|
|
|
$this->assertSame( array(), $licensing->stored_licenses() ); |
|
72
|
|
|
|
|
73
|
|
|
update_option( Licensing::LICENSES_OPTION_NAME, new stdClass() ); |
|
74
|
|
|
$this->assertSame( array(), $licensing->stored_licenses() ); |
|
75
|
|
|
|
|
76
|
|
|
update_option( Licensing::LICENSES_OPTION_NAME, array() ); |
|
77
|
|
|
$this->assertSame( array(), $licensing->stored_licenses() ); |
|
78
|
|
|
|
|
79
|
|
|
update_option( Licensing::LICENSES_OPTION_NAME, array( null ) ); |
|
80
|
|
|
$this->assertSame( array(), $licensing->stored_licenses() ); |
|
81
|
|
|
|
|
82
|
|
|
update_option( Licensing::LICENSES_OPTION_NAME, array( new stdClass() ) ); |
|
83
|
|
|
$this->assertSame( array(), $licensing->stored_licenses() ); |
|
84
|
|
|
|
|
85
|
|
|
update_option( Licensing::LICENSES_OPTION_NAME, array( 1 ) ); |
|
86
|
|
|
$this->assertSame( array( '1' ), $licensing->stored_licenses() ); |
|
87
|
|
|
|
|
88
|
|
|
update_option( Licensing::LICENSES_OPTION_NAME, array( 'foo', 'bar' ) ); |
|
89
|
|
|
$this->assertSame( array( 'foo', 'bar' ), $licensing->stored_licenses() ); |
|
90
|
|
|
|
|
91
|
|
|
delete_option( Licensing::LICENSES_OPTION_NAME ); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Test append_license(). |
|
96
|
|
|
*/ |
|
97
|
|
|
public function test_append_license() { |
|
98
|
|
|
$licensing = new Licensing(); |
|
99
|
|
|
|
|
100
|
|
|
delete_option( Licensing::LICENSES_OPTION_NAME ); |
|
101
|
|
|
|
|
102
|
|
|
$did_update = $licensing->append_license( 'foo' ); |
|
103
|
|
|
$this->assertTrue( $did_update ); |
|
104
|
|
|
$this->assertSame( array( 'foo' ), $licensing->stored_licenses() ); |
|
105
|
|
|
|
|
106
|
|
|
update_option( Licensing::LICENSES_OPTION_NAME, array( 'foo', 'bar' ) ); |
|
107
|
|
|
$did_update = $licensing->append_license( 'baz' ); |
|
108
|
|
|
$this->assertTrue( $did_update ); |
|
109
|
|
|
$this->assertSame( array( 'foo', 'bar', 'baz' ), $licensing->stored_licenses() ); |
|
110
|
|
|
|
|
111
|
|
|
delete_option( Licensing::LICENSES_OPTION_NAME ); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Test attach_licenses() without an active Jetpack connection. |
|
116
|
|
|
*/ |
|
117
|
|
|
public function test_attach_licenses__without_connection() { |
|
118
|
|
|
$connection = $this->createMock( Connection_Manager::class ); |
|
119
|
|
|
|
|
120
|
|
|
$connection->method( 'is_active' )->willReturn( false ); |
|
121
|
|
|
|
|
122
|
|
|
$licensing = $this->createPartialMock( |
|
123
|
|
|
Licensing::class, |
|
124
|
|
|
array( 'connection' ) |
|
125
|
|
|
); |
|
126
|
|
|
|
|
127
|
|
|
$licensing->method( 'connection' )->willReturn( $connection ); |
|
128
|
|
|
|
|
129
|
|
|
$result = $licensing->attach_licenses( array() ); |
|
130
|
|
|
|
|
131
|
|
|
$this->assertInstanceOf( WP_Error::class, $result ); |
|
132
|
|
|
$this->assertSame( 'not_connected', $result->get_error_code() ); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Test attach_licenses() with an empty input. |
|
137
|
|
|
*/ |
|
138
|
|
|
public function test_attach_licenses__empty_input() { |
|
139
|
|
|
$connection = $this->createMock( Connection_Manager::class ); |
|
140
|
|
|
|
|
141
|
|
|
$connection->method( 'is_active' )->willReturn( true ); |
|
142
|
|
|
|
|
143
|
|
|
$licensing = $this->createPartialMock( |
|
144
|
|
|
Licensing::class, |
|
145
|
|
|
array( 'connection' ) |
|
146
|
|
|
); |
|
147
|
|
|
|
|
148
|
|
|
$licensing->method( 'connection' )->willReturn( $connection ); |
|
149
|
|
|
|
|
150
|
|
|
$this->assertSame( array(), $licensing->attach_licenses( array() ) ); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* Test attach_licenses() with request failure. |
|
155
|
|
|
*/ |
|
156
|
|
|
public function test_attach_licenses__request_failure() { |
|
157
|
|
|
$licenses = array( 'foo', 'bar' ); |
|
158
|
|
|
|
|
159
|
|
|
$connection = $this->createMock( Connection_Manager::class ); |
|
160
|
|
|
|
|
161
|
|
|
$connection->method( 'is_active' )->willReturn( true ); |
|
162
|
|
|
|
|
163
|
|
|
$licensing = $this->createPartialMock( |
|
164
|
|
|
Licensing::class, |
|
165
|
|
|
array( 'connection', 'attach_licenses_request' ) |
|
166
|
|
|
); |
|
167
|
|
|
|
|
168
|
|
|
$licensing->expects( $this->once() ) |
|
169
|
|
|
->method( 'connection' ) |
|
170
|
|
|
->willReturn( $connection ); |
|
171
|
|
|
|
|
172
|
|
|
$ixr_client = $this->createMock( Jetpack_IXR_ClientMulticall::class ); |
|
173
|
|
|
$ixr_client->method( 'isError' )->willReturn( true ); |
|
174
|
|
|
$ixr_client->method( 'getErrorCode' )->willReturn( 1 ); |
|
175
|
|
|
$ixr_client->method( 'getErrorMessage' )->willReturn( 'Expected error message' ); |
|
176
|
|
|
|
|
177
|
|
|
$licensing->expects( $this->once() ) |
|
178
|
|
|
->method( 'attach_licenses_request' ) |
|
179
|
|
|
->with( $licenses ) |
|
180
|
|
|
->willReturn( $ixr_client ); |
|
181
|
|
|
|
|
182
|
|
|
$result = $licensing->attach_licenses( $licenses ); |
|
183
|
|
|
|
|
184
|
|
|
$this->assertInstanceOf( WP_Error::class, $result ); |
|
185
|
|
|
$this->assertSame( array( 'request_failed', 1 ), $result->get_error_codes() ); |
|
186
|
|
|
$this->assertSame( 'Expected error message', $result->get_error_messages()[1] ); |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* Test attach_licenses() with multiple licenses. |
|
191
|
|
|
*/ |
|
192
|
|
|
public function test_attach_licenses__multiple_licenses() { |
|
193
|
|
|
$licenses = array( 'foo', 'bar' ); |
|
194
|
|
|
|
|
195
|
|
|
$connection = $this->createMock( Connection_Manager::class ); |
|
196
|
|
|
|
|
197
|
|
|
$connection->method( 'is_active' )->willReturn( true ); |
|
198
|
|
|
|
|
199
|
|
|
$licensing = $this->createPartialMock( |
|
200
|
|
|
Licensing::class, |
|
201
|
|
|
array( 'connection', 'attach_licenses_request' ) |
|
202
|
|
|
); |
|
203
|
|
|
|
|
204
|
|
|
$licensing->expects( $this->once() ) |
|
205
|
|
|
->method( 'connection' ) |
|
206
|
|
|
->willReturn( $connection ); |
|
207
|
|
|
|
|
208
|
|
|
$ixr_client = $this->createMock( Jetpack_IXR_ClientMulticall::class ); |
|
209
|
|
|
$ixr_client->method( 'isError' ) |
|
210
|
|
|
->willReturn( false ); |
|
211
|
|
|
$ixr_client->method( 'getResponse' ) |
|
212
|
|
|
->willReturn( |
|
213
|
|
|
array( |
|
214
|
|
|
array( |
|
215
|
|
|
'faultCode' => 1, |
|
216
|
|
|
'faultString' => 'Expected error message', |
|
217
|
|
|
), |
|
218
|
|
|
true, |
|
219
|
|
|
) |
|
220
|
|
|
); |
|
221
|
|
|
|
|
222
|
|
|
$licensing->expects( $this->once() ) |
|
223
|
|
|
->method( 'attach_licenses_request' ) |
|
224
|
|
|
->with( $licenses ) |
|
225
|
|
|
->willReturn( $ixr_client ); |
|
226
|
|
|
|
|
227
|
|
|
$result = $licensing->attach_licenses( $licenses ); |
|
228
|
|
|
|
|
229
|
|
|
$this->assertCount( 2, $result ); |
|
230
|
|
|
$this->assertInstanceOf( WP_Error::class, $result[0] ); |
|
231
|
|
|
$this->assertSame( 1, $result[0]->get_error_code() ); |
|
232
|
|
|
$this->assertSame( 'Expected error message', $result[0]->get_error_message() ); |
|
233
|
|
|
$this->assertTrue( $result[1] ); |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
/** |
|
237
|
|
|
* Test attach_stored_licenses(). |
|
238
|
|
|
*/ |
|
239
|
|
|
public function test_attach_stored_licenses() { |
|
240
|
|
|
$result0 = new WP_Error(); |
|
241
|
|
|
$result1 = true; |
|
242
|
|
|
$licenses = array( 'foo', 'bar' ); |
|
243
|
|
|
|
|
244
|
|
|
$licensing = $this->createPartialMock( |
|
245
|
|
|
Licensing::class, |
|
246
|
|
|
array( 'stored_licenses', 'attach_licenses' ) |
|
247
|
|
|
); |
|
248
|
|
|
|
|
249
|
|
|
$licensing->expects( $this->once() ) |
|
250
|
|
|
->method( 'stored_licenses' ) |
|
251
|
|
|
->willReturn( $licenses ); |
|
252
|
|
|
|
|
253
|
|
|
$licensing->expects( $this->once() ) |
|
254
|
|
|
->method( 'attach_licenses' ) |
|
255
|
|
|
->with( $licenses ) |
|
256
|
|
|
->willReturn( array( $result0, $result1 ) ); |
|
257
|
|
|
|
|
258
|
|
|
$this->assertSame( array( $result0, $result1 ), $licensing->attach_stored_licenses() ); |
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
|
|
/** |
|
262
|
|
|
* Test attach_stored_licenses() logs request failure. |
|
263
|
|
|
*/ |
|
264
|
|
View Code Duplication |
public function test_attach_stored_licenses__returns_error() { |
|
265
|
|
|
$licenses = array( 'foo', 'bar' ); |
|
266
|
|
|
|
|
267
|
|
|
$error = new WP_Error( 'foo' ); |
|
268
|
|
|
|
|
269
|
|
|
$licensing = $this->createPartialMock( |
|
270
|
|
|
Licensing::class, |
|
271
|
|
|
array( 'stored_licenses', 'attach_licenses', 'log_error' ) |
|
272
|
|
|
); |
|
273
|
|
|
|
|
274
|
|
|
$licensing->method( 'stored_licenses' ) |
|
275
|
|
|
->willReturn( $licenses ); |
|
276
|
|
|
|
|
277
|
|
|
$licensing->method( 'attach_licenses' ) |
|
278
|
|
|
->with( $licenses ) |
|
279
|
|
|
->willReturn( $error ); |
|
280
|
|
|
|
|
281
|
|
|
$licensing->expects( $this->never() )->method( 'log_error' ); |
|
282
|
|
|
|
|
283
|
|
|
$result = $licensing->attach_stored_licenses(); |
|
284
|
|
|
|
|
285
|
|
|
$this->assertSame( $error, $result ); |
|
286
|
|
|
} |
|
287
|
|
|
|
|
288
|
|
|
/** |
|
289
|
|
|
* Test attach_stored_licenses() logs request failure. |
|
290
|
|
|
*/ |
|
291
|
|
View Code Duplication |
public function test_attach_stored_licenses__logs_request_failure() { |
|
292
|
|
|
$licenses = array( 'foo', 'bar' ); |
|
293
|
|
|
|
|
294
|
|
|
$error = new WP_Error( 'request_failed' ); |
|
295
|
|
|
|
|
296
|
|
|
$licensing = $this->createPartialMock( |
|
297
|
|
|
Licensing::class, |
|
298
|
|
|
array( 'stored_licenses', 'attach_licenses', 'log_error' ) |
|
299
|
|
|
); |
|
300
|
|
|
|
|
301
|
|
|
$licensing->method( 'stored_licenses' ) |
|
302
|
|
|
->willReturn( $licenses ); |
|
303
|
|
|
|
|
304
|
|
|
$licensing->method( 'attach_licenses' ) |
|
305
|
|
|
->with( $licenses ) |
|
306
|
|
|
->willReturn( $error ); |
|
307
|
|
|
|
|
308
|
|
|
$licensing->expects( $this->once() ) |
|
309
|
|
|
->method( 'log_error' ) |
|
310
|
|
|
->with( 'Failed to attach your Jetpack license(s). Please try reconnecting Jetpack.' ); |
|
311
|
|
|
|
|
312
|
|
|
$result = $licensing->attach_stored_licenses(); |
|
313
|
|
|
|
|
314
|
|
|
$this->assertSame( $error, $result ); |
|
315
|
|
|
} |
|
316
|
|
|
|
|
317
|
|
|
/** |
|
318
|
|
|
* Test attach_stored_licenses() logs license attaching failures. |
|
319
|
|
|
*/ |
|
320
|
|
|
public function test_attach_stored_licenses__logs_license_attaching_failures() { |
|
321
|
|
|
$result0 = new WP_Error(); |
|
322
|
|
|
$result1 = true; |
|
323
|
|
|
$result2 = new WP_Error(); |
|
324
|
|
|
$licenses = array( 'foo', 'bar', 'baz' ); |
|
325
|
|
|
|
|
326
|
|
|
$licensing = $this->createPartialMock( |
|
327
|
|
|
Licensing::class, |
|
328
|
|
|
array( 'stored_licenses', 'attach_licenses', 'log_error' ) |
|
329
|
|
|
); |
|
330
|
|
|
|
|
331
|
|
|
$licensing->method( 'stored_licenses' ) |
|
332
|
|
|
->willReturn( $licenses ); |
|
333
|
|
|
|
|
334
|
|
|
$licensing->method( 'attach_licenses' ) |
|
335
|
|
|
->with( $licenses ) |
|
336
|
|
|
->willReturn( array( $result0, $result1, $result2 ) ); |
|
337
|
|
|
|
|
338
|
|
|
$licensing->expects( $this->once() ) |
|
339
|
|
|
->method( 'log_error' ) |
|
340
|
|
|
->with( 'The following Jetpack licenses are invalid, already in use, or revoked: foo, baz' ); |
|
341
|
|
|
|
|
342
|
|
|
$licensing->attach_stored_licenses(); |
|
343
|
|
|
} |
|
344
|
|
|
|
|
345
|
|
|
/** |
|
346
|
|
|
* Test attach_stored_licenses_on_connection() for the master user. |
|
347
|
|
|
*/ |
|
348
|
|
View Code Duplication |
public function test_attach_stored_licenses_on_connection__master_user() { |
|
349
|
|
|
$connection = $this->createMock( Connection_Manager::class ); |
|
350
|
|
|
|
|
351
|
|
|
$connection->method( 'is_connection_owner' )->willReturn( true ); |
|
352
|
|
|
|
|
353
|
|
|
$licensing = $this->createPartialMock( |
|
354
|
|
|
Licensing::class, |
|
355
|
|
|
array( 'connection', 'attach_stored_licenses' ) |
|
356
|
|
|
); |
|
357
|
|
|
|
|
358
|
|
|
$licensing->method( 'connection' )->willReturn( $connection ); |
|
359
|
|
|
|
|
360
|
|
|
$licensing->expects( $this->once() ) |
|
361
|
|
|
->method( 'attach_stored_licenses' ); |
|
362
|
|
|
|
|
363
|
|
|
$licensing->attach_stored_licenses_on_connection(); |
|
364
|
|
|
} |
|
365
|
|
|
|
|
366
|
|
|
/** |
|
367
|
|
|
* Test attach_stored_licenses_on_connection() for a secondary user. |
|
368
|
|
|
*/ |
|
369
|
|
View Code Duplication |
public function test_attach_stored_licenses_on_connection__secondary_user() { |
|
370
|
|
|
$connection = $this->createMock( Connection_Manager::class ); |
|
371
|
|
|
|
|
372
|
|
|
$connection->method( 'is_connection_owner' )->willReturn( false ); |
|
373
|
|
|
|
|
374
|
|
|
$licensing = $this->createPartialMock( |
|
375
|
|
|
Licensing::class, |
|
376
|
|
|
array( 'connection', 'attach_stored_licenses' ) |
|
377
|
|
|
); |
|
378
|
|
|
|
|
379
|
|
|
$licensing->method( 'connection' )->willReturn( $connection ); |
|
380
|
|
|
|
|
381
|
|
|
$licensing->expects( $this->never() ) |
|
382
|
|
|
->method( 'attach_stored_licenses' ); |
|
383
|
|
|
|
|
384
|
|
|
$licensing->attach_stored_licenses_on_connection(); |
|
385
|
|
|
} |
|
386
|
|
|
} |
|
387
|
|
|
|