Core  3.2
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
PU_test_dcp_exportxml.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  * @var \DOMDocument
19  */
20  private $dom;
21  static function getCommonImportFile()
22  {
23  return array(
24  "PU_data_dcp_exportfamily.ods",
25  "PU_data_dcp_exportrelation.ods",
26  "PU_data_dcp_exporttitlelimits.ods",
27  "PU_data_dcp_exportdocimagexml.ods"
28  );
29  }
30  /**
31  * @return \DOMDocument
32  */
33  private function getExportedSelectionDom()
34  {
35  if (!$this->dom) {
36  $selNames = array(
37  "TST_REL1",
38  "TST_REL2",
39  "TST_REL3",
40  "TST_REL4"
41  );
42  $config = array();
43  foreach ($selNames as $name) {
44  $id = getIdFromName(self::$dbaccess, $name);
45  if ($id) $config["selectionItems"][] = $id;
46  }
47 
48  $s = new \Fdl_DocumentSelection($config);
49  //print_r( $s->search());
50  $export = new \exportXmlFolder();
51  try {
52  $export->setOutputFormat(\exportXmlFolder::xmlFormat);
53  $export->useIdentificator(false);
54  $export->exportFromSelection($s);
55  $this->dom = new \DOMDocument();
56  $this->dom->load($export->getOutputFile());
57  @unlink($export->getOutputFile());
58  return $this->dom;
59  }
60  catch(\Exception $e) {
61  print $e->getMessage();
62  }
63  return null;
64  } else {
65  return $this->dom;
66  }
67  }
68  /**
69  * @return \DOMDocument
70  */
71  private function getExportedSearchDom()
72  {
73  if (!$this->dom) {
74  $s = new \SearchDoc(self::$dbaccess, "TST_EXPORTFAM1");
75  //print_r( $s->search());
76  $export = new \exportXmlFolder();
77 
78  $export->setOutputFormat(\exportXmlFolder::xmlFormat);
79  $export->useIdentificator(false);
80  $export->exportFromSearch($s);
81 
82  $this->dom = new \DOMDocument();
83  $this->dom->load($export->getOutputFile());
84  @unlink($export->getOutputFile());
85  return $this->dom;
86  } else {
87  return $this->dom;
88  }
89  }
90  /**
91  * @dataProvider dataDocumentFiles
92  */
93  public function testExportRelation($docName, $attrName, $expectedValue)
94  {
95 
96  $dom = $this->getExportedSearchDom();
97  $this->domTestExportValue($dom, $docName, $attrName, 'name', $expectedValue);
98  }
99  /**
100  * @dataProvider dataDocumentFiles
101  */
102  public function testExportSelectionRelation($docName, $attrName, $expectedValue)
103  {
104 
105  $dom = $this->getExportedSelectionDom();
106  $this->domTestExportValue($dom, $docName, $attrName, 'name', $expectedValue);
107  }
108  /**
109  * @dataProvider dataValues
110  */
111  public function testExportSimpleValue($docName, $attrName, $expectedValue)
112  {
113 
114  $dom = $this->getExportedSearchDom();
115  $this->domTestExportValue($dom, $docName, $attrName, 'content', $expectedValue);
116  }
117  private function domTestExportValue(\DOMDocument $dom, $docName, $attrName, $domAttr, $expectedValue)
118  {
119 
120  $docs = $dom->getElementsByTagName("tst_exportfam1");
121  /**
122  * \DOMElement $xmldoc
123  */
124  $xmldoc = null;
125  foreach ($docs as $doc) {
126  /**
127  * @var \DOMElement $doc
128  */
129 
130  if ($doc->getAttribute('name') == $docName) {
131  $xmldoc = $doc;
132  break;
133  }
134  }
135 
136  $this->assertNotEmpty($xmldoc, sprintf("document %s not found in xml", $docName));
137  /**
138  * @var \DOMNodeList $attrs
139  */
140  $attrs = $xmldoc->getElementsByTagName($attrName);
141  if (!is_array($expectedValue)) $expectedValue = array(
142  $expectedValue
143  );
144  $this->assertEquals(count($expectedValue) , $attrs->length, sprintf("attribute %s not found in %s document", $attrName, $docName));
145 
146  $ka = 0;
147  foreach ($attrs as $attr) {
148  /**
149  * @var \DOMElement $attr
150  */
151  if ($domAttr == 'content') {
152  $value = $attr->nodeValue;
153  } else {
154  $value = $attr->getAttribute($domAttr);
155  }
156  $this->assertTrue($expectedValue[$ka] === $value, sprintf("incorrect value for attribute %s in %s document", $attrName, $docName));
157  $ka++;
158  }
159  }
160  /**
161  * @dataProvider dataExportTitleLimits
162  */
163  function testExportTitleLimits($folderId)
164  {
165  if (file_exists("/.dockerenv")) {
166  $this->markTestSkipped("Detected incompatible Docker environment");
167  }
168  $export = new \exportXmlFolder();
169  $catchedMessage = '';
170  try {
171  $export->setOutputFormat(\exportXmlFolder::xmlFormat);
172  $export->useIdentificator(false);
173  $export->exportFromFolder($folderId);
174  $this->dom = new \DOMDocument();
175  $this->dom->load($export->getOutputFile());
176  @unlink($export->getOutputFile());
177  }
178  catch(\Exception $e) {
179  $catchedMessage = $e->getMessage();
180  }
181  $this->assertNotNull($this->dom->documentElement, sprintf("Invalid XML export for folder '%s': %s", $folderId, ($catchedMessage != '') ? $catchedMessage : '<no-error-message>'));
182  }
183  /**
184  * Test that exported documents have no param columns
185  * @param array $archiveFile
186  * @param $needles
187  * @param $type
188  * @throws \Dcp\Exception
189  * @dataProvider dataExportImage
190  */
191  public function testExportImageXmlZip($archiveFile, $needles, $type)
192  {
193  include_once ('FDL/exportfld.php');
194  include_once ('Lib.FileDir.php');
195 
196  $oImport = new \ImportDocument();
197  $oImport->importDocuments(self::getAction() , $archiveFile, false, true);
198  $err = $oImport->getErrorMessage();
199  if ($err) throw new \Dcp\Exception($err);
200 
201  $folderId = "TEXT_FOLDER_EXPORT_IMAGE_XML";
202  $famid = "TST_EXPORT_IMAGE_XML";
203  $testFolder = uniqid(getTmpDir() . "/testexportimage");
204  $testExtractFolder = uniqid(getTmpDir() . "/testexportextractimage");
205  mkdir($testFolder);
206  $testarchivefile = $testFolder . "/xml";
207  if ($type == "X") $testarchivefile.= ".zip";
208  else $testarchivefile.= ".xml";
209  SetHttpVar("wfile", "Y");
210 
211  exportxmlfld(self::getAction() , $folderId, $famid, null, $testarchivefile, $type, "Y", null, false);
212 
213  if ($type == "X") {
214  $err = extractTar($testarchivefile, $testExtractFolder);
215  $this->assertEmpty($err, sprintf("Unexpected error while extracting archive '%s': %s", $testarchivefile, $err));
216  } else $testExtractFolder = $testFolder;
217 
218  $output = array();
219  exec(sprintf("cat %s/*.xml", escapeshellarg($testExtractFolder)) , $output);
220  foreach ($needles as $needle) {
221  $found = false;
222  foreach ($output as $line) {
223  if (stripos($line, $needle) !== false) {
224  $found = true;
225  break;
226  }
227  }
228  $this->assertTrue($found, sprintf("file %s not found in export archive", $needle));
229  }
230  remove_dir($testFolder);
231  }
232 
233  public function dataExportImage()
234  {
235  return array(
236  array(
237  "./DCPTEST/PU_dcp_data_exportxmlimage.zip",
238  array(
239  "PU_data_dcp_exportdocimageexample.png",
240  "PU_data_dcp_exportdocimage.ods"
241  ) ,
242  "X"
243  ) ,
244  array(
245  "./DCPTEST/PU_dcp_data_exportxmlimage.zip",
246  array(
247  "PU_data_dcp_exportdocimageexample.png",
248  "PU_data_dcp_exportdocimage.ods"
249  ) ,
250  "Y"
251  )
252  );
253  }
254 
255  public function dataDocumentFiles()
256  {
257  return array(
258  array(
259  "TST_REL1",
260  "tst_relone",
261  "TST_OUTREL1"
262  ) ,
263  array(
264  "TST_REL1",
265  "tst_account",
266  "TST_U_USERONE"
267  ) ,
268  array(
269  "TST_REL2",
270  "tst_relmul",
271  "TST_OUTREL1,TST_OUTREL2,TST_OUTREL3"
272  ) ,
273  array(
274  "TST_REL3",
275  "tst_colrelone",
276  array(
277  "TST_OUTREL1",
278  "TST_OUTREL2",
279  "TST_OUTREL3"
280  )
281  ) ,
282  array(
283  "TST_REL4",
284  "tst_colrelmul",
285  array(
286  "TST_OUTREL1",
287  "TST_OUTREL1,TST_OUTREL3",
288  "TST_OUTREL1,TST_OUTREL2,TST_OUTREL3"
289  )
290  )
291  );
292  }
293 
294  public function dataValues()
295  {
296  return array(
297  array(
298  "TST_NUM1",
299  "tst_number",
300  "1"
301  ) ,
302  array(
303  "TST_NUM0",
304  "tst_number",
305  "0"
306  ) ,
307  array(
308  "TST_DATE1",
309  "tst_date",
310  "2012-02-20"
311  ) ,
312  array(
313  "TST_DATE1",
314  "tst_number",
315  ""
316  )
317  );
318  }
319 
320  public function dataExportTitleLimits()
321  {
322  return array(
323  array(
324  "TST_EXPORTTITLELIMITS_DIR"
325  )
326  );
327  }
328 }
329 ?>
remove_dir($dir)
Definition: Lib.FileDir.php:53
exportxmlfld(Action &$action, $aflid="0", $famid="", SearchDoc $specSearch=null, $outputFile= '', $eformat="", $wident= 'Y', Fdl_DocumentSelection $aSelection=null, $toDownload=true)
SetHttpVar($name, $def)
Definition: Lib.Http.php:150
if($famId) $s
testExportSimpleValue($docName, $attrName, $expectedValue)
print
Definition: checklist.php:49
getTmpDir($def= '/tmp')
Definition: Lib.Common.php:150
testExportImageXmlZip($archiveFile, $needles, $type)
extractTar($tar, $untardir, $mime="")
testExportSelectionRelation($docName, $attrName, $expectedValue)
$dbaccess
Definition: checkVault.php:17
getIdFromName($dbaccess, $name)
testExportRelation($docName, $attrName, $expectedValue)
if($file) if($subject==""&&$file) if($subject=="") $err
if($dirid) $oImport
$value
$export
← centre documentaire © anakeen