0009954: resource manager and email handling
authorPhilipp Schüle <p.schuele@metaways.de>
Fri, 30 May 2014 13:42:24 +0000 (15:42 +0200)
committerPhilipp Schüle <p.schuele@metaways.de>
Fri, 13 Jun 2014 09:29:49 +0000 (11:29 +0200)
* converts resource to contact when sending event notifications
* some code cleanup / generalization

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

Change-Id: I143ace1c4f18a0af1fc4977a96eea530d6565e48
Reviewed-on: http://gerrit.tine20.com/customers/712
Tested-by: Jenkins CI (http://ci.tine20.com/)
Reviewed-by: Philipp Schüle <p.schuele@metaways.de>
tests/tine20/Calendar/Controller/EventNotificationsTests.php
tests/tine20/Calendar/Controller/ResourceTest.php
tests/tine20/Calendar/TestCase.php
tine20/Calendar/Controller/EventNotifications.php
tine20/Calendar/Model/Attender.php
tine20/Tinebase/Notification.php

index 87c9ccb..4fb0fda 100644 (file)
@@ -1147,22 +1147,6 @@ class Calendar_Controller_EventNotificationsTests extends Calendar_TestCase
     }
     
     /**
-     * create new attender
-     * 
-     * @param string $_userId
-     * @return Calendar_Model_Attender
-     */
-    protected function _createAttender($_userId)
-    {
-        return new Calendar_Model_Attender(array(
-            'user_id'        => $_userId,
-            'user_type'      => Calendar_Model_Attender::USERTYPE_USER,
-            'role'           => Calendar_Model_Attender::ROLE_REQUIRED,
-            'status_authkey' => Tinebase_Record_Abstract::generateUID(),
-        ));
-    }
-    
-    /**
      * setup preferences for personas
      * 
      * jsmith   -> no updates
@@ -1209,4 +1193,32 @@ class Calendar_Controller_EventNotificationsTests extends Calendar_TestCase
             $preferences->setValueForUser(Tinebase_Preference::LOCALE, 'en', $account->getId(), TRUE);
         }
     }
+    
+    /**
+     * testResourceNotification
+     * 
+     * checks if notification mail is sent to configured mail address of a resource
+     * 
+     * @see 0009954: resource manager and email handling
+     */
+    public function testResourceNotification()
+    {
+        // create resource with email address of unittest user
+        $resource = $this->_getResource();
+        $resource->email = Tinebase_Core::getUser()->accountEmailAddress;
+        $persistentResource = Calendar_Controller_Resource::getInstance()->create($resource);
+        
+        // create event with this resource as attender
+        $event = $this->_getEvent(/* now = */ true);
+        $event->attendee->addRecord($this->_createAttender($persistentResource->getId(), Calendar_Model_Attender::USERTYPE_RESOURCE));
+
+        self::flushMailer();
+        $persistentEvent = $this->_eventController->create($event);
+        
+        $this->assertEquals(3, count($persistentEvent->attendee));
+
+        $messages = self::getMessages();
+        
+        $this->assertEquals(2, count($messages), 'two mails should be send to current user (resource + attender)');
+    }
 }
index 33f2f28..ecf3c46 100644 (file)
@@ -42,22 +42,17 @@ class Calendar_Controller_ResourceTest extends Calendar_TestCase
     
     public function testCreateResource()
     {
-        $resource = new Calendar_Model_Resource(array(
-            'name'                 => 'Meeting Room',
-            'description'          => 'Our main meeting room',
-            'email'                => 'room@example.com',
-            'is_location'          => TRUE,
-        ));
+        $resource = $this->_getResource();
         
-        $persitentResource = Calendar_Controller_Resource::getInstance()->create($resource);
-        $this->_toCleanup->addRecord($persitentResource);
+        $persistentResource = Calendar_Controller_Resource::getInstance()->create($resource);
+        $this->_toCleanup->addRecord($persistentResource);
         
-        $this->assertEquals($resource->name, $persitentResource->name);
+        $this->assertEquals($resource->name, $persistentResource->name);
         
         return $resource;
     }
     
