EntityFormTest.php 2.29 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?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,
48
      'user_id' => mt_rand(0, 128),
49
50
51
52
53
      "field_test_text[$langcode][0][value]" => $this->randomName(16),
    );

    $this->drupalPost('entity-test/add', $edit, t('Save'));
    $entity = $this->loadEntityByName($name1);
54
    $this->assertTrue($entity, 'Entity found in the database.');
55
56
57
58
59
60
61

    $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.');
62
    $this->assertNotEqual($entity->name->value, $name1, 'The entity name has been modified.');
63
64
65

    $this->drupalPost('entity-test/manage/' . $entity->id() . '/edit', array(), t('Delete'));
    $entity = $this->loadEntityByName($name2);
66
    $this->assertFalse($entity, 'Entity not found in the database.');
67
68
69
70
71
72
73
74
75
76
77
78
79
  }

  /**
   * 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)));
  }
}