/usr/share/civicrm/Civi/Test.php is in civicrm-common 4.7.30+dfsg-1ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 | <?php
namespace Civi;
use PDO;
use PDOException;
/**
* Class Test
*
* A facade for managing the test environment.
*/
class Test {
/**
* @var array
*/
private static $singletons = array();
/**
* Get the data source used for testing.
*
* @param string|NULL $part
* One of NULL, 'hostspec', 'port', 'username', 'password', 'database'.
* @return string|array|NULL
* If $part is omitted, return full DSN array.
* If $part is a string, return that part of the DSN.
*/
public static function dsn($part = NULL) {
if (!isset(self::$singletons['dsn'])) {
require_once "DB.php";
self::$singletons['dsn'] = \DB::parseDSN(CIVICRM_DSN);
}
if ($part === NULL) {
return self::$singletons['dsn'];
}
if (isset(self::$singletons['dsn'][$part])) {
return self::$singletons['dsn'][$part];
}
return NULL;
}
/**
* Get a connection to the test database.
*
* @return PDO
*/
public static function pdo() {
if (!isset(self::$singletons['pdo'])) {
$dsninfo = self::dsn();
$host = $dsninfo['hostspec'];
$port = @$dsninfo['port'];
try {
self::$singletons['pdo'] = new PDO("mysql:host={$host}" . ($port ? ";port=$port" : ""),
$dsninfo['username'], $dsninfo['password'],
array(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => TRUE)
);
}
catch (PDOException $e) {
echo "Can't connect to MySQL server:" . PHP_EOL . $e->getMessage() . PHP_EOL;
exit(1);
}
}
return self::$singletons['pdo'];
}
/**
* Create a builder for the headless environment.
*
* @return \Civi\Test\CiviEnvBuilder
*
* @code
* \Civi\Test::headless()->apply();
* \Civi\Test::headless()->sqlFile('ex.sql')->apply();
* @endCode
*/
public static function headless() {
$civiRoot = dirname(__DIR__);
$builder = new \Civi\Test\CiviEnvBuilder('CiviEnvBuilder');
$builder
->callback(function ($ctx) {
if (CIVICRM_UF !== 'UnitTests') {
throw new \RuntimeException("\\Civi\\Test::headless() requires CIVICRM_UF=UnitTests");
}
$dbName = \Civi\Test::dsn('database');
echo "Installing {$dbName} schema\n";
\Civi\Test::schema()->dropAll();
}, 'headless-drop')
->sqlFile($civiRoot . "/sql/civicrm.mysql")
->sql("DELETE FROM civicrm_extension")
->callback(function ($ctx) {
\Civi\Test::data()->populate();
}, 'populate');
return $builder;
}
/**
* Create a builder for end-to-end testing on the live environment.
*
* @return \Civi\Test\CiviEnvBuilder
*
* @code
* \Civi\Test::e2e()->apply();
* \Civi\Test::e2e()->install('foo.bar')->apply();
* @endCode
*/
public static function e2e() {
$builder = new \Civi\Test\CiviEnvBuilder('CiviEnvBuilder');
$builder
->callback(function ($ctx) {
if (CIVICRM_UF === 'UnitTests') {
throw new \RuntimeException("\\Civi\\Test::e2e() requires a real CMS. Found CIVICRM_UF=UnitTests.");
}
}, 'e2e-check');
return $builder;
}
/**
* @return \Civi\Test\Schema
*/
public static function schema() {
if (!isset(self::$singletons['schema'])) {
self::$singletons['schema'] = new \Civi\Test\Schema();
}
return self::$singletons['schema'];
}
/**
* @return \Civi\Test\Data
*/
public static function data() {
if (!isset(self::$singletons['data'])) {
self::$singletons['data'] = new \Civi\Test\Data('CiviTesterData');
}
return self::$singletons['data'];
}
/**
* Prepare and execute a batch of SQL statements.
*
* @param string $query
* @return bool
*/
public static function execute($query) {
$pdo = \Civi\Test::pdo();
$string = preg_replace("/^#[^\n]*$/m", "\n", $query);
$string = preg_replace("/^(--[^-]).*/m", "\n", $string);
$queries = preg_split('/;\s*$/m', $string);
foreach ($queries as $query) {
$query = trim($query);
if (!empty($query)) {
$result = $pdo->query($query);
if ($pdo->errorCode() == 0) {
continue;
}
else {
var_dump($result);
var_dump($pdo->errorInfo());
// die( "Cannot execute $query: " . $pdo->errorInfo() );
}
}
}
return TRUE;
}
}
|