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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?php
/**
* @file
* Contains \Drupal\system\Tests\Entity\EntityBundleFieldTest.
*/
namespace Drupal\system\Tests\Entity;
/**
* Tests adding a custom bundle field.
*/
class EntityBundleFieldTest extends EntityUnitTestBase {
/**
* The module handler.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $moduleHandler;
/**
* The database connection used.
*
* @var \Drupal\Core\Database\Connection
*/
protected $database;
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('menu_link');
/**
* {@inheritdoc}
*/
public static function getInfo() {
return array(
'name' => 'Entity bundle fields',
'description' => 'Tests providing a custom bundle field.',
'group' => 'Entity API',
);
}
/**
* {@inheritdoc}
*/
public function setUp() {
parent::setUp();
$this->installSchema('user', array('users_data'));
$this->installSchema('system', array('router'));
$this->moduleHandler = $this->container->get('module_handler');
$this->database = $this->container->get('database');
}
/**
* Tests the custom bundle field creation and deletion.
*/
public function testCustomBundleFieldCreateDelete() {
// Install the module which adds the field.
$this->moduleHandler->install(array('entity_bundle_field_test'), FALSE);
$definition = $this->entityManager->getFieldDefinitions('entity_test', 'custom')['custom_field'];
$this->assertNotNull($definition, 'Field definition found.');
// Make sure the table has been created.
$table = $this->entityManager->getStorage('entity_test')->_fieldTableName($definition);
$this->assertTrue($this->database->schema()->tableExists($table), 'Table created');
$this->moduleHandler->uninstall(array('entity_bundle_field_test'), FALSE);
$this->assertFalse($this->database->schema()->tableExists($table), 'Table dropped');
}
/**
* Tests making use of a custom bundle field.
*/
public function testCustomBundleFieldUsage() {
// Check that an entity with bundle entity_test does not have the custom
// field.
$this->moduleHandler->install(array('entity_bundle_field_test'), FALSE);
$storage = $this->entityManager->getStorage('entity_test');
$entity = $storage->create([
'type' => 'entity_test',
]);
$this->assertFalse($entity->hasField('custom_field'));
// Check that the custom bundle has the defined custom field and check
// saving and deleting of custom field data.
$entity = $storage->create([
'type' => 'custom',
]);
$this->assertTrue($entity->hasField('custom_field'));
$entity->custom_field->value = 'swanky';
$entity->save();
$storage->resetCache();
$entity = $storage->load($entity->id());
$this->assertEqual($entity->custom_field->value, 'swanky', 'Entity was saved correct.y');
$entity->custom_field->value = 'cozy';
$entity->save();
$storage->resetCache();
$entity = $storage->load($entity->id());
$this->assertEqual($entity->custom_field->value, 'cozy', 'Entity was updated correctly.');
$entity->delete();
$table = $storage->_fieldTableName($entity->getFieldDefinition('custom_field'));
$result = $this->database->select($table, 'f')
->fields('f')
->condition('f.entity_id', $entity->id())
->execute();
$this->assertFalse($result->fetchAssoc(), 'Field data has been deleted');
// Create another entity to test that values are marked as deleted when a
// bundle is deleted.
$entity = $storage->create(['type' => 'custom', 'custom_field' => 'new']);
$entity->save();
entity_test_delete_bundle('custom');
$table = $storage->_fieldTableName($entity->getFieldDefinition('custom_field'));
$result = $this->database->select($table, 'f')
->condition('f.entity_id', $entity->id())
->condition('deleted', 1)
->countQuery()
->execute();
$this->assertEqual(1, $result->fetchField(), 'Field data has been deleted');
// @todo Test field purge and table deletion once supported.
// $this->assertFalse($this->database->schema()->tableExists($table), 'Custom field table was deleted');
}
}