Core  3.2
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
PU_test_dcp_updateattribute.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 TST_UPDTATTR
19  * @static
20  * @return array|string
21  */
22  protected static function getCommonImportFile()
23  {
24  return array(
25  "PU_data_dcp_updateattribute.ods"
26  );
27  }
28 
29  protected $famName = 'TST_UPDTATTR';
30  /**
31  * @dataProvider dataSetValue
32  */
33  public function testSetValue($attrid, $newValue)
34  {
35  $s = new \SearchDoc(self::$dbaccess, $this->famName);
36  $s->setObjectReturn();
37  $s->addFilter("name ~ '^TST_DUPTATTR'");
38  $dl = $s->search()->getDocumentList();
39  $ua = new \UpdateAttribute();
40  $ua->useCollection($dl);
41  $ua->setValue($attrid, $newValue);
42 
43  $s->reset();
44  $dl = $s->search()->getDocumentList();
45  /**
46  * @var \Doc $doc
47  */
48  foreach ($dl as $doc) {
49  $this->assertEquals($newValue, $doc->getRawValue($attrid) , sprintf("doc %s [#%d] not correct value for %s attribute", $doc->title, $doc->id, $attrid));
50  }
51  }
52  /**
53  * @dataProvider dataSetValueWithRevision
54  */
55  public function testSetValueWithRevision($attrid, $newValue, $unchangedCount, $changedCount)
56  {
57  $s = new \SearchDoc(self::$dbaccess, $this->famName);
58  $s->setObjectReturn();
59  $s->setSlice(20);
60  $s->addFilter("name ~ '^TST_DUPTATTR'");
61  $s->setOrder('initid');
62  $dl = $s->search()->getDocumentList();
63  $ua = new \UpdateAttribute();
64  $ua->useCollection($dl);
65  $ua->addRevision(true);
66  $ua->setValue($attrid, $newValue);
67 
68  $s->reset();
69  $dl = $s->search()->getDocumentList();
70 
71  $results = $ua->getResults();
72  /**
73  * @var \Doc $doc
74  */
75  foreach ($dl as $doc) {
76  $this->assertEquals($newValue, $doc->getRawValue($attrid) , sprintf("doc %s [#%d] not correct value for %s attribute", $doc->title, $doc->id, $attrid));
77  if ($results[$doc->initid]->changed) {
78  $this->assertGreaterThan($doc->initid, $doc->id, "revision not done for %s (#%d)", $doc->title, $doc->id);
79  $this->assertTrue($results[$doc->initid]->revised);
80  }
81  }
82  $changed = $unchanged = 0;
83  foreach ($results as $r) {
84  if ($r->changed) {
85  $changed++;
86  } else $unchanged++;
87  }
88  $this->assertEquals($unchangedCount, $changed, sprintf("revised unchanged count mistmatch %s", print_r($results, true)));
89  $this->assertEquals($changedCount, $unchanged, sprintf("revised changed count mistmatch %s", print_r($results, true)));
90  }
91  /**
92  * @dataProvider dataSetValue
93  */
94  public function testSetValueWithHistory($attrid, $newValue)
95  {
96  $s = new \SearchDoc(self::$dbaccess, $this->famName);
97  $s->setObjectReturn();
98  $s->addFilter("name ~ '^TST_DUPTATTR'");
99  $s->setOrder('initid');
100  $s->setSlice(20);
101  // $dl = $s->search();
102  $u = getCurrentUser();
103  $u->lastname = "o'Connor";
104 
105  $dl = new \DocumentList($s);
106 
107  $ua = new \UpdateAttribute();
108  $ua->useCollection($dl);
109  $comment = "coucou";
110  $ua->addHistoryComment($comment);
111  $ua->setValue($attrid, $newValue);
112  $ua->useTransaction();
113  $s->reset();
114  $dl = $s->search()->getDocumentList();
115  /**
116  * @var \Doc $doc
117  */
118  foreach ($dl as $doc) {
119  $this->assertEquals($newValue, $doc->getRawValue($attrid) , sprintf("doc %s [#%d] not correct value for %s attribute", $doc->title, $doc->initid, $attrid));
120  $histo = $doc->getHisto(false, "UPDATE");
121 
122  $this->assertEquals($comment, $histo[0]["comment"], sprintf("not good history #%d:%s", $doc->initid, print_r($histo, true)));
123  }
124  }
125  /**
126  * @dataProvider dataSetValueWithProfiling
127  */
128  public function testSetValueWithProfiling($attrid, $newValue, $login, $expectedCountNames)
129  {
130  $s = new \SearchDoc(self::$dbaccess, $this->famName);
131  $s->setObjectReturn();
132  $s->addFilter("name ~ '^TST_DUPTATTR'");
133  $s->setOrder('initid');
134  $dl = new \DocumentList($s);
135 
136  $ua = new \UpdateAttribute();
137  $ua->useCollection($dl);
138  $ua->useProfileUpdating(true);
139  if ($newValue) $ua->setValue($attrid, $newValue);
140 
141  $this->sudo($login);
142  $ns = new \SearchDoc(self::$dbaccess, $this->famName);
143  $ns->setObjectReturn();
144  $ns->addFilter("name ~ '^TST_DUPTATTR'");
145  $ns->search();
146  $c = $ns->count();
147 
148  $this->assertEquals($expectedCountNames, $c, "not the good count for view docs" . print_r($ns->getSearchInfo() , true));
149  $this->exitSudo();
150  }
151  /**
152  * @dataProvider dataReplaceValue
153  */
154  public function testReplaceValue($attrid, $oldvalue, $newValue, $expectedChangedCount)
155  {
156  $ws = new \SearchDoc(self::$dbaccess, $this->famName);
157  $ws->setObjectReturn();
158  $ws->addFilter("name ~ '^TST_DUPTATTR'");
159 
160  $ws->addFilter("%s ~ E'\\\\y%s\\\\y'", $attrid, $oldvalue);
161  $wdl = $ws->search()->getDocumentList();
162  $needChanged = array();
163  foreach ($wdl as $id => $doc) {
164  $needChanged[] = $doc->initid;
165  }
166  $ws = new \SearchDoc(self::$dbaccess, $this->famName);
167  $ws->setObjectReturn();
168  $ws->addFilter("name ~ '^TST_DUPTATTR'");
169 
170  $ws->addFilter("%s ~ E'\\\\y%s\\\\y'", $attrid, $newValue);
171  $wdl = $ws->search()->getDocumentList();
172  $noNeedChanged = array();
173  foreach ($wdl as $id => $doc) {
174  $noNeedChanged[] = $doc->initid;
175  }
176 
177  $s = new \SearchDoc(self::$dbaccess, $this->famName);
178  $s->setObjectReturn();
179  $s->addFilter("name ~ '^TST_DUPTATTR'");
180  $dl = $s->search()->getDocumentList();
181  $ua = new \UpdateAttribute();
182  $ua->useCollection($dl);
183  $ua->replaceValue($attrid, $oldvalue, $newValue);
184 
185  $s->reset();
186  $dl = $s->search()->getDocumentList();
187  /**
188  * @var \Doc $doc
189  */
190  foreach ($dl as $doc) {
191  $isIn = (preg_match("/\b$newValue\b/", $doc->getRawValue($attrid)) > 0);
192  if (in_array($doc->initid, $needChanged)) {
193  $this->assertTrue($isIn, sprintf("doc %s [#%d] not updated : value is \"%s\" for %s attribute", $doc->title, $doc->initid, $doc->getRawValue($attrid) , $attrid));
194  } else {
195  if (!in_array($doc->initid, $noNeedChanged)) {
196  $this->assertFalse($isIn, sprintf("doc %s [#%d] must not be updated : value is \"%s\" for %s attribute", $doc->title, $doc->initid, $doc->getRawValue($attrid) , $attrid));
197  }
198  }
199  }
200  $results = $ua->getResults();
201  $changed = $unchanged = 0;
202  foreach ($results as $r) {
203  if ($r->changed) {
204  $changed++;
205  } else $unchanged++;
206  }
207  $this->assertEquals($expectedChangedCount, $changed, "not correct changed count");
208  }
209  /**
210  * @dataProvider dataAddValue
211  */
212  public function testAddValue($attrid, $valueToAdd, $isUnique, $expectedChangedCount)
213  {
214  $fam = new_doc(self::$dbaccess, $this->famName);
215  $oa = $fam->getAttribute($attrid);
216  $expectedAdd = $valueToAdd;
217  if (($oa->type == "docid") || ($oa->type == "account")) {
218  if (is_array($valueToAdd)) {
219  foreach ($valueToAdd as $aValue) {
220  $aValue = \UpdateAttribute::getInitIdFromName($aValue);
221  $expectedAdd[] = $aValue;
222  }
223  } else {
224  if (!is_numeric($valueToAdd)) $expectedAdd = \UpdateAttribute::getInitIdFromName($valueToAdd);
225  }
226  }
227  $noNeedChanged = array();
228  if ($isUnique) {
229  $ws = new \SearchDoc(self::$dbaccess, $this->famName);
230  $ws->setObjectReturn();
231  $ws->addFilter("name ~ '^TST_DUPTATTR'");
232  if (!is_array($expectedAdd)) $ws->addFilter("%s ~ E'\\\\y%s\\\\y'", $attrid, $expectedAdd);
233  $wdl = $ws->search()->getDocumentList();
234  foreach ($wdl as $id => $doc) {
235  $noNeedChanged[] = $doc->initid;
236  }
237  }
238  $s = new \SearchDoc(self::$dbaccess, $this->famName);
239  $s->setObjectReturn();
240  $s->addFilter("name ~ '^TST_DUPTATTR'");
241  $dl = $s->search()->getDocumentList();
242  $ua = new \UpdateAttribute();
243  $ua->useCollection($dl);
244  $ua->addValue($attrid, $valueToAdd);
245 
246  $s->reset();
247  $dl = $s->search()->getDocumentList();
248 
249  $singleMultiple = false;
250  $doubleMultiple = false;
251 
252  if (!$oa) $this->markTestIncomplete("cannot find $attrid attribute");
253  if ($oa->isMultipleInArray()) {
254  $doubleMultiple = true;
255  } elseif ($oa->isMultiple()) {
256  $singleMultiple = true;
257  }
258  /**
259  * @var \Doc $doc
260  */
261  foreach ($dl as $doc) {
262  $tv = $doc->getMultipleRawValues($attrid);
263  if ($singleMultiple) {
264  if (!in_array($doc->initid, $noNeedChanged)) {
265  if (is_array($expectedAdd)) {
266  $this->assertEquals(end($expectedAdd) , end($tv) , sprintf("value \"%s\" not added : %s", print_r($valueToAdd, true) , implode(',', $tv)));
267  } else {
268  $this->assertEquals($expectedAdd, end($tv) , sprintf("value \"%s\" not added : %s", $valueToAdd, implode(',', $tv)));
269  }
270  }
271  }
272  }
273  $results = $ua->getResults();
274  $changed = $unchanged = 0;
275  foreach ($results as $r) {
276  if ($r->changed) $changed++;
277  else $unchanged++;
278  }
279  $this->assertEquals($expectedChangedCount, $changed, "not correct changed count");
280  }
281  /**
282  * @dataProvider dataAddErrorValue
283  */
284  public function testAddErrorValue($attrid, $valueToAdd, $expectedError)
285  {
286 
287  $s = new \SearchDoc(self::$dbaccess, $this->famName);
288  $s->setObjectReturn();
289  $s->addFilter("name ~ '^TST_DUPTATTR'");
290  $dl = $s->search()->getDocumentList();
291  $ua = new \UpdateAttribute();
292  $ua->useCollection($dl);
293  $statusFile = $ua->bgAddValue($attrid, $valueToAdd);
294  $sua = new \UpdateAttributeStatus($statusFile);
295 
296  while (!$sua->isFinished()) {
297  sleep(1);
298  }
299  $err = $sua->getError();
300  $this->assertNotEmpty($err, "An error must be returned");
301  $this->assertContains($expectedError, $err, "not expected error message");
302  }
303  /**
304  * @dataProvider dataRemoveValue
305  */
306  public function testRemoveValue($attrid, $valueToRemove, $expectedChangedCount)
307  {
308  $fam = new_doc(self::$dbaccess, $this->famName);
309  $oa = $fam->getAttribute($attrid);
310  $expectedRemove = $valueToRemove;
311  if (($oa->type == "docid") || ($oa->type == "account")) {
312  if (is_array($valueToRemove)) {
313  foreach ($valueToRemove as $aValue) {
314  $aValue = \UpdateAttribute::getInitIdFromName($aValue);
315  $expectedRemove[] = $aValue;
316  }
317  } else {
318  if (!is_numeric($valueToRemove)) $expectedRemove = \UpdateAttribute::getInitIdFromName($valueToRemove);
319  }
320  }
321  $noNeedChanged = array();
322  $ws = new \SearchDoc(self::$dbaccess, $this->famName);
323  $ws->setObjectReturn();
324  $ws->addFilter("name ~ '^TST_DUPTATTR'");
325  if (!is_array($expectedRemove)) $ws->addFilter("%s ~ E'\\\\y%s\\\\y'", $attrid, $expectedRemove);
326  $wdl = $ws->search()->getDocumentList();
327  foreach ($wdl as $id => $doc) {
328  $noNeedChanged[] = $doc->initid;
329  }
330 
331  $s = new \SearchDoc(self::$dbaccess, $this->famName);
332  $s->setObjectReturn();
333  $s->addFilter("name ~ '^TST_DUPTATTR'");
334  $dl = $s->search()->getDocumentList();
335  $ua = new \UpdateAttribute();
336  $ua->useCollection($dl);
337  $ua->removeValue($attrid, $valueToRemove);
338 
339  $s->reset();
340  $dl = $s->search()->getDocumentList();
341 
342  $singleMultiple = false;
343  $doubleMultiple = false;
344 
345  if (!$oa) $this->markTestIncomplete("cannot find $attrid attribute");
346  if ($oa->isMultipleInArray()) {
347  $doubleMultiple = true;
348  } elseif ($oa->isMultiple()) {
349  $singleMultiple = true;
350  }
351  /**
352  * @var \Doc $doc
353  */
354  foreach ($dl as $doc) {
355  $tv = $doc->getMultipleRawValues($attrid);
356  if ($singleMultiple) {
357  if (!in_array($doc->initid, $noNeedChanged)) {
358  if (is_array($expectedRemove)) {
359  foreach ($expectedRemove as $aExpect) {
360  $this->assertFalse(in_array($aExpect, $tv) , sprintf("value \"%s\" is not removed : %s", $aExpect, implode(',', $tv)));
361  }
362  } else {
363 
364  $this->assertFalse(in_array($expectedRemove, $tv) , sprintf("value \"%s\" is not removed : %s", $valueToRemove, implode(',', $tv)));
365  }
366  }
367  }
368  }
369  $results = $ua->getResults();
370  $changed = $unchanged = 0;
371  foreach ($results as $r) {
372  if ($r->changed) {
373  $changed++;
374  } else $unchanged++;
375  }
376  $this->assertEquals($expectedChangedCount, $changed, "not correct changed count");
377  }
378  /**
379  * @dataProvider dataErrorSetValue
380  */
381  public function testErrorSetValue($attrid, $newValue, $expectErrorCode)
382  {
383  $s = new \SearchDoc(self::$dbaccess, $this->famName);
384  $s->setObjectReturn();
385  $s->addFilter("name ~ '^TST_DUPTATTR'");
386  $dl = $s->search()->getDocumentList();
387  $ua = new \UpdateAttribute();
388  try {
389  $ua->useCollection($dl);
390  $ua->setValue($attrid, $newValue);
391  }
392  catch(\Dcp\Upat\Exception $e) {
393  $this->assertEquals($expectErrorCode, $e->getDcpCode() , "erroneous exception code");
394  return;
395  }
396  $this->assertTrue(true, false, "an error must be catched");
397  }
398  /**
399  * @dataProvider dataSetValue
400  * cannot use it in bg mode cause postgresql transaction locking
401  */
402  private function _testBgSetValue($attrid, $newValue)
403  {
404  $s = new \SearchDoc(self::$dbaccess, $this->famName);
405  $s->setObjectReturn();
406  $s->setSlice(20);
407  $s->setStart(5);
408  //$dl = $s->search()->getDocumentList();
409  $dl = new \DocumentList($s);
410  $ua = new \UpdateAttribute();
411  $ua->useCollection($dl);
412  $statusFile = $ua->bgSetValue($attrid, $newValue);
413  $sua = new \UpdateAttributeStatus($statusFile);
414  error_log($statusFile);
415 
416  while (!$sua->isFinished()) {
417  error_log($sua->getStatus());
418  error_log($sua->getLastMessage());
419  usleep(800000);
420  }
421 
422  $s->reset();
423  $dl = $s->search()->getDocumentList();
424  /**
425  * @var \Doc $doc
426  */
427  foreach ($dl as $doc) {
428  $this->assertEquals($newValue, $doc->getRawValue($attrid) , sprintf("doc %s [#%d] not correct value for %s attribute", $doc->title, $doc->id, $attrid));
429  }
430  }
431 
432  public function dataSetValueWithProfiling()
433  {
434  return array(
435 
436  array(
437  "TST_REDACTOR",
438  "TST_UUPDTATTR2",
439  "admin",
440  28
441  ) ,
442  array(
443  "TST_REDACTOR",
444  "",
445  "tstLoginUpdtU2",
446  18
447  ) ,
448  array(
449  "TST_REDACTOR",
450  "TST_UUPDTATTR2",
451  "tstLoginUpdtU2",
452  28
453  ) ,
454  array(
455  "TST_REDACTOR",
456  "TST_UUPDTATTR1",
457  "tstLoginUpdtU2",
458  0
459  )
460  );
461  }
462  public function dataAddErrorValue()
463  {
464 
465  return array(
466  array(
467  "TST_NOTHING",
468  "3",
469  "not found"
470  )
471  );
472  }
473 
474  public function dataErrorSetValue()
475  {
476 
477  return array(
478  array(
479  "TST_NOTHING",
480  "3",
481  "UPAT0004"
482  )
483  );
484  }
485 
486  public function dataSetValue()
487  {
488 
489  return array(
490  array(
491  "TST_ENUM",
492  "3"
493  ) ,
494  array(
495  "TST_ENUMS",
496  "3\n4\n6"
497  )
498  );
499  }
500 
501  public function dataRemoveValue()
502  {
503 
504  return array(
505  array(
506  "TST_INTS",
507  "3",
508  3
509  ) ,
510  array(
511  "TST_DOUBLES",
512  "3.1",
513  1
514  ) ,
515  array(
516  "TST_DOUBLES",
517  "334.64",
518  9
519  ) ,
520  array(
521  "TST_TEXTS",
522  "Deux lignes",
523  9
524  ) ,
525  array(
526  "TST_RELMUL",
527  "TST_DUPTATTR2",
528  2
529  ) ,
530  array(
531  "TST_RELMULS",
532  "TST_DUPTATTR7",
533  7
534  ) ,
535  array(
536  "TST_INTS",
537  array(
538  "4",
539  "5",
540  "10"
541  ) ,
542  13
543  )
544  );
545  }
546  public function dataAddValue()
547  {
548 
549  return array(
550  array(
551  "TST_INTS",
552  "3",
553  false,
554  28
555  ) ,
556  array(
557  "TST_DOUBLES",
558  "3.14159",
559  false,
560  28
561  ) ,
562  array(
563  "TST_TEXTS",
564  "C'est un test",
565  false,
566  28
567  ) ,
568  array(
569  "TST_ENUMS2",
570  "3",
571  false,
572  28
573  ) ,
574  array(
575  "TST_ENUMS",
576  "3",
577  true,
578  25
579  ) ,
580  array(
581  "TST_RELMUL",
582  "TST_DUPTATTR1",
583  true,
584  26
585  ) ,
586  array(
587  "TST_RELLATESTS",
588  array(
589  "TST_DUPTATTR2",
590  "TST_DUPTATTR3"
591  ) ,
592  false,
593  28
594  )
595  );
596  }
597  public function dataReplaceValue()
598  {
599 
600  return array(
601  array(
602  "TST_ENUM",
603  "5",
604  "10",
605  2
606  ) ,
607  array(
608  "TST_ENUMS",
609  "3",
610  "6",
611  3
612  ) ,
613  array(
614  "TST_ENUM",
615  "1",
616  "11",
617  3
618  ) ,
619 
620  array(
621  "TST_INTS",
622  "4",
623  "115",
624  3
625  ) ,
626  array(
627  "TST_ENUMS",
628  "1",
629  "11",
630  16
631  ) ,
632  array(
633  "TST_TITLE",
634  "Titre 3",
635  "C'est le n°3",
636  1
637  )
638  );
639  }
640 
641  public function dataSetValueWithRevision()
642  {
643 
644  return array(
645  array(
646  "TST_ENUM",
647  "6",
648  18,
649  2
650  ) ,
651  array(
652  "TST_ENUMS",
653  "1\n6",
654  19,
655  1
656  )
657  );
658  }
659 }
660 ?>
static getInitIdFromName($name)
static sudo($login)
$comment
Definition: fdl_execute.php:35
testSetValueWithProfiling($attrid, $newValue, $login, $expectedCountNames)
testAddErrorValue($attrid, $valueToAdd, $expectedError)
if($famId) $s
$statusFile
$login
Definition: dav.php:40
getCurrentUser()
Definition: Lib.Common.php:250
testErrorSetValue($attrid, $newValue, $expectErrorCode)
$dbaccess
Definition: checkVault.php:17
if($file) if($subject==""&&$file) if($subject=="") $err
← centre documentaire © anakeen