Subversion Repositories eFlore/Applications.cel

Compare Revisions

No changes between revisions

Ignore whitespace Rev 1767 → Rev 1768

/trunk/jrest/tests/cmd-test.php
New file
0,0 → 1,32
<?php
 
$options = getopt("hiou:d:e:p:FU:P:",array("help", "images", "obs", "user:", "domain:", "email:", "pass:", "flush", "upload:", "phpsessid"));
if(isset($options['h']) || isset($options['help'])) {
die(basename(__FILE__) . ' -i|--images -o|--obs [-u|--user = 22506] [-d|--domain = http://cel]');
}
 
define('USER', isset($options['u']) ? $options['u'] : (isset($options['user']) ? $options['user'] : 22506));
define('DOMAIN', isset($options['d']) ? $options['d'] : (isset($options['domain']) ? $options['domain'] : 'http://cel'));
define('EMAIL', isset($options['e']) ? $options['e'] : (isset($options['email']) ? $options['email'] : NULL));
define('PASS', isset($options['p']) ? $options['p'] : (isset($options['pass']) ? $options['pass'] : NULL));
define('COOKIE', isset($options['P']) ? $options['P'] : (isset($options['phpsessid']) ? $options['phpsessid'] : NULL));
 
// API::auth() define()'s COOKIE
require_once('api.php');
 
if(isset($options['o']) || isset($options['obs']) || isset($options['F']) || isset($options['flush'])) {
cel_delete_all_obs();
}
 
if(isset($options['i']) || isset($options['images']) || isset($options['F']) || isset($options['flush'])) {
cel_delete_all_images();
}
 
$upload = isset($options['U']) ? $options['U'] : (isset($options['upload']) ? $options['upload'] : NULL);
if($upload) cel_upload_image($upload);
 
if(! COOKIE) { if(!auth()) die('no auth'); }
 
 
// $csv = genCSV(array('ordre' => 1, 'Date' => '23/06/1991', 'rien' => 'rien'));
var_dump(getCSV_line(export(), 1));
/trunk/jrest/tests/data/image-test.jpg
Cannot display: file marked as a binary type.
svn:mime-type = image/jpeg
/trunk/jrest/tests/data/image-test.jpg
New file
Property changes:
Added: svn:mime-type
+image/jpeg
\ No newline at end of property
/trunk/jrest/tests/data/images.sh
New file
0,0 → 1,8
# upload
curl -s -F file=@test/image-test.jpg -F ce_utilisateur=22506 http://cel/jrest/InventoryImage|grep OK
 
# list
http://cel/jrest/InventoryImageList/22506/?limite=50&numero_page=0|json_pp|egrep 'id_image|nom_original'|awk -F'"' '{print $4}'
 
