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

File

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

Class

WorkspaceAssociationTest
Tests workspace associations.

Namespace

Drupal\Tests\workspaces\Kernel

Code

public function testWorkspaceAssociation() : void {
    $this->createNode([
        'title' => 'Test article 1 - live - unpublished',
        'type' => 'article',
        'status' => 0,
    ]);
    $this->createNode([
        'title' => 'Test article 2 - live - published',
        'type' => 'article',
    ]);
    // Edit one of the existing nodes in 'stage'.
    $this->switchToWorkspace('stage');
    $node = $this->entityTypeManager
        ->getStorage('node')
        ->load(1);
    $node->setTitle('Test article 1 - stage - published');
    $node->setPublished();
    // This creates rev. 3.
    $node->save();
    // Generate content with the following structure:
    // Stage:
    // - Test article 3 - stage - unpublished (rev. 4)
    // - Test article 4 - stage - published (rev. 5 and 6)
    $this->createNode([
        'title' => 'Test article 3 - stage - unpublished',
        'type' => 'article',
        'status' => 0,
    ]);
    $this->createNode([
        'title' => 'Test article 4 - stage - published',
        'type' => 'article',
    ]);
    $expected_latest_revisions = [
        'stage' => [
            3,
            4,
            6,
        ],
    ];
    $expected_all_revisions = [
        'stage' => [
            3,
            4,
            5,
            6,
        ],
    ];
    $expected_initial_revisions = [
        'stage' => [
            4,
            5,
        ],
    ];
    $this->assertWorkspaceAssociations('node', $expected_latest_revisions, $expected_all_revisions, $expected_initial_revisions);
    // Dev:
    // - Test article 1 - stage - published (rev. 3)
    // - Test article 3 - stage - unpublished (rev. 4)
    // - Test article 4 - stage - published (rev. 5 and 6)
    // - Test article 5 - dev - unpublished (rev. 7)
    // - Test article 6 - dev - published (rev. 8 and 9)
    $this->switchToWorkspace('dev');
    $this->createNode([
        'title' => 'Test article 5 - dev - unpublished',
        'type' => 'article',
        'status' => 0,
    ]);
    $this->createNode([
        'title' => 'Test article 6 - dev - published',
        'type' => 'article',
    ]);
    $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('node', $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('node', $expected_latest_revisions, $expected_all_revisions, $expected_initial_revisions);
}

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