You are here

protected function WorkspaceIntegrationTest::assertWorkspaceStatus in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/workspaces/tests/src/Kernel/WorkspaceIntegrationTest.php \Drupal\Tests\workspaces\Kernel\WorkspaceIntegrationTest::assertWorkspaceStatus()
  2. 10 core/modules/workspaces/tests/src/Kernel/WorkspaceIntegrationTest.php \Drupal\Tests\workspaces\Kernel\WorkspaceIntegrationTest::assertWorkspaceStatus()

Checks entity load, entity queries and views results for a test scenario.

Parameters

array $expected: An array of expected values, as defined in ::testWorkspaces().

string $entity_type_id: The ID of the entity type that is being tested.

1 call to WorkspaceIntegrationTest::assertWorkspaceStatus()
WorkspaceIntegrationTest::testWorkspaces in core/modules/workspaces/tests/src/Kernel/WorkspaceIntegrationTest.php
Tests various scenarios for creating and publishing content in workspaces.

File

core/modules/workspaces/tests/src/Kernel/WorkspaceIntegrationTest.php, line 757

Class

WorkspaceIntegrationTest
Tests a complete publishing scenario across different workspaces.

Namespace

Drupal\Tests\workspaces\Kernel

Code

protected function assertWorkspaceStatus(array $expected, $entity_type_id) {
  $expected = $this
    ->flattenExpectedValues($expected, $entity_type_id);
  $entity_keys = $this->entityTypeManager
    ->getDefinition($entity_type_id)
    ->getKeys();
  foreach ($expected as $workspace_id => $expected_values) {
    $this
      ->switchToWorkspace($workspace_id);

    // Check that default revisions are swapped with the workspace revision.
    $this
      ->assertEntityLoad($expected_values, $entity_type_id);

    // Check that non-default revisions are not changed.
    $this
      ->assertEntityRevisionLoad($expected_values, $entity_type_id);

    // Check that entity queries return the correct results.
    $this
      ->assertEntityQuery($expected_values, $entity_type_id);

    // Check that the 'Frontpage' view only shows published content that is
    // also considered as the default revision in the given workspace.
    $expected_frontpage = array_filter($expected_values, function ($expected_value) {
      return $expected_value['status'] === TRUE && $expected_value['default_revision'] === TRUE;
    });

    // The 'Frontpage' view will output nodes in reverse creation order.
    usort($expected_frontpage, function ($a, $b) {
      return $b['nid'] - $a['nid'];
    });
    $view = Views::getView('frontpage');
    $view
      ->execute();
    $this
      ->assertIdenticalResultset($view, $expected_frontpage, [
      'nid' => 'nid',
    ]);
    $rendered_view = $view
      ->render('page_1');
    $output = \Drupal::service('renderer')
      ->renderRoot($rendered_view);
    $this
      ->setRawContent($output);
    foreach ($expected_values as $expected_entity_values) {
      if ($expected_entity_values[$entity_keys['published']] === TRUE && $expected_entity_values['default_revision'] === TRUE) {
        $this
          ->assertRaw($expected_entity_values[$entity_keys['label']]);
      }
      elseif ($workspace_id != 'stage' && $expected_entity_values[$entity_keys['id']] != 4) {
        $this
          ->assertNoRaw($expected_entity_values[$entity_keys['label']]);
      }
    }

    // Add a filter on a field that is stored in a dedicated table in order to
    // test field joins with extra conditions (e.g. 'deleted' and 'langcode').
    $view
      ->destroy();
    $view
      ->setDisplay('page_1');
    $filters = $view->displayHandlers
      ->get('page_1')
      ->getOption('filters');
    $view->displayHandlers
      ->get('page_1')
      ->overrideOption('filters', $filters + [
      'body_value' => [
        'id' => 'body_value',
        'table' => 'node__body',
        'field' => 'body_value',
        'operator' => 'not empty',
        'plugin_id' => 'string',
      ],
    ]);
    $view
      ->execute();
    $this
      ->assertIdenticalResultset($view, $expected_frontpage, [
      'nid' => 'nid',
    ]);
  }
}