You are here

public function ConstraintsTest::testValidation in Media entity slideshow 8

Same name and namespace in other branches
  1. 8.2 tests/src/Unit/ConstraintsTest.php \Drupal\Tests\media_entity_slideshow\Unit\ConstraintsTest::testValidation()

Tests ItemsCount constraint.

@covers \Drupal\media_entity_slideshow\Plugin\Validation\Constraint\ItemsCountConstraintValidator @covers \Drupal\media_entity_slideshow\Plugin\Validation\Constraint\ItemsCountConstraint

File

tests/src/Unit/ConstraintsTest.php, line 22

Class

ConstraintsTest
Tests media_entity_slideshow constraints.

Namespace

Drupal\Tests\media_entity_slideshow\Unit

Code

public function testValidation() {

  // Check message in constraint.
  $constraint = new ItemsCountConstraint([
    'sourceFieldName' => 'test_field',
  ]);
  $this
    ->assertEquals('At least one slideshow item must exist.', $constraint->message, 'Correct constraint message found.');

  // Test the validator with valid values.
  $execution_context = $this
    ->getMockBuilder('\\Drupal\\Core\\TypedData\\Validation\\ExecutionContext')
    ->disableOriginalConstructor()
    ->getMock();
  $execution_context
    ->expects($this
    ->exactly(0))
    ->method('addViolation');
  $value = new TestMediaEntityConstraints('test_field', 'Some text');
  $validator = new ItemsCountConstraintValidator();
  $validator
    ->initialize($execution_context);
  $validator
    ->validate($value, $constraint);

  // Test the validator with invalid values.
  $execution_context = $this
    ->getMockBuilder('\\Drupal\\Core\\TypedData\\Validation\\ExecutionContext')
    ->disableOriginalConstructor()
    ->getMock();
  $execution_context
    ->expects($this
    ->exactly(1))
    ->method('addViolation')
    ->with($constraint->message);
  $value = new TestMediaEntityConstraints('test_field');
  $validator = new ItemsCountConstraintValidator();
  $validator
    ->initialize($execution_context);
  $validator
    ->validate($value, $constraint);
}