You are here

function AbstractDrupalSolrOnlineWebTestCase::setUpSolr in Apache Solr Search 7

Same name and namespace in other branches
  1. 8 tests/solr_index_and_search.test \AbstractDrupalSolrOnlineWebTestCase::setUpSolr()
  2. 6.3 tests/solr_index_and_search.test \AbstractDrupalSolrOnlineWebTestCase::setUpSolr()
1 call to AbstractDrupalSolrOnlineWebTestCase::setUpSolr()
DrupalSolrOnlineWebTestCase::setUp in tests/solr_index_and_search.test
Implementation of setUp().

File

tests/solr_index_and_search.test, line 26

Class

AbstractDrupalSolrOnlineWebTestCase

Code

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'));
  }
}