| | 1 | <?php |
| | 2 | /** |
| | 3 | * Testing the search columns support in `WP_Query`. |
| | 4 | * |
| | 5 | * @package WordPress\UnitTests |
| | 6 | * @since 5.0.0 |
| | 7 | */ |
| | 8 | |
| | 9 | /** |
| | 10 | * Test cases for the search columns feature. |
| | 11 | * |
| | 12 | * @group query |
| | 13 | * @group search |
| | 14 | * |
| | 15 | * @since 5.0.0 |
| | 16 | */ |
| | 17 | class Tests_Query_SearchColumns extends WP_UnitTestCase { |
| | 18 | /** |
| | 19 | * The post ID of the first fixture post. |
| | 20 | * |
| | 21 | * @since 5.0.0 |
| | 22 | * @var int $pid1 |
| | 23 | */ |
| | 24 | protected static $pid1; |
| | 25 | |
| | 26 | /** |
| | 27 | * The post ID of the second fixture post. |
| | 28 | * |
| | 29 | * @since 5.0.0 |
| | 30 | * @var int $pid2 |
| | 31 | */ |
| | 32 | protected static $pid2; |
| | 33 | |
| | 34 | /** |
| | 35 | * The post ID of the third fixture post. |
| | 36 | * |
| | 37 | * @since 5.0.0 |
| | 38 | * @var int $pid3 |
| | 39 | */ |
| | 40 | protected static $pid3; |
| | 41 | |
| | 42 | /** |
| | 43 | * Create posts fixtures. |
| | 44 | * |
| | 45 | * @param WP_UnitTest_Factory $factory The factory instance. |
| | 46 | */ |
| | 47 | public static function wpSetUpBeforeClass( $factory ) { |
| | 48 | self::$pid1 = self::factory()->post->create( |
| | 49 | array( |
| | 50 | 'post_status' => 'publish', |
| | 51 | 'post_title' => 'foo title', |
| | 52 | 'post_excerpt' => 'foo excerpt', |
| | 53 | 'post_content' => 'foo content', |
| | 54 | ) |
| | 55 | ); |
| | 56 | self::$pid2 = self::factory()->post->create( |
| | 57 | array( |
| | 58 | 'post_status' => 'publish', |
| | 59 | 'post_title' => 'bar title', |
| | 60 | 'post_excerpt' => 'foo bar excerpt', |
| | 61 | 'post_content' => 'foo bar content', |
| | 62 | ) |
| | 63 | ); |
| | 64 | |
| | 65 | self::$pid3 = self::factory()->post->create( |
| | 66 | array( |
| | 67 | 'post_status' => 'publish', |
| | 68 | 'post_title' => 'baz title', |
| | 69 | 'post_excerpt' => 'baz bar excerpt', |
| | 70 | 'post_content' => 'baz bar foo content', |
| | 71 | ) |
| | 72 | ); |
| | 73 | } |
| | 74 | |
| | 75 | /** |
| | 76 | * The search should use default search columns when search columns are empty. |
| | 77 | */ |
| | 78 | public function test_s_should_use_default_search_columns_when_empty_search_columns() { |
| | 79 | $q = new WP_Query( |
| | 80 | array( |
| | 81 | 's' => 'foo', |
| | 82 | 'search_columns' => array(), |
| | 83 | 'fields' => 'ids', |
| | 84 | ) |
| | 85 | ); |
| | 86 | |
| | 87 | $this->assertContains( 'post_title', $q->request ); |
| | 88 | $this->assertContains( 'post_excerpt', $q->request ); |
| | 89 | $this->assertContains( 'post_content', $q->request ); |
| | 90 | $this->assertEqualSets( array( self::$pid1, self::$pid2, self::$pid3 ), $q->posts ); |
| | 91 | } |
| | 92 | |
| | 93 | /** |
| | 94 | * The search should support the post_title search column. |
| | 95 | */ |
| | 96 | public function test_s_should_support_post_title_search_column() { |
| | 97 | $q = new WP_Query( |
| | 98 | array( |
| | 99 | 's' => 'foo', |
| | 100 | 'search_columns' => array( 'post_title' ), |
| | 101 | 'fields' => 'ids', |
| | 102 | ) |
| | 103 | ); |
| | 104 | |
| | 105 | $this->assertEqualSets( array( self::$pid1 ), $q->posts ); |
| | 106 | } |
| | 107 | |
| | 108 | /** |
| | 109 | * The search should support the `post_excerpt` search column. |
| | 110 | */ |
| | 111 | public function test_s_should_support_post_excerpt_search_column() { |
| | 112 | $q = new WP_Query( |
| | 113 | array( |
| | 114 | 's' => 'foo', |
| | 115 | 'search_columns' => array( 'post_excerpt' ), |
| | 116 | 'fields' => 'ids', |
| | 117 | ) |
| | 118 | ); |
| | 119 | |
| | 120 | $this->assertEqualSets( array( self::$pid1, self::$pid2 ), $q->posts ); |
| | 121 | } |
| | 122 | |
| | 123 | /** |
| | 124 | * The search should support the `post_content` search column. |
| | 125 | */ |
| | 126 | public function test_s_should_support_post_content_search_column() { |
| | 127 | $q = new WP_Query( |
| | 128 | array( |
| | 129 | 's' => 'foo', |
| | 130 | 'search_columns' => array( 'post_content' ), |
| | 131 | 'fields' => 'ids', |
| | 132 | ) |
| | 133 | ); |
| | 134 | $this->assertEqualSets( array( self::$pid1, self::$pid2, self::$pid3 ), $q->posts ); |
| | 135 | } |
| | 136 | |
| | 137 | /** |
| | 138 | * The search should support the `post_title` and `post_excerpt` search columns together. |
| | 139 | */ |
| | 140 | public function test_s_should_support_post_title_and_post_excerpt_search_columns() { |
| | 141 | $q = new WP_Query( |
| | 142 | array( |
| | 143 | 's' => 'foo', |
| | 144 | 'search_columns' => array( 'post_title', 'post_excerpt' ), |
| | 145 | 'fields' => 'ids', |
| | 146 | ) |
| | 147 | ); |
| | 148 | |
| | 149 | $this->assertEqualSets( array( self::$pid1, self::$pid2 ), $q->posts ); |
| | 150 | } |
| | 151 | |
| | 152 | /** |
| | 153 | * The search should support the `post_title` and `post_content` search columns together. |
| | 154 | */ |
| | 155 | public function test_s_should_support_post_title_and_post_content_search_columns() { |
| | 156 | $q = new WP_Query( |
| | 157 | array( |
| | 158 | 's' => 'foo', |
| | 159 | 'search_columns' => array( 'post_title', 'post_content' ), |
| | 160 | 'fields' => 'ids', |
| | 161 | ) |
| | 162 | ); |
| | 163 | |
| | 164 | $this->assertEqualSets( array( self::$pid1, self::$pid2, self::$pid3 ), $q->posts ); |
| | 165 | } |
| | 166 | |
| | 167 | /** |
| | 168 | * The search should support the `post_excerpt` and `post_content` search columns together. |
| | 169 | */ |
| | 170 | public function test_s_should_support_post_excerpt_and_post_content_search_columns() { |
| | 171 | $q = new WP_Query( |
| | 172 | array( |
| | 173 | 's' => 'foo', |
| | 174 | 'search_columns' => array( 'post_excerpt', 'post_content' ), |
| | 175 | 'fields' => 'ids', |
| | 176 | ) |
| | 177 | ); |
| | 178 | |
| | 179 | $this->assertEqualSets( array( self::$pid1, self::$pid2, self::$pid3 ), $q->posts ); |
| | 180 | } |
| | 181 | |
| | 182 | /** |
| | 183 | * The search should support the `post_title`, `post_excerpt` and `post_content` search columns together. |
| | 184 | */ |
| | 185 | public function test_s_should_support_post_title_and_post_excerpt_and_post_content_search_columns() { |
| | 186 | $q = new WP_Query( |
| | 187 | array( |
| | 188 | 's' => 'foo', |
| | 189 | 'search_columns' => array( 'post_title', 'post_excerpt', 'post_content' ), |
| | 190 | 'fields' => 'ids', |
| | 191 | ) |
| | 192 | ); |
| | 193 | |
| | 194 | $this->assertEqualSets( array( self::$pid1, self::$pid2, self::$pid3 ), $q->posts ); |
| | 195 | } |
| | 196 | |
| | 197 | /** |
| | 198 | * The search should use default support columns when using a non existing search column. |
| | 199 | */ |
| | 200 | public function test_s_should_use_default_search_columns_when_using_non_existing_search_column() { |
| | 201 | $q = new WP_Query( |
| | 202 | array( |
| | 203 | 's' => 'foo', |
| | 204 | 'search_columns' => array( 'post_non_existing_column' ), |
| | 205 | 'fields' => 'ids', |
| | 206 | ) |
| | 207 | ); |
| | 208 | |
| | 209 | $this->assertContains( 'post_title', $q->request ); |
| | 210 | $this->assertContains( 'post_excerpt', $q->request ); |
| | 211 | $this->assertContains( 'post_content', $q->request ); |
| | 212 | $this->assertEqualSets( array( self::$pid1, self::$pid2, self::$pid3 ), $q->posts ); |
| | 213 | } |
| | 214 | |
| | 215 | /** |
| | 216 | * The search should support ignore a non existing search column when used together with a supported one. |
| | 217 | */ |
| | 218 | public function test_s_should_ignore_non_existing_search_column_when_used_with_supported_one() { |
| | 219 | $q = new WP_Query( |
| | 220 | array( |
| | 221 | 's' => 'foo', |
| | 222 | 'search_columns' => array( 'post_title', 'post_non_existing_column' ), |
| | 223 | 'fields' => 'ids', |
| | 224 | ) |
| | 225 | ); |
| | 226 | |
| | 227 | $this->assertEqualSets( array( self::$pid1 ), $q->posts ); |
| | 228 | } |
| | 229 | |
| | 230 | /** |
| | 231 | * The search should support search columns when searching multiple terms. |
| | 232 | */ |
| | 233 | public function test_s_should_support_search_columns_when_searching_multiple_terms() { |
| | 234 | $q = new WP_Query( |
| | 235 | array( |
| | 236 | 's' => 'foo bar', |
| | 237 | 'search_columns' => array( 'post_content' ), |
| | 238 | 'fields' => 'ids', |
| | 239 | ) |
| | 240 | ); |
| | 241 | |
| | 242 | $this->assertEqualSets( array( self::$pid2, self::$pid3 ), $q->posts ); |
| | 243 | } |
| | 244 | |
| | 245 | /** |
| | 246 | * The search should support search columns when searching for a sentence. |
| | 247 | */ |
| | 248 | public function test_s_should_support_search_columns_when_sentence_true() { |
| | 249 | $q = new WP_Query( |
| | 250 | array( |
| | 251 | 's' => 'bar foo', |
| | 252 | 'search_columns' => array( 'post_content' ), |
| | 253 | 'sentence' => true, |
| | 254 | 'fields' => 'ids', |
| | 255 | ) |
| | 256 | ); |
| | 257 | |
| | 258 | $this->assertEqualSets( array( self::$pid3 ), $q->posts ); |
| | 259 | } |
| | 260 | |
| | 261 | /** |
| | 262 | * The search should support search columns when searching for a sentence. |
| | 263 | */ |
| | 264 | public function test_s_should_support_search_columns_when_sentence_false() { |
| | 265 | $q = new WP_Query( |
| | 266 | array( |
| | 267 | 's' => 'bar foo', |
| | 268 | 'search_columns' => array( 'post_content' ), |
| | 269 | 'sentence' => false, |
| | 270 | 'fields' => 'ids', |
| | 271 | ) |
| | 272 | ); |
| | 273 | |
| | 274 | $this->assertEqualSets( array( self::$pid2, self::$pid3 ), $q->posts ); |
| | 275 | } |
| | 276 | |
| | 277 | /** |
| | 278 | * The search should support search columns when searched with a term exclusion. |
| | 279 | */ |
| | 280 | public function test_s_should_support_search_columns_when_searched_with_term_exclusion() { |
| | 281 | $q = new WP_Query( |
| | 282 | array( |
| | 283 | 's' => 'bar -baz', |
| | 284 | 'search_columns' => array( 'post_excerpt', 'post_content' ), |
| | 285 | 'fields' => 'ids', |
| | 286 | ) |
| | 287 | ); |
| | 288 | |
| | 289 | $this->assertEqualSets( array( self::$pid2 ), $q->posts ); |
| | 290 | } |
| | 291 | |
| | 292 | /** |
| | 293 | * The search columns should be filterable with the `post_search_columns` filter. |
| | 294 | */ |
| | 295 | public function test_search_columns_should_be_filterable() { |
| | 296 | add_filter( 'post_search_columns', array( $this, 'post_supported_search_column' ), 10, 3 ); |
| | 297 | $q = new WP_Query( |
| | 298 | array( |
| | 299 | 's' => 'foo', |
| | 300 | 'fields' => 'ids', |
| | 301 | ) |
| | 302 | ); |
| | 303 | remove_filter( 'post_search_columns', array( $this, 'post_supported_search_column' ) ); |
| | 304 | |
| | 305 | $this->assertEqualSets( array( self::$pid1 ), $q->posts ); |
| | 306 | } |
| | 307 | |
| | 308 | /** |
| | 309 | * Filter callback that sets a supported search column. |
| | 310 | * |
| | 311 | * @param string[] $search_columns Array of column names to be searched. |
| | 312 | * @param string $search Text being searched. |
| | 313 | * @param WP_Query $wp_query The current WP_Query instance. |
| | 314 | * @return string[] $search_columns Array of column names to be searched. |
| | 315 | */ |
| | 316 | public function post_supported_search_column( $search_columns, $search, $wp_query ) { |
| | 317 | $search_columns = array( 'post_title' ); |
| | 318 | return $search_columns; |
| | 319 | } |
| | 320 | |
| | 321 | /** |
| | 322 | * The search columns should not be filterable when using non supported search columns. |
| | 323 | */ |
| | 324 | public function test_search_columns_should_not_filterable_when_non_supported_search_columns() { |
| | 325 | add_filter( 'post_search_columns', array( $this, 'post_non_supported_search_column' ), 10, 3 ); |
| | 326 | $q = new WP_Query( |
| | 327 | array( |
| | 328 | 's' => 'foo', |
| | 329 | 'fields' => 'ids', |
| | 330 | ) |
| | 331 | ); |
| | 332 | remove_filter( 'post_search_columns', array( $this, 'post_non_supported_search_column' ) ); |
| | 333 | |
| | 334 | $this->assertNotContains( 'post_name', $q->request ); |
| | 335 | $this->assertEqualSets( array( self::$pid1, self::$pid2, self::$pid3 ), $q->posts ); |
| | 336 | } |
| | 337 | |
| | 338 | /** |
| | 339 | * Filter callback that sets an existing but non supported search column. |
| | 340 | * |
| | 341 | * @param string[] $search_columns Array of column names to be searched. |
| | 342 | * @param string $search Text being searched. |
| | 343 | * @param WP_Query $wp_query The current WP_Query instance. |
| | 344 | * @return string[] $search_columns Array of column names to be searched. |
| | 345 | */ |
| | 346 | public function post_non_supported_search_column( $search_columns, $search, $wp_query ) { |
| | 347 | $search_columns = array( 'post_name' ); |
| | 348 | return $search_columns; |
| | 349 | } |
| | 350 | |
| | 351 | /** |
| | 352 | * The search columns should not be filterable with non existing search columns. |
| | 353 | */ |
| | 354 | public function xtest_search_columns_should_not_filterable_non_existing_search_column() { |
| | 355 | add_filter( 'post_search_columns', array( $this, 'post_non_existing_search_column' ), 10, 3 ); |
| | 356 | $q = new WP_Query( |
| | 357 | array( |
| | 358 | 's' => 'foo', |
| | 359 | 'fields' => 'ids', |
| | 360 | ) |
| | 361 | ); |
| | 362 | remove_filter( 'post_search_columns', array( $this, 'post_non_existing_search_column' ) ); |
| | 363 | |
| | 364 | $this->assertNotContains( 'post_non_existing_column', $q->request ); |
| | 365 | $this->assertEqualSets( array( self::$pid1, self::$pid2, self::$pid3 ), $q->posts ); |
| | 366 | } |
| | 367 | |
| | 368 | /** |
| | 369 | * Filter callback that sets a non existing search column. |
| | 370 | * |
| | 371 | * @param string[] $search_columns Array of column names to be searched. |
| | 372 | * @param string $search Text being searched. |
| | 373 | * @param WP_Query $wp_query The current WP_Query instance. |
| | 374 | * @return string[] $search_columns Array of column names to be searched. |
| | 375 | */ |
| | 376 | public function post_non_existing_search_column( $search_columns, $search, $wp_query ) { |
| | 377 | $search_columns = array( 'post_non_existing_column' ); |
| | 378 | return $search_columns; |
| | 379 | } |
| | 380 | |
| | 381 | } |