Make WordPress Core


Ignore:
Timestamp:
02/23/2016 02:20:28 AM (8 years ago)
Author:
boonebgorges
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/query/conditionals.php

    r36138 r36625  
    10181018        $this->assertTrue( is_page_template( array('test.php', 'example.php') ) );
    10191019    }
     1020
     1021    /**
     1022     * @ticket 35902
     1023     */
     1024    public function test_is_attachment_should_not_match_numeric_id_to_post_title_beginning_with_id() {
     1025        $p1 = self::factory()->post->create( array(
     1026            'post_type' => 'attachment',
     1027            'post_title' => 'Foo',
     1028            'post_name' => 'foo',
     1029        ) );
     1030        $p2 = self::factory()->post->create( array(
     1031            'post_type' => 'attachment',
     1032            'post_title' => "$p1 Foo",
     1033            'post_name' => 'foo-2',
     1034        ) );
     1035
     1036        $this->go_to( get_permalink( $p2 ) );
     1037
     1038        $this->assertTrue( is_attachment( $p2 ) );
     1039        $this->assertFalse( is_attachment( $p1 ) );
     1040    }
     1041
     1042    /**
     1043     * @ticket 35902
     1044     */
     1045    public function test_is_attachment_should_not_match_numeric_id_to_post_name_beginning_with_id() {
     1046        $p1 = self::factory()->post->create( array(
     1047            'post_type' => 'attachment',
     1048            'post_title' => 'Foo',
     1049            'post_name' => 'foo',
     1050        ) );
     1051        $p2 = self::factory()->post->create( array(
     1052            'post_type' => 'attachment',
     1053            'post_title' => 'Foo',
     1054            'post_name' => "$p1-foo",
     1055        ) );
     1056
     1057        $this->go_to( get_permalink( $p2 ) );
     1058
     1059        $this->assertTrue( is_attachment( $p2 ) );
     1060        $this->assertFalse( is_attachment( $p1 ) );
     1061    }
     1062
     1063    /**
     1064     * @ticket 35902
     1065     */
     1066    public function test_is_author_should_not_match_numeric_id_to_nickname_beginning_with_id() {
     1067        $u1 = self::factory()->user->create( array(
     1068            'nickname' => 'Foo',
     1069            'user_nicename' => 'foo',
     1070        ) );
     1071        $u2 = self::factory()->user->create( array(
     1072            'nickname' => "$u1 Foo",
     1073            'user_nicename' => 'foo-2',
     1074        ) );
     1075
     1076        $this->go_to( get_author_posts_url( $u2 ) );
     1077
     1078        $this->assertTrue( is_author( $u2 ) );
     1079        $this->assertFalse( is_author( $u1 ) );
     1080    }
     1081
     1082    /**
     1083     * @ticket 35902
     1084     */
     1085    public function test_is_author_should_not_match_numeric_id_to_user_nicename_beginning_with_id() {
     1086        $u1 = self::factory()->user->create( array(
     1087            'nickname' => 'Foo',
     1088            'user_nicename' => 'foo',
     1089        ) );
     1090        $u2 = self::factory()->user->create( array(
     1091            'nickname' => 'Foo',
     1092            'user_nicename' => "$u1-foo",
     1093        ) );
     1094
     1095        $this->go_to( get_author_posts_url( $u2 ) );
     1096
     1097        $this->assertTrue( is_author( $u2 ) );
     1098        $this->assertFalse( is_author( $u1 ) );
     1099    }
     1100
     1101    /**
     1102     * @ticket 35902
     1103     */
     1104    public function test_is_category_should_not_match_numeric_id_to_name_beginning_with_id() {
     1105        $t1 = self::factory()->term->create( array(
     1106            'taxonomy' => 'category',
     1107            'slug' => 'foo',
     1108            'name' => 'foo',
     1109        ) );
     1110        $t2 = self::factory()->term->create( array(
     1111            'taxonomy' => 'category',
     1112            'slug' => "$t1-foo",
     1113            'name' => 'foo 2',
     1114        ) );
     1115
     1116        $this->go_to( get_term_link( $t2 ) );
     1117
     1118        $this->assertTrue( is_category( $t2 ) );
     1119        $this->assertFalse( is_category( $t1 ) );
     1120    }
     1121
     1122    /**
     1123     * @ticket 35902
     1124     */
     1125    public function test_is_category_should_not_match_numeric_id_to_slug_beginning_with_id() {
     1126        $t1 = self::factory()->term->create( array(
     1127            'taxonomy' => 'category',
     1128            'slug' => 'foo',
     1129            'name' => 'foo',
     1130        ) );
     1131        $t2 = self::factory()->term->create( array(
     1132            'taxonomy' => 'category',
     1133            'slug' => 'foo-2',
     1134            'name' => "$t1 foo",
     1135        ) );
     1136
     1137        $this->go_to( get_term_link( $t2 ) );
     1138
     1139        $this->assertTrue( is_category( $t2 ) );
     1140        $this->assertFalse( is_category( $t1 ) );
     1141    }
     1142
     1143    /**
     1144     * @ticket 35902
     1145     */
     1146    public function test_is_tag_should_not_match_numeric_id_to_name_beginning_with_id() {
     1147        $t1 = self::factory()->term->create( array(
     1148            'taxonomy' => 'post_tag',
     1149            'slug' => 'foo',
     1150            'name' => 'foo',
     1151        ) );
     1152        $t2 = self::factory()->term->create( array(
     1153            'taxonomy' => 'post_tag',
     1154            'slug' => "$t1-foo",
     1155            'name' => 'foo 2',
     1156        ) );
     1157
     1158        $this->go_to( get_term_link( $t2 ) );
     1159
     1160        $this->assertTrue( is_tag( $t2 ) );
     1161        $this->assertFalse( is_tag( $t1 ) );
     1162    }
     1163
     1164    /**
     1165     * @ticket 35902
     1166     */
     1167    public function test_is_tag_should_not_match_numeric_id_to_slug_beginning_with_id() {
     1168        $t1 = self::factory()->term->create( array(
     1169            'taxonomy' => 'post_tag',
     1170            'slug' => 'foo',
     1171            'name' => 'foo',
     1172        ) );
     1173        $t2 = self::factory()->term->create( array(
     1174            'taxonomy' => 'post_tag',
     1175            'slug' => 'foo-2',
     1176            'name' => "$t1 foo",
     1177        ) );
     1178
     1179        $this->go_to( get_term_link( $t2 ) );
     1180
     1181        $this->assertTrue( is_tag( $t2 ) );
     1182        $this->assertFalse( is_tag( $t1 ) );
     1183    }
     1184
     1185    /**
     1186     * @ticket 35902
     1187     */
     1188    public function test_is_page_should_not_match_numeric_id_to_post_title_beginning_with_id() {
     1189        $p1 = self::factory()->post->create( array(
     1190            'post_type' => 'page',
     1191            'post_title' => 'Foo',
     1192            'post_name' => 'foo',
     1193        ) );
     1194        $p2 = self::factory()->post->create( array(
     1195            'post_type' => 'page',
     1196            'post_title' => "$p1 Foo",
     1197            'post_name' => 'foo-2',
     1198        ) );
     1199
     1200        $this->go_to( get_permalink( $p2 ) );
     1201
     1202        $this->assertTrue( is_page( $p2 ) );
     1203        $this->assertFalse( is_page( $p1 ) );
     1204    }
     1205
     1206    /**
     1207     * @ticket 35902
     1208     */
     1209    public function test_is_page_should_not_match_numeric_id_to_post_name_beginning_with_id() {
     1210        $p1 = self::factory()->post->create( array(
     1211            'post_type' => 'page',
     1212            'post_title' => 'Foo',
     1213            'post_name' => 'foo',
     1214        ) );
     1215        $p2 = self::factory()->post->create( array(
     1216            'post_type' => 'page',
     1217            'post_title' => 'Foo',
     1218            'post_name' => "$p1-foo",
     1219        ) );
     1220
     1221        $this->go_to( get_permalink( $p2 ) );
     1222
     1223        $this->assertTrue( is_page( $p2 ) );
     1224        $this->assertFalse( is_page( $p1 ) );
     1225    }
     1226
     1227    /**
     1228     * @ticket 35902
     1229     */
     1230    public function test_is_single_should_not_match_numeric_id_to_post_title_beginning_with_id() {
     1231        $p1 = self::factory()->post->create( array(
     1232            'post_type' => 'post',
     1233            'post_title' => 'Foo',
     1234            'post_name' => 'foo',
     1235        ) );
     1236        $p2 = self::factory()->post->create( array(
     1237            'post_type' => 'post',
     1238            'post_title' => "$p1 Foo",
     1239            'post_name' => 'foo-2',
     1240        ) );
     1241
     1242        $this->go_to( get_permalink( $p2 ) );
     1243
     1244        $this->assertTrue( is_single( $p2 ) );
     1245        $this->assertFalse( is_single( $p1 ) );
     1246    }
     1247
     1248    /**
     1249     * @ticket 35902
     1250     */
     1251    public function test_is_single_should_not_match_numeric_id_to_post_name_beginning_with_id() {
     1252        $p1 = self::factory()->post->create( array(
     1253            'post_type' => 'post',
     1254            'post_title' => 'Foo',
     1255            'post_name' => 'foo',
     1256        ) );
     1257        $p2 = self::factory()->post->create( array(
     1258            'post_type' => 'post',
     1259            'post_title' => 'Foo',
     1260            'post_name' => "$p1-foo",
     1261        ) );
     1262
     1263        $this->go_to( get_permalink( $p2 ) );
     1264
     1265        $this->assertTrue( is_single( $p2 ) );
     1266        $this->assertFalse( is_single( $p1 ) );
     1267    }
    10201268}
Note: See TracChangeset for help on using the changeset viewer.