You are here

public function ViewsFieldTraitTest::testPreRender in Search API 8

Tests that entity loading in the pre-render phase works correctly.

@covers ::preRender

File

tests/src/Kernel/Views/ViewsFieldTraitTest.php, line 141

Class

ViewsFieldTraitTest
Tests the functionality of our Views field plugin trait.

Namespace

Drupal\Tests\search_api\Kernel\Views

Code

public function testPreRender() {
  $datasource_id = 'entity:entity_test_mulrev_changed';
  $entity_name = Utility::createCombinedId($datasource_id, 'name');
  $entity_body = Utility::createCombinedId($datasource_id, 'body');
  $entity_user_name = Utility::createCombinedId($datasource_id, 'user_id:entity:name');
  $user_name = Utility::createCombinedId('entity:user', 'name');

  /** @var \Drupal\search_api\Plugin\views\ResultRow[] $values */
  $values = [];
  $item_id = Utility::createCombinedId($datasource_id, '1:en');
  $item = new Item($this->index, $item_id);
  $item
    ->setOriginalObject($this->entities[1]
    ->getTypedData());
  $values[] = new ResultRow([
    '_item' => $item,
    '_object' => $item
      ->getOriginalObject(),
    '_relationship_objects' => [
      NULL => [
        $item
          ->getOriginalObject(),
      ],
    ],
  ]);
  $item_id = Utility::createCombinedId($datasource_id, '2:en');
  $values[] = new ResultRow([
    '_item' => new Item($this->index, $item_id),
    $entity_name => [
      'Fake value',
    ],
    $entity_user_name => [
      'Other fake value',
    ],
  ]);
  $item_id = Utility::createCombinedId('entity:user', '3:en');
  $values[] = new ResultRow([
    '_item' => new Item($this->index, $item_id),
  ]);
  $item_id = Utility::createCombinedId($datasource_id, '3:en');
  $item = new Item($this->index, $item_id);
  $values[] = new ResultRow([
    '_item' => $item,
  ]);

  // Set some other required properties which can easily be set automatically.
  foreach ($values as $row) {
    $row->search_api_id = $row->_item
      ->getId();
    $row->search_api_datasource = $row->_item
      ->getDatasourceId();
    $row->search_api_language = 'en';
  }
  $this->field
    ->addRetrievedProperty($entity_name);
  $this->field
    ->addRetrievedProperty("{$entity_body}:_object");
  $this->field
    ->addRetrievedProperty($entity_user_name);
  $this->field
    ->addRetrievedProperty($user_name);
  $this->field
    ->addRetrievedProperty('aggregated_field');
  $this->field
    ->preRender($values);
  $this
    ->assertObjectHasAttribute($entity_name, $values[0]);
  $this
    ->assertEquals([
    'Test entity 1 name',
  ], $values[0]->{$entity_name});
  $this
    ->assertObjectHasAttribute("{$entity_body}:_object", $values[0]);
  $this
    ->assertCount(1, $values[0]->{"{$entity_body}:_object"});
  $this
    ->assertInstanceOf(TypedDataInterface::class, $values[0]->{"{$entity_body}:_object"}[0]);
  $this
    ->assertArrayHasKey($entity_body, $values[0]->_relationship_objects);
  $this
    ->assertNotEmpty($values[0]->_relationship_objects[$entity_body]);
  $this
    ->assertObjectHasAttribute($entity_user_name, $values[0]);
  $this
    ->assertEquals([
    'User 1',
  ], $values[0]->{$entity_user_name});
  $this
    ->assertTrue(empty($values[0]->{$user_name}), 'User name should not be extracted for non-user entity.');
  $this
    ->assertObjectHasAttribute('aggregated_field', $values[0]);
  $this
    ->assertContains('Test entity 1 name', $values[0]->aggregated_field);
  $this
    ->assertContains('Test entity 1 body', $values[0]->aggregated_field);
  $this
    ->assertObjectHasAttribute($entity_name, $values[1]);
  $this
    ->assertEquals([
    'Fake value',
  ], $values[1]->{$entity_name});
  $this
    ->assertArrayHasKey($entity_body, $values[1]->_relationship_objects);
  $this
    ->assertNotEmpty($values[1]->_relationship_objects[$entity_body]);
  $this
    ->assertObjectHasAttribute($entity_user_name, $values[1]);
  $this
    ->assertEquals([
    'Other fake value',
  ], $values[1]->{$entity_user_name});
  $this
    ->assertTrue(empty($values[1]->{$user_name}), 'User name should not be extracted for non-user entity.');
  $this
    ->assertObjectHasAttribute('aggregated_field', $values[1]);
  $this
    ->assertContains('Test entity 2 name', $values[1]->aggregated_field);
  $this
    ->assertContains('Test entity 2 body', $values[1]->aggregated_field);

  // Since we provided the values on the row, most relationship objects should
  // not have been loaded.
  $this
    ->assertArrayNotHasKey($entity_name, $values[1]->_relationship_objects);
  $this
    ->assertArrayNotHasKey(Utility::createCombinedId($datasource_id, 'user_id'), $values[1]->_relationship_objects);
  $this
    ->assertArrayNotHasKey(Utility::createCombinedId($datasource_id, 'user_id:entity'), $values[1]->_relationship_objects);
  $this
    ->assertArrayNotHasKey($entity_user_name, $values[1]->_relationship_objects);
  $this
    ->assertObjectHasAttribute($user_name, $values[2]);
  $this
    ->assertEquals([
    'User 3',
  ], $values[2]->{$user_name});
  $this
    ->assertTrue(empty($values[2]->{$entity_name}), 'Test entity name should not be extracted for user entity.');
  $this
    ->assertTrue(empty($values[2]->{$entity_user_name}), 'Test entity author name should not be extracted for user entity.');
  $this
    ->assertObjectHasAttribute('aggregated_field', $values[2]);
  $this
    ->assertEquals([
    'User 3',
  ], $values[2]->aggregated_field);
  $this
    ->assertObjectHasAttribute($entity_name, $values[3]);
  $this
    ->assertEquals([
    'Test entity 3 name',
  ], $values[3]->{$entity_name});
  $this
    ->assertObjectHasAttribute("{$entity_body}:_object", $values[3]);
  $this
    ->assertCount(1, $values[3]->{"{$entity_body}:_object"});
  $this
    ->assertInstanceOf(TypedDataInterface::class, $values[3]->{"{$entity_body}:_object"}[0]);
  $this
    ->assertArrayHasKey($entity_body, $values[3]->_relationship_objects);
  $this
    ->assertNotEmpty($values[3]->_relationship_objects[$entity_body]);
  $this
    ->assertObjectHasAttribute($entity_user_name, $values[3]);
  $this
    ->assertEquals([
    'User 3',
  ], $values[3]->{$entity_user_name});
  $this
    ->assertTrue(empty($values[3]->{$user_name}), 'User name should not be extracted for non-user entity.');
  $this
    ->assertObjectHasAttribute('aggregated_field', $values[3]);
  $this
    ->assertContains('Test entity 3 name', $values[3]->aggregated_field);
  $this
    ->assertContains('Test entity 3 body', $values[3]->aggregated_field);
}