MDL-32960 refactor phpunit integration to one class per file structure
[moodle.git] / lib / phpunit / classes / util.php
CommitLineData
7e7cfe7a
PS
1<?php
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * Utility class.
19 *
20 * @package core
21 * @category phpunit
22 * @copyright 2012 Petr Skoda {@link http://skodak.org}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26
27/**
28 * Collection of utility methods.
29 *
30 * @package core
31 * @category phpunit
32 * @copyright 2012 Petr Skoda {@link http://skodak.org}
33 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34 */
35class phpunit_util {
36 /** @var string current version hash from php files */
37 protected static $versionhash = null;
38
39 /** @var array original content of all database tables*/
40 protected static $tabledata = null;
41
42 /** @var array original structure of all database tables */
43 protected static $tablestructure = null;
44
45 /** @var array original structure of all database tables */
46 protected static $sequencenames = null;
47
48 /** @var array An array of original globals, restored after each test */
49 protected static $globals = array();
50
51 /** @var int last value of db writes counter, used for db resetting */
52 public static $lastdbwrites = null;
53
54 /** @var phpunit_data_generator */
55 protected static $generator = null;
56
57 /** @var resource used for prevention of parallel test execution */
58 protected static $lockhandle = null;
59
60 /**
61 * Prevent parallel test execution - this can not work in Moodle because we modify database and dataroot.
62 *
63 * Note: do not call manually!
64 *
65 * @internal
66 * @static
67 * @return void
68 */
69 public static function acquire_test_lock() {
70 global $CFG;
71 if (!file_exists("$CFG->phpunit_dataroot/phpunit")) {
72 // dataroot not initialised yet
73 return;
74 }
75 if (!file_exists("$CFG->phpunit_dataroot/phpunit/lock")) {
76 file_put_contents("$CFG->phpunit_dataroot/phpunit/lock", 'This file prevents concurrent execution of Moodle PHPUnit tests');
77 phpunit_boostrap_fix_file_permissions("$CFG->phpunit_dataroot/phpunit/lock");
78 }
79 if (self::$lockhandle = fopen("$CFG->phpunit_dataroot/phpunit/lock", 'r')) {
80 $wouldblock = null;
81 $locked = flock(self::$lockhandle, (LOCK_EX | LOCK_NB), $wouldblock);
82 if (!$locked) {
83 if ($wouldblock) {
84 echo "Waiting for other test execution to complete...\n";
85 }
86 $locked = flock(self::$lockhandle, LOCK_EX);
87 }
88 if (!$locked) {
89 fclose(self::$lockhandle);
90 self::$lockhandle = null;
91 }
92 }
93 register_shutdown_function(array('phpunit_util', 'release_test_lock'));
94 }
95
96 /**
97 * Note: do not call manually!
98 * @internal
99 * @static
100 * @return void
101 */
102 public static function release_test_lock() {
103 if (self::$lockhandle) {
104 flock(self::$lockhandle, LOCK_UN);
105 fclose(self::$lockhandle);
106 self::$lockhandle = null;
107 }
108 }
109
110 /**
111 * Load global $CFG;
112 * @internal
113 * @static
114 * @return void
115 */
116 public static function initialise_cfg() {
117 global $DB;
118 $dbhash = false;
119 try {
120 $dbhash = $DB->get_field('config', 'value', array('name'=>'phpunittest'));
121 } catch (Exception $e) {
122 // not installed yet
123 initialise_cfg();
124 return;
125 }
126 if ($dbhash !== phpunit_util::get_version_hash()) {
127 // do not set CFG - the only way forward is to drop and reinstall
128 return;
129 }
130 // standard CFG init
131 initialise_cfg();
132 }
133
134 /**
135 * Get data generator
136 * @static
137 * @return phpunit_data_generator
138 */
139 public static function get_data_generator() {
140 if (is_null(self::$generator)) {
141 require_once(__DIR__.'/../generatorlib.php');
142 self::$generator = new phpunit_data_generator();
143 }
144 return self::$generator;
145 }
146
147 /**
148 * Returns contents of all tables right after installation.
149 * @static
150 * @return array $table=>$records
151 */
152 protected static function get_tabledata() {
153 global $CFG;
154
155 if (!file_exists("$CFG->dataroot/phpunit/tabledata.ser")) {
156 // not initialised yet
157 return array();
158 }
159
160 if (!isset(self::$tabledata)) {
161 $data = file_get_contents("$CFG->dataroot/phpunit/tabledata.ser");
162 self::$tabledata = unserialize($data);
163 }
164
165 if (!is_array(self::$tabledata)) {
166 phpunit_bootstrap_error(1, 'Can not read dataroot/phpunit/tabledata.ser or invalid format, reinitialize test database.');
167 }
168
169 return self::$tabledata;
170 }
171
172 /**
173 * Returns structure of all tables right after installation.
174 * @static
175 * @return array $table=>$records
176 */
177 public static function get_tablestructure() {
178 global $CFG;
179
180 if (!file_exists("$CFG->dataroot/phpunit/tablestructure.ser")) {
181 // not initialised yet
182 return array();
183 }
184
185 if (!isset(self::$tablestructure)) {
186 $data = file_get_contents("$CFG->dataroot/phpunit/tablestructure.ser");
187 self::$tablestructure = unserialize($data);
188 }
189
190 if (!is_array(self::$tablestructure)) {
191 phpunit_bootstrap_error(1, 'Can not read dataroot/phpunit/tablestructure.ser or invalid format, reinitialize test database.');
192 }
193
194 return self::$tablestructure;
195 }
196
197 /**
198 * Returns the names of sequences for each autoincrementing id field in all standard tables.
199 * @static
200 * @return array $table=>$sequencename
201 */
202 public static function get_sequencenames() {
203 global $DB;
204
205 if (isset(self::$sequencenames)) {
206 return self::$sequencenames;
207 }
208
209 if (!$structure = self::get_tablestructure()) {
210 return array();
211 }
212
213 self::$sequencenames = array();
214 foreach ($structure as $table=>$ignored) {
215 $name = $DB->get_manager()->generator->getSequenceFromDB(new xmldb_table($table));
216 if ($name !== false) {
217 self::$sequencenames[$table] = $name;
218 }
219 }
220
221 return self::$sequencenames;
222 }
223
224 /**
225 * Returns list of tables that are unmodified and empty.
226 *
227 * @static
228 * @return array of table names, empty if unknown
229 */
230 protected static function guess_unmodified_empty_tables() {
231 global $DB;
232
233 $dbfamily = $DB->get_dbfamily();
234
235 if ($dbfamily === 'mysql') {
236 $empties = array();
237 $prefix = $DB->get_prefix();
238 $rs = $DB->get_recordset_sql("SHOW TABLE STATUS LIKE ?", array($prefix.'%'));
239 foreach ($rs as $info) {
240 $table = strtolower($info->name);
241 if (strpos($table, $prefix) !== 0) {
242 // incorrect table match caused by _
243 continue;
244 }
245 if (!is_null($info->auto_increment)) {
246 $table = preg_replace('/^'.preg_quote($prefix, '/').'/', '', $table);
247 if ($info->auto_increment == 1) {
248 $empties[$table] = $table;
249 }
250 }
251 }
252 $rs->close();
253 return $empties;
254
255 } else if ($dbfamily === 'mssql') {
256 $empties = array();
257 $prefix = $DB->get_prefix();
258 $sql = "SELECT t.name
259 FROM sys.identity_columns i
260 JOIN sys.tables t ON t.object_id = i.object_id
261 WHERE t.name LIKE ?
262 AND i.name = 'id'
263 AND i.last_value IS NULL";
264 $rs = $DB->get_recordset_sql($sql, array($prefix.'%'));
265 foreach ($rs as $info) {
266 $table = strtolower($info->name);
267 if (strpos($table, $prefix) !== 0) {
268 // incorrect table match caused by _
269 continue;
270 }
271 $table = preg_replace('/^'.preg_quote($prefix, '/').'/', '', $table);
272 $empties[$table] = $table;
273 }
274 $rs->close();
275 return $empties;
276
277 } else if ($dbfamily === 'oracle') {
278 $sequences = phpunit_util::get_sequencenames();
279 $sequences = array_map('strtoupper', $sequences);
280 $lookup = array_flip($sequences);
281 $empties = array();
282 list($seqs, $params) = $DB->get_in_or_equal($sequences);
283 $sql = "SELECT sequence_name FROM user_sequences WHERE last_number = 1 AND sequence_name $seqs";
284 $rs = $DB->get_recordset_sql($sql, $params);
285 foreach ($rs as $seq) {
286 $table = $lookup[$seq->sequence_name];
287 $empties[$table] = $table;
288 }
289 $rs->close();
290 return $empties;
291
292 } else {
293 return array();
294 }
295 }
296
297 /**
298 * Reset all database sequences to initial values.
299 *
300 * @static
301 * @param array $empties tables that are known to be unmodified and empty
302 * @return void
303 */
304 public static function reset_all_database_sequences(array $empties = null) {
305 global $DB;
306
307 if (!$data = self::get_tabledata()) {
308 // not initialised yet
309 return;
310 }
311 if (!$structure = self::get_tablestructure()) {
312 // not initialised yet
313 return;
314 }
315
316 $dbfamily = $DB->get_dbfamily();
317 if ($dbfamily === 'postgres') {
318 $queries = array();
319 $prefix = $DB->get_prefix();
320 foreach ($data as $table=>$records) {
321 if (isset($structure[$table]['id']) and $structure[$table]['id']->auto_increment) {
322 if (empty($records)) {
323 $nextid = 1;
324 } else {
325 $lastrecord = end($records);
326 $nextid = $lastrecord->id + 1;
327 }
328 $queries[] = "ALTER SEQUENCE {$prefix}{$table}_id_seq RESTART WITH $nextid";
329 }
330 }
331 if ($queries) {
332 $DB->change_database_structure(implode(';', $queries));
333 }
334
335 } else if ($dbfamily === 'mysql') {
336 $sequences = array();
337 $prefix = $DB->get_prefix();
338 $rs = $DB->get_recordset_sql("SHOW TABLE STATUS LIKE ?", array($prefix.'%'));
339 foreach ($rs as $info) {
340 $table = strtolower($info->name);
341 if (strpos($table, $prefix) !== 0) {
342 // incorrect table match caused by _
343 continue;
344 }
345 if (!is_null($info->auto_increment)) {
346 $table = preg_replace('/^'.preg_quote($prefix, '/').'/', '', $table);
347 $sequences[$table] = $info->auto_increment;
348 }
349 }
350 $rs->close();
351 $prefix = $DB->get_prefix();
352 foreach ($data as $table=>$records) {
353 if (isset($structure[$table]['id']) and $structure[$table]['id']->auto_increment) {
354 if (isset($sequences[$table])) {
355 if (empty($records)) {
356 $nextid = 1;
357 } else {
358 $lastrecord = end($records);
359 $nextid = $lastrecord->id + 1;
360 }
361 if ($sequences[$table] != $nextid) {
362 $DB->change_database_structure("ALTER TABLE {$prefix}{$table} AUTO_INCREMENT = $nextid");
363 }
364
365 } else {
366 // some problem exists, fallback to standard code
367 $DB->get_manager()->reset_sequence($table);
368 }
369 }
370 }
371
372 } else if ($dbfamily === 'oracle') {
373 $sequences = phpunit_util::get_sequencenames();
374 $sequences = array_map('strtoupper', $sequences);
375 $lookup = array_flip($sequences);
376
377 $current = array();
378 list($seqs, $params) = $DB->get_in_or_equal($sequences);
379 $sql = "SELECT sequence_name, last_number FROM user_sequences WHERE sequence_name $seqs";
380 $rs = $DB->get_recordset_sql($sql, $params);
381 foreach ($rs as $seq) {
382 $table = $lookup[$seq->sequence_name];
383 $current[$table] = $seq->last_number;
384 }
385 $rs->close();
386
387 foreach ($data as $table=>$records) {
388 if (isset($structure[$table]['id']) and $structure[$table]['id']->auto_increment) {
389 $lastrecord = end($records);
390 if ($lastrecord) {
391 $nextid = $lastrecord->id + 1;
392 } else {
393 $nextid = 1;
394 }
395 if (!isset($current[$table])) {
396 $DB->get_manager()->reset_sequence($table);
397 } else if ($nextid == $current[$table]) {
398 continue;
399 }
400 // reset as fast as possible - alternatively we could use http://stackoverflow.com/questions/51470/how-do-i-reset-a-sequence-in-oracle
401 $seqname = $sequences[$table];
402 $cachesize = $DB->get_manager()->generator->sequence_cache_size;
403 $DB->change_database_structure("DROP SEQUENCE $seqname");
404 $DB->change_database_structure("CREATE SEQUENCE $seqname START WITH $nextid INCREMENT BY 1 NOMAXVALUE CACHE $cachesize");
405 }
406 }
407
408 } else {
409 // note: does mssql support any kind of faster reset?
410 if (is_null($empties)) {
411 $empties = self::guess_unmodified_empty_tables();
412 }
413 foreach ($data as $table=>$records) {
414 if (isset($empties[$table])) {
415 continue;
416 }
417 if (isset($structure[$table]['id']) and $structure[$table]['id']->auto_increment) {
418 $DB->get_manager()->reset_sequence($table);
419 }
420 }
421 }
422 }
423
424 /**
425 * Reset all database tables to default values.
426 * @static
427 * @return bool true if reset done, false if skipped
428 */
429 public static function reset_database() {
430 global $DB;
431
432 if (!is_null(self::$lastdbwrites) and self::$lastdbwrites == $DB->perf_get_writes()) {
433 return false;
434 }
435
436 $tables = $DB->get_tables(false);
437 if (!$tables or empty($tables['config'])) {
438 // not installed yet
439 return false;
440 }
441
442 if (!$data = self::get_tabledata()) {
443 // not initialised yet
444 return false;
445 }
446 if (!$structure = self::get_tablestructure()) {
447 // not initialised yet
448 return false;
449 }
450
451 $empties = self::guess_unmodified_empty_tables();
452
453 foreach ($data as $table=>$records) {
454 if (empty($records)) {
455 if (isset($empties[$table])) {
456 // table was not modified and is empty
457 } else {
458 $DB->delete_records($table, array());
459 }
460 continue;
461 }
462
463 if (isset($structure[$table]['id']) and $structure[$table]['id']->auto_increment) {
464 $currentrecords = $DB->get_records($table, array(), 'id ASC');
465 $changed = false;
466 foreach ($records as $id=>$record) {
467 if (!isset($currentrecords[$id])) {
468 $changed = true;
469 break;
470 }
471 if ((array)$record != (array)$currentrecords[$id]) {
472 $changed = true;
473 break;
474 }
475 unset($currentrecords[$id]);
476 }
477 if (!$changed) {
478 if ($currentrecords) {
479 $lastrecord = end($records);
480 $DB->delete_records_select($table, "id > ?", array($lastrecord->id));
481 continue;
482 } else {
483 continue;
484 }
485 }
486 }
487
488 $DB->delete_records($table, array());
489 foreach ($records as $record) {
490 $DB->import_record($table, $record, false, true);
491 }
492 }
493
494 // reset all next record ids - aka sequences
495 self::reset_all_database_sequences($empties);
496
497 // remove extra tables
498 foreach ($tables as $table) {
499 if (!isset($data[$table])) {
500 $DB->get_manager()->drop_table(new xmldb_table($table));
501 }
502 }
503
504 self::$lastdbwrites = $DB->perf_get_writes();
505
506 return true;
507 }
508
509 /**
510 * Purge dataroot directory
511 * @static
512 * @return void
513 */
514 public static function reset_dataroot() {
515 global $CFG;
516
517 $handle = opendir($CFG->dataroot);
518 $skip = array('.', '..', 'phpunittestdir.txt', 'phpunit', '.htaccess');
519 while (false !== ($item = readdir($handle))) {
520 if (in_array($item, $skip)) {
521 continue;
522 }
523 if (is_dir("$CFG->dataroot/$item")) {
524 remove_dir("$CFG->dataroot/$item", false);
525 } else {
526 unlink("$CFG->dataroot/$item");
527 }
528 }
529 closedir($handle);
530 make_temp_directory('');
531 make_cache_directory('');
532 make_cache_directory('htmlpurifier');
533 }
534
535 /**
536 * Reset contents of all database tables to initial values, reset caches, etc.
537 *
538 * Note: this is relatively slow (cca 2 seconds for pg and 7 for mysql) - please use with care!
539 *
540 * @static
541 * @param bool $logchanges log changes in global state and database in error log
542 * @return void
543 */
544 public static function reset_all_data($logchanges = false) {
545 global $DB, $CFG, $USER, $SITE, $COURSE, $PAGE, $OUTPUT, $SESSION;
546
547 // reset global $DB in case somebody mocked it
548 $DB = self::get_global_backup('DB');
549
550 if ($DB->is_transaction_started()) {
551 // we can not reset inside transaction
552 $DB->force_transaction_rollback();
553 }
554
555 $resetdb = self::reset_database();
556 $warnings = array();
557
558 if ($logchanges) {
559 if ($resetdb) {
560 $warnings[] = 'Warning: unexpected database modification, resetting DB state';
561 }
562
563 $oldcfg = self::get_global_backup('CFG');
564 $oldsite = self::get_global_backup('SITE');
565 foreach($CFG as $k=>$v) {
566 if (!property_exists($oldcfg, $k)) {
567 $warnings[] = 'Warning: unexpected new $CFG->'.$k.' value';
568 } else if ($oldcfg->$k !== $CFG->$k) {
569 $warnings[] = 'Warning: unexpected change of $CFG->'.$k.' value';
570 }
571 unset($oldcfg->$k);
572
573 }
574 if ($oldcfg) {
575 foreach($oldcfg as $k=>$v) {
576 $warnings[] = 'Warning: unexpected removal of $CFG->'.$k;
577 }
578 }
579
580 if ($USER->id != 0) {
581 $warnings[] = 'Warning: unexpected change of $USER';
582 }
583
584 if ($COURSE->id != $oldsite->id) {
585 $warnings[] = 'Warning: unexpected change of $COURSE';
586 }
587 }
588
589 // restore original globals
590 $_SERVER = self::get_global_backup('_SERVER');
591 $CFG = self::get_global_backup('CFG');
592 $SITE = self::get_global_backup('SITE');
593 $COURSE = $SITE;
594
595 // reinitialise following globals
596 $OUTPUT = new bootstrap_renderer();
597 $PAGE = new moodle_page();
598 $FULLME = null;
599 $ME = null;
600 $SCRIPT = null;
601 $SESSION = new stdClass();
602 $_SESSION['SESSION'] =& $SESSION;
603
604 // set fresh new not-logged-in user
605 $user = new stdClass();
606 $user->id = 0;
607 $user->mnethostid = $CFG->mnet_localhost_id;
608 session_set_user($user);
609
610 // reset all static caches
611 accesslib_clear_all_caches(true);
612 get_string_manager()->reset_caches();
613 events_get_handlers('reset');
614 textlib::reset_caches();
615 //TODO: add more resets here and probably refactor them to new core function
616
617 // purge dataroot directory
618 self::reset_dataroot();
619
620 // restore original config once more in case resetting of caches changed CFG
621 $CFG = self::get_global_backup('CFG');
622
623 // inform data generator
624 self::get_data_generator()->reset();
625
626 // fix PHP settings
627 error_reporting($CFG->debug);
628
629 // verify db writes just in case something goes wrong in reset
630 if (self::$lastdbwrites != $DB->perf_get_writes()) {
631 error_log('Unexpected DB writes in phpunit_util::reset_all_data()');
632 self::$lastdbwrites = $DB->perf_get_writes();
633 }
634
635 if ($warnings) {
636 $warnings = implode("\n", $warnings);
637 trigger_error($warnings, E_USER_WARNING);
638 }
639 }
640
641 /**
642 * Called during bootstrap only!
643 * @internal
644 * @static
645 * @return void
646 */
647 public static function bootstrap_init() {
648 global $CFG, $SITE, $DB;
649
650 // backup the globals
651 self::$globals['_SERVER'] = $_SERVER;
652 self::$globals['CFG'] = clone($CFG);
653 self::$globals['SITE'] = clone($SITE);
654 self::$globals['DB'] = $DB;
655
656 // refresh data in all tables, clear caches, etc.
657 phpunit_util::reset_all_data();
658 }
659
660 /**
661 * Returns original state of global variable.
662 * @static
663 * @param string $name
664 * @return mixed
665 */
666 public static function get_global_backup($name) {
667 if ($name === 'DB') {
668 // no cloning of database object,
669 // we just need the original reference, not original state
670 return self::$globals['DB'];
671 }
672 if (isset(self::$globals[$name])) {
673 if (is_object(self::$globals[$name])) {
674 $return = clone(self::$globals[$name]);
675 return $return;
676 } else {
677 return self::$globals[$name];
678 }
679 }
680 return null;
681 }
682
683 /**
684 * Does this site (db and dataroot) appear to be used for production?
685 * We try very hard to prevent accidental damage done to production servers!!
686 *
687 * @static
688 * @return bool
689 */
690 public static function is_test_site() {
691 global $DB, $CFG;
692
693 if (!file_exists("$CFG->dataroot/phpunittestdir.txt")) {
694 // this is already tested in bootstrap script,
695 // but anyway presence of this file means the dataroot is for testing
696 return false;
697 }
698
699 $tables = $DB->get_tables(false);
700 if ($tables) {
701 if (!$DB->get_manager()->table_exists('config')) {
702 return false;
703 }
704 if (!get_config('core', 'phpunittest')) {
705 return false;
706 }
707 }
708
709 return true;
710 }
711
712 /**
713 * Is this site initialised to run unit tests?
714 *
715 * @static
716 * @return int array errorcode=>message, 0 means ok
717 */
718 public static function testing_ready_problem() {
719 global $CFG, $DB;
720
721 $tables = $DB->get_tables(false);
722
723 if (!self::is_test_site()) {
724 // dataroot was verified in bootstrap, so it must be DB
725 return array(PHPUNIT_EXITCODE_CONFIGERROR, 'Can not use database for testing, try different prefix');
726 }
727
728 if (empty($tables)) {
729 return array(PHPUNIT_EXITCODE_INSTALL, '');
730 }
731
732 if (!file_exists("$CFG->dataroot/phpunit/tabledata.ser") or !file_exists("$CFG->dataroot/phpunit/tablestructure.ser")) {
733 return array(PHPUNIT_EXITCODE_REINSTALL, '');
734 }
735
736 if (!file_exists("$CFG->dataroot/phpunit/versionshash.txt")) {
737 return array(PHPUNIT_EXITCODE_REINSTALL, '');
738 }
739
740 $hash = phpunit_util::get_version_hash();
741 $oldhash = file_get_contents("$CFG->dataroot/phpunit/versionshash.txt");
742
743 if ($hash !== $oldhash) {
744 return array(PHPUNIT_EXITCODE_REINSTALL, '');
745 }
746
747 $dbhash = get_config('core', 'phpunittest');
748 if ($hash !== $dbhash) {
749 return array(PHPUNIT_EXITCODE_REINSTALL, '');
750 }
751
752 return array(0, '');
753 }
754
755 /**
756 * Drop all test site data.
757 *
758 * Note: To be used from CLI scripts only.
759 *
760 * @static
761 * @return void may terminate execution with exit code
762 */
763 public static function drop_site() {
764 global $DB, $CFG;
765
766 if (!self::is_test_site()) {
767 phpunit_bootstrap_error(PHPUNIT_EXITCODE_CONFIGERROR, 'Can not drop non-test site!!');
768 }
769
770 // purge dataroot
771 self::reset_dataroot();
772 phpunit_bootstrap_initdataroot($CFG->dataroot);
773 $keep = array('.', '..', 'lock', 'webrunner.xml');
774 $files = scandir("$CFG->dataroot/phpunit");
775 foreach ($files as $file) {
776 if (in_array($file, $keep)) {
777 continue;
778 }
779 $path = "$CFG->dataroot/phpunit/$file";
780 if (is_dir($path)) {
781 remove_dir($path, false);
782 } else {
783 unlink($path);
784 }
785 }
786
787 // drop all tables
788 $tables = $DB->get_tables(false);
789 if (isset($tables['config'])) {
790 // config always last to prevent problems with interrupted drops!
791 unset($tables['config']);
792 $tables['config'] = 'config';
793 }
794 foreach ($tables as $tablename) {
795 $table = new xmldb_table($tablename);
796 $DB->get_manager()->drop_table($table);
797 }
798 }
799
800 /**
801 * Perform a fresh test site installation
802 *
803 * Note: To be used from CLI scripts only.
804 *
805 * @static
806 * @return void may terminate execution with exit code
807 */
808 public static function install_site() {
809 global $DB, $CFG;
810
811 if (!self::is_test_site()) {
812 phpunit_bootstrap_error(PHPUNIT_EXITCODE_CONFIGERROR, 'Can not install on non-test site!!');
813 }
814
815 if ($DB->get_tables()) {
816 list($errorcode, $message) = phpunit_util::testing_ready_problem();
817 if ($errorcode) {
818 phpunit_bootstrap_error(PHPUNIT_EXITCODE_REINSTALL, 'Database tables already present, Moodle PHPUnit test environment can not be initialised');
819 } else {
820 phpunit_bootstrap_error(0, 'Moodle PHPUnit test environment is already initialised');
821 }
822 }
823
824 $options = array();
825 $options['adminpass'] = 'admin';
826 $options['shortname'] = 'phpunit';
827 $options['fullname'] = 'PHPUnit test site';
828
829 install_cli_database($options, false);
830
831 // install timezone info
832 $timezones = get_records_csv($CFG->libdir.'/timezone.txt', 'timezone');
833 update_timezone_records($timezones);
834
835 // add test db flag
836 $hash = phpunit_util::get_version_hash();
837 set_config('phpunittest', $hash);
838
839 // store data for all tables
840 $data = array();
841 $structure = array();
842 $tables = $DB->get_tables();
843 foreach ($tables as $table) {
844 $columns = $DB->get_columns($table);
845 $structure[$table] = $columns;
846 if (isset($columns['id']) and $columns['id']->auto_increment) {
847 $data[$table] = $DB->get_records($table, array(), 'id ASC');
848 } else {
849 // there should not be many of these
850 $data[$table] = $DB->get_records($table, array());
851 }
852 }
853 $data = serialize($data);
854 file_put_contents("$CFG->dataroot/phpunit/tabledata.ser", $data);
855 phpunit_boostrap_fix_file_permissions("$CFG->dataroot/phpunit/tabledata.ser");
856
857 $structure = serialize($structure);
858 file_put_contents("$CFG->dataroot/phpunit/tablestructure.ser", $structure);
859 phpunit_boostrap_fix_file_permissions("$CFG->dataroot/phpunit/tablestructure.ser");
860
861 // hash all plugin versions - helps with very fast detection of db structure changes
862 file_put_contents("$CFG->dataroot/phpunit/versionshash.txt", $hash);
863 phpunit_boostrap_fix_file_permissions("$CFG->dataroot/phpunit/versionshash.txt", $hash);
864 }
865
866 /**
867 * Calculate unique version hash for all plugins and core.
868 * @static
869 * @return string sha1 hash
870 */
871 public static function get_version_hash() {
872 global $CFG;
873
874 if (self::$versionhash) {
875 return self::$versionhash;
876 }
877
878 $versions = array();
879
880 // main version first
881 $version = null;
882 include($CFG->dirroot.'/version.php');
883 $versions['core'] = $version;
884
885 // modules
886 $mods = get_plugin_list('mod');
887 ksort($mods);
888 foreach ($mods as $mod => $fullmod) {
889 $module = new stdClass();
890 $module->version = null;
891 include($fullmod.'/version.php');
892 $versions[$mod] = $module->version;
893 }
894
895 // now the rest of plugins
896 $plugintypes = get_plugin_types();
897 unset($plugintypes['mod']);
898 ksort($plugintypes);
899 foreach ($plugintypes as $type=>$unused) {
900 $plugs = get_plugin_list($type);
901 ksort($plugs);
902 foreach ($plugs as $plug=>$fullplug) {
903 $plugin = new stdClass();
904 $plugin->version = null;
905 @include($fullplug.'/version.php');
906 $versions[$plug] = $plugin->version;
907 }
908 }
909
910 self::$versionhash = sha1(serialize($versions));
911
912 return self::$versionhash;
913 }
914
915 /**
916 * Builds dirroot/phpunit.xml and dataroot/phpunit/webrunner.xml files using defaults from /phpunit.xml.dist
917 * @static
918 * @return bool true means main config file created, false means only dataroot file created
919 */
920 public static function build_config_file() {
921 global $CFG;
922
923 $template = '
924 <testsuite name="@component@">
925 <directory suffix="_test.php">@dir@</directory>
926 </testsuite>';
927 $data = file_get_contents("$CFG->dirroot/phpunit.xml.dist");
928
929 $suites = '';
930
931 $plugintypes = get_plugin_types();
932 ksort($plugintypes);
933 foreach ($plugintypes as $type=>$unused) {
934 $plugs = get_plugin_list($type);
935 ksort($plugs);
936 foreach ($plugs as $plug=>$fullplug) {
937 if (!file_exists("$fullplug/tests/")) {
938 continue;
939 }
940 $dir = substr($fullplug, strlen($CFG->dirroot)+1);
941 $dir .= '/tests';
942 $component = $type.'_'.$plug;
943
944 $suite = str_replace('@component@', $component, $template);
945 $suite = str_replace('@dir@', $dir, $suite);
946
947 $suites .= $suite;
948 }
949 }
950
951 $data = preg_replace('|<!--@plugin_suites_start@-->.*<!--@plugin_suites_end@-->|s', $suites, $data, 1);
952
953 $result = false;
954 if (is_writable($CFG->dirroot)) {
955 if ($result = file_put_contents("$CFG->dirroot/phpunit.xml", $data)) {
956 phpunit_boostrap_fix_file_permissions("$CFG->dirroot/phpunit.xml");
957 }
958 }
959
960 // relink - it seems that xml:base does not work in phpunit xml files, remove this nasty hack if you find a way to set xml base for relative refs
961 $data = str_replace('lib/phpunit/', $CFG->dirroot.DIRECTORY_SEPARATOR.'lib'.DIRECTORY_SEPARATOR.'phpunit'.DIRECTORY_SEPARATOR, $data);
962 $data = preg_replace('|<directory suffix="_test.php">([^<]+)</directory>|',
963 '<directory suffix="_test.php">'.$CFG->dirroot.(DIRECTORY_SEPARATOR === '\\' ? '\\\\' : DIRECTORY_SEPARATOR).'$1</directory>',
964 $data);
965 file_put_contents("$CFG->dataroot/phpunit/webrunner.xml", $data);
966 phpunit_boostrap_fix_file_permissions("$CFG->dataroot/phpunit/webrunner.xml");
967
968 return (bool)$result;
969 }
970
971 /**
972 * Builds phpunit.xml files for all components using defaults from /phpunit.xml.dist
973 *
974 * @static
975 * @return void, stops if can not write files
976 */
977 public static function build_component_config_files() {
978 global $CFG;
979
980 $template = '
981 <testsuites>
982 <testsuite name="@component@">
983 <directory suffix="_test.php">.</directory>
984 </testsuite>
985 </testsuites>';
986
987 // Use the upstream file as source for the distributed configurations
988 $ftemplate = file_get_contents("$CFG->dirroot/phpunit.xml.dist");
989 $ftemplate = preg_replace('|<!--All core suites.*</testsuites>|s', '<!--@component_suite@-->', $ftemplate);
990
991 // Get all the components
992 $components = self::get_all_plugins_with_tests() + self::get_all_subsystems_with_tests();
993
994 // Get all the directories having tests
995 $directories = self::get_all_directories_with_tests();
996
997 // Find any directory not covered by proper components
998 $remaining = array_diff($directories, $components);
999
1000 // Add them to the list of components
1001 $components += $remaining;
1002
1003 // Create the corresponding phpunit.xml file for each component
1004 foreach ($components as $cname => $cpath) {
1005 // Calculate the component suite
1006 $ctemplate = $template;
1007 $ctemplate = str_replace('@component@', $cname, $ctemplate);
1008
1009 // Apply it to the file template
1010 $fcontents = str_replace('<!--@component_suite@-->', $ctemplate, $ftemplate);
1011
1012 // fix link to schema
1013 $level = substr_count(str_replace('\\', '/', $cpath), '/') - substr_count(str_replace('\\', '/', $CFG->dirroot), '/');
1014 $fcontents = str_replace('lib/phpunit/phpunit.xsd', str_repeat('../', $level).'lib/phpunit/phpunit.xsd', $fcontents);
1015 $fcontents = str_replace('lib/phpunit/bootstrap.php', str_repeat('../', $level).'lib/phpunit/bootstrap.php', $fcontents);
1016
1017 // Write the file
1018 $result = false;
1019 if (is_writable($cpath)) {
1020 if ($result = (bool)file_put_contents("$cpath/phpunit.xml", $fcontents)) {
1021 phpunit_boostrap_fix_file_permissions("$cpath/phpunit.xml");
1022 }
1023 }
1024 // Problems writing file, throw error
1025 if (!$result) {
1026 phpunit_bootstrap_error(PHPUNIT_EXITCODE_CONFIGWARNING, "Can not create $cpath/phpunit.xml configuration file, verify dir permissions");
1027 }
1028 }
1029 }
1030
1031 /**
1032 * Returns all the plugins having PHPUnit tests
1033 *
1034 * @return array all the plugins having PHPUnit tests
1035 *
1036 */
1037 private static function get_all_plugins_with_tests() {
1038 $pluginswithtests = array();
1039
1040 $plugintypes = get_plugin_types();
1041 ksort($plugintypes);
1042 foreach ($plugintypes as $type => $unused) {
1043 $plugs = get_plugin_list($type);
1044 ksort($plugs);
1045 foreach ($plugs as $plug => $fullplug) {
1046 // Look for tests recursively
1047 if (self::directory_has_tests($fullplug)) {
1048 $pluginswithtests[$type . '_' . $plug] = $fullplug;
1049 }
1050 }
1051 }
1052 return $pluginswithtests;
1053 }
1054
1055 /**
1056 * Returns all the subsystems having PHPUnit tests
1057 *
1058 * Note we are hacking here the list of subsystems
1059 * to cover some well-known subsystems that are not properly
1060 * returned by the {@link get_core_subsystems()} function.
1061 *
1062 * @return array all the subsystems having PHPUnit tests
1063 */
1064 private static function get_all_subsystems_with_tests() {
1065 global $CFG;
1066
1067 $subsystemswithtests = array();
1068
1069 $subsystems = get_core_subsystems();
1070
1071 // Hack the list a bit to cover some well-known ones
1072 $subsystems['backup'] = 'backup';
1073 $subsystems['db-dml'] = 'lib/dml';
1074 $subsystems['db-ddl'] = 'lib/ddl';
1075
1076 ksort($subsystems);
1077 foreach ($subsystems as $subsys => $relsubsys) {
1078 if ($relsubsys === null) {
1079 continue;
1080 }
1081 $fullsubsys = $CFG->dirroot . '/' . $relsubsys;
1082 if (!is_dir($fullsubsys)) {
1083 continue;
1084 }
1085 // Look for tests recursively
1086 if (self::directory_has_tests($fullsubsys)) {
1087 $subsystemswithtests['core_' . $subsys] = $fullsubsys;
1088 }
1089 }
1090 return $subsystemswithtests;
1091 }
1092
1093 /**
1094 * Returns all the directories having tests
1095 *
1096 * @return array all directories having tests
1097 */
1098 private static function get_all_directories_with_tests() {
1099 global $CFG;
1100
1101 $dirs = array();
1102 $dirite = new RecursiveDirectoryIterator($CFG->dirroot);
1103 $iteite = new RecursiveIteratorIterator($dirite);
1104 $sep = preg_quote(DIRECTORY_SEPARATOR, '|');
1105 $regite = new RegexIterator($iteite, '|'.$sep.'tests'.$sep.'.*_test\.php$|');
1106 foreach ($regite as $path => $element) {
1107 $key = dirname(dirname($path));
1108 $value = trim(str_replace('/', '_', str_replace($CFG->dirroot, '', $key)), '_');
1109 $dirs[$key] = $value;
1110 }
1111 ksort($dirs);
1112 return array_flip($dirs);
1113 }
1114
1115 /**
1116 * Returns if a given directory has tests (recursively)
1117 *
1118 * @param $dir string full path to the directory to look for phpunit tests
1119 * @return bool if a given directory has tests (true) or no (false)
1120 */
1121 private static function directory_has_tests($dir) {
1122 if (!is_dir($dir)) {
1123 return false;
1124 }
1125
1126 $dirite = new RecursiveDirectoryIterator($dir);
1127 $iteite = new RecursiveIteratorIterator($dirite);
1128 $sep = preg_quote(DIRECTORY_SEPARATOR, '|');
1129 $regite = new RegexIterator($iteite, '|'.$sep.'tests'.$sep.'.*_test\.php$|');
1130 $regite->rewind();
1131 if ($regite->valid()) {
1132 return true;
1133 }
1134 return false;
1135 }
1136}