Skip to content

Commit

Permalink
Merge pull request #136 from wp-graphql/add/test/term
Browse files Browse the repository at this point in the history
Add basic tests for TermType object queries.
  • Loading branch information
jasonbahl committed May 24, 2017
2 parents 6996b6d + 9e82ab5 commit dc3213f
Show file tree
Hide file tree
Showing 2 changed files with 257 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Data/DataSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ public static function resolve_term_object( $id, $taxonomy ) {

$term_object = \WP_Term::get_instance( $id, $taxonomy );
if ( empty( $term_object ) ) {
throw new \Exception( sprintf( __( 'No %1$s was found with the ID: %2$s', 'wp-graphql' ), $id, $taxonomy ) );
throw new \Exception( sprintf( __( 'No %1$s was found with the ID: %2$s', 'wp-graphql' ), $taxonomy, $id ) );
}

return $term_object;
Expand Down
256 changes: 256 additions & 0 deletions tests/test-term-object-queries.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,256 @@
<?php
/**
* WPGraphQL Test Term Object Queries
* This tests term queries (singular and plural) checking to see if the available fields return the expected response
* @package WPGraphQL
* @since 0.0.5
*/

/**
* Tests term object queries.
*/
class WP_GraphQL_Test_Term_Object_Queries extends WP_UnitTestCase {
/**
* This function is run before each method
* @since 0.0.5
*/
public function setUp() {
parent::setUp();
}

/**
* Runs after each method.
* @since 0.0.5
*/
public function tearDown() {
parent::tearDown();
}

public function createTermObject( $args = [] ) {
/**
* Create the term
*/
$term_id = $this->factory->term->create( $args );

/**
* Return the $id of the term_object that was created
*/
return $term_id;

}

/**
* testTermQuery
*
* This tests creating a single term with data and retrieving said term via a GraphQL query
*
* @since 0.0.5
*/
public function testTermQuery() {

/**
* Create a term
*/
$term_id = $this->createTermObject( [
'name' => 'A Category',
'taxonomy' => 'category',
'description' => 'just a description',
] );

$taxonomy = 'category';

/**
* Create the global ID based on the term_type and the created $id
*/
$global_id = \GraphQLRelay\Relay::toGlobalId( $taxonomy, $term_id );

/**
* Create the query string to pass to the $query
*/
$query = "
query {
category(id: \"{$global_id}\") {
categoryId
count
description
id
link
name
posts {
edges {
node {
postId
}
}
}
slug
taxonomy {
name
}
termGroupId
termTaxonomyId
}
}";

/**
* Run the GraphQL query
*/
$actual = do_graphql_request( $query );

/**
* Establish the expectation for the output of the query
*/
$expected = [
'data' => [
'category' => [
'categoryId' => $term_id,
'count' => null,
'description' => 'just a description',
'id' => $global_id,
'link' => "http://example.org/?cat={$term_id}",
'name' => 'A Category',
'posts' => [
'edges' => [],
],
'slug' => 'a-category',
'taxonomy' => [
'name' => 'category'
],
'termGroupId' => null,
'termTaxonomyId' => $term_id,
],
],
];

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

/**
* testTermQueryWithAssociatedPostObjects
*
* Tests a term with associated post objects.
*
* @since 0.0.5
*/
public function testTermQueryWithAssociatedPostObjects() {

/**
* Create a term
*/
$term_id = $this->createTermObject( [ 'name' => 'A category', 'taxonomy' => 'category' ] );

// Create a comment and assign it to term.
$post_id = $this->factory->post->create( [ 'post_type' => 'post' ] );
$page_id = $this->factory->post->create( [ 'post_type' => 'page'] );
$media_id = $this->factory->post->create( [ 'post_type' => 'attachment'] );

wp_set_object_terms( $post_id, $term_id, 'category' );
wp_set_object_terms( $page_id, $term_id, 'category' );
wp_set_object_terms( $media_id, $term_id, 'category' );

$taxonomy = 'category';

/**
* Create the global ID based on the term_type and the created $id
*/
$global_id = \GraphQLRelay\Relay::toGlobalId( $taxonomy, $term_id );

/**
* Create the query string to pass to the $query
*/
$query = "
query {
category(id: \"{$global_id}\") {
posts {
edges {
node {
postId
}
}
}
}
}";

/**
* Run the GraphQL query
*/
$actual = do_graphql_request( $query );

/**
* Establish the expectation for the output of the query
*/
$expected = [
'data' => [
'category' => [
'posts' => [
'edges' => [
[
'node' => [
'postId' => $post_id,
],
],
],
],
],
],
];

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

/**
* testTermQueryWhereTermDoesNotExist
*
* Tests a query for non existant term.
*
* @since 0.0.5
*/
public function testTermQueryWhereTermDoesNotExist() {
$taxonomy = 'category';

/**
* Create the global ID based on the term_type and the created $id
*/
$global_id = \GraphQLRelay\Relay::toGlobalId( $taxonomy, 'doesNotExist' );

/**
* Create the query string to pass to the $query
*/
$query = "
query {
category(id: \"{$global_id}\") {
categoryId
}
}";

/**
* Run the GraphQL query
*/
$actual = do_graphql_request( $query );

/**
* Establish the expectation for the output of the query
*/
$expected = [
'data' => [
'category' => null,
],
'errors' => [
[
'message' => 'No category was found with the ID: doesNotExist',
'locations' => [
[
'line' => 3,
'column' => 4,
],
],
'path' => [
'category',
],
],
],
];

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

0 comments on commit dc3213f

Please sign in to comment.