Newer
Older
Angie Byron
committed
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
<?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() {
// All entity variations have to have the same results.
foreach (entity_test_entity_types() as $entity_type) {
$this->assertFormCRUD($entity_type);
}
}
Alex Pott
committed
/**
* Tests hook_entity_form_display_alter().
*
* @see entity_test_entity_form_display_alter()
*/
function testEntityFormDisplayAlter() {
$this->drupalGet('entity_test/add');
Angie Byron
committed
$altered_field = $this->xpath('//input[@name="field_test_text[0][value]" and @size="42"]');
Alex Pott
committed
$this->assertTrue(count($altered_field) === 1, 'The altered field has the correct size value.');
}
/**
* 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) {
Angie Byron
committed
$name1 = $this->randomName(8);
catch
committed
$name2 = $this->randomName(10);
Angie Byron
committed
$edit = array(
'name' => $name1,
Dries Buytaert
committed
'user_id' => mt_rand(0, 128),
Angie Byron
committed
'field_test_text[0][value]' => $this->randomName(16),
Angie Byron
committed
);
$this->drupalPostForm($entity_type . '/add', $edit, t('Save'));
$entity = $this->loadEntityByName($entity_type, $name1);
$this->assertTrue($entity, format_string('%entity_type: Entity found in the database.', array('%entity_type' => $entity_type)));
Angie Byron
committed
$edit['name'] = $name2;
Angie Byron
committed
$this->drupalPostForm($entity_type . '/manage/' . $entity->id(), $edit, t('Save'));
$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)));
Angie Byron
committed
Angie Byron
committed
$this->drupalPostForm($entity_type . '/manage/' . $entity->id(), array(), t('Delete'));
$entity = $this->loadEntityByName($entity_type, $name2);
$this->assertFalse($entity, format_string('%entity_type: Entity not found in the database.', array('%entity_type' => $entity_type)));
Angie Byron
committed
}
/**
catch
committed
* Loads a test entity by name always resetting the storage cache.
Angie Byron
committed
*/
protected function loadEntityByName($entity_type, $name) {
Angie Byron
committed
// Always load the entity from the database to ensure that changes are
// correctly picked up.
catch
committed
$this->container->get('entity.manager')->getStorage($entity_type)->resetCache();
Angie Byron
committed
return current(entity_load_multiple_by_properties($entity_type, array('name' => $name)));
}
}