EntityFormTest.php 3.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
<?php

/**
 * @file
 * Definition of Drupal\system\Tests\Entity\EntityFormTest.
 */

namespace Drupal\system\Tests\Entity;

use Drupal\simpletest\WebTestBase;

/**
13 14 15
 * Tests the entity form.
 *
 * @group Entity
16 17 18 19 20 21 22 23 24 25
 */
class EntityFormTest extends WebTestBase {

  /**
   * Modules to enable.
   *
   * @var array
   */
  public static $modules = array('entity_test', 'language');

26
  protected function setUp() {
27 28 29 30 31 32 33 34 35
    parent::setUp();
    $web_user = $this->drupalCreateUser(array('administer entity_test content'));
    $this->drupalLogin($web_user);
  }

  /**
   * Tests basic form CRUD functionality.
   */
  function testFormCRUD() {
36 37 38 39 40 41
    // All entity variations have to have the same results.
    foreach (entity_test_entity_types() as $entity_type) {
      $this->assertFormCRUD($entity_type);
    }
  }

42 43 44 45 46 47 48
  /**
   * Tests hook_entity_form_display_alter().
   *
   * @see entity_test_entity_form_display_alter()
   */
  function testEntityFormDisplayAlter() {
    $this->drupalGet('entity_test/add');
49
    $altered_field = $this->xpath('//input[@name="field_test_text[0][value]" and @size="42"]');
50 51 52
    $this->assertTrue(count($altered_field) === 1, 'The altered field has the correct size value.');
  }

53 54 55 56 57 58 59
  /**
   * Executes the form CRUD tests for the given entity type.
   *
   * @param string $entity_type
   *   The entity type to run the tests with.
   */
  protected function assertFormCRUD($entity_type) {
60 61
    $name1 = $this->randomMachineName(8);
    $name2 = $this->randomMachineName(10);
62 63

    $edit = array(
64 65
      'name' => $name1,
      'user_id' => mt_rand(0, 128),
66
      'field_test_text[0][value]' => $this->randomMachineName(16),
67 68
    );

69
    $this->drupalPostForm($entity_type . '/add', $edit, t('Save'));
70 71
    $entity = $this->loadEntityByName($entity_type, $name1);
    $this->assertTrue($entity, format_string('%entity_type: Entity found in the database.', array('%entity_type' => $entity_type)));
72

73
    $edit['name'] = $name2;
74
    $this->drupalPostForm($entity_type . '/manage/' . $entity->id(), $edit, t('Save'));
75 76 77 78 79
    $entity = $this->loadEntityByName($entity_type, $name1);
    $this->assertFalse($entity, format_string('%entity_type: The entity has been modified.', array('%entity_type' => $entity_type)));
    $entity = $this->loadEntityByName($entity_type, $name2);
    $this->assertTrue($entity, format_string('%entity_type: Modified entity found in the database.', array('%entity_type' => $entity_type)));
    $this->assertNotEqual($entity->name->value, $name1, format_string('%entity_type: The entity name has been modified.', array('%entity_type' => $entity_type)));
80

81 82 83
    $this->drupalGet($entity_type . '/manage/' . $entity->id());
    $this->clickLink(t('Delete'));
    $this->drupalPostForm(NULL, array(), t('Confirm'));
84 85
    $entity = $this->loadEntityByName($entity_type, $name2);
    $this->assertFalse($entity, format_string('%entity_type: Entity not found in the database.', array('%entity_type' => $entity_type)));
86 87 88
  }

  /**
89
   * Loads a test entity by name always resetting the storage cache.
90
   */
91
  protected function loadEntityByName($entity_type, $name) {
92 93
    // Always load the entity from the database to ensure that changes are
    // correctly picked up.
94
    $this->container->get('entity.manager')->getStorage($entity_type)->resetCache();
95 96 97
    return current(entity_load_multiple_by_properties($entity_type, array('name' => $name)));
  }
}