[wp-trac] [WordPress Trac] #63635: Tests: rest-posts-controller hooks blow under certain circumstances

WordPress Trac noreply at wordpress.org
Mon Jun 30 00:28:42 UTC 2025


#63635: Tests: rest-posts-controller hooks blow under certain circumstances
--------------------------+-----------------------------
 Reporter:  SirLouen      |      Owner:  (none)
     Type:  defect (bug)  |     Status:  new
 Priority:  normal        |  Milestone:  Awaiting Review
Component:  REST API      |    Version:  4.8
 Severity:  blocker       |   Keywords:  needs-patch
  Focuses:  tests         |
--------------------------+-----------------------------
 In this revision [39079] it was introduced some code

 In this file `tests/phpunit/tests/rest-api/rest-posts-controller.php`:


 {{{#!php
 <?php
         add_filter( 'rest_pre_dispatch', array( $this,
 'wpSetUpBeforeRequest' ), 10, 3 );
         add_filter( 'posts_clauses', array( $this, 'save_posts_clauses' ),
 10, 2 );
 }

 public function wpSetUpBeforeRequest( $result, $server, $request ) {
         $this->posts_clauses = array();
         return $result;
 }
 public function save_posts_clauses( $orderby, $query ) {
         if ( 'revision' !== $query->query_vars['post_type'] ) {
                 array_push( $this->posts_clauses, $orderby );
         }
         return $orderby;
 }
 }}}


 If we introduce the following test in this file, it will blow:

 {{{#!php
 public function test_blowing_example() {
         $this->go_to( get_permalink( self::$post_id ) );
 }
 }}}

 The reason is that `rest_pre_dispatch` is running after `posts_clauses`,
 `$this->posts_clauses = array();` is not being set on time and the
 `array_push` blows because `$this->posts_clauses` is `null`

 Why severity `blocker`? Because I cannot continue working on another
 report until this is solved, so technically is a blocker report.

 Possible solutions that come to my mind:

 1. Changing the filter to another that runs earlier for
 `wpSetUpBeforeRequest` (or later `save_posts_clauses`). I can't think of
 which filter would be the most adequate
 2. Adding a `$this->posts_clauses` `empty()` conditional check in
 `save_posts_clauses` to ignore `array_push` in case it's empty (a possible
 safe option)

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/63635>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list