View source
<?php
namespace Drupal\Tests\acquia_search\Functional;
use Drupal\acquia_connector\Helper\Storage;
use Drupal\Core\Database\Database;
use Drupal\Tests\BrowserTestBase;
class AcquiaConnectorSearchOverrideTest extends BrowserTestBase {
protected $defaultTheme = 'stark';
protected $id;
protected $key;
protected $salt;
protected $server;
protected $index;
public static $modules = [
'acquia_connector',
'search_api',
'search_api_solr',
'toolbar',
'acquia_connector_test',
'node',
'path_alias',
'acquia_search_test',
];
public function setUp() {
parent::setUp();
$this->id = 'TEST_AcquiaConnectorTestID';
$this->key = 'TEST_AcquiaConnectorTestKey';
$this->salt = $this
->randomString(32);
$this->server = 'acquia_search_server';
$this->index = 'acquia_search_index';
$content_type = $this
->drupalCreateContentType();
$node_data = [
'type' => $content_type
->id(),
];
$this
->drupalCreateNode($node_data);
$this
->connect();
$this
->setAvailableSearchCores();
}
public function testOverrides() {
$this
->caseNonAcquiaHosted();
$this
->caseAcquiaHostingEnvironmentDetected();
$this
->caseAcquiaHostingEnvironmentDetectedNoAvailableCores();
$this
->caseAcquiaHostingProdEnvironmentDetectedWithoutProdFlag();
$this
->caseAcquiaHostingProdEnvironmentDetectedWithProdFlag();
}
protected function caseNonAcquiaHosted() {
$this
->drupalGet('/admin/config/search/search-api/server/' . $this->server);
$this
->assertText('automatically enforced read-only mode on this connection.');
$this
->drupalGet('/admin/config/search/search-api/index/' . $this->index);
$this
->assertText('automatically enforced read-only mode on this connection.');
}
protected function caseAcquiaHostingEnvironmentDetected() {
$overrides = [
'env-overrides' => 1,
'AH_SITE_ENVIRONMENT' => 'dev',
'AH_SITE_NAME' => 'testsite1dev',
'AH_SITE_GROUP' => 'testsite1',
];
$this
->drupalGet('/admin/config/search/search-api/server/' . $this->server, [
'query' => $overrides,
]);
$this
->assertNoText('automatically enforced read-only mode on this connection.');
$this
->assertNoText('The following Acquia Search Solr index IDs would have worked for your current environment');
$this
->drupalGet('/admin/config/search/search-api/index/' . $this->index, [
'query' => $overrides,
]);
$this
->assertNoText('automatically enforced read-only mode on this connection.');
$this
->assertNoText('The following Acquia Search Solr index IDs would have worked for your current environment');
}
protected function caseAcquiaHostingEnvironmentDetectedNoAvailableCores() {
$overrides = [
'env-overrides' => 1,
'AH_SITE_ENVIRONMENT' => 'test',
'AH_SITE_NAME' => 'testsite1test',
'AH_SITE_GROUP' => 'testsite1',
];
$this
->drupalGet('/admin/config/search/search-api/server/' . $this->server, [
'query' => $overrides,
]);
$this
->assertText('automatically enforced read-only mode on this connection.');
$this
->assertText('The following Acquia Search Solr index IDs would have worked for your current environment');
$this
->assertText($this->id . '.test.' . $this
->getDbName());
$this
->assertText($this->id . '.test.' . $this
->getSiteFolderName());
$this
->drupalGet('/admin/config/search/search-api/index/' . $this->index, [
'query' => $overrides,
]);
$this
->assertText('automatically enforced read-only mode on this connection.');
}
protected function caseAcquiaHostingProdEnvironmentDetectedWithoutProdFlag() {
$overrides = [
'env-overrides' => 1,
'AH_SITE_ENVIRONMENT' => 'prod',
'AH_SITE_NAME' => 'testsite1prod',
'AH_SITE_GROUP' => 'testsite1',
];
$this
->drupalGet('/admin/config/search/search-api/server/' . $this->server, [
'query' => $overrides,
]);
$this
->assertText('automatically enforced read-only mode on this connection.');
$this
->assertText('The following Acquia Search Solr index IDs would have worked for your current environment');
$this
->assertText($this->id . '.prod.' . $this
->getDbName());
$this
->assertText($this->id . '.prod.' . $this
->getSiteFolderName());
$this
->drupalGet('/admin/config/search/search-api/index/' . $this->index, [
'query' => $overrides,
]);
$this
->assertText('automatically enforced read-only mode on this connection.');
}
protected function caseAcquiaHostingProdEnvironmentDetectedWithProdFlag() {
$overrides = [
'env-overrides' => 1,
'AH_SITE_ENVIRONMENT' => 'prod',
'AH_SITE_NAME' => 'testsite1prod',
'AH_SITE_GROUP' => 'testsite1',
'AH_PRODUCTION' => 1,
];
$this
->drupalGet('/admin/config/search/search-api/server/' . $this->server, [
'query' => $overrides,
]);
$this
->assertNoText('automatically enforced read-only mode on this connection.');
$this
->assertNoText('The following Acquia Search Solr index IDs would have worked for your current environment');
$this
->drupalGet('/admin/config/search/search-api/index/' . $this->index, [
'query' => $overrides,
]);
$this
->assertNoText('automatically enforced read-only mode on this connection.');
$this
->assertNoText('The following Acquia Search Solr index IDs would have worked for your current environment');
}
protected function connect() {
\Drupal::configFactory()
->getEditable('acquia_connector.settings')
->set('spi.ssl_verify', FALSE)
->save();
\Drupal::configFactory()
->getEditable('acquia_connector.settings')
->set('spi.ssl_override', TRUE)
->save();
\Drupal::configFactory()
->getEditable('acquia_connector.settings')
->set('spi.server', 'http://mock-spi-server')
->save();
$admin_user = $this
->createAdminUser();
$this
->drupalLogin($admin_user);
$edit_fields = [
'acquia_identifier' => $this->id,
'acquia_key' => $this->key,
];
$submit_button = 'Connect';
$this
->drupalPostForm('admin/config/system/acquia-connector/credentials', $edit_fields, $submit_button);
\Drupal::service('module_installer')
->install([
'acquia_search',
]);
}
protected function createAdminUser() {
$permissions = [
'administer site configuration',
'access administration pages',
'access toolbar',
'administer search_api',
];
return $this
->drupalCreateUser($permissions);
}
protected function setAvailableSearchCores($no_db_flag = FALSE) {
$acquia_identifier = $this->id;
$solr_hostname = 'mock.acquia-search.com';
$site_folder = $this
->getSiteFolderName();
$ah_db_name = $this
->getDbName();
$core_with_folder_name = [
'balancer' => $solr_hostname,
'core_id' => "{$acquia_identifier}.dev.{$site_folder}",
];
$core_with_db_name = [
'balancer' => $solr_hostname,
'core_id' => "{$acquia_identifier}.dev.{$ah_db_name}",
];
$core_with_acquia_identifier = [
'balancer' => $solr_hostname,
'core_id' => "{$acquia_identifier}",
];
if ($no_db_flag) {
$available_cores = [
$core_with_folder_name,
$core_with_acquia_identifier,
];
}
else {
$available_cores = [
$core_with_folder_name,
$core_with_db_name,
$core_with_acquia_identifier,
];
}
$storage = new Storage();
$storage
->setIdentifier($acquia_identifier);
$subscription = \Drupal::state()
->get('acquia_subscription_data');
$subscription['heartbeat_data'] = [
'search_cores' => $available_cores,
];
\Drupal::state()
->set('acquia_subscription_data', $subscription);
}
protected function getSiteFolderName() {
$conf_path = \Drupal::service('site.path');
return substr($conf_path, strrpos($conf_path, '/') + 1);
}
protected function getDbName() {
$db_conn_options = Database::getConnection()
->getConnectionOptions();
return $db_conn_options['database'];
}
}