Problem/Motivation

I discovered an interesting bug, thanks to a failing Commerce invoice test, caused by #3083407: Fire intended events when invoking a specific transition.

I realized that because we're storing the transition to apply in a variable and because we're not "nullifying" the transition after applying it/dispatching the event, changing the entity state (without calling the applyTransition() or applyTransitionById() during the same request would result in the wrong transition events to be fired (for the previously applied transition).

So basically with the following code:

    $entity->getState()->applyTransitionById('create');
    $entity->save();
    $entity->set('state', 'canceled')->save();

The second save would trigger the events associated to the first transition applied (instead of the events associated to the "cancel" transition.

I managed to write a failing test.

Comments

jsacksick created an issue. See original summary.

jsacksick’s picture

Category: Task » Bug report
jsacksick’s picture

Status: Active » Needs review
StatusFileSize
new3.11 KB
new3.71 KB

I believe this fix deserves tagging a new State machine release.

jsacksick’s picture

Note that I previously tried to nullify the transitionToApply() variable right after we're accessing it, but this was causing the events triggered on postSave() to be incorrect. So this needs to happen on postSave().

jsacksick’s picture

Title: Setting the state manually after applying a transition results in the wrong transition evens being dispatched » Setting the state manually after applying a transition results in the wrong transition events being dispatched

  • jsacksick committed c411127 on 8.x-1.x
    Issue #3227087 by jsacksick: Setting the state manually after applying a...
jsacksick’s picture

Status: Needs review » Fixed

Committed!

Status: Fixed » Closed (fixed)

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