Security update for Core, with self-updated composer
[yaffs-website] / web / core / tests / Drupal / Tests / Component / Plugin / Context / ContextTest.php
1 <?php
2
3 namespace Drupal\Tests\Component\Plugin\Context;
4
5 use Drupal\Component\Plugin\Context\Context;
6 use PHPUnit\Framework\TestCase;
7
8 /**
9  * @coversDefaultClass \Drupal\Component\Plugin\Context\Context
10  * @group Plugin
11  */
12 class ContextTest extends TestCase {
13
14   /**
15    * Data provider for testGetContextValue.
16    */
17   public function providerGetContextValue() {
18     return [
19       ['context_value', 'context_value', FALSE, 'data_type'],
20       [NULL, NULL, FALSE, 'data_type'],
21       ['will throw exception', NULL, TRUE, 'data_type'],
22     ];
23   }
24
25   /**
26    * @covers ::getContextValue
27    * @dataProvider providerGetContextValue
28    */
29   public function testGetContextValue($expected, $context_value, $is_required, $data_type) {
30     // Mock a Context object.
31     $mock_context = $this->getMockBuilder('Drupal\Component\Plugin\Context\Context')
32       ->disableOriginalConstructor()
33       ->setMethods(['getContextDefinition'])
34       ->getMock();
35
36     // If the context value exists, getContextValue() behaves like a normal
37     // getter.
38     if ($context_value) {
39       // Set visibility of contextValue.
40       $ref_context_value = new \ReflectionProperty($mock_context, 'contextValue');
41       $ref_context_value->setAccessible(TRUE);
42       // Set contextValue to a testable state.
43       $ref_context_value->setValue($mock_context, $context_value);
44       // Exercise getContextValue().
45       $this->assertEquals($context_value, $mock_context->getContextValue());
46     }
47     // If no context value exists, we have to cover either returning NULL or
48     // throwing an exception if the definition requires it.
49     else {
50       // Create a mock definition.
51       $mock_definition = $this->getMockBuilder('Drupal\Component\Plugin\Context\ContextDefinitionInterface')
52         ->setMethods(['isRequired', 'getDataType'])
53         ->getMockForAbstractClass();
54
55       // Set expectation for isRequired().
56       $mock_definition->expects($this->once())
57         ->method('isRequired')
58         ->willReturn($is_required);
59
60       // Set expectation for getDataType().
61       $mock_definition->expects($this->exactly(
62             $is_required ? 1 : 0
63         ))
64         ->method('getDataType')
65         ->willReturn($data_type);
66
67       // Set expectation for getContextDefinition().
68       $mock_context->expects($this->once())
69         ->method('getContextDefinition')
70         ->willReturn($mock_definition);
71
72       // Set expectation for exception.
73       if ($is_required) {
74         $this->setExpectedException(
75           'Drupal\Component\Plugin\Exception\ContextException',
76           sprintf("The %s context is required and not present.", $data_type)
77         );
78       }
79
80       // Exercise getContextValue().
81       $this->assertEquals($context_value, $mock_context->getContextValue());
82     }
83   }
84
85   /**
86    * @covers ::getContextValue
87    */
88   public function testDefaultValue() {
89     $mock_definition = $this->getMockBuilder('Drupal\Component\Plugin\Context\ContextDefinitionInterface')
90       ->setMethods(['getDefaultValue'])
91       ->getMockForAbstractClass();
92
93     $mock_definition->expects($this->once())
94       ->method('getDefaultValue')
95       ->willReturn('test');
96
97     $context = new Context($mock_definition);
98     $this->assertEquals('test', $context->getContextValue());
99   }
100
101 }