0004934: Ensure that container does exists
authorMichael Spahn <m.spahn@metaways.de>
Wed, 10 Sep 2014 10:07:10 +0000 (12:07 +0200)
committerPhilipp Schüle <p.schuele@metaways.de>
Tue, 16 Sep 2014 11:59:18 +0000 (13:59 +0200)
Change-Id: I06f63f0b189717abb68212d0e0dfc2280d852c85
Reviewed-on: http://gerrit.tine20.com/customers/1117
Tested-by: sstamer <s.stamer@metaways.de>
Tested-by: Jenkins CI (http://ci.tine20.com/)
Reviewed-by: Philipp Schüle <p.schuele@metaways.de>
tests/tine20/Calendar/Import/ICalTest.php
tine20/Calendar/Import/Ical.php
tine20/Tinebase/Controller/ScheduledImport.php

index c806881..9d65dd9 100644 (file)
@@ -37,6 +37,34 @@ class Calendar_Import_ICalTest extends Calendar_TestCase
         $this->assertEquals('2008-11-05 15:00:00', $startbucks->dtstart->format(Tinebase_Record_Abstract::ISO8601LONG));
     }
     
         $this->assertEquals('2008-11-05 15:00:00', $startbucks->dtstart->format(Tinebase_Record_Abstract::ISO8601LONG));
     }
     
