You are here

class SearchApiAcquiaUnitTestCase in Acquia Search for Search API 7.2

Unit tests of the functionality of the Search API Acquia module.

Hierarchy

Expanded class hierarchy of SearchApiAcquiaUnitTestCase

File

tests/search_api_acquia.test, line 12
Tests for the Search API Acquia module.

View source
class SearchApiAcquiaUnitTestCase extends DrupalUnitTestCase {
  protected $id;
  protected $key;
  protected $salt;
  protected $derivedKey;
  public static function getInfo() {
    return array(
      'name' => 'Search API Acquia unit tests',
      'description' => 'Tests the low level Search API Acquia functions.',
      'group' => 'Acquia',
    );
  }

  /**
   * Overrides DrupalTestCase::setUp().
   */
  public function setUp() {
    parent::setUp();
    require_once dirname(dirname(__FILE__)) . '/search_api_acquia.module';

    // Generate and store a random set of credentials.
    // Make them as close to the production values as possible
    // Something like AAAA-1234
    $this->id = $this
      ->randomName(10);

    // Most of the keys and salts have a 32char lenght
    $this->key = $this
      ->randomName(32);
  }

  /**
   * Tests that Search API Acquia properly overrides the connection
   * details to avoid unintended writing to a wrong search index.
   */
  public function testApacheSolrOverride() {
    global $conf;
    $acquia_identifier = $this->id;
    $site_folder = $this
      ->randomName(32);
    $solr_hostname = $this
      ->randomName(10) . '.acquia-search.com';
    $available_cores = array(
      array(
        'balancer' => $solr_hostname,
        'core_id' => "{$acquia_identifier}.dev.{$site_folder}",
      ),
      array(
        'balancer' => $solr_hostname,
        'core_id' => "{$acquia_identifier}.dev.testdbname",
      ),
      array(
        'balancer' => $solr_hostname,
        'core_id' => "{$acquia_identifier}",
      ),
    );
    $server_id = 'ACQUIA';

    // No Acquia hosting and DB detected - should override into Readonly.
    unset($conf['search_api_acquia_overrides']);
    $ah_env = NULL;
    $ah_db_name = '';
    require_once dirname(dirname(__FILE__)) . '/src/SAPIPreferredSearchCoreService.php';
    $core_service = new SAPIPreferredSearchCoreService($acquia_identifier, $ah_env, $site_folder, $ah_db_name, $available_cores);
    search_api_acquia_override_server($core_service, 'ACQUIA');
    $this
      ->assertIdentical($conf['search_api_acquia_overrides']['ACQUIA']['overridden_by_acquia_search'], SEARCH_API_ACQUIA_AUTO_SHOULD_OVERRIDE_READ_ONLY);
    $this
      ->assertTrue(empty($conf['search_api_acquia_overrides']['ACQUIA']['derived_key']));

    // Acquia Dev hosting environment detected - configs point to the index on the Dev environment.
    unset($conf['search_api_acquia_overrides']);
    $ah_env = 'dev';
    $core_service = new SAPIPreferredSearchCoreService($acquia_identifier, $ah_env, $site_folder, $ah_db_name, $available_cores);
    search_api_acquia_override_server($core_service, $server_id);
    $this
      ->assertIdentical($conf['search_api_acquia_overrides']['ACQUIA']['overridden_by_acquia_search'], SEARCH_API_ACQUIA_OVERRIDE_AUTO_SET);
    $this
      ->assertTrue(!empty($conf['search_api_acquia_overrides']['ACQUIA']['derived_key']));
    $this
      ->assertIdentical($conf['search_api_acquia_overrides']['ACQUIA']['host'], $solr_hostname);
    $this
      ->assertIdentical($conf['search_api_acquia_overrides']['ACQUIA']['path'], "/solr/{$acquia_identifier}.dev.{$site_folder}");

    // Acquia dev environment and a DB name.
    unset($conf['search_api_acquia_overrides']);
    $ah_env = 'dev';
    $ah_db_name = 'testdbname';
    $core_service = new SAPIPreferredSearchCoreService($acquia_identifier, $ah_env, $site_folder, $ah_db_name, $available_cores);
    search_api_acquia_override_server($core_service, $server_id);
    $this
      ->assertIdentical($conf['search_api_acquia_overrides']['ACQUIA']['overridden_by_acquia_search'], SEARCH_API_ACQUIA_OVERRIDE_AUTO_SET);
    $this
      ->assertTrue(!empty($conf['search_api_acquia_overrides']['ACQUIA']['derived_key']));
    $this
      ->assertIdentical($conf['search_api_acquia_overrides']['ACQUIA']['host'], $solr_hostname);
    $this
      ->assertIdentical($conf['search_api_acquia_overrides']['ACQUIA']['path'], "/solr/{$acquia_identifier}.dev.{$ah_db_name}");

    // Acquia Test environment and a DB name. According to the mock, no cores available for the Test environment so it is read only.
    unset($conf['search_api_acquia_overrides']);
    $ah_env = 'test';
    $ah_db_name = 'testdbname';
    $core_service = new SAPIPreferredSearchCoreService($acquia_identifier, $ah_env, $site_folder, $ah_db_name, $available_cores);
    search_api_acquia_override_server($core_service, $server_id);
    $this
      ->assertIdentical($conf['search_api_acquia_overrides']['ACQUIA']['overridden_by_acquia_search'], SEARCH_API_ACQUIA_AUTO_SHOULD_OVERRIDE_READ_ONLY);
    $this
      ->assertTrue(empty($conf['search_api_acquia_overrides']['ACQUIA']['derived_key']));
    $this
      ->assertTrue(empty($conf['search_api_acquia_overrides']['ACQUIA']['host']));
    $this
      ->assertTrue(empty($conf['search_api_acquia_overrides']['ACQUIA']['path']));

    // Acquia Prod environment and a DB name but AH_PRODUCTION isn't set - so read only.
    unset($conf['search_api_acquia_overrides']);
    $ah_env = 'prod';
    $ah_db_name = 'testdbname';
    $core_service = new SAPIPreferredSearchCoreService($acquia_identifier, $ah_env, $site_folder, $ah_db_name, $available_cores);
    search_api_acquia_override_server($core_service, $server_id);
    $this
      ->assertIdentical($conf['search_api_acquia_overrides']['ACQUIA']['overridden_by_acquia_search'], SEARCH_API_ACQUIA_AUTO_SHOULD_OVERRIDE_READ_ONLY);
    $this
      ->assertTrue(empty($conf['search_api_acquia_overrides']['ACQUIA']['derived_key']));
    $this
      ->assertTrue(empty($conf['search_api_acquia_overrides']['ACQUIA']['host']));
    $this
      ->assertTrue(empty($conf['search_api_acquia_overrides']['ACQUIA']['path']));

    // Acquia Prod environment and a DB name and AH_PRODUCTION is set - so it should override to connect to the prod index.
    unset($conf['search_api_acquia_overrides']);
    $_SERVER['AH_PRODUCTION'] = 1;
    $ah_env = 'prod';
    $ah_db_name = 'testdbname';
    $core_service = new SAPIPreferredSearchCoreService($acquia_identifier, $ah_env, $site_folder, $ah_db_name, $available_cores);
    search_api_acquia_override_server($core_service, $server_id);
    $this
      ->assertIdentical($conf['search_api_acquia_overrides']['ACQUIA']['overridden_by_acquia_search'], SEARCH_API_ACQUIA_OVERRIDE_AUTO_SET);
    $this
      ->assertTrue(!empty($conf['search_api_acquia_overrides']['ACQUIA']['derived_key']));
    $this
      ->assertIdentical($conf['search_api_acquia_overrides']['ACQUIA']['host'], $solr_hostname);
    $this
      ->assertIdentical($conf['search_api_acquia_overrides']['ACQUIA']['path'], "/solr/{$acquia_identifier}");

    // Trying to override already overridden settings should not succeed.
    unset($_SERVER['AH_PRODUCTION']);
    $ah_env = 'dev';
    $ah_db_name = 'testdbname';
    $core_service = new SAPIPreferredSearchCoreService($acquia_identifier, $ah_env, $site_folder, $ah_db_name, $available_cores);
    search_api_acquia_override_server($core_service, $server_id);
    $this
      ->assertIdentical($conf['search_api_acquia_overrides']['ACQUIA']['overridden_by_acquia_search'], SEARCH_API_ACQUIA_OVERRIDE_AUTO_SET);
    $this
      ->assertTrue(!empty($conf['search_api_acquia_overrides']['ACQUIA']['derived_key']));
    $this
      ->assertIdentical($conf['search_api_acquia_overrides']['ACQUIA']['host'], $solr_hostname);
    $this
      ->assertIdentical($conf['search_api_acquia_overrides']['ACQUIA']['path'], "/solr/{$acquia_identifier}");
  }

