Files

405 lines
12 KiB
PHP
Raw Permalink Normal View History

2020-09-16 20:09:08 -04:00
<?php
class CoreInterfaceQueriesTest extends \Codeception\TestCase\WPTestCase {
public function setUp(): void {
// before
parent::setUp();
2020-09-16 20:09:08 -04:00
// your set up methods here
$this->products = $this->getModule( '\Helper\Wpunit' )->product();
$this->variations = $this->getModule( '\Helper\Wpunit' )->product_variation();
$this->orders = $this->getModule( '\Helper\Wpunit' )->order();
2020-09-16 20:09:08 -04:00
}
public function graphql() {
$results = graphql( ...func_get_args() );
// use --debug flag to view.
\codecept_debug( $results );
return $results;
}
// tests
public function testProductAsNodeWithComments() {
2020-09-16 20:09:08 -04:00
// Create product and review to be queried.
$product_id = $this->products->create_simple();
$comment_id = $this->factory()->comment->create(
[
'comment_author' => 'Rude customer',
'comment_author_email' => 'rude-guy@example.com',
'comment_post_ID' => $product_id,
'comment_content' => 'It came covered in poop!!!',
'comment_approved' => 1,
'comment_type' => 'review',
]
);
2020-09-16 20:09:08 -04:00
update_comment_meta( $comment_id, 'rating', 1 );
// Define query and variables.
$query = '
query ( $id: ID! ) {
product( id: $id, idType: DATABASE_ID ) {
id
... on NodeWithComments {
commentCount
commentStatus
}
}
}
';
$variables = [ 'id' => $product_id ];
2020-09-16 20:09:08 -04:00
// Execute query and retrieve response.
$response = $this->graphql( compact( 'query', 'variables' ) );
// Define expected data object.
$expected = [
'data' => [
'product' => [
'id' => \GraphQLRelay\Relay::toGlobalId( 'post', $product_id ),
2020-09-16 20:09:08 -04:00
'commentCount' => 1,
'commentStatus' => 'open',
],
],
];
2020-09-16 20:09:08 -04:00
// Assert query response valid.
$this->assertEquals( $expected, $response );
}
public function testOrderAsNodeWithComments() {
// Create order and order note to be queried.
$order_id = $this->orders->create();
$order = \wc_get_order( $order_id );
// Adding a customer note triggers the Customer Note email, which on newer
// WooCommerce logs an extra "Email sent" order note. Disable it so the
// comment count stays deterministic.
add_filter( 'woocommerce_email_enabled_customer_note', '__return_false' );
2020-09-16 20:09:08 -04:00
$order->add_order_note( 'testnote' );
2020-10-13 15:01:44 -04:00
$order->add_order_note( 'testcustomernote', 1, true );
2020-09-16 20:09:08 -04:00
// Define query and variables.
$query = '
2020-09-16 20:09:08 -04:00
query ( $id: ID! ) {
order( id: $id, idType: DATABASE_ID ) {
id
... on NodeWithComments {
commentCount
commentStatus
}
}
}
';
$variables = [ 'id' => $order_id ];
2020-09-16 20:09:08 -04:00
/**
* Assertion One
*
* Authenticate as a shop manager and execute query and validate response.
*/
wp_set_current_user( $this->factory->user->create( [ 'role' => 'shop_manager' ] ) );
2020-09-16 20:09:08 -04:00
$response = $this->graphql( compact( 'query', 'variables' ) );
// Define expected data object.
$expected = [
'data' => [
'order' => [
2023-06-21 13:21:29 -04:00
'id' => \GraphQLRelay\Relay::toGlobalId( 'order', $order_id ),
2020-10-13 15:01:44 -04:00
'commentCount' => 2,
2020-09-16 20:09:08 -04:00
'commentStatus' => 'open',
],
],
];
2020-09-16 20:09:08 -04:00
// Assert query response valid.
$this->assertEquals( $expected, $response );
/**
* Assertion Two
*
* Authenticate as a shop manager and execute query and confirm 'commentStatus' is 'closed'.
*/
wp_set_current_user( $order->get_customer_id() );
$response = $this->graphql( compact( 'query', 'variables' ) );
// Define expected data object.
$expected = [
'data' => [
'order' => [
2023-06-21 13:21:29 -04:00
'id' => \GraphQLRelay\Relay::toGlobalId( 'order', $order_id ),
2020-09-16 20:09:08 -04:00
'commentCount' => 1,
'commentStatus' => 'closed',
],
],
];
2020-09-16 20:09:08 -04:00
// Assert query response valid.
$this->assertEquals( $expected, $response );
}
public function testProductAsNodeWithContentEditor() {
// Create product to be queried.
$product_id = $this->products->create_simple();
$product = \wc_get_product( $product_id );
2020-10-01 01:45:26 -04:00
// Authenticate to view RAW content.
wp_set_current_user( $this->factory->user->create( [ 'role' => 'shop_manager' ] ) );
2020-10-01 01:45:26 -04:00
2020-09-16 20:09:08 -04:00
// Define query and variables.
$query = '
query ( $id: ID!, $format: PostObjectFieldFormatEnum ) {
product( id: $id, idType: DATABASE_ID ) {
id
... on NodeWithContentEditor {
content( format: $format )
}
}
}
';
$variables = [
2020-09-16 20:09:08 -04:00
'id' => $product_id,
'format' => 'RAW',
];
2020-09-16 20:09:08 -04:00
// Execute query and retrieve response.
$response = $this->graphql( compact( 'query', 'variables' ) );
// Define expected data object.
$expected = [
'data' => [
'product' => [
'id' => \GraphQLRelay\Relay::toGlobalId( 'post', $product_id ),
2020-09-16 20:09:08 -04:00
'content' => $product->get_description(),
],
],
];
2020-09-16 20:09:08 -04:00
// Assert query response valid.
$this->assertEquals( $expected, $response );
}
public function testProductAsNodeWithFeaturedImage() {
// Create product to be queried.
2020-09-22 08:17:01 -04:00
$attachment_id = $this->factory()->attachment->create(
[
2020-09-22 08:17:01 -04:00
'post_mime_type' => 'image/gif',
'post_author' => $this->admin,
]
2020-09-22 08:17:01 -04:00
);
$product_id = $this->products->create_simple( [ 'image_id' => $attachment_id ] );
2020-09-16 20:09:08 -04:00
// Define query and variables.
$query = '
query ( $id: ID! ) {
product( id: $id, idType: DATABASE_ID ) {
id
... on NodeWithFeaturedImage {
featuredImageId
featuredImageDatabaseId
}
}
}
';
$variables = [ 'id' => $product_id ];
2020-09-16 20:09:08 -04:00
// Execute query and retrieve response.
$response = $this->graphql( compact( 'query', 'variables' ) );
// Define expected data object.
$expected = [
'data' => [
'product' => [
'id' => \GraphQLRelay\Relay::toGlobalId( 'post', $product_id ),
2020-09-22 08:17:01 -04:00
'featuredImageId' => \GraphQLRelay\Relay::toGlobalId( 'post', $attachment_id ),
'featuredImageDatabaseId' => $attachment_id,
],
],
];
2020-09-16 20:09:08 -04:00
// Assert query response valid.
$this->assertEquals( $expected, $response );
}
public function testProductAsContentNode() {
// Create product to be queried.
$product_id = $this->products->create_simple();
2020-09-22 08:17:01 -04:00
$wc_product = \wc_get_product( $product_id );
2020-09-16 20:09:08 -04:00
$wp_product = get_post( $product_id );
// Define query and variables.
$query = '
query ( $id: ID! ) {
product( id: $id, idType: DATABASE_ID ) {
id
... on ContentNode {
id
databaseId
date
dateGmt
enclosure
status
slug
modified
modifiedGmt
guid
desiredSlug
link
uri
isRestricted
isPreview
previewRevisionDatabaseId
previewRevisionId
}
}
}
';
$variables = [ 'id' => $product_id ];
2020-09-16 20:09:08 -04:00
// Execute query and retrieve response.
$response = $this->graphql( compact( 'query', 'variables' ) );
// Define expected data object.
$expected = [
'data' => [
'product' => [
'id' => \GraphQLRelay\Relay::toGlobalId( 'post', $product_id ),
2020-09-16 20:09:08 -04:00
'databaseId' => $wp_product->ID,
2020-09-22 08:17:01 -04:00
'date' => (string) $wc_product->get_date_created(),
2020-09-16 20:09:08 -04:00
'dateGmt' => \WPGraphQL\Utils\Utils::prepare_date_response( $wp_product->post_date_gmt ),
'enclosure' => get_post_meta( $wp_product->ID, 'enclosure', true ) ?? null,
2020-09-16 20:09:08 -04:00
'status' => $wp_product->post_status,
'slug' => $wp_product->post_name,
2020-09-22 08:17:01 -04:00
'modified' => (string) $wc_product->get_date_modified(),
'modifiedGmt' => \WPGraphQL\Utils\Utils::prepare_date_response( $wp_product->post_modified_gmt ),
2020-09-16 20:09:08 -04:00
'guid' => $wp_product->guid,
2020-09-22 08:17:01 -04:00
'desiredSlug' => null,
2020-09-16 20:09:08 -04:00
'link' => get_permalink( $wp_product->ID ),
'uri' => str_ireplace( home_url(), '', get_permalink( $wp_product->ID ) ),
'isRestricted' => false,
2020-09-22 08:17:01 -04:00
'isPreview' => null,
2020-09-16 20:09:08 -04:00
'previewRevisionDatabaseId' => null,
'previewRevisionId' => null,
],
],
];
2020-09-16 20:09:08 -04:00
// Assert query response valid.
$this->assertEquals( $expected, $response );
}
public function testProductAsUniformResourceIdentifiable() {
// Create product to be queried.
$product_id = $this->products->create_simple();
$product = \wc_get_product( $product_id );
$wp_product = get_post( $product_id );
// Define query and variables.
$query = '
query ( $id: ID! ) {
product( id: $id, idType: DATABASE_ID ) {
... on UniformResourceIdentifiable {
id
uri
}
}
}
';
$variables = [ 'id' => $product_id ];
2020-09-16 20:09:08 -04:00
// Execute query and retrieve response.
$response = $this->graphql( compact( 'query', 'variables' ) );
// Define expected data object.
$expected = [
'data' => [
'product' => [
'id' => \GraphQLRelay\Relay::toGlobalId( 'post', $product_id ),
2020-09-16 20:09:08 -04:00
'uri' => str_ireplace( home_url(), '', get_permalink( $wp_product->ID ) ),
],
],
];
2020-09-16 20:09:08 -04:00
// Assert query response valid.
$this->assertEquals( $expected, $response );
}
2020-09-22 08:17:01 -04:00
public function testNodeInterfacesOnProductVariation() {
2020-09-16 20:09:08 -04:00
// Create product collection to be queried.
$product_ids = $this->variations->create( $this->products->create_variable() );
2020-09-22 08:17:01 -04:00
$variation_id = $product_ids['variations'][1];
$wc_product = \wc_get_product( $variation_id );
2020-09-16 20:09:08 -04:00
$wp_product = get_post( $variation_id );
// Define query and variables.
$query = '
query ( $id: ID! ) {
productVariation( id: $id, idType: DATABASE_ID ) {
id
2020-09-22 08:17:01 -04:00
... on ContentNode {
databaseId
date
dateGmt
enclosure
status
slug
modified
modifiedGmt
guid
desiredSlug
link
uri
isRestricted
isPreview
previewRevisionDatabaseId
previewRevisionId
}
... on NodeWithFeaturedImage {
featuredImageId
featuredImageDatabaseId
}
2020-09-16 20:09:08 -04:00
}
}
';
$variables = [ 'id' => $variation_id ];
2020-09-16 20:09:08 -04:00
// Execute query and retrieve response.
$response = $this->graphql( compact( 'query', 'variables' ) );
// Define expected data object.
$expected = [
'data' => [
'productVariation' => [
'id' => \GraphQLRelay\Relay::toGlobalId( 'post', $variation_id ),
2020-09-22 08:17:01 -04:00
'databaseId' => $wp_product->ID,
'date' => (string) $wc_product->get_date_created(),
'dateGmt' => \WPGraphQL\Utils\Utils::prepare_date_response( $wp_product->post_date_gmt ),
'enclosure' => get_post_meta( $wp_product->ID, 'enclosure', true ) ?? null,
2020-09-22 08:17:01 -04:00
'status' => $wp_product->post_status,
'slug' => $wp_product->post_name,
'modified' => (string) $wc_product->get_date_modified(),
'modifiedGmt' => \WPGraphQL\Utils\Utils::prepare_date_response( $wp_product->post_modified_gmt ),
'guid' => $wp_product->guid,
'desiredSlug' => null,
'link' => get_permalink( $wp_product->ID ),
'uri' => str_ireplace( home_url(), '', get_permalink( $wp_product->ID ) ),
'isRestricted' => false,
'isPreview' => null,
'previewRevisionDatabaseId' => null,
'previewRevisionId' => null,
'featuredImageId' => \GraphQLRelay\Relay::toGlobalId( 'post', $wc_product->get_image_id() ),
'featuredImageDatabaseId' => $wc_product->get_image_id(),
],
],
];
2020-09-16 20:09:08 -04:00
// Assert query response valid.
$this->assertEquals( $expected, $response );
}
public function testQueryProductWithNodeByUri() {
}
}