@@ -41,6 +41,14 @@ class Tests_Query_SearchColumns extends WP_UnitTestCase {
4141 */
4242 protected static $ pid3 ;
4343
44+ /**
45+ * The post ID of the fixture post used for slug search tests.
46+ *
47+ * @since 7.1.0
48+ * @var int $pid_slug
49+ */
50+ protected static $ pid_slug ;
51+
4452 /**
4553 * Create posts fixtures.
4654 *
@@ -72,6 +80,16 @@ public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
7280 'post_content ' => 'baz bar foo content ' ,
7381 )
7482 );
83+
84+ self ::$ pid_slug = $ factory ->post ->create (
85+ array (
86+ 'post_status ' => 'publish ' ,
87+ 'post_title ' => 'Taco ' ,
88+ 'post_name ' => 'burrito ' ,
89+ 'post_content ' => 'Enchilada ' ,
90+ 'post_excerpt ' => 'Torta ' ,
91+ )
92+ );
7593 }
7694
7795 /**
@@ -353,6 +371,7 @@ public function post_supported_search_column( $search_columns, $search, $wp_quer
353371 * Tests that search columns ignores non-supported search columns from the `post_search_columns` filter.
354372 *
355373 * @ticket 43867
374+ * @ticket 20044
356375 */
357376 public function test_search_columns_should_not_be_filterable_with_non_supported_search_columns () {
358377 add_filter ( 'post_search_columns ' , array ( $ this , 'post_non_supported_search_column ' ), 10 , 3 );
@@ -363,7 +382,7 @@ public function test_search_columns_should_not_be_filterable_with_non_supported_
363382 )
364383 );
365384
366- $ this ->assertStringNotContainsString ( 'post_name ' , $ q ->request , "SQL request shouldn't contain post_name string. " );
385+ $ this ->assertStringNotContainsString ( 'post_author ' , $ q ->request , "SQL request shouldn't contain post_author string. " );
367386 $ this ->assertSameSets ( array ( self ::$ pid1 , self ::$ pid2 , self ::$ pid3 ), $ q ->posts , 'Query results should be equal to the set. ' );
368387 }
369388
@@ -376,7 +395,7 @@ public function test_search_columns_should_not_be_filterable_with_non_supported_
376395 * @return string[] $search_columns Array of column names to be searched.
377396 */
378397 public function post_non_supported_search_column ( $ search_columns , $ search , $ wp_query ) {
379- $ search_columns = array ( 'post_name ' );
398+ $ search_columns = array ( 'post_author ' );
380399 return $ search_columns ;
381400 }
382401
@@ -410,4 +429,66 @@ public function post_non_existing_search_column( $search_columns, $search, $wp_q
410429 $ search_columns = array ( 'post_non_existing_column ' );
411430 return $ search_columns ;
412431 }
432+
433+ /**
434+ * Tests that `post_name` is not searched by default.
435+ *
436+ * @ticket 20044
437+ */
438+ public function test_s_should_not_search_post_name_by_default () {
439+ $ q = new WP_Query (
440+ array (
441+ 's ' => 'burrito ' ,
442+ 'fields ' => 'ids ' ,
443+ )
444+ );
445+
446+ $ this ->assertSame ( array (), $ q ->posts );
447+ }
448+
449+ /**
450+ * Tests that search supports the `post_name` search column via the `post_search_columns` filter.
451+ *
452+ * @ticket 20044
453+ */
454+ public function test_s_should_support_post_name_search_column_via_filter () {
455+ add_filter ( 'post_search_columns ' , array ( $ this , 'filter_add_post_name_search_column ' ) );
456+ $ q = new WP_Query (
457+ array (
458+ 's ' => 'burrito ' ,
459+ 'fields ' => 'ids ' ,
460+ )
461+ );
462+ remove_filter ( 'post_search_columns ' , array ( $ this , 'filter_add_post_name_search_column ' ) );
463+
464+ $ this ->assertSame ( array ( self ::$ pid_slug ), $ q ->posts );
465+ }
466+
467+ /**
468+ * Tests that search supports the `post_name` search column via the `search_columns` query var.
469+ *
470+ * @ticket 20044
471+ */
472+ public function test_s_should_support_post_name_search_column_via_query_var () {
473+ $ q = new WP_Query (
474+ array (
475+ 's ' => 'burrito ' ,
476+ 'fields ' => 'ids ' ,
477+ 'search_columns ' => array ( 'post_name ' ),
478+ )
479+ );
480+
481+ $ this ->assertSame ( array ( self ::$ pid_slug ), $ q ->posts );
482+ }
483+
484+ /**
485+ * Filter callback that adds `post_name` to the search columns.
486+ *
487+ * @param string[] $search_columns Array of column names to be searched.
488+ * @return string[] $search_columns Array of column names to be searched.
489+ */
490+ public function filter_add_post_name_search_column ( $ search_columns ) {
491+ $ search_columns [] = 'post_name ' ;
492+ return $ search_columns ;
493+ }
413494}
0 commit comments