From 01551942b3449663d7dbf11e93bbd83fdc8e5f04 Mon Sep 17 00:00:00 2001 From: Eric Date: Wed, 26 Sep 2018 15:46:11 +0200 Subject: [PATCH] =?UTF-8?q?N=C2=B0917:=20Fix=20Bulk=20modify=20for=20AttCo?= =?UTF-8?q?deSet?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- application/cmdbabstract.class.inc.php | 14 +- core/attributedef.class.inc.php | 145 ++++++++++++++------ core/cmdbobject.class.inc.php | 4 +- core/dbobject.class.php | 2 +- core/ormset.class.inc.php | 27 ++-- core/ormtagset.class.inc.php | 177 ++++--------------------- test/core/ormTagSetTest.php | 96 +++++++------- 7 files changed, 201 insertions(+), 264 deletions(-) diff --git a/application/cmdbabstract.class.inc.php b/application/cmdbabstract.class.inc.php index aed1eeed6..cbe67cda6 100644 --- a/application/cmdbabstract.class.inc.php +++ b/application/cmdbabstract.class.inc.php @@ -2057,11 +2057,11 @@ EOF $oPage->add_dict_entry('Core:AttributeTagSet:placeholder'); - /** @var \ormSet $value */ if (isset($aArgs['this'])) { $oAttDef->SetTargetClass($aArgs['this']); } + /** @var \ormSet $value */ $sJson = $oAttDef->GetJsonForWidget($value); $sInputId = "attr_{$sFormPrefix}{$sAttCode}"; $sHTMLValue = "
{$sValidationSpan}{$sReloadSpan}"; @@ -3975,9 +3975,9 @@ EOF { $currValue = ' '; // Don't put an empty string, in case the field would be considered as mandatory... } - elseif ($currValue instanceof ormTagSet) + elseif ($currValue instanceof ormSet) { - $currValue = implode(' ', $currValue->GetValue()); + $currValue = $oAttDef->GetEditValue($currValue, $oObj); } if (is_object($currValue)) { @@ -4107,12 +4107,8 @@ EOF { continue; } - $aTagCodes = array(); - if (!empty($sValues)) - { - $aTagCodes = explode(' ', $sValues); - } - $oTagSet->GenerateDiffFromTags($aTagCodes); + $aTagCodes = $oAttDef->FromStringToArray($sValues); + $oTagSet->GenerateDiffFromArray($aTagCodes); } $oDummyObj->Set($sAttCode, $oTagSet); } diff --git a/core/attributedef.class.inc.php b/core/attributedef.class.inc.php index 3d56fe9aa..638bae046 100644 --- a/core/attributedef.class.inc.php +++ b/core/attributedef.class.inc.php @@ -6744,8 +6744,8 @@ class AttributeTagSet extends AttributeSet } else { - $aJson['partial_values'] = $oValue->GetModifiedTags(); - $aJson['orig_value'] = array_merge($oValue->GetValue(), $oValue->GetModifiedTags()); + $aJson['partial_values'] = $oValue->GetModified(); + $aJson['orig_value'] = array_merge($oValue->GetValues(), $oValue->GetModified()); } $aJson['added'] = array(); $aJson['removed'] = array(); @@ -6756,6 +6756,20 @@ class AttributeTagSet extends AttributeSet return json_encode($aJson); } + public function FromStringToArray($proposedValue) + { + $aValues = array(); + if (!empty($proposedValue)) + { + foreach(explode(' ', $proposedValue) as $sCode) + { + $sValue = trim($sCode); + $aValues[] = $sValue; + } + } + return $aValues; + } + /** * Extract all existing tags from a string and ignore bad tags * @@ -6768,7 +6782,7 @@ class AttributeTagSet extends AttributeSet */ public function GetExistingTagsFromString($sValue, $bNoLimit = false) { - $aTagCodes = explode(' ', "$sValue"); + $aTagCodes = $this->FromStringToArray("$sValue"); $sAttCode = $this->GetCode(); $sClass = MetaModel::GetAttributeOrigin($this->GetHostClass(), $sAttCode); if ($bNoLimit) @@ -6796,7 +6810,7 @@ class AttributeTagSet extends AttributeSet } } } - $oTagSet->SetValue($aGoodTags); + $oTagSet->SetValues($aGoodTags); return $oTagSet; } @@ -6819,7 +6833,7 @@ class AttributeTagSet extends AttributeSet } if ($value instanceof ormTagSet) { - $aValues = $value->GetValue(); + $aValues = $value->GetValues(); return implode(' ', $aValues); } @@ -6887,17 +6901,20 @@ class AttributeTagSet extends AttributeSet * @param $proposedValue * @param $oHostObj * + * @param bool $bIgnoreErrors + * * @return mixed - * @throws \Exception + * @throws \CoreException + * @throws \CoreUnexpectedValue */ - public function MakeRealValue($proposedValue, $oHostObj) + public function MakeRealValue($proposedValue, $oHostObj, $bIgnoreErrors = false) { $oTagSet = new ormTagSet(MetaModel::GetAttributeOrigin($this->GetHostClass(), $this->GetCode()), $this->GetCode(), $this->GetMaxItems()); if (is_string($proposedValue) && !empty($proposedValue)) { $proposedValue = trim("$proposedValue"); - $aTagCodes = explode(' ', $proposedValue); - $oTagSet->SetValue($aTagCodes); + $aTagCodes = $this->FromStringToArray($proposedValue); + $oTagSet->SetValues($aTagCodes); } elseif ($proposedValue instanceof ormTagSet) { @@ -6958,7 +6975,7 @@ class AttributeTagSet extends AttributeSet } /** @var \ormTagSet $proposedValue */ - return count($proposedValue->GetValue()) == 0; + return count($proposedValue->GetValues()) == 0; } /** @@ -7008,7 +7025,7 @@ class AttributeTagSet extends AttributeSet } if ($value instanceof ormTagSet) { - $aValues = $value->GetValue(); + $aValues = $value->GetValues(); return implode(' ', $aValues); } @@ -7040,7 +7057,7 @@ class AttributeTagSet extends AttributeSet } else { - $aValues = $value->GetValue(); + $aValues = $value->GetValues(); } if (empty($aValues)) { @@ -7060,7 +7077,7 @@ class AttributeTagSet extends AttributeSet { // unknown tags are present display the code instead } - $aTagCodes = explode(' ', $value); + $aTagCodes = $this->FromStringToArray($value); $aValues = array(); $oTagSet = new ormTagSet(MetaModel::GetAttributeOrigin($this->GetHostClass(), $this->GetCode()), $this->GetCode(), $this->GetMaxItems()); @@ -7068,7 +7085,7 @@ class AttributeTagSet extends AttributeSet { try { - $oTagSet->AddTag($sTagCode); + $oTagSet->Add($sTagCode); } catch (Exception $e) { $aValues[] = $sTagCode; @@ -7096,8 +7113,8 @@ class AttributeTagSet extends AttributeSet { $sResult = Dict::Format('Change:AttName_Changed', $this->GetLabel()).", "; - $aNewValues = explode(' ', $sNewValue); - $aOldValues = explode(' ', $sOldValue); + $aNewValues = $this->FromStringToArray($sNewValue); + $aOldValues = $this->FromStringToArray($sOldValue); $aDelta['removed'] = array_diff($aOldValues, $aNewValues); $aDelta['added'] = array_diff($aNewValues, $aOldValues); @@ -7194,7 +7211,7 @@ class AttributeTagSet extends AttributeSet } else { - $sHtml .= ''.$oTag.''; + $sHtml .= ''.$oTag.''; } } $sHtml .= ''; @@ -7221,7 +7238,7 @@ class AttributeTagSet extends AttributeSet } else { - $aValues = $value->GetValue(); + $aValues = $value->GetValues(); } if (!empty($aValuess)) { @@ -7261,7 +7278,7 @@ class AttributeTagSet extends AttributeSet } else { - $aValues = $value->GetValue(); + $aValues = $value->GetValues(); } $sRes = implode($sSepItem, $aValues); } @@ -7306,7 +7323,7 @@ class AttributeTagSet extends AttributeSet } else { - $aValues = $value->GetValue(); + $aValues = $value->GetValues(); $sSep = ' '; } @@ -7338,7 +7355,7 @@ class AttributeTagSet extends AttributeSet $aRet = array(); if (is_object($value) && ($value instanceof ormTagSet)) { - $aRet = $value->GetValue(); + $aRet = $value->GetValues(); } return $aRet; @@ -7358,7 +7375,7 @@ class AttributeTagSet extends AttributeSet public function FromJSONToValue($json) { $oSet = new ormTagSet($this->GetHostClass(), $this->GetCode(), $this->GetMaxItems()); - $oSet->SetValue($json); + $oSet->SetValues($json); return $oSet; } @@ -7415,7 +7432,7 @@ class AttributeTagSet extends AttributeSet { if ($value instanceof ormTagSet) { - $aValues = $value->GetValue(); + $aValues = $value->GetValues(); return implode(' ', $aValues); } @@ -8750,7 +8767,7 @@ class AttributeSubItem extends AttributeDefinition static public function LoadInObject() { return false; - } // if this verb returns false, then GetValue must be implemented + } // if this verb returns false, then GetValues must be implemented /** * Used by DBOBject::Get() @@ -9367,9 +9384,9 @@ class AttributeSet extends AttributeDBFieldVoid $aJson = array(); // possible_values - $aSetObjectData = $this->GetAllowedValues(); + $aAllowedValues = $this->GetAllowedValues(); $aSetKeyValData = array(); - foreach($aSetObjectData as $sCode => $sLabel) + foreach($aAllowedValues as $sCode => $sLabel) { $aSetKeyValData[] = [ 'code' => $sCode, @@ -9385,8 +9402,24 @@ class AttributeSet extends AttributeDBFieldVoid } else { - $aJson['partial_values'] = $oValue->GetModified(); - $aJson['orig_value'] = array_merge($oValue->GetValues(), $oValue->GetModified()); + $aPartialValues = $oValue->GetModified(); + foreach ($aPartialValues as $key => $value) + { + if (!isset($aAllowedValues[$value])) + { + unset($aPartialValues[$key]); + } + } + $aJson['partial_values'] = array_values($aPartialValues); + $aOrigValues = array_merge($oValue->GetValues(), $oValue->GetModified()); + foreach ($aOrigValues as $key => $value) + { + if (!isset($aAllowedValues[$value])) + { + unset($aOrigValues[$key]); + } + } + $aJson['orig_value'] = array_values($aOrigValues); } $aJson['added'] = array(); $aJson['removed'] = array(); @@ -9409,7 +9442,7 @@ class AttributeSet extends AttributeDBFieldVoid public function GetEditClass() { - return "List"; + return "Set"; } public function GetEditValue($value, $oHostObj = null) @@ -9442,6 +9475,20 @@ class AttributeSet extends AttributeDBFieldVoid return 255; } + public function FromStringToArray($proposedValue) + { + $aValues = array(); + if (!empty($proposedValue)) + { + foreach(explode(',', $proposedValue) as $sCode) + { + $sValue = trim($sCode); + $aValues[] = $sValue; + } + } + return $aValues; + } + /** * @param array $aCols * @param string $sPrefix @@ -9453,7 +9500,7 @@ class AttributeSet extends AttributeDBFieldVoid { $sValue = $aCols["$sPrefix"]; - return $this->MakeRealValue($sValue, null); + return $this->MakeRealValue($sValue, null, true); } /** @@ -9476,21 +9523,19 @@ class AttributeSet extends AttributeDBFieldVoid * @param $proposedValue * @param \DBObject $oHostObj * + * @param bool $bIgnoreErrors + * * @return mixed - * @throws \Exception + * @throws \CoreException + * @throws \CoreUnexpectedValue */ - public function MakeRealValue($proposedValue, $oHostObj) + public function MakeRealValue($proposedValue, $oHostObj, $bIgnoreErrors = false) { $oSet = new ormSet(MetaModel::GetAttributeOrigin($this->GetHostClass(), $this->GetCode()), $this->GetCode(), $this->GetMaxItems()); if (is_string($proposedValue) && !empty($proposedValue)) { $proposedValue = trim("$proposedValue"); - $aValues = array(); - foreach (explode(',', $proposedValue) as $sCode) - { - $sValue = trim($sCode); - $aValues[] = $sValue; - } + $aValues = $this->FromStringToArray($proposedValue); $oSet->SetValues($aValues); } elseif ($proposedValue instanceof ormSet) @@ -9700,17 +9745,23 @@ class AttributeClassAttCodeSet extends AttributeSet * @param $proposedValue * @param \DBObject $oHostObj * + * @param bool $bIgnoreErrors + * * @return mixed + * @throws \CoreException + * @throws \CoreUnexpectedValue + * @throws \OQLException * @throws \Exception */ - public function MakeRealValue($proposedValue, $oHostObj) + public function MakeRealValue($proposedValue, $oHostObj, $bIgnoreErrors = false) { $oSet = new ormSet(MetaModel::GetAttributeOrigin($this->GetHostClass(), $this->GetCode()), $this->GetCode(), $this->GetMaxItems()); $this->SetTargetClass($oHostObj); $aAllowedAttributes = $this->GetAllowedValues(); + $aInvalidAttCodes = array(); if (is_string($proposedValue) && !empty($proposedValue)) { - $proposedValue = trim("$proposedValue"); + $proposedValue = trim($proposedValue); $aValues = array(); foreach(explode(',', $proposedValue) as $sValue) { @@ -9721,7 +9772,7 @@ class AttributeClassAttCodeSet extends AttributeSet } else { - throw new CoreUnexpectedValue("The attribute {$sAttCode} does not exist in class {$this->sTargetClass}"); + $aInvalidAttCodes[] = $sAttCode; } } $oSet->SetValues($aValues); @@ -9730,6 +9781,10 @@ class AttributeClassAttCodeSet extends AttributeSet { $oSet = $proposedValue; } + if (!empty($aInvalidAttCodes) && !$bIgnoreErrors) + { + throw new CoreUnexpectedValue("The attribute(s) ".implode(', ', $aInvalidAttCodes)." are invalid for class {$this->sTargetClass}"); + } return $oSet; } @@ -9757,7 +9812,13 @@ class AttributeClassAttCodeSet extends AttributeSet $aLocalizedValues = array(); foreach($value as $sAttCode) { - $aLocalizedValues[] = MetaModel::GetLabel($this->sTargetClass, $sAttCode); + try + { + $aLocalizedValues[] = MetaModel::GetLabel($this->sTargetClass, $sAttCode); + } catch (Exception $e) + { + // Ignore bad values + } } $value = $aLocalizedValues; } diff --git a/core/cmdbobject.class.inc.php b/core/cmdbobject.class.inc.php index 8f2cf1485..e92aa45f6 100644 --- a/core/cmdbobject.class.inc.php +++ b/core/cmdbobject.class.inc.php @@ -417,8 +417,8 @@ abstract class CMDBObject extends DBObject $oMyChangeOp->Set("objclass", get_class($this)); $oMyChangeOp->Set("objkey", $this->GetKey()); $oMyChangeOp->Set("attcode", $sAttCode); - $oMyChangeOp->Set("oldvalue", implode(' ', $original->GetValue())); - $oMyChangeOp->Set("newvalue", implode(' ', $value->GetValue())); + $oMyChangeOp->Set("oldvalue", implode(' ', $original->GetValues())); + $oMyChangeOp->Set("newvalue", implode(' ', $value->GetValues())); $iId = $oMyChangeOp->DBInsertNoReload(); } else diff --git a/core/dbobject.class.php b/core/dbobject.class.php index aaad1cb10..09c6be54e 100644 --- a/core/dbobject.class.php +++ b/core/dbobject.class.php @@ -1285,7 +1285,7 @@ abstract class DBObject implements iDisplay $oTag = new ormTagSet(get_class($this), $sAttCode); try { - $oTag->SetValue(explode(' ', $toCheck)); + $oTag->SetValues(explode(' ', $toCheck)); } catch (Exception $e) { return "Tag value '$toCheck' is not a valid tag list"; diff --git a/core/ormset.class.inc.php b/core/ormset.class.inc.php index 99efeb5d9..490e0fcee 100644 --- a/core/ormset.class.inc.php +++ b/core/ormset.class.inc.php @@ -24,36 +24,36 @@ * Date: 24/08/2018 * Time: 14:35 */ -final class ormSet +class ormSet { - private $sClass; // class of the field - private $sAttCode; // attcode of the field - private $aOriginalObjects = null; + protected $sClass; // class of the field + protected $sAttCode; // attcode of the field + protected $aOriginalObjects = null; /** * Object from the original set, minus the removed objects */ - private $aPreserved = array(); + protected $aPreserved = array(); /** * New items */ - private $aAdded = array(); + protected $aAdded = array(); /** * Removed items */ - private $aRemoved = array(); + protected $aRemoved = array(); /** * Modified items (mass edit) */ - private $aModified = array(); + protected $aModified = array(); /** * @var int Max number of tags in collection */ - private $iLimit; + protected $iLimit; /** * __toString magical function overload. @@ -231,8 +231,8 @@ final class ormSet /** * Apply a delta to the current ItemSet - * $aDelta['added] = array of tag code for only the added tags - * $aDelta['removed'] = array of tag code for only the removed tags + * $aDelta['added] = array of added items + * $aDelta['removed'] = array of removed items * * @param $aDelta * @@ -254,6 +254,9 @@ final class ormSet $this->Add($oItem); } } + + // Reset the object + $this->SetValues($this->GetValues()); } /** @@ -347,7 +350,7 @@ final class ormSet * * @throws \CoreException */ - public function GenerateDiffFromItems($aItems) + public function GenerateDiffFromArray($aItems) { foreach($this->GetValues() as $oCurrentItem) { diff --git a/core/ormtagset.class.inc.php b/core/ormtagset.class.inc.php index f06ce9efb..ab4cdfdf6 100644 --- a/core/ormtagset.class.inc.php +++ b/core/ormtagset.class.inc.php @@ -24,55 +24,8 @@ * Date: 24/08/2018 * Time: 14:35 */ -final class ormTagSet +final class ormTagSet extends ormSet { - private $sClass; // class of the tag field - private $sAttCode; // attcode of the tag field - private $aOriginalObjects = null; - - /** - * Object from the original set, minus the removed objects - * - * @var DBObject[] array of iObjectId => DBObject - */ - private $aPreserved = array(); - - /** - * @var DBObject[] New items - */ - private $aAdded = array(); - - /** - * @var DBObject[] Removed items - */ - private $aRemoved = array(); - - /** - * @var DBObject[] Modified items (mass edit) - */ - private $aModified = array(); - - /** - * @var int Max number of tags in collection - */ - private $iLimit; - - /** - * __toString magical function overload. - */ - public function __toString() - { - $aValue = $this->GetValue(); - if (!empty($aValue)) - { - return implode(' ', $aValue); - } - else - { - return ' '; - } - } - /** * ormTagSet constructor. * @@ -84,31 +37,7 @@ final class ormTagSet */ public function __construct($sClass, $sAttCode, $iLimit = 12) { - $this->sAttCode = $sAttCode; - - $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode); - if (!$oAttDef instanceof AttributeTagSet) - { - throw new Exception("ormTagSet: field {$sClass}:{$sAttCode} is not a tag"); - } - $this->sClass = $sClass; - $this->iLimit = $iLimit; - } - - /** - * @return string - */ - public function GetClass() - { - return $this->sClass; - } - - /** - * @return string - */ - public function GetAttCode() - { - return $this->sAttCode; + parent::__construct($sClass, $sAttCode, $iLimit); } /** @@ -118,7 +47,7 @@ final class ormTagSet * @throws \CoreException * @throws \CoreUnexpectedValue when a code is invalid */ - public function SetValue($aTagCodes) + public function SetValues($aTagCodes) { if (!is_array($aTagCodes)) { @@ -152,15 +81,10 @@ final class ormTagSet } } - private function GetCount() - { - return count($this->aPreserved) + count($this->aAdded) - count($this->aRemoved); - } - /** * @return array of tag codes */ - public function GetValue() + public function GetValues() { $aValues = array(); foreach($this->aPreserved as $sTagCode => $oTag) @@ -183,6 +107,7 @@ final class ormTagSet public function GetLabels() { $aTags = array(); + /** @var \TagSetFieldData $oTag */ foreach($this->aPreserved as $sTagCode => $oTag) { try @@ -300,21 +225,21 @@ final class ormTagSet * @throws \CoreUnexpectedValue * @throws \Exception */ - public function GetDelta(ormTagSet $oOtherTagSet) + public function GetDelta(ormSet $oOtherTagSet) { $oTag = new ormTagSet($this->sClass, $this->sAttCode); // Set the initial value - $aOrigTagCodes = $this->GetValue(); - $oTag->SetValue($aOrigTagCodes); + $aOrigTagCodes = $this->GetValues(); + $oTag->SetValues($aOrigTagCodes); // now remove everything foreach($aOrigTagCodes as $sTagCode) { - $oTag->RemoveTag($sTagCode); + $oTag->Remove($sTagCode); } // now add the tags of the other TagSet - foreach($oOtherTagSet->GetValue() as $sTagCode) + foreach($oOtherTagSet->GetValues() as $sTagCode) { - $oTag->AddTag($sTagCode); + $oTag->Add($sTagCode); } $aDelta = array(); $aDelta['added'] = $oTag->GetAddedCodes(); @@ -340,17 +265,17 @@ final class ormTagSet { $oTag = new ormTagSet($this->sClass, $this->sAttCode); // Set the initial value - $aOrigTagCodes = $this->GetValue(); - $oTag->SetValue($aOrigTagCodes); + $aOrigTagCodes = $this->GetValues(); + $oTag->SetValues($aOrigTagCodes); // now remove everything foreach($aOrigTagCodes as $sTagCode) { - $oTag->RemoveTag($sTagCode); + $oTag->Remove($sTagCode); } // now add the tags of the other TagSet - foreach($oOtherTagSet->GetValue() as $sTagCode) + foreach($oOtherTagSet->GetValues() as $sTagCode) { - $oTag->AddTag($sTagCode); + $oTag->Add($sTagCode); } $aDelta = array(); $aDelta['added'] = $oTag->GetAddedTags(); @@ -362,7 +287,7 @@ final class ormTagSet /** * @return string[] list of codes for partial entries */ - public function GetModifiedTags() + public function GetModified() { $aModifiedTagCodes = array_keys($this->aModified); sort($aModifiedTagCodes); @@ -370,34 +295,6 @@ final class ormTagSet return $aModifiedTagCodes; } - /** - * Apply a delta to the current TagSet - * $aDelta['added] = array of tag code for only the added tags - * $aDelta['removed'] = array of tag code for only the removed tags - * - * @param $aDelta - * - * @throws \CoreException - * @throws \CoreUnexpectedValue - */ - public function ApplyDelta($aDelta) - { - if (isset($aDelta['removed'])) - { - foreach($aDelta['removed'] as $sTagCode) - { - $this->RemoveTag($sTagCode); - } - } - if (isset($aDelta['added'])) - { - foreach($aDelta['added'] as $sTagCode) - { - $this->AddTag($sTagCode); - } - } - } - /** * Check whether a tag code is valid or not for this TagSet * @@ -424,9 +321,9 @@ final class ormTagSet * @throws \CoreException * @throws \CoreUnexpectedValue */ - public function AddTag($sTagCode) + public function Add($sTagCode) { - if ($this->GetCount() === $this->iLimit) + if ($this->Count() === $this->iLimit) { throw new CoreException("Maximum number of tags ({$this->iLimit}) reached for {$this->sClass}:{$this->sAttCode}"); } @@ -440,7 +337,7 @@ final class ormTagSet { // put it back into preserved $this->aPreserved[$sTagCode] = $oTag; - // no need to add it to aModified : was already done when calling RemoveTag method + // no need to add it to aModified : was already done when calling Remove method } else { @@ -453,7 +350,7 @@ final class ormTagSet /** * @param $sTagCode */ - public function RemoveTag($sTagCode) + public function Remove($sTagCode) { if ($this->IsTagInList($this->aRemoved, $sTagCode)) { @@ -501,30 +398,6 @@ final class ormTagSet return $oTag; } - /** - * Populates the added and removed arrays for bulk edit - * - * @param string[] $aTagCodes - * - * @throws \CoreException - * @throws \CoreUnexpectedValue - */ - public function GenerateDiffFromTags($aTagCodes) - { - foreach($this->GetValue() as $sCurrentTagCode) - { - if (!in_array($sCurrentTagCode, $aTagCodes)) - { - $this->RemoveTag($sCurrentTagCode); - } - } - - foreach($aTagCodes as $sNewTagCode) - { - $this->AddTag($sNewTagCode); - } - } - /** * @param $sTagCode * @@ -583,14 +456,18 @@ final class ormTagSet * * @return bool true if same tag set */ - public function Equals(ormTagSet $other) + public function Equals(ormSet $other) { + if (!($other instanceof ormTagSet)) + { + return false; + } if ($this->GetTagDataClass() !== $other->GetTagDataClass()) { return false; } - return implode(' ', $this->GetValue()) === implode(' ', $other->GetValue()); + return implode(' ', $this->GetValues()) === implode(' ', $other->GetValues()); } public function GetTagDataClass() diff --git a/test/core/ormTagSetTest.php b/test/core/ormTagSetTest.php index ad27abca7..1b8f54a89 100644 --- a/test/core/ormTagSetTest.php +++ b/test/core/ormTagSetTest.php @@ -66,30 +66,30 @@ class ormTagSetTest extends ItopDataTestCase public function testGetValue() { $oTagSet = new ormTagSet(TAG_CLASS, TAG_ATTCODE, MAX_TAGS); - static::assertEquals($oTagSet->GetValue(), array()); + static::assertEquals($oTagSet->GetValues(), array()); - $oTagSet->AddTag('tag1'); - static::assertEquals($oTagSet->GetValue(), array('tag1')); + $oTagSet->Add('tag1'); + static::assertEquals($oTagSet->GetValues(), array('tag1')); - $oTagSet->AddTag('tag2'); - static::assertEquals($oTagSet->GetValue(), array('tag1', 'tag2')); + $oTagSet->Add('tag2'); + static::assertEquals($oTagSet->GetValues(), array('tag1', 'tag2')); } public function testAddTag() { $oTagSet = new ormTagSet(TAG_CLASS, TAG_ATTCODE, MAX_TAGS); - $oTagSet->AddTag('tag1'); - static::assertEquals($oTagSet->GetValue(), array('tag1')); + $oTagSet->Add('tag1'); + static::assertEquals($oTagSet->GetValues(), array('tag1')); - $oTagSet->SetValue(array('tag1', 'tag2')); - static::assertEquals($oTagSet->GetValue(), array('tag1', 'tag2')); + $oTagSet->SetValues(array('tag1', 'tag2')); + static::assertEquals($oTagSet->GetValues(), array('tag1', 'tag2')); - $oTagSet->RemoveTag('tag1'); - static::assertEquals($oTagSet->GetValue(), array('tag2')); + $oTagSet->Remove('tag1'); + static::assertEquals($oTagSet->GetValues(), array('tag2')); - $oTagSet->AddTag('tag1'); - static::assertEquals($oTagSet->GetValue(), array('tag1', 'tag2')); + $oTagSet->Add('tag1'); + static::assertEquals($oTagSet->GetValues(), array('tag1', 'tag2')); } @@ -102,13 +102,13 @@ class ormTagSetTest extends ItopDataTestCase { $oTagSet = new ormTagSet(TAG_CLASS, TAG_ATTCODE, 3); - $oTagSet->SetValue(array('tag1', 'tag2', 'tag3')); + $oTagSet->SetValues(array('tag1', 'tag2', 'tag3')); - static::assertEquals($oTagSet->GetValue(), array('tag1', 'tag2', 'tag3')); + static::assertEquals($oTagSet->GetValues(), array('tag1', 'tag2', 'tag3')); try { - $oTagSet->SetValue(array('tag1', 'tag2', 'tag3', 'tag4')); + $oTagSet->SetValues(array('tag1', 'tag2', 'tag3', 'tag4')); } catch (\CoreException $e) { @@ -120,15 +120,15 @@ class ormTagSetTest extends ItopDataTestCase public function testEquals() { $oTagSet1 = new ormTagSet(TAG_CLASS, TAG_ATTCODE, MAX_TAGS); - $oTagSet1->AddTag('tag1'); + $oTagSet1->Add('tag1'); static::assertTrue($oTagSet1->Equals($oTagSet1)); $oTagSet2 = new ormTagSet(TAG_CLASS, TAG_ATTCODE, MAX_TAGS); - $oTagSet2->SetValue(array('tag1')); + $oTagSet2->SetValues(array('tag1')); static::assertTrue($oTagSet1->Equals($oTagSet2)); - $oTagSet1->AddTag('tag2'); + $oTagSet1->Add('tag2'); static::assertFalse($oTagSet1->Equals($oTagSet2)); } @@ -136,48 +136,48 @@ class ormTagSetTest extends ItopDataTestCase { $oTagSet = new ormTagSet(TAG_CLASS, TAG_ATTCODE, MAX_TAGS); - $oTagSet->SetValue(array('tag1')); - static::assertEquals($oTagSet->GetValue(), array('tag1')); + $oTagSet->SetValues(array('tag1')); + static::assertEquals($oTagSet->GetValues(), array('tag1')); - $oTagSet->SetValue(array('tag1', 'tag2')); - static::assertEquals($oTagSet->GetValue(), array('tag1', 'tag2')); + $oTagSet->SetValues(array('tag1', 'tag2')); + static::assertEquals($oTagSet->GetValues(), array('tag1', 'tag2')); } public function testRemoveTag() { $oTagSet = new ormTagSet(TAG_CLASS, TAG_ATTCODE, MAX_TAGS); - $oTagSet->RemoveTag('tag_unknown'); - static::assertEquals($oTagSet->GetValue(), array()); + $oTagSet->Remove('tag_unknown'); + static::assertEquals($oTagSet->GetValues(), array()); - $oTagSet->SetValue(array('tag1')); - $oTagSet->RemoveTag('tag_unknown'); - static::assertEquals($oTagSet->GetValue(), array('tag1')); + $oTagSet->SetValues(array('tag1')); + $oTagSet->Remove('tag_unknown'); + static::assertEquals($oTagSet->GetValues(), array('tag1')); - $oTagSet->SetValue(array('tag1', 'tag2')); - $oTagSet->RemoveTag('tag1'); - static::assertEquals($oTagSet->GetValue(), array('tag2')); + $oTagSet->SetValues(array('tag1', 'tag2')); + $oTagSet->Remove('tag1'); + static::assertEquals($oTagSet->GetValues(), array('tag2')); - $oTagSet->AddTag('tag1'); - static::assertEquals($oTagSet->GetValue(), array('tag1', 'tag2')); + $oTagSet->Add('tag1'); + static::assertEquals($oTagSet->GetValues(), array('tag1', 'tag2')); - $oTagSet->RemoveTag('tag1'); - static::assertEquals($oTagSet->GetValue(), array('tag2')); + $oTagSet->Remove('tag1'); + static::assertEquals($oTagSet->GetValues(), array('tag2')); - $oTagSet->RemoveTag('tag1'); - static::assertEquals($oTagSet->GetValue(), array('tag2')); + $oTagSet->Remove('tag1'); + static::assertEquals($oTagSet->GetValues(), array('tag2')); - $oTagSet->RemoveTag('tag2'); - static::assertEquals($oTagSet->GetValue(), array()); + $oTagSet->Remove('tag2'); + static::assertEquals($oTagSet->GetValues(), array()); } public function testGetDelta() { $oTagSet1 = new ormTagSet(TAG_CLASS, TAG_ATTCODE, MAX_TAGS); - $oTagSet1->SetValue(array('tag1', 'tag2')); + $oTagSet1->SetValues(array('tag1', 'tag2')); $oTagSet2 = new ormTagSet(TAG_CLASS, TAG_ATTCODE, MAX_TAGS); - $oTagSet2->SetValue(array('tag1', 'tag3', 'tag4')); + $oTagSet2->SetValues(array('tag1', 'tag3', 'tag4')); $aDelta = $oTagSet1->GetDelta($oTagSet2); static::assertCount(2, $aDelta); @@ -188,10 +188,10 @@ class ormTagSetTest extends ItopDataTestCase public function testApplyDelta() { $oTagSet1 = new ormTagSet(TAG_CLASS, TAG_ATTCODE, MAX_TAGS); - $oTagSet1->SetValue(array('tag1', 'tag2')); + $oTagSet1->SetValues(array('tag1', 'tag2')); $oTagSet2 = new ormTagSet(TAG_CLASS, TAG_ATTCODE, MAX_TAGS); - $oTagSet2->SetValue(array('tag1', 'tag3', 'tag4')); + $oTagSet2->SetValues(array('tag1', 'tag3', 'tag4')); $aDelta = $oTagSet1->GetDelta($oTagSet2); @@ -212,12 +212,12 @@ class ormTagSetTest extends ItopDataTestCase public function testGetModified($aInitialTags, $aDiffAndExpectedTags) { $oTagSet1 = new ormTagSet(TAG_CLASS, TAG_ATTCODE, MAX_TAGS); - $oTagSet1->SetValue($aInitialTags); + $oTagSet1->SetValues($aInitialTags); foreach($aDiffAndExpectedTags as $aTestItem) { - $oTagSet1->GenerateDiffFromTags($aTestItem['diff']); - static::assertEquals($aTestItem['modified'], $oTagSet1->GetModifiedTags()); + $oTagSet1->GenerateDiffFromArray($aTestItem['diff']); + static::assertEquals($aTestItem['modified'], $oTagSet1->GetModified()); } } @@ -264,11 +264,11 @@ class ormTagSetTest extends ItopDataTestCase public function testBulkModify($aInitialTags, $aDelta, $aExpectedTags) { $oTagSet1 = new ormTagSet(TAG_CLASS, TAG_ATTCODE, MAX_TAGS); - $oTagSet1->SetValue($aInitialTags); + $oTagSet1->SetValues($aInitialTags); $oTagSet1->ApplyDelta($aDelta); - static::assertEquals($aExpectedTags, $oTagSet1->GetValue()); + static::assertEquals($aExpectedTags, $oTagSet1->GetValues()); } public function BulkModifyProvider()