Skip to content

Commit 10fa78d

Browse files
committed
Add SUT test for d8:field generator
1 parent 0f4cc8e commit 10fa78d

File tree

12 files changed

+450
-36
lines changed

12 files changed

+450
-36
lines changed

scripts/run-sut-tests.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,10 @@ if [ $TARGET_TEST = all -o $TARGET_TEST = module_component ]; then
126126
$DCG d8:layout -d $MODULE_DIR -a '{"machine_name":"bar","layout_name":"Foo","layout_machine_name":"foo","category":"my","js":"Yes","css":"Yes"}'
127127
$DCG d8:render-element -d $MODULE_DIR -a '{"machine_name":"bar"}'
128128

129+
$DCG d8:field -d $MODULE_DIR -a '{"machine_name":"bar","field_label":"Example 1","subfield_count":"10","type_1":"Boolean","type_2":"Text","type_3":"Text (long)","type_4":"Integer","type_5":"Float","type_6":"Numeric","type_7":"Email","type_8":"Telephone","type_9":"Url","type_10":"Date"}'
130+
$DCG d8:field -d $MODULE_DIR -a '{"machine_name":"bar","field_label":"Example 2","subfield_count":"10","type_1":"Boolean","required_1":"Yes","type_2":"Text","list_2":"Yes","required_2":"Yes","type_3":"Text (long)","required_3":"Yes","type_4":"Integer","list_4":"Yes","type_5":"Float","list_5":"Yes","required_5":"Yes","type_6":"Numeric","list_6":"Yes","type_7":"Email","list_7":"Yes","required_7":"Yes","type_8":"Telephone","list_8":"Yes","type_9":"Url","list_9":"Yes","required_9":"Yes","type_10":"Date","list_10":"Yes","date_type_10":"datetime"}'
131+
$DCG d8:field -d $MODULE_DIR -a '{"machine_name":"bar","field_label":"Example 3","subfield_count":"5","type_1":"Boolean","type_2":"Text","type_3":"Text (long)","type_4":"Email","type_5":"Url","storage_settings":"Yes","instance_settings":"Yes","widget_settings":"Yes","formatter_settings":"Yes","table_formatter":"Yes","key_value_formatter":"Yes"}'
132+
129133
dcg_phpcs $MODULE_DIR
130134
dcg_drush en $MODULE_MACHINE_NAME
131135
dcg_phpunit $MODULE_DIR/tests

