Test coverage

This commit is contained in:
Mike Jolley 2016-08-25 17:42:47 +01:00
parent 658970cbc8
commit 627b08ba37
4 changed files with 74 additions and 8 deletions

View File

@ -411,7 +411,7 @@ abstract class WC_Data {
try {
$setter = "set_$prop";
if ( ! is_null( $value ) && is_callable( array( $this, $setter ) ) ) {
$this->{$setter}( wc_clean( wp_unslash( $value ) ) );
$this->{$setter}( $value );
}
} catch ( WC_Data_Exception $e ) {
$errors->add( $e->getErrorCode(), $e->getMessage() );

View File

@ -202,7 +202,7 @@ function wc_save_order_items( $order_id, $items ) {
$item_data = array();
foreach ( $data_keys as $key => $default ) {
$item_data[ $key ] = isset( $items[ $key ][ $item_id ] ) ? $items[ $key ][ $item_id ] : $default;
$item_data[ $key ] = isset( $items[ $key ][ $item_id ] ) ? wc_clean( wp_unslash( $items[ $key ][ $item_id ] ) ) : $default;
}
if ( '0' === $item_data['order_item_qty'] ) {

View File

@ -8,8 +8,9 @@ class WC_Mock_WC_Data extends WC_Data {
* Data array
*/
protected $_data = array(
'id' => 0,
'content' => '',
'id' => 0,
'content' => '',
'bool_value' => false,
);
// see WC_Data
@ -56,6 +57,7 @@ class WC_Mock_WC_Data extends WC_Data {
* Simple read.
*/
public function __construct( $id = '' ) {
parent::__construct();
if ( ! empty( $id ) ) {
$this->read( $id );
}
@ -74,15 +76,34 @@ class WC_Mock_WC_Data extends WC_Data {
* @return string
*/
public function get_content() {
return $this->_data['content'];
return $this->get_prop( 'content' );
}
/**
* SImple set content.
* Simple set content.
* @param string $content
*/
public function set_content( $content ) {
$this->_data['content'] = $content;
$this->set_prop( 'content', $content );
}
/**
* Simple get bool value.
* @return bool
*/
public function get_bool_value() {
return $this->get_prop( 'bool_value' );
}
/**
* Simple set bool value.
* @return bool
*/
public function set_bool_value( $value ) {
if ( ! is_bool( $value ) ) {
$this->error( 'invalid_bool_value', 'O noes' );
}
$this->set_prop( 'bool_value', $value );
}
/**
@ -116,6 +137,7 @@ class WC_Mock_WC_Data extends WC_Data {
* Simple read.
*/
public function read( $id ) {
$this->set_defaults();
if ( 'user' === $this->_meta_type ) {
if ( empty( $id ) || ! ( $user_object = get_userdata( $id ) ) ) {

View File

@ -4,7 +4,7 @@
* Meta
* @package WooCommerce\Tests\CRUD
*/
class WC_Tests_CRUD_Meta extends WC_Unit_Test_Case {
class WC_Tests_CRUD_Data extends WC_Unit_Test_Case {
/**
* Create a test post we can add/test meta against.
@ -28,6 +28,50 @@ class WC_Tests_CRUD_Meta extends WC_Unit_Test_Case {
return $object;
}
/**
* Test: get_data
*/
function test_get_data() {
$object = new WC_Mock_WC_Data();
$this->assertInternalType( 'array', $object->get_data() );
}
/**
* Test: delete_meta_data_by_mid
*/
function test_delete_meta_data_by_mid() {
$object = $this->create_test_post();
$object_id = $object->get_id();
$meta_id = add_metadata( 'post', $object_id, 'test_meta_key', 'val1', true );
$object->delete_meta_data_by_mid( $meta_id );
$this->assertEmpty( $object->get_meta( 'test_meta_key' ) );
}
/**
* Test: set_props
*/
function test_set_props() {
$object = new WC_Mock_WC_Data();
$data_to_set = array(
'content' => 'I am a fish',
'bool_value' => true
);
$result = $object->set_props( $data_to_set );
var_dump($result);
$this->assertFalse( is_wp_error( $result ) );
$this->assertEquals( 'I am a fish', $object->get_content() );
$this->assertEquals( true, $object->get_bool_value() );
$data_to_set = array(
'content' => 'I am also a fish',
'bool_value' => 'thisisinvalid'
);
$result = $object->set_props( $data_to_set );
$this->assertTrue( is_wp_error( $result ) );
$this->assertEquals( 'I am also a fish', $object->get_content() );
$this->assertNotEquals( 'thisisinvalid', $object->get_bool_value() );
}
/**
* Tests reading and getting set metadata.
*/