Changeset 54692 for branches/6.1/tests/phpunit/tests/query/cacheResults.php
- Timestamp:
- 10/25/2022 06:31:35 PM (2 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/6.1/tests/phpunit/tests/query/cacheResults.php
r54634 r54692 35 35 36 36 /** 37 * @var array 38 */ 39 protected $cache_args; 40 41 /** 42 * @var string 43 */ 44 protected $new_request; 37 * For testing test_generate_cache_key() includes a test containing the 38 * placeholder within the generated SQL query. 39 * 40 * @var bool 41 */ 42 public static $sql_placeholder_cache_key_tested = false; 43 44 /** 45 * For testing test_generate_cache_key() includes a test containing the 46 * placeholder within the generated WP_Query variables. 47 * 48 * @var bool 49 */ 50 public static $wp_query_placeholder_cache_key_tested = false; 45 51 46 52 public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) { … … 68 74 } 69 75 70 function set_up() { 71 parent::set_up(); 72 $this->cache_args = null; 73 $this->new_request = null; 74 add_filter( 'wp_query_cache_key', array( $this, 'filter_wp_query_cache_key' ), 15, 3 ); 75 } 76 77 /** 76 /** 77 * Ensure cache keys are generated without WPDB placeholders. 78 * 79 * @ticket 56802 80 * 81 * @covers WP_Query::generate_cache_key 82 * 78 83 * @dataProvider data_query_cache 84 */ 85 public function test_generate_cache_key( $args ) { 86 global $wpdb; 87 $query1 = new WP_Query(); 88 $query1->query( $args ); 89 90 $query_vars = $query1->query_vars; 91 $request = $query1->request; 92 $request_no_placeholder = $wpdb->remove_placeholder_escape( $request ); 93 94 $this->assertStringNotContainsString( $wpdb->placeholder_escape(), $request_no_placeholder, 'Placeholder escape should be removed from the modified request.' ); 95 96 if ( str_contains( $request, $wpdb->placeholder_escape() ) ) { 97 self::$sql_placeholder_cache_key_tested = true; 98 } 99 100 if ( str_contains( serialize( $query_vars ), $wpdb->placeholder_escape() ) ) { 101 self::$wp_query_placeholder_cache_key_tested = true; 102 } 103 104 $reflection = new ReflectionMethod( $query1, 'generate_cache_key' ); 105 $reflection->setAccessible( true ); 106 107 $cache_key_1 = $reflection->invoke( $query1, $query_vars, $request ); 108 $cache_key_2 = $reflection->invoke( $query1, $query_vars, $request_no_placeholder ); 109 110 $this->assertSame( $cache_key_1, $cache_key_2, 'Cache key differs when using wpdb placeholder.' ); 111 } 112 113 /** 114 * Ensure cache keys tests include WPDB placeholder in SQL Query. 115 * 116 * @ticket 56802 117 * 118 * @covers WP_Query::generate_cache_key 119 * 120 * @depends test_generate_cache_key 121 */ 122 public function test_sql_placeholder_cache_key_tested() { 123 $this->assertTrue( self::$sql_placeholder_cache_key_tested, 'Cache key containing WPDB placeholder in SQL query was not tested.' ); 124 } 125 126 /** 127 * Ensure cache keys tests include WPDB placeholder in WP_Query arguments. 128 * 129 * This test mainly covers the search query which generates the `search_orderby_title` 130 * query_var in WP_Query. 131 * 132 * @ticket 56802 133 * 134 * @covers WP_Query::generate_cache_key 135 * 136 * @depends test_generate_cache_key 137 */ 138 public function test_wp_query_placeholder_cache_key_tested() { 139 $this->assertTrue( self::$wp_query_placeholder_cache_key_tested, 'Cache key containing WPDB placeholder in WP_Query arguments was not tested.' ); 140 } 141 142 /** 143 * Ensure cache keys are generated without WPDB placeholders. 144 * 145 * @ticket 56802 146 * 147 * @covers WP_Query::generate_cache_key 148 */ 149 public function test_generate_cache_key_placeholder() { 150 global $wpdb; 151 $query1 = new WP_Query(); 152 $query1->query( array() ); 153 154 $query_vars = $query1->query_vars; 155 $request = $query1->request; 156 $query_vars['test']['nest'] = '%'; 157 $query_vars['test2']['nest']['nest']['nest'] = '%'; 158 $this->assertStringNotContainsString( $wpdb->placeholder_escape(), serialize( $query_vars ), 'Query vars should not contain the wpdb placeholder.' ); 159 160 $reflection = new ReflectionMethod( $query1, 'generate_cache_key' ); 161 $reflection->setAccessible( true ); 162 163 $cache_key_1 = $reflection->invoke( $query1, $query_vars, $request ); 164 165 $query_vars['test']['nest'] = $wpdb->placeholder_escape(); 166 $query_vars['test2']['nest']['nest']['nest'] = $wpdb->placeholder_escape(); 167 $this->assertStringContainsString( $wpdb->placeholder_escape(), serialize( $query_vars ), 'Query vars should not contain the wpdb placeholder.' ); 168 169 $cache_key_2 = $reflection->invoke( $query1, $query_vars, $request ); 170 171 $this->assertSame( $cache_key_1, $cache_key_2, 'Cache key differs when using wpdb placeholder.' ); 172 } 173 174 /** 175 * @dataProvider data_query_cache 79 176 * @ticket 22176 80 177 */ 81 178 public function test_query_cache( $args ) { 82 global $wpdb;83 84 179 $query1 = new WP_Query(); 85 180 $posts1 = $query1->query( $args ); 86 87 $placeholder = $wpdb->placeholder_escape();88 $this->assertNotEmpty( $this->new_request, 'Check new request is not empty' );89 $this->assertStringNotContainsString( $placeholder, $this->new_request, 'Check if request does not contain placeholder' );90 $this->assertStringNotContainsString( $placeholder, wp_json_encode( $this->cache_args ), 'Check if cache arrays does not contain placeholder' );91 181 92 182 $queries_before = get_num_queries(); … … 228 318 ), 229 319 ), 320 'cache nested meta query search' => array( 321 'args' => array( 322 'cache_results' => true, 323 'meta_query' => array( 324 'relation' => 'AND', 325 array( 326 'key' => 'color', 327 'value' => '00', 328 'compare' => 'LIKE', 329 ), 330 array( 331 'relation' => 'OR', 332 array( 333 'key' => 'color', 334 'value' => '00', 335 'compare' => 'LIKE', 336 ), 337 array( 338 'relation' => 'AND', 339 array( 340 'key' => 'wp_test_suite', 341 'value' => '56802', 342 'compare' => 'LIKE', 343 ), 344 array( 345 'key' => 'wp_test_suite_too', 346 'value' => '56802', 347 'compare' => 'LIKE', 348 ), 349 ), 350 ), 351 ), 352 ), 353 ), 230 354 'cache meta query not search' => array( 231 355 'args' => array( … … 888 1012 } 889 1013 890 public function filter_wp_query_cache_key( $cache_key, $cache_args, $new_request ) {891 $this->cache_args = $cache_args;892 $this->new_request = $new_request;893 894 return $cache_key;895 }896 897 1014 /** 898 1015 * @ticket 22176
Note: See TracChangeset
for help on using the changeset viewer.