+    /**
+     * test simple import from file into a not existing container
+     * 
+     *  - the calendar should be created
+     */
+    public function testSimpleImportIntoNewContainer()
+    {
+        $importer = new Calendar_Import_Ical(array(
+            'container_id' => 'unittest_not_existing',
+        ));
+
+        $result = $importer->importFile(dirname(__FILE__) . '/files/simple.ics');
+
+        $importedContainerId = Tinebase_Container::getInstance()->getContainerByName(
+                    'Calendar',
+                    'unittest_not_existing',
+                    Tinebase_Model_Container::TYPE_PERSONAL,
+                    Tinebase_Core::getUser()->getId()
+                )->getId();
+
+        $events = Calendar_Controller_Event::getInstance()->search(new Calendar_Model_EventFilter(array(
+            array('field' => 'container_id', 'operator' => 'equals', 'value' => $importedContainerId)
+        )), NULL);
+        
+        $this->assertEquals($importedContainerId, Tinebase_Container::getInstance()->getContainerById($importedContainerId)->getId());
+        $this->assertEquals(6, $events->count(), 'events was not imported');
+    }
+    
     public function testImportSimpleFromFile()
     {
         $importer = new Calendar_Import_Ical(array(
     public function testImportSimpleFromFile()
     {
         $importer = new Calendar_Import_Ical(array(
index d156eb7..2b2d5bb 100644 (file)
@@ -120,14 +120,17 @@ class Calendar_Import_Ical extends Tinebase_Import_Abstract
             throw $isce;
         }
         
             throw $isce;
         }
         
-        // find container or create a new one on the fly
-        $containerId = $this->_options['container_id'];
         
         try {
         
         try {
-            $container = Tinebase_Container::getInstance()->getContainerById($containerId);
+            if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) Tinebase_Core::getLogger()->debug(__METHOD__ . ' ' . __LINE__ . ' ' 
+                . ' Trying to find container with ID ' . print_r($this->_options['container_id'], true));
+            $container = Tinebase_Container::getInstance()->getContainerById($this->_options['container_id']);
         } catch (Tinebase_Exception_InvalidArgument $e) {
         } catch (Tinebase_Exception_InvalidArgument $e) {
+            if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) Tinebase_Core::getLogger()->debug(__METHOD__ . ' ' . __LINE__ . ' ' 
+                . ' Could not found container with Id ' . print_r($this->_options['container_id'], true) . ' assuming this is a container name.' );
+                         
             $container = new Tinebase_Model_Container(array(
             $container = new Tinebase_Model_Container(array(
-                'name'              => $containerId,
+                'name'              => $this->_options['container_id'],
                 'type'              => Tinebase_Model_Container::TYPE_PERSONAL,
                 'backend'           => Tinebase_User::SQL,
                 'color'             => '#ffffff',
                 'type'              => Tinebase_Model_Container::TYPE_PERSONAL,
                 'backend'           => Tinebase_User::SQL,
                 'color'             => '#ffffff',
@@ -138,17 +141,25 @@ class Calendar_Import_Ical extends Tinebase_Import_Abstract
             $container = Tinebase_Container::getInstance()->addContainer($container);
         }
         
             $container = Tinebase_Container::getInstance()->addContainer($container);
         }
         
-        $events->container_id = $container->getId();
+        try {
+            $this->_options['container_id'] = $container->getId();
+        } catch (Exception $ex) {
+            throw new Tinebase_Exception_NotFound('Could not find container by ID: ' . $this->_options['container_id']);
+        }
+        
+        if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) Tinebase_Core::getLogger()->debug(__METHOD__ . ' ' . __LINE__ . ' ' 
+            . ' Import into calendar: ' . print_r($this->_options['container_id'], true));
         
         $cc = Calendar_Controller_MSEventFacade::getInstance();
         $sendNotifications = Calendar_Controller_Event::getInstance()->sendNotifications(FALSE);
         
         // search uid's and remove already existing -> only in import cal?
         $existingEventsFilter = new Calendar_Model_EventFilter(array(
         
         $cc = Calendar_Controller_MSEventFacade::getInstance();
         $sendNotifications = Calendar_Controller_Event::getInstance()->sendNotifications(FALSE);
         
         // search uid's and remove already existing -> only in import cal?
         $existingEventsFilter = new Calendar_Model_EventFilter(array(
-            array('field' => 'container_id', 'operator' => 'in', 'value' => array($this->_options['container_id'])),
+            array('field' => 'container_id', 'operator' => 'equals', 'value' => $this->_options['container_id']),
             array('field' => 'uid', 'operator' => 'in', 'value' => array_unique($events->uid)),
         ));
         $existingEvents = $cc->search($existingEventsFilter);
             array('field' => 'uid', 'operator' => 'in', 'value' => array_unique($events->uid)),
         ));
         $existingEvents = $cc->search($existingEventsFilter);
+        
         if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) Tinebase_Core::getLogger()->debug(__METHOD__ . ' ' . __LINE__ . ' ' 
                 . ' Found ' . count($existingEvents) . ' existing events');
         if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) Tinebase_Core::getLogger()->trace(__METHOD__ . ' ' . __LINE__ . ' '
         if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) Tinebase_Core::getLogger()->debug(__METHOD__ . ' ' . __LINE__ . ' ' 
                 . ' Found ' . count($existingEvents) . ' existing events');
         if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) Tinebase_Core::getLogger()->trace(__METHOD__ . ' ' . __LINE__ . ' '
@@ -160,6 +171,7 @@ class Calendar_Import_Ical extends Tinebase_Import_Abstract
             $existingEvent = $existingEvents->find('uid', $event->uid);
             try {
                 if (! $existingEvent) {
             $existingEvent = $existingEvents->find('uid', $event->uid);
             try {
                 if (! $existingEvent) {
+                    $event->container_id = $this->_options['container_id'];
                     $event = $cc->create($event, FALSE);
                     $this->_importResult['totalcount'] += 1;
                     $this->_importResult['results']->addRecord($event);
                     $event = $cc->create($event, FALSE);
                     $this->_importResult['totalcount'] += 1;
                     $this->_importResult['results']->addRecord($event);
index 26825ea..f728a82 100644 (file)
@@ -158,11 +158,15 @@ class Tinebase_Controller_ScheduledImport extends Tinebase_Controller_Record_Abs
             'importContainerId' => $record->container_id,
         );
         
             'importContainerId' => $record->container_id,
         );
         
-        if (strpos($record->source, 'http') === 0) {
-            $client = new Zend_Http_Client($record->source);
-            $toImport = $client->request()->getBody();
-        } else {
-            $toImport = file_get_contents($record->source);
+        try {
+            if (strpos($record->source, 'http') === 0) {
+                $client = new Zend_Http_Client($record->source);
+                $toImport = $client->request()->getBody();
+            } else {
+                $toImport = file_get_contents($record->source);
+            }
+        } catch (Exception $e) {
+            throw new Tinebase_Exception_NotFound('Could not load file to import: ' . $record->source);
         }
         
         $importer = new $importer($options);
         }
         
         $importer = new $importer($options);