1768 |
raphael |
1 |
<?php
|
|
|
2 |
|
1775 |
raphael |
3 |
$options = getopt("hu:d:e:p:P:D",array("help", "user:", "domain:", "email:", "pass:", "phpsessid", "debug"));
|
1768 |
raphael |
4 |
if(isset($options['h']) || isset($options['help'])) {
|
1793 |
raphael |
5 |
printf(<<<EOF
|
|
|
6 |
%s [fichiers de test]
|
|
|
7 |
--help|-h: cette aide
|
1802 |
raphael |
8 |
-debug|-D: informations sur les requêtes curl
|
1793 |
raphael |
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 |
|
1794 |
raphael |
26 |
Attention: lancer un test supprime TOUTES les observations existantes de l'utilisateur !
|
1802 |
raphael |
27 |
|
1793 |
raphael |
28 |
EOF
|
|
|
29 |
, /* relax emacs*/
|
|
|
30 |
basename(__FILE__));
|
|
|
31 |
die;
|
1768 |
raphael |
32 |
}
|
|
|
33 |
|
1793 |
raphael |
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 |
|
1768 |
raphael |
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));
|
1775 |
raphael |
42 |
define('DEBUG', isset($options['D']) ? 1 : (isset($options['debug']) ? 1 : 0));
|
1768 |
raphael |
43 |
|
1802 |
raphael |
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 |
}
|
1768 |
raphael |
48 |
|
1802 |
raphael |
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 |
|
1803 |
raphael |
54 |
require_once('api.php');
|
1802 |
raphael |
55 |
|
|
|
56 |
if(!auth()) {
|
|
|
57 |
die('auth problem');
|
|
|
58 |
}
|
|
|
59 |
|
1768 |
raphael |
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 |
|
1793 |
raphael |
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__);
|
1768 |
raphael |
70 |
|
1793 |
raphael |
71 |
$all_tests = array_map('basename', glob(_TESTDIR . '/*.test.php'));
|
|
|
72 |
$tests = array_intersect(array_map('basename', $argv), $all_tests);
|
|
|
73 |
|
1768 |
raphael |
74 |
if(!$tests) $tests = $all_tests;
|
|
|
75 |
|
1793 |
raphael |
76 |
//@array_walk(glob('run/*'), function(&$i) { unlink($i);});
|
|
|
77 |
|
1768 |
raphael |
78 |
foreach($tests as $test) {
|
1793 |
raphael |
79 |
setupTestEnv();
|
|
|
80 |
$testfile = _TESTDIR . '/' . $test;
|
|
|
81 |
|
1768 |
raphael |
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)
|
1793 |
raphael |
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)) {
|
1775 |
raphael |
91 |
$csv = genCSV($test_array['data']);
|
1768 |
raphael |
92 |
file_put_contents($runfile, $csv);
|
|
|
93 |
}
|
|
|
94 |
|
1802 |
raphael |
95 |
//echo "\tcurl -F \"upload=@$runfile\" -F utilisateur=" . USER . " \"" . DOMAIN . "/jrest/ImportXLS\"\n";
|
1769 |
raphael |
96 |
echo "$test: ";
|
1793 |
raphael |
97 |
$var_expected = include(_TESTDIR . '/' . str_replace('.test.', '.result.', $test));
|
1768 |
raphael |
98 |
|
|
|
99 |
$retour = import($runfile);
|
|
|
100 |
$count_warn = 0;
|
|
|
101 |
preg_match('/^ligne /', $retour, $count_warn);
|
|
|
102 |
$count_warn = count($count_warn);
|
|
|
103 |
|
1793 |
raphael |
104 |
$result = getCSV_line(export($cols), 1);
|
1802 |
raphael |
105 |
if(!$result) {
|
|
|
106 |
echo ("\n!!! export vide, problème probable d'authentification ?\n");
|
|
|
107 |
continue;
|
|
|
108 |
}
|
1768 |
raphael |
109 |
|
1775 |
raphael |
110 |
//var_dump(champsLongToShort2($test_array['data']));die;
|
1768 |
raphael |
111 |
if($var_expected) {
|
1775 |
raphael |
112 |
$result = __diff_fields($test_array, $result, $var_expected);
|
|
|
113 |
// unset($result['date_creation'], $result['date_modification'], $result['id_observation']);
|
|
|
114 |
|
1769 |
raphael |
115 |
$d1 = array_diff($var_expected, $result);
|
|
|
116 |
$d2 = array_diff($result, $var_expected);
|
|
|
117 |
if(!$d1 && !$d2) echo "OK\n";
|
1768 |
raphael |
118 |
else {
|
|
|
119 |
echo "FAIL\n";
|
1802 |
raphael |
120 |
if($d1) { echo "expect: "; print_r($d1); }
|
|
|
121 |
if($d2) { echo "result: "; print_r($d2); }
|
1768 |
raphael |
122 |
//var_dump($var_expected, $result);
|
|
|
123 |
}
|
1775 |
raphael |
124 |
if(DEBUG) echo $retour;
|
|
|
125 |
if($count_warn && (!isset($test_array['warn']) || $test_array['warn'] != $count_warn)) { echo "warnings: $count_warn\n"; }
|
1768 |
raphael |
126 |
}
|
1775 |
raphael |
127 |
// pas de résultat de test défini ?
|
1768 |
raphael |
128 |
else {
|
1775 |
raphael |
129 |
if(DEBUG) echo $retour;
|
|
|
130 |
if($count_warn && (!isset($test_array['warn']) || $test_array['warn'] != $count_warn)) { echo "warnings: $count_warn\n"; }
|
1769 |
raphael |
131 |
|
|
|
132 |
echo '<?php return ' . var_export($result, true) . ';';
|
1768 |
raphael |
133 |
}
|
1775 |
raphael |
134 |
}
|
1769 |
raphael |
135 |
|
1775 |
raphael |
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
|
1768 |
raphael |
158 |
}
|
|
|
159 |
|
|
|
160 |
// sed -i -e '1{/<?php return/!s:^:<?php return :}' -e '${/^)$/s:$:;:}' phptests/*.result.php
|