# IO
POST http://cel/jrest/ExportXLS/22506<<<"format=csv&range=342212"|curl -F "upload=@-" -F utilisateur=22506 "http://cel/jrest/ImportXLS"
Property changes:
Added: svn:eol-style
+native
\ No newline at end of property
Added: svn:executable
+*
\ No newline at end of property
/trunk/jrest/tests/data/image-test2.jpg
Cannot display: file marked as a binary type.
svn:mime-type = image/jpeg
/trunk/jrest/tests/data/image-test2.jpg
New file
Property changes:
Added: svn:mime-type
+image/jpeg
\ No newline at end of property
/trunk/jrest/tests/phptests/accents.test.php
New file
0,0 → 1,3
<?php
// test date format texte, pré 1900
return array('Lieu-dit' => 'œufs', 'Station' => 'Ératostène', 'Milieu' => 'Byørg');
/trunk/jrest/tests/phptests/determ-espece.test.php
New file
0,0 → 1,3
<?php
// test détermination espèce, simple avec erreur
return array('Espèce' => 'heliotropium europaeum Xxumm');
/trunk/jrest/tests/phptests/date-post-1900.result.php
New file
0,0 → 1,29
<?php return array (
'nom_sel' => '',
'nom_sel_nn' => '',
'nom_ret' => '',
'nom_ret_nn' => '',
'nt' => '',
'famille' => '',
'nom_referentiel' => 'autre',
'zone_geo' => '',
'ce_zone_geo' => '',
'date_observation' => '1991/01/23',
'lieudit' => '',
'station' => '',
'milieu' => '',
'commentaire' => '',
'latitude' => '',
'longitude' => '',
'altitude' => '',
'geodatum' => '',
'ordre' => '1',
'mots_cles_texte' => '',
'transmission' => '',
'date_transmission' => '',
'abondance' => '',
'certitude' => '',
'phenologie' => '',
'images' => '',
'nom_commun' => '',
);
/trunk/jrest/tests/phptests/date-post-1900.test.php
New file
0,0 → 1,3
<?php
// test date format texte, post 1900
return array('Date' => '23/01/1991');
/trunk/jrest/tests/phptests/date-post-2900.result.php
New file
0,0 → 1,29
<?php return array (
'nom_sel' => '',
'nom_sel_nn' => '',
'nom_ret' => '',
'nom_ret_nn' => '',
'nt' => '',
'famille' => '',
'nom_referentiel' => '',
'zone_geo' => '',
'ce_zone_geo' => '',
'date_observation' => '',
'lieudit' => '',
'station' => '',
'milieu' => '',
'commentaire' => '',
'latitude' => '',
'longitude' => '',
'altitude' => '',
'geodatum' => '',
'ordre' => '1',
'mots_cles_texte' => '',
'transmission' => '',
'date_transmission' => '',
'abondance' => '',
'certitude' => '',
'phenologie' => '',
'images' => '',
'nom_commun' => '',
);
/trunk/jrest/tests/phptests/localisation-detect.test.php
New file
0,0 → 1,3
<?php
// test date format texte, pré 1900
return array('Commune' => 'Marseille'):
/trunk/jrest/tests/phptests/date-post-2900.test.php
New file
0,0 → 1,3
<?php
// test date format texte, futur
return array('Date' => '23/06/2900');
/trunk/jrest/tests/phptests/date-pre-1900.test.php
New file
0,0 → 1,3
<?php
// test date format texte, pré 1900
return array('Date' => '23/06/1811');
/trunk/jrest/tests/phptests/transmission.test.php
New file
0,0 → 1,3
<?php
// test transmission
return array('Transmis' => 'OuI');
/trunk/jrest/tests/phptests/01-sample.result.php
New file
0,0 → 1,29
<?php return array (
'nom_sel' => '',
'nom_sel_nn' => '',
'nom_ret' => '',
'nom_ret_nn' => '',
'nt' => '',
'famille' => '',
'nom_referentiel' => 'autre',
'zone_geo' => '',
'ce_zone_geo' => '',
'date_observation' => '1991/06/23',
'lieudit' => '',
'station' => '',
'milieu' => '',
'commentaire' => '',
'latitude' => '',
'longitude' => '',
'altitude' => '',
'geodatum' => '',
'ordre' => '1',
'mots_cles_texte' => '',
'transmission' => '',
'date_transmission' => '',
'abondance' => '',
'certitude' => '',
'phenologie' => '',
'images' => '',
'nom_commun' => '',
);
/trunk/jrest/tests/phptests/01-sample.test.php
New file
0,0 → 1,7
<?php
//TODO: test settings:
// * colonnes (standard, avancé, ...)
// * field ignore
// * nombre de warnings
// sample test
return array('ordre' => 1, 'Date' => '23/06/1991', 'rien' => 'rien');
/trunk/jrest/tests/phptests/date-format-invert.result.php
New file
0,0 → 1,29
<?php return array (
'nom_sel' => '',
'nom_sel_nn' => '',
'nom_ret' => '',
'nom_ret_nn' => '',
'nt' => '',
'famille' => '',
'nom_referentiel' => 'autre',
'zone_geo' => '',
'ce_zone_geo' => '',
'date_observation' => '1991/01/23',
'lieudit' => '',
'station' => '',
'milieu' => '',
'commentaire' => '',
'latitude' => '',
'longitude' => '',
'altitude' => '',
'geodatum' => '',
'ordre' => '1',
'mots_cles_texte' => '',
'transmission' => '',
'date_transmission' => '',
'abondance' => '',
'certitude' => '',
'phenologie' => '',
'images' => '',
'nom_commun' => '',
);
/trunk/jrest/tests/phptests/date-format-revert.result.php
New file
0,0 → 1,29
<?php return array (
'nom_sel' => '',
'nom_sel_nn' => '',
'nom_ret' => '',
'nom_ret_nn' => '',
'nt' => '',
'famille' => '',
'nom_referentiel' => 'autre',
'zone_geo' => '',
'ce_zone_geo' => '',
'date_observation' => '1991/01/23',
'lieudit' => '',
'station' => '',
'milieu' => '',
'commentaire' => '',
'latitude' => '',
'longitude' => '',
'altitude' => '',
'geodatum' => '',
'ordre' => '1',
'mots_cles_texte' => '',
'transmission' => '',
'date_transmission' => '',
'abondance' => '',
'certitude' => '',
'phenologie' => '',
'images' => '',
'nom_commun' => '',
);
/trunk/jrest/tests/phptests/date-format-invert.test.php
New file
0,0 → 1,3
<?php
// test date format inverse YYYY/MM/DD
return array('Date' => '1991/01/23');
/trunk/jrest/tests/phptests/date-format-revert.test.php
New file
0,0 → 1,3
<?php
// test date format inverse YYYY/MM/DD
return array('Date' => '23/01/1991');
/trunk/jrest/tests/phptests/accents.result.php
New file
0,0 → 1,29
<?php return array (
'nom_sel' => '',
'nom_sel_nn' => '',
'nom_ret' => '',
'nom_ret_nn' => '',
'nt' => '',
'famille' => '',
'nom_referentiel' => 'autre',
'zone_geo' => '',
'ce_zone_geo' => '',
'date_observation' => '',
'lieudit' => 'œufs',
'station' => 'Ératostène',
'milieu' => 'Byørg',
'commentaire' => '',
'latitude' => '',
'longitude' => '',
'altitude' => '',
'geodatum' => '',
'ordre' => '1',
'mots_cles_texte' => '',
'transmission' => '',
'date_transmission' => '',
'abondance' => '',
'certitude' => '',
'phenologie' => '',
'images' => '',
'nom_commun' => '',
);
/trunk/jrest/tests/IO-fields-unittest.php
New file
0,0 → 1,67
<?php
 
