You are here

public function MonitoringSearchAPITest::testSensors in Monitoring 8

Tests individual sensors.

File

tests/src/Kernel/MonitoringSearchAPITest.php, line 61

Class

MonitoringSearchAPITest
Tests for search API sensor.

Namespace

Drupal\Tests\monitoring\Kernel

Code

public function testSensors() {

  // Create content first to avoid a Division by zero error.
  // Two new articles, none indexed.
  $entity = EntityTestMulRevChanged::create(array(
    'type' => 'article',
  ));
  $entity
    ->save();
  $entity = EntityTestMulRevChanged::create(array(
    'type' => 'article',
  ));
  $entity
    ->save();
  $result = $this
    ->runSensor('search_api_database_search_index');
  $this
    ->assertEqual($result
    ->getValue(), 2);

  // Update the index to test sensor result.
  $index = Index::load('database_search_index');
  $index
    ->indexItems();
  $entity = EntityTestMulRevChanged::create(array(
    'type' => 'article',
  ));
  $entity
    ->save();
  $entity = EntityTestMulRevChanged::create(array(
    'type' => 'article',
  ));
  $entity
    ->save();
  $entity = EntityTestMulRevChanged::create(array(
    'type' => 'article',
  ));
  $entity
    ->save();

  // New articles are not yet indexed.
  $result = $this
    ->runSensor('search_api_database_search_index');
  $this
    ->assertEqual($result
    ->getValue(), 3);
  $index = Index::load('database_search_index');
  $index
    ->indexItems();

  // Everything should be indexed.
  $result = $this
    ->runSensor('search_api_database_search_index');
  $this
    ->assertEqual($result
    ->getValue(), 0);

  // Verify that hooks do not break when sensors unexpectedly do exist or
  // don't exist.
  $sensor = SensorConfig::create(array(
    'id' => 'search_api_existing',
    'label' => 'Existing sensor',
    'plugin_id' => 'search_api_unindexed',
    'settings' => array(
      'index_id' => 'existing',
    ),
  ));
  $sensor
    ->save();
  $index_existing = Index::create([
    'id' => 'existing',
    'status' => FALSE,
    'name' => 'Existing',
    'tracker' => 'default',
  ]);
  $index_existing
    ->save();

  // Manually delete the sensor and then the index.
  $sensor
    ->delete();
  $index_existing
    ->delete();
}