PageRenderTime 27ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 0ms

/core/modules/field/src/Tests/NestedFormTest.php

http://github.com/drupal/drupal
PHP | 168 lines | 125 code | 17 blank | 26 comment | 0 complexity | d883a1478e19f9d33b627255c951ad79 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. namespace Drupal\field\Tests;
  3. use Drupal\Core\Field\FieldStorageDefinitionInterface;
  4. use Drupal\field\Entity\FieldConfig;
  5. use Drupal\field\Entity\FieldStorageConfig;
  6. /**
  7. * Tests field elements in nested forms.
  8. *
  9. * @group field
  10. */
  11. class NestedFormTest extends FieldTestBase {
  12. /**
  13. * Modules to enable.
  14. *
  15. * @var array
  16. */
  17. public static $modules = array('field_test', 'entity_test');
  18. protected function setUp() {
  19. parent::setUp();
  20. $web_user = $this->drupalCreateUser(array('view test entity', 'administer entity_test content'));
  21. $this->drupalLogin($web_user);
  22. $this->fieldStorageSingle = array(
  23. 'field_name' => 'field_single',
  24. 'entity_type' => 'entity_test',
  25. 'type' => 'test_field',
  26. );
  27. $this->fieldStorageUnlimited = array(
  28. 'field_name' => 'field_unlimited',
  29. 'entity_type' => 'entity_test',
  30. 'type' => 'test_field',
  31. 'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
  32. );
  33. $this->field = array(
  34. 'entity_type' => 'entity_test',
  35. 'bundle' => 'entity_test',
  36. 'label' => $this->randomMachineName() . '_label',
  37. 'description' => '[site:name]_description',
  38. 'weight' => mt_rand(0, 127),
  39. 'settings' => array(
  40. 'test_field_setting' => $this->randomMachineName(),
  41. ),
  42. );
  43. }
  44. /**
  45. * Tests Field API form integration within a subform.
  46. */
  47. function testNestedFieldForm() {
  48. // Add two fields on the 'entity_test'
  49. FieldStorageConfig::create($this->fieldStorageSingle)->save();
  50. FieldStorageConfig::create($this->fieldStorageUnlimited)->save();
  51. $this->field['field_name'] = 'field_single';
  52. $this->field['label'] = 'Single field';
  53. FieldConfig::create($this->field)->save();
  54. entity_get_form_display($this->field['entity_type'], $this->field['bundle'], 'default')
  55. ->setComponent($this->field['field_name'])
  56. ->save();
  57. $this->field['field_name'] = 'field_unlimited';
  58. $this->field['label'] = 'Unlimited field';
  59. FieldConfig::create($this->field)->save();
  60. entity_get_form_display($this->field['entity_type'], $this->field['bundle'], 'default')
  61. ->setComponent($this->field['field_name'])
  62. ->save();
  63. // Create two entities.
  64. $entity_type = 'entity_test';
  65. $storage = $this->container->get('entity_type.manager')
  66. ->getStorage($entity_type);
  67. $entity_1 = $storage->create(['id' => 1]);
  68. $entity_1->enforceIsNew();
  69. $entity_1->field_single->value = 0;
  70. $entity_1->field_unlimited->value = 1;
  71. $entity_1->save();
  72. $entity_2 = $storage->create(array('id' => 2));
  73. $entity_2->enforceIsNew();
  74. $entity_2->field_single->value = 10;
  75. $entity_2->field_unlimited->value = 11;
  76. $entity_2->save();
  77. // Display the 'combined form'.
  78. $this->drupalGet('test-entity/nested/1/2');
  79. $this->assertFieldByName('field_single[0][value]', 0, 'Entity 1: field_single value appears correctly is the form.');
  80. $this->assertFieldByName('field_unlimited[0][value]', 1, 'Entity 1: field_unlimited value 0 appears correctly is the form.');
  81. $this->assertFieldByName('entity_2[field_single][0][value]', 10, 'Entity 2: field_single value appears correctly is the form.');
  82. $this->assertFieldByName('entity_2[field_unlimited][0][value]', 11, 'Entity 2: field_unlimited value 0 appears correctly is the form.');
  83. // Submit the form and check that the entities are updated accordingly.
  84. $edit = array(
  85. 'field_single[0][value]' => 1,
  86. 'field_unlimited[0][value]' => 2,
  87. 'field_unlimited[1][value]' => 3,
  88. 'entity_2[field_single][0][value]' => 11,
  89. 'entity_2[field_unlimited][0][value]' => 12,
  90. 'entity_2[field_unlimited][1][value]' => 13,
  91. );
  92. $this->drupalPostForm(NULL, $edit, t('Save'));
  93. $entity_1 = $storage->load(1);
  94. $entity_2 = $storage->load(2);
  95. $this->assertFieldValues($entity_1, 'field_single', array(1));
  96. $this->assertFieldValues($entity_1, 'field_unlimited', array(2, 3));
  97. $this->assertFieldValues($entity_2, 'field_single', array(11));
  98. $this->assertFieldValues($entity_2, 'field_unlimited', array(12, 13));
  99. // Submit invalid values and check that errors are reported on the
  100. // correct widgets.
  101. $edit = array(
  102. 'field_unlimited[1][value]' => -1,
  103. );
  104. $this->drupalPostForm('test-entity/nested/1/2', $edit, t('Save'));
  105. $this->assertRaw(t('%label does not accept the value -1', array('%label' => 'Unlimited field')), 'Entity 1: the field validation error was reported.');
  106. $error_field = $this->xpath('//input[@id=:id and contains(@class, "error")]', array(':id' => 'edit-field-unlimited-1-value'));
  107. $this->assertTrue($error_field, 'Entity 1: the error was flagged on the correct element.');
  108. $edit = array(
  109. 'entity_2[field_unlimited][1][value]' => -1,
  110. );
  111. $this->drupalPostForm('test-entity/nested/1/2', $edit, t('Save'));
  112. $this->assertRaw(t('%label does not accept the value -1', array('%label' => 'Unlimited field')), 'Entity 2: the field validation error was reported.');
  113. $error_field = $this->xpath('//input[@id=:id and contains(@class, "error")]', array(':id' => 'edit-entity-2-field-unlimited-1-value'));
  114. $this->assertTrue($error_field, 'Entity 2: the error was flagged on the correct element.');
  115. // Test that reordering works on both entities.
  116. $edit = array(
  117. 'field_unlimited[0][_weight]' => 0,
  118. 'field_unlimited[1][_weight]' => -1,
  119. 'entity_2[field_unlimited][0][_weight]' => 0,
  120. 'entity_2[field_unlimited][1][_weight]' => -1,
  121. );
  122. $this->drupalPostForm('test-entity/nested/1/2', $edit, t('Save'));
  123. $this->assertFieldValues($entity_1, 'field_unlimited', array(3, 2));
  124. $this->assertFieldValues($entity_2, 'field_unlimited', array(13, 12));
  125. // Test the 'add more' buttons. Only Ajax submission is tested, because
  126. // the two 'add more' buttons present in the form have the same #value,
  127. // which confuses drupalPostForm().
  128. // 'Add more' button in the first entity:
  129. $this->drupalGet('test-entity/nested/1/2');
  130. $this->drupalPostAjaxForm(NULL, array(), 'field_unlimited_add_more');
  131. $this->assertFieldByName('field_unlimited[0][value]', 3, 'Entity 1: field_unlimited value 0 appears correctly is the form.');
  132. $this->assertFieldByName('field_unlimited[1][value]', 2, 'Entity 1: field_unlimited value 1 appears correctly is the form.');
  133. $this->assertFieldByName('field_unlimited[2][value]', '', 'Entity 1: field_unlimited value 2 appears correctly is the form.');
  134. $this->assertFieldByName('field_unlimited[3][value]', '', 'Entity 1: an empty widget was added for field_unlimited value 3.');
  135. // 'Add more' button in the first entity (changing field values):
  136. $edit = array(
  137. 'entity_2[field_unlimited][0][value]' => 13,
  138. 'entity_2[field_unlimited][1][value]' => 14,
  139. 'entity_2[field_unlimited][2][value]' => 15,
  140. );
  141. $this->drupalPostAjaxForm(NULL, $edit, 'entity_2_field_unlimited_add_more');
  142. $this->assertFieldByName('entity_2[field_unlimited][0][value]', 13, 'Entity 2: field_unlimited value 0 appears correctly is the form.');
  143. $this->assertFieldByName('entity_2[field_unlimited][1][value]', 14, 'Entity 2: field_unlimited value 1 appears correctly is the form.');
  144. $this->assertFieldByName('entity_2[field_unlimited][2][value]', 15, 'Entity 2: field_unlimited value 2 appears correctly is the form.');
  145. $this->assertFieldByName('entity_2[field_unlimited][3][value]', '', 'Entity 2: an empty widget was added for field_unlimited value 3.');
  146. // Save the form and check values are saved correctly.
  147. $this->drupalPostForm(NULL, array(), t('Save'));
  148. $this->assertFieldValues($entity_1, 'field_unlimited', array(3, 2));
  149. $this->assertFieldValues($entity_2, 'field_unlimited', array(13, 14, 15));
  150. }
  151. }