summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorwebchick2012-09-13 06:14:51 (GMT)
committerwebchick2012-09-13 06:14:51 (GMT)
commit3d2aad9e0a6345942fe703cb62941bd122f7e812 (patch)
treed59d29c66664b3faac623144d59eb25e0c96278d
parent12100bab406a5fb964160043877792c1b45e3264 (diff)
Issue #1757232 by peximo, plach: Improve test coverage for the entity form controller.
-rw-r--r--core/modules/system/lib/Drupal/system/Tests/Entity/EntityFormTest.php79
-rw-r--r--core/modules/system/tests/modules/entity_test/entity_test.module62
-rw-r--r--core/modules/system/tests/modules/entity_test/lib/Drupal/entity_test/EntityTestFormController.php95
3 files changed, 236 insertions, 0 deletions
diff --git a/core/modules/system/lib/Drupal/system/Tests/Entity/EntityFormTest.php b/core/modules/system/lib/Drupal/system/Tests/Entity/EntityFormTest.php
new file mode 100644
index 0000000..da1ff3f
--- /dev/null
+++ b/core/modules/system/lib/Drupal/system/Tests/Entity/EntityFormTest.php
@@ -0,0 +1,79 @@
+<?php
+
+/**
+ * @file
+ * Definition of Drupal\system\Tests\Entity\EntityFormTest.
+ */
+
+namespace Drupal\system\Tests\Entity;
+
+use Drupal\simpletest\WebTestBase;
+
+/**
+ * Tests the Entity Form Controller.
+ */
+class EntityFormTest extends WebTestBase {
+
+ /**
+ * Modules to enable.
+ *
+ * @var array
+ */
+ public static $modules = array('entity_test', 'language');
+
+ public static function getInfo() {
+ return array(
+ 'name' => 'Entity form',
+ 'description' => 'Tests the entity form controller.',
+ 'group' => 'Entity API',
+ );
+ }
+
+ function setUp() {
+ parent::setUp();
+ $web_user = $this->drupalCreateUser(array('administer entity_test content'));
+ $this->drupalLogin($web_user);
+ }
+
+ /**
+ * Tests basic form CRUD functionality.
+ */
+ function testFormCRUD() {
+ $langcode = LANGUAGE_NOT_SPECIFIED;
+ $name1 = $this->randomName(8);
+ $name2 = $this->randomName(8);
+
+ $edit = array(
+ 'name' => $name1,
+ 'uid' => mt_rand(0, 128),
+ "field_test_text[$langcode][0][value]" => $this->randomName(16),
+ );
+
+ $this->drupalPost('entity-test/add', $edit, t('Save'));
+ $entity = $this->loadEntityByName($name1);
+ $this->assertTrue($entity, t('Entity found in the database.'));
+
+ $edit['name'] = $name2;
+ $this->drupalPost('entity-test/manage/' . $entity->id() . '/edit', $edit, t('Save'));
+ $entity = $this->loadEntityByName($name1);
+ $this->assertFalse($entity, 'The entity has been modified.');
+ $entity = $this->loadEntityByName($name2);
+ $this->assertTrue($entity, 'Modified entity found in the database.');
+ $this->assertNotEqual($entity->get('name'), $name1, 'The entity name has been modified.');
+
+ $this->drupalPost('entity-test/manage/' . $entity->id() . '/edit', array(), t('Delete'));
+ $entity = $this->loadEntityByName($name2);
+ $this->assertFalse($entity, t('Entity not found in the database.'));
+ }
+
+ /**
+ * Loads a test entity by name always resetting the storage controller cache.
+ */
+ protected function loadEntityByName($name) {
+ $entity_type = 'entity_test';
+ // Always load the entity from the database to ensure that changes are
+ // correctly picked up.
+ entity_get_controller($entity_type)->resetCache();
+ return current(entity_load_multiple_by_properties($entity_type, array('name' => $name)));
+ }
+}
diff --git a/core/modules/system/tests/modules/entity_test/entity_test.module b/core/modules/system/tests/modules/entity_test/entity_test.module
index 9b1b224..d2dc67e 100644
--- a/core/modules/system/tests/modules/entity_test/entity_test.module
+++ b/core/modules/system/tests/modules/entity_test/entity_test.module
@@ -13,6 +13,9 @@ function entity_test_entity_info() {
'label' => t('Test entity'),
'entity class' => 'Drupal\entity_test\EntityTest',
'controller class' => 'Drupal\entity_test\EntityTestStorageController',
+ 'form controller class' => array(
+ 'default' => 'Drupal\entity_test\EntityTestFormController',
+ ),
'base table' => 'entity_test',
'fieldable' => TRUE,
'entity keys' => array(
@@ -28,6 +31,65 @@ function entity_test_entity_info() {
}
/**
+ * Implements hook_permission().
+ */
+function entity_test_permission() {
+ $permissions = array(
+ 'administer entity_test content' => array(
+ 'title' => t('Administer entity_test content'),
+ 'description' => t('Manage entity_test content'),
+ ),
+ );
+ return $permissions;
+}
+
+/**
+ * Implements hook_menu().
+ */
+function entity_test_menu() {
+ $items = array();
+
+ $items['entity-test/add'] = array(
+ 'title' => 'Add an entity_test',
+ 'page callback' => 'entity_test_add',
+ 'access arguments' => array('administer entity_test content'),
+ 'type' => MENU_NORMAL_ITEM,
+ );
+
+ $items['entity-test/manage/%entity_test'] = array(
+ 'title' => 'Edit test entity',
+ 'page callback' => 'entity_test_edit',
+ 'page arguments' => array(2),
+ 'access arguments' => array('administer entity_test content'),
+ 'type' => MENU_NORMAL_ITEM,
+ );
+
+ $items['entity-test/manage/%entity_test/edit'] = array(
+ 'title' => 'Edit',
+ 'type' => MENU_DEFAULT_LOCAL_TASK,
+ );
+
+ return $items;
+}
+
+/**
+ * Menu callback: displays the 'Add new entity_test' form.
+ */
+function entity_test_add() {
+ drupal_set_title(t('Create an entity_test'));
+ $entity = entity_create('entity_test', array());
+ return entity_get_form($entity);
+}
+
+/**
+ * Menu callback: displays the 'Edit existing entity_test' form.
+ */
+function entity_test_edit($entity) {
+ drupal_set_title(t('entity_test @id', array('@id' => $entity->id)), PASS_THROUGH);
+ return entity_get_form($entity);
+}
+
+/**
* Loads a test entity.
*
* @param int $id
diff --git a/core/modules/system/tests/modules/entity_test/lib/Drupal/entity_test/EntityTestFormController.php b/core/modules/system/tests/modules/entity_test/lib/Drupal/entity_test/EntityTestFormController.php
new file mode 100644
index 0000000..0c70b13
--- /dev/null
+++ b/core/modules/system/tests/modules/entity_test/lib/Drupal/entity_test/EntityTestFormController.php
@@ -0,0 +1,95 @@
+<?php
+/**
+ * @file
+ * Definition of Drupal\entity_test\EntityTestFormController.
+ */
+
+namespace Drupal\entity_test;
+
+use Drupal\Core\Entity\EntityInterface;
+use Drupal\Core\Entity\EntityFormController;
+
+/**
+ * Form controller for the test entity edit forms.
+ */
+class EntityTestFormController extends EntityFormController {
+
+ /**
+ * Overrides Drupal\Core\Entity\EntityFormController::form().
+ */
+ public function form(array $form, array &$form_state, EntityInterface $entity) {
+ $form = parent::form($form, $form_state, $entity);
+
+ $langcode = $this->getFormLangcode($form_state);
+ $name = $entity->get('name', $langcode);
+ $uid = $entity->get('uid', $langcode);
+
+ $form['name'] = array(
+ '#type' => 'textfield',
+ '#title' => t('Name'),
+ '#default_value' => !empty($name) ? $name : '',
+ '#size' => 60,
+ '#maxlength' => 128,
+ '#required' => TRUE,
+ '#weight' => -10,
+ );
+
+ $form['uid'] = array(
+ '#type' => 'textfield',
+ '#title' => 'UID',
+ '#default_value' => !empty($uid) ? $uid : '',
+ '#size' => 60,
+ '#maxlength' => 128,
+ '#required' => TRUE,
+ '#weight' => -10,
+ );
+
+ return $form;
+ }
+
+ /**
+ * Overrides Drupal\Core\Entity\EntityFormController::submit().
+ */
+ public function submit(array $form, array &$form_state) {
+ $entity = parent::submit($form, $form_state);
+ $langcode = $this->getFormLangcode($form_state);
+
+ // Updates multilingual properties.
+ foreach (array('name', 'uid') as $property) {
+ $entity->set($property, $form_state['values'][$property], $langcode);
+ }
+
+ return $entity;
+ }
+
+ /**
+ * Overrides Drupal\Core\Entity\EntityFormController::save().
+ */
+ public function save(array $form, array &$form_state) {
+ $entity = $this->getEntity($form_state);
+ $is_new = $entity->isNew();
+ $entity->save();
+
+ $message = $is_new ? t('entity_test @id has been created.', array('@id' => $entity->id())) : t('entity_test @id has been updated.', array('@id' => $entity->id()));
+ drupal_set_message($message);
+
+ if ($entity->id()) {
+ $form_state['redirect'] = 'entity-test/manage/' . $entity->id() . '/edit';
+ }
+ else {
+ // Error on save.
+ drupal_set_message(t('The entity could not be saved.'), 'error');
+ $form_state['rebuild'] = TRUE;
+ }
+ }
+
+ /**
+ * Overrides Drupal\Core\Entity\EntityFormController::delete().
+ */
+ public function delete(array $form, array &$form_state) {
+ $entity = $this->getEntity($form_state);
+ $entity->delete();
+ drupal_set_message(t('entity_test @id has been deleted.', array('@id' => $entity->id())));
+ $form_state['redirect'] = '<front>';
+ }
+}