diff --git a/core/modules/options/lib/Drupal/options/Tests/OptionsDynamicValuesValidationTest.php b/core/modules/options/lib/Drupal/options/Tests/OptionsDynamicValuesValidationTest.php index d13b25a..f978a79 100644 --- a/core/modules/options/lib/Drupal/options/Tests/OptionsDynamicValuesValidationTest.php +++ b/core/modules/options/lib/Drupal/options/Tests/OptionsDynamicValuesValidationTest.php @@ -7,8 +7,6 @@ namespace Drupal\options\Tests; -use Drupal\Core\Language\Language; - /** * Tests the Options field allowed values function. */ @@ -25,31 +23,19 @@ public static function getInfo() { * Test that allowed values function gets the entity. */ function testDynamicAllowedValues() { - // Verify that the test passes against every value we had. + // Verify that validation passes against every value we had. foreach ($this->test as $key => $value) { - // @todo Adjust when validation works $this->entity->test_options->value = $value; - try { - field_attach_validate($this->entity); - $this->pass("$key should pass"); - } - catch (FieldValidationException $e) { - // This will display as an exception, no need for a separate error. - throw($e); - } + $violations = $this->entity->test_options->validate(); + $this->assertEqual(count($violations), 0, "$key is a valid value"); } - // Now verify that the test does not pass against anything else. + + // Now verify that validation does not pass against anything else. foreach ($this->test as $key => $value) { $this->entity->test_options->value = is_numeric($value) ? (100 - $value) : ('X' . $value); - $pass = FALSE; - // @todo Adjust when validation works - try { - field_attach_validate($this->entity); - } - catch (FieldValidationException $e) { - $pass = TRUE; - } - $this->assertTrue($pass, $key . ' should not pass'); + $violations = $this->entity->test_options->validate(); + $this->assertEqual(count($violations), 1, "$key is not a valid value"); } } + } diff --git a/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/TermFieldTest.php b/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/TermFieldTest.php index 0bcc5e9..37a39a8 100644 --- a/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/TermFieldTest.php +++ b/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/TermFieldTest.php @@ -80,31 +80,19 @@ function setUp() { * Test term field validation. */ function testTaxonomyTermFieldValidation() { - // Test valid and invalid values with field_attach_validate(). - $langcode = Language::LANGCODE_NOT_SPECIFIED; - $entity = entity_create('entity_test', array()); + // Test validation with a valid value. $term = $this->createTerm($this->vocabulary); - // @todo Adjust when validation works + $entity = entity_create('entity_test', array()); $entity->{$this->field_name}->tid = $term->id(); - try { - field_attach_validate($entity); - $this->pass('Correct term does not cause validation error.'); - } - catch (FieldValidationException $e) { - $this->fail('Correct term does not cause validation error.'); - } + $violations = $entity->{$this->field_name}->validate(); + $this->assertEqual(count($violations) , 0, 'Correct term does not cause validation error.'); - $entity = entity_create('entity_test', array()); + // Test validation with an invalid valid value (wrong vocabulary). $bad_term = $this->createTerm($this->createVocabulary()); - // @todo Adjust when validation works + $entity = entity_create('entity_test', array()); $entity->{$this->field_name}->tid = $bad_term->id(); - try { - field_attach_validate($entity); - $this->fail('Wrong term causes validation error.'); - } - catch (FieldValidationException $e) { - $this->pass('Wrong term causes validation error.'); - } + $violations = $entity->{$this->field_name}->validate(); + $this->assertEqual(count($violations) , 1, 'Wrong term causes validation error.'); } /** diff --git a/core/modules/text/lib/Drupal/text/Plugin/field/field_type/CTextItemBase.php b/core/modules/text/lib/Drupal/text/Plugin/field/field_type/CTextItemBase.php index 0ae57e0..e00a155 100644 --- a/core/modules/text/lib/Drupal/text/Plugin/field/field_type/CTextItemBase.php +++ b/core/modules/text/lib/Drupal/text/Plugin/field/field_type/CTextItemBase.php @@ -77,12 +77,13 @@ public function getConstraints() { // ), // )); - if (!empty($this->field->settings['max_length'])) { + $max_length = $this->instance->getField()->settings['max_length']; + if (!empty($max_length)) { $constraints[] = $constraint_manager->create('ComplexData', array( 'value' => array( 'Length' => array( - 'max' => $this->field->settings['max_length'], - 'maxMessage' => t('%name: the text may not be longer than @max characters.', array('%name' => $this->instance->label, '@max' => $this->field->settings['max_length'])), + 'max' => $max_length, + 'maxMessage' => t('%name: the text may not be longer than @max characters.', array('%name' => $this->instance->label, '@max' => $max_length)), ) ), )); diff --git a/core/modules/text/lib/Drupal/text/Plugin/field/field_type/CTextWithSummaryItem.php b/core/modules/text/lib/Drupal/text/Plugin/field/field_type/CTextWithSummaryItem.php index 71fc0b5..2725c5f 100644 --- a/core/modules/text/lib/Drupal/text/Plugin/field/field_type/CTextWithSummaryItem.php +++ b/core/modules/text/lib/Drupal/text/Plugin/field/field_type/CTextWithSummaryItem.php @@ -110,12 +110,13 @@ public function getConstraints() { $constraint_manager = \Drupal::typedData()->getValidationConstraintManager(); $constraints = parent::getConstraints(); - if (!empty($this->field->settings['max_length'])) { + $max_length = $this->instance->getField()->settings['max_length']; + if (!empty($max_length)) { $constraints[] = $constraint_manager->create('ComplexData', array( 'value' => array( 'Length' => array( - 'max' => $this->field->settings['max_length'], - 'maxMessage' => t('%name: the summary may not be longer than @max characters.', array('%name' => $this->instance->label, '@max' => $this->field->settings['max_length'])), + 'max' => $max_length, + 'maxMessage' => t('%name: the summary may not be longer than @max characters.', array('%name' => $this->instance->label, '@max' => $max_length)), ) ), )); diff --git a/core/modules/text/lib/Drupal/text/Tests/TextFieldTest.php b/core/modules/text/lib/Drupal/text/Tests/TextFieldTest.php index eafef10..fc8a1d5 100644 --- a/core/modules/text/lib/Drupal/text/Tests/TextFieldTest.php +++ b/core/modules/text/lib/Drupal/text/Tests/TextFieldTest.php @@ -65,18 +65,16 @@ function testTextFieldValidation() { ); field_create_instance($this->instance); - // Test valid and invalid values with field_attach_validate(). - // @todo Adjust when field validation works again. + // Test validation with valid and invalid values. $entity = entity_create('entity_test', array()); - $langcode = Language::LANGCODE_NOT_SPECIFIED; for ($i = 0; $i <= $max_length + 2; $i++) { $entity->{$this->field['field_name']}->value = str_repeat('x', $i); - try { - field_attach_validate($entity); - $this->assertTrue($i <= $max_length, "Length $i does not cause validation error when max_length is $max_length"); + $violations = $entity->{$this->field['field_name']}->validate(); + if ($i <= $max_length) { + $this->assertEqual(count($violations), 0, "Length $i does not cause validation error when max_length is $max_length"); } - catch (FieldValidationException $e) { - $this->assertTrue($i > $max_length, "Length $i causes validation error when max_length is $max_length"); + else { + $this->assertEqual(count($violations), 1, "Length $i causes validation error when max_length is $max_length"); } } }