WordPress.org

Make WordPress Core

Changeset 242 in tests


Ignore:
Timestamp:
09/17/2009 07:51:48 PM (9 years ago)
Author:
westi
Message:

Add test cases for the post_meta functions.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • wp-testcase/test_includes_post.php

    r222 r242  
    566566}
    567567
     568class WPTestPostMeta extends WPTestCase {
     569    function setUp() {
     570        parent::setUp();
     571       
     572        $this->author = get_userdatabylogin(WP_USER_NAME);
     573       
     574        $post = array(
     575            'post_author' => $this->author->ID,
     576            'post_status' => 'publish',
     577            'post_content' => rand_str(),
     578            'post_title' => rand_str(),
     579        );
     580
     581        // insert a post
     582        $this->post_id = wp_insert_post($post);
     583    }
     584
     585    function tearDown() {
     586        parent::tearDown();
     587        wp_delete_post($this->post_id);
     588    }
     589
     590    function test_unique_postmeta() {
     591        // Add a unique post meta item
     592        $this->assertTrue(add_post_meta($this->post_id, 'unique', 'value', true));
     593       
     594        // Check unique is enforced
     595        $this->assertFalse(add_post_meta($this->post_id, 'unique', 'another value', true));
     596       
     597        //Check it exists
     598        $this->assertEquals('value', get_post_meta($this->post_id, 'unique', true));
     599        $this->assertEquals(array('value'), get_post_meta($this->post_id, 'unique', false));
     600       
     601        //Fail to delete the wrong value
     602        $this->assertFalse(delete_post_meta($this->post_id, 'unique', 'wrong value'));
     603       
     604        //Delete it
     605        $this->assertTrue(delete_post_meta($this->post_id, 'unique', 'value'));
     606
     607        //Check it is deleted
     608        $this->assertEquals('', get_post_meta($this->post_id, 'unique', true));
     609        //TODO raise WP Ticket for this as we get ''
     610        $this->assertEquals(array(''), get_post_meta($this->post_id, 'unique', false));
     611       
     612    }
     613   
     614    function test_nonunique_postmeta() {
     615        // Add two non unique post meta item
     616        $this->assertTrue(add_post_meta($this->post_id, 'nonunique', 'value'));
     617        $this->assertTrue(add_post_meta($this->post_id, 'nonunique', 'another value'));
     618       
     619        //Check they exists
     620        $this->assertEquals('value', get_post_meta($this->post_id, 'nonunique', true));
     621        $this->assertEquals(array('value', 'another value'), get_post_meta($this->post_id, 'nonunique', false));
     622       
     623        //Fail to delete the wrong value
     624        $this->assertFalse(delete_post_meta($this->post_id, 'nonunique', 'wrong value'));
     625       
     626        //Delete the first one
     627        $this->assertTrue(delete_post_meta($this->post_id, 'nonunique', 'value'));
     628
     629        //Check the remainder exists
     630        $this->assertEquals('another value', get_post_meta($this->post_id, 'nonunique', true));
     631        $this->assertEquals(array('another value'), get_post_meta($this->post_id, 'nonunique', false));
     632       
     633        //Add a third one
     634        $this->assertTrue(add_post_meta($this->post_id, 'nonunique', 'someother value'));
     635       
     636        //Check they exists
     637        $this->assertEquals('someother value', get_post_meta($this->post_id, 'nonunique', true));
     638        $this->assertEquals(array('someother value', 'another value'), get_post_meta($this->post_id, 'nonunique', false));
     639       
     640        //Delete the lot
     641        $this->assertTrue(delete_post_meta_by_key('nonunique'));
     642    }
     643   
     644    function test_update_post_meta() {
     645        // Add a unique post meta item
     646        $this->assertTrue(add_post_meta($this->post_id, 'unique_update', 'value', true));
     647
     648        // Add two non unique post meta item
     649        $this->assertTrue(add_post_meta($this->post_id, 'nonunique_update', 'value'));
     650        $this->assertTrue(add_post_meta($this->post_id, 'nonunique_update', 'another value'));
     651       
     652        //Check they exists
     653        $this->assertEquals('value', get_post_meta($this->post_id, 'unique_update', true));
     654        $this->assertEquals(array('value'), get_post_meta($this->post_id, 'unique_update', false));
     655        $this->assertEquals('value', get_post_meta($this->post_id, 'nonunique_update', true));
     656        $this->assertEquals(array('value', 'another value'), get_post_meta($this->post_id, 'nonunique_update', false));
     657       
     658        // Update them
     659        $this->assertTrue(update_post_meta($this->post_id, 'unique_update', 'new', 'value'));
     660        $this->assertTrue(update_post_meta($this->post_id, 'nonunique_update', 'new', 'value'));
     661        $this->assertTrue(update_post_meta($this->post_id, 'nonunique_update', 'another new', 'another value'));
     662
     663        //Check they updated
     664        $this->assertEquals('new', get_post_meta($this->post_id, 'unique_update', true));
     665        $this->assertEquals(array('new'), get_post_meta($this->post_id, 'unique_update', false));
     666        $this->assertEquals('new', get_post_meta($this->post_id, 'nonunique_update', true));
     667        $this->assertEquals(array('new', 'another new'), get_post_meta($this->post_id, 'nonunique_update', false));
     668       
     669    }
     670   
     671}
    568672?>
Note: See TracChangeset for help on using the changeset viewer.