  /**
   * Tests that it selects the correct preferred search core ID for the
   * override URL when limited number of core ID is available.
   */
  public function testApacheSolrOverrideWhenCoreWithDbNameNotAvailable() {

    // When the core ID with the DB name in it is not available, it should
    // override the URL value with the core ID that has the site folder name
    // in it.
    global $conf;
    unset($conf['search_api_acquia_overrides']);
    $acquia_identifier = $this->id;
    $site_folder = $this
      ->randomName(32);
    $solr_hostname = $this
      ->randomName(10) . '.acquia-search.com';
    $available_cores = array(
      array(
        'balancer' => $solr_hostname,
        'core_id' => "{$acquia_identifier}.dev.{$site_folder}",
      ),
      array(
        'balancer' => $solr_hostname,
        'core_id' => "{$acquia_identifier}",
      ),
    );
    $ah_env = 'dev';
    $ah_db_name = 'testdbname';
    $server_id = 'ACQUIA';
    $core_service = new SAPIPreferredSearchCoreService($acquia_identifier, $ah_env, $site_folder, $ah_db_name, $available_cores);
    search_api_acquia_override_server($core_service, $server_id);
    $this
      ->assertIdentical($conf['search_api_acquia_overrides']['ACQUIA']['host'], $solr_hostname);
    $this
      ->assertIdentical($conf['search_api_acquia_overrides']['ACQUIA']['path'], "/solr/{$acquia_identifier}.dev.{$site_folder}");
  }

