0011668: fix Calendar_Frontend_* Tests with postgresql backend
authorPhilipp Schüle <p.schuele@metaways.de>
Wed, 22 Jun 2016 13:28:40 +0000 (15:28 +0200)
committerPhilipp Schüle <p.schuele@metaways.de>
Mon, 27 Jun 2016 09:31:42 +0000 (11:31 +0200)
* Calendar_Frontend_ActiveSync: comparing strings with ints
* RecordSet: only allow unique records to be added
* improves some tests

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

Change-Id: Id18f14f58006344a6cddc48bf707092524cf1a19
Reviewed-on: http://gerrit.tine20.com/customers/3270
Tested-by: Jenkins CI (http://ci.tine20.com/)
Reviewed-by: Philipp Schüle <p.schuele@metaways.de>
tests/tine20/Calendar/Controller/MSEventFacadeTest.php
tests/tine20/Calendar/Frontend/ActiveSyncTest.php
tests/tine20/Calendar/Frontend/CalDAV/PluginManagedAttachmentsTest.php
tests/tine20/Calendar/Frontend/WebDAV/EventTest.php
tine20/Calendar/Frontend/ActiveSync.php
tine20/Calendar/Frontend/CalDAV/PluginManagedAttachments.php
tine20/Tinebase/Record/RecordSet.php

index 3183e4d..34bdcc4 100644 (file)
@@ -241,6 +241,7 @@ class Calendar_Controller_MSEventFacadeTest extends Calendar_TestCase
         $newPersistentException->dtstart->addDay(2)->addHour(2);
         $newPersistentException->dtend->addDay(2)->addHour(2);
         $newPersistentException->summary = 'new exception';
+        $newPersistentException->setId(null);
         $event->exdate->addRecord($newPersistentException);
         
         $newDeletedInstance = clone $event->exdate->filter('is_deleted', 1)->getFirstRecord();
@@ -248,6 +249,7 @@ class Calendar_Controller_MSEventFacadeTest extends Calendar_TestCase
         $newDeletedInstance->dtend->addDay(2);
         $newDeletedInstance->recurid = clone $newDeletedInstance->dtstart;
         $newDeletedInstance->is_deleted = TRUE;
+        $newPersistentException->setId(null);
         $event->exdate->addRecord($newDeletedInstance);
         
         $this->_fixConcurrency($event);
index 9e28731..3a1cbc9 100644 (file)
@@ -1170,10 +1170,6 @@ AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAMAAAAFAAMA
     
     public function testGetEntriesIPhone()
     {
-        if ($this->_dbIsPgsql()) {
-            $this->markTestSkipped('0011668: fix Calendar_Frontend_* Tests with postgresql backend');
-        }
-
         $syncrotonFolder = $this->testCreateFolder();
         
         $syncrotonFolder2 = $this->testCreateFolder();
@@ -1195,7 +1191,6 @@ AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAMAAAAFAAMA
         
         $this->assertEmpty($syncrotonEvent->attendees, 'Events attendees found in folder which is not the default calendar');
         
-        
         $controller = Syncroton_Data_Factory::factory($this->_class, $this->_getDevice(Syncroton_Model_Device::TYPE_IPHONE), Tinebase_DateTime::now());
         
         $now = Tinebase_DateTime::now();
@@ -1252,10 +1247,6 @@ AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAMAAAAFAAMA
      */
     public function testUpdateEntriesIPhoneNonDefaultFolder()
     {
-        if ($this->_dbIsPgsql()) {
-            $this->markTestSkipped('0011668: fix Calendar_Frontend_* Tests with postgresql backend');
-        }
-
         // create event in folder1
         $syncrotonFolder = $this->testCreateFolder();
         $controller = Syncroton_Data_Factory::factory($this->_class, $this->_getDevice(Syncroton_Model_Device::TYPE_IPHONE), Tinebase_DateTime::now());
index 7b4729c..b7abffc 100644 (file)
@@ -123,10 +123,7 @@ class Calendar_Frontend_CalDAV_PluginManagedAttachmentsTest extends TestCase
         $agenda = 'HELLO WORLD';
         $request->setBody($agenda);
 
-        $this->server->httpRequest = $request;
-        $this->server->exec();
-        
-        $vcalendar = stream_get_contents($this->response->body);
+        $vcalendar = $this->_execAndGetVCalendarFromRequest($request);
         
         $baseAttachments = Tinebase_FileSystem_RecordAttachments::getInstance()
             ->getRecordAttachments($event->getRecord());
@@ -156,10 +153,6 @@ class Calendar_Frontend_CalDAV_PluginManagedAttachmentsTest extends TestCase
      */
     public function testOverwriteAttachment()
     {
-        if ($this->_dbIsPgsql()) {
-            $this->markTestSkipped('0011668: fix Calendar_Frontend_* Tests with postgresql backend');
-        }
-
         $_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.21) Gecko/20110831 Lightning/1.0b2 Thunderbird/3.1.13';
 
         $event = $this->calDAVTests->createEventWithAttachment();
@@ -178,12 +171,8 @@ class Calendar_Frontend_CalDAV_PluginManagedAttachmentsTest extends TestCase
         
         $agenda = 'GODDBYE WORLD';
         $request->setBody($agenda);
-        
-        $this->server->httpRequest = $request;
-        $this->server->exec();
 
-        $vcalendar = stream_get_contents($this->response->body);
-        //         echo $vcalendar;
+        $vcalendar = $this->_execAndGetVCalendarFromRequest($request);
         
         $attachments = Tinebase_FileSystem_RecordAttachments::getInstance()
         ->getRecordAttachments($event->getRecord());
@@ -196,10 +185,6 @@ class Calendar_Frontend_CalDAV_PluginManagedAttachmentsTest extends TestCase
     
     public function testUpdateAttachment()
     {
-        if ($this->_dbIsPgsql()) {
-            $this->markTestSkipped('0011668: fix Calendar_Frontend_* Tests with postgresql backend');
-        }
-
         $_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.21) Gecko/20110831 Lightning/1.0b2 Thunderbird/3.1.13';
 
         $event = $this->calDAVTests->createEventWithAttachment();
@@ -220,25 +205,15 @@ class Calendar_Frontend_CalDAV_PluginManagedAttachmentsTest extends TestCase
         $agenda = 'GODDBYE WORLD';
         $request->setBody($agenda);
         
-        $this->server->httpRequest = $request;
-        $this->server->exec();
-        
-        $vcalendar = stream_get_contents($this->response->body);
-//         echo $vcalendar;
-        
-//         $this->assertEquals('HTTP/1.1 201 Created', $this->response->status);
+        $vcalendar = $this->_execAndGetVCalendarFromRequest($request);
         $this->assertContains('ATTACH;MANAGED-ID='. sha1($agenda), $vcalendar, $vcalendar);
          $this->assertNotContains($attachmentNode->hash, $vcalendar, 'old managed-id');
         //@TODO assert URI
-        //@TODO /fetch attachement & assert contents
+        //@TODO /fetch attachment & assert contents
     }
     
     public function testRemoveAttachment()
     {
-        if ($this->_dbIsPgsql()) {
-            $this->markTestSkipped('0011668: fix Calendar_Frontend_* Tests with postgresql backend');
-        }
-
         $_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.21) Gecko/20110831 Lightning/1.0b2 Thunderbird/3.1.13';
 
         $event = $this->calDAVTests->createEventWithAttachment();
@@ -256,18 +231,31 @@ class Calendar_Frontend_CalDAV_PluginManagedAttachmentsTest extends TestCase
                 'HTTP_DEPTH'     => '0',
         ));
         