$options = getopt("hu:d:e:p:P:",array("help", "user:", "domain:", "email:", "pass:", "phpsessid"));
if(isset($options['h']) || isset($options['help'])) {
die(basename(__FILE__) . ' [-u|--user = 22506] [-d|--domain = http://cel]');
}
 
define('USER', isset($options['u']) ? $options['u'] : (isset($options['user']) ? $options['user'] : 22506));
define('DOMAIN', isset($options['d']) ? $options['d'] : (isset($options['domain']) ? $options['domain'] : 'http://cel'));
define('EMAIL', isset($options['e']) ? $options['e'] : (isset($options['email']) ? $options['email'] : NULL));
define('PASS', isset($options['p']) ? $options['p'] : (isset($options['pass']) ? $options['pass'] : NULL));
define('COOKIE', isset($options['P']) ? $options['P'] : (isset($options['phpsessid']) ? $options['phpsessid'] : NULL));
 
require_once('api.php');
 
function setupTestEnv() {
cel_delete_all_obs();
cel_delete_all_images();
cel_upload_image('image-test.jpg');
cel_upload_image('image-test2.jpg');
}
 
if(! is_dir('phptests')) die('no phptests/');
if(! is_dir('run')) mkdir('run');
if(! is_dir('run')) die('no run/');
 
 
$all_tests = glob('phptests/*.test.php');
$tests = array_intersect($argv, $all_tests);
if(!$tests) $tests = $all_tests;
 
