Core  3.2
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
PU_test_dcp_importCsvDocuments.php
Go to the documentation of this file.
1 <?php
2 /*
3  * @author Anakeen
4  * @package FDL
5 */
6 
7 namespace Dcp\Pu;
8 /**
9  * @author Anakeen
10  * @package Dcp\Pu
11  */
12 
13 require_once 'PU_testcase_dcp.php';
14 
16 {
17  /**
18  * @param $fileName
19  * @param $separator
20  * @param $enclosure
21  * @param $expected
22  * @throws \Dcp\Exception
23  * @dataProvider dataImportCsvFamily
24  */
25  public function testImportCsvFamily($fileName, $separator, $enclosure, $famName, $expected)
26  {
27  $oImport = new \ImportDocument();
28  $oImport->setCsvOptions($separator, $enclosure);
29  $oImport->importDocuments(self::getAction() , "DCPTEST/" . $fileName);
30  $err = $oImport->getErrorMessage();
31  $this->assertEmpty($err, "import family error : $err");
32  $f = new_doc('', $famName);
33  $this->assertTrue($f->isAlive() , sprintf("family %s not found", $famName));
34  $this->assertEquals($expected["title"], $f->getTitle() , "incorrect family title");
35  foreach ($expected["alabel"] as $aid => $elabel) {
36 
37  $this->assertEquals($elabel, $f->getLabel($aid) , "incorrect attribute label");
38  }
39  foreach ($expected["doc"] as $k => $v) {
40 
41  $d = new_doc('', $v["name"]);
42  $this->assertTrue($d->isAlive() , sprintf("document %s not found", $v["name"]));
43  foreach ($v["values"] as $aid => $aval) {
44  $this->assertEquals($aval, $d->getRawValue($aid) , sprintf("incorrect attribute [%s] value : %s ", $aid, print_r($d->getValues() , true)));
45  }
46  }
47  }
48  /**
49  * @param $fileName
50  * @param $expectedSeparator
51  * @param $expectedEnclosure
52  * @dataProvider dataDetectCsvOptions
53  */
54  public function testDetectCsvOptions($fileName, $expectedSeparator, $expectedEnclosure)
55  {
56  $oImport = new \importDocumentDescription("DCPTEST/" . $fileName);
57  $options = $oImport->setCsvOptions('auto', 'auto');
58 
59  $this->assertEquals($expectedSeparator, $options["separator"], "incorrect csv separator");
60  $this->assertEquals($expectedEnclosure, $options["enclosure"], "incorrect csv enclosure");
61  }
62 
63  public function dataDetectCsvOptions()
64  {
65  return array(
66  array(
67  "PU_data_dcp_goodfamilyforcsvcommadoublequote1.csv",
68  ",",
69  '"'
70  ) ,
71  array(
72  "PU_data_dcp_goodfamilyforcsvsemicolonsinglequote1.csv",
73  ";",
74  "'"
75  ) ,
76  array(
77  "PU_data_dcp_goodfamilyforcsvcommadoublequote2.csv",
78  ",",
79  '"'
80  ) ,
81  array(
82  "PU_data_dcp_goodfamilyforcsvsemicolonsinglequote2.csv",
83  ";",
84  "'"
85  ) ,
86  array(
87  "PU_data_dcp_goodfamilyforcsvsemicolondoublequote1.csv",
88  ";",
89  '"'
90  ) ,
91  array(
92  "PU_data_dcp_goodfamilyforcsvsemicolondoublequote2.csv",
93  ";",
94  '"'
95  ) ,
96  array(
97  "PU_data_dcp_goodfamilyforcsvsemicolonsinglequote2iso.csv",
98  ";",
99  "'"
100  ) ,
101  array(
102  "PU_data_dcp_goodfamilyforcsvsemicolon.csv",
103  ";",
104  ""
105  )
106  );
107  }
108  public function dataImportCsvFamily()
109  {
110  return array(
111 
112  array(
113  "file" => "PU_data_dcp_goodfamilyforcsvsemicolon.csv",
114  "separator" => ";",
115  "enclosure" => '',
116  "famname" => "TST_GOODFAMIMPCSVSEMICOLON
117  ",
118  "expect" => array(
119  "title" => 'Test Famille, "Csv"',
120  "alabel" => array(
121  "tst_text" => 'Texte "principal"',
122  "tst_date" => "Date, 'principale'"
123  ) ,
124  "doc" => array(
125  array(
126  "name" => "TST_CVSSEMICOLON1",
127  "values" => array(
128  "tst_title" => "Hello",
129  "tst_text" => "The world",
130  "tst_coltext" => "Un\nDeux",
131  "tst_coldate" => "2012-02-17\n2013-06-12"
132  )
133  )
134  )
135  )
136  ) ,
137  array(
138  "file" => "PU_data_dcp_goodfamilyforcsvcommadoublequote1.csv",
139  "separator" => ",",
140  "enclosure" => '"',
141  "famname" => "TST_GOODFAMIMPCSVCOMMA1",
142  "expect" => array(
143  "title" => 'Test Famille, "Csv"',
144  "alabel" => array(
145  "tst_text" => 'Texte "principal"',
146  "tst_date" => "Date, 'principale'"
147  ) ,
148  "doc" => array(
149  array(
150  "name" => "TST_CVSCOMMA11",
151  "values" => array(
152  "tst_title" => "Hello",
153  "tst_text" => "The world",
154  "tst_coltext" => "Un\nDeux",
155  "tst_coldate" => "2012-02-17\n2013-06-12"
156  )
157  ) ,
158  array(
159  "name" => "TST_CVSCOMMA12",
160  "values" => array(
161  "tst_title" => "Virgule , et ;",
162  "tst_text" => 'The "world" end "earth"',
163  "tst_coltext" => "Un\nDeux",
164  "tst_coldate" => "2012-02-18\n2013-10-12"
165  )
166  )
167  )
168  )
169  ) ,
170 
171  array(
172  "file" => "PU_data_dcp_goodfamilyforcsvcommadoublequote2.csv",
173  "separator" => ",",
174  "enclosure" => '"',
175  "famname" => "TST_GOODFAMIMPCSVCOMMA2",
176  "expect" => array(
177  "title" => 'Test Famille, "Csv"',
178  "alabel" => array(
179  "tst_text" => 'Texte "principal"',
180  "tst_date" => "Date, 'principale'"
181  ) ,
182  "doc" => array(
183  array(
184  "name" => "TST_CVSCOMMA21",
185  "values" => array(
186  "tst_title" => "Hello",
187  "tst_text" => "The world",
188  "tst_coltext" => "Un\nDeux",
189  "tst_coldate" => "2012-02-17\n2013-06-12"
190  )
191  ) ,
192  array(
193  "name" => "TST_CVSCOMMA22",
194  "values" => array(
195  "tst_title" => "Virgule , et ;",
196  "tst_text" => 'The "world" end "earth"',
197  "tst_coltext" => "Un\nDeux",
198  "tst_coldate" => "2012-02-18\n2013-10-12"
199  )
200  )
201  )
202  )
203  ) ,
204  array(
205  "file" => "PU_data_dcp_goodfamilyforcsvsemicolonsinglequote1.csv",
206  "separator" => ";",
207  "enclosure" => "'",
208  "famname" => "TST_GOODFAMIMPCSVSEMICOLON1",
209  "expect" => array(
210  "title" => 'Test Famille, "Csv"',
211  "alabel" => array(
212  "tst_text" => 'Texte "principal"',
213  "tst_date" => "Date, 'principale'"
214  ) ,
215  "doc" => array(
216  array(
217  "name" => "TST_CSVSEMICOLON11",
218  "values" => array(
219  "tst_title" => "Hello",
220  "tst_text" => "The world",
221  "tst_coltext" => "Un\nDeux",
222  "tst_coldate" => "2012-02-17\n2013-06-12"
223  )
224  ) ,
225  array(
226  "name" => "TST_CSVSEMICOLON12",
227  "values" => array(
228  "tst_title" => "Virgule , et ;",
229  "tst_text" => "L'être ou le n°3\nAccentué : ça c'est fait",
230  "tst_coltext" => "Un\nDeux\nTrois",
231  "tst_coldate" => "2012-02-18\n2013-10-12\n"
232  )
233  ) ,
234  array(
235  "name" => "TST_CSVSEMICOLON13",
236  "values" => array(
237  "tst_title" => 'quote \' double " point-virgule ; et virgule ,',
238  "tst_text" => "The \"world\" \nis beautiful\nisn't it",
239  "tst_coltext" => "Un\nDeux\nTrois",
240  "tst_coldate" => "2012-02-18\n2013-10-12\n"
241  )
242  )
243  )
244  )
245  ) ,
246  array(
247  "file" => "PU_data_dcp_goodfamilyforcsvsemicolonsinglequote2.csv",
248  "separator" => ";",
249  "enclosure" => "'",
250  "famname" => "TST_GOODFAMIMPCSVSEMICOLON2",
251  "expect" => array(
252  "title" => 'Test Famille, "Csv"',
253  "alabel" => array(
254  "tst_text" => 'Texte "principal"',
255  "tst_date" => "Date, 'principale'"
256  ) ,
257  "doc" => array(
258  array(
259  "name" => "TST_CSVSEMICOLON21",
260  "values" => array(
261  "tst_title" => "Hello",
262  "tst_text" => "The world",
263  "tst_coltext" => "Un\nDeux",
264  "tst_coldate" => "2012-02-17\n2013-06-12"
265  )
266  ) ,
267  array(
268  "name" => "TST_CSVSEMICOLON22",
269  "values" => array(
270  "tst_title" => "Virgule , et ;",
271  "tst_text" => "L'être ou le n°3\nAccentué : ça c'est fait",
272  "tst_coltext" => "Un\nDeux\nTrois",
273  "tst_coldate" => "2012-02-18\n2013-10-12\n"
274  )
275  ) ,
276  array(
277  "name" => "TST_CSVSEMICOLON23",
278  "values" => array(
279  "tst_title" => 'quote \' double " point-virgule ; et virgule ,',
280  "tst_text" => "The \"world\" \nis beautiful\nisn't it",
281  "tst_coltext" => "Un\nDeux\nTrois",
282  "tst_coldate" => "2012-02-18\n2013-10-12\n"
283  )
284  )
285  )
286  )
287  ) ,
288  array(
289  "file" => "PU_data_dcp_goodfamilyforcsvsemicolonsinglequote2iso.csv",
290  "separator" => ";",
291  "enclosure" => "'",
292  "famname" => "TST_GOODFAMIMPCSVSEMICOLON5",
293  "expect" => array(
294  "title" => 'Test Famille, "Csv"',
295  "alabel" => array(
296  "tst_text" => 'Texte "principal"',
297  "tst_date" => "Date, 'principale'"
298  ) ,
299  "doc" => array(
300  array(
301  "name" => "TST_CSVSEMICOLON51",
302  "values" => array(
303  "tst_title" => "Hello",
304  "tst_text" => "The world",
305  "tst_coltext" => "Un\nDeux",
306  "tst_coldate" => "2012-02-17\n2013-06-12"
307  )
308  ) ,
309  array(
310  "name" => "TST_CSVSEMICOLON52",
311  "values" => array(
312  "tst_title" => "Virgule , et ;",
313  "tst_text" => "L'être ou le n°3\nAccentué : ça c'est fait",
314  "tst_coltext" => "Un\nDeux\nTrois",
315  "tst_coldate" => "2012-02-18\n2013-10-12\n"
316  )
317  ) ,
318  array(
319  "name" => "TST_CSVSEMICOLON53",
320  "values" => array(
321  "tst_title" => 'quote \' double " point-virgule ; et virgule ,',
322  "tst_text" => "The \"world\" \nis beautiful\nisn't it",
323  "tst_coltext" => "Un\nDeux\nTrois",
324  "tst_coldate" => "2012-02-18\n2013-10-12\n"
325  )
326  )
327  )
328  )
329  ) ,
330  array(
331  "file" => "PU_data_dcp_goodfamilyforcsvsemicolondoublequote1.csv",
332  "separator" => ";",
333  "enclosure" => '"',
334  "famname" => "TST_GOODFAMIMPCSVSEMICOLON3",
335  "expect" => array(
336  "title" => 'Test Famille, "Csv"',
337  "alabel" => array(
338  "tst_text" => 'Texte "principal"',
339  "tst_date" => "Date, 'principale'"
340  ) ,
341  "doc" => array(
342  array(
343  "name" => "TST_CSVSEMICOLON31",
344  "values" => array(
345  "tst_title" => "Hello",
346  "tst_text" => "The world",
347  "tst_coltext" => "Un\nDeux",
348  "tst_coldate" => "2012-02-17\n2013-06-12"
349  )
350  ) ,
351  array(
352  "name" => "TST_CSVSEMICOLON32",
353  "values" => array(
354  "tst_title" => "Virgule , et ;",
355  "tst_text" => "L'être ou le n°3\nAccentué : ça c'est fait",
356  "tst_coltext" => "Un\nDeux\nTrois",
357  "tst_coldate" => "2012-02-18\n2013-10-12\n"
358  )
359  ) ,
360  array(
361  "name" => "TST_CSVSEMICOLON33",
362  "values" => array(
363  "tst_title" => 'quote \' double " point-virgule ; et virgule ,',
364  "tst_text" => "The \"world\" \nis beautiful\nisn't it",
365  "tst_coltext" => "Un\nDeux\nTrois",
366  "tst_coldate" => "2012-02-18\n2013-10-12\n"
367  )
368  )
369  )
370  )
371  ) ,
372  array(
373  "file" => "PU_data_dcp_goodfamilyforcsvsemicolondoublequote2.csv",
374  "separator" => ";",
375  "enclosure" => '"',
376  "famname" => "TST_GOODFAMIMPCSVSEMICOLON4",
377  "expect" => array(
378  "title" => 'Test Famille, "Csv"',
379  "alabel" => array(
380  "tst_text" => 'Texte "principal"',
381  "tst_date" => "Date, 'principale'"
382  ) ,
383  "doc" => array(
384  array(
385  "name" => "TST_CSVSEMICOLON41",
386  "values" => array(
387  "tst_title" => "Hello",
388  "tst_text" => "The world",
389  "tst_coltext" => "Un\nDeux",
390  "tst_coldate" => "2012-02-17\n2013-06-12"
391  )
392  ) ,
393  array(
394  "name" => "TST_CSVSEMICOLON42",
395  "values" => array(
396  "tst_title" => "Virgule , et ;",
397  "tst_text" => "L'être ou le n°3\nAccentué : ça c'est fait",
398  "tst_coltext" => "Un\nDeux\nTrois",
399  "tst_coldate" => "2012-02-18\n2013-10-12\n"
400  )
401  ) ,
402  array(
403  "name" => "TST_CSVSEMICOLON43",
404  "values" => array(
405  "tst_title" => 'quote \' double " point-virgule ; et virgule ,',
406  "tst_text" => "The \"world\" \nis beautiful\nisn't it",
407  "tst_coltext" => "Un\nDeux\nTrois",
408  "tst_coldate" => "2012-02-18\n2013-10-12\n"
409  )
410  )
411  )
412  )
413  ) ,
414  array(
415  "file" => "PU_data_dcp_goodfamilyforcsvcommai.csv",
416  "separator" => ",",
417  "enclosure" => "i",
418  "famname" => "TST_GOODFAMIMPCSVSEMICOMMAI",
419  "expect" => array(
420  "title" => 'Test Famille, "Csv"',
421  "alabel" => array(
422  "tst_text" => 'Texte "principal"',
423  "tst_date" => "Date, 'principale'"
424  ) ,
425  "doc" => array(
426  array(
427  "name" => "TST_CVSSEMICOMMAI1",
428  "values" => array(
429  "tst_title" => "Hello",
430  "tst_text" => "The i world",
431  "tst_coltext" => "i\nii\niii\niv",
432  "tst_coldate" => "2012-02-17\n2013-06-12\n2012-12-17\n2013-06-17"
433  )
434  ) ,
435  array(
436  "name" => "TST_CVSSEMICOMMAI2",
437  "values" => array(
438  "tst_title" => "Le monde des i's",
439  "tst_text" => "The \"i world\" \nis beautiful\nisn't it",
440  "tst_coltext" => "i\nii\niii",
441  "tst_coldate" => "2012-02-18\n2013-10-12\n2013-10-13"
442  )
443  )
444  )
445  )
446  ) ,
447  array(
448  "file" => "PU_data_dcp_goodfamilyforcsvcommadoublequote2iso.csv",
449  "separator" => "auto",
450  "enclosure" => 'auto',
451  "famname" => "TST_GOODFAMIMPCSVCOMMA3",
452  "expect" => array(
453  "title" => 'Test Famille, "Csv"',
454  "alabel" => array(
455  "tst_text" => 'Texte "principal"',
456  "tst_date" => "Date, 'principale'"
457  ) ,
458  "doc" => array(
459  array(
460  "name" => "TST_CVSCOMMA31",
461  "values" => array(
462  "tst_title" => "Hello",
463  "tst_text" => "Le monde à l'été",
464  "tst_coltext" => "Un\nDeux",
465  "tst_coldate" => "2012-02-17\n2013-06-12"
466  )
467  ) ,
468  array(
469  "name" => "TST_CVSCOMMA32",
470  "values" => array(
471  "tst_title" => "Virgule , et ;",
472  "tst_text" => 'The "world" end "earth"',
473  "tst_coltext" => "Un\nDeux",
474  "tst_coldate" => "2012-02-18\n2013-10-12"
475  )
476  )
477  )
478  )
479  ) ,
480  );
481  }
482 }
483 ?>
testImportCsvFamily($fileName, $separator, $enclosure, $famName, $expected)
testDetectCsvOptions($fileName, $expectedSeparator, $expectedEnclosure)
$d
Definition: dav.php:77
if($file) if($subject==""&&$file) if($subject=="") $err
if($dirid) $oImport
← centre documentaire © anakeen