summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorwebchick2014-08-23 21:53:58 (GMT)
committerwebchick2014-08-23 21:53:58 (GMT)
commit673e1e58b0b514fb7ee833a1d6b3c97f18fa798e (patch)
treec6c3f67ce9472da9ba0157069bfb6c4f4fbebe03
parent37709d66f54cc66a1fb070c91b9366722e210be3 (diff)
Issue #2279071 by andypost: Fixed Fragile form_id generation for bundlable entity forms.
-rw-r--r--core/lib/Drupal/Core/Entity/EntityForm.php8
-rw-r--r--core/tests/Drupal/Tests/Core/Entity/EntityFormTest.php97
2 files changed, 100 insertions, 5 deletions
diff --git a/core/lib/Drupal/Core/Entity/EntityForm.php b/core/lib/Drupal/Core/Entity/EntityForm.php
index 3cf5558..75d9014 100644
--- a/core/lib/Drupal/Core/Entity/EntityForm.php
+++ b/core/lib/Drupal/Core/Entity/EntityForm.php
@@ -73,11 +73,9 @@ class EntityForm extends FormBase implements EntityFormInterface {
* {@inheritdoc}
*/
public function getFormId() {
- $entity_type = $this->entity->getEntityTypeId();
- $bundle = $this->entity->bundle();
- $form_id = $entity_type;
- if ($bundle != $entity_type) {
- $form_id = $bundle . '_' . $form_id;
+ $form_id = $this->entity->getEntityTypeId();
+ if ($this->entity->getEntityType()->hasKey('bundle')) {
+ $form_id = $this->entity->bundle() . '_' . $form_id;
}
if ($this->operation != 'default') {
$form_id = $form_id . '_' . $this->operation;
diff --git a/core/tests/Drupal/Tests/Core/Entity/EntityFormTest.php b/core/tests/Drupal/Tests/Core/Entity/EntityFormTest.php
new file mode 100644
index 0000000..35f8913
--- /dev/null
+++ b/core/tests/Drupal/Tests/Core/Entity/EntityFormTest.php
@@ -0,0 +1,97 @@
+<?php
+
+/**
+ * @file
+ * Contains \Drupal\Tests\Core\Entity\EntityFormTest.
+ */
+
+namespace Drupal\Tests\Core\Entity;
+
+use Drupal\Core\Entity\EntityForm;
+use Drupal\Tests\UnitTestCase;
+
+/**
+ * @coversDefaultClass \Drupal\Core\Entity\EntityForm
+ * @group Entity
+ */
+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('article_node_form', array(
+ 'bundle' => 'article',
+ 'entity_type' => 'node',
+ 'operation' => 'default',
+ )),
+ array('article_node_delete_form', array(
+ 'bundle' => 'article',
+ 'entity_type' => 'node',
+ 'operation' => 'delete',
+ )),
+ array('user_user_form', array(
+ 'bundle' => 'user',
+ 'entity_type' => 'user',
+ 'operation' => 'default',
+ )),
+ array('user_form', array(
+ 'bundle' => '',
+ 'entity_type' => 'user',
+ 'operation' => 'default',
+ )),
+ array('user_delete_form', array(
+ 'bundle' => '',
+ 'entity_type' => 'user',
+ 'operation' => 'delete',
+ )),
+ );
+ }
+
+}