DateTimeItemTest.php 3.93 KB
Newer Older
1
2
3
4
5
6
7
8
9
<?php

/**
 * @file
 * Contains \Drupal\datetime\Tests\DateTimeItemTest.
 */

namespace Drupal\datetime\Tests;

10
11
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FieldItemInterface;
12
13
14
15
use Drupal\field\Tests\FieldUnitTestBase;

/**
 * Tests the new entity API for the date field type.
16
17
 *
 * @group datetime
18
19
20
21
22
23
24
25
26
27
 */
class DateTimeItemTest extends FieldUnitTestBase {

  /**
   * Modules to enable.
   *
   * @var array
   */
  public static $modules = array('datetime');

28
  protected function setUp() {
29
30
31
    parent::setUp();

    // Create a field with settings to validate.
32
    $field_storage = entity_create('field_storage_config', array(
33
      'field_name' => 'field_datetime',
34
      'type' => 'datetime',
35
      'entity_type' => 'entity_test',
36
37
      'settings' => array('datetime_type' => 'date'),
    ));
38
    $field_storage->save();
39
    $field = entity_create('field_config', array(
40
      'field_storage' => $field_storage,
41
42
43
44
45
      'bundle' => 'entity_test',
      'settings' => array(
        'default_value' => 'blank',
      ),
    ));
46
    $field->save();
47
48
49
50
51
52
53
  }

  /**
   * Tests using entity fields of the date field type.
   */
  public function testDateTimeItem() {
    // Verify entity creation.
54
    $entity = entity_create('entity_test');
55
56
    $value = '2014-01-01T20:00:00Z';
    $entity->field_datetime = $value;
57
    $entity->name->value = $this->randomMachineName();
58
59
60
61
62
    $entity->save();

    // Verify entity has been created properly.
    $id = $entity->id();
    $entity = entity_load('entity_test', $id);
63
    $this->assertTrue($entity->field_datetime instanceof FieldItemListInterface, 'Field implements interface.');
64
65
66
67
68
    $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.
69
    $new_value = $this->randomMachineName();
70
71
72
73
74
75
76
    $entity->field_datetime->value = $new_value;
    $this->assertEqual($entity->field_datetime->value, $new_value);

    // Read changed entity and assert changed values.
    $entity->save();
    $entity = entity_load('entity_test', $id);
    $this->assertEqual($entity->field_datetime->value, $new_value);
77
78
79
80
81

    // Test the generateSampleValue() method.
    $entity = entity_create('entity_test');
    $entity->field_datetime->generateSampleItems();
    $this->entityValidateAndSave($entity);
82
83
84
85
86
87
88
  }

  /**
   * Tests DateTimeItem::setValue().
   */
  public function testSetValue() {
    // Test DateTimeItem::setValue() using string.
89
    $entity = entity_create('entity_test');
90
    $value = '2014-01-01T20:00:00Z';
91
    $entity->get('field_datetime')->set(0, $value);
92
93
94
95
96
97
98
    $entity->save();
    // Load the entity and ensure the field was saved correctly.
    $id = $entity->id();
    $entity = entity_load('entity_test', $id);
    $this->assertEqual($entity->field_datetime[0]->value, $value, 'DateTimeItem::setValue() works with string value.');

    // Test DateTimeItem::setValue() using property array.
99
    $entity = entity_create('entity_test');
100
    $value = '2014-01-01T20:00:00Z';
101
    $entity->set('field_datetime', $value);
102
103
104
105
106
107
108
109
110
111
112
113
    $entity->save();
    // Load the entity and ensure the field was saved correctly.
    $id = $entity->id();
    $entity = entity_load('entity_test', $id);
    $this->assertEqual($entity->field_datetime[0]->value, $value, 'DateTimeItem::setValue() works with array value.');
  }

  /**
   * Tests setting the value of the DateTimeItem directly.
   */
  public function testSetValueProperty() {
    // Test Date::setValue().
114
    $entity = entity_create('entity_test');
115
116
    $value = '2014-01-01T20:00:00Z';

117
    $entity->set('field_datetime', $value);
118
119
120
121
122
123
124
125
    $entity->save();
    // Load the entity and ensure the field was saved correctly.
    $id = $entity->id();
    $entity = entity_load('entity_test', $id);
    $this->assertEqual($entity->field_datetime[0]->value, $value, '"Value" property can be set directly.');
  }

}