You are here

protected function BundleConstraintValidatorTest::assertValidation in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/system/src/Tests/Entity/BundleConstraintValidatorTest.php \Drupal\system\Tests\Entity\BundleConstraintValidatorTest::assertValidation()

Executes the BundleConstraintValidator test for a given bundle.

Parameters

string|array $bundle: Bundle/bundles to use as constraint option.

1 call to BundleConstraintValidatorTest::assertValidation()
BundleConstraintValidatorTest::testValidation in core/modules/system/src/Tests/Entity/BundleConstraintValidatorTest.php
Tests bundle constraint validation.

File

core/modules/system/src/Tests/Entity/BundleConstraintValidatorTest.php, line 52
Contains \Drupal\system\Tests\Entity\BundleConstraintValidatorTest.

Class

BundleConstraintValidatorTest
Tests validation constraints for BundleConstraintValidator.

Namespace

Drupal\system\Tests\Entity

Code

protected function assertValidation($bundle) {

  // Create a typed data definition with a Bundle constraint.
  $definition = DataDefinition::create('entity_reference')
    ->addConstraint('Bundle', $bundle);

  // Test the validation.
  $node = $this->container
    ->get('entity.manager')
    ->getStorage('node')
    ->create(array(
    'type' => 'foo',
  ));
  $typed_data = $this->typedData
    ->create($definition, $node);
  $violations = $typed_data
    ->validate();
  $this
    ->assertEqual($violations
    ->count(), 0, 'Validation passed for correct value.');

  // Test the validation when an invalid value is passed.
  $page_node = $this->container
    ->get('entity.manager')
    ->getStorage('node')
    ->create(array(
    'type' => 'baz',
  ));
  $typed_data = $this->typedData
    ->create($definition, $page_node);
  $violations = $typed_data
    ->validate();
  $this
    ->assertEqual($violations
    ->count(), 1, 'Validation failed for incorrect value.');

  // Make sure the information provided by a violation is correct.
  $violation = $violations[0];
  $this
    ->assertEqual($violation
    ->getMessage(), t('The entity must be of bundle %bundle.', array(
    '%bundle' => implode(', ', (array) $bundle),
  )), 'The message for invalid value is correct.');
  $this
    ->assertEqual($violation
    ->getRoot(), $typed_data, 'Violation root is correct.');
  $this
    ->assertEqual($violation
    ->getInvalidValue(), $page_node, 'The invalid value is set correctly in the violation.');
}