Problem/Motivation

EntityRepository::loadEntityByUuid() is documented with the following documentation comment.

  /**
   * Loads an entity by UUID.
   *
   * Note that some entity types may not support UUIDs.
   *
   * @param string $entity_type_id
   *   The entity type ID to load from.
   * @param string $uuid
   *   The UUID of the entity to load.
   *
   * @return \Drupal\Core\Entity\EntityInterface|null
   *   The entity object, or NULL if there is no entity with the given UUID.
   *
   * @throws \Drupal\Core\Entity\EntityStorageException
   *   Thrown in case the requested entity type does not support UUIDs.
   */

EntityRepository::loadEntityByUuid() doesn't check for invalid nor unexisting UUIDs.

Proposed resolution

Let's return NULL, in case we have a wrong UUID.

Remaining tasks

User interface changes

API changes

Beta phase evaluation

Reference: https://www.drupal.org/core/beta-changes
Issue category Bug because the behavior does not match the interface documentation.
Issue priority Normal.
Prioritized changes The goal of this issue is to clarify the entity API, and to expand testing to make sure implementations meet that expectation.
Disruption No disruption.
CommentFileSizeAuthor
#104 2403271-104.patch15.25 KBsnehalgaikwad
#100 2403271-100.patch15.24 KBmanav
#98 2403271-98.patch15.13 KBnikitagupta
#95 interdiff_89-95.txt2.15 KBprabha1997
#95 2403271-95.patch15.21 KBprabha1997
#89 interdiff_82-89.txt15.47 KBneelam_wadhwani
#89 2403271-89.patch15.47 KBneelam_wadhwani
#82 2403271_82.patch15.41 KBvsujeetkumar
#71 2403271_71.patch16.32 KBmile23
#70 2403271_70.patch16.53 KBmile23
#69 interdiff.txt4.91 KBmile23
#69 2403271_69.patch16.54 KBmile23
#67 interdiff.txt16.67 KBmile23
#67 2403271_67.patch16.12 KBmile23
#63 interdiff.txt6.73 KBmile23
#63 2403271_63.patch11.8 KBmile23
#58 interdiff.txt3.48 KBmile23
#58 2403271_58.patch9.52 KBmile23
#55 interdiff_45.txt9.02 KBmile23
#55 2403271_55.patch9.25 KBmile23
#51 2403271_51_poc.patch3.27 KBmile23
#45 interdiff-44-45.txt792 byteserik.erskine
#45 2403271-45.patch3.25 KBerik.erskine
#44 2403271-44.patch2.48 KBerik.erskine
#39 2403271-39.patch2.48 KBrpayanm
#39 2403271-interdiff.txt596 bytesrpayanm
#37 2403271-37.patch2.14 KBrpayanm
#30 interdiff-26-30.txt1.5 KBprateekMehta
#30 interdiff-28-30.txt3.14 KBprateekMehta
#30 return_false_on-2403271-30.patch2.43 KBprateekMehta
#28 interdiff-2403271-26-28.txt2.04 KBanksy
#28 return_false_on-2403271-28.patch1.21 KBanksy
#26 2403271-return-null-if-invalid-uuid-26.patch1.21 KBprateekMehta
#23 2403271-return-null-if-invalid-uuid-23.patch1.22 KBprateekMehta
#22 2403271-return-null-if-invalid-uuid-21.patch1.22 KBprateekMehta
#20 2403271-return-null-if-invalid-uuid-20.patch2.06 KBprateekMehta
#18 return-on-entity-manager-2403271-18.patch993 bytesneelam.chaudhary
#16 revised-entity-manager-2403271-16.patch1.93 KBzealfire
#11 return-on-EntityManager-2403271-11.patch920 byteszealfire
#8 2403271-7.patch509 bytesrpayanm

Comments

dawehner’s picture

Issue tags: +Novice

.

damiankloip’s picture

Nice, sounds sensible.

berdir’s picture

why FALSE and not NULL? Or should it throw an exception? Node::load(NULL) btw also gives you errors.

rpayanm’s picture

Title: return FALSE on EntityManager::loadByUuid with invalid UUID / non existing one » return FALSE on EntityManager::loadEntityByUuid with invalid UUID / non existing one
Issue summary: View changes
dawehner’s picture

