| 1 | <?php |
|---|
| 2 | |
|---|
| 3 | require_once(dirname($argv[0]) . "/../config.php"); |
|---|
| 4 | require_once(IA_ROOT_DIR."common/log.php"); |
|---|
| 5 | require_once(IA_ROOT_DIR."common/common.php"); |
|---|
| 6 | require_once(IA_ROOT_DIR."common/db/db.php"); |
|---|
| 7 | require_once(IA_ROOT_DIR."common/db/user.php"); |
|---|
| 8 | |
|---|
| 9 | if (!IA_DEVELOPMENT_MODE) { |
|---|
| 10 | // These tests alter the database and can remove user created content |
|---|
| 11 | // by mistake (it has happened in the past). |
|---|
| 12 | log_error("You should never run these tests in a production environment"); |
|---|
| 13 | } |
|---|
| 14 | |
|---|
| 15 | // Test with curl. $args format: |
|---|
| 16 | // * url: url to curl. If http:// is ommited IA_URL_HOST is assumed. |
|---|
| 17 | // * post: post arguments (use url_ functions for get args). |
|---|
| 18 | // * user: user to curl as. HTTP auth is used, password is always pwd. |
|---|
| 19 | // * validate_html: boolean, default true. Automatic html validation. |
|---|
| 20 | // |
|---|
| 21 | // TODO: random passwords in test_prepare. |
|---|
| 22 | function curl_test($args) |
|---|
| 23 | { |
|---|
| 24 | $curl_args = array( |
|---|
| 25 | CURLOPT_FOLLOWLOCATION => true, |
|---|
| 26 | CURLOPT_RETURNTRANSFER => true, |
|---|
| 27 | // CURLOPT_VERBOSE => true, |
|---|
| 28 | ); |
|---|
| 29 | |
|---|
| 30 | if (strpos($args["url"], 'http://') === false) { |
|---|
| 31 | $curl_args[CURLOPT_URL] = IA_URL_HOST . $args['url']; |
|---|
| 32 | } else { |
|---|
| 33 | $curl_args[CURLOPT_URL] = $args['url']; |
|---|
| 34 | } |
|---|
| 35 | |
|---|
| 36 | // Properly encode POSTFIELDS |
|---|
| 37 | if (isset($args['post'])) { |
|---|
| 38 | $post_args = array(); |
|---|
| 39 | // Properly encode array args. Fucking awesome |
|---|
| 40 | // FIXME: does not properly handle multiple levels. |
|---|
| 41 | // FIXTHEM: people who use that deserve to die. |
|---|
| 42 | foreach ($args['post'] as $name => $val) { |
|---|
| 43 | if (is_array($val)) { |
|---|
| 44 | // Oh yeah, pure evil baby. |
|---|
| 45 | foreach ($val as $k => $v) { |
|---|
| 46 | $post_args["{$name}[{$k}]"] = $v; |
|---|
| 47 | } |
|---|
| 48 | } else { |
|---|
| 49 | $post_args[$name] = $val; |
|---|
| 50 | } |
|---|
| 51 | } |
|---|
| 52 | $curl_args[CURLOPT_POSTFIELDS] = $post_args; |
|---|
| 53 | } |
|---|
| 54 | |
|---|
| 55 | if (isset($args['user'])) { |
|---|
| 56 | if (array_key_exists('pwd', $args)) { |
|---|
| 57 | $curl_args[CURLOPT_USERPWD] = $args['user'].':'.$args['pwd']; |
|---|
| 58 | } else { |
|---|
| 59 | $curl_args[CURLOPT_USERPWD] = $args['user'].':pwd'; |
|---|
| 60 | } |
|---|
| 61 | } |
|---|
| 62 | |
|---|
| 63 | $ch = curl_init(); |
|---|
| 64 | curl_setopt_array($ch, $curl_args); |
|---|
| 65 | |
|---|
| 66 | $content = curl_exec($ch); |
|---|
| 67 | if ($content === false) { |
|---|
| 68 | log_print_r($curl_args); |
|---|
| 69 | log_error("Failed curling."); |
|---|
| 70 | } |
|---|
| 71 | $res = curl_getinfo($ch); |
|---|
| 72 | $res['content'] = $content; |
|---|
| 73 | curl_close($ch); |
|---|
| 74 | |
|---|
| 75 | if (getattr($args, 'validate_html', false)) { |
|---|
| 76 | validate_html($content); |
|---|
| 77 | } |
|---|
| 78 | |
|---|
| 79 | return $res; |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | // Validates a string as a html document. |
|---|
| 83 | // Uses the external "validate" tool, packages wdg-html-validator in debian. |
|---|
| 84 | // dies on failure. |
|---|
| 85 | function validate_html($content) |
|---|
| 86 | { |
|---|
| 87 | $fname = IA_ROOT_DIR . 'tests/temp.html'; |
|---|
| 88 | file_put_contents($fname, $content); |
|---|
| 89 | |
|---|
| 90 | $result = shell_exec("validate --warn --verbose $fname"); |
|---|
| 91 | |
|---|
| 92 | if (strstr($result, 'Error') || strstr($result, 'Warning')) { |
|---|
| 93 | log_print($result); |
|---|
| 94 | log_error("HTML validation failed"); |
|---|
| 95 | } |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| 98 | // Create test users. |
|---|
| 99 | function test_prepare() |
|---|
| 100 | { |
|---|
| 101 | $user = user_init(); |
|---|
| 102 | $user['email'] = "no@spam.com"; |
|---|
| 103 | |
|---|
| 104 | $user['username'] = 'test_dudE1'; |
|---|
| 105 | $user['password'] = user_hash_password('pwd', $user['username']); |
|---|
| 106 | $user['full_name'] = 'Testing Dude 1'; |
|---|
| 107 | log_assert(user_create($user)); |
|---|
| 108 | |
|---|
| 109 | $user['username'] = 'teSt_dude2'; |
|---|
| 110 | $user['password'] = user_hash_password('pwd', $user['username']); |
|---|
| 111 | $user['full_name'] = 'Testing Dude 2'; |
|---|
| 112 | log_assert(user_create($user)); |
|---|
| 113 | |
|---|
| 114 | $user['username'] = 'teSt_helper1'; |
|---|
| 115 | $user['password'] = user_hash_password('pwd', $user['username']); |
|---|
| 116 | $user['full_name'] = 'Testing Helper 1'; |
|---|
| 117 | $user['email'] = 'no@spam.com'; |
|---|
| 118 | $user['security_level'] = 'helper'; |
|---|
| 119 | log_assert(user_create($user)); |
|---|
| 120 | |
|---|
| 121 | $user['username'] = 'test_hElper2'; |
|---|
| 122 | $user['password'] = user_hash_password('pwd', $user['username']); |
|---|
| 123 | $user['full_name'] = 'Testing Helper 2'; |
|---|
| 124 | $user['security_level'] = 'helper'; |
|---|
| 125 | log_assert(user_create($user)); |
|---|
| 126 | |
|---|
| 127 | $user['username'] = 'tEst_adMin'; |
|---|
| 128 | $user['password'] = user_hash_password('pwd', $user['username']); |
|---|
| 129 | $user['full_name'] = 'Testing Admin'; |
|---|
| 130 | $user['email'] = 'no@spam.com'; |
|---|
| 131 | $user['security_level'] = 'admin'; |
|---|
| 132 | log_assert(user_create($user)); |
|---|
| 133 | } |
|---|
| 134 | |
|---|
| 135 | // Cleanup for testing. |
|---|
| 136 | // Warning: bugs might fuck the db. |
|---|
| 137 | // However, tests are only run on a local copy anyway. |
|---|
| 138 | function test_cleanup() |
|---|
| 139 | { |
|---|
| 140 | $test_accounts = "('test_dudE1', 'teSt_dude2', 'teSt_helper1', 'test_hElper2', 'tEst_adMin')"; |
|---|
| 141 | db_query("DELETE FROM ia_user WHERE `username` IN $test_accounts"); |
|---|
| 142 | db_query("DELETE FROM ia_smf_members WHERE `memberName` IN $test_accounts"); |
|---|
| 143 | db_query("DELETE FROM ia_task WHERE `id` LIKE 'test\_%'"); |
|---|
| 144 | db_query("DELETE FROM ia_round WHERE `id` LIKE 'test\_%'"); |
|---|
| 145 | // Remove various stuff from the wiki. |
|---|
| 146 | $prefixes = array('sandbox', 'utilizator', 'runda', 'problema'); |
|---|
| 147 | foreach ($prefixes as $prefix) { |
|---|
| 148 | db_query("DELETE FROM `ia_textblock` ". |
|---|
| 149 | "WHERE `name` LIKE '$prefix/test\_%'"); |
|---|
| 150 | db_query("DELETE FROM `ia_textblock_revision` ". |
|---|
| 151 | "WHERE `name` LIKE '$prefix/test\_%'"); |
|---|
| 152 | db_query("DELETE FROM `ia_file` ". |
|---|
| 153 | "WHERE `page` LIKE '$prefix/test\_%'"); |
|---|
| 154 | } |
|---|
| 155 | } |
|---|
| 156 | |
|---|
| 157 | db_connect(); |
|---|
| 158 | mem_cache_purge(); |
|---|
| 159 | disk_cache_purge(); |
|---|
| 160 | check_requirements(); |
|---|
| 161 | |
|---|
| 162 | // Add log timestamps. |
|---|
| 163 | define("IA_LOG_TIMESTAMP_FORMAT", "Y-m-d H:i:s"); |
|---|
| 164 | |
|---|
| 165 | ?> |
|---|