Subversion Repositories eFlore/Applications.cel

Rev

Rev 1802 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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