-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTestCoAuthorsPlus.php
More file actions
287 lines (235 loc) · 8.41 KB
/
Copy pathTestCoAuthorsPlus.php
File metadata and controls
287 lines (235 loc) · 8.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
<?php
/**
* Test Co-Authors Plus feature
*
* @since 1.1.0
* @package ElasticPressLabs
*/
namespace ElasticPressLabsTest;
use ElasticPressLabs;
use ElasticPress;
/**
* CoAuthors Plus test class
*
* @since 1.1.0
*/
class TestCoAuthorsPlus extends BaseTestCase {
/**
* Setup each test.
*
* @since 1.1.0
*/
public function set_up() {
parent::set_up();
ElasticPress\Elasticsearch::factory()->delete_all_indices();
ElasticPress\Indexables::factory()->get( 'post' )->put_mapping();
ElasticPress\Indexables::factory()->get( 'post' )->sync_manager->reset_sync_queue();
$instance = new ElasticPressLabs\Feature\CoAuthorsPlus();
\ElasticPress\Features::factory()->register_feature( $instance );
}
/**
* Get Co-Authors Plus feature
*
* @since 1.1.0
* @return CoAuthorsPlus
*/
protected function get_feature() {
return \ElasticPress\Features::factory()->get_registered_feature( 'co_authors_plus' );
}
/**
* Get protected function as public
*
* @since 1.1.0
* @param string $function_name Function name
* @param string $class_name Class name
* @return ReflectionClass
*/
protected function get_protected_function( $function_name, $class_name = 'ElasticPressLabs\Feature\CoAuthorsPlus' ) {
$reflector = new \ReflectionClass( $class_name );
$function = $reflector->getMethod( $function_name );
$function->setAccessible( true );
return $function;
}
/**
* Test construct
*
* @since 1.1.0
*/
public function testConstruct() {
$instance = $this->get_feature();
$instance->set_i18n_strings();
$this->assertEquals( 'co_authors_plus', $instance->slug );
$this->assertEquals( 'Co-Authors Plus', $instance->title );
}
/**
* Test Protected Content status is checked during requirements validation.
*
* @since 2.5.2
*/
public function test_protected_content_status_is_checked_during_requirements_validation() {
ElasticPress\Features::factory()->activate_feature( 'protected_content' );
$instance = new class() extends ElasticPressLabs\Feature\CoAuthorsPlus {
/**
* Return the protected content feature state.
*
* @return bool
*/
public function is_protected_content_feature_active() {
return $this->is_protected_content_feature_active;
}
};
$this->assertFalse( $instance->is_protected_content_feature_active() );
$instance->requirements_status();
$this->assertTrue( $instance->is_protected_content_feature_active() );
}
/**
* Test box summary
*
* @since 1.1.0
*/
public function testBoxSummary() {
ob_start();
$this->get_feature()->output_feature_box_summary();
$output = ob_get_clean();
$this->assertStringContainsString( 'Add support for the Co-Authors Plus plugin in the Admin Post List screen by Author name', $output );
}
/**
* Test filter out author name and id from Elasticsearch query
*
* @since 1.1.0
*/
public function testFilterOutAuthorNameAndId() {
$feature = $this->get_feature();
$function_filter_out_author_name_and_id_from_es_filter = $this->get_protected_function( 'filter_out_author_name_and_id_from_es_filter' );
$this->assertEquals( [], $function_filter_out_author_name_and_id_from_es_filter->invokeArgs( $feature, array( [] ) ) );
$this->assertEquals( '', $function_filter_out_author_name_and_id_from_es_filter->invokeArgs( $feature, array( '' ) ) );
$formatted_args = [
'post_filter' => [
'bool' => [
'must' => [
[
'term' => [
'post_author.display_name' => [ 'test' ],
],
],
],
],
],
];
$filtered_formatted_args = $function_filter_out_author_name_and_id_from_es_filter->invokeArgs( $feature, [ $formatted_args ] );
$this->assertEmpty( $filtered_formatted_args );
$formatted_args['post_filter']['bool']['must'][] = [
'terms' => [
'post_type.raw' => [ 'post' ],
],
];
$filtered_formatted_args = $function_filter_out_author_name_and_id_from_es_filter->invokeArgs( $feature, [ $formatted_args ] );
$this->assertNotEmpty( $filtered_formatted_args );
$this->assertCount( 1, $filtered_formatted_args );
$this->assertArrayHasKey( 'terms', $filtered_formatted_args[0] );
$formatted_args['post_filter']['bool']['must'][] = [
'term' => [
'post_author.id' => [ 1 ],
],
];
$filtered_formatted_args = $function_filter_out_author_name_and_id_from_es_filter->invokeArgs( $feature, [ $formatted_args ] );
$this->assertNotEmpty( $filtered_formatted_args );
$this->assertCount( 1, $filtered_formatted_args );
$this->assertArrayHasKey( 'terms', $filtered_formatted_args[0] );
}
/**
* Test settings schema.
*
* @since 2.5.0
*/
public function test_settings_schema() {
$expected = [
[
'default' => false,
'key' => 'active',
'label' => 'Enable',
'requires_feature' => [ 'search' ],
'requires_sync' => true,
'type' => 'toggle',
],
[
'key' => 'instructions',
'label' => '<p>When enabled, this feature integrates ElasticPress with Co-Authors Plus to enhance author-related queries on the frontend. If "Protected Content" is activated, visit the Admin Post List screen by Author name <code>wp-admin/edit.php?author_name=<name></code> and see correct results.</p>',
'type' => 'markup',
],
];
$this->assertSame( $expected, $this->get_feature()->get_settings_schema() );
}
/**
* Test attribute add in weight dashboard.
*
* @since 2.5.0
*/
public function test_attribute_add_in_weight_dashboard() {
ElasticPress\Features::factory()->activate_feature( 'co_authors_plus' );
ElasticPress\Features::factory()->get_registered_feature( 'co_authors_plus' )->setup();
$search = ElasticPress\Features::factory()->get_registered_feature( 'search' );
$fields = $search->weighting->get_weightable_fields_for_post_type( 'post' );
$this->assertArrayHasKey( 'terms.author.name', $fields['attributes']['children'] );
$this->assertEquals( 'Guest Author', $fields['attributes']['children']['terms.author.name']['label'] );
$this->assertEquals( 'terms.author.name', $fields['attributes']['children']['terms.author.name']['key'] );
}
/**
* Test add author default weight.
*
* @since 2.5.0
*/
public function test_add_author_default_weight() {
ElasticPress\Features::factory()->activate_feature( 'co_authors_plus' );
ElasticPress\Features::factory()->get_registered_feature( 'co_authors_plus' )->setup();
$search = ElasticPress\Features::factory()->get_registered_feature( 'search' );
$fields = $search->weighting->get_post_type_default_settings( 'post' );
$this->assertArrayHasKey( 'terms.author.name', $fields );
$this->assertEquals( 1, $fields['terms.author.name']['weight'] );
$this->assertTrue( $fields['terms.author.name']['enabled'] );
}
/**
* Test search query returns the posts if search query is a co-author.
*
* @since 2.5.0
*/
public function test_search_query_with_co_authors_plus() {
global $coauthors_plus;
ElasticPress\Features::factory()->activate_feature( 'co_authors_plus' );
ElasticPress\Features::factory()->get_registered_feature( 'co_authors_plus' )->setup();
$post_id = $this->ep_factory->post->create();
$user_login = 'guest-author';
$user_display_name = 'Guest Author';
$coauthors_plus->guest_authors->create(
[
'display_name' => $user_display_name,
'user_login' => $user_login,
]
);
$coauthors_plus->add_coauthors( $post_id, [ $user_login ], true, 'user_login' );
ElasticPress\Features::factory()->get_registered_feature( 'search' );
ElasticPress\Indexables::factory()->get( 'post' )->index( $post_id, true );
ElasticPress\Elasticsearch::factory()->refresh_indices();
$query = new \WP_Query(
[
's' => $user_display_name,
]
);
$this->assertTrue( $query->elasticsearch_success );
$this->assertEquals( 1, $query->found_posts );
$this->assertEquals( $post_id, $query->posts[0]->ID );
}
/**
* Test ep_coauthors_plus_skip_frontend_integration filter removes author weighting.
*
* @since 2.5.0
*/
public function test_ep_coauthors_plus_skip_frontend_integration() {
add_filter( 'ep_coauthors_plus_skip_frontend_integration', '__return_true' );
ElasticPress\Features::factory()->activate_feature( 'co_authors_plus' );
ElasticPress\Features::factory()->get_registered_feature( 'co_authors_plus' )->setup();
$search = ElasticPress\Features::factory()->get_registered_feature( 'search' );
$fields = $search->weighting->get_post_type_default_settings( 'post' );
$this->assertArrayNotHasKey( 'terms.author.name', $fields );
}
}