Unverified Commit c889311f authored by Alex Pott's avatar Alex Pott
Browse files

Issue #3532962 by phenaproxima: Ensure that the default content system can...

Issue #3532962 by phenaproxima: Ensure that the default content system can import a workspace with content in it
parent 8cd685b9
Loading
Loading
Loading
Loading
+23 −0
Original line number Diff line number Diff line
@@ -35,6 +35,7 @@
use Drupal\Tests\media\Traits\MediaTypeCreationTrait;
use Drupal\Tests\taxonomy\Traits\TaxonomyTestTrait;
use Drupal\user\UserInterface;
use Drupal\workspaces\Entity\Workspace;
use Psr\Log\LogLevel;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;

@@ -72,6 +73,7 @@ class ContentImportTest extends BrowserTestBase {
    'system',
    'taxonomy',
    'user',
    'workspaces',
  ];

  /**
@@ -180,6 +182,22 @@ public function testDirectContentImport(): void {
      );
    };
    $this->assertTrue($logger->hasRecordThatPasses($predicate, LogLevel::WARNING));

    // Visit a page that is published in a non-live workspace; we should not be
    // able to see it, because we don't have permission.
    $node_in_workspace = $this->container->get(EntityRepositoryInterface::class)
      ->loadEntityByUuid('node', '48475954-e878-439c-9d3d-226724a44269');
    $this->assertInstanceOf(NodeInterface::class, $node_in_workspace);
    $node_url = $node_in_workspace->toUrl();
    $this->drupalGet($node_url);
    $assert_session = $this->assertSession();
    $assert_session->statusCodeEquals(403);
    // If we log in with administrative privileges (i.e., we can look at any
    // workspace), we should be able to see it.
    $this->drupalLogin($this->adminAccount);
    $this->drupalGet($node_url);
    $assert_session->statusCodeEquals(200);
    $assert_session->pageTextContains($node_in_workspace->label());
  }

  /**
@@ -303,6 +321,11 @@ private function assertContentWasImported(AccountInterface $account): void {
    $this->assertInstanceOf(Section::class, $section);
    $this->assertCount(2, $section->getComponents());
    $this->assertSame('system_powered_by_block', $section->getComponent('03b45f14-cf74-469a-8398-edf3383ce7fa')->getPluginId());

    // Workspaces should have been imported with their parent references intact.
    $workspaces = Workspace::loadMultiple();
    $this->assertArrayHasKey('test_workspace', $workspaces);
    $this->assertSame('test_workspace', $workspaces['inner_test']?->parent->entity->id());
  }

  /**
+48 −11
Original line number Diff line number Diff line
@@ -19,18 +19,55 @@ class FinderTest extends UnitTestCase {
   */
  public function testFoundDataIsInDependencyOrder(): void {
    $finder = new Finder(__DIR__ . '/../../../../fixtures/default_content');
    $actual_order = array_keys($finder->data);

    $expected_order = [
      // First is the author of the node.
      '94503467-be7f-406c-9795-fc25baa22203',
      // Next, the taxonomy term referenced by the node.
      '550f86ad-aa11-4047-953f-636d42889f85',
      // Then we have the node itself, since it has no other dependencies.
      'e1714f23-70c0-4493-8e92-af1901771921',
      // Finally, the menu link to the node.
      '3434bd5a-d2cd-4f26-bf79-a7f6b951a21b',
    ];
    $this->assertSame($expected_order, array_slice(array_keys($finder->data), 0, 4));
    $node_uuid = 'e1714f23-70c0-4493-8e92-af1901771921';
    // The author of the node should come before the node itself. We're using
    // named arguments here purely for clarity.
    $this->assertRelativeOrder(
      $actual_order,
      earlier: '94503467-be7f-406c-9795-fc25baa22203',
      later: $node_uuid,
    );
    // Same with the taxonomy term referenced by the node.
    $this->assertRelativeOrder(
      $actual_order,
      earlier: '550f86ad-aa11-4047-953f-636d42889f85',
      later: $node_uuid,
    );
    // The menu link to the node should come after the node.
    $this->assertRelativeOrder(
      $actual_order,
      earlier: $node_uuid,
      later: '3434bd5a-d2cd-4f26-bf79-a7f6b951a21b',
    );

    // A node that is in a workspace should come after the workspace itself.
    $this->assertRelativeOrder(
      $actual_order,
      earlier: '384c4c10-cc41-4d7e-a1cc-85d1cdc9e87d',
      later: '48475954-e878-439c-9d3d-226724a44269',
    );
  }

  /**
   * Asserts that an item in an array comes before another item in that array.
   *
   * @param array $haystack
   *   The array to examine.
   * @param mixed $earlier
   *   The item which should come first.
   * @param mixed $later
   *   The item which should come after.
   */
  private function assertRelativeOrder(array $haystack, mixed $earlier, mixed $later): void {
    $haystack = array_values($haystack);
    $earlier_index = array_search($earlier, $haystack, TRUE);
    $later_index = array_search($later, $haystack, TRUE);
    $this->assertIsInt($earlier_index);
    $this->assertIsInt($later_index);
    // "Later" should be greater than "earlier".
    $this->assertGreaterThan($earlier_index, $later_index);
  }

  /**
+47 −0
Original line number Diff line number Diff line
_meta:
  version: '1.0'
  entity_type: node
  uuid: 48475954-e878-439c-9d3d-226724a44269
  bundle: page
  default_langcode: en
  depends:
    384c4c10-cc41-4d7e-a1cc-85d1cdc9e87d: workspace
default:
  revision_uid:
    -
      target_id: 1
  status:
    -
      value: false
  uid:
    -
      target_id: 1
  title:
    -
      value: 'A happy little workspace'
  created:
    -
      value: 1751155670
  promote:
    -
      value: false
  sticky:
    -
      value: false
  revision_translation_affected:
    -
      value: true
  path:
    -
      alias: ''
      langcode: en
  # TRICKY! Default Content does not export the `workspace` field because it skips internal
  # properties, but core's exporter should be sure to include it.
  workspace:
    -
      target_id: test_workspace
  body:
    -
      value: 'This page lives in a workspace! How neat!'
      format: plain_text
      summary: ''
+24 −0
Original line number Diff line number Diff line
_meta:
  version: '1.0'
  entity_type: workspace
  uuid: 93f5b0b4-ada9-4bcd-a11d-f7329e9afe21
  depends:
    384c4c10-cc41-4d7e-a1cc-85d1cdc9e87d: workspace
default:
  # TRICKY! Default Content does not export the `id` field, but core's exporter should.
  # Without it, the import will fail.
  id:
    -
      value: inner_test
  uid:
    -
      target_id: 1
  label:
    -
      value: 'Inner Test'
  parent:
    -
      entity: 384c4c10-cc41-4d7e-a1cc-85d1cdc9e87d
  created:
    -
      value: 1751154834
+19 −0
Original line number Diff line number Diff line
_meta:
  version: '1.0'
  entity_type: workspace
  uuid: 384c4c10-cc41-4d7e-a1cc-85d1cdc9e87d
default:
  # TRICKY! Default Content does not export the `id` field, but core's exporter should.
  # Without it, the import will fail.
  id:
    -
      value: test_workspace
  uid:
    -
      target_id: 1
  label:
    -
      value: 'Test Workspace'
  created:
    -
      value: 1751154825