Skip to content
Snippets Groups Projects
TrashKernelTest.php 1.92 KiB
Newer Older
<?php

namespace Drupal\Tests\trash\Kernel;

use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\trash_test\Entity\TrashTest;

Andrei Mateescu's avatar
Andrei Mateescu committed
/**
 * @group trash
 */
class TrashKernelTest extends TrashKernelTestBase {

  public function testDeletion() {
    assert($entity instanceof ContentEntityInterface);
    $entity->save();
    $entity_id = $entity->id();

    $this->assertNotNull(TrashTest::load($entity_id));
    $this->assertTrue($entity->get('deleted')->isEmpty());
    $this->assertNull($entity->get('deleted')->value);
    // Test the default 'active' trash context.
    $entity = TrashTest::load($entity_id);
    $this->assertNull($entity, 'Deleted entities can not be loaded in the default (active) trash context.');
    // Test the 'ignore' trash context.
    $entity = $this->getTrashManager()->executeInTrashContext('ignore', function () use ($entity_id) {
      return TrashTest::load($entity_id);
    });
    assert($entity instanceof ContentEntityInterface);
    $this->assertNotNull($entity, 'Deleted entities can still be loaded in the "ignore" trash context.');
    $this->assertEquals(\Drupal::time()->getRequestTime(), $entity->get('deleted')->value);

    $second_entity = TrashTest::create();
    $second_entity->save();
    $second_entity_id = $second_entity->id();

    // Test the default 'active' trash context for multiple load.
    $entities = TrashTest::loadMultiple();
    $this->assertCount(1, $entities);
    $this->assertEquals($second_entity_id, $entities[$second_entity_id]->id());

    // Test the 'ignore' trash context  for multiple load.
    $entities = $this->getTrashManager()->executeInTrashContext('ignore', function () {
      return TrashTest::loadMultiple();
    });
    $this->assertCount(2, $entities);
    $this->assertEquals($entity_id, $entities[$entity_id]->id());
    $this->assertEquals($second_entity_id, $entities[$second_entity_id]->id());