From 65873adedc41b530089b1bd26b3f53c7292aaeab Mon Sep 17 00:00:00 2001
From: catch <catch@35733.no-reply.drupal.org>
Date: Thu, 1 Apr 2021 18:46:09 +0100
Subject: [PATCH] Issue #3204163 by jonathanshaw: EntityQuery accessCheck:
 tests should not check access unless relevant

---
 .../src/Functional/AggregatorCronTest.php     |  5 +-
 .../src/Functional/AggregatorTestBase.php     | 23 ++++++--
 .../tests/src/Functional/DeleteFeedTest.php   |  7 ++-
 .../tests/src/Functional/FeedParserTest.php   | 10 +++-
 .../tests/src/Functional/ImportOpmlTest.php   |  4 +-
 .../src/Functional/UpdateFeedItemTest.php     | 10 +++-
 .../src/Functional/BlockContentSaveTest.php   |  5 +-
 .../tests/src/Functional/PageEditTest.php     |  5 +-
 ...ockContentEntityReferenceSelectionTest.php | 12 +++-
 .../tests/src/Functional/CommentAdminTest.php |  1 +
 .../src/Functional/CommentLanguageTest.php    |  1 +
 .../Kernel/ContentModerationSyncingTest.php   |  1 +
 .../src/Kernel/DefaultRevisionStateTest.php   |  2 +-
 .../EntityReferenceAutoCreateTest.php         |  5 +-
 .../field/tests/src/Kernel/BulkDeleteTest.php |  4 ++
 .../src/Functional/FileFieldTestBase.php      |  5 +-
 .../src/Functional/SaveUploadFormTest.php     | 15 ++++-
 .../tests/src/Functional/SaveUploadTest.php   | 25 ++++++--
 .../FileManagedFileElementTest.php            |  5 +-
 .../file/tests/src/Kernel/SaveTest.php        |  1 +
 .../forum/tests/src/Functional/ForumTest.php  |  1 +
 .../src/Functional/ImageFieldTestBase.php     |  5 +-
 .../tests/src/Functional/ResourceTestBase.php |  7 ++-
 .../tests/src/Kernel/Query/FilterTest.php     |  4 +-
 .../FunctionalJavascript/InlineBlockTest.php  |  1 +
 .../InlineBlockTestBase.php                   |  6 +-
 .../src/Functional/MediaRevisionTest.php      |  1 +
 .../src/Functional/MediaUiFunctionalTest.php  |  4 +-
 .../FunctionalJavascript/MediaDisplayTest.php |  1 +
 .../MediaStandardProfileTest.php              |  5 ++
 .../tests/src/Kernel/HighWaterTest.php        |  2 +-
 .../tests/src/Kernel/TrackChangesTest.php     |  2 +-
 .../src/Functional/MigrateUpgradeTestBase.php |  2 +-
 .../NodeAccessTestAutoBubblingController.php  |  1 +
 .../src/Functional/NodeQueryAlterTest.php     |  1 +
 .../tests/src/Functional/NodeSaveTest.php     |  1 +
 .../src/Controller/CacheTestController.php    |  2 +-
 .../src/Controller/EntityTestController.php   |  3 +-
 .../Module/InstallUninstallTest.php           |  2 +-
 .../tests/src/Functional/TermParentsTest.php  |  1 +
 .../src/Functional/TermTranslationUITest.php  |  1 +
 .../src/Kernel/TaxonomyQueryAlterTest.php     |  4 +-
 .../tests/src/Kernel/TermEntityQueryTest.php  |  3 +-
 .../tests/src/Kernel/VocabularyCrudTest.php   |  2 +-
 .../user/tests/src/Kernel/UserInstallTest.php |  2 +-
 .../tests/src/Functional/DisplayPathTest.php  |  1 +
 .../src/Kernel/WorkspaceIntegrationTest.php   | 14 +++--
 .../UninstallDefaultContentTest.php           |  7 +++
 .../src/Functional/DemoUmamiProfileTest.php   |  1 +
 .../Core/Entity/ContentEntityChangedTest.php  | 14 ++---
 .../ContentEntityNonRevisionableFieldTest.php |  4 ++
 .../Entity/ContentEntityNullStorageTest.php   |  8 +--
 .../Core/Entity/EntityCrudHookTest.php        |  5 +-
 .../Entity/EntityQueryRelationshipTest.php    | 15 +++++
 .../Core/Entity/EntityQueryTest.php           | 59 ++++++++++++++++---
 .../Core/Entity/EntityTranslationTest.php     |  4 +-
 .../FieldableEntityDefinitionUpdateTest.php   |  2 +-
 .../Tests/Traits/Core/PathAliasTestTrait.php  | 12 +++-
 58 files changed, 278 insertions(+), 78 deletions(-)

