Commit 5ca1f81b authored by Nerea Enrique's avatar Nerea Enrique Committed by Łukasz Zaroda
Browse files

Issue #3274597 by CurriedN, Luke_Nuke: Purge only installed entities

parent c0bfc473
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -9,7 +9,7 @@ services:
    class: Drupal\content_fixtures\Service\ReferenceRepository

  content_fixtures_content_purger:
    arguments: ['@entity_type.manager']
    arguments: ['@entity_type.manager', '@entity.last_installed_schema.repository']
    class: Drupal\content_fixtures\Purger\ContentPurger

  content_fixtures_default_purger:
+18 −3
Original line number Diff line number Diff line
@@ -4,6 +4,8 @@ namespace Drupal\content_fixtures\Purger;

use Drupal\Core\Entity\ContentEntityTypeInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityLastInstalledSchemaRepositoryInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;

/**
@@ -15,11 +17,17 @@ class ContentPurger implements PurgerInterface {
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface*/
  private $entityTypeManager;

  /**
   * @var \Drupal\Core\Entity\EntityLastInstalledSchemaRepositoryInterface
   */
  private $entityLastInstalledSchemaRepository;

  /**
   *
   */
  public function __construct(EntityTypeManagerInterface $entityTypeManager) {
  public function __construct(EntityTypeManagerInterface $entityTypeManager, EntityLastInstalledSchemaRepositoryInterface $entityLastInstalledSchemaRepository) {
    $this->entityTypeManager = $entityTypeManager;
    $this->entityLastInstalledSchemaRepository = $entityLastInstalledSchemaRepository;
  }

  /**
@@ -51,9 +59,16 @@ class ContentPurger implements PurgerInterface {
    $entity_type_definations = $this->entityTypeManager->getDefinitions();
    /* @var $definition \Drupal\Core\Entity\EntityTypeInterface */
    foreach ($entity_type_definations as $key => $definition) {
      if ($definition instanceof ContentEntityTypeInterface) {
        $contentEntityTypes[] = $key;
      $lastInstalledDefinition = $this->entityLastInstalledSchemaRepository->getLastInstalledDefinition($definition->id());
      if (!$lastInstalledDefinition instanceof EntityTypeInterface) {
        continue;
      }

      if (!$definition instanceof ContentEntityTypeInterface) {
        continue;
      }

      $contentEntityTypes[] = $key;
    }

    return $contentEntityTypes;
+8 −0
Original line number Diff line number Diff line
name: 'Content Fixtures entity test'
type: module
description: 'Support module for testing Content Fixtures in relation to entities.'
core_version_requirement: ^8.7.7 || ^9
package: Testing
version: VERSION
dependencies:
  - drupal:node
+31 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\content_fixtures_entity_test\Entity;

use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;

/**
 * @ContentEntityType(
 *   id = "content_fixtures_test_entity",
 *   label = @Translation("Content Fixtures Test Entity"),
 *   base_table = "content_fixtures_test_entity",
 *   entity_keys = {
 *     "id" = "id",
 *   },
 * )
 */
class ContentFixturesTestEntity extends ContentEntityBase {

  public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
    // Standard field, used as unique if primary index.
    $fields['id'] = BaseFieldDefinition::create('integer')
      ->setLabel(t('ID'))
      ->setDescription(t('ID.'))
      ->setReadOnly(TRUE);

    return $fields;
  }

}
+87 −0
Original line number Diff line number Diff line
<?php

/**
 * @file
 */

namespace Drupal\Tests\content_fixtures\Kernel;

use Drupal\KernelTests\KernelTestBase;

/**
 * Class EntityTest.
 *
 * @group content_fixtures
 */
class EntityTest extends KernelTestBase {

  protected static $modules = [
    'content_fixtures',
    'content_fixtures_entity_test',
  ];

  /** @var \Drupal\content_fixtures\Purger\PurgerInterface */
  private $purger;

  /** @var \Drupal\Core\Entity\EntityStorageInterface */
  private $contentFixturesTestEntityStorage;

  protected function setUp(): void {
    parent::setUp();

    $this->installConfig(['content_fixtures']);

    $this->purger = $this->container->get('content_fixtures_default_purger');
    $this->contentFixturesTestEntityStorage = $this->container
      ->get('entity_type.manager')
      ->getStorage('content_fixtures_test_entity');
  }

  /**
   * Tests if purge works if we have uninstalled entity in codebase.
   *
   * @covers \Drupal\content_fixtures\Purger\ContentPurger
   */
  public function testPurgeWithUninstalledCustomEntity() {
    $this->purger->purge();

    // @doesNotPerformAssertions doesn't work correctly at the time of
    // writing this.
    // See: https://www.drupal.org/project/drupalci/issues/3281201
    // So we need to fake assertion.
    $this->assertTrue(true);
  }

  /**
   * Tests if installed custom entities are being correctly purged.
   *
   * @covers \Drupal\content_fixtures\Purger\ContentPurger
   */
  public function testPurgeInstalledCustomEntity() {
    $this->installEntitySchema('content_fixtures_test_entity');

    $this->contentFixturesTestEntityStorage->create([
      'id' => 1,
    ])->save();

    $this->assertEquals(
      1,
      $this->contentFixturesTestEntityStorage
        ->getQuery()
        ->accessCheck(false)
        ->count()
        ->execute()
    );

    $this->purger->purge();
    $this->assertEquals(
      0,
      $this->contentFixturesTestEntityStorage
        ->getQuery()
        ->accessCheck(false)
        ->count()
        ->execute()
    );
  }

}