Skip to content

Commit

Permalink
devops: QLSessionHandlerTest patched for suite testing
Browse files Browse the repository at this point in the history
  • Loading branch information
kidunot89 committed Jun 20, 2024
1 parent ff68a64 commit 55e9e13
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 10 deletions.
28 changes: 27 additions & 1 deletion includes/utils/class-ql-session-handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ class QL_Session_Handler extends WC_Session_Handler {
*/
protected $_issuing_new_token = false; // @codingStandardsIgnoreLine

/**
* True when a new session cookie has been issued.
*
* @var bool $_issuing_new_cookie
*/
protected $_issuing_new_cookie = false; // @codingStandardsIgnoreLine

/**
* Constructor for the session class.
*/
Expand Down Expand Up @@ -281,10 +288,19 @@ public function get_session_header() {
*
* @return bool
*/
private function sending_token() {
public function sending_token() {
return $this->_has_token || $this->_issuing_new_token;
}

/**
* Determine if a HTTP cookie is being sent in the page response.
*
* @return bool
*/
public function sending_cookie() {
return $this->_has_cookie || $this->_issuing_new_cookie;
}

/**
* Creates JSON Web Token for customer session.
*
Expand Down Expand Up @@ -388,6 +404,16 @@ function ( $headers ) {
}
}

/**
* {@inheritDoc}
*/
public function set_customer_session_cookie( $set ) {

Check failure on line 410 in includes/utils/class-ql-session-handler.php

View workflow job for this annotation

GitHub Actions / Testing WooGraphQL code quality w/ PHPStan

Method WPGraphQL\WooCommerce\Utils\QL_Session_Handler::set_customer_session_cookie() has no return type specified.
parent::set_customer_session_cookie( $set );
if ( $set ) {
$this->_issuing_new_cookie = true;
}
}

/**
* Return true if the current user has an active session, i.e. a cookie to retrieve values.
*
Expand Down
32 changes: 23 additions & 9 deletions tests/wpunit/QLSessionHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,17 @@
define( 'GRAPHQL_WOOCOMMERCE_SECRET_KEY', 'graphql-woo-cart-session' );
}

/**
* @runTestsInSeparateProcesses
* @preserveGlobalState disabled
*/
class QLSessionHandlerTest extends \Tests\WPGraphQL\WooCommerce\TestCase\WooGraphQLTestCase {

public function setUp(): void {
parent::setUp();

// before
unset( $_SERVER['HTTP_WOOCOMMERCE_SESSION'] );
$customer_cookie_key = apply_filters( 'woocommerce_cookie', 'wp_woocommerce_session_' . COOKIEHASH );
wc_setcookie( $customer_cookie_key, 0, time() - HOUR_IN_SECONDS );
unset( $_COOKIE[ $customer_cookie_key ] );
}
public function tearDown(): void {
unset( $_SERVER );
WC()->session->destroy_session();
Expand Down Expand Up @@ -78,8 +84,6 @@ public function test_init_on_graphql_request() {
}

public function test_init_on_non_graphql_request() {
$product_id = $this->factory()->product->createSimple();

// Create session handler.
$session = new QL_Session_Handler();

Expand All @@ -90,13 +94,23 @@ public function test_init_on_non_graphql_request() {
$session->init();

// Add product to cart and start the session.
$this->factory()->cart->add( $product_id );
$this->factory->cart->add(
$this->factory->product->createSimple(),
$this->factory->product->createSimple(),
$this->factory->product->createSimple(),
$this->factory->product->createSimple(),
$this->factory->product->createSimple(),
$this->factory->product->createSimple(),
$this->factory->product->createSimple(),
);


// Assert session has started.
$this->assertTrue( $session->has_session(), 'Should have session.' );
//$this->assertTrue( $session->sending_cookie(), 'Issuing new customer session cookie.' );

// Assert no tokens are being issued.
$this->assertFalse( $session->build_token(), 'Should not have a token.' );
$this->assertFalse( $session->sending_token(), 'Should not be issuing a new customer token.' );
$this->assertFalse( $session->build_token(), 'Should not be issuing a new customer token.' );
}

public function test_init_on_non_graphql_request_with_session_token() {
Expand Down

0 comments on commit 55e9e13

Please sign in to comment.