Skip to content
Open
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
6 changes: 6 additions & 0 deletions src/WooCommerce/Method/OrderMethods.php
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,9 @@ public function haveOrderItemMetaInDatabase(int $orderItemId, string $metaKey, m
* $I->assertSame($orderId, $found);
* ```
*
* Accepts a `status` criterion with or without the `wc-` prefix (e.g. `processing` or
* `wc-processing`); it is normalized before matching against the stored value.
*
* @param array<string, mixed> $criteria Database query criteria (e.g., ['status' => 'pending', 'id' => 123]). Supports storage-agnostic keys like 'id', 'status'
*
* @return int|false Order ID if found, false otherwise
Expand Down Expand Up @@ -321,6 +324,9 @@ public function grabOrderItemFromDatabase(array $criteria): array
* $I->seeOrderInDatabase(['id' => $orderId, 'status' => 'completed']);
* ```
*
* Accepts a `status` criterion with or without the `wc-` prefix (e.g. `completed` or
* `wc-completed`); it is normalized before matching against the stored value.
*
* @param array<string, mixed> $criteria Database query criteria (e.g., ['id' => 123, 'status' => 'pending']). Supports storage-agnostic keys like 'id', 'status'
*
* @return void
Expand Down
9 changes: 9 additions & 0 deletions src/WooCommerce/Method/SubscriptionMethods.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@ public function haveSubscriptionProductInDatabase(array $overrides = []): int
* $I->assertSame($subscriptionId, $found);
* ```
*
* Accepts a `status` criterion with or without the `wc-` prefix (e.g. `active` or
* `wc-active`); it is normalized before matching against the stored value.
*
* @param array<string, mixed> $criteria Database query criteria (e.g., ['status' => 'wc-active', 'id' => 123])
*
* @return int|false Subscription ID if found, false otherwise
Expand Down Expand Up @@ -295,6 +298,9 @@ public function expireSubscription(int $subscriptionId): void
* $I->seeSubscriptionInDatabase(['id' => $subscriptionId, 'status' => 'wc-active']);
* ```
*
* Accepts a `status` criterion with or without the `wc-` prefix (e.g. `active` or
* `wc-active`); it is normalized before matching against the stored value.
*
* @param array<string, mixed> $criteria Database query criteria (e.g., ['id' => 123, 'status' => 'wc-active'])
*
* @return void
Expand Down Expand Up @@ -354,6 +360,9 @@ public function seeSubscriptionStatus(int $subscriptionId, string $status): void
* $I->dontSeeSubscriptionInDatabase(['id' => 999]);
* ```
*
* Accepts a `status` criterion with or without the `wc-` prefix (e.g. `cancelled` or
* `wc-cancelled`); it is normalized before matching against the stored value.
*
* @param array<string, mixed> $criteria Database query criteria (e.g., ['status' => 'wc-deleted'])
*
* @return void
Expand Down
12 changes: 3 additions & 9 deletions src/WooCommerce/OrderStorage/HPOSOrderStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,17 +122,11 @@ public function haveOrderAddressInDatabase(int $orderId, string $addressType, ar
*/
public function mapCriteria(array $criteria): array
{
$mapped = [];
$prepped = [];
foreach ($criteria as $key => $value) {
if ($key === 'post_status') {
$mapped['status'] = $value;
} elseif ($key === 'post_title') {
$mapped['title'] = $value;
} else {
$mapped[$key] = $value;
}
$prepped[$key === 'post_title' ? 'title' : $key] = $value;
}
return $mapped;
return parent::mapCriteria($prepped);
}

/**
Expand Down
7 changes: 6 additions & 1 deletion src/WooCommerce/Storage/AbstractHPOSStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,12 @@ public function mapCriteria(array $criteria): array
$mapped[$key === 'post_status' ? 'status' : $key] = $value;
}

return $mapped;
return $this->normalizeStatusInCriteria($mapped);
}

protected function getStatusColumnName(): string
{
return 'status';
}

protected function grabEntityStatus(int $entityId): string
Expand Down
7 changes: 6 additions & 1 deletion src/WooCommerce/Storage/AbstractLegacyStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,12 @@ public function mapCriteria(array $criteria): array
}
}

return $mapped;
return $this->normalizeStatusInCriteria($mapped);
}

protected function getStatusColumnName(): string
{
return 'post_status';
}

protected function grabEntityStatus(int $entityId): string
Expand Down
18 changes: 18 additions & 0 deletions src/WooCommerce/Storage/AbstractStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public function __construct(protected WPDb $wpDb)

abstract protected function getEntityIdKey(): string;

abstract protected function getStatusColumnName(): string;

/**
* Normalize a status value, with an unprefixed WC status accepted.
*
Expand All @@ -25,6 +27,22 @@ protected function normalizeStatusValue(mixed $status): mixed
return is_string($status) ? StatusNormalizer::normalize($status) : $status;
}

/**
* Normalize the status criterion, when present, with an unprefixed WC status accepted.
*
* @param array<string, mixed> $criteria Database query criteria, already mapped to storage keys.
* @return array<string, mixed> Criteria with the status column normalized.
*/
protected function normalizeStatusInCriteria(array $criteria): array
{
$statusKey = $this->getStatusColumnName();
if (isset($criteria[$statusKey])) {
$criteria[$statusKey] = $this->normalizeStatusValue($criteria[$statusKey]);
}

return $criteria;
}

public function getMetaTableName(): string
{
return $this->wpDb->grabPostMetaTableName();
Expand Down
11 changes: 3 additions & 8 deletions src/WooCommerce/SubscriptionStorage/HPOSSubscriptionStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,16 +87,11 @@ public function haveSubscriptionStatus(int $subscriptionId, string $status): voi

public function mapCriteria(array $criteria): array
{
$mapped = [];
$prepped = [];
foreach ($criteria as $key => $value) {
if ($key === 'post_status') {
$mapped['status'] = $value;
} elseif ($key === 'ID') {
$mapped['id'] = $value;
} else {
$mapped[$key] = $value;
}
$prepped[$key === 'ID' ? 'id' : $key] = $value;
}
$mapped = parent::mapCriteria($prepped);
$mapped['type'] = 'shop_subscription';
return $mapped;
}
Expand Down
12 changes: 12 additions & 0 deletions tests/acceptance/OrderCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,18 @@ public function testSeeOrderInDatabase(AcceptanceTester $I): void
]);
}

public function testSeeOrderInDatabaseNormalizesUnprefixedStatusCriterion(AcceptanceTester $I): void
{
$orderId = $I->haveOrderInDatabase([
'post_status' => 'wc-active',
]);

$I->seeOrderInDatabase([
'ID' => $orderId,
'post_status' => 'active',
]);
}

public function testSeeOrderMetaInDatabase(AcceptanceTester $I): void
{
$orderId = $I->haveOrderInDatabase();
Expand Down
25 changes: 25 additions & 0 deletions tests/acceptance/OrderHPOSCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,19 @@ public function testGrabOrderIdFromDatabase(AcceptanceTester $I): void
$I->assertFalse($notFound);
}

public function testGrabOrderIdFromDatabaseNormalizesUnprefixedStatusCriterion(AcceptanceTester $I): void
{
$uniqueCustomerId = 9998;

$orderId = $I->haveOrderInDatabase([
'status' => 'wc-active',
'customer_id' => $uniqueCustomerId,
]);

$grabbedId = $I->grabOrderIdFromDatabase(['status' => 'active', 'customer_id' => $uniqueCustomerId]);
$I->assertSame($orderId, $grabbedId);
}

public function testGrabOrderItemFromDatabase(AcceptanceTester $I): void
{
$orderId = $I->haveOrderInDatabase();
Expand Down Expand Up @@ -310,6 +323,18 @@ public function testSeeOrderInDatabase(AcceptanceTester $I): void
]);
}

public function testSeeOrderInDatabaseNormalizesUnprefixedStatusCriterion(AcceptanceTester $I): void
{
$orderId = $I->haveOrderInDatabase([
'status' => 'wc-active',
]);

$I->seeOrderInDatabase([
'id' => $orderId,
'status' => 'active',
]);
}

public function testSeeOrderMetaInDatabase(AcceptanceTester $I): void
{
$orderId = $I->haveOrderInDatabase();
Expand Down
9 changes: 9 additions & 0 deletions tests/acceptance/SubscriptionCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,15 @@ public function testSeeSubscriptionInDatabase(AcceptanceTester $I): void
$I->seeSubscriptionInDatabase(['ID' => $subscriptionId, 'post_status' => 'wc-active']);
}

public function testSeeSubscriptionInDatabaseNormalizesUnprefixedStatusCriterion(AcceptanceTester $I): void
{
$subscriptionId = $I->haveSubscriptionInDatabase([
'post_status' => 'wc-active',
]);

$I->seeSubscriptionInDatabase(['ID' => $subscriptionId, 'post_status' => 'active']);
}

public function testSeeSubscriptionMetaWithSubscriptionId(AcceptanceTester $I): void
{
$subscriptionId = $I->haveSubscriptionInDatabase();
Expand Down
52 changes: 52 additions & 0 deletions tests/acceptance/SubscriptionHPOSCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Aztec\WPBrowser\Tests\Acceptance;

use Aztec\WPBrowser\Tests\Support\AcceptanceTester;
use PHPUnit\Framework\AssertionFailedError;

class SubscriptionHPOSCest
{
Expand Down Expand Up @@ -117,6 +118,23 @@ public function testGrabSubscriptionIdFromDatabase(AcceptanceTester $I): void
$I->assertSame($subscriptionId, $grabbedId);
}

public function testGrabSubscriptionIdFromDatabaseNormalizesUnprefixedStatusCriterion(AcceptanceTester $I): void
{
$uniqueCustomerId = 8887;

$subscriptionId = $I->haveSubscriptionInDatabase([
'status' => 'wc-active',
'customer_id' => $uniqueCustomerId,
]);

$grabbedId = $I->grabSubscriptionIdFromDatabase([
'status' => 'active',
'customer_id' => $uniqueCustomerId,
]);

$I->assertSame($subscriptionId, $grabbedId);
}

public function testGrabSubscriptionIdNotFound(AcceptanceTester $I): void
{
$result = $I->grabSubscriptionIdFromDatabase([
Expand Down Expand Up @@ -220,6 +238,15 @@ public function testSeeSubscriptionInDatabase(AcceptanceTester $I): void
$I->seeSubscriptionInDatabase(['id' => $subscriptionId, 'status' => 'wc-active']);
}

public function testSeeSubscriptionInDatabaseNormalizesUnprefixedStatusCriterion(AcceptanceTester $I): void
{
$subscriptionId = $I->haveSubscriptionInDatabase([
'status' => 'wc-active',
]);

$I->seeSubscriptionInDatabase(['id' => $subscriptionId, 'status' => 'active']);
}

public function testSeeSubscriptionMetaWithSubscriptionId(AcceptanceTester $I): void
{
$subscriptionId = $I->haveSubscriptionInDatabase();
Expand Down Expand Up @@ -262,6 +289,31 @@ public function testDontSeeSubscriptionInDatabase(AcceptanceTester $I): void
]);
}

public function testDontSeeSubscriptionInDatabaseNormalizesUnprefixedStatusCriterion(
AcceptanceTester $I,
): void {
$subscriptionId = $I->haveSubscriptionInDatabase([
'status' => 'wc-active',
]);

$I->dontSeeSubscriptionInDatabase([
'id' => $subscriptionId,
'status' => 'cancelled',
]);

// If the unprefixed 'active' criterion were not normalized to 'wc-active', it would
// never match the stored row and dontSeeSubscriptionInDatabase would (incorrectly) pass.
$I->expectThrowable(
AssertionFailedError::class,
function () use ($I, $subscriptionId): void {
$I->dontSeeSubscriptionInDatabase([
'id' => $subscriptionId,
'status' => 'active',
]);
},
);
}

public function testDontSeeSubscriptionMetaInDatabase(AcceptanceTester $I): void
{
$subscriptionId = $I->haveSubscriptionInDatabase();
Expand Down
Loading