Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
drupal
Manage
Activity
Members
Labels
Plan
Wiki
Custom issue tracker
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Model registry
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
project
drupal
Commits
8f76f3c1
Commit
8f76f3c1
authored
11 years ago
by
Angie Byron
Browse files
Options
Downloads
Patches
Plain Diff
Issue
#2142991
by fago, mariancalinro: Test for ComplexDataConstraintValidator.
parent
f49fa091
No related branches found
No related tags found
2 merge requests
!7452
Issue #1797438. HTML5 validation is preventing form submit and not fully...
,
!789
Issue #3210310: Adjust Database API to remove deprecated Drupal 9 code in Drupal 10
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
core/modules/system/lib/Drupal/system/Tests/Validation/ComplexDataConstraintValidatorTest.php
+86
-0
86 additions, 0 deletions
...m/Tests/Validation/ComplexDataConstraintValidatorTest.php
with
86 additions
and
0 deletions
core/modules/system/lib/Drupal/system/Tests/Validation/ComplexDataConstraintValidatorTest.php
0 → 100644
+
86
−
0
View file @
8f76f3c1
<?php
/**
* @file
* Contains Drupal\system\Tests\Validation\ComplexDataConstraintValidatorTest.
*/
namespace
Drupal\system\Tests\Validation
;
use
Drupal\Core\TypedData\DataDefinition
;
use
Drupal\simpletest\DrupalUnitTestBase
;
/**
* Tests the ComplexData validation constraint validator.
*/
class
ComplexDataConstraintValidatorTest
extends
DrupalUnitTestBase
{
/**
* The typed data manager to use.
*
* @var \Drupal\Core\TypedData\TypedDataManager
*/
protected
$typedData
;
public
static
function
getInfo
()
{
return
array
(
'name'
=>
'Complex data constraint'
,
'description'
=>
'Tests ComplexData validation constraint with both valid and invalid values for a key'
,
'group'
=>
'Validation'
,
);
}
public
function
setUp
()
{
parent
::
setUp
();
$this
->
typedData
=
$this
->
container
->
get
(
'typed_data_manager'
);
}
/**
* Tests the ComplexData validation constraint validator.
*
* For testing a map including a constraint on one of its keys is defined.
*/
public
function
testValidation
()
{
// Create a definition that specifies some ComplexData constraint.
$definition
=
DataDefinition
::
create
(
'map'
)
->
addConstraint
(
'ComplexData'
,
array
(
'key'
=>
array
(
'AllowedValues'
=>
array
(
1
,
2
,
3
)
),
));
// Test the validation.
$typed_data
=
$this
->
typedData
->
create
(
$definition
,
array
(
'key'
=>
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
,
array
(
'key'
=>
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.'
);
// Test using the constraint with a map without the specified key. This
// should be ignored as long as there is no NotNull or NotBlank constraint.
$typed_data
=
$this
->
typedData
->
create
(
$definition
,
array
(
'foo'
=>
'bar'
));
$violations
=
$typed_data
->
validate
();
$this
->
assertEqual
(
$violations
->
count
(),
0
,
'Constraint on non-existing key is ignored.'
);
$definition
=
DataDefinition
::
create
(
'map'
)
->
addConstraint
(
'ComplexData'
,
array
(
'key'
=>
array
(
'NotNull'
=>
array
()
),
));
$typed_data
=
$this
->
typedData
->
create
(
$definition
,
array
(
'foo'
=>
'bar'
));
$violations
=
$typed_data
->
validate
();
$this
->
assertEqual
(
$violations
->
count
(),
1
,
'Key is required.'
);
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment