Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion projects/packages/woocommerce-analytics/.phan/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,12 @@
// Require base config.
require __DIR__ . '/../../../../.phan/config.base.php';

return make_phan_config( dirname( __DIR__ ), array( '+stubs' => array( 'woocommerce' ) ) );
return make_phan_config(
dirname( __DIR__ ),
array(
'+stubs' => array( 'woocommerce' ),
'exclude_file_regex' => array(
'tests/', // Exclude test files from static analysis.
),
)
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: fixed

Fix product_purchase event not tracking for shortcode checkout due to incorrect order ID type check
1 change: 1 addition & 0 deletions projects/packages/woocommerce-analytics/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"require-dev": {
"yoast/phpunit-polyfills": "^4.0.0",
"automattic/jetpack-changelogger": "@dev",
"automattic/jetpack-test-environment": "@dev",
"automattic/phpunit-select-config": "@dev"
},
"autoload": {
Expand Down
13 changes: 3 additions & 10 deletions projects/packages/woocommerce-analytics/src/class-universal.php
Original file line number Diff line number Diff line change
Expand Up @@ -289,19 +289,12 @@ public function checkout_process() {
/**
* After the order processed, fire an event for each item in the order
*
* @param string|WC_Order $order_id_or_order Order Id or Order object.
* @param int|string|WC_Order $order_id_or_order Order Id or Order object.
*/
public function order_process( $order_id_or_order ) {
if ( is_string( $order_id_or_order ) ) {
$order = wc_get_order( $order_id_or_order );
} else {
$order = $order_id_or_order;
}
$order = wc_get_order( $order_id_or_order );

if (
! $order
|| ! $order instanceof WC_Order
) {
if ( ! $order ) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php
/**
* Tests for the Universal class.
*
* @package automattic/woocommerce-analytics
*/

namespace Automattic\Woocommerce_Analytics;

use WC_Order;
use WorDBless\BaseTestCase;

/**
* Tests for the Universal class.
*/
class Universal_Test extends BaseTestCase {

/**
* Reset global mocks before each test.
*/
public function set_up(): void {
parent::set_up();
global $wc_get_order_calls, $wc_get_order_mock_return;
$wc_get_order_calls = array();
$wc_get_order_mock_return = false;
}

/**
* Test that order_process calls wc_get_order with an integer order ID.
*/
public function test_order_process_handles_integer_order_id(): void {
global $wc_get_order_calls, $wc_get_order_mock_return;

// Set up mock to return false (order not found).
$wc_get_order_mock_return = false;

$universal = new Universal();
$universal->order_process( 12345 );

$this->assertCount( 1, $wc_get_order_calls, 'wc_get_order should be called once.' );
$this->assertSame( 12345, $wc_get_order_calls[0], 'wc_get_order should receive the integer order ID.' );
}

/**
* Test that order_process calls wc_get_order with a string order ID.
*/
public function test_order_process_handles_string_order_id(): void {
global $wc_get_order_calls, $wc_get_order_mock_return;

// Set up mock to return false (order not found).
$wc_get_order_mock_return = false;

$universal = new Universal();
$universal->order_process( '12345' );

$this->assertCount( 1, $wc_get_order_calls, 'wc_get_order should be called once.' );
$this->assertSame( '12345', $wc_get_order_calls[0], 'wc_get_order should receive the string order ID.' );
}

/**
* Test that order_process calls wc_get_order with a WC_Order object.
*/
public function test_order_process_handles_wc_order_object(): void {
global $wc_get_order_calls, $wc_get_order_mock_return;

// Set up mock to return false (order not found).
$wc_get_order_mock_return = false;

$order = new WC_Order();

$universal = new Universal();
$universal->order_process( $order );

$this->assertCount( 1, $wc_get_order_calls, 'wc_get_order should be called once.' );
$this->assertSame( $order, $wc_get_order_calls[0], 'wc_get_order should receive the WC_Order object.' );
}

/**
* Test that order_process returns early when wc_get_order returns false.
*/
public function test_order_process_returns_early_when_order_not_found(): void {
global $wc_get_order_mock_return;

// Set up mock to return false.
$wc_get_order_mock_return = false;

$universal = new Universal();
$universal->order_process( 12345 );

// If we get here without errors, the method completed without processing a non-existent order.
$this->assertTrue( true, 'order_process should handle a missing order without throwing an exception.' );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,11 @@
* Include the composer autoloader.
*/
require_once __DIR__ . '/../../vendor/autoload.php';

// Include WooCommerce mocks before initializing test environment.
require_once __DIR__ . '/mocks/woocommerce-functions.php';
require_once __DIR__ . '/mocks/class-wc-order.php';
require_once __DIR__ . '/mocks/class-wc-tracks.php';

// Initialize WordPress test environment.
\Automattic\Jetpack\Test_Environment::init();
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
<?php
/**
* Mock WC_Order class for testing.
*
* @package automattic/woocommerce-analytics
*/

if ( ! class_exists( 'WC_Order' ) ) {
/**
* Mock WC_Order class for testing.
*/
class WC_Order {
/**
* Order ID.
*
* @var int
*/
private $id = 123;

/**
* Order items.
*
* @var array
*/
private $items = array();

/**
* Order coupons.
*
* @var array
*/
private $coupons = array();

/**
* Get order ID.
*
* @return int
*/
public function get_id() {
return $this->id;
}

/**
* Get payment method.
*
* @return string
*/
public function get_payment_method() {
return 'stripe';
}

/**
* Get payment method title.
*
* @return string
*/
public function get_payment_method_title() {
return 'Credit Card';
}

/**
* Get user.
*
* @return object|false
*/
public function get_user() {
return false;
}

/**
* Get created via.
*
* @return string
*/
public function get_created_via() {
return 'checkout';
}

/**
* Get items.
*
* @return array
*/
public function get_items() {
return $this->items;
}

/**
* Set items for testing.
*
* @param array $items Items to set.
*/
public function set_items( $items ) {
$this->items = $items;
}

/**
* Get coupons.
*
* @return array
*/
public function get_coupons() {
return $this->coupons;
}

/**
* Get order number.
*
* @return string
*/
public function get_order_number() {
return '123';
}

/**
* Get subtotal.
*
* @return float
*/
public function get_subtotal() {
return 100.00;
}

/**
* Get total.
*
* @return float
*/
public function get_total() {
return 110.00;
}

/**
* Get discount total.
*
* @return float
*/
public function get_discount_total() {
return 0.00;
}

/**
* Get total tax.
*
* @return float
*/
public function get_total_tax() {
return 10.00;
}

/**
* Get shipping total.
*
* @return float
*/
public function get_shipping_total() {
return 5.00;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
/**
* Mock WC_Tracks class for testing.
*
* @package automattic/woocommerce-analytics
*/

if ( ! class_exists( 'WC_Tracks' ) ) {
/**
* Mock WC_Tracks class for testing.
*/
class WC_Tracks {
// Empty base class for WC_Analytics_Tracking to extend.
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php
/**
* WooCommerce function mocks for testing.
*
* @package automattic/woocommerce-analytics
*/

// phpcs:disable WordPress.NamingConventions.ValidFunctionName.FunctionNameInvalid

/**
* Global variable to store mock order for testing.
*
* @var mixed
*/
global $wc_get_order_mock_return;
$wc_get_order_mock_return = false;

/**
* Global variable to track wc_get_order calls.
*
* @var array
*/
global $wc_get_order_calls;
$wc_get_order_calls = array();

if ( ! function_exists( 'wc_get_order' ) ) {
/**
* Mock wc_get_order function.
*
* @param mixed $the_order Post object or post ID of the order.
* @return mixed The mocked return value.
*/
function wc_get_order( $the_order = false ) {
global $wc_get_order_mock_return, $wc_get_order_calls;
$wc_get_order_calls[] = $the_order;
return $wc_get_order_mock_return;
}
}

if ( ! function_exists( 'WC' ) ) {
/**
* Mock WC function.
*
* @return object Mock WooCommerce object.
*/
function WC() {
return new class() {
/**
* Session property.
*
* @var object|null
*/
public $session = null;
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: other

Updated package dependencies.
Loading
Loading