3 namespace Drupal\Tests\views\Unit\Plugin\argument_validator;
5 use Drupal\Tests\UnitTestCase;
6 use Drupal\views\Plugin\views\argument_validator\Entity;
9 * @coversDefaultClass \Drupal\views\Plugin\views\argument_validator\Entity
12 class EntityTest extends UnitTestCase {
15 * The view executable.
17 * @var \Drupal\views\ViewExecutable
19 protected $executable;
24 * @var \Drupal\views\Plugin\views\display\DisplayPluginBase
31 * @var \PHPUnit_Framework_MockObject_MockObject|\Drupal\Core\Entity\EntityManagerInterface
33 protected $entityManager;
36 * The tested argument validator.
38 * @var \Drupal\views\Plugin\views\argument_validator\Entity
40 protected $argumentValidator;
45 protected function setUp() {
48 $this->entityManager = $this->getMock('Drupal\Core\Entity\EntityManagerInterface');
50 $mock_entity = $this->getMockForAbstractClass('Drupal\Core\Entity\Entity', [], '', FALSE, TRUE, TRUE, ['bundle', 'access']);
51 $mock_entity->expects($this->any())
53 ->will($this->returnValue('test_bundle'));
54 $mock_entity->expects($this->any())
56 ->will($this->returnValueMap([
57 ['test_op', NULL, FALSE, TRUE],
58 ['test_op_2', NULL, FALSE, FALSE],
59 ['test_op_3', NULL, FALSE, TRUE],
62 $mock_entity_bundle_2 = $this->getMockForAbstractClass('Drupal\Core\Entity\Entity', [], '', FALSE, TRUE, TRUE, ['bundle', 'access']);
63 $mock_entity_bundle_2->expects($this->any())
65 ->will($this->returnValue('test_bundle_2'));
66 $mock_entity_bundle_2->expects($this->any())
68 ->will($this->returnValueMap([
69 ['test_op', NULL, FALSE, FALSE],
70 ['test_op_2', NULL, FALSE, FALSE],
71 ['test_op_3', NULL, FALSE, TRUE],
74 $storage = $this->getMock('Drupal\Core\Entity\EntityStorageInterface');
76 // Setup values for IDs passed as strings or numbers.
79 [[1], [1 => $mock_entity]],
80 [['1'], [1 => $mock_entity]],
81 [[1, 2], [1 => $mock_entity, 2 => $mock_entity_bundle_2]],
82 [['1', '2'], [1 => $mock_entity, 2 => $mock_entity_bundle_2]],
83 [[2], [2 => $mock_entity_bundle_2]],
84 [['2'], [2 => $mock_entity_bundle_2]],
86 $storage->expects($this->any())
87 ->method('loadMultiple')
88 ->will($this->returnValueMap($value_map));
90 $this->entityManager->expects($this->any())
91 ->method('getStorage')
93 ->will($this->returnValue($storage));
95 $this->executable = $this->getMockBuilder('Drupal\views\ViewExecutable')
96 ->disableOriginalConstructor()
98 $this->display = $this->getMockBuilder('Drupal\views\Plugin\views\display\DisplayPluginBase')
99 ->disableOriginalConstructor()
103 'entity_type' => 'entity_test',
106 $this->argumentValidator = new Entity([], 'entity_test', $definition, $this->entityManager);
110 * Tests the validate argument method with no access and bundles.
112 * @see \Drupal\views\Plugin\views\argument_validator\Entity::validateArgument()
114 public function testValidateArgumentNoAccess() {
116 $options['access'] = FALSE;
117 $options['bundles'] = [];
118 $this->argumentValidator->init($this->executable, $this->display, $options);
120 $this->assertFalse($this->argumentValidator->validateArgument(3));
121 $this->assertFalse($this->argumentValidator->validateArgument(''));
123 $this->assertTrue($this->argumentValidator->validateArgument(1));
124 $this->assertTrue($this->argumentValidator->validateArgument(2));
125 $this->assertFalse($this->argumentValidator->validateArgument('1,2'));
129 * Tests the validate argument method with access and no bundles.
131 * @see \Drupal\views\Plugin\views\argument_validator\Entity::validateArgument()
133 public function testValidateArgumentAccess() {
135 $options['access'] = TRUE;
136 $options['bundles'] = [];
137 $options['operation'] = 'test_op';
138 $this->argumentValidator->init($this->executable, $this->display, $options);
140 $this->assertFalse($this->argumentValidator->validateArgument(3));
141 $this->assertFalse($this->argumentValidator->validateArgument(''));
143 $this->assertTrue($this->argumentValidator->validateArgument(1));
146 $options['access'] = TRUE;
147 $options['bundles'] = [];
148 $options['operation'] = 'test_op_2';
149 $this->argumentValidator->init($this->executable, $this->display, $options);
151 $this->assertFalse($this->argumentValidator->validateArgument(3));
152 $this->assertFalse($this->argumentValidator->validateArgument(''));
154 $this->assertFalse($this->argumentValidator->validateArgument(1));
155 $this->assertFalse($this->argumentValidator->validateArgument(2));
159 * Tests the validate argument method with bundle checking.
161 public function testValidateArgumentBundle() {
163 $options['access'] = FALSE;
164 $options['bundles'] = ['test_bundle' => 1];
165 $this->argumentValidator->init($this->executable, $this->display, $options);
167 $this->assertTrue($this->argumentValidator->validateArgument(1));
168 $this->assertFalse($this->argumentValidator->validateArgument(2));
172 * @covers ::calculateDependencies
174 public function testCalculateDependencies() {
175 // Create an entity manager, storage, entity type, and entity to mock the
176 // loading of entities providing bundles.
177 $entityManager = $this->getMock('Drupal\Core\Entity\EntityManagerInterface');
178 $storage = $this->getMock('Drupal\Core\Entity\EntityStorageInterface');
179 $entity_type = $this->getMock('Drupal\Core\Entity\EntityTypeInterface');
180 $mock_entity = $this->getMock('Drupal\Core\Entity\EntityInterface');
182 $mock_entity->expects($this->any())
183 ->method('getConfigDependencyKey')
184 ->willReturn('config');
185 $mock_entity->expects($this->any())
186 ->method('getConfigDependencyName')
187 ->willReturn('test_bundle');
188 $storage->expects($this->any())
189 ->method('loadMultiple')
190 ->with(['test_bundle'])
191 ->willReturn(['test_bundle' => $mock_entity]);
193 $entity_type->expects($this->any())
194 ->method('getBundleEntityType')
195 ->willReturn('entity_test_bundle');
196 $entityManager->expects($this->any())
197 ->method('getDefinition')
198 ->with('entity_test')
199 ->willReturn($entity_type);
200 $entityManager->expects($this->any())
201 ->method('hasHandler')
202 ->with('entity_test_bundle', 'storage')
204 $entityManager->expects($this->any())
205 ->method('getStorage')
206 ->with('entity_test_bundle')
207 ->willReturn($storage);
209 // Set up the argument validator.
210 $argumentValidator = new Entity([], 'entity_test', ['entity_type' => 'entity_test'], $entityManager);
212 $options['access'] = FALSE;
213 $options['bundles'] = ['test_bundle' => 1];
214 $argumentValidator->init($this->executable, $this->display, $options);
216 $this->assertEquals(['config' => ['test_bundle']], $argumentValidator->calculateDependencies());
220 * Tests the validate argument method with multiple argument splitting.
222 public function testValidateArgumentMultiple() {
224 $options['access'] = TRUE;
225 $options['bundles'] = [];
226 $options['operation'] = 'test_op';
227 $options['multiple'] = TRUE;
228 $this->argumentValidator->init($this->executable, $this->display, $options);
230 $this->assertTrue($this->argumentValidator->validateArgument('1'));
231 $this->assertFalse($this->argumentValidator->validateArgument('2'));
233 $this->assertFalse($this->argumentValidator->validateArgument('1,2'));
234 $this->assertFalse($this->argumentValidator->validateArgument('1+2'));
237 $options['access'] = TRUE;
238 $options['bundles'] = [];
239 $options['operation'] = 'test_op_3';
240 $options['multiple'] = TRUE;
241 $this->argumentValidator->init($this->executable, $this->display, $options);
243 $this->assertTrue($this->argumentValidator->validateArgument('1,2'));
244 $this->assertTrue($this->argumentValidator->validateArgument('1+2'));