Changeset 43339
- Timestamp:
- 06/11/2018 04:32:32 PM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tests/phpunit/tests/post/meta.php
r42343 r43339 6 6 */ 7 7 class 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, 15 18 'post_status' => 'publish', 16 19 'post_content' => rand_str(), 17 20 '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, 25 25 'post_status' => 'publish', 26 26 'post_content' => rand_str(), 27 27 '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 ); 32 35 } 33 36 34 37 function test_unique_postmeta() { 35 38 // 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 ) ); 37 40 38 41 // 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 ) ); 40 43 41 44 //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 ) ); 44 47 45 48 //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' ) ); 47 50 48 51 //Delete it 49 $this->assertTrue( delete_post_meta( $this->post_id, 'unique', 'value' ) );52 $this->assertTrue( delete_post_meta( self::$post_id, 'unique', 'value' ) ); 50 53 51 54 //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 ) ); 54 57 55 58 } … … 57 60 function test_nonunique_postmeta() { 58 61 // 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 ) ); 65 68 66 69 //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' ) ); 68 71 69 72 //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' ) ); 71 74 72 75 //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 ) ); 75 78 76 79 //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' ) ); 78 81 79 82 //Check they exists … … 83 86 ); 84 87 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 ); 87 90 sort( $actual ); 88 91 $this->assertEquals( $expected, $actual ); … … 94 97 function test_update_post_meta() { 95 98 // 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 ) ); 97 100 98 101 // 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 ) ); 107 110 108 111 // 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' ) ); 112 115 113 116 //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 ) ); 118 121 119 122 } … … 121 124 function test_delete_post_meta() { 122 125 // 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 ) ); 129 132 130 133 //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' ) ); 132 135 133 136 //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 ) ); 135 138 136 139 } … … 138 141 function test_delete_post_meta_by_key() { 139 142 // 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 ) ); 142 145 143 146 //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 ) ); 146 149 147 150 //Delete one of them … … 149 152 150 153 //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 ) ); 153 156 } 154 157 155 158 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 ); 157 160 $this->assertInternalType( 'integer', $mid ); 158 161 159 162 $mobj = new stdClass; 160 163 $mobj->meta_id = $mid; 161 $mobj->post_id = $this->post_id;164 $mobj->post_id = self::$post_id; 162 165 $mobj->meta_key = 'get_post_meta_by_key'; 163 166 $mobj->meta_value = 'get_post_meta_by_key_value'; … … 165 168 delete_metadata_by_mid( 'post', $mid ); 166 169 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 ); 168 171 $this->assertInternalType( 'integer', $mid ); 169 172 $mobj->meta_id = $mid; … … 174 177 175 178 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 ); 177 180 $this->assertInternalType( 'integer', $mid ); 178 181 … … 185 188 function test_update_meta() { 186 189 // 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 ) ); 188 191 189 192 // 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' ) ); 192 195 193 196 //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 ) ); 198 201 199 202 // Update them … … 203 206 204 207 //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 ) ); 209 212 210 213 // Slashed update … … 230 233 231 234 // 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 ) ); 236 239 237 240 }
Note: See TracChangeset
for help on using the changeset viewer.