EntityDisplayTest.php 9.74 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?php

/**
 * @file
 * Contains \Drupal\entity\Tests\EntityDisplayTest.
 */

namespace Drupal\entity\Tests;

use Drupal\simpletest\DrupalUnitTestBase;

/**
 * Tests the EntityDisplay configuration entities.
 */
class EntityDisplayTest extends DrupalUnitTestBase {

17
  public static $modules = array('entity', 'field', 'entity_test');
18
19
20
21
22
23
24
25
26

  public static function getInfo() {
    return array(
      'name' => 'Entity display configuration entities',
      'description' => 'Tests the entity display configuration entities.',
      'group' => 'Entity API',
    );
  }

27
28
29
30
31
  protected function setUp() {
    parent::setUp();
    $this->installConfig(array('field'));
  }

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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
  /**
   * Tests basic CRUD operations on EntityDisplay objects.
   */
  public function testEntityDisplayCRUD() {
    $display = entity_create('entity_display', array(
      'targetEntityType' => 'entity_test',
      'bundle' => 'entity_test',
      'viewMode' => 'default',
    ));

    $expected = array();

    // Check that providing no 'weight' results in the highest current weight
    // being assigned.
    $expected['component_1'] = array('weight' => 0);
    $expected['component_2'] = array('weight' => 1);
    $display->setComponent('component_1');
    $display->setComponent('component_2');
    $this->assertEqual($display->getComponent('component_1'), $expected['component_1']);
    $this->assertEqual($display->getComponent('component_2'), $expected['component_2']);

    // Check that arbitrary options are correctly stored.
    $expected['component_3'] = array('weight' => 10, 'foo' => 'bar');
    $display->setComponent('component_3', $expected['component_3']);
    $this->assertEqual($display->getComponent('component_3'), $expected['component_3']);

    // Check that the display can be properly saved and read back.
    $display->save();
    $display = entity_load('entity_display', $display->id());
    foreach (array('component_1', 'component_2', 'component_3') as $name) {
      $this->assertEqual($display->getComponent($name), $expected[$name]);
    }

    // Check that getComponents() returns options for all components.
    $this->assertEqual($display->getComponents(), $expected);

    // Check that a component can be removed.
    $display->removeComponent('component_3');
    $this->assertNULL($display->getComponent('component_3'));

    // Check that the removal is correctly persisted.
    $display->save();
    $display = entity_load('entity_display', $display->id());
    $this->assertNULL($display->getComponent('component_3'));

    // Check that CreateCopy() creates a new component that can be correclty
    // saved.
    $new_display = $display->createCopy('other_view_mode');
    $new_display->save();
    $new_display = entity_load('entity_display', $new_display->id());
    $this->assertEqual($new_display->targetEntityType, $display->targetEntityType);
    $this->assertEqual($new_display->bundle, $display->bundle);
    $this->assertEqual($new_display->viewMode, 'other_view_mode');
    $this->assertEqual($new_display->getComponents(), $display->getComponents());
  }

  /**
   * Tests entity_get_display().
   */
  public function testEntityGetDisplay() {
    // Check that entity_get_display() returns a fresh object when no
    // configuration entry exists.
    $display = entity_get_display('entity_test', 'entity_test', 'default');
    $this->assertTrue($display->isNew());

    // Add some components and save the display.
    $display->setComponent('component_1', array('weight' => 10))
      ->save();

    // Check that entity_get_display() returns the correct object.
    $display = entity_get_display('entity_test', 'entity_test', 'default');
    $this->assertFalse($display->isNew());
    $this->assertEqual($display->id, 'entity_test.entity_test.default');
    $this->assertEqual($display->getComponent('component_1'), array('weight' => 10));
  }

  /**
   * Tests the behavior of a field component within an EntityDisplay object.
   */
  public function testExtraFieldComponent() {
    $display = entity_create('entity_display', array(
      'targetEntityType' => 'entity_test',
      'bundle' => 'entity_test',
      'viewMode' => 'default',
    ));

    // Check that the default visibility taken into account for extra fields
    // unknown in the display.
    $this->assertEqual($display->getComponent('display_extra_field'), array('weight' => 5));
    $this->assertNull($display->getComponent('display_extra_field_hidden'));

    // Check that setting explicit options overrides the defaults.
    $display->removeComponent('display_extra_field');
    $display->setComponent('display_extra_field_hidden', array('weight' => 10));
    $this->assertNull($display->getComponent('display_extra_field'));
    $this->assertEqual($display->getComponent('display_extra_field_hidden'), array('weight' => 10));
  }

