You are here

class EntityFormTest in Zircon Profile 8

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

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;

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();
    $this->entityForm = new EntityForm();
  }

  /**
   * Tests the form ID generation.
   *
   * @covers ::getFormId
   *
   * @dataProvider providerTestFormIds
   */
  public function testFormId($expected, $definition) {
    $entity_type = $this
      ->getMock('Drupal\\Core\\Entity\\EntityTypeInterface');
    $entity_type
      ->expects($this
      ->any())
      ->method('hasKey')
      ->with('bundle')
      ->will($this
      ->returnValue($definition['bundle']));
    $entity = $this
      ->getMockForAbstractClass('Drupal\\Core\\Entity\\Entity', array(
      array(),
      $definition['entity_type'],
    ), '', TRUE, TRUE, TRUE, array(
      'getEntityType',
      'bundle',
    ));
    $entity
      ->expects($this
      ->any())
      ->method('getEntityType')
      ->will($this
      ->returnValue($entity_type));
    $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 array(
      array(
        'node_article_form',
        array(
          'entity_type' => 'node',
          'bundle' => 'article',
          'operation' => 'default',
        ),
      ),
      array(
        'node_article_delete_form',
        array(
          'entity_type' => 'node',
          'bundle' => 'article',
          'operation' => 'delete',
        ),
      ),
      array(
        'user_user_form',
        array(
          'entity_type' => 'user',
          'bundle' => 'user',
          'operation' => 'default',
        ),
      ),
      array(
        'user_form',
        array(
          'entity_type' => 'user',
          'bundle' => '',
          'operation' => 'default',
        ),
      ),
      array(
        'user_delete_form',
        array(
          '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'));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EntityFormTest::$entityForm protected property The mocked entity form.
EntityFormTest::providerTestFormIds public function Provides test data for testFormId().
EntityFormTest::setUp protected function Overrides UnitTestCase::setUp
EntityFormTest::testCopyFormValuesToEntity public function @covers ::copyFormValuesToEntity
EntityFormTest::testFormId public function Tests the form ID generation.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root.
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName protected function Mocks a block with a block plugin.
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed in 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.