Core  3.2
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
PU_test_dcp_exportcsv.php
Go to the documentation of this file.
1 <?php
2 /*
3  * @author Anakeen
4  * @package Dcp\Pu
5 */
6 
7 namespace Dcp\Pu;
8 
9 require_once 'PU_testcase_dcp.php';
10 
12 {
13  static function getCommonImportFile()
14  {
15  return array(
16  "PU_data_dcp_exportfamilycsv.ods",
17  "PU_data_dcp_exportdoccsv.ods",
18  "PU_data_dcp_exportdocimage.ods"
19  );
20  }
21  /**
22  * Test that exported documents have no param columns
23  * @param array $archiveFile
24  * @param $needles
25  * @throws \Dcp\Exception
26  * @dataProvider dataExportImage
27  */
28  public function testExportImage($archiveFile, $needles)
29  {
30  include_once ('FDL/exportfld.php');
31  include_once ('Lib.FileDir.php');
32 
33  $oImport = new \ImportDocument();
34  $oImport->importDocuments(self::getAction() , $archiveFile, false, true);
35  $err = $oImport->getErrorMessage();
36  if ($err) throw new \Dcp\Exception($err);
37 
38  $folderId = "TEXT_FOLDER_EXPORT_IMAGE";
39  $famid = "TST_EXPORT_IMAGE";
40  $testFolder = uniqid(getTmpDir() . "/testexportimage");
41  $testExtractFolder = uniqid(getTmpDir() . "/testexportextractimage");
42  SetHttpVar("wfile", "Y");
43  SetHttpVar("eformat", "I");
44  SetHttpVar("app", "FDL");
45 
46  exportfld(self::getAction() , $folderId, $famid, $testFolder);
47 
48  $testarchivefile = $testFolder . "/fdl.zip";
49  $err = extractTar($testarchivefile, $testExtractFolder);
50  $this->assertEmpty($err, sprintf("Unexpected error while extracting archive '%s': %s", $testarchivefile, $err));
51 
52  $output = array();
53  exec(sprintf("ls -R %s", escapeshellarg($testExtractFolder)) , $output);
54  foreach ($needles as $needle) {
55  $this->assertContains($needle, $output, sprintf("file %s not found in export archive", $needle));
56  }
57 
58  remove_dir($testFolder);
59  }
60  /**
61  * Test that exported documents have no param columns
62  * @param string|int $folderId
63  * @param array $expectDoc
64  * @param string $separator
65  * @param string $enclosure
66  * @param array $expectedProfil
67  * @dataProvider dataExportFolder
68  */
69  public function testExportFolder($folderId, array $expectDoc, $separator, $enclosure, array $expectedProfil)
70  {
71  include_once ('FDL/exportfld.php');
72 
73  SetHttpVar("wfile", "N");
74  SetHttpVar("wprof", ($expectedProfil ? "Y" : "N"));
75  SetHttpVar("eformat", "I");
76  SetHttpVar("app", "FDL");
77  SetHttpVar("csv-enclosure", $enclosure);
78  SetHttpVar("csv-separator", $separator);
79 
80  $exportOutput = uniqid(getTmpDir() . "/testExport") . ".csv";
81 
82  exportfld(self::getAction() , $folderId, 0, $exportOutput);
83 
84  $this->assertTrue(file_exists($exportOutput) , sprintf('Export File "%s" nor create', $exportOutput));
85 
86  $exportDocName = array();
87  $h = fopen($exportOutput, "r");
88 
89  while (($data = fgetcsv($h, 0, $separator, $enclosure)) !== FALSE) {
90  if (isset($data[0]) && $data[0] === "DOC") {
91  $docName = $data[2];
92  $exportDocName[] = $docName;
93  if (isset($expectDoc[$docName])) {
94  $expecDoc = $expectDoc[$docName];
95  foreach ($expecDoc as $k => $v) {
96  $this->assertEquals($v, $data[$k], sprintf("Invalid value for %s column : [%s] : see %s", $k, implode("][", $data) , $exportOutput));
97  }
98  }
99  }
100  if (isset($data[0]) && $data[0] === "PROFIL") {
101 
102  $docName = $data[1];
103  if (isset($expectedProfil[$docName])) {
104  $prof = $expectedProfil[$docName];
105 
106  foreach ($prof as $k => $v) {
107  $this->assertEquals($v, $data[$k], sprintf("Invalid profil value for %s column : [%s]: see %s", $k, implode("][", $data) , $exportOutput));
108  }
109  }
110  }
111  }
112  fclose($h);
113 
114  $this->assertEquals(array_keys($expectDoc) , $exportDocName, "No same exported documents: See $exportOutput");
115  //unlink($exportOutput);
116 
117  }
118  /**
119  * Test that exported documents have no param columns
120  * @param string|int $familyId
121  * @param array $expectData
122  * @param string $separator
123  * @param string $enclosure
124  * @dataProvider dataExportFamily
125  */
126  public function testExportamily($familyId, array $expectData, $separator, $enclosure)
127  {
128  include_once ('FDL/exportfld.php');
129 
130  SetHttpVar("wfile", "N");
131  SetHttpVar("wprof", "Y");
132  SetHttpVar("eformat", "I");
133  SetHttpVar("app", "FDL");
134  SetHttpVar("csv-enclosure", $enclosure);
135  SetHttpVar("csv-separator", $separator);
136  /**
137  * @var \Dir $tmpFolder
138  */
139  $tmpFolder = createTmpDoc(self::$dbaccess, "DIR");
140  $err = $tmpFolder->add();
141  $err.= $tmpFolder->insertDocument($familyId);
142 
143  $this->assertEmpty($err, "Error when create family folder");
144  $exportOutput = uniqid(getTmpDir() . "/testExport") . ".csv";
145 
146  exportfld(self::getAction() , $tmpFolder->id, 0, $exportOutput);
147 
148  $this->assertTrue(file_exists($exportOutput) , sprintf('Export File "%s" nor create', $exportOutput));
149 
150  $h = fopen($exportOutput, "r");
151 
152  $keys = array();
153  while (($data = fgetcsv($h, 0, $separator, $enclosure)) !== FALSE) {
154  if (!empty($data[0])) {
155  $keys[] = $data[0];
156  if (isset($expectData[$data[0]])) {
157  foreach ($expectData[$data[0]] as $k => $v) {
158  $this->assertEquals($v, $data[$k], sprintf("Invalid value for %s column : [%s] : see %s", $k, implode("][", $data) , $exportOutput));
159  }
160  }
161  }
162  }
163  fclose($h);
164  foreach ($expectData as $key => $v) {
165  $this->assertTrue(in_array($key, $keys) , "Missing key $key see $exportOutput");
166  }
167  //unlink($exportOutput);
168 
169  }
170  /**
171  * Test that exported documents have no param columns
172  * @param array $data test specification
173  * @throws \Exception
174  * @dataProvider dataExportNoParam
175  */
176  public function testExportNoParam($data)
177  {
178  include_once ('FDL/exportfld.php');
179 
180  foreach (array(
181  'export:doc',
182  'expect:no:order'
183  ) as $key) {
184  if (!isset($data[$key])) {
185  throw new \Exception(sprintf("Missing key '%s' in test data."));
186  }
187  }
188  /* doc */
189  $doc = new_Doc(self::$dbaccess, $data['export:doc']);
190  if (!$doc->isAlive()) {
191  throw new \Exception(sprintf("Could not get document with id '%s'.", $data['export:doc']));
192  }
193  /* fout */
194  $tmpfile = tempnam(getTmpDir() , 'TST_EXPORT_PARAM');
195  if ($tmpfile === false) {
196  throw new \Exception(sprintf("Could not create temporary file in '%s'.", getTmpDir()));
197  }
198  $fout = fopen($tmpfile, 'w');
199  if ($fout === false) {
200  throw new \Exception(sprintf("Could not create temporary file '%s'.", $tmpfile));
201  }
202  /* ef */
203  $ef = array();
204  /* wprof */
205  $wprof = false;
206  /* wfile */
207  $wfile = false;
208  /* wident */
209  $wident = true;
210  /* wutf8 */
211  $wutf8 = true;
212  /* nopref */
213  $nopref = true;
214  /* eformat */
215  $eformat = 'I';
216 
217  $ed = new \Dcp\ExportDocument();
218  $ed->csvExport($doc, $ef, $fout, $wprof, $wfile, $wident, $wutf8, $nopref, $eformat);
219 
220  fclose($fout);
221 
222  $out = file_get_contents($tmpfile);
223  $lines = preg_split("/\n/", $out);
224  foreach ($lines as & $line) {
225  if (!preg_match('/^ORDER;/', $line)) {
226  continue;
227  }
228  foreach ($data['expect:no:order'] as $column) {
229  $match = preg_match(sprintf('/;%s;/', preg_quote($column, '/')) , $line);
230  $this->assertTrue(($match <= 0) , sprintf("Found param '%s' in ORDER line '%s'.", $column, $line));
231  }
232  }
233  unset($line);
234 
235  unlink($tmpfile);
236  }
237 
238  public function dataExportFamily()
239  {
240  return array(
241  array(
242  "family" => "TST_EXPORT_PARAM",
243  "expectedData" => array(
244  "BEGIN" => array(
245  1 => '',
246  2 => '',
247  3 => '',
248  4 => '',
249  5 => "TST_EXPORT_PARAM"
250  ) ,
251  "PROFID" => array(
252  1 => "TST_PROFFAMEXPORT"
253  ) ,
254  "CPROFID" => array(
255  1 => "TST_PROFEXPORT"
256  ) ,
257  "END" => array()
258  ) ,
259  "separator" => ";",
260  "enclosure" => '"'
261  ) ,
262  array(
263  "family" => "TST_EXPORT_PARAM",
264  "expectedData" => array(
265  "BEGIN" => array(
266  1 => '',
267  2 => '',
268  3 => '',
269  4 => '',
270  5 => "TST_EXPORT_PARAM"
271  ) ,
272  "PROFID" => array(
273  1 => "TST_PROFFAMEXPORT"
274  ) ,
275  "CPROFID" => array(
276  1 => "TST_PROFEXPORT"
277  )
278  ) ,
279  "separator" => ",",
280  "enclosure" => "'"
281  )
282  );
283  }
284 
285  public function dataExportFolder()
286  {
287  return array(
288  array(
289  "folderId" => "TST_EXPORT_FOLDER",
290  "expectedDocument" => array(
291  "TST_EXPORT_PARAM_01" => array(
292  4 => "L'un et l'autre",
293  5 => 1
294  ) ,
295  "TST_EXPORT_PARAM_02" => array(
296  4 => "Déja vu\nUne impression",
297  5 => 2
298  ) ,
299  "TST_EXPORT_PARAM_03" => array(
300  4 => 'Dicton "Qui élargit son cœur, rétrécit sa bouche"',
301  5 => 3
302  )
303  ) ,
304  "separator" => ";",
305  "enclosure" => '"',
306  "profil" => array()
307  ) ,
308  array(
309  "folderId" => "TST_EXPORT_FOLDER",
310  "expectedDocument" => array(
311  "TST_EXPORT_PARAM_01" => array(
312  4 => "L'un et l'autre"
313  ) ,
314  "TST_EXPORT_PARAM_02" => array(
315  4 => "Déja vu\nUne impression"
316  ) ,
317  "TST_EXPORT_PARAM_03" => array(
318  4 => 'Dicton "Qui élargit son cœur, rétrécit sa bouche"'
319  )
320  ) ,
321  "separator" => ",",
322  "enclosure" => "'",
323  "profil" => array()
324  ) ,
325  array(
326  "folderId" => "TST_EXPORT_FOLDER",
327  "expectedDocument" => array(
328  "TST_EXPORT_PARAM_01" => array(
329  4 => "L'un et l'autre"
330  ) ,
331  "TST_PROFEXPORT" => array(
332  4 => "Profil de test, d'exportation"
333  ) ,
334  "TST_EXPORT_PARAM_02" => array(
335  4 => "Déja vu\nUne impression"
336  ) ,
337  "TST_EXPORT_PARAM_03" => array(
338  4 => 'Dicton "Qui élargit son cœur, rétrécit sa bouche"'
339  )
340  ) ,
341  "separator" => ",",
342  "enclosure" => "'",
343  "profil" => array(
344  "TST_PROFEXPORT" => array(
345  2 => ":useAccount",
346  4 => "view=all",
347  5 => "view=gadmin"
348  )
349  )
350  )
351  );
352  }
353 
354  public function dataExportImage()
355  {
356  return array(
357  array(
358  "./DCPTEST/PU_dcp_data_exportcsvimage.zip",
359  array(
360  "PU_data_dcp_exportdocimageexample.png",
361  "PU_data_dcp_exportdocimage.ods"
362  )
363  )
364  );
365  }
366 
367  public function dataExportNoParam()
368  {
369  return array(
370  array(
371  array(
372  "export:doc" => "TST_EXPORT_PARAM_01",
373  "expect:no:order" => array(
374  "a_param_text"
375  )
376  )
377  )
378  );
379  }
380 }
381 ?>
remove_dir($dir)
Definition: Lib.FileDir.php:53
$tmpfile
SetHttpVar($name, $def)
Definition: Lib.Http.php:150
exportfld(Action &$action, $aflid="0", $famid="", $outputPath="", $exportInvisibleVisibilities=false)
Definition: exportfld.php:41
getTmpDir($def= '/tmp')
Definition: Lib.Common.php:150
testExportImage($archiveFile, $needles)
new_Doc($dbaccess, $id= '', $latest=false)
extractTar($tar, $untardir, $mime="")
$dbaccess
Definition: checkVault.php:17
createTmpDoc($dbaccess, $fromid, $defaultvalue=true)
if($file) if($subject==""&&$file) if($subject=="") $err
if($dirid) $oImport
testExportFolder($folderId, array $expectDoc, $separator, $enclosure, array $expectedProfil)
$data
← centre documentaire © anakeen