class EntityFormTest

Same name in this branch
  1. 11.x core/modules/system/tests/src/Functional/Entity/EntityFormTest.php \Drupal\Tests\system\Functional\Entity\EntityFormTest
Same name and namespace in other branches
  1. 9 core/modules/system/tests/src/Functional/Entity/EntityFormTest.php \Drupal\Tests\system\Functional\Entity\EntityFormTest
  2. 9 core/tests/Drupal/Tests/Core/Entity/EntityFormTest.php \Drupal\Tests\Core\Entity\EntityFormTest
  3. 8.9.x core/modules/system/tests/src/Functional/Entity/EntityFormTest.php \Drupal\Tests\system\Functional\Entity\EntityFormTest
  4. 8.9.x core/tests/Drupal/Tests/Core/Entity/EntityFormTest.php \Drupal\Tests\Core\Entity\EntityFormTest
  5. 10 core/modules/system/tests/src/Functional/Entity/EntityFormTest.php \Drupal\Tests\system\Functional\Entity\EntityFormTest
  6. 10 core/tests/Drupal/Tests/Core/Entity/EntityFormTest.php \Drupal\Tests\Core\Entity\EntityFormTest

@coversDefaultClass \Drupal\Core\Entity\EntityForm
@group Entity

Hierarchy

Expanded class hierarchy of EntityFormTest

File

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

Namespace

Drupal\Tests\Core\Entity
View source
class EntityFormTest extends UnitTestCase {
  
  /**
   * The mocked entity form.
   *
   * @var \Drupal\Core\Entity\EntityFormInterface|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $entityForm;
  
  /**
   * A fake entity type used in the test.
   *
   * @var \Drupal\Core\Entity\EntityTypeInterface
   */
  protected $entityType;
  
  /**
   * {@inheritdoc}
   */
  protected function setUp() : void {
    parent::setUp();
    $this->entityForm = new EntityForm();
    $this->entityType = new EntityType([
      'id' => 'entity_test',
    ]);
  }
  
  /**
   * Tests the form ID generation.
   *
   * @covers ::getFormId
   *
   * @dataProvider providerTestFormIds
   */
  public function testFormId($expected, $definition) : void {
    $this->entityType
      ->set('entity_keys', [
      'bundle' => $definition['bundle'],
    ]);
    $entity = $this->getMockBuilder(StubEntityBase::class)
      ->setConstructorArgs([
      [],
      $definition['entity_type'],
    ])
      ->onlyMethods([
      'getEntityType',
      'bundle',
    ])
      ->getMock();
    $entity->expects($this->any())
      ->method('getEntityType')
      ->willReturn($this->entityType);
    $entity->expects($this->any())
      ->method('bundle')
      ->willReturn($definition['bundle']);
    $this->entityForm
      ->setEntity($entity);
    $this->entityForm
      ->setOperation($definition['operation']);
    $this->assertSame($expected, $this->entityForm
      ->getFormId());
  }
  
  /**
   * Provides test data for testFormId().
   */
  public static function providerTestFormIds() {
    return [
      [
        'node_article_form',
        [
          'entity_type' => 'node',
          'bundle' => 'article',
          'operation' => 'default',
        ],
      ],
      [
        'node_article_delete_form',
        [
          'entity_type' => 'node',
          'bundle' => 'article',
          'operation' => 'delete',
        ],
      ],
      [
        'user_user_form',
        [
          'entity_type' => 'user',
          'bundle' => 'user',
          'operation' => 'default',
        ],
      ],
      [
        'user_form',
        [
          'entity_type' => 'user',
          'bundle' => '',
          'operation' => 'default',
        ],
      ],
      [
        'user_delete_form',
        [
          'entity_type' => 'user',
          'bundle' => '',
          'operation' => 'delete',
        ],
      ],
    ];
  }
  