  /**
   * Tests that Search API Acquia properly overrides the connection
   * details to avoid unintended writing to a wrong search index and db.
   */
  public function testApacheSolrOverrideWithUnderscoreInDb() {
    global $conf;
    $acquia_identifier = $this->id;
    $site_folder = $this
      ->randomName(32);
    $solr_hostname = $this
      ->randomName(10) . '.acquia-search.com';
    $available_cores = array(
      array(
        'balancer' => $solr_hostname,
        'core_id' => "{$acquia_identifier}.dev.{$site_folder}",
      ),
      array(
        'balancer' => $solr_hostname,
        'core_id' => "{$acquia_identifier}.dev.test_db_name",
      ),
      array(
        'balancer' => $solr_hostname,
        'core_id' => "{$acquia_identifier}",
      ),
    );
    $server_id = 'ACQUIA';

    // Acquia dev environment and a DB name with '_'.
    unset($conf['search_api_acquia_overrides']);
    $ah_env = 'dev';
    $ah_db_name = 'test_db_name';
    $core_service = new SAPIPreferredSearchCoreService($acquia_identifier, $ah_env, $site_folder, $ah_db_name, $available_cores);
    search_api_acquia_override_server($core_service, $server_id);
    $this
      ->assertIdentical($conf['search_api_acquia_overrides']['ACQUIA']['overridden_by_acquia_search'], SEARCH_API_ACQUIA_OVERRIDE_AUTO_SET);
    $this
      ->assertTrue(!empty($conf['search_api_acquia_overrides']['ACQUIA']['derived_key']));
    $this
      ->assertIdentical($conf['search_api_acquia_overrides']['ACQUIA']['host'], $solr_hostname);
    $this
      ->assertIdentical($conf['search_api_acquia_overrides']['ACQUIA']['path'], "/solr/{$acquia_identifier}.dev.{$ah_db_name}");

    // Acquia dev environment and a DB name with '-' .
    unset($conf['search_api_acquia_overrides']);
    $ah_env = 'dev';
    $ah_db_name = 'test-db-name';
    $core_service = new SAPIPreferredSearchCoreService($acquia_identifier, $ah_env, $site_folder, $ah_db_name, $available_cores);
    search_api_acquia_override_server($core_service, $server_id);
    $this
      ->assertIdentical($conf['search_api_acquia_overrides']['ACQUIA']['overridden_by_acquia_search'], SEARCH_API_ACQUIA_OVERRIDE_AUTO_SET);
    $this
      ->assertTrue(!empty($conf['search_api_acquia_overrides']['ACQUIA']['derived_key']));
    $this
      ->assertIdentical($conf['search_api_acquia_overrides']['ACQUIA']['host'], $solr_hostname);
    $this
      ->assertTrue($conf['search_api_acquia_overrides']['ACQUIA']['path'], "A database with a dash(-) is an invalid database");
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DrupalTestCase::$assertions protected property Assertions thrown in that test case.
DrupalTestCase::$databasePrefix protected property The database prefix of this test run.
DrupalTestCase::$originalFileDirectory protected property The original file directory, before it was changed for testing purposes.
DrupalTestCase::$results public property Current results of this test case.
DrupalTestCase::$setup protected property Flag to indicate whether the test has been set up.
DrupalTestCase::$setupDatabasePrefix protected property
DrupalTestCase::$setupEnvironment protected property
DrupalTestCase::$skipClasses protected property This class is skipped when looking for the source of an assertion.
DrupalTestCase::$testId protected property The test run ID.
DrupalTestCase::$timeLimit protected property Time limit for the test.
DrupalTestCase::$useSetupInstallationCache public property Whether to cache the installation part of the setUp() method.
DrupalTestCase::$useSetupModulesCache public property Whether to cache the modules installation part of the setUp() method.
DrupalTestCase::$verboseDirectoryUrl protected property URL to the verbose output file directory.
DrupalTestCase::assert protected function Internal helper: stores the assert.
DrupalTestCase::assertEqual protected function Check to see if two values are equal.
DrupalTestCase::assertFalse protected function Check to see if a value is false (an empty string, 0, NULL, or FALSE).
DrupalTestCase::assertIdentical protected function Check to see if two values are identical.
DrupalTestCase::assertNotEqual protected function Check to see if two values are not equal.
DrupalTestCase::assertNotIdentical protected function Check to see if two values are not identical.
DrupalTestCase::assertNotNull protected function Check to see if a value is not NULL.
DrupalTestCase::assertNull protected function Check to see if a value is NULL.
DrupalTestCase::assertTrue protected function Check to see if a value is not false (not an empty string, 0, NULL, or FALSE).
DrupalTestCase::deleteAssert public static function Delete an assertion record by message ID.
DrupalTestCase::error protected function Fire an error assertion. 1
DrupalTestCase::errorHandler public function Handle errors during test runs. 1
DrupalTestCase::exceptionHandler protected function Handle exceptions.
DrupalTestCase::fail protected function Fire an assertion that is always negative.
DrupalTestCase::generatePermutations public static function Converts a list of possible parameters into a stack of permutations.
DrupalTestCase::getAssertionCall protected function Cycles through backtrace until the first non-assertion method is found.
DrupalTestCase::getDatabaseConnection public static function Returns the database connection to the site running Simpletest.
DrupalTestCase::insertAssert public static function Store an assertion from outside the testing context.
DrupalTestCase::pass protected function Fire an assertion that is always positive.
DrupalTestCase::randomName public static function Generates a random string containing letters and numbers.
DrupalTestCase::randomString public static function Generates a random string of ASCII characters of codes 32 to 126.
DrupalTestCase::run public function Run all tests in this class.
DrupalTestCase::verbose protected function Logs a verbose message in a text file.
DrupalUnitTestCase::tearDown protected function 1
DrupalUnitTestCase::__construct function Constructor for DrupalUnitTestCase. Overrides DrupalTestCase::__construct
SearchApiAcquiaUnitTestCase::$derivedKey protected property
SearchApiAcquiaUnitTestCase::$id protected property
SearchApiAcquiaUnitTestCase::$key protected property
SearchApiAcquiaUnitTestCase::$salt protected property
SearchApiAcquiaUnitTestCase::getInfo public static function
SearchApiAcquiaUnitTestCase::setUp public function Overrides DrupalTestCase::setUp(). Overrides DrupalUnitTestCase::setUp
SearchApiAcquiaUnitTestCase::testApacheSolrOverride public function Tests that Search API Acquia properly overrides the connection details to avoid unintended writing to a wrong search index.
SearchApiAcquiaUnitTestCase::testApacheSolrOverrideWhenCoreWithDbNameNotAvailable public function Tests that it selects the correct preferred search core ID for the override URL when limited number of core ID is available.
SearchApiAcquiaUnitTestCase::testApacheSolrOverrideWithUnderscoreInDb public function Tests that Search API Acquia properly overrides the connection details to avoid unintended writing to a wrong search index and db.