View source
<?php
namespace Drupal\Tests\views\Unit\Plugin\argument_validator;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Tests\UnitTestCase;
use Drupal\views\Plugin\views\argument_validator\Entity;
class EntityTest extends UnitTestCase {
protected $executable;
protected $display;
protected $entityTypeManager;
protected $entityTypeBundleInfo;
protected $argumentValidator;
protected function setUp() {
parent::setUp();
$this->entityTypeManager = $this
->createMock(EntityTypeManagerInterface::class);
$this->entityTypeBundleInfo = $this
->createMock(EntityTypeBundleInfoInterface::class);
$mock_entity = $this
->getMockForAbstractClass('Drupal\\Core\\Entity\\EntityBase', [], '', FALSE, TRUE, TRUE, [
'bundle',
'access',
]);
$mock_entity
->expects($this
->any())
->method('bundle')
->will($this
->returnValue('test_bundle'));
$mock_entity
->expects($this
->any())
->method('access')
->will($this
->returnValueMap([
[
'test_op',
NULL,
FALSE,
TRUE,
],
[
'test_op_2',
NULL,
FALSE,
FALSE,
],
[
'test_op_3',
NULL,
FALSE,
TRUE,
],
]));
$mock_entity_bundle_2 = $this
->getMockForAbstractClass('Drupal\\Core\\Entity\\EntityBase', [], '', FALSE, TRUE, TRUE, [
'bundle',
'access',
]);
$mock_entity_bundle_2
->expects($this
->any())
->method('bundle')
->will($this
->returnValue('test_bundle_2'));
$mock_entity_bundle_2
->expects($this
->any())
->method('access')
->will($this
->returnValueMap([
[
'test_op',
NULL,
FALSE,
FALSE,
],
[
'test_op_2',
NULL,
FALSE,
FALSE,
],
[
'test_op_3',
NULL,
FALSE,
TRUE,
],
]));
$storage = $this
->createMock('Drupal\\Core\\Entity\\EntityStorageInterface');
$value_map = [
[
[],
[],
],
[
[
1,
],
[
1 => $mock_entity,
],
],
[
[
'1',
],
[
1 => $mock_entity,
],
],
[
[
1,
2,
],
[
1 => $mock_entity,
2 => $mock_entity_bundle_2,
],
],
[
[
'1',
'2',
],
[
1 => $mock_entity,
2 => $mock_entity_bundle_2,
],
],
[
[
2,
],
[
2 => $mock_entity_bundle_2,
],
],
[
[
'2',
],
[
2 => $mock_entity_bundle_2,
],
],
];
$storage
->expects($this
->any())
->method('loadMultiple')
->will($this
->returnValueMap($value_map));
$this->entityTypeManager
->expects($this
->any())
->method('getStorage')
->with('entity_test')
->will($this
->returnValue($storage));
$this->executable = $this
->getMockBuilder('Drupal\\views\\ViewExecutable')
->disableOriginalConstructor()
->getMock();
$this->display = $this
->getMockBuilder('Drupal\\views\\Plugin\\views\\display\\DisplayPluginBase')
->disableOriginalConstructor()
->getMock();
$definition = [
'entity_type' => 'entity_test',
];
$this->argumentValidator = new Entity([], 'entity_test', $definition, $this->entityTypeManager, $this->entityTypeBundleInfo);
}
public function testValidateArgumentNoAccess() {
$options = [];
$options['access'] = FALSE;
$options['bundles'] = [];
$this->argumentValidator
->init($this->executable, $this->display, $options);
$this
->assertFalse($this->argumentValidator
->validateArgument(3));
$this
->assertFalse($this->argumentValidator
->validateArgument(''));
$this
->assertTrue($this->argumentValidator
->validateArgument(1));
$this
->assertTrue($this->argumentValidator
->validateArgument(2));
$this
->assertFalse($this->argumentValidator
->validateArgument('1,2'));
}
public function testValidateArgumentAccess() {
$options = [];
$options['access'] = TRUE;
$options['bundles'] = [];
$options['operation'] = 'test_op';
$this->argumentValidator
->init($this->executable, $this->display, $options);
$this
->assertFalse($this->argumentValidator
->validateArgument(3));
$this
->assertFalse($this->argumentValidator
->validateArgument(''));
$this
->assertTrue($this->argumentValidator
->validateArgument(1));
$options = [];
$options['access'] = TRUE;
$options['bundles'] = [];
$options['operation'] = 'test_op_2';
$this->argumentValidator
->init($this->executable, $this->display, $options);
$this
->assertFalse($this->argumentValidator
->validateArgument(3));
$this
->assertFalse($this->argumentValidator
->validateArgument(''));
$this
->assertFalse($this->argumentValidator
->validateArgument(1));
$this
->assertFalse($this->argumentValidator
->validateArgument(2));
}
public function testValidateArgumentBundle() {
$options = [];
$options['access'] = FALSE;
$options['bundles'] = [
'test_bundle' => 1,
];
$this->argumentValidator
->init($this->executable, $this->display, $options);
$this
->assertTrue($this->argumentValidator
->validateArgument(1));
$this
->assertFalse($this->argumentValidator
->validateArgument(2));
$options['bundles'] = NULL;
$this->argumentValidator
->init($this->executable, $this->display, $options);
$this
->assertTrue($this->argumentValidator
->validateArgument(1));
$this
->assertTrue($this->argumentValidator
->validateArgument(2));
}
public function testCalculateDependencies() {
$entity_type_manager = $this
->createMock(EntityTypeManagerInterface::class);
$storage = $this
->createMock('Drupal\\Core\\Entity\\EntityStorageInterface');
$entity_type = $this
->createMock('Drupal\\Core\\Entity\\EntityTypeInterface');
$mock_entity = $this
->createMock('Drupal\\Core\\Entity\\EntityInterface');
$mock_entity
->expects($this
->any())
->method('getConfigDependencyKey')
->willReturn('config');
$mock_entity
->expects($this
->any())
->method('getConfigDependencyName')
->willReturn('test_bundle');
$storage
->expects($this
->any())
->method('loadMultiple')
->with([
'test_bundle',
])
->willReturn([
'test_bundle' => $mock_entity,
]);
$entity_type
->expects($this
->any())
->method('getBundleEntityType')
->willReturn('entity_test_bundle');
$entity_type_manager
->expects($this
->any())
->method('getDefinition')
->with('entity_test')
->willReturn($entity_type);
$entity_type_manager
->expects($this
->any())
->method('hasHandler')
->with('entity_test_bundle', 'storage')
->willReturn(TRUE);
$entity_type_manager
->expects($this
->any())
->method('getStorage')
->with('entity_test_bundle')
->willReturn($storage);
$argumentValidator = new Entity([], 'entity_test', [
'entity_type' => 'entity_test',
], $entity_type_manager, $this->entityTypeBundleInfo);
$options = [];
$options['access'] = FALSE;
$options['bundles'] = [
'test_bundle' => 1,
];
$argumentValidator
->init($this->executable, $this->display, $options);
$this
->assertEquals([
'config' => [
'test_bundle',
],
], $argumentValidator
->calculateDependencies());
}
public function testValidateArgumentMultiple() {
$options = [];
$options['access'] = TRUE;
$options['bundles'] = [];
$options['operation'] = 'test_op';
$options['multiple'] = TRUE;
$this->argumentValidator
->init($this->executable, $this->display, $options);
$this
->assertTrue($this->argumentValidator
->validateArgument('1'));
$this
->assertFalse($this->argumentValidator
->validateArgument('2'));
$this
->assertFalse($this->argumentValidator
->validateArgument('1,2'));
$this
->assertFalse($this->argumentValidator
->validateArgument('1+2'));
$options = [];
$options['access'] = TRUE;
$options['bundles'] = [];
$options['operation'] = 'test_op_3';
$options['multiple'] = TRUE;
$this->argumentValidator
->init($this->executable, $this->display, $options);
$this
->assertTrue($this->argumentValidator
->validateArgument('1,2'));
$this
->assertTrue($this->argumentValidator
->validateArgument('1+2'));
}
}