  /**
   * @covers ::copyFormValuesToEntity
   */
  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'));
  }
  
  /**
   * Tests EntityForm::getEntityFromRouteMatch() for edit and delete forms.
   *
   * @covers ::getEntityFromRouteMatch
   */
  public function testGetEntityFromRouteMatchEditDelete() : void {
    $entity = $this->prophesize(EntityInterface::class)
      ->reveal();
    $id = $this->entityType
      ->id();
    $route_match = new RouteMatch('test_route', new Route('/entity-test/manage/{' . $id . '}/edit'), [
      $id => $entity,
    ], [
      $id => 1,
    ]);
    $actual = $this->entityForm
      ->getEntityFromRouteMatch($route_match, $id);
    $this->assertEquals($entity, $actual);
  }
  
  /**
   * Tests EntityForm::getEntityFromRouteMatch() for add forms without a bundle.
   *
   * @covers ::getEntityFromRouteMatch
   */
  public function testGetEntityFromRouteMatchAdd() : void {
    $entity = $this->prophesize(EntityInterface::class)
      ->reveal();
    $this->setUpStorage()
      ->create([])
      ->willReturn($entity);
    $route_match = new RouteMatch('test_route', new Route('/entity-test/add'));
    $actual = $this->entityForm
      ->getEntityFromRouteMatch($route_match, $this->entityType
      ->id());
    $this->assertEquals($entity, $actual);
  }
  
  /**
   * Tests EntityForm::getEntityFromRouteMatch() with a static bundle.
   *
   * @covers ::getEntityFromRouteMatch
   */
  public function testGetEntityFromRouteMatchAddStatic() : void {
    $entity = $this->prophesize(EntityInterface::class)
      ->reveal();
    $bundle_key = 'bundle';
    $bundle = 'test_bundle';
    $this->entityType
      ->set('entity_keys', [
      'bundle' => $bundle_key,
    ]);
    $storage = $this->setUpStorage();
    // Test without a bundle parameter in the route.
    $storage->create([])
      ->willReturn($entity);
    $route_match = new RouteMatch('test_route', new Route('/entity-test/add'));
    $actual = $this->entityForm
      ->getEntityFromRouteMatch($route_match, $this->entityType
      ->id());
    $this->assertEquals($entity, $actual);
    // Test with a static bundle parameter.
    $storage->create([
      $bundle_key => 'test_bundle',
    ])
      ->willReturn($entity);
    $route_match = new RouteMatch('test_route', new Route('/entity-test/add/{' . $bundle_key . '}'), [
      $bundle_key => $bundle,
    ], [
      $bundle_key => $bundle,
    ]);
    $actual = $this->entityForm
      ->getEntityFromRouteMatch($route_match, $this->entityType
      ->id());
    $this->assertEquals($entity, $actual);
  }
  
  /**
   * Tests EntityForm::getEntityFromRouteMatch() with a config entity bundle.
   *
   * @covers ::getEntityFromRouteMatch
   */
  public function testGetEntityFromRouteMatchAddEntity() : void {
    $entity = $this->prophesize(EntityInterface::class)
      ->reveal();
    $bundle_entity_type_id = 'entity_test_bundle';
    $bundle = 'test_entity_bundle';
    $this->entityType
      ->set('bundle_entity_type', $bundle_entity_type_id);
    $storage = $this->setUpStorage();
    // Test without a bundle parameter in the route.
    $storage->create([])
      ->willReturn($entity);
    $route_match = new RouteMatch('test_route', new Route('/entity-test/add'));
    $actual = $this->entityForm
      ->getEntityFromRouteMatch($route_match, $this->entityType
      ->id());
    $this->assertEquals($entity, $actual);
    // Test with an entity bundle parameter.
    $storage->create([
      'bundle' => $bundle,
    ])
      ->willReturn($entity);
    $bundle_entity = $this->prophesize(EntityInterface::class);
    $bundle_entity->id()
      ->willReturn('test_entity_bundle');
    $route_match = new RouteMatch('test_route', new Route('/entity-test/add/{entity_test_bundle}'), [
      $bundle_entity_type_id => $bundle_entity->reveal(),
    ], [
      $bundle_entity_type_id => $bundle,
    ]);
    $actual = $this->entityForm
      ->getEntityFromRouteMatch($route_match, $this->entityType
      ->id());
    $this->assertEquals($entity, $actual);
  }
  
  /**
   * Sets up the storage accessed via the entity type manager in the form.
   *
   * @return \Prophecy\Prophecy\ObjectProphecy<\Drupal\Core\Entity\EntityStorageInterface>
   *   The storage prophecy.
   */
  protected function setUpStorage() : ObjectProphecy {
    $storage = $this->prophesize(EntityStorageInterface::class);
    $entity_type_manager = $this->prophesize(EntityTypeManagerInterface::class);
    $entity_type_manager->getDefinition($this->entityType
      ->id())
      ->willReturn($this->entityType);
    $entity_type_manager->getStorage($this->entityType
      ->id())
      ->willReturn($storage->reveal());
    $this->entityForm
      ->setEntityTypeManager($entity_type_manager->reveal());
    return $storage;
  }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
EntityFormTest::$entityForm protected property The mocked entity form.
EntityFormTest::$entityType protected property A fake entity type used in the test.
EntityFormTest::providerTestFormIds public static function Provides test data for testFormId().
EntityFormTest::setUp protected function Overrides UnitTestCase::setUp
EntityFormTest::setUpStorage protected function Sets up the storage accessed via the entity type manager in the form.
EntityFormTest::testCopyFormValuesToEntity public function @covers ::copyFormValuesToEntity[[api-linebreak]]
EntityFormTest::testFormId public function Tests the form ID generation.
EntityFormTest::testGetEntityFromRouteMatchAdd public function Tests EntityForm::getEntityFromRouteMatch() for add forms without a bundle.
EntityFormTest::testGetEntityFromRouteMatchAddEntity public function Tests EntityForm::getEntityFromRouteMatch() with a config entity bundle.
EntityFormTest::testGetEntityFromRouteMatchAddStatic public function Tests EntityForm::getEntityFromRouteMatch() with a static bundle.
EntityFormTest::testGetEntityFromRouteMatchEditDelete public function Tests EntityForm::getEntityFromRouteMatch() for edit and delete forms.
ExpectDeprecationTrait::expectDeprecation public function Adds an expected deprecation.
ExpectDeprecationTrait::setUpErrorHandler public function Sets up the test error handler.
ExpectDeprecationTrait::tearDownErrorHandler public function Tears down the test error handler.
RandomGeneratorTrait::getRandomGenerator protected function Gets the random generator for the utility methods.
RandomGeneratorTrait::randomMachineName protected function Generates a unique random string containing letters and numbers.
RandomGeneratorTrait::randomObject public function Generates a random PHP object.
RandomGeneratorTrait::randomString public function Generates a pseudo-random string of ASCII characters of codes 32 to 126.
UnitTestCase::$root protected property The app root.
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::setDebugDumpHandler public static function Registers the dumper CLI handler when the DebugDump extension is enabled.

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