Skip to content

BUG: PersistenceManager getObjectByIdentifier does not check for objectType on new objects #3384

Open
@lufonius

Description

@lufonius

Is there an existing issue for this?

  • I have searched the existing issues

Current Behavior

I wrote a functional test, where I create multiple new entities, inject some services and assert the results.
The code to be tested takes a persistent_object_identifier $id as a param and checks against two repositories using $repositoryX->findByIdentifier($id) (which uses getObjectByIdentifier in the background). The id could belong to two possible entity types. using the repository method findByIdentifier, the code finds out what entity type the id belongs to and based on that type, the control flow differs.

However, when the entity is marked as new in the persistence manager (NOT in the unit of work, see the link below for the code), this distinction is not made. the object type is being ignored. See here: https://github.com/neos/flow/blob/b44540d13874862e29671e4f7c40127c87b101b9/Classes/Persistence/Doctrine/PersistenceManager.php#L209

Expected Behavior

I expect an API, that doesn't ignore parameters and has no side effects. i don't know neos flow very well, however it seems like there is no unit test for this method. please correct me if I'm wrong.

Steps To Reproduce

Maybe a little code is best to explain it:

class FindByIdentifierExperimentalTest extends FunctionalTestCase
{
    protected static $testablePersistenceEnabled = true;

    public function testAssumptionThatAnyRepoCanFindAnyEntityTypeByIdentifier() {
        // given: an entity of type Price
        $originalPriceEntity = new Price();
        $originalPriceEntity->amount = 5;
        $originalPriceEntity->currency = "EUR";

        $this->persistenceManager->add($originalPriceEntity);
        $this->persistenceManager->persistAll();

        $priceIdentifier = $this->persistenceManager->getIdentifierByObject($originalPriceEntity);

        // when: a repository that manages a totally different entity type searches for the Price Entity
        $voucherRepository = $this->objectManager->get(VoucherRepository::class);
        $priceEntityFoundViaVoucherRepo = $voucherRepository->findByIdentifier($priceIdentifier);

        // Then: the entity is being found, even though the repository queries for Voucher entities. Voucher and Price entity are not related at all.
        $this->assertEquals($originalPriceEntity, $priceEntityFoundViaVoucherRepo);
    }
}

Environment

- Flow:* 8.3.9
- PHP:8.1.23

Anything else?

I got a bit of mixed feelings about that. however, since I don't know the framework I'd like to have your views on this before I settle for an opinion.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions