shop_manager = $this->factory->user->create( array( 'role' => 'shop_manager', 'user_login' => 'shopManager25' ) ); $this->customer = $this->factory->user->create( array( 'role' => 'customer', 'user_login' => 'customer43' ) ); $this->helper = $this->getModule('\Helper\Wpunit')->customer(); $this->order_helper = $this->getModule('\Helper\Wpunit')->order(); $this->new_customer = $this->helper->create(); } public function tearDown() { // your tear down methods here // then parent::tearDown(); } // tests public function testCustomerQueryAndArgs() { $query = ' query ( $id: ID, $customerId: Int ) { customer( id: $id, customerId: $customerId ) { id customerId isVatExempt hasCalculatedShipping calculatedShipping orderCount totalSpent username email firstName lastName displayName role date modified lastOrder { id orderId } billing { firstName lastName company address1 address2 city state postcode country email phone } shipping { firstName lastName company address1 address2 city state postcode country } isPayingCustomer jwtAuthToken jwtRefreshToken } } '; /** * Assertion One * * Query should return null value due to lack of permissions. */ wp_set_current_user( $this->customer ); $variables = array( 'id' => Relay::toGlobalId( 'customer', $this->new_customer ) ); $actual = graphql( array( 'query' => $query, 'variables' => $variables ) ); // use --debug flag to view. codecept_debug( $actual ); $this->assertNotEmpty( $actual['data'] ); $this->assertNotEmpty( $actual['errors'] ); $this->assertNull( $actual['data']['customer'] ); // Clear customer cache. $this->getModule('\Helper\Wpunit')->clear_loader_cache( 'wc_customer' ); /** * Assertion Two * * Query should return requested data because user queried themselves. */ $variables = array( 'id' => Relay::toGlobalId( 'customer', $this->customer ) ); $actual = graphql( array( 'query' => $query, 'variables' => $variables ) ); $expected = array( 'data' => array( 'customer' => $this->helper->print_query( $this->customer ) ) ); // use --debug flag to view. codecept_debug( $actual ); $this->assertEquals( $expected, $actual ); // Clear customer cache. $this->getModule('\Helper\Wpunit')->clear_loader_cache( 'wc_customer' ); /** * Assertion Three * * Query should return requested data because has sufficient permissions, * but should not have access to JWT fields. */ wp_set_current_user( $this->shop_manager ); $variables = array( 'id' => Relay::toGlobalId( 'customer', $this->new_customer ) ); $actual = graphql( array( 'query' => $query, 'variables' => $variables ) ); $expected = array( 'data' => array( 'customer' => $this->helper->print_query( $this->new_customer ) ) ); // use --debug flag to view. codecept_debug( $actual ); $this->assertEquals( $expected['data'], $actual['data'] ); $this->assertNotEmpty( $actual['errors'] ); // Clear customer cache. $this->getModule('\Helper\Wpunit')->clear_loader_cache( 'wc_customer' ); /** * Assertion Four * * Query should return data corresponding with current user when no ID is provided. */ wp_set_current_user( $this->new_customer ); $actual = graphql( array( 'query' => $query ) ); $expected = array( 'data' => array( 'customer' => $this->helper->print_query( $this->new_customer ) ) ); // use --debug flag to view. codecept_debug( $actual ); $this->assertEquals( $expected, $actual ); /** * Assertion Five * * Query should return requested data because user queried themselves. */ wp_set_current_user( $this->new_customer ); $variables = array( 'customerId' => $this->new_customer ); $actual = graphql( array( 'query' => $query, 'variables' => $variables ) ); $expected = array( 'data' => array( 'customer' => $this->helper->print_query( $this->new_customer ) ) ); // use --debug flag to view. codecept_debug( $actual ); $this->assertEquals( $expected, $actual ); // Clear customer cache. $this->getModule('\Helper\Wpunit')->clear_loader_cache( 'wc_customer' ); /** * Assertion Six * * Query should return null value due to lack of permissions.. */ wp_set_current_user( $this->customer ); $variables = array( 'customerId' => $this->new_customer ); $actual = graphql( array( 'query' => $query, 'variables' => $variables ) ); // use --debug flag to view. codecept_debug( $actual ); $this->assertNotEmpty( $actual['data'] ); $this->assertNotEmpty( $actual['errors'] ); $this->assertNull( $actual['data']['customer'] ); } public function testCustomersQueryAndWhereArgs() { $user_id = $this->helper->create( array( 'email' => 'gotcha@example.com', 'username' => 'megaman8080' ) ); $users = get_users( array ( 'count_total' => false, 'order' => 'ASC', 'fields' => 'ID' ) ); $query = ' query ( $search: String, $include: [Int], $exclude: [Int], $email: String, $role: UserRoleEnum, $roleIn: [UserRoleEnum], $roleNotIn: [UserRoleEnum], $orderby: CustomerConnectionOrderbyEnum, $order: OrderEnum ) { customers( where: { search: $search, include: $include, exclude: $exclude, email: $email, role: $role, roleIn: $roleIn, roleNotIn: $roleNotIn, orderby: $orderby, order: $order } ) { nodes{ id } } } '; /** * Assertion One * * Query should return null value due to lack of capabilities... */ wp_set_current_user( $this->customer ); $actual = graphql( array( 'query' => $query ) ); $expected = array( 'data' => array( 'customers' => array( 'nodes' => array() ) ) ); // use --debug flag to view. codecept_debug( $actual ); $this->assertEquals( $expected, $actual ); // Clear customer cache. $this->getModule('\Helper\Wpunit')->clear_loader_cache( 'wc_customer' ); /** * Assertion Two * * Query should return requested data because user has proper capabilities. */ wp_set_current_user( $this->shop_manager ); $actual = graphql( array( 'query' => $query ) ); $expected = array( 'data' => array( 'customers' => array( 'nodes' => $this->helper->print_nodes( $users ), ), ), ); // use --debug flag to view. codecept_debug( $actual ); $this->assertEquals( $expected, $actual ); /** * Assertion Three * * Tests "search" where argument. */ $variables = array( 'search' => 'megaman8080' ); $actual = graphql( array( 'query' => $query, 'variables' => $variables ) ); $expected = array( 'data' => array( 'customers' => array( 'nodes' => $this->helper->print_nodes( $users, array( 'filter' => function( $id ) { $customer = new \WC_Customer( $id ); return 'megaman8080' === $customer->get_username(); } ) ), ), ), ); // use --debug flag to view. codecept_debug( $actual ); $this->assertEquals( $expected, $actual ); /** * Assertion Four * * Tests "include" where argument. */ $variables = array( 'include' => array( $user_id ) ); $actual = graphql( array( 'query' => $query, 'variables' => $variables ) ); $expected = array( 'data' => array( 'customers' => array( 'nodes' => $this->helper->print_nodes( $users, array( 'filter' => function( $id ) use ( $user_id ) { return absint( $id ) === $user_id; } ) ), ), ), ); // use --debug flag to view. codecept_debug( $actual ); $this->assertEquals( $expected, $actual ); /** * Assertion Five * * Tests "exclude" where argument. */ $variables = array( 'exclude' => array( $user_id ) ); $actual = graphql( array( 'query' => $query, 'variables' => $variables ) ); $expected = array( 'data' => array( 'customers' => array( 'nodes' => $this->helper->print_nodes( $users, array( 'filter' => function( $id ) use( $user_id ) { return absint( $id ) !== $user_id; } ) ), ), ), ); // use --debug flag to view. codecept_debug( $actual ); $this->assertEquals( $expected, $actual ); /** * Assertion Six * * Tests "email" where argument. */ $variables = array( 'email' => 'gotcha@example.com' ); $actual = graphql( array( 'query' => $query, 'variables' => $variables ) ); $expected = array( 'data' => array( 'customers' => array( 'nodes' => $this->helper->print_nodes( $users, array( 'filter' => function( $id ) { $customer = new \WC_Customer( $id ); return 'gotcha@example.com' === $customer->get_email(); } ) ), ), ), ); // use --debug flag to view. codecept_debug( $actual ); $this->assertEquals( $expected, $actual ); /** * Assertion Seven * * Tests "role" where argument. */ $variables = array( 'role' => 'SHOP_MANAGER' ); $actual = graphql( array( 'query' => $query, 'variables' => $variables ) ); $expected = array( 'data' => array( 'customers' => array( 'nodes' => $this->helper->print_nodes( $users, array( 'filter' => function( $id ) { $customer = new \WC_Customer( $id ); return 'shop_manager' === $customer->get_role(); } ) ), ), ), ); // use --debug flag to view. codecept_debug( $actual ); $this->assertEquals( $expected, $actual ); /** * Assertion Eight * * Tests "roleIn" where argument. */ $variables = array( 'roleIn' => array( 'SHOP_MANAGER' ) ); $actual = graphql( array( 'query' => $query, 'variables' => $variables ) ); $expected = array( 'data' => array( 'customers' => array( 'nodes' => $this->helper->print_nodes( $users, array( 'filter' => function( $id ) { $customer = new \WC_Customer( $id ); return 'shop_manager' === $customer->get_role(); } ) ), ), ), ); // use --debug flag to view. codecept_debug( $actual ); $this->assertEquals( $expected, $actual ); /** * Assertion Nine * * Tests "roleNotIn" where argument. */ $variables = array( 'roleNotIn' => array( 'SHOP_MANAGER' ) ); $actual = graphql( array( 'query' => $query, 'variables' => $variables ) ); $expected = array( 'data' => array( 'customers' => array( 'nodes' => $this->helper->print_nodes( $users, array( 'filter' => function( $id ) { $customer = new \WC_Customer( $id ); return 'shop_manager' !== $customer->get_role(); } ) ), ), ), ); // use --debug flag to view. codecept_debug( $actual ); $this->assertEquals( $expected, $actual ); /** * Assertion Ten * * Tests "orderby" and "order" where arguments. */ $variables = array( 'orderby' => 'USERNAME', 'order' => 'ASC' ); $actual = graphql( array( 'query' => $query, 'variables' => $variables ) ); $expected = array( 'data' => array( 'customers' => array( 'nodes' => $this->helper->print_nodes( $users, array( 'sorter' => function( $id_a, $id_b ) { $data = new \WC_Customer( $id_a ); $username_a = $data->get_username(); $data = new \WC_Customer( $id_b ); $username_b = $data->get_username(); return strnatcmp( $username_a, $username_b ); } ) ), ), ), ); // use --debug flag to view. codecept_debug( $actual ); $this->assertEquals( $expected, $actual ); } public function testCustomerToOrdersConnection() { $this->order_1 = $this->order_helper->create( array( 'customer_id' => $this->customer ) ); $this->order_2 = $this->order_helper->create( array( 'customer_id' => $this->new_customer ) ); $query = ' query ($customerId: Int) { customer { orders(where: { customerId: $customerId }) { nodes { id } } } } '; /** * Assertion One * * Query for authenticated customer's orders. */ wp_set_current_user( $this->customer ); $actual = graphql( array( 'query' => $query ) ); $expected = array( 'data' => array( 'customer' => array( 'orders' => array( 'nodes' => array( array( 'id' => $this->order_helper->to_relay_id( $this->order_1 ) ), ) ) ) ) ); $this->assertEquals( $expected, $actual ); /** * Assertion Two * * Query for authenticated customer's orders and other customer's orders, but query * should only return authenticated customer's orders. */ wp_set_current_user( $this->new_customer ); $variables = array( 'customerId' => $this->customer ); $actual = graphql( array( 'query' => $query, 'variables' => $variables ) ); $expected = array( 'data' => array( 'customer' => array( 'orders' => array( 'nodes' => array( array( 'id' => $this->order_helper->to_relay_id( $this->order_2 ) ), ) ) ) ) ); $this->assertEquals( $expected, $actual ); } }