You are here

class EntityFormTest in Drupal 9

Same name in this branch
  1. 9 core/tests/Drupal/Tests/Core/Entity/EntityFormTest.php \Drupal\Tests\Core\Entity\EntityFormTest
  2. 9 core/modules/system/tests/src/Functional/Entity/EntityFormTest.php \Drupal\Tests\system\Functional\Entity\EntityFormTest
Same name and namespace in other branches
  1. 8 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 19

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) {
    $this->entityType
      ->set('entity_keys', [
      'bundle' => $definition['bundle'],
    ]);
    $entity = $this
      ->getMockForAbstractClass('Drupal\\Core\\Entity\\EntityBase', [
      [],
      $definition['entity_type'],
    ], '', TRUE, TRUE, TRUE, [
      'getEntityType',
      'bundle',
    ]);
    $entity
      ->expects($this
      ->any())
      ->method('getEntityType')
      ->will($this
      ->returnValue($this->entityType));
    $entity
      ->expects($this
      ->any())
      ->method('bundle')
      ->will($this
      ->returnValue($definition['bundle']));
    $this->entityForm
      ->setEntity($entity);
    $this->entityForm
      ->setOperation($definition['operation']);
    $this
      ->assertSame($expected, $this->entityForm
      ->getFormId());
  }

  /**
   * Provides test data for testFormId().
   */
  public 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() {
    $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',
    ])
      ->setMethods([
      '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() {
    $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() {
    $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() {
    $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() {
    $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
   *   The storage prophecy.
   */
  protected function setUpStorage() {
    $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

Namesort descending Modifiers Type Description Overrides
EntityFormTest::$entityForm protected property The mocked entity form.
EntityFormTest::$entityType protected property A fake entity type used in the test.
EntityFormTest::providerTestFormIds public 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
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.
PhpUnitWarnings::$deprecationWarnings private static property Deprecation warnings from PHPUnit to raise with @trigger_error().
PhpUnitWarnings::addWarning public function Converts PHPUnit deprecation warnings to E_USER_DEPRECATED.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals Deprecated protected function Asserts if two arrays are equal by sorting them first.
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::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.
UnitTestCase::setUpBeforeClass public static function