Projet

Général

Profil

Paste
Télécharger (4,57 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / date / tests / DateEmwTestCase.test @ 599a39cd

1
<?php
2

    
3
/**
4
 * @file
5
 * Test Date (ISO) and the Entity Metadata Wrappers play well together.
6
 */
7

    
8
/**
9
 * Test Date (ISO) and the Entity Metadata Wrappers play well together.
10
 */
11
class DateEmwTestCase extends DrupalWebTestCase {
12

    
13
  /**
14
   * Info about this test.
15
   */
16
  public static function getInfo() {
17
    return array(
18
      'name' => 'Date Entity Metadata Wrappers',
19
      'description' => 'Ensure default timezone setting in metadata wrapper integration is working as expected.',
20
      'group' => 'Date',
21
      'dependencies' => array('entity'),
22
    );
23
  }
24

    
25
  /**
26
   * Set up a user and a content type with a Date (ISO) field.
27
   */
28
  public function setUp(array $modules = array()) {
29
    // Define the dependencies.
30
    $modules[] = 'date_api';
31
    $modules[] = 'date';
32
    $modules[] = 'field';
33
    $modules[] = 'field_ui';
34
    $modules[] = 'entity';
35
    $modules[] = 'entity_token';
36
    parent::setUp($modules);
37

    
38
    // Select a specific default timezone, and turn off user-configurable
39
    // timezone handling.
40
    variable_set('date_default_timezone', 'Pacific/Honolulu');
41
    variable_set('configurable_timezones', 0);
42

    
43
    // Create a privileged user and log in with it.
44
    $this->privileged_user = $this->drupalCreateUser(array(
45
      'administer site configuration',
46
      'administer content types',
47
      'administer nodes',
48
      'bypass node access',
49
      'administer fields',
50
    ));
51
    $this->drupalLogin($this->privileged_user);
52

    
53
    // Create a content type to which we can add our date fields.
54
    $edit = array();
55
    $edit['name'] = 'Story';
56
    $edit['type'] = 'story';
57
    $this->drupalPost('admin/structure/types/add', $edit, t('Save content type'));
58
    $this->assertText('The content type Story has been added.', 'Content type added.');
59

    
60
    // Add each of our date field types.
61
    $this->date_types = array(
62
      'date' => 'Date (ISO format)',
63
      'datetime' => 'Date',
64
      'datestamp' => 'Date (Unix timestamp)',
65
    );
66
    foreach ($this->date_types as $type => $label) {
67
      $field_name_for_human = 'test_' . $type;
68

    
69
      // Add the field.
70
      $field = array();
71
      $field['fields[_add_new_field][label]'] = $label;
72
      $field['fields[_add_new_field][field_name]'] = $field_name_for_human;
73
      $field['fields[_add_new_field][type]'] = $type;
74
      $field['fields[_add_new_field][widget_type]'] = 'date_text';
75
      $this->drupalPost('admin/structure/types/manage/story/fields', $field, 'Save');
76
      $this->assertText('These settings apply to the ' . $field['fields[_add_new_field][label]'] . ' field everywhere it is used.', $label . ' field added to content type.');
77

    
78
      // Set the timezone handling to 'none'.
79
      $field = array();
80
      $field['field[settings][tz_handling]'] = 'none';
81
      $this->drupalPost('admin/structure/types/manage/story/fields/field_' . $field_name_for_human, $field, 'Save settings');
82
      $this->assertText('Saved ' . $label . ' configuration.');
83
    }
84
  }
85

    
86
  /**
87
   * Test the edge case where timezone_db is empty.
88
   *
89
   * In this case the value returned by entity metadata wrappers is not the same
90
   * as that which is stored in the node.
91
   *
92
   * @see https://www.drupal.org/node/2123039
93
   */
94
  public function testCheckEntityMetadataWrapper() {
95
    // Create and save a edit.
96
    $edit = array();
97
    $edit['title'] = 'Testing dates';
98
    $edit['type'] = 'story';
99
    $this->drupalCreateNode($edit);
100
    // Fetch the Node.
101
    $node = $this->drupalGetNodeByTitle($edit['title']);
102
    // Prepare variables for token replacement.
103
    $variables['node'] = $node;
104
    // At this point we have a node with the appropriate field types available.
105
    $wrapper = entity_metadata_wrapper('node', $node);
106
    $original = '1482721671';
107
    foreach ($this->date_types as $type => $label) {
108
      $field = 'field_test_' . $type;
109
      $wrapper->{$field} = $original;
110
      $from_metadata_wrapper = $wrapper->{$field}->value();
111

    
112
      $this->assertEqual(
113
        $original,
114
        $from_metadata_wrapper,
115
        t(
116
          '!type plays nicely with Entity Metadata Wrappers. !original == !fetched',
117
          array(
118
            '!type' => $label,
119
            '!original' => $original,
120
            '!fetched' => $from_metadata_wrapper,
121
          )
122
        )
123
      );
124

    
125
      $token = "[node:field-test-${type}:raw]";
126
      $from_token = token_replace($token, $variables);
127

    
128
      $this->assertEqual(
129
        $original,
130
        $from_token,
131
        t(
132
          '!type plays nicely with Entity tokens. !original == !fetched',
133
          array(
134
            '!type' => $label,
135
            '!original' => $original,
136
            '!fetched' => $from_token,
137
          )
138
        )
139
      );
140
    }
141
  }
142

    
143
}