source: trunk/tests/utilities.php @ 1184

Revision 1162, 5.3 KB checked in by bogdan2412, 5 months ago (diff)

More fixes from live.

  • Fix 500 errors appearing when attachment name contained ilegal characters.

(was not security issue, only failing assert)

  • Fix 500 errors appearing when textblock revision was not an integer

(was not security issue, got converted into integer -1 and caused mysql
syntax error)

  • Fix coding style, variable name mismatch and memory leak in userwidget view.
  • Fix bug in account controller when trying to change user's security level.
  • Do not allow tests to be run if not in development mode.
  • Property svn:eol-style set to native
Line 
1<?php
2
3require_once(dirname($argv[0]) . "/../config.php");
4require_once(IA_ROOT_DIR."common/log.php");
5require_once(IA_ROOT_DIR."common/common.php");
6require_once(IA_ROOT_DIR."common/db/db.php");
7require_once(IA_ROOT_DIR."common/db/user.php");
8
9if (!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.
22function 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.
85function 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.
99function 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.
138function 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
157db_connect();
158mem_cache_purge();
159disk_cache_purge();
160check_requirements();
161
162// Add log timestamps.
163define("IA_LOG_TIMESTAMP_FORMAT", "Y-m-d H:i:s");
164
165?>
Note: See TracBrowser for help on using the repository browser.