1 |
fmichon |
9227 |
<?php |
2 |
|
|
/** |
3 |
|
|
* Ce script contient la définition de la classe 'ImportSpecificCommon'. |
4 |
|
|
* |
5 |
|
|
* @package openads |
6 |
|
|
* @version SVN : $Id$ |
7 |
|
|
*/ |
8 |
|
|
|
9 |
softime |
9282 |
require_once "../tests/resources/omtestcase.class.php"; |
10 |
fmichon |
9227 |
require_once "../obj/import_specific.class.php"; |
11 |
|
|
|
12 |
|
|
/** |
13 |
|
|
* Tests unitaires des contrôles et manipulations des données récupérées |
14 |
|
|
* depuis le fichier CSV; dans l'import spécifique de CSV |
15 |
|
|
*/ |
16 |
softime |
9282 |
abstract class ImportSpecificCommon extends OMTestCase { |
17 |
fmichon |
9227 |
|
18 |
|
|
/** |
19 |
|
|
* Méthode lancée en début de traitement |
20 |
|
|
*/ |
21 |
|
|
public function common_setUp() { |
22 |
softime |
9282 |
parent::common_setUp(); |
23 |
|
|
// |
24 |
fmichon |
9227 |
$this->init_data_test(); |
25 |
softime |
9282 |
// Instanciation de la classe *om_application* |
26 |
|
|
$this->f = $this->get_inst_om_application("admin", "admin"); |
27 |
|
|
// Instanciation de la classe à tester |
28 |
fmichon |
9227 |
$this->inst = new import_specific(); |
29 |
|
|
} |
30 |
|
|
|
31 |
|
|
/** |
32 |
|
|
* Méthode lancée en fin de traitement |
33 |
|
|
*/ |
34 |
|
|
public function common_tearDown() { |
35 |
softime |
9282 |
parent::common_tearDown(); |
36 |
|
|
// |
37 |
|
|
$this->clean_session(); |
38 |
fmichon |
9227 |
} |
39 |
|
|
|
40 |
|
|
/** |
41 |
|
|
* Jeu de données : deux tableaux pour chaque test : |
42 |
|
|
* colonnes et valeurs. |
43 |
|
|
* |
44 |
|
|
* Les colonnes sont commentées par un exemple du CSV comme suit : |
45 |
|
|
* COL (débute à 0) - Libellé header |
46 |
|
|
*/ |
47 |
|
|
function init_data_test() { |
48 |
|
|
|
49 |
|
|
// check_required() |
50 |
|
|
$this->required_column = array( |
51 |
|
|
// COL 0 - Type |
52 |
|
|
"type" => array( |
53 |
|
|
"require" => true, |
54 |
|
|
"header" => "Type", |
55 |
|
|
), |
56 |
|
|
// COL 1 - Numéro |
57 |
|
|
"numero" => array( |
58 |
|
|
"require" => true, |
59 |
|
|
"header" => "Numero", |
60 |
|
|
), |
61 |
|
|
// COL 5 - INSEE |
62 |
|
|
"insee" => array( |
63 |
|
|
"require" => false, |
64 |
|
|
"header" => "INSEE", |
65 |
|
|
), |
66 |
|
|
); |
67 |
|
|
$this->required_row_data = array( |
68 |
|
|
"type" => "", |
69 |
|
|
"numero" => "10", |
70 |
|
|
"insee" => "", |
71 |
|
|
); |
72 |
|
|
|
73 |
|
|
// check_type() |
74 |
|
|
$this->type_column = array( |
75 |
|
|
// COL 38 - Date envoi demande de pièces |
76 |
|
|
0 => array( |
77 |
|
|
"type" => "date", |
78 |
|
|
"header" => "Date envoi demande de pieces", |
79 |
|
|
), |
80 |
|
|
1 => array( |
81 |
|
|
"type" => "date", |
82 |
|
|
"header" => "Date envoi demande de pieces", |
83 |
|
|
), |
84 |
|
|
// COL 8 - Projet |
85 |
|
|
2 => array( |
86 |
|
|
"type" => "string", |
87 |
|
|
"header" => "Projet", |
88 |
|
|
), |
89 |
|
|
3 => array( |
90 |
|
|
"type" => "string", |
91 |
|
|
"header" => "Projet", |
92 |
|
|
), |
93 |
|
|
// COL 10 - Nb logements |
94 |
|
|
4 => array( |
95 |
|
|
"type" => "integer", |
96 |
|
|
"header" => "Nb logements", |
97 |
|
|
), |
98 |
|
|
5 => array( |
99 |
|
|
"type" => "integer", |
100 |
|
|
"header" => "Nb logements", |
101 |
|
|
), |
102 |
|
|
6 => array( |
103 |
|
|
"type" => "integer", |
104 |
|
|
"header" => "Nb logements", |
105 |
|
|
), |
106 |
|
|
// COL 11 - Surface terrain |
107 |
|
|
7 => array( |
108 |
|
|
"type" => "float", |
109 |
|
|
"header" => "Surface terrain", |
110 |
|
|
), |
111 |
|
|
8 => array( |
112 |
|
|
"type" => "float", |
113 |
|
|
"header" => "Surface terrain", |
114 |
|
|
), |
115 |
|
|
// COL 24 - Lotissement |
116 |
|
|
9 => array( |
117 |
|
|
"type" => "boolean", |
118 |
|
|
"header" => "Lotissement", |
119 |
|
|
), |
120 |
|
|
10 => array( |
121 |
|
|
"type" => "boolean", |
122 |
|
|
"header" => "Lotissement", |
123 |
|
|
), |
124 |
|
|
); |
125 |
|
|
$this->type_row_data = array( |
126 |
|
|
0 => "10/10/2015", |
127 |
|
|
1 => "101/5/2015", |
128 |
|
|
2 => "chaîne valide", |
129 |
|
|
4 => "50", |
130 |
|
|
5 => "50a", |
131 |
|
|
6 => "50,5", |
132 |
|
|
7 => "50,5", |
133 |
|
|
8 => "50a", |
134 |
|
|
9 => "Oui", |
135 |
|
|
10 => "true", |
136 |
|
|
); |
137 |
|
|
|
138 |
|
|
$this->type_return_expected = array( |
139 |
|
|
// Cas 1 : date valide |
140 |
|
|
0 =>true, |
141 |
|
|
// Cas 2 : date invalide |
142 |
|
|
1 =>false, |
143 |
|
|
// Cas 3 : chaîne de caractères valide |
144 |
|
|
2 =>true, |
145 |
|
|
// Cas 5 : nombre entier valide |
146 |
|
|
4 =>true, |
147 |
|
|
// Cas 6 : nombre entier invalide |
148 |
|
|
5 =>false, |
149 |
|
|
// Cas 7 : nombre entier invalide |
150 |
|
|
6 =>false, |
151 |
|
|
// Cas 8 : nombre décimal valide |
152 |
|
|
7 =>true, |
153 |
|
|
// Cas 9 : nombre décimal invalide |
154 |
|
|
8 =>false, |
155 |
|
|
// Cas 10 : booléen valide |
156 |
|
|
9 =>true, |
157 |
|
|
// Cas 11 : booléen invalide |
158 |
|
|
10 =>false |
159 |
|
|
); |
160 |
|
|
|
161 |
|
|
// check_foreign_key() |
162 |
softime |
12124 |
// TODO : correction temporaire |
163 |
|
|
// les clés étarngères ne sont plus récupérée de la même manière |
164 |
|
|
// la liste des valeurs possibles est définies dans la clé "link" |
165 |
|
|
// Les valeurs des clés étrangères sont récupérées via une requête |
166 |
|
|
// sql et stocké dans une variable de cache. |
167 |
fmichon |
9227 |
$this->fk_column = array( |
168 |
|
|
// COL 0 - Type |
169 |
|
|
0 => array( |
170 |
softime |
12124 |
"link" => array( |
171 |
|
|
"PC MI" => "PI", |
172 |
|
|
"PCMI" => "PI", |
173 |
|
|
"PCI" => "PI", |
174 |
|
|
"PA PC" => "PA", |
175 |
|
|
"PC PD" => "PC", |
176 |
|
|
"PA PD" => "PA", |
177 |
|
|
"IA" => "DIA", |
178 |
|
|
), |
179 |
fmichon |
9227 |
"foreign_key" => array( |
180 |
softime |
12124 |
"id" => "dossier_autorisation_type_detaille", |
181 |
|
|
"table" => "dossier_autorisation_type_detaille", |
182 |
|
|
"field" => "code", |
183 |
|
|
), |
184 |
|
|
"header" => "Type", |
185 |
|
|
), |
186 |
|
|
1 => array( |
187 |
|
|
"link" => array( |
188 |
|
|
"PC MI" => "PI", |
189 |
|
|
"PCMI" => "PI", |
190 |
|
|
"PCI" => "PI", |
191 |
|
|
"PA PC" => "PA", |
192 |
|
|
"PC PD" => "PC", |
193 |
|
|
"PA PD" => "PA", |
194 |
|
|
"IA" => "DIA", |
195 |
|
|
), |
196 |
|
|
"foreign_key" => array( |
197 |
|
|
"id" => "dossier_autorisation_type_detaille", |
198 |
|
|
"table" => "dossier_autorisation_type_detaille", |
199 |
|
|
"field" => "code", |
200 |
|
|
), |
201 |
|
|
"header" => "Type", |
202 |
|
|
), |
203 |
|
|
); |
204 |
|
|
|
205 |
|
|
/* |
206 |
|
|
$this->fk_column = array( |
207 |
|
|
// COL 0 - Type |
208 |
|
|
0 => array( |
209 |
|
|
"foreign_key" => array( |
210 |
fmichon |
9227 |
"sql"=>"SELECT dossier_autorisation_type_detaille FROM openads.dossier_autorisation_type_detaille WHERE code ='<value>'", |
211 |
|
|
"table" => "dossier_autorisation_type_detaille", |
212 |
|
|
"field" => "code", |
213 |
|
|
), |
214 |
|
|
"header" => "Type", |
215 |
|
|
), |
216 |
|
|
1 => array( |
217 |
|
|
"foreign_key" => array( |
218 |
|
|
"sql"=>"SELECT dossier_autorisation_type_detaille FROM openads.dossier_autorisation_type_detaille WHERE code ='<value>'", |
219 |
|
|
"table" => "dossier_autorisation_type_detaille", |
220 |
|
|
"field" => "code", |
221 |
|
|
), |
222 |
|
|
"header" => "Type", |
223 |
|
|
), |
224 |
softime |
12124 |
);*/ |
225 |
fmichon |
9227 |
$this->fk_row_data = array( |
226 |
|
|
0 => "PCI", |
227 |
|
|
1 => "lambda", |
228 |
|
|
); |
229 |
|
|
|
230 |
|
|
// set_linked_value() |
231 |
|
|
$this->linked_values_column = array( |
232 |
|
|
// COL 0 - Type |
233 |
|
|
0 => array( |
234 |
|
|
"link" => array( |
235 |
|
|
"PC MI" => "PI", |
236 |
|
|
), |
237 |
|
|
"header" => "Type", |
238 |
|
|
), |
239 |
|
|
1 => array( |
240 |
|
|
"link" => array( |
241 |
|
|
"PC MI" => "PI", |
242 |
|
|
), |
243 |
|
|
"header" => "Type", |
244 |
|
|
), |
245 |
|
|
); |
246 |
|
|
$this->linked_values_row_data = array( |
247 |
|
|
0 => "PCI", |
248 |
|
|
1 => "PC MI", |
249 |
|
|
); |
250 |
|
|
} |
251 |
|
|
|
252 |
|
|
/** |
253 |
|
|
* Teste la méthode check_required() de la classe import_specific. |
254 |
|
|
* Est-ce qu'il y a une valeur si champ obligatoire ? |
255 |
|
|
* |
256 |
|
|
* @param $key [integer] colonne du fichier CSV |
257 |
|
|
* @param $value [string] valeur à tester |
258 |
|
|
* @return [string] message d'erreur ou chaîne vide si succès |
259 |
|
|
*/ |
260 |
|
|
public function test_check_required() { |
261 |
|
|
// Message d'erreur |
262 |
|
|
$error = _("La colonne %s est obligatoire"); |
263 |
|
|
// Utilisation du jeu de données |
264 |
|
|
$this->inst->column = $this->required_column; |
265 |
|
|
$this->inst->line = $this->required_row_data; |
266 |
|
|
// Cas 1 : champ requis vide |
267 |
|
|
$ret = $this->inst->check_required("type"); |
268 |
|
|
$this->assertEquals($ret, false); |
269 |
|
|
// Cas 2 : champ requis renseigné |
270 |
|
|
$ret = $this->inst->check_required("numero"); |
271 |
|
|
$this->assertEquals($ret, true); |
272 |
|
|
// Cas 3 : champ non requis vide |
273 |
|
|
$ret = $this->inst->check_required("insee"); |
274 |
|
|
$this->assertEquals($ret, true); |
275 |
|
|
} |
276 |
|
|
|
277 |
|
|
/** |
278 |
softime |
12124 |
* Teste la méthode check_type() de la classe import_specific. |
279 |
|
|
* Est-ce que la valeur est valide pour le type attendu ? |
280 |
|
|
* |
281 |
|
|
* @param $key [integer] colonne du fichier CSV |
282 |
|
|
* @param $value [string] valeur à tester |
283 |
|
|
* @return [string] message d'erreur ou chaîne vide si succès |
284 |
|
|
*/ |
285 |
|
|
public function test_check_type() { |
286 |
|
|
|
287 |
|
|
// Utilisation du jeu de données |
288 |
|
|
$this->inst->column = $this->type_column; |
289 |
|
|
$this->inst->line = $this->type_row_data; |
290 |
|
|
// Définition des résultats attendus |
291 |
|
|
$this->type_return_expected; |
292 |
|
|
// Pour chaque colonne testée |
293 |
|
|
foreach ($this->inst->line as $key => $value) { |
294 |
|
|
// Vérification du retour de la méthode |
295 |
|
|
$this->assertEquals( |
296 |
|
|
$this->inst->check_type($key), |
297 |
|
|
$this->type_return_expected[$key] |
298 |
|
|
); |
299 |
|
|
} |
300 |
|
|
} |
301 |
|
|
|
302 |
|
|
/** |
303 |
|
|
* Teste la méthode get_foreign_key_id() de la classe import_specific. |
304 |
|
|
* Est-ce que la clé étrangère existe ? |
305 |
|
|
* |
306 |
|
|
* @param $key [integer] colonne du fichier CSV |
307 |
|
|
* @return [string] message d'erreur ou chaîne vide si succès |
308 |
|
|
*/ |
309 |
|
|
public function test_get_foreign_key_id() { |
310 |
|
|
|
311 |
|
|
// Utilisation du jeu de données |
312 |
|
|
$this->inst->column = $this->fk_column; |
313 |
|
|
$this->inst->line = $this->fk_row_data; |
314 |
|
|
// Chargement du cache pour toutes les clés étrangères |
315 |
|
|
$this->inst->load_foreign_keys(); |
316 |
|
|
// Cas 1 : clé étrangère valide |
317 |
|
|
$ret = $this->inst->get_foreign_key_id(0); |
318 |
|
|
$this->assertEquals($ret, true); |
319 |
|
|
// Cas 2 : clé étrangère invalide |
320 |
|
|
$ret = $this->inst->get_foreign_key_id(1); |
321 |
|
|
$this->assertEquals($ret, false); |
322 |
|
|
} |
323 |
|
|
|
324 |
|
|
/** |
325 |
|
|
* Teste la méthode explode_address() de la classe import_specific. |
326 |
|
|
* Doit retourner un tableau d'éléments composant l'adresse fournie. |
327 |
|
|
* |
328 |
|
|
* @param $value [string] adresse complète |
329 |
|
|
* @return [array] tableau (numéro, rue, CP, ville, ...) |
330 |
|
|
*/ |
331 |
|
|
public function test_explode_address() { |
332 |
|
|
// Cas 1 : adresse 'normale' à transformer |
333 |
|
|
$adresse_complete1 = "27 bis Avenue Jules Cantini 13008 Marseille France"; |
334 |
|
|
// Récupération de la transformation |
335 |
|
|
$adresse_triee1 = $this->inst->explode_address($adresse_complete1); |
336 |
|
|
// Vérification des éléments |
337 |
|
|
$this->assertEquals($adresse_triee1['numero'], '27bis'); |
338 |
|
|
$this->assertEquals($adresse_triee1['voie'], 'Avenue Jules Cantini'); |
339 |
|
|
$this->assertEquals($adresse_triee1['complement1'], ''); |
340 |
|
|
$this->assertEquals($adresse_triee1['complement2'], ''); |
341 |
|
|
$this->assertEquals($adresse_triee1['cp'], '13008'); |
342 |
|
|
$this->assertEquals($adresse_triee1['ville'], 'Marseille'); |
343 |
|
|
// Cas 2 : adresse longue (libellée voie supérieur à 30 caractères) |
344 |
|
|
$adresse_complete2 = "27 bis Avenue Jules Cantini Boulevard de la Blancarde 06001 Nice"; |
345 |
|
|
// Récupération de la transformation |
346 |
|
|
$adresse_triee2 = $this->inst->explode_address($adresse_complete2); |
347 |
|
|
// Vérification des éléments |
348 |
|
|
$this->assertEquals($adresse_triee2['numero'], '27bis'); |
349 |
|
|
$this->assertEquals($adresse_triee2['voie'], 'Avenue Jules Cantini Boulevard'); |
350 |
|
|
$this->assertEquals($adresse_triee2['complement1'], 'de la Blancarde'); |
351 |
|
|
$this->assertEquals($adresse_triee2['complement2'], ''); |
352 |
|
|
$this->assertEquals($adresse_triee2['cp'], '06001'); |
353 |
|
|
$this->assertEquals($adresse_triee2['ville'], 'Nice'); |
354 |
|
|
// Cas 3 : adresse très longue (libellée voie supérieur à 60 caractères) |
355 |
|
|
// Numéro composé suivi d'une virgule |
356 |
|
|
$adresse_complete3 = "11-12, Place des grands hommes Boulevard du Panthéon Route de Paris 13380 Plan de Cuques"; |
357 |
|
|
// Récupération de la transformation |
358 |
|
|
$adresse_triee3 = $this->inst->explode_address($adresse_complete3); |
359 |
|
|
// Vérification des éléments |
360 |
|
|
$this->assertEquals($adresse_triee3['numero'], '11-12'); |
361 |
|
|
$this->assertEquals($adresse_triee3['voie'], 'Place des grands hommes'); |
362 |
|
|
$this->assertEquals($adresse_triee3['complement1'], 'Boulevard du Panthéon Route'); |
363 |
|
|
$this->assertEquals($adresse_triee3['complement2'], 'de Paris'); |
364 |
|
|
$this->assertEquals($adresse_triee3['cp'], '13380'); |
365 |
|
|
$this->assertEquals($adresse_triee3['ville'], 'Plan de Cuques'); |
366 |
|
|
} |
367 |
|
|
|
368 |
|
|
/** |
369 |
fmichon |
9227 |
* Teste la méthode set_linked_value() qui subsitue les valeurs du CSV |
370 |
|
|
* par le paramétrage. |
371 |
|
|
* |
372 |
|
|
* @param $key [integer] colonne du fichier CSV |
373 |
|
|
* @return [void] |
374 |
|
|
*/ |
375 |
|
|
function test_set_linked_value(){ |
376 |
|
|
// Utilisation du jeu de données |
377 |
|
|
$this->inst->column = $this->linked_values_column; |
378 |
|
|
$this->inst->line = $this->linked_values_row_data; |
379 |
|
|
// Cas 1 : aucune correspondance |
380 |
|
|
// PCI reste PCI |
381 |
|
|
$this->inst->set_linked_value(0); |
382 |
|
|
$this->assertEquals($this->inst->line[0], "PCI"); |
383 |
|
|
// Cas 2 : existance d'une correspondance |
384 |
|
|
// PC MI devient PI |
385 |
|
|
$this->inst->set_linked_value(1); |
386 |
|
|
$this->assertEquals($this->inst->line[1], "PI"); |
387 |
|
|
} |
388 |
|
|
} |