fixes/improves some more tests for ldap backend
[tine20] / tests / tine20 / Calendar / Model / AttenderTests.php
1 <?php
2 /**
3  * Tine 2.0 - http://www.tine20.org
4  * 
5  * @package     Calendar
6  * @license     http://www.gnu.org/licenses/agpl.html AGPL Version 3
7  * @copyright   Copyright (c) 2009-2013 Metaways Infosystems GmbH (http://www.metaways.de)
8  * @author      Cornelius Weiss <c.weiss@metaways.de>
9  */
10
11 /**
12  * Test helper
13  */
14 require_once dirname(dirname(dirname(__FILE__))) . DIRECTORY_SEPARATOR . 'TestHelper.php';
15
16 /**
17  * Test class for Calendar_Model_Attender
18  * 
19  * @package     Calendar
20  */
21 class Calendar_Model_AttenderTests extends Calendar_TestCase
22 {
23     protected $_testEmailContacts = array();
24     
25     /**
26      * tear down tests
27      *
28      */
29     public function tearDown()
30     {
31         parent::tearDown();
32         
33         foreach($this->_testEmailContacts as $email) {
34             $contactIdsToDelete = Addressbook_Controller_Contact::getInstance()->search(new Addressbook_Model_ContactFilter(array(
35                 array('field' => 'containerType', 'operator' => 'equals', 'value' => 'all'),
36                 array('field' => 'email',      'operator'  => 'equals', 'value' => $email)
37             )), null, false, true);
38             
39             Addressbook_Controller_Contact::getInstance()->delete($contactIdsToDelete);
40         }
41     }
42     
43     /**
44      * @return void
45      */
46     public function testEmailsToAttendee()
47     {
48         $event = $this->_getEvent();
49         
50         $persistentEvent = Calendar_Controller_Event::getInstance()->create($event);
51         
52         $sclever = Tinebase_User::getInstance()->getUserByLoginName('sclever', 'Tinebase_Model_FullUser');
53         $newEmail = Tinebase_Record_Abstract::generateUID() . '@unittest.com';
54         
55         // delete newly created contact in tearDown()
56         $this->_testEmailContacts[] = $newEmail;
57         
58         $newAttendees = array(
59             array(
60                 'userType'    => Calendar_Model_Attender::USERTYPE_USER,
61                 'firstName'   => $this->_originalTestUser->accountFirstName,
62                 'lastName'    => $this->_originalTestUser->accountLastName,
63                 'partStat'    => Calendar_Model_Attender::STATUS_ACCEPTED,
64                 'role'        => Calendar_Model_Attender::ROLE_REQUIRED,
65                 'email'       => $this->_originalTestUser->accountEmailAddress
66             ),
67             array(
68                 'userType'    => Calendar_Model_Attender::USERTYPE_USER,
69                 'displayName' => $sclever->accountDisplayName,
70                 'partStat'    => Calendar_Model_Attender::STATUS_DECLINED,
71                 'role'        => Calendar_Model_Attender::ROLE_REQUIRED,
72                 'email'       => $sclever->accountEmailAddress
73             ),
74             array(
75                 'userType'    => Calendar_Model_Attender::USERTYPE_USER,
76                 'firstName'   => 'Lars',
77                 'lastName'    => 'Kneschke',
78                 'partStat'    => Calendar_Model_Attender::STATUS_TENTATIVE,
79                 'role'        => Calendar_Model_Attender::ROLE_REQUIRED,
80                 'email'       => $newEmail
81             )
82         );
83         
84         Calendar_Model_Attender::emailsToAttendee($persistentEvent, $newAttendees, TRUE);
85         
86         $this->assertEquals(3, count($persistentEvent->attendee));
87         $this->assertEquals(1, count($persistentEvent->attendee->filter('status', Calendar_Model_Attender::STATUS_ACCEPTED)));
88         $this->assertEquals(1, count($persistentEvent->attendee->filter('status', Calendar_Model_Attender::STATUS_DECLINED)));
89         $this->assertEquals(1, count($persistentEvent->attendee->filter('status', Calendar_Model_Attender::STATUS_TENTATIVE)));
90     }
91     
92     /**
93      * @return void
94      */
95     public function testEmailsToAttendeeWithGroups()
96     {
97         if (Tinebase_User::getConfiguredBackend() === Tinebase_User::LDAP) {
98             $this->markTestSkipped('FIXME: Does not work with LDAP backend (full test suite run only)');
99         }
100
101         $event = $this->_getEvent();
102         
103         $persistentEvent = Calendar_Controller_Event::getInstance()->create($event);
104         
105         $primaryGroup = Tinebase_Group::getInstance()->getGroupById(Tinebase_Core::getUser()->accountPrimaryGroup);
106         
107         $newAttendees = array(
108             array(
109                 'userType'    => Calendar_Model_Attender::USERTYPE_USER,
110                 'firstName'   => $this->_originalTestUser->accountFirstName,
111                 'lastName'    => $this->_originalTestUser->accountLastName,
112                 'partStat'    => Calendar_Model_Attender::STATUS_TENTATIVE,
113                 'role'        => Calendar_Model_Attender::ROLE_REQUIRED,
114                 'email'       => $this->_originalTestUser->accountEmailAddress
115             ),
116             array(
117                 'userType'    => Calendar_Model_Attender::USERTYPE_GROUP,
118                 'displayName' => $primaryGroup->name,
119                 'partStat'    => Calendar_Model_Attender::STATUS_NEEDSACTION,
120                 'role'        => Calendar_Model_Attender::ROLE_REQUIRED,
121                 'email'       => 'foobar@example.org'
122             )
123         );
124         
125         Calendar_Model_Attender::emailsToAttendee($persistentEvent, $newAttendees, TRUE);
126         
127         $persistentEvent = Calendar_Controller_Event::getInstance()->update($persistentEvent);
128
129         $attendees = clone $persistentEvent->attendee;
130         
131         Calendar_Model_Attender::resolveAttendee($attendees);
132         
133         $newAttendees = array();
134         
135         foreach ($attendees as $attendee) {
136             $newAttendees[] = array(
137                 'userType'    => $attendee->user_type == 'group' ? Calendar_Model_Attender::USERTYPE_GROUP : Calendar_Model_Attender::USERTYPE_USER,
138                 'partStat'    => Calendar_Model_Attender::STATUS_TENTATIVE,
139                 'role'        => Calendar_Model_Attender::ROLE_REQUIRED,
140                 'email'       => $attendee->user_type == 'group' ? $attendee->user_id->getId() : $attendee->user_id->email,
141                 'displayName' => $attendee->user_type == 'group' ? $attendee->user_id->name : $attendee->user_id->n_fileas
142             );
143         }
144         
145         Calendar_Model_Attender::emailsToAttendee($persistentEvent, $newAttendees, TRUE);
146         
147         $persistentEvent = Calendar_Controller_Event::getInstance()->update($persistentEvent);
148         
149 //         print_r($persistentEvent->attendee->toArray());
150         
151         // there must be more than 2 attendees the user, the group + the groupmembers
152         $this->assertGreaterThan(2, count($persistentEvent->attendee));
153         
154         // current account must not be a groupmember
155         $this->assertFalse(!! Calendar_Model_Attender::getAttendee($persistentEvent->attendee, new Calendar_Model_Attender(array(
156             'user_type'    => Calendar_Model_Attender::USERTYPE_GROUPMEMBER,
157             'user_id'      => $this->_originalTestUser->contact_id
158         ))), 'found user as groupmember');
159         
160         $this->assertEquals(Calendar_Model_Attender::STATUS_TENTATIVE, Calendar_Model_Attender::getOwnAttender($persistentEvent->attendee)->status);
161     }
162     
163     public function testEmailsToAttendeeWithMissingMail()
164     {
165         $contact = new Addressbook_Model_Contact(array(
166             'org_name'   => 'unittestorg',
167             'email'      => '',
168             'email_home' => '',
169         ));
170         $persistentContact = Addressbook_Controller_Contact::getInstance()->create($contact, FALSE);
171         
172         $event = $this->_getEvent();
173         $event->attendee->addRecord(new Calendar_Model_Attender(array(
174             'user_type'    => Calendar_Model_Attender::USERTYPE_USER,
175             'user_id'      => $persistentContact->getId(),
176             'role'         => Calendar_Model_Attender::ROLE_REQUIRED
177         )));
178         
179         $persistentEvent = Calendar_Controller_Event::getInstance()->create($event);
180         
181         $clientAttendee = array();
182         foreach($persistentEvent->attendee as $attendee) {
183             $clientAttendee[] = array(
184                 'userType'  => Calendar_Model_Attender::USERTYPE_USER,
185                 'email'     => $attendee->getEmail(),
186                 'role'      => $attendee->role,
187             );
188         }
189         
190         Calendar_Model_Attender::emailsToAttendee($persistentEvent, $clientAttendee, TRUE);
191         
192         $userIds = $persistentEvent->attendee->user_id;
193         foreach($userIds as $idx => $id) {
194             if ($id instanceof Tinebase_Record_Abstract) {
195                 $userIds[$idx] = $id->getId();
196             }
197         }
198         $this->assertEquals(3, count($userIds));
199         $this->assertTrue(in_array($this->_testUserContact->getId(), $userIds), 'testaccount missing');
200         $this->assertTrue(in_array($this->_personasContacts['sclever']->getId(), $userIds), 'sclever missing');
201         $this->assertTrue(in_array($persistentContact->getId(), $userIds), 'unittestorg missing');
202     }
203     
204     /**
205      * testLongEmailAddress
206      * 
207      * @see 0009066: iCal .ics file import fails
208      * @see 0011000: Cannot accept invitation to meeting when organiser email is too long
209      */
210     public function testLongEmailAddress()
211     {
212         $attenderData = array(
213             'email' => '5:an_Opportunities/Conferenceroom/CMT_Marsta@corpmx01.external.atlascopco.com',
214             'n_family' => 'Opportunities/Conferenceroom/CMT',
215             'n_given' => '5:an'
216         );
217         
218         $contact = Calendar_Model_Attender::resolveEmailToContact($attenderData);
219         $this->assertEquals(strtolower($attenderData['email']), $contact->email);
220     }
221 }