You are here

class SearchApiTestService in Search API 7

Test service class.

Hierarchy

Expanded class hierarchy of SearchApiTestService

1 string reference to 'SearchApiTestService'
search_api_test_search_api_service_info in tests/search_api_test.module
Implements hook_search_api_service_info().

File

tests/search_api_test.module, line 226
Test functions and classes for testing the Search API.

View source
class SearchApiTestService extends SearchApiAbstractService {

  /**
   * Overrides SearchApiAbstractService::configurationForm().
   *
   * Returns a single text field for testing purposes.
   */
  public function configurationForm(array $form, array &$form_state) {
    $form = array(
      'test' => array(
        '#type' => 'textfield',
        '#title' => 'Test option',
      ),
    );
    if (!empty($this->options)) {
      $form['test']['#default_value'] = $this->options['test'];
    }
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function addIndex(SearchApiIndex $index) {
    $this
      ->checkErrorState();
  }

  /**
   * {@inheritdoc}
   */
  public function fieldsUpdated(SearchApiIndex $index) {
    $this
      ->checkErrorState();
    return db_query('SELECT COUNT(*) FROM {search_api_test}')
      ->fetchField() > 0;
  }

  /**
   * {@inheritdoc}
   */
  public function removeIndex($index) {
    $this
      ->checkErrorState();
    parent::removeIndex($index);
  }

  /**
   * Implements SearchApiServiceInterface::indexItems().
   *
   * Indexes items by storing their IDs in the server's options.
   *
   * If the "search_api_test_indexing_break" variable is set, the item with
   * that ID will not be indexed.
   */
  public function indexItems(SearchApiIndex $index, array $items) {
    $this
      ->checkErrorState();

    // Refuse to index the item with the same ID as the
    // "search_api_test_indexing_break" variable, if it is set.
    $exclude = variable_get('search_api_test_indexing_break', 8);
    foreach ($items as $id => $item) {
      if ($id == $exclude) {
        unset($items[$id]);
      }
    }
    $ids = array_keys($items);
    $this->options += array(
      'indexes' => array(),
    );
    $this->options['indexes'] += array(
      $index->machine_name => array(),
    );
    $this->options['indexes'][$index->machine_name] += drupal_map_assoc($ids);
    asort($this->options['indexes'][$index->machine_name]);
    $this->server
      ->save();
    return $ids;
  }

  /**
   * Overrides SearchApiAbstractService::preDelete().
   *
   * Overridden so deleteItems() isn't called which would otherwise lead to the
   * server being updated and, eventually, to a notice because there is no
   * server to be updated anymore.
   */
  public function preDelete() {
  }

  /**
   * {@inheritdoc}
   */
  public function deleteItems($ids = 'all', SearchApiIndex $index = NULL) {
    $this
      ->checkErrorState();
    if ($ids == 'all') {
      if ($index) {
        $this->options['indexes'][$index->machine_name] = array();
      }
      else {
        $this->options['indexes'] = array();
      }
    }
    else {
      foreach ($ids as $id) {
        unset($this->options['indexes'][$index->machine_name][$id]);
      }
    }
    $this->server
      ->save();
  }

  /**
   * Implements SearchApiServiceInterface::indexItems().
   *
   * Will ignore all query settings except the range, as only the item IDs are
   * indexed.
   */
  public function search(SearchApiQueryInterface $query) {
    $options = $query
      ->getOptions();
    $ret = array();
    $index_id = $query
      ->getIndex()->machine_name;
    if (empty($this->options['indexes'][$index_id])) {
      return array(
        'result count' => 0,
        'results' => array(),
      );
    }
    $items = $this->options['indexes'][$index_id];
    $min = isset($options['offset']) ? $options['offset'] : 0;
    $max = $min + (isset($options['limit']) ? $options['limit'] : count($items));
    $i = 0;
    $ret['result count'] = count($items);
    $ret['results'] = array();
    foreach ($items as $id) {
      ++$i;
      if ($i > $max) {
        break;
      }
      if ($i > $min) {
        $ret['results'][$id] = array(
          'id' => $id,
          'score' => 1,
        );
      }
    }
    return $ret;
  }

  /**
   * Throws an exception if the "search_api_test_error_state" variable is set.
   *
   * @throws SearchApiException
   *   If the "search_api_test_error_state" variable is set.
   */
  protected function checkErrorState() {
    if (variable_get('search_api_test_error_state', FALSE)) {
      throw new SearchApiException();
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
SearchApiAbstractService::$options protected property Direct reference to the server's $options property.
SearchApiAbstractService::$server protected property
SearchApiAbstractService::configurationFormSubmit public function Implements SearchApiServiceInterface::__construct(). Overrides SearchApiServiceInterface::configurationFormSubmit
SearchApiAbstractService::configurationFormValidate public function Implements SearchApiServiceInterface::__construct(). Overrides SearchApiServiceInterface::configurationFormValidate
SearchApiAbstractService::getExtraInformation public function Returns additional, service-specific information about this server.
SearchApiAbstractService::postCreate public function Implements SearchApiServiceInterface::__construct(). Overrides SearchApiServiceInterface::postCreate
SearchApiAbstractService::postUpdate public function Implements SearchApiServiceInterface::__construct(). Overrides SearchApiServiceInterface::postUpdate
SearchApiAbstractService::query public function Implements SearchApiServiceInterface::__construct(). Overrides SearchApiServiceInterface::query
SearchApiAbstractService::supportsFeature public function Implements SearchApiServiceInterface::__construct(). Overrides SearchApiServiceInterface::supportsFeature 1
SearchApiAbstractService::viewSettings public function Implements SearchApiServiceInterface::__construct(). Overrides SearchApiServiceInterface::viewSettings
SearchApiAbstractService::__construct public function Implements SearchApiServiceInterface::__construct(). Overrides SearchApiServiceInterface::__construct
SearchApiTestService::addIndex public function Implements SearchApiServiceInterface::__construct(). Overrides SearchApiAbstractService::addIndex
SearchApiTestService::checkErrorState protected function Throws an exception if the "search_api_test_error_state" variable is set.
SearchApiTestService::configurationForm public function Overrides SearchApiAbstractService::configurationForm(). Overrides SearchApiAbstractService::configurationForm
SearchApiTestService::deleteItems public function Deletes indexed items from this server. Overrides SearchApiServiceInterface::deleteItems
SearchApiTestService::fieldsUpdated public function Implements SearchApiServiceInterface::__construct(). Overrides SearchApiAbstractService::fieldsUpdated
SearchApiTestService::indexItems public function Implements SearchApiServiceInterface::indexItems(). Overrides SearchApiServiceInterface::indexItems
SearchApiTestService::preDelete public function Overrides SearchApiAbstractService::preDelete(). Overrides SearchApiAbstractService::preDelete
SearchApiTestService::removeIndex public function Implements SearchApiServiceInterface::__construct(). Overrides SearchApiAbstractService::removeIndex
SearchApiTestService::search public function Implements SearchApiServiceInterface::indexItems(). Overrides SearchApiServiceInterface::search