Tinebase_RoleMembers - fix diff behaviour for replication
authorPaul Mehrer <p.mehrer@metaways.de>
Thu, 3 Aug 2017 15:34:58 +0000 (17:34 +0200)
committerPhilipp Schüle <p.schuele@metaways.de>
Fri, 4 Aug 2017 07:48:41 +0000 (09:48 +0200)
Change-Id: I6f3a1ce8079f1e3055782b62a8428dd217f6b378
Reviewed-on: http://gerrit.tine20.com/customers/5389
Tested-by: Jenkins CI (http://ci.tine20.com/) <tine20-jenkins@metaways.de>
Reviewed-by: Philipp Schüle <p.schuele@metaways.de>
tests/tine20/Tinebase/Timemachine/ModificationLogTest.php
tine20/Tinebase/Model/RoleMember.php

index a9de524..09bd74e 100644 (file)
@@ -415,10 +415,10 @@ class Tinebase_Timemachine_ModificationLogTest extends PHPUnit_Framework_TestCas
         $mod = clone ($roleModifications->getByIndex(2));
         $diff = new Tinebase_Record_Diff(json_decode($mod->new_value, true));
         $rsDiff = new Tinebase_Record_RecordSetDiff($diff->diff['members']);
+        $rsDiff->removed = $rsDiff->added;
         $modified = $rsDiff->added;
-        $rsDiff->added = array();
         $modified[0]['account_id'] = 'test3';
-        $rsDiff->modified = $modified;
+        $rsDiff->added = $modified;
         $diffArray = $diff->diff;
         $diffArray['members'] = $rsDiff;
         $diff->diff = $diffArray;
index 94c21c0..4208bc9 100644 (file)
@@ -141,4 +141,54 @@ class Tinebase_Model_RoleMember extends Tinebase_Record_Abstract
 
         return true;
     }
+
+    /**
+     * @param Tinebase_Record_RecordSet $_recordSetOne
+     * @param Tinebase_Record_RecordSet $_recordSetTwo
+     * @return Tinebase_Record_RecordSetDiff
+     */
+    public static function recordSetDiff(Tinebase_Record_RecordSet $_recordSetOne, Tinebase_Record_RecordSet $_recordSetTwo)
+    {
+        $shallowCopyOne = new Tinebase_Record_RecordSet(self::class);
+        $removed = new Tinebase_Record_RecordSet(self::class);
+        $added = new Tinebase_Record_RecordSet(self::class);
+
+        foreach ($_recordSetOne as $roleMemberOne) {
+            $shallowCopyOne->addRecord($roleMemberOne);
+        }
+
+        /** @var Tinebase_Model_RoleMember $roleMemberTwo */
+        foreach ($_recordSetTwo as $roleMemberTwo) {
+            $found = false;
+            /** @var Tinebase_Model_RoleMember $roleMemberTwo */
+            foreach ($shallowCopyOne as $roleMemberOne) {
+                if (    $roleMemberOne->role_id        === $roleMemberTwo->role_id        &&
+                        $roleMemberOne->account_id     === $roleMemberTwo->account_id     &&
+                        $roleMemberOne->account_type   === $roleMemberTwo->account_type       ) {
+                    $found = true;
+                    break;
+                }
+            }
+
+            if (true === $found) {
+                $shallowCopyOne->removeRecord($roleMemberOne);
+            } else {
+                $added->addRecord($roleMemberTwo);
+            }
+        }
+
+        /** @var Tinebase_Model_RoleMember $roleMemberTwo */
+        foreach ($shallowCopyOne as $roleMemberOne) {
+            $removed->addRecord($roleMemberOne);
+        }
+
+        $result = new Tinebase_Record_RecordSetDiff(array(
+            'model'    => self::class,
+            'added'    => $added,
+            'removed'  => $removed,
+            'modified' => new Tinebase_Record_RecordSet('Tinebase_Record_Diff')
+        ));
+
+        return $result;
+    }
 }