Core  3.2
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
PU_test_dcp_autocompletion.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_action.php';
10 include_once 'WHAT/Lib.Http.php';
11 include_once 'FDL/autocompletion.php';
12 
14 {
15  static public $externalsList = array();
16 
17  public $errmsg = '';
18 
19  public static function setUpBeforeClass()
20  {
21  parent::setUpBeforeClass();
22 
23  self::addExternals("PU_data_dcp_autocompletion.php");
24 
25  self::connectUser();
26  self::beginTransaction();
27  self::importDocument("PU_data_dcp_autocompletion_family.ods");
28  }
29 
30  public static function tearDownAfterClass()
31  {
32  self::rollbackTransaction();
33 
34  self::rollbackExternals();
35 
36  parent::tearDownAfterClass();
37  }
38 
39  public static function addExternals($file)
40  {
41  $source = '..' . DIRECTORY_SEPARATOR . 'DCPTEST' . DIRECTORY_SEPARATOR . $file;
42  $destination = 'EXTERNALS' . DIRECTORY_SEPARATOR . basename($file);
43  /* Check that the destination does not already exists */
44  if (file_exists($destination)) {
45  throw new \Exception(sprintf("External destination file '%s' already exists.", $destination));
46  }
47  /* Create the symlink */
48  $ret = symlink($source, $destination);
49  if ($ret === false) {
50  self::rollbackExternals();
51  throw new \Exception(sprintf("Could not symlink '%s' into '%s'.", $source, $destination));
52  }
53 
54  self::$externalsList[] = $destination;
55  }
56 
57  public static function rollbackExternals()
58  {
59  foreach (self::$externalsList as $file) {
60  if (is_link($file)) {
61  unlink($file);
62  }
63  }
64  }
65 
66  public function setUp()
67  {
68  $this->setUpTestAction("FDL", "AUTOCOMPLETION");
69  }
70 
71  public function tearDown()
72  {
73  $this->resetCurrentParameters();
74  }
75  /**
76  * @dataProvider dataMainAutocompletion
77  */
78  public function testMainAutocompletion($data)
79  {
80  global $ZONE_ARGS;
81  $ZONE_ARGS = array();
82 
83  $doc = false;
84  if (isset($data['document'])) {
85  $doc = new_Doc(self::$dbaccess, $data['document'], true);
86  $this->assertTrue(is_object($doc) , sprintf("Error retrieving document '%s'.", $data['document']));
87  } else {
89  $this->assertTrue(is_object($doc) , sprintf("Could not create new document from family '%s'.", $data['fam']));
90  $err = $doc->add();
91  $this->assertEmpty($err, sprintf("Could not add new document to database: %s", $err));
92  }
93 
94  $oattr = $doc->getAttribute($data['attr']);
95  $this->assertTrue(is_object($oattr) , sprintf("Could not get attribute '%s' on document '%s' (id=%s).", $data['attr'], $doc->name, $doc->id));
96 
97  if (isset($data['http:vars'])) {
98  foreach ($data['http:vars'] as $name => $value) {
99  SetHttpVar($name, $value);
100  }
101  }
102 
103  $this->setCurrentParameters('docid', $doc->id);
104  $this->setCurrentParameters('attrid', $data['attr']);
105  if (isset($data['action:args'])) {
106  foreach ($data['action:args'] as $name => $value) {
107  $this->setCurrentParameters($name, $value);
108  }
109  }
110 
111  $out = $this->testAction->execute();
112 
113  $dom = new \DOMDocument();
114  $ret = $dom->loadXML($out);
115  $this->assertTrue($ret, sprintf("response does not seems to be valid XML: [%s…]", substr($out, 0, 30)));
116 
117  $xp = new \DOMXpath($dom);
118  $res = $xp->query('/status');
119  $this->assertTrue((is_object($res) && ($res->length > 0)) , sprintf("response does not contains a status node."));
120 
121  $statusNode = $res->item(0);
122 
123  if (isset($data['expected:warning'])) {
124  $warning = $statusNode->getAttribute('warning');
125  $this->assertTrue(($warning == $data['expected:warning']) , sprintf("response contains non expected warning: found '%s' while expecting '%s'", $warning, $data['expected:warning']));
126  }
127 
128  $cibles = $this->getAutocompletionCibles($xp, $statusNode);
129  $this->assertTrue(is_array($cibles) , sprintf("Error getting <cible> nodes: %s", $this->errmsg));
130 
131  $options = $this->getAutocompletionOptions($xp, $statusNode);
132  $this->assertTrue(is_array($options) , sprintf("Error getting <option> nodes: %s", $this->errmsg));
133 
134  if (isset($data['expected:results'])) {
135  $results = $this->getArrayFromOptions($options);
136  $err = $this->checkResult($data['expected:results'], $results);
137  $this->assertEmpty($err, sprintf("Expected results not met: %s", $err));
138  }
139 
140  if (isset($data['expected:cibles'])) {
141  $err = $this->checkCibles($data['expected:cibles'], $cibles);
142  $this->assertEmpty($err, sprintf("Expected cibles not met: %s", $err));
143  }
144  }
145 
146  public function getAutocompletionCibles(&$xp, &$statusNode)
147  {
148  $cibles = array();
149  $cibleNodeList = $xp->query('cibles/cible', $statusNode);
150  if ($cibleNodeList === false) {
151  $this->errmsg = sprintf("XPath query error: 'cibles/cible'");
152  return false;
153  }
154  foreach ($cibleNodeList as $cibleNode) {
155  $cibles[] = $cibleNode->textContent;
156  }
157  return $cibles;
158  }
159 
160  public function getAutocompletionOptions(&$xp, &$statusNode)
161  {
162  $options = array();
163  $optionNodeList = $xp->query('option', $statusNode);
164  if ($optionNodeList === false) {
165  $this->errmsg = sprintf("XPath query error: 'option'");
166  return false;
167  }
168  foreach ($optionNodeList as $i => $optionNode) {
169  $options[$i] = array();
170  $titleNodeList = $xp->query('title', $optionNode);
171  $options[$i]['title'] = $titleNodeList->item(0)->textContent;
172  $options[$i]['value'] = $optionNode->getAttribute('value');
173  $options[$i]['values'] = array();
174 
175  $valNodeList = $xp->query('values/val', $optionNode);
176  foreach ($valNodeList as $valNode) {
177  if ($valNode->nodeName != 'val') {
178  continue;
179  }
180  $options[$i]['values'][] = $valNode->textContent;
181  }
182  }
183  return $options;
184  }
185 
186  public function getArrayFromOptions(&$options)
187  {
188  $res = array();
189  foreach ($options as $i => & $option) {
190  $line = array(
191  $option['title']
192  );
193  foreach ($option['values'] as & $value) {
194  $line[] = $value;
195  }
196  unset($value);
197  $res[$i] = $line;
198  }
199  return $res;
200  }
201 
202  public function checkResult(&$expectedResult, &$actualResult)
203  {
204  $expectedCount = count($expectedResult);
205  $actualCount = count($actualResult);
206  if ($actualCount != $expectedCount) {
207  return sprintf("Result count mismatch: found '%s' while expecting '%s'", $actualCount, $expectedCount);
208  }
209 
210  for ($line = 0; $line < $expectedCount; $line++) {
211  $expectedLine = $expectedResult[$line];
212  $actualLine = $actualResult[$line];
213  $expectedLineCount = count($expectedLine);
214  $actualLineCount = count($actualLine);
215  if ($actualLineCount != $expectedLineCount) {
216  return sprintf("Line #%s count mismatch: found '%s' while expecting '%s'", $line, $actualLineCount, $expectedLineCount);
217  }
218 
219  for ($col = 0; $col < $expectedLineCount; $col++) {
220  $actualCell = $actualLine[$col];
221  $expectedCell = $expectedLine[$col];
222  if ($actualCell != $expectedCell) {
223  return sprintf("Data mismatch at Line #%s, Column #%s: found '%s' while expecting '%s'", $line, $col, $actualCell, $expectedCell);
224  }
225  }
226  }
227  return '';
228  }
229 
230  public function checkCibles(&$expectedCibles, &$actualCibles)
231  {
232  $expectedCount = count($expectedCibles);
233  $actualCount = count($actualCibles);
234  if ($actualCount != $expectedCount) {
235  return sprintf("Cible count mismatch: found '%s' while expecting '%s'", $actualCount, $expectedCount);
236  }
237 
238  for ($i = 0; $i < $expectedCount; $i++) {
239  $expected = $expectedCibles[$i];
240  $actual = $actualCibles[$i];
241  if ($actual != $expected) {
242  return sprintf("Data mismatch at index #%s: found '%s' while expecting '%s'", $i, $actual, $expected);
243  }
244  }
245  return '';
246  }
247  public function dataMainAutocompletion()
248  {
249  return array(
250  array(
251  array(
252  'fam' => 'TST_AUTOCOMPLETION',
253  'attr' => 'S_GRAVITE',
254  'expected:warning' => '',
255  'expected:results' => array(
256  array(
257  'mineure',
258  'Mi'
259  ) ,
260  array(
261  'majeure',
262  'Ma'
263  ) ,
264  array(
265  'bloquante',
266  'Bl'
267  )
268  ) ,
269  'expected:cibles' => array(
270  's_gravite'
271  )
272  )
273  ) ,
274  array(
275  array(
276  'fam' => 'TST_AUTOCOMPLETION',
277  'attr' => 'S_TITLE_1',
278  'http:vars' => array(
279  '_s_title_1' => 'Relation'
280  ) ,
281  'expected:warning' => '',
282  'expected:results' => array(
283  array(
284  'Test Relation 2',
285  'Test Relation 2'
286  ) ,
287  array(
288  'Test Relation 3',
289  'Test Relation 3'
290  )
291  ) ,
292  'expected:cibles' => array(
293  's_title_1'
294  )
295  )
296  ) ,
297  array(
298  array(
299  'fam' => 'TST_AUTOCOMPLETION',
300  'attr' => 'S_TITLE_2',
301  'http:vars' => array(
302  '_s_title_2' => 'Relation'
303  ) ,
304  'expected:warning' => '',
305  'expected:results' => array(
306  array(
307  'Test Relation 2',
308  'Test Relation 2'
309  ) ,
310  array(
311  'Test Relation 3',
312  'Test Relation 3'
313  )
314  ) ,
315  'expected:cibles' => array(
316  's_title_2'
317  )
318  )
319  ) ,
320  array(
321  // issue #3713
322  array(
323  'fam' => 'TST_AUTOCOMPLETION',
324  'attr' => 'S_TITLE_2',
325  'http:vars' => array(
326  '_s_title_2' => 'Quoted'
327  ) ,
328  'expected:warning' => '',
329  'expected:results' => array(
330  array(
331  'Quoted " title "',
332  'Quoted " title "'
333  )
334  ) ,
335  'expected:cibles' => array(
336  's_title_2'
337  )
338  )
339  ) ,
340  array(
341  // issue #3734
342  array(
343  'fam' => 'TST_AUTOCOMPLETION',
344  'attr' => 'S_TITLE_2',
345  'http:vars' => array(
346  '_s_title_2' => 'CDATA'
347  ) ,
348  'expected:warning' => '',
349  'expected:results' => array(
350  array(
351  'CDATA injection]]></error>',
352  'CDATA injection]]></error>'
353  )
354  ) ,
355  'expected:cibles' => array(
356  's_title_2'
357  )
358  )
359  ) ,
360  array(
361  array(
362  'fam' => 'TST_AUTOCOMPLETION',
363  'attr' => 'BUG_2108',
364  'http:vars' => array(
365  '_s_text' => 'Bug 2108',
366  '_ilink_bug_2108' => 'Current Title'
367  ) ,
368  'expected:warning' => '',
369  'expected:results' => array(
370  array(
371  'Current Title, Bug 2108',
372  'Current Title, Bug 2108'
373  )
374  ) ,
375  'expected:cibles' => array(
376  'ilink_bug_2108',
377  'bug_2108'
378  )
379  )
380  ) ,
381  array(
382  array(
383  'fam' => 'TST_AUTOCOMPLETION',
384  'attr' => 'BUG_2108_OK',
385  'http:vars' => array(
386  '_s_text' => 'Test Bug 2108 OK',
387  '_ilink_bug_2108_ok' => 'Current Title'
388  ) ,
389  'expected:warning' => '',
390  'expected:results' => array(
391  array(
392  'Test Bug 2108 OK, Current Title',
393  'Test Bug 2108 OK, Current Title'
394  )
395  ) ,
396  'expected_cibles' => array(
397  'bug_2108_ok',
398  'ilink_bug_2108_ok'
399  )
400  )
401  ) ,
402  array(
403  array(
404  'fam' => 'TST_AUTOCOMPLETION',
405  'document' => 'TST_AUTOCOMPLETION_2',
406  'attr' => 'RELATION_2',
407  'http:vars' => array(
408  '_s_text' => 'Test Relation 2',
409  '_ilink_relation_1' => 'Document test',
410  '_ilink_relation_2' => 'Current Title'
411  ) ,
412  'expected:warning' => '',
413  'expected:results' => array(
414  array(
415  'Current Title, Document test, Test Relation 2',
416  'Current Title, Document test, Test Relation 2'
417  )
418  ) ,
419  'expected:cibles' => array(
420  'ilink_relation_1',
421  'relation_2',
422  'ilink_relation_2'
423  )
424  )
425  ) ,
426  array(
427  array(
428  'fam' => 'TST_AUTOCOMPLETION',
429  'document' => 'TST_AUTOCOMPLETION_3',
430  'attr' => 'RELATION_3',
431  'http:vars' => array(
432  '_s_text' => 'Test Relation 3',
433  '_ilink_relation_1' => 'Document test',
434  '_ilink_relation_3' => 'Current Title'
435  ) ,
436  'expected:warning' => '',
437  'expected:results' => array(
438  array(
439  'Test Relation 3, Current Title, Document test',
440  'Test Relation 3, Current Title, Document test'
441  )
442  ) ,
443  'expected:cibles' => array(
444  'ilink_relation_3',
445  'relation_3',
446  'ilink_relation_1'
447  )
448  )
449  ) ,
450  // Bug 5151
451  array(
452  array(
453  'fam' => 'TST_AUTOCOMPLETION',
454  'attr' => 'BUG_5151_FAM',
455  'http:vars' => array(
456  '_bug_5151_fam' => 'Relation'
457  ) ,
458  'expected:warning' => '',
459  'expected:results' => array(
460  array(
461  'Test Relation 2',
462  'Test Relation 2'
463  ) ,
464  array(
465  'Test Relation 3',
466  'Test Relation 3'
467  )
468  ) ,
469  'expected:cibles' => array(
470  'bug_5151_fam'
471  )
472  )
473  ) ,
474  array(
475  array(
476  'fam' => 'TST_AUTOCOMPLETION',
477  'attr' => 'BUG_5151_PARAM',
478  'http:vars' => array() ,
479  'expected:warning' => '',
480  'expected:results' => array(
481  array(
482  'OK'
483  )
484  ) ,
485  'expected:cibles' => array(
486  'bug_5151_param'
487  )
488  )
489  ) ,
490  // defaultphpfunc=yes
491  array(
492  array(
493  'fam' => 'TST_AUTOCOMPLETION',
494  'attr' => 'DEFAULTPHPFUNC_DOCID',
495  'http:vars' => array(
496  '_ilink_defaultphpfunc_docid' => 'Test Relation',
497  'defaultphpfunc' => 'yes'
498  ) ,
499  'expected:warning' => '',
500  'expected:results' => array(
501  array(
502  'Test Relation 2',
503  new \Dcp\Pu\LateNameResolver("TST_AUTOCOMPLETION_2") ,
504  'Test Relation 2'
505  ) ,
506  array(
507  'Test Relation 3',
508  new \Dcp\Pu\LateNameResolver("TST_AUTOCOMPLETION_3") ,
509  'Test Relation 3'
510  )
511  ) ,
512  'expected:cibles' => array(
513  'defaultphpfunc_docid',
514  'ilink_defaultphpfunc_docid'
515  )
516  )
517  ) ,
518  array(
519  array(
520  'fam' => 'TST_AUTOCOMPLETION',
521  'attr' => 'DEFAULTPHPFUNC_ACCOUNT',
522  'http:vars' => array(
523  '_ilink_defaultphpfunc_account' => 'barfirst',
524  'defaultphpfunc' => 'yes'
525  ) ,
526  'expected:warning' => '',
527  'expected:results' => array(
528  array(
529  'barlast barfirst',
530  new \Dcp\Pu\LateNameResolver('TST_AUTOCOMPLETION_U_BAR') ,
531  'barlast barfirst'
532  )
533  ) ,
534  'expected:cibles' => array(
535  'defaultphpfunc_account',
536  'ilink_defaultphpfunc_account'
537  )
538  )
539  )
540  );
541  }
542 }
SetHttpVar($name, $def)
Definition: Lib.Http.php:150
$ret
$file
setUpTestAction($appName, $actionName)
checkCibles(&$expectedCibles, &$actualCibles)
createDoc($dbaccess, $fromid, $control=true, $defaultvalues=true, $temporary=false)
new_Doc($dbaccess, $id= '', $latest=false)
checkResult(&$expectedResult, &$actualResult)
getAutocompletionOptions(&$xp, &$statusNode)
$dbaccess
Definition: checkVault.php:17
if($file) if($subject==""&&$file) if($subject=="") $err
$value
$data
← centre documentaire © anakeen