  /**
   * Tests the behavior of a field component within an EntityDisplay object.
   */
  public function testFieldComponent() {
    $this->enableModules(array('field_sql_storage', 'field_test'));

    $display = entity_create('entity_display', array(
      'targetEntityType' => 'entity_test',
      'bundle' => 'entity_test',
      'viewMode' => 'default',
    ));

    // Create a field and an instance.
    $field = array(
      'field_name' => 'test_field',
      'type' => 'test_field'
    );
    field_create_field($field);
    $instance = array(
      'field_name' => $field['field_name'],
      'entity_type' => 'entity_test',
      'bundle' => 'entity_test',
    );
    field_create_instance($instance);

    // Check that providing no options results in default values being used.
    $display->setComponent($field['field_name']);
    $field_type_info = field_info_field_types($field['type']);
    $default_formatter = $field_type_info['default_formatter'];
    $default_settings = field_info_formatter_settings($default_formatter);
    $expected = array(
      'weight' => 0,
      'label' => 'above',
      'type' => $default_formatter,
      'settings' => $default_settings,
    );
    $this->assertEqual($display->getComponent($field['field_name']), $expected);

    // Check that the getFormatter() method returns the correct formatter plugin.
    $formatter = $display->getFormatter($field['field_name']);
    $this->assertEqual($formatter->getPluginId(), $default_formatter);
    $this->assertEqual($formatter->getSettings(), $default_settings);

    // Check that the formatter is statically persisted, by assigning an
    // arbitrary property and reading it back.
    $random_value = $this->randomString();
    $formatter->randomValue = $random_value;
    $formatter = $display->getFormatter($field['field_name']);
    $this->assertEqual($formatter->randomValue, $random_value );

    // Check that changing the definition creates a new formatter.
    $display->setComponent($field['field_name'], array(
      'type' => 'field_test_multiple',
    ));
    $formatter = $display->getFormatter($field['field_name']);
    $this->assertEqual($formatter->getPluginId(), 'field_test_multiple');
    $this->assertFalse(isset($formatter->randomValue));

    // Check that specifying an unknown formatter (e.g. case of a disabled
    // module) gets stored as is in the display, but results in the default
    // formatter being used.
    $display->setComponent($field['field_name'], array(
      'type' => 'unknown_formatter',
    ));
    $options = $display->getComponent($field['field_name']);
    $this->assertEqual($options['type'], 'unknown_formatter');
    $formatter = $display->getFormatter($field['field_name']);
    $this->assertEqual($formatter->getPluginId(), $default_formatter);
  }

200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
  /**
   * Tests renaming and deleting a bundle.
   */
  public function testRenameDeleteBundle() {
    $this->enableModules(array('field_sql_storage', 'field_test', 'node', 'system'));
    $this->installSchema('node', array('node_type'));
    $this->installSchema('system', array('variable'));

    // Create a node bundle and display object.
    node_type_save((object) array('type' => 'article'));
    entity_get_display('node', 'article', 'default')->save();

    // Rename the article bundle and assert the entity display is renamed.
    $info = node_type_load('article');
    $info->old_type = 'article';
    $info->type = 'article_rename';
    node_type_save($info);
    $old_display = entity_load('entity_display', 'node.article.default');
    $this->assertFalse($old_display);
    $new_display = entity_load('entity_display', 'node.article_rename.default');
    $this->assertEqual('article_rename', $new_display->bundle);
    $this->assertEqual('node.article_rename.default', $new_display->id);

    // Delete the bundle.
    node_type_delete('article_rename');
    $display = entity_load('entity_display', 'node.article_rename.default');
    $this->assertFalse($display);
  }

229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
  /**
   * Tests deleting field instance.
   */
  public function testDeleteFieldInstance() {
    $this->enableModules(array('field_sql_storage', 'field_test'));

    // Create a field and an instance.
    $field = array(
      'field_name' => 'test_field',
      'type' => 'test_field'
    );
    field_create_field($field);
    $instance = array(
      'field_name' => $field['field_name'],
      'entity_type' => 'entity_test',
      'bundle' => 'entity_test',
    );
    field_create_instance($instance);

    // Create an entity display.
    $display = entity_create('entity_display', array(
      'targetEntityType' => 'entity_test',
      'bundle' => 'entity_test',
      'viewMode' => 'default',
    ))->setComponent($field['field_name'])->save();

    // Delete the instance.
    $instance = field_info_instance('entity_test', $field['field_name'], 'entity_test');
    field_delete_instance($instance);

    // Check that the component has been removed from the entity display.
    $display = entity_get_display('entity_test', 'entity_test', 'default');
    $this->assertFalse($display->getComponent($field['field_name']));
  }

264
}