Follow-up to #2403271: Return NULL on EntityRepository::loadEntityByUuid() when the UUID is invalid or it doesn't exist

Problem/Motivation

There is no unit test to verify the behavior of EntityStorageBase::load() or EntityStorageBase::loadMultiple().

Proposed resolution

Write some tests.

Remaining tasks

User interface changes

API changes

Beta phase evaluation

Reference: https://www.drupal.org/core/beta-changes
Issue category Task because we're writing tests to verify behavior that isn't wrong.
Issue priority Normal because nothing is broken.
Unfrozen changes Unfrozen because we're adding automated unit tests.

Comments

joshi.rohit100’s picture

I think its Entity::load()/loadMultiple() ?

anksy’s picture

Title: return FALSE on EntityManager::load/loadMultiple() with invalid UUID / non existing one » return FALSE on EntityManager::load()/loadMultiple() with invalid UUID / non existing one
Issue summary: View changes
shivanshuag’s picture

The loadMuliple case a bit tricky. It takes an array of UUID's as an argument. Should it return NULL even if a single UUID is invalid or NULL in the return array for those UUID's that are invalid/non-existing?

In the case of invalid id's, EntityStorage::loadMultiple(), which is called by Entity::loadMultiple($ids), removes them from the result array, so I think there shouldn't be any need to make changes in loadMultiple() function.

Entity::load($id) makes use of EntityStorage::load() function, which calls EntityStorage::loadMultiple() with a single id in an array as the parameter. EntityStorage::load() returns null if EntityStorage::loadMultiple() returns an empty array, so I think there is no need to make changes in load() function too.

berdir’s picture

Title: return FALSE on EntityManager::load()/loadMultiple() with invalid UUID / non existing one » return FALSE on EntityManager::load()/loadMultiple() with invalid ID / non existing one

Id, not UUID.

berdir’s picture

Title: return FALSE on EntityManager::load()/loadMultiple() with invalid ID / non existing one » return NULL on EntityManager::load()/loadMultiple() with invalid ID / non existing one

Also, we should return NULL

m0zzy’s picture

Hi all,

I'm in DrupalDevDays in Montpellier today (mentoring session)
And I'll try to fix this issue.

bhoquy’s picture

Issue tags: +drupaldevdays

Hi, I'm in drupal dev days and i'll try to fix this issue.

andrewsuth’s picture

In the absence of m0zzy in the mentoring room, I investigated this issue and will post the results soon.

andrewsuth’s picture

I am unable to reproduce this issue.

Some quick debugging seems to show that there is no issue with load and loadMultiple while using their calling functions (entity_load and entity_load_multiple) which returns their expected results:

  • NULL for single entities
  • An empty array for multiple entities.

Loading a single entity

$id = entity_load('node', 'foo');
$id2 = entity_load('node', 35453);
$id3 = entity_load('node', NULL);
var_dump($id);
var_dump($id2);
var_dump($id3);

Results in:

null
null
null

Loading multiple entities

$multiple = entity_load_multiple('node', array('foo'));
$multiple2 = entity_load_multiple('node', array());
$multiple3 = entity_load_multiple('node', array(234234));
$multiple4 = entity_load_multiple('node', array(NULL));

var_dump($multiple);
var_dump($multiple2);
var_dump($multiple3);
var_dump($multiple4);

Results in:

array (size=0)
  empty
array (size=0)
  empty
array (size=0)
  empty
array (size=0)
  empty
manningpete’s picture

Status: Active » Postponed (maintainer needs more info)
Issue tags: -Novice

Postponed for more information and removed the novice tag.

Why: Per #9 issue couldn't be recreated; need more information. Removed novice tag since issue is not actionable by novice.

Novice tag documentation: https://www.drupal.org/core-mentoring/novice-tasks

andrewsuth’s picture

Can you provide inputs or unit tests which replicate this issue?

mile23’s picture

