View source
<?php
class AcquiaSPITestCase extends DrupalWebTestCase {
protected $privileged_user;
protected $setup_path;
protected $credentials_path;
protected $settings_path;
protected $status_report_url;
protected $platformKeys = array(
'php',
'webserver_type',
'webserver_version',
'apache_modules',
'php_extensions',
'php_quantum',
'database_type',
'database_version',
'system_type',
'system_version',
'mysql',
);
protected $spiDataKeys = array(
'spi_data_version',
'site_key',
'modules',
'platform',
'quantum',
'system_status',
'failed_logins',
'404s',
'watchdog_size',
'watchdog_data',
'last_nodes',
'last_users',
'extra_files',
'ssl_login',
'file_hashes',
'hashes_md5',
'hashes_sha1',
'fileinfo',
'distribution',
'base_version',
'build_data',
'roles',
'uid_0_present',
);
public static function getInfo() {
return array(
'name' => 'Acquia SPI',
'description' => 'Test sending Acquia SPI data.',
'group' => 'Acquia',
);
}
public function setUp() {
global $base_url;
parent::setUp('acquia_agent', 'acquia_spi', 'acquia_connector_test');
$this->privileged_user = $this
->drupalCreateUser(array(
'administer site configuration',
'access administration pages',
));
$this
->drupalLogin($this->privileged_user);
$this->credentials_path = 'admin/config/system/acquia-agent/credentials';
$this->settings_path = 'admin/config/system/acquia-agent';
$this->status_report_url = 'admin/reports/status';
variable_set('acquia_network_address', $base_url);
variable_set('acquia_spi_server', $base_url);
variable_set('acquia_spi_ssl_override', TRUE);
variable_set('acquia_agent_verify_peer', FALSE);
}
private function acquiaSPIStrings($id) {
switch ($id) {
case 'spi-status-text':
return 'SPI data will be sent once every 30 minutes once cron is called';
case 'spi-not-sent':
return 'SPI data has not been sent';
case 'spi-send-text':
return 'manually send SPI data';
case 'spi-data-sent':
return 'SPI data sent';
case 'spi-data-sent-error':
return 'Error sending SPI data. Consult the logs for more information.';
case 'spi-new-def':
return 'There are new checks that will be performed on your site by the Acquia Connector';
}
}
public function testAcquiaSPIUI() {
$this
->drupalGet($this->status_report_url);
$this
->assertNoText($this
->acquiaSPIStrings('spi-status-text'), 'SPI send option does not exist when site is not connected');
$edit_fields = array(
'acquia_identifier' => ACQTEST_ERROR_ID,
'acquia_key' => ACQTEST_ERROR_KEY,
);
$submit_button = 'Connect';
$this
->drupalPost($this->credentials_path, $edit_fields, $submit_button);
$this
->drupalGet($this->status_report_url);
$this
->assertText($this
->acquiaSPIStrings('spi-status-text'), 'SPI explanation text exists');
$this
->clickLink($this
->acquiaSPIStrings('spi-send-text'));
$this
->assertNoText($this
->acquiaSPIStrings('spi-data-sent'), 'SPI data was not sent');
$this
->assertText($this
->acquiaSPIStrings('spi-data-sent-error'), 'Page says there was an error sending data');
$this
->connectSite();
$this
->drupalGet($this->status_report_url);
$this
->clickLink($this
->acquiaSPIStrings('spi-send-text'));
$this
->assertText($this
->acquiaSPIStrings('spi-data-sent'), 'SPI data was sent');
$this
->assertNoText($this
->acquiaSPIStrings('spi-not-sent'), 'SPI does not say "data has not been sent"');
}
public function testAcquiaSPIDataStore() {
$data = array(
'foo' => 'bar',
);
acquia_spi_data_store_set(array(
'testdata' => $data,
));
$stored_data = acquia_spi_data_store_get(array(
'testdata',
));
$diff = array_diff($stored_data['testdata'], $data);
$this
->assertTrue(empty($diff), 'Storage can store simple array');
$this
->drupalGet('/node');
$stored = acquia_spi_data_store_get(array(
'platform',
));
$diff = array_diff(array_keys($stored['platform']), $this->platformKeys);
$this
->assertTrue(empty($diff), 'Platform element contains expected keys');
}
public function testAcquiaSPIGet() {
$spi_data = acquia_spi_get();
$valid = is_array($spi_data);
$this
->assertTrue($valid, 'acquia_spi_get() returns an array');
if ($valid) {
foreach ($this->spiDataKeys as $key) {
if (!array_key_exists($key, $spi_data)) {
$valid = FALSE;
break;
}
}
$this
->assertTrue($valid, 'Array has expected keys');
$this
->assertEqual(sha1(drupal_get_private_key()), $spi_data['site_key'], 'Site key is sha1 of Drupal private key');
$this
->assertTrue(!empty($spi_data['spi_data_version']), 'SPI data version is set');
$vars = drupal_json_decode($spi_data['system_vars']);
$this
->assertTrue(is_array($vars), 'SPI data system_vars is a JSON-encoded array');
$this
->assertTrue(isset($vars['user_admin_role']), 'user_admin_role included in SPI data');
$this
->assertTrue(!empty($spi_data['modules']), 'Modules is not empty');
$modules = array(
'status',
'name',
'version',
'package',
'core',
'project',
'filename',
'module_data',
);
$diff = array_diff(array_keys($spi_data['modules'][0]), $modules);
$this
->assertTrue(empty($diff), 'Module elements have expected keys');
$diff = array_diff(array_keys($spi_data['platform']), $this->platformKeys);
$this
->assertTrue(empty($diff), 'Platform contains expected keys');
$this
->assertTrue(isset($spi_data['platform']['php_quantum']['SERVER']), 'Global server data included in SPI data');
$this
->assertTrue(isset($spi_data['platform']['php_quantum']['SERVER']['SERVER_SOFTWARE']), 'Server software data set within global server info');
$this
->assertTrue(isset($spi_data['platform']['mysql']['Select_scan']), 'Mysql info in platform contains an expected key');
$this
->assertTrue(isset($spi_data['file_hashes']['includes/database']), 'File hashes array contains an expected key');
$roles = drupal_json_decode($spi_data['roles']);
$this
->assertTrue(is_array($roles), 'Roles is an array');
$this
->assertTrue(isset($roles[1]) && $roles[1] === 'anonymous user', 'Roles array contains anonymous user');
$this
->assertTrue(isset($spi_data['fileinfo']['scripts/drupal.sh']), 'Fileinfo contains an expected key');
$this
->assertTrue(strpos($spi_data['fileinfo']['scripts/drupal.sh'], 'mt') === 0, 'Fileinfo element begins with expected value');
}
}
public function testAcquiaSPISend() {
$edit_fields = array(
'acquia_identifier' => ACQTEST_ERROR_ID,
'acquia_key' => ACQTEST_ERROR_KEY,
);
$submit_button = 'Connect';
$this
->drupalPost($this->credentials_path, $edit_fields, $submit_button);
$result = acquia_spi_send_data(array());
$this
->assertFalse($result, 'acquia_spi_send_data returns false when not connected.');
$this
->connectSite();
$spi_data = acquia_spi_get();
$result = acquia_spi_send_data($spi_data);
$this
->assertTrue(is_array($result), 'SPI update result is an array');
$spi_data['test_validation_error'] = TRUE;
$result = acquia_spi_send_data($spi_data);
$this
->assertFalse($result, 'SPI result is false if validation error.');
unset($spi_data['test_validation_error']);
$spi_data['spi_def_update'] = TRUE;
$result = acquia_spi_send_data($spi_data);
$this
->assertTrue(!empty($result['update_spi_definition']), 'SPI result array has expected "update_spi_definition" key.');
}
public function testAcquiaSPIUpdateResponse() {
$def_timestamp = variable_get('acquia_spi_def_timestamp', 0);
$this
->assertEqual($def_timestamp, 0, 'SPI definition has not been called before');
$def_vars = variable_get('acquia_spi_def_vars', array());
$this
->assertTrue(empty($def_vars), 'SPI definition variables is empty');
$waived_vars = variable_get('acquia_spi_def_waived_vars', array());
$this
->assertTrue(empty($waived_vars), 'SPI definition waived variables is empty');
$update_res = acquia_spi_update_definition();
$this
->assertTrue($update_res, 'Update definition call returned true');
$def_timestamp = variable_get('acquia_spi_def_timestamp', 0);
$this
->assertNotEqual($def_timestamp, 0, 'SPI definition timestamp set');
$def_vars = variable_get('acquia_spi_def_vars', array());
$this
->assertTrue(!empty($def_vars), 'SPI definition variable set');
variable_set('acquia_spi_def_waived_vars', array(
'user_admin_role',
));
$spi_data = acquia_spi_get();
$vars = drupal_json_decode($spi_data['system_vars']);
$this
->assertTrue(!empty($vars['file_temporary_path']), 'New variables included in SPI data');
$this
->assertTrue(!isset($vars['user_admin_role']), 'user_admin_role not included in SPI data');
}
public function testAcquiaSPIMessages() {
$this
->connectSite();
$spi_data = acquia_spi_get();
$response = acquia_spi_send_data($spi_data);
$this
->assertTrue(!isset($response['nspi_messages']), 'No NSPI messages when send_method not set');
$method = $this
->randomString();
$spi_data['send_method'] = $method;
$response = acquia_spi_send_data($spi_data);
$this
->assertIdentical($response['nspi_messages'][0], $method, 'NSPI messages when send_method is set');
$this
->drupalGet($this->status_report_url);
$this
->clickLink($this
->acquiaSPIStrings('spi-send-text'));
$this
->assertText(ACQUIA_SPI_METHOD_CALLBACK, 'NSPI messages printed on status page');
}
public function testAcquiaSPISetVariables() {
$spi_data = acquia_spi_get();
$vars = drupal_json_decode($spi_data['system_vars']);
$this
->assertTrue(empty($vars['acquia_spi_saved_variables']), 'Have not saved any variables');
$edit = array(
'error_level' => 2,
);
$this
->drupalPost('admin/config/development/logging', $edit, 'Save configuration');
$set_variables = array(
'error_level' => 0,
);
acquia_spi_set_variables($set_variables);
$new = variable_get('error_level', '2');
$this
->assertTrue($new === 0, 'Set error reporting to log only');
$vars = drupal_json_decode(acquia_spi_get_variables_data());
$this
->assertTrue(in_array('error_level', $vars['acquia_spi_saved_variables']['variables']), 'SPI data reports error level was saved');
$this
->assertTrue(isset($vars['acquia_spi_saved_variables']['time']), 'Set time for saved variables');
$current = variable_get('clean_url', 'test');
$set_variables = array(
'clean_url' => 0,
);
acquia_spi_set_variables($set_variables);
$after = variable_get('clean_url', 'set');
$this
->assertIdentical($current, $after, 'Non-whitelisted variable cannot be automatically set');
$vars = drupal_json_decode(acquia_spi_get_variables_data());
$this
->assertFalse(in_array('clean_url', $vars['acquia_spi_saved_variables']['variables']), 'SPI data does not include anything about trying to save clean url');
$set_variables = array(
'acquia_spi_set_variables_automatic' => array(
'test_variable',
),
);
acquia_spi_set_variables($set_variables);
$vars = drupal_json_decode(acquia_spi_get_variables_data());
$this
->assertFalse(isset($vars['acquia_spi_set_variables_automatic']), 'Using default list of approved list of variables');
variable_set('acquia_spi_set_variables_override', 1);
$set_variables = array(
'acquia_spi_set_variables_automatic' => array(
'test_variable',
),
);
acquia_spi_set_variables($set_variables);
$vars = drupal_json_decode(acquia_spi_get_variables_data());
$this
->assertIdentical($vars['acquia_spi_set_variables_automatic'], array(
'test_variable',
), 'Altered approved list of variables that can be set');
}
protected function connectSite() {
$edit_fields = array(
'acquia_identifier' => ACQTEST_ID,
'acquia_key' => ACQTEST_KEY,
);
$submit_button = 'Connect';
$this
->drupalPost($this->credentials_path, $edit_fields, $submit_button);
}
}