13 require_once
'PU_testcase_dcp_commonfamily.php';
25 "PU_data_dcp_updateattribute.ods"
33 public function testSetValue($attrid, $newValue)
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);
44 $dl =
$s->search()->getDocumentList();
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));
55 public function testSetValueWithRevision($attrid, $newValue, $unchangedCount, $changedCount)
58 $s->setObjectReturn();
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);
69 $dl =
$s->search()->getDocumentList();
71 $results =
$ua->getResults();
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);
82 $changed = $unchanged = 0;
83 foreach ($results as
$r) {
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)));
94 public function testSetValueWithHistory($attrid, $newValue)
97 $s->setObjectReturn();
98 $s->addFilter(
"name ~ '^TST_DUPTATTR'");
99 $s->setOrder(
'initid');
103 $u->lastname =
"o'Connor";
105 $dl = new \DocumentList(
$s);
107 $ua = new \UpdateAttribute();
108 $ua->useCollection($dl);
111 $ua->setValue($attrid, $newValue);
112 $ua->useTransaction();
114 $dl =
$s->search()->getDocumentList();
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");
122 $this->assertEquals(
$comment, $histo[0][
"comment"], sprintf(
"not good history #%d:%s", $doc->initid, print_r($histo,
true)));
131 $s->setObjectReturn();
132 $s->addFilter(
"name ~ '^TST_DUPTATTR'");
133 $s->setOrder(
'initid');
134 $dl = new \DocumentList(
$s);
136 $ua = new \UpdateAttribute();
137 $ua->useCollection($dl);
138 $ua->useProfileUpdating(
true);
139 if ($newValue)
$ua->setValue($attrid, $newValue);
143 $ns->setObjectReturn();
144 $ns->addFilter(
"name ~ '^TST_DUPTATTR'");
148 $this->assertEquals($expectedCountNames,
$c,
"not the good count for view docs" . print_r($ns->getSearchInfo() ,
true));
154 public function testReplaceValue($attrid, $oldvalue, $newValue, $expectedChangedCount)
157 $ws->setObjectReturn();
158 $ws->addFilter(
"name ~ '^TST_DUPTATTR'");
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;
167 $ws->setObjectReturn();
168 $ws->addFilter(
"name ~ '^TST_DUPTATTR'");
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;
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);
186 $dl =
$s->search()->getDocumentList();
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));
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));
200 $results =
$ua->getResults();
201 $changed = $unchanged = 0;
202 foreach ($results as $r) {
207 $this->assertEquals($expectedChangedCount, $changed,
"not correct changed count");
212 public function testAddValue($attrid, $valueToAdd, $isUnique, $expectedChangedCount)
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) {
221 $expectedAdd[] = $aValue;
227 $noNeedChanged = array();
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;
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);
247 $dl =
$s->search()->getDocumentList();
249 $singleMultiple =
false;
250 $doubleMultiple =
false;
252 if (!$oa) $this->markTestIncomplete(
"cannot find $attrid attribute");
253 if ($oa->isMultipleInArray()) {
254 $doubleMultiple =
true;
255 } elseif ($oa->isMultiple()) {
256 $singleMultiple =
true;
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)));
268 $this->assertEquals($expectedAdd, end($tv) , sprintf(
"value \"%s\" not added : %s", $valueToAdd, implode(
',', $tv)));
273 $results =
$ua->getResults();
274 $changed = $unchanged = 0;
275 foreach ($results as $r) {
276 if ($r->changed) $changed++;
279 $this->assertEquals($expectedChangedCount, $changed,
"not correct changed count");
288 $s->setObjectReturn();
289 $s->addFilter(
"name ~ '^TST_DUPTATTR'");
290 $dl =
$s->search()->getDocumentList();
291 $ua = new \UpdateAttribute();
292 $ua->useCollection($dl);
296 while (!$sua->isFinished()) {
299 $err = $sua->getError();
300 $this->assertNotEmpty(
$err,
"An error must be returned");
301 $this->assertContains($expectedError,
$err,
"not expected error message");
306 public function testRemoveValue($attrid, $valueToRemove, $expectedChangedCount)
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) {
315 $expectedRemove[] = $aValue;
321 $noNeedChanged = array();
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;
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);
340 $dl =
$s->search()->getDocumentList();
342 $singleMultiple =
false;
343 $doubleMultiple =
false;
345 if (!$oa) $this->markTestIncomplete(
"cannot find $attrid attribute");
346 if ($oa->isMultipleInArray()) {
347 $doubleMultiple =
true;
348 } elseif ($oa->isMultiple()) {
349 $singleMultiple =
true;
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)));
364 $this->assertFalse(in_array($expectedRemove, $tv) , sprintf(
"value \"%s\" is not removed : %s", $valueToRemove, implode(
',', $tv)));
369 $results =
$ua->getResults();
370 $changed = $unchanged = 0;
371 foreach ($results as $r) {
376 $this->assertEquals($expectedChangedCount, $changed,
"not correct changed count");
384 $s->setObjectReturn();
385 $s->addFilter(
"name ~ '^TST_DUPTATTR'");
386 $dl =
$s->search()->getDocumentList();
387 $ua = new \UpdateAttribute();
389 $ua->useCollection($dl);
390 $ua->setValue($attrid, $newValue);
393 $this->assertEquals($expectErrorCode, $e->getDcpCode() ,
"erroneous exception code");
396 $this->assertTrue(
true,
false,
"an error must be catched");
402 private function _testBgSetValue($attrid, $newValue)
405 $s->setObjectReturn();
409 $dl = new \DocumentList(
$s);
410 $ua = new \UpdateAttribute();
411 $ua->useCollection($dl);
416 while (!$sua->isFinished()) {
417 error_log($sua->getStatus());
418 error_log($sua->getLastMessage());
423 $dl =
$s->search()->getDocumentList();
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));
static getInitIdFromName($name)
testSetValueWithProfiling($attrid, $newValue, $login, $expectedCountNames)
dataSetValueWithProfiling()
testAddErrorValue($attrid, $valueToAdd, $expectedError)
dataSetValueWithRevision()
testErrorSetValue($attrid, $newValue, $expectErrorCode)
if($file) if($subject==""&&$file) if($subject=="") $err
static getCommonImportFile()