Core  3.2
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
PU_test_dcp_exportrevision.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_exportfamilyrevision.ods"
17  );
18  }
19  protected $famName = "tst_export_revision";
20 
21  public static function setUpBeforeClass()
22  {
23  parent::setUpBeforeClass();
24 
25  $d4 = new_doc(self::$dbaccess, "TST_EREV4");
26  $d5 = new_doc(self::$dbaccess, "TST_EREV5");
27  $d6 = new_doc(self::$dbaccess, "TST_EREV6");
28 
29  $d1 = new_doc(self::$dbaccess, "TST_EREV1");
30  $d1->revise();
31  $d1->setValue(\Dcp\AttributeIdentifiers\tst_export_revision::tst_title, "Référence n°2");
32  $d1->store();
33 
34  $d4->setValue(\Dcp\AttributeIdentifiers\tst_export_revision::tst_doc_fixed, $d1->id);
35 
36  $d2 = new_doc(self::$dbaccess, "TST_EREV2");
37  $d2->revise();
38  $d2->setValue(\Dcp\AttributeIdentifiers\tst_export_revision::tst_title, "Identifiant n°2");
39  $d2->store();
40 
41  $d6->setValue(\Dcp\AttributeIdentifiers\tst_export_revision::tst_doc_fixed, $d2->id);
42  $d5->setValue(\Dcp\AttributeIdentifiers\tst_export_revision::tst_doc_state, $d2->id);
43 
44  $d2->revise();
45  $d2->setValue(\Dcp\AttributeIdentifiers\tst_export_revision::tst_title, "Identifiant n°3");
46  $d2->store();
47 
48  $d5->setValue(\Dcp\AttributeIdentifiers\tst_export_revision::tst_doc_fixed, $d2->id);
49 
50  $d3 = new_doc(self::$dbaccess, "TST_EREV3");
51  $d3->revise();
52  $d5->setValue(\Dcp\AttributeIdentifiers\tst_export_revision::tst_doc_state, $d3->id);
53  $d3->setValue(\Dcp\AttributeIdentifiers\tst_export_revision::tst_title, "Révision n°2");
54  $d3->store();
55  $d3->revise();
56  $d3->setValue(\Dcp\AttributeIdentifiers\tst_export_revision::tst_title, "Révision n°3");
57  $d3->store();
58 
59  $d6->setValue(\Dcp\AttributeIdentifiers\tst_export_revision::tst_doc_state, $d3->id);
60 
61  $d3->revise();
62  $d3->setValue(\Dcp\AttributeIdentifiers\tst_export_revision::tst_title, "Révision n°4");
63  $d3->store();
64 
65  $d6->setValue(\Dcp\AttributeIdentifiers\tst_export_revision::tst_doc_fixed, $d3->id);
66 
67  $d4->store();
68  $d5->store();
69  $d6->store();
70  }
71  /**
72  * @param $separator
73  * @param $enclosure
74  * @param array $expectedData
75  * @dataProvider dataExportCsv
76  */
77  public function testExportRevisionCsv($separator, $enclosure, array $expectedData)
78  {
79  $outFile = tempnam(getTmpDir() , 'tstexport');
80  $s = new \SearchDoc(self::$dbaccess, $this->famName);
81  $s->setObjectReturn();
82  $s->search();
83 
84  $this->assertEmpty($s->searchError() , sprintf("Error in search %s", print_r($s->getSearchInfo() , true)));
85 
86  $ec = new \Dcp\ExportCollection();
87 
88  $ec->setCvsEnclosure($enclosure);
89  $ec->setCvsSeparator($separator);
90  $ec->setOutputFilePath($outFile);
91  $ec->setDocumentlist($s->getDocumentList());
92  $ec->export();
93 
94  $this->assertTrue(filesize($outFile) > 0, sprintf("\"%s\" file not produced", $outFile));
95 
96  $this->verifyCsvContains($outFile, $separator, $enclosure, $expectedData, 2);
97  }
98  /**
99  * @param $separator
100  * @param $enclosure
101  * @param array $expectedData
102  * @dataProvider dataExportRevisionXml
103  */
104  public function testExportRevisionXml(array $expectedData)
105  {
106  $outFile = tempnam(getTmpDir() , 'tstexport');
107  $s = new \SearchDoc(self::$dbaccess, $this->famName);
108  $s->setObjectReturn();
109  $s->search();
110 
111  $this->assertEmpty($s->searchError() , sprintf("Error in search %s", print_r($s->getSearchInfo() , true)));
112 
113  $ec = new \Dcp\ExportCollection();
114 
115  $ec->setOutputFormat(\Dcp\ExportCollection::xmlFileOutputFormat);
116  $ec->setOutputFilePath($outFile);
117  $ec->setDocumentlist($s->getDocumentList());
118  $ec->export();
119 
120  $this->assertTrue(filesize($outFile) > 0, sprintf("\"%s\" file not produced", $outFile));
121 
122  $dom = new \DOMDocument();
123  $dom->load($outFile);
124 
125  $this->XPathTesting($dom, $expectedData);
126  }
127 
128  protected function verifyCsvContains($outFile, $separator, $enclosure, $expectedData, $columnId)
129  {
130  $results = fopen($outFile, "r");
131  $resultData = array();
132  while (($data = fgetcsv($results, 1000, $separator, $enclosure)) !== FALSE) {
133  $docName = $data[$columnId];
134  $resultData[$docName] = $data;
135  }
136  fclose($results);
137  foreach ($expectedData as $docName => $docValues) {
138  $this->assertTrue(isset($resultData[$docName]) , sprintf("%s document not found : %s", $docName, print_r($resultData, true)));
139  foreach ($docValues as $index => $value) {
140  $revName = "";
141  if (preg_match("/([A-Z_0-9]+)#([0-9]+)/", $value, $reg)) {
142  simpleQuery("", sprintf("select id from docread where name='%s' and revision=%d", pg_escape_string($reg[1]) , $reg[2]) , $value, true, true);
143  simpleQuery("", sprintf("select name, revision from docread where id=%d", $resultData[$docName][$index]) , $revName, false, true);
144  }
145 
146  if (strpos($value, "*") === false) {
147  $this->assertEquals($value, $resultData[$docName][$index], sprintf("%s (index %s : %s) : %s \n %s \n %s", $docName, $index, $docValues[$index], print_r($revName, true) , print_r($resultData, true) , $outFile));
148  } else {
149  $this->assertEquals(preg_match('/' . $value . '/', $resultData[$docName][$index]) , 1, sprintf("expected \"%s\" %s (index %s) : %s \n %s", $docValues[$index], $docName, $index, print_r($resultData, true) , $outFile));
150  }
151  }
152  }
153  }
154  protected function XPathTesting(\DOMDocument $dom, array $expectedValues)
155  {
156 
157  $xp = new \DOMXpath($dom);
158  foreach ($expectedValues as $path => $value) {
159  $entries = $xp->query($path);
160  $found = 0;
161  $foundValues = array();
162  if (is_array($value)) {
163  foreach ($entries as $entry) {
164  if ($entry->nodeValue == $value) {
165  $found++;
166  }
167  $foundValues[] = $entry->nodeValue;
168  }
169  $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)));
170  } else {
171  foreach ($entries as $entry) {
172  if ($entry->nodeValue == $value) $found++;
173  $foundValues[] = $entry->nodeValue;
174  }
175  $this->assertGreaterThan(0, $found, sprintf("Item \"%s\" not found in %s path, found \n\t%s\n", $value, $path, implode("\n\t", $foundValues)));
176  }
177  }
178  }
179  public function dataExportRevisionXml()
180  {
181  return array(
182  array(
183  array(
184  $this->famName . "[@name = \"TST_EREV1\"]/tst_frame/tst_int" => "1",
185  $this->famName . "[@name = \"TST_EREV2\"]/tst_frame/tst_int" => "2",
186  $this->famName . "[@name = \"TST_EREV3\"]/tst_frame/tst_int" => "3",
187  $this->famName . "[@name = \"TST_EREV4\"]/tst_frame/tst_doc_fixed[@name= \"TST_EREV1\"][@revision = \"1\"]" => "Référence n°2",
188  $this->famName . "[@name = \"TST_EREV5\"]/tst_frame/tst_doc_fixed[@name= \"TST_EREV2\"][@revision = \"2\"]" => "Identifiant n°3",
189  $this->famName . "[@name = \"TST_EREV6\"]/tst_frame/tst_doc_fixed[@name= \"TST_EREV3\"][@revision = \"3\"]" => "Révision n°4",
190  $this->famName . "[@name = \"TST_EREV4\"]/tst_frame/tst_doc_state[@name= \"TST_EREV1\"][@revision = \"state:first\"]" => "Référence n°1",
191  $this->famName . "[@name = \"TST_EREV5\"]/tst_frame/tst_doc_state[@name= \"TST_EREV3\"][@revision = \"state:first\"]" => "Révision n°2",
192  $this->famName . "[@name = \"TST_EREV6\"]/tst_frame/tst_doc_state[@name= \"TST_EREV3\"][@revision = \"state:first\"]" => "Révision n°3",
193  )
194  )
195  );
196  }
197  public function dataExportCsv()
198  {
199  return array(
200  array(
201  ";",
202  '"',
203  array(
204  "TST_EREV1" => array(
205  4 => "Référence n°2",
206  5 => "1"
207  ) ,
208  "TST_EREV2" => array(
209  4 => "Identifiant n°3",
210  5 => "2"
211  ) ,
212  "TST_EREV3" => array(
213  4 => "Révision n°4",
214  5 => "3"
215  ) ,
216  "TST_EREV4" => array(
217  4 => "Un",
218  5 => "1",
219  6 => "TST_EREV1",
220  7 => "TST_EREV1#1",
221  8 => "TST_EREV1#0"
222  ) ,
223  "TST_EREV5" => array(
224  4 => "A",
225  5 => "2",
226  6 => "TST_EREV2",
227  7 => "TST_EREV2#2",
228  8 => "TST_EREV3#1"
229  ) ,
230  "TST_EREV6" => array(
231  4 => "Hello",
232  5 => "3",
233  6 => "TST_EREV3",
234  7 => "TST_EREV3#3",
235  8 => "TST_EREV3#2"
236  )
237  )
238  )
239  );
240  }
241 }
242 ?>
XPathTesting(\DOMDocument $dom, array $expectedValues)
if($famId) $s
$d1
Definition: dav.php:11
$d2
Definition: dav.php:67
$path
Definition: dav.php:39
getTmpDir($def= '/tmp')
Definition: Lib.Common.php:150
testExportRevisionCsv($separator, $enclosure, array $expectedData)
verifyCsvContains($outFile, $separator, $enclosure, $expectedData, $columnId)
testExportRevisionXml(array $expectedData)
$dbaccess
Definition: checkVault.php:17
simpleQuery($dbaccess, $query, &$result=array(), $singlecolumn=false, $singleresult=false, $useStrict=null)
Definition: Lib.Common.php:484
$value
$data
← centre documentaire © anakeen