| 1361 | raphael | 1 | <?php
 | 
        
           |  |  | 2 | require_once __DIR__ . '/../ServiceDelPhpUnit.php';
 | 
        
           |  |  | 3 | error_reporting(E_ALL);
 | 
        
           |  |  | 4 |   | 
        
           |  |  | 5 | class ObservationsTest extends ServiceDelPhpUnit {
 | 
        
           |  |  | 6 | 	//static function creerUrl($service, $ressources, $parametres = NULL) {
 | 
        
           |  |  | 7 |   | 
        
           |  |  | 8 | 	public function testNonExistant() {
 | 
        
           |  |  | 9 | 		$url = $this->creerUrl('observations');
 | 
        
           |  |  | 10 | 		$i = new Observations();
 | 
        
           |  |  | 11 | 		$retour = $this->consulterDirectJson($i->consulter([0], []),
 | 
        
           |  |  | 12 | 											$url);
 | 
        
           | 1378 | raphael | 13 | 		// doit retourner une 404
 | 
        
           | 1361 | raphael | 14 | 		$this->assertEmpty($retour, "Le json doit retourner un tableau vide. Voir : $url");
 | 
        
           |  |  | 15 | 	}
 | 
        
           |  |  | 16 |   | 
        
           |  |  | 17 | 	public function testExistant() {
 | 
        
           |  |  | 18 | 		$url = $this->creerUrl('observations');
 | 
        
           |  |  | 19 | 		$i = new Observations();
 | 
        
           |  |  | 20 | 		$retour = $this->consulterDirectJson($i->consulter([1043942], []),
 | 
        
           |  |  | 21 | 											$url);
 | 
        
           | 1378 | raphael | 22 |   | 
        
           | 1361 | raphael | 23 | 		$this->hasKeysAndNotEmpty($retour, ['auteur.id', 'auteur.nom', 'date_transmission', 'observateur', 'id_observation'], $url);
 | 
        
           |  |  | 24 | 		$this->hasKeys($retour, ['auteur.nom'], $url);
 | 
        
           |  |  | 25 | 		$this->assertArrayHasKey('auteur.id', $retour, "attribut auteur.id manquant. Voir : $url");
 | 
        
           |  |  | 26 | 	}
 | 
        
           |  |  | 27 |   | 
        
           | 1382 | raphael | 28 |   | 
        
           |  |  | 29 | 	public function testExistantEtContenu() {
 | 
        
           |  |  | 30 | 		$url = $this->creerUrl('observations');
 | 
        
           |  |  | 31 | 		$i = new Observations();
 | 
        
           |  |  | 32 | 		$retour = $this->consulterDirectJson($i->consulter([1043942], []),
 | 
        
           |  |  | 33 | 											$url);
 | 
        
           |  |  | 34 |   | 
        
           |  |  | 35 | 		// from
 | 
        
           |  |  | 36 | 		// "http://www.tela-botanica.org/eflore/del/services/0.1/observations/1048532
 | 
        
           |  |  | 37 | 		$expected = json_decode(file_get_contents("obs-1048532-complete.data.json"), true);
 | 
        
           |  |  | 38 |   | 
        
           |  |  | 39 | 		self::ignoreNullValuesAndSort($expected);
 | 
        
           |  |  | 40 | 		self::ignoreNullValuesAndSort($retour);
 | 
        
           |  |  | 41 |   | 
        
           |  |  | 42 | 		// echo implode(',', array_keys($retour['resultats'])) . "\n" . implode(',', array_keys($expected['resultats']));die;
 | 
        
           |  |  | 43 | 		$this->clefsIdentiques($expected, $retour, $url);
 | 
        
           |  |  | 44 | 		$this->assertEquals($expected['resultats'], $retour['resultats'], "Différences dans le tableau pour l'obs 1048532, $url");
 | 
        
           |  |  | 45 | 	}
 | 
        
           |  |  | 46 |   | 
        
           | 1378 | raphael | 47 | 	public function testSansImage() {
 | 
        
           |  |  | 48 | 		$url = $this->creerUrl('observations');
 | 
        
           |  |  | 49 | 		$i = new Observations();
 | 
        
           |  |  | 50 | 		$retour = $this->consulterDirectJson($i->consulter([14203], []),
 | 
        
           |  |  | 51 | 											$url);
 | 
        
           |  |  | 52 | 		// doit retourner une 404
 | 
        
           |  |  | 53 | 		$this->assertEmpty($retour, "Observation sans image devrait ne rien retourner. Voir : $url");
 | 
        
           |  |  | 54 | 	}
 | 
        
           |  |  | 55 |   | 
        
           |  |  | 56 | 	public function testRechercheNoObs() {
 | 
        
           |  |  | 57 | 		$url = $this->creerUrl('observations');
 | 
        
           |  |  | 58 | 		$i = new Observations();
 | 
        
           |  |  | 59 | 		$retour = $this->consulterDirectJson($i->consulter([], ['masque.date'=>-1e11]), // 10^11 secondes avant 1970
 | 
        
           |  |  | 60 | 											$url);
 | 
        
           |  |  | 61 | 		// doit retourner une 404
 | 
        
           |  |  | 62 | 		$this->assertEquals(0, $retour['entete']['total'], "Incongruité sur ['entete'][0]. Voir : $url");
 | 
        
           |  |  | 63 | 		$this->assertEmpty($retour['resultats'], "Absence d'observation devrait retourner un result-set vide. Voir : $url");
 | 
        
           |  |  | 64 | 	}
 | 
        
           |  |  | 65 |   | 
        
           |  |  | 66 | 	/* guidelines pour des tests pérennes:
 | 
        
           |  |  | 67 | 	 * - Utiliser ordre=asc pour prendre les enregistrement les plus anciens, moins susceptibles de changer
 | 
        
           |  |  | 68 | 	 * - Restreindre par date (évite les date_transmission = NULL, qui, après des GROUP BY affectent le result-set
 | 
        
           |  |  | 69 | 	 * - Utiliser moins de "$limite" résultats à cause de la sélection (arbitraire[GROUP-BY])) des obs ayant la même date */
 | 
        
           | 1372 | raphael | 70 | 	public function testMasque() {
 | 
        
           |  |  | 71 | 		$url = $this->creerUrl('observations');
 | 
        
           |  |  | 72 | 		$i = new Observations();
 | 
        
           | 1378 | raphael | 73 | 		$retour = $this->consulterDirectJson($i->consulter([], ['navigation.depart'=>0,'navigation.limite'=>12,'ordre'=>'asc','masque'=>'Grand','masque.date'=>2009]),
 | 
        
           | 1372 | raphael | 74 | 											$url);
 | 
        
           | 1378 | raphael | 75 | 		// from
 | 
        
           |  |  | 76 | 		// "http://www.tela-botanica.org/eflore/del/services/0.1/observations?navigation.depart=0&navigation.limite=12&masque=Grand&masque.date=2009&ordre=asc"
 | 
        
           |  |  | 77 | 		$expected = json_decode(file_get_contents("masque=G-date=2009.data.json"), true);
 | 
        
           | 1374 | raphael | 78 |   | 
        
           |  |  | 79 | 		self::ignoreNullValuesAndSort($expected);
 | 
        
           |  |  | 80 | 		self::ignoreNullValuesAndSort($retour);
 | 
        
           |  |  | 81 |   | 
        
           |  |  | 82 | 		// echo implode(',', array_keys($retour['resultats'])) . "\n" . implode(',', array_keys($expected['resultats']));die;
 | 
        
           | 1378 | raphael | 83 | 		$this->clefsIdentiques($expected, $retour, $url);
 | 
        
           | 1374 | raphael | 84 | 		$this->assertEquals($expected['resultats'], $retour['resultats'], "Différences dans le tableau, $url");
 | 
        
           | 1372 | raphael | 85 | 	}
 | 
        
           |  |  | 86 |   | 
        
           | 1378 | raphael | 87 | 	public function testType() {
 | 
        
           |  |  | 88 | 		@$url = $this->creerUrl('observations');
 | 
        
           |  |  | 89 | 		$i = new Observations();
 | 
        
           |  |  | 90 | 		$retour = $this->consulterDirectJson($i->consulter([], ['ordre'=>'asc','masque.type'=>'endiscussion']),
 | 
        
           |  |  | 91 | 											$url);
 | 
        
           |  |  | 92 | 		if(Config::get('nb_commentaires_discussion') != 1) {
 | 
        
           |  |  | 93 | 			printf("can't do test: Config::get('nb_commentaires_discussion') == %d <> 1\n", Config::get('nb_commentaires_discussion'));
 | 
        
           |  |  | 94 | 			return;
 | 
        
           |  |  | 95 | 		}
 | 
        
           |  |  | 96 |   | 
        
           |  |  | 97 | 		// from
 | 
        
           |  |  | 98 | 		// "http://www.tela-botanica.org/eflore/del/services/0.1/observations?masque.type=endiscussion&ordre=asc"
 | 
        
           |  |  | 99 | 		$expected = json_decode(file_get_contents("masque.type=endiscussion.data.json"), true);
 | 
        
           |  |  | 100 |   | 
        
           |  |  | 101 | 		self::ignoreNullValuesAndSort($expected);
 | 
        
           |  |  | 102 | 		self::ignoreNullValuesAndSort($retour);
 | 
        
           |  |  | 103 |   | 
        
           |  |  | 104 | 		// echo implode(',', array_keys($retour['resultats'])) . "\n" . implode(',', array_keys($expected['resultats']));die;
 | 
        
           |  |  | 105 | 		$this->clefsIdentiques($expected, $retour, $url);
 | 
        
           |  |  | 106 | 		$this->assertEquals($expected['resultats'], $retour['resultats'], "Différences dans le tableau, $url");
 | 
        
           |  |  | 107 | 	}
 | 
        
           |  |  | 108 |   | 
        
           |  |  | 109 |   | 
        
           | 1372 | raphael | 110 | 	public function testMasqueEtType() {
 | 
        
           | 1374 | raphael | 111 | 		@$url = $this->creerUrl('observations');
 | 
        
           | 1372 | raphael | 112 | 		$i = new Observations();
 | 
        
           |  |  | 113 | 		$retour = $this->consulterDirectJson($i->consulter([], ['navigation.depart'=>0,'navigation.limite'=>12,'ordre'=>'asc','masque'=>'G','masque.type'=>'endiscussion']),
 | 
        
           |  |  | 114 | 											$url);
 | 
        
           | 1374 | raphael | 115 |   | 
        
           | 1378 | raphael | 116 | 		$expected = json_decode(file_get_contents("masque=G-masque.type=endiscussion.data.json"), true);
 | 
        
           |  |  | 117 |   | 
        
           | 1374 | raphael | 118 | 		self::ignoreNullValuesAndSort($expected);
 | 
        
           |  |  | 119 | 		self::ignoreNullValuesAndSort($retour);
 | 
        
           |  |  | 120 |   | 
        
           |  |  | 121 | 		// echo implode(',', array_keys($retour['resultats'])) . "\n" . implode(',', array_keys($expected['resultats']));die;
 | 
        
           | 1378 | raphael | 122 | 		$this->clefsIdentiques($expected, $retour, $url);
 | 
        
           | 1374 | raphael | 123 | 		$this->assertEquals($expected['resultats'], $retour['resultats'], "Différences dans le tableau, $url");
 | 
        
           | 1372 | raphael | 124 | 	}
 | 
        
           |  |  | 125 |   | 
        
           |  |  | 126 |   | 
        
           |  |  | 127 |   | 
        
           | 1361 | raphael | 128 | 	public function hasKeys($arr, $keys, $url) {
 | 
        
           |  |  | 129 | 		foreach($keys as $k) {
 | 
        
           |  |  | 130 | 			$this->assertArrayHasKey($k, $arr, "attribut {$k} manquant. Voir : $url");
 | 
        
           |  |  | 131 | 		}
 | 
        
           |  |  | 132 | 	}
 | 
        
           |  |  | 133 |   | 
        
           |  |  | 134 | 	public function hasKeysAndNotEmpty($arr, $keys, $url) {
 | 
        
           |  |  | 135 | 		foreach($keys as $k) {
 | 
        
           |  |  | 136 | 			$this->assertArrayHasKey($k, $arr, "attribut {$k} manquant. Voir : $url");
 | 
        
           |  |  | 137 | 			$this->assertNotEmpty($arr[$k], "attribut {$k} vide. Voir : $url");
 | 
        
           |  |  | 138 | 		}
 | 
        
           |  |  | 139 | 	}
 | 
        
           | 1374 | raphael | 140 |   | 
        
           |  |  | 141 | 	static function ignoreNullValuesAndSort(&$arr) {
 | 
        
           |  |  | 142 | 		$arr['resultats'] = array_map('array_filter', $arr['resultats']);
 | 
        
           | 1378 | raphael | 143 | 		ksort($arr['resultats']);
 | 
        
           | 1374 | raphael | 144 | 	}
 | 
        
           | 1378 | raphael | 145 |   | 
        
           |  |  | 146 | 	public function clefsIdentiques($e, $r, $url) {
 | 
        
           |  |  | 147 | 		$e = array_map(function($a) { return intval(trim($a, '"')); }, array_keys($e['resultats']));
 | 
        
           |  |  | 148 | 		sort($e, SORT_NUMERIC);
 | 
        
           |  |  | 149 | 		$r = array_map(function($a) { return intval(trim($a, '"')); }, array_keys($r['resultats']));
 | 
        
           |  |  | 150 | 		sort($r, SORT_NUMERIC);
 | 
        
           |  |  | 151 | 		$this->assertEquals($e, $r, "Différences dans les clefs du tableau, $url");
 | 
        
           |  |  | 152 | 	}
 | 
        
           |  |  | 153 |   | 
        
           | 1387 | raphael | 154 | 	// obs-1048891-masque.ns=Fleur+violette-masque.comune=Arette.data.json
 | 
        
           |  |  | 155 | 	// test la présence des commentaires en général
 | 
        
           |  |  | 156 |   | 
        
           |  |  | 157 | 	// obs-1047949-masque.ns=Echium+wildpretii+Pearson-masque.commune=Vilaflor.data.json
 | 
        
           |  |  | 158 | 	// test la présence des commentaires parents: "1047949"[commentaires][7730][nb_commentaires] == 1 ?
 | 
        
           |  |  | 159 |   | 
        
           |  |  | 160 | 	// obs-1043671-masque.ns=plante+rose-masque.departement=38451
 | 
        
           |  |  | 161 | 	// idem
 | 
        
           | 1361 | raphael | 162 | }
 |