You are here

public function AddURLTest::testAddFieldValues in Search API 8

Tests whether the "URI" field is correctly filled by the processor.

File

tests/src/Unit/Processor/AddURLTest.php, line 72

Class

AddURLTest
Tests the "URL field" processor.

Namespace

Drupal\Tests\search_api\Unit\Processor

Code

public function testAddFieldValues() {

  /** @var \Drupal\node\Entity\Node $node */
  $node = $this
    ->getMockBuilder('Drupal\\node\\Entity\\Node')
    ->disableOriginalConstructor()
    ->getMock();
  $body_value = [
    'Some text value',
  ];
  $fields = [
    'search_api_url' => [
      'type' => 'string',
    ],
    'entity:node/body' => [
      'type' => 'text',
      'values' => $body_value,
    ],
  ];
  $items = $this
    ->createItems($this->index, 2, $fields, EntityAdapter::createFromEntity($node));
  foreach ($items as $item) {

    // Add a second URL field with "Generate absolute URL" enabled.
    $field = (clone $item
      ->getField('url'))
      ->setFieldIdentifier('url_1')
      ->setConfiguration([
      'absolute' => TRUE,
    ]);
    $item
      ->setField('url_1', $field);

    // Add the processor's field values to the items.
    $this->processor
      ->addFieldValues($item);
  }

  // Check the generated URLs.
  $item_1 = $items[$this->itemIds[0]];
  $this
    ->assertEquals([
    '/node/example',
  ], $item_1
    ->getField('url')
    ->getValues());
  $this
    ->assertEquals([
    'http://www.example.com/node/example',
  ], $item_1
    ->getField('url_1')
    ->getValues());

  // Check that no other fields were changed.
  $this
    ->assertEquals($body_value, $item_1
    ->getField('body')
    ->getValues());

  // Check the second item to be sure that all are processed.
  $item_2 = $items[$this->itemIds[1]];
  $this
    ->assertEquals([
    '/node/example',
  ], $item_2
    ->getField('url')
    ->getValues());
  $this
    ->assertEquals([
    'http://www.example.com/node/example',
  ], $item_2
    ->getField('url_1')
    ->getValues());
}