function EntityFormTest::testCopyFormValuesToEntity

Same name and namespace in other branches
  1. 9 core/tests/Drupal/Tests/Core/Entity/EntityFormTest.php \Drupal\Tests\Core\Entity\EntityFormTest::testCopyFormValuesToEntity()
  2. 8.9.x core/tests/Drupal/Tests/Core/Entity/EntityFormTest.php \Drupal\Tests\Core\Entity\EntityFormTest::testCopyFormValuesToEntity()
  3. 11.x core/tests/Drupal/Tests/Core/Entity/EntityFormTest.php \Drupal\Tests\Core\Entity\EntityFormTest::testCopyFormValuesToEntity()

@covers ::copyFormValuesToEntity

File

core/tests/Drupal/Tests/Core/Entity/EntityFormTest.php, line 123

Class

EntityFormTest
@coversDefaultClass \Drupal\Core\Entity\EntityForm[[api-linebreak]] @group Entity

Namespace

Drupal\Tests\Core\Entity

Code

public function testCopyFormValuesToEntity() : void {
  $entity_id = 'test_config_entity_id';
  $values = [
    'id' => $entity_id,
  ];
  $entity = $this->getMockBuilder('\\Drupal\\Tests\\Core\\Config\\Entity\\Fixtures\\ConfigEntityBaseWithPluginCollections')
    ->setConstructorArgs([
    $values,
    'test_config_entity',
  ])
    ->onlyMethods([
    'getPluginCollections',
  ])
    ->getMock();
  $entity->expects($this->atLeastOnce())
    ->method('getPluginCollections')
    ->willReturn([
    'key_controlled_by_plugin_collection' => NULL,
  ]);
  $this->entityForm
    ->setEntity($entity);
  $form_state = (new FormState())->setValues([
    'regular_key' => 'foo',
    'key_controlled_by_plugin_collection' => 'bar',
  ]);
  $result = $this->entityForm
    ->buildEntity([], $form_state);
  $this->assertSame($entity_id, $result->id());
  // The regular key should have a value, but the one controlled by a plugin
  // collection should not have been set.
  $this->assertSame('foo', $result->get('regular_key'));
  $this->assertNull($result->get('key_controlled_by_plugin_collection'));
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.