Completed
Pull Request — master (#157)
by
unknown
12:36
created

Drupal7FieldHandlerTest::testFieldHandlers()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 5
1
<?php
2
3
namespace Drupal\Tests\Driver\Unit;
4
5
/**
6
 * Tests the Drupal 7 field handlers.
7
 */
8
class Drupal7FieldHandlerTest extends FieldHandlerAbstractTest {
9
10
  /**
11
   * Tests the field handlers.
12
   *
13
   * @param string $class_name
14
   *   The name of the field handler class under test.
15
   * @param object $entity
16
   *   An object representing an entity. Should contain a single property which
17
   *   represents a field containing a value.
18
   * @param string $entity_type
19
   *   The entity type under test.
20
   * @param array $field
21
   *   An associative array with the following keys:
22
   *   - 'field_name': the field name that is used for the property on $entity.
23
   *   - 'columns': an optional array containing the column names of the field
24
   *     as keys.
25
   * @param array $expected_values
26
   *   The values in the expected format after expansion.
27
   *
28
   * @dataProvider dataProvider
29
   */
30
  public function testFieldHandlers($class_name, $entity, $entity_type, array $field, array $expected_values) {
31
    $handler = $this->getMockHandler($class_name, $entity, $entity_type, $field);
32
33
    $field_name = $field['field_name'];
34
    $expanded_values = $handler->expand($this->values($entity->$field_name));
0 ignored issues
show
Bug introduced by
The method expand() does not seem to exist on object<Mockery\MockInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
35
    $this->assertArraySubset($expected_values, $expanded_values);
36
  }
37
38
  /**
39
   * Data provider.
40
   *
41
   * @return array
42
   *   An array of test data.
43
   */
44
  public function dataProvider() {
45
    return array(
46
      // Test default text field provided as simple text.
47
      array(
48
        'DefaultHandler',
49
        (object) array('field_text' => 'Text'),
50
        'node',
51
        array('field_name' => 'field_text'),
52
        array('en' => array(array('value' => 'Text'))),
53
      ),
54
55
      // Test default text field provided as array.
56
      array(
57
        'DefaultHandler',
58
        (object) array('field_text' => array('Text')),
59
        'node',
60
        array('field_name' => 'field_text'),
61
        array('en' => array(array('value' => 'Text'))),
62
      ),
63
64
      // Test default field handler using custom field columns.
65
      array(
66
        'DefaultHandler',
67
        (object) array(
68
          'field_addressfield' => array(
69
            array(
70
              'country' => 'BE',
71
              'locality' => 'Brussels',
72
              'thoroughfare' => 'Grote Markt 1',
73
              'postal_code' => '1000',
74
            ),
75
          ),
76
        ),
77
        'node',
78
        array('field_name' => 'field_addressfield'),
79
        array(
80
          'en' => array(
81
            array(
82
              'country' => 'BE',
83
              'locality' => 'Brussels',
84
              'thoroughfare' => 'Grote Markt 1',
85
              'postal_code' => '1000',
86
            ),
87
          ),
88
        ),
89
      ),
90
91
      // Test single-value date field provided as simple text.
92
      array(
93
        'DatetimeHandler',
94
        (object) array('field_date' => '2015-01-01 00:00:00'),
95
        'node',
96
        array('field_name' => 'field_date'),
97
        array('en' => array(array('value' => '2015-01-01 00:00:00'))),
98
      ),
99
100
      // Test single-value date field provided as an array.
101
      array(
102
        'DatetimeHandler',
103
        (object) array('field_date' => array('2015-01-01 00:00:00')),
104
        'node',
105
        array('field_name' => 'field_date'),
106
        array('en' => array(array('value' => '2015-01-01 00:00:00'))),
107
      ),
108
109
      // Test double-value date field. Can only be provided as an array
110
      // due to array type casting we perform in
111
      // \Drupal\Driver\Fields\Drupal7\AbstractFieldHandler::__call()
112
      array(
113
        'DatetimeHandler',
114
        (object) array(
115
          'field_date' => array(
116
            array(
117
              '2015-01-01 00:00:00',
118
              '2015-01-02 00:00:00',
119
            ),
120
          ),
121
        ),
122
        'node',
123
        array(
124
          'field_name' => 'field_date',
125
          'columns' => array('value' => '', 'value2' => ''),
126
        ),
127
        array(
128
          'en' => array(
129
            array(
130
              'value' => '2015-01-01 00:00:00',
131
              'value2' => '2015-01-02 00:00:00',
132
            ),
133
          ),
134
        ),
135
      ),
136
137
      // Test list boolean field with blank 'On' and 'Off' values.
138
      array(
139
        'ListBooleanHandler',
140
        (object) array('field_list_boolean' => array(0)),
141
        'node',
142
        array(
143
          'field_name' => 'field_list_boolean',
144
          'settings' => array(
145
            'allowed_values' => array(
146
              0 => '',
147
              1 => '',
148
            ),
149
          ),
150
        ),
151
        array(
152
          'en' => array(
153
            array(
154
              'value' => 0,
155
            ),
156
          ),
157
        ),
158
      ),
159
    );
160
  }
161
162
}
163