Completed
Pull Request — master (#2)
by Stephen
13:19
created

Tests_Query_Conditionals::test_y_feed()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 10
nc 4
nop 0
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Test the is_*() functions in query.php across the URL structure
5
 *
6
 * This exercises both query.php and rewrite.php: urls are fed through the rewrite code,
7
 * then we test the effects of each url on the wp_query object.
8
 *
9
 * @group query
10
 * @group rewrite
11
 */
12
class Tests_Query_Conditionals extends WP_UnitTestCase {
13
14
	protected $page_ids;
15
	protected $post_ids;
16
17
	function setUp() {
18
		parent::setUp();
19
20
		set_current_screen( 'front' );
21
22
		update_option( 'comments_per_page', 5 );
23
		update_option( 'posts_per_page', 5 );
24
25
		$this->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' );
26
27
		create_initial_taxonomies();
28
	}
29
30
	function test_home() {
31
		$this->go_to('/');
32
		$this->assertQueryTrue( 'is_home', 'is_front_page' );
33
	}
34
35
	function test_page_on_front() {
36
		$page_on_front = self::factory()->post->create( array(
37
			'post_type' => 'page',
38
		) );
39
		$page_for_posts = self::factory()->post->create( array(
40
			'post_type' => 'page',
41
		) );
42
		update_option( 'show_on_front', 'page' );
43
		update_option( 'page_on_front', $page_on_front );
44
		update_option( 'page_for_posts', $page_for_posts );
45
46
		$this->go_to( '/' );
47
		$this->assertQueryTrue( 'is_front_page', 'is_page', 'is_singular' );
48
49
		$this->go_to( get_permalink( $page_for_posts ) );
50
		$this->assertQueryTrue( 'is_home', 'is_posts_page' );
51
52
		update_option( 'show_on_front', 'posts' );
53
		delete_option( 'page_on_front' );
54
		delete_option( 'page_for_posts' );
55
	}
56
57
	function test_404() {
58
		$this->go_to( '/notapage' );
59
		$this->assertQueryTrue('is_404');
60
	}
61
62
	function test_permalink() {
63
		$post_id = self::factory()->post->create( array( 'post_title' => 'hello-world' ) );
64
		$this->go_to( get_permalink( $post_id ) );
65
		$this->assertQueryTrue('is_single', 'is_singular');
66
	}
67
68
	function test_post_comments_feed() {
69
		$post_id = self::factory()->post->create( array( 'post_title' => 'hello-world' ) );
70
		self::factory()->comment->create_post_comments( $post_id, 2 );
71
		$this->go_to( get_post_comments_feed_link( $post_id ) );
72
		$this->assertQueryTrue('is_feed', 'is_single', 'is_singular', 'is_comment_feed');
73
	}
74
75
76
	function test_post_comments_feed_with_no_comments() {
77
		$post_id = self::factory()->post->create( array( 'post_title' => 'hello-world' ) );
78
		$this->go_to( get_post_comments_feed_link( $post_id ) );
79
		$this->assertQueryTrue('is_feed', 'is_single', 'is_singular', 'is_comment_feed');
80
	}
81
82
	function test_attachment_comments_feed() {
83
		$attachment_id = self::factory()->post->create( array( 'post_type' => 'attachment' ) );
84
		self::factory()->comment->create_post_comments( $attachment_id, 2 );
85
		$this->go_to( get_post_comments_feed_link( $attachment_id ) );
86
		$this->assertQueryTrue( 'is_feed', 'is_attachment', 'is_single', 'is_singular', 'is_comment_feed' );
87
	}
88
89
	function test_page() {
90
		$page_id = self::factory()->post->create( array( 'post_type' => 'page', 'post_title' => 'about' ) );
91
		$this->go_to( get_permalink( $page_id ) );
92
		$this->assertQueryTrue('is_page','is_singular');
93
	}
94
95
	function test_parent_page() {
96
		$page_id = self::factory()->post->create( array( 'post_type' => 'page', 'post_title' => 'parent-page' ) );
97
		$this->go_to( get_permalink( $page_id ) );
98
99
		$this->assertQueryTrue('is_page','is_singular');
100
	}
101
102
	function test_child_page_1() {
103
		$page_id = self::factory()->post->create( array( 'post_type' => 'page', 'post_title' => 'parent-page' ) );
104
		$page_id = self::factory()->post->create( array( 'post_type' => 'page', 'post_title' => 'child-page-1', 'post_parent' => $page_id ) );
105
		$this->go_to( get_permalink( $page_id ) );
106
107
		$this->assertQueryTrue('is_page','is_singular');
108
	}
109
110
	function test_child_page_2() {
111
		$page_id = self::factory()->post->create( array( 'post_type' => 'page', 'post_title' => 'parent-page' ) );
112
		$page_id = self::factory()->post->create( array( 'post_type' => 'page', 'post_title' => 'child-page-1', 'post_parent' => $page_id ) );
113
		$page_id = self::factory()->post->create( array( 'post_type' => 'page', 'post_title' => 'child-page-2', 'post_parent' => $page_id ) );
114
		$this->go_to( get_permalink( $page_id ) );
115
116
		$this->assertQueryTrue('is_page','is_singular');
117
	}
118
119
	// '(about)/trackback/?$' => 'index.php?pagename=$matches[1]&tb=1'
120
	function test_page_trackback() {
121
		$page_ids = array();
122
		$page_ids[] = $page_id = self::factory()->post->create( array( 'post_type' => 'page', 'post_title' => 'parent-page' ) );
123
		$page_ids[] = $page_id = self::factory()->post->create( array( 'post_type' => 'page', 'post_title' => 'child-page-1', 'post_parent' => $page_id ) );
124
		$page_ids[] = $page_id = self::factory()->post->create( array( 'post_type' => 'page', 'post_title' => 'child-page-2', 'post_parent' => $page_id ) );
125
		foreach ( $page_ids as $page_id ) {
126
			$url = get_permalink( $page_id );
127
			$this->go_to("{$url}trackback/");
128
129
			// make sure the correct wp_query flags are set
130
			$this->assertQueryTrue('is_page','is_singular','is_trackback');
131
132
			// make sure the correct page was fetched
133
			global $wp_query;
134
			$this->assertEquals( $page_id, $wp_query->get_queried_object()->ID );
135
		}
136
	}
137
138
	//'(about)/feed/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?pagename=$matches[1]&feed=$matches[2]'
139
	function test_page_feed() {
140
		$page_ids = array();
141
		$page_ids[] = $page_id = self::factory()->post->create( array( 'post_type' => 'page', 'post_title' => 'parent-page' ) );
142
		$page_ids[] = $page_id = self::factory()->post->create( array( 'post_type' => 'page', 'post_title' => 'child-page-1', 'post_parent' => $page_id ) );
143
		$page_ids[] = $page_id = self::factory()->post->create( array( 'post_type' => 'page', 'post_title' => 'child-page-2', 'post_parent' => $page_id ) );
144
		foreach ( $page_ids as $page_id ) {
145
			self::factory()->comment->create_post_comments( $page_id, 2 );
146
			$url = get_permalink( $page_id );
147
			$this->go_to("{$url}feed/");
148
149
			// make sure the correct wp_query flags are set
150
			$this->assertQueryTrue('is_page', 'is_singular', 'is_feed', 'is_comment_feed');
151
152
			// make sure the correct page was fetched
153
			global $wp_query;
154
			$this->assertEquals( $page_id, $wp_query->get_queried_object()->ID );
155
		}
156
	}
157
158
	function test_page_feed_with_no_comments() {
159
		$page_ids = array();
160
		$page_ids[] = $page_id = self::factory()->post->create( array( 'post_type' => 'page', 'post_title' => 'parent-page' ) );
161
		$page_ids[] = $page_id = self::factory()->post->create( array( 'post_type' => 'page', 'post_title' => 'child-page-1', 'post_parent' => $page_id ) );
162
		$page_ids[] = $page_id = self::factory()->post->create( array( 'post_type' => 'page', 'post_title' => 'child-page-2', 'post_parent' => $page_id ) );
163
		foreach ( $page_ids as $page_id ) {
164
			$url = get_permalink( $page_id );
165
			$this->go_to("{$url}feed/");
166
167
			// make sure the correct wp_query flags are set
168
			$this->assertQueryTrue('is_page', 'is_singular', 'is_feed', 'is_comment_feed');
169
170
			// make sure the correct page was fetched
171
			global $wp_query;
172
			$this->assertEquals( $page_id, $wp_query->get_queried_object()->ID );
173
		}
174
	}
175
176
	// '(about)/feed/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?pagename=$matches[1]&feed=$matches[2]'
177
	function test_page_feed_atom() {
178
		$page_ids = array();
179
		$page_ids[] = $page_id = self::factory()->post->create( array( 'post_type' => 'page', 'post_title' => 'parent-page' ) );
180
		$page_ids[] = $page_id = self::factory()->post->create( array( 'post_type' => 'page', 'post_title' => 'child-page-1', 'post_parent' => $page_id ) );
181
		$page_ids[] = $page_id = self::factory()->post->create( array( 'post_type' => 'page', 'post_title' => 'child-page-2', 'post_parent' => $page_id ) );
182
		foreach ( $page_ids as $page_id ) {
183
			self::factory()->comment->create_post_comments( $page_id, 2 );
184
185
			$url = get_permalink( $page_id );
186
			$this->go_to("{$url}feed/atom/");
187
188
			// make sure the correct wp_query flags are set
189
			$this->assertQueryTrue('is_page', 'is_singular', 'is_feed', 'is_comment_feed');
190
191
			// make sure the correct page was fetched
192
			global $wp_query;
193
			$this->assertEquals( $page_id, $wp_query->get_queried_object()->ID );
194
		}
195
	}
196
197
	// '(about)/page/?([0-9]{1,})/?$' => 'index.php?pagename=$matches[1]&paged=$matches[2]'
198
	function test_page_page_2() {
199
		$page_id = self::factory()->post->create( array( 'post_type' => 'page', 'post_title' => 'about', 'post_content' => 'Page 1 <!--nextpage--> Page 2' ) );
200
		$this->go_to("/about/page/2/");
201
202
		// make sure the correct wp_query flags are set
203
		$this->assertQueryTrue('is_page', 'is_singular', 'is_paged');
204
205
		// make sure the correct page was fetched
206
		global $wp_query;
207
		$this->assertEquals( $page_id, $wp_query->get_queried_object()->ID );
208
	}
209
210
	// '(about)/page/?([0-9]{1,})/?$' => 'index.php?pagename=$matches[1]&paged=$matches[2]'
211
	function test_page_page_2_no_slash() {
212
		$page_id = self::factory()->post->create( array( 'post_type' => 'page', 'post_title' => 'about', 'post_content' => 'Page 1 <!--nextpage--> Page 2' ) );
213
		$this->go_to("/about/page2/");
214
215
		// make sure the correct wp_query flags are set
216
		$this->assertQueryTrue('is_page', 'is_singular', 'is_paged');
217
218
		// make sure the correct page was fetched
219
		global $wp_query;
220
		$this->assertEquals( $page_id, $wp_query->get_queried_object()->ID );
221
	}
222
223
	// '(about)(/[0-9]+)?/?$' => 'index.php?pagename=$matches[1]&page=$matches[2]'
224
	function test_pagination_of_posts_page() {
225
		$page_id = self::factory()->post->create( array( 'post_type' => 'page', 'post_title' => 'about', 'post_content' => 'Page 1 <!--nextpage--> Page 2' ) );
226
		update_option( 'show_on_front', 'page' );
227
		update_option( 'page_for_posts', $page_id );
228
229
		$this->go_to('/about/2/');
230
231
		$this->assertQueryTrue( 'is_home', 'is_posts_page' );
232
233
		// make sure the correct page was fetched
234
		global $wp_query;
235
		$this->assertEquals( $page_id, $wp_query->get_queried_object()->ID );
236
237
		update_option( 'show_on_front', 'posts' );
238
		delete_option( 'page_for_posts' );
239
	}
240
241
	// FIXME: no tests for these yet
242
	// 'about/attachment/([^/]+)/?$' => 'index.php?attachment=$matches[1]',
243
	// 'about/attachment/([^/]+)/trackback/?$' => 'index.php?attachment=$matches[1]&tb=1',
244
	// 'about/attachment/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?attachment=$matches[1]&feed=$matches[2]',
245
	// 'about/attachment/([^/]+)/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?attachment=$matches[1]&feed=$matches[2]',
246
247
	// 'feed/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?&feed=$matches[1]',
248
	// '(feed|rdf|rss|rss2|atom)/?$' => 'index.php?&feed=$matches[1]',
249
	function test_main_feed_2() {
250
		self::factory()->post->create(); // @test_404
251
		$feeds = array('feed', 'rdf', 'rss', 'rss2', 'atom');
252
253
		// long version
254
		foreach ($feeds as $feed) {
255
			$this->go_to("/feed/{$feed}/");
256
			$this->assertQueryTrue('is_feed');
257
		}
258
259
		// short version
260
		foreach ($feeds as $feed) {
261
			$this->go_to("/{$feed}/");
262
			$this->assertQueryTrue('is_feed');
263
		}
264
265
	}
266
267
	function test_main_feed() {
268
		self::factory()->post->create(); // @test_404
269
		$types = array('rss2', 'rss', 'atom');
270
		foreach ($types as $type) {
271
			$this->go_to(get_feed_link($type));
272
			$this->assertQueryTrue('is_feed');
273
		}
274
	}
275
276
	// 'page/?([0-9]{1,})/?$' => 'index.php?&paged=$matches[1]',
277
	function test_paged() {
278
		update_option( 'posts_per_page', 2 );
279
		self::factory()->post->create_many( 5 );
280
		for ( $i = 2; $i <= 3; $i++ ) {
281
			$this->go_to("/page/{$i}/");
282
			$this->assertQueryTrue( 'is_home', 'is_front_page', 'is_paged' );
283
		}
284
	}
285
286
	// 'comments/feed/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?&feed=$matches[1]&withcomments=1',
287
	// 'comments/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?&feed=$matches[1]&withcomments=1',
288
	function test_main_comments_feed() {
289
		$post_id = self::factory()->post->create( array( 'post_title' => 'hello-world' ) );
290
		self::factory()->comment->create_post_comments( $post_id, 2 );
291
292
		// check the url as generated by get_post_comments_feed_link()
293
		$this->go_to( get_post_comments_feed_link( $post_id ) );
294
		$this->assertQueryTrue('is_feed', 'is_single', 'is_singular', 'is_comment_feed');
295
296
		// check the long form
297
		$types = array('feed', 'rdf', 'rss', 'rss2', 'atom');
298
		foreach ($types as $type) {
299
				$this->go_to("/comments/feed/{$type}");
300
				$this->assertQueryTrue('is_feed', 'is_comment_feed');
301
		}
302
303
		// check the short form
304
		$types = array('feed', 'rdf', 'rss', 'rss2', 'atom');
305
		foreach ($types as $type) {
306
				$this->go_to("/comments/{$type}");
307
				$this->assertQueryTrue('is_feed', 'is_comment_feed');
308
		}
309
310
	}
311
312
	// 'search/(.+)/feed/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?s=$matches[1]&feed=$matches[2]',
313
	// 'search/(.+)/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?s=$matches[1]&feed=$matches[2]',
314
	function test_search_feed() {
315
		// check the long form
316
		$types = array('feed', 'rdf', 'rss', 'rss2', 'atom');
317
		foreach ($types as $type) {
318
				$this->go_to("/search/test/feed/{$type}");
319
				$this->assertQueryTrue('is_feed', 'is_search');
320
		}
321
322
		// check the short form
323
		$types = array('feed', 'rdf', 'rss', 'rss2', 'atom');
324
		foreach ($types as $type) {
325
				$this->go_to("/search/test/{$type}");
326
				$this->assertQueryTrue('is_feed', 'is_search');
327
		}
328
	}
329
330
	// 'search/(.+)/page/?([0-9]{1,})/?$' => 'index.php?s=$matches[1]&paged=$matches[2]',
331
	function test_search_paged() {
332
		update_option( 'posts_per_page', 2 );
333
		self::factory()->post->create_many( 3, array( 'post_title' => 'test' ) );
334
		$this->go_to('/search/test/page/2/');
335
		$this->assertQueryTrue('is_search', 'is_paged');
336
	}
337
338
	// 'search/(.+)/?$' => 'index.php?s=$matches[1]',
339
	function test_search() {
340
		$this->go_to('/search/test/');
341
		$this->assertQueryTrue('is_search');
342
	}
343
344
	/**
345
	 * @ticket 13961
346
	 */
347
	function test_search_encoded_chars() {
348
		$this->go_to('/search/F%C3%BCnf%2Bbar/');
349
		$this->assertEquals( get_query_var( 's' ), 'Fünf+bar' );
350
	}
351
352
	// 'category/(.+?)/feed/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?category_name=$matches[1]&feed=$matches[2]',
353
	// 'category/(.+?)/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?category_name=$matches[1]&feed=$matches[2]',
354
	function test_category_feed() {
355
		self::factory()->term->create( array( 'name' => 'cat-a', 'taxonomy' => 'category' ) );
356
357
		// check the long form
358
		$types = array('feed', 'rdf', 'rss', 'rss2', 'atom');
359
		foreach ($types as $type) {
360
			$this->go_to("/category/cat-a/feed/{$type}");
361
			$this->assertQueryTrue('is_archive', 'is_feed', 'is_category');
362
		}
363
364
		// check the short form
365
		$types = array('feed', 'rdf', 'rss', 'rss2', 'atom');
366
		foreach ($types as $type) {
367
			$this->go_to("/category/cat-a/{$type}");
368
			$this->assertQueryTrue('is_archive', 'is_feed', 'is_category');
369
		}
370
	}
371
372
	// 'category/(.+?)/page/?([0-9]{1,})/?$' => 'index.php?category_name=$matches[1]&paged=$matches[2]',
373
	function test_category_paged() {
374
		update_option( 'posts_per_page', 2 );
375
		self::factory()->post->create_many( 3 );
376
		$this->go_to('/category/uncategorized/page/2/');
377
		$this->assertQueryTrue('is_archive', 'is_category', 'is_paged');
378
	}
379
380
	// 'category/(.+?)/?$' => 'index.php?category_name=$matches[1]',
381
	function test_category() {
382
		self::factory()->term->create( array( 'name' => 'cat-a', 'taxonomy' => 'category' ) );
383
		$this->go_to('/category/cat-a/');
384
		$this->assertQueryTrue('is_archive', 'is_category');
385
	}
386
387
	// 'tag/(.+?)/feed/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?tag=$matches[1]&feed=$matches[2]',
388
	// 'tag/(.+?)/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?tag=$matches[1]&feed=$matches[2]',
389
	function test_tag_feed() {
390
		self::factory()->term->create( array( 'name' => 'tag-a', 'taxonomy' => 'post_tag' ) );
391
		// check the long form
392
		$types = array('feed', 'rdf', 'rss', 'rss2', 'atom');
393
		foreach ($types as $type) {
394
				$this->go_to("/tag/tag-a/feed/{$type}");
395
				$this->assertQueryTrue('is_archive', 'is_feed', 'is_tag');
396
		}
397
398
		// check the short form
399
		$types = array('feed', 'rdf', 'rss', 'rss2', 'atom');
400
		foreach ($types as $type) {
401
				$this->go_to("/tag/tag-a/{$type}");
402
				$this->assertQueryTrue('is_archive', 'is_feed', 'is_tag');
403
		}
404
	}
405
406
	// 'tag/(.+?)/page/?([0-9]{1,})/?$' => 'index.php?tag=$matches[1]&paged=$matches[2]',
407
	function test_tag_paged() {
408
		update_option( 'posts_per_page', 2 );
409
		$post_ids = self::factory()->post->create_many( 3 );
410
		foreach ( $post_ids as $post_id )
411
			self::factory()->term->add_post_terms( $post_id, 'tag-a', 'post_tag' );
412
		$this->go_to('/tag/tag-a/page/2/');
413
		$this->assertQueryTrue('is_archive', 'is_tag', 'is_paged');
414
	}
415
416
	// 'tag/(.+?)/?$' => 'index.php?tag=$matches[1]',
417
	function test_tag() {
418
		$term_id = self::factory()->term->create( array( 'name' => 'Tag Named A', 'slug' => 'tag-a', 'taxonomy' => 'post_tag' ) );
419
		$this->go_to('/tag/tag-a/');
420
		$this->assertQueryTrue('is_archive', 'is_tag');
421
422
		$tag = get_term( $term_id, 'post_tag' );
423
424
		$this->assertTrue( is_tag() );
425
		$this->assertTrue( is_tag( $tag->name ) );
426
		$this->assertTrue( is_tag( $tag->slug ) );
427
		$this->assertTrue( is_tag( $tag->term_id ) );
428
		$this->assertTrue( is_tag( array() ) );
429
		$this->assertTrue( is_tag( array( $tag->name ) ) );
430
		$this->assertTrue( is_tag( array( $tag->slug ) ) );
431
		$this->assertTrue( is_tag( array( $tag->term_id ) ) );
432
	}
433
434
	// 'author/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?author_name=$matches[1]&feed=$matches[2]',
435
	// 'author/([^/]+)/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?author_name=$matches[1]&feed=$matches[2]',
436
	function test_author_feed() {
437
		self::factory()->user->create( array( 'user_login' => 'user-a' ) );
438
		// check the long form
439
		$types = array('feed', 'rdf', 'rss', 'rss2', 'atom');
440
		foreach ($types as $type) {
441
				$this->go_to("/author/user-a/feed/{$type}");
442
				$this->assertQueryTrue('is_archive', 'is_feed', 'is_author');
443
		}
444
445
		// check the short form
446
		$types = array('feed', 'rdf', 'rss', 'rss2', 'atom');
447
		foreach ($types as $type) {
448
				$this->go_to("/author/user-a/{$type}");
449
				$this->assertQueryTrue('is_archive', 'is_feed', 'is_author');
450
		}
451
	}
452
453
	// 'author/([^/]+)/page/?([0-9]{1,})/?$' => 'index.php?author_name=$matches[1]&paged=$matches[2]',
454
	function test_author_paged() {
455
		update_option( 'posts_per_page', 2 );
456
		$user_id = self::factory()->user->create( array( 'user_login' => 'user-a' ) );
457
		self::factory()->post->create_many( 3, array( 'post_author' => $user_id ) );
458
		$this->go_to('/author/user-a/page/2/');
459
		$this->assertQueryTrue('is_archive', 'is_author', 'is_paged');
460
	}
461
462
	// 'author/([^/]+)/?$' => 'index.php?author_name=$matches[1]',
463
	function test_author() {
464
		$user_id = self::factory()->user->create( array( 'user_login' => 'user-a' ) );
465
		self::factory()->post->create( array( 'post_author' => $user_id ) );
466
		$this->go_to('/author/user-a/');
467
		$this->assertQueryTrue('is_archive', 'is_author');
468
	}
469
470
	function test_author_with_no_posts() {
471
		$user_id = self::factory()->user->create( array( 'user_login' => 'user-a' ) );
0 ignored issues
show
Unused Code introduced by
$user_id 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...
472
		$this->go_to('/author/user-a/');
473
		$this->assertQueryTrue('is_archive', 'is_author');
474
	}
475
476
	// '([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/feed/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?year=$matches[1]&monthnum=$matches[2]&day=$matches[3]&feed=$matches[4]',
477
	// '([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?year=$matches[1]&monthnum=$matches[2]&day=$matches[3]&feed=$matches[4]',
478
	function test_ymd_feed() {
479
		self::factory()->post->create( array( 'post_date' => '2007-09-04 00:00:00' ) );
480
		// check the long form
481
		$types = array('feed', 'rdf', 'rss', 'rss2', 'atom');
482
		foreach ($types as $type) {
483
				$this->go_to("/2007/09/04/feed/{$type}");
484
				$this->assertQueryTrue('is_archive', 'is_feed', 'is_day', 'is_date');
485
		}
486
487
		// check the short form
488
		$types = array('feed', 'rdf', 'rss', 'rss2', 'atom');
489
		foreach ($types as $type) {
490
				$this->go_to("/2007/09/04/{$type}");
491
				$this->assertQueryTrue('is_archive', 'is_feed', 'is_day', 'is_date');
492
		}
493
	}
494
495
	// '([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/page/?([0-9]{1,})/?$' => 'index.php?year=$matches[1]&monthnum=$matches[2]&day=$matches[3]&paged=$matches[4]',
496
	function test_ymd_paged() {
497
		update_option( 'posts_per_page', 2 );
498
		self::factory()->post->create_many( 3, array( 'post_date' => '2007-09-04 00:00:00' ) );
499
		$this->go_to('/2007/09/04/page/2/');
500
		$this->assertQueryTrue('is_archive', 'is_day', 'is_date', 'is_paged');
501
	}
502
503
	// '([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/?$' => 'index.php?year=$matches[1]&monthnum=$matches[2]&day=$matches[3]',
504
	function test_ymd() {
505
		self::factory()->post->create( array( 'post_date' => '2007-09-04 00:00:00' ) );
506
		$this->go_to('/2007/09/04/');
507
		$this->assertQueryTrue('is_archive', 'is_day', 'is_date');
508
	}
509
510
	// '([0-9]{4})/([0-9]{1,2})/feed/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?year=$matches[1]&monthnum=$matches[2]&feed=$matches[3]',
511
	// '([0-9]{4})/([0-9]{1,2})/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?year=$matches[1]&monthnum=$matches[2]&feed=$matches[3]',
512
	function test_ym_feed() {
513
		self::factory()->post->create( array( 'post_date' => '2007-09-04 00:00:00' ) );
514
		// check the long form
515
		$types = array('feed', 'rdf', 'rss', 'rss2', 'atom');
516
		foreach ($types as $type) {
517
				$this->go_to("/2007/09/feed/{$type}");
518
				$this->assertQueryTrue('is_archive', 'is_feed', 'is_month', 'is_date');
519
		}
520
521
		// check the short form
522
		$types = array('feed', 'rdf', 'rss', 'rss2', 'atom');
523
		foreach ($types as $type) {
524
				$this->go_to("/2007/09/{$type}");
525
				$this->assertQueryTrue('is_archive', 'is_feed', 'is_month', 'is_date');
526
		}
527
	}
528
529
	// '([0-9]{4})/([0-9]{1,2})/page/?([0-9]{1,})/?$' => 'index.php?year=$matches[1]&monthnum=$matches[2]&paged=$matches[3]',
530
	function test_ym_paged() {
531
		update_option( 'posts_per_page', 2 );
532
		self::factory()->post->create_many( 3, array( 'post_date' => '2007-09-04 00:00:00' ) );
533
		$this->go_to('/2007/09/page/2/');
534
		$this->assertQueryTrue('is_archive', 'is_date', 'is_month', 'is_paged');
535
	}
536
537
	// '([0-9]{4})/([0-9]{1,2})/?$' => 'index.php?year=$matches[1]&monthnum=$matches[2]',
538
	function test_ym() {
539
		self::factory()->post->create( array( 'post_date' => '2007-09-04 00:00:00' ) );
540
		$this->go_to('/2007/09/');
541
		$this->assertQueryTrue('is_archive', 'is_date', 'is_month');
542
	}
543
544
	// '([0-9]{4})/feed/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?year=$matches[1]&feed=$matches[2]',
545
	// '([0-9]{4})/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?year=$matches[1]&feed=$matches[2]',
546
	function test_y_feed() {
547
		self::factory()->post->create( array( 'post_date' => '2007-09-04 00:00:00' ) );
548
		// check the long form
549
		$types = array('feed', 'rdf', 'rss', 'rss2', 'atom');
550
		foreach ($types as $type) {
551
				$this->go_to("/2007/feed/{$type}");
552
				$this->assertQueryTrue('is_archive', 'is_feed', 'is_year', 'is_date');
553
		}
554
555
		// check the short form
556
		$types = array('feed', 'rdf', 'rss', 'rss2', 'atom');
557
		foreach ($types as $type) {
558
				$this->go_to("/2007/{$type}");
559
				$this->assertQueryTrue('is_archive', 'is_feed', 'is_year', 'is_date');
560
		}
561
	}
562
563
	// '([0-9]{4})/page/?([0-9]{1,})/?$' => 'index.php?year=$matches[1]&paged=$matches[2]',
564
	function test_y_paged() {
565
		update_option( 'posts_per_page', 2 );
566
		self::factory()->post->create_many( 3, array( 'post_date' => '2007-09-04 00:00:00' ) );
567
		$this->go_to('/2007/page/2/');
568
		$this->assertQueryTrue('is_archive', 'is_date', 'is_year', 'is_paged');
569
	}
570
571
	// '([0-9]{4})/?$' => 'index.php?year=$matches[1]',
572
	function test_y() {
573
		self::factory()->post->create( array( 'post_date' => '2007-09-04 00:00:00' ) );
574
		$this->go_to('/2007/');
575
		$this->assertQueryTrue('is_archive', 'is_date', 'is_year');
576
	}
577
578
	// '([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/([^/]+)/trackback/?$' => 'index.php?year=$matches[1]&monthnum=$matches[2]&day=$matches[3]&name=$matches[4]&tb=1',
579
	function test_post_trackback() {
580
		$post_id = self::factory()->post->create();
581
		$permalink = get_permalink( $post_id );
582
		$this->go_to("{$permalink}trackback/");
583
		$this->assertQueryTrue('is_single', 'is_singular', 'is_trackback');
584
	}
585
586
	// '([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?year=$matches[1]&monthnum=$matches[2]&day=$matches[3]&name=$matches[4]&feed=$matches[5]',
587
	// '([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/([^/]+)/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?year=$matches[1]&monthnum=$matches[2]&day=$matches[3]&name=$matches[4]&feed=$matches[5]',
588
	function test_post_comment_feed() {
589
		$post_id = self::factory()->post->create();
590
		$permalink = get_permalink( $post_id );
591
592
		$types = array('feed', 'rdf', 'rss', 'rss2', 'atom');
593
		foreach ($types as $type) {
594
				$this->go_to("{$permalink}feed/{$type}");
595
				$this->assertQueryTrue('is_single', 'is_singular', 'is_feed', 'is_comment_feed');
596
		}
597
598
		// check the short form
599
		$types = array('feed', 'rdf', 'rss', 'rss2', 'atom');
600
		foreach ($types as $type) {
601
				$this->go_to("{$permalink}{$type}");
602
				$this->assertQueryTrue('is_single', 'is_singular', 'is_feed', 'is_comment_feed');
603
		}
604
	}
605
606
	// '([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/([^/]+)(/[0-9]+)?/?$' => 'index.php?year=$matches[1]&monthnum=$matches[2]&day=$matches[3]&name=$matches[4]&page=$matches[5]',
607
	function test_post_paged_short() {
608
		$post_id = self::factory()->post->create( array(
609
			'post_date' => '2007-09-04 00:00:00',
610
			'post_title' => 'a-post-with-multiple-pages',
611
			'post_content' => 'Page 1 <!--nextpage--> Page 2'
612
		) );
613
		$this->go_to( get_permalink( $post_id ) . '2/' );
614
		// should is_paged be true also?
615
		$this->assertQueryTrue('is_single', 'is_singular');
616
617
	}
618
619
	// '[0-9]{4}/[0-9]{1,2}/[0-9]{1,2}/[^/]+/([^/]+)/?$' => 'index.php?attachment=$matches[1]',
620
	function test_post_attachment() {
621
		$post_id = self::factory()->post->create( array( 'post_type' => 'attachment' ) );
622
		$permalink = get_attachment_link( $post_id );
623
		$this->go_to($permalink);
624
		$this->assertQueryTrue('is_single', 'is_attachment', 'is_singular');
625
	}
626
627
	// '[0-9]{4}/[0-9]{1,2}/[0-9]{1,2}/[^/]+/([^/]+)/trackback/?$' => 'index.php?attachment=$matches[1]&tb=1',
628
	// '[0-9]{4}/[0-9]{1,2}/[0-9]{1,2}/[^/]+/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?attachment=$matches[1]&feed=$matches[2]',
629
	// '[0-9]{4}/[0-9]{1,2}/[0-9]{1,2}/[^/]+/([^/]+)/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?attachment=$matches[1]&feed=$matches[2]',
630
	// '[0-9]{4}/[0-9]{1,2}/[0-9]{1,2}/[^/]+/attachment/([^/]+)/?$' => 'index.php?attachment=$matches[1]',
631
	// '[0-9]{4}/[0-9]{1,2}/[0-9]{1,2}/[^/]+/attachment/([^/]+)/trackback/?$' => 'index.php?attachment=$matches[1]&tb=1',
632
	// '[0-9]{4}/[0-9]{1,2}/[0-9]{1,2}/[^/]+/attachment/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?attachment=$matches[1]&feed=$matches[2]',
633
	// '[0-9]{4}/[0-9]{1,2}/[0-9]{1,2}/[^/]+/attachment/([^/]+)/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?attachment=$matches[1]&feed=$matches[2]',
634
635
	/**
636
	 * @expectedIncorrectUsage WP_Date_Query
637
	 */
638
	function test_bad_dates() {
639
		$this->go_to( '/2013/13/13/' );
640
		$this->assertQueryTrue( 'is_404' );
641
642
		$this->go_to( '/2013/11/41/' );
643
		$this->assertQueryTrue( 'is_404' );
644
	}
645
646
	function test_post_type_archive_with_tax_query() {
647
		delete_option( 'rewrite_rules' );
648
649
		$cpt_name = 'ptawtq';
650
		register_post_type( $cpt_name, array(
651
			'taxonomies' => array( 'post_tag', 'category' ),
652
			'rewrite' => true,
653
			'has_archive' => true,
654
			'public' => true
655
		) );
656
657
		$tag_id = self::factory()->tag->create( array( 'slug' => 'tag-slug' ) );
658
		$post_id = self::factory()->post->create( array( 'post_type' => $cpt_name ) );
659
		wp_set_object_terms( $post_id, $tag_id, 'post_tag' );
660
661
		$this->go_to( '/ptawtq/' );
662
		$this->assertQueryTrue( 'is_post_type_archive', 'is_archive' );
663
		$this->assertEquals( get_queried_object(), get_post_type_object( $cpt_name ) );
664
665
		add_action( 'pre_get_posts', array( $this, 'pre_get_posts_with_tax_query' ) );
666
667
		$this->go_to( '/ptawtq/' );
668
		$this->assertQueryTrue( 'is_post_type_archive', 'is_archive' );
669
		$this->assertEquals( get_queried_object(), get_post_type_object( $cpt_name ) );
670
671
		remove_action( 'pre_get_posts', array( $this, 'pre_get_posts_with_tax_query' ) );
672
	}
673
674
	function pre_get_posts_with_tax_query( &$query ) {
675
		$term = get_term_by( 'slug', 'tag-slug', 'post_tag' );
676
		$query->set( 'tax_query', array(
677
			array( 'taxonomy' => 'post_tag', 'field' => 'term_id', 'terms' => $term->term_id )
678
		) );
679
	}
680
681
	function test_post_type_array() {
682
		delete_option( 'rewrite_rules' );
683
684
		$cpt_name = 'thearray';
685
		register_post_type( $cpt_name, array(
686
			'taxonomies' => array( 'post_tag', 'category' ),
687
			'rewrite' => true,
688
			'has_archive' => true,
689
			'public' => true
690
		) );
691
		self::factory()->post->create( array( 'post_type' => $cpt_name ) );
692
693
		$this->go_to( "/$cpt_name/" );
694
		$this->assertQueryTrue( 'is_post_type_archive', 'is_archive' );
695
		$this->assertEquals( get_queried_object(), get_post_type_object( $cpt_name ) );
696
697
		add_action( 'pre_get_posts', array( $this, 'pre_get_posts_with_type_array' ) );
698
699
		$this->go_to( "/$cpt_name/" );
700
		$this->assertQueryTrue( 'is_post_type_archive', 'is_archive' );
701
		$this->assertEquals( get_queried_object(), get_post_type_object( 'post' ) );
702
703
		remove_action( 'pre_get_posts', array( $this, 'pre_get_posts_with_type_array' ) );
704
	}
705
706
	function pre_get_posts_with_type_array( &$query ) {
707
		$query->set( 'post_type', array( 'post', 'thearray' ) );
708
	}
709
710
	function test_is_single() {
711
		$post_id = self::factory()->post->create();
712
		$this->go_to( "/?p=$post_id" );
713
714
		$post = get_queried_object();
715
		$q = $GLOBALS['wp_query'];
716
717
		$this->assertTrue( is_single() );
718
		$this->assertTrue( $q->is_single );
719
		$this->assertFalse( $q->is_page );
720
		$this->assertFalse( $q->is_attachment );
721
		$this->assertTrue( is_single( $post ) );
0 ignored issues
show
Documentation introduced by
$post is of type object, but the function expects a string|integer|array.

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...
722
		$this->assertTrue( is_single( $post->ID ) );
723
		$this->assertTrue( is_single( $post->post_title ) );
724
		$this->assertTrue( is_single( $post->post_name ) );
725
	}
726
727
	/**
728
	 * @ticket 16802
729
	 */
730
	function test_is_single_with_parent() {
731
		// Use custom hierarchical post type
732
		$post_type = 'test_hierarchical';
733
734
		register_post_type( $post_type, array(
735
			'hierarchical' => true,
736
			'rewrite'      => true,
737
			'has_archive'  => true,
738
			'public'       => true
739
		) );
740
741
		// Create parent and child posts
742
		$parent_id = self::factory()->post->create( array(
743
			'post_type' => $post_type,
744
			'post_name' => 'foo'
745
		) );
746
747
		$post_id = self::factory()->post->create( array(
748
			'post_type'   => $post_type,
749
			'post_name'   => 'bar',
750
			'post_parent' => $parent_id
751
		) );
752
753
		// Tests
754
		$this->go_to( "/?p=$post_id&post_type=$post_type" );
755
756
		$post = get_queried_object();
757
		$q = $GLOBALS['wp_query'];
758
759
		$this->assertTrue( is_single() );
760
		$this->assertFalse( $q->is_page );
761
		$this->assertTrue( $q->is_single );
762
		$this->assertFalse( $q->is_attachment );
763
		$this->assertTrue( is_single( $post ) );
0 ignored issues
show
Documentation introduced by
$post is of type object, but the function expects a string|integer|array.

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...
764
		$this->assertTrue( is_single( $post->ID ) );
765
		$this->assertTrue( is_single( $post->post_title ) );
766
		$this->assertTrue( is_single( $post->post_name ) );
767
		$this->assertTrue( is_single( 'foo/bar' ) );
768
		$this->assertFalse( is_single( $parent_id ) );
769
		$this->assertFalse( is_single( 'foo/bar/baz' ) );
770
		$this->assertFalse( is_single( 'bar/bar' ) );
771
		$this->assertFalse( is_single( 'foo' ) );
772
	}
773
774
	/**
775
	 * @ticket 24674
776
	 */
777
	public function test_is_single_with_slug_that_begins_with_a_number_that_clashes_with_another_post_id() {
778
		$p1 = self::factory()->post->create();
779
780
		$p2_name = $p1 . '-post';
781
		$p2 = self::factory()->post->create( array(
782
			'slug' => $p2_name,
783
		) );
784
785
		$this->go_to( "/?p=$p1" );
786
787
		$q = $GLOBALS['wp_query'];
788
789
		$this->assertTrue( $q->is_single() );
790
		$this->assertTrue( $q->is_single( $p1 ) );
791
		$this->assertFalse( $q->is_single( $p2_name ) );
792
		$this->assertFalse( $q->is_single( $p2 ) );
793
	}
794
795
	/**
796
	 * @ticket 24612
797
	 */
798
	public function test_is_single_with_slug_that_clashes_with_attachment() {
799
		$this->set_permalink_structure( '/%postname%/' );
800
801
		$attachment_id = $this->factory->post->create( array(
802
			'post_type'  => 'attachment',
803
		) );
804
805
		$post_id = $this->factory->post->create( array(
806
			'post_title' => get_post( $attachment_id )->post_title
807
		) );
808
809
		$this->go_to( get_permalink( $post_id ) );
810
811
		$q = $GLOBALS['wp_query'];
812
813
		$this->assertTrue( $q->is_single() );
814
		$this->assertTrue( $q->is_single( $post_id ) );
815
		$this->assertFalse( $q->is_attachment() );
816
		$this->assertFalse( $q->is_404() );
817
818
		$this->set_permalink_structure();
819
	}
820
821
	/**
822
	 * @ticket 38225
823
	 */
824
	function test_is_single_with_attachment() {
825
		$post_id = self::factory()->post->create();
826
827
		$attachment_id = self::factory()->attachment->create_object( 'image.jpg', $post_id, array(
0 ignored issues
show
Documentation introduced by
'image.jpg' is of type string, but the function expects a array.

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...
828
			'post_mime_type' => 'image/jpeg',
829
		) );
830
831
		$this->go_to( get_permalink( $attachment_id ) );
832
833
		$q = $GLOBALS['wp_query'];
834
835
		$this->assertTrue( is_single() );
836
		$this->assertTrue( $q->is_single );
837
		$this->assertTrue( $q->is_attachment );
838
	}
839
840
	function test_is_page() {
841
		$post_id = self::factory()->post->create( array( 'post_type' => 'page' ) );
842
		$this->go_to( "/?page_id=$post_id" );
843
844
		$post = get_queried_object();
845
		$q = $GLOBALS['wp_query'];
846
847
		$this->assertTrue( is_page() );
848
		$this->assertFalse( $q->is_single );
849
		$this->assertTrue( $q->is_page );
850
		$this->assertFalse( $q->is_attachment );
851
		$this->assertTrue( is_page( $post ) );
0 ignored issues
show
Documentation introduced by
$post is of type object, but the function expects a string|integer|array.

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...
852
		$this->assertTrue( is_page( $post->ID ) );
853
		$this->assertTrue( is_page( $post->post_title ) );
854
		$this->assertTrue( is_page( $post->post_name ) );
855
	}
856
857
	/**
858
	 * @ticket 16802
859
	 */
860
	function test_is_page_with_parent() {
861
		$parent_id = self::factory()->post->create( array(
862
			'post_type' => 'page',
863
			'post_name' => 'foo',
864
		) );
865
		$post_id = self::factory()->post->create( array(
866
			'post_type'   => 'page',
867
			'post_name'   => 'bar',
868
			'post_parent' => $parent_id,
869
		) );
870
		$this->go_to( "/?page_id=$post_id" );
871
872
		$post = get_queried_object();
873
		$q = $GLOBALS['wp_query'];
874
875
		$this->assertTrue( is_page() );
876
		$this->assertFalse( $q->is_single );
877
		$this->assertTrue( $q->is_page );
878
		$this->assertFalse( $q->is_attachment );
879
		$this->assertTrue( is_page( $post ) );
0 ignored issues
show
Documentation introduced by
$post is of type object, but the function expects a string|integer|array.

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...
880
		$this->assertTrue( is_page( $post->ID ) );
881
		$this->assertTrue( is_page( $post->post_title ) );
882
		$this->assertTrue( is_page( $post->post_name ) );
883
		$this->assertTrue( is_page( 'foo/bar' ) );
884
		$this->assertFalse( is_page( $parent_id ) );
885
		$this->assertFalse( is_page( 'foo/bar/baz' ) );
886
		$this->assertFalse( is_page( 'bar/bar' ) );
887
		$this->assertFalse( is_page( 'foo' ) );
888
	}
889
890
	function test_is_attachment() {
891
		$post_id = self::factory()->post->create( array( 'post_type' => 'attachment' ) );
892
		$this->go_to( "/?attachment_id=$post_id" );
893
894
		$post = get_queried_object();
895
		$q = $GLOBALS['wp_query'];
896
897
		$this->assertTrue( is_attachment() );
898
		$this->assertTrue( is_single() );
899
		$this->assertTrue( $q->is_attachment );
900
		$this->assertTrue( $q->is_single );
901
		$this->assertFalse( $q->is_page );
902
		$this->assertTrue( is_attachment( $post ) );
903
		$this->assertTrue( is_attachment( $post->ID ) );
904
		$this->assertTrue( is_attachment( $post->post_title ) );
905
		$this->assertTrue( is_attachment( $post->post_name ) );
906
	}
907
908
	/**
909
	 * @ticket 24674
910
	 */
911
	public function test_is_attachment_with_slug_that_begins_with_a_number_that_clashes_with_a_page_ID() {
912
		$p1 = self::factory()->post->create( array( 'post_type' => 'attachment' ) );
913
914
		$p2_name = $p1 . '-attachment';
915
		$p2 = self::factory()->post->create( array(
916
			'post_type' => 'attachment',
917
			'post_name' => $p2_name,
918
		) );
919
920
		$this->go_to( "/?attachment_id=$p1" );
921
922
		$q = $GLOBALS['wp_query'];
923
924
		$this->assertTrue( $q->is_attachment() );
925
		$this->assertTrue( $q->is_attachment( $p1 ) );
926
		$this->assertFalse( $q->is_attachment( $p2_name ) );
927
		$this->assertFalse( $q->is_attachment( $p2 ) );
928
	}
929
930
	/**
931
	 * @ticket 24674
932
	 */
933
	public function test_is_author_with_nicename_that_begins_with_a_number_that_clashes_with_another_author_id() {
934
		$u1 = self::factory()->user->create();
935
936
		$u2_name = $u1 . '_user';
937
		$u2 = self::factory()->user->create( array(
938
			'user_nicename' => $u2_name,
939
		) );
940
941
		$this->go_to( "/?author=$u1" );
942
943
		$q = $GLOBALS['wp_query'];
944
945
		$this->assertTrue( $q->is_author() );
946
		$this->assertTrue( $q->is_author( $u1 ) );
947
		$this->assertFalse( $q->is_author( $u2_name ) );
948
		$this->assertFalse( $q->is_author( $u2 ) );
949
	}
950
951
	/**
952
	 * @ticket 24674
953
	 */
954
	public function test_is_category_with_slug_that_begins_with_a_number_that_clashes_with_another_category_id() {
955
		$c1 = self::factory()->category->create();
956
957
		$c2_name = $c1 . '-category';
958
		$c2 = self::factory()->category->create( array(
959
			'slug' => $c2_name,
960
		) );
961
962
		$this->go_to( "/?cat=$c1" );
963
964
		$q = $GLOBALS['wp_query'];
965
966
		$this->assertTrue( $q->is_category() );
967
		$this->assertTrue( $q->is_category( $c1 ) );
968
		$this->assertFalse( $q->is_category( $c2_name ) );
969
		$this->assertFalse( $q->is_category( $c2 ) );
970
	}
971
972
	/**
973
	 * @ticket 24674
974
	 */
975
	public function test_is_tag_with_slug_that_begins_with_a_number_that_clashes_with_another_tag_id() {
976
		$t1 = self::factory()->tag->create();
977
978
		$t2_name = $t1 . '-tag';
979
		$t2 = self::factory()->tag->create( array(
980
			'slug' => $t2_name,
981
		) );
982
983
		$this->go_to( "/?tag_id=$t1" );
984
985
		$q = $GLOBALS['wp_query'];
986
987
		$this->assertTrue( $q->is_tag() );
988
		$this->assertTrue( $q->is_tag( $t1 ) );
989
		$this->assertFalse( $q->is_tag( $t2_name ) );
990
		$this->assertFalse( $q->is_tag( $t2 ) );
991
	}
992
993
	/**
994
	 * @ticket 24674
995
	 */
996
	public function test_is_page_with_page_id_zero_and_random_page_slug() {
997
		$post_id = self::factory()->post->create( array( 'post_type' => 'page' ) );
998
		$this->go_to( "/?page_id=$post_id" );
999
1000
		// override post ID to 0 temporarily for testing
1001
		$_id = $GLOBALS['wp_query']->post->ID;
1002
		$GLOBALS['wp_query']->post->ID = 0;
1003
1004
		$post = get_queried_object();
0 ignored issues
show
Unused Code introduced by
$post 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...
1005
		$q = $GLOBALS['wp_query'];
1006
1007
		$this->assertTrue( $q->is_page() );
1008
		$this->assertFalse( $q->is_page( 'sample-page' ) );
1009
		$this->assertFalse( $q->is_page( 'random-page-slug' ) );
1010
1011
		// revert $wp_query global change
1012
		$GLOBALS['wp_query']->post->ID = $_id;
1013
	}
1014
1015
	/**
1016
	 * @ticket 24674
1017
	 */
1018
	public function test_is_page_with_page_slug_that_begins_with_a_number_that_clashes_with_a_page_ID() {
1019
		$p1 = self::factory()->post->create( array( 'post_type' => 'page' ) );
1020
1021
		$p2_name = $p1 . '-page';
1022
		$p2 = self::factory()->post->create( array(
1023
			'post_type' => 'page',
1024
			'post_name' => $p2_name,
1025
		) );
1026
1027
		$this->go_to( "/?page_id=$p1" );
1028
1029
		$q = $GLOBALS['wp_query'];
1030
1031
		$this->assertTrue( $q->is_page() );
1032
		$this->assertTrue( $q->is_page( $p1 ) );
1033
		$this->assertFalse( $q->is_page( $p2_name ) );
1034
		$this->assertFalse( $q->is_page( $p2 ) );
1035
	}
1036
1037
	function test_is_page_template() {
1038
		$post_id = self::factory()->post->create( array( 'post_type' => 'page' ) );
1039
		update_post_meta($post_id, '_wp_page_template', 'example.php');
1040
		$this->go_to( "/?page_id=$post_id" );
1041
		$this->assertTrue( is_page_template( 'example.php' ) );
1042
	}
1043
1044
	/**
1045
	 * @ticket 31271
1046
	 */
1047
	function test_is_page_template_default() {
1048
		$post_id = self::factory()->post->create( array( 'post_type' => 'page' ) );
1049
		$this->go_to( "/?page_id=$post_id" );
1050
		$this->assertTrue( is_page_template( 'default' ) );
1051
		$this->assertTrue( is_page_template( array( 'random', 'default' ) ) );
1052
	}
1053
1054
	/**
1055
	 * @ticket 31271
1056
	 */
1057
	function test_is_page_template_array() {
1058
		$post_id = self::factory()->post->create( array( 'post_type' => 'page' ) );
1059
		update_post_meta($post_id, '_wp_page_template', 'example.php');
1060
		$this->go_to( "/?page_id=$post_id" );
1061
		$this->assertFalse( is_page_template( array( 'test.php' ) ) );
1062
		$this->assertTrue( is_page_template( array('test.php', 'example.php') ) );
1063
	}
1064
1065
	/**
1066
	 * @ticket 18375
1067
	 */
1068
	function test_is_page_template_other_post_type() {
1069
		$post_id = self::factory()->post->create( array( 'post_type' => 'post' ) );
1070
		update_post_meta( $post_id, '_wp_page_template', 'example.php' );
1071
		$this->go_to( get_post_permalink( $post_id ) );
1072
		$this->assertFalse( is_page_template( array( 'test.php' ) ) );
1073
		$this->assertTrue( is_page_template( array( 'test.php', 'example.php' ) ) );
1074
	}
1075
1076
	/**
1077
	 * @ticket 39211
1078
	 */
1079
	function test_is_page_template_not_singular() {
1080
		global $wpdb;
1081
1082
		// We need a non-post that shares an ID with a post assigned a template.
1083
		$user_id = self::factory()->user->create();
1084
		if ( ! get_post( $user_id ) ) {
1085
			$post_id = self::factory()->post->create( array( 'post_type' => 'post' ) );
1086
			$wpdb->update( $wpdb->posts, array( 'ID' => $user_id ), array( 'ID' => $post_id ), array( '%d' ) );
1087
		}
1088
1089
		update_post_meta( $user_id, '_wp_page_template', 'example.php' );
1090
1091
		// Verify that the post correctly reports having a template.
1092
		$this->go_to( get_post_permalink( $user_id ) );
1093
		$this->assertInstanceOf( 'WP_Post', get_queried_object() );
1094
		$this->assertTrue( is_page_template( 'example.php' ) );
1095
1096
		// Verify that the non-post with a matching ID does not report having a template.
1097
		$this->go_to( get_author_posts_url( $user_id ) );
1098
		$this->assertInstanceOf( 'WP_User', get_queried_object() );
1099
		$this->assertFalse( is_page_template( 'example.php' ) );
1100
	}
1101
1102
	/**
1103
	 * @ticket 35902
1104
	 */
1105
	public function test_is_attachment_should_not_match_numeric_id_to_post_title_beginning_with_id() {
1106
		$p1 = self::factory()->post->create( array(
1107
			'post_type' => 'attachment',
1108
			'post_title' => 'Foo',
1109
			'post_name' => 'foo',
1110
		) );
1111
		$p2 = self::factory()->post->create( array(
1112
			'post_type' => 'attachment',
1113
			'post_title' => "$p1 Foo",
1114
			'post_name' => 'foo-2',
1115
		) );
1116
1117
		$this->go_to( get_permalink( $p2 ) );
1118
1119
		$this->assertTrue( is_attachment( $p2 ) );
1120
		$this->assertFalse( is_attachment( $p1 ) );
1121
	}
1122
1123
	/**
1124
	 * @ticket 35902
1125
	 */
1126
	public function test_is_attachment_should_not_match_numeric_id_to_post_name_beginning_with_id() {
1127
		$p1 = self::factory()->post->create( array(
1128
			'post_type' => 'attachment',
1129
			'post_title' => 'Foo',
1130
			'post_name' => 'foo',
1131
		) );
1132
		$p2 = self::factory()->post->create( array(
1133
			'post_type' => 'attachment',
1134
			'post_title' => 'Foo',
1135
			'post_name' => "$p1-foo",
1136
		) );
1137
1138
		$this->go_to( get_permalink( $p2 ) );
1139
1140
		$this->assertTrue( is_attachment( $p2 ) );
1141
		$this->assertFalse( is_attachment( $p1 ) );
1142
	}
1143
1144
	/**
1145
	 * @ticket 35902
1146
	 */
1147
	public function test_is_author_should_not_match_numeric_id_to_nickname_beginning_with_id() {
1148
		$u1 = self::factory()->user->create( array(
1149
			'nickname' => 'Foo',
1150
			'user_nicename' => 'foo',
1151
		) );
1152
		$u2 = self::factory()->user->create( array(
1153
			'nickname' => "$u1 Foo",
1154
			'user_nicename' => 'foo-2',
1155
		) );
1156
1157
		$this->go_to( get_author_posts_url( $u2 ) );
1158
1159
		$this->assertTrue( is_author( $u2 ) );
1160
		$this->assertFalse( is_author( $u1 ) );
1161
	}
1162
1163
	/**
1164
	 * @ticket 35902
1165
	 */
1166
	public function test_is_author_should_not_match_numeric_id_to_user_nicename_beginning_with_id() {
1167
		$u1 = self::factory()->user->create( array(
1168
			'nickname' => 'Foo',
1169
			'user_nicename' => 'foo',
1170
		) );
1171
		$u2 = self::factory()->user->create( array(
1172
			'nickname' => 'Foo',
1173
			'user_nicename' => "$u1-foo",
1174
		) );
1175
1176
		$this->go_to( get_author_posts_url( $u2 ) );
1177
1178
		$this->assertTrue( is_author( $u2 ) );
1179
		$this->assertFalse( is_author( $u1 ) );
1180
	}
1181
1182
	/**
1183
	 * @ticket 35902
1184
	 */
1185
	public function test_is_category_should_not_match_numeric_id_to_name_beginning_with_id() {
1186
		$t1 = self::factory()->term->create( array(
1187
			'taxonomy' => 'category',
1188
			'slug' => 'foo',
1189
			'name' => 'foo',
1190
		) );
1191
		$t2 = self::factory()->term->create( array(
1192
			'taxonomy' => 'category',
1193
			'slug' => "$t1-foo",
1194
			'name' => 'foo 2',
1195
		) );
1196
1197
		$this->go_to( get_term_link( $t2 ) );
1198
1199
		$this->assertTrue( is_category( $t2 ) );
1200
		$this->assertFalse( is_category( $t1 ) );
1201
	}
1202
1203
	/**
1204
	 * @ticket 35902
1205
	 */
1206
	public function test_is_category_should_not_match_numeric_id_to_slug_beginning_with_id() {
1207
		$t1 = self::factory()->term->create( array(
1208
			'taxonomy' => 'category',
1209
			'slug' => 'foo',
1210
			'name' => 'foo',
1211
		) );
1212
		$t2 = self::factory()->term->create( array(
1213
			'taxonomy' => 'category',
1214
			'slug' => 'foo-2',
1215
			'name' => "$t1 foo",
1216
		) );
1217
1218
		$this->go_to( get_term_link( $t2 ) );
1219
1220
		$this->assertTrue( is_category( $t2 ) );
1221
		$this->assertFalse( is_category( $t1 ) );
1222
	}
1223
1224
	/**
1225
	 * @ticket 35902
1226
	 */
1227
	public function test_is_tag_should_not_match_numeric_id_to_name_beginning_with_id() {
1228
		$t1 = self::factory()->term->create( array(
1229
			'taxonomy' => 'post_tag',
1230
			'slug' => 'foo',
1231
			'name' => 'foo',
1232
		) );
1233
		$t2 = self::factory()->term->create( array(
1234
			'taxonomy' => 'post_tag',
1235
			'slug' => "$t1-foo",
1236
			'name' => 'foo 2',
1237
		) );
1238
1239
		$this->go_to( get_term_link( $t2 ) );
1240
1241
		$this->assertTrue( is_tag( $t2 ) );
1242
		$this->assertFalse( is_tag( $t1 ) );
1243
	}
1244
1245
	/**
1246
	 * @ticket 35902
1247
	 */
1248
	public function test_is_tag_should_not_match_numeric_id_to_slug_beginning_with_id() {
1249
		$t1 = self::factory()->term->create( array(
1250
			'taxonomy' => 'post_tag',
1251
			'slug' => 'foo',
1252
			'name' => 'foo',
1253
		) );
1254
		$t2 = self::factory()->term->create( array(
1255
			'taxonomy' => 'post_tag',
1256
			'slug' => 'foo-2',
1257
			'name' => "$t1 foo",
1258
		) );
1259
1260
		$this->go_to( get_term_link( $t2 ) );
1261
1262
		$this->assertTrue( is_tag( $t2 ) );
1263
		$this->assertFalse( is_tag( $t1 ) );
1264
	}
1265
1266
	/**
1267
	 * @ticket 35902
1268
	 */
1269
	public function test_is_page_should_not_match_numeric_id_to_post_title_beginning_with_id() {
1270
		$p1 = self::factory()->post->create( array(
1271
			'post_type' => 'page',
1272
			'post_title' => 'Foo',
1273
			'post_name' => 'foo',
1274
		) );
1275
		$p2 = self::factory()->post->create( array(
1276
			'post_type' => 'page',
1277
			'post_title' => "$p1 Foo",
1278
			'post_name' => 'foo-2',
1279
		) );
1280
1281
		$this->go_to( get_permalink( $p2 ) );
1282
1283
		$this->assertTrue( is_page( $p2 ) );
1284
		$this->assertFalse( is_page( $p1 ) );
1285
	}
1286
1287
	/**
1288
	 * @ticket 35902
1289
	 */
1290
	public function test_is_page_should_not_match_numeric_id_to_post_name_beginning_with_id() {
1291
		$p1 = self::factory()->post->create( array(
1292
			'post_type' => 'page',
1293
			'post_title' => 'Foo',
1294
			'post_name' => 'foo',
1295
		) );
1296
		$p2 = self::factory()->post->create( array(
1297
			'post_type' => 'page',
1298
			'post_title' => 'Foo',
1299
			'post_name' => "$p1-foo",
1300
		) );
1301
1302
		$this->go_to( get_permalink( $p2 ) );
1303
1304
		$this->assertTrue( is_page( $p2 ) );
1305
		$this->assertFalse( is_page( $p1 ) );
1306
	}
1307
1308
	/**
1309
	 * @ticket 35902
1310
	 */
1311
	public function test_is_single_should_not_match_numeric_id_to_post_title_beginning_with_id() {
1312
		$p1 = self::factory()->post->create( array(
1313
			'post_type' => 'post',
1314
			'post_title' => 'Foo',
1315
			'post_name' => 'foo',
1316
		) );
1317
		$p2 = self::factory()->post->create( array(
1318
			'post_type' => 'post',
1319
			'post_title' => "$p1 Foo",
1320
			'post_name' => 'foo-2',
1321
		) );
1322
1323
		$this->go_to( get_permalink( $p2 ) );
1324
1325
		$this->assertTrue( is_single( $p2 ) );
1326
		$this->assertFalse( is_single( $p1 ) );
1327
	}
1328
1329
	/**
1330
	 * @ticket 35902
1331
	 */
1332
	public function test_is_single_should_not_match_numeric_id_to_post_name_beginning_with_id() {
1333
		$p1 = self::factory()->post->create( array(
1334
			'post_type' => 'post',
1335
			'post_title' => 'Foo',
1336
			'post_name' => 'foo',
1337
		) );
1338
		$p2 = self::factory()->post->create( array(
1339
			'post_type' => 'post',
1340
			'post_title' => 'Foo',
1341
			'post_name' => "$p1-foo",
1342
		) );
1343
1344
		$this->go_to( get_permalink( $p2 ) );
1345
1346
		$this->assertTrue( is_single( $p2 ) );
1347
		$this->assertFalse( is_single( $p1 ) );
1348
	}
1349
}
1350