// do all wildcard translations first
- // FIXME: Is there a constant we can reference instead of hardcoding en_US?
- $replacementsLocale = $this->locale ? $this->locale : 'en_US';
- if (!isset(Civi::$statics[__CLASS__]) || !array_key_exists($replacementsLocale, Civi::$statics[__CLASS__])) {
- if (defined('CIVICRM_DSN') && !CRM_Core_Config::isUpgradeMode()) {
- Civi::$statics[__CLASS__][$replacementsLocale] = CRM_Core_BAO_WordReplacement::getLocaleCustomStrings($replacementsLocale);
- }
- else {
- Civi::$statics[__CLASS__][$replacementsLocale] = [];
- }
- }
- $stringTable = Civi::$statics[__CLASS__][$replacementsLocale];
+ $stringTable = $this->getWordReplacements();
$exactMatch = FALSE;
if (isset($stringTable['enabled']['exactMatch'])) {
return $tsLocale ? $tsLocale : 'en_US';
}
+ /**
+ * @return array
+ * Ex: $stringTable['enabled']['wildcardMatch']['foo'] = 'bar';
+ */
+ private function getWordReplacements() {
+ if (isset(Civi::$statics['testPreInstall'])) {
+ return [];
+ }
+
+ // FIXME: Is there a constant we can reference instead of hardcoding en_US?
+ $replacementsLocale = $this->locale ? $this->locale : 'en_US';
+ if ((!isset(Civi::$statics[__CLASS__]) || !array_key_exists($replacementsLocale, Civi::$statics[__CLASS__]))) {
+ if (defined('CIVICRM_DSN') && !CRM_Core_Config::isUpgradeMode()) {
+ Civi::$statics[__CLASS__][$replacementsLocale] = CRM_Core_BAO_WordReplacement::getLocaleCustomStrings($replacementsLocale);
+ }
+ else {
+ Civi::$statics[__CLASS__][$replacementsLocale] = [];
+ }
+ }
+ return Civi::$statics[__CLASS__][$replacementsLocale];
+ }
+
}
/**
*/
private static $singletons = [];
+ /**
+ * @var array
+ */
+ public static $statics = [];
+
+ /**
+ * Run code in a pre-boot fashion.
+ *
+ * @param callable $callback
+ * @return mixed
+ * Pass through the result of the callback.
+ */
+ public static function asPreInstall($callback) {
+ $conn = \Civi\Test::pdo();
+
+ $oldEscaper = \CRM_Core_I18n::$SQL_ESCAPER;
+ \Civi::$statics['testPreInstall'] = (\Civi::$statics['testPreInstall'] ?? 0) + 1;
+ try {
+ \CRM_Core_I18n::$SQL_ESCAPER = function ($text) use ($conn) {
+ return substr($conn->quote($text), 1, -1);
+ };
+ return $callback();
+ } finally {
+ \CRM_Core_I18n::$SQL_ESCAPER = $oldEscaper;
+ \Civi::$statics['testPreInstall']--;
+ if (\Civi::$statics['testPreInstall'] <= 0) {
+ unset(\Civi::$statics['testPreInstall']);
+ }
+ }
+ }
+
/**
* Get the data source used for testing.
*
* @return CiviEnvBuilder
*/
public function apply($force = FALSE) {
- $dbName = \Civi\Test::dsn('database');
- $query = "USE {$dbName};"
- . "CREATE TABLE IF NOT EXISTS civitest_revs (name VARCHAR(64) PRIMARY KEY, rev VARCHAR(64));";
+ return \Civi\Test::asPreInstall(function() use ($force) {
+ $dbName = \Civi\Test::dsn('database');
+ $query = "USE {$dbName};"
+ . "CREATE TABLE IF NOT EXISTS civitest_revs (name VARCHAR(64) PRIMARY KEY, rev VARCHAR(64));";
- if (\Civi\Test::execute($query) === FALSE) {
- throw new \RuntimeException("Failed to flag schema version: $query");
- }
+ if (\Civi\Test::execute($query) === FALSE) {
+ throw new \RuntimeException("Failed to flag schema version: $query");
+ }
- $this->assertValid();
+ $this->assertValid();
- if (!$force && $this->getSavedSignature() === $this->getTargetSignature()) {
+ if (!$force && $this->getSavedSignature() === $this->getTargetSignature()) {
+ return $this;
+ }
+ foreach ($this->steps as $step) {
+ $step->run($this);
+ }
+ $this->setSavedSignature($this->getTargetSignature());
return $this;
- }
- foreach ($this->steps as $step) {
- $step->run($this);
- }
- $this->setSavedSignature($this->getTargetSignature());
- return $this;
+ });
}
/**
* @return bool
*/
public function populate() {
- \Civi\Test::schema()->truncateAll();
-
- \Civi\Test::schema()->setStrict(FALSE);
-
- // Ensure that when we populate the database it is done in utf8 mode
- \Civi\Test::execute('SET NAMES utf8');
- $sqlDir = dirname(dirname(__DIR__)) . "/sql";
-
- $query2 = file_get_contents("$sqlDir/civicrm_data.mysql");
- $query3 = file_get_contents("$sqlDir/test_data.mysql");
- $query4 = file_get_contents("$sqlDir/test_data_second_domain.mysql");
- if (\Civi\Test::execute($query2) === FALSE) {
- throw new RuntimeException("Cannot load civicrm_data.mysql. Aborting.");
- }
- if (\Civi\Test::execute($query3) === FALSE) {
- throw new RuntimeException("Cannot load test_data.mysql. Aborting.");
- }
- if (\Civi\Test::execute($query4) === FALSE) {
- throw new RuntimeException("Cannot load test_data.mysql. Aborting.");
- }
-
- unset($query, $query2, $query3);
-
- \Civi\Test::schema()->setStrict(TRUE);
+ \Civi\Test::asPreInstall(function() {
+ \Civi\Test::schema()->truncateAll();
+
+ \Civi\Test::schema()->setStrict(FALSE);
+
+ // Ensure that when we populate the database it is done in utf8 mode
+ \Civi\Test::execute('SET NAMES utf8');
+ $sqlDir = dirname(dirname(__DIR__)) . "/sql";
+
+ $query2 = file_get_contents("$sqlDir/civicrm_data.mysql");
+ $query3 = file_get_contents("$sqlDir/test_data.mysql");
+ $query4 = file_get_contents("$sqlDir/test_data_second_domain.mysql");
+ if (\Civi\Test::execute($query2) === FALSE) {
+ throw new RuntimeException("Cannot load civicrm_data.mysql. Aborting.");
+ }
+ if (\Civi\Test::execute($query3) === FALSE) {
+ throw new RuntimeException("Cannot load test_data.mysql. Aborting.");
+ }
+ if (\Civi\Test::execute($query4) === FALSE) {
+ throw new RuntimeException("Cannot load test_data.mysql. Aborting.");
+ }
+
+ unset($query, $query2, $query3);
+
+ \Civi\Test::schema()->setStrict(TRUE);
+ });
// Rebuild triggers
civicrm_api('system', 'flush', ['version' => 3, 'triggers' => 1]);