Make WordPress Core

Changeset 35183


Ignore:
Timestamp:
10/15/2015 03:30:09 AM (11 years ago)
Author:
wonderboymusic
Message:

Unit Tests: in Tests_Post, create fixtures for users.

See #30017, #33968.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/post.php

    r35162 r35183  
    77 */
    88class Tests_Post extends WP_UnitTestCase {
     9        protected static $editor_id;
     10        protected static $grammarian_id;
     11
     12        public static function setUpBeforeClass() {
     13                parent::setUpBeforeClass();
     14
     15                $factory = new WP_UnitTest_Factory();
     16
     17                self::$editor_id = $factory->user->create( array( 'role' => 'editor' ) );
     18
     19                add_role( 'grammarian', 'Grammarian', array(
     20                        'read'                 => true,
     21                        'edit_posts'           => true,
     22                        'edit_others_posts'    => true,
     23                        'edit_published_posts' => true,
     24                ) );
     25
     26                self::$grammarian_id = $factory->user->create( array( 'role' => 'grammarian' ) );
     27
     28                self::commit_transaction();
     29        }
     30
     31        public static function tearDownAfterClass() {
     32                parent::tearDownAfterClass();
     33
     34                $ids = array( self::$editor_id, self::$grammarian_id );
     35                foreach ( $ids as $id ) {
     36                        if ( is_multisite() ) {
     37                                wpmu_delete_user( $id );
     38                        } else {
     39                                wp_delete_user( $id );
     40                        }
     41                }
     42
     43                self::commit_transaction();
     44        }
     45
    946        function setUp() {
    1047                parent::setUp();
    11                 $this->author_id = $this->factory->user->create( array( 'role' => 'editor' ) );
    12                 $this->old_current_user = get_current_user_id();
    13                 wp_set_current_user( $this->author_id );
    14                 _set_cron_array(array());
     48
     49                wp_set_current_user( self::$editor_id );
     50                _set_cron_array( array() );
     51
    1552                $this->post_ids = array();
    16         }
    17 
    18         function tearDown() {
    19                 wp_set_current_user( $this->old_current_user );
    20                 parent::tearDown();
    2153        }
    2254
     
    4173                foreach ( $post_types as $post_type ) {
    4274                        $post = array(
    43                                 'post_author' => $this->author_id,
     75                                'post_author' => self::$editor_id,
    4476                                'post_status' => 'publish',
    4577                                'post_content' => rand_str(),
     
    95127
    96128                $post = array(
    97                         'post_author' => $this->author_id,
     129                        'post_author' => self::$editor_id,
    98130                        'post_status' => 'publish',
    99131                        'post_content' => rand_str(),
     
    130162
    131163                $post = array(
    132                         'post_author' => $this->author_id,
     164                        'post_author' => self::$editor_id,
    133165                        'post_status' => 'publish',
    134166                        'post_content' => rand_str(),
     
    172204
    173205                $post = array(
    174                         'post_author' => $this->author_id,
     206                        'post_author' => self::$editor_id,
    175207                        'post_status' => 'publish',
    176208                        'post_content' => rand_str(),
     
    212244
    213245                $post = array(
    214                         'post_author' => $this->author_id,
     246                        'post_author' => self::$editor_id,
    215247                        'post_status' => 'draft',
    216248                        'post_content' => rand_str(),
     
    244276
    245277                $post = array(
    246                         'post_author' => $this->author_id,
     278                        'post_author' => self::$editor_id,
    247279                        'post_status' => 'publish',
    248280                        'post_content' => rand_str(),
     
    285317                foreach ($statuses as $status) {
    286318                        $post = array(
    287                                 'post_author' => $this->author_id,
     319                                'post_author' => self::$editor_id,
    288320                                'post_status' => 'publish',
    289321                                'post_content' => rand_str(),
     
    325357
    326358                $post = array(
    327                         'post_author' => $this->author_id,
     359                        'post_author' => self::$editor_id,
    328360                        'post_status' => 'private',
    329361                        'post_content' => rand_str(),
     
    358390
    359391                $post = array(
    360                         'post_author' => $this->author_id,
     392                        'post_author' => self::$editor_id,
    361393                        'post_status' => 'public',
    362394                        'post_content' => rand_str(),
     
    379411
    380412                $post = array(
    381                         'post_author' => $this->author_id,
     413                        'post_author' => self::$editor_id,
    382414                        'post_status' => 'publish',
    383415                        'post_content' => rand_str(),
     
    477509
    478510                $post = array(
    479                         'post_author' => $this->author_id,
     511                        'post_author' => self::$editor_id,
    480512                        'post_status' => 'publish',
    481513                        'post_content' => rand_str(),
     
    506538
    507539                $post = array(
    508                         'post_author' => $this->author_id,
     540                        'post_author' => self::$editor_id,
    509541                        'post_status' => 'publish',
    510542                        'post_content' => rand_str(),
     
    770802                $title = rand_str();
    771803                $post_data = array(
    772                         'post_author' => $this->author_id,
     804                        'post_author' => self::$editor_id,
    773805                        'post_status' => 'public',
    774806                        'post_content' => rand_str(),
     
    782814
    783815                $post = get_post( $insert_post_id );
    784                 $this->assertEquals( $post->post_author, $this->author_id );
     816                $this->assertEquals( $post->post_author, self::$editor_id );
    785817                $this->assertEquals( $post->post_title, $title );
    786818        }
     
    794826                $this->factory->post->create( array(
    795827                        'post_type' => $post_type,
    796                         'post_author' => $this->author_id
     828                        'post_author' => self::$editor_id
    797829                ) );
    798830                $count = wp_count_posts( $post_type, 'readable' );
     
    807839                $this->factory->post->create_many( 3, array(
    808840                        'post_type' => $post_type,
    809                         'post_author' => $this->author_id
     841                        'post_author' => self::$editor_id
    810842                ) );
    811843                $count1 = wp_count_posts( $post_type, 'readable' );
     
    10271059        function test_wp_insert_post_default_comment_ping_status_open() {
    10281060                $post_id = $this->factory->post->create( array(
    1029                         'post_author' => $this->author_id,
     1061                        'post_author' => self::$editor_id,
    10301062                        'post_status' => 'public',
    10311063                        'post_content' => rand_str(),
     
    10431075        function test_wp_insert_post_page_default_comment_ping_status_closed() {
    10441076                $post_id = $this->factory->post->create( array(
    1045                         'post_author' => $this->author_id,
     1077                        'post_author' => self::$editor_id,
    10461078                        'post_status' => 'public',
    10471079                        'post_content' => rand_str(),
     
    10621094                register_post_type( $post_type, array( 'supports' => array( 'comments', 'trackbacks' ) ) );
    10631095                $post_id = $this->factory->post->create( array(
    1064                         'post_author' => $this->author_id,
     1096                        'post_author' => self::$editor_id,
    10651097                        'post_status' => 'public',
    10661098                        'post_content' => rand_str(),
     
    10821114                register_post_type( $post_type );
    10831115                $post_id = $this->factory->post->create( array(
    1084                         'post_author' => $this->author_id,
     1116                        'post_author' => self::$editor_id,
    10851117                        'post_status' => 'public',
    10861118                        'post_content' => rand_str(),
     
    11021134         */
    11031135        function test_user_without_publish_cannot_affect_sticky() {
    1104                 // Create a role with edit_others_posts.
    1105                 add_role( 'grammarian', 'Grammarian', array(
    1106                         'read'                 => true,
    1107                         'edit_posts'           => true,
    1108                         'edit_others_posts'    => true,
    1109                         'edit_published_posts' => true,
    1110                 ) );
    1111                 $editor_user = $this->factory->user->create( array( 'role' => 'grammarian' ) );
    1112                 $old_uid = get_current_user_id();
    1113                 wp_set_current_user( $editor_user );
     1136                wp_set_current_user( self::$grammarian_id );
    11141137
    11151138                // Sanity Check.
     
    11381161                $this->assertEquals( 'Updated', $saved_post->post_title );
    11391162                $this->assertEquals( 'Updated', $saved_post->post_content );
    1140 
    1141                 // Teardown
    1142                 wp_set_current_user( $old_uid );
    11431163        }
    11441164
     
    11601180                $this->assertTrue( is_sticky( $post->ID ) );
    11611181
    1162                 // Create a role with edit_others_posts.
    1163                 add_role( 'grammarian', 'Grammarian', array(
    1164                         'read'                 => true,
    1165                         'edit_posts'           => true,
    1166                         'edit_others_posts'    => true,
    1167                         'edit_published_posts' => true,
    1168                 ) );
    1169                 $editor_user = $this->factory->user->create( array( 'role' => 'grammarian' ) );
    1170                 $old_uid = get_current_user_id();
    1171                 wp_set_current_user( $editor_user );
     1182                wp_set_current_user( self::$grammarian_id );
    11721183
    11731184                // Sanity Check.
     
    11891200                $this->assertEquals( 'Updated', $saved_post->post_title );
    11901201                $this->assertEquals( 'Updated', $saved_post->post_content );
    1191 
    1192                 // Teardown
    1193                 wp_set_current_user( $old_uid );
    11941202        }
    11951203
     
    12091217                $post_id = $this->factory->post->create( array( 'post_author' => null ) );
    12101218
    1211                 $this->assertEquals( $this->author_id, get_post( $post_id )->post_author );
     1219                $this->assertEquals( self::$editor_id, get_post( $post_id )->post_author );
    12121220        }
    12131221
     
    12171225        function test_wp_insert_post_should_respect_post_date_gmt() {
    12181226                $post = array(
    1219                         'post_author' => $this->author_id,
     1227                        'post_author' => self::$editor_id,
    12201228                        'post_status' => 'publish',
    12211229                        'post_content' => rand_str(),
Note: See TracChangeset for help on using the changeset viewer.