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

/**
 * @file
 * Definition of Drupal\field\Tests\FormTest.
 */

namespace Drupal\field\Tests;

10
use Drupal\Component\Utility\String;
11
use Drupal\Core\Field\FieldStorageDefinitionInterface;
12
use Drupal\Core\Form\FormState;
13

14
15
/**
 * Tests field form handling.
16
17
 *
 * @group field
18
 */
19
class FormTest extends FieldTestBase {
20

21
22
23
24
25
26
27
28
29
  /**
   * Set to TRUE to strict check all configuration saved.
   *
   * @see \Drupal\Core\Config\Testing\ConfigSchemaChecker
   *
   * @var bool
   */
  protected $strictConfigSchema = TRUE;

30
31
32
33
34
  /**
   * Modules to enable.
   *
   * @var array
   */
35
  public static $modules = array('node', 'field_test', 'options', 'entity_test');
36

37
38
39
40
41
  /**
   * An array of values defining a field single.
   *
   * @var array
   */
42
  protected $fieldStorageSingle;
43
44
45
46
47
48

  /**
   * An array of values defining a field multiple.
   *
   * @var array
   */
49
  protected $fieldStorageMultiple;
50
51
52
53
54
55

  /**
   * An array of values defining a field with unlimited cardinality.
   *
   * @var array
   */
56
  protected $fieldStorageUnlimited;
57
58

  /**
59
   * An array of values defining a field.
60
61
62
   *
   * @var array
   */
63
  protected $field;
64

65
  protected function setUp() {
66
    parent::setUp();
67

68
    $web_user = $this->drupalCreateUser(array('view test entity', 'administer entity_test content'));
69
70
    $this->drupalLogin($web_user);

71
    $this->fieldStorageSingle = array(
72
      'field_name' => 'field_single',
73
74
75
      'entity_type' => 'entity_test',
      'type' => 'test_field',
    );
76
    $this->fieldStorageMultiple = array(
77
      'field_name' => 'field_multiple',
78
79
80
81
      'entity_type' => 'entity_test',
      'type' => 'test_field',
      'cardinality' => 4,
    );
82
    $this->fieldStorageUnlimited = array(
83
      'field_name' => 'field_unlimited',
84
85
      'entity_type' => 'entity_test',
      'type' => 'test_field',
86
      'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
87
    );
88

89
    $this->field = array(
90
91
      'entity_type' => 'entity_test',
      'bundle' => 'entity_test',
92
      'label' => $this->randomMachineName() . '_label',
93
      'description' => '[site:name]_description',
94
95
      'weight' => mt_rand(0, 127),
      'settings' => array(
96
        'test_field_setting' => $this->randomMachineName(),
97
98
99
100
101
      ),
    );
  }

