|
| 1 | +<?php |
| 2 | + |
| 3 | +$existing_functions = array( |
| 4 | + 'db_fetch_row', |
| 5 | + 'syslog_db_table_exists', |
| 6 | + 'syslog_db_execute', |
| 7 | + 'syslog_db_execute_prepared' |
| 8 | +); |
| 9 | + |
| 10 | +foreach ($existing_functions as $existing_function) { |
| 11 | + if (function_exists($existing_function)) { |
| 12 | + fwrite(STDERR, "This test must run in isolated mode; function already exists: $existing_function\n"); |
| 13 | + exit(1); |
| 14 | + } |
| 15 | +} |
| 16 | + |
| 17 | +if (!function_exists('cacti_sizeof')) { |
| 18 | + function cacti_sizeof($value) { |
| 19 | + if (is_array($value) || $value instanceof Countable) { |
| 20 | + return count($value); |
| 21 | + } |
| 22 | + |
| 23 | + return 0; |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +$GLOBALS['syslog_replace_data_execute_calls'] = array(); |
| 28 | +$GLOBALS['syslog_replace_data_prepared_calls'] = array(); |
| 29 | +$GLOBALS['issue258_logs'] = array(); |
| 30 | +$GLOBALS['issue258_show_create'] = array('Create Table' => 'CREATE TABLE `syslog_alert` (`id` INT NOT NULL)'); |
| 31 | + |
| 32 | +if (!function_exists('db_fetch_row')) { |
| 33 | + function db_fetch_row($sql) { |
| 34 | + return $GLOBALS['issue258_show_create']; |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +if (!function_exists('syslog_db_table_exists')) { |
| 39 | + function syslog_db_table_exists($table) { |
| 40 | + return false; |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +if (!function_exists('syslog_db_execute')) { |
| 45 | + function syslog_db_execute($sql) { |
| 46 | + $GLOBALS['syslog_replace_data_execute_calls'][] = $sql; |
| 47 | + |
| 48 | + return true; |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +if (!function_exists('syslog_db_execute_prepared')) { |
| 53 | + function syslog_db_execute_prepared($sql, $params) { |
| 54 | + $GLOBALS['syslog_replace_data_prepared_calls'][] = array( |
| 55 | + 'sql' => $sql, |
| 56 | + 'params' => $params |
| 57 | + ); |
| 58 | + |
| 59 | + return true; |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +if (!function_exists('cacti_log')) { |
| 64 | + function cacti_log($message, $output = false, $facility = 'SYSTEM', $level = '') { |
| 65 | + $GLOBALS['issue258_logs'][] = $message; |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +require_once dirname(__DIR__, 2) . '/setup.php'; |
| 70 | + |
| 71 | +$data = array( |
| 72 | + array( |
| 73 | + 'id' => 1, |
| 74 | + 'hash' => 'abc123', |
| 75 | + 'name' => 'sample' |
| 76 | + ) |
| 77 | +); |
| 78 | + |
| 79 | +syslog_replace_data('syslog_alert', $data); |
| 80 | + |
| 81 | +$executed = $GLOBALS['syslog_replace_data_execute_calls']; |
| 82 | + |
| 83 | +if (cacti_sizeof($executed) < 2) { |
| 84 | + fwrite(STDERR, "Expected CREATE + TRUNCATE calls to run.\n"); |
| 85 | + exit(1); |
| 86 | +} |
| 87 | + |
| 88 | +if ($executed[0] !== 'CREATE TABLE `syslog_alert` (`id` INT NOT NULL)') { |
| 89 | + fwrite(STDERR, "Expected CREATE TABLE SQL to be executed from create_sql.\n"); |
| 90 | + exit(1); |
| 91 | +} |
| 92 | + |
| 93 | +if ($executed[1] !== 'TRUNCATE TABLE syslog_alert') { |
| 94 | + fwrite(STDERR, "Expected TRUNCATE TABLE to run after CREATE TABLE.\n"); |
| 95 | + exit(1); |
| 96 | +} |
| 97 | + |
| 98 | +$prepared = $GLOBALS['syslog_replace_data_prepared_calls']; |
| 99 | + |
| 100 | +if (cacti_sizeof($prepared) !== 1) { |
| 101 | + fwrite(STDERR, "Expected a single prepared INSERT statement execution.\n"); |
| 102 | + exit(1); |
| 103 | +} |
| 104 | + |
| 105 | +$GLOBALS['syslog_replace_data_execute_calls'] = array(); |
| 106 | +$GLOBALS['syslog_replace_data_prepared_calls'] = array(); |
| 107 | +$GLOBALS['issue258_show_create'] = false; |
| 108 | + |
| 109 | +syslog_replace_data('syslog_alert', $data); |
| 110 | + |
| 111 | +if (cacti_sizeof($GLOBALS['syslog_replace_data_execute_calls']) !== 0) { |
| 112 | + fwrite(STDERR, "Expected no execute calls when CREATE TABLE SQL is unavailable.\n"); |
| 113 | + exit(1); |
| 114 | +} |
| 115 | + |
| 116 | +if (cacti_sizeof($GLOBALS['syslog_replace_data_prepared_calls']) !== 0) { |
| 117 | + fwrite(STDERR, "Expected no prepared inserts when CREATE TABLE SQL is unavailable.\n"); |
| 118 | + exit(1); |
| 119 | +} |
| 120 | + |
| 121 | +echo "issue258_replication_create_sql_test passed\n"; |
0 commit comments