-    public function testRecouseConfict()
+    public function testResourceConfict()
     {
         $resource = $this->testCreateResource();
         
index 0513305..875a080 100644 (file)
@@ -183,25 +183,41 @@ abstract class Calendar_TestCase extends TestCase
     protected function _getAttendee()
     {
         return new Tinebase_Record_RecordSet('Calendar_Model_Attender', array(
-            array(
-                'user_id'        => $this->_testUserContact->getId(),
-                'user_type'      => Calendar_Model_Attender::USERTYPE_USER,
-                'role'           => Calendar_Model_Attender::ROLE_REQUIRED,
-                'status_authkey' => Tinebase_Record_Abstract::generateUID(),
-            ),
-            array(
-                'user_id'        => $this->_personasContacts['sclever']->getId(),
-                'user_type'      => Calendar_Model_Attender::USERTYPE_USER,
-                'role'           => Calendar_Model_Attender::ROLE_REQUIRED,
-                'status_authkey' => Tinebase_Record_Abstract::generateUID(),
-            ),
-            /* no group suppoert yet
-            array(
-                'user_id'        => Tinebase_Core::getUser()->accountPrimaryGroup,
-                'user_type'      => Calendar_Model_Attender::USERTYPE_GROUP,
-                'status_authkey' => Tinebase_Record_Abstract::generateUID(),
-            )
-            */
+            $this->_createAttender($this->_testUserContact->getId())->toArray(),
+            $this->_createAttender($this->_personasContacts['sclever']->getId())->toArray(),
+        ));
+    }
+    
+
+    /**
+     * create new attender
+     *
+     * @param string $userId
+     * @param string $type
+     * @return Calendar_Model_Attender
+     */
+    protected function _createAttender($userId, $type = Calendar_Model_Attender::USERTYPE_USER)
+    {
+        return new Calendar_Model_Attender(array(
+            'user_id'        => $userId,
+            'user_type'      => $type,
+            'role'           => Calendar_Model_Attender::ROLE_REQUIRED,
+            'status_authkey' => Tinebase_Record_Abstract::generateUID(),
+        ));
+    }
+    
+    /**
+     * get resource
+     * 
+     * @return Calendar_Model_Resource
+     */
+    protected function _getResource()
+    {
+        return new Calendar_Model_Resource(array(
+            'name'                 => 'Meeting Room',
+            'description'          => 'Our main meeting room',
+            'email'                => 'room@example.com',
+            'is_location'          => TRUE,
         ));
     }
     
index 76e9783..826bf25 100644 (file)
     {
         // we only send notifications to attendee
         if (! $_event->attendee instanceof Tinebase_Record_RecordSet) {
+            if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__
+                . " Event has no attendee");
             return;
         }
 
         if ($_event->dtend === NULL) {
-            if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . " " . print_r($_event->toArray(), TRUE));
+            if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ 
+                . " ". print_r($_event->toArray(), TRUE));
             throw new Tinebase_Exception_UnexpectedValue('no dtend set in event');
         }
         
-        // skip notifications to past events
         if (Tinebase_DateTime::now()->subHour(1)->isLater($_event->dtend)) {
             if ($_action == 'alarm' || ! ($_event->isRecurException() || $_event->rrule)) {
+                if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ 
+                    . " Skip notifications to past events");
                 return;
             }
         }
         $attendee = clone $_event->attendee;
         Calendar_Model_Attender::resolveAttendee($attendee);
         
+        if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) Tinebase_Core::getLogger()->trace(__METHOD__ . '::' . __LINE__
+            . " " . print_r($_event->toArray(), true));
+        if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__
+            . " Notification action: " . $_action);
+        
         switch ($_action) {
             case 'alarm':
                 foreach($_event->attendee as $attender) {
             // check if user wants this notification NOTE: organizer gets mails unless she set notificationlevel to NONE
             // NOTE prefUser is organzier for external notifications
             if (($attendeeAccountId == $_updater->getId() && ! $sendOnOwnActions) || ($sendLevel < $_notificationLevel && ($attendeeAccountId != $organizerAccountId || $sendLevel == self::NOTIFICATION_LEVEL_NONE))) {
-                if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . " Preferred notification level not reached -> skipping notification for {$_attender->getEmail()}");
+                if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__
+                    . " Preferred notification level not reached -> skipping notification for {$_attender->getEmail()}");
                 return;
             }
             
         
             Tinebase_Notification::getInstance()->send($sender, array($attendee), $messageSubject, $messageBody, $calendarPart, $attachments);
         } catch (Exception $e) {
-            if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . " exception: " . $e);
+            Tinebase_Exception::log($e);
             return;
         }
     }
index f4000ba..abc777f 100644 (file)
@@ -182,7 +182,19 @@ class Calendar_Model_Attender extends Tinebase_Record_Abstract
         $resolvable = new Tinebase_Record_RecordSet('Calendar_Model_Attender', array($clone));
         self::resolveAttendee($resolvable);
         
-        return $clone->user_id;
+        if ($this->user_type === self::USERTYPE_RESOURCE) {
+            $resource = $clone->user_id;
+            // return pseudo contact with resource data
+            $result = new Addressbook_Model_Contact(array(
+                'n_family'  => $resource->name,
+                'email'     => $resource->email,
+                'id'        => $resource->getId(),
+            ));
+        } else {
+            $result = $clone->user_id;
+        }
+        
+        return $result;
     }
     
     public function getStatusString()
index 17afc28..a316e82 100644 (file)
@@ -74,9 +74,9 @@ class Tinebase_Notification
         
         $exception = NULL;
         $sentContactIds = array();
-        foreach($_recipients as $recipient) {
+        foreach ($_recipients as $recipient) {
             try {
-                if (!$recipient instanceof Addressbook_Model_Contact) {
+                if (! $recipient instanceof Addressbook_Model_Contact) {
                     $recipient = $contactsBackend->get($recipient);
                 }
                 if (! in_array($recipient->getId(), $sentContactIds)) {