  function testFieldFormSingle() {
102
    $field_storage = $this->fieldStorageSingle;
103
    $field_name = $field_storage['field_name'];
104
    $this->field['field_name'] = $field_name;
105
    entity_create('field_storage_config', $field_storage)->save();
106
107
    entity_create('field_config', $this->field)->save();
    entity_get_form_display($this->field['entity_type'], $this->field['bundle'], 'default')
108
      ->setComponent($field_name)
109
      ->save();
110
111

    // Display creation form.
112
    $this->drupalGet('entity_test/add');
113
114

    // Create token value expected for description.
115
    $token_description = String::checkPlain(\Drupal::config('system.site')->get('name')) . '_description';
116
    $this->assertText($token_description, 'Token replacement for description is displayed');
117
118
    $this->assertFieldByName("{$field_name}[0][value]", '', 'Widget is displayed');
    $this->assertNoField("{$field_name}[1][value]", 'No extraneous widget is displayed');
119

120
121
    // Check that hook_field_widget_form_alter() does not believe this is the
    // default value form.
122
123
    $this->assertNoText('From hook_field_widget_form_alter(): Default form is true.', 'Not default value form in hook_field_widget_form_alter().');

124
    // Submit with invalid value (field-level validation).
125
    $edit = array(
126
      "{$field_name}[0][value]" => -1
127
    );
128
    $this->drupalPostForm(NULL, $edit, t('Save'));
129
    $this->assertRaw(t('%name does not accept the value -1.', array('%name' => $this->field['label'])), 'Field validation fails with invalid input.');
130
131
132
133
    // TODO : check that the correct field is flagged for error.

    // Create an entity
    $value = mt_rand(1, 127);
134
    $edit = array(
135
      "{$field_name}[0][value]" => $value,
136
    );
137
    $this->drupalPostForm(NULL, $edit, t('Save'));
138
    preg_match('|entity_test/manage/(\d+)|', $this->url, $match);
139
    $id = $match[1];
140
141
142
    $this->assertText(t('entity_test @id has been created.', array('@id' => $id)), 'Entity was created');
    $entity = entity_load('entity_test', $id);
    $this->assertEqual($entity->{$field_name}->value, $value, 'Field value was saved');
143
144

    // Display edit form.
145
    $this->drupalGet('entity_test/manage/' . $id);
146
147
    $this->assertFieldByName("{$field_name}[0][value]", $value, 'Widget is displayed with the correct default value');
    $this->assertNoField("{$field_name}[1][value]", 'No extraneous widget is displayed');
148
149
150

    // Update the entity.
    $value = mt_rand(1, 127);
151
    $edit = array(
152
      "{$field_name}[0][value]" => $value,
153
    );
154
    $this->drupalPostForm(NULL, $edit, t('Save'));
155
    $this->assertText(t('entity_test @id has been updated.', array('@id' => $id)), 'Entity was updated');
156
    $this->container->get('entity.manager')->getStorage('entity_test')->resetCache(array($id));
157
158
    $entity = entity_load('entity_test', $id);
    $this->assertEqual($entity->{$field_name}->value, $value, 'Field value was updated');
159
160
161

    // Empty the field.
    $value = '';
162
    $edit = array(
163
      "{$field_name}[0][value]" => $value
164
    );
165
    $this->drupalPostForm('entity_test/manage/' . $id, $edit, t('Save'));
166
    $this->assertText(t('entity_test @id has been updated.', array('@id' => $id)), 'Entity was updated');
167
    $this->container->get('entity.manager')->getStorage('entity_test')->resetCache(array($id));
168
169
    $entity = entity_load('entity_test', $id);
    $this->assertTrue($entity->{$field_name}->isEmpty(), 'Field was emptied');
170
  }
171

172
173
174
175
  /**
   * Tests field widget default values on entity forms.
   */
  function testFieldFormDefaultValue() {
176
    $field_storage = $this->fieldStorageSingle;
177
    $field_name = $field_storage['field_name'];
178
    $this->field['field_name'] = $field_name;
179
    $default = rand(1, 127);
180
    $this->field['default_value'] = array(array('value' => $default));
181
    entity_create('field_storage_config', $field_storage)->save();
182
183
    entity_create('field_config', $this->field)->save();
    entity_get_form_display($this->field['entity_type'], $this->field['bundle'], 'default')
184
      ->setComponent($field_name)
185
      ->save();
186
187

    // Display creation form.
188
    $this->drupalGet('entity_test/add');
189
    // Test that the default value is displayed correctly.
190
    $this->assertFieldByXpath("//input[@name='{$field_name}[0][value]' and @value='$default']");
191
192

    // Try to submit an empty value.
193
    $edit = array(
194
      "{$field_name}[0][value]" => '',
195
    );
196
    $this->drupalPostForm(NULL, $edit, t('Save'));
197
    preg_match('|entity_test/manage/(\d+)|', $this->url, $match);
198
    $id = $match[1];
199
200
201
    $this->assertText(t('entity_test @id has been created.', array('@id' => $id)), 'Entity was created.');
    $entity = entity_load('entity_test', $id);
    $this->assertTrue($entity->{$field_name}->isEmpty(), 'Field is now empty.');
202
203
204
  }