-        $this->server->httpRequest = $request;
-        $this->server->exec();
-        
-        $vcalendar = stream_get_contents($this->response->body);
-//         echo $vcalendar;
-        
-//         $this->assertEquals('HTTP/1.1 204 No Content', $this->response->status);
+        $vcalendar = $this->_execAndGetVCalendarFromRequest($request);
         $this->assertNotContains('ATTACH;MANAGED-ID=', $vcalendar, $vcalendar);
         
         $attachments = Tinebase_FileSystem_RecordAttachments::getInstance()->getRecordAttachments($event->getRecord());
         $this->assertEquals(0, $attachments->count());
     }
+
+    /**
+     * exec request and get vcalendar
+     *
+     * @param Sabre\HTTP\Request $request
+     * @return string
+     */
+    protected function _execAndGetVCalendarFromRequest($request)
+    {
+        $this->server->httpRequest = $request;
+        $this->server->exec();
+
+        $this->assertTrue(is_resource($this->response->body), 'expecting a resource, got: ' . $this->response->body);
+        $vcalendar = stream_get_contents($this->response->body);
+
+//         echo $vcalendar;
+
+        return $vcalendar;
+    }
     
     public function testCreateRecurringExceptionWithManagedBaseAttachment()
     {
index 2814525..85e683e 100644 (file)
@@ -247,12 +247,21 @@ class Calendar_Frontend_WebDAV_EventTest extends Calendar_TestCase
         $record = $event->getRecord();
         $exdate = $record->exdate[0];
         $this->assertEquals('New Event', $record->summary);
-        $this->assertEquals(Tinebase_Core::getUser()->contact_id, $exdate->organizer,
-            'organizer mismatch, expected :' . print_r(Addressbook_Controller_Contact::getInstance()->get(Tinebase_Core::getUser()->contact_id)->toArray(), TRUE) .
-            'got: ' . print_r(Addressbook_Controller_Contact::getInstance()->get($exdate->organizer)->toArray(), TRUE));
-        $this->assertTrue(in_array(Tinebase_Core::getUser()->contact_id, $exdate->attendee->user_id), 'user contact id not found in exdate attendee: ' . print_r($exdate->attendee->toArray(), TRUE));
+
+        $organizer = (is_object($exdate->organizer)) ? $exdate->organizer->getId() : $exdate->organizer;
+        $this->assertEquals(Tinebase_Core::getUser()->contact_id, $organizer,
+            'organizer mismatch, expected :'
+                . print_r(Addressbook_Controller_Contact::getInstance()->get(
+                    Tinebase_Core::getUser()->contact_id
+                )->toArray(), TRUE) .
+            'got: '
+                . print_r(Addressbook_Controller_Contact::getInstance()->get($exdate->organizer)->toArray(), TRUE));
+        $this->assertTrue(in_array(Tinebase_Core::getUser()->contact_id, $exdate->attendee->user_id),
+            'user contact id not found in exdate attendee: ' . print_r($exdate->attendee->toArray(), TRUE));
+
         foreach ($exdate->attendee as $attender) {
-            $this->assertTrue(! empty($attender->displaycontainer_id), 'displaycontainer_id not set for attender: ' . print_r($attender->toArray(), TRUE));
+            $this->assertTrue(! empty($attender->displaycontainer_id),
+                'displaycontainer_id not set for attender: ' . print_r($attender->toArray(), TRUE));
         }
     }
     
