2023-05-22 13:43:10 -04:00
|
|
|
<?php
|
|
|
|
|
|
2024-08-07 12:39:45 -04:00
|
|
|
class SessionMutationsTest extends \Tests\WPGraphQL\WooCommerce\TestCase\WooGraphQLTestCase {
|
2023-05-22 13:43:10 -04:00
|
|
|
public function testUpdateSessionMutation() {
|
|
|
|
|
// Create registered customer.
|
|
|
|
|
$registered = $this->factory->customer->create();
|
|
|
|
|
$this->loginAs( $registered );
|
|
|
|
|
|
|
|
|
|
// Create query.
|
|
|
|
|
$query = '
|
|
|
|
|
mutation($input: UpdateSessionInput!) {
|
|
|
|
|
updateSession(input: $input) {
|
|
|
|
|
session {
|
|
|
|
|
id
|
|
|
|
|
key
|
|
|
|
|
value
|
|
|
|
|
}
|
|
|
|
|
customer {
|
|
|
|
|
id
|
|
|
|
|
session {
|
|
|
|
|
id
|
|
|
|
|
key
|
|
|
|
|
value
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
';
|
|
|
|
|
|
|
|
|
|
$variables = [
|
|
|
|
|
'input' => [
|
|
|
|
|
'sessionData' => [
|
|
|
|
|
[
|
|
|
|
|
'key' => 'test-2',
|
|
|
|
|
'value' => 'test-value',
|
|
|
|
|
],
|
|
|
|
|
],
|
|
|
|
|
],
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Assert working.
|
|
|
|
|
*/
|
|
|
|
|
$response = $this->graphql( compact( 'query', 'variables' ) );
|
|
|
|
|
$expected = [
|
|
|
|
|
$this->expectedObject(
|
|
|
|
|
'updateSession.session.#',
|
|
|
|
|
[
|
|
|
|
|
$this->expectedField( 'key', 'test-2' ),
|
|
|
|
|
$this->expectedField( 'value', 'test-value' ),
|
|
|
|
|
]
|
|
|
|
|
),
|
2024-08-06 16:54:18 -06:00
|
|
|
$this->expectedField( 'updateSession.customer.id', $this->toRelayId( 'user', $registered ) ),
|
2023-05-22 13:43:10 -04:00
|
|
|
$this->expectedObject(
|
|
|
|
|
'updateSession.customer.session.#',
|
|
|
|
|
[
|
|
|
|
|
$this->expectedField( 'key', 'test-2' ),
|
|
|
|
|
$this->expectedField( 'value', 'test-value' ),
|
|
|
|
|
]
|
|
|
|
|
),
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
$this->assertQuerySuccessful( $response, $expected );
|
|
|
|
|
}
|
2024-08-07 12:39:45 -04:00
|
|
|
|
|
|
|
|
public function testForgetSessionMutation() {
|
2024-11-06 19:23:36 -05:00
|
|
|
$this->markTestSkipped( 'This test has not been implemented yet.' );
|
|
|
|
|
|
2024-08-07 12:39:45 -04:00
|
|
|
// Create registered customer.
|
|
|
|
|
$registered = $this->factory->customer->create();
|
|
|
|
|
$this->loginAs( $registered );
|
|
|
|
|
|
|
|
|
|
// Add products to cart.
|
|
|
|
|
$this->factory->cart->add(
|
|
|
|
|
[
|
|
|
|
|
'product_id' => $this->factory->product->createSimple(),
|
|
|
|
|
'quantity' => 2,
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
'product_id' => $this->factory->product->createSimple(),
|
|
|
|
|
'quantity' => 1,
|
|
|
|
|
]
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Save session.
|
|
|
|
|
\WC()->session->save_data();
|
|
|
|
|
|
|
|
|
|
// Reinitialize session.
|
|
|
|
|
\WC()->session->init();
|
|
|
|
|
|
|
|
|
|
// Confirm cart has items.
|
|
|
|
|
$cart_query = '
|
|
|
|
|
query {
|
|
|
|
|
cart {
|
|
|
|
|
contents {
|
|
|
|
|
nodes {
|
|
|
|
|
key
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
';
|
|
|
|
|
|
|
|
|
|
$response = $this->graphql( [ 'query' => $cart_query ] );
|
|
|
|
|
$this->assertQuerySuccessful(
|
|
|
|
|
$response,
|
|
|
|
|
[ $this->expectedField( 'cart.contents.nodes', static::NOT_FALSY ) ]
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Forget session.
|
|
|
|
|
$query = 'mutation {
|
|
|
|
|
forgetSession(input: {}) {
|
|
|
|
|
session {
|
|
|
|
|
id
|
|
|
|
|
key
|
|
|
|
|
value
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}';
|
|
|
|
|
|
|
|
|
|
$response = $this->graphql( compact( 'query' ) );
|
|
|
|
|
$this->assertQuerySuccessful( $response );
|
|
|
|
|
|
|
|
|
|
// Reinitialize session.
|
|
|
|
|
\WC()->session->init();
|
|
|
|
|
|
|
|
|
|
// Confirm cart is empty.
|
|
|
|
|
$response = $this->graphql( [ 'query' => $cart_query ] );
|
|
|
|
|
$this->assertQuerySuccessful(
|
|
|
|
|
$response,
|
|
|
|
|
[ $this->expectedField( 'cart.contents.nodes', static::IS_FALSY ) ]
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|