foreach($tests as $test) {
setupTestEnv();
// cache car l'upload de fichier PHP-curl ne peut être
// simulé avec le contenu d'une variable (cf CURLOPT_POSTFIELDS et @fichier)
$runfile = 'run/' . basename($test);
if(!is_file($runfile)) {
$csv = genCSV(require($test));
file_put_contents($runfile, $csv);
}
 
$var_expected = include(str_replace('.test.', '.result.', $test));
 
$retour = import($runfile);
$count_warn = 0;
preg_match('/^ligne /', $retour, $count_warn);
$count_warn = count($count_warn);
 
echo $count_warn . "\n";
$result = getCSV_line(export(), 1);
unset($result['date_creation'], $result['date_modification'], $result['id_observation']);
 
echo "$test\n";
if($var_expected) {
if(! array_diff($var_expected, $result) && ! array_diff($result, $var_expected)) echo "OK\n";
else {
echo "FAIL\n";
//var_dump($var_expected, $result);
var_dump(array_diff($var_expected, $result), array_diff($result, $var_expected));
}
}
else {
echo '<?php return ' . var_export($result, true) . ';'
}
}
 
// sed -i -e '1{/<?php return/!s:^:<?php return :}' -e '${/^)$/s:$:;:}' phptests/*.result.php
/trunk/jrest/tests/api.php
New file
0,0 → 1,238
<?php
 
global $champs;
$champs = Array(
'nom_sel' => 'Espèce',
'nom_sel_nn' => 'Numéro nomenclatural',
'nom_ret' => 'Nom retenu',
'nom_ret_nn' => 'Numéro nomenclatural nom retenu',
'nt' => 'Numéro taxonomique',
'famille' => 'Famille',
'nom_referentiel' => 'Referentiel taxonomique',
'zone_geo' => 'Commune',
'ce_zone_geo' => 'Identifiant Commune',
'date_observation' => 'Date',
'lieudit' => 'Lieu-dit',
'station' => 'Station',
'milieu' => 'Milieu',
'commentaire' => 'Notes',
'latitude' => 'Latitude',
'longitude' => 'Longitude',
'altitude' => 'Altitude',
'geodatum' => 'Référentiel Géographique',
 
'ordre' => 'Ordre',
'id_observation' => 'Identifiant',
 
'mots_cles_texte' => 'Mots Clés',
 
'date_creation' => 'Date Création',
'date_modification' => 'Date Modification',
 
'transmission' => 'Transmis',
'date_transmission' => 'Date Transmission',
'abondance' => 'Abondance',
'certitude' => 'Certitude',
'phenologie' => 'Phénologie',
'images' => 'Image(s)',
'nom_commun' => 'Nom Commun',
);
 
 
function cel_get_obs() {
$URL = DOMAIN . '/jrest/InventoryObservationList/' . USER;
return json_decode(file_get_contents($URL . '/?limite=100000&numero_page=0'));
}
 
function cel_get_images() {
$URL = DOMAIN . '/jrest/InventoryImageList/' . USER;
return json_decode(file_get_contents($URL . '/?limite=100000&numero_page=0'));
}
 
function cel_delete_obs($obs) {
$URL = DOMAIN . '/jrest/InventoryObservationList/' . USER;
$ch = curl_init();
 
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $URL . '/' . implode(',', $obs));
curl_setopt($ch,CURLOPT_POSTFIELDS, array('action' => 'DELETE'));
curl_setopt($ch,CURLOPT_RETURNTRANSFER, TRUE);
//execute post
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
 
function cel_delete_image($img) {
$URL = DOMAIN . '/jrest/InventoryImageList/' . USER;
$ch = curl_init();
 
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $URL . '/' . implode(',', $img));
curl_setopt($ch,CURLOPT_POSTFIELDS, array('action' => 'DELETE'));
curl_setopt($ch,CURLOPT_RETURNTRANSFER, TRUE);
//execute post
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
 
 
function cel_delete_all_obs() {
$obs = cel_get_obs();
$ordres = array_map(function($item) { return $item->ordre; }, $obs);
cel_delete_obs($ordres);
}
 
 
function cel_delete_all_images() {
$img = cel_get_images();
$ids = array_map(function($item) { return $item->id_image; }, $img);
cel_delete_image($ids);
}
 
function cel_upload_image($file) {
if(! file_exists($file)) return NULL;
$URL = DOMAIN . '/jrest/InventoryImage';
 
$ch = curl_init();
 
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $URL);
curl_setopt($ch,CURLOPT_POSTFIELDS, array(
'file' => '@' . $file,
'ce_utilisateur' => USER
));
 
