Make WordPress Core

Changeset 43339


Ignore:
Timestamp:
06/11/2018 04:32:32 PM (8 years ago)
Author:
flixos90
Message:

Tests: Improve performance of post meta tests.

See #38323.

File:
1 edited

Legend:

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

    r42343 r43339  
    66 */
    77class Tests_Post_Meta extends WP_UnitTestCase {
    8         function setUp() {
    9                 parent::setUp();
    10 
    11                 $this->author = new WP_User( self::factory()->user->create( array( 'role' => 'editor' ) ) );
    12 
    13                 $post = array(
    14                         'post_author'  => $this->author->ID,
     8
     9        protected static $author;
     10        protected static $post_id;
     11        protected static $post_id_2;
     12
     13        public static function wpSetUpBeforeClass( $factory ) {
     14                self::$author = $factory->user->create_and_get( array( 'role' => 'editor' ) );
     15
     16                self::$post_id = $factory->post->create( array(
     17                        'post_author'  => self::$author->ID,
    1518                        'post_status'  => 'publish',
    1619                        'post_content' => rand_str(),
    1720                        'post_title'   => rand_str(),
    18                 );
    19 
    20                 // insert a post
    21                 $this->post_id = wp_insert_post( $post );
    22 
    23                 $post = array(
    24                         'post_author'  => $this->author->ID,
     21                ) );
     22
     23                self::$post_id_2 = $factory->post->create( array(
     24                        'post_author'  => self::$author->ID,
    2525                        'post_status'  => 'publish',
    2626                        'post_content' => rand_str(),
    2727                        'post_title'   => rand_str(),
    28                 );
    29 
    30                 // insert a post
    31                 $this->post_id_2 = wp_insert_post( $post );
     28                ) );
     29        }
     30
     31        public static function wpTearDownAfterClass() {
     32                wp_delete_post( self::$post_id, true );
     33                wp_delete_post( self::$post_id_2, true );
     34                self::delete_user( self::$author );
    3235        }
    3336
    3437        function test_unique_postmeta() {
    3538                // Add a unique post meta item
    36                 $this->assertInternalType( 'integer', add_post_meta( $this->post_id, 'unique', 'value', true ) );
     39                $this->assertInternalType( 'integer', add_post_meta( self::$post_id, 'unique', 'value', true ) );
    3740
    3841                // Check unique is enforced
    39                 $this->assertFalse( add_post_meta( $this->post_id, 'unique', 'another value', true ) );
     42                $this->assertFalse( add_post_meta( self::$post_id, 'unique', 'another value', true ) );
    4043
    4144                //Check it exists
    42                 $this->assertEquals( 'value', get_post_meta( $this->post_id, 'unique', true ) );
    43                 $this->assertEquals( array( 'value' ), get_post_meta( $this->post_id, 'unique', false ) );
     45                $this->assertEquals( 'value', get_post_meta( self::$post_id, 'unique', true ) );
     46                $this->assertEquals( array( 'value' ), get_post_meta( self::$post_id, 'unique', false ) );
    4447
    4548                //Fail to delete the wrong value
    46                 $this->assertFalse( delete_post_meta( $this->post_id, 'unique', 'wrong value' ) );
     49                $this->assertFalse( delete_post_meta( self::$post_id, 'unique', 'wrong value' ) );
    4750
    4851                //Delete it
    49                 $this->assertTrue( delete_post_meta( $this->post_id, 'unique', 'value' ) );
     52                $this->assertTrue( delete_post_meta( self::$post_id, 'unique', 'value' ) );
    5053
    5154                //Check it is deleted
    52                 $this->assertEquals( '', get_post_meta( $this->post_id, 'unique', true ) );
    53                 $this->assertEquals( array(), get_post_meta( $this->post_id, 'unique', false ) );
     55                $this->assertEquals( '', get_post_meta( self::$post_id, 'unique', true ) );
     56                $this->assertEquals( array(), get_post_meta( self::$post_id, 'unique', false ) );
    5457
    5558        }
    … …  
    5760        function test_nonunique_postmeta() {
    5861                // Add two non unique post meta item
    59                 $this->assertInternalType( 'integer', add_post_meta( $this->post_id, 'nonunique', 'value' ) );
    60                 $this->assertInternalType( 'integer', add_post_meta( $this->post_id, 'nonunique', 'another value' ) );
    61 
    62                 //Check they exists
    63                 $this->assertEquals( 'value', get_post_meta( $this->post_id, 'nonunique', true ) );
    64                 $this->assertEquals( array( 'value', 'another value' ), get_post_meta( $this->post_id, 'nonunique', false ) );
     62                $this->assertInternalType( 'integer', add_post_meta( self::$post_id, 'nonunique', 'value' ) );
     63                $this->assertInternalType( 'integer', add_post_meta( self::$post_id, 'nonunique', 'another value' ) );
     64
     65                //Check they exists
     66                $this->assertEquals( 'value', get_post_meta( self::$post_id, 'nonunique', true ) );
     67                $this->assertEquals( array( 'value', 'another value' ), get_post_meta( self::$post_id, 'nonunique', false ) );
    6568
    6669                //Fail to delete the wrong value
    67                 $this->assertFalse( delete_post_meta( $this->post_id, 'nonunique', 'wrong value' ) );
     70                $this->assertFalse( delete_post_meta( self::$post_id, 'nonunique', 'wrong value' ) );
    6871
    6972                //Delete the first one
    70                 $this->assertTrue( delete_post_meta( $this->post_id, 'nonunique', 'value' ) );
     73                $this->assertTrue( delete_post_meta( self::$post_id, 'nonunique', 'value' ) );
    7174
    7275                //Check the remainder exists
    73                 $this->assertEquals( 'another value', get_post_meta( $this->post_id, 'nonunique', true ) );
    74                 $this->assertEquals( array( 'another value' ), get_post_meta( $this->post_id, 'nonunique', false ) );
     76                $this->assertEquals( 'another value', get_post_meta( self::$post_id, 'nonunique', true ) );
     77                $this->assertEquals( array( 'another value' ), get_post_meta( self::$post_id, 'nonunique', false ) );
    7578
    7679                //Add a third one
    77                 $this->assertInternalType( 'integer', add_post_meta( $this->post_id, 'nonunique', 'someother value' ) );
     80                $this->assertInternalType( 'integer', add_post_meta( self::$post_id, 'nonunique', 'someother value' ) );
    7881
    7982                //Check they exists
    … …  
    8386                );
    8487                sort( $expected );
    85                 $this->assertTrue( in_array( get_post_meta( $this->post_id, 'nonunique', true ), $expected ) );
    86                 $actual = get_post_meta( $this->post_id, 'nonunique', false );
     88                $this->assertTrue( in_array( get_post_meta( self::$post_id, 'nonunique', true ), $expected ) );
     89                $actual = get_post_meta( self::$post_id, 'nonunique', false );
    8790                sort( $actual );
    8891                $this->assertEquals( $expected, $actual );
    … …  
    9497        function test_update_post_meta() {
    9598                // Add a unique post meta item
    96                 $this->assertInternalType( 'integer', add_post_meta( $this->post_id, 'unique_update', 'value', true ) );
     99                $this->assertInternalType( 'integer', add_post_meta( self::$post_id, 'unique_update', 'value', true ) );
    97100
    98101                // Add two non unique post meta item
    99                 $this->assertInternalType( 'integer', add_post_meta( $this->post_id, 'nonunique_update', 'value' ) );
    100                 $this->assertInternalType( 'integer', add_post_meta( $this->post_id, 'nonunique_update', 'another value' ) );
    101 
    102                 //Check they exists
    103                 $this->assertEquals( 'value', get_post_meta( $this->post_id, 'unique_update', true ) );
    104                 $this->assertEquals( array( 'value' ), get_post_meta( $this->post_id, 'unique_update', false ) );
    105                 $this->assertEquals( 'value', get_post_meta( $this->post_id, 'nonunique_update', true ) );
    106                 $this->assertEquals( array( 'value', 'another value' ), get_post_meta( $this->post_id, 'nonunique_update', false ) );
     102                $this->assertInternalType( 'integer', add_post_meta( self::$post_id, 'nonunique_update', 'value' ) );
     103                $this->assertInternalType( 'integer', add_post_meta( self::$post_id, 'nonunique_update', 'another value' ) );
     104
     105                //Check they exists
     106                $this->assertEquals( 'value', get_post_meta( self::$post_id, 'unique_update', true ) );
     107                $this->assertEquals( array( 'value' ), get_post_meta( self::$post_id, 'unique_update', false ) );
     108                $this->assertEquals( 'value', get_post_meta( self::$post_id, 'nonunique_update', true ) );
     109                $this->assertEquals( array( 'value', 'another value' ), get_post_meta( self::$post_id, 'nonunique_update', false ) );
    107110
    108111                // Update them
    109                 $this->assertTrue( update_post_meta( $this->post_id, 'unique_update', 'new', 'value' ) );
    110                 $this->assertTrue( update_post_meta( $this->post_id, 'nonunique_update', 'new', 'value' ) );
    111                 $this->assertTrue( update_post_meta( $this->post_id, 'nonunique_update', 'another new', 'another value' ) );
     112                $this->assertTrue( update_post_meta( self::$post_id, 'unique_update', 'new', 'value' ) );
     113                $this->assertTrue( update_post_meta( self::$post_id, 'nonunique_update', 'new', 'value' ) );
     114                $this->assertTrue( update_post_meta( self::$post_id, 'nonunique_update', 'another new', 'another value' ) );
    112115
    113116                //Check they updated
    114                 $this->assertEquals( 'new', get_post_meta( $this->post_id, 'unique_update', true ) );
    115                 $this->assertEquals( array( 'new' ), get_post_meta( $this->post_id, 'unique_update', false ) );
    116                 $this->assertEquals( 'new', get_post_meta( $this->post_id, 'nonunique_update', true ) );
    117                 $this->assertEquals( array( 'new', 'another new' ), get_post_meta( $this->post_id, 'nonunique_update', false ) );
     117                $this->assertEquals( 'new', get_post_meta( self::$post_id, 'unique_update', true ) );
     118                $this->assertEquals( array( 'new' ), get_post_meta( self::$post_id, 'unique_update', false ) );
     119                $this->assertEquals( 'new', get_post_meta( self::$post_id, 'nonunique_update', true ) );
     120                $this->assertEquals( array( 'new', 'another new' ), get_post_meta( self::$post_id, 'nonunique_update', false ) );
    118121
    119122        }
    … …  
    121124        function test_delete_post_meta() {
    122125                // Add a unique post meta item
    123                 $this->assertInternalType( 'integer', add_post_meta( $this->post_id, 'unique_delete', 'value', true ) );
    124                 $this->assertInternalType( 'integer', add_post_meta( $this->post_id_2, 'unique_delete', 'value', true ) );
    125 
    126                 //Check they exists
    127                 $this->assertEquals( 'value', get_post_meta( $this->post_id, 'unique_delete', true ) );
    128                 $this->assertEquals( 'value', get_post_meta( $this->post_id_2, 'unique_delete', true ) );
     126                $this->assertInternalType( 'integer', add_post_meta( self::$post_id, 'unique_delete', 'value', true ) );
     127                $this->assertInternalType( 'integer', add_post_meta( self::$post_id_2, 'unique_delete', 'value', true ) );
     128
     129                //Check they exists
     130                $this->assertEquals( 'value', get_post_meta( self::$post_id, 'unique_delete', true ) );
     131                $this->assertEquals( 'value', get_post_meta( self::$post_id_2, 'unique_delete', true ) );
    129132
    130133                //Delete one of them
    131                 $this->assertTrue( delete_post_meta( $this->post_id, 'unique_delete', 'value' ) );
     134                $this->assertTrue( delete_post_meta( self::$post_id, 'unique_delete', 'value' ) );
    132135
    133136                //Check the other still exitsts
    134                 $this->assertEquals( 'value', get_post_meta( $this->post_id_2, 'unique_delete', true ) );
     137                $this->assertEquals( 'value', get_post_meta( self::$post_id_2, 'unique_delete', true ) );
    135138
    136139        }
    … …  
    138141        function test_delete_post_meta_by_key() {
    139142                // Add a unique post meta item
    140                 $this->assertInternalType( 'integer', add_post_meta( $this->post_id, 'unique_delete_by_key', 'value', true ) );
    141                 $this->assertInternalType( 'integer', add_post_meta( $this->post_id_2, 'unique_delete_by_key', 'value', true ) );
     143                $this->assertInternalType( 'integer', add_post_meta( self::$post_id, 'unique_delete_by_key', 'value', true ) );
     144                $this->assertInternalType( 'integer', add_post_meta( self::$post_id_2, 'unique_delete_by_key', 'value', true ) );
    142145
    143146                //Check they exist
    144                 $this->assertEquals( 'value', get_post_meta( $this->post_id, 'unique_delete_by_key', true ) );
    145                 $this->assertEquals( 'value', get_post_meta( $this->post_id_2, 'unique_delete_by_key', true ) );
     147                $this->assertEquals( 'value', get_post_meta( self::$post_id, 'unique_delete_by_key', true ) );
     148                $this->assertEquals( 'value', get_post_meta( self::$post_id_2, 'unique_delete_by_key', true ) );
    146149
    147150                //Delete one of them
    … …  
    149152
    150153                //Check the other still exists
    151                 $this->assertEquals( '', get_post_meta( $this->post_id_2, 'unique_delete_by_key', true ) );
    152                 $this->assertEquals( '', get_post_meta( $this->post_id_2, 'unique_delete_by_key', true ) );
     154                $this->assertEquals( '', get_post_meta( self::$post_id_2, 'unique_delete_by_key', true ) );
     155                $this->assertEquals( '', get_post_meta( self::$post_id_2, 'unique_delete_by_key', true ) );
    153156        }
    154157
    155158        function test_get_post_meta_by_id() {
    156                 $mid = add_post_meta( $this->post_id, 'get_post_meta_by_key', 'get_post_meta_by_key_value', true );
     159                $mid = add_post_meta( self::$post_id, 'get_post_meta_by_key', 'get_post_meta_by_key_value', true );
    157160                $this->assertInternalType( 'integer', $mid );
    158161
    159162                $mobj             = new stdClass;
    160163                $mobj->meta_id    = $mid;
    161                 $mobj->post_id    = $this->post_id;
     164                $mobj->post_id    = self::$post_id;
    162165                $mobj->meta_key   = 'get_post_meta_by_key';
    163166                $mobj->meta_value = 'get_post_meta_by_key_value';
    … …  
    165168                delete_metadata_by_mid( 'post', $mid );
    166169
    167                 $mid = add_post_meta( $this->post_id, 'get_post_meta_by_key', array( 'foo', 'bar' ), true );
     170                $mid = add_post_meta( self::$post_id, 'get_post_meta_by_key', array( 'foo', 'bar' ), true );
    168171                $this->assertInternalType( 'integer', $mid );
    169172                $mobj->meta_id    = $mid;
    … …  
    174177
    175178        function test_delete_meta() {
    176                 $mid = add_post_meta( $this->post_id, 'delete_meta', 'delete_meta_value', true );
     179                $mid = add_post_meta( self::$post_id, 'delete_meta', 'delete_meta_value', true );
    177180                $this->assertInternalType( 'integer', $mid );
    178181
    … …  
    185188        function test_update_meta() {
    186189                // Add a unique post meta item
    187                 $this->assertInternalType( 'integer', $mid1 = add_post_meta( $this->post_id, 'unique_update', 'value', true ) );
     190                $this->assertInternalType( 'integer', $mid1 = add_post_meta( self::$post_id, 'unique_update', 'value', true ) );
    188191
    189192                // Add two non unique post meta item
    190                 $this->assertInternalType( 'integer', $mid2 = add_post_meta( $this->post_id, 'nonunique_update', 'value' ) );
    191                 $this->assertInternalType( 'integer', $mid3 = add_post_meta( $this->post_id, 'nonunique_update', 'another value' ) );
     193                $this->assertInternalType( 'integer', $mid2 = add_post_meta( self::$post_id, 'nonunique_update', 'value' ) );
     194                $this->assertInternalType( 'integer', $mid3 = add_post_meta( self::$post_id, 'nonunique_update', 'another value' ) );
    192195
    193196                //Check they exist
    194                 $this->assertEquals( 'value', get_post_meta( $this->post_id, 'unique_update', true ) );
    195                 $this->assertEquals( array( 'value' ), get_post_meta( $this->post_id, 'unique_update', false ) );
    196                 $this->assertEquals( 'value', get_post_meta( $this->post_id, 'nonunique_update', true ) );
    197                 $this->assertEquals( array( 'value', 'another value' ), get_post_meta( $this->post_id, 'nonunique_update', false ) );
     197                $this->assertEquals( 'value', get_post_meta( self::$post_id, 'unique_update', true ) );
     198                $this->assertEquals( array( 'value' ), get_post_meta( self::$post_id, 'unique_update', false ) );
     199                $this->assertEquals( 'value', get_post_meta( self::$post_id, 'nonunique_update', true ) );
     200                $this->assertEquals( array( 'value', 'another value' ), get_post_meta( self::$post_id, 'nonunique_update', false ) );
    198201
    199202                // Update them
    … …  
    203206
    204207                //Check they updated
    205                 $this->assertEquals( 'new', get_post_meta( $this->post_id, 'unique_update', true ) );
    206                 $this->assertEquals( array( 'new' ), get_post_meta( $this->post_id, 'unique_update', false ) );
    207                 $this->assertEquals( 'new', get_post_meta( $this->post_id, 'nonunique_update', true ) );
    208                 $this->assertEquals( array( 'new', 'another new' ), get_post_meta( $this->post_id, 'nonunique_update', false ) );
     208                $this->assertEquals( 'new', get_post_meta( self::$post_id, 'unique_update', true ) );
     209                $this->assertEquals( array( 'new' ), get_post_meta( self::$post_id, 'unique_update', false ) );
     210                $this->assertEquals( 'new', get_post_meta( self::$post_id, 'nonunique_update', true ) );
     211                $this->assertEquals( array( 'new', 'another new' ), get_post_meta( self::$post_id, 'nonunique_update', false ) );
    209212
    210213                // Slashed update
    … …  
    230233
    231234                // Add a post meta item
    232                 $this->assertInternalType( 'integer', add_post_meta( $this->post_id, 'test_funky_post_meta', $funky_meta, true ) );
    233 
    234                 //Check they exists
    235                 $this->assertEquals( $funky_meta, get_post_meta( $this->post_id, 'test_funky_post_meta', true ) );
     235                $this->assertInternalType( 'integer', add_post_meta( self::$post_id, 'test_funky_post_meta', $funky_meta, true ) );
     236
     237                //Check they exists
     238                $this->assertEquals( $funky_meta, get_post_meta( self::$post_id, 'test_funky_post_meta', true ) );
    236239
    237240        }
Note: See TracChangeset for help on using the changeset viewer.