index 50075c7..14224bc 100644 (file)
@@ -284,7 +284,8 @@ class Calendar_Frontend_ActiveSync extends ActiveSync_Frontend_Abstract implemen
                     
                 case 'attendee':
                     if ($this->_device->devicetype === Syncroton_Model_Device::TYPE_IPHONE &&
-                        $this->_syncFolderId       !== $this->_getDefaultContainerId()) {
+                        // note: might comparing an integer with a string here (at least with pgsql)
+                        $this->_syncFolderId       != $this->_getDefaultContainerId()) {
                         
                         continue;
                     }
index f6a54e2..4718543 100644 (file)
@@ -85,7 +85,8 @@ class Calendar_Frontend_CalDAV_PluginManagedAttachments extends \Sabre\DAV\Serve
      * @param array $returnedProperties
      * @return void
      */
-    public function beforeGetProperties($path, \Sabre\DAV\INode $node, &$requestedProperties, &$returnedProperties) {
+    public function beforeGetProperties($path, \Sabre\DAV\INode $node, &$requestedProperties, &$returnedProperties)
+    {
         if ($node instanceof \Sabre\DAVACL\IPrincipal) {
             // dropbox-home-URL property
             $scheduleProp = '{' . \Sabre\CalDAV\Plugin::NS_CALENDARSERVER . '}dropbox-home-URL';
@@ -251,7 +252,7 @@ class Calendar_Frontend_CalDAV_PluginManagedAttachments extends \Sabre\DAV\Serve
      * 
      * @param  Calendar_Model_Event $event
      * @param  array $rid
-     * @param  Function $method
+     * @param  callable $method
      * @return Tinebase_Record_RecordSet affectedEvents
      */
     protected function _iterateByRid($event, $rid, $method)
@@ -269,8 +270,10 @@ class Calendar_Frontend_CalDAV_PluginManagedAttachments extends \Sabre\DAV\Serve
                 }
             }
         }
