You are here

abstract class AbstractDrupalSolrOnlineWebTestCase in Apache Solr Search 8

Same name and namespace in other branches
  1. 6.3 tests/solr_index_and_search.test \AbstractDrupalSolrOnlineWebTestCase
  2. 7 tests/solr_index_and_search.test \AbstractDrupalSolrOnlineWebTestCase

Hierarchy

Expanded class hierarchy of AbstractDrupalSolrOnlineWebTestCase

File

tests/solr_index_and_search.test, line 3

View source
abstract class AbstractDrupalSolrOnlineWebTestCase extends DrupalWebTestCase {
  protected $solr;
  protected $solr_available = FALSE;

  // workaround for drupal.org test bot

  /**
   * Implementation of setUp().
   */
  function setUp() {

    // Install modules needed for this test. This could have been passed in as
    // either a single array argument or a variable number of string arguments.
    $modules = func_get_args();
    if (isset($modules[0]) && is_array($modules[0])) {
      $modules = $modules[0];
    }
    $modules[] = 'apachesolr';
    $modules[] = 'apachesolr_search';
    $modules[] = 'search';
    parent::setUp($modules);
  }
  function setUpSolr() {

    // Load the default server.
    $env_id = apachesolr_default_environment();
    $environment = apachesolr_environment_load($env_id);
    $this->base_solr_url = $environment['url'];

    // Because we are in a clean environment, this will always be the default
    // http://localhost:8983/solr
    $this->core_admin_url = "{$this->base_solr_url}/admin/cores";

    // The core admin url will give a valid response if the
    // Solr server is running locally.
    if ($this
      ->coreAdminAvailable()) {

      // We will use a core named after the simpletest prefix.
      $environment['url'] .= '/' . $this->databasePrefix;
      $filesdir = file_directory_temp();

      // Create our Solr core directory.
      drupal_mkdir("{$filesdir}/solr", 0777, TRUE);

      // Our temporary core is located here.
      $instancedir = realpath($filesdir . "/solr");

      // use the Solr version confs where appropriate.
      $version = $this
        ->getSolrVersion();
      if (isset($version) && $version == 3) {
        $conf_path = dirname(__FILE__) . '/../solr-conf/solr-3.x/*';
      }
      elseif (isset($version) && $version == 4) {
        $conf_path = dirname(__FILE__) . '/../solr-conf/solr-4.x/*';
      }
      else {
        $conf_path = dirname(__FILE__) . '/../solr-conf/solr-1.4/*';
      }
      $patterns = array(
        $conf_path,
        dirname(__FILE__) . '/conf/*',
      );

      // Copy all files in solr-conf dir to our temporary solr core.
      drupal_mkdir("{$instancedir}/conf", 0777, TRUE);
      foreach ($patterns as $pattern) {
        foreach (glob($pattern) as $conf_file) {
          copy($conf_file, "{$instancedir}/conf/" . basename($conf_file));
        }
      }
      $contents = file_get_contents("{$instancedir}/conf/solrconfig.xml");

      // Change the autoCommit time down to 1 second.
      // @todo - use solrcore.properties file for 3.x.
      file_put_contents("{$instancedir}/conf/solrconfig.xml", preg_replace('@<maxTime>[0-9]+</maxTime>@', '<maxTime>1000</maxTime>', $contents));

      // hard chmod -R because it seems drupal dirs are too restricted in a
      // testing environment
      system("chmod -R 777 {$instancedir}");
      $query['name'] = $this->databasePrefix;
      $query['instanceDir'] = $instancedir;
      $created = $this
        ->coreAdmin('CREATE', $query);
      if ($created && apachesolr_server_status($environment['url'])) {
        $this->instancedir = $instancedir;
        $this->solr_url = $environment['url'];
        apachesolr_environment_save($environment);
        $this->solr = apachesolr_get_solr($env_id);
        $this->solr_available = TRUE;
        $this
          ->checkCoreStatus($this->databasePrefix);
      }
    }

    // Workaround for drupal.org test bot.
    // The tests succeed but further tests will not run because $this->solr_available is FALSE.
    if (!$this->solr_available) {
      $this
        ->pass(t('Warning : The solr instance could not be found. Please enable a multicore one on http://localhost:8983/solr'));
    }
  }
  protected function coreAdminAvailable() {
    $url = url($this->core_admin_url, array(
      'query' => array(
        'action' => 'STATUS',
      ),
    ));
    $options['timeout'] = 2;
    $result = drupal_http_request($url, $options);
    return $result->code == 200 && empty($result->error);
  }
  protected function getSolrVersion() {
    $status = $this
      ->coreAdmin('STATUS');
    foreach ($status['status'] as $core_id => $core) {
      $solr = new DrupalApacheSolrService($this->base_solr_url . '/' . $core_id);
      $version = $solr
        ->getSolrVersion();
      if (!empty($version)) {
        return $version;
      }
      else {
        return "1";
      }
    }
  }

  /**
   * Helper function to invoke core admin actions.
   */
  protected function coreAdmin($action, $query = array()) {
    $query['action'] = $action;
    $query['wt'] = 'json';
    $url = url($this->core_admin_url, array(
      'query' => $query,
    ));
    $options['timeout'] = 2;
    $result = drupal_http_request($url, $options);
    if ($result->code == 200) {
      return json_decode($result->data, TRUE);
    }
    else {
      return FALSE;
    }
  }

  /**
   * Helper function to verify that the expected core exists.
   */
  protected function checkCoreStatus($core_name) {
    $response = $this
      ->coreAdmin('STATUS', array(
      'core' => $core_name,
    ));
    $this
      ->assertTrue(isset($response['status'][$core_name]['index']), 'Found Solr test core index status');
  }
  function tearDown() {

    // Workaround for drupal.org test bot
    if ($this->solr_available) {

      // Unload the Solr core & delete all files
      $query = array(
        'core' => $this->databasePrefix,
        'deleteIndex' => 'true',
        'deleteDataDir' => 'true',
        'deleteInstanceDir' => 'true',
      );

      // This is currently broken due to
      // https://issues.apache.org/jira/browse/SOLR-3586
      $this
        ->coreAdmin('UNLOAD', $query);
    }
    parent::tearDown();
  }

}

Members