DisplayApiTest.php 11.4 KB
Newer Older
1 2 3 4 5 6 7 8 9
<?php

/**
 * @file
 * Definition of Drupal\field\Tests\DisplayApiTest.
 */

namespace Drupal\field\Tests;

10 11
use Drupal\Core\Entity\Entity\EntityViewMode;

12 13
/**
 * Tests the field display API.
14 15
 *
 * @group field
16
 */
17
class DisplayApiTest extends FieldUnitTestBase {
18

19 20 21 22 23 24 25 26 27
  /**
   * Set to TRUE to strict check all configuration saved.
   *
   * @see \Drupal\Core\Config\Testing\ConfigSchemaChecker
   *
   * @var bool
   */
  protected $strictConfigSchema = TRUE;

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
  /**
   * The field name to use in this test.
   *
   * @var string
   */
  protected $field_name;

  /**
   * The field label to use in this test.
   *
   * @var string
   */
  protected $label;

  /**
   * The field cardinality to use in this test.
   *
   * @var number
   */
  protected $cardinality;

  /**
   * The field display options to use in this test.
   *
   * @var array
   */
  protected $display_options;

  /**
57
   * The test entity.
58 59 60 61 62 63 64 65 66 67 68 69
   *
   * @var \Drupal\Core\Entity\EntityInterface
   */
  protected $entity;

  /**
   * An array of random values, in the format expected for field values.
   *
   * @var array
   */
  protected $values;

70
  protected function setUp() {
71
    parent::setUp();
72

73
    // Create a field and its storage.
74
    $this->field_name = 'test_field';
75
    $this->label = $this->randomMachineName();
76 77
    $this->cardinality = 4;

78
    $field_storage = array(
79
      'field_name' => $this->field_name,
80
      'entity_type' => 'entity_test',
81 82 83
      'type' => 'test_field',
      'cardinality' => $this->cardinality,
    );
84
    $field = array(
85
      'field_name' => $this->field_name,
86 87
      'entity_type' => 'entity_test',
      'bundle' => 'entity_test',
88
      'label' => $this->label,
89 90 91 92 93 94
    );

    $this->display_options = array(
      'default' => array(
        'type' => 'field_test_default',
        'settings' => array(
95
          'test_formatter_setting' => $this->randomMachineName(),
96
        ),
97 98 99 100
      ),
      'teaser' => array(
        'type' => 'field_test_default',
        'settings' => array(
101
          'test_formatter_setting' => $this->randomMachineName(),
102 103 104
        ),
      ),
    );
105

106
    entity_create('field_storage_config', $field_storage)->save();
107
    entity_create('field_config', $field)->save();
108
    // Create a display for the default view mode.
109
    entity_get_display($field['entity_type'], $field['bundle'], 'default')
110 111 112
      ->setComponent($this->field_name, $this->display_options['default'])
      ->save();
    // Create a display for the teaser view mode.
113
    EntityViewMode::create(array('id' =>  'entity_test.teaser', 'targetEntityType' => 'entity_test'))->save();
114
    entity_get_display($field['entity_type'], $field['bundle'], 'teaser')
115 116
      ->setComponent($this->field_name, $this->display_options['teaser'])
      ->save();
117 118 119

    // Create an entity with values.
    $this->values = $this->_generateTestFieldValues($this->cardinality);
120
    $this->entity = entity_create('entity_test');
121 122
    $this->entity->{$this->field_name}->setValue($this->values);
    $this->entity->save();
123 124 125
  }

