test and improve customfield import handling
authorPhilipp Schüle <p.schuele@metaways.de>
Fri, 26 Aug 2016 13:34:14 +0000 (15:34 +0200)
committerPhilipp Schüle <p.schuele@metaways.de>
Wed, 31 Aug 2016 16:22:41 +0000 (18:22 +0200)
* adds a test with customfield merge for duplicates
* adds cf sorting by key

Change-Id: Ib229335785de5ead2f07d39e495e8b28e778bc0c
Reviewed-on: http://gerrit.tine20.com/customers/3486
Tested-by: Jenkins CI (http://ci.tine20.com/)
Reviewed-by: Philipp Schüle <p.schuele@metaways.de>
tests/tine20/Addressbook/Import/CsvTest.php
tests/tine20/Addressbook/Import/files/adb_import_csv_duplicate.xml
tests/tine20/Addressbook/Import/files/import_duplicate_1_cf.csv [new file with mode: 0644]
tests/tine20/Addressbook/Import/files/import_duplicate_2_cf.csv [new file with mode: 0644]
tine20/Tinebase/CustomField.php
tine20/Tinebase/Model/Filter/CustomField.php

index feab879..d9a63ad 100644 (file)
@@ -275,24 +275,48 @@ class Addressbook_Import_CsvTest extends ImportTestCase
      * testImportDuplicateResolve
      * 
      * @see 0009316: add duplicate resolving to cli import
+     *
+     * @param bool $withCustomfields
+     * @return array
      */
-    public function testImportDuplicateResolve()
+    public function testImportDuplicateResolve($withCustomfields = false)
     {
         $definition = $this->_getDefinitionFromFile('adb_import_csv_duplicate.xml');
         
-        $this->_filename = dirname(__FILE__) . '/files/import_duplicate_1.csv';
+        $this->_filename = dirname(__FILE__) . ($withCustomfields ? '/files/import_duplicate_1_cf.csv' : '/files/import_duplicate_1.csv');
         $this->_deleteImportFile = FALSE;
         
         $this->_doImport(array(), $definition);
         $this->_deletePersonalContacts = TRUE;
 
-        $this->_filename = dirname(__FILE__) . '/files/import_duplicate_2.csv';
+        $this->_filename = dirname(__FILE__) .($withCustomfields ? '/files/import_duplicate_2_cf.csv' : '/files/import_duplicate_2.csv');
         
         $result = $this->_doImport(array(), $definition);
         
         $this->assertEquals(1, $result['updatecount'], 'should have updated 1 contact');
+        $this->assertEquals(0, $result['totalcount']);
+        $this->assertEquals(0, $result['failcount']);
         $this->assertEquals('joerg@home.com', $result['results'][0]->email_home, 'duplicates resolving did not work: ' . print_r($result['results']->toArray(), true));
         $this->assertEquals('Jörg', $result['results'][0]->n_given, 'wrong encoding: ' . print_r($result['results']->toArray(), true));
+
+        return $result;
+    }
+
+    /**
+     * testImportDuplicateResolveCustomfields
+     */
+    public function testImportDuplicateResolveCustomfields()
+    {
+        $this->_createCustomField('customfield1');
+        $this->_createCustomField('customfield2');
+
+        $result = $this->testImportDuplicateResolve(/* $withCustomfields */ true);
+
+        // check customfields in result
+        $joerg = $result['results'][0]->toArray();
+        $this->assertTrue(isset($joerg['customfields']), 'cfs missing: ' .  print_r($joerg, true));
+        $this->assertFalse(isset($joerg['customfields']['customfield1']), print_r($joerg, true));
+        $this->assertEquals('cf2-2', $joerg['customfields']['customfield2']);
     }
 
     /**
index db5ef94..ed0b14f 100644 (file)
@@ -11,7 +11,7 @@
     <label>Simple CSV import</label>
     <description>simple import</description>
     <extension>csv</extension>
-    <duplicateResolveStrategy>mergeTheirs</duplicateResolveStrategy>
+    <duplicateResolveStrategy>mergeMine</duplicateResolveStrategy>
     <mapping>
         <field>
             <source>adr_one_countryname</source>
             <source>tel_work</source>
             <destination>tel_work</destination>
         </field>
+        <field>
+            <source>customfield1</source>
+            <destination>customfield1</destination>
+        </field>
+        <field>
+            <source>customfield2</source>
+            <destination>customfield2</destination>
+        </field>
     </mapping>
 </config>
\ No newline at end of file
diff --git a/tests/tine20/Addressbook/Import/files/import_duplicate_1_cf.csv b/tests/tine20/Addressbook/Import/files/import_duplicate_1_cf.csv
new file mode 100644 (file)
index 0000000..f71acac
--- /dev/null
@@ -0,0 +1,3 @@
+"org_name","salutation","title","n_given","n_family","role","adr_one_street","adr_one_postalcode","adr_one_locality","adr_one_countryname","tel_work","tel_fax","tel_cell","email","tel_fax_home","tel_home","email_home","customfield1","customfield2"\r
+"effect gmbh","Herr","Dr.","Jörg","Heinz","Geschäftsführer","Str. 25","21222","Köln","DE","022324321","","","joerg.heinz@honk.com","","","","cf1","cf2"\r
+"effect gmbh","Herr","","Hans","Fritz","Technical Lead","Str. 25","21222","Köln","DE","022324321","","","hans.fritz@honk.com","","","","cf1","cf2"\r
diff --git a/tests/tine20/Addressbook/Import/files/import_duplicate_2_cf.csv b/tests/tine20/Addressbook/Import/files/import_duplicate_2_cf.csv
new file mode 100644 (file)
index 0000000..784eeb2
--- /dev/null
@@ -0,0 +1,3 @@
+"org_name","salutation","title","n_given","n_family","role","adr_one_street","adr_one_postalcode","adr_one_locality","adr_one_countryname","tel_work","tel_fax","tel_cell","email","tel_fax_home","tel_home","email_home","customfield1","customfield2"\r
+"effect gmbh","Herr","Dr.","Jörg","Heinz","Geschäftsführer","Str. 25","21222","Köln","DE","022324321","","","joerg.heinz@honk.com","022324322","022324323","joerg@home.com","","cf2-2"\r
+"effect gmbh","Herr","","Hans","Fritz","Technical Lead","Str. 25","21222","Köln","DE","022324321","","","hans.fritz@honk.com","","","","cf1","cf2"\r
index 85ce231..1eda4f2 100644 (file)
@@ -285,7 +285,7 @@ class Tinebase_CustomField implements Tinebase_Controller_SearchInterface
  
         $filterValues = array(array(
             'field'     => 'application_id', 
-            'operator'  => 'equals', 
+            'operator'  => 'equals',
             'value'     => $applicationId
         ));
             
@@ -485,6 +485,11 @@ class Tinebase_CustomField implements Tinebase_Controller_SearchInterface
             }
             $recordCfs[$config->name] = $value;
         }
+
+        // sort customfields by key
+        if (is_array($recordCfs)) {
+            ksort($recordCfs);
+        }
         
         $record->customfields = $recordCfs;
     }
index 15aca2a..0c98d2b 100644 (file)
@@ -117,7 +117,8 @@ class Tinebase_Model_Filter_CustomField extends Tinebase_Model_Filter_Abstract
         // make sure $correlationName is a string
         $correlationName = Tinebase_Record_Abstract::generateUID(30);
         
-        if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' Adding custom field filter: ' . print_r($this->_value, true));
+        if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) Tinebase_Core::getLogger()->trace(__METHOD__ . '::' . __LINE__ .
+            ' Adding custom field filter: ' . print_r($this->_value, true));
         
         $db = Tinebase_Core::getDb();
         $idProperty = $db->quoteIdentifier($this->_options['idProperty']);