0013120: Via Admin-App created calendar-container have a wrong model
authorMichael Spahn <m.spahn@metaways.de>
Tue, 23 May 2017 12:02:14 +0000 (14:02 +0200)
committerPhilipp Schüle <p.schuele@metaways.de>
Wed, 31 May 2017 11:14:24 +0000 (13:14 +0200)
https://forge.tine20.org/view.php?id=13120

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

index d41fb97..0d88e15 100644 (file)
@@ -1036,8 +1036,26 @@ class Admin_JsonTest extends TestCase
         }
         // record should not be found
         $this->assertEquals($e->getMessage(), 'Addressbook_Model_Contact record with id = ' . $contact->getId().' not found!');
-        
-        
+    }
+
+    /**
+     * test create container with model
+     * 0013120: Via Admin-App created calendar-container have a wrong model
+     */
+    public function testCreateContainerWithModel()
+    {
+        $container = $this->_json->saveContainer(array(
+            "type" => Tinebase_Model_Container::TYPE_SHARED,
+            "backend" => "Sql",
+            "name" => "testtest",
+            "color" => "#008080",
+            "application_id" => Tinebase_Application::getInstance()->getApplicationByName('Calendar')->getId(),
+            "model" => "Tine.Calendar.Model.Event",
+            "note" => ""
+        ));
+
+        // check if the model was set
+        $this->assertEquals($container['model'], 'Calendar_Model_Event');
     }
     
     /**
index 497e315..df84a69 100644 (file)
@@ -1070,7 +1070,14 @@ class Admin_Frontend_Json extends Tinebase_Frontend_Json_Abstract
         if (empty($recordData['model'])) {
             $recordData['model'] = Tinebase_Core::getApplicationInstance($application)->getDefaultModel();
         } else {
-            $recordData['model'] = strstr($recordData['model'], '_Model_') ? $recordData['model'] : $application . '_Model_' . $recordData['model'];
+            $applicationModelParts = \explode('.', $recordData['model']);
+
+            if (0 === \count($applicationModelParts) && false === \strpos($recordData['model'], '_Model_')) {
+                throw new \InvalidArgumentException('Invalid model specified.');
+            }
+            // Handling if a model is either in php format like Application_Model_Foobar or Application.Model.Foobar
+            $recordData['model'] = \strstr($recordData['model'], '_Model_') ? $recordData['model'] : $application . '_Model_' . \end($applicationModelParts);
+            \reset($applicationModelParts);
         }
         $additionalArguments = ((isset($recordData['note']) || array_key_exists('note', $recordData))) ? array(array('note' => $recordData['note'])) : array();
         return $this->_save($recordData, Admin_Controller_Container::getInstance(), 'Tinebase_Model_Container', 'id', $additionalArguments);