Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"format" arg added to Product description fields #139

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions includes/model/class-product.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,32 @@ protected function init() {
return ! empty( $this->data->get_catalog_visibility() ) ? $this->data->get_catalog_visibility() : null;
},
'description' => function() {
return ! empty( $this->data->get_description() ) ? $this->data->get_description() : null;
return ! empty( $this->data->get_description() )
? apply_filters( 'the_content', $this->data->get_description() )
: null;
},
'descriptionRaw' => array(
'callback' => function() {
return ! empty( $this->data->get_description() ) ? $this->data->get_description() : null;
},
'capability' => $this->post_type_object->cap->edit_posts,
),
'shortDescription' => function() {
return ! empty( $this->data->get_short_description() ) ? $this->data->get_short_description() : null;
$short_description = ! empty( $this->data->get_short_description() )
? apply_filters(
'get_the_excerpt',
$this->data->get_short_description(),
get_post( $this->data->get_id() )
)
: null;
return apply_filters( 'the_excerpt', $short_description );
},
'shortDescriptionRaw' => array(
'callback' => function() {
return ! empty( $this->data->get_short_description() ) ? $this->data->get_short_description() : null;
},
'capability' => $this->post_type_object->cap->edit_posts,
),
'sku' => function() {
return ! empty( $this->data->get_sku() ) ? $this->data->get_sku() : null;
},
Expand Down
27 changes: 27 additions & 0 deletions includes/type/object/class-product-type.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,37 @@ public static function register() {
'description' => array(
'type' => 'String',
'description' => __( 'Product description', 'wp-graphql-woocommerce' ),
'args' => array(
'format' => array(
'type' => 'PostObjectFieldFormatEnum',
'description' => __( 'Format of the field output', 'wp-graphql-woocommerce' ),
),
),
'resolve' => function( $source, $args ) {
if ( isset( $args['format'] ) && 'raw' === $args['format'] ) {
// @codingStandardsIgnoreLine.
return $source->descriptionRaw;
}
return $source->description;
},
),
'shortDescription' => array(
'type' => 'String',
'description' => __( 'Product short description', 'wp-graphql-woocommerce' ),
'args' => array(
'format' => array(
'type' => 'PostObjectFieldFormatEnum',
'description' => __( 'Format of the field output', 'wp-graphql-woocommerce' ),
),
),
'resolve' => function( $source, $args ) {
if ( isset( $args['format'] ) && 'raw' === $args['format'] ) {
// @codingStandardsIgnoreLine.
return $source->shortDescriptionRaw;
}
// @codingStandardsIgnoreLine.
return $source->shortDescription;
},
),
'sku' => array(
'type' => 'String',
Expand Down
11 changes: 11 additions & 0 deletions tests/_support/Helper/Wpunit.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ public function _beforeSuite( $settings = null ) {
$helper->create_attribute( 'size', array( 'small', 'medium', 'large' ) );
$helper->create_attribute( 'color', array( 'red', 'blue', 'green' ) );
codecept_debug( 'ATTRIBUTES_LOADED' );
add_action( 'init_graphql_request', array( __CLASS__, 'shortcode_test_init' ) );
codecept_debug( 'SHORTCODE_INITIALIZED' );
}

public function cart() {
Expand Down Expand Up @@ -87,4 +89,13 @@ public function clear_loader_cache( $loader_name ) {
$loader = \WPGraphQL::get_app_context()->getLoader( $loader_name );
$loader->clearAll();
}

public static function shortcode_test_init() {
add_shortcode( 'shortcode_test', array( __CLASS__, 'shortcode_test_handler' ) );
codecept_debug( 'shortcode created' );
}

public static function shortcode_test_handler( $atts ) {
return '<p>This is the product description.</p>';
}
}
41 changes: 25 additions & 16 deletions tests/_support/Helper/crud-helpers/product.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,17 +69,19 @@ public function create_simple( $args = array() ) {

$props = array_merge(
array(
'name' => $name,
'slug' => $this->next_slug(),
'regular_price' => $regular_price,
'price' => $price,
'sku' => uniqid(),
'manage_stock' => false,
'tax_status' => 'taxable',
'downloadable' => false,
'virtual' => false,
'stock_status' => 'instock',
'weight' => '1.1',
'name' => $name,
'slug' => $this->next_slug(),
'regular_price' => $regular_price,
'price' => $price,
'sku' => uniqid(),
'manage_stock' => false,
'tax_status' => 'taxable',
'downloadable' => false,
'virtual' => false,
'stock_status' => 'instock',
'weight' => '1.1',
'description' => '[shortcode_test]',
'short_description' => $this->dummy->sentence(),
),
$args
);
Expand Down Expand Up @@ -292,7 +294,7 @@ public function create_download( $id = 0 ) {
return $download;
}

public function print_query( $id ) {
public function print_query( $id, $raw = false ) {
$data = wc_get_product( $id );

return array(
Expand All @@ -306,11 +308,18 @@ public function print_query( $id ) {
'featured' => $data->get_featured(),
'catalogVisibility' => strtoupper( $data->get_catalog_visibility() ),
'description' => ! empty( $data->get_description() )
? $data->get_description()
? $raw
? $data->get_description()
: apply_filters( 'the_content', $data->get_description() )
: null,
'shortDescription' => ! empty( $data->get_short_description() )
? $data->get_short_description()
: null,
? $raw
? $data->get_short_description()
: apply_filters(
'get_the_excerpt',
apply_filters( 'the_excerpt', $data->get_short_description() )
)
: null,
'sku' => $data->get_sku(),
'price' => ! empty( $data->get_price() )
? \wc_graphql_price( $data->get_price() )
Expand Down Expand Up @@ -352,7 +361,7 @@ public function print_query( $id ) {
'onSale' => $data->is_on_sale(),
'purchasable' => $data->is_purchasable(),
'shippingRequired' => $data->needs_shipping(),
'shippingTaxable' => $data->is_shipping_taxable()
'shippingTaxable' => $data->is_shipping_taxable(),
);
}

Expand Down
48 changes: 43 additions & 5 deletions tests/wpunit/ProductQueriesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function tearDown() {
// tests
public function testProductQuery() {
$query = '
query productQuery( $id: ID! ) {
query ( $id: ID!, $format: PostObjectFieldFormatEnum ) {
product(id: $id) {
id
productId
Expand All @@ -66,8 +66,8 @@ public function testProductQuery() {
status
featured
catalogVisibility
description
shortDescription
description(format: $format)
shortDescription(format: $format)
sku
price
regularPrice
Expand Down Expand Up @@ -104,8 +104,17 @@ public function testProductQuery() {
}
';

$variables = array( 'id' => Relay::toGlobalId( 'product', $this->product ) );
$actual = do_graphql_request( $query, 'productQuery', $variables );
/**
* Assertion One
*
* Test querying product.
*/
$actual = graphql(
array(
'query' => $query,
'variables' => array( 'id' => $this->helper->to_relay_id( $this->product ) ),
)
);
$expected = array(
'data' => array(
'product' => $this->helper->print_query( $this->product ),
Expand All @@ -116,6 +125,35 @@ public function testProductQuery() {
codecept_debug( $actual );

$this->assertEqualSets( $expected, $actual );

// Clear cache
$this->getModule('\Helper\Wpunit')->clear_loader_cache( 'wc_post_crud' );

/**
* Assertion Two
*
* Test querying product with unformatted content (edit-product cap required).
*/
wp_set_current_user( $this->shop_manager );
$actual = graphql(
array(
'query' => $query,
'variables' => array(
'id' => $this->helper->to_relay_id( $this->product ),
'format' => 'RAW',
),
)
);
$expected = array(
'data' => array(
'product' => $this->helper->print_query( $this->product, true ),
),
);

// use --debug flag to view.
codecept_debug( $actual );

$this->assertEqualSets( $expected, $actual );
}

public function testProductByQueryAndArgs() {
Expand Down