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
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
project
drupal
Commits
88a87872
Commit
88a87872
authored
Feb 16, 2017
by
Jess
Browse files
Options
Downloads
Patches
Plain Diff
Issue
#2832264
by mpdonadio, jhedstrom: DateTimeItemTest is not valid
parent
04ca0b78
Branches
Branches containing commit
Tags
Tags containing commit
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
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
core/modules/datetime/tests/src/Kernel/DateTimeItemTest.php
+119
-18
119 additions, 18 deletions
core/modules/datetime/tests/src/Kernel/DateTimeItemTest.php
with
119 additions
and
18 deletions
core/modules/datetime/tests/src/Kernel/DateTimeItemTest.php
+
119
−
18
View file @
88a87872
...
@@ -4,6 +4,7 @@
...
@@ -4,6 +4,7 @@
use
Drupal\Core\Field\FieldItemListInterface
;
use
Drupal\Core\Field\FieldItemListInterface
;
use
Drupal\Core\Field\FieldItemInterface
;
use
Drupal\Core\Field\FieldItemInterface
;
use
Drupal\datetime\Plugin\Field\FieldType\DateTimeItem
;
use
Drupal\entity_test
\Entity\EntityTest
;
use
Drupal\entity_test
\Entity\EntityTest
;
use
Drupal\field\Entity\FieldConfig
;
use
Drupal\field\Entity\FieldConfig
;
use
Drupal\Tests\field\Kernel\FieldKernelTestBase
;
use
Drupal\Tests\field\Kernel\FieldKernelTestBase
;
...
@@ -16,6 +17,20 @@
...
@@ -16,6 +17,20 @@
*/
*/
class
DateTimeItemTest
extends
FieldKernelTestBase
{
class
DateTimeItemTest
extends
FieldKernelTestBase
{
/**
* A field storage to use in this test class.
*
* @var \Drupal\field\Entity\FieldStorageConfig
*/
protected
$fieldStorage
;
/**
* The field used in this test class.
*
* @var \Drupal\field\Entity\FieldConfig
*/
protected
$field
;
/**
/**
* Modules to enable.
* Modules to enable.
*
*
...
@@ -27,33 +42,74 @@ protected function setUp() {
...
@@ -27,33 +42,74 @@ protected function setUp() {
parent
::
setUp
();
parent
::
setUp
();
// Create a field with settings to validate.
// Create a field with settings to validate.
$field
_s
torage
=
FieldStorageConfig
::
create
(
array
(
$
this
->
field
S
torage
=
FieldStorageConfig
::
create
(
array
(
'field_name'
=>
'field_datetime'
,
'field_name'
=>
'field_datetime'
,
'type'
=>
'datetime'
,
'type'
=>
'datetime'
,
'entity_type'
=>
'entity_test'
,
'entity_type'
=>
'entity_test'
,
'settings'
=>
array
(
'datetime_type'
=>
'd
ate
'
),
'settings'
=>
array
(
'datetime_type'
=>
D
ate
TimeItem
::
DATETIME_TYPE_DATETIME
),
));
));
$field
_s
torage
->
save
();
$
this
->
field
S
torage
->
save
();
$field
=
FieldConfig
::
create
([
$
this
->
field
=
FieldConfig
::
create
([
'field_storage'
=>
$field
_s
torage
,
'field_storage'
=>
$
this
->
field
S
torage
,
'bundle'
=>
'entity_test'
,
'bundle'
=>
'entity_test'
,
'settings'
=>
array
(
'settings'
=>
array
(
'default_value'
=>
'blank'
,
'default_value'
=>
'blank'
,
),
),
]);
]);
$field
->
save
();
$this
->
field
->
save
();
}
/**
* Tests using entity fields of the datetime field type.
*/
public
function
testDateTime
()
{
$this
->
fieldStorage
->
setSetting
(
'datetime_type'
,
DateTimeItem
::
DATETIME_TYPE_DATETIME
);
$this
->
fieldStorage
->
save
();
// Verify entity creation.
$entity
=
EntityTest
::
create
();
$value
=
'2014-01-01T20:00:00'
;
$entity
->
field_datetime
=
$value
;
$entity
->
name
->
value
=
$this
->
randomMachineName
();
$this
->
entityValidateAndSave
(
$entity
);
// Verify entity has been created properly.
$id
=
$entity
->
id
();
$entity
=
EntityTest
::
load
(
$id
);
$this
->
assertTrue
(
$entity
->
field_datetime
instanceof
FieldItemListInterface
,
'Field implements interface.'
);
$this
->
assertTrue
(
$entity
->
field_datetime
[
0
]
instanceof
FieldItemInterface
,
'Field item implements interface.'
);
$this
->
assertEqual
(
$entity
->
field_datetime
->
value
,
$value
);
$this
->
assertEqual
(
$entity
->
field_datetime
[
0
]
->
value
,
$value
);
// Verify changing the date value.
$new_value
=
'2016-11-04T00:21:00'
;
$entity
->
field_datetime
->
value
=
$new_value
;
$this
->
assertEqual
(
$entity
->
field_datetime
->
value
,
$new_value
);
// Read changed entity and assert changed values.
$this
->
entityValidateAndSave
(
$entity
);
$entity
=
EntityTest
::
load
(
$id
);
$this
->
assertEqual
(
$entity
->
field_datetime
->
value
,
$new_value
);
// Test the generateSampleValue() method.
$entity
=
EntityTest
::
create
();
$entity
->
field_datetime
->
generateSampleItems
();
$this
->
entityValidateAndSave
(
$entity
);
}
}
/**
/**
* Tests using entity fields of the date field type.
* Tests using entity fields of the date field type.
*/
*/
public
function
testDateTimeItem
()
{
public
function
testDateOnly
()
{
$this
->
fieldStorage
->
setSetting
(
'datetime_type'
,
DateTimeItem
::
DATETIME_TYPE_DATE
);
$this
->
fieldStorage
->
save
();
// Verify entity creation.
// Verify entity creation.
$entity
=
EntityTest
::
create
();
$entity
=
EntityTest
::
create
();
$value
=
'2014-01-01
T20:00:00Z
'
;
$value
=
'2014-01-01'
;
$entity
->
field_datetime
=
$value
;
$entity
->
field_datetime
=
$value
;
$entity
->
name
->
value
=
$this
->
randomMachineName
();
$entity
->
name
->
value
=
$this
->
randomMachineName
();
$
entity
->
save
(
);
$
this
->
entityValidateAndSave
(
$entity
);
// Verify entity has been created properly.
// Verify entity has been created properly.
$id
=
$entity
->
id
();
$id
=
$entity
->
id
();
...
@@ -64,12 +120,12 @@ public function testDateTimeItem() {
...
@@ -64,12 +120,12 @@ public function testDateTimeItem() {
$this
->
assertEqual
(
$entity
->
field_datetime
[
0
]
->
value
,
$value
);
$this
->
assertEqual
(
$entity
->
field_datetime
[
0
]
->
value
,
$value
);
// Verify changing the date value.
// Verify changing the date value.
$new_value
=
$this
->
randomMachineName
()
;
$new_value
=
'2016-11-04'
;
$entity
->
field_datetime
->
value
=
$new_value
;
$entity
->
field_datetime
->
value
=
$new_value
;
$this
->
assertEqual
(
$entity
->
field_datetime
->
value
,
$new_value
);
$this
->
assertEqual
(
$entity
->
field_datetime
->
value
,
$new_value
);
// Read changed entity and assert changed values.
// Read changed entity and assert changed values.
$
entity
->
save
(
);
$
this
->
entityValidateAndSave
(
$entity
);
$entity
=
EntityTest
::
load
(
$id
);
$entity
=
EntityTest
::
load
(
$id
);
$this
->
assertEqual
(
$entity
->
field_datetime
->
value
,
$new_value
);
$this
->
assertEqual
(
$entity
->
field_datetime
->
value
,
$new_value
);
...
@@ -83,11 +139,15 @@ public function testDateTimeItem() {
...
@@ -83,11 +139,15 @@ public function testDateTimeItem() {
* Tests DateTimeItem::setValue().
* Tests DateTimeItem::setValue().
*/
*/
public
function
testSetValue
()
{
public
function
testSetValue
()
{
// Test a date+time field.
$this
->
fieldStorage
->
setSetting
(
'datetime_type'
,
DateTimeItem
::
DATETIME_TYPE_DATETIME
);
$this
->
fieldStorage
->
save
();
// Test DateTimeItem::setValue() using string.
// Test DateTimeItem::setValue() using string.
$entity
=
EntityTest
::
create
();
$entity
=
EntityTest
::
create
();
$value
=
'2014-01-01T20:00:00
Z
'
;
$value
=
'2014-01-01T20:00:00'
;
$entity
->
get
(
'field_datetime'
)
->
set
(
0
,
$value
);
$entity
->
get
(
'field_datetime'
)
->
set
(
0
,
$value
);
$
entity
->
save
(
);
$
this
->
entityValidateAndSave
(
$entity
);
// Load the entity and ensure the field was saved correctly.
// Load the entity and ensure the field was saved correctly.
$id
=
$entity
->
id
();
$id
=
$entity
->
id
();
$entity
=
EntityTest
::
load
(
$id
);
$entity
=
EntityTest
::
load
(
$id
);
...
@@ -95,9 +155,33 @@ public function testSetValue() {
...
@@ -95,9 +155,33 @@ public function testSetValue() {
// Test DateTimeItem::setValue() using property array.
// Test DateTimeItem::setValue() using property array.
$entity
=
EntityTest
::
create
();
$entity
=
EntityTest
::
create
();
$value
=
'2014-01-01T20:00:00
Z
'
;
$value
=
'2014-01-01T20:00:00'
;
$entity
->
set
(
'field_datetime'
,
$value
);
$entity
->
set
(
'field_datetime'
,
$value
);
$entity
->
save
();
$this
->
entityValidateAndSave
(
$entity
);
// Load the entity and ensure the field was saved correctly.
$id
=
$entity
->
id
();
$entity
=
EntityTest
::
load
(
$id
);
$this
->
assertEqual
(
$entity
->
field_datetime
[
0
]
->
value
,
$value
,
'DateTimeItem::setValue() works with array value.'
);
// Test a date-only field.
$this
->
fieldStorage
->
setSetting
(
'datetime_type'
,
DateTimeItem
::
DATETIME_TYPE_DATE
);
$this
->
fieldStorage
->
save
();
// Test DateTimeItem::setValue() using string.
$entity
=
EntityTest
::
create
();
$value
=
'2014-01-01'
;
$entity
->
get
(
'field_datetime'
)
->
set
(
0
,
$value
);
$this
->
entityValidateAndSave
(
$entity
);
// Load the entity and ensure the field was saved correctly.
$id
=
$entity
->
id
();
$entity
=
EntityTest
::
load
(
$id
);
$this
->
assertEqual
(
$entity
->
field_datetime
[
0
]
->
value
,
$value
,
'DateTimeItem::setValue() works with string value.'
);
// Test DateTimeItem::setValue() using property array.
$entity
=
EntityTest
::
create
();
$value
=
'2014-01-01'
;
$entity
->
set
(
'field_datetime'
,
$value
);
$this
->
entityValidateAndSave
(
$entity
);
// Load the entity and ensure the field was saved correctly.
// Load the entity and ensure the field was saved correctly.
$id
=
$entity
->
id
();
$id
=
$entity
->
id
();
$entity
=
EntityTest
::
load
(
$id
);
$entity
=
EntityTest
::
load
(
$id
);
...
@@ -108,12 +192,29 @@ public function testSetValue() {
...
@@ -108,12 +192,29 @@ public function testSetValue() {
* Tests setting the value of the DateTimeItem directly.
* Tests setting the value of the DateTimeItem directly.
*/
*/
public
function
testSetValueProperty
()
{
public
function
testSetValueProperty
()
{
// Test Date::setValue().
// Test Date::setValue() with a date+time field.
// Test a date+time field.
$this
->
fieldStorage
->
setSetting
(
'datetime_type'
,
DateTimeItem
::
DATETIME_TYPE_DATETIME
);
$this
->
fieldStorage
->
save
();
$entity
=
EntityTest
::
create
();
$value
=
'2014-01-01T20:00:00'
;
$entity
->
set
(
'field_datetime'
,
$value
);
$this
->
entityValidateAndSave
(
$entity
);
// Load the entity and ensure the field was saved correctly.
$id
=
$entity
->
id
();
$entity
=
EntityTest
::
load
(
$id
);
$this
->
assertEqual
(
$entity
->
field_datetime
[
0
]
->
value
,
$value
,
'"Value" property can be set directly.'
);
// Test Date::setValue() with a date-only field.
// Test a date+time field.
$this
->
fieldStorage
->
setSetting
(
'datetime_type'
,
DateTimeItem
::
DATETIME_TYPE_DATE
);
$this
->
fieldStorage
->
save
();
$entity
=
EntityTest
::
create
();
$entity
=
EntityTest
::
create
();
$value
=
'2014-01-01
T20:00:00Z
'
;
$value
=
'2014-01-01'
;
$entity
->
set
(
'field_datetime'
,
$value
);
$entity
->
set
(
'field_datetime'
,
$value
);
$
entity
->
save
(
);
$
this
->
entityValidateAndSave
(
$entity
);
// Load the entity and ensure the field was saved correctly.
// Load the entity and ensure the field was saved correctly.
$id
=
$entity
->
id
();
$id
=
$entity
->
id
();
$entity
=
EntityTest
::
load
(
$id
);
$entity
=
EntityTest
::
load
(
$id
);
...
...
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