Query: is_*( $int ) should not falsely match strings starting with "$int".

Another chapter in the Storied Annals of Weird `in_array()` Behavior:
`in_array( 4, array( "4-cool-dudes" ) );` resolves to `true`, such that
`is_page( 4 )` was returning true for posts with the name `'4-cool-dudes'`.

We work around this behavior by ensuring that values passed to the `is_`
methods are cast to strings before the `in_array()` checks. ID checks still
work as expected; see #24674.

Props mikejolley, swissspidy, boonebgorges.
Fixes #35902.

git-svn-id: https://develop.svn.wordpress.org/trunk@36625 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Boone Gorges
2016-02-23 02:20:28 +00:00
parent b30e77f5fc
commit 0809037f61
2 changed files with 254 additions and 6 deletions

View File

@@ -1017,4 +1017,252 @@ class Tests_Query_Conditionals extends WP_UnitTestCase {
$this->assertFalse( is_page_template( array( 'test.php' ) ) );
$this->assertTrue( is_page_template( array('test.php', 'example.php') ) );
}
/**
* @ticket 35902
*/
public function test_is_attachment_should_not_match_numeric_id_to_post_title_beginning_with_id() {
$p1 = self::factory()->post->create( array(
'post_type' => 'attachment',
'post_title' => 'Foo',
'post_name' => 'foo',
) );
$p2 = self::factory()->post->create( array(
'post_type' => 'attachment',
'post_title' => "$p1 Foo",
'post_name' => 'foo-2',
) );
$this->go_to( get_permalink( $p2 ) );
$this->assertTrue( is_attachment( $p2 ) );
$this->assertFalse( is_attachment( $p1 ) );
}
/**
* @ticket 35902
*/
public function test_is_attachment_should_not_match_numeric_id_to_post_name_beginning_with_id() {
$p1 = self::factory()->post->create( array(
'post_type' => 'attachment',
'post_title' => 'Foo',
'post_name' => 'foo',
) );
$p2 = self::factory()->post->create( array(
'post_type' => 'attachment',
'post_title' => 'Foo',
'post_name' => "$p1-foo",
) );
$this->go_to( get_permalink( $p2 ) );
$this->assertTrue( is_attachment( $p2 ) );
$this->assertFalse( is_attachment( $p1 ) );
}
/**
* @ticket 35902
*/
public function test_is_author_should_not_match_numeric_id_to_nickname_beginning_with_id() {
$u1 = self::factory()->user->create( array(
'nickname' => 'Foo',
'user_nicename' => 'foo',
) );
$u2 = self::factory()->user->create( array(
'nickname' => "$u1 Foo",
'user_nicename' => 'foo-2',
) );
$this->go_to( get_author_posts_url( $u2 ) );
$this->assertTrue( is_author( $u2 ) );
$this->assertFalse( is_author( $u1 ) );
}
/**
* @ticket 35902
*/
public function test_is_author_should_not_match_numeric_id_to_user_nicename_beginning_with_id() {
$u1 = self::factory()->user->create( array(
'nickname' => 'Foo',
'user_nicename' => 'foo',
) );
$u2 = self::factory()->user->create( array(
'nickname' => 'Foo',
'user_nicename' => "$u1-foo",
) );
$this->go_to( get_author_posts_url( $u2 ) );
$this->assertTrue( is_author( $u2 ) );
$this->assertFalse( is_author( $u1 ) );
}
/**
* @ticket 35902
*/
public function test_is_category_should_not_match_numeric_id_to_name_beginning_with_id() {
$t1 = self::factory()->term->create( array(
'taxonomy' => 'category',
'slug' => 'foo',
'name' => 'foo',
) );
$t2 = self::factory()->term->create( array(
'taxonomy' => 'category',
'slug' => "$t1-foo",
'name' => 'foo 2',
) );
$this->go_to( get_term_link( $t2 ) );
$this->assertTrue( is_category( $t2 ) );
$this->assertFalse( is_category( $t1 ) );
}
/**
* @ticket 35902
*/
public function test_is_category_should_not_match_numeric_id_to_slug_beginning_with_id() {
$t1 = self::factory()->term->create( array(
'taxonomy' => 'category',
'slug' => 'foo',
'name' => 'foo',
) );
$t2 = self::factory()->term->create( array(
'taxonomy' => 'category',
'slug' => 'foo-2',
'name' => "$t1 foo",
) );
$this->go_to( get_term_link( $t2 ) );
$this->assertTrue( is_category( $t2 ) );
$this->assertFalse( is_category( $t1 ) );
}
/**
* @ticket 35902
*/
public function test_is_tag_should_not_match_numeric_id_to_name_beginning_with_id() {
$t1 = self::factory()->term->create( array(
'taxonomy' => 'post_tag',
'slug' => 'foo',
'name' => 'foo',
) );
$t2 = self::factory()->term->create( array(
'taxonomy' => 'post_tag',
'slug' => "$t1-foo",
'name' => 'foo 2',
) );
$this->go_to( get_term_link( $t2 ) );
$this->assertTrue( is_tag( $t2 ) );
$this->assertFalse( is_tag( $t1 ) );
}
/**
* @ticket 35902
*/
public function test_is_tag_should_not_match_numeric_id_to_slug_beginning_with_id() {
$t1 = self::factory()->term->create( array(
'taxonomy' => 'post_tag',
'slug' => 'foo',
'name' => 'foo',
) );
$t2 = self::factory()->term->create( array(
'taxonomy' => 'post_tag',
'slug' => 'foo-2',
'name' => "$t1 foo",
) );
$this->go_to( get_term_link( $t2 ) );
$this->assertTrue( is_tag( $t2 ) );
$this->assertFalse( is_tag( $t1 ) );
}
/**
* @ticket 35902
*/
public function test_is_page_should_not_match_numeric_id_to_post_title_beginning_with_id() {
$p1 = self::factory()->post->create( array(
'post_type' => 'page',
'post_title' => 'Foo',
'post_name' => 'foo',
) );
$p2 = self::factory()->post->create( array(
'post_type' => 'page',
'post_title' => "$p1 Foo",
'post_name' => 'foo-2',
) );
$this->go_to( get_permalink( $p2 ) );
$this->assertTrue( is_page( $p2 ) );
$this->assertFalse( is_page( $p1 ) );
}
/**
* @ticket 35902
*/
public function test_is_page_should_not_match_numeric_id_to_post_name_beginning_with_id() {
$p1 = self::factory()->post->create( array(
'post_type' => 'page',
'post_title' => 'Foo',
'post_name' => 'foo',
) );
$p2 = self::factory()->post->create( array(
'post_type' => 'page',
'post_title' => 'Foo',
'post_name' => "$p1-foo",
) );
$this->go_to( get_permalink( $p2 ) );
$this->assertTrue( is_page( $p2 ) );
$this->assertFalse( is_page( $p1 ) );
}
/**
* @ticket 35902
*/
public function test_is_single_should_not_match_numeric_id_to_post_title_beginning_with_id() {
$p1 = self::factory()->post->create( array(
'post_type' => 'post',
'post_title' => 'Foo',
'post_name' => 'foo',
) );
$p2 = self::factory()->post->create( array(
'post_type' => 'post',
'post_title' => "$p1 Foo",
'post_name' => 'foo-2',
) );
$this->go_to( get_permalink( $p2 ) );
$this->assertTrue( is_single( $p2 ) );
$this->assertFalse( is_single( $p1 ) );
}
/**
* @ticket 35902
*/
public function test_is_single_should_not_match_numeric_id_to_post_name_beginning_with_id() {
$p1 = self::factory()->post->create( array(
'post_type' => 'post',
'post_title' => 'Foo',
'post_name' => 'foo',
) );
$p2 = self::factory()->post->create( array(
'post_type' => 'post',
'post_title' => 'Foo',
'post_name' => "$p1-foo",
) );
$this->go_to( get_permalink( $p2 ) );
$this->assertTrue( is_single( $p2 ) );
$this->assertFalse( is_single( $p1 ) );
}
}