You are here

search_api_et.test in Search API Entity Translation 7.2

Tests for Search API Entity Translation.

File

tests/search_api_et.test
View source
<?php

/**
 * @file
 * Tests for Search API Entity Translation.
 */

/**
 * Tests adding, updating and deleting multilingual Search API items.
 */
class SearchApiEtTestCase extends EntityTranslationTestCase {
  protected $serverId;
  protected $indexId;

  /**
   * Returns the test information.
   */
  public static function getInfo() {
    return array(
      'name' => 'Search API Entity translation',
      'description' => 'Ensure that Search API Entity translation functions properly.',
      'group' => 'Search API Entity translation',
    );
  }

  /**
   * {@inheritdoc}
   */
  public function setUp() {
    parent::setUp('locale', 'entity_translation', 'search_api', 'search_api_db', 'search_api_et');
    $this
      ->login($this
      ->getAdminUser());
    $this
      ->addLanguage('en');
    $this
      ->addLanguage('es');
    $this
      ->addLanguage('fr');
    $this
      ->disableLanguage('fr');
    $this
      ->configureContentType();
    $this
      ->login($this
      ->getTranslatorUser());
    $this
      ->createServer();
    $this
      ->createIndex();
  }

  /**
   * Tests adding, updating and deleting multilingual Search API items.
   */
  public function testIndex() {

    // Create Basic page in English.
    $node_title = $this
      ->randomName();
    $node_body = $this
      ->randomName();
    $node = $this
      ->createPage($node_title, $node_body, 'en');
    $item_id = SearchApiEtHelper::buildItemId($node->nid, 'en');
    $this
      ->assertTrue($this
      ->itemIdExists($item_id));
    $this
      ->assertTrue($this
      ->itemIdExistsOnBackend($item_id));

    // Update without any changes.
    $this
      ->drupalPost('node/' . $node->nid . '/edit', array(), t('Save'));
    $this
      ->assertTrue($this
      ->itemIdNotExistsOnBackend($node->nid));

    // Delete the Basic page.
    $edit = array();
    $this
      ->drupalPost('node/' . $node->nid . '/delete', $edit, t('Delete'));
    $this
      ->assertTrue($this
      ->itemIdNotExists($item_id));
    $this
      ->assertTrue($this
      ->itemIdNotExistsOnBackend($item_id));

    // Create a second page in Language neutral.
    $node_title = $this
      ->randomName();
    $node_body = $this
      ->randomName();
    $edit = array(
      'title' => $node_title,
      "body[en][0][value]" => $node_body,
      'language' => LANGUAGE_NONE,
    );
    $this
      ->drupalPost('node/add/page', $edit, t('Save'));
    $node2 = $this
      ->drupalGetNodeByTitle($node_title);
    $item_id = SearchApiEtHelper::buildItemId($node2->nid, LANGUAGE_NONE);
    $this
      ->assertTrue($this
      ->itemIdExists($item_id));
    $this
      ->assertTrue($this
      ->itemIdExistsOnBackend($item_id));

    // Delete the second page.
    $edit = array();
    $this
      ->drupalPost('node/' . $node2->nid . '/delete', $edit, t('Delete'));
    $this
      ->assertTrue($this
      ->itemIdNotExists($item_id));
    $this
      ->assertTrue($this
      ->itemIdNotExistsOnBackend($item_id));

    // Add Basic page with translations.
    $node = $this
      ->createPage($this
      ->randomName(), $this
      ->randomName(), 'en');
    $en_item_id = SearchApiEtHelper::buildItemId($node->nid, 'en');
    $this
      ->assertTrue($this
      ->itemIdExists($en_item_id));
    $this
      ->assertTrue($this
      ->itemIdExistsOnBackend($en_item_id));

    // Add a French translation.
    $this
      ->createTranslation($node, $node_title, $this
      ->randomName(), 'fr');
    $fr_item_id = SearchApiEtHelper::buildItemId($node->nid, 'fr');
    $this
      ->assertTrue($this
      ->itemIdExists($fr_item_id));
    $this
      ->assertTrue($this
      ->itemIdExistsOnBackend($fr_item_id));

    // Delete the French translation.
    $this
      ->deleteTranslation($node, 'fr');
    $this
      ->assertTrue($this
      ->itemIdExists($en_item_id));
    $this
      ->assertTrue($this
      ->itemIdExistsOnBackend($en_item_id));
    $this
      ->assertTrue($this
      ->itemIdNotExists($fr_item_id));
    $this
      ->assertTrue($this
      ->itemIdNotExistsOnBackend($fr_item_id));

    // Add Basic page and change its source language.
    $node = $this
      ->createPage($this
      ->randomName(), $this
      ->randomName(), 'en');
    $en_item_id = SearchApiEtHelper::buildItemId($node->nid, 'en');
    $this
      ->assertTrue($this
      ->itemIdExists($en_item_id));
    $this
      ->assertTrue($this
      ->itemIdExistsOnBackend($en_item_id));
    $this
      ->drupalPost('node/' . $node->nid . '/edit', array(
      'language' => 'fr',
    ), t('Save'));
    $fr_item_id = SearchApiEtHelper::buildItemId($node->nid, 'fr');
    $this
      ->assertTrue($this
      ->itemIdExists($fr_item_id));
    $this
      ->assertTrue($this
      ->itemIdExistsOnBackend($fr_item_id));
    $this
      ->assertTrue($this
      ->itemIdNotExists($en_item_id));
    $this
      ->assertTrue($this
      ->itemIdNotExistsOnBackend($en_item_id));
  }