  function testFieldFormSingleRequired() {
205
    $field_storage = $this->fieldStorageSingle;
206
    $field_name = $field_storage['field_name'];
207
208
    $this->field['field_name'] = $field_name;
    $this->field['required'] = TRUE;
209
    entity_create('field_storage_config', $field_storage)->save();
210
211
    entity_create('field_config', $this->field)->save();
    entity_get_form_display($this->field['entity_type'], $this->field['bundle'], 'default')
212
      ->setComponent($field_name)
213
      ->save();
214
215
216

    // Submit with missing required value.
    $edit = array();
217
    $this->drupalPostForm('entity_test/add', $edit, t('Save'));
218
    $this->assertRaw(t('!name field is required.', array('!name' => $this->field['label'])), 'Required field with no value fails validation');
219
220
221

    // Create an entity
    $value = mt_rand(1, 127);
222
    $edit = array(
223
      "{$field_name}[0][value]" => $value,
224
    );
225
    $this->drupalPostForm(NULL, $edit, t('Save'));
226
    preg_match('|entity_test/manage/(\d+)|', $this->url, $match);
227
    $id = $match[1];
228
229
230
    $this->assertText(t('entity_test @id has been created.', array('@id' => $id)), 'Entity was created');
    $entity = entity_load('entity_test', $id);
    $this->assertEqual($entity->{$field_name}->value, $value, 'Field value was saved');
231
232
233

    // Edit with missing required value.
    $value = '';
234
    $edit = array(
235
      "{$field_name}[0][value]" => $value,
236
    );
237
    $this->drupalPostForm('entity_test/manage/' . $id, $edit, t('Save'));
238
    $this->assertRaw(t('!name field is required.', array('!name' => $this->field['label'])), 'Required field with no value fails validation');
239
240
241
242
  }

//  function testFieldFormMultiple() {
//    $this->field = $this->field_multiple;
243
244
//    $field_name = $this->field['field_name'];
//    $this->instance['field_name'] = $field_name;
245
//    entity_create('field_storage_config', $this->field)->save();
246
//    entity_create('field_config', $this->instance)->save();
247
248
249
//  }

  function testFieldFormUnlimited() {
250
    $field_storage = $this->fieldStorageUnlimited;
251
    $field_name = $field_storage['field_name'];
252
    $this->field['field_name'] = $field_name;
253
    entity_create('field_storage_config', $field_storage)->save();
254
255
    entity_create('field_config', $this->field)->save();
    entity_get_form_display($this->field['entity_type'], $this->field['bundle'], 'default')
256
      ->setComponent($field_name)
257
      ->save();
258
259

    // Display creation form -> 1 widget.
260
    $this->drupalGet('entity_test/add');
261
262
    $this->assertFieldByName("{$field_name}[0][value]", '', 'Widget 1 is displayed');
    $this->assertNoField("{$field_name}[1][value]", 'No extraneous widget is displayed');
263
264

    // Press 'add more' button -> 2 widgets.
265
    $this->drupalPostForm(NULL, array(), t('Add another item'));
266
267
268
    $this->assertFieldByName("{$field_name}[0][value]", '', 'Widget 1 is displayed');
    $this->assertFieldByName("{$field_name}[1][value]", '', 'New widget is displayed');
    $this->assertNoField("{$field_name}[2][value]", 'No extraneous widget is displayed');
269
270
271
    // TODO : check that non-field inpurs are preserved ('title')...

    // Yet another time so that we can play with more values -> 3 widgets.
272
    $this->drupalPostForm(NULL, array(), t('Add another item'));
273
274
275
276

    // Prepare values and weights.
    $count = 3;
    $delta_range = $count - 1;
277
    $values = $weights = $pattern = $expected_values = array();
278
    $edit = array();
279
280
281
282
283
284
285
286
    for ($delta = 0; $delta <= $delta_range; $delta++) {
      // Assign unique random values and weights.
      do {
        $value = mt_rand(1, 127);
      } while (in_array($value, $values));
      do {
        $weight = mt_rand(-$delta_range, $delta_range);
      } while (in_array($weight, $weights));
287
288
      $edit["{$field_name}[$delta][value]"] = $value;
      $edit["{$field_name}[$delta][_weight]"] = $weight;
289
290
291
292
293
294
295
296
      // We'll need three slightly different formats to check the values.
      $values[$delta] = $value;
      $weights[$delta] = $weight;
      $field_values[$weight]['value'] = (string) $value;
      $pattern[$weight] = "<input [^>]*value=\"$value\" [^>]*";
    }

    // Press 'add more' button -> 4 widgets
297
    $this->drupalPostForm(NULL, $edit, t('Add another item'));
298
    for ($delta = 0; $delta <= $delta_range; $delta++) {
299
300
      $this->assertFieldByName("{$field_name}[$delta][value]", $values[$delta], "Widget $delta is displayed and has the right value");
      $this->assertFieldByName("{$field_name}[$delta][_weight]", $weights[$delta], "Widget $delta has the right weight");
301
302
303
304
    }
    ksort($pattern);
    $pattern = implode('.*', array_values($pattern));
    $this->assertPattern("|$pattern|s", 'Widgets are displayed in the correct order');
305
306
307
    $this->assertFieldByName("{$field_name}[$delta][value]", '', "New widget is displayed");
    $this->assertFieldByName("{$field_name}[$delta][_weight]", $delta, "New widget has the right weight");
    $this->assertNoField("{$field_name}[" . ($delta + 1) . '][value]', 'No extraneous widget is displayed');
308
309

    // Submit the form and create the entity.
310
    $this->drupalPostForm(NULL, $edit, t('Save'));
311
    preg_match('|entity_test/manage/(\d+)|', $this->url, $match);
312
    $id = $match[1];
313
314
    $this->assertText(t('entity_test @id has been created.', array('@id' => $id)), 'Entity was created');
    $entity = entity_load('entity_test', $id);
315
316
    ksort($field_values);
    $field_values = array_values($field_values);
317
    $this->assertIdentical($entity->{$field_name}->getValue(), $field_values, 'Field values were saved in the correct order');
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332

    // Display edit form: check that the expected number of widgets is
    // displayed, with correct values change values, reorder, leave an empty
    // value in the middle.
    // Submit: check that the entity is updated with correct values
    // Re-submit: check that the field can be emptied.

    // Test with several multiple fields in a form
  }

