You are here

public function WorkspaceManagerTest::testGetActiveWorkspace in Multiversion 8

Tests that getActiveWorkspace() gets from the negotiator.

File

tests/src/Unit/WorkspaceManagerTest.php, line 232

Class

WorkspaceManagerTest
@coversDefaultClass \Drupal\multiversion\Workspace\WorkspaceManager @group multiversion

Namespace

Drupal\Tests\multiversion\Unit

Code

public function testGetActiveWorkspace() {
  $workspace_id = '123';

  // Create the request we will use.
  $request = $this
    ->getMock('Symfony\\Component\\HttpFoundation\\Request');
  $this->requestStack
    ->method('getCurrentRequest')
    ->willReturn($request);

  // Create the workspace that we will get.
  $workspace = $this
    ->getMockBuilder('Drupal\\multiversion\\Entity\\Workspace')
    ->disableOriginalConstructor()
    ->getMock();

  // Create the negotiator and stub the applies and getWorkspaceId methods.
  $negotiator = $this
    ->getMock('Drupal\\multiversion\\Workspace\\DefaultWorkspaceNegotiator');
  $negotiator
    ->method('applies')
    ->willReturn(TRUE);
  $negotiator
    ->method('getWorkspaceId')
    ->willReturn($workspace_id);

  // Create the storage and stub the load method.
  $storage = $this
    ->getMock('Drupal\\Core\\Entity\\EntityStorageInterface');
  $storage
    ->method('load')
    ->with($workspace_id)
    ->willReturn($workspace);

  // Stub the entity manager to return $storage.
  $this->entityTypeManager
    ->method('getStorage')
    ->with($this->entityTypeId)
    ->willReturn($storage);

  // Create the workspace manager with the negotiator.
  $workspace_manager = new WorkspaceManager($this->requestStack, $this->entityTypeManager, $this->currentUser, $this->logger);
  $workspace_manager
    ->addNegotiator($negotiator, 1);

  // Execute the code under test.
  $active_workspace = $workspace_manager
    ->getActiveWorkspace();

  // Ensure value is the workspace we stubbed.
  $this
    ->assertSame($workspace, $active_workspace);
}