Core  3.2
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
PU_test_dcp_exportcollection.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 require_once 'PU_testcase_dcp_commonfamily.php';
11 {
12  static function getCommonImportFile()
13  {
14  return array(
15  "PU_data_dcp_exportcollection.ods",
16  "PU_data_dcp_document_exportcollection.xml"
17  );
18  }
19 
20  protected $famName = "tst_expcoll1";
21  /**
22  * @param $separator
23  * @param $enclosure
24  * @param array $expectedData
25  * @dataProvider dataExportCsv
26  */
27  public function testExportRawCsv($separator, $enclosure, array $expectedData)
28  {
29  $outFile = tempnam(getTmpDir() , 'tstexport');
30  $s = new \SearchDoc(self::$dbaccess, $this->famName);
31  $s->setObjectReturn();
32  $s->search();
33 
34  $this->assertEmpty($s->searchError() , sprintf("Error in search %s", print_r($s->getSearchInfo() , true)));
35 
36  $ec = new \Dcp\ExportCollection();
37 
38  $ec->setCvsEnclosure($enclosure);
39  $ec->setCvsSeparator($separator);
40  $ec->setOutputFilePath($outFile);
41  $ec->setDocumentlist($s->getDocumentList());
42  $ec->export();
43 
44  $this->assertTrue(filesize($outFile) > 0, sprintf("\"%s\" file not produced", $outFile));
45 
46  $this->verifyCsvContains($outFile, $separator, $enclosure, $expectedData, 2);
47  }
48  /**
49  * @param $format
50  * @param $separator
51  * @param $enclosure
52  * @param array $expectedData
53  * @dataProvider dataExportDisplayCsv
54  */
55  public function testExportDisplayCsv($format, $separator, $enclosure, array $expectedData)
56  {
57  $outFile = tempnam(getTmpDir() , 'tstexport');
58  $s = new \SearchDoc(self::$dbaccess, $this->famName);
59  $s->setObjectReturn();
60  $s->search();
61 
62  $this->assertEmpty($s->searchError() , sprintf("Error in search %s", print_r($s->getSearchInfo() , true)));
63 
64  $ec = new \Dcp\ExportCollection();
65 
66  $ec->setCvsEnclosure($enclosure);
67  $ec->setCvsSeparator($separator);
68  $ec->setOutputFilePath($outFile);
69  $ec->setOutputFormat($format);
70  $ec->setDocumentlist($s->getDocumentList());
71  $ec->export();
72 
73  $this->assertTrue(filesize($outFile) > 0, sprintf("\"%s\" file not produced", $outFile));
74  $this->verifyCsvContains($outFile, $separator, $enclosure, $expectedData, 0);
75  }
76 
77  protected function verifyCsvContains($outFile, $separator, $enclosure, $expectedData, $columnId)
78  {
79  $results = fopen($outFile, "r");
80  $resultData = array();
81  while (($data = fgetcsv($results, 1000, $separator, $enclosure)) !== FALSE) {
82  $docName = $data[$columnId];
83  $resultData[$docName] = $data;
84  }
85  fclose($results);
86  foreach ($expectedData as $docName => $docValues) {
87  $this->assertTrue(isset($resultData[$docName]) , sprintf("%s document not found : %s", $docName, print_r($resultData, true)));
88  foreach ($docValues as $index => $value) {
89  if (strpos($value, "*") === false) {
90  $this->assertEquals($value, $resultData[$docName][$index], sprintf("%s (index %s) : %s \n %s", $docName, $index, print_r($resultData, true) , $outFile));
91  } else {
92  $this->assertEquals(preg_match('/' . $value . '/', $resultData[$docName][$index]) , 1, sprintf("expected \"%s\" %s (index %s) : %s \n %s", $value, $docName, $index, print_r($resultData, true) , $outFile));
93  }
94  }
95  }
96  }
97  /**
98  * @dataProvider dataExportXmlSingle
99  */
100  public function testExportXmlSingle(array $expectedData)
101  {
102  $outFile = tempnam(getTmpDir() , 'tstexport');
103  $s = new \SearchDoc(self::$dbaccess, $this->famName);
104  $s->setObjectReturn();
105  $s->search();
106 
107  $this->assertEmpty($s->searchError() , sprintf("Error in search %s", print_r($s->getSearchInfo() , true)));
108 
109  $ec = new \Dcp\ExportCollection();
110 
111  $ec->setOutputFilePath($outFile);
112  $ec->setOutputFormat(\Dcp\ExportCollection::xmlFileOutputFormat);
113  $ec->setDocumentlist($s->getDocumentList());
114  $ec->export();
115 
116  $this->assertTrue(filesize($outFile) > 0, sprintf("\"%s\" file not produced", $outFile));
117 
118  $dom = new \DOMDocument();
119  $dom->load($outFile);
120 
121  $this->XPathTesting($dom, $expectedData);
122  }
123  /**
124  * @dataProvider dataExportXmlArchive
125  */
126  public function testExportXmlArchive($file, array $xmlPathes)
127  {
128  $outFile = tempnam(getTmpDir() , 'tstexport');
129  $s = new \SearchDoc(self::$dbaccess, $this->famName);
130  $s->setObjectReturn();
131  $s->search();
132 
133  $this->assertEmpty($s->searchError() , sprintf("Error in search %s", print_r($s->getSearchInfo() , true)));
134 
135  $ec = new \Dcp\ExportCollection();
136 
137  $ec->setOutputFilePath($outFile);
138  $ec->setOutputFormat(\Dcp\ExportCollection::xmlArchiveOutputFormat);
139  $ec->setDocumentlist($s->getDocumentList());
140  $ec->export();
141 
142  $this->assertTrue(filesize($outFile) > 0, sprintf("\"%s\" file not produced", $outFile));
143 
144  $outDir = tempnam(getTmpDir() , 'tstextract');
145  if (is_file($outDir)) {
146  unlink($outDir);
147  }
148  mkdir($outDir);
149  $zip = new \ZipArchive;
150  $res = $zip->open($outFile);
151 
152  $this->assertTrue($res, sprintf("\"%s\" cannot unarchive", $outFile));
153  for ($i = 0; $i < $zip->numFiles; $i++) {
154  $stat = $zip->statIndex($i);
155  if (preg_match("/" . $file . "/", basename($stat['name']))) {
156  $file = $stat['name'];
157  break;
158  }
159  }
160 
161  $zip->extractTo($outDir, array(
162  $file
163  ));
164  $zip->close();
165 
166  $xmlFile = sprintf("%s/%s", $outDir, $file);
167  $this->assertTrue(is_file($xmlFile) , sprintf("\"%s\" zip content not found", $xmlFile));
168 
169  $dom = new \DOMDocument();
170  $dom->load($xmlFile);
171 
172  $this->XPathTesting($dom, $xmlPathes);
173  }
174 
175  protected function XPathTesting(\DOMDocument $dom, array $expectedValues)
176  {
177 
178  $xp = new \DOMXpath($dom);
179  foreach ($expectedValues as $path => $value) {
180  $entries = $xp->query($path);
181  $found = 0;
182  $foundValues = array();
183  if (is_array($value)) {
184  foreach ($entries as $entry) {
185  if ($entry->nodeValue == $value) {
186  $found++;
187  }
188  $foundValues[] = $entry->nodeValue;
189  }
190  $this->assertEquals($value, $foundValues, sprintf("Item \"%s\" not found in %s path, found \n\t%s\n", print_r($value, true) , $path, implode("\n\t", $foundValues)));
191  } else {
192  foreach ($entries as $entry) {
193  if ($entry->nodeValue == $value) $found++;
194  $foundValues[] = $entry->nodeValue;
195  }
196  $this->assertGreaterThan(0, $found, sprintf("Item \"%s\" not found in %s path, found \n\t%s\n", $value, $path, implode("\n\t", $foundValues)));
197  }
198  }
199  }
200  /**
201  * @param $separator
202  * @param $enclosure
203  * @param array $expectedData
204  * @dataProvider dataExportProfilCsv
205  */
206  public function testExportProfilCsv($separator, $enclosure, array $expectedData)
207  {
208  $outFile = tempnam(getTmpDir() , 'tstexport');
209  $s = new \SearchDoc(self::$dbaccess, $this->famName);
210  $s->setObjectReturn();
211  $s->search();
212 
213  $this->assertEmpty($s->searchError() , sprintf("Error in search %s", print_r($s->getSearchInfo() , true)));
214 
215  $ec = new \Dcp\ExportCollection();
216 
217  $ec->setCvsEnclosure($enclosure);
218  $ec->setCvsSeparator($separator);
219  $ec->setOutputFilePath($outFile);
220  $ec->setDocumentlist($s->getDocumentList());
221  $ec->setExportProfil(true);
222  $ec->export();
223  $this->assertTrue(filesize($outFile) > 0, sprintf("\"%s\" file not produced", $outFile));
224  $results = fopen($outFile, "r");
225  $resultData = array();
226  while (($data = fgetcsv($results, 1000, $separator, $enclosure)) !== FALSE) {
227  if ($data[0] === "PROFIL") {
228  $docName = $data[1];
229  $resultData[$docName] = $data;
230  }
231  }
232  fclose($results);
233  foreach ($expectedData as $docName => $docValues) {
234  $this->assertTrue(isset($resultData[$docName]) , sprintf("%s document not found : %s", $docName, print_r($resultData, true)));
235  foreach ($docValues as $index => $value) {
236  $this->assertEquals($value, $resultData[$docName][$index], sprintf("%s (index %s) : %s", $docName, $index, print_r($resultData, true)));
237  }
238  }
239  }
240  /**
241  * @param $separator
242  * @param $enclosure
243  * @param array $expectedData
244  * @dataProvider dataExportFileCsv
245  */
246  public function testExportFileCsv($separator, $enclosure, $file, array $expectedData)
247  {
248  $outFile = tempnam(getTmpDir() , 'tstexportfile');
249  $s = new \SearchDoc(self::$dbaccess, $this->famName);
250  $s->setObjectReturn();
251  $s->search();
252 
253  $this->assertEmpty($s->searchError() , sprintf("Error in search %s", print_r($s->getSearchInfo() , true)));
254 
255  $ec = new \Dcp\ExportCollection();
256 
257  $ec->setCvsEnclosure($enclosure);
258  $ec->setCvsSeparator($separator);
259  $ec->setOutputFilePath($outFile);
260  $ec->setDocumentlist($s->getDocumentList());
261  $ec->setExportFiles(true);
262  $ec->export();
263  $this->assertTrue(filesize($outFile) > 0, sprintf("\"%s\" file not produced", $outFile));
264 
265  $outDir = tempnam(getTmpDir() , 'tstextract');
266  if (is_file($outDir)) {
267  unlink($outDir);
268  }
269  mkdir($outDir);
270  $zip = new \ZipArchive;
271  $res = $zip->open($outFile);
272 
273  $this->assertTrue($res, sprintf("\"%s\" cannot unarchive", $outFile));
274  for ($i = 0; $i < $zip->numFiles; $i++) {
275  $stat = $zip->statIndex($i);
276  if (preg_match("/" . $file . "/", basename($stat['name']))) {
277  $file = $stat['name'];
278  break;
279  }
280  }
281 
282  $zip->extractTo($outDir, array(
283  $file
284  ));
285  $zip->close();
286 
287  $contentFile = sprintf("%s/%s", $outDir, $file);
288  $this->assertTrue(is_file($contentFile) , sprintf("\"%s\" zip content not found", $contentFile));
289 
290  $this->verifyCsvContains($contentFile, $separator, $enclosure, $expectedData, 2);
291  }
292  /**
293  * @param $separator
294  * @param $enclosure
295  * @param array $expectedData
296  * @dataProvider dataExportFamilyCsv
297  */
298  public function testExportFamilyCsv($separator, $enclosure, array $expectedData)
299  {
300  $outFile = tempnam(getTmpDir() , 'tstexport');
301  $s = new \SearchDoc(self::$dbaccess, $this->famName);
302  $s->setObjectReturn();
303  $s->search();
304 
305  $this->assertEmpty($s->searchError() , sprintf("Error in search %s", print_r($s->getSearchInfo() , true)));
306 
307  $ec = new \Dcp\ExportCollection();
308 
309  $ec->setCvsEnclosure($enclosure);
310  $ec->setCvsSeparator($separator);
311  $ec->setOutputFilePath($outFile);
312  $ec->setDocumentlist($s->getDocumentList());
313  $ec->export();
314 
315  $this->assertTrue(filesize($outFile) > 0, sprintf("\"%s\" file not produced", $outFile));
316 
317  $this->verifyCsvContains($outFile, $separator, $enclosure, $expectedData, 2);
318  }
319 
320  public function dataExportFamilyCsv()
321  {
322  return array(
323  array(
324  ";",
325  '"',
326  array(
327  "TST_EXPCOLL_DOC1" => array(
328  4 => "Titre 1",
329  5 => "1",
330  6 => "2014-02-23",
331  7 => "A",
332 
333  8 => "Un",
334  9 => "1.1",
335  10 => "Un long"
336  ) ,
337  "TST_EXPCOLL_DOC2" => array(
338  4 => "Titre 2",
339  5 => "2",
340  6 => "2014-12-24",
341  7 => "B",
342  8 => "Deux",
343  9 => "2.2",
344  10 => "Deux long",
345  11 => "TST_EXPCOLL_DOC1"
346  ) ,
347  "TST_EXPCOLL_DOC3" => array(
348  4 => "Titre 3",
349  11 => "TST_EXPCOLL_DOC1\nTST_EXPCOLL_DOC2"
350  )
351  )
352  )
353  );
354  }
355  public function dataExportFileCsv()
356  {
357  return array(
358  array(
359  ";",
360  '"',
361  "fdl.csv",
362  array(
363  "TST_EXPCOLL_DOC1" => array(
364  4 => "Titre 1",
365  5 => "1",
366  6 => "2014-02-23",
367  7 => "A",
368 
369  8 => "Un",
370  11 => "1.1",
371  12 => "Un long"
372  ) ,
373  "TST_EXPCOLL_DOC2" => array(
374  4 => "Titre 2",
375  5 => "2",
376  6 => "2014-12-24",
377  7 => "B",
378  8 => "Deux",
379  11 => "2.2",
380  12 => "Deux long",
381  13 => "TST_EXPCOLL_DOC1"
382  ) ,
383  "TST_EXPCOLL_DOC3" => array(
384  4 => "Titre 3",
385  13 => "TST_EXPCOLL_DOC1\nTST_EXPCOLL_DOC2"
386  ) ,
387  "TST_EXPCOLL_DOC6" => array(
388  4 => "Titre 6",
389  9 => ".*\\/Hello.txt",
390  10 => ".*\\/red.png"
391  )
392  )
393  )
394  );
395  }
396  public function dataExportProfilCsv()
397  {
398  return array(
399  array(
400  ",",
401  '"',
402  array(
403  "TST_PRF_EXPCOLL" => array(
404  2 => ":useAccount",
405  4 => "view=uexpcoll1",
406  5 => "edit=uexpcoll2"
407  ) ,
408  "TST_EXPCOLL_DOC1" => array(
409  2 => "TST_PRF_EXPCOLL"
410  ) ,
411  "TST_EXPCOLL_DOC2" => array(
412  2 => "TST_PRF_EXPCOLL"
413  ) ,
414  "TST_EXPCOLL_DOC3" => array(
415  2 => "TST_PRF_EXPCOLL"
416  ) ,
417  "TST_EXPCOLL_DOC4" => array(
418  2 => "TST_PRF_EXPCOLL"
419  ) ,
420  "TST_EXPCOLL_DOC5" => array(
421  2 => "TST_PRF_EXPCOLL"
422  ) ,
423  "TST_EXPCOLL_DOC6" => array(
424  2 => "TST_PRF_EXPCOLL"
425  )
426  )
427  )
428  );
429  }
430  public function dataExportXmlArchive()
431  {
432  return array(
433  array(
434  "Titre1.*.xml",
435  array(
436  "tst_frame1/tst_title" => "Titre 1",
437  "tst_frame1/tst_number" => "1",
438  "tst_frame1/tst_date" => "2014-02-23"
439  )
440  ) ,
441  array(
442  "Titre2.*.xml",
443  array(
444  "tst_frame1/tst_number" => "2",
445  "tst_frame1/tst_date" => "2014-12-24"
446  )
447  ) ,
448  array(
449  "Titre3.*.xml",
450  array(
451  "tst_frame1/tst_number" => "3",
452  "tst_tab_i/tst_frame2/tst_longtext" => "Trois long",
453  // "tst_tab_i/tst_frame2/tst_array/tst_othertexts" => "Une deuxième",
454  "tst_tab_i/tst_frame2/tst_array/tst_othertexts" => array(
455  "Une ligne<BR>avec retour",
456  "Une deuxième"
457  )
458  )
459  )
460  );
461  }
462  public function dataExportXmlSingle()
463  {
464  return array(
465  array(
466  array(
467  $this->famName . "[@name = \"TST_EXPCOLL_DOC1\"]/tst_frame1/tst_number" => "1",
468  $this->famName . "[@name = \"TST_EXPCOLL_DOC1\"]/tst_frame1/tst_date" => "2014-02-23",
469  $this->famName . "[@name = \"TST_EXPCOLL_DOC2\"]/tst_frame1/tst_number" => "2"
470  )
471  )
472  );
473  }
474  public function dataExportDisplayCsv()
475  {
476  return array(
477  array(
479  ";",
480  '"',
481  array(
482  "Titre 1" => array(
483  0 => "Titre 1",
484  1 => "1",
485  2 => "23/02/2014",
486  3 => "La",
487  4 => "Un",
488  5 => "1.1",
489  6 => "Un long"
490  ) ,
491  "Titre 2" => array(
492  0 => "Titre 2",
493  1 => "2",
494  2 => "24/12/2014",
495  3 => "Si",
496  4 => "Deux",
497  5 => "2.2",
498  6 => "Deux long",
499  7 => "Titre 1"
500  ) ,
501  "Titre 3" => array(
502  0 => "Titre 3",
503  7 => "Titre 1\nTitre 2",
504  8 => "Une ligne\navec retour\nUne deuxième"
505  )
506  )
507  ) ,
508  array(
509  \Dcp\ExportCollection::csvDisplayValueOutputFormat,
510  ",",
511  '"',
512  array(
513  "Titre 1" => array(
514  0 => "Titre 1",
515  1 => "1",
516  2 => "23/02/2014",
517  3 => "La",
518  4 => "Un",
519  5 => "1.1",
520  6 => "Un long"
521  ) ,
522  "Titre 2" => array(
523  0 => "Titre 2",
524  1 => "2",
525  2 => "24/12/2014",
526  3 => "Si",
527  4 => "Deux",
528  5 => "2.2",
529  6 => "Deux long"
530  )
531  )
532  ) ,
533  array(
535  ";",
536  '"',
537  array(
538  "Titre 1" => array(
539  0 => "Titre 1",
540  1 => "1",
541  2 => "2014-02-23",
542  3 => "A",
543  4 => "Un",
544  5 => "1.1",
545  6 => "Un long"
546  ) ,
547  "Titre 2" => array(
548  0 => "Titre 2",
549  1 => "2",
550  2 => "2014-12-24",
551  3 => "B",
552  4 => "Deux",
553  5 => "2.2",
554  6 => "Deux long"
555  ) ,
556  "Titre 3" => array(
557  0 => "Titre 3",
558  7 => "TST_EXPCOLL_DOC1\nTST_EXPCOLL_DOC2",
559  8 => "Une ligne<BR>avec retour\nUne deuxième"
560  )
561  )
562  )
563  );
564  }
565  public function dataExportCsv()
566  {
567  return array(
568  array(
569  ";",
570  '"',
571  array(
572  "TST_EXPCOLL_DOC1" => array(
573  4 => "Titre 1",
574  5 => "1",
575  6 => "2014-02-23",
576  7 => "A",
577 
578  8 => "Un",
579  9 => "1.1",
580  10 => "Un long"
581  ) ,
582  "TST_EXPCOLL_DOC2" => array(
583  4 => "Titre 2",
584  5 => "2",
585  6 => "2014-12-24",
586  7 => "B",
587  8 => "Deux",
588  9 => "2.2",
589  10 => "Deux long",
590  11 => "TST_EXPCOLL_DOC1"
591  ) ,
592  "TST_EXPCOLL_DOC3" => array(
593  4 => "Titre 3",
594  11 => "TST_EXPCOLL_DOC1\nTST_EXPCOLL_DOC2"
595  )
596  )
597  ) ,
598  array(
599  ",",
600  "'",
601  array(
602  "TST_EXPCOLL_DOC1" => array(
603  4 => "Titre 1",
604  5 => "1",
605  6 => "2014-02-23",
606  7 => "A",
607  8 => "Un",
608  9 => "1.1",
609  10 => "Un long"
610  ) ,
611  "TST_EXPCOLL_DOC2" => array(
612  4 => "Titre 2",
613  5 => "2",
614  6 => "2014-12-24",
615  7 => "B",
616  8 => "Deux",
617  9 => "2.2",
618  10 => "Deux long"
619  )
620  )
621  )
622  );
623  }
624 }
testExportProfilCsv($separator, $enclosure, array $expectedData)
verifyCsvContains($outFile, $separator, $enclosure, $expectedData, $columnId)
$file
testExportDisplayCsv($format, $separator, $enclosure, array $expectedData)
if($famId) $s
testExportFamilyCsv($separator, $enclosure, array $expectedData)
$path
Definition: dav.php:39
testExportFileCsv($separator, $enclosure, $file, array $expectedData)
testExportRawCsv($separator, $enclosure, array $expectedData)
getTmpDir($def= '/tmp')
Definition: Lib.Common.php:150
testExportXmlArchive($file, array $xmlPathes)
$dbaccess
Definition: checkVault.php:17
XPathTesting(\DOMDocument $dom, array $expectedValues)
$value
$data
← centre documentaire © anakeen