  /**
126
   * Tests the FieldItemListInterface::view() method.
127
   */
128 129 130
  function testFieldItemListView() {
    $items = $this->entity->get($this->field_name);

131
    // No display settings: check that default display settings are used.
132 133
    $build = $items->view();
    $this->render($build);
134
    $settings = \Drupal::service('plugin.manager.field.formatter')->getDefaultSettings('field_test_default');
135
    $setting = $settings['test_formatter_setting'];
136
    $this->assertText($this->label, 'Label was displayed.');
137
    foreach ($this->values as $delta => $value) {
138
      $this->assertText($setting . '|' . $value['value'], format_string('Value @delta was displayed with expected setting.', array('@delta' => $delta)));
139 140
    }

141
    // Display settings: Check hidden field.
142 143 144 145
    $display = array(
      'label' => 'hidden',
      'type' => 'field_test_multiple',
      'settings' => array(
146
        'test_formatter_setting_multiple' => $this->randomMachineName(),
147 148 149
        'alter' => TRUE,
      ),
    );
150 151
    $build = $items->view($display);
    $this->render($build);
152
    $setting = $display['settings']['test_formatter_setting_multiple'];
153
    $this->assertNoText($this->label, 'Label was not displayed.');
154
    $this->assertText('field_test_entity_display_build_alter', 'Alter fired, display passed.');
155
    $this->assertText('entity language is en', 'Language is placed onto the context.');
156 157 158 159
    $array = array();
    foreach ($this->values as $delta => $value) {
      $array[] = $delta . ':' . $value['value'];
    }
160
    $this->assertText($setting . '|' . implode('|', $array), 'Values were displayed with expected setting.');
161

162
    // Display settings: Check visually_hidden field.
163
    $display = array(
164
      'label' => 'visually_hidden',
165 166
      'type' => 'field_test_multiple',
      'settings' => array(
167
        'test_formatter_setting_multiple' => $this->randomMachineName(),
168 169 170
        'alter' => TRUE,
      ),
    );
171 172
    $build = $items->view($display);
    $this->render($build);
173 174 175
    $setting = $display['settings']['test_formatter_setting_multiple'];
    $this->assertRaw('visually-hidden', 'Label was visually hidden.');
    $this->assertText('field_test_entity_display_build_alter', 'Alter fired, display passed.');
176
    $this->assertText('entity language is en', 'Language is placed onto the context.');
177 178 179 180 181 182
    $array = array();
    foreach ($this->values as $delta => $value) {
      $array[] = $delta . ':' . $value['value'];
    }
    $this->assertText($setting . '|' . implode('|', $array), 'Values were displayed with expected setting.');

183 184 185 186 187
    // Check the prepare_view steps are invoked.
    $display = array(
      'label' => 'hidden',
      'type' => 'field_test_with_prepare_view',
      'settings' => array(
188
        'test_formatter_setting_additional' => $this->randomMachineName(),
189 190
      ),
    );
191 192
    $build = $items->view($display);
    $this->render($build);
193
    $setting = $display['settings']['test_formatter_setting_additional'];
194
    $this->assertNoText($this->label, 'Label was not displayed.');
195
    $this->assertNoText('field_test_entity_display_build_alter', 'Alter not fired.');
196
    foreach ($this->values as $delta => $value) {
197
      $this->assertText($setting . '|' . $value['value'] . '|' . ($value['value'] + 1), format_string('Value @delta was displayed with expected setting.', array('@delta' => $delta)));
198 199
    }

200 201
    // View mode: check that display settings specified in the display object
    // are used.
202 203
    $build = $items->view('teaser');
    $this->render($build);
204
    $setting = $this->display_options['teaser']['settings']['test_formatter_setting'];
205
    $this->assertText($this->label, 'Label was displayed.');
206
    foreach ($this->values as $delta => $value) {
207
      $this->assertText($setting . '|' . $value['value'], format_string('Value @delta was displayed with expected setting.', array('@delta' => $delta)));
208 209 210 211
    }

    // Unknown view mode: check that display settings for 'default' view mode
    // are used.
212 213
    $build = $items->view('unknown_view_mode');
    $this->render($build);
214
    $setting = $this->display_options['default']['settings']['test_formatter_setting'];
215
    $this->assertText($this->label, 'Label was displayed.');
216
    foreach ($this->values as $delta => $value) {
217
      $this->assertText($setting . '|' . $value['value'], format_string('Value @delta was displayed with expected setting.', array('@delta' => $delta)));
218 219 220 221
    }
  }

