Given:

  • An Order Type with Refresh Mode set to "Customer"
  • A draft order whose customer uid == 0
  • A CLI process that loads the order e.g. search_api
  • The CLI process is running as uid == 0 (Which is the default behavior)

Expected result:
The order is NOT refreshed when it is loaded.

Actual result:
The order IS refreshed when loaded.

Details:
This causes inconsistent behavior, as only "anonymous" draft orders are refreshed, but not draft orders associated with a user.

Another inconsistent behavior is if you run the process from the web UX, (again, using search indexing as an example) the administrative user will have a uid > 0, and therefore the anonymous orders will NOT be refreshed when indexing.

While there might be a use case for wanting the order to be refreshed from the CLI, it most likely would expect the behavior to be consistent between both anonymous and non-anonymous draft orders.

I would also posit that we shouldn't refresh order types with RefreshMode == 'Always' during CLI processing either, but am reticent to change the meaning of 'Always'. :)

A side effect of this is that the changed date is updated, extending the window of this draft anonymous order for stale cart purging.

It is a small change to avoid a refresh in this scenario.

Patch to follow.

Comments

TomTech created an issue. See original summary.

tomtech’s picture

Status: Active » Needs review
StatusFileSize
new1.52 KB
jsacksick’s picture

Status: Needs review » Closed (duplicate)

Probably a duplicate of #2994377: Race conditions can theoretically change placed orders, feel free to reopen if you think this isn't a duplicate.

jsacksick’s picture

Maybe the right fix is to copy what 1.x did...

// If the order is anonymous, check the session to see if the order
      // belongs to the current user. Otherwise just check that the order uid
      // matches the current user.
      if ($order->uid == 0 && !commerce_cart_order_session_exists($order->order_id)) {
        return FALSE;
      }

I'm guessing we could check if the customer is anonymous that the current customer can update the order.

jsacksick’s picture

Status: Closed (duplicate) » Active

So I'm guessing a 2.x equivalent fix could look like the following:

// Skip the order refresh in case the current customer cannot update the order.
if (!$order->getCustomer()->isAuthenticated() && !$order->access('update')) {
  return FALSE;
}
tomtech’s picture

Status: Active » Needs review
StatusFileSize
new1.19 KB

Updated patch implementing the suggestion in #3379152-5: Skip Order Refresh on draft anonymous orders on CLI.

Tested and resolves the issue.

While this isn't as performant as just checking the CLI variable, it is more logical, and better matches the logic back in 7.x.

jsacksick’s picture

Issue tags: +Needs tests

Let's test this from OrderRefreshTest::testShouldRefresh().

tomtech’s picture

As requested in #7, test steps are added in OrderRefreshTest::testShouldRefresh().

This doesn't actually invoke the _commerce_cart_order_access, though, which would ensure that OrderRefreshTest::testShouldRefresh() returns TRUE when the cart is present in the cart_session.

CartOrderRefreshTest.php has been added to the cart module to test those scenarios, both when the anonymous user has the cart in the cart_session (returning TRUE) and when it does not (returning FALSE).

  • jsacksick committed 8641e417 on 8.x-2.x authored by TomTech
    Issue #3379152 by TomTech, jsacksick: Skip Order Refresh on draft...

  • jsacksick committed f8b21ed9 on 3.0.x authored by TomTech
    Issue #3379152 by TomTech, jsacksick: Skip Order Refresh on draft...
jsacksick’s picture

Status: Needs review » Fixed

Committed, thanks Tom!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.