73a8f1f5f231fb20793da5de3e9b40d36830b620
[yaffs-website] / web / modules / contrib / media_entity_slideshow / tests / src / Unit / ConstraintsTest.php
1 <?php
2
3 namespace Drupal\Tests\media_entity_slideshow\Unit;
4
5 use Drupal\media_entity_slideshow\Plugin\Validation\Constraint\ItemsCountConstraint;
6 use Drupal\media_entity_slideshow\Plugin\Validation\Constraint\ItemsCountConstraintValidator;
7 use Drupal\Tests\UnitTestCase;
8
9 /**
10  * Tests media_entity_slideshow constraints.
11  *
12  * @group media_entity
13  */
14 class ConstraintsTest extends UnitTestCase {
15
16   /**
17    * Tests ItemsCount constraint.
18    *
19    * @covers \Drupal\media_entity_slideshow\Plugin\Validation\Constraint\ItemsCountConstraintValidator
20    * @covers \Drupal\media_entity_slideshow\Plugin\Validation\Constraint\ItemsCountConstraint
21    */
22   public function testValidation() {
23     // Check message in constraint.
24     $constraint = new ItemsCountConstraint(['sourceFieldName' => 'test_field']);
25     $this->assertEquals('At least one slideshow item must exist.', $constraint->message, 'Correct constraint message found.');
26
27     // Test the validator with valid values.
28     $execution_context = $this->getMockBuilder('\Drupal\Core\TypedData\Validation\ExecutionContext')
29       ->disableOriginalConstructor()
30       ->getMock();
31
32     $execution_context->expects($this->exactly(0))
33       ->method('addViolation');
34
35     $value = new TestMediaEntityConstraints('test_field', 'Some text');
36
37     $validator = new ItemsCountConstraintValidator();
38     $validator->initialize($execution_context);
39     $validator->validate($value, $constraint);
40
41     // Test the validator with invalid values.
42     $execution_context = $this->getMockBuilder('\Drupal\Core\TypedData\Validation\ExecutionContext')
43       ->disableOriginalConstructor()
44       ->getMock();
45
46     $execution_context->expects($this->exactly(1))
47       ->method('addViolation')
48       ->with($constraint->message);
49
50     $value = new TestMediaEntityConstraints('test_field');
51     $validator = new ItemsCountConstraintValidator();
52     $validator->initialize($execution_context);
53     $validator->validate($value, $constraint);
54   }
55
56 }
57
58 /**
59  * Mock class to test the ItemsCount constraint.
60  */
61 class TestMediaEntityConstraints {
62
63   /**
64    * The source field names.
65    *
66    * @var array
67    */
68   protected $sourceFields = array();
69
70   /**
71    * TestMediaEntityConstraints constructor.
72    *
73    * @param string $name
74    *   The source field name used for this test.
75    * @param string|null $value
76    *   (optional) The source field value used for this test.
77    */
78   public function __construct($name, $value = NULL) {
79     $this->sourceFields[$name] = new TestField($value);
80   }
81
82   /**
83    * Mocks get() on \Drupal\Core\Entity\FieldableEntityInterface.
84    */
85   public function get($name) {
86     return $this->sourceFields[$name];
87   }
88
89 }
90
91 /**
92  * Mock class for fields to test the ItemsCount constraint.
93  */
94 class TestField {
95
96   /**
97    * The field property.
98    *
99    * @var string
100    */
101   protected $property;
102
103   /**
104    * TestField constructor.
105    *
106    * @param string|null $value
107    *   (optional) The property value used for this test.
108    */
109   public function __construct($value = NULL) {
110     $this->property = $value;
111   }
112
113   /**
114    * Mocks isEmpty() on \Drupal\Core\Entity\Plugin\DataType\EntityAdapter.
115    */
116   public function isEmpty() {
117     return !isset($this->property);
118   }
119
120 }