  /**
222
   * Tests the FieldItemInterface::view() method.
223
   */
224
  function testFieldItemView() {
225
    // No display settings: check that default display settings are used.
226
    $settings = \Drupal::service('plugin.manager.field.formatter')->getDefaultSettings('field_test_default');
227 228
    $setting = $settings['test_formatter_setting'];
    foreach ($this->values as $delta => $value) {
229
      $item = $this->entity->{$this->field_name}[$delta];
230 231
      $build = $item->view();
      $this->render($build);
232
      $this->assertText($setting . '|' . $value['value'], format_string('Value @delta was displayed with expected setting.', array('@delta' => $delta)));
233 234 235 236 237 238
    }

    // Check that explicit display settings are used.
    $display = array(
      'type' => 'field_test_multiple',
      'settings' => array(
239
        'test_formatter_setting_multiple' => $this->randomMachineName(),
240 241 242 243
      ),
    );
    $setting = $display['settings']['test_formatter_setting_multiple'];
    foreach ($this->values as $delta => $value) {
244
      $item = $this->entity->{$this->field_name}[$delta];
245 246
      $build = $item->view($display);
      $this->render($build);
247
      $this->assertText($setting . '|0:' . $value['value'], format_string('Value @delta was displayed with expected setting.', array('@delta' => $delta)));
248 249 250 251 252 253
    }

    // Check that prepare_view steps are invoked.
    $display = array(
      'type' => 'field_test_with_prepare_view',
      'settings' => array(
254
        'test_formatter_setting_additional' => $this->randomMachineName(),
255 256 257 258
      ),
    );
    $setting = $display['settings']['test_formatter_setting_additional'];
    foreach ($this->values as $delta => $value) {
259
      $item = $this->entity->{$this->field_name}[$delta];
260 261
      $build = $item->view($display);
      $this->render($build);
262
      $this->assertText($setting . '|' . $value['value'] . '|' . ($value['value'] + 1), format_string('Value @delta was displayed with expected setting.', array('@delta' => $delta)));
263 264
    }

265
    // View mode: check that display settings specified in the field are used.
266
    $setting = $this->display_options['teaser']['settings']['test_formatter_setting'];
267
    foreach ($this->values as $delta => $value) {
268
      $item = $this->entity->{$this->field_name}[$delta];
269 270
      $build = $item->view('teaser');
      $this->render($build);
271
      $this->assertText($setting . '|' . $value['value'], format_string('Value @delta was displayed with expected setting.', array('@delta' => $delta)));
272 273 274 275
    }

    // Unknown view mode: check that display settings for 'default' view mode
    // are used.
276
    $setting = $this->display_options['default']['settings']['test_formatter_setting'];
277
    foreach ($this->values as $delta => $value) {
278
      $item = $this->entity->{$this->field_name}[$delta];
279 280
      $build = $item->view('unknown_view_mode');
      $this->render($build);
281
      $this->assertText($setting . '|' . $value['value'], format_string('Value @delta was displayed with expected setting.', array('@delta' => $delta)));
282 283
    }
  }
284 285 286 287 288

  /**
   * Tests that the prepareView() formatter method still fires for empty values.
   */
  function testFieldEmpty() {
289
    // Uses \Drupal\field_test\Plugin\Field\FieldFormatter\TestFieldEmptyFormatter.
290 291 292 293
    $display = array(
      'label' => 'hidden',
      'type' => 'field_empty_test',
      'settings' => array(
294
        'test_empty_string' => '**EMPTY FIELD**' . $this->randomMachineName(),
295 296 297 298
      ),
    );
    // $this->entity is set by the setUp() method and by default contains 4
    // numeric values.  We only want to test the display of this one field.
299 300
    $build = $this->entity->get($this->field_name)->view($display);
    $this->render($build);
301 302 303 304 305
    // The test field by default contains values, so should not display the
    // default "empty" text.
    $this->assertNoText($display['settings']['test_empty_string']);

    // Now remove the values from the test field and retest.
306 307
    $this->entity->{$this->field_name} = array();
    $this->entity->save();
308 309
    $build = $this->entity->get($this->field_name)->view($display);
    $this->render($build);
310 311 312 313
    // This time, as the field values have been removed, we *should* show the
    // default "empty" text.
    $this->assertText($display['settings']['test_empty_string']);
  }
314
}