You are here

public function RenderedItemTest::testAddFieldValues in Search API 8

Tests whether the rendered_item field is correctly filled by the processor.

File

tests/src/Kernel/Processor/RenderedItemTest.php, line 182

Class

RenderedItemTest
Tests the "Rendered item" processor.

Namespace

Drupal\Tests\search_api\Kernel\Processor

Code

public function testAddFieldValues() {
  $this->nodes[4] = $this->nodes[3]
    ->addTranslation('de');
  $this->nodes[4]
    ->set('title', 'Titel für Knoten 4');
  $this->nodes[4]
    ->set('body', [
    'value' => 'Körper für Knoten 4',
    'summary' => 'Zusammenfassung für Knoten 4',
  ]);
  $this->nodes[4]
    ->save();
  $this
    ->assertEquals('en', $this->nodes[1]
    ->language()
    ->getId());
  $this
    ->assertEquals('en', $this->nodes[2]
    ->language()
    ->getId());
  $this
    ->assertEquals('en', $this->nodes[3]
    ->language()
    ->getId());
  $this
    ->assertEquals('de', $this->nodes[4]
    ->language()
    ->getId());
  $items = [];
  foreach ($this->nodes as $i => $node) {
    $items[] = [
      'datasource' => 'entity:node',
      'item' => $node
        ->getTypedData(),
      'item_id' => $i,
    ];
  }
  $user = User::create([
    'uid' => 2,
    'name' => 'User #2',
  ]);
  $items[] = [
    'datasource' => 'entity:user',
    'item' => $user
      ->getTypedData(),
    'item_id' => $user
      ->id(),
  ];
  $comment = Comment::create([
    'entity_type' => 'node',
    'entity_id' => 1,
    'field_name' => 'comment',
    'cid' => 1,
    'comment_type' => 'comment',
    'subject' => 'Subject of comment 1',
  ]);
  $comment
    ->save();
  $items[] = [
    'datasource' => 'entity:comment',
    'item' => $comment
      ->getTypedData(),
    'item_id' => $comment
      ->id(),
  ];
  $items = $this
    ->generateItems($items);

  // Add the processor's field values to the items.
  foreach ($items as $item) {
    $this->processor
      ->addFieldValues($item);
  }
  foreach ($items as $key => $item) {
    list($datasource_id, $entity_id) = Utility::splitCombinedId($key);
    $type = $this->index
      ->getDatasource($datasource_id)
      ->label();
    $field = $item
      ->getField('rendered_item');
    $this
      ->assertEquals('text', $field
      ->getType(), "{$type} item {$entity_id} rendered value is identified as text.");

    /** @var \Drupal\search_api\Plugin\search_api\data_type\value\TextValueInterface[] $values */
    $values = $field
      ->getValues();

    // Test that the value is properly wrapped in a
    // \Drupal\search_api\Plugin\search_api\data_type\value\TextValueInterface
    // object, which contains a string (not, for example, some markup object).
    $this
      ->assertInstanceOf('Drupal\\search_api\\Plugin\\search_api\\data_type\\value\\TextValueInterface', $values[0], "{$type} item {$entity_id} rendered value is properly wrapped in a text value object.");
    $field_value = $values[0]
      ->getText();
    $this
      ->assertIsString($field_value, "{$type} item {$entity_id} rendered value is a string.");
    $this
      ->assertEquals(1, count($values), "{$type} item {$entity_id} rendered value is a single value.");
    switch ($datasource_id) {
      case 'entity:node':
        $this
          ->checkRenderedNode($this->nodes[$entity_id], $field_value);
        break;
      case 'entity:user':
        $this
          ->checkRenderedUser($user, $field_value);
        break;
      case 'entity:comment':
        $this
          ->checkRenderedComment($comment, $field_value);
        break;
      default:
        $this
          ->assertTrue(FALSE);
    }
  }
}