From e210e08511aad96de31b2bcef2c82f86d6b0568c Mon Sep 17 00:00:00 2001
From: webchick <webchick@24967.no-reply.drupal.org>
Date: Wed, 8 Jan 2014 00:57:36 -0800
Subject: [PATCH] Issue #2142981 by fago, mariancalinro: Test for
 AllowedValuesConstraintValidator.

---
 .../AllowedValuesConstraintValidatorTest.php  | 65 +++++++++++++++++++
 1 file changed, 65 insertions(+)
 create mode 100644 core/modules/system/lib/Drupal/system/Tests/Validation/AllowedValuesConstraintValidatorTest.php

diff --git a/core/modules/system/lib/Drupal/system/Tests/Validation/AllowedValuesConstraintValidatorTest.php b/core/modules/system/lib/Drupal/system/Tests/Validation/AllowedValuesConstraintValidatorTest.php
new file mode 100644
index 000000000000..563b71111ad4
--- /dev/null
+++ b/core/modules/system/lib/Drupal/system/Tests/Validation/AllowedValuesConstraintValidatorTest.php
@@ -0,0 +1,65 @@
+<?php
+
+/**
+ * @file
+ * Contains Drupal\system\Tests\Validation\AllowedValuesConstraintValidatorTest.
+ */
+
+namespace Drupal\system\Tests\Validation;
+
+use Drupal\Core\TypedData\DataDefinition;
+use Drupal\simpletest\DrupalUnitTestBase;
+
+/**
+ * Tests the AllowedValues validation constraint validator.
+ */
+class AllowedValuesConstraintValidatorTest extends DrupalUnitTestBase {
+
+  /**
+   * The typed data manager to use.
+   *
+   * @var \Drupal\Core\TypedData\TypedDataManager
+   */
+  protected $typedData;
+
+  public static function getInfo() {
+    return array(
+      'name' => 'Allowed values constraint',
+      'description' => 'Tests AllowedValues validation constraint with both valid and invalid values.',
+      'group' => 'Validation',
+    );
+  }
+
+  public function setUp() {
+    parent::setUp();
+    $this->typedData = $this->container->get('typed_data_manager');
+  }
+
+  /**
+   * Tests the AllowedValues validation constraint validator.
+   *
+   * For testing we define an integer with a set of allowed values.
+   */
+  public function testValidation() {
+    // Create a definition that specifies some AllowedValues.
+    $definition = DataDefinition::create('integer')
+      ->addConstraint('AllowedValues', array(1, 2, 3));
+
+    // Test the validation.
+    $typed_data = $this->typedData->create($definition, 1);
+    $violations = $typed_data->validate();
+    $this->assertEqual($violations->count(), 0, 'Validation passed for correct value.');
+
+    // Test the validation when an invalid value is passed.
+    $typed_data = $this->typedData->create($definition, 4);
+    $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 value you selected is not a valid choice.'), 'The message for invalid value is correct.');
+    $this->assertEqual($violation->getRoot(), $typed_data, 'Violation root is correct.');
+    $this->assertEqual($violation->getInvalidValue(), 4, 'The invalid value is set correctly in the violation.');
+  }
+
+}
-- 
GitLab