Commit 65873ade authored by catch's avatar catch

Issue #3204163 by jonathanshaw: EntityQuery accessCheck: tests should not...

Issue #3204163 by jonathanshaw: EntityQuery accessCheck: tests should not check access unless relevant
parent 3f057d61
......@@ -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());
......
......@@ -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);
}
......
......@@ -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');
}
......
......@@ -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.');
}
......
......@@ -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.
......
......@@ -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.
......
......@@ -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 = [
......
......@@ -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.');
......
......@@ -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());
}
......
......@@ -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.
......
......@@ -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')
......
......@@ -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')
......
......@@ -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)
......
......@@ -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);
......
......@@ -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();
......
......@@ -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'];
}
/**
......
......@@ -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']);
......
......@@ -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.');
}
......
......@@ -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'];
}
}
......@@ -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();
......
......@@ -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');
......
......@@ -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'];
}
}
......@@ -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());
}
......
......@@ -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();
......
......@@ -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()
......
......@@ -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;
......
......@@ -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())
......
......@@ -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");
......
......@@ -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);
......
......@@ -124,6 +124,7 @@ protected function audioTest() {
->get('entity_type.manager')
->getStorage('media')
->getQuery()