Verified Commit aeae5fd1 authored by Théodore Biadala's avatar Théodore Biadala
Browse files

task: #3558021 Default content tests are slow

By: @catch
By: @alexpott
(cherry picked from commit e19de463)
parent 3b9d87bc
Loading
Loading
Loading
Loading
Loading
+22 −8
Original line number Diff line number Diff line
@@ -38,6 +38,7 @@
#[CoversClass(Exporter::class)]
#[Group('DefaultContent')]
#[Group('Recipe')]
#[Group('#slow')]
#[RunTestsInSeparateProcesses]
class ContentExportTest extends BrowserTestBase {

@@ -157,10 +158,23 @@ public function testExportContent(): void {
    }
  }

  /**
   * Tests various entity export scenarios.
   */
  public function testEntityExportScenarios(): void {
    $this->doTestExportSingleEntityToDirectory();
    $this->doTestExportWithDependencies();
    $this->doTestCircularDependency();
    $this->doTestMissingDependenciesAreLogged();
    $this->doTestExportFileEntityWithMissingPhysicalFile();
    $this->doTestExportedPasswordIsPreserved();
    $this->doTestExportEntitiesFilteredByType();
  }

  /**
   * Tests that an exported user account can be logged in with after import.
   */
  public function testExportedPasswordIsPreserved(): void {
  protected function doTestExportedPasswordIsPreserved(): void {
    $account = $this->createUser();
    $this->assertNotEmpty($account->passRaw);

@@ -171,7 +185,7 @@ public function testExportedPasswordIsPreserved(): void {
      $account->id(),
    ]);
    $this->assertSame(0, $process->wait());
    $dir = 'public://content';
    $dir = 'public://user-content';
    mkdir($dir);
    file_put_contents($dir . '/user.yml', $process->getOutput());

@@ -191,7 +205,7 @@ public function testExportedPasswordIsPreserved(): void {
  /**
   * Tests exporting a single entity to a directory with attachments.
   */
  public function testExportSingleEntityToDirectory(): void {
  protected function doTestExportSingleEntityToDirectory(): void {
    $file = $this->container->get(EntityRepositoryInterface::class)
      ->loadEntityByUuid('file', '7fb09f9f-ba5f-4db4-82ed-aa5ccf7d425d');
    $this->assertInstanceOf(File::class, $file);
@@ -212,7 +226,7 @@ public function testExportSingleEntityToDirectory(): void {
  /**
   * Tests exporting a piece of content with its dependencies.
   */
  public function testExportWithDependencies(): void {
  protected function doTestExportWithDependencies(): void {
    $image_uri = $this->getRandomGenerator()
      ->image(uniqid('public://') . '.png', '200x200', '300x300');
    $file = File::create(['uri' => $image_uri]);
@@ -263,7 +277,7 @@ public function testExportWithDependencies(): void {
  /**
   * Tests that the exporter handles circular dependencies gracefully.
   */
  public function testCircularDependency(): void {
  protected function doTestCircularDependency(): void {
    $this->createEntityReferenceField('node', 'article', 'field_related', 'Related Content', 'node', selection_handler_settings: [
      'target_bundles' => ['page' => 'page'],
    ]);
@@ -298,7 +312,7 @@ public function testCircularDependency(): void {
  /**
   * Tests that the exporter handles missing dependencies gracefully.
   */
  public function testMissingDependenciesAreLogged(): void {
  protected function doTestMissingDependenciesAreLogged(): void {
    $this->createEntityReferenceField('node', 'article', 'field_related', 'Related Content', 'node', selection_handler_settings: [
      'target_bundles' => ['page' => 'page'],
    ]);
@@ -345,7 +359,7 @@ public function testMissingDependenciesAreLogged(): void {
  /**
   * Tests exporting file entities without an accompanying physical file.
   */
  public function testExportFileEntityWithMissingPhysicalFile(): void {
  protected function doTestExportFileEntityWithMissingPhysicalFile(): void {
    $file = $this->container->get(EntityRepositoryInterface::class)
      ->loadEntityByUuid('file', '2b8e0616-3ef0-4a91-8cfb-b31d9128f9f8');
    $this->assertInstanceOf(File::class, $file);
@@ -376,7 +390,7 @@ public function testExportFileEntityWithMissingPhysicalFile(): void {
  /**
   * Tests exporting entities filtered by type.
   */
  public function testExportEntitiesFilteredByType(): void {
  protected function doTestExportEntitiesFilteredByType(): void {
    // We should get an error if we try to export a non-existent entity type.
    $process = $this->runDrupalCommand(['content:export', 'camels']);
    $this->assertSame(1, $process->wait());
+109 −0
Original line number Diff line number Diff line
<?php

declare(strict_types=1);

namespace Drupal\FunctionalTests\DefaultContent;

use Drupal\Core\DefaultContent\Existing;
use Drupal\Core\DefaultContent\Finder;
use Drupal\Core\DefaultContent\Importer;
use Drupal\Core\DefaultContent\ImportException;
use Drupal\Core\File\FileExists;
use Drupal\FunctionalTests\Core\Recipe\RecipeTestTrait;
use Drupal\Tests\BrowserTestBase;
use Drupal\user\UserInterface;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\RunTestsInSeparateProcesses;

/**
 * Tests Content Import.
 */
#[Group('DefaultContent')]
#[Group('Recipe')]
#[Group('#slow')]
#[CoversClass(Importer::class)]
#[RunTestsInSeparateProcesses]
class ContentImportAlreadyExistsTest extends BrowserTestBase {

  use RecipeTestTrait;

  /**
   * {@inheritdoc}
   */
  protected $defaultTheme = 'stark';

  /**
   * {@inheritdoc}
   */
  protected static $modules = [
    'block_content',
    'content_translation',
    'entity_test',
    'layout_builder',
    'media',
    'menu_link_content',
    'node',
    'path',
    'path_alias',
    'system',
    'taxonomy',
    'user',
    'workspaces',
  ];

  /**
   * The directory with the source data.
   */
  private readonly string $contentDir;

  /**
   * The admin account.
   */
  private UserInterface $adminAccount;

  /**
   * {@inheritdoc}
   */
  protected function setUp(): void {
    parent::setUp();
    $this->adminAccount = $this->setUpCurrentUser(admin: TRUE);

    // Apply the recipe that sets up the fields and configuration for our
    // default content.
    $fixtures_dir = $this->getDrupalRoot() . '/core/tests/fixtures';
    $this->applyRecipe($fixtures_dir . '/recipes/default_content_base');

    $this->contentDir = $fixtures_dir . '/default_content';
    \Drupal::service('file_system')->copy($this->contentDir . '/file/druplicon_copy.png', $this->publicFilesDirectory . '/druplicon_copy.png', FileExists::Error);
  }

  /**
   * @return array<array<mixed>>
   *   An array of test cases, each containing an existing entity handling mode.
   */
  public static function providerImportEntityThatAlreadyExists(): array {
    return [
      [Existing::Error],
      [Existing::Skip],
    ];
  }

  /**
 * Tests import entity that already exists.
 */
  #[DataProvider('providerImportEntityThatAlreadyExists')]
  public function testImportEntityThatAlreadyExists(Existing $existing): void {
    $this->drupalCreateUser(values: ['uuid' => '94503467-be7f-406c-9795-fc25baa22203']);

    if ($existing === Existing::Error) {
      $this->expectException(ImportException::class);
      $this->expectExceptionMessage('user 94503467-be7f-406c-9795-fc25baa22203 already exists.');
    }

    $this->container->get(Importer::class)
      ->importContent(new Finder($this->contentDir), $existing);
  }

}
+0 −29
Original line number Diff line number Diff line
@@ -10,7 +10,6 @@
use Drupal\Core\DefaultContent\Existing;
use Drupal\Core\DefaultContent\Finder;
use Drupal\Core\DefaultContent\Importer;
use Drupal\Core\DefaultContent\ImportException;
use Drupal\Core\DefaultContent\InvalidEntityException;
use Drupal\Core\DefaultContent\PreImportEvent;
use Drupal\Core\Entity\EntityRepositoryInterface;
@@ -28,7 +27,6 @@
use Drupal\user\UserInterface;
use Drupal\workspaces\Entity\Workspace;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\RunTestsInSeparateProcesses;
use Psr\Log\LogLevel;
@@ -96,33 +94,6 @@ protected function setUp(): void {
    \Drupal::service('file_system')->copy($this->contentDir . '/file/druplicon_copy.png', $this->publicFilesDirectory . '/druplicon_copy.png', FileExists::Error);
  }

  /**
   * @return array<array<mixed>>
   *   An array of test cases, each containing an existing entity handling mode.
   */
  public static function providerImportEntityThatAlreadyExists(): array {
    return [
      [Existing::Error],
      [Existing::Skip],
    ];
  }

  /**
 * Tests import entity that already exists.
 */
  #[DataProvider('providerImportEntityThatAlreadyExists')]
  public function testImportEntityThatAlreadyExists(Existing $existing): void {
    $this->drupalCreateUser(values: ['uuid' => '94503467-be7f-406c-9795-fc25baa22203']);

    if ($existing === Existing::Error) {
      $this->expectException(ImportException::class);
      $this->expectExceptionMessage('user 94503467-be7f-406c-9795-fc25baa22203 already exists.');
    }

    $this->container->get(Importer::class)
      ->importContent(new Finder($this->contentDir), $existing);
  }

  /**
   * Tests importing content directly, via the API.
   */