Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
F
field_validation
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
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
project
field_validation
Commits
a226d6ac
Commit
a226d6ac
authored
Jul 20, 2023
by
sumit kumar
Committed by
howard ge
Jul 20, 2023
Browse files
Options
Downloads
Patches
Plain Diff
Issue
#3369907
by sumit-k, g089h515r806: Write tests for ItemCountFieldValidationRule
parent
48b6ff5a
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
tests/src/Kernel/Plugin/FieldValidationRule/ItemCountFieldValidationRuleTest.php
+145
-0
145 additions, 0 deletions
.../FieldValidationRule/ItemCountFieldValidationRuleTest.php
with
145 additions
and
0 deletions
tests/src/Kernel/Plugin/FieldValidationRule/ItemCountFieldValidationRuleTest.php
0 → 100644
+
145
−
0
View file @
a226d6ac
<?php
namespace
Drupal\Tests\field_validation\Kernel\Plugin\FieldValidationRule
;
use
Drupal\field\Entity\FieldConfig
;
use
Drupal\field\Entity\FieldStorageConfig
;
use
Drupal\node\Entity\NodeType
;
use
Drupal\field_validation
\Plugin\Validation\Constraint\FieldValidationConstraint
;
/**
* Tests ItemCountFieldValidationRule.
*
* @group field_validation
*
* @package Drupal\Tests\field_validation\Kernel
*/
class
ItemCountFieldValidationRuleTest
extends
FieldValidationRuleBase
{
/**
* Entity interface.
*
* @var \Drupal\Core\Entity\EntityInterface
*/
protected
$entity
;
/**
* Field name.
*/
const
FIELD_NAME
=
'field_item_count'
;
/**
* Rule id.
*/
const
RULE_ID
=
'item_count_field_validation_rule'
;
/**
* Rule title.
*/
const
RULE_TITLE
=
'validation rule item count'
;
/**
* Stores mock ruleset.
*
* @var \Drupal\field_validation\Entity\FieldValidationRuleSet
*/
protected
$ruleSet
;
/**
* {@inheritdoc}
*/
protected
function
setUp
():
void
{
parent
::
setUp
();
NodeType
::
create
([
'type'
=>
'article'
,
'label'
=>
'Article'
,
])
->
save
();
FieldStorageConfig
::
create
([
'entity_type'
=>
'node'
,
'field_name'
=>
self
::
FIELD_NAME
,
'type'
=>
'text'
,
'cardinality'
=>
4
,
])
->
save
();
FieldConfig
::
create
([
'entity_type'
=>
'node'
,
'field_name'
=>
self
::
FIELD_NAME
,
'bundle'
=>
'article'
,
])
->
save
();
$this
->
ruleSet
=
$this
->
ruleSetStorage
->
create
([
'name'
=>
'item_count_test'
,
'entity_type'
=>
'node'
,
'bundle'
=>
'article'
,
]);
$this
->
ruleSet
->
addFieldValidationRule
([
'id'
=>
self
::
RULE_ID
,
'title'
=>
self
::
RULE_TITLE
,
'weight'
=>
1
,
'field_name'
=>
self
::
FIELD_NAME
,
'column'
=>
'value'
,
'error_message'
=>
'Item count error!'
,
'data'
=>
[
'min'
=>
1
,
'max'
=>
3
,
],
]);
$this
->
ruleSet
->
save
();
$field_values
=
array_merge
([
'one'
],
[
'two'
],
[
'three'
],
[
'four'
]);
$this
->
entity
=
$this
->
nodeStorage
->
create
([
'type'
=>
'article'
,
'title'
=>
'test count'
,
self
::
FIELD_NAME
=>
$field_values
,
]);
$this
->
entity
->
get
(
self
::
FIELD_NAME
)
->
getFieldDefinition
()
->
addConstraint
(
'FieldValidationConstraint'
,
[
'ruleset_name'
=>
$this
->
ruleSet
->
getName
()]
);
}
/**
* Tests ItemCountFieldValidationRule.
*/
public
function
testItemCount
()
{
$entity
=
$this
->
entity
;
$fieldName
=
self
::
FIELD_NAME
;
$ruleSet
=
$this
->
ruleSet
;
$violations
=
$entity
->
validate
();
$this
->
assertCount
(
4
,
$violations
);
$this
->
assertInstanceOf
(
FieldValidationConstraint
::
class
,
$violations
[
0
]
->
getConstraint
()
);
$this
->
assertEquals
(
$ruleSet
->
getName
(),
$violations
[
0
]
->
getConstraint
()
->
ruleset_name
);
$this
->
updateSettings
(
[
'min'
=>
'1'
,
'max'
=>
'5'
,
],
self
::
RULE_ID
,
self
::
RULE_TITLE
,
$this
->
ruleSet
,
self
::
FIELD_NAME
);
$this
->
assertConstraintPass
(
$this
->
entity
,
self
::
FIELD_NAME
,
'3'
,
);
}
}
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