  /**
   * Tests widget handling of multiple required radios.
   */
  function testFieldFormMultivalueWithRequiredRadio() {
    // Create a multivalue test field.
333
    $field_storage = $this->fieldStorageUnlimited;
334
    $field_name = $field_storage['field_name'];
335
    $this->field['field_name'] = $field_name;
336
    entity_create('field_storage_config', $field_storage)->save();
337
338
    entity_create('field_config', $this->field)->save();
    entity_get_form_display($this->field['entity_type'], $this->field['bundle'], 'default')
339
      ->setComponent($field_name)
340
      ->save();
341
342

    // Add a required radio field.
343
    entity_create('field_storage_config', array(
344
      'field_name' => 'required_radio_test',
345
      'entity_type' => 'entity_test',
346
      'type' => 'list_string',
347
348
349
      'settings' => array(
        'allowed_values' => array('yes' => 'yes', 'no' => 'no'),
      ),
350
    ))->save();
351
    $field = array(
352
      'field_name' => 'required_radio_test',
353
354
      'entity_type' => 'entity_test',
      'bundle' => 'entity_test',
355
      'required' => TRUE,
356
    );
357
358
359
    entity_create('field_config', $field)->save();
    entity_get_form_display($field['entity_type'], $field['bundle'], 'default')
      ->setComponent($field['field_name'], array(
360
361
362
        'type' => 'options_buttons',
      ))
      ->save();
363
364

    // Display creation form.
365
    $this->drupalGet('entity_test/add');
366
367

    // Press the 'Add more' button.
368
    $this->drupalPostForm(NULL, array(), t('Add another item'));
369
370
371
372
373

    // Verify that no error is thrown by the radio element.
    $this->assertNoFieldByXpath('//div[contains(@class, "error")]', FALSE, 'No error message is displayed.');

    // Verify that the widget is added.
374
375
376
    $this->assertFieldByName("{$field_name}[0][value]", '', 'Widget 1 is displayed');
    $this->assertFieldByName("{$field_name}[1][value]", '', 'New widget is displayed');
    $this->assertNoField("{$field_name}[2][value]", 'No extraneous widget is displayed');
377
378
379
  }

