diff -u b/core/modules/field/src/Tests/FieldAssertTrait.php b/core/modules/field/src/Tests/FieldAssertTrait.php --- b/core/modules/field/src/Tests/FieldAssertTrait.php +++ b/core/modules/field/src/Tests/FieldAssertTrait.php @@ -3,7 +3,9 @@ namespace Drupal\field\Tests; use Drupal\Core\Entity\EntityInterface; +use Drupal\Core\Entity\EntityStorageInterface; use Drupal\Core\Language\LanguageInterface; +use Drupal\entity_test\Entity\EntityTest; trait FieldAssertTrait { @@ -43,18 +45,21 @@ */ function assertFieldValues(EntityInterface $entity, $field_name, $expected_values, $langcode = LanguageInterface::LANGCODE_DEFAULT, $column = 'value') { // Re-load the entity to make sure we have the latest changes. + /** @var EntityStorageInterface $storage */ $storage = $this->container->get('entity_type.manager') ->getStorage($entity->getEntityTypeId()); $storage->resetCache([$entity->id()]); + + /** @var EntityTest $e */ $e = $storage->load($entity->id()); $field = $values = $e->getTranslation($langcode)->$field_name; // Filter out empty values so that they don't mess with the assertions. $field->filterEmptyItems(); $values = $field->getValue(); - $this->assertEqual(count($values), count($expected_values), 'Expected number of values were saved.'); + $this->assertEquals(count($values), count($expected_values), 'Expected number of values were saved.'); foreach ($expected_values as $key => $value) { - $this->assertEqual($values[$key][$column], $value, format_string('Value @value was saved correctly.', array('@value' => $value))); + $this->assertEquals($values[$key][$column], $value, format_string('Value @value was saved correctly.', array('@value' => $value))); } } diff -u b/core/modules/field/tests/src/FunctionalJavascript/NestedFormTest.php b/core/modules/field/tests/src/FunctionalJavascript/NestedFormTest.php --- b/core/modules/field/tests/src/FunctionalJavascript/NestedFormTest.php +++ b/core/modules/field/tests/src/FunctionalJavascript/NestedFormTest.php @@ -2,12 +2,14 @@ namespace Drupal\Tests\field\FunctionalJavascript; -use Drupal\FunctionalJavascriptTests\JavascriptTestBase; +use Drupal\Core\Entity\Entity\EntityFormDisplay; use Drupal\Core\Field\FieldStorageDefinitionInterface; +use Drupal\entity_test\Entity\EntityTest; use Drupal\field\Entity\FieldConfig; use Drupal\field\Entity\FieldStorageConfig; use Drupal\field\Tests\FieldAssertTrait; -use Drupal\Core\Entity\Entity\EntityFormDisplay; +use Drupal\FunctionalJavascriptTests\JavascriptTestBase; +use Drupal\Tests\Core\Entity\EntityManagerTestEntity; /** * Tests the JavaScript library caching on consecutive requests. @@ -49,12 +51,12 @@ ]); $this->drupalLogin($account); - $this->fieldStorageSingle = array( + $fieldStorageSingle = array( 'field_name' => 'field_single', 'entity_type' => 'entity_test', 'type' => 'test_field', ); - $this->fieldStorageUnlimited = array( + $fieldStorageUnlimited = array( 'field_name' => 'field_unlimited', 'entity_type' => 'entity_test', 'type' => 'test_field', @@ -73,8 +75,8 @@ ); // Add two fields on the 'entity_test' - FieldStorageConfig::create($this->fieldStorageSingle)->save(); - FieldStorageConfig::create($this->fieldStorageUnlimited)->save(); + FieldStorageConfig::create($fieldStorageSingle)->save(); + FieldStorageConfig::create($fieldStorageUnlimited)->save(); $this->field['field_name'] = 'field_single'; $this->field['label'] = 'Single field'; FieldConfig::create($this->field)->save(); @@ -96,15 +98,19 @@ // Create two entities. $entity_type = 'entity_test'; + + /** @var EntityManagerTestEntity $storage */ $storage = $this->container->get('entity_type.manager') ->getStorage($entity_type); + /** @var EntityTest $entity_1 */ $entity_1 = $storage->create(['id' => 1]); $entity_1->enforceIsNew(); $entity_1->field_single->value = 0; $entity_1->field_unlimited->value = 1; $entity_1->save(); + /** @var EntityTest $entity_2 */ $entity_2 = $storage->create(array('id' => 2)); $entity_2->enforceIsNew(); $entity_2->field_single->value = 10; @@ -119,6 +125,7 @@ // Find the fields in the page/ $field_single = $page->findField('field_single[0][value]'); + $field_unlimited_0 = $page->findField('field_unlimited[0][value]'); $field_unlimited_1 = $page->findField('field_unlimited[1][value]'); $field_entity_2_single = $page->findField('entity_2[field_single][0][value]'); @@ -140,7 +147,8 @@ $field_entity_2_unlimited_1->setValue('13'); // Submit the page. - $page->findButton('Save')->click(); + $button_save = $page->findButton('Save'); + $button_save->click(); // Validate the values in the entities are as expected. $entity_1 = $storage->load(1); @@ -154,16 +162,92 @@ // correct widgets. $field_unlimited_1->setValue('-1'); - $page->findButton('Save')->click(); + $button_save->click(); $assert_session->responseContains(t('%label does not accept the value -1', array('%label' => 'Unlimited field'))); $this->assertTrue($field_unlimited_1->hasClass('error'), 'Entity 1: the error was flagged on the correct element.'); $field_entity_2_unlimited_1->setValue('-1'); - $page->findButton('Save')->click(); + $button_save->click(); $assert_session->responseContains(t('%label does not accept the value -1', array('%label' => 'Unlimited field'))); $this->assertTrue($field_entity_2_unlimited_1->hasClass('error'), 'Entity 2: the error was flagged on the correct element.'); + + // Test that reordering works on both entities. + $this->drupalGet('test-entity/nested/1/2'); + + // Expand the second entity for this test, the elements need to be visible + // to interact with them. + $page->find('css', '#edit-entity-2 summary')->click(); + $this->assertTrue($field_entity_2_unlimited_0->isVisible()); + + // Find the relevant elements. + $field_unlimited_weight_0 = $page->findField('field_unlimited[0][_weight]'); + $field_unlimited_weight_1 = $page->findField('field_unlimited[1][_weight]'); + $field_entity_2_unlimited_weight_0 = $page->findField('entity_2[field_unlimited][0][_weight]'); + $field_entity_2_unlimited_weight_1 = $page->findField('entity_2[field_unlimited][1][_weight]'); + + // Drag on both the base entity and the nested entity. + $field_unlimited_drag_0 = $field_unlimited_weight_0->getParent()->getParent()->getParent()->find('css', '.tabledrag-handle'); + $field_entity_2_unlimited_drag_1 = $field_entity_2_unlimited_weight_1->getParent()->getParent()->getParent()->find('css', '.tabledrag-handle'); + $field_unlimited_drag_0->dragTo($field_unlimited_1); + $field_entity_2_unlimited_drag_1->dragTo($field_entity_2_unlimited_0); + + // Validate the weights are updated accordingly. + $this->assertGreaterThan($field_unlimited_weight_1->getValue(), $field_unlimited_weight_0->getValue()); + $this->assertGreaterThan($field_entity_2_unlimited_weight_0->getValue(), $field_entity_2_unlimited_weight_1->getValue()); + + $button_save->click(); + + // Test the 'add more' buttons. Only Ajax submission is tested, because + // the two 'add more' buttons present in the form have the same #value, + // which confuses drupalPostForm(). + // 'Add more' button in the first entity: + $this->drupalGet('test-entity/nested/1/2'); + + // Expand the second entity for this test, the elements need to be visible + // to interact with them. + $page->find('css', '#edit-entity-2 summary')->click(); + $this->assertTrue($field_entity_2_unlimited_0->isVisible()); + + $add_more_unlimited = $page->find('css', '#edit-field-unlimited-add-more'); + $entity_2_add_more_unlimited = $page->find('css', '#edit-entity-2-field-unlimited-add-more'); + + // Trigger add on the base entity. + $add_more_unlimited->click(); + $assert_session->assertWaitOnAjaxRequest(); + + // Validate the fields are added correctly, retaining the original values. + $field_unlimited_2 = $page->findField('field_unlimited[2][value]'); + $field_unlimited_3 = $page->findField('field_unlimited[3][value]'); + $field_entity_2_unlimited_2 = $page->findField('entity_2[field_unlimited][2][value]'); + $field_entity_2_unlimited_3 = $page->findField('entity_2[field_unlimited][3][value]'); + $this->assertEquals(3, $field_unlimited_0->getValue()); + $this->assertEquals(2, $field_unlimited_1->getValue()); + $this->assertEmpty($field_unlimited_2->getValue()); + $this->assertEmpty($field_unlimited_3->getValue()); + + // Validate the other field doesn't have more fields yet. + $this->assertEmpty($field_entity_2_unlimited_3); + + // 'Add more' button in the first entity (changing field values). + $field_entity_2_unlimited_0->setValue('13'); + $field_entity_2_unlimited_1->setValue('14'); + $field_entity_2_unlimited_2->setValue('15'); + $entity_2_add_more_unlimited->click(); + $assert_session->assertWaitOnAjaxRequest(); + + // Validate the values are as expected after adding more. + $field_entity_2_unlimited_3 = $page->findField('entity_2[field_unlimited][3][value]'); + $this->assertEquals(13, $field_entity_2_unlimited_0->getValue()); + $this->assertEquals(14, $field_entity_2_unlimited_1->getValue()); + $this->assertEquals(15, $field_entity_2_unlimited_2->getValue()); + $this->assertEmpty($field_entity_2_unlimited_3->getValue()); + + // Save the form and check values are saved correctly. + $button_save->click(); + $this->assertFieldValues($entity_1, 'field_unlimited', array(3, 2)); + $this->assertFieldValues($entity_2, 'field_unlimited', array(13, 14, 15)); } }