-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathtest-active-layer.php
317 lines (250 loc) · 12 KB
/
test-active-layer.php
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
<?php
class Ad_Layers_Active_Layer_Tests extends Ad_Layers_UnitTestCase {
protected $post_id, $page_id, $cat_id, $tag_id, $tax_id, $editor_id;
public function setUp() {
parent::setUp();
register_taxonomy( 'test-taxonomy', 'post' );
$this->editor_id = $this->factory->user->create( array( 'role' => 'editor' ) );
$this->cat_id = $this->factory->term->create( array( 'taxonomy' => 'category', 'name' => 'cat-a' ) );
$this->tag_id = $this->factory->term->create( array( 'taxonomy' => 'post_tag', 'name' => 'tag-b' ) );
$this->tax_id = $this->factory->term->create( array( 'taxonomy' => 'test-taxonomy', 'name' => 'tax-c' ) );
$this->post_id = $this->factory->post->create( array(
'post_title' => 'hello-world',
'post_author' => $this->editor_id,
'post_category' => array( $this->cat_id ),
'tax_input' => array( 'post_tag' => 'tag-b', 'test-taxonomy' => 'tax-c' ),
'post_date' => '2015-03-14 12:34:56',
) );
$this->page_id = $this->factory->post->create( array( 'post_title' => 'hello-page', 'post_type' => 'page' ) );
}
public function tearDown() {
self::delete_user( $this->editor_id );
parent::tearDown();
}
protected function build_and_get_layer( $args = array() ) {
$layer = $this->factory->post->create( array( 'post_type' => 'ad-layer' ) );
$args = wp_parse_args( $args, array(
'post_types' => array(),
'page_types' => array(),
'taxonomies' => array(),
) );
add_post_meta( $layer, 'ad_layer_post_types', (array) $args['post_types'] );
add_post_meta( $layer, 'ad_layer_page_types', (array) $args['page_types'] );
add_post_meta( $layer, 'ad_layer_taxonomies', (array) $args['taxonomies'] );
return $layer;
}
protected function get_active_ad_layer() {
$layers = get_option( 'ad_layers', array() );
usort( $layers, function( $a, $b ) {
if ( $a['post_id'] == $b['post_id'] ) {
return 0;
}
return ( $a['post_id'] < $b['post_id'] ) ? 1 : -1;
} );
update_option( 'ad_layers', $layers );
Ad_Layers::instance()->setup();
Ad_Layers::instance()->set_active_ad_layer();
$layer = Ad_Layers::instance()->ad_layer;
return ( ! empty( $layer['post_id'] ) ? $layer['post_id'] : false );
}
public function test_active_layer_failed() {
// Set this to something that won't match
$layer = $this->build_and_get_layer( array( 'page_types' => 'search' ) );
$this->go_to( '/' );
$this->assertTrue( is_home() );
$this->assertNotSame( $layer, $this->get_active_ad_layer() );
}
public function test_active_layer_home() {
$layer = $this->build_and_get_layer( array( 'page_types' => 'home' ) );
$this->go_to( '/' );
$this->assertTrue( is_home() );
$this->assertSame( $layer, $this->get_active_ad_layer() );
}
public function test_active_layer_single_post() {
$layer = $this->build_and_get_layer( array( 'page_types' => 'post' ) );
$this->go_to( get_permalink( $this->post_id ) );
$this->assertTrue( is_single() );
$this->assertSame( $layer, $this->get_active_ad_layer() );
}
public function test_active_layer_filter_override() {
$layer_override = $this->build_and_get_layer( array( 'page_type' => 'author' ) );
$layer_natural = $this->build_and_get_layer( array( 'page_type' => 'post' ) );
// verify that without an override, the post matches $layer_natural
$this->go_to( get_permalink( $this->post_id ) );
$this->assertTrue( is_single() );
$this->assertSame( $layer_natural, $this->get_active_ad_layer() );
$this->assertNotSame( $layer_override, $this->get_active_ad_layer() );
// now use the filter to override the natural match
add_filter( 'ad_layers_active_ad_layer', function() use ( $layer_override ) {
return array(
'post_id' => $layer_override,
'title' => get_the_title( $layer_override ),
);
} );
$this->assertSame( $layer_override, $this->get_active_ad_layer() );
}
public function test_active_layer_post_with_layer_override() {
$layer_override = $this->build_and_get_layer( array( 'page_type' => 'search' ) );
$layer_natural = $this->build_and_get_layer( array( 'page_type' => 'post' ) );
// verify that without an override, the post matches $layer_natural
$this->go_to( get_permalink( $this->post_id ) );
$this->assertTrue( is_single() );
$this->assertSame( $layer_natural, $this->get_active_ad_layer() );
$this->assertNotSame( $layer_override, $this->get_active_ad_layer() );
// now use the post meta to override the natural match
add_post_meta( $this->post_id, 'ad_layer', $layer_override );
$this->assertSame( $layer_override, $this->get_active_ad_layer() );
}
public function test_active_layer_post_by_terms() {
$layer = $this->build_and_get_layer();
wp_set_object_terms( $layer, $this->cat_id, 'category' );
$this->go_to( get_permalink( $this->post_id ) );
$this->assertTrue( is_single() );
$this->assertSame( $layer, $this->get_active_ad_layer() );
}
public function test_active_layer_single_page() {
$layer = $this->build_and_get_layer( array( 'page_types' => 'page' ) );
$this->go_to( get_permalink( $this->page_id ) );
$this->assertTrue( is_page() );
$this->assertSame( $layer, $this->get_active_ad_layer() );
}
public function test_active_layer_category() {
$layer = $this->build_and_get_layer( array( 'page_types' => 'category' ) );
$this->go_to( get_category_link( $this->cat_id ) );
$this->assertTrue( is_category() );
$this->assertSame( $layer, $this->get_active_ad_layer() );
}
public function test_active_layer_taxonomies() {
$layer = $this->build_and_get_layer( array( 'taxonomies' => 'category' ) );
$this->go_to( get_category_link( $this->cat_id ) );
$this->assertTrue( is_category() );
$this->assertSame( $layer, $this->get_active_ad_layer() );
}
public function test_active_layer_terms() {
$layer = $this->build_and_get_layer();
wp_set_object_terms( $layer, $this->cat_id, 'category' );
$this->go_to( get_category_link( $this->cat_id ) );
$this->assertTrue( is_category() );
$this->assertSame( $layer, $this->get_active_ad_layer() );
}
public function test_active_layer_tag() {
$layer = $this->build_and_get_layer( array( 'page_types' => 'post_tag' ) );
$this->go_to( get_tag_link( $this->tag_id ) );
$this->assertTrue( is_tag() );
$this->assertSame( $layer, $this->get_active_ad_layer() );
}
public function test_active_layer_custom_taxonomy() {
$layer = $this->build_and_get_layer( array( 'page_types' => 'test-taxonomy' ) );
$this->go_to( get_term_link( $this->tax_id, 'test-taxonomy' ) );
$this->assertTrue( is_tax() );
$this->assertSame( $layer, $this->get_active_ad_layer() );
}
public function test_active_layer_author() {
$layer = $this->build_and_get_layer( array( 'page_types' => 'author' ) );
$this->go_to( get_author_posts_url( $this->editor_id ) );
$this->assertTrue( is_author() );
$this->assertSame( $layer, $this->get_active_ad_layer() );
}
public function test_active_layer_date() {
$layer = $this->build_and_get_layer( array( 'page_types' => 'date' ) );
$this->go_to( get_year_link( '2015' ) );
$this->assertTrue( is_year() );
$this->assertSame( $layer, $this->get_active_ad_layer() );
$this->go_to( get_month_link( '2015', '03' ) );
$this->assertTrue( is_month() );
$this->assertSame( $layer, $this->get_active_ad_layer() );
$this->go_to( get_day_link( '2015', '03', '14' ) );
$this->assertTrue( is_day() );
$this->assertSame( $layer, $this->get_active_ad_layer() );
$this->go_to( '/' );
$this->assertNotSame( $layer, $this->get_active_ad_layer() );
}
public function test_active_layer_notfound() {
$layer = $this->build_and_get_layer( array( 'page_types' => 'notfound' ) );
$this->go_to( '?name=' . rand_str() );
$this->assertTrue( is_404() );
$this->assertSame( $layer, $this->get_active_ad_layer() );
$this->go_to( get_year_link( '2025' ) );
$this->assertTrue( is_404() );
$this->assertSame( $layer, $this->get_active_ad_layer() );
$this->go_to( '/' );
$this->assertNotSame( $layer, $this->get_active_ad_layer() );
}
public function test_active_layer_search() {
$layer = $this->build_and_get_layer( array( 'page_types' => 'search' ) );
$this->go_to( '?s=hello' );
$this->assertTrue( is_search() );
$this->assertSame( $layer, $this->get_active_ad_layer() );
}
public function test_active_layer_cpt_without_archive() {
$post_type = rand_str( 20 );
register_post_type( $post_type, array( 'public' => true ) );
$cpt_id = $this->factory->post->create( array( 'post_title' => 'hello-cpt', 'post_type' => $post_type ) );
$layer = $this->build_and_get_layer( array( 'page_types' => $post_type ) );
$this->go_to( get_permalink( $cpt_id ) );
$this->assertTrue( is_singular( $post_type ) );
$this->assertSame( $layer, $this->get_active_ad_layer() );
}
public function test_active_layer_cpt_with_archive() {
$post_type = rand_str( 20 );
register_post_type( $post_type, array( 'public' => true, 'has_archive' => true ) );
$cpt_id = $this->factory->post->create( array( 'post_title' => 'hello-cpt', 'post_type' => $post_type ) );
$layer_single = $this->build_and_get_layer( array( 'page_types' => $post_type ) );
$layer_archive = $this->build_and_get_layer( array( 'page_types' => 'archive::' . $post_type ) );
// Singular view
$this->go_to( get_permalink( $cpt_id ) );
$this->assertTrue( is_singular( $post_type ) );
$this->assertSame( $layer_single, $this->get_active_ad_layer() );
// Archive view
$this->go_to( get_post_type_archive_link( $post_type ) );
$this->assertTrue( is_post_type_archive( $post_type ) );
$this->assertSame( $layer_archive, $this->get_active_ad_layer() );
}
public function test_active_layer_post_types() {
$layer = $this->build_and_get_layer( array( 'post_types' => 'page' ) );
// Ensure that a singular post fails
$this->go_to( get_permalink( $this->post_id ) );
$this->assertTrue( is_single() );
$this->assertNotSame( $layer, $this->get_active_ad_layer() );
// ... but that a singular page passes
$this->go_to( get_permalink( $this->page_id ) );
$this->assertTrue( is_page() );
$this->assertSame( $layer, $this->get_active_ad_layer() );
}
public function test_active_layer_post_types_by_archive() {
$post_type_1 = rand_str( 20 );
$post_type_2 = rand_str( 20 );
register_post_type( $post_type_1, array( 'public' => true, 'has_archive' => true ) );
register_post_type( $post_type_2, array( 'public' => true, 'has_archive' => true ) );
$cpt_1 = $this->factory->post->create( array( 'post_title' => 'hello-cpt-1', 'post_type' => $post_type_1 ) );
$cpt_2 = $this->factory->post->create( array( 'post_title' => 'hello-cpt-2', 'post_type' => $post_type_2 ) );
$layer_1 = $this->build_and_get_layer( array( 'post_types' => $post_type_1 ) );
$layer_2 = $this->build_and_get_layer( array( 'post_types' => $post_type_2 ) );
// Singular view
$this->go_to( get_permalink( $cpt_1 ) );
$this->assertTrue( is_singular( $post_type_1 ) );
$this->assertSame( $layer_1, $this->get_active_ad_layer() );
$this->go_to( get_permalink( $cpt_2 ) );
$this->assertTrue( is_singular( $post_type_2 ) );
$this->assertSame( $layer_2, $this->get_active_ad_layer() );
// Archive view
$this->go_to( get_post_type_archive_link( $post_type_1 ) );
$this->assertTrue( is_post_type_archive( $post_type_1 ) );
$this->assertSame( $layer_1, $this->get_active_ad_layer() );
$this->go_to( get_post_type_archive_link( $post_type_2 ) );
$this->assertTrue( is_post_type_archive( $post_type_2 ) );
$this->assertSame( $layer_2, $this->get_active_ad_layer() );
}
public function test_active_layer_term_override() {
$layer_override = $this->build_and_get_layer( array( 'page_types' => 'test-taxonomy' ) );
$layer = $this->build_and_get_layer( array( 'page_types' => 'post_tag' ) );
$this->go_to( get_tag_link( $this->tag_id ) );
$this->assertTrue( is_tag() );
// the tag level ad-layer reflects here as active ad-layer.
$this->assertSame( $layer, $this->get_active_ad_layer() );
// After adding ad_layer to the term, the ad_layer for that term overrides the tag level ad-layer.
add_term_meta( $this->tag_id, 'ad_layer', $layer_override );
$this->assertNotSame( $layer, $this->get_active_ad_layer() );
$this->assertSame( $layer_override, $this->get_active_ad_layer() );
}
}