diff --git a/core/modules/aggregator/tests/src/Functional/AggregatorCronTest.php b/core/modules/aggregator/tests/src/Functional/AggregatorCronTest.php
index acc6b61b5fb2..60bc2784e238 100644
--- a/core/modules/aggregator/tests/src/Functional/AggregatorCronTest.php
+++ b/core/modules/aggregator/tests/src/Functional/AggregatorCronTest.php
@@ -25,7 +25,10 @@ public function testCron() {
     // Create feed and test basic updating on cron.
     $this->createSampleNodes();
     $feed = $this->createFeed();
-    $count_query = \Drupal::entityQuery('aggregator_item')->condition('fid', $feed->id())->count();
+    $count_query = \Drupal::entityQuery('aggregator_item')
+      ->accessCheck(FALSE)
+      ->condition('fid', $feed->id())
+      ->count();
 
     $this->cronRun();
     $this->assertEqual(5, $count_query->execute());
diff --git a/core/modules/aggregator/tests/src/Functional/AggregatorTestBase.php b/core/modules/aggregator/tests/src/Functional/AggregatorTestBase.php
index bafe8d504f2b..bdd3d5cd00a5 100644
--- a/core/modules/aggregator/tests/src/Functional/AggregatorTestBase.php
+++ b/core/modules/aggregator/tests/src/Functional/AggregatorTestBase.php
@@ -80,7 +80,11 @@ public function createFeed($feed_url = NULL, array $edit = []) {
     // Verify that the creation message contains a link to a feed.
     $this->assertSession()->elementExists('xpath', '//div[@data-drupal-messages]//a[contains(@href, "aggregator/sources/")]');
 
-    $fids = \Drupal::entityQuery('aggregator_feed')->condition('title', $edit['title[0][value]'])->condition('url', $edit['url[0][value]'])->execute();
+    $fids = \Drupal::entityQuery('aggregator_feed')
+      ->accessCheck(FALSE)
+      ->condition('title', $edit['title[0][value]'])
+      ->condition('url', $edit['url[0][value]'])
+      ->execute();
     $this->assertNotEmpty($fids, 'The feed found in database.');
     return Feed::load(array_values($fids)[0]);
   }
@@ -195,7 +199,10 @@ public function updateFeedItems(FeedInterface $feed, $expected_count = NULL) {
     $this->clickLink('Update items');
 
     // Ensure we have the right number of items.
-    $item_ids = \Drupal::entityQuery('aggregator_item')->condition('fid', $feed->id())->execute();
+    $item_ids = \Drupal::entityQuery('aggregator_item')
+      ->accessCheck(FALSE)
+      ->condition('fid', $feed->id())
+      ->execute();
     $feed->items = array_values($item_ids);
 
     if ($expected_count !== NULL) {
@@ -224,7 +231,10 @@ public function deleteFeedItems(FeedInterface $feed) {
    *   Expected number of feed items.
    */
   public function updateAndDelete(FeedInterface $feed, $expected_count) {
-    $count_query = \Drupal::entityQuery('aggregator_item')->condition('fid', $feed->id())->count();
+    $count_query = \Drupal::entityQuery('aggregator_item')
+      ->accessCheck(FALSE)
+      ->condition('fid', $feed->id())
+      ->count();
     $this->updateFeedItems($feed, $expected_count);
     $count = $count_query->execute();
     $this->assertGreaterThan(0, $count);
@@ -245,7 +255,12 @@ public function updateAndDelete(FeedInterface $feed, $expected_count) {
    *   TRUE if feed is unique.
    */
   public function uniqueFeed($feed_name, $feed_url) {
-    $result = \Drupal::entityQuery('aggregator_feed')->condition('title', $feed_name)->condition('url', $feed_url)->count()->execute();
+    $result = \Drupal::entityQuery('aggregator_feed')
+      ->accessCheck(FALSE)
+      ->condition('title', $feed_name)
+      ->condition('url', $feed_url)
+      ->count()
+      ->execute();
     return (1 == $result);
   }
 
diff --git a/core/modules/aggregator/tests/src/Functional/DeleteFeedTest.php b/core/modules/aggregator/tests/src/Functional/DeleteFeedTest.php
index e31fe47cf6ea..780cdad7d89a 100644
--- a/core/modules/aggregator/tests/src/Functional/DeleteFeedTest.php
+++ b/core/modules/aggregator/tests/src/Functional/DeleteFeedTest.php
@@ -48,7 +48,12 @@ public function testDeleteFeed() {
     $this->assertSession()->statusCodeEquals(404);
 
     // Check database for feed.
-    $result = \Drupal::entityQuery('aggregator_feed')->condition('title', $feed1->label())->condition('url', $feed1->getUrl())->count()->execute();
+    $result = \Drupal::entityQuery('aggregator_feed')
+      ->accessCheck(FALSE)
+      ->condition('title', $feed1->label())
+      ->condition('url', $feed1->getUrl())
+      ->count()
+      ->execute();
     $this->assertEquals(0, $result, 'Feed not found in database');
   }
 
diff --git a/core/modules/aggregator/tests/src/Functional/FeedParserTest.php b/core/modules/aggregator/tests/src/Functional/FeedParserTest.php
index beafe070462a..644a31e0a964 100644
--- a/core/modules/aggregator/tests/src/Functional/FeedParserTest.php
+++ b/core/modules/aggregator/tests/src/Functional/FeedParserTest.php
@@ -64,7 +64,10 @@ public function testAtomSample() {
     $this->assertText('Atom-Powered Robots Run Amok');
     $this->assertSession()->linkByHrefExists('http://example.org/2003/12/13/atom03');
     $this->assertText('Some text.');
-    $item_ids = \Drupal::entityQuery('aggregator_item')->condition('link', 'http://example.org/2003/12/13/atom03')->execute();
+    $item_ids = \Drupal::entityQuery('aggregator_item')
+      ->accessCheck(FALSE)
+      ->condition('link', 'http://example.org/2003/12/13/atom03')
+      ->execute();
     $item = Item::load(array_values($item_ids)[0]);
     $this->assertEqual('urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a', $item->getGuid(), 'Atom entry id element is parsed correctly.');
 
@@ -72,7 +75,10 @@ public function testAtomSample() {
     $this->assertText('We tried to stop them, but we failed.');
     $this->assertSession()->linkByHrefExists('http://example.org/2003/12/14/atom03');
     $this->assertText('Some other text.');
-    $item_ids = \Drupal::entityQuery('aggregator_item')->condition('link', 'http://example.org/2003/12/14/atom03')->execute();
+    $item_ids = \Drupal::entityQuery('aggregator_item')
+      ->accessCheck(FALSE)
+      ->condition('link', 'http://example.org/2003/12/14/atom03')
+      ->execute();
     $item = Item::load(array_values($item_ids)[0]);
     $this->assertEqual('urn:uuid:1225c695-cfb8-4ebb-bbbb-80da344efa6a', $item->getGuid(), 'Atom entry id element is parsed correctly.');
   }
diff --git a/core/modules/aggregator/tests/src/Functional/ImportOpmlTest.php b/core/modules/aggregator/tests/src/Functional/ImportOpmlTest.php
index 2689af9fc0e1..484ef93288a4 100644
--- a/core/modules/aggregator/tests/src/Functional/ImportOpmlTest.php
+++ b/core/modules/aggregator/tests/src/Functional/ImportOpmlTest.php
@@ -57,7 +57,7 @@ public function openImportForm() {
    * Submits form filled with invalid fields.
    */
   public function validateImportFormFields() {
-    $count_query = \Drupal::entityQuery('aggregator_feed')->count();
+    $count_query = \Drupal::entityQuery('aggregator_feed')->accessCheck(FALSE)->count();
     $before = $count_query->execute();
 
     $edit = [];
@@ -85,7 +85,7 @@ public function validateImportFormFields() {
    * Submits form with invalid, empty, and valid OPML files.
    */
   protected function submitImportForm() {
-    $count_query = \Drupal::entityQuery('aggregator_feed')->count();
+    $count_query = \Drupal::entityQuery('aggregator_feed')->accessCheck(FALSE)->count();
     $before = $count_query->execute();
 
     // Attempting to upload invalid XML.
diff --git a/core/modules/aggregator/tests/src/Functional/UpdateFeedItemTest.php b/core/modules/aggregator/tests/src/Functional/UpdateFeedItemTest.php
index c051d0da4c3c..1c43db482029 100644
--- a/core/modules/aggregator/tests/src/Functional/UpdateFeedItemTest.php
+++ b/core/modules/aggregator/tests/src/Functional/UpdateFeedItemTest.php
@@ -49,11 +49,17 @@ public function testUpdateFeedItem() {
     // Verify that the creation message contains a link to a feed.
     $this->assertSession()->elementExists('xpath', '//div[@data-drupal-messages]//a[contains(@href, "aggregator/sources/")]');
 
-    $fids = \Drupal::entityQuery('aggregator_feed')->condition('url', $edit['url[0][value]'])->execute();
+    $fids = \Drupal::entityQuery('aggregator_feed')
+      ->accessCheck(FALSE)
+      ->condition('url', $edit['url[0][value]'])
+      ->execute();
     $feed = Feed::load(array_values($fids)[0]);
 
     $feed->refreshItems();
-    $item_ids = \Drupal::entityQuery('aggregator_item')->condition('fid', $feed->id())->execute();
+    $item_ids = \Drupal::entityQuery('aggregator_item')
+      ->accessCheck(FALSE)
+      ->condition('fid', $feed->id())
+      ->execute();
     $before = Item::load(array_values($item_ids)[0])->getPostedTime();
 
     // Sleep for 3 second.
diff --git a/core/modules/block_content/tests/src/Functional/BlockContentSaveTest.php b/core/modules/block_content/tests/src/Functional/BlockContentSaveTest.php
index 3c360a6a036c..ca5ea45ba6d5 100644
--- a/core/modules/block_content/tests/src/Functional/BlockContentSaveTest.php
+++ b/core/modules/block_content/tests/src/Functional/BlockContentSaveTest.php
@@ -37,7 +37,10 @@ protected function setUp(): void {
    */
   public function testImport() {
     // Custom block ID must be a number that is not in the database.
-    $max_id = (int) \Drupal::entityQueryAggregate('block_content')->aggregate('id', 'max')->execute()[0]['id_max'];
+    $max_id = (int) \Drupal::entityQueryAggregate('block_content')
+      ->accessCheck(FALSE)
+      ->aggregate('id', 'max')
+      ->execute()[0]['id_max'];
     $test_id = $max_id + mt_rand(1000, 1000000);
     $info = $this->randomMachineName(8);
     $block_array = [
diff --git a/core/modules/block_content/tests/src/Functional/PageEditTest.php b/core/modules/block_content/tests/src/Functional/PageEditTest.php
index 174231e4edc4..223f3891cfc0 100644
--- a/core/modules/block_content/tests/src/Functional/PageEditTest.php
+++ b/core/modules/block_content/tests/src/Functional/PageEditTest.php
@@ -37,7 +37,10 @@ public function testPageEdit() {
     $this->drupalPostForm('block/add/basic', $edit, 'Save');
 
     // Check that the block exists in the database.
-    $blocks = \Drupal::entityQuery('block_content')->condition('info', $edit['info[0][value]'])->execute();
+    $blocks = \Drupal::entityQuery('block_content')
+      ->accessCheck(FALSE)
+      ->condition('info', $edit['info[0][value]'])
+      ->execute();
     $block = BlockContent::load(reset($blocks));
     $this->assertNotEmpty($block, 'Custom block found in database.');
 
diff --git a/core/modules/block_content/tests/src/Kernel/BlockContentEntityReferenceSelectionTest.php b/core/modules/block_content/tests/src/Kernel/BlockContentEntityReferenceSelectionTest.php
index 2e38db42ef4d..1b7cc7fd5d09 100644
--- a/core/modules/block_content/tests/src/Kernel/BlockContentEntityReferenceSelectionTest.php
+++ b/core/modules/block_content/tests/src/Kernel/BlockContentEntityReferenceSelectionTest.php
@@ -121,14 +121,20 @@ public function setUp(): void {
    */
   public function testQueriesNotAltered() {
     // Ensure that queries without all the tags are not altered.
-    $query = $this->entityTypeManager->getStorage('block_content')->getQuery();
+    $query = $this->entityTypeManager->getStorage('block_content')
+      ->getQuery()
+      ->accessCheck(FALSE);
     $this->assertCount(2, $query->execute());
 
-    $query = $this->entityTypeManager->getStorage('block_content')->getQuery();
+    $query = $this->entityTypeManager->getStorage('block_content')
+      ->getQuery()
+      ->accessCheck(FALSE);
     $query->addTag('block_content_access');
     $this->assertCount(2, $query->execute());
 
-    $query = $this->entityTypeManager->getStorage('block_content')->getQuery();
+    $query = $this->entityTypeManager->getStorage('block_content')
+      ->getQuery()
+      ->accessCheck(FALSE);
     $query->addTag('entity_query_block_content');
     $this->assertCount(2, $query->execute());
   }
diff --git a/core/modules/comment/tests/src/Functional/CommentAdminTest.php b/core/modules/comment/tests/src/Functional/CommentAdminTest.php
index ec7e19a0287c..0d823751b21a 100644
--- a/core/modules/comment/tests/src/Functional/CommentAdminTest.php
+++ b/core/modules/comment/tests/src/Functional/CommentAdminTest.php
@@ -258,6 +258,7 @@ public function testCommentedTranslationDeletion() {
     $count_query = \Drupal::entityTypeManager()
       ->getStorage('comment')
       ->getQuery()
+      ->accessCheck(FALSE)
       ->count();
     $before_count = $count_query->execute();
     // Post 2 anonymous comments without contact info.
diff --git a/core/modules/comment/tests/src/Functional/CommentLanguageTest.php b/core/modules/comment/tests/src/Functional/CommentLanguageTest.php
index 2ee4eafe3401..dafb973b5f6e 100644
--- a/core/modules/comment/tests/src/Functional/CommentLanguageTest.php
+++ b/core/modules/comment/tests/src/Functional/CommentLanguageTest.php
@@ -132,6 +132,7 @@ public function testCommentLanguage() {
 
         // Check that comment language matches the current content language.
         $cids = \Drupal::entityQuery('comment')
+          ->accessCheck(FALSE)
           ->condition('entity_id', $node->id())
           ->condition('entity_type', 'node')
           ->condition('field_name', 'comment')
diff --git a/core/modules/content_moderation/tests/src/Kernel/ContentModerationSyncingTest.php b/core/modules/content_moderation/tests/src/Kernel/ContentModerationSyncingTest.php
index 5825ed286390..76b6bc1f3870 100644
--- a/core/modules/content_moderation/tests/src/Kernel/ContentModerationSyncingTest.php
+++ b/core/modules/content_moderation/tests/src/Kernel/ContentModerationSyncingTest.php
@@ -192,6 +192,7 @@ protected function getAllRevisionNames(EntityTestMulRevPub $entity) {
     return array_map(function ($revision_id) use ($storage) {
       return $storage->loadRevision($revision_id)->name->value;
     }, array_keys($storage->getQuery()
+      ->accessCheck(FALSE)
       ->allRevisions()
       ->condition('id', $entity->id())
       ->sort('revision_id', 'ASC')
diff --git a/core/modules/content_moderation/tests/src/Kernel/DefaultRevisionStateTest.php b/core/modules/content_moderation/tests/src/Kernel/DefaultRevisionStateTest.php
index 795007042335..065973a242cd 100644
--- a/core/modules/content_moderation/tests/src/Kernel/DefaultRevisionStateTest.php
+++ b/core/modules/content_moderation/tests/src/Kernel/DefaultRevisionStateTest.php
@@ -127,7 +127,7 @@ public function testMultilingual() {
   protected function assertModerationState($revision_id, $langcode, $expected_state, $expected_workflow = 'editorial') {
     $moderation_state_storage = $this->entityTypeManager->getStorage('content_moderation_state');
 
-    $query = $moderation_state_storage->getQuery();
+    $query = $moderation_state_storage->getQuery()->accessCheck(FALSE);
     $results = $query->allRevisions()
       ->condition('content_entity_revision_id', $revision_id)
       ->condition('langcode', $langcode)
diff --git a/core/modules/field/tests/src/Functional/EntityReference/EntityReferenceAutoCreateTest.php b/core/modules/field/tests/src/Functional/EntityReference/EntityReferenceAutoCreateTest.php
index 7f4bcda3f223..a4df3849c8f4 100644
--- a/core/modules/field/tests/src/Functional/EntityReference/EntityReferenceAutoCreateTest.php
+++ b/core/modules/field/tests/src/Functional/EntityReference/EntityReferenceAutoCreateTest.php
@@ -111,7 +111,7 @@ public function testAutoCreate() {
     $new_title = $this->randomMachineName();
 
     // Assert referenced node does not exist.
-    $base_query = \Drupal::entityQuery('node');
+    $base_query = \Drupal::entityQuery('node')->accessCheck(FALSE);
     $base_query
       ->condition('type', $this->referencedType)
       ->condition('title', $new_title);
@@ -135,6 +135,7 @@ public function testAutoCreate() {
 
     // Assert the referenced node is associated with referencing node.
     $result = \Drupal::entityQuery('node')
+      ->accessCheck(FALSE)
       ->condition('type', $this->referencingType)
       ->execute();
 
@@ -270,6 +271,7 @@ public function testNoBundles() {
 
     // Assert referenced entity was created.
     $result = \Drupal::entityQuery('entity_test_no_bundle_with_label')
+      ->accessCheck(FALSE)
       ->condition('name', $name)
       ->execute();
     $this->assertNotEmpty($result, 'Referenced entity was created.');
@@ -277,6 +279,7 @@ public function testNoBundles() {
 
     // Assert the referenced entity is associated with referencing node.
     $result = \Drupal::entityQuery('node')
+      ->accessCheck(FALSE)
       ->condition('type', $this->referencingType)
       ->execute();
     $this->assertCount(1, $result);
diff --git a/core/modules/field/tests/src/Kernel/BulkDeleteTest.php b/core/modules/field/tests/src/Kernel/BulkDeleteTest.php
index dbeb11286ada..a5d879b4698b 100644
--- a/core/modules/field/tests/src/Kernel/BulkDeleteTest.php
+++ b/core/modules/field/tests/src/Kernel/BulkDeleteTest.php
@@ -168,6 +168,7 @@ public function testDeleteField() {
     // There are 10 entities of this bundle.
     $found = $storage
       ->getQuery()
+      ->accessCheck(FALSE)
       ->condition('type', $bundle)
       ->execute();
     $this->assertCount(10, $found, 'Correct number of entities found before deleting');
@@ -197,6 +198,7 @@ public function testDeleteField() {
     // There are 0 entities of this bundle with non-deleted data.
     $found = $storage
       ->getQuery()
+      ->accessCheck(FALSE)
       ->condition('type', $bundle)
       ->condition("$field_name.deleted", 0)
       ->execute();
@@ -206,6 +208,7 @@ public function testDeleteField() {
     // their values are correct.
     $found = $storage
       ->getQuery()
+      ->accessCheck(FALSE)
       ->condition('type', $bundle)
       ->condition("$field_name.deleted", 1)
       ->sort('id')
@@ -334,6 +337,7 @@ public function testPurgeField() {
 
       // There are $count deleted entities left.
       $found = \Drupal::entityQuery('entity_test')
+        ->accessCheck(FALSE)
         ->condition('type', $bundle)
         ->condition($field_name . '.deleted', 1)
         ->execute();
diff --git a/core/modules/file/tests/src/Functional/FileFieldTestBase.php b/core/modules/file/tests/src/Functional/FileFieldTestBase.php
index ce0503f9f995..77577d561106 100644
--- a/core/modules/file/tests/src/Functional/FileFieldTestBase.php
+++ b/core/modules/file/tests/src/Functional/FileFieldTestBase.php
@@ -74,7 +74,10 @@ public function getTestFile($type_name, $size = NULL) {
    * Retrieves the fid of the last inserted file.
    */
   public function getLastFileId() {
-    return (int) \Drupal::entityQueryAggregate('file')->aggregate('fid', 'max')->execute()[0]['fid_max'];
+    return (int) \Drupal::entityQueryAggregate('file')
+      ->accessCheck(FALSE)
+      ->aggregate('fid', 'max')
+      ->execute()[0]['fid_max'];
   }
 
   /**
diff --git a/core/modules/file/tests/src/Functional/SaveUploadFormTest.php b/core/modules/file/tests/src/Functional/SaveUploadFormTest.php
index 0a316463b0cc..6a4e5015782e 100644
--- a/core/modules/file/tests/src/Functional/SaveUploadFormTest.php
+++ b/core/modules/file/tests/src/Functional/SaveUploadFormTest.php
@@ -76,7 +76,10 @@ protected function setUp(): void {
     $this->phpfile = current($this->drupalGetTestFiles('php'));
     $this->assertFileExists($this->phpfile->uri);
 
-    $this->maxFidBefore = (int) \Drupal::entityQueryAggregate('file')->aggregate('fid', 'max')->execute()[0]['fid_max'];
+    $this->maxFidBefore = (int) \Drupal::entityQueryAggregate('file')
+      ->accessCheck(FALSE)
+      ->aggregate('fid', 'max')
+      ->execute()[0]['fid_max'];
 
     /** @var \Drupal\Core\File\FileSystemInterface $file_system */
     $file_system = \Drupal::service('file_system');
@@ -99,7 +102,10 @@ protected function setUp(): void {
    * Tests the _file_save_upload_from_form() function.
    */
   public function testNormal() {
-    $max_fid_after = (int) \Drupal::entityQueryAggregate('file')->aggregate('fid', 'max')->execute()[0]['fid_max'];
+    $max_fid_after = (int) \Drupal::entityQueryAggregate('file')
+      ->accessCheck(FALSE)
+      ->aggregate('fid', 'max')
+      ->execute()[0]['fid_max'];
     // Verify that a new file was created.
     $this->assertGreaterThan($this->maxFidBefore, $max_fid_after);
     $file1 = File::load($max_fid_after);
@@ -118,7 +124,10 @@ public function testNormal() {
     $this->drupalPostForm('file-test/save_upload_from_form_test', $edit, 'Submit');
     $this->assertSession()->statusCodeEquals(200);
     $this->assertRaw(t('You WIN!'));
-    $max_fid_after = (int) \Drupal::entityQueryAggregate('file')->aggregate('fid', 'max')->execute()[0]['fid_max'];
+    $max_fid_after = (int) \Drupal::entityQueryAggregate('file')
+      ->accessCheck(FALSE)
+      ->aggregate('fid', 'max')
+      ->execute()[0]['fid_max'];
 
     // Check that the correct hooks were called.
     $this->assertFileHooksCalled(['validate', 'insert']);
diff --git a/core/modules/file/tests/src/Functional/SaveUploadTest.php b/core/modules/file/tests/src/Functional/SaveUploadTest.php
index fb3b73a10bfc..359c5251fba3 100644
--- a/core/modules/file/tests/src/Functional/SaveUploadTest.php
+++ b/core/modules/file/tests/src/Functional/SaveUploadTest.php
@@ -73,7 +73,10 @@ protected function setUp(): void {
     $this->phpfile = current($this->drupalGetTestFiles('php'));
     $this->assertFileExists($this->phpfile->uri);
 
-    $this->maxFidBefore = (int) \Drupal::entityQueryAggregate('file')->aggregate('fid', 'max')->execute()[0]['fid_max'];
+    $this->maxFidBefore = (int) \Drupal::entityQueryAggregate('file')
+      ->accessCheck(FALSE)
+      ->aggregate('fid', 'max')
+      ->execute()[0]['fid_max'];
 
     // Upload with replace to guarantee there's something there.
     $edit = [
@@ -95,7 +98,10 @@ protected function setUp(): void {
    * Test the file_save_upload() function.
    */
   public function testNormal() {
-    $max_fid_after = (int) \Drupal::entityQueryAggregate('file')->aggregate('fid', 'max')->execute()[0]['fid_max'];
+    $max_fid_after = (int) \Drupal::entityQueryAggregate('file')
+      ->accessCheck(FALSE)
+      ->aggregate('fid', 'max')
+      ->execute()[0]['fid_max'];
     // Verify that a new file was created.
     $this->assertGreaterThan($this->maxFidBefore, $max_fid_after);
     $file1 = File::load($max_fid_after);
@@ -112,7 +118,10 @@ public function testNormal() {
     $this->drupalPostForm('file-test/upload', $edit, 'Submit');
     $this->assertSession()->statusCodeEquals(200);
     $this->assertRaw(t('You WIN!'));
-    $max_fid_after = (int) \Drupal::entityQueryAggregate('file')->aggregate('fid', 'max')->execute()[0]['fid_max'];
+    $max_fid_after = (int) \Drupal::entityQueryAggregate('file')
+      ->accessCheck(FALSE)
+      ->aggregate('fid', 'max')
+      ->execute()[0]['fid_max'];
 
     // Check that the correct hooks were called.
     $this->assertFileHooksCalled(['validate', 'insert']);
@@ -149,7 +158,10 @@ public function testDuplicate() {
     $image1 = current($this->drupalGetTestFiles('image'));
     $edit = ['files[file_test_upload]' => \Drupal::service('file_system')->realpath($image1->uri)];
     $this->drupalPostForm('file-test/upload', $edit, 'Submit');
-    $max_fid_after = (int) \Drupal::entityQueryAggregate('file')->aggregate('fid', 'max')->execute()[0]['fid_max'];
+    $max_fid_after = (int) \Drupal::entityQueryAggregate('file')
+      ->accessCheck(FALSE)
+      ->aggregate('fid', 'max')
+      ->execute()[0]['fid_max'];
     $file1 = File::load($max_fid_after);
 
     // Simulate a race condition where two files are uploaded at almost the same
@@ -166,7 +178,10 @@ public function testDuplicate() {
     $message = t('The file %file already exists. Enter a unique file URI.', ['%file' => $file1->getFileUri()]);
     $this->assertRaw($message);
     $max_fid_before_duplicate = $max_fid_after;
-    $max_fid_after = (int) \Drupal::entityQueryAggregate('file')->aggregate('fid', 'max')->execute()[0]['fid_max'];
+    $max_fid_after = (int) \Drupal::entityQueryAggregate('file')
+      ->accessCheck(FALSE)
+      ->aggregate('fid', 'max')
+      ->execute()[0]['fid_max'];
     $this->assertEqual($max_fid_before_duplicate, $max_fid_after, 'A new managed file was not created.');
   }
 
diff --git a/core/modules/file/tests/src/FunctionalJavascript/FileManagedFileElementTest.php b/core/modules/file/tests/src/FunctionalJavascript/FileManagedFileElementTest.php
index 944d107d8dcb..f2d788a424cc 100644
--- a/core/modules/file/tests/src/FunctionalJavascript/FileManagedFileElementTest.php
+++ b/core/modules/file/tests/src/FunctionalJavascript/FileManagedFileElementTest.php
@@ -110,7 +110,10 @@ public function testManagedFile() {
    * Retrieves the fid of the last inserted file.
    */
   protected function getLastFileId() {
-    return (int) \Drupal::entityQueryAggregate('file')->aggregate('fid', 'max')->execute()[0]['fid_max'];
+    return (int) \Drupal::entityQueryAggregate('file')
+      ->accessCheck(FALSE)
+      ->aggregate('fid', 'max')
+      ->execute()[0]['fid_max'];
   }
 
 }
diff --git a/core/modules/file/tests/src/Kernel/SaveTest.php b/core/modules/file/tests/src/Kernel/SaveTest.php
index 2d577980e3b1..c5e523c4094f 100644
--- a/core/modules/file/tests/src/Kernel/SaveTest.php
+++ b/core/modules/file/tests/src/Kernel/SaveTest.php
@@ -77,6 +77,7 @@ public function testFileSave() {
     $this->assertEqual(t('The file %value already exists. Enter a unique file URI.', ['%value' => $uppercase_file_duplicate->getFileUri()]), $violations[0]->getMessage());
     // Ensure that file URI entity queries are case sensitive.
     $fids = \Drupal::entityQuery('file')
+      ->accessCheck(FALSE)
       ->condition('uri', $uppercase_file->getFileUri())
       ->execute();
 
diff --git a/core/modules/forum/tests/src/Functional/ForumTest.php b/core/modules/forum/tests/src/Functional/ForumTest.php
index e48506ffea74..9d11008b7c54 100644
--- a/core/modules/forum/tests/src/Functional/ForumTest.php
+++ b/core/modules/forum/tests/src/Functional/ForumTest.php
@@ -273,6 +273,7 @@ public function testAddOrphanTopic() {
     // Must remove forum topics to test creating orphan topics.
     $vid = $this->config('forum.settings')->get('vocabulary');
     $tids = \Drupal::entityQuery('taxonomy_term')
+      ->accessCheck(FALSE)
       ->condition('vid', $vid)
       ->execute();
     $term_storage = \Drupal::entityTypeManager()->getStorage('taxonomy_term');
diff --git a/core/modules/image/tests/src/Functional/ImageFieldTestBase.php b/core/modules/image/tests/src/Functional/ImageFieldTestBase.php
index 291e962102b8..77e75b31a807 100644
--- a/core/modules/image/tests/src/Functional/ImageFieldTestBase.php
+++ b/core/modules/image/tests/src/Functional/ImageFieldTestBase.php
@@ -121,7 +121,10 @@ public function uploadNodeImage($image, $field_name, $type, $alt = '') {
    * Retrieves the fid of the last inserted file.
    */
   protected function getLastFileId() {
-    return (int) \Drupal::entityQueryAggregate('file')->aggregate('fid', 'max')->execute()[0]['fid_max'];
+    return (int) \Drupal::entityQueryAggregate('file')
+      ->accessCheck(FALSE)
+      ->aggregate('fid', 'max')
+      ->execute()[0]['fid_max'];
   }
 
 }
diff --git a/core/modules/jsonapi/tests/src/Functional/ResourceTestBase.php b/core/modules/jsonapi/tests/src/Functional/ResourceTestBase.php
index 6a57a91ae674..c6c940d8b7fd 100644
--- a/core/modules/jsonapi/tests/src/Functional/ResourceTestBase.php
+++ b/core/modules/jsonapi/tests/src/Functional/ResourceTestBase.php
@@ -355,10 +355,13 @@ protected function setUpFields(EntityInterface $entity, UserInterface $account)
    * @throws \Drupal\Core\Entity\EntityStorageException
    */
   protected function getData() {
-    if ($this->entityStorage->getQuery()->count()->execute() < 2) {
+    if ($this->entityStorage->getQuery()->accessCheck(FALSE)->count()->execute() < 2) {
       $this->createAnotherEntity('two');
     }
-    $query = $this->entityStorage->getQuery()->sort($this->entity->getEntityType()->getKey('id'));
+    $query = $this->entityStorage
+      ->getQuery()
+      ->accessCheck(FALSE)
+      ->sort($this->entity->getEntityType()->getKey('id'));
     return $this->entityStorage->loadMultiple($query->execute());
   }
 
diff --git a/core/modules/jsonapi/tests/src/Kernel/Query/FilterTest.php b/core/modules/jsonapi/tests/src/Kernel/Query/FilterTest.php
index 4adb923590b9..2cc221c0db5e 100644
--- a/core/modules/jsonapi/tests/src/Kernel/Query/FilterTest.php
+++ b/core/modules/jsonapi/tests/src/Kernel/Query/FilterTest.php
@@ -169,7 +169,7 @@ public function testQueryCondition() {
       $expected_query = $case[1];
       $filter = Filter::createFromQueryParameter($parameter, $resource_type, $this->fieldResolver);
 
-      $query = $this->nodeStorage->getQuery();
+      $query = $this->nodeStorage->getQuery()->accessCheck(FALSE);
 
       // Get the query condition parsed from the input.
       $condition = $filter->queryCondition($query);
@@ -192,7 +192,7 @@ public function testQueryCondition() {
    */
   protected function queryConditionData() {
     // ((RED or CIRCLE) or (YELLOW and SQUARE))
-    $query = $this->nodeStorage->getQuery();
+    $query = $this->nodeStorage->getQuery()->accessCheck(FALSE);
 
     $or_group = $query->orConditionGroup();
 
diff --git a/core/modules/layout_builder/tests/src/FunctionalJavascript/InlineBlockTest.php b/core/modules/layout_builder/tests/src/FunctionalJavascript/InlineBlockTest.php
index 856a4a83a2a8..14efc24d1356 100644
--- a/core/modules/layout_builder/tests/src/FunctionalJavascript/InlineBlockTest.php
+++ b/core/modules/layout_builder/tests/src/FunctionalJavascript/InlineBlockTest.php
@@ -342,6 +342,7 @@ protected function assertNodeRevisionContent($revision_id, array $content) {
    */
   protected function assertBlockRevisionCountByTitle($block_title, $expected_revision_count) {
     $actual_revision_count = $this->blockStorage->getQuery()
+      ->accessCheck(FALSE)
       ->condition('info', $block_title)
       ->allRevisions()
       ->count()
diff --git a/core/modules/layout_builder/tests/src/FunctionalJavascript/InlineBlockTestBase.php b/core/modules/layout_builder/tests/src/FunctionalJavascript/InlineBlockTestBase.php
index 24e7f3ece564..dbf68d6179c1 100644
--- a/core/modules/layout_builder/tests/src/FunctionalJavascript/InlineBlockTestBase.php
+++ b/core/modules/layout_builder/tests/src/FunctionalJavascript/InlineBlockTestBase.php
@@ -97,7 +97,11 @@ protected function assertSaveLayout() {
    * Gets the latest block entity id.
    */
   protected function getLatestBlockEntityId() {
-    $block_ids = \Drupal::entityQuery('block_content')->sort('id', 'DESC')->range(0, 1)->execute();
+    $block_ids = \Drupal::entityQuery('block_content')
+      ->accessCheck(FALSE)
+      ->sort('id', 'DESC')
+      ->range(0, 1)
+      ->execute();
     $block_id = array_pop($block_ids);
     $this->assertNotEmpty($this->blockStorage->load($block_id));
     return $block_id;
diff --git a/core/modules/media/tests/src/Functional/MediaRevisionTest.php b/core/modules/media/tests/src/Functional/MediaRevisionTest.php
index 184a1b284c4c..c08b392a63bf 100644
--- a/core/modules/media/tests/src/Functional/MediaRevisionTest.php
+++ b/core/modules/media/tests/src/Functional/MediaRevisionTest.php
@@ -198,6 +198,7 @@ protected function assertRevisionCount(EntityInterface $entity, $expected_revisi
       ->get('entity_type.manager')
       ->getStorage($entity_type->id())
       ->getQuery()
+      ->accessCheck(FALSE)
       ->count()
       ->allRevisions()
       ->condition($entity_type->getKey('id'), $entity->id())
diff --git a/core/modules/media/tests/src/Functional/MediaUiFunctionalTest.php b/core/modules/media/tests/src/Functional/MediaUiFunctionalTest.php
index 6155a1acbde9..99953f796473 100644
--- a/core/modules/media/tests/src/Functional/MediaUiFunctionalTest.php
+++ b/core/modules/media/tests/src/Functional/MediaUiFunctionalTest.php
@@ -67,6 +67,7 @@ public function testMediaWithOnlyOneMediaType() {
     $media_id = $this->container->get('entity_type.manager')
       ->getStorage('media')
       ->getQuery()
+      ->accessCheck(FALSE)
       ->execute();
     $media_id = reset($media_id);
     /** @var \Drupal\media\MediaInterface $media */
@@ -139,7 +140,7 @@ public function testMediaWithOnlyOneMediaType() {
     $page->clickLink('Delete');
     $assert_session->pageTextContains('This action cannot be undone');
     $page->pressButton('Delete');
-    $media_id = \Drupal::entityQuery('media')->execute();
+    $media_id = \Drupal::entityQuery('media')->accessCheck(FALSE)->execute();
     $this->assertEmpty($media_id);
   }
 
@@ -446,6 +447,7 @@ public function testMediaCreateRedirect() {
     $media_id = $this->container->get('entity_type.manager')
       ->getStorage('media')
       ->getQuery()
+      ->accessCheck(FALSE)
       ->execute();
     $media_id = reset($media_id);
     $assert_session->addressEquals("media/$media_id/edit");
diff --git a/core/modules/media/tests/src/FunctionalJavascript/MediaDisplayTest.php b/core/modules/media/tests/src/FunctionalJavascript/MediaDisplayTest.php
index adc4dd1284d2..ce548a0a5f36 100644
--- a/core/modules/media/tests/src/FunctionalJavascript/MediaDisplayTest.php
+++ b/core/modules/media/tests/src/FunctionalJavascript/MediaDisplayTest.php
@@ -91,6 +91,7 @@ public function testMediaDisplay() {
       ->get('entity_type.manager')
       ->getStorage('media')
       ->getQuery()
+      ->accessCheck(FALSE)
       ->sort('mid', 'DESC')
       ->execute();
     $image_media_id = reset($image_media_id);
diff --git a/core/modules/media/tests/src/FunctionalJavascript/MediaStandardProfileTest.php b/core/modules/media/tests/src/FunctionalJavascript/MediaStandardProfileTest.php
index e3293b591ead..40c69186007e 100644
--- a/core/modules/media/tests/src/FunctionalJavascript/MediaStandardProfileTest.php
+++ b/core/modules/media/tests/src/FunctionalJavascript/MediaStandardProfileTest.php
@@ -124,6 +124,7 @@ protected function audioTest() {
       ->get('entity_type.manager')
       ->getStorage('media')
       ->getQuery()
+      ->accessCheck(FALSE)
       ->sort('mid', 'DESC')
       ->execute();
     $audio_media_id = reset($audio_media_id);
@@ -209,6 +210,7 @@ protected function imageTest() {
       ->get('entity_type.manager')
       ->getStorage('media')
       ->getQuery()
+      ->accessCheck(FALSE)
       ->sort('mid', 'DESC')
       ->execute();
     $image_media_id = reset($image_media_id);
@@ -308,6 +310,7 @@ protected function documentTest() {
       ->get('entity_type.manager')
       ->getStorage('media')
       ->getQuery()
+      ->accessCheck(FALSE)
       ->sort('mid', 'DESC')
       ->execute();
     $file_media_id = reset($file_media_id);
@@ -395,6 +398,7 @@ protected function remoteVideoTest() {
       ->get('entity_type.manager')
       ->getStorage('media')
       ->getQuery()
+      ->accessCheck(FALSE)
       ->sort('mid', 'DESC')
       ->execute();
     $remote_video_media_id = reset($remote_video_media_id);
@@ -491,6 +495,7 @@ protected function videoTest() {
       ->get('entity_type.manager')
       ->getStorage('media')
       ->getQuery()
+      ->accessCheck(FALSE)
       ->sort('mid', 'DESC')
       ->execute();
     $video_media_id = reset($video_media_id);
diff --git a/core/modules/migrate/tests/src/Kernel/HighWaterTest.php b/core/modules/migrate/tests/src/Kernel/HighWaterTest.php
index 26cc7dcd3f9c..eb648b7cedb6 100644
--- a/core/modules/migrate/tests/src/Kernel/HighWaterTest.php
+++ b/core/modules/migrate/tests/src/Kernel/HighWaterTest.php
@@ -289,7 +289,7 @@ protected function assertNodeDoesNotExist($title) {
    * @return bool
    */
   protected function nodeExists($title) {
-    $query = \Drupal::entityQuery('node');
+    $query = \Drupal::entityQuery('node')->accessCheck(FALSE);
     $result = $query
       ->condition('title', $title)
       ->range(0, 1)
diff --git a/core/modules/migrate/tests/src/Kernel/TrackChangesTest.php b/core/modules/migrate/tests/src/Kernel/TrackChangesTest.php
index 0c27bf299194..04d45b8c04d6 100644
--- a/core/modules/migrate/tests/src/Kernel/TrackChangesTest.php
+++ b/core/modules/migrate/tests/src/Kernel/TrackChangesTest.php
@@ -182,7 +182,7 @@ protected function assertTermDoesNotExist($property, $value) {
    */
   protected function termExists($property, $value) {
     $property = $property === 'description' ? 'description__value' : $property;
-    $query = \Drupal::entityQuery('taxonomy_term');
+    $query = \Drupal::entityQuery('taxonomy_term')->accessCheck(FALSE);
     $result = $query
       ->condition($property, $value)
       ->range(0, 1)
diff --git a/core/modules/migrate_drupal_ui/tests/src/Functional/MigrateUpgradeTestBase.php b/core/modules/migrate_drupal_ui/tests/src/Functional/MigrateUpgradeTestBase.php
index 2cca7136bd87..6565ec7c4dc1 100644
--- a/core/modules/migrate_drupal_ui/tests/src/Functional/MigrateUpgradeTestBase.php
+++ b/core/modules/migrate_drupal_ui/tests/src/Functional/MigrateUpgradeTestBase.php
@@ -253,7 +253,7 @@ protected function assertUpgrade(array $entity_counts) {
     // Assert the correct number of entities exists.
     $actual_entity_counts = [];
     foreach ($entity_definitions as $entity_type) {
-      $actual_entity_counts[$entity_type] = (int) \Drupal::entityQuery($entity_type)->count()->execute();
+      $actual_entity_counts[$entity_type] = (int) \Drupal::entityQuery($entity_type)->accessCheck(FALSE)->count()->execute();
     }
     $this->assertSame($entity_counts, $actual_entity_counts);
 
diff --git a/core/modules/node/tests/node_access_test_auto_bubbling/src/Controller/NodeAccessTestAutoBubblingController.php b/core/modules/node/tests/node_access_test_auto_bubbling/src/Controller/NodeAccessTestAutoBubblingController.php
index 46f4a5e1026f..feb40c60e1f8 100644
--- a/core/modules/node/tests/node_access_test_auto_bubbling/src/Controller/NodeAccessTestAutoBubblingController.php
+++ b/core/modules/node/tests/node_access_test_auto_bubbling/src/Controller/NodeAccessTestAutoBubblingController.php
@@ -19,6 +19,7 @@ class NodeAccessTestAutoBubblingController extends ControllerBase implements Con
    */
   public function latest() {
     $nids = $this->entityTypeManager()->getStorage('node')->getQuery()
+      ->accessCheck(TRUE)
       ->condition('status', NodeInterface::PUBLISHED)
       ->sort('created', 'DESC')
       ->range(0, 3)
diff --git a/core/modules/node/tests/src/Functional/NodeQueryAlterTest.php b/core/modules/node/tests/src/Functional/NodeQueryAlterTest.php
index ad9324244cbc..a54a93ba9d40 100644
--- a/core/modules/node/tests/src/Functional/NodeQueryAlterTest.php
+++ b/core/modules/node/tests/src/Functional/NodeQueryAlterTest.php
@@ -92,6 +92,7 @@ public function testNodeQueryAlterWithRevisions() {
     try {
       $query = \Drupal::entityTypeManager()->getStorage('node')->getQuery();
       $result = $query
+        ->accessCheck(TRUE)
         ->allRevisions()
         ->execute();
 
diff --git a/core/modules/node/tests/src/Functional/NodeSaveTest.php b/core/modules/node/tests/src/Functional/NodeSaveTest.php
index 69c8563a3f47..7ea6f4054795 100644
--- a/core/modules/node/tests/src/Functional/NodeSaveTest.php
+++ b/core/modules/node/tests/src/Functional/NodeSaveTest.php
@@ -50,6 +50,7 @@ protected function setUp(): void {
   public function testImport() {
     // Node ID must be a number that is not in the database.
     $nids = \Drupal::entityTypeManager()->getStorage('node')->getQuery()
+      ->accessCheck(FALSE)
       ->sort('nid', 'DESC')
       ->range(0, 1)
       ->execute();
diff --git a/core/modules/system/tests/modules/cache_test/src/Controller/CacheTestController.php b/core/modules/system/tests/modules/cache_test/src/Controller/CacheTestController.php
index b773d5bb5ae0..9ebc6f3188f4 100644
--- a/core/modules/system/tests/modules/cache_test/src/Controller/CacheTestController.php
+++ b/core/modules/system/tests/modules/cache_test/src/Controller/CacheTestController.php
@@ -32,7 +32,7 @@ public function urlBubbling() {
    */
   public function bundleTags($entity_type_id, $bundle) {
     $storage = \Drupal::entityTypeManager()->getStorage($entity_type_id);
-    $entity_ids = $storage->getQuery()->condition('type', $bundle)->execute();
+    $entity_ids = $storage->getQuery()->accessCheck(TRUE)->condition('type', $bundle)->execute();
     $page = [];
 
     $entities = $storage->loadMultiple($entity_ids);
diff --git a/core/modules/system/tests/modules/entity_test/src/Controller/EntityTestController.php b/core/modules/system/tests/modules/entity_test/src/Controller/EntityTestController.php
index e9f0f8a689a4..3c5603729149 100644
--- a/core/modules/system/tests/modules/entity_test/src/Controller/EntityTestController.php
+++ b/core/modules/system/tests/modules/entity_test/src/Controller/EntityTestController.php
@@ -42,6 +42,7 @@ public function listReferencingEntities($entity_reference_field_name, $reference
     }
 
     $query = $this->entityTypeManager()->getStorage('entity_test')->getQuery()
+      ->accessCheck(TRUE)
       ->condition($entity_reference_field_name . '.target_id', $referenced_entity_id);
     $entities = $this->entityTypeManager()
       ->getStorage('entity_test')
@@ -62,7 +63,7 @@ public function listReferencingEntities($entity_reference_field_name, $reference
    */
   public function listEntitiesAlphabetically($entity_type_id) {
     $entity_type_definition = $this->entityTypeManager()->getDefinition($entity_type_id);
-    $query = $this->entityTypeManager()->getStorage($entity_type_id)->getQuery();
+    $query = $this->entityTypeManager()->getStorage($entity_type_id)->getQuery()->accessCheck(TRUE);
 
     // Sort by label field, if any.
     if ($label_field = $entity_type_definition->getKey('label')) {
diff --git a/core/modules/system/tests/src/Functional/Module/InstallUninstallTest.php b/core/modules/system/tests/src/Functional/Module/InstallUninstallTest.php
index 754b3b18ac21..2897fcee6460 100644
--- a/core/modules/system/tests/src/Functional/Module/InstallUninstallTest.php
+++ b/core/modules/system/tests/src/Functional/Module/InstallUninstallTest.php
@@ -359,7 +359,7 @@ protected function assertHelp($module, $name) {
   protected function preUninstallForum() {
     // There only should be a 'General discussion' term in the 'forums'
     // vocabulary, but just delete any terms there in case the name changes.
-    $query = \Drupal::entityQuery('taxonomy_term');
+    $query = \Drupal::entityQuery('taxonomy_term')->accessCheck(FALSE);
     $query->condition('vid', 'forums');
     $ids = $query->execute();
     $storage = \Drupal::entityTypeManager()->getStorage('taxonomy_term');
diff --git a/core/modules/taxonomy/tests/src/Functional/TermParentsTest.php b/core/modules/taxonomy/tests/src/Functional/TermParentsTest.php
index 03a71dd98442..52c9da687c55 100644
--- a/core/modules/taxonomy/tests/src/Functional/TermParentsTest.php
+++ b/core/modules/taxonomy/tests/src/Functional/TermParentsTest.php
@@ -121,6 +121,7 @@ protected function submitAddTermForm($name) {
 
     $result = $this->termStorage
       ->getQuery()
+      ->accessCheck(FALSE)
       ->condition('name', $name)
       ->execute();
     /* @var \Drupal\taxonomy\TermInterface $term_1 */
diff --git a/core/modules/taxonomy/tests/src/Functional/TermTranslationUITest.php b/core/modules/taxonomy/tests/src/Functional/TermTranslationUITest.php
index 58678d6ed48a..f6cdca485e7f 100644
--- a/core/modules/taxonomy/tests/src/Functional/TermTranslationUITest.php
+++ b/core/modules/taxonomy/tests/src/Functional/TermTranslationUITest.php
@@ -97,6 +97,7 @@ public function testTranslationUI() {
     // Make sure that no row was inserted for taxonomy vocabularies which do
     // not have translations enabled.
     $tids = \Drupal::entityQueryAggregate('taxonomy_term')
+      ->accessCheck(FALSE)
       ->aggregate('tid', 'COUNT')
       ->condition('vid', $this->bundle, '<>')
       ->groupBy('tid')
diff --git a/core/modules/taxonomy/tests/src/Kernel/TaxonomyQueryAlterTest.php b/core/modules/taxonomy/tests/src/Kernel/TaxonomyQueryAlterTest.php
index 0743adb63795..544194356a3d 100644
--- a/core/modules/taxonomy/tests/src/Kernel/TaxonomyQueryAlterTest.php
+++ b/core/modules/taxonomy/tests/src/Kernel/TaxonomyQueryAlterTest.php
@@ -96,7 +96,7 @@ public function testTaxonomyQueryAlter() {
     $this->assertQueryTagTestResult(1, 1);
 
     $this->setupQueryTagTestHooks();
-    $query = \Drupal::entityQuery('taxonomy_term');
+    $query = \Drupal::entityQuery('taxonomy_term')->accessCheck(FALSE);
     $query->addTag('taxonomy_term_access');
     $result = $query->execute();
     // All term IDs were retrieved.
@@ -105,7 +105,7 @@ public function testTaxonomyQueryAlter() {
     $this->assertQueryTagTestResult(1, 1);
 
     $this->setupQueryTagTestHooks();
-    $query = \Drupal::entityQuery('taxonomy_term');
+    $query = \Drupal::entityQuery('taxonomy_term')->accessCheck(FALSE);
     $query->addTag('term_access');
     $result = $query->execute();
     // All term IDs were retrieved.
diff --git a/core/modules/taxonomy/tests/src/Kernel/TermEntityQueryTest.php b/core/modules/taxonomy/tests/src/Kernel/TermEntityQueryTest.php
index afe93fe12d0b..886df937ac45 100644
--- a/core/modules/taxonomy/tests/src/Kernel/TermEntityQueryTest.php
+++ b/core/modules/taxonomy/tests/src/Kernel/TermEntityQueryTest.php
@@ -37,7 +37,7 @@ public function testTermEntityQuery() {
       $term = $this->createTerm($vocabulary);
       $terms[$term->id()] = $term;
     }
-    $result = \Drupal::entityQuery('taxonomy_term')->execute();
+    $result = \Drupal::entityQuery('taxonomy_term')->accessCheck(FALSE)->execute();
     sort($result);
     $this->assertEquals(array_keys($terms), $result);
     $tid = reset($result);
@@ -58,6 +58,7 @@ public function testTermEntityQuery() {
     }
 
     $result = \Drupal::entityQuery('taxonomy_term')
+      ->accessCheck(FALSE)
       ->condition('vid', $vocabulary2->id())
       ->execute();
     sort($result);
diff --git a/core/modules/taxonomy/tests/src/Kernel/VocabularyCrudTest.php b/core/modules/taxonomy/tests/src/Kernel/VocabularyCrudTest.php
index 1078e92cd344..471a0dc5a7cf 100644
--- a/core/modules/taxonomy/tests/src/Kernel/VocabularyCrudTest.php
+++ b/core/modules/taxonomy/tests/src/Kernel/VocabularyCrudTest.php
@@ -44,7 +44,7 @@ protected function setUp(): void {
    */
   public function testTaxonomyVocabularyDeleteWithTerms() {
     $vocabulary = $this->createVocabulary();
-    $query = \Drupal::entityQuery('taxonomy_term')->count();
+    $query = \Drupal::entityQuery('taxonomy_term')->accessCheck(FALSE)->count();
 
     // Assert that there are no terms left.
     $this->assertEquals(0, $query->execute());
diff --git a/core/modules/user/tests/src/Kernel/UserInstallTest.php b/core/modules/user/tests/src/Kernel/UserInstallTest.php
index 1ab0df44acb0..e93980cf34aa 100644
--- a/core/modules/user/tests/src/Kernel/UserInstallTest.php
+++ b/core/modules/user/tests/src/Kernel/UserInstallTest.php
@@ -32,7 +32,7 @@ protected function setUp(): void {
    * Test that the initial users have correct values.
    */
   public function testUserInstall() {
-    $user_ids = \Drupal::entityQuery('user')->sort('uid')->execute();
+    $user_ids = \Drupal::entityQuery('user')->sort('uid')->accessCheck(FALSE)->execute();
     $users = \Drupal::entityTypeManager()->getStorage('user')->loadMultiple($user_ids);
     $anon = $users[0];
     $admin = $users[1];
diff --git a/core/modules/views_ui/tests/src/Functional/DisplayPathTest.php b/core/modules/views_ui/tests/src/Functional/DisplayPathTest.php
index 155db90990a5..7bc995139f12 100644
--- a/core/modules/views_ui/tests/src/Functional/DisplayPathTest.php
+++ b/core/modules/views_ui/tests/src/Functional/DisplayPathTest.php
@@ -214,6 +214,7 @@ public function testDefaultMenuTabRegression() {
     $this->drupalPostForm('admin/structure/menu/manage/admin/add', $edit, 'Save');
 
     $menu_items = \Drupal::entityTypeManager()->getStorage('menu_link_content')->getQuery()
+      ->accessCheck(FALSE)
       ->sort('id', 'DESC')
       ->pager(1)
       ->execute();
diff --git a/core/modules/workspaces/tests/src/Kernel/WorkspaceIntegrationTest.php b/core/modules/workspaces/tests/src/Kernel/WorkspaceIntegrationTest.php
index 9d12b1f08209..e9ba0eb350cf 100644
--- a/core/modules/workspaces/tests/src/Kernel/WorkspaceIntegrationTest.php
+++ b/core/modules/workspaces/tests/src/Kernel/WorkspaceIntegrationTest.php
@@ -487,14 +487,14 @@ public function testEntityQueryWithoutConditions() {
     $node->title->value = 'stage - 2 - r3 - published';
     $node->save();
 
-    $query = $this->entityTypeManager->getStorage('node')->getQuery();
+    $query = $this->entityTypeManager->getStorage('node')->getQuery()->accessCheck(FALSE);
     $query->sort('nid');
     $query->pager(1);
     $result = $query->execute();
 
     $this->assertSame([1 => '1'], $result);
 
-    $query = $this->entityTypeManager->getStorage('node')->getQuery();
+    $query = $this->entityTypeManager->getStorage('node')->getQuery()->accessCheck(FALSE);
     $query->sort('nid', 'DESC');
     $query->pager(10);
     $result = $query->execute();
@@ -546,7 +546,7 @@ public function testEntityQueryRelationship() {
     $entity_test->save();
 
     // Make sure that we're requesting the default revision.
-    $query = $this->entityTypeManager->getStorage('node')->getQuery();
+    $query = $this->entityTypeManager->getStorage('node')->getQuery()->accessCheck(FALSE);
     $query->currentRevision();
 
     $query
@@ -722,6 +722,7 @@ public function testExecuteInWorkspaceContext() {
     $this->assertEquals('live node 1', $live_node->title->value);
 
     $result = $storage->getQuery()
+      ->accessCheck(FALSE)
       ->condition('title', 'live node 1')
       ->execute();
     $this->assertEquals([$live_node->getRevisionId() => $node->id()], $result);
@@ -734,6 +735,7 @@ public function testExecuteInWorkspaceContext() {
       $this->assertEquals('stage node 1', $stage_node->title->value);
 
       $result = $storage->getQuery()
+        ->accessCheck(FALSE)
         ->condition('title', 'stage node 1')
         ->execute();
       $this->assertEquals([$stage_node->getRevisionId() => $stage_node->id()], $result);
@@ -914,20 +916,20 @@ protected function assertEntityQuery(array $expected_values, $entity_type_id) {
     });
 
     // Check entity query counts.
-    $result = (int) $storage->getQuery()->count()->execute();
+    $result = (int) $storage->getQuery()->accessCheck(FALSE)->count()->execute();
     $this->assertSame(count($expected_default_revisions), $result);
 
     $result = (int) $storage->getAggregateQuery()->count()->execute();
     $this->assertSame(count($expected_default_revisions), $result);
 
     // Check entity queries with no conditions.
-    $result = $storage->getQuery()->execute();
+    $result = $storage->getQuery()->accessCheck(FALSE)->execute();
     $expected_result = array_combine(array_column($expected_default_revisions, $revision_key), array_column($expected_default_revisions, $id_key));
     $this->assertEquals($expected_result, $result);
 
     // Check querying each revision individually.
     foreach ($expected_values as $expected_value) {
-      $query = $storage->getQuery();
+      $query = $storage->getQuery()->accessCheck(FALSE);
       $query
         ->condition($entity_keys['id'], $expected_value[$id_key])
         ->condition($entity_keys['label'], $expected_value[$label_key])
diff --git a/core/profiles/demo_umami/modules/demo_umami_content/tests/src/Functional/UninstallDefaultContentTest.php b/core/profiles/demo_umami/modules/demo_umami_content/tests/src/Functional/UninstallDefaultContentTest.php
index 3b97c5c33c29..205575be3a9c 100644
--- a/core/profiles/demo_umami/modules/demo_umami_content/tests/src/Functional/UninstallDefaultContentTest.php
+++ b/core/profiles/demo_umami/modules/demo_umami_content/tests/src/Functional/UninstallDefaultContentTest.php
@@ -32,6 +32,7 @@ public function testReinstall() {
     $this->assertRecipesImported($node_storage);
 
     $count = $node_storage->getQuery()
+      ->accessCheck(FALSE)
       ->condition('type', 'article')
       ->count()
       ->execute();
@@ -47,6 +48,7 @@ public function testReinstall() {
     // Assert the removal of blocks on uninstall.
     foreach ($this->expectedBlocks() as $block_info) {
       $count = $block_storage->getQuery()
+        ->accessCheck(FALSE)
         ->condition('type', $block_info['type'])
         ->count()
         ->execute();
@@ -57,12 +59,14 @@ public function testReinstall() {
 
     // Assert the removal of nodes on uninstall.
     $count = $node_storage->getQuery()
+      ->accessCheck(FALSE)
       ->condition('type', 'article')
       ->count()
       ->execute();
     $this->assertEquals(0, $count);
 
     $count = $node_storage->getQuery()
+      ->accessCheck(FALSE)
       ->condition('type', 'recipe')
       ->count()
       ->execute();
@@ -83,6 +87,7 @@ public function testReinstall() {
    */
   protected function assertRecipesImported(EntityStorageInterface $node_storage) {
     $count = $node_storage->getQuery()
+      ->accessCheck(FALSE)
       ->condition('type', 'recipe')
       ->count()
       ->execute();
@@ -101,6 +106,7 @@ protected function assertRecipesImported(EntityStorageInterface $node_storage) {
    */
   protected function assertArticlesImported(EntityStorageInterface $node_storage) {
     $count = $node_storage->getQuery()
+      ->accessCheck(FALSE)
       ->condition('type', 'article')
       ->count()
       ->execute();
@@ -134,6 +140,7 @@ protected function assertImportedCustomBlock(EntityStorageInterface $block_stora
 
       // Verify that the block can be loaded.
       $count = $block_storage->getQuery()
+        ->accessCheck(FALSE)
         ->condition('type', $block_info['type'])
         ->count()
         ->execute();
diff --git a/core/profiles/demo_umami/tests/src/Functional/DemoUmamiProfileTest.php b/core/profiles/demo_umami/tests/src/Functional/DemoUmamiProfileTest.php
index 90d77e884971..d287572c3c9d 100644
--- a/core/profiles/demo_umami/tests/src/Functional/DemoUmamiProfileTest.php
+++ b/core/profiles/demo_umami/tests/src/Functional/DemoUmamiProfileTest.php
@@ -96,6 +96,7 @@ protected function assertDefaultConfig(StorageInterface $default_config_storage,
   public function testUser() {
     $password = $this->rootUser->pass_raw;
     $ids = \Drupal::entityQuery('user')
+      ->accessCheck(FALSE)
       ->condition('roles', ['author', 'editor'], 'IN')
       ->execute();
 
diff --git a/core/tests/Drupal/KernelTests/Core/Entity/ContentEntityChangedTest.php b/core/tests/Drupal/KernelTests/Core/Entity/ContentEntityChangedTest.php
index 9904c9cc671b..27c8d1e09f78 100644
--- a/core/tests/Drupal/KernelTests/Core/Entity/ContentEntityChangedTest.php
+++ b/core/tests/Drupal/KernelTests/Core/Entity/ContentEntityChangedTest.php
@@ -165,17 +165,17 @@ public function testChanged() {
     // At this point the changed time of the original language (en) is newer
     // than the changed time of the German translation. Now test that entity
     // queries work as expected.
-    $query = $this->mulChangedStorage->getQuery();
+    $query = $this->mulChangedStorage->getQuery()->accessCheck(FALSE);
     $ids = $query->condition('changed', $changed_en)->execute();
 
     $this->assertEqual($entity->id(), reset($ids), 'Entity query can access changed time of original language.');
 
-    $query = $this->mulChangedStorage->getQuery();
+    $query = $this->mulChangedStorage->getQuery()->accessCheck(FALSE);
     $ids = $query->condition('changed', $changed_en, '=', 'en')->execute();
 
     $this->assertEqual($entity->id(), reset($ids), 'Entity query can access changed time of original language by setting the original language as condition.');
 
-    $query = $this->mulChangedStorage->getQuery();
+    $query = $this->mulChangedStorage->getQuery()->accessCheck(FALSE);
     $ids = $query->condition('changed', $changed_de, '=', 'en')->execute();
 
     $this->assertEmpty(
@@ -183,12 +183,12 @@ public function testChanged() {
       'There\'s no original entity stored having the changed time of the German translation.'
     );
 
-    $query = $this->mulChangedStorage->getQuery();
+    $query = $this->mulChangedStorage->getQuery()->accessCheck(FALSE);
     $ids = $query->condition('changed', $changed_en)->condition('default_langcode', '1')->execute();
 
     $this->assertEqual($entity->id(), reset($ids), 'Entity query can access changed time of default language.');
 
-    $query = $this->mulChangedStorage->getQuery();
+    $query = $this->mulChangedStorage->getQuery()->accessCheck(FALSE);
     $ids = $query->condition('changed', $changed_de)->condition('default_langcode', '1')->execute();
 
     $this->assertEmpty(
@@ -196,12 +196,12 @@ public function testChanged() {
       'There\'s no entity stored using the default language having the changed time of the German translation.'
     );
 
-    $query = $this->mulChangedStorage->getQuery();
+    $query = $this->mulChangedStorage->getQuery()->accessCheck(FALSE);
     $ids = $query->condition('changed', $changed_de)->execute();
 
     $this->assertEqual($entity->id(), reset($ids), 'Entity query can access changed time of the German translation.');
 
-    $query = $this->mulChangedStorage->getQuery();
+    $query = $this->mulChangedStorage->getQuery()->accessCheck(FALSE);
     $ids = $query->condition('changed', $changed_de, '=', 'de')->execute();
 
     $this->assertEqual($entity->id(), reset($ids), 'Entity query can access changed time of the German translation.');
diff --git a/core/tests/Drupal/KernelTests/Core/Entity/ContentEntityNonRevisionableFieldTest.php b/core/tests/Drupal/KernelTests/Core/Entity/ContentEntityNonRevisionableFieldTest.php
index 92dfd589dbb6..be73cf8ecd5d 100644
--- a/core/tests/Drupal/KernelTests/Core/Entity/ContentEntityNonRevisionableFieldTest.php
+++ b/core/tests/Drupal/KernelTests/Core/Entity/ContentEntityNonRevisionableFieldTest.php
@@ -101,6 +101,7 @@ public function testMulNonRevisionableField() {
       1 => 1,
     ];
     $revision_ids = $this->mulRev->getQuery()
+      ->accessCheck(FALSE)
       ->allRevisions()
       ->sort('revision_id', 'DESC')
       ->execute();
@@ -111,6 +112,7 @@ public function testMulNonRevisionableField() {
       1 => 1,
     ];
     $non_rev_field_revision_ids = $this->mulRev->getQuery()
+      ->accessCheck(FALSE)
       ->allRevisions()
       ->condition('non_rev_field', 'Huron')
       ->sort('revision_id', 'DESC')
@@ -160,6 +162,7 @@ public function testNonRevisionableField() {
       1 => 1,
     ];
     $revision_ids = $this->rev->getQuery()
+      ->accessCheck(FALSE)
       ->allRevisions()
       ->sort('revision_id', 'DESC')
       ->execute();
@@ -170,6 +173,7 @@ public function testNonRevisionableField() {
       1 => 1,
     ];
     $non_rev_field_revision_ids = $this->rev->getQuery()
+      ->accessCheck(FALSE)
       ->allRevisions()
       ->condition('non_rev_field', 'Superior')
       ->sort('revision_id', 'DESC')
diff --git a/core/tests/Drupal/KernelTests/Core/Entity/ContentEntityNullStorageTest.php b/core/tests/Drupal/KernelTests/Core/Entity/ContentEntityNullStorageTest.php
index 83baf006f085..3b9dcf2af17a 100644
--- a/core/tests/Drupal/KernelTests/Core/Entity/ContentEntityNullStorageTest.php
+++ b/core/tests/Drupal/KernelTests/Core/Entity/ContentEntityNullStorageTest.php
@@ -30,10 +30,10 @@ class ContentEntityNullStorageTest extends KernelTestBase {
    * @see \Drupal\Core\Entity\Query\Null\Query
    */
   public function testEntityQuery() {
-    $this->assertSame(0, \Drupal::entityQuery('contact_message')->count()->execute(), 'Counting a null storage returns 0.');
-    $this->assertSame([], \Drupal::entityQuery('contact_message')->execute(), 'Querying a null storage returns an empty array.');
-    $this->assertSame([], \Drupal::entityQuery('contact_message')->condition('contact_form', 'test')->execute(), 'Querying a null storage returns an empty array and conditions are ignored.');
-    $this->assertSame([], \Drupal::entityQueryAggregate('contact_message')->aggregate('name', 'AVG')->execute(), 'Aggregate querying a null storage returns an empty array');
+    $this->assertSame(0, \Drupal::entityQuery('contact_message')->accessCheck(FALSE)->count()->execute(), 'Counting a null storage returns 0.');
+    $this->assertSame([], \Drupal::entityQuery('contact_message')->accessCheck(FALSE)->execute(), 'Querying a null storage returns an empty array.');
+    $this->assertSame([], \Drupal::entityQuery('contact_message')->accessCheck(FALSE)->condition('contact_form', 'test')->execute(), 'Querying a null storage returns an empty array and conditions are ignored.');
+    $this->assertSame([], \Drupal::entityQueryAggregate('contact_message')->accessCheck(FALSE)->aggregate('name', 'AVG')->execute(), 'Aggregate querying a null storage returns an empty array');
 
   }
 
diff --git a/core/tests/Drupal/KernelTests/Core/Entity/EntityCrudHookTest.php b/core/tests/Drupal/KernelTests/Core/Entity/EntityCrudHookTest.php
index 3968696745c2..6f0ac63ac6b4 100644
--- a/core/tests/Drupal/KernelTests/Core/Entity/EntityCrudHookTest.php
+++ b/core/tests/Drupal/KernelTests/Core/Entity/EntityCrudHookTest.php
@@ -554,7 +554,10 @@ public function testEntityRollback() {
     }
 
     // Check that the block does not exist in the database.
-    $ids = \Drupal::entityQuery('entity_test')->condition('name', 'fail_insert')->execute();
+    $ids = \Drupal::entityQuery('entity_test')
+      ->accessCheck(FALSE)
+      ->condition('name', 'fail_insert')
+      ->execute();
     $this->assertEmpty($ids);
   }
 
diff --git a/core/tests/Drupal/KernelTests/Core/Entity/EntityQueryRelationshipTest.php b/core/tests/Drupal/KernelTests/Core/Entity/EntityQueryRelationshipTest.php
index 016117407116..2c88dccefc52 100644
--- a/core/tests/Drupal/KernelTests/Core/Entity/EntityQueryRelationshipTest.php
+++ b/core/tests/Drupal/KernelTests/Core/Entity/EntityQueryRelationshipTest.php
@@ -114,78 +114,92 @@ public function testQuery() {
     // This returns the 0th entity as that's the only one pointing to the 0th
     // account.
     $this->queryResults = $storage->getQuery()
+      ->accessCheck(FALSE)
       ->condition("user_id.entity.name", $this->accounts[0]->getAccountName())
       ->execute();
     $this->assertResults([0]);
     // This returns the 1st and 2nd entity as those point to the 1st account.
     $this->queryResults = $storage->getQuery()
+      ->accessCheck(FALSE)
       ->condition("user_id.entity.name", $this->accounts[0]->getAccountName(), '<>')
       ->execute();
     $this->assertResults([1, 2]);
     // This returns all three entities because all of them point to an
     // account.
     $this->queryResults = $storage->getQuery()
+      ->accessCheck(FALSE)
       ->exists("user_id.entity.name")
       ->execute();
     $this->assertResults([0, 1, 2]);
     // This returns no entities because all of them point to an account.
     $this->queryResults = $storage->getQuery()
+      ->accessCheck(FALSE)
       ->notExists("user_id.entity.name")
       ->execute();
     $this->assertCount(0, $this->queryResults);
     // This returns the 0th entity as that's only one pointing to the 0th
     // term (test without specifying the field column).
     $this->queryResults = $storage->getQuery()
+      ->accessCheck(FALSE)
       ->condition("$this->fieldName.entity.name", $this->terms[0]->name->value)
       ->execute();
     $this->assertResults([0]);
     // This returns the 0th entity as that's only one pointing to the 0th
     // term (test with specifying the column name).
     $this->queryResults = $storage->getQuery()
+      ->accessCheck(FALSE)
       ->condition("$this->fieldName.target_id.entity.name", $this->terms[0]->name->value)
       ->execute();
     $this->assertResults([0]);
     // This returns the 1st and 2nd entity as those point to the 1st term.
     $this->queryResults = $storage->getQuery()
+      ->accessCheck(FALSE)
       ->condition("$this->fieldName.entity.name", $this->terms[0]->name->value, '<>')
       ->execute();
     $this->assertResults([1, 2]);
     // This returns the 0th entity as that's only one pointing to the 0th
     // account.
     $this->queryResults = $storage->getQuery()
+      ->accessCheck(FALSE)
       ->condition("user_id.entity:user.name", $this->accounts[0]->getAccountName())
       ->execute();
     $this->assertResults([0]);
     // This returns the 1st and 2nd entity as those point to the 1st account.
     $this->queryResults = $storage->getQuery()
+      ->accessCheck(FALSE)
       ->condition("user_id.entity:user.name", $this->accounts[0]->getAccountName(), '<>')
       ->execute();
     $this->assertResults([1, 2]);
     // This returns all three entities because all of them point to an
     // account.
     $this->queryResults = $storage->getQuery()
+      ->accessCheck(FALSE)
       ->exists("user_id.entity:user.name")
       ->execute();
     $this->assertResults([0, 1, 2]);
     // This returns no entities because all of them point to an account.
     $this->queryResults = $storage->getQuery()
+      ->accessCheck(FALSE)
       ->notExists("user_id.entity:user.name")
       ->execute();
     $this->assertCount(0, $this->queryResults);
     // This returns the 0th entity as that's only one pointing to the 0th
     // term (test without specifying the field column).
     $this->queryResults = $storage->getQuery()
+      ->accessCheck(FALSE)
       ->condition("$this->fieldName.entity:taxonomy_term.name", $this->terms[0]->name->value)
       ->execute();
     $this->assertResults([0]);
     // This returns the 0th entity as that's only one pointing to the 0th
     // term (test with specifying the column name).
     $this->queryResults = $storage->getQuery()
+      ->accessCheck(FALSE)
       ->condition("$this->fieldName.target_id.entity:taxonomy_term.name", $this->terms[0]->name->value)
       ->execute();
     $this->assertResults([0]);
     // This returns the 1st and 2nd entity as those point to the 1st term.
     $this->queryResults = $storage->getQuery()
+      ->accessCheck(FALSE)
       ->condition("$this->fieldName.entity:taxonomy_term.name", $this->terms[0]->name->value, '<>')
       ->execute();
     $this->assertResults([1, 2]);
@@ -200,6 +214,7 @@ public function testInvalidSpecifier() {
       ->get('entity_type.manager')
       ->getStorage('taxonomy_term')
       ->getQuery()
+      ->accessCheck(FALSE)
       ->condition('langcode.language.foo', 'bar')
       ->execute();
   }
diff --git a/core/tests/Drupal/KernelTests/Core/Entity/EntityQueryTest.php b/core/tests/Drupal/KernelTests/Core/Entity/EntityQueryTest.php
index ca138f86306f..7b9ce50d2015 100644
--- a/core/tests/Drupal/KernelTests/Core/Entity/EntityQueryTest.php
+++ b/core/tests/Drupal/KernelTests/Core/Entity/EntityQueryTest.php
@@ -162,6 +162,7 @@ public function testEntityQuery() {
     $figures = $this->figures;
     $this->queryResults = $this->storage
       ->getQuery()
+      ->accessCheck(FALSE)
       ->exists($greetings, 'tr')
       ->condition("$figures.color", 'red')
       ->sort('id')
@@ -172,6 +173,7 @@ public function testEntityQuery() {
 
     $query = $this->storage
       ->getQuery('OR')
+      ->accessCheck(FALSE)
       ->exists($greetings, 'tr')
       ->condition("$figures.color", 'red')
       ->sort('id');
@@ -185,6 +187,7 @@ public function testEntityQuery() {
     // Test cloning of query conditions.
     $query = $this->storage
       ->getQuery()
+      ->accessCheck(FALSE)
       ->condition("$figures.color", 'red')
       ->sort('id');
     $cloned_query = clone $query;
@@ -197,7 +200,7 @@ public function testEntityQuery() {
     $this->queryResults = $cloned_query->execute();
     $this->assertResult();
 
-    $query = $this->storage->getQuery();
+    $query = $this->storage->getQuery()->accessCheck(FALSE);
     $group = $query->orConditionGroup()
       ->exists($greetings, 'tr')
       ->condition("$figures.color", 'red');
@@ -212,6 +215,7 @@ public function testEntityQuery() {
     // No figure has both the colors blue and red at the same time.
     $this->queryResults = $this->storage
       ->getQuery()
+      ->accessCheck(FALSE)
       ->condition("$figures.color", 'blue')
       ->condition("$figures.color", 'red')
       ->sort('id')
@@ -219,7 +223,7 @@ public function testEntityQuery() {
     $this->assertResult();
 
     // But an entity might have a red and a blue figure both.
-    $query = $this->storage->getQuery();
+    $query = $this->storage->getQuery()->accessCheck(FALSE);
     $group_blue = $query->andConditionGroup()->condition("$figures.color", 'blue');
     $group_red = $query->andConditionGroup()->condition("$figures.color", 'red');
     $this->queryResults = $query
@@ -231,7 +235,7 @@ public function testEntityQuery() {
     $this->assertResult(3, 7, 11, 15);
 
     // Do the same test but with IN operator.
-    $query = $this->storage->getQuery();
+    $query = $this->storage->getQuery()->accessCheck(FALSE);
     $group_blue = $query->andConditionGroup()->condition("$figures.color", ['blue'], 'IN');
     $group_red = $query->andConditionGroup()->condition("$figures.color", ['red'], 'IN');
     $this->queryResults = $query
@@ -245,6 +249,7 @@ public function testEntityQuery() {
     // An entity might have either red or blue figure.
     $this->queryResults = $this->storage
       ->getQuery()
+      ->accessCheck(FALSE)
       ->condition("$figures.color", ['blue', 'red'], 'IN')
       ->sort('id')
       ->execute();
@@ -253,6 +258,7 @@ public function testEntityQuery() {
 
     $this->queryResults = $this->storage
       ->getQuery()
+      ->accessCheck(FALSE)
       ->exists("$figures.color")
       ->notExists("$greetings.value")
       ->sort('id')
@@ -263,6 +269,7 @@ public function testEntityQuery() {
     // word but allows us to test revisions and string operations.
     $ids = $this->storage
       ->getQuery()
+      ->accessCheck(FALSE)
       ->condition("$greetings.value", 'merhaba')
       ->sort('id')
       ->execute();
@@ -278,6 +285,7 @@ public function testEntityQuery() {
     // Test querying all revisions with a condition on the revision ID field.
     $this->queryResults = $this->storage
       ->getQuery()
+      ->accessCheck(FALSE)
       ->condition('revision_id', $first_entity->getRevisionId())
       ->allRevisions()
       ->execute();
@@ -286,12 +294,14 @@ public function testEntityQuery() {
     // We changed the entity names, so the current revision should not match.
     $this->queryResults = $this->storage
       ->getQuery()
+      ->accessCheck(FALSE)
       ->condition('name.value', $old_name)
       ->execute();
     $this->assertResult();
     // Only if all revisions are queried, we find the old revision.
     $this->queryResults = $this->storage
       ->getQuery()
+      ->accessCheck(FALSE)
       ->condition('name.value', $old_name)
       ->allRevisions()
       ->sort('revision_id')
@@ -300,11 +310,13 @@ public function testEntityQuery() {
     // When querying current revisions, this string is no longer found.
     $this->queryResults = $this->storage
       ->getQuery()
+      ->accessCheck(FALSE)
       ->condition("$greetings.value", 'merhaba')
       ->execute();
     $this->assertResult();
     $this->queryResults = $this->storage
       ->getQuery()
+      ->accessCheck(FALSE)
       ->condition("$greetings.value", 'merhaba')
       ->allRevisions()
       ->sort('revision_id')
@@ -313,6 +325,7 @@ public function testEntityQuery() {
     $this->assertRevisionResult([4, 5, 6, 7, 12, 13, 14, 15], [4, 5, 6, 7, 12, 13, 14, 15]);
     $results = $this->storage
       ->getQuery()
+      ->accessCheck(FALSE)
       ->condition("$greetings.value", 'siema', 'CONTAINS')
       ->sort('id')
       ->execute();
@@ -322,6 +335,7 @@ public function testEntityQuery() {
     $this->assertSame($assert, $results);
     $results = $this->storage
       ->getQuery()
+      ->accessCheck(FALSE)
       ->condition("$greetings.value", 'siema', 'STARTS_WITH')
       ->sort('revision_id')
       ->execute();
@@ -330,6 +344,7 @@ public function testEntityQuery() {
     $this->assertSame(array_slice($assert, 4, 8, TRUE), $results);
     $results = $this->storage
       ->getQuery()
+      ->accessCheck(FALSE)
       ->condition("$greetings.value", 'a', 'ENDS_WITH')
       ->sort('revision_id')
       ->execute();
@@ -338,6 +353,7 @@ public function testEntityQuery() {
     $this->assertSame(array_slice($assert, 4, 8, TRUE), $results);
     $results = $this->storage
       ->getQuery()
+      ->accessCheck(FALSE)
       ->condition("$greetings.value", 'a', 'ENDS_WITH')
       ->allRevisions()
       ->sort('id')
@@ -351,6 +367,7 @@ public function testEntityQuery() {
     // the correct results.
     $results = $this->storage
       ->getQuery()
+      ->accessCheck(FALSE)
       ->latestRevision()
       ->sort('id')
       ->sort('revision_id')
@@ -370,16 +387,19 @@ public function testSort() {
     // Order up and down on a number.
     $this->queryResults = $this->storage
       ->getQuery()
+      ->accessCheck(FALSE)
       ->sort('id')
       ->execute();
     $this->assertResult(range(1, 15));
     $this->queryResults = $this->storage
       ->getQuery()
+      ->accessCheck(FALSE)
       ->sort('id', 'DESC')
       ->execute();
     $this->assertResult(range(15, 1));
     $query = $this->storage
       ->getQuery()
+      ->accessCheck(FALSE)
       ->sort("$figures.color")
       ->sort("$greetings.format")
       ->sort('id');
@@ -431,6 +451,7 @@ public function testSort() {
     \Drupal::getContainer()->get('request_stack')->push($request);
     $this->queryResults = $this->storage
       ->getQuery()
+      ->accessCheck(FALSE)
       ->sort("$figures.color")
       ->sort("$greetings.format")
       ->sort('id')
@@ -441,6 +462,7 @@ public function testSort() {
     // Now test the reversed order.
     $query = $this->storage
       ->getQuery()
+      ->accessCheck(FALSE)
       ->sort("$figures.color", 'DESC')
       ->sort("$greetings.format", 'DESC')
       ->sort('id', 'DESC');
@@ -471,6 +493,7 @@ public function testTableSort() {
 
     $this->queryResults = array_values($this->storage
       ->getQuery()
+      ->accessCheck(FALSE)
       ->tableSort($header)
       ->execute());
     $this->assertBundleOrder('asc');
@@ -486,6 +509,7 @@ public function testTableSort() {
     ];
     $this->queryResults = array_values($this->storage
       ->getQuery()
+      ->accessCheck(FALSE)
       ->tableSort($header)
       ->execute());
     $this->assertBundleOrder('desc');
@@ -497,6 +521,7 @@ public function testTableSort() {
     \Drupal::getContainer()->get('request_stack')->push($request);
     $this->queryResults = $this->storage
       ->getQuery()
+      ->accessCheck(FALSE)
       ->tableSort($header)
       ->execute();
     $this->assertResult(range(15, 1));
@@ -534,6 +559,7 @@ public function testCount() {
     $count = $this->container->get('entity_type.manager')
       ->getStorage('entity_test')
       ->getQuery()
+      ->accessCheck(FALSE)
       ->exists("$field_name.color")
       ->count()
       ->execute();
@@ -546,7 +572,7 @@ public function testCount() {
   public function testNestedConditionGroups() {
     // Query for all entities of the first bundle that have either a red
     // triangle as a figure or the Turkish greeting as a greeting.
-    $query = $this->storage->getQuery();
+    $query = $this->storage->getQuery()->accessCheck(FALSE);
 
     $first_and = $query->andConditionGroup()
       ->condition($this->figures . '.color', 'red')
@@ -574,7 +600,7 @@ public function testNestedConditionGroups() {
   public function testConditionCount() {
     // Query for all entities of the first bundle that
     // have red as a color AND are triangle shaped.
-    $query = $this->storage->getQuery();
+    $query = $this->storage->getQuery()->accessCheck(FALSE);
 
     // Add an AND condition group with 2 conditions in it.
     $and_condition_group = $query->andConditionGroup()
@@ -601,6 +627,7 @@ public function testDelta() {
     // Test numeric delta value in field condition.
     $this->queryResults = $this->storage
       ->getQuery()
+      ->accessCheck(FALSE)
       ->condition("$figures.0.color", 'red')
       ->sort('id')
       ->execute();
@@ -609,6 +636,7 @@ public function testDelta() {
 
     $this->queryResults = $this->storage
       ->getQuery()
+      ->accessCheck(FALSE)
       ->condition("$figures.1.color", 'red')
       ->sort('id')
       ->execute();
@@ -616,7 +644,7 @@ public function testDelta() {
     $this->assertResult();
 
     // Test on two different deltas.
-    $query = $this->storage->getQuery();
+    $query = $this->storage->getQuery()->accessCheck(FALSE);
     $or = $query->andConditionGroup()
       ->condition("$figures.0.color", 'red')
       ->condition("$figures.1.color", 'blue');
@@ -629,6 +657,7 @@ public function testDelta() {
     // Test the delta range condition.
     $this->queryResults = $this->storage
       ->getQuery()
+      ->accessCheck(FALSE)
       ->condition("$figures.%delta.color", ['blue', 'red'], 'IN')
       ->condition("$figures.%delta", [0, 1], 'IN')
       ->sort('id')
@@ -639,6 +668,7 @@ public function testDelta() {
     // Test the delta range condition without conditions on the value.
     $this->queryResults = $this->storage
       ->getQuery()
+      ->accessCheck(FALSE)
       ->condition("$figures.%delta", 1)
       ->sort('id')
       ->execute();
@@ -649,12 +679,14 @@ public function testDelta() {
     // the first item is being targeted.
     $this->queryResults = $this->storage
       ->getQuery()
+      ->accessCheck(FALSE)
       ->condition("id.0.value", [1, 3, 5], 'IN')
       ->sort('id')
       ->execute();
     $this->assertResult(1, 3, 5);
     $this->queryResults = $this->storage
       ->getQuery()
+      ->accessCheck(FALSE)
       ->condition("id.1.value", [1, 3, 5], 'IN')
       ->sort('id')
       ->execute();
@@ -664,12 +696,14 @@ public function testDelta() {
     // only if just the field value is targeted.
     $this->queryResults = $this->storage
       ->getQuery()
+      ->accessCheck(FALSE)
       ->condition("id.%delta.value", [1, 3, 5], 'IN')
       ->sort('id')
       ->execute();
     $this->assertResult(1, 3, 5);
     $this->queryResults = $this->storage
       ->getQuery()
+      ->accessCheck(FALSE)
       ->condition("id.%delta.value", [1, 3, 5], 'IN')
       ->condition("id.%delta", 0, '=')
       ->sort('id')
@@ -677,6 +711,7 @@ public function testDelta() {
     $this->assertResult(1, 3, 5);
     $this->queryResults = $this->storage
       ->getQuery()
+      ->accessCheck(FALSE)
       ->condition("id.%delta.value", [1, 3, 5], 'IN')
       ->condition("id.%delta", 1, '=')
       ->sort('id')
@@ -733,7 +768,7 @@ protected function assertBundleOrder($order) {
    * The tags and metadata should propagate to the SQL query object.
    */
   public function testMetaData() {
-    $query = $this->storage->getQuery();
+    $query = $this->storage->getQuery()->accessCheck(FALSE);
     $query
       ->addTag('efq_metadata_test')
       ->addMetaData('foo', 'bar')
@@ -809,18 +844,21 @@ public function testCaseSensitivity() {
     // Check the case insensitive field, = operator.
     $result = $this->storage
       ->getQuery()
+      ->accessCheck(FALSE)
       ->condition('field_ci', $fixtures[0]['lowercase'] . $fixtures[1]['lowercase'])
       ->execute();
     $this->assertCount(1, $result, 'Case insensitive, lowercase');
 
     $result = $this->storage
       ->getQuery()
+      ->accessCheck(FALSE)
       ->condition('field_ci', $fixtures[0]['uppercase'] . $fixtures[1]['uppercase'])
       ->execute();
     $this->assertCount(1, $result, 'Case insensitive, uppercase');
 
     $result = $this->storage
       ->getQuery()
+      ->accessCheck(FALSE)
       ->condition('field_ci', $fixtures[0]['uppercase'] . $fixtures[1]['lowercase'])
       ->execute();
     $this->assertCount(1, $result, 'Case insensitive, mixed.');
@@ -828,18 +866,21 @@ public function testCaseSensitivity() {
     // Check the case sensitive field, = operator.
     $result = $this->storage
       ->getQuery()
+      ->accessCheck(FALSE)
       ->condition('field_cs', $fixtures[0]['lowercase'] . $fixtures[1]['lowercase'])
       ->execute();
     $this->assertCount(0, $result, 'Case sensitive, lowercase.');
 
     $result = $this->storage
       ->getQuery()
+      ->accessCheck(FALSE)
       ->condition('field_cs', $fixtures[0]['uppercase'] . $fixtures[1]['uppercase'])
       ->execute();
     $this->assertCount(0, $result, 'Case sensitive, uppercase.');
 
     $result = $this->storage
       ->getQuery()
+      ->accessCheck(FALSE)
       ->condition('field_cs', $fixtures[0]['uppercase'] . $fixtures[1]['lowercase'])
       ->execute();
     $this->assertCount(1, $result, 'Case sensitive, exact match.');
@@ -847,17 +888,20 @@ public function testCaseSensitivity() {
     // Check the case insensitive field, IN operator.
     $result = $this->storage
       ->getQuery()
+      ->accessCheck(FALSE)
       ->condition('field_ci', [$fixtures[0]['lowercase'] . $fixtures[1]['lowercase']], 'IN')
       ->execute();
     $this->assertCount(1, $result, 'Case insensitive, lowercase');
 
     $result = $this->storage
       ->getQuery()
+      ->accessCheck(FALSE)
       ->condition('field_ci', [$fixtures[0]['uppercase'] . $fixtures[1]['uppercase']], 'IN')->execute();
     $this->assertCount(1, $result, 'Case insensitive, uppercase');
 
     $result = $this->storage
       ->getQuery()
+      ->accessCheck(FALSE)
       ->condition('field_ci', [$fixtures[0]['uppercase'] . $fixtures[1]['lowercase']], 'IN')
       ->execute();
     $this->assertCount(1, $result, 'Case insensitive, mixed');
@@ -865,6 +909,7 @@ public function testCaseSensitivity() {
     // Check the case sensitive field, IN operator.
     $result = $this->storage
       ->getQuery()
+      ->accessCheck(FALSE)
       ->condition('field_cs', [$fixtures[0]['lowercase'] . $fixtures[1]['lowercase']], 'IN')
       ->execute();
     $this->assertCount(0, $result, 'Case sensitive, lowercase');
diff --git a/core/tests/Drupal/KernelTests/Core/Entity/EntityTranslationTest.php b/core/tests/Drupal/KernelTests/Core/Entity/EntityTranslationTest.php
index 944883af3247..f138296ec956 100644
--- a/core/tests/Drupal/KernelTests/Core/Entity/EntityTranslationTest.php
+++ b/core/tests/Drupal/KernelTests/Core/Entity/EntityTranslationTest.php
@@ -273,7 +273,7 @@ protected function doTestMultilingualProperties($entity_type) {
 
     // Test property conditions and orders with multiple languages in the same
     // query.
-    $query = \Drupal::entityQuery($entity_type);
+    $query = \Drupal::entityQuery($entity_type)->accessCheck(FALSE);
     $group = $query->andConditionGroup()
       ->condition('user_id', $properties[$default_langcode]['user_id'][0], '=', $default_langcode)
       ->condition('name', $properties[$default_langcode]['name'][0], '=', $default_langcode);
@@ -289,7 +289,7 @@ protected function doTestMultilingualProperties($entity_type) {
     $field_value = $this->randomString();
     $entity->getTranslation($langcode)->set($this->fieldName, [['value' => $field_value]]);
     $entity->save();
-    $query = \Drupal::entityQuery($entity_type);
+    $query = \Drupal::entityQuery($entity_type)->accessCheck(FALSE);
     $default_langcode_group = $query->andConditionGroup()
       ->condition('user_id', $properties[$default_langcode]['user_id'][0], '=', $default_langcode)
       ->condition('name', $properties[$default_langcode]['name'][0], '=', $default_langcode);
diff --git a/core/tests/Drupal/KernelTests/Core/Entity/FieldableEntityDefinitionUpdateTest.php b/core/tests/Drupal/KernelTests/Core/Entity/FieldableEntityDefinitionUpdateTest.php
index a4c49794026c..fe7cf34d4393 100644
--- a/core/tests/Drupal/KernelTests/Core/Entity/FieldableEntityDefinitionUpdateTest.php
+++ b/core/tests/Drupal/KernelTests/Core/Entity/FieldableEntityDefinitionUpdateTest.php
@@ -295,7 +295,7 @@ protected function insertData($revisionable, $translatable) {
     // least three times.
     /** @var \Drupal\Core\Entity\TranslatableRevisionableStorageInterface|\Drupal\Core\Entity\EntityStorageInterface $storage */
     $storage = $this->entityTypeManager->getStorage($this->entityTypeId);
-    $next_id = $storage->getQuery()->count()->execute() + 1;
+    $next_id = $storage->getQuery()->accessCheck(FALSE)->count()->execute() + 1;
 
     // Create test entities with two translations and two revisions.
     /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
diff --git a/core/tests/Drupal/Tests/Traits/Core/PathAliasTestTrait.php b/core/tests/Drupal/Tests/Traits/Core/PathAliasTestTrait.php
index 24153b3ba247..08761c309a4a 100644
--- a/core/tests/Drupal/Tests/Traits/Core/PathAliasTestTrait.php
+++ b/core/tests/Drupal/Tests/Traits/Core/PathAliasTestTrait.php
@@ -48,7 +48,7 @@ protected function createPathAlias($path, $alias, $langcode = LanguageInterface:
    */
   protected function loadPathAliasByConditions($conditions) {
     $storage = \Drupal::entityTypeManager()->getStorage('path_alias');
-    $query = $storage->getQuery();
+    $query = $storage->getQuery()->accessCheck(FALSE);
     foreach ($conditions as $field => $value) {
       $query->condition($field, $value);
     }
@@ -70,7 +70,10 @@ protected function loadPathAliasByConditions($conditions) {
    *   (optional) A message to display with the assertion.
    */
   protected function assertPathAliasExists($alias, $langcode = NULL, $path = NULL, $message = NULL) {
-    $query = \Drupal::entityTypeManager()->getStorage('path_alias')->getQuery();
+    $query = \Drupal::entityTypeManager()
+      ->getStorage('path_alias')
+      ->getQuery()
+      ->accessCheck(FALSE);
     $query->condition('alias', $alias, '=');
     if ($langcode) {
       $query->condition('langcode', $langcode, '=');
@@ -96,7 +99,10 @@ protected function assertPathAliasExists($alias, $langcode = NULL, $path = NULL,
    *   (optional) A message to display with the assertion.
    */
   protected function assertPathAliasNotExists($alias, $langcode = NULL, $path = NULL, $message = NULL) {
-    $query = \Drupal::entityTypeManager()->getStorage('path_alias')->getQuery();
+    $query = \Drupal::entityTypeManager()
+      ->getStorage('path_alias')
+      ->getQuery()
+      ->accessCheck(FALSE);
     $query->condition('alias', $alias, '=');
     if ($langcode) {
       $query->condition('langcode', $langcode, '=');
-- 
GitLab