From 4967d60ca8f1c3c34ac19d35c3f40d1547a8eabc Mon Sep 17 00:00:00 2001 From: Geoff Taylor Date: Wed, 16 Sep 2020 20:09:08 -0400 Subject: [PATCH] CoreInterfaceQueriesTest added. --- tests/wpunit/CoreInterfaceQueriesTest.php | 404 ++++++++++++++++++++++ 1 file changed, 404 insertions(+) create mode 100644 tests/wpunit/CoreInterfaceQueriesTest.php diff --git a/tests/wpunit/CoreInterfaceQueriesTest.php b/tests/wpunit/CoreInterfaceQueriesTest.php new file mode 100644 index 00000000..a98ab1ba --- /dev/null +++ b/tests/wpunit/CoreInterfaceQueriesTest.php @@ -0,0 +1,404 @@ +products = $this->getModule('\Helper\Wpunit')->product(); + $this->variations = $this->getModule('\Helper\Wpunit')->product_variation(); + $this->orders = $this->getModule('\Helper\Wpunit')->order(); + } + + public function tearDown() { + // your tear down methods here + + // then + parent::tearDown(); + } + + public function graphql() { + $results = graphql( ...func_get_args() ); + + // use --debug flag to view. + \codecept_debug( $results ); + + return $results; + } + + // tests + public function testProductAsNodeWithComments() { + // Create product and review to be queried. + $product_id = $this->products->create_simple(); + $comment_id = $this->factory()->comment->create( + array( + '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', + ) + ); + 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 = array( 'id' => $product_id ); + + // Execute query and retrieve response. + $response = $this->graphql( compact( 'query', 'variables' ) ); + + // Define expected data object. + $expected = array( + 'data' => array( + 'product' => array( + 'id' => \GraphQLRelay\Relay::toGlobalId( 'product', $product_id ), + 'commentCount' => 1, + 'commentStatus' => 'open', + ), + ), + ); + + // 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 ); + $order->add_order_note( 'testnote' ); + + // Define query and variables. + $query = ' + query ( $id: ID! ) { + order( id: $id, idType: DATABASE_ID ) { + id + ... on NodeWithComments { + commentCount + commentStatus + } + } + } + '; + $variables = array( 'id' => $order_id ); + + /** + * Assertion One + * + * Authenticate as a shop manager and execute query and validate response. + */ + wp_set_current_user( $this->factory->user->create( array( 'role' => 'shop_manager' ) ) ); + $response = $this->graphql( compact( 'query', 'variables' ) ); + + // Define expected data object. + $expected = array( + 'data' => array( + 'order' => array( + 'id' => \GraphQLRelay\Relay::toGlobalId( 'shop_order', $order_id ), + 'commentCount' => 1, + 'commentStatus' => 'open', + ), + ), + ); + + // 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 = array( + 'data' => array( + 'order' => array( + 'id' => \GraphQLRelay\Relay::toGlobalId( 'shop_order', $order_id ), + 'commentCount' => 1, + 'commentStatus' => 'closed', + ), + ), + ); + + // 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 ); + + // Define query and variables. + $query = ' + query ( $id: ID!, $format: PostObjectFieldFormatEnum ) { + product( id: $id, idType: DATABASE_ID ) { + id + ... on NodeWithContentEditor { + content( format: $format ) + } + } + } + '; + $variables = array( + 'id' => $product_id, + 'format' => 'RAW', + ); + + // Execute query and retrieve response. + $response = $this->graphql( compact( 'query', 'variables' ) ); + + // Define expected data object. + $expected = array( + 'data' => array( + 'product' => array( + 'id' => \GraphQLRelay\Relay::toGlobalId( 'product', $product_id ), + 'content' => $product->get_description(), + ), + ), + ); + + // Assert query response valid. + $this->assertEquals( $expected, $response ); + } + + public function testProductAsNodeWithFeaturedImage() { + // Create product to be queried. + $product_id = $this->products->create_simple(); + $product = \wc_get_product( $product_id ); + + // Define query and variables. + $query = ' + query ( $id: ID! ) { + product( id: $id, idType: DATABASE_ID ) { + id + ... on NodeWithFeaturedImage { + featuredImageId + featuredImageDatabaseId + } + } + } + '; + $variables = array( + 'id' => $product_id, + 'format' => 'RAW', + ); + + // Execute query and retrieve response. + $response = $this->graphql( compact( 'query', 'variables' ) ); + + // Define expected data object. + $expected = array( + 'data' => array( + 'product' => array( + 'id' => \GraphQLRelay\Relay::toGlobalId( 'product', $product_id ), + 'featuredImageId' => \GraphQLRelay\Relay::toGlobalId( 'post', $product->get_image_id() ), + 'featuredImageDatabaseId' => $product->get_image_id(), + ), + ), + ); + + // Assert query response valid. + $this->assertEquals( $expected, $response ); + } + + public function testProductAsContentNode() { + // 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 ) { + id + ... on ContentNode { + id + databaseId + date + dateGmt + enclosure + status + slug + modified + modifiedGmt + guid + desiredSlug + link + uri + isRestricted + isPreview + previewRevisionDatabaseId + previewRevisionId + } + } + } + '; + $variables = array( 'id' => $product_id ); + + // Execute query and retrieve response. + $response = $this->graphql( compact( 'query', 'variables' ) ); + + // Define expected data object. + $expected = array( + 'data' => array( + 'product' => array( + 'id' => \GraphQLRelay\Relay::toGlobalId( 'product', $product_id ), + 'databaseId' => $wp_product->ID, + 'date' => \WPGraphQL\Utils\Utils::prepare_date_response( null, $wp_product->post_date ), + 'dateGmt' => \WPGraphQL\Utils\Utils::prepare_date_response( $wp_product->post_date_gmt ), + 'enclosure' => get_post_meta( $this->data->ID, 'enclosure', true ) ?: null, + 'status' => $wp_product->post_status, + 'slug' => $wp_product->post_name, + 'modified' => $wp_product->post_modified, + 'modifiedGmt' => $wp_product->post_modified_gmt, + 'guid' => $wp_product->guid, + 'desiredSlug' => '', + 'link' => get_permalink( $wp_product->ID ), + 'uri' => str_ireplace( home_url(), '', get_permalink( $wp_product->ID ) ), + 'isRestricted' => false, + 'isPreview' => false, + 'previewRevisionDatabaseId' => null, + 'previewRevisionId' => null, + ), + ), + ); + + // 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 = array( 'id' => $product_id ); + + // Execute query and retrieve response. + $response = $this->graphql( compact( 'query', 'variables' ) ); + + // Define expected data object. + $expected = array( + 'data' => array( + 'product' => array( + 'id' => \GraphQLRelay\Relay::toGlobalId( 'product', $product_id ), + 'uri' => str_ireplace( home_url(), '', get_permalink( $wp_product->ID ) ), + ), + ), + ); + + // Assert query response valid. + $this->assertEquals( $expected, $response ); + } + + public function testProductAsHierarchicalContentNode() { + // Create product collection to be queried. + $product_ids = $this->products->create_grouped(); + $product_id = $product_ids['children'][0]; + $wp_product = get_post( $product_id ); + + // Define query and variables. + $query = ' + query ( $id: ID! ) { + product( id: $id, idType: DATABASE_ID ) { + id + ... on HierarchicalContentNode { + parentId + parentDatabaseId + } + } + } + '; + $variables = array( 'id' => $product_id ); + + // Execute query and retrieve response. + $response = $this->graphql( compact( 'query', 'variables' ) ); + + // Define expected data object. + $expected = array( + 'data' => array( + 'product' => array( + 'id' => \GraphQLRelay\Relay::toGlobalId( 'product', $product_id ), + 'parentId' => \GraphQLRelay\Relay::toGlobalId( 'post', $wp_product->post_parent ), + 'parentDatabaseId' => $wp_product->post_parent, + ), + ), + ); + + // Assert query response valid. + $this->assertEquals( $expected, $response ); + } + + public function testProductVariationAsHierarchicalContentNode() { + // Create product collection to be queried. + $product_ids = $this->variations->create( $this->products->create_variable() ); + $variation_id = $product_ids['variations'][0]; + $wp_product = get_post( $variation_id ); + + // Define query and variables. + $query = ' + query ( $id: ID! ) { + productVariation( id: $id, idType: DATABASE_ID ) { + id + ... on HierarchicalContentNode { + parentId + parentDatabaseId + } + } + } + '; + $variables = array( 'id' => $variation_id ); + + // Execute query and retrieve response. + $response = $this->graphql( compact( 'query', 'variables' ) ); + + // Define expected data object. + $expected = array( + 'data' => array( + 'productVariation' => array( + 'id' => \GraphQLRelay\Relay::toGlobalId( 'product_variation', $variation_id ), + 'parentId' => \GraphQLRelay\Relay::toGlobalId( 'post', $wp_product->post_parent ), + 'parentDatabaseId' => $wp_product->post_parent, + ), + ), + ); + + // Assert query response valid. + $this->assertEquals( $expected, $response ); + } + + public function testQueryProductWithNodeByUri() { + + } + +}