src/Command/Drupal_8/Field.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,9 @@ protected function interact(InputInterface $input, OutputInterface $output) {
173173
$vars['datetime'] = FALSE;
174174

175175
for ($i = 1; $i <= $vars['subfield_count']; $i++) {
176-
177-
$output->writeln('<fg=green>–––––––––––––––––––––––––––––––––––––––––––––––––––</>');
176+
if (!$input->getOption('answers')) {
177+
$output->writeln('<fg=green>–––––––––––––––––––––––––––––––––––––––––––––––––––</>');
178+
}
178179
$subfield_questions = [];
179180
$subfield_questions['name_' . $i] = new Question("Label for sub-field #$i", "Value $i");
180181
$default_machine_name = function (array $vars) use ($i) {
@@ -264,7 +265,9 @@ protected function interact(InputInterface $input, OutputInterface $output) {
264265

265266
}
266267

267-
$output->writeln('<fg=green>–––––––––––––––––––––––––––––––––––––––––––––––––––</>');
268+
if (!$input->getOption('answers')) {
269+
$output->writeln('<fg=green>–––––––––––––––––––––––––––––––––––––––––––––––––––</>');
270+
}
268271

269272
$questions = [];
270273
$questions['storage_settings'] = new ConfirmationQuestion('Would you like to create field storage settings form?', FALSE);

templates/d8/_field/default-formatter.twig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,10 @@ class {{ formatter_class }} extends FormatterBase {
8787
{%- endset %}
8888
{%- if subfield.type == 'datetime' %}
8989
$date = DrupalDateTime::createFromFormat('{{ subfield.date_storage_format }}', $item->{{ subfield.machine_name }});
90+
// @DCG: Consider injecting the date formatter service.
91+
// @codingStandardsIgnoreStart
9092
$date_formatter = \Drupal::service('date.formatter');
93+
// @codingStandardsIgnoreStart
9194
$timestamp = $date->getTimestamp();
9295
{%- if subfield.list %}
9396
$formatted_date = {{ item_value }};

templates/d8/_field/schema.twig

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ field.storage_settings.{{ field_id }}:
77
foo:
88
type: string
99
label: Foo
10-
1110
{% endif %}
1211
{% if instance_settings %}
1312
# Field instance.
@@ -18,7 +17,6 @@ field.field_settings.{{ field_id }}:
1817
bar:
1918
type: string
2019
label: Bar
21-
2220
{% endif %}
2321
# Default value.
2422
field.value.{{ field_id }}:
@@ -30,7 +28,6 @@ field.value.{{ field_id }}:
3028
type: {{ subfield.type }}
3129
label: {{ subfield.name }}
3230
{% endfor %}
33-
3431
{% if widget_settings %}
3532
# Field widget.
3633
field.widget.settings.{{ field_id }}:
@@ -40,7 +37,6 @@ field.widget.settings.{{ field_id }}:
4037
foo:
4138
type: string
4239
label: Foo
43-
4440
{% endif %}
4541
{% if formatter_settings %}
4642
# Field formatter.
@@ -51,5 +47,4 @@ field.formatter.settings.{{ field_id }}_default:
5147
foo:
5248
type: string
5349
label: Foo
54-
55-
{% endif %}
50+
{% endif %}

templates/d8/_field/table-formatter.twig

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@ use Drupal\Core\Datetime\DrupalDateTime;
88
{%- endif %}
99
use Drupal\Core\Field\FieldItemListInterface;
1010
use Drupal\Core\Field\FormatterBase;
11-
{%- if formatter_settings %}
12-
use Drupal\Core\Form\FormStateInterface;
13-
{%- endif %}
1411
{%- if list %}
1512
use Drupal\{{ machine_name }}\Plugin\Field\FieldType\{{ type_class }};
1613
{%- endif %}

templates/d8/_field/type.twig

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ class {{ type_class }} extends FieldItemBase {
227227
228228
{%- elseif subfield.type == 'float' %}
229229
$scale = rand(1, 5);
230-
$random_decimal = mt_rand() / mt_getrandmax() * (1000 - 0);
230+
$random_decimal = mt_rand() / mt_getrandmax() * (1000 - 0);
231231
$values['{{ subfield.machine_name }}'] = floor($random_decimal * pow(10, $scale)) / pow(10, $scale);
232232
233233
{%- elseif subfield.type == 'numeric' %}
@@ -282,9 +282,9 @@ class {{ type_class }} extends FieldItemBase {
282282
'4.56' => '4.56',
283283
'0.789' => '0.789',
284284
{%- elseif subfield.type == 'numeric' %}
285-
'12.30' => '12.30',
286-
'45.60' => '45.60',
287-
'78.90' => '78.90',
285+
'12.35' => '12.35',
286+
'45.65' => '45.65',
287+
'78.95' => '78.95',
288288
{%- elseif subfield.type == 'email' %}
289289
290290

templates/d8/_field/widget.twig

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class {{ widget_class }} extends WidgetBase {
6565
{% for subfield in subfields %}
6666
{%- set title %}'#title' => $this->t('{{ subfield.name }}'),{% endset %}
6767
{%- set default_value %}'#default_value' => isset($items[$delta]->{{ subfield.machine_name }}) ? $items[$delta]->{{ subfield.machine_name }} : NULL,{% endset %}
68-
{%- set size %}#size' => 15,{% endset %}
68+
{%- set size %}'#size' => 20,{% endset %}
6969
{%- if subfield.list %}
7070
$element['{{ subfield.machine_name }}'] = [
7171
'#type' => 'select',
@@ -100,28 +100,19 @@ class {{ widget_class }} extends WidgetBase {
100100
'#type' => 'number',
101101
{{ title }}
102102
{{ default_value }}
103-
{%- if inline %}
104-
{{ size }}
105-
{%- endif %}
106103
];
107104
{%- elseif subfield.type == 'float' %}
108105
$element['{{ subfield.machine_name }}'] = [
109106
'#type' => 'number',
110107
{{ title }}
111108
{{ default_value }}
112-
{%- if inline %}
113-
{{ size }}
114-
{%- endif %}
115109
'#step' => 0.001,
116110
];
117111
{%- elseif subfield.type == 'numeric' %}
118112
$element['{{ subfield.machine_name }}'] = [
119113
'#type' => 'number',
120114
{{ title }}
121115
{{ default_value }}
122-
{%- if inline %}
123-
{{ size }}
124-
{%- endif %}
125116
'#step' => 0.01,
126117
];
127118
{%- elseif subfield.type == 'email' %}

tests/dcg/Generator/Drupal_8/_field/_default_formatter.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,10 @@ public function viewElements(FieldItemListInterface $items, $langcode) {
229229

230230
if ($item->value_19) {
231231
$date = DrupalDateTime::createFromFormat('Y-m-d', $item->value_19);
232+
// @DCG: Consider injecting the date formatter service.
233+
// @codingStandardsIgnoreStart
232234
$date_formatter = \Drupal::service('date.formatter');
235+
// @codingStandardsIgnoreStart
233236
$timestamp = $date->getTimestamp();
234237
$formatted_date = $date_formatter->format($timestamp, 'long');
235238
$iso_date = $date_formatter->format($timestamp, 'custom', 'Y-m-d\TH:i:s') . 'Z';
@@ -255,7 +258,10 @@ public function viewElements(FieldItemListInterface $items, $langcode) {
255258
if ($item->value_20) {
256259
$allowed_values = FooItem::allowedValue20Values();
257260
$date = DrupalDateTime::createFromFormat('Y-m-d', $item->value_20);
261+
// @DCG: Consider injecting the date formatter service.
262+
// @codingStandardsIgnoreStart
258263
$date_formatter = \Drupal::service('date.formatter');
264+
// @codingStandardsIgnoreStart
259265
$timestamp = $date->getTimestamp();
260266
$formatted_date = $allowed_values[$item->value_20];
261267
$iso_date = $date_formatter->format($timestamp, 'custom', 'Y-m-d\TH:i:s') . 'Z';

tests/dcg/Generator/Drupal_8/_field/_schema.yml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ field.storage_settings.example_foo:
66
foo:
77
type: string
88
label: Foo
9-
109
# Field instance.
1110
field.field_settings.example_foo:
1211
type: mapping
@@ -15,7 +14,6 @@ field.field_settings.example_foo:
1514
bar:
1615
type: string
1716
label: Bar
18-
1917
# Default value.
2018
field.value.example_foo:
2119
type: mapping
@@ -81,7 +79,6 @@ field.value.example_foo:
8179
value_20:
8280
type: datetime
8381
label: Value 20
84-
8582
# Field widget.
8683
field.widget.settings.example_foo:
8784
type: mapping
@@ -90,7 +87,6 @@ field.widget.settings.example_foo:
9087
foo:
9188
type: string
9289
label: Foo
93-
9490
# Field formatter.
9591
field.formatter.settings.example_foo_default:
9692
type: mapping
@@ -99,4 +95,3 @@ field.formatter.settings.example_foo_default:
9995
foo:
10096
type: string
10197
label: Foo
102-

tests/dcg/Generator/Drupal_8/_field/_table_formatter.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use Drupal\Core\Datetime\DrupalDateTime;
66
use Drupal\Core\Field\FieldItemListInterface;
77
use Drupal\Core\Field\FormatterBase;
8-
use Drupal\Core\Form\FormStateInterface;
98
use Drupal\example\Plugin\Field\FieldType\FooItem;
109

1110
/**

0 commit comments

Comments
 (0)