class SortableviewsAccessTest in Sortableviews 8
@coversDefaultClass \Drupal\sortableviews\Access\SortableviewsAccess @group sortableviews
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\sortableviews\Unit\Access\SortableviewsAccessTest
Expanded class hierarchy of SortableviewsAccessTest
File
- tests/
src/ Unit/ Access/ SortableviewsAccessTest.php, line 15
Namespace
Drupal\Tests\sortableviews\Unit\AccessView source
class SortableviewsAccessTest extends UnitTestCase {
/**
* The instance of SortableviewsAccess to be tested.
*
* @var \Drupal\sortableviews\Access\SortableviewsAccess
*/
protected $sortableViewsAccess;
/**
* An object mocked from AccountInterface.
*
* @var \Drupal\Core\Session\AccountInterface
*/
protected $account;
/**
* {@inheritdoc}
*/
protected function setUp() {
$view_style = $this
->getMockBuilder('Drupal\\views\\Plugin\\views\\style\\StylePluginBase')
->disableOriginalConstructor()
->getMock();
$view_style->options['weight_field'] = 'some_field';
$entity_type = $this
->createMock('Drupal\\Component\\Plugin\\Definition\\PluginDefinitionInterface');
$entity_type
->expects($this
->any())
->method('id')
->willReturn('some_entity');
$view_executable = $this
->getMockBuilder('Drupal\\views\\ViewExecutable')
->disableOriginalConstructor()
->getMock();
$view_executable
->expects($this
->any())
->method('getBaseEntityType')
->willReturn($entity_type);
$view_executable
->expects($this
->any())
->method('getStyle')
->willReturn($view_style);
$view_entity = $this
->createMock('Drupal\\views\\ViewEntityInterface');
$executable_factory = $this
->getMockBuilder('Drupal\\views\\ViewExecutableFactory')
->disableOriginalConstructor()
->getMock();
$executable_factory
->expects($this
->any())
->method('get')
->with($view_entity)
->willReturn($view_executable);
$entities = [];
for ($aux = 1; $aux <= 2; $aux++) {
$entity = $this
->createMock('Drupal\\Core\\Access\\AccessibleInterface');
$entity
->expects($this
->any())
->method('access')
->willReturn(TRUE);
$entities[] = $entity;
}
$entity_storage = $this
->createMock('Drupal\\Core\\Entity\\EntityStorageInterface');
$entity_storage
->expects($this
->any())
->method('load')
->with('some_view')
->willReturn($view_entity);
$entity_storage
->expects($this
->any())
->method('loadMultiple')
->willReturn($entities);
$entity_manager = $this
->createMock('Drupal\\Core\\Entity\\EntityTypeManagerInterface');
$entity_manager
->expects($this
->any())
->method('getStorage')
->willReturn($entity_storage);
$this->sortableViewsAccess = new SortableviewsAccess($entity_manager, $executable_factory);
$this->account = $this
->createMock('Drupal\\Core\\Session\\AccountInterface');
}
/**
* Tests the access method.
*
* @covers ::access
*
* @dataProvider dataProvider
*/
public function testAccess(Request $request, $is_valid) {
$result = $this->sortableViewsAccess
->access($request, $this->account);
if ($is_valid) {
$this
->assertTrue($result instanceof AccessResultAllowed);
$this
->assertEquals($request
->get('entity_type'), 'some_entity');
$this
->assertEquals($request
->get('weight_field'), 'some_field');
}
else {
$this
->assertTrue($result instanceof AccessResultForbidden);
}
}
/**
* Provides test data for testAccess().
*/
public function dataProvider() {
$data = [];
$request = new Request();
$data[] = [
$request,
FALSE,
];
unset($request);
$request = new Request();
$request->attributes
->set('view_name', 'some_view');
$request->attributes
->set('display_name', uniqid());
$request->attributes
->set('current_order', [
1,
2,
]);
$data[] = [
$request,
TRUE,
];
unset($request);
return $data;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
PhpunitCompatibilityTrait:: |
public | function | Returns a mock object for the specified class using the available method. | |
PhpunitCompatibilityTrait:: |
public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
SortableviewsAccessTest:: |
protected | property | An object mocked from AccountInterface. | |
SortableviewsAccessTest:: |
protected | property | The instance of SortableviewsAccess to be tested. | |
SortableviewsAccessTest:: |
public | function | Provides test data for testAccess(). | |
SortableviewsAccessTest:: |
protected | function |
Overrides UnitTestCase:: |
|
SortableviewsAccessTest:: |
public | function | Tests the access method. | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. |