View source
<?php
namespace Drupal\Tests\acquia_search\Kernel;
use Drupal\acquia_connector\Helper\Storage;
use Drupal\acquia_search\EventSubscriber\SearchSubscriber;
use Drupal\acquia_search\Plugin\SolrConnector\SearchApiSolrAcquiaConnector;
use Drupal\Core\Database\Database;
use Drupal\KernelTests\KernelTestBase;
use GuzzleHttp\Client;
class AcquiaSearchOverrideTest extends KernelTestBase {
public static $modules = [
'user',
'path_alias',
'acquia_connector',
'search_api',
'acquia_search',
];
protected function setUp() {
parent::setUp();
$this
->installConfig([
'acquia_connector',
]);
$guzzle = $this
->createMock(Client::class);
$guzzle
->expects($this
->any())
->method('__call')
->with('get')
->will($this
->returnValue(''));
$client_factory = $this
->getMockBuilder('Drupal\\Core\\Http\\ClientFactory')
->disableOriginalConstructor()
->getMock();
$client_factory
->expects($this
->any())
->method('fromOptions')
->will($this
->returnValue($guzzle));
$this->container
->set('http_client_factory', $client_factory);
$search_subscriber = new SearchSubscriber();
$event_dispatcher = \Drupal::service('event_dispatcher');
$event_dispatcher
->addSubscriber($search_subscriber);
}
public function testNonAcquiaHosted() {
$this
->setAvailableSearchCores();
$connector_plugin = new SearchApiSolrAcquiaConnector([], 'foo', [
'foo',
]);
$solr_connector = $connector_plugin
->create($this->container, [], 'foo', [
'foo',
]);
$config = $solr_connector
->defaultConfiguration();
$this
->assertEquals(ACQUIA_SEARCH_AUTO_OVERRIDE_READ_ONLY, $config['overridden_by_acquia_search']);
$this
->assertGetUpdateQueryException($solr_connector);
}
public function testAcquiaHostingEnvironmentDetected() {
$_ENV['AH_SITE_ENVIRONMENT'] = 'dev';
$_ENV['AH_SITE_NAME'] = 'testsite1dev';
$_ENV['AH_SITE_GROUP'] = 'testsite1';
$this
->setAvailableSearchCores();
$connector_plugin = new SearchApiSolrAcquiaConnector([], 'foo', [
'foo',
]);
$solr_connector = $connector_plugin
->create($this->container, [], 'foo', [
'foo',
]);
$config = $solr_connector
->defaultConfiguration();
$db_name = $this
->getDbName();
$this
->assertEquals(ACQUIA_SEARCH_OVERRIDE_AUTO_SET, $config['overridden_by_acquia_search']);
$this
->assertEquals('WXYZ-12345.dev.' . $db_name, $config['index_id']);
$this
->assertGetUpdateQueryNoException($solr_connector);
}
public function testAcquiaSearchV3CoreDetected() {
$_ENV['AH_SITE_ENVIRONMENT'] = 'dev';
$_ENV['AH_SITE_NAME'] = 'testsite1dev';
$_ENV['AH_SITE_GROUP'] = 'testsite1';
$this
->setAvailableSearchCores();
$connector_plugin = new SearchApiSolrAcquiaConnector([], 'foo', [
'foo',
]);
$solr_connector = $connector_plugin
->create($this->container, [], 'foo', [
'foo',
]);
$config = $solr_connector
->defaultConfiguration();
$db_name = $this
->getDbName();
$this
->assertEquals(ACQUIA_SEARCH_OVERRIDE_AUTO_SET, $config['overridden_by_acquia_search']);
$this
->assertEquals('WXYZ-12345.dev.' . $db_name, $config['index_id']);
$this
->assertEquals('sr-dev.acquia.com', $config['host']);
$this
->assertGetUpdateQueryNoException($solr_connector);
}
public function testAcquiaHostingEnvironmentDetectedNoAvailableCores() {
$_ENV['AH_SITE_ENVIRONMENT'] = 'test';
$_ENV['AH_SITE_NAME'] = 'testsite1test';
$_ENV['AH_SITE_GROUP'] = 'testsite1';
$this
->setAvailableSearchCores();
$connector_plugin = new SearchApiSolrAcquiaConnector([], 'foo', [
'foo',
]);
$solr_connector = $connector_plugin
->create($this->container, [], 'foo', [
'foo',
]);
$config = $solr_connector
->defaultConfiguration();
$this
->assertEquals(ACQUIA_SEARCH_AUTO_OVERRIDE_READ_ONLY, $config['overridden_by_acquia_search']);
$this
->assertGetUpdateQueryException($solr_connector);
}
public function testAcquiaHostingProdEnvironmentDetectedWithoutProdFlag() {
$_ENV['AH_SITE_ENVIRONMENT'] = 'prod';
$_ENV['AH_SITE_NAME'] = 'testsite1prod';
$_ENV['AH_SITE_GROUP'] = 'testsite1';
$this
->setAvailableSearchCores();
$connector_plugin = new SearchApiSolrAcquiaConnector([], 'foo', [
'foo',
]);
$solr_connector = $connector_plugin
->create($this->container, [], 'foo', [
'foo',
]);
$config = $solr_connector
->defaultConfiguration();
$this
->assertEquals(ACQUIA_SEARCH_AUTO_OVERRIDE_READ_ONLY, $config['overridden_by_acquia_search']);
$this
->assertGetUpdateQueryException($solr_connector);
}
public function testAcquiaHostingProdEnvironmentDetectedWithProdFlag() {
$_ENV['AH_SITE_ENVIRONMENT'] = 'prod';
$_ENV['AH_SITE_NAME'] = 'testsite1prod';
$_ENV['AH_SITE_GROUP'] = 'testsite1';
$_SERVER['AH_PRODUCTION'] = TRUE;
$this
->setAvailableSearchCores();
$connector_plugin = new SearchApiSolrAcquiaConnector([], 'foo', [
'foo',
]);
$solr_connector = $connector_plugin
->create($this->container, [], 'foo', [
'foo',
]);
$config = $solr_connector
->defaultConfiguration();
$this
->assertEquals(ACQUIA_SEARCH_OVERRIDE_AUTO_SET, $config['overridden_by_acquia_search']);
$this
->assertEquals('WXYZ-12345', $config['index_id']);
$this
->assertGetUpdateQueryNoException($solr_connector);
}
public function testApacheSolrOverrideWhenCoreWithDbNameNotAvailable() {
$_ENV['AH_SITE_ENVIRONMENT'] = 'dev';
$_ENV['AH_SITE_NAME'] = 'testsite1dev';
$_ENV['AH_SITE_GROUP'] = 'testsite1';
$this
->setAvailableSearchCores(TRUE);
$connector_plugin = new SearchApiSolrAcquiaConnector([], 'foo', [
'foo',
]);
$solr_connector = $connector_plugin
->create($this->container, [], 'foo', [
'foo',
]);
$config = $solr_connector
->defaultConfiguration();
$site_folder = $this
->getSiteFolderName();
$this
->assertEquals(ACQUIA_SEARCH_OVERRIDE_AUTO_SET, $config['overridden_by_acquia_search']);
$this
->assertEquals('WXYZ-12345.dev.' . $site_folder, $config['index_id']);
$this
->assertGetUpdateQueryNoException($solr_connector);
}
protected function assertGetUpdateQueryException(SearchApiSolrAcquiaConnector $solr_connector) {
$this
->expectExceptionMessage('The Search API Server serving this index is currently in read-only mode.');
$solr_connector
->getUpdateQuery();
}
protected function assertGetUpdateQueryNoException(SearchApiSolrAcquiaConnector $solr_connector) {
try {
$solr_connector
->getUpdateQuery();
} catch (\Exception $e) {
$this
->fail('getUpdateQuery() should not throw exception');
}
}
protected function setAvailableSearchCores($no_db_flag = FALSE) {
$acquia_identifier = 'WXYZ-12345';
$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}",
];
$search_v3_core = [
'balancer' => 'sr-dev.acquia.com',
'core_id' => "{$acquia_identifier}.dev.{$ah_db_name}",
'version' => "v3",
];
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,
$search_v3_core,
];
}
$storage = new Storage();
$storage
->setIdentifier($acquia_identifier);
\Drupal::state()
->set('acquia_subscription_data', [
'heartbeat_data' => [
'search_cores' => $available_cores,
],
]);
}
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'];
}
}