Files
Geoff TaylorandGitHub 146a4430f9 fix: add authenticate flag to registerCustomer mutation (#973)
* fix: add authenticate flag to registerCustomer mutation

Adds an optional `authenticate` boolean input to the registerCustomer
mutation. When true, the mutation sets the current user and reinitializes
the session token for the newly registered customer. Defaults to false
to prevent nonce verification failures in contexts like GraphiQL that
send a nonce with the request.

Resolves #464

* test: add authenticate flag to existing register customer tests

The existing wpunit tests for registerCustomer rely on the user being
authenticated after registration to assert on customer.databaseId and
viewer.userId. Now that authenticate defaults to false, the tests must
explicitly pass authenticate: true.

* test: add authenticate flag to testCustomerMutationsWithMeta

* fix: use explicit billing emails in CustomerQueriesTest assertions

Use wildcard index matching with specific email values instead of
hardcoded node indices with NOT_NULL checks. This prevents failures
when other users in the DB shift the node positions.
2026-03-18 13:13:45 -04:00

117 lines
4.9 KiB
PHP

<?php
/**
* Factory class for the WooCommerce's customer data objects.
*
* @since v0.10.0
* @package Tests\WPGraphQL\WooCommerce\Factory
*/
namespace Tests\WPGraphQL\WooCommerce\Factory;
use Faker\Factory;
/**
* Customer factory class for testing.
*/
class CustomerFactory extends \WP_UnitTest_Factory_For_Thing {
public function __construct( $factory = null ) {
parent::__construct( $factory );
$this->default_generation_definitions = [];
$this->dummy = Factory::create();
}
public function create_object( $args ) {
if ( is_wp_error( $args ) ) {
codecept_debug( $args );
}
$customer = new \WC_Customer();
// Create customer details
$username = empty( $args['username'] ) ? $this->dummy->userName() : $args['username'];
$first_name = empty( $args['first_name'] ) ? $this->dummy->firstName() : $args['first_name'];
$last_name = empty( $args['last_name'] ) ? $this->dummy->lastName() : $args['last_name'];
$street = empty( $args['street'] ) ? $this->dummy->streetAddress() : $args['street'];
$city = empty( $args['city'] ) ? $this->dummy->city() : $args['city'];
$state = empty( $args['state'] ) ? $this->dummy->state() : $args['state'];
$postcode = empty( $args['postcode'] ) ? $this->dummy->postcode() : $args['postcode'];
$country = empty( $args['country'] ) ? 'US' : $args['country'];
$email = empty( $args['email'] ) ? $this->dummy->email() : $args['email'];
$phone = empty( $args['phone'] ) ? $this->dummy->phoneNumber() : $args['phone'];
$args = array_merge(
[
'billing' => [],
'shipping' => [],
],
$args
);
$customer->set_billing_first_name( ! empty( $args['billing']['first_name'] ) ? $args['billing']['first_name'] : $first_name );
$customer->set_billing_last_name( ! empty( $args['billing']['last_name'] ) ? $args['billing']['last_name'] : $last_name );
$customer->set_billing_address_1( ! empty( $args['billing']['address_1'] ) ? $args['billing']['address_1'] : $street );
$customer->set_billing_address_2( ! empty( $args['billing']['address_2'] ) ? $args['billing']['address_2'] : '' );
$customer->set_billing_city( ! empty( $args['billing']['city'] ) ? $args['billing']['city'] : $city );
$customer->set_billing_state( ! empty( $args['billing']['state'] ) ? $args['billing']['state'] : $state );
$customer->set_billing_postcode( ! empty( $args['billing']['postcode'] ) ? $args['billing']['postcode'] : $postcode );
$customer->set_billing_country( ! empty( $args['billing']['country'] ) ? $args['billing']['country'] : $country );
$customer->set_billing_email( ! empty( $args['billing']['email'] ) ? $args['billing']['email'] : $email );
$customer->set_billing_phone( ! empty( $args['billing']['phone'] ) ? $args['billing']['phone'] : $phone );
$customer->set_shipping_first_name( ! empty( $args['shipping']['first_name'] ) ? $args['shipping']['first_name'] : $first_name );
$customer->set_shipping_last_name( ! empty( $args['shipping']['last_name'] ) ? $args['shipping']['last_name'] : $last_name );
$customer->set_shipping_address_1( ! empty( $args['shipping']['address_1'] ) ? $args['shipping']['address_1'] : $street );
$customer->set_shipping_address_2( ! empty( $args['shipping']['address_2'] ) ? $args['shipping']['address_2'] : '' );
$customer->set_shipping_city( ! empty( $args['shipping']['city'] ) ? $args['shipping']['city'] : $city );
$customer->set_shipping_state( ! empty( $args['shipping']['state'] ) ? $args['shipping']['state'] : $state );
$customer->set_shipping_postcode( ! empty( $args['shipping']['postcode'] ) ? $args['shipping']['postcode'] : $postcode );
$customer->set_shipping_country( ! empty( $args['shipping']['country'] ) ? $args['shipping']['country'] : $country );
$customer->set_shipping_phone( ! empty( $args['shipping']['phone'] ) ? $args['shipping']['phone'] : $phone );
// Set data.
$customer->set_props(
array_merge(
[
'email' => $email,
'first_name' => $first_name,
'last_name' => $last_name,
'display_name' => $username,
'role' => 'customer',
'username' => $username,
'is_paying_customer' => false,
],
$args
)
);
// Set meta data.
if ( ! empty( $args['meta_data'] ) ) {
$customer->set_meta_data( $args['meta_data'] );
}
return $customer->save();
}
public function update_object( $object, $fields ) {
if ( ! $object instanceof \WC_Customer && 0 !== absint( $object ) ) {
$object = $this->get_object_by_id( $object );
}
foreach ( $fields as $field => $field_value ) {
if ( ! is_callable( [ $object, "set_{$field}" ] ) ) {
throw new \Exception(
sprintf( '"%1$s" is not a valid %2$s coupon field.', $field, $object->get_type() )
);
}
$object->{"set_{$field}"}( $field_value );
}
$object->save();
}
public function get_object_by_id( $id ) {
return new \WC_Customer( $id );
}
}