Changeset 51276 for trunk/tests/phpunit/tests/query/postStatus.php
- Timestamp:
- 06/30/2021 04:59:10 AM (3 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tests/phpunit/tests/query/postStatus.php
r49899 r51276 7 7 public static $editor_user_id; 8 8 public static $author_user_id; 9 public static $editor_private_post; 10 public static $author_private_post; 11 public static $editor_privatefoo_post; 12 public static $author_privatefoo_post; 9 public static $subscriber_user_id; 10 public static $post_ids; 13 11 14 12 public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) { 15 self::$editor_user_id = $factory->user->create( array( 'role' => 'editor' ) ); 16 self::$author_user_id = $factory->user->create( array( 'role' => 'author' ) ); 17 18 self::$editor_private_post = $factory->post->create( 13 self::$editor_user_id = $factory->user->create( array( 'role' => 'editor' ) ); 14 self::$author_user_id = $factory->user->create( array( 'role' => 'author' ) ); 15 self::$subscriber_user_id = $factory->user->create( array( 'role' => 'subscriber' ) ); 16 17 self::$post_ids['editor_private_post'] = $factory->post->create( 19 18 array( 20 19 'post_author' => self::$editor_user_id, … … 22 21 ) 23 22 ); 24 self::$ author_private_post= $factory->post->create(23 self::$post_ids['author_private_post'] = $factory->post->create( 25 24 array( 26 25 'post_author' => self::$author_user_id, … … 31 30 // Custom status with private=true. 32 31 register_post_status( 'privatefoo', array( 'private' => true ) ); 33 self::$ editor_privatefoo_post= $factory->post->create(32 self::$post_ids['editor_privatefoo_post'] = $factory->post->create( 34 33 array( 35 34 'post_author' => self::$editor_user_id, … … 37 36 ) 38 37 ); 39 self::$ author_privatefoo_post= $factory->post->create(38 self::$post_ids['author_privatefoo_post'] = $factory->post->create( 40 39 array( 41 40 'post_author' => self::$author_user_id, … … 44 43 ); 45 44 _unregister_post_status( 'privatefoo' ); 45 46 self::register_custom_post_objects(); 47 48 self::$post_ids['wptests_pt1_p1'] = $factory->post->create( 49 array( 50 'post_type' => 'wptests_pt1', 51 'post_status' => 'private', 52 'post_author' => self::$editor_user_id, 53 ) 54 ); 55 56 self::$post_ids['wptests_pt1_p2'] = $factory->post->create( 57 array( 58 'post_type' => 'wptests_pt1', 59 'post_status' => 'publish', 60 'post_author' => self::$editor_user_id, 61 ) 62 ); 63 64 self::$post_ids['wptests_pt2_p1'] = $factory->post->create( 65 array( 66 'post_type' => 'wptests_pt2', 67 'post_status' => 'private', 68 'post_author' => self::$editor_user_id, 69 ) 70 ); 71 72 self::$post_ids['wptests_pt2_p2'] = $factory->post->create( 73 array( 74 'post_type' => 'wptests_pt2', 75 'post_status' => 'publish', 76 'post_author' => self::$editor_user_id, 77 ) 78 ); 79 } 80 81 public function setUp() { 82 parent::setUp(); 83 self::register_custom_post_objects(); 84 } 85 86 /** 87 * Register custom post types and statuses used in multiple tests. 88 * 89 * CPTs and CPSs are reset between each test run so need to be registered 90 * in both the wpSetUpBeforeClass() and setUp() methods. 91 */ 92 public static function register_custom_post_objects() { 93 register_post_type( 94 'wptests_pt1', 95 array( 96 'exclude_from_search' => false, 97 'capabilities' => array( 98 'read_private_posts' => 'read_private_pt1s', 99 ), 100 ) 101 ); 102 103 register_post_type( 104 'wptests_pt2', 105 array( 106 'exclude_from_search' => false, 107 ) 108 ); 46 109 } 47 110 … … 79 142 80 143 $expected = array( 81 self::$ editor_private_post,82 self::$ author_private_post,144 self::$post_ids['editor_private_post'], 145 self::$post_ids['author_private_post'], 83 146 ); 84 147 … … 110 173 111 174 $expected = array( 112 self::$ author_private_post,175 self::$post_ids['author_private_post'], 113 176 ); 114 177 … … 127 190 128 191 $expected = array( 129 self::$ author_private_post,130 self::$ editor_private_post,192 self::$post_ids['author_private_post'], 193 self::$post_ids['editor_private_post'], 131 194 ); 132 195 … … 145 208 146 209 $expected = array( 147 self::$ author_private_post,210 self::$post_ids['author_private_post'], 148 211 ); 149 212 … … 162 225 163 226 $expected = array( 164 self::$ author_private_post,165 self::$ editor_private_post,227 self::$post_ids['author_private_post'], 228 self::$post_ids['editor_private_post'], 166 229 ); 167 230 … … 226 289 ); 227 290 228 $this->assertContains( self::$ author_privatefoo_post, wp_list_pluck( $q->posts, 'ID' ) );229 $this->assertContains( self::$ editor_privatefoo_post, wp_list_pluck( $q->posts, 'ID' ) );291 $this->assertContains( self::$post_ids['author_privatefoo_post'], wp_list_pluck( $q->posts, 'ID' ) ); 292 $this->assertContains( self::$post_ids['editor_privatefoo_post'], wp_list_pluck( $q->posts, 'ID' ) ); 230 293 } 231 294 … … 237 300 $q = new WP_Query( 238 301 array( 239 'posts_per_page' => 2, // Or the query will short-circuit.302 'posts_per_page' => -1, // Or the query will short-circuit. 240 303 ) 241 304 ); 242 305 243 306 $expected = array( 244 self::$ author_privatefoo_post,245 ); 246 247 $this->assertContains( self::$ author_privatefoo_post, wp_list_pluck( $q->posts, 'ID' ) );248 $this->assertNotContains( self::$ editor_privatefoo_post, wp_list_pluck( $q->posts, 'ID' ) );307 self::$post_ids['author_privatefoo_post'], 308 ); 309 310 $this->assertContains( self::$post_ids['author_privatefoo_post'], wp_list_pluck( $q->posts, 'ID' ) ); 311 $this->assertNotContains( self::$post_ids['editor_privatefoo_post'], wp_list_pluck( $q->posts, 'ID' ) ); 249 312 } 250 313 … … 458 521 $this->assertContains( $p1, wp_list_pluck( $q->posts, 'ID' ) ); 459 522 } 523 524 /** 525 * @ticket 48556 526 * @ticket 13509 527 */ 528 public function test_non_singular_queries_using_post_type_any_should_respect_post_type_read_private_posts_cap() { 529 $post_ids = self::$post_ids; 530 531 wp_set_current_user( 0 ); 532 533 $q = new WP_Query( 534 array( 535 'post_type' => 'any', 536 ) 537 ); 538 539 $this->assertSameSets( array( $post_ids['wptests_pt1_p2'], $post_ids['wptests_pt2_p2'] ), wp_list_pluck( $q->posts, 'ID' ) ); 540 541 wp_set_current_user( self::$subscriber_user_id ); 542 $GLOBALS['current_user']->add_cap( 'read_private_pt1s' ); 543 544 $q = new WP_Query( 545 array( 546 'post_type' => 'any', 547 ) 548 ); 549 550 $this->assertSameSets( array( $post_ids['wptests_pt1_p1'], $post_ids['wptests_pt1_p2'], $post_ids['wptests_pt2_p2'] ), wp_list_pluck( $q->posts, 'ID' ) ); 551 } 552 553 /** 554 * @ticket 48556 555 * @ticket 13509 556 */ 557 public function test_non_singular_queries_using_multiple_post_type_should_respect_post_type_read_private_posts_cap() { 558 wp_set_current_user( 0 ); 559 560 $post_ids = self::$post_ids; 561 562 $q = new WP_Query( 563 array( 564 'post_type' => 'any', 565 'posts_per_page' => -1, 566 ) 567 ); 568 569 $this->assertSameSets( array( $post_ids['wptests_pt1_p2'], $post_ids['wptests_pt2_p2'] ), wp_list_pluck( $q->posts, 'ID' ) ); 570 571 wp_set_current_user( self::$subscriber_user_id ); 572 $GLOBALS['current_user']->add_cap( 'read_private_pt1s' ); 573 574 $q = new WP_Query( 575 array( 576 'post_type' => array( 'wptests_pt1', 'wptests_pt2' ), 577 'posts_per_page' => -1, 578 ) 579 ); 580 581 $this->assertSameSets( array( $post_ids['wptests_pt1_p1'], $post_ids['wptests_pt1_p2'], $post_ids['wptests_pt2_p2'] ), wp_list_pluck( $q->posts, 'ID' ) ); 582 } 460 583 }
Note: See TracChangeset
for help on using the changeset viewer.