CRM-17860 - CiviUnitTestCase - Extract body of populateDB to CiviTestDB
[civicrm-core.git] / tests / phpunit / CiviTest / CiviTestDB.php
CommitLineData
cdc44255
TO
1<?php
2
3class CiviTestDB {
4
5 /**
6 * @param string $dbName
7 * @param CiviTestPdoUtils $pdoUtils
8 * @return bool
9 */
10 public static function realPopulateDB($dbName, $pdoUtils) {
11 $pdo = $pdoUtils->pdo;
12 // only consider real tables and not views
13 $tables = $pdo->query("SELECT table_name FROM INFORMATION_SCHEMA.TABLES
14 WHERE TABLE_SCHEMA = '{$dbName}' AND TABLE_TYPE = 'BASE TABLE'");
15
16 $truncates = array();
17 $drops = array();
18 foreach ($tables as $table) {
19 // skip log tables
20 if (substr($table['table_name'], 0, 4) == 'log_') {
21 continue;
22 }
23
24 // don't change list of installed extensions
25 if ($table['table_name'] == 'civicrm_extension') {
26 continue;
27 }
28
29 if (substr($table['table_name'], 0, 14) == 'civicrm_value_') {
30 $drops[] = 'DROP TABLE ' . $table['table_name'] . ';';
31 }
32 else {
33 $truncates[] = 'TRUNCATE ' . $table['table_name'] . ';';
34 }
35 }
36
37 $queries = array(
38 "USE {$dbName};",
39 "SET foreign_key_checks = 0",
40 // SQL mode needs to be strict, that's our standard
41 "SET SQL_MODE='STRICT_ALL_TABLES';",
42 "SET global innodb_flush_log_at_trx_commit = 2;",
43 );
44 $queries = array_merge($queries, $truncates);
45 $queries = array_merge($queries, $drops);
46 foreach ($queries as $query) {
47 if ($pdoUtils->do_query($query) === FALSE) {
48 // failed to create test database
49 echo "failed to create test db.";
50 exit;
51 }
52 }
53
54 // initialize test database
55 $sql_file2 = dirname(dirname(dirname(dirname(__FILE__)))) . "/sql/civicrm_data.mysql";
56 $sql_file3 = dirname(dirname(dirname(dirname(__FILE__)))) . "/sql/test_data.mysql";
57 $sql_file4 = dirname(dirname(dirname(dirname(__FILE__)))) . "/sql/test_data_second_domain.mysql";
58
59 $query2 = file_get_contents($sql_file2);
60 $query3 = file_get_contents($sql_file3);
61 $query4 = file_get_contents($sql_file4);
62 if ($pdoUtils->do_query($query2) === FALSE) {
63 echo "Cannot load civicrm_data.mysql. Aborting.";
64 exit;
65 }
66 if ($pdoUtils->do_query($query3) === FALSE) {
67 echo "Cannot load test_data.mysql. Aborting.";
68 exit;
69 }
70 if ($pdoUtils->do_query($query4) === FALSE) {
71 echo "Cannot load test_data.mysql. Aborting.";
72 exit;
73 }
74
75 // done with all the loading, get transactions back
76 if ($pdoUtils->do_query("set global innodb_flush_log_at_trx_commit = 1;") === FALSE) {
77 echo "Cannot set global? Huh?";
78 exit;
79 }
80
81 if ($pdoUtils->do_query("SET foreign_key_checks = 1") === FALSE) {
82 echo "Cannot get foreign keys back? Huh?";
83 exit;
84 }
85
86 unset($query, $query2, $query3);
87
88 // Rebuild triggers
89 civicrm_api('system', 'flush', array('version' => 3, 'triggers' => 1));
90
91 CRM_Core_BAO_ConfigSetting::setEnabledComponents(array(
92 'CiviEvent',
93 'CiviContribute',
94 'CiviMember',
95 'CiviMail',
96 'CiviReport',
97 'CiviPledge',
98 ));
99
100 return TRUE;
101 }
102
103}