function WorkspaceAssociationRevisionTableUpdateTest::testWorkspaceAssociationRevisionTableUpdate

Tests that the workspace_association_revision table is created and populated.

File

core/modules/workspaces/tests/src/Functional/Update/WorkspaceAssociationRevisionTableUpdateTest.php, line 29

Class

WorkspaceAssociationRevisionTableUpdateTest
Tests the upgrade path for the workspace_association_revision table.

Namespace

Drupal\Tests\workspaces\Functional\Update

Code

public function testWorkspaceAssociationRevisionTableUpdate() : void {
  $connection = \Drupal::database();
  $schema = $connection->schema();
  $entity_type_manager = \Drupal::entityTypeManager();
  // Ensure the table doesn't exist before the update.
  $this->assertFalse($schema->tableExists('workspace_association_revision'));
  // Get workspace associations to test with.
  $workspace_associations = $connection->select('workspace_association', 'wa')
    ->fields('wa')
    ->execute()
    ->fetchAll();
  // Verify we have the correct test data in the workspace_association table:
  // 4 node revisions and 4 taxonomy term revisions
  $this->assertCount(8, $workspace_associations, 'Test data exists in workspace_association table.');
  // Store expected results using the old getAssociatedRevisions logic.
  $expected_revisions = [];
  $expected_initial_revisions = [];
  foreach ($workspace_associations as $association) {
    $workspace_id = $association->workspace;
    $entity_type_id = $association->target_entity_type_id;
    if (!isset($expected_revisions[$workspace_id][$entity_type_id])) {
      // Replicate old getAssociatedRevisions logic.
      $expected_revisions[$workspace_id][$entity_type_id] = $this->getOldAssociatedRevisions($connection, $entity_type_manager, $workspace_id, $entity_type_id);
      // Replicate old getAssociatedInitialRevisions logic.
      $expected_initial_revisions[$workspace_id][$entity_type_id] = $this->getOldAssociatedInitialRevisions($connection, $entity_type_manager, $workspace_id, $entity_type_id);
    }
  }
  // Check that we have the proper expectations based on the test data from
  // update fixture.
  $this->assertCount(10, $expected_revisions['summer']['node']);
  $this->assertCount(4, $expected_revisions['summer']['taxonomy_term']);
  $this->assertCount(10, $expected_revisions['winter']['node']);
  $this->assertCount(4, $expected_revisions['winter']['taxonomy_term']);
  // Run the update.
  $this->runUpdates();
  // Verify the table was created.
  $this->assertTrue($schema->tableExists('workspace_association_revision'));
  // Now test with the updated methods from the workspace association service.
  $workspace_association = \Drupal::service('workspaces.association');
  // Compare results for each workspace/entity type combination.
  foreach ($expected_revisions as $workspace_id => $entity_types) {
    foreach ($entity_types as $entity_type_id => $expected_result) {
      $actual_result = $workspace_association->getAssociatedRevisions($workspace_id, $entity_type_id);
      $this->assertEquals($expected_result, $actual_result, sprintf('Associated revisions match for workspace %s, entity type %s', $workspace_id, $entity_type_id));
    }
  }
  // Compare initial revisions.
  foreach ($expected_initial_revisions as $workspace_id => $entity_types) {
    foreach ($entity_types as $entity_type_id => $expected_result) {
      $actual_result = $workspace_association->getAssociatedInitialRevisions($workspace_id, $entity_type_id);
      $this->assertEquals($expected_result, $actual_result, sprintf('Associated initial revisions match for workspace %s, entity type %s', $workspace_id, $entity_type_id));
    }
  }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.