curl_setopt($ch,CURLOPT_RETURNTRANSFER, TRUE);
//execute post
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
 
function cel_link_images($user, $id_image, $id_obs) {
$URL = DOMAIN . '/jrest/inventoryImageLink/';
$ch = curl_init();
 
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $URL);
curl_setopt($ch,CURLOPT_POSTFIELDS, array(
'id_image' => $id_obs,
'id_observation' => $id_obs,
'ce_utilisateur' => $user
));
 
curl_setopt($ch,CURLOPT_RETURNTRANSFER, TRUE);
//execute post
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
 
 
function auth() {
// TODO: CURLOPT_NETRC + Cel API support
if(!defined('EMAIL') || !defined('PASS') || ! EMAIL || ! PASS) return FALSE;
$URL = DOMAIN . '/jrest/User/' . EMAIL . '/' . PASS;
 
$ch = curl_init();
 
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $URL);
curl_setopt($ch,CURLOPT_HEADER, true);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, TRUE);
 
$result = curl_exec($ch);
curl_close($ch);
$sess = array();
preg_match('/.*PHPSESSID=(\w+).*/', $result, $sess);
if(isset($sess[1])) {
define('COOKIE', $sess[1]);
return true;
}
return false;
}
 
function genCSV($d = Array()) {
$out = fopen("php://temp", 'r+');
// don't filter-out unknown field (for testing)
// $d = array_intersect_key($d, $GLOBALS['champs']);
$head = array();
foreach($d as $k => $v) {
if(isset($GLOBALS['champs'][$k])) $head[$GLOBALS['champs'][$k]] = $v;
else $head[$k] = $v;
}
fputcsv($out, array_keys($head) , ',', '"');
fputcsv($out, array_values($head) , ',', '"');
rewind($out);
$csv = stream_get_contents($out);
fclose($out);
return $csv;
}
 
function import($d) {
$URL = DOMAIN . '/jrest/ImportXLS';
if(!is_file($d)) return FALSE;
$ch = curl_init();
 
// curl does not support setting filename="X.csv";type=application/octet-stream'
// with plain POST variables
 
curl_setopt($ch,CURLOPT_URL, $URL);
curl_setopt($ch,CURLOPT_POSTFIELDS, array(
'upload' => '@' . $d . ';filename=test.csv',
'utilisateur' => USER
));
 
curl_setopt($ch,CURLOPT_RETURNTRANSFER, TRUE);
//execute post
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
 
function export($cols = array("standard", "avance")) {
if(!defined('COOKIE')) return FALSE;
 
$URL = DOMAIN . '/jrest/CelWidgetExport/export?';
$URL .= http_build_query(array('colonnes' => implode(',', $cols),
'id_utilisateur' => USER,
'format' => 'csv'));
$ch = curl_init();
 
curl_setopt($ch,CURLOPT_URL, $URL);
curl_setopt($ch,CURLOPT_COOKIE, 'PHPSESSID=' . COOKIE);
 
curl_setopt($ch,CURLOPT_RETURNTRANSFER, TRUE);
//execute post
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
 
function getCSV_line($csv, $line) {
$line = max(0, intval($line - 1)); // -1 à cause du header
 
$out = fopen("php://temp", 'rw');
fwrite($out, $csv);
rewind($out);
 
$head = champsLongToShort(fgetcsv($out));
while ($line--) fgetcsv($out);
$l = fgetcsv($out);
fclose($out);
return array_combine($head, $l);
}
 
 
function champsLongToShort($a) {
$r = array();
$x = array_flip($GLOBALS['champs']);
foreach($a as $k => $v) {
if(isset($x[$v])) $r[$k] = $x[$v];
else $r[$k] = -1;
}
return $r;
}
 
/trunk/jrest/tests/multi-tests.xls
Cannot display: file marked as a binary type.
svn:mime-type = application/octet-stream
/trunk/jrest/tests/multi-tests.xls
New file
Property changes:
Added: svn:mime-type
+application/octet-stream
\ No newline at end of property