mirror of
https://github.com/wp-graphql/wp-graphql-woocommerce.git
synced 2026-08-14 12:53:44 +02:00
Grouped product and product variation connection tests refactored.
This commit is contained in:
@@ -71,12 +71,32 @@ class Products {
|
||||
register_graphql_connection(
|
||||
self::get_connection_config(
|
||||
array(
|
||||
'fromType' => 'Product',
|
||||
'fromFieldName' => 'related',
|
||||
'resolve' => function( $source, array $args, AppContext $context, ResolveInfo $info ) {
|
||||
'fromType' => 'Product',
|
||||
'fromFieldName' => 'related',
|
||||
'connectionArgs' => array_merge(
|
||||
self::get_connection_args(),
|
||||
array(
|
||||
'shuffle' => array(
|
||||
'type' => 'Boolean',
|
||||
'description' => __( 'Shuffle results? (Pagination currently not support by this argument)', 'wp-graphql-woocommerce' ),
|
||||
)
|
||||
)
|
||||
),
|
||||
'resolve' => function( $source, array $args, AppContext $context, ResolveInfo $info ) {
|
||||
$resolver = new Product_Connection_Resolver( $source, $args, $context, $info );
|
||||
|
||||
$resolver->set_query_arg( 'post__in', $source->related_ids );
|
||||
// Bypass randomization by default for pagination support.
|
||||
if ( empty( $args['where']['shuffle'] ) ) {
|
||||
add_filter(
|
||||
'woocommerce_product_related_posts_shuffle',
|
||||
function() {
|
||||
return false;
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
$related_ids = wc_get_related_products( $source->ID, $resolver->get_query_amount() );
|
||||
$resolver->set_query_arg( 'post__in', $related_ids );
|
||||
|
||||
// Change default ordering
|
||||
if ( ! in_array( 'orderby', array_keys( $resolver->get_query_args() ) ) ) {
|
||||
@@ -98,7 +118,7 @@ class Products {
|
||||
|
||||
$resolver->set_query_arg( 'post__in', $source->upsell_ids );
|
||||
|
||||
// Change default ordering
|
||||
// Change default ordering.
|
||||
if ( ! in_array( 'orderby', array_keys( $resolver->get_query_args() ) ) ) {
|
||||
$resolver->set_query_arg( 'orderby', 'post__in' );
|
||||
}
|
||||
@@ -119,7 +139,7 @@ class Products {
|
||||
|
||||
$resolver->set_query_arg( 'post__in', $source->grouped_ids );
|
||||
|
||||
// Change default ordering
|
||||
// Change default ordering.
|
||||
if ( ! in_array( 'orderby', array_keys( $resolver->get_query_args() ) ) ) {
|
||||
$resolver->set_query_arg( 'orderby', 'post__in' );
|
||||
}
|
||||
@@ -138,7 +158,7 @@ class Products {
|
||||
|
||||
$resolver->set_query_arg( 'post__in', $source->cross_sell_ids );
|
||||
|
||||
// Change default ordering
|
||||
// Change default ordering.
|
||||
if ( ! in_array( 'orderby', array_keys( $resolver->get_query_args() ) ) ) {
|
||||
$resolver->set_query_arg( 'orderby', 'post__in' );
|
||||
}
|
||||
@@ -167,11 +187,12 @@ class Products {
|
||||
'resolve' => function( $source, array $args, AppContext $context, ResolveInfo $info ) {
|
||||
$resolver = new Product_Connection_Resolver( $source, $args, $context, $info );
|
||||
|
||||
\codecept_debug( $source->variations_ids );
|
||||
$resolver->set_query_arg( 'post_parent', $source->ID );
|
||||
$resolver->set_query_arg( 'post_type', 'product_variation' );
|
||||
$resolver->set_query_arg( 'post__in', $source->variation_ids );
|
||||
|
||||
// Change default ordering
|
||||
// Change default ordering.
|
||||
if ( ! in_array( 'orderby', array_keys( $resolver->get_query_args() ) ) ) {
|
||||
$resolver->set_query_arg( 'orderby', 'post__in' );
|
||||
}
|
||||
|
||||
@@ -266,10 +266,6 @@ class Product extends Crud_CPT {
|
||||
* These field resolvers are used in connection resolvers to define WP_Query argument
|
||||
* Note: underscore naming style is used as a quick identifier
|
||||
*/
|
||||
'related_ids' => function() {
|
||||
$related_ids = array_map( 'absint', wc_get_related_products( $this->data->get_id() ) );
|
||||
return ! empty( $related_ids ) ? $related_ids : array( '0' );
|
||||
},
|
||||
'upsell_ids' => function() {
|
||||
return ! empty( $this->data->get_upsell_ids() )
|
||||
? array_map( 'absint', $this->data->get_upsell_ids() )
|
||||
|
||||
@@ -1192,7 +1192,7 @@ class ProductQueriesTest extends \Codeception\TestCase\WPTestCase {
|
||||
query ($id: ID!) {
|
||||
product(id: $id) {
|
||||
... on SimpleProduct {
|
||||
related(first: 1) {
|
||||
related {
|
||||
nodes {
|
||||
... on SimpleProduct {
|
||||
id
|
||||
@@ -1220,41 +1220,49 @@ class ProductQueriesTest extends \Codeception\TestCase\WPTestCase {
|
||||
|
||||
$variables = array( 'id' => $this->helper->to_relay_id( $product->get_id() ) );
|
||||
$actual = graphql( array( 'query' => $query, 'variables' => $variables ) );
|
||||
$expected = array(
|
||||
'data' => array(
|
||||
'product' => array(
|
||||
'related' => array(
|
||||
'nodes' => array_map(
|
||||
function( $id ) {
|
||||
return array( 'id' => $this->helper->to_relay_id( $id ) );
|
||||
},
|
||||
array_slice( wc_get_related_products( $product->get_id() ), 0, 1 )
|
||||
),
|
||||
),
|
||||
'crossSell' => array(
|
||||
'nodes' => array_map(
|
||||
function( $id ) {
|
||||
return array( 'id' => $this->helper->to_relay_id( $id ) );
|
||||
},
|
||||
$product->get_cross_sell_ids()
|
||||
),
|
||||
),
|
||||
'upsell' => array(
|
||||
'nodes' => array_map(
|
||||
function( $id ) {
|
||||
return array( 'id' => $this->helper->to_relay_id( $id ) );
|
||||
},
|
||||
$product->get_upsell_ids()
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
// use --debug flag to view.
|
||||
codecept_debug( $actual );
|
||||
|
||||
$this->assertEquals( $expected, $actual );
|
||||
$related = $actual['data']['product']['related']['nodes'];
|
||||
$cross_sell = $actual['data']['product']['crossSell']['nodes'];
|
||||
$upsell = $actual['data']['product']['upsell']['nodes'];
|
||||
|
||||
// Assert that related products are valid.
|
||||
foreach( wc_get_related_products( $product->get_id() ) as $pid ) {
|
||||
$this->assertTrue(
|
||||
in_array(
|
||||
array( 'id' => $this->helper->to_relay_id( $pid ) ),
|
||||
$related,
|
||||
true
|
||||
),
|
||||
$this->helper->to_relay_id( $pid ) . ' not a related product of ' . $product->get_name()
|
||||
);
|
||||
}
|
||||
|
||||
// Assert that cross sell products are valid.
|
||||
foreach( $product->get_cross_sell_ids() as $pid ) {
|
||||
$this->assertTrue(
|
||||
in_array(
|
||||
array( 'id' => $this->helper->to_relay_id( $pid ) ),
|
||||
$cross_sell,
|
||||
true
|
||||
),
|
||||
$this->helper->to_relay_id( $pid ) . ' not being cross selling by ' . $product->get_name()
|
||||
);
|
||||
}
|
||||
|
||||
// Assert that upsell products are valid.
|
||||
foreach( $product->get_upsell_ids() as $pid ) {
|
||||
$this->assertTrue(
|
||||
in_array(
|
||||
array( 'id' => $this->helper->to_relay_id( $pid ) ),
|
||||
$upsell,
|
||||
true
|
||||
),
|
||||
$this->helper->to_relay_id( $pid ) . ' not a upsold product of ' . $product->get_name()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
public function testProductToReviewConnections() {
|
||||
|
||||
@@ -123,7 +123,8 @@ class ProductVariationQueriesTest extends \Codeception\TestCase\WPTestCase {
|
||||
}
|
||||
|
||||
public function testVariationsQueryAndWhereArgs() {
|
||||
$id = $this->product_helper->to_relay_id( $this->products['product'] );
|
||||
$id = $this->product_helper->to_relay_id( $this->products['product'] );
|
||||
$product = wc_get_product( $this->products['product'] );
|
||||
$variations = $this->products['variations'];
|
||||
|
||||
$query = '
|
||||
@@ -163,34 +164,39 @@ class ProductVariationQueriesTest extends \Codeception\TestCase\WPTestCase {
|
||||
$variables = array( 'id' => $id );
|
||||
$actual = graphql( array( 'query' => $query, 'variables' => $variables ) );
|
||||
|
||||
$prices = $this->product_helper->field( $this->products['product'], 'variation_prices', array( true ) );
|
||||
$product_fields = array(
|
||||
'price' => \wc_graphql_price( current( $prices['price'] ) )
|
||||
. ' - '
|
||||
. \wc_graphql_price( end( $prices['price'] ) ),
|
||||
'regularPrice' => \wc_graphql_price( current( $prices['regular_price'] ) )
|
||||
. ' - '
|
||||
. \wc_graphql_price( end( $prices['regular_price'] ) ),
|
||||
'salePrice' => null,
|
||||
);
|
||||
|
||||
$expected = array(
|
||||
'data' => array(
|
||||
'product' => array_merge(
|
||||
$product_fields,
|
||||
array(
|
||||
'variations' => array(
|
||||
'nodes' => $this->helper->print_nodes( $variations ),
|
||||
),
|
||||
)
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
// use --debug flag to view.
|
||||
codecept_debug( $actual );
|
||||
|
||||
$this->assertEquals( $expected, $actual );
|
||||
// Get product data.
|
||||
$product_data = $actual['data']['product'];
|
||||
|
||||
// Assert variations.
|
||||
foreach( $variations as $vid ) {
|
||||
$this->assertTrue(
|
||||
in_array(
|
||||
array( 'id' => $this->helper->to_relay_id( $vid ) ),
|
||||
$product_data['variations']['nodes'],
|
||||
true
|
||||
),
|
||||
$this->helper->to_relay_id( $vid ) . ' not a variation of ' . $product->get_name()
|
||||
);
|
||||
}
|
||||
|
||||
// Assert prices.
|
||||
$prices = $this->product_helper->field( $this->products['product'], 'variation_prices', array( true ) );
|
||||
$this->assertTrue(
|
||||
$product_data['price'] === \wc_graphql_price( current( $prices['price'] ) )
|
||||
. ' - '
|
||||
. \wc_graphql_price( end( $prices['price'] ) )
|
||||
);
|
||||
|
||||
$this->assertTrue(
|
||||
$product_data['regularPrice'] === \wc_graphql_price( current( $prices['regular_price'] ) )
|
||||
. ' - '
|
||||
. \wc_graphql_price( end( $prices['regular_price'] ) )
|
||||
);
|
||||
|
||||
$this->assertTrue( $product_data['salePrice'] === null );
|
||||
|
||||
/**
|
||||
* Assertion Two
|
||||
@@ -199,31 +205,29 @@ class ProductVariationQueriesTest extends \Codeception\TestCase\WPTestCase {
|
||||
*/
|
||||
$variables = array( 'id' => $id, 'minPrice' => 15 );
|
||||
$actual = graphql( array( 'query' => $query, 'variables' => $variables ) );
|
||||
$expected = array(
|
||||
'data' => array(
|
||||
'product' => array_merge(
|
||||
$product_fields,
|
||||
array(
|
||||
'variations' => array(
|
||||
'nodes' => $this->helper->print_nodes(
|
||||
$variations,
|
||||
array(
|
||||
'filter' => function( $id ) {
|
||||
$variation = new WC_Product_Variation( $id );
|
||||
return 15.00 <= floatval( $variation->get_price() );
|
||||
}
|
||||
)
|
||||
),
|
||||
),
|
||||
)
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
// use --debug flag to view.
|
||||
codecept_debug( $actual );
|
||||
|
||||
$this->assertEquals( $expected, $actual );
|
||||
// Get product data.
|
||||
$product_data = $actual['data']['product'];
|
||||
|
||||
// Assert variations.
|
||||
$filter = function( $id ) {
|
||||
$variation = new WC_Product_Variation( $id );
|
||||
return 15.00 <= floatval( $variation->get_price() );
|
||||
};
|
||||
|
||||
foreach( array_filter( $variations, $filter ) as $vid ) {
|
||||
$this->assertTrue(
|
||||
in_array(
|
||||
array( 'id' => $this->helper->to_relay_id( $vid ) ),
|
||||
$product_data['variations']['nodes'],
|
||||
true
|
||||
),
|
||||
$this->helper->to_relay_id( $vid ) . ' not a variation of ' . $product->get_name()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
public function testProductVariationToMediaItemConnections() {
|
||||
|
||||
Vendored
+1
-1
@@ -4,4 +4,4 @@
|
||||
|
||||
require_once __DIR__ . '/composer/autoload_real.php';
|
||||
|
||||
return ComposerAutoloaderInit51f6ef77ec7a78ec4a9df8dd9f5dc45f::getLoader();
|
||||
return ComposerAutoloaderInitd71d86c43e8ed05b96e37706ed82275d::getLoader();
|
||||
|
||||
Vendored
+7
-7
@@ -2,7 +2,7 @@
|
||||
|
||||
// autoload_real.php @generated by Composer
|
||||
|
||||
class ComposerAutoloaderInit51f6ef77ec7a78ec4a9df8dd9f5dc45f
|
||||
class ComposerAutoloaderInitd71d86c43e8ed05b96e37706ed82275d
|
||||
{
|
||||
private static $loader;
|
||||
|
||||
@@ -22,15 +22,15 @@ class ComposerAutoloaderInit51f6ef77ec7a78ec4a9df8dd9f5dc45f
|
||||
return self::$loader;
|
||||
}
|
||||
|
||||
spl_autoload_register(array('ComposerAutoloaderInit51f6ef77ec7a78ec4a9df8dd9f5dc45f', 'loadClassLoader'), true, true);
|
||||
spl_autoload_register(array('ComposerAutoloaderInitd71d86c43e8ed05b96e37706ed82275d', 'loadClassLoader'), true, true);
|
||||
self::$loader = $loader = new \Composer\Autoload\ClassLoader();
|
||||
spl_autoload_unregister(array('ComposerAutoloaderInit51f6ef77ec7a78ec4a9df8dd9f5dc45f', 'loadClassLoader'));
|
||||
spl_autoload_unregister(array('ComposerAutoloaderInitd71d86c43e8ed05b96e37706ed82275d', 'loadClassLoader'));
|
||||
|
||||
$useStaticLoader = PHP_VERSION_ID >= 50600 && !defined('HHVM_VERSION') && (!function_exists('zend_loader_file_encoded') || !zend_loader_file_encoded());
|
||||
if ($useStaticLoader) {
|
||||
require_once __DIR__ . '/autoload_static.php';
|
||||
|
||||
call_user_func(\Composer\Autoload\ComposerStaticInit51f6ef77ec7a78ec4a9df8dd9f5dc45f::getInitializer($loader));
|
||||
call_user_func(\Composer\Autoload\ComposerStaticInitd71d86c43e8ed05b96e37706ed82275d::getInitializer($loader));
|
||||
} else {
|
||||
$map = require __DIR__ . '/autoload_namespaces.php';
|
||||
foreach ($map as $namespace => $path) {
|
||||
@@ -51,19 +51,19 @@ class ComposerAutoloaderInit51f6ef77ec7a78ec4a9df8dd9f5dc45f
|
||||
$loader->register(true);
|
||||
|
||||
if ($useStaticLoader) {
|
||||
$includeFiles = Composer\Autoload\ComposerStaticInit51f6ef77ec7a78ec4a9df8dd9f5dc45f::$files;
|
||||
$includeFiles = Composer\Autoload\ComposerStaticInitd71d86c43e8ed05b96e37706ed82275d::$files;
|
||||
} else {
|
||||
$includeFiles = require __DIR__ . '/autoload_files.php';
|
||||
}
|
||||
foreach ($includeFiles as $fileIdentifier => $file) {
|
||||
composerRequire51f6ef77ec7a78ec4a9df8dd9f5dc45f($fileIdentifier, $file);
|
||||
composerRequired71d86c43e8ed05b96e37706ed82275d($fileIdentifier, $file);
|
||||
}
|
||||
|
||||
return $loader;
|
||||
}
|
||||
}
|
||||
|
||||
function composerRequire51f6ef77ec7a78ec4a9df8dd9f5dc45f($fileIdentifier, $file)
|
||||
function composerRequired71d86c43e8ed05b96e37706ed82275d($fileIdentifier, $file)
|
||||
{
|
||||
if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) {
|
||||
require $file;
|
||||
|
||||
Vendored
+4
-4
@@ -4,7 +4,7 @@
|
||||
|
||||
namespace Composer\Autoload;
|
||||
|
||||
class ComposerStaticInit51f6ef77ec7a78ec4a9df8dd9f5dc45f
|
||||
class ComposerStaticInitd71d86c43e8ed05b96e37706ed82275d
|
||||
{
|
||||
public static $files = array (
|
||||
'944484f100dc1864a5320474d49ebd5a' => __DIR__ . '/../..' . '/includes/connection/wc-cpt-connection-args.php',
|
||||
@@ -176,9 +176,9 @@ class ComposerStaticInit51f6ef77ec7a78ec4a9df8dd9f5dc45f
|
||||
public static function getInitializer(ClassLoader $loader)
|
||||
{
|
||||
return \Closure::bind(function () use ($loader) {
|
||||
$loader->prefixLengthsPsr4 = ComposerStaticInit51f6ef77ec7a78ec4a9df8dd9f5dc45f::$prefixLengthsPsr4;
|
||||
$loader->prefixDirsPsr4 = ComposerStaticInit51f6ef77ec7a78ec4a9df8dd9f5dc45f::$prefixDirsPsr4;
|
||||
$loader->classMap = ComposerStaticInit51f6ef77ec7a78ec4a9df8dd9f5dc45f::$classMap;
|
||||
$loader->prefixLengthsPsr4 = ComposerStaticInitd71d86c43e8ed05b96e37706ed82275d::$prefixLengthsPsr4;
|
||||
$loader->prefixDirsPsr4 = ComposerStaticInitd71d86c43e8ed05b96e37706ed82275d::$prefixDirsPsr4;
|
||||
$loader->classMap = ComposerStaticInitd71d86c43e8ed05b96e37706ed82275d::$classMap;
|
||||
|
||||
}, null, ClassLoader::class);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user