Completed
Push — update/tracking-assets ( 294fcb...ba0029 )
by
unknown
31:15 queued 15:24
created

WP_Test_Jetpack_Search_Helpers::tearDown()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
1
<?php
2
3
if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
4
	require_once WPMU_PLUGIN_DIR . '/jetpack-plugin/vendor/autoload_packages.php';
5
}
6
7
use Automattic\Jetpack\Constants;
8
9
require_jetpack_file( 'modules/search/class.jetpack-search.php' );
10
require_jetpack_file( 'modules/search/class.jetpack-search-helpers.php' );
11
require_jetpack_file( 'modules/search/class-jetpack-search-options.php' );
12
13
class WP_Test_Jetpack_Search_Helpers_Customize {
14
	public $previewing = false;
15
16
	public function is_preview() {
17
		return (bool) $this->previewing;
18
	}
19
}
20
21
class WP_Test_Jetpack_Search_Helpers_Query {
22
	public $searching = true;
23
	public function is_search() {
24
		return $this->searching;
25
	}
26
}
27
28
class WP_Test_Jetpack_Search_Helpers extends WP_UnitTestCase {
29
	protected $request_uri;
30
	protected $get;
31
	protected $post;
32
	protected $registered_widgets;
33
	protected $query;
34
	protected $post_types;
35
36
	function setup() {
37
		$GLOBALS['wp_customize'] = new WP_Test_Jetpack_Search_Helpers_Customize();
38
39
		$this->request_uri = $_SERVER['REQUEST_URI'];
40
		$this->get = $_GET;
41
		$this->get = $_POST;
42
		$this->registered_widgets = $GLOBALS['wp_registered_widgets'];
43
		$this->query = $GLOBALS['wp_query'];
44
		$this->post_types = $GLOBALS['wp_post_types'];
45
		delete_option( Jetpack_Search_Helpers::get_widget_option_name() );
46
	}
47
48
	function tearDown() {
49
		$_SERVER['REQUEST_URI'] = $this->request_uri;
50
		$_GET = $this->get;
51
		$_POST = $this->post;
52
		$GLOBALS['wp_registered_widgets'] = $this->registered_widgets;
53
		$GLOBALS['wp_query'] = $this->query;
54
		$GLOBALS['wp_post_types'] = $this->post_types;
55
		remove_filter( 'sidebars_widgets', array( $this, '_fake_out_search_widget' ) );
56
57
		unset( $GLOBALS['wp_customize'] );
58
59
		Constants::clear_constants();
60
		remove_all_filters( 'jetpack_search_has_vip_index' );
61
	}
62
63
	function test_get_search_url_removes_page_when_no_query_s() {
64
		$_SERVER['REQUEST_URI'] = "http://example.com/search/test/page/2/";
65
		set_query_var( 's', 'test' );
66
67
		$url = Jetpack_Search_Helpers::get_search_url();
68
69
		$this->assertNotContains( '/search/test/', $url );
70
		$this->assertNotContains( '/page/', $url );
71
		$this->assertContains( 's=test', $url );
72
	}
73
74
	function test_get_search_url_removes_page() {
75
		$_SERVER['REQUEST_URI'] = "http://example.com/page/2/?s=test";
76
		$_GET['s'] = 'test';
77
78
		$url = Jetpack_Search_Helpers::get_search_url();
79
80
		$this->assertNotContains( '/page/', $url );
81
		$this->assertContains( 's=test', $url );
82
	}
83
84
	function test_get_search_url_removes_paged_query_arg() {
85
		$_SERVER['REQUEST_URI'] = "http://example.com/page/2/?s=test&paged=2";
86
		$_GET['s'] = 'test';
87
		$_GET['paged'] = '2';
88
89
		$url = Jetpack_Search_Helpers::get_search_url();
90
91
		$this->assertNotContains( 'paged=', $url );
92
		$this->assertContains( 's=test', $url );
93
	}
94
95
	function test_add_query_arg_works_when_sending_array_of_args() {
96
		$_SERVER['REQUEST_URI'] = "http://example.com/page/2/?s=test&post_type=page";
97
		$_GET['s'] = 'test';
98
99
		$url = Jetpack_Search_Helpers::add_query_arg( array(
100
			'post_type' => 'page',
101
			'category' => 'uncategorized',
102
		) );
103
104
		$this->assertContains( 's=test', $url );
105
		$this->assertContains( 'post_type=page', $url );
106
		$this->assertContains( 'category=uncategorized', $url );
107
	}
108
109 View Code Duplication
	function test_add_query_arg_does_not_persist_page() {
110
		$_SERVER['REQUEST_URI'] = "http://example.com/page/2/?s=test&post_type=page";
111
		$_GET['s'] = 'test';
112
113
		$url = Jetpack_Search_Helpers::add_query_arg( 'post_type', 'page' );
114
115
		$this->assertNotContains( '/page/', $url );
116
		$this->assertContains( 's=test', $url );
117
	}
118
119
	function test_remove_query_arg_does_not_persist_page() {
120
		$_SERVER['REQUEST_URI'] = "http://example.com/page/2/?s=test";
121
		$_GET['s'] = 'test';
122
123
		$url = Jetpack_Search_Helpers::remove_query_arg( 'post_type' );
124
125
		$this->assertNotContains( '/page/', $url );
126
		$this->assertContains( 's=test', $url );
127
		$this->assertNotContains( 'post_type=', $url );
128
	}
129
130 View Code Duplication
	function test_add_query_arg_respects_url_passed() {
131
		$input_url = 'http://example.com/page/2/?s=test';
132
		$_SERVER['REQUEST_URI'] = $input_url;
133
		$_GET['s'] = 'test';
134
135
		$url = Jetpack_Search_Helpers::add_query_arg( 'post_type', 'page', $input_url );
136
		$this->assertSame( 'http://example.com/page/2/?s=test&post_type=page', $url );
137
	}
138
139 View Code Duplication
	function test_remove_query_arg_respects_url_passed() {
140
		$input_url = 'http://example.com/page/2/?s=test&post_type=post,page';
141
		$_SERVER['REQUEST_URI'] = $input_url;
142
		$_GET['s'] = 'test';
143
		$_GET['post_type'] = 'post,page';
144
145
		$url = Jetpack_Search_Helpers::remove_query_arg( 'post_type', $input_url );
0 ignored issues
show
Documentation introduced by
$input_url is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
146
		$this->assertSame( 'http://example.com/page/2/?s=test', $url );
147
	}
148
149
	function test_get_widget_option_name() {
150
		$this->assertSame( 'widget_jetpack-search-filters', Jetpack_Search_Helpers::get_widget_option_name() );
151
	}
152
153
	function test_get_widgets_from_option_empty_widget_option() {
154
		$this->assertSame( array(), Jetpack_Search_Helpers::get_widgets_from_option() );
155
	}
156
157
	function test_get_widgets_from_option_with_widgets_saved() {
158
		update_option( Jetpack_Search_Helpers::get_widget_option_name(), $this->get_sample_widgets_option() );
159
160
		$filters = Jetpack_Search_Helpers::get_widgets_from_option();
161
162
		$expected = $this->get_sample_widgets_option();
163
		unset( $expected['_multiwidget'] );
164
165
		$this->assertSame( $expected, $filters );
166
	}
167
	/**
168
	 * @dataProvider get_build_widget_id_data
169
	 */
170
	function test_build_widget_id( $number, $expected ) {
171
		$this->assertSame( $expected, Jetpack_Search_Helpers::build_widget_id( $number ) );
172
	}
173
174
	/**
175
	 * @dataProvider get_test_is_active_widget_data
176
	 */
177
	function test_is_active_widget( $number, $expected ) {
178
		$this->register_fake_widgets();
179
180
		$widget_id = Jetpack_Search_Helpers::build_widget_id( $number );
181
182
		$this->assertSame( $expected, Jetpack_Search_Helpers::is_active_widget( $widget_id ) );
183
184
	}
185
186
	function test_get_filters_from_widgets() {
187
		$raw_option = $this->get_sample_widgets_option();
188
		$filters = $raw_option[22]['filters'];
189
		$additional_filters = array(
190
			$this->get_cat_filter(),
191
			$this->get_tag_filter(),
192
			$this->get_post_type_filter(),
193
			$this->get_date_histogram_posts_by_month_filter(),
194
			$this->get_date_histogram_posts_by_year_filter(),
195
			$this->get_date_histogram_posts_modified_by_month_filter(),
196
			$this->get_date_histogram_posts_modified_by_year_filter(),
197
			$this->get_date_histogram_posts_by_month_gmt__filter(),
198
			$this->get_date_histogram_posts_by_year_gmt__filter(),
199
			$this->get_date_histogram_posts_modified_by_month_gmt_filter(),
200
			$this->get_date_histogram_posts_modified_by_year_gmt_filter(),
201
		);
202
203
		// Let's remove the name of the additional filters that way we can test our default name generation
204
		foreach ( $additional_filters as $filter ) {
205
			if ( isset( $filter['name'] ) ) {
206
				unset( $filter['name'] );
207
			}
208
209
			$filters[] = $filter;
210
		}
211
212
		$raw_option[22]['filters'] = $filters;
213
214
		update_option( Jetpack_Search_Helpers::get_widget_option_name(), $raw_option );
215
		$this->register_fake_widgets();
216
217
		$expected = array(
218
			'taxonomy_0' => array(
219
				'name' => 'Categories',
220
				'type' => 'taxonomy',
221
				'taxonomy' => 'category',
222
				'count' => 4,
223
				'widget_id' => 'jetpack-search-filters-22',
224
			),
225
			'post_type_1' => array(
226
				'name' => 'Post Type',
227
				'type' => 'post_type',
228
				'count' => 5,
229
				'widget_id' => 'jetpack-search-filters-22',
230
			),
231
			'taxonomy_2' => array(
232
				'type' => 'taxonomy',
233
				'taxonomy' => 'category',
234
				'count' => 4,
235
				'widget_id' => 'jetpack-search-filters-22',
236
				'name' => 'Categories',
237
			),
238
			'taxonomy_3' => array(
239
				'type' => 'taxonomy',
240
				'taxonomy' => 'post_tag',
241
				'count' => 2,
242
				'widget_id' => 'jetpack-search-filters-22',
243
				'name' => 'Tags',
244
			),
245
			'post_type_4' => array(
246
				'type' => 'post_type',
247
				'count' => 5,
248
				'widget_id' => 'jetpack-search-filters-22',
249
				'name' => 'Post Types',
250
			),
251
			'date_histogram_5' => array(
252
				'type' => 'date_histogram',
253
				'field' => 'post_date',
254
				'interval' => 'month',
255
				'count' => 10,
256
				'widget_id' => 'jetpack-search-filters-22',
257
				'name' => 'Month',
258
			),
259
			'date_histogram_6' => array(
260
				'type' => 'date_histogram',
261
				'field' => 'post_date',
262
				'interval' => 'year',
263
				'count' => 10,
264
				'widget_id' => 'jetpack-search-filters-22',
265
				'name' => 'Year',
266
			),
267
			'date_histogram_7' => array(
268
				'type' => 'date_histogram',
269
				'field' => 'post_modified',
270
				'interval' => 'month',
271
				'count' => 10,
272
				'widget_id' => 'jetpack-search-filters-22',
273
				'name' => 'Month Updated',
274
			),
275
			'date_histogram_8' => array(
276
				'type' => 'date_histogram',
277
				'field' => 'post_modified',
278
				'interval' => 'year',
279
				'count' => 10,
280
				'widget_id' => 'jetpack-search-filters-22',
281
				'name' => 'Year Updated',
282
			),
283
			'date_histogram_9' => array(
284
				'type' => 'date_histogram',
285
				'field' => 'post_date_gmt',
286
				'interval' => 'month',
287
				'count' => 10,
288
				'widget_id' => 'jetpack-search-filters-22',
289
				'name' => 'Month',
290
			),
291
			'date_histogram_10' => array(
292
				'type' => 'date_histogram',
293
				'field' => 'post_date_gmt',
294
				'interval' => 'year',
295
				'count' => 10,
296
				'widget_id' => 'jetpack-search-filters-22',
297
				'name' => 'Year',
298
			),
299
			'date_histogram_11' => array(
300
				'type' => 'date_histogram',
301
				'field' => 'post_modified_gmt',
302
				'interval' => 'month',
303
				'count' => 10,
304
				'widget_id' => 'jetpack-search-filters-22',
305
				'name' => 'Month Updated',
306
			),
307
			'date_histogram_12' => array(
308
				'type' => 'date_histogram',
309
				'field' => 'post_modified_gmt',
310
				'interval' => 'year',
311
				'count' => 10,
312
				'widget_id' => 'jetpack-search-filters-22',
313
				'name' => 'Year Updated',
314
			),
315
		);
316
317
		$this->assertSame( $expected, Jetpack_Search_Helpers::get_filters_from_widgets() );
318
	}
319
320
	/**
321
	 * @dataProvider get_should_rerun_search_in_customizer_preview_data
322
	 */
323
	function test_should_rerun_search_in_customizer_preview( $expected, $previewing = false, $post = false ) {
324
		if ( $previewing ) {
325
			$GLOBALS['wp_customize']->previewing = true;
326
		}
327
		if ( $post ) {
328
			$_POST = array( 'test' => 1 );
329
		}
330
331
		$this->assertSame( $expected, Jetpack_Search_Helpers::should_rerun_search_in_customizer_preview() );
332
	}
333
334
	/**
335
	 * @dataProvider get_array_diff_data
336
	 */
337
	function test_array_diff( $expected, $array_1, $array_2 ) {
338
		$this->assertSame( $expected, Jetpack_Search_Helpers::array_diff( $array_1, $array_2 ) );
339
	}
340
341
	/**
342
	 * @dataProvider get_post_types_differ_searchable_data
343
	 */
344
	function test_post_types_differ_searchable( $expected, $post_types = array() ) {
345
		$GLOBALS['wp_post_types'] = array(
346
			'post'       => array( 'name' => 'post', 'exclude_from_search' => false ),
347
			'page'       => array( 'name' => 'page', 'exclude_from_search' => false ),
348
			'attachment' => array( 'name' => 'attachment', 'exclude_from_search' => false )
349
		);
350
		$this->assertSame( $expected, Jetpack_Search_Helpers::post_types_differ_searchable( $post_types ) );
351
	}
352
353
	/**
354
	 * @dataProvider get_post_types_differ_query_data
355
	 */
356
	function test_post_types_differ_query( $expected, $post_types = array(), $get = array() ) {
357
		$_GET = $get;
358
		$this->assertSame( $expected, Jetpack_Search_Helpers::post_types_differ_query( $post_types ) );
359
	}
360
361
	/**
362
	 * @dataProvider get_filter_properties_for_tracks_data
363
	 */
364
	function test_get_filter_properties_for_tracks( $expected, $filters ) {
365
		$this->assertSame( $expected, Jetpack_Search_Helpers::get_filter_properties_for_tracks( $filters ) );
366
	}
367
368
	/**
369
	 * @dataProvider get_widget_properties_for_tracks_data
370
	 */
371
	function test_get_widget_properties_for_tracks( $expected, $widget ) {
372
		$this->assertSame( $expected, Jetpack_Search_Helpers::get_widget_properties_for_tracks( $widget ) );
373
	}
374
375
	/**
376
	 * @dataProvider get_widget_tracks_value_data
377
	 */
378
	function test_get_widget_tracks_value( $expected, $old_value, $new_value ) {
379
		$this->assertSame( $expected, Jetpack_Search_Helpers::get_widget_tracks_value( $old_value, $new_value ) );
380
	}
381
382
	/**
383
	 * @dataProvider get_remove_active_from_post_type_buckets_data
384
	 */
385
	public function test_remove_active_from_post_type_buckets( $expected, $input ) {
386
		$this->assertSame(
387
			$expected,
388
			Jetpack_Search_Helpers::remove_active_from_post_type_buckets( $input )
389
		);
390
	}
391
392
	/**
393
	 * @dataProvider get_add_post_types_to_url_data
394
	 */
395
	function test_add_post_types_to_url( $expected, $url, $post_types ) {
396
		$this->assertSame(
397
			$expected,
398
			Jetpack_Search_Helpers::add_post_types_to_url( $url, $post_types )
399
		);
400
	}
401
402
	/**
403
	 * @dataProvider get_ensure_post_types_on_remove_url_data
404
	 */
405
	function test_ensure_post_types_on_remove_url( $expected, $filters, $post_types ) {
406
		$this->assertSame(
407
			$expected,
408
			Jetpack_Search_Helpers::ensure_post_types_on_remove_url( $filters, $post_types )
409
		);
410
	}
411
412
	/**
413
	 * @dataProvider get_site_has_vip_index_data
414
	 */
415
	public function test_site_has_vip_index( $expected, $constant = null, $filter = false ) {
416
		if ( ! is_null( $constant ) ) {
417
			Constants::set_constant( 'JETPACK_SEARCH_VIP_INDEX', $constant );
418
		}
419
420
		if ( $filter ) {
421
			add_filter( 'jetpack_search_has_vip_index', $filter );
0 ignored issues
show
Documentation introduced by
$filter is of type boolean, but the function expects a callable.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
422
		}
423
424
		$this->assertSame( $expected, Jetpack_Search_Options::site_has_vip_index() );
425
	}
426
427
	/**
428
	 * @dataProvider get_max_posts_per_page_data
429
	 */
430
	public function test_get_max_posts_per_page( $expected, $has_vip_index ) {
431
		Constants::set_constant( 'JETPACK_SEARCH_VIP_INDEX', $has_vip_index );
432
		$this->assertSame( $expected, Jetpack_Search_Helpers::get_max_posts_per_page() );
433
	}
434
435
	/**
436
	 * @dataProvider get_max_offset_data
437
	 */
438
	public function test_get_max_offset( $expected, $has_vip_index ) {
439
		Constants::set_constant( 'JETPACK_SEARCH_VIP_INDEX', $has_vip_index );
440
		$this->assertSame( $expected, Jetpack_Search_Helpers::get_max_offset() );
441
	}
442
443
	/**
444
	 * @dataProvider get_date_filter_type_name_data
445
	 */
446
	public function test_get_date_filter_type_name( $expected, $type, $is_updated ) {
447
		$this->assertSame(
448
			$expected, Jetpack_Search_Helpers::get_date_filter_type_name(
449
				$type,
450
				$is_updated
451
			)
452
		);
453
	}
454
455
	public function get_date_filter_type_name_data() {
456
		return array(
457
			'default' => array(
458
				'Month',
459
				'something',
460
				null,
461
			),
462
			'month' => array(
463
				'Month',
464
				'month',
465
				false,
466
			),
467
			'month_update' => array(
468
				'Month Updated',
469
				'month',
470
				true,
471
			),
472
			'year' => array(
473
				'Year',
474
				'year',
475
				false,
476
			),
477
			'year_updated' => array(
478
				'Year Updated',
479
				'year',
480
				true,
481
			),
482
		);
483
	}
484
485
	/**
486
	 * Data providers
487
	 */
488
	function get_build_widget_id_data() {
489
		return array(
490
			'jetpack-search-filters-22' => array(
491
				22,
492
				'jetpack-search-filters-22'
493
			),
494
			'jetpack-search-filters-10' => array(
495
				10,
496
				'jetpack-search-filters-10'
497
			)
498
		);
499
	}
500
501
	function get_test_is_active_widget_data() {
502
		return array(
503
			'jetpack-search-filters-22' => array(
504
				22,
505
				true
506
			),
507
			'jetpack-search-filters-10' => array(
508
				10,
509
				false
510
			)
511
		);
512
	}
513
514
	function get_should_rerun_search_in_customizer_preview_data() {
515
		return array(
516
			'not_previewing' => array(
517
				false
518
			),
519
			'is_previewing_not_post' => array(
520
				false,
521
				true
522
			),
523
			'is_preview_and_post_filters_initially_empty' => array(
524
				true,
525
				true,
526
				true,
527
			),
528
		);
529
	}
530
531
	function get_array_diff_data() {
532
		return array(
533
			'all_empty' => array(
534
				array(),
535
				array(),
536
				array(),
537
			),
538
			'same_count_same_items' => array(
539
				array(),
540
				array( 'post' ),
541
				array( 'post' ),
542
			),
543
			'same_count_different_items' => array(
544
				array( 'post' ),
545
				array( 'post' ),
546
				array( 'page' ),
547
			),
548
			'array_1_more_items' => array(
549
				array( 'jetpack-testimonial' ),
550
				array( 'post', 'page', 'jetpack-testimonial' ),
551
				array( 'post', 'page' ),
552
			),
553
			'array_2_more_items' => array(
554
				array( 'jetpack-testimonial' ),
555
				array( 'post', 'page' ),
556
				array( 'post', 'page', 'jetpack-testimonial' ),
557
			)
558
		);
559
	}
560
561
	function get_post_types_differ_searchable_data() {
562
		return array(
563
			'no_post_types' => array(
564
				false,
565
				array(),
566
			),
567
			'post_types_same' => array(
568
				false,
569
				array( 'post', 'page', 'attachment' )
570
			),
571
			'post_types_same_count_different_types' => array(
572
				true,
573
				array( 'post', 'page', 'jetpack-testimonial' )
574
			),
575
			'post_types_has_fewer' => array(
576
				true,
577
				'post_types' => array( 'post' )
578
			),
579
			'post_types_has_more' => array(
580
				true,
581
				array( 'post', 'page', 'attachment', 'jetpack-testimonial' )
582
			),
583
		);
584
	}
585
586
	function get_post_types_differ_query_data() {
587
		return array(
588
			'no_post_types_on_instance' => array(
589
				false,
590
				array(),
591
			),
592
			'post_types_same' => array(
593
				false,
594
				array( 'post', 'page', 'attachment' ),
595
				array( 'post_type' => array( 'post', 'page', 'attachment' ) )
596
			),
597
			'post_types_same_count_different_types' => array(
598
				true,
599
				array( 'post', 'page', 'jetpack-testimonial' ),
600
				array( 'post_type' => array( 'post', 'page', 'attachment' ) )
601
			),
602
			'post_types_instance_has_fewer' => array(
603
				true,
604
				array( 'post' ),
605
				array( 'post_type' => array( 'post', 'page' ) )
606
			),
607
			'post_types_instance_has_more' => array(
608
				true,
609
				array( 'post', 'page', 'attachment', 'jetpack-testimonial' ),
610
				array( 'post_type' => 'post,page' )
611
			),
612
			'post_types_same_csv' => array(
613
				false,
614
				array( 'post', 'page', 'attachment' ),
615
				array( 'post_type' => 'post, page, attachment' )
616
			),
617
			'post_types_same_count_different_types_csv' => array(
618
				true,
619
				array( 'post', 'page', 'jetpack-testimonial' ),
620
				array( 'post_type' => 'post, page, attachment' )
621
			),
622
			'post_types_instance_has_fewer_csv' => array(
623
				true,
624
				array( 'post' ),
625
				array( 'post_type' => 'post, page' )
626
			),
627
			'post_types_instance_has_more_csv' => array(
628
				true,
629
				array( 'post', 'page', 'attachment', 'jetpack-testimonial' ),
630
				array( 'post_type' => 'post, page' )
631
			),
632
		);
633
	}
634
635
	function get_filter_properties_for_tracks_data() {
636
		return array(
637
			'empty_filters' => array(
638
				array(),
639
				array()
640
			),
641
			'single_filter' => array(
642
				array(
643
					'widget_filter_count' => 1,
644
					'widget_filter_type_taxonomy' => 1,
645
				),
646
				array(
647
					$this->get_cat_filter()
648
				)
649
			),
650
			'multiple_filters' => array(
651
				array(
652
					'widget_filter_count' => 3,
653
					'widget_filter_type_taxonomy' => 2,
654
					'widget_filter_type_post_type' => 1,
655
				),
656
				array(
657
					$this->get_cat_filter(),
658
					$this->get_post_type_filter(),
659
					$this->get_tag_filter()
660
				)
661
			)
662
		);
663
	}
664
665
	function get_widget_properties_for_tracks_data() {
666
		return array(
667
			'empty_instance' => array(
668
				array(),
669
				array()
670
			),
671
			'instance_with_only_multiwidet' => array(
672
				array(),
673
				array(
674
					'_multiwidget' => 1,
675
				),
676
			),
677
			'instance_with_no_filters' => array(
678
				array(
679
					'widget_title' => 'Search',
680
					'widget_search_box_enabled' => 1,
681
				),
682
				$this->get_sample_widget_instance( 0 )
683
			),
684
			'instance_with_filters' => array(
685
				array(
686
					'widget_title' => 'Search',
687
					'widget_search_box_enabled' => 1,
688
					'widget_filter_count' => 1,
689
					'widget_filter_type_taxonomy' => 1,
690
				),
691
				$this->get_sample_widget_instance( 1 )
692
			),
693
		);
694
	}
695
696
	function get_widget_tracks_value_data() {
697
		$instance_with_filter_updated = $this->get_sample_widget_instance();
698
		$instance_with_filter_updated['filters'][1] = $this->get_tag_filter();
699
700
		return array(
701
			'widget_updated_added_filters' => array(
702
				array(
703
					'action' => 'widget_updated',
704
					'widget' => array(
705
						'widget_title' => 'Search',
706
						'widget_search_box_enabled' => 1,
707
						'widget_filter_count' => 1,
708
						'widget_filter_type_taxonomy' => 1,
709
					)
710
				),
711
				array( $this->get_sample_widget_instance( 0 ) ),
712
				array( $this->get_sample_widget_instance( 1 ) ),
713
			),
714
			'widget_updated_title_changed' => array(
715
				array(
716
					'action' => 'widget_updated',
717
					'widget' => array(
718
						'widget_title' => 'changed',
719
						'widget_search_box_enabled' => 1,
720
						'widget_filter_count' => 2,
721
						'widget_filter_type_taxonomy' => 1,
722
						'widget_filter_type_post_type' => 1
723
					)
724
				),
725
				array( $this->get_sample_widget_instance() ),
726
				array( array_merge( $this->get_sample_widget_instance(), array( 'title' => 'changed' ) ) ),
727
			),
728
			'widget_update_removed_filters' => array(
729
				array(
730
					'action' => 'widget_updated',
731
					'widget' => array(
732
						'widget_title' => 'Search',
733
						'widget_search_box_enabled' => 1,
734
					)
735
				),
736
				array( $this->get_sample_widget_instance( 2 ) ),
737
				array( $this->get_sample_widget_instance( 0 ) ),
738
			),
739
			'multiple_widgets_one_title_changed' => array(
740
				array(
741
					'action' => 'widget_updated',
742
					'widget' => array(
743
						'widget_title' => 'updated',
744
						'widget_search_box_enabled' => 1,
745
					)
746
				),
747
				array(
748
					'0' => $this->get_sample_widget_instance( 0 ),
749
					'1' => $this->get_sample_widget_instance( 1 ),
750
					'2' => $this->get_sample_widget_instance( 2 ),
751
					'_multiwidget' => 1
752
				),
753
				array(
754
					'0' => array_merge( $this->get_sample_widget_instance( 0 ), array( 'title' => 'updated' ) ),
755
					'1' => $this->get_sample_widget_instance( 1 ),
756
					'2' => $this->get_sample_widget_instance( 2 ),
757
					'_multiwidget' => 1
758
				),
759
				array(
760
					'_multiwidget' => 1
761
				)
762
			),
763
			'multiple_widgets_filter_added' => array(
764
				array(
765
					'action' => 'widget_updated',
766
					'widget' => array(
767
						'widget_title' => 'Search',
768
						'widget_search_box_enabled' => 1,
769
						'widget_filter_count' => 2,
770
						'widget_filter_type_taxonomy' => 1,
771
						'widget_filter_type_post_type' => 1
772
					)
773
				),
774
				array(
775
					'0' => $this->get_sample_widget_instance( 0 ),
776
					'1' => $this->get_sample_widget_instance( 1 ),
777
					'2' => $this->get_sample_widget_instance( 2 ),
778
					'_multiwidget' => 1
779
				),
780
				array(
781
					'0' => $this->get_sample_widget_instance( 0 ),
782
					'1' => $this->get_sample_widget_instance( 2 ),
783
					'2' => $this->get_sample_widget_instance( 2 ),
784
					'_multiwidget' => 1
785
				),
786
				array(
787
					'_multiwidget' => 1
788
				)
789
			),
790
			'multiple_widgets_filter_updated' => array(
791
				array(
792
					'action' => 'widget_updated',
793
					'widget' => array(
794
						'widget_title' => 'Search',
795
796
						'widget_search_box_enabled' => 1,
797
						'widget_filter_count' => 2,
798
						'widget_filter_type_taxonomy' => 2,
799
					)
800
				),
801
				array(
802
					'0' => $this->get_sample_widget_instance( 0 ),
803
					'1' => $this->get_sample_widget_instance( 1 ),
804
					'2' => $this->get_sample_widget_instance( 2 ),
805
					'_multiwidget' => 1
806
				),
807
				array(
808
					'0' => $this->get_sample_widget_instance( 0 ),
809
					'1' => $this->get_sample_widget_instance( 1 ),
810
					'2' => $instance_with_filter_updated,
811
					'_multiwidget' => 1
812
				),
813
				array(
814
					'_multiwidget' => 1
815
				)
816
			),
817
			'widget_added_from_empty' => array(
818
				array(
819
					'action' => 'widget_added',
820
					'widget' => array(
821
						'widget_title' => 'Search',
822
						'widget_search_box_enabled' => 1,
823
						'widget_filter_count' => 2,
824
						'widget_filter_type_taxonomy' => 1,
825
						'widget_filter_type_post_type' => 1,
826
					)
827
				),
828
				array( '_multiwidget' => 1 ),
829
				array(
830
					'0' => $this->get_sample_widget_instance(),
831
					'_multiwidget' => 1,
832
				),
833
			),
834
			'widget_removed_none_to_empty' => array(
835
				array(
836
					'action' => 'widget_deleted',
837
					'widget' => array(
838
						'widget_title' => 'Search',
839
						'widget_search_box_enabled' => 1,
840
						'widget_filter_count' => 2,
841
						'widget_filter_type_taxonomy' => 1,
842
						'widget_filter_type_post_type' => 1,
843
					)
844
				),
845
				array(
846
					'0' => $this->get_sample_widget_instance(),
847
					'_multiwidget' => 1
848
				),
849
				array( '_multiwidget' => 1 ),
850
			),
851
			'widget_added_one_to_two' => array(
852
				array(
853
					'action' => 'widget_added',
854
					'widget' => array(
855
						'widget_title' => 'Search',
856
						'widget_search_box_enabled' => 1,
857
						'widget_filter_count' => 1,
858
						'widget_filter_type_taxonomy' => 1,
859
					)
860
				),
861
				array(
862
					$this->get_sample_widget_instance(),
863
					'_multiwidget' => 1,
864
				),
865
				array(
866
					$this->get_sample_widget_instance(),
867
					$this->get_sample_widget_instance( 1 ),
868
					'_multiwidget' => 1,
869
				)
870
			),
871
			'widget_added_two_to_one' => array(
872
				array(
873
					'action' => 'widget_deleted',
874
					'widget' => array(
875
						'widget_title' => 'Search',
876
						'widget_search_box_enabled' => 1,
877
					)
878
				),
879
				array(
880
					'1' => $this->get_sample_widget_instance( 0 ),
881
					'2' => $this->get_sample_widget_instance( 1 ),
882
					'_multiwidget' => 1,
883
				),
884
				array(
885
					'2' => $this->get_sample_widget_instance( 1 ),
886
					'_multiwidget' => 1,
887
				),
888
			),
889
		);
890
	}
891
892
	public function get_remove_active_from_post_type_buckets_data() {
893
		return array(
894
			'empty_array' => array(
895
				array(),
896
				array(),
897
			),
898
			'unchanged_if_not_post_type_filter' => array(
899
				array(
900
					'taxonomy_0' => array(
901
						'type' => 'taxonomy',
902
					),
903
				),
904
				array(
905
					'taxonomy_0' => array(
906
						'type' => 'taxonomy',
907
					),
908
				),
909
			),
910
			'unchanged_if_post_type_but_no_buckets' => array(
911
				array(
912
					'post_type_0' => array(
913
						'type' => 'post_type',
914
					),
915
				),
916
				array(
917
					'post_type_0' => array(
918
						'type' => 'post_type',
919
					),
920
				),
921
			),
922
			'active_false_on_post_type_buckets' => array(
923
				array(
924
					'post_type_0' => array(
925
						'type' => 'post_type',
926
						'buckets' => array(
927
							array(
928
								'active' => false,
929
							),
930
							array(
931
								'active' => false,
932
							),
933
						),
934
					),
935
				),
936
				array(
937
					'post_type_0' => array(
938
						'type' => 'post_type',
939
						'buckets' => array(
940
							array(
941
								'active' => true,
942
							),
943
							array(
944
								'active' => true,
945
							),
946
						),
947
					),
948
				),
949
			),
950
		);
951
	}
952
953
	function get_add_post_types_to_url_data() {
954
		return array(
955
			'same_url_empty_post_types' => array(
956
				'http://jetpack.com?s=test',
957
				'http://jetpack.com?s=test',
958
				array(),
959
			),
960
			'no_post_types_on_url' => array(
961
				'http://jetpack.com?s=test&post_type=post,page',
962
				'http://jetpack.com?s=test',
963
				array( 'post', 'page' ),
964
			),
965
			'overwrite_existing_post_types' => array(
966
				'http://jetpack.com?s=test&post_type=post,page',
967
				'http://jetpack.com?s=test&post_type=jetpack-testimonial',
968
				array( 'post', 'page' ),
969
			),
970
		);
971
	}
972
973
	function get_ensure_post_types_on_remove_url_data() {
974
		return array(
975
			'unmodified_if_no_post_types' => array(
976
				array(
977
					'taxonomy_0' => array(
978
						'type' => 'taxonomy',
979
					),
980
				),
981
				array(
982
					'taxonomy_0' => array(
983
						'type' => 'taxonomy',
984
					),
985
				),
986
				array(),
987
			),
988
			'unmodified_if_post_type_no_buckets' => array(
989
				array(
990
					'post_type_0' => array(
991
						'type' => 'post_type',
992
					),
993
				),
994
				array(
995
					'post_type_0' => array(
996
						'type' => 'post_type',
997
					),
998
				),
999
				array(),
1000
			),
1001
			'unmodified_if_remove_url_not_on_bucket' => array(
1002
				array(
1003
					'post_type_0' => array(
1004
						'type' => 'post_type',
1005
						'buckets' => array(
1006
							array(
1007
								'name' => 'test',
1008
							),
1009
						),
1010
					),
1011
				),
1012
				array(
1013
					'post_type_0' => array(
1014
						'type' => 'post_type',
1015
						'buckets' => array(
1016
							array(
1017
								'name' => 'test',
1018
							),
1019
						),
1020
					),
1021
				),
1022
				array(),
1023
			),
1024
			'unmodified_if_remove_url_bad' => array(
1025
				array(
1026
					'post_type_0' => array(
1027
						'type' => 'post_type',
1028
						'buckets' => array(
1029
							array(
1030
								'name' => 'test',
1031
								'remove_url' => 'http://:80',
1032
							),
1033
						),
1034
					),
1035
				),
1036
				array(
1037
					'post_type_0' => array(
1038
						'type' => 'post_type',
1039
						'buckets' => array(
1040
							array(
1041
								'name' => 'test',
1042
								'remove_url' => 'http://:80',
1043
							),
1044
						),
1045
					),
1046
				),
1047
				array(),
1048
			),
1049
			'unmodified_if_no_query' => array(
1050
				array(
1051
					'post_type_0' => array(
1052
						'type' => 'post_type',
1053
						'buckets' => array(
1054
							array(
1055
								'name' => 'test',
1056
								'remove_url' => 'http://jetpack.com',
1057
							),
1058
						),
1059
					),
1060
				),
1061
				array(
1062
					'post_type_0' => array(
1063
						'type' => 'post_type',
1064
						'buckets' => array(
1065
							array(
1066
								'name' => 'test',
1067
								'remove_url' => 'http://jetpack.com',
1068
							),
1069
						),
1070
					),
1071
				),
1072
				array(),
1073
			),
1074
			'unmodified_if_query_has_post_type' => array(
1075
				array(
1076
					'post_type_0' => array(
1077
						'type' => 'post_type',
1078
						'buckets' => array(
1079
							array(
1080
								'name' => 'test',
1081
								'remove_url' => 'http://jetpack.com?post_type=post,page',
1082
							),
1083
						),
1084
					),
1085
				),
1086
				array(
1087
					'post_type_0' => array(
1088
						'type' => 'post_type',
1089
						'buckets' => array(
1090
							array(
1091
								'name' => 'test',
1092
								'remove_url' => 'http://jetpack.com?post_type=post,page',
1093
							),
1094
						),
1095
					),
1096
				),
1097
				array(),
1098
			),
1099
			'adds_post_types_if_no_post_types_on_remove_url' => array(
1100
				array(
1101
					'post_type_0' => array(
1102
						'type' => 'post_type',
1103
						'buckets' => array(
1104
							array(
1105
								'name' => 'test',
1106
								'remove_url' => 'http://jetpack.com?post_type=post,page',
1107
							),
1108
						),
1109
					),
1110
				),
1111
				array(
1112
					'post_type_0' => array(
1113
						'type' => 'post_type',
1114
						'buckets' => array(
1115
							array(
1116
								'name' => 'test',
1117
								'remove_url' => 'http://jetpack.com',
1118
							),
1119
						),
1120
					),
1121
				),
1122
				array( 'post', 'page' ),
1123
			),
1124
		);
1125
	}
1126
1127
	public function get_site_has_vip_index_data() {
1128
		return array(
1129
			'default_constants_filter' => array(
1130
				false,
1131
			),
1132
			'constant_false_no_filter' => array(
1133
				false,
1134
				false,
1135
			),
1136
			'constant_true_no_filter' => array(
1137
				true,
1138
				true,
1139
			),
1140
			'constant_false_filter_true' => array(
1141
				true,
1142
				false,
1143
				'__return_true',
1144
			),
1145
			'constant_true_filter_false' => array(
1146
				false,
1147
				true,
1148
				'__return_false',
1149
			),
1150
		);
1151
	}
1152
1153 View Code Duplication
	public function get_max_offset_data() {
1154
		return array (
1155
			'not_vip_index' => array(
1156
				1000,
1157
				false,
1158
			),
1159
			'has_vip_index' => array(
1160
				9000,
1161
				true,
1162
			),
1163
		);
1164
	}
1165
1166 View Code Duplication
	public function get_max_posts_per_page_data() {
1167
		return array (
1168
			'not_vip_index' => array(
1169
				100,
1170
				false,
1171
			),
1172
			'has_vip_index' => array(
1173
				1000,
1174
				true,
1175
			),
1176
		);
1177
	}
1178
1179
	/**
1180
	 * Helpers
1181
	 */
1182
	function _fake_out_search_widget( $widgets ) {
1183
		$widgets['wp_inactive_widgets'][] = 'jetpack-search-filters-10';
1184
1185
		$override = array();
0 ignored issues
show
Unused Code introduced by
$override is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
1186
		foreach ( $widgets as $key => $sidebar ) {
1187
			if ( 'wp_inactive_widgets' == $key ) {
1188
				continue;
1189
			}
1190
1191
			$widgets[ $key ][] = 'jetpack-search-filters-22';
1192
			return $widgets;
1193
		}
1194
1195
		return $widgets;
1196
	}
1197
1198
	function register_fake_widgets() {
1199
		add_filter( 'sidebars_widgets', array( $this, '_fake_out_search_widget' ) );
1200
1201
		$widget_ids = array(
1202
			'jetpack-search-filters-10',
1203
			'jetpack-search-filters-22',
1204
		);
1205
1206
		foreach( $widget_ids as $id ) {
1207
			$GLOBALS['wp_registered_widgets'][ $id ] = array(
1208
				'id' => $id,
1209
			);
1210
		}
1211
	}
1212
1213
	function get_sample_widgets_option() {
1214
		return array(
1215
			'15' => array(
1216
				'title' => 'Search',
1217
				'search_box_enabled' => 0,
1218
				'filters' => array(
1219
					array(
1220
						'name' => 'Categories',
1221
						'type' => 'taxonomy',
1222
						'taxonomy' => 'category',
1223
						'count' => 4
1224
					)
1225
				)
1226
			),
1227
			'22' => $this->get_sample_widget_instance(),
1228
			'_multiwidget' => 1
1229
		);
1230
	}
1231
1232
	function get_sample_filters( $count_filters = 2, $count_cat = 4 ) {
1233
		$filters = array();
1234
1235
		if ( $count_filters > 0 ) {
1236
			$filters[] = $this->get_cat_filter( $count_cat );
1237
		}
1238
1239
		if ( $count_filters > 1 ) {
1240
			$filters[] = $this->get_post_type_filter();
1241
		}
1242
1243
		return $filters;
1244
	}
1245
1246
	function get_sample_widget_instance( $count_filters = 2, $count_cat = 4 ) {
1247
		$instance = array(
1248
			'title' => 'Search',
1249
			'search_box_enabled' => 1,
1250
		);
1251
1252
		if ( $count_filters > 0 ) {
1253
			$instance['filters'] = $this->get_sample_filters( $count_filters, $count_cat );
1254
		}
1255
1256
		return $instance;
1257
	}
1258
1259
	function get_cat_filter( $count = 4 ) {
1260
		return array(
1261
			'name' => 'Categories',
1262
			'type' => 'taxonomy',
1263
			'taxonomy' => 'category',
1264
			'count' => $count,
1265
		);
1266
	}
1267
1268
	function get_tag_filter() {
1269
		return array(
1270
			'name' => 'Tags',
1271
			'type' => 'taxonomy',
1272
			'taxonomy' => 'post_tag',
1273
			'count' => 2,
1274
		);
1275
	}
1276
1277
	function get_post_type_filter() {
1278
		return array(
1279
			'name' => 'Post Type',
1280
			'type' => 'post_type',
1281
			'count' => 5,
1282
		);
1283
	}
1284
1285
	function get_date_histogram_posts_by_month_filter() {
1286
		return array(
1287
			'type'     => 'date_histogram',
1288
			'field'    => 'post_date',
1289
			'interval' => 'month',
1290
			'count'    => 10,
1291
		);
1292
	}
1293
1294
	function get_date_histogram_posts_by_year_filter() {
1295
		return array(
1296
			'type'     => 'date_histogram',
1297
			'field'    => 'post_date',
1298
			'interval' => 'year',
1299
			'count'    => 10,
1300
		);
1301
	}
1302
1303
	function get_date_histogram_posts_modified_by_month_filter() {
1304
		return array(
1305
			'type'     => 'date_histogram',
1306
			'field'    => 'post_modified',
1307
			'interval' => 'month',
1308
			'count'    => 10,
1309
		);
1310
	}
1311
1312
	function get_date_histogram_posts_modified_by_year_filter() {
1313
		return array(
1314
			'type'     => 'date_histogram',
1315
			'field'    => 'post_modified',
1316
			'interval' => 'year',
1317
			'count'    => 10,
1318
		);
1319
	}
1320
1321
	function get_date_histogram_posts_by_month_gmt__filter() {
1322
		return array(
1323
			'type'     => 'date_histogram',
1324
			'field'    => 'post_date_gmt',
1325
			'interval' => 'month',
1326
			'count'    => 10,
1327
		);
1328
	}
1329
1330
	function get_date_histogram_posts_by_year_gmt__filter() {
1331
		return array(
1332
			'type'     => 'date_histogram',
1333
			'field'    => 'post_date_gmt',
1334
			'interval' => 'year',
1335
			'count'    => 10,
1336
		);
1337
	}
1338
1339
	function get_date_histogram_posts_modified_by_month_gmt_filter() {
1340
		return array(
1341
			'type'     => 'date_histogram',
1342
			'field'    => 'post_modified_gmt',
1343
			'interval' => 'month',
1344
			'count'    => 10,
1345
		);
1346
	}
1347
1348
	function get_date_histogram_posts_modified_by_year_gmt_filter() {
1349
		return array(
1350
			'type'     => 'date_histogram',
1351
			'field'    => 'post_modified_gmt',
1352
			'interval' => 'year',
1353
			'count'    => 10,
1354
		);
1355
	}
1356
}
1357