<?php namespace Drupal\Tests\trash\Kernel; use Drupal\trash\EntityQuery\Sql\Query; use Drupal\trash_test\Entity\TrashTest; /** * @group trash */ class EntityQueryTest extends TrashKernelTestBase { public function testQueryWithoutDeletedAccess() { $entities = []; $entity_storage = \Drupal::entityTypeManager()->getStorage('trash_test_entity'); for ($i = 0; $i < 5; $i++) { $entity = TrashTest::create(); $entity->save(); $entities[] = $entity; } // Test whether they appear in an entity query. $this->assertCount(5, $entity_storage->getQuery()->accessCheck(FALSE)->execute()); // Delete the first three of them. They should no longer accessible via the // entity query. for ($i = 0; $i < 3; $i++) { $entities[$i]->delete(); } $this->assertCount(2, $entity_storage->getQuery()->accessCheck(FALSE)->execute()); // Check that deleted entities can still be retrieved by an entity query if // the trash context is disabled. $result = $this->getTrashManager()->executeInTrashContext('ignore', function () use ($entity_storage) { return $entity_storage->getQuery()->accessCheck(FALSE)->execute(); }); $this->assertCount(5, $result); } public function testQueryWithDeletedAccess() { $entities = []; $entity_type_manager = \Drupal::entityTypeManager(); $entity_storage = $entity_type_manager->getStorage('trash_test_entity'); for ($i = 0; $i < 5; $i++) { $entity = TrashTest::create(); $entity->save(); $entities[] = $entity; } // Test whether they appear in an entity query. $this->assertCount(5, $entity_storage->getQuery()->accessCheck(FALSE)->execute()); // Delete the first three of them. They should all be individual loadable // but not longer accessible via the entity query. for ($i = 0; $i < 3; $i++) { $entities[$i]->delete(); } // Try to fetch deleted content. $query = $entity_storage->getQuery(); assert($query instanceof Query); $query->accessCheck(FALSE); $query->isDeleted(); $this->assertCount(3, $query->execute()); // Try the same but with using a direct condition on the deleted flag. $query = $entity_storage->getQuery(); assert($query instanceof Query); $query->accessCheck(FALSE); $query->condition('deleted', 0, '>'); $this->assertCount(3, $query->execute()); // Try to fetch not deleted content. $query = $entity_storage->getQuery(); assert($query instanceof Query); $query->accessCheck(FALSE); $query->isNotDeleted(); $this->assertCount(2, $query->execute()); // Try the same but with using a direct condition on the deleted flag. $query = $entity_storage->getQuery(); assert($query instanceof Query); $query->accessCheck(FALSE); $query->condition('deleted', 0, '>'); $this->assertCount(3, $query->execute()); } }