Completed
Push — add/publishing-tweetstorms ( 53ba9c...ada03e )
by Gary
08:38
created

Test_Licensing::test_last_error()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 17
rs 9.7
c 0
b 0
f 0
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 Automattic\Jetpack\Licensing;
12
use Jetpack_IXR_ClientMulticall;
13
use Jetpack_Options;
14
use stdClass;
15
use WorDBless\BaseTestCase;
16
use WP_Error;
17
use WP_User;
18
19
/**
20
 * Class Test_Licensing
21
 *
22
 * @package Automattic\Jetpack
23
 */
24
class Test_Licensing extends BaseTestCase {
25
	/**
26
	 * Test last_error().
27
	 */
28
	public function test_last_error() {
29
		$licensing = new Licensing();
30
31
		delete_option( 'jetpack_options' );
32
		$this->assertSame( '', $licensing->last_error() );
33
34
		update_option( 'jetpack_options', array() );
35
		$this->assertSame( '', $licensing->last_error() );
36
37
		update_option( 'jetpack_options', array( 'licensing_error' => '' ) );
38
		$this->assertSame( '', $licensing->last_error() );
39
40
		update_option( 'jetpack_options', array( 'licensing_error' => 'foo' ) );
41
		$this->assertSame( 'foo', $licensing->last_error() );
42
43
		delete_option( 'jetpack_options' );
44
	}
45
46
	/**
47
	 * Test log_error().
48
	 */
49
	public function test_log_error() {
50
		$licensing = new Licensing();
51
52
		delete_option( 'jetpack_options' );
53
		$this->assertSame( '', $licensing->last_error() );
54
55
		$licensing->log_error( '' );
56
		$this->assertSame( '', $licensing->last_error() );
57
58
		$licensing->log_error( 'foo' );
59
		$this->assertSame( 'foo', $licensing->last_error() );
60
61
		$licensing->log_error( str_repeat( 'a', 2048 ) );
62
		$this->assertSame( str_repeat( 'a', 1024 ), $licensing->last_error() );
63
64
		delete_option( 'jetpack_options' );
65
	}
66
67
	/**
68
	 * Test stored_licenses().
69
	 */
70
	public function test_stored_licenses() {
71
		$licensing = new Licensing();
72
73
		delete_option( Licensing::LICENSES_OPTION_NAME );
74
		$this->assertSame( array(), $licensing->stored_licenses() );
75
76
		update_option( Licensing::LICENSES_OPTION_NAME, new stdClass() );
77
		$this->assertSame( array(), $licensing->stored_licenses() );
78
79
		update_option( Licensing::LICENSES_OPTION_NAME, array() );
80
		$this->assertSame( array(), $licensing->stored_licenses() );
81
82
		update_option( Licensing::LICENSES_OPTION_NAME, array( null ) );
83
		$this->assertSame( array(), $licensing->stored_licenses() );
84
85
		update_option( Licensing::LICENSES_OPTION_NAME, array( new stdClass() ) );
86
		$this->assertSame( array(), $licensing->stored_licenses() );
87
88
		update_option( Licensing::LICENSES_OPTION_NAME, array( 1 ) );
89
		$this->assertSame( array( '1' ), $licensing->stored_licenses() );
90
91
		update_option( Licensing::LICENSES_OPTION_NAME, array( 'foo', 'bar' ) );
92
		$this->assertSame( array( 'foo', 'bar' ), $licensing->stored_licenses() );
93
94
		delete_option( Licensing::LICENSES_OPTION_NAME );
95
	}
96
97
	/**
98
	 * Test attach_licenses() without an active Jetpack connection.
99
	 */
100
	public function test_attach_licenses__without_connection() {
101
		$connection = $this->createMock( Connection_Manager::class );
102
103
		$connection->method( 'is_active' )->willReturn( false );
104
105
		$licensing = $this->createPartialMock(
106
			Licensing::class,
107
			array( 'connection' )
108
		);
109
110
		$licensing->method( 'connection' )->willReturn( $connection );
111
112
		$result = $licensing->attach_licenses( array() );
113
114
		$this->assertInstanceOf( WP_Error::class, $result );
115
		$this->assertSame( 'not_connected', $result->get_error_code() );
116
	}
117
118
	/**
119
	 * Test attach_licenses() with an empty input.
120
	 */
121
	public function test_attach_licenses__empty_input() {
122
		$connection = $this->createMock( Connection_Manager::class );
123
124
		$connection->method( 'is_active' )->willReturn( true );
125
126
		$licensing = $this->createPartialMock(
127
			Licensing::class,
128
			array( 'connection' )
129
		);
130
131
		$licensing->method( 'connection' )->willReturn( $connection );
132
133
		$this->assertSame( array(), $licensing->attach_licenses( array() ) );
134
	}
135
136
	/**
137
	 * Test attach_licenses() with request failure.
138
	 */
139
	public function test_attach_licenses__request_failure() {
140
		$licenses = array( 'foo', 'bar' );
141
142
		$connection = $this->createMock( Connection_Manager::class );
143
144
		$connection->method( 'is_active' )->willReturn( true );
145
146
		$licensing = $this->createPartialMock(
147
			Licensing::class,
148
			array( 'connection', 'attach_licenses_request' )
149
		);
150
151
		$licensing->expects( $this->once() )
152
			->method( 'connection' )
153
			->willReturn( $connection );
154
155
		$ixr_client = $this->createMock( Jetpack_IXR_ClientMulticall::class );
156
		$ixr_client->method( 'isError' )->willReturn( true );
157
		$ixr_client->method( 'getErrorCode' )->willReturn( 1 );
158
		$ixr_client->method( 'getErrorMessage' )->willReturn( 'Expected error message' );
159
160
		$licensing->expects( $this->once() )
161
			->method( 'attach_licenses_request' )
162
			->with( $licenses )
163
			->willReturn( $ixr_client );
164
165
		$result = $licensing->attach_licenses( $licenses );
166
167
		$this->assertInstanceOf( WP_Error::class, $result );
168
		$this->assertSame( array( 'request_failed', 1 ), $result->get_error_codes() );
169
		$this->assertSame( 'Expected error message', $result->get_error_messages()[1] );
170
	}
171
172
	/**
173
	 * Test attach_licenses() with multiple licenses.
174
	 */
175
	public function test_attach_licenses__multiple_licenses() {
176
		$licenses = array( 'foo', 'bar' );
177
178
		$connection = $this->createMock( Connection_Manager::class );
179
180
		$connection->method( 'is_active' )->willReturn( true );
181
182
		$licensing = $this->createPartialMock(
183
			Licensing::class,
184
			array( 'connection', 'attach_licenses_request' )
185
		);
186
187
		$licensing->expects( $this->once() )
188
			->method( 'connection' )
189
			->willReturn( $connection );
190
191
		$ixr_client = $this->createMock( Jetpack_IXR_ClientMulticall::class );
192
		$ixr_client->method( 'isError' )
193
			->willReturn( false );
194
		$ixr_client->method( 'getResponse' )
195
			->willReturn(
196
				array(
197
					array(
198
						'faultCode'   => 1,
199
						'faultString' => 'Expected error message',
200
					),
201
					true,
202
				)
203
			);
204
205
		$licensing->expects( $this->once() )
206
			->method( 'attach_licenses_request' )
207
			->with( $licenses )
208
			->willReturn( $ixr_client );
209
210
		$result = $licensing->attach_licenses( $licenses );
211
212
		$this->assertSame( 2, count( $result ) );
213
		$this->assertInstanceOf( WP_Error::class, $result[0] );
214
		$this->assertSame( 1, $result[0]->get_error_code() );
215
		$this->assertSame( 'Expected error message', $result[0]->get_error_message() );
216
		$this->assertTrue( $result[1] );
217
	}
218
219
	/**
220
	 * Test attach_stored_licenses().
221
	 */
222
	public function test_attach_stored_licenses() {
223
		$result0  = new WP_Error();
224
		$result1  = true;
225
		$licenses = array( 'foo', 'bar' );
226
227
		$licensing = $this->createPartialMock(
228
			Licensing::class,
229
			array( 'stored_licenses', 'attach_licenses' )
230
		);
231
232
		$licensing->expects( $this->once() )
233
			->method( 'stored_licenses' )
234
			->willReturn( $licenses );
235
236
		$licensing->expects( $this->once() )
237
			->method( 'attach_licenses' )
238
			->with( $licenses )
239
			->willReturn( array( $result0, $result1 ) );
240
241
		$this->assertSame( array( $result0, $result1 ), $licensing->attach_stored_licenses() );
242
	}
243
244
	/**
245
	 * Test attach_stored_licenses() logs request failure.
246
	 */
247 View Code Duplication
	public function test_attach_stored_licenses__returns_error() {
248
		$licenses = array( 'foo', 'bar' );
249
250
		$error = new WP_Error( 'foo' );
0 ignored issues
show
Unused Code introduced by
The call to WP_Error::__construct() has too many arguments starting with 'foo'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
251
252
		$licensing = $this->createPartialMock(
253
			Licensing::class,
254
			array( 'stored_licenses', 'attach_licenses', 'log_error' )
255
		);
256
257
		$licensing->method( 'stored_licenses' )
258
			->willReturn( $licenses );
259
260
		$licensing->method( 'attach_licenses' )
261
			->with( $licenses )
262
			->willReturn( $error );
263
264
		$licensing->expects( $this->never() )->method( 'log_error' );
265
266
		$result = $licensing->attach_stored_licenses();
267
268
		$this->assertSame( $error, $result );
269
	}
270
271
	/**
272
	 * Test attach_stored_licenses() logs request failure.
273
	 */
274 View Code Duplication
	public function test_attach_stored_licenses__logs_request_failure() {
275
		$licenses = array( 'foo', 'bar' );
276
277
		$error = new WP_Error( 'request_failed' );
0 ignored issues
show
Unused Code introduced by
The call to WP_Error::__construct() has too many arguments starting with 'request_failed'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
278
279
		$licensing = $this->createPartialMock(
280
			Licensing::class,
281
			array( 'stored_licenses', 'attach_licenses', 'log_error' )
282
		);
283
284
		$licensing->method( 'stored_licenses' )
285
			->willReturn( $licenses );
286
287
		$licensing->method( 'attach_licenses' )
288
			->with( $licenses )
289
			->willReturn( $error );
290
291
		$licensing->expects( $this->once() )
292
			->method( 'log_error' )
293
			->with( 'Failed to attach your Jetpack license(s). Please try reconnecting Jetpack.' );
294
295
		$result = $licensing->attach_stored_licenses();
296
297
		$this->assertSame( $error, $result );
298
	}
299
300
	/**
301
	 * Test attach_stored_licenses() logs license attaching failures.
302
	 */
303
	public function test_attach_stored_licenses__logs_license_attaching_failures() {
304
		$result0  = new WP_Error();
305
		$result1  = true;
306
		$result2  = new WP_Error();
307
		$licenses = array( 'foo', 'bar', 'baz' );
308
309
		$licensing = $this->createPartialMock(
310
			Licensing::class,
311
			array( 'stored_licenses', 'attach_licenses', 'log_error' )
312
		);
313
314
		$licensing->method( 'stored_licenses' )
315
			->willReturn( $licenses );
316
317
		$licensing->method( 'attach_licenses' )
318
			->with( $licenses )
319
			->willReturn( array( $result0, $result1, $result2 ) );
320
321
		$licensing->expects( $this->once() )
322
			->method( 'log_error' )
323
			->with( 'The following Jetpack licenses are invalid, already in use or revoked: foo, baz' );
324
325
		$licensing->attach_stored_licenses();
326
	}
327
328
	/**
329
	 * Test attach_stored_licenses_on_connection() for the master user.
330
	 */
331 View Code Duplication
	public function test_attach_stored_licenses_on_connection__master_user() {
332
		$connection = $this->createMock( Connection_Manager::class );
333
334
		$connection->method( 'is_connection_owner' )->willReturn( true );
335
336
		$licensing = $this->createPartialMock(
337
			Licensing::class,
338
			array( 'connection', 'attach_stored_licenses' )
339
		);
340
341
		$licensing->method( 'connection' )->willReturn( $connection );
342
343
		$licensing->expects( $this->once() )
344
			->method( 'attach_stored_licenses' );
345
346
		$licensing->attach_stored_licenses_on_connection();
347
	}
348
349
	/**
350
	 * Test attach_stored_licenses_on_connection() for a secondary user.
351
	 */
352 View Code Duplication
	public function test_attach_stored_licenses_on_connection__secondary_user() {
353
		$connection = $this->createMock( Connection_Manager::class );
354
355
		$connection->method( 'is_connection_owner' )->willReturn( false );
356
357
		$licensing = $this->createPartialMock(
358
			Licensing::class,
359
			array( 'connection', 'attach_stored_licenses' )
360
		);
361
362
		$licensing->method( 'connection' )->willReturn( $connection );
363
364
		$licensing->expects( $this->never() )
365
			->method( 'attach_stored_licenses' );
366
367
		$licensing->attach_stored_licenses_on_connection();
368
	}
369
}
370