Drupal7FieldHandlerTest   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 155
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 2
lcom 1
cbo 1
dl 0
loc 155
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A testFieldHandlers() 0 7 1
B dataProvider() 0 117 1
1
<?php
2
3
namespace Drupal\Tests\Driver;
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);
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit\Framework\Assert::assertArraySubset() has been deprecated with message: https://github.com/sebastianbergmann/phpunit/issues/3494

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
36
  }
37
38
  /**
39
   * Data provider.
40
   *
41
   * @return array
42
   *   An array of test data.
43
   */
44
  public function dataProvider() {
45
    return [
46
      // Test default text field provided as simple text.
47
      [
48
        'DefaultHandler',
49
        (object) ['field_text' => 'Text'],
50
        'node',
51
        ['field_name' => 'field_text'],
52
        ['en' => [['value' => 'Text']]],
53
      ],
54
55
      // Test default text field provided as array.
56
      [
57
        'DefaultHandler',
58
        (object) ['field_text' => ['Text']],
59
        'node',
60
        ['field_name' => 'field_text'],
61
        ['en' => [['value' => 'Text']]],
62
      ],
63
64
      // Test default field handler using custom field columns.
65
      [
66
        'DefaultHandler',
67
        (object) [
68
          'field_addressfield' => [
69
            [
70
              'country' => 'BE',
71
              'locality' => 'Brussels',
72
              'thoroughfare' => 'Grote Markt 1',
73
              'postal_code' => '1000',
74
            ],
75
          ],
76
        ],
77
        'node',
78
        ['field_name' => 'field_addressfield'],
79
        [
80
          'en' => [
81
            [
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
      [
93
        'DatetimeHandler',
94
        (object) ['field_date' => '2015-01-01 00:00:00'],
95
        'node',
96
        ['field_name' => 'field_date'],
97
        ['en' => [['value' => '2015-01-01 00:00:00']]],
98
      ],
99
100
      // Test single-value date field provided as an array.
101
      [
102
        'DatetimeHandler',
103
        (object) ['field_date' => ['2015-01-01 00:00:00']],
104
        'node',
105
        ['field_name' => 'field_date'],
106
        ['en' => [['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
      [
113
        'DatetimeHandler',
114
        (object) [
115
          'field_date' => [
116
            [
117
              '2015-01-01 00:00:00',
118
              '2015-01-02 00:00:00',
119
            ],
120
          ],
121
        ],
122
        'node',
123
        [
124
          'field_name' => 'field_date',
125
          'columns' => ['value' => '', 'value2' => ''],
126
        ],
127
        [
128
          'en' => [
129
            [
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
      [
139
        'ListBooleanHandler',
140
        (object) ['field_list_boolean' => [0]],
141
        'node',
142
        [
143
          'field_name' => 'field_list_boolean',
144
          'settings' => [
145
            'allowed_values' => [
146
              0 => '',
147
              1 => '',
148
            ],
149
          ],
150
        ],
151
        [
152
          'en' => [
153
            [
154
              'value' => 0,
155
            ],
156
          ],
157
        ],
158
      ],
159
    ];
160
  }
161
162
}
163