-        foreach($affectedEvents as $record) {
-            if ($method($record) === false) break;
+        foreach ($affectedEvents as $record) {
+            if ($method($record) === false) {
+                break;
+            }
         }
         
         return $affectedEvents;
index 64ab3cf..81612dd 100644 (file)
@@ -95,11 +95,11 @@ class Tinebase_Record_RecordSet implements IteratorAggregate, Countable, ArrayAc
     }
     
     /**
-     * add Tinebase_Record_Interface like object to internal list
+     * add Tinebase_Record_Interface like object to internal list (it is not inserted if record is already in set)
      *
      * @param Tinebase_Record_Interface $_record
      * @param integer $_index
-     * @return int index in set of inserted record
+     * @return int index in set of inserted record or index of existing record
      */
     public function addRecord(Tinebase_Record_Interface $_record, $_index = NULL)
     {
@@ -107,12 +107,20 @@ class Tinebase_Record_RecordSet implements IteratorAggregate, Countable, ArrayAc
             throw new Tinebase_Exception_Record_NotAllowed('Attempt to add/set record of wrong record class ('
                 . get_class($_record) . ') Should be ' . $this->_recordClass);
         }
+
+        $recordId = $_record->getId();
+
+        if ($recordId && isset($this->_idMap[$recordId]) && isset($this->_listOfRecords[$this->_idMap[$recordId]])) {
+            if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__
+                . ' Record (id ' . $recordId . ') already in set - we don\'t want duplicates)');
+            return $this->_idMap[$recordId];
+        }
+
         $this->_listOfRecords[] = $_record;
         end($this->_listOfRecords);
         $index = ($_index !== NULL) ? $_index : key($this->_listOfRecords);
         
         // maintain indices
-        $recordId = $_record->getId();
         if ($recordId) {
             $this->_idMap[$recordId] = $index;
         } else {
@@ -127,7 +135,7 @@ class Tinebase_Record_RecordSet implements IteratorAggregate, Countable, ArrayAc
      */
     public function removeAll()
     {
-        foreach($this->_listOfRecords as $record) {
+        foreach ($this->_listOfRecords as $record) {
             $this->removeRecord($record);
         }
     }
@@ -763,12 +771,11 @@ class Tinebase_Record_RecordSet implements IteratorAggregate, Countable, ArrayAc
     
     /**
      * translate all member records of this set
-     * 
      */
     public function translate()
     {
         foreach ($this->_listOfRecords as $record) {
             $record->translate();
         }
-    }    
+    }
 }