1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Integration Tests for Search syncing. |
4
|
|
|
* |
5
|
|
|
* @package automattic/jetpack-sync |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
// phpcs:disable Squiz.Commenting |
9
|
|
|
// phpcs:disable Generic.Commenting |
10
|
|
|
|
11
|
|
|
use Automattic\Jetpack\Sync\Modules; |
12
|
|
|
|
13
|
|
|
class Test_Jetpack_Sync_Search extends WP_Test_Jetpack_Sync_Base { |
14
|
|
|
protected $post_id; |
15
|
|
|
protected static $search_sync; |
16
|
|
|
|
17
|
|
|
public static function setUpBeforeClass() { |
18
|
|
|
parent::setUpBeforeClass(); |
19
|
|
|
|
20
|
|
|
self::$search_sync = Modules::get_module( 'search' ); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public function setUp() { |
24
|
|
|
parent::setUp(); |
25
|
|
|
|
26
|
|
|
// create a post. |
27
|
|
|
$this->post_id = $this->factory->post->create(); |
28
|
|
|
$this->sender->do_sync(); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function test_module_is_enabled() { |
32
|
|
|
$this->assertTrue( (bool) Modules::get_module( 'search' ) ); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Data Providers. |
37
|
|
|
*/ |
38
|
|
|
|
39
|
|
View Code Duplication |
public function get_allowed_postmeta_keys() { |
40
|
|
|
$search_sync = Modules::get_module( 'search' ); |
41
|
|
|
$params = array(); |
42
|
|
|
$keys = $search_sync->get_all_postmeta_keys(); |
43
|
|
|
foreach ( $keys as $k ) { |
44
|
|
|
$params[] = array( $k ); |
45
|
|
|
} |
46
|
|
|
return $params; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
View Code Duplication |
public function get_allowed_taxonomies() { |
50
|
|
|
$search_sync = Modules::get_module( 'search' ); |
51
|
|
|
$params = array(); |
52
|
|
|
$keys = $search_sync->get_all_taxonomies(); |
53
|
|
|
foreach ( $keys as $k ) { |
54
|
|
|
$params[] = array( $k ); |
55
|
|
|
} |
56
|
|
|
return $params; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Post meta tests. |
61
|
|
|
*/ |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @dataProvider get_allowed_postmeta_keys |
65
|
|
|
*/ |
66
|
|
|
public function test_add_post_meta( $key ) { |
67
|
|
|
$v = md5( wp_rand() ); |
68
|
|
|
add_post_meta( $this->post_id, $key, $v, true ); |
69
|
|
|
|
70
|
|
|
$this->sender->do_sync(); |
71
|
|
|
$this->assertEquals( |
72
|
|
|
array( $v ), |
73
|
|
|
$this->server_replica_storage->get_metadata( 'post', $this->post_id, $key ) |
74
|
|
|
); |
75
|
|
|
delete_post_meta( $this->post_id, $key, $v ); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function test_doesn_t_sync_meta() { |
79
|
|
|
add_post_meta( $this->post_id, 'no_sync_jetpack_search', 'foo' ); |
80
|
|
|
|
81
|
|
|
$this->sender->do_sync(); |
82
|
|
|
|
83
|
|
|
$this->assertEquals( |
84
|
|
|
array(), |
85
|
|
|
$this->server_replica_storage->get_metadata( 'post', $this->post_id, 'no_sync_jetpack_search' ) |
86
|
|
|
); |
87
|
|
|
delete_post_meta( $this->post_id, 'no_sync_jetpack_search', 'foo' ); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function test_meta_is_indexable() { |
91
|
|
|
$this->assertTrue( self::$search_sync->is_indexable( 'postmeta', 'jetpack-search-meta0' ) ); |
|
|
|
|
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function test_meta_is_not_indexable() { |
95
|
|
|
$this->assertFalse( self::$search_sync->is_indexable( 'postmeta', 'no_one_wants_to_index_me' ), 'no_one_wants_to_index_me' ); |
|
|
|
|
96
|
|
|
$this->assertFalse( self::$search_sync->is_indexable( 'postmeta', 'no_one_wants_to_index_me' ), '_no_one_wants_to_index_me' ); |
|
|
|
|
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function test_meta_no_overlap() { |
100
|
|
|
$indexed_keys = self::$search_sync->get_all_postmeta_keys(); |
|
|
|
|
101
|
|
|
asort( $indexed_keys ); |
102
|
|
|
$unindexed_keys = self::$search_sync->get_all_unindexed_postmeta_keys(); |
|
|
|
|
103
|
|
|
asort( $unindexed_keys ); |
104
|
|
|
$this->assertEmpty( array_intersect( $unindexed_keys, $indexed_keys ) ); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Important that we double check the specification format since |
109
|
|
|
* this will often get added to. |
110
|
|
|
* |
111
|
|
|
* @dataProvider get_allowed_postmeta_keys |
112
|
|
|
*/ |
113
|
|
|
public function test_check_postmeta_spec( $key ) { |
114
|
|
|
$spec = self::$search_sync->get_postmeta_spec( $key ); |
|
|
|
|
115
|
|
|
|
116
|
|
|
$this->assertIsArray( $spec ); |
117
|
|
|
$this->assertThat( |
118
|
|
|
$spec, |
119
|
|
|
$this->logicalOr( |
120
|
|
|
$this->isEmpty(), |
121
|
|
|
$this->arrayHasKey( 'searchable_in_all_content' ), |
122
|
|
|
$this->arrayHasKey( 'available' ), |
123
|
|
|
$this->arrayHasKey( 'alternatives' ) |
124
|
|
|
), |
125
|
|
|
'Post meta specification does not match.' |
126
|
|
|
); |
127
|
|
|
|
128
|
|
|
if ( isset( $spec['searchable_in_all_content'] ) ) { |
129
|
|
|
$this->assertIsBool( $spec['searchable_in_all_content'] ); |
130
|
|
|
} |
131
|
|
|
if ( isset( $spec['available'] ) ) { |
132
|
|
|
$this->assertIsBool( $spec['available'] ); |
133
|
|
|
} |
134
|
|
|
if ( isset( $spec['alternatives'] ) ) { |
135
|
|
|
$this->assertIsArray( $spec['alternatives'] ); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Custom Taxonomy Tests. |
141
|
|
|
*/ |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @dataProvider get_allowed_taxonomies |
145
|
|
|
*/ |
146
|
|
|
public function test_add_taxonomy( $taxonomy ) { |
147
|
|
|
register_taxonomy( |
148
|
|
|
$taxonomy, |
149
|
|
|
'post', |
150
|
|
|
array( |
151
|
|
|
'label' => __( 'Taxonomy Test', 'jetpack' ), |
152
|
|
|
'rewrite' => array( 'slug' => $taxonomy ), |
153
|
|
|
'hierarchical' => true, |
154
|
|
|
) |
155
|
|
|
); |
156
|
|
|
|
157
|
|
|
$term = md5( wp_rand() ); |
158
|
|
|
if ( 'post_format' === $taxonomy ) { |
159
|
|
|
// Special case in Core. |
160
|
|
|
$term = 'Standard'; |
161
|
|
|
} |
162
|
|
|
$term_obj = wp_insert_term( $term, $taxonomy ); |
163
|
|
|
wp_set_post_terms( $this->post_id, array( $term_obj['term_id'] ), $taxonomy, false ); |
164
|
|
|
$this->sender->do_sync(); |
165
|
|
|
|
166
|
|
|
// Check taxonomy and added term. |
167
|
|
|
$this->assertEquals( |
168
|
|
|
$this->get_terms( $taxonomy ), |
169
|
|
|
$this->server_replica_storage->get_terms( $taxonomy ), |
170
|
|
|
'Terms on cache site match do not match client site' |
171
|
|
|
); |
172
|
|
|
|
173
|
|
|
$this->assertEqualsObject( |
174
|
|
|
get_the_terms( $this->post_id, $taxonomy ), |
175
|
|
|
$this->server_replica_storage->get_the_terms( $this->post_id, $taxonomy ), |
176
|
|
|
'Adeded term does not match local term.' |
177
|
|
|
); |
178
|
|
|
|
179
|
|
|
// clean up - speeds up tests |
180
|
|
|
wp_remove_object_terms( $this->post_id, array( $term_obj['term_id'] ), $taxonomy ); |
181
|
|
|
unregister_taxonomy_for_object_type( $taxonomy, 'post' ); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
public function test_taxonomy_is_indexable() { |
185
|
|
|
$this->assertTrue( self::$search_sync->is_indexable( 'taxonomy', 'jetpack-search-tag0' ) ); |
|
|
|
|
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
public function test_taxonomy_is_not_indexable() { |
189
|
|
|
$this->assertFalse( self::$search_sync->is_indexable( 'taxonomy', 'no_one_wants_to_index_me' ) ); |
|
|
|
|
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Helpers |
194
|
|
|
*/ |
195
|
|
|
|
196
|
|
|
protected function get_terms( $taxonomy ) { |
197
|
|
|
$terms = get_terms( |
198
|
|
|
array( |
199
|
|
|
'taxonomy' => $taxonomy, |
200
|
|
|
'hide_empty' => false, |
201
|
|
|
) |
202
|
|
|
); |
203
|
|
|
// We need an array of stdClass rather than WP_Term. |
204
|
|
|
return array_map( |
205
|
|
|
function ( $object ) { |
206
|
|
|
return (object) (array) $object; }, |
207
|
|
|
$terms |
208
|
|
|
); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: