11
11
use lloc \Msls \MslsJson ;
12
12
use lloc \Msls \MslsMetaBox ;
13
13
use lloc \Msls \MslsOptions ;
14
+ use lloc \Msls \MslsOptionsPost ;
14
15
use lloc \Msls \MslsPostType ;
15
16
16
17
class TestMslsMetaBox extends MslsUnitTestCase {
@@ -34,6 +35,7 @@ protected function setUp(): void {
34
35
$ collection ->shouldReceive ( 'get ' )->andReturn ( array ( $ blog ) );
35
36
$ collection ->shouldReceive ( 'has_current_blog ' )->andReturnTrue ();
36
37
$ collection ->shouldReceive ( 'get_current_blog ' )->andReturn ( $ blog );
38
+ $ collection ->shouldReceive ( 'get_blog_id ' )->andReturn ( 1 );
37
39
38
40
$ this ->test = new MslsMetaBox ( $ options , $ collection );
39
41
}
@@ -104,7 +106,7 @@ public function test_render_option_not_selected(): void {
104
106
$ this ->assertEquals ( '<option value="1" >Test</option> ' , $ this ->test ->render_option ( 1 , 2 ) );
105
107
}
106
108
107
- public function test_render_options () {
109
+ public function test_render_options (): void {
108
110
$ post = \Mockery::mock ( 'WP_Post ' );
109
111
$ post ->ID = 42 ;
110
112
@@ -142,7 +144,7 @@ public function test_add( $post_type, $content_import, $autocomplete ) {
142
144
$ this ->test ->add ();
143
145
}
144
146
145
- public function test_render_select_not_hierarchical () {
147
+ public function test_render_select_not_hierarchical (): void {
146
148
global $ post ;
147
149
148
150
$ post = \Mockery::mock ( 'WP_Post ' );
@@ -174,7 +176,7 @@ public function test_render_select_not_hierarchical() {
174
176
$ this ->test ->render_select ();
175
177
}
176
178
177
- public function test_render_select_hierarchical () {
179
+ public function test_render_select_hierarchical (): void {
178
180
global $ post ;
179
181
180
182
$ post = \Mockery::mock ( 'WP_Post ' );
@@ -241,7 +243,7 @@ public function test_render_input( $option, $the_title_times, $current_blog_id_t
241
243
$ this ->test ->render_input ();
242
244
}
243
245
244
- public function test_render_select_only_one_blog () {
246
+ public function test_render_select_only_one_blog (): void {
245
247
$ options = \Mockery::mock ( MslsOptions::class );
246
248
247
249
$ collection = \Mockery::mock ( MslsBlogCollection::class );
@@ -255,7 +257,7 @@ public function test_render_select_only_one_blog() {
255
257
$ this ->test ->render_select ();
256
258
}
257
259
258
- public function test_render_input_only_one_blog () {
260
+ public function test_render_input_only_one_blog (): void {
259
261
$ options = \Mockery::mock ( MslsOptions::class );
260
262
261
263
$ collection = \Mockery::mock ( MslsBlogCollection::class );
@@ -269,34 +271,107 @@ public function test_render_input_only_one_blog() {
269
271
$ this ->test ->render_input ();
270
272
}
271
273
272
- public function test_set_no_request () {
274
+ public function test_set_no_request (): void {
273
275
Functions \expect ( 'wp_is_post_revision ' )->once ()->andReturn ( false );
274
276
275
277
$ this ->expectNotToPerformAssertions ();
276
278
$ this ->test ->set ( 13 );
277
279
}
278
280
279
- public function test_set_with_request () {
281
+ public function test_set_with_request_current_user_cannot (): void {
280
282
Functions \expect ( 'wp_is_post_revision ' )->once ()->andReturn ( false );
281
283
Functions \expect ( 'filter_has_var ' )->once ()->with ( INPUT_POST , MslsFields::FIELD_MSLS_NONCENAME )->andReturnTrue ();
282
284
Functions \expect ( 'wp_verify_nonce ' )->once ()->andReturnTrue ();
283
- Functions \expect ( 'current_user_can ' )->once ()->andReturnTrue ();
284
- Functions \expect ( 'get_option ' )->atLeast ()->once ()->andReturn ( array () );
285
- Functions \expect ( 'delete_option ' )->atLeast ()->once ();
286
- Functions \expect ( 'switch_to_blog ' )->once ();
287
- Functions \expect ( 'restore_current_blog ' )->once ();
285
+ Functions \expect ( 'current_user_can ' )->once ()->andReturnFalse ();
288
286
289
287
$ this ->expectNotToPerformAssertions ();
290
288
$ this ->test ->set ( 13 );
291
289
}
292
290
293
- public function test_set_with_request_current_user_cannot () {
291
+ public function test_set_with_request (): void {
294
292
Functions \expect ( 'wp_is_post_revision ' )->once ()->andReturn ( false );
295
293
Functions \expect ( 'filter_has_var ' )->once ()->with ( INPUT_POST , MslsFields::FIELD_MSLS_NONCENAME )->andReturnTrue ();
296
294
Functions \expect ( 'wp_verify_nonce ' )->once ()->andReturnTrue ();
297
- Functions \expect ( 'current_user_can ' )->once ()->andReturnFalse ();
295
+ Functions \expect ( 'current_user_can ' )->once ()->andReturnTrue ();
296
+ Functions \expect ( 'get_option ' )->atLeast ()->once ()->andReturn ( array () );
297
+ Functions \expect ( 'delete_option ' )->atLeast ()->once ();
298
+ Functions \expect ( 'switch_to_blog ' )->once ();
299
+ Functions \expect ( 'restore_current_blog ' )->once ();
298
300
299
301
$ this ->expectNotToPerformAssertions ();
300
302
$ this ->test ->set ( 13 );
301
303
}
304
+
305
+ public function test_maybe_set_linked_post () {
306
+ $ post = \Mockery::mock ( 'WP_Post ' );
307
+
308
+ Functions \expect ( 'filter_has_var ' )->once ()->with ( INPUT_GET , MslsFields::FIELD_MSLS_ID )->andReturnTrue ();
309
+ Functions \expect ( 'filter_has_var ' )->once ()->with ( INPUT_GET , MslsFields::FIELD_MSLS_LANG )->andReturnTrue ();
310
+ Functions \expect ( 'filter_input ' )->once ()->with ( INPUT_GET , MslsFields::FIELD_MSLS_LANG , FILTER_SANITIZE_FULL_SPECIAL_CHARS )->andReturn ( 'de_DE ' );
311
+ Functions \expect ( 'filter_input ' )->once ()->with ( INPUT_GET , MslsFields::FIELD_MSLS_ID , FILTER_SANITIZE_NUMBER_INT )->andReturn ( 42 );
312
+ Functions \expect ( 'get_post ' )->once ()->andReturn ( $ post );
313
+ Functions \expect ( 'restore_current_blog ' )->once ();
314
+ Functions \expect ( 'switch_to_blog ' )->once ();
315
+ Functions \expect ( 'get_option ' )->once ()->andReturn ( array () );
316
+
317
+ $ mydata = new MslsOptionsPost ();
318
+ $ mydata = $ this ->test ->maybe_set_linked_post ( $ mydata );
319
+
320
+ $ this ->assertEquals ( 42 , $ mydata ->de_DE );
321
+ }
322
+
323
+ public function test_maybe_set_linked_post_with_no_post () {
324
+ Functions \expect ( 'filter_has_var ' )->once ()->with ( INPUT_GET , MslsFields::FIELD_MSLS_ID )->andReturnTrue ();
325
+ Functions \expect ( 'filter_has_var ' )->once ()->with ( INPUT_GET , MslsFields::FIELD_MSLS_LANG )->andReturnTrue ();
326
+ Functions \expect ( 'filter_input ' )->once ()->with ( INPUT_GET , MslsFields::FIELD_MSLS_LANG , FILTER_SANITIZE_FULL_SPECIAL_CHARS )->andReturn ( 'de_DE ' );
327
+ Functions \expect ( 'filter_input ' )->once ()->with ( INPUT_GET , MslsFields::FIELD_MSLS_ID , FILTER_SANITIZE_NUMBER_INT )->andReturn ( 42 );
328
+ Functions \expect ( 'get_post ' )->once ()->andReturn ( null );
329
+ Functions \expect ( 'restore_current_blog ' )->once ();
330
+ Functions \expect ( 'switch_to_blog ' )->once ();
331
+ Functions \expect ( 'get_option ' )->once ()->andReturn ( array () );
332
+
333
+ $ mydata = new MslsOptionsPost ();
334
+ $ mydata = $ this ->test ->maybe_set_linked_post ( $ mydata );
335
+
336
+ $ this ->assertNull ( $ mydata ->de_DE );
337
+ }
338
+
339
+ function test_maybe_set_linked_post_with_no_blog_id () {
340
+ $ options = \Mockery::mock ( MslsOptions::class );
341
+
342
+ $ collection = \Mockery::mock ( MslsBlogCollection::class );
343
+ $ collection ->shouldReceive ( 'get_blog_id ' )->andReturn ( null );
344
+
345
+ $ this ->test = new MslsMetaBox ( $ options , $ collection );
346
+
347
+ Functions \expect ( 'filter_has_var ' )->once ()->with ( INPUT_GET , MslsFields::FIELD_MSLS_ID )->andReturnTrue ();
348
+ Functions \expect ( 'filter_has_var ' )->once ()->with ( INPUT_GET , MslsFields::FIELD_MSLS_LANG )->andReturnTrue ();
349
+ Functions \expect ( 'filter_input ' )->once ()->with ( INPUT_GET , MslsFields::FIELD_MSLS_LANG , FILTER_SANITIZE_FULL_SPECIAL_CHARS )->andReturn ( 'de_DE ' );
350
+ Functions \expect ( 'filter_input ' )->once ()->with ( INPUT_GET , MslsFields::FIELD_MSLS_ID , FILTER_SANITIZE_NUMBER_INT )->andReturn ( 42 );
351
+ Functions \expect ( 'get_option ' )->once ()->andReturn ( array () );
352
+
353
+ $ mydata = new MslsOptionsPost ();
354
+ $ mydata = $ this ->test ->maybe_set_linked_post ( $ mydata );
355
+
356
+ $ this ->assertNull ( $ mydata ->de_DE );
357
+ }
358
+
359
+ function test_maybe_set_linked_post_with_mydata_already_set () {
360
+ $ options = \Mockery::mock ( MslsOptions::class );
361
+
362
+ $ collection = \Mockery::mock ( MslsBlogCollection::class );
363
+
364
+ $ this ->test = new MslsMetaBox ( $ options , $ collection );
365
+
366
+ Functions \expect ( 'filter_has_var ' )->once ()->with ( INPUT_GET , MslsFields::FIELD_MSLS_ID )->andReturnTrue ();
367
+ Functions \expect ( 'filter_has_var ' )->once ()->with ( INPUT_GET , MslsFields::FIELD_MSLS_LANG )->andReturnTrue ();
368
+ Functions \expect ( 'filter_input ' )->once ()->with ( INPUT_GET , MslsFields::FIELD_MSLS_LANG , FILTER_SANITIZE_FULL_SPECIAL_CHARS )->andReturn ( 'de_DE ' );
369
+ Functions \expect ( 'get_option ' )->once ()->andReturn ( array () );
370
+
371
+ $ mydata = new MslsOptionsPost ();
372
+ $ mydata ->de_DE = 42 ;
373
+ $ mydata = $ this ->test ->maybe_set_linked_post ( $ mydata );
374
+
375
+ $ this ->assertEquals ( 42 , $ mydata ->de_DE );
376
+ }
302
377
}
0 commit comments