Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / views / tests / src / Unit / Plugin / argument_validator / EntityTest.php
1 <?php
2
3 namespace Drupal\Tests\views\Unit\Plugin\argument_validator;
4
5 use Drupal\Tests\UnitTestCase;
6 use Drupal\views\Plugin\views\argument_validator\Entity;
7
8 /**
9  * @coversDefaultClass \Drupal\views\Plugin\views\argument_validator\Entity
10  * @group views
11  */
12 class EntityTest extends UnitTestCase {
13
14   /**
15    * The view executable.
16    *
17    * @var \Drupal\views\ViewExecutable
18    */
19   protected $executable;
20
21   /**
22    * The view display.
23    *
24    * @var \Drupal\views\Plugin\views\display\DisplayPluginBase
25    */
26   protected $display;
27
28   /**
29    * The entity manager.
30    *
31    * @var \PHPUnit_Framework_MockObject_MockObject|\Drupal\Core\Entity\EntityManagerInterface
32    */
33   protected $entityManager;
34
35   /**
36    * The tested argument validator.
37    *
38    * @var \Drupal\views\Plugin\views\argument_validator\Entity
39    */
40   protected $argumentValidator;
41
42   /**
43    * {@inheritdoc}
44    */
45   protected function setUp() {
46     parent::setUp();
47
48     $this->entityManager = $this->getMock('Drupal\Core\Entity\EntityManagerInterface');
49
50     $mock_entity = $this->getMockForAbstractClass('Drupal\Core\Entity\Entity', [], '', FALSE, TRUE, TRUE, ['bundle', 'access']);
51     $mock_entity->expects($this->any())
52       ->method('bundle')
53       ->will($this->returnValue('test_bundle'));
54     $mock_entity->expects($this->any())
55       ->method('access')
56       ->will($this->returnValueMap([
57         ['test_op', NULL, FALSE, TRUE],
58         ['test_op_2', NULL, FALSE, FALSE],
59         ['test_op_3', NULL, FALSE, TRUE],
60       ]));
61
62     $mock_entity_bundle_2 = $this->getMockForAbstractClass('Drupal\Core\Entity\Entity', [], '', FALSE, TRUE, TRUE, ['bundle', 'access']);
63     $mock_entity_bundle_2->expects($this->any())
64       ->method('bundle')
65       ->will($this->returnValue('test_bundle_2'));
66     $mock_entity_bundle_2->expects($this->any())
67       ->method('access')
68       ->will($this->returnValueMap([
69         ['test_op', NULL, FALSE, FALSE],
70         ['test_op_2', NULL, FALSE, FALSE],
71         ['test_op_3', NULL, FALSE, TRUE],
72       ]));
73
74     $storage = $this->getMock('Drupal\Core\Entity\EntityStorageInterface');
75
76     // Setup values for IDs passed as strings or numbers.
77     $value_map = [
78       [[], []],
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]],
85     ];
86     $storage->expects($this->any())
87       ->method('loadMultiple')
88       ->will($this->returnValueMap($value_map));
89
90     $this->entityManager->expects($this->any())
91       ->method('getStorage')
92       ->with('entity_test')
93       ->will($this->returnValue($storage));
94
95     $this->executable = $this->getMockBuilder('Drupal\views\ViewExecutable')
96       ->disableOriginalConstructor()
97       ->getMock();
98     $this->display = $this->getMockBuilder('Drupal\views\Plugin\views\display\DisplayPluginBase')
99       ->disableOriginalConstructor()
100       ->getMock();
101
102     $definition = [
103       'entity_type' => 'entity_test',
104     ];
105
106     $this->argumentValidator = new Entity([], 'entity_test', $definition, $this->entityManager);
107   }
108
109   /**
110    * Tests the validate argument method with no access and bundles.
111    *
112    * @see \Drupal\views\Plugin\views\argument_validator\Entity::validateArgument()
113    */
114   public function testValidateArgumentNoAccess() {
115     $options = [];
116     $options['access'] = FALSE;
117     $options['bundles'] = [];
118     $this->argumentValidator->init($this->executable, $this->display, $options);
119
120     $this->assertFalse($this->argumentValidator->validateArgument(3));
121     $this->assertFalse($this->argumentValidator->validateArgument(''));
122
123     $this->assertTrue($this->argumentValidator->validateArgument(1));
124     $this->assertTrue($this->argumentValidator->validateArgument(2));
125     $this->assertFalse($this->argumentValidator->validateArgument('1,2'));
126   }
127
128   /**
129    * Tests the validate argument method with access and no bundles.
130    *
131    * @see \Drupal\views\Plugin\views\argument_validator\Entity::validateArgument()
132    */
133   public function testValidateArgumentAccess() {
134     $options = [];
135     $options['access'] = TRUE;
136     $options['bundles'] = [];
137     $options['operation'] = 'test_op';
138     $this->argumentValidator->init($this->executable, $this->display, $options);
139
140     $this->assertFalse($this->argumentValidator->validateArgument(3));
141     $this->assertFalse($this->argumentValidator->validateArgument(''));
142
143     $this->assertTrue($this->argumentValidator->validateArgument(1));
144
145     $options = [];
146     $options['access'] = TRUE;
147     $options['bundles'] = [];
148     $options['operation'] = 'test_op_2';
149     $this->argumentValidator->init($this->executable, $this->display, $options);
150
151     $this->assertFalse($this->argumentValidator->validateArgument(3));
152     $this->assertFalse($this->argumentValidator->validateArgument(''));
153
154     $this->assertFalse($this->argumentValidator->validateArgument(1));
155     $this->assertFalse($this->argumentValidator->validateArgument(2));
156   }
157
158   /**
159    * Tests the validate argument method with bundle checking.
160    */
161   public function testValidateArgumentBundle() {
162     $options = [];
163     $options['access'] = FALSE;
164     $options['bundles'] = ['test_bundle' => 1];
165     $this->argumentValidator->init($this->executable, $this->display, $options);
166
167     $this->assertTrue($this->argumentValidator->validateArgument(1));
168     $this->assertFalse($this->argumentValidator->validateArgument(2));
169   }
170
171   /**
172    * @covers ::calculateDependencies
173    */
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');
181
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]);
192
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')
203       ->willReturn(TRUE);
204     $entityManager->expects($this->any())
205       ->method('getStorage')
206       ->with('entity_test_bundle')
207       ->willReturn($storage);
208
209     // Set up the argument validator.
210     $argumentValidator = new Entity([], 'entity_test', ['entity_type' => 'entity_test'], $entityManager);
211     $options = [];
212     $options['access'] = FALSE;
213     $options['bundles'] = ['test_bundle' => 1];
214     $argumentValidator->init($this->executable, $this->display, $options);
215
216     $this->assertEquals(['config' => ['test_bundle']], $argumentValidator->calculateDependencies());
217   }
218
219   /**
220    * Tests the validate argument method with multiple argument splitting.
221    */
222   public function testValidateArgumentMultiple() {
223     $options = [];
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);
229
230     $this->assertTrue($this->argumentValidator->validateArgument('1'));
231     $this->assertFalse($this->argumentValidator->validateArgument('2'));
232
233     $this->assertFalse($this->argumentValidator->validateArgument('1,2'));
234     $this->assertFalse($this->argumentValidator->validateArgument('1+2'));
235
236     $options = [];
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);
242
243     $this->assertTrue($this->argumentValidator->validateArgument('1,2'));
244     $this->assertTrue($this->argumentValidator->validateArgument('1+2'));
245   }
246
247 }