Core  3.2
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
PU_test_dcp_DocVaultIndex.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_commonfamily.php';
14 
16 {
17  /**
18  * import some documents
19  * @static
20  * @return string
21  */
22  protected static function getCommonImportFile()
23  {
24  return array();
25  }
26  /**
27  * @dataProvider dataDocVaultIndex
28  * @param array() $data
29  * @throws \Exception
30  */
31  public function testDocVaultIndex($data)
32  {
33  foreach ($data['import:family'] as $file => $callback) {
34  $oImport = new \ImportDocument();
35  $oImport->importDocuments($this->getAction() , $file, $onlyAnalyze = false, $archive = false);
36  $err = $oImport->getErrorMessage();
37  $this->assertEmpty($err, sprintf("import error [%s] %s", $file, $err));
38  /*
39  * Run post-import callback
40  */
41  $err = call_user_func_array($callback, array(
43  ));
44  $this->assertEmpty($err, sprintf("Callback for import file '%s' returned with error: %s", $file, $err));
45  }
46  /*
47  * Set
48  */
49  foreach ($data['set'] as $famName => $set) {
50  /**
51  * @var \DocFam $docFam
52  */
53  $docFam = new_Doc(self::$dbaccess, $famName);
54  $this->assertTrue($docFam->isAlive() , sprintf("Could not find family with name '%s'.", $famName));
55  /*
56  * docfam.param
57  */
58  if (isset($set['param']) && is_array($set['param'])) {
59  foreach ($set['param'] as $attrName => $files) {
60  $vids = array();
61  foreach ($files as $file => $name) {
62  $vid = $docFam->vaultRegisterFile($file, $name, $info);
63  $vids[] = $vid;
64  }
65  $vids = join("\n", $vids);
66  $err = $docFam->setParam($attrName, $vids);
67  $this->assertEmpty($err, sprintf("Error setting param of '%s' with vids = {%s}: %s", $attrName, join(', ', explode("\n", $vids)) , $err));
68  }
69  }
70  /*
71  * docfam.defval
72  */
73  if (isset($set['defval']) && is_array($set['defval'])) {
74  foreach ($set['defval'] as $attrName => $files) {
75  $vids = array();
76  foreach ($files as $file => $name) {
77  $vid = $docFam->vaultRegisterFile($file, $name, $info);
78  $vids[] = $vid;
79  }
80  $vids = join("\n", $vids);
81  $err = $docFam->setDefValue($attrName, $vids);
82  $this->assertEmpty($err, sprintf("Error setting defval of '%s' with vids = {%s}: %s", $attrName, join(', ', explode("\n", $vids)) , $err));
83  }
84  }
85  /*
86  * store
87  */
88  $err = $docFam->store();
89  $this->assertEmpty($err, sprintf("Error storing modified family '%s': %s", $docFam->name, $err));
90  }
91  /*
92  * Import documents
93  */
94  $oImport = new \ImportDocument();
95  $oImport->importDocuments($this->getAction() , $data['import:documents'], $onlyAnalyze = false, $archive = true);
96  $err = $oImport->getErrorMessage();
97  $this->assertEmpty($err, sprintf("import error %s", $err));
98  /*
99  printf("%s", $this->dumpDocVaultIndex($this->getDocVaultIndex(array(
100  'TST_DOCVAULTINDEX',
101  'TST_DOCVAULTINDEX_CHILD_1',
102  'TST_DOCVAULTINDEX_CHILD_2',
103  'TST_DOCVAULTINDEX_01',
104  'TST_DOCVAULTINDEX_02'
105  )) , true));
106  */
107  /*
108  * Check expectations
109  */
110  foreach ($data['expect'] as $docName => $expect) {
111  $res = $this->getDocVaultIndex(array(
112  $docName
113  ));
114  if (isset($expect['count'])) {
115  $this->assertTrue((count($res) == $expect['count']) , sprintf("Unexpected count (expected %d got %d) from docvaultindex for '%s':\n%s", $expect['count'], count($res) , $docName, $this->dumpDocVaultIndex($res, true)));
116  }
117  if (isset($expect['files'])) {
118  $files = array();
119  foreach ($res as $row) {
120  $files[] = $row['filename'];
121  }
122  foreach ($expect['files'] as $expectedFile) {
123  $this->assertContains($expectedFile, $files, sprintf("Missing file '%s' for '%s' in docvaultindex: %s", $expectedFile, $docName, $this->dumpDocVaultIndex($res, true)));
124  }
125  foreach ($files as $foundFile) {
126  $this->assertContains($foundFile, $expect['files'], sprintf("Found unexpected file '%s' for '%s' in docvaultindex:\n%s", $foundFile, $docName, $this->dumpDocVaultIndex($res, true)));
127  }
128  }
129  }
130  /*
131  * Check docvaultindex consistency
132  */
133  $report = array();
134  $vaultAnalyzer = new \Dcp\Vault\VaultAnalyzer();
135  // Inhibit/buffer progress messages from checkDocVaultIndex
136  ob_start();
137  try {
138  $ret = $vaultAnalyzer->checkDocVaultIndex($report);
139  }
140  catch(\Exception $e) {
141  // Printout buffered progress messages in case of error
142  ob_end_flush();
143  throw $e;
144  }
145  // Discard buffered progress messages
146  ob_end_clean();
147  $missing = array();
148  foreach ($report['missing']['iterator'] as $row) {
149  $missing[] = $row;
150  }
151  $new = array();
152  foreach ($report['new']['iterator'] as $row) {
153  $new[] = $row;
154  }
155  $this->assertTrue($ret, sprintf("checkDocVaultIndex reported inconsistencies: %s", var_export(array(
156  'missing' => $missing,
157  'new' => $new
158  ) , true)));
159  }
160 
161  public function dumpDocVaultIndex($res, $return = false)
162  {
163  $out = '';
164  $out.= sprintf("docvaultindex:\n");
165  foreach ($res as $row) {
166  $out.= sprintf(" %9d | %32s | %9d | %32s\n", $row['docid'], $row['docname'], $row['vaultid'], $row['filename']);
167  }
168  $out.= sprintf(" (%d rows)\n", count($res));
169  $out.= sprintf("\n");
170  if ($return === true) {
171  return $out;
172  }
173  print $out;
174  return null;
175  }
176 
177  public function getDocVaultIndex($names = array())
178  {
179  $res = array();
180  $where = '';
181  if (count($names) > 0) {
182  $where = array();
183  foreach ($names as $name) {
184  $where[] = sprintf("doc.name = %s", pg_escape_literal($name));
185  }
186  $where = sprintf("AND (%s)", join(' OR ', $where));
187  }
188  $q = sprintf("SELECT dvi.docid, doc.name AS docname, dvi.vaultid, vds.name AS filename FROM docvaultindex AS dvi, doc, vaultdiskstorage AS vds WHERE dvi.docid = doc.id AND dvi.vaultid = vds.id_file %s ORDER BY dvi.docid, dvi.vaultid", $where);
189  simpleQuery(self::$dbaccess, $q, $res, false, false, true);
190  return $res;
191  }
192 
193  public function dataDocVaultIndex()
194  {
195  return array(
196  array(
197  array(
198  'import:family' => array(
199  './DCPTEST/PU_data_dcp_docvaultindex_family_parent.csv' => function ($dbaccess)
200  {
201  $docFam = new_Doc($dbaccess, 'TST_DOCVAULTINDEX');
202  if (!$docFam->isAlive()) {
203  return sprintf("Could not find family '%s'.", 'TST_DOCVAULTINDEX');
204  }
205  $vid = $docFam->vaultRegisterFile('DCPTEST/Images/img_one.png', 'icône TST_DOCVAULTINDEX.png', $info);
206  if (($err = $docFam->changeIcon($vid)) !== '') {
207  return $err;
208  }
209  return '';
210  }
211  ,
212  './DCPTEST/PU_data_dcp_docvaultindex_family_childs.csv' => function ($dbaccess)
213  {
214  return '';
215  }
216  ) ,
217  'set' => array(
218  'TST_DOCVAULTINDEX' => array(
219  'param' => array(
220  'P_S_FILE' => array(
221  'DCPTEST/Images/img_one.png' => 'file param 1 of 3.png'
222  ) ,
223  'P_S_IMAGE' => array(
224  'DCPTEST/Images/img_one.png' => 'image param 1 of 3.png'
225  ) ,
226  'P_M_FILE' => array(
227  'DCPTEST/Images/img_one.png' => 'file param 2 of 3.png',
228  'DCPTEST/Images/img_two.png' => 'file param 3 of 3.png'
229  ) ,
230  'P_M_IMAGE' => array(
231  'DCPTEST/Images/img_one.png' => 'image param 2 of 3.png',
232  'DCPTEST/Images/img_two.png' => 'image param 3 of 3.png'
233  )
234  ) ,
235  'defval' => array(
236  'S_FILE' => array(
237  'DCPTEST/Images/img_one.png' => 'file defval 1 of 3.png'
238  ) ,
239  'S_IMAGE' => array(
240  'DCPTEST/Images/img_one.png' => 'image defval 1 of 3.png'
241  ) ,
242  'M_FILE' => array(
243  'DCPTEST/Images/img_one.png' => 'file defval 2 of 3.png',
244  'DCPTEST/Images/img_two.png' => 'file defval 3 of 3.png'
245  ) ,
246  'M_IMAGE' => array(
247  'DCPTEST/Images/img_one.png' => 'image defval 2 of 3.png',
248  'DCPTEST/Images/img_two.png' => 'image defval 3 of 3.png'
249  )
250  )
251  ) ,
252  'TST_DOCVAULTINDEX_CHILD_2' => array(
253  'param' => array(
254  'P_S_FILE' => array(
255  'DCPTEST/Images/img_one.png' => 'overwrite file param 1 of 3 from parent.png'
256  ) ,
257  'P_S_IMAGE' => array(
258  'DCPTEST/Images/img_one.png' => 'overwrite image param 1 of 3 from parent.png'
259  ) ,
260  'P_S_FILE_2' => array(
261  'DCPTEST/Images/img_one.png' => 'self file param 1 of 1.png'
262  ) ,
263  'P_S_IMAGE_2' => array(
264  'DCPTEST/Images/img_one.png' => 'self image param 1 of 1.png'
265  )
266  ) ,
267  'defval' => array(
268  'S_FILE' => array(
269  'DCPTEST/Images/img_one.png' => 'overwrite file defval 1 of 3 from parent.png'
270  ) ,
271  'S_IMAGE' => array(
272  'DCPTEST/Images/img_one.png' => 'overwrite image defval 1 of 3 from parent.png'
273  ) ,
274  'S_FILE_2' => array(
275  'DCPTEST/Images/img_one.png' => 'self file defval 1 of 1.png'
276  ) ,
277  'S_IMAGE_2' => array(
278  'DCPTEST/Images/img_one.png' => 'self image defval 1 of 1.png'
279  )
280  )
281  )
282  ) ,
283  'import:documents' => './DCPTEST/PU_data_dcp_docvaultindex_documents.zip',
284  'expect' => array(
285  'TST_DOCVAULTINDEX' => array(
286  'count' => 13,
287  'files' => array(
288  'icône TST_DOCVAULTINDEX.png',
289  'file param 1 of 3.png',
290  'image param 1 of 3.png',
291  'file param 2 of 3.png',
292  'file param 3 of 3.png',
293  'image param 2 of 3.png',
294  'image param 3 of 3.png',
295  'file defval 1 of 3.png',
296  'image defval 1 of 3.png',
297  'file defval 2 of 3.png',
298  'file defval 3 of 3.png',
299  'image defval 2 of 3.png',
300  'image defval 3 of 3.png'
301  )
302  ) ,
303  'TST_DOCVAULTINDEX_CHILD_1' => array(
304  'count' => 0,
305  'files' => array()
306  ) ,
307  'TST_DOCVAULTINDEX_CHILD_2' => array(
308  'count' => 8,
309  'files' => array(
310  'overwrite file param 1 of 3 from parent.png',
311  'overwrite image param 1 of 3 from parent.png',
312  'self file param 1 of 1.png',
313  'self image param 1 of 1.png',
314  'overwrite file defval 1 of 3 from parent.png',
315  'overwrite image defval 1 of 3 from parent.png',
316  'self file defval 1 of 1.png',
317  'self image defval 1 of 1.png'
318  )
319  ) ,
320  'TST_DOCVAULTINDEX_01' => array(
321  'count' => 7,
322  'files' => array(
323  'icône TST_DOCVAULTINDEX.png',
324  'img_one.png',
325  'img_one_bis.png',
326  'img_two.png',
327  'img_three.png',
328  'img_two_bis.png',
329  'img_three_bis.png'
330  )
331  ) ,
332  'TST_DOCVAULTINDEX_02' => array(
333  'count' => 7,
334  'files' => array(
335  'icône TST_DOCVAULTINDEX.png',
336  'file defval 1 of 3.png',
337  'image defval 1 of 3.png',
338  'file defval 2 of 3.png',
339  'image defval 2 of 3.png',
340  'file defval 3 of 3.png',
341  'image defval 3 of 3.png'
342  )
343  )
344  )
345  )
346  )
347  );
348  }
349 }
$archive
$ret
$file
$vaultAnalyzer
$onlyAnalyze
print
Definition: checklist.php:49
new_Doc($dbaccess, $id= '', $latest=false)
$dbaccess
Definition: checkVault.php:17
$info
Definition: geticon.php:30
simpleQuery($dbaccess, $query, &$result=array(), $singlecolumn=false, $singleresult=false, $useStrict=null)
Definition: Lib.Common.php:484
if($file) if($subject==""&&$file) if($subject=="") $err
dumpDocVaultIndex($res, $return=false)
if($dirid) $oImport
$data
← centre documentaire © anakeen