Newer
Older
<?php
namespace Drupal\Tests\trash\Kernel;
use Drupal\trash_test\Entity\TrashTest;
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());

Andrei Mateescu
committed
// 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());

Andrei Mateescu
committed
// 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 = [];

Daniel Wehner
committed
$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();
}

Daniel Wehner
committed
// Try to fetch deleted content.
$query = $entity_storage->getQuery();
assert($query instanceof Query);
$query->accessCheck(FALSE);
$query->isDeleted();
$this->assertCount(3, $query->execute());

Daniel Wehner
committed
// 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, '>');

Daniel Wehner
committed
$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());

Daniel Wehner
committed
// 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, '>');

Daniel Wehner
committed
$this->assertCount(3, $query->execute());