0012564: related records should be updateable during create
authorPhilipp Schüle <p.schuele@metaways.de>
Tue, 24 Jan 2017 08:50:05 +0000 (09:50 +0100)
committerPhilipp Schüle <p.schuele@metaways.de>
Wed, 25 Jan 2017 11:05:42 +0000 (12:05 +0100)
https://forge.tine20.org/view.php?id=12564

Change-Id: I71794412d0b36d457788c55037d0b584be869171
Reviewed-on: http://gerrit.tine20.com/customers/4127
Tested-by: Jenkins CI (http://ci.tine20.com/)
Reviewed-by: Philipp Schüle <p.schuele@metaways.de>
tests/tine20/Crm/JsonTest.php
tine20/Tinebase/Relations.php

index 71dcbfc..82b499a 100644 (file)
@@ -375,6 +375,26 @@ class Crm_JsonTest extends Crm_AbstractTest
         $this->assertEquals(1, count($savedLead['relations']), 'Relation has not been added');
         $this->assertEquals('CUSTOMER', $savedLead['relations'][0]['type'], 'default type should be CUSTOMER');
     }
+
+    public function testUpdateContactRelationOnCreate()
+    {
+        $contact      = $this->_getContact();
+        $savedContact = Addressbook_Controller_Contact::getInstance()->create($contact, FALSE);
+        $lead         = $this->_getLead();
+
+        $leadData = $lead->toArray();
+        $street = 'Heinrichstrasse 193';
+        $contactData = $savedContact->toArray();
+        $contactData['adr_one_street'] = $street;
+        $leadData['relations'] = array(
+            array('type'  => 'CUSTOMER', 'related_record' => $contactData),
+        );
+        $savedLead = $this->_getUit()->saveLead($leadData);
+        $this->assertTrue(isset($savedLead['relations'][0]));
+        $contactRelation = $savedLead['relations'][0];
+        $this->assertEquals($street, $contactRelation['related_record']['adr_one_street'],
+            'street not set in contact: ' . print_r($contactRelation, true));
+    }
     
     /**
      * testConcurrentRelationSetting
index a2c7740..03ded49 100644 (file)
@@ -122,11 +122,16 @@ class Tinebase_Relations
         
         // add new relations
         foreach ($toAdd as $idx) {
-            if(isset($emptyRelatedId[$idx])) {
-                $relations[$idx]->related_id = null;
-                $this->_setAppRecord($relations[$idx], $_doCreateUpdateCheck);
+            $relation = $relations[$idx];
+            if (isset($emptyRelatedId[$idx])) {
+                // create related record
+                $relation->related_id = null;
+                $this->_setAppRecord($relation, $_doCreateUpdateCheck);
+            } else if ($_inspectRelated && ! empty($relation->related_id) && ! empty($relation->related_record)) {
+                // update related record
+                $this->_setAppRecord($relation, $_doCreateUpdateCheck);
             }
-            $this->_addRelation($relations[$idx]);
+            $this->_addRelation($relation);
         }
         
         // update relations