0010560: Import contacts using merge mine
authorsstamer <s.stamer@metaways.de>
Thu, 29 Jan 2015 14:12:57 +0000 (15:12 +0100)
committerPhilipp Schüle <p.schuele@metaways.de>
Wed, 4 Feb 2015 12:29:05 +0000 (13:29 +0100)
* adds a mergeMine test with tags
* fixes merge handling when importing via web interface

https://forge.tine20.org/view.php?id=10560

Change-Id: I04307bd01ca05ea57cb6aef9f792338c96f727ee
Reviewed-on: http://gerrit.tine20.com/customers/1578
Tested-by: Jenkins CI (http://ci.tine20.com/)
Reviewed-by: Philipp Schüle <p.schuele@metaways.de>
Tested-by: Philipp Schüle <p.schuele@metaways.de>
tests/tine20/Addressbook/JsonTest.php
tine20/Tinebase/Frontend/Json/Abstract.php
tine20/Tinebase/Import/Abstract.php

index 199d7bc..9851f50 100644 (file)
@@ -951,6 +951,38 @@ class Addressbook_JsonTest extends PHPUnit_Framework_TestCase
     }
     
     /**
+     * testImportMergeTheirsWithTag
+     *
+     */
+    public function testImportMergeTheirsWithTag()
+    {
+        $result = $this->_importHelper(array('dryrun' => 0));
+        $this->assertTrue(count($result['results']) > 0, 'no record were imported');
+        $klaus = $result['results'][0];
+        
+        $klaus['tags'][] = $this->_getTag()->toArray();
+        $klaus['adr_one_postalcode'] = '12345';
+        
+        $clientRecords = array(array(
+            'recordData'        => $klaus,
+            'resolveStrategy'   => 'mergeTheirs',
+            'index'             => 0,
+        ));
+        
+        $options = array(
+            'dryrun'     => 0,
+            'duplicateResolveStrategy' => 'mergeTheirs',
+        );
+        
+        $result = $this->_importHelper($options, $clientRecords);
+        $this->assertEquals(2, count($result['results'][0]['tags']), 'klaus should have both tags: ' . print_r($result['results'][0], TRUE));
+        
+        $klaus = $this->_instance->getContact($klaus['id']);
+        $this->assertEquals(2, count($klaus['tags']), 'klaus should have both tags: ' . print_r($klaus, TRUE));
+        $this->assertEquals('12345', $klaus['adr_one_postalcode']);
+    }
+    
+    /**
      * helper for import with tags and keep/discard strategy
      * 
      * @param string $resolveStrategy
index dd32403..d67a4d2 100644 (file)
@@ -437,7 +437,7 @@ abstract class Tinebase_Frontend_Json_Abstract extends Tinebase_Frontend_Abstrac
         if (! is_object($importer)) {
             throw new Tinebase_Exception_NotFound('No importer found for ' . $definition->name);
         }
-
+        
         // extend execution time to 30 minutes
         $this->_longRunningRequest(1800);
 
index 3546a99..1fa5421 100644 (file)
@@ -542,7 +542,7 @@ abstract class Tinebase_Import_Abstract implements Tinebase_Import_Interface
      *
      * @param Tinebase_Record_Abstract $_record
      * @param string $_resolveStrategy
-     * @param array $_recordData
+     * @param array $_recordData not needed here but in other import classes (i.a. Admin_Import_Csv)
      * @return void
      * @throws Tinebase_Exception_Record_Validation
      */
@@ -808,13 +808,15 @@ abstract class Tinebase_Import_Abstract implements Tinebase_Import_Interface
      * import record and resolve possible conflicts
      * 
      * supports $_resolveStrategy(s): ['mergeTheirs', ('Merge, keeping existing details')],
-     *                              ['mergeMine',   ('Merge, keeping my details')],
-     *                              ['keep',        ('Keep both records')]
+     *                                ['mergeMine',   ('Merge, keeping my details')],
+     *                                ['keep',        ('Keep both records')]
      * 
      * @param Tinebase_Record_Abstract $record
      * @param string $resolveStrategy
      * @param Tinebase_Record_Abstract $clientRecord
      * @return Tinebase_Record_Abstract
+     * 
+     * @todo we should refactor the merge handling: this function should always get the merged record OR always do the merging itself
      */
     protected function _importAndResolveConflict(Tinebase_Record_Abstract $record, $resolveStrategy = null, $clientRecord = null)
     {
@@ -826,12 +828,14 @@ abstract class Tinebase_Import_Abstract implements Tinebase_Import_Interface
         switch ($resolveStrategy) {
             case 'mergeTheirs':
             case 'mergeMine':
-                if ($resolveStrategy === 'mergeTheirs') {
-                    $recordToUpdate = $this->_mergeRecord($record, $clientRecord);
-                } else if ($clientRecord) {
-                    $recordToUpdate = $this->_mergeRecord($clientRecord, $record);
+                if ($clientRecord) {
+                    if ($resolveStrategy === 'mergeTheirs') {
+                        $recordToUpdate = $this->_mergeRecord($record, $clientRecord);
+                    } else {
+                        $recordToUpdate = $this->_mergeRecord($clientRecord, $record);
+                    }
                 } else {
-                    $recordToUpdate = null;
+                    $recordToUpdate = $record;
                 }
                 
                 if ($recordToUpdate !== null) {