  /**
   * Checks if a Search API Entity translation item id is indexed.
   *
   * @param string $item_id
   *   A Search API Entity translation id, eg. 2/en.
   *
   * @return bool
   *   TRUE if an item_id exists, otherwise FALSE.
   */
  protected function itemIdExists($item_id) {
    $result = db_select('search_api_et_item', 's')
      ->fields('s', array(
      'item_id',
    ))
      ->condition('s.item_id', $item_id)
      ->execute()
      ->fetchField();
    return $result === $item_id;
  }

  /**
   * Checks if a Search API Entity item is indexed on the backend.
   *
   * @param string $item_id
   *   A Search API Entity translation id, eg. 2/en, or a generic Search API
   *   item id, eg. 2.
   *
   * @return bool
   *   TRUE if an item_id exists, otherwise FALSE.
   */
  protected function itemIdExistsOnBackend($item_id) {
    $result = db_select('search_api_db_test_index', 's')
      ->fields('s', array(
      'item_id',
    ))
      ->condition('s.item_id', $item_id)
      ->execute()
      ->fetchField();
    return $result === $item_id;
  }

  /**
   * Checks if a Search API Entity translation item id is not indexed.
   *
   * @param string $item_id
   *   A Search API Entity translation id, eg. 2/en.
   *
   * @return bool
   *   TRUE if an item_id does not exist, otherwise FALSE.
   */
  protected function itemIdNotExists($item_id) {
    $result = db_select('search_api_et_item', 's')
      ->fields('s', array(
      'item_id',
    ))
      ->condition('s.item_id', $item_id)
      ->execute()
      ->fetchField();
    return empty($result);
  }

  /**
   * Checks if a Search API item is not indexed on the backend.
   *
   * @param string $item_id
   *   A Search API Entity translation id, eg. 2/en, or a generic Search API
   *   item id, eg. 2.
   *
   * @return bool
   *   TRUE if an item_id exists, otherwise FALSE.
   */
  protected function itemIdNotExistsOnBackend($item_id) {
    $result = db_select('search_api_db_test_index', 's')
      ->fields('s', array(
      'item_id',
    ))
      ->condition('s.item_id', $item_id)
      ->execute()
      ->fetchField();
    return empty($result);
  }

  /**
   * Helper function to create a Search API server.
   */
  protected function createServer() {
    $this->serverId = 'database_search_server';
    global $databases;
    $database = 'default:default';

    // Make sure to pick an available connection and to not rely on any
    // defaults.
    foreach ($databases as $key => $targets) {
      foreach ($targets as $target => $info) {
        $database = "{$key}:{$target}";
        break;
      }
    }
    $values = array(
      'name' => 'Database search server',
      'machine_name' => $this->serverId,
      'enabled' => 1,
      'description' => 'A server used for testing.',
      'class' => 'search_api_db_service',
      'options' => array(
        'min_chars' => 3,
        'database' => $database,
        'partial_matches' => FALSE,
      ),
    );
    $success = (bool) entity_create('search_api_server', $values)
      ->save();
    $this
      ->assertTrue($success, 'The server was successfully created.');
  }

  /**
   * Helper function to create a Search API index.
   */
  protected function createIndex() {
    $this->indexId = 'test_index';
    $values = array(
      'name' => 'Test index',
      'machine_name' => $this->indexId,
      'item_type' => 'search_api_et_node',
      'enabled' => 1,
      'description' => 'An index used for testing.',
      'server' => $this->serverId,
      'options' => array(
        'cron_limit' => -1,
        'index_directly' => TRUE,
        'fields' => array(
          'id' => array(
            'type' => 'integer',
          ),
          'title' => array(
            'type' => 'text',
            'boost' => '5.0',
          ),
          'body' => array(
            'type' => 'text',
          ),
          'type' => array(
            'type' => 'string',
          ),
          'keywords' => array(
            'type' => 'list<string>',
          ),
          'search_api_language' => array(
            'type' => 'string',
          ),
        ),
      ),
    );
    $index = entity_create('search_api_index', $values);
    $success = (bool) $index
      ->save();
    $this
      ->assertTrue($success, 'The index was successfully created.');
  }

  /**
   * Deletes a node translation.
   *
   * @param object $node
   *   Node of the basic page to delete translation for.
   * @param string $langcode
   *   The language code of the translation to be removed.
   */
  protected function deleteTranslation($node, $langcode) {
    $this
      ->drupalPost('node/' . $node->nid . '/translate/delete/' . $langcode, array(), t('Delete'));
    $this
      ->assertNoLinkByHref('node/' . $node->nid . '/edit/' . $langcode, 0, t('Translation edit link not found. Translation deleted.'));
  }

}

Classes

Namesort descending Description
SearchApiEtTestCase Tests adding, updating and deleting multilingual Search API items.