function WorkspaceAssociationTest::testWorkspaceAssociation

Same name in other branches
  1. 10 core/modules/workspaces/tests/src/Kernel/WorkspaceAssociationTest.php \Drupal\Tests\workspaces\Kernel\WorkspaceAssociationTest::testWorkspaceAssociation()

Tests the revisions tracked by a workspace.

@covers ::getTrackedEntities @covers ::getAssociatedRevisions

@dataProvider getEntityTypeIds

Parameters

string $entity_type_id: The ID of the entity type to test.

array $entity_values: An array of values for the entities created in this test.

File

core/modules/workspaces/tests/src/Kernel/WorkspaceAssociationTest.php, line 85

Class

WorkspaceAssociationTest
Tests workspace associations.

Namespace

Drupal\Tests\workspaces\Kernel

Code

public function testWorkspaceAssociation(string $entity_type_id, array $entity_values) : void {
    $entity_1 = $this->createEntity($entity_type_id, $entity_values[1]);
    $this->createEntity($entity_type_id, $entity_values[2]);
    // Edit one of the existing nodes in 'stage'.
    $this->switchToWorkspace('stage');
    $entity_1->set('name', 'Test entity 1 - stage - published');
    $entity_1->setPublished();
    // This creates rev. 3.
    $entity_1->save();
    // Generate content with the following structure:
    // Stage:
    // - Test entity 3 - stage - unpublished (rev. 4)
    // - Test entity 4 - stage - published (rev. 5 and 6)
    $this->createEntity($entity_type_id, $entity_values[3]);
    $this->createEntity($entity_type_id, $entity_values[4]);
    $expected_latest_revisions = [
        'stage' => [
            3,
            4,
            6,
        ],
    ];
    $expected_all_revisions = [
        'stage' => [
            3,
            4,
            5,
            6,
        ],
    ];
    $expected_initial_revisions = [
        'stage' => [
            4,
            5,
        ],
    ];
    $this->assertWorkspaceAssociations($entity_type_id, $expected_latest_revisions, $expected_all_revisions, $expected_initial_revisions);
    // Dev:
    // - Test entity 1 - stage - published (rev. 3)
    // - Test entity 3 - stage - unpublished (rev. 4)
    // - Test entity 4 - stage - published (rev. 5 and 6)
    // - Test entity 5 - dev - unpublished (rev. 7)
    // - Test entity 6 - dev - published (rev. 8 and 9)
    $this->switchToWorkspace('dev');
    $this->createEntity($entity_type_id, $entity_values[5]);
    $this->createEntity($entity_type_id, $entity_values[6]);
    $expected_latest_revisions += [
        'dev' => [
            3,
            4,
            6,
            7,
            9,
        ],
    ];
    // Revisions 3, 4, 5 and 6 that were created in the parent 'stage' workspace
    // are also considered as being part of the child 'dev' workspace.
    $expected_all_revisions += [
        'dev' => [
            3,
            4,
            5,
            6,
            7,
            8,
            9,
        ],
    ];
    $expected_initial_revisions += [
        'dev' => [
            7,
            8,
        ],
    ];
    $this->assertWorkspaceAssociations($entity_type_id, $expected_latest_revisions, $expected_all_revisions, $expected_initial_revisions);
    // Merge 'dev' into 'stage' and check the workspace associations.
    
    /** @var \Drupal\workspaces\WorkspaceMergerInterface $workspace_merger */
    $workspace_merger = \Drupal::service('workspaces.operation_factory')->getMerger($this->workspaces['dev'], $this->workspaces['stage']);
    $workspace_merger->merge();
    // The latest revisions from 'dev' are now tracked in 'stage'.
    $expected_latest_revisions['stage'] = $expected_latest_revisions['dev'];
    // Two revisions (8 and 9) were created for 'Test article 6', but only the
    // latest one (9) is being merged into 'stage'.
    $expected_all_revisions['stage'] = [
        3,
        4,
        5,
        6,
        7,
        9,
    ];
    // Revision 7 was both an initial and latest revision in 'dev', so it is now
    // considered an initial revision in 'stage'.
    $expected_initial_revisions['stage'] = [
        4,
        5,
        7,
    ];
    // Which leaves revision 8 as the only remaining initial revision in 'dev'.
    $expected_initial_revisions['dev'] = [
        8,
    ];
    $this->assertWorkspaceAssociations($entity_type_id, $expected_latest_revisions, $expected_all_revisions, $expected_initial_revisions);
    // Publish 'stage' and check the workspace associations.
    
    /** @var \Drupal\workspaces\WorkspacePublisherInterface $workspace_publisher */
    $workspace_publisher = \Drupal::service('workspaces.operation_factory')->getPublisher($this->workspaces['stage']);
    $workspace_publisher->publish();
    $expected_revisions['stage'] = $expected_revisions['dev'] = [];
    $this->assertWorkspaceAssociations($entity_type_id, $expected_revisions, $expected_revisions, $expected_revisions);
}

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