function WorkspaceEntityOperationsTest::testEntityCreation

Test published entity creation in a workspace.

File

core/modules/workspaces/tests/src/Kernel/WorkspaceEntityOperationsTest.php, line 70

Class

WorkspaceEntityOperationsTest
Tests entity operations with workspaces.

Namespace

Drupal\Tests\workspaces\Kernel

Code

public function testEntityCreation() : void {
  $this->switchToWorkspace('stage');
  // Create a published entity in the workspace.
  $entity = EntityTestMulRevPub::create([
    'name' => 'Test published entity in workspace',
    'status' => TRUE,
    'revision_test_field' => $this->randomString(),
  ]);
  $entity->save();
  // Get the revision sequence that was tracked during entity saves.
  $sequence_key = 'entity_test_mulrevpub.' . $entity->uuid() . '.revision_sequence';
  $revision_sequence = \Drupal::keyValue('ws_test')->get($sequence_key, []);
  $field_sequence_key = 'entity_test_mulrevpub.' . $entity->uuid() . '.field_revision_sequence';
  $field_revision_sequence = \Drupal::keyValue('ws_test')->get($field_sequence_key, []);
  // We expect exactly 2 presave calls when a published entity is created in a
  // workspace:
  // 1. First save: unpublished default revision.
  // 2. Second save: published pending revision.
  $this->assertCount(2, $revision_sequence);
  $this->assertCount(2, $field_revision_sequence);
  // Verify the is_new_revision status.
  $this->assertTrue($revision_sequence[0]['is_new_revision']);
  $this->assertTrue($revision_sequence[1]['is_new_revision']);
  $this->assertTrue($field_revision_sequence[0]['is_new_revision']);
  $this->assertTrue($field_revision_sequence[1]['is_new_revision']);
  // Verify the is_new status.
  $this->assertTrue($revision_sequence[0]['is_new']);
  $this->assertFalse($revision_sequence[1]['is_new']);
  $this->assertTrue($field_revision_sequence[0]['is_new']);
  $this->assertFalse($field_revision_sequence[1]['is_new']);
  // Verify the publishing status of each revision.
  $this->assertFalse($revision_sequence[0]['is_published']);
  $this->assertTrue($revision_sequence[1]['is_published']);
  // The entity presave hook is fired after the field's presave()
  // implementation, so at this point the first revision is still published.
  $this->assertTrue($field_revision_sequence[0]['is_published']);
  $this->assertTrue($field_revision_sequence[1]['is_published']);
  // Verify the default revision status.
  $this->assertTrue($revision_sequence[0]['is_default_revision']);
  $this->assertFalse($revision_sequence[1]['is_default_revision']);
  $this->assertTrue($field_revision_sequence[0]['is_default_revision']);
  $this->assertFalse($field_revision_sequence[1]['is_default_revision']);
}

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