Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
D
drupal
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Custom Issue Tracker
Custom Issue Tracker
Labels
Merge Requests
220
Merge Requests
220
Requirements
Requirements
List
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Analytics
Analytics
Code Review
Insights
Issue
Repository
Value Stream
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Commits
Open sidebar
project
drupal
Commits
02cd2f43
Commit
02cd2f43
authored
Apr 26, 2017
by
lauriii
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Issue
#2802541
by dagmar, mpdonadio: Timestamp field type doesn't provide sample values
parent
081132dd
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
103 additions
and
1 deletion
+103
-1
core/lib/Drupal/Core/Field/Plugin/Field/FieldType/TimestampItem.php
...rupal/Core/Field/Plugin/Field/FieldType/TimestampItem.php
+12
-1
core/modules/field/tests/src/Kernel/Timestamp/TimestampItemTest.php
...es/field/tests/src/Kernel/Timestamp/TimestampItemTest.php
+91
-0
No files found.
core/lib/Drupal/Core/Field/Plugin/Field/FieldType/TimestampItem.php
View file @
02cd2f43
...
...
@@ -2,8 +2,9 @@
namespace
Drupal\Core\Field\Plugin\Field\FieldType
;
use
Drupal\Core\Field\Field
Storage
DefinitionInterface
;
use
Drupal\Core\Field\FieldDefinitionInterface
;
use
Drupal\Core\Field\FieldItemBase
;
use
Drupal\Core\Field\FieldStorageDefinitionInterface
;
use
Drupal\Core\TypedData\DataDefinition
;
/**
...
...
@@ -53,4 +54,14 @@ public static function schema(FieldStorageDefinitionInterface $field_definition)
];
}
/**
* {@inheritdoc}
*/
public
static
function
generateSampleValue
(
FieldDefinitionInterface
$field_definition
)
{
// Pick a random timestamp in the past year.
$timestamp
=
\
Drupal
::
time
()
->
getRequestTime
()
-
mt_rand
(
0
,
86400
*
365
);
$values
[
'value'
]
=
$timestamp
;
return
$values
;
}
}
core/modules/field/tests/src/Kernel/Timestamp/TimestampItemTest.php
0 → 100644
View file @
02cd2f43
<?php
namespace
Drupal\Tests\field\Kernel\Timestamp
;
use
Drupal\Core\Field\FieldItemInterface
;
use
Drupal\Core\Field\FieldItemListInterface
;
use
Drupal\entity_test
\
Entity\EntityTest
;
use
Drupal\field\Entity\FieldConfig
;
use
Drupal\field\Entity\FieldStorageConfig
;
use
Drupal\Tests\field\Kernel\FieldKernelTestBase
;
/**
* Tests the timestamp fields.
*
* @group field
*/
class
TimestampItemTest
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
;
/**
* {@inheritdoc}
*/
protected
function
setUp
()
{
parent
::
setUp
();
// Create a field with settings to validate.
$this
->
fieldStorage
=
FieldStorageConfig
::
create
([
'field_name'
=>
'field_timestamp'
,
'type'
=>
'timestamp'
,
'entity_type'
=>
'entity_test'
,
]);
$this
->
fieldStorage
->
save
();
$this
->
field
=
FieldConfig
::
create
([
'field_storage'
=>
$this
->
fieldStorage
,
'bundle'
=>
'entity_test'
,
]);
$this
->
field
->
save
();
}
/**
* Tests using entity fields of the datetime field type.
*/
public
function
testDateTime
()
{
// Verify entity creation.
$entity
=
EntityTest
::
create
();
$value
=
1488914208
;
$entity
->
field_timestamp
=
$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_timestamp
instanceof
FieldItemListInterface
,
'Field implements interface.'
);
$this
->
assertTrue
(
$entity
->
field_timestamp
[
0
]
instanceof
FieldItemInterface
,
'Field item implements interface.'
);
$this
->
assertEquals
(
$entity
->
field_timestamp
->
value
,
$value
);
$this
->
assertEquals
(
$entity
->
field_timestamp
[
0
]
->
value
,
$value
);
// Verify changing the date value.
$new_value
=
1488914000
;
$entity
->
field_timestamp
->
value
=
$new_value
;
$this
->
assertEquals
(
$entity
->
field_timestamp
->
value
,
$new_value
);
// Read changed entity and assert changed values.
$this
->
entityValidateAndSave
(
$entity
);
$entity
=
EntityTest
::
load
(
$id
);
$this
->
assertEquals
(
$entity
->
field_timestamp
->
value
,
$new_value
);
// Test sample item generation.
$entity
=
EntityTest
::
create
();
$entity
->
field_timestamp
->
generateSampleItems
();
$this
->
entityValidateAndSave
(
$entity
);
// Ensure there is sample value a generated for the field.
$this
->
assertNotNull
(
$entity
->
field_timestamp
->
value
);
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment