You are here

public function EntityLoadingTest::testLoadingEntities in Multiversion 8.2

Same name and namespace in other branches
  1. 8 tests/src/Kernel/EntityLoadingTest.php \Drupal\Tests\multiversion\Kernel\EntityLoadingTest::testLoadingEntities()

Tests loading entities.

File

tests/src/Kernel/EntityLoadingTest.php, line 61

Class

EntityLoadingTest
@group multiversion

Namespace

Drupal\Tests\multiversion\Kernel

Code

public function testLoadingEntities() {
  $admin = $this
    ->createUser([
    'administer nodes',
    'create workspace',
    'view any workspace',
    'edit any workspace',
    'delete any workspace',
  ]);
  $this
    ->setCurrentUser($admin);
  $storage = \Drupal::entityTypeManager()
    ->getStorage('node');

  /** @var \Drupal\workspaces\WorkspaceManagerInterface $workspace_manager */
  $workspace_manager = \Drupal::service('workspaces.manager');
  $un_workspace = Workspace::create([
    'id' => 'un_workspace',
    'label' => 'Un Workspace',
  ]);
  $un_workspace
    ->save();
  $workspace_manager
    ->setActiveWorkspace($un_workspace);
  $this
    ->assertEquals($un_workspace
    ->id(), $workspace_manager
    ->getActiveWorkspace()
    ->id());
  $node = $this
    ->createNode();
  $entities = $storage
    ->loadMultiple();
  $this
    ->assertEquals(1, count($entities));
  $this
    ->assertEquals($node
    ->id(), reset($entities)
    ->id());
  $results = $storage
    ->getQuery()
    ->execute();
  $this
    ->assertEquals(1, count($results));
  $this
    ->assertEquals($node
    ->id(), reset($results));
  $dau_workspace = Workspace::create([
    'id' => 'dau_workspace',
    'label' => 'Dau Workspace',
  ]);
  $dau_workspace
    ->save();
  $workspace_manager
    ->setActiveWorkspace($dau_workspace);
  $this
    ->assertEquals($dau_workspace
    ->id(), $workspace_manager
    ->getActiveWorkspace()
    ->id());
  $node2 = $this
    ->createNode();
  $entities = $storage
    ->loadMultiple();
  $this
    ->assertEquals(1, count($entities));
  $this
    ->assertEquals($node2
    ->id(), reset($entities)
    ->id());
  $results = $storage
    ->getQuery()
    ->execute();
  $this
    ->assertEquals(1, count($results));
  $this
    ->assertEquals($node2
    ->id(), reset($results));

  // Create one more entity on the second workspace.
  $node3 = $this
    ->createNode();
  $entities = $storage
    ->loadMultiple();
  $this
    ->assertEquals(2, count($entities));
  $this
    ->assertEquals($node2
    ->id(), $entities[$node2
    ->id()]
    ->id());
  $this
    ->assertEquals($node3
    ->id(), $entities[$node3
    ->id()]
    ->id());
  $results = $storage
    ->getQuery()
    ->execute();
  $this
    ->assertEquals(2, count($results));
  $ids = array_values($results);
  $this
    ->assertEquals($node2
    ->id(), $ids[0]);
  $this
    ->assertEquals($node3
    ->id(), $ids[1]);

  // Switch back to the first workspace and check if we still have the same
  // number of nodes associated with it.
  $workspace_manager
    ->setActiveWorkspace($un_workspace);
  $entities = $storage
    ->loadMultiple();
  $this
    ->assertEquals(1, count($entities));
  $this
    ->assertEquals($node
    ->id(), reset($entities)
    ->id());
  $results = $storage
    ->getQuery()
    ->execute();
  $this
    ->assertEquals(1, count($results));
  $this
    ->assertEquals($node
    ->id(), reset($results));
}