Newer
Older
catch
committed
<?php
/**
* @file
* Contains \Drupal\Tests\Core\Entity\EntityListBuilderTest.
catch
committed
*/
namespace Drupal\Tests\Core\Entity;
catch
committed
use Drupal\Core\Access\AccessResult;
use Drupal\Core\DependencyInjection\ContainerBuilder;
Alex Pott
committed
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityListBuilder;
Lee Rowlands
committed
use Drupal\Core\Routing\RedirectDestinationInterface;
use Drupal\entity_test\EntityTestListBuilder;
catch
committed
use Drupal\Tests\UnitTestCase;
/**
* @coversDefaultClass \Drupal\entity_test\EntityTestListBuilder
catch
committed
* @group Entity
*/
class EntityListBuilderTest extends UnitTestCase {
catch
committed
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/**
* The entity type used for testing.
*
* @var \Drupal\Core\Entity\EntityTypeInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $entityType;
/**
* The module handler used for testing.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $moduleHandler;
/**
* The translation manager used for testing.
*
* @var \Drupal\Core\StringTranslation\TranslationInterface
*/
protected $translationManager;
/**
* The role storage used for testing.
*
* @var \Drupal\user\RoleStorageInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $roleStorage;
/**
* The service container used for testing.
*
* @var \Drupal\Core\DependencyInjection\ContainerBuilder
*/
protected $container;
catch
committed
/**
* The entity used to construct the EntityListBuilder.
catch
committed
*
Alex Pott
committed
* @var \Drupal\user\RoleInterface|\PHPUnit_Framework_MockObject_MockObject
catch
committed
*/
protected $role;
Lee Rowlands
committed
/**
* The redirect destination service.
*
* @var \Drupal\Core\Routing\RedirectDestinationInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $redirectDestination;
catch
committed
/**
* The EntityListBuilder object to test.
catch
committed
*
* @var \Drupal\Core\Entity\EntityListBuilder
catch
committed
*/
protected $entityListBuilder;
catch
committed
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
Alex Pott
committed
$this->role = $this->getMock('Drupal\user\RoleInterface');
$this->roleStorage = $this->getMock('\Drupal\user\RoleStorageInterface');
$this->moduleHandler = $this->getMock('\Drupal\Core\Extension\ModuleHandlerInterface');
$this->entityType = $this->getMock('\Drupal\Core\Entity\EntityTypeInterface');
$this->translationManager = $this->getMock('\Drupal\Core\StringTranslation\TranslationInterface');
Lee Rowlands
committed
$this->entityListBuilder = new TestEntityListBuilder($this->entityType, $this->roleStorage);
$this->redirectDestination = $this->getMock(RedirectDestinationInterface::class);
$this->container = new ContainerBuilder();
\Drupal::setContainer($this->container);
}
/**
* @covers ::getOperations
*/
public function testGetOperations() {
Alex Pott
committed
$operation_name = $this->randomMachineName();
$operations = [
$operation_name => [
Alex Pott
committed
'title' => $this->randomMachineName(),
],
];
$this->moduleHandler->expects($this->once())
->method('invokeAll')
->with('entity_operation', [$this->role])
->will($this->returnValue($operations));
$this->moduleHandler->expects($this->once())
->method('alter')
->with('entity_operation');
$this->container->set('module_handler', $this->moduleHandler);
$this->role->expects($this->any())
->method('access')
catch
committed
->will($this->returnValue(AccessResult::allowed()));
$this->role->expects($this->any())
->method('hasLinkTemplate')
->will($this->returnValue(TRUE));
$url = $this->getMockBuilder('\Drupal\Core\Url')
->disableOriginalConstructor()
->getMock();
Lee Rowlands
committed
$url->expects($this->atLeastOnce())
->method('mergeOptions')
->with(['query' => ['destination' => '/foo/bar']]);
$this->role->expects($this->any())
Lee Rowlands
committed
->method('toUrl')
->will($this->returnValue($url));
Lee Rowlands
committed
$this->redirectDestination->expects($this->atLeastOnce())
->method('getAsArray')
->willReturn(['destination' => '/foo/bar']);
$list = new EntityListBuilder($this->entityType, $this->roleStorage);
Alex Pott
committed
$list->setStringTranslation($this->translationManager);
Lee Rowlands
committed
$list->setRedirectDestination($this->redirectDestination);
$operations = $list->getOperations($this->role);
$this->assertInternalType('array', $operations);
$this->assertArrayHasKey('edit', $operations);
$this->assertInternalType('array', $operations['edit']);
$this->assertArrayHasKey('title', $operations['edit']);
$this->assertArrayHasKey('delete', $operations);
$this->assertInternalType('array', $operations['delete']);
$this->assertArrayHasKey('title', $operations['delete']);
$this->assertArrayHasKey($operation_name, $operations);
$this->assertInternalType('array', $operations[$operation_name]);
$this->assertArrayHasKey('title', $operations[$operation_name]);
catch
committed
}
}
class TestEntityListBuilder extends EntityTestListBuilder {
Alex Pott
committed
public function buildOperations(EntityInterface $entity) {
return [];
Alex Pott
committed
}