View source
<?php
namespace Drupal\Tests\multiversion\Unit;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Tests\UnitTestCase;
use Drupal\multiversion\Workspace\WorkspaceManager;
use Prophecy\Argument;
class WorkspaceManagerTest extends UnitTestCase {
protected $entities;
protected $values;
protected $container;
protected $entityTypeManager;
protected $requestStack;
protected $entityTypeId;
protected $entityType;
protected $workspaceNegotiators;
protected $currentUser;
protected $logger;
protected $block_manager;
protected function setUp() {
parent::setUp();
$this->entityTypeId = 'workspace';
$first_machine_name = $this
->randomMachineName();
$second_machine_name = $this
->randomMachineName();
$this->values = [
[
'machine_name' => $first_machine_name,
],
[
'machine_name' => $second_machine_name,
],
];
$this->entityType = $this
->getMock('Drupal\\multiversion\\Entity\\WorkspaceInterface');
$this->entityTypeManager = $this
->getMock('Drupal\\Core\\Entity\\EntityTypeManagerInterface');
$this->currentUser = $this
->getMock('Drupal\\Core\\Session\\AccountProxyInterface');
$this->logger = $this
->getMock('Psr\\Log\\LoggerInterface');
$this->block_manager = $this
->getMockBuilder('Drupal\\Core\\Block\\BlockManager')
->disableOriginalConstructor()
->getMock();
$this->entityTypeManager
->expects($this
->any())
->method('getDefinition')
->with($this->entityTypeId)
->will($this
->returnValue($this->entityType));
$this->requestStack = $this
->getMock('Symfony\\Component\\HttpFoundation\\RequestStack');
$container = new ContainerBuilder();
$container
->set('entity.manager', $this->entityTypeManager);
$container
->set('request_stack', $this->requestStack);
$container
->setParameter('workspace.default', 1);
\Drupal::setContainer($container);
foreach ($this->values as $value) {
$entity = $this
->getMockBuilder('Drupal\\multiversion\\Entity\\Workspace')
->disableOriginalConstructor()
->getMock();
$entity
->expects($this
->any())
->method('create')
->with($value)
->will($this
->returnValue($this->entityType));
$this->entities[] = $entity;
}
$this->workspaceNegotiators[] = [
$this
->getMock('Drupal\\multiversion\\Workspace\\DefaultWorkspaceNegotiator'),
];
$session_workspace_negotiator = $this
->getMockBuilder('Drupal\\multiversion\\Workspace\\SessionWorkspaceNegotiator')
->disableOriginalConstructor()
->getMock();
$this->workspaceNegotiators[] = [
$session_workspace_negotiator,
];
}
public function testAddNegotiator() {
$workspace_manager = new WorkspaceManager($this->requestStack, $this->entityTypeManager, $this->currentUser, $this->logger);
$workspace_manager
->addNegotiator($this->workspaceNegotiators[0][0], 0);
$workspace_manager
->addNegotiator($this->workspaceNegotiators[1][0], 1);
$property = new \ReflectionProperty('Drupal\\multiversion\\Workspace\\WorkspaceManager', 'negotiators');
$property
->setAccessible(TRUE);
$this
->assertSame($this->workspaceNegotiators, $property
->getValue($workspace_manager));
}
public function testLoad() {
$storage = $this
->getMock('Drupal\\Core\\Entity\\EntityStorageInterface');
$storage
->expects($this
->once())
->method('load')
->with(1)
->will($this
->returnValue($this->entities[0]));
$this->entityTypeManager
->expects($this
->once())
->method('getStorage')
->with($this->entityTypeId)
->will($this
->returnValue($storage));
$workspace_manager = new WorkspaceManager($this->requestStack, $this->entityTypeManager, $this->currentUser, $this->logger);
$entity = $workspace_manager
->load(1);
$this
->assertSame($this->entities[0], $entity);
}
public function testLoadMultiple() {
$ids = [
1,
2,
];
$storage = $this
->getMock('Drupal\\Core\\Entity\\EntityStorageInterface');
$storage
->expects($this
->once())
->method('loadMultiple')
->with($ids)
->will($this
->returnValue($this->entities));
$this->entityTypeManager
->expects($this
->once())
->method('getStorage')
->with($this->entityTypeId)
->will($this
->returnValue($storage));
$workspace_manager = new WorkspaceManager($this->requestStack, $this->entityTypeManager, $this->currentUser, $this->logger);
$entities = $workspace_manager
->loadMultiple($ids);
$this
->assertSame($this->entities, $entities);
}
public function testSetActiveWorkspace() {
$request = $this
->getMock('Symfony\\Component\\HttpFoundation\\Request');
$this->requestStack
->method('getCurrentRequest')
->willReturn($request);
$workspace = $this
->getMockBuilder('Drupal\\multiversion\\Entity\\Workspace')
->disableOriginalConstructor()
->getMock();
$workspace
->expects($this
->any())
->method("isPublished")
->willReturn(TRUE);
$workspace
->expects($this
->any())
->method("access")
->willReturn(TRUE);
$negotiator = $this
->prophesize('Drupal\\multiversion\\Workspace\\DefaultWorkspaceNegotiator');
$negotiator
->applies(Argument::any())
->willReturn(TRUE);
$negotiator
->persist(Argument::any())
->will(function () {
return $this;
});
$workspace_manager = new WorkspaceManager($this->requestStack, $this->entityTypeManager, $this->currentUser, $this->logger);
$workspace_manager
->addNegotiator($negotiator
->reveal(), 1);
$workspace_manager
->setActiveWorkspace($workspace);
$negotiator
->persist($workspace)
->shouldHaveBeenCalled();
}
public function testGetActiveWorkspace() {
$workspace_id = '123';
$request = $this
->getMock('Symfony\\Component\\HttpFoundation\\Request');
$this->requestStack
->method('getCurrentRequest')
->willReturn($request);
$workspace = $this
->getMockBuilder('Drupal\\multiversion\\Entity\\Workspace')
->disableOriginalConstructor()
->getMock();
$negotiator = $this
->getMock('Drupal\\multiversion\\Workspace\\DefaultWorkspaceNegotiator');
$negotiator
->method('applies')
->willReturn(TRUE);
$negotiator
->method('getWorkspaceId')
->willReturn($workspace_id);
$storage = $this
->getMock('Drupal\\Core\\Entity\\EntityStorageInterface');
$storage
->method('load')
->with($workspace_id)
->willReturn($workspace);
$this->entityTypeManager
->method('getStorage')
->with($this->entityTypeId)
->willReturn($storage);
$workspace_manager = new WorkspaceManager($this->requestStack, $this->entityTypeManager, $this->currentUser, $this->logger);
$workspace_manager
->addNegotiator($negotiator, 1);
$active_workspace = $workspace_manager
->getActiveWorkspace();
$this
->assertSame($workspace, $active_workspace);
}
public function testGetSortedNegotiators() {
$workspace_manager = new WorkspaceManager($this->requestStack, $this->entityTypeManager, $this->currentUser, $this->logger);
$workspace_manager
->addNegotiator($this->workspaceNegotiators[0][0], 1);
$workspace_manager
->addNegotiator($this->workspaceNegotiators[1][0], 3);
$method = new \ReflectionMethod('Drupal\\multiversion\\Workspace\\WorkspaceManager', 'getSortedNegotiators');
$method
->setAccessible(TRUE);
$sorted_negotiators = new \ReflectionProperty('Drupal\\multiversion\\Workspace\\WorkspaceManager', 'sortedNegotiators');
$sorted_negotiators
->setAccessible(TRUE);
$sorted_negotiators_value = $sorted_negotiators
->getValue($workspace_manager);
$negotiators = new \ReflectionProperty('Drupal\\multiversion\\Workspace\\WorkspaceManager', 'negotiators');
$negotiators
->setAccessible(TRUE);
$negotiators_value = $negotiators
->getValue($workspace_manager);
if (!isset($sorted_negotiators_value)) {
krsort($negotiators_value);
$sorted_negotiators_value = [];
foreach ($negotiators_value as $builders) {
$sorted_negotiators_value = array_merge($sorted_negotiators_value, $builders);
}
}
$this
->assertSame($sorted_negotiators_value, $method
->invoke($workspace_manager));
}
}