1807 |
raphael |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
$options = getopt("hu:d:e:p:P:D",array("help", "user:", "domain:", "email:", "pass:", "phpsessid", "debug"));
|
|
|
4 |
if(isset($options['h']) || isset($options['help'])) {
|
|
|
5 |
printf(<<<EOF
|
|
|
6 |
%s [fichiers de test]
|
|
|
7 |
--help|-h: cette aide
|
|
|
8 |
-debug|-D: informations sur les requêtes curl
|
|
|
9 |
[-u|--user = 22506]
|
|
|
10 |
[-d|--domain = "http://cel"]
|
|
|
11 |
--pass|-p: pour l'authentifiation
|
|
|
12 |
--email|-e: pour l'authentifiation
|
|
|
13 |
--phpsessid|-P: pour l'authentifiation, alternative à -e/-p
|
|
|
14 |
|
|
|
15 |
* --phpsessid permet de passer un identifiant de session PHP pour effectuer l'export.
|
|
|
16 |
Exemple:
|
|
|
17 |
$ sqlite3 ~/.mozilla/firefox/*.default/cookies.sqlite<<<"SELECT * FROM moz_cookies WHERE baseDomain = 'cel' AND name = 'PHPSESSID';"
|
|
|
18 |
|
|
|
19 |
* [fichiers de test]: un ou plusieurs fichier(s) de test valide(s), commençant par "phptests/" et finissant en ".test.php"
|
|
|
20 |
Exemple: phptests/a*.test.php ou bien phptests/latitude-virgule.test.php
|
|
|
21 |
|
|
|
22 |
Exemple:
|
|
|
23 |
$ php IO-fields-unittest.php -P s97knu4axzi6440n1ihja9ppk1
|
|
|
24 |
$ php IO-fields-unittest.php -e me@tela -p blah -u 23004 -d http://localhost phptests/determ-espece*
|
|
|
25 |
|
|
|
26 |
Attention: lancer un test supprime TOUTES les observations existantes de l'utilisateur !
|
|
|
27 |
|
|
|
28 |
EOF
|
|
|
29 |
, /* relax emacs*/
|
|
|
30 |
basename(__FILE__));
|
|
|
31 |
die;
|
|
|
32 |
}
|
|
|
33 |
|
|
|
34 |
// stocke les test
|
|
|
35 |
define('_TESTDIR', dirname(__FILE__) . '/' . 'phptests');
|
|
|
36 |
// stocke les CSV générés à partir des test (répertoire de cache)
|
|
|
37 |
define('_RUNDIR', dirname(__FILE__) . '/' . 'run');
|
|
|
38 |
|
|
|
39 |
define('DOMAIN', isset($options['d']) ? $options['d'] : (isset($options['domain']) ? $options['domain'] : 'http://cel'));
|
|
|
40 |
define('EMAIL', isset($options['e']) ? $options['e'] : (isset($options['email']) ? $options['email'] : NULL));
|
|
|
41 |
define('PASS', isset($options['p']) ? $options['p'] : (isset($options['pass']) ? $options['pass'] : NULL));
|
|
|
42 |
define('DEBUG', isset($options['D']) ? 1 : (isset($options['debug']) ? 1 : 0));
|
|
|
43 |
|
|
|
44 |
// could be defined by the API::auth()
|
|
|
45 |
if(isset($options['u']) || isset($options['user'])) {
|
|
|
46 |
define('USER', isset($options['u']) ? $options['u'] : $options['user']);
|
|
|
47 |
}
|
|
|
48 |
|
|
|
49 |
// could be defined by the API::auth()
|
|
|
50 |
if(isset($options['P']) || isset($options['phpsessid'])) {
|
|
|
51 |
define('COOKIE', isset($options['P']) ? $options['P'] : $options['phpsessid']);
|
|
|
52 |
}
|
|
|
53 |
|
|
|
54 |
require_once('api.php');
|
|
|
55 |
|
|
|
56 |
if(!auth()) {
|
|
|
57 |
die('auth problem');
|
|
|
58 |
}
|
|
|
59 |
|
|
|
60 |
function setupTestEnv() {
|
|
|
61 |
cel_delete_all_obs();
|
|
|
62 |
cel_delete_all_images();
|
|
|
63 |
cel_upload_image('image-test.jpg');
|
|
|
64 |
cel_upload_image('image-test2.jpg');
|
|
|
65 |
}
|
|
|
66 |
|
|
|
67 |
if(! is_dir(_TESTDIR)) die('no phptests/ directory inside ' . __DIR__);
|
|
|
68 |
if(! is_dir(_RUNDIR)) mkdir(_RUNDIR);
|
|
|
69 |
if(! is_dir(_RUNDIR)) die('no run/ directory inside ' . __DIR__);
|
|
|
70 |
|
|
|
71 |
$all_tests = array_map('basename', glob(_TESTDIR . '/*.test.php'));
|
|
|
72 |
$tests = array_intersect(array_map('basename', $argv), $all_tests);
|
|
|
73 |
|
|
|
74 |
if(!$tests) $tests = $all_tests;
|
|
|
75 |
|
|
|
76 |
//@array_walk(glob('run/*'), function(&$i) { unlink($i);});
|
|
|
77 |
|
|
|
78 |
foreach($tests as $test) {
|
|
|
79 |
setupTestEnv();
|
|
|
80 |
$testfile = _TESTDIR . '/' . $test;
|
|
|
81 |
|
|
|
82 |
// cache car l'upload de fichier PHP-curl ne peut être
|
|
|
83 |
// simulé avec le contenu d'une variable (cf CURLOPT_POSTFIELDS et @fichier)
|
|
|
84 |
$runfile = _RUNDIR . '/' . $test;
|
|
|
85 |
|
|
|
86 |
$test_array = require($testfile);
|
|
|
87 |
|
|
|
88 |
$cols = isset($test_array['dumpCols']) ? $test_array['dumpCols'] : 'standard';
|
|
|
89 |
|
|
|
90 |
if(!is_file($runfile) || filemtime($runfile) < filemtime($testfile)) {
|
|
|
91 |
$csv = genCSV($test_array['data']);
|
|
|
92 |
file_put_contents($runfile, $csv);
|
|
|
93 |
}
|
|
|
94 |
|
|
|
95 |
//echo "\tcurl -F \"upload=@$runfile\" -F utilisateur=" . USER . " \"" . DOMAIN . "/jrest/ImportXLS\"\n";
|
|
|
96 |
echo "$test: ";
|
|
|
97 |
$var_expected = include(_TESTDIR . '/' . str_replace('.test.', '.result.', $test));
|
|
|
98 |
|
|
|
99 |
$retour = importXLS($runfile);
|
|
|
100 |
$count_warn = 0;
|
|
|
101 |
preg_match('/^ligne /', $retour, $count_warn);
|
|
|
102 |
$count_warn = count($count_warn);
|
|
|
103 |
|
|
|
104 |
$result = getCSV_line(exportXLS($cols), 1);
|
|
|
105 |
if(!$result) {
|
|
|
106 |
echo ("\n!!! export vide, problème probable d'authentification ?\n");
|
|
|
107 |
continue;
|
|
|
108 |
}
|
|
|
109 |
|
|
|
110 |
//var_dump(champsLongToShort2($test_array['data']));die;
|
|
|
111 |
if($var_expected) {
|
|
|
112 |
$result = __diff_fields($test_array, $result, $var_expected);
|
|
|
113 |
// unset($result['date_creation'], $result['date_modification'], $result['id_observation']);
|
|
|
114 |
|
|
|
115 |
$d1 = array_diff($var_expected, $result);
|
|
|
116 |
$d2 = array_diff($result, $var_expected);
|
|
|
117 |
if(!$d1 && !$d2) echo "OK\n";
|
|
|
118 |
else {
|
|
|
119 |
echo "FAIL\n";
|
|
|
120 |
if($d1) { echo "expect: "; print_r($d1); }
|
|
|
121 |
if($d2) { echo "result: "; print_r($d2); }
|
|
|
122 |
//var_dump($var_expected, $result);
|
|
|
123 |
}
|
|
|
124 |
if(DEBUG) echo $retour;
|
|
|
125 |
if($count_warn && (!isset($test_array['warn']) || $test_array['warn'] != $count_warn)) { echo "warnings: $count_warn\n"; }
|
|
|
126 |
}
|
|
|
127 |
// pas de résultat de test défini ?
|
|
|
128 |
else {
|
|
|
129 |
if(DEBUG) echo $retour;
|
|
|
130 |
if($count_warn && (!isset($test_array['warn']) || $test_array['warn'] != $count_warn)) { echo "warnings: $count_warn\n"; }
|
|
|
131 |
|
|
|
132 |
echo '<?php return ' . var_export($result, true) . ';';
|
|
|
133 |
}
|
|
|
134 |
}
|
|
|
135 |
|
|
|
136 |
|
|
|
137 |
|
|
|
138 |
|
|
|
139 |
|
|
|
140 |
function __diff_fields($test_array, $result, $var_expected) {
|
|
|
141 |
if(! isset($test_array['cmpCols'])) {
|
|
|
142 |
return array_intersect_key($result, $var_expected);
|
|
|
143 |
}
|
|
|
144 |
|
|
|
145 |
if($test_array['cmpCols'] == 'def') {
|
|
|
146 |
return array_intersect_key($result, champsLongToShort2($test_array['data']));
|
|
|
147 |
}
|
|
|
148 |
|
|
|
149 |
$e = array_flip(champsLongToShort2(array_flip(explode(',', ltrim($test_array['cmpCols'], '+-')))));
|
|
|
150 |
if($test_array['cmpCols'][0] == '-') {
|
|
|
151 |
return array_diff_key($result, array_flip(champsLongToShort2($e)));
|
|
|
152 |
}
|
|
|
153 |
else { //if($test_array['cmpCols'][0] == '+') {
|
|
|
154 |
return array_intersect_key($result, array_flip(champsLongToShort2($e)));
|
|
|
155 |
}
|
|
|
156 |
|
|
|
157 |
// pas de 'cmpCols' définie: comparaison de tous les champs par rapport au tableau de résultats attendus
|
|
|
158 |
}
|
|
|
159 |
|
|
|
160 |
// sed -i -e '1{/<?php return/!s:^:<?php return :}' -e '${/^)$/s:$:;:}' phptests/*.result.php
|