View source
<?php
if (!defined('REQUEST_TIME')) {
define('REQUEST_TIME', (int) $_SERVER['REQUEST_TIME']);
}
class AcquiaSearchUnitTestCase extends DrupalUnitTestCase {
protected $id;
protected $key;
protected $salt;
protected $derivedKey;
public static function getInfo() {
return array(
'name' => 'Acquia Search unit tests',
'description' => 'Tests the low level Acquia Search functions.',
'group' => 'Acquia',
);
}
public function setUp() {
parent::setUp();
require_once dirname(dirname(__FILE__)) . '/acquia_search.module';
$this->id = $this
->randomAcquiaSearchName(10);
$this->key = $this
->randomAcquiaSearchName(32);
$this->salt = $this
->randomAcquiaSearchName(32);
$this->derivedKey = _acquia_search_create_derived_key($this->salt, $this->id, $this->key);
}
public function testDerivedKey() {
$derivation_string = $this->id . 'solr' . $this->salt;
$derived_key = hash_hmac('sha1', str_pad($derivation_string, 80, $derivation_string), $this->key);
$this
->assertEqual($this->derivedKey, $derived_key, t('Derived key API function generates the expected hash.'), 'Acquia Search');
}
public static function randomAcquiaSearchName($length = 8) {
$values = array_merge(range(65, 90), range(97, 122), range(48, 57));
$max = count($values) - 1;
$str = chr(mt_rand(97, 122));
for ($i = 1; $i < $length; $i++) {
$str .= chr($values[mt_rand(0, $max)]);
}
return $str;
}
public function testHMACCookie() {
$time = REQUEST_TIME;
$nonce = $this
->randomAcquiaSearchName(32);
$string = $time . $nonce . $this
->randomAcquiaSearchName();
$hmac = hash_hmac('sha1', $time . $nonce . $string, $this->derivedKey);
$authenticator = acquia_search_authenticator($string, $nonce, $this->derivedKey);
preg_match('/acquia_solr_hmac=([a-zA-Z0-9]{40});/', $authenticator, $matches);
$this
->assertEqual($hmac, $matches[1], t('HMAC API function generates the expected hmac hash.'), 'Acquia Search');
preg_match('/acquia_solr_time=([0-9]{10});/', $authenticator, $matches);
$this
->assertNotNull($matches, t('HMAC API function generates a timestamp.'), 'Acquia Search');
preg_match('/acquia_solr_nonce=([a-zA-Z0-9]{32});/', $authenticator, $matches);
$this
->assertEqual($nonce, $matches[1], t('HMAC API function generates the expected nonce.'), 'Acquia Search');
}
}
class AcquiaSearchWebTestCase extends DrupalWebTestCase {
protected $id;
protected $key;
protected $salt;
protected $derivedKey;
protected $url;
public static function getInfo() {
return array(
'name' => 'Acquia Search UI tests',
'description' => 'Tests the Acquia Search user interface and functionality.',
'group' => 'Acquia',
);
}
public function setUp() {
parent::setUp('acquia_agent', 'search', 'content', 'apachesolr', 'apachesolr_search', 'acquia_search');
$this->id = AcquiaSearchUnitTestCase::randomAcquiaSearchName(10);
$this->key = AcquiaSearchUnitTestCase::randomAcquiaSearchName(32);
$this->salt = AcquiaSearchUnitTestCase::randomAcquiaSearchName(32);
$this->derivedKey = _acquia_search_create_derived_key($this->salt, $this->id, $this->key);
$subscription = array(
'timestamp' => REQUEST_TIME - 60,
'active' => '1',
);
variable_set('acquia_identifier', $this->id);
variable_set('acquia_key', $this->key);
variable_set('acquia_subscription_data', $subscription);
$environment = acquia_search_get_environment();
$this->url = $environment['url'];
}
public function createAdminUser() {
$permissions = array(
'access content',
'search content',
'administer nodes',
'administer search',
);
return $this
->drupalCreateUser($permissions);
}
public function clearStaticCache() {
apachesolr_default_environment(NULL, TRUE);
apachesolr_load_all_environments(TRUE);
}
public function enableEnvironment() {
acquia_search_enable_acquia_solr_environment();
}
public function testEnvironment() {
$this
->enableEnvironment();
$this
->clearStaticCache();
$environment = apachesolr_environment_load(ACQUIA_SEARCH_ENVIRONMENT_ID);
$default_environment = apachesolr_default_environment();
$this
->assertTrue($environment, t('Acquia Search environment saved.'), 'Acquia Search');
$this
->assertEqual(ACQUIA_SEARCH_ENVIRONMENT_ID, $default_environment, t('Acquia Search is set to the default environment.'), 'Acquia Search');
$class = isset($environment['service_class']) ? $environment['service_class'] : '';
$this
->assertEqual('AcquiaSearchService', $class, t('Acquia Search is using the AcquiaSearchService service class.'), 'Acquia Search');
$this
->assertEqual($this->url, $environment['url'], t('Acquia Search is connected to the expected URL.'), 'Acquia Search');
}
public function testEnvironmentUI() {
$this
->enableEnvironment();
$this
->clearStaticCache();
$admin_user = $this
->createAdminUser();
$this
->drupalLogin($admin_user);
$settings_path = 'admin/settings/apachesolr/settings';
$this
->drupalGet($settings_path);
$this
->assertText('Acquia Search', t('The Acquia Search environment is displayed in the UI.'), 'Acquia Search');
$path = 'admin/settings/apachesolr/settings/' . ACQUIA_SEARCH_ENVIRONMENT_ID . '/edit';
$options = array(
'query' => array(
'destination' => $settings_path,
),
);
$link = l('Acquia Search', $path, $options);
$raw = t('!environment <em>(Default)</em>', array(
'!environment' => $link,
));
$this
->assertRaw($raw, t('The Acquia Search environment is reflected as the default in the UI.'), 'Acquia Search');
$this
->drupalGet('admin/config/search/apachesolr/settings/' . ACQUIA_SEARCH_ENVIRONMENT_ID . '/delete');
$this
->assertResponse(403, t('The Acquia Search environment cannot be deleted via the UI.'));
}
}