/** * Create a test user we can add/test meta against. */ public function create_test_user() { $object = new WC_Mock_WC_Data(); $object->set_meta_type('user'); $object->set_object_id_field('user_id'); $object->set_content('*****@*****.**'); $object->save(); return $object; }
/** * Test saving metadata.. (Actually making sure changes are written to DB) */ function test_save_meta_data() { global $wpdb; $object = $this->create_test_post(); $object_id = $object->get_id(); $object->add_meta_data('test_meta_key', 'val1', true); $object->add_meta_data('test_meta_key_2', 'val2', true); $object->save_meta_data(); $object = new WC_Mock_WC_Data($object_id); $raw_metadata = $wpdb->get_results($wpdb->prepare("\n\t\t\tSELECT meta_id, meta_key, meta_value\n\t\t\tFROM {$wpdb->prefix}postmeta\n\t\t\tWHERE post_id = %d ORDER BY meta_id\n\t\t", $object_id)); $object->delete_meta_data('test_meta_key'); $object->update_meta_data('test_meta_key_2', 'updated_value', $raw_metadata[1]->meta_id); $object->save(); $object = new WC_Mock_WC_Data($object_id); // rereads from the DB $this->assertEmpty($object->get_meta('test_meta_key')); $this->assertEquals('updated_value', $object->get_meta('test_meta_key_2')); }