Status: Postponed (maintainer needs more info) » Needs work

@andrewsuth If it's at all possible, could you turn the experimentation in #9 into a runnable test?

That way we can be sure the behavior is correct.

Thanks.

mile23’s picture

Title: return NULL on EntityManager::load()/loadMultiple() with invalid ID / non existing one » return NULL on EntityStorageBase::load(), empty array on loadMultiple() with invalid ID / non existing one
Issue summary: View changes
Status: Needs work » Needs review
StatusFileSize
new5.2 KB

Updated issue summary to reflect that EntityManager::load()/loadMultiple() doesn't exist any more, and is on the storage object.

Rescoping a bit to concentrate on EntityStorageBase.

Since EntityStorageInterface::load() accepts an ID of mixed type, there's no such thing as an invalid parameter to it, or to loadMultiple(), since it accepts an array of the same type. Whether a given ID is of an illegal type would be determined by doLoadMultiple() in the concrete storage class, rather than EntityStorageBase, which is abstract. In such a case, doLoadMultiple() should simply ignore the illegal ID.

This patch contains a test class to cover EntityStorageBase.

It @covers ::load and ::loadMultiple.

For load():

  • Return NULL when there are no entities for the given ID.
  • Return the entity when its ID exists.

For loadMultiple():

  • Return an empty array if there are no results for the given IDs.
  • Return an array containing entities matching the given IDs.
  • Return an array containing matching entities for queries with partial matches for the given IDs.

Note that we're only testing the behavior of loadMultiple() for entity types without static caching.

mile23’s picture

Issue tags: +rc eligible

Still applies, still passes.

mile23’s picture

Title: return NULL on EntityStorageBase::load(), empty array on loadMultiple() with invalid ID / non existing one » Test unit behavior of EntityStorageBase::load(), loadMultiple() with invalid ID, UUID

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.

mile23’s picture

StatusFileSize
new5.28 KB
new2.21 KB

Removed @file per new CS, added keys to data provider arrays for readability.

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.

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

Drupal 8.6.x will not receive any further development aside from security fixes. Bug reports should be targeted against the 8.8.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.9.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.

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

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should 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.

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

Drupal 8 is end-of-life as of November 17, 2021. There will not be further changes made to Drupal 8. Bugfixes are now made to the 9.3.x and higher branches only. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.2.x-dev » 9.3.x-dev
quietone’s picture

Version: 9.3.x-dev » 10.0.x-dev
Category: Bug report » Task
StatusFileSize
new1.01 KB
new5.36 KB

Changing to a task,

I have also rerolled the patch for 10.0.x

lendude’s picture

Status: Needs review » Reviewed & tested by the community

Discussed briefly with @quiteone in #bugsmash about the added value here and it was pointed out that this still checks for logic errors even if the expected value is basically the same as whatever we pass as the return value for doLoadMultiple.

So, more test coverage ++

alexpott’s picture

Version: 10.0.x-dev » 9.4.x-dev
Status: Reviewed & tested by the community » Fixed

Committed and pushed 50ddf37fda to 10.1.x and 9909596e5e to 10.0.x and 112de03974 to 9.5.x and 38e9b318a3 to 9.4.x. Thanks!

Backported to 9.4.x to keep test aligned. I ran the test on 9.4.x / php 7.3 locally.

  • alexpott committed 50ddf37 on 10.1.x
    Issue #2464041 by Mile23, quietone, andrewsuth: Test unit behavior of...

  • alexpott committed 9909596 on 10.0.x
    Issue #2464041 by Mile23, quietone, andrewsuth: Test unit behavior of...

  • alexpott committed 112de03 on 9.5.x
    Issue #2464041 by Mile23, quietone, andrewsuth: Test unit behavior of...

  • alexpott committed 38e9b31 on 9.4.x
    Issue #2464041 by Mile23, quietone, andrewsuth: Test unit behavior of...

Status: Fixed » Closed (fixed)

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