Entity cache invalidation issue in tests

Created on 20 November 2018, about 6 years ago
Updated 17 April 2023, over 1 year ago

I bumped into a weird entity cache invalidation issue in one of my module's test.
After my test updates a user it seems the user storage does not get invalidated automatically - only in tests, I could not reproduce this problem by executing the same actions in the UI. I think this problem is a side-effect of \Drupal::service() vs. $this->container usage in tests.
When I saw my test had started to fail then I automatically added $this->container->get('entity_type.manager')->getStorage('user')->resetCache(); call to my test to ensure user cache is empty before my test loads the user with user_load_by_mail(). Even after this forced cache clear my test still failed so I started to go deeper in code and then I realized the memory cache inside $this->container->get('entity_type.manager')->getStorage('user) is not the same as in \Drupal::service('entity_type.manager')->getStorage('user') . So basically I cleared an already empty memory cache and user_load_by_email() used a completely different storage/memory cache which has not been cleared yet.
I think when a user entity gets updated Drupal automatically invalidates the related cache entries, but what if in tests it runs the invalidation in an incorrect user storage? Or I do not know what else could explain why I have to call \Drupal::service('entity_type.manager')->getStorage('user')->resetCache(); manually.

Fun fact, the test that required this fix runs the user synchronization in the UI. There is an another test which runs the synchronization with Drush and that test does not require this fix.

🐛 Bug report
Status

Closed: duplicate

Version

10.0

Component
Entity 

Last updated 1 day ago

Created by

🇭🇺Hungary mxr576 Hungary

Live updates comments and jobs are added and updated live.
  • Needs issue summary update

    Issue summaries save everyone time if they are kept up-to-date. See Update issue summary task instructions.

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024