You are here

public function DatasourceTaskTest::testItemTranslations in Search API 8

Tests that datasource config changes are reflected correctly.

File

tests/src/Kernel/Datasource/DatasourceTaskTest.php, line 132

Class

DatasourceTaskTest
Tests task integration of the content entity datasource.

Namespace

Drupal\Tests\search_api\Kernel\Datasource

Code

public function testItemTranslations() {

  // Test retrieving language and translations when no translations are
  // available.

  /** @var \Drupal\entity_test\Entity\EntityTestMulRevChanged $entity_1 */
  $uid = $this->container
    ->get('current_user')
    ->id();
  $entity_1 = EntityTestMulRevChanged::create([
    'id' => 1,
    'name' => 'test 1',
    'user_id' => $uid,
    'type' => 'item',
    'langcode' => 'l0',
  ]);
  $entity_1
    ->save();
  $entity_1
    ->addTranslation('l1')
    ->save();
  $entity_1
    ->addTranslation('l2')
    ->save();

  /** @var \Drupal\entity_test\Entity\EntityTestMulRevChanged $entity_2 */
  $entity_2 = EntityTestMulRevChanged::create([
    'id' => 2,
    'name' => 'test 2',
    'user_id' => $uid,
    'type' => 'article',
    'langcode' => 'l1',
  ]);
  $entity_2
    ->save();
  $entity_2
    ->addTranslation('l0')
    ->save();
  $entity_2
    ->addTranslation('l2')
    ->save();
  $index = $this->index;
  $tracker = $index
    ->getTrackerInstance();
  $datasource_id = 'entity:' . $this->testEntityTypeId;
  $datasource = $index
    ->getDatasource($datasource_id);
  $get_ids = function (array $raw_ids) use ($datasource_id) {
    foreach ($raw_ids as $i => $id) {
      $raw_ids[$i] = Utility::createCombinedId($datasource_id, $id);
    }
    return $raw_ids;
  };
  $this
    ->assertEquals(6, $tracker
    ->getTotalItemsCount());
  $this
    ->assertEquals(6, $tracker
    ->getRemainingItemsCount());
  $configuration = [
    'bundles' => [
      'default' => TRUE,
      'selected' => [
        'item',
      ],
    ],
    'languages' => [
      'default' => FALSE,
      'selected' => [
        'l0',
        'l2',
      ],
    ],
  ];
  $datasource
    ->setConfiguration($configuration);
  $index
    ->save();
  $this
    ->runBatch();
  $expected = $get_ids([
    '2:l0',
    '2:l2',
  ]);
  $this
    ->assertEquals(count($expected), $tracker
    ->getTotalItemsCount());
  $remaining = $tracker
    ->getRemainingItems();
  sort($remaining);
  $this
    ->assertEquals($expected, $remaining);
  $configuration['bundles']['default'] = FALSE;
  $configuration['bundles']['selected'][] = 'article';
  $configuration['languages']['selected'] = [
    'l0',
  ];
  $datasource
    ->setConfiguration($configuration);
  $index
    ->save();
  $this
    ->runBatch();
  $expected = $get_ids([
    '1:l0',
    '2:l0',
  ]);
  $this
    ->assertEquals(count($expected), $tracker
    ->getTotalItemsCount());
  $remaining = $tracker
    ->getRemainingItems();
  sort($remaining);
  $this
    ->assertEquals($expected, $remaining);
  $configuration['languages']['selected'][] = 'l1';
  $datasource
    ->setConfiguration($configuration);
  $index
    ->save();
  $this
    ->runBatch();
  $expected = $get_ids([
    '1:l0',
    '1:l1',
    '2:l0',
    '2:l1',
  ]);
  $this
    ->assertEquals(count($expected), $tracker
    ->getTotalItemsCount());
  $remaining = $tracker
    ->getRemainingItems();
  sort($remaining);
  $this
    ->assertEquals($expected, $remaining);
  $configuration['bundles']['selected'] = [
    'article',
  ];
  $datasource
    ->setConfiguration($configuration);
  $index
    ->save();
  $this
    ->runBatch();
  $expected = $get_ids([
    '2:l0',
    '2:l1',
  ]);
  $this
    ->assertEquals(count($expected), $tracker
    ->getTotalItemsCount());
  $remaining = $tracker
    ->getRemainingItems();
  sort($remaining);
  $this
    ->assertEquals($expected, $remaining);
}