It should mimic the behaviour of NodeStorageController(non-existing-ID) afaik

berdir’s picture

Oh, I see, loadEntityByUuid() is documented as return FALSE. That's inconsistent with load() in the first place :)

Also, it is consistent with load($non_existing_id), that also returns NULL.

The only problem is when you pass in NULL. That results in errors, at least with #2358079: Require a specific placeholder format in db_query() in order to trigger argument expansion, and require explicit 'IN' parameter for conditions. Which is also consistent with Node::load(NULL), that also gives you errors :p

dawehner’s picture

Ah right, I'm totally fine with NULL.

It would be great to not error in the case of NULL though. This might happen here and there, won't it?

rpayanm’s picture

Status: Active » Needs review
StatusFileSize
new509 bytes

Please review.

pwolanin’s picture

Status: Needs review » Needs work

Should check if the UUID is empty before even trying to load it.

dawehner’s picture

Well, we should use Uuid::isValid()

zealfire’s picture

Status: Needs work » Needs review
StatusFileSize
new920 bytes

Please review.Thanks.

dawehner’s picture

+++ b/core/lib/Drupal/Core/Entity/EntityManager.php
@@ -958,6 +959,10 @@ public function loadEntityByUuid($entity_type_id, $uuid) {
+    if(! Uuid::isValid($uuid)){

Now that we return NULL we should update the documentation as well. Note: you need some more spaces to get the right codestyle done :)

Status: Needs review » Needs work

The last submitted patch, 11: return-on-EntityManager-2403271-11.patch, failed testing.

berdir’s picture

+++ b/core/lib/Drupal/Core/Entity/EntityManager.php
@@ -958,6 +959,10 @@ public function loadEntityByUuid($entity_type_id, $uuid) {
+    if(! Uuid::isValid($uuid)){

should be "if (!Uuid::" instead.

Also, a comment would be useful, something like "Immediately return NULL for empty or invalid UUID's".

Also, right now, the method is documented as return FALSE. We either need to change that to return NULL (and update the reset() to return NULL if there are no entities, as that returns FALSE) or return FALSE as well...

And there is also the option to throw an exception instead. Because calling it like this is a bug and often hints at a bigger problem in the calling code IMHO. So far, I've only seen it when doing a load(NULL), then you get the famous message about array_flip() only working for scalars. And that is usually incorrect code in the caller.

Not sure what, thoughts?

Whatever we decide, I think we should update load/loadMultiple() in a follow-up to behave the same way..

The same test fails were in #2358079: Require a specific placeholder format in db_query() in order to trigger argument expansion, and require explicit 'IN' parameter for conditions, see the changes to EntityReferenceItemNormalizer. Not sure if we should let it fail like that or check for it not being a string instead (Which would have hidden this clearly wrong code, @see exception argument :))

zealfire’s picture

Assigned: Unassigned » zealfire

@Berdir,so do you think will it be a nice idea to make patch for this issue by returning NULL and commenting it.Then later as a follow up task make another issue where documentation related stuff can be deal with.
thanks

zealfire’s picture

Status: Needs work » Needs review
StatusFileSize
new1.93 KB

I have used both empty() and is_string() as discussed with dawehner and pwolanin on irc earlier.Also i have made some changes in the document.Not sure if i am doing this in correct manner so give your opinions.Please review.
Thanks.

Status: Needs review » Needs work

The last submitted patch, 16: revised-entity-manager-2403271-16.patch, failed testing.

neelam.chaudhary’s picture

Status: Needs work » Needs review
Issue tags: +SprintWeekend2015
StatusFileSize
new993 bytes

Returning NULL if UUID is either empty or invalid. And Adding a comment for it as discussed above.
Please review.

Status: Needs review » Needs work

The last submitted patch, 18: return-on-entity-manager-2403271-18.patch, failed testing.

prateekMehta’s picture

Status: Needs work » Needs review
StatusFileSize
new2.06 KB

Checking if the uuid is not invalid or empty, and returning null as mentioned in the /core/lib/Drupal/Core/Entity/EntityManagerInterface.php

Status: Needs review » Needs work

The last submitted patch, 20: 2403271-return-null-if-invalid-uuid-20.patch, failed testing.

prateekMehta’s picture

wrong patch in previous comment. reposting.

prateekMehta’s picture

Status: Needs work » Needs review
StatusFileSize
new1.22 KB
prateekMehta’s picture

Assigned: zealfire » prateekMehta

Status: Needs review » Needs work

The last submitted patch, 23: 2403271-return-null-if-invalid-uuid-23.patch, failed testing.

prateekMehta’s picture

Status: Needs work » Needs review
StatusFileSize
new1.21 KB
berdir’s picture

Status: Needs review » Needs work
Issue tags: +Needs tests
+++ b/core/lib/Drupal/Core/Entity/EntityManager.php
@@ -976,6 +981,10 @@ public function loadEntityByUuid($entity_type_id, $uuid) {
+    if (!reset($entities)) {
+      return NULL;
+    }

if (empty($entities) is a bit easier.

Some unit tests would be nice here, looks like we actually don't have any in EntityManagerTest. If you know a bit about unit tests and mocking, then it shouldn't be too hard. Just call that method with NULL or an invalid UUID and make sure that the stuff below is never called and it returns NULL.

anksy’s picture

Status: Needs work » Needs review
StatusFileSize
new1.21 KB
new2.04 KB

Here are the tests for the patch. This is my first attempt at writing tests for drupal so please give feedback on the tests.

Status: Needs review » Needs work

The last submitted patch, 28: return_false_on-2403271-28.patch, failed testing.

prateekMehta’s picture

StatusFileSize
new2.43 KB
new3.14 KB
new1.5 KB

Added the tests to my original patch from #26. Also used empty() instead of !reset().

Adding new patch and its interdiff with the previous two patches as the patch in #28 just had the tests and not the actual changes.

prateekMehta’s picture

Status: Needs work » Needs review

The last submitted patch, 22: 2403271-return-null-if-invalid-uuid-21.patch, failed testing.

anksy’s picture

@prateekMehta Now I know why my patch failed testing. I had forgotten to add the actual changes. Thanks for correcting it.

prateekMehta’s picture

@anksy, actually the way you were using getMock was also a little more complicated. I tried to use your test on the original changes it, didn't work out. check interdiff-28-30.txt , you will know.

anksy’s picture

Status: Needs review » Reviewed & tested by the community

@prateekMehta Thanks for pointing out the error. I tested the patch and it works fine.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 30: return_false_on-2403271-30.patch, failed testing.

rpayanm’s picture

Status: Needs work » Needs review
StatusFileSize
new2.14 KB

Rerolled.

Status: Needs review » Needs work

The last submitted patch, 37: 2403271-37.patch, failed testing.

rpayanm’s picture

Status: Needs work » Needs review
StatusFileSize
new596 bytes
new2.48 KB

Off course.

yesct’s picture

Status: Needs review » Needs work

I do not think the concerns from @Berdir in #14 have been addressed.

especially

1.

Also, right now, the method is documented as return FALSE. We either need to change that to return NULL (and update the reset() to return NULL if there are no entities, as that returns FALSE) or return FALSE as well...

2.

Whatever we decide, I think we should update load/loadMultiple() in a follow-up to behave the same way..

This follow-up should be created and made "related" to this issue.

yesct’s picture

  1. +++ b/core/lib/Drupal/Core/Entity/EntityManager.php
    @@ -1006,6 +1007,10 @@ protected function getDisplayModeOptions($display_type, $entity_type_id, $includ
    +    // Return NULL for invalid UUID's.
    +    if (empty($uuid) || !Uuid::isValid($uuid)) {
    +      return NULL;
    +    }
    
    +++ b/core/tests/Drupal/Tests/Core/Entity/EntityManagerTest.php
    @@ -1545,6 +1545,37 @@ protected function getTestHandlerClass() {
    +  public function providerTestLoadEntityByUuid() {
    

    I think the provider tests these two cases.

  2. +++ b/core/lib/Drupal/Core/Entity/EntityManager.php
    @@ -1014,6 +1019,10 @@ public function loadEntityByUuid($entity_type_id, $uuid) {
    +    if (empty($entities)) {
    +      return NULL;
    +    }
    

    but I dont see how the empty entities case is tested.

    is it?

disasm’s picture

Issue tags: +Needs documentation
disasm’s picture

Issue tags: +Needs followup
erik.erskine’s picture

StatusFileSize
new2.48 KB

#39 looks good to me. It catches instances in tests where loadEntityByUuid is being called with malformed UUIDs. These currently rely on those bogus UUIDs not actually existing - see #2491989: [PP-1] Use the 'uuid' database schema type (with native PostgreSQL implementation) for UUID fields.

Rerolled, along with a small typo fix.

erik.erskine’s picture

Status: Needs work » Needs review
StatusFileSize
new3.25 KB
new792 bytes

With regards to the comments in #14 and #41:

  • do you mean the EntityStorageInterface::load method? If so that does return NULL.
  • added a new test to EntityUUIDTest to cover testing for NULL with a non-existent, but valid, UUID.
sorressean’s picture

I did not actually realize that this was done (first patch review), so I wrote the same thing before finding the last patch--looks good!

sorressean’s picture

Status: Needs review » Reviewed & tested by the community
alexpott’s picture

Status: Reviewed & tested by the community » Needs review
+++ b/core/lib/Drupal/Core/Entity/EntityManager.php
@@ -1117,6 +1118,10 @@ protected function getDisplayModeOptions($display_type, $entity_type_id, $includ
+    // Return NULL for invalid UUIDs.
+    if (empty($uuid) || !Uuid::isValid($uuid)) {
+      return NULL;
+    }

This looks super defensive to me. Are we sure we want to do this? Perhaps when we have assertions in core we can use that instead.

\Drupal::entityManager()->loadEntityByUuid('node',NULL);
exception 'Drupal\Core\Database\InvalidQueryException' with message 'Query condition 'node.uuid IN ()' cannot be empty.' in /Volumes/devdisk/dev/sites/drupal8alt.dev/core/lib/Drupal/Core/Database/Query/Condition.php:76

This is what happens atm - the exception produced looks fine to me. If you provide an invalid uuid it works as you expect.

@Berdir what do you think?

bzrudi71’s picture

mile23’s picture

Title: return FALSE on EntityManager::loadEntityByUuid with invalid UUID / non existing one » Return NULL on EntityManager::loadEntityByUuid with invalid UUID / non existing one
Issue summary: View changes
Issue tags: -Needs tests

Added beta evaluation.

Patch still applies.

mile23’s picture

StatusFileSize
new3.27 KB

Seems to me that if we're throwing an EntityStorageException somewhere other than an EntityStorgeInterface class, we might be doing it wrong.

So let's move the property-exists check to EntityStorageInterface/Base where all code can benefit, and then just handle the empty-or-bad-uuid check within EntityManager.

In this PoC we rely on the query and database layers to throw exceptions validating the UUID and checking whether it's NULL. That means there isn't any validation of whether the given UUID is a valid one. It seems that should be baked in somewhere, but I'm not quite sure where.

This patch does not have tests. It passes a PHPUnit test run. Let's see what the testbot thinks.

Discuss. :-)

mile23’s picture

Assigned: prateekMehta » Unassigned

Status: Needs review » Needs work

The last submitted patch, 51: 2403271_51_poc.patch, failed testing.

mile23’s picture

Assigned: Unassigned » mile23

OK, yah, never mind. I forgot that we're not allowed to encapsulate things that should be encapsulated in one place. Our entity abstraction only lets us map a few special-case keys, and apparently can't just give us a list of properties that are allowable.

As an object lesson, note that a) All unit tests pass even though none of the functional ones do, and b) Nowhere in the process of writing that patch did the docs warn me I was making an error. I might be a fool, but Drupal is still an oral tradition.

Moving on...

mile23’s picture

Status: Needs work » Needs review
StatusFileSize
new9.25 KB
new9.02 KB

Interdiff from #45. Let's all ignore #51. :-)

Removed the UUID validity checks, though there should be some way to perform that check within the query, either at loadByProperties() or some other point, since there's no point sending it off to the database to fail.

Moved loadByProperties() into a try/catch which then returns NULL for the NULL-UUID exception. The query builder's Condition class is the *only* place InvalidQueryException is thrown, specifically for empty conditions in queries. It's kind of strange, actually.

Added a bunch of unit tests.

mile23’s picture

Assigned: mile23 » Unassigned
daffie’s picture

Status: Needs review » Needs work

Looks good to me. Just three observations:

+++ b/core/tests/Drupal/Tests/Core/Entity/EntityManagerTest.php
@@ -1818,6 +1819,192 @@ protected function getTestHandlerClass() {
+  public function providerTestLoadEntityByUuid() {
...
+   * @dataProvider providerTestLoadEntityByUuid
...
+   * @dataProvider providerTestLoadEntityByUuid
...
+   * @dataProvider providerTestLoadEntityByUuid

Nitpick: Can we rename the dataprovider to providerLoadEntityByUuid.

+++ b/core/tests/Drupal/Tests/Core/Entity/EntityManagerTest.php
@@ -1818,6 +1819,192 @@ protected function getTestHandlerClass() {
+  public function testloadEntityByUuidNullQuery() {

Why no dataprovider for this test. You have one for the other tests (return ['apple', NULL];).

+++ b/core/tests/Drupal/Tests/Core/Entity/EntityManagerTest.php
@@ -1818,6 +1819,192 @@ protected function getTestHandlerClass() {
+    $manager->loadEntityByUuid($entity_type_id, $uuid);

Can we add a comment that there is no assertion because we are expection an exception.

mile23’s picture

Status: Needs work » Needs review
StatusFileSize
new9.52 KB
new3.48 KB

Thanks.

#57.1: Done.

#57.2: Like it says in the inline comments, we throw an exception in all cases, so there's no point in having a data provider. I'll expand the docs.

#57.3: The @expectedException annotation *is* the comment and also the assertion. But I'll expand on it.

Still need a follow-up, which might already exist, to do some kind of validation of UUIDs, or general validation before we hit the database.

mile23’s picture

daffie’s picture

Status: Needs review » Reviewed & tested by the community

Now it looks good to me.

Thanks for fixing my issues Mile23.

damiankloip’s picture

  1. +++ b/core/lib/Drupal/Core/Entity/EntityManager.php
    @@ -1126,8 +1128,15 @@ public function loadEntityByUuid($entity_type_id, $uuid) {
    +    } catch (InvalidQueryException $ex) {
    

    Won't you only get this exception in the condition value is empty and it's an array? Can't we just make sure the uuid is a string before hand?

  2. +++ b/core/lib/Drupal/Core/Entity/EntityManager.php
    @@ -1126,8 +1128,15 @@ public function loadEntityByUuid($entity_type_id, $uuid) {
    +    if (empty($entities)) {
    

    Won't reset($entities) do the same here anyway?

damiankloip’s picture

Status: Reviewed & tested by the community » Needs work
mile23’s picture

Status: Needs work » Needs review
StatusFileSize
new11.8 KB
new6.73 KB

#61.2: reset() on an empty array returns FALSE. We want NULL.

I misread #48.

Let's try this again...

In Drupal, UUIDs are actually arbitrary strings, and some discussion is underway as to whether we should validate UUIDs: #1805576: Add a 'uuid' database schema type (Personally, I say we should add a 'uuid' column type to the Drupal schema API and add validation constraints.)

This means it's up in the air as to whether we should validate UUIDs in loadByUuid(). Let's do it, with a note that we may change policy later.

In the case that we validate UUIDs in loadByUuid(), should we throw an exception or just return NULL? Let's throw InvalidQueryException, as per #48.

We still throw EntityStorageException if the entity type doesn't support a UUID key, and we don't catch any other exceptions, so that things explode usefully.

In this patch, EntityUUIDTest does a functional check of the behavior for passing in a non-existent UUID to an entity type that supports UUIDs.

EntityManagerTest verifies the following:

  • That we return an entity for the UUID when it's present.
  • That we return NULL when there is no entity for the type/uuid query.
  • That we throw a EntityStorageException if the entity type does not support UUID keys.
  • That we throw an InvalidQueryException for malformed UUIDs.

Patch passes PHPUnit run and Entity namespace tests. Let's see what the testbot thinks.

Status: Needs review » Needs work

The last submitted patch, 63: 2403271_63.patch, failed testing.

damiankloip’s picture

I like the new version of loadEntityByUuid much better, thanks @Mile23!

mile23’s picture

Yah but too bad all the tests are built on non-conforming test UUIDs.

I just searched entity_test module for 'uuid' and it doesn't tell me where the UUIDs are generated or defined.

Drupal.

mile23’s picture

StatusFileSize
new16.12 KB
new16.67 KB

So it turns out the tests tell me that ConfigManager needed some love, as well as adding try/catch to loadEntityByConfigTarget().

Note that I had to break some tests, because I don't have the time to fix views_ui or editor modules. Added @todo to those. These should either be fixed here or in follow-ups.

mile23’s picture

Status: Needs work » Needs review
mile23’s picture

StatusFileSize
new16.54 KB
new4.91 KB

And now fixing the modules.

Summary in #63.

mile23’s picture

Issue tags: -Novice
StatusFileSize
new16.53 KB

Needed reroll.

mile23’s picture

StatusFileSize
new16.32 KB

Another re-roll.

Status: Needs review » Needs work

The last submitted patch, 71: 2403271_71.patch, failed testing.

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

avpaderno’s picture

Version: 8.6.x-dev » 8.9.x-dev
mile23’s picture

Ooo. Ancient patch from before EntityManager was split into 11 classes. https://www.drupal.org/node/2549139

avpaderno’s picture

vsujeetkumar’s picture

Status: Needs work » Needs review
StatusFileSize
new15.41 KB

Re-roll patch created, Please review.

avpaderno’s picture

Title: Return NULL on EntityManager::loadEntityByUuid with invalid UUID / non existing one » Return NULL on EntityRepository::loadEntityByUuid() when the UUID is invalid or it doesn't exist
Issue summary: View changes
Issue tags: -Needs issue summary update, -Needs reroll
avpaderno’s picture

(Wrong comment: Please ignore it.)

Status: Needs review » Needs work

The last submitted patch, 82: 2403271_82.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

avpaderno’s picture

Actually, print_r() is used for when $uuid doesn't contain a string, but I don't see how an exception message like Invalid UUID: (output when $uuid contains FALSE or NULL) or Invalid UUID: Array() (output when $uuid contains an empty array) could be helpful. The message doesn't allow to notice the UUID is invalid because it's an empty string, FALSE, 1, or TRUE. (print_r(TRUE, TRUE) outputs 1, in the same way print_r(1, TRUE) does.)

It would be probably more helpful for the exception message to say Invalid UUID passed to EntityRepository::loadEntityByUuid().

avpaderno’s picture

Alternatively, the code throwing the exception could be the following one.

if (empty($uuid) || !is_string($uuid) || !Uuid::isValid($uuid)) {
  throw new InvalidQueryException('Invalid value passed for $uuid: ' . var_dump($uuid));
}

In this way, the exception message would make clear what exactly the value passed as argument is, as var_dump(TRUE), var_dump(1), var_dump(FALSE), var_dump(""), and var_dump(NULL) output different values.

neelam_wadhwani’s picture

Assigned: Unassigned » neelam_wadhwani
Issue tags: +VbContribution2020
neelam_wadhwani’s picture

Assigned: neelam_wadhwani » Unassigned
Status: Needs work » Needs review
StatusFileSize
new15.47 KB
new15.47 KB

Hello @kiamlaluno,
Updated the message.

Kindly review patch.

Status: Needs review » Needs work

The last submitted patch, 89: 2403271-89.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

avpaderno’s picture

The issue summary still needs to be updated, as it speaks of returning NULL in case of bad UUID, while the patch is raising an exception. (I just changed the name of the method to alter, which changed because of splitting an entity class in multiple classes.)

See also comment #48.

+++ b/core/lib/Drupal/Core/Entity/EntityManager.php
@@ -1117,6 +1118,10 @@ protected function getDisplayModeOptions($display_type, $entity_type_id, $includ
+    // Return NULL for invalid UUIDs.
+    if (empty($uuid) || !Uuid::isValid($uuid)) {
+      return NULL;
+    }

This looks super defensive to me. Are we sure we want to do this? Perhaps when we have assertions in core we can use that instead.

\Drupal::entityManager()->loadEntityByUuid('node',NULL);
exception 'Drupal\Core\Database\InvalidQueryException' with message 'Query condition 'node.uuid IN ()' cannot be empty.' in /Volumes/devdisk/dev/sites/drupal8alt.dev/core/lib/Drupal/Core/Database/Query/Condition.php:76

This is what happens atm - the exception produced looks fine to me. If you provide an invalid uuid it works as you expect.

@Berdir what do you think?

avpaderno’s picture

The test code seems wrong.

+    $entity_type = $this->getMockBuilder('Drupal\Core\Entity\EntityTypeInterface')
+      ->setMethods(['getKey'])
+      ->getMockForAbstractClass();
+    $entity_type->expects($this->once())
+      ->method('getKey')
+      ->with('uuid')
+      ->willReturn('uuid');

getKey('uuid') should return $uuid, not a literal string.

+    $entity_storage->expects($this->once())
+      ->method('loadByProperties')
+      ->willReturn(['this entity contains uuid: ' . $uuid]);

loadByProperties() needs to return entities, not strings.

jungle’s picture

Coding standards must pass as well, https://www.drupal.org/pift-ci-job/1611745

9 coding standards messages
/var/lib/drupalci/workspace/jenkins-drupal_patches-37230/source/core/lib/Drupal/Core/Entity/EntityRepository.php
line 71 Line indented incorrectly; expected 6 spaces, found 7
/var/lib/drupalci/workspace/jenkins-drupal_patches-37230/source/core/modules/editor/editor.module
461 Expected 1 space after IF keyword; 0 found
/var/lib/drupalci/workspace/jenkins-drupal_patches-37230/source/core/tests/Drupal/Tests/Core/Entity/EntityRepositoryTest.php
14 Unused use statement
107 Short array syntax must be used to define arrays
108 Short array syntax must be used to define arrays
150 Line indented incorrectly; expected 6 spaces, found 5
150 Object operator not indented correctly; expected 6 spaces but found 5
213 @expectedException tags should not be used, use $this->setExpectedException() or $this->expectException() instead
270 @expectedException tags should not be used, use $this->setExpectedException() or $this->expectException() instead

prabha1997’s picture

Assigned: Unassigned » prabha1997
prabha1997’s picture

Assigned: prabha1997 » Unassigned
Status: Needs work » Needs review
StatusFileSize
new15.21 KB
new2.15 KB

Here i upload new patch with no drupal coding standards issues. Kindly review patch

Status: Needs review » Needs work

The last submitted patch, 95: 2403271-95.patch, failed testing. View results

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

nikitagupta’s picture

Status: Needs work » Needs review
StatusFileSize
new15.13 KB

Reroll the patch #95.

Status: Needs review » Needs work

The last submitted patch, 98: 2403271-98.patch, failed testing. View results

manav’s picture

Assigned: Unassigned » manav
StatusFileSize
new15.24 KB

I have applied the last submitted patch #98 but it failed.
So I have tested the code and found that the culprit is DeprecatedServicePropertyTrait in EditorFileReference.php file

manav’s picture

manav’s picture

Assigned: manav » Unassigned
andypost’s picture

Issue tags: +Needs reroll, +blocker
snehalgaikwad’s picture

Status: Needs work » Needs review
Issue tags: -Needs reroll
StatusFileSize
new15.25 KB

Re-rolling the patch for 9.1.x.

Status: Needs review » Needs work

The last submitted patch, 104: 2403271-104.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

meena.bisht’s picture

Assigned: Unassigned » meena.bisht
meena.bisht’s picture

Assigned: meena.bisht » Unassigned
anmolgoyal74’s picture

Assigned: Unassigned » anmolgoyal74
Status: Needs work » Needs review
StatusFileSize
new15.28 KB
new726 bytes

Removed dump statement.
Working on the other issues.

Status: Needs review » Needs work

The last submitted patch, 108: 2403271-108.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

anmolgoyal74’s picture

Status: Needs work » Needs review
StatusFileSize
new14.06 KB
new2.66 KB

Status: Needs review » Needs work

The last submitted patch, 110: 2403271-110.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

meena.bisht’s picture

Assigned: anmolgoyal74 » Unassigned
avpaderno’s picture

avpaderno’s picture

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.