You are here

public function BlazyEntityReferenceFormatterTest::testFormatterDisplay in Blazy 8

Tests the Blazy formatter display.

@todo: Useful assertions.

File

tests/src/Kernel/BlazyEntityReferenceFormatterTest.php, line 97

Class

BlazyEntityReferenceFormatterTest
Tests the Blazy entity reference file formatter.

Namespace

Drupal\Tests\blazy\Kernel

Code

public function testFormatterDisplay() {

  // @todo remove once corrected, likely broken since Drupal 8.4+.
  if (!$this->formatterInstance) {
    $this
      ->assertEquals(NULL, $this->formatterInstance);
    return;
  }
  $this
    ->buildContents();
  $field_name = $this->entityFieldName;
  $plugin_id = $this->entityPluginId;
  $formatter = $this->formatterInstance;
  $definition = array_merge($formatter
    ->getScopedFormElements(), $this
    ->getFormatterDefinition());
  $settings = array_merge($definition['settings'], $this
    ->getDefaultFields(TRUE)) + $formatter::defaultSettings();
  $settings['grid'] = 4;
  $settings['style'] = 'grid';
  $settings['overlay'] = 'field_image';
  $settings['image'] = $this->testFieldName;
  foreach ([
    'field_title',
    'field_teaser',
    'field_link',
  ] as $key) {
    $settings['caption'][$key] = $key;
  }
  $this->referencingDisplay
    ->setComponent($this->entityFieldName, [
    'type' => $this->entityPluginId,
    'settings' => $settings,
    'label' => 'hidden',
  ]);
  $this->referencingDisplay
    ->save();

  // Create referencing entity.
  $this->referencingEntity = $this
    ->createReferencingEntity();

  // Verify the un-accessible item still exists.
  $this
    ->assertEquals($this->referencingEntity->{$field_name}->target_id, $this->referencedEntity
    ->id(), "The un-accessible item still exists after {$plugin_id} formatter was executed.");
  $component = $this->referencingDisplay
    ->getComponent($this->entityFieldName);
  $this
    ->assertEquals($this->entityPluginId, $component['type']);
  $array = $this->referencingEntity
    ->get($this->entityFieldName);
  $value = $array
    ->getValue();
  $list = $this->fieldTypePluginManager
    ->createFieldItemList($this->referencingEntity, $this->entityFieldName, $value);
  $entities = $list
    ->referencedEntities();
  $entities = array_values($entities);
  $elements['settings'] = $settings;
  $formatter
    ->buildElements($elements, $entities, NULL);
  $this
    ->assertArrayHasKey('items', $elements);
  $build = $this->referencingDisplay
    ->build($this->referencingEntity);
  $render = $this->blazyManager
    ->getRenderer()
    ->renderRoot($build);
  $this
    ->assertNotEmpty($render);
  $string = $formatter
    ->getFieldString($this->referencedEntity, '', NULL);
  $this
    ->assertEmpty($string);
  $data['settings'] = $settings;
  $data['settings']['delta'] = 0;
  $data['settings']['vanilla'] = TRUE;
  $formatter
    ->buildElement($data, $this->referencedEntity, NULL);
  $this
    ->assertArrayHasKey('items', $data);
  $data['settings'] = $settings;
  $data['settings']['delta'] = 0;
  $data['settings']['vanilla'] = FALSE;
  $data['settings']['image'] = $this->testFieldName;
  $data['settings']['media_switch'] = 'rendered';
  $data['settings']['nav'] = TRUE;
  $data['settings']['thumbnail_style'] = 'thumbnail';
  $data['settings']['thumbnail_caption'] = 'field_text';
  $formatter
    ->buildElement($data, $entities[0], NULL);
  $this
    ->assertArrayHasKey('items', $data);
}