  function testFieldFormJSAddMore() {
380
    $field_storage = $this->fieldStorageUnlimited;
381
    $field_name = $field_storage['field_name'];
382
    $this->field['field_name'] = $field_name;
383
    entity_create('field_storage_config', $field_storage)->save();
384
385
    entity_create('field_config', $this->field)->save();
    entity_get_form_display($this->field['entity_type'], $this->field['bundle'], 'default')
386
      ->setComponent($field_name)
387
      ->save();
388
389

    // Display creation form -> 1 widget.
390
    $this->drupalGet('entity_test/add');
391
392

    // Press 'add more' button a couple times -> 3 widgets.
393
    // drupalPostAjaxForm() will not work iteratively, so we add those through
394
    // non-JS submission.
395
396
    $this->drupalPostForm(NULL, array(), t('Add another item'));
    $this->drupalPostForm(NULL, array(), t('Add another item'));
397
398
399
400
401
402
403
404
405
406
407
408
409

    // Prepare values and weights.
    $count = 3;
    $delta_range = $count - 1;
    $values = $weights = $pattern = $expected_values = $edit = array();
    for ($delta = 0; $delta <= $delta_range; $delta++) {
      // Assign unique random values and weights.
      do {
        $value = mt_rand(1, 127);
      } while (in_array($value, $values));
      do {
        $weight = mt_rand(-$delta_range, $delta_range);
      } while (in_array($weight, $weights));
410
411
      $edit["{$field_name}[$delta][value]"] = $value;
      $edit["{$field_name}[$delta][_weight]"] = $weight;
412
413
414
415
416
417
418
419
      // We'll need three slightly different formats to check the values.
      $values[$delta] = $value;
      $weights[$delta] = $weight;
      $field_values[$weight]['value'] = (string) $value;
      $pattern[$weight] = "<input [^>]*value=\"$value\" [^>]*";
    }
    // Press 'add more' button through Ajax, and place the expected HTML result
    // as the tested content.
420
    $commands = $this->drupalPostAjaxForm(NULL, $edit, $field_name . '_add_more');
421
    $this->setRawContent($commands[1]['data']);
422
423

    for ($delta = 0; $delta <= $delta_range; $delta++) {
424
425
      $this->assertFieldByName("{$field_name}[$delta][value]", $values[$delta], "Widget $delta is displayed and has the right value");
      $this->assertFieldByName("{$field_name}[$delta][_weight]", $weights[$delta], "Widget $delta has the right weight");
426
427
428
429
    }
    ksort($pattern);
    $pattern = implode('.*', array_values($pattern));
    $this->assertPattern("|$pattern|s", 'Widgets are displayed in the correct order');
430
431
432
    $this->assertFieldByName("{$field_name}[$delta][value]", '', "New widget is displayed");
    $this->assertFieldByName("{$field_name}[$delta][_weight]", $delta, "New widget has the right weight");
    $this->assertNoField("{$field_name}[" . ($delta + 1) . '][value]', 'No extraneous widget is displayed');
433
434
435
436
437
438
  }

  /**
   * Tests widgets handling multiple values.
   */
  function testFieldFormMultipleWidget() {
439
440
    // Create a field with fixed cardinality, configure the form to use a
    // "multiple" widget.
441
    $field_storage = $this->fieldStorageMultiple;
442
    $field_name = $field_storage['field_name'];
443
    $this->field['field_name'] = $field_name;
444
    entity_create('field_storage_config', $field_storage)->save();
445
446
    entity_create('field_config', $this->field)->save();
    entity_get_form_display($this->field['entity_type'], $this->field['bundle'], 'default')
447
      ->setComponent($field_name, array(
448
449
450
        'type' => 'test_field_widget_multiple',
      ))
      ->save();
451
452

    // Display creation form.
453
    $this->drupalGet('entity_test/add');
454
    $this->assertFieldByName($field_name, '', 'Widget is displayed.');
455
456

    // Create entity with three values.
457
    $edit = array(
458
      $field_name => '1, 2, 3',
459
    );
460
    $this->drupalPostForm(NULL, $edit, t('Save'));
461
    preg_match('|entity_test/manage/(\d+)|', $this->url, $match);
462
463
464
    $id = $match[1];

    // Check that the values were saved.
465
    $entity_init = entity_load('entity_test', $id);
466
    $this->assertFieldValues($entity_init, $field_name, array(1, 2, 3));
467
468

    // Display the form, check that the values are correctly filled in.
469
    $this->drupalGet('entity_test/manage/' . $id);
470
    $this->assertFieldByName($field_name, '1, 2, 3', 'Widget is displayed.');
471
472

    // Submit the form with more values than the field accepts.
473
    $edit = array($field_name => '1, 2, 3, 4, 5');
474
    $this->drupalPostForm(NULL, $edit, t('Save'));
475
    $this->assertRaw('this field cannot hold more than 4 values', 'Form validation failed.');
476
    // Check that the field values were not submitted.
477
    $this->assertFieldValues($entity_init, $field_name, array(1, 2, 3));
478
479
480
481
482
483
  }

  /**
   * Tests fields with no 'edit' access.
   */
  function testFieldFormAccess() {
484
    $entity_type = 'entity_test_rev';
485
    // Create a "regular" field.
486
487
    $field_storage = $this->fieldStorageSingle;
    $field_storage['entity_type'] = $entity_type;
488
    $field_name = $field_storage['field_name'];
489
490
491
492
    $field = $this->field;
    $field['field_name'] = $field_name;
    $field['entity_type'] = $entity_type;
    $field['bundle'] = $entity_type;
493
    entity_create('field_storage_config', $field_storage)->save();
494
    entity_create('field_config', $field)->save();
495
    entity_get_form_display($entity_type, $entity_type, 'default')
496
497
      ->setComponent($field_name)
      ->save();
498

499
500
    // Create a field with no edit access. See
    // field_test_entity_field_access().
501
    $field_storage_no_access = array(
502
      'field_name' => 'field_no_edit_access',
503
      'entity_type' => $entity_type,
504
505
      'type' => 'test_field',
    );
506
    $field_name_no_access = $field_storage_no_access['field_name'];
507
    $field_no_access = array(
508
      'field_name' => $field_name_no_access,
509
510
      'entity_type' => $entity_type,
      'bundle' => $entity_type,
511
512
      'default_value' => array(0 => array('value' => 99)),
    );
513
    entity_create('field_storage_config', $field_storage_no_access)->save();
514
515
    entity_create('field_config', $field_no_access)->save();
    entity_get_form_display($field_no_access['entity_type'], $field_no_access['bundle'], 'default')
516
517
      ->setComponent($field_name_no_access)
      ->save();
518
519
520

    // Test that the form structure includes full information for each delta
    // apart from #access.
521
    $entity = entity_create($entity_type, array('id' => 0, 'revision_id' => 0));
522

523
    $display = entity_get_form_display($entity_type, $entity_type, 'default');
524
    $form = array();
525
    $form_state = new FormState();
526
    $display->buildForm($entity, $form, $form_state);
527
528
529
530

    $this->assertFalse($form[$field_name_no_access]['#access'], 'Field #access is FALSE for the field without edit access.');

    // Display creation form.
531
    $this->drupalGet($entity_type . '/add');
532
    $this->assertNoFieldByName("{$field_name_no_access}[0][value]", '', 'Widget is not displayed if field access is denied.');
533
534

    // Create entity.
535
    $edit = array(
536
      "{$field_name}[0][value]" => 1,
537
    );
538
    $this->drupalPostForm(NULL, $edit, t('Save'));
539
    preg_match("|$entity_type/manage/(\d+)|", $this->url, $match);
540
541
542
    $id = $match[1];

    // Check that the default value was saved.
543
544
545
    $entity = entity_load($entity_type, $id);
    $this->assertEqual($entity->$field_name_no_access->value, 99, 'Default value was saved for the field with no edit access.');
    $this->assertEqual($entity->$field_name->value, 1, 'Entered value vas saved for the field with edit access.');
546
547

    // Create a new revision.
548
    $edit = array(
549
      "{$field_name}[0][value]" => 2,
550
551
      'revision' => TRUE,
    );
552
    $this->drupalPostForm($entity_type . '/manage/' . $id, $edit, t('Save'));
553
554

    // Check that the new revision has the expected values.
555
    $this->container->get('entity.manager')->getStorage($entity_type)->resetCache(array($id));
556
557
558
    $entity = entity_load($entity_type, $id);
    $this->assertEqual($entity->$field_name_no_access->value, 99, 'New revision has the expected value for the field with no edit access.');
    $this->assertEqual($entity->$field_name->value, 2, 'New revision has the expected value for the field with edit access.');
559
560

    // Check that the revision is also saved in the revisions table.
561
562
563
//    $entity = entity_revision_load($entity_type, $entity->getRevisionId());
    $this->assertEqual($entity->$field_name_no_access->value, 99, 'New revision has the expected value for the field with no edit access.');
    $this->assertEqual($entity->$field_name->value, 2, 'New revision has the expected value for the field with edit access.');
564
565
  }

566
  /**
567
   * Tests hiding a field in a form.
568
   */
569
  function testHiddenField() {
570
    $entity_type = 'entity_test_rev';
571
572
    $field_storage = $this->fieldStorageSingle;
    $field_storage['entity_type'] = $entity_type;
573
    $field_name = $field_storage['field_name'];
574
575
576
577
    $this->field['field_name'] = $field_name;
    $this->field['default_value'] = array(0 => array('value' => 99));
    $this->field['entity_type'] = $entity_type;
    $this->field['bundle'] = $entity_type;
578
    entity_create('field_storage_config', $field_storage)->save();
579
580
    $this->field = entity_create('field_config', $this->field);
    $this->field->save();
581
582
    // We explicitly do not assign a widget in a form display, so the field
    // stays hidden in forms.
583
584

    // Display the entity creation form.
585
    $this->drupalGet($entity_type . '/add');
586
587
588

    // Create an entity and test that the default value is assigned correctly to
    // the field that uses the hidden widget.
589
    $this->assertNoField("{$field_name}[0][value]", 'The field does not appear in the form');
590
    $this->drupalPostForm(NULL, array(), t('Save'));
591
    preg_match('|' . $entity_type . '/manage/(\d+)|', $this->url, $match);
592
    $id = $match[1];
593
594
595
    $this->assertText(t('entity_test_rev @id has been created.', array('@id' => $id)), 'Entity was created');
    $entity = entity_load($entity_type, $id);
    $this->assertEqual($entity->{$field_name}->value, 99, 'Default value was saved');
596

597
598
    // Update the field to remove the default value, and switch to the default
    // widget.
599
    $this->field->default_value = array();
600
601
602
    $this->field->save();
    entity_get_form_display($entity_type, $this->field->bundle, 'default')
      ->setComponent($this->field->getName(), array(
603
604
605
        'type' => 'test_field_widget',
      ))
      ->save();
606
607

    // Display edit form.
608
    $this->drupalGet($entity_type . '/manage/' . $id);
609
    $this->assertFieldByName("{$field_name}[0][value]", 99, 'Widget is displayed with the correct default value');
610
611
612

    // Update the entity.
    $value = mt_rand(1, 127);
613
    $edit = array("{$field_name}[0][value]" => $value);
614
    $this->drupalPostForm(NULL, $edit, t('Save'));
615
    $this->assertText(t('entity_test_rev @id has been updated.', array('@id' => $id)), 'Entity was updated');
616
    \Drupal::entityManager()->getStorage($entity_type)->resetCache(array($id));
617
618
    $entity = entity_load($entity_type, $id);
    $this->assertEqual($entity->{$field_name}->value, $value, 'Field value was updated');
619

620
    // Set the field back to hidden.
621
622
    entity_get_form_display($entity_type, $this->field->bundle, 'default')
      ->removeComponent($this->field->getName())
623
      ->save();
624
625
626

    // Create a new revision.
    $edit = array('revision' => TRUE);
627
    $this->drupalPostForm($entity_type . '/manage/' . $id, $edit, t('Save'));
628
629

    // Check that the expected value has been carried over to the new revision.
630
    \Drupal::entityManager()->getStorage($entity_type)->resetCache(array($id));
631
632
    $entity = entity_load($entity_type, $id);
    $this->assertEqual($entity->{$field_name}->value, $value, 'New revision has the expected value for the field with the Hidden widget');
633
  }
634

635
}