d8a2ba7b3d4d9a0b285166172e8bf68d973e2b68
[tine20] / tests / tine20 / Felamimail / Frontend / JsonTest.php
1 <?php
2
3 use Sabre\DAV;
4
5 /**
6  * Tine 2.0 - http://www.tine20.org
7  *
8  * @package     Felamimail
9  * @license     http://www.gnu.org/licenses/agpl.html
10  * @copyright   Copyright (c) 2009-2014 Metaways Infosystems GmbH (http://www.metaways.de)
11  * @author      Philipp Schüle <p.schuele@metaways.de>
12  */
13
14 /**
15  * Test helper
16  */
17 require_once dirname(dirname(dirname(__FILE__))) . DIRECTORY_SEPARATOR . 'TestHelper.php';
18
19 /**
20  * Test class for Tinebase_Group
21  */
22 class Felamimail_Frontend_JsonTest extends TestCase
23 {
24     /**
25      * @var Felamimail_Frontend_Json
26      */
27     protected $_json = array();
28
29     /**
30      * message ids to delete
31      *
32      * @var array
33      */
34     protected $_messageIds = array();
35     
36     /**
37      * @var Felamimail_Model_Account
38      */
39     protected $_account = NULL;
40     
41     /**
42      * imap backend
43
44      * @var Felamimail_Backend_ImapProxy
45      */
46     protected $_imap = NULL;
47     
48     /**
49      * name of the folder to use for tests
50      * @var string
51      */
52     protected $_testFolderName = 'Junk';
53     
54     /**
55      * folders to delete in tearDown()
56      * 
57      * @var array
58      */
59     protected $_createdFolders = array();
60
61     /**
62      * are there messages to delete?
63      * 
64      * @var array
65      */
66     protected $_foldersToClear = array();
67
68     /**
69      * active sieve script name to be restored
70      *
71      * @var string
72      */
73     protected $_oldActiveSieveScriptName = NULL;
74
75     /**
76      * was sieve_vacation_active ?
77      *
78      * @var boolean
79      */
80     protected $_oldSieveVacationActiveState = FALSE;
81     
82     /**
83      * old sieve data
84      *
85      * @var Felamimail_Sieve_Backend_Sql
86      */
87     protected $_oldSieveData = NULL;
88
89     /**
90      * sieve script name to delete
91      *
92      * @var string
93      */
94     protected $_testSieveScriptName = NULL;
95
96     /**
97      * sieve vacation template file name
98      *
99      * @var string
100      */
101     protected $_sieveVacationTemplateFile = 'vacation_template.tpl';
102
103     /**
104      * test email domain
105      *
106      * @var string
107      */
108     protected $_mailDomain = 'tine20.org';
109
110     /**
111      * @var Felamimail_Model_Folder
112      */
113     protected $_folder = NULL;
114
115     /**
116      * paths in the vfs to delete
117      *
118      * @var array
119      */
120     protected $_pathsToDelete = array();
121
122     /**
123      *
124      * @var Tinebase_Frontend_Json
125      */
126     protected $_frontend = NULL;
127     
128     /**
129      * Sets up the fixture.
130      * This method is called before a test is executed.
131      *
132      * @access protected
133      */
134     protected function setUp()
135     {
136         Tinebase_TransactionManager::getInstance()->startTransaction(Tinebase_Core::getDb());
137         
138         // get (or create) test accout
139         $this->_account = Felamimail_Controller_Account::getInstance()->search()->getFirstRecord();
140         $this->_oldSieveVacationActiveState = $this->_account->sieve_vacation_active;
141         try {
142             $this->_oldSieveData = new Felamimail_Sieve_Backend_Sql($this->_account);
143         } catch (Tinebase_Exception_NotFound $tenf) {
144             // do nothing
145         }
146         
147         $this->_json = new Felamimail_Frontend_Json();
148         $this->_imap = Felamimail_Backend_ImapFactory::factory($this->_account);
149         
150         foreach (array($this->_testFolderName, $this->_account->sent_folder, $this->_account->trash_folder) as $folderToCreate) {
151             // create folder if it does not exist
152             $this->_getFolder($folderToCreate);
153         }
154         
155         $this->_mailDomain = TestServer::getPrimaryMailDomain();
156
157         $this->_frontend = new Tinebase_Frontend_Json();
158     }
159
160     /**
161      * Tears down the fixture
162      * This method is called after a test is executed.
163      *
164      * @access protected
165      */
166     protected function tearDown()
167     {
168         if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__
169             . ' Tearing down ...');
170         
171         if (count($this->_createdFolders) > 0) {
172             foreach ($this->_createdFolders as $folderName) {
173                 //echo "delete $folderName\n";
174                 try {
175                     $this->_imap->removeFolder(Felamimail_Model_Folder::encodeFolderName($folderName));
176                 } catch (Zend_Mail_Storage_Exception $zmse) {
177                     // already deleted
178                 }
179             }
180             Felamimail_Controller_Cache_Folder::getInstance()->clear($this->_account);
181         }
182         
183         if (! empty($this->_foldersToClear)) {
184             foreach ($this->_foldersToClear as $folderName) {
185                 // delete test messages from given folders on imap server (search by special header)
186                 $this->_imap->selectFolder($folderName);
187                 $result = $this->_imap->search(array(
188                     'HEADER X-Tine20TestMessage jsontest'
189                 ));
190                 //print_r($result);
191                 foreach ($result as $messageUid) {
192                     $this->_imap->removeMessage($messageUid);
193                 }
194                 
195                 // clear message cache
196                 $folder = Felamimail_Controller_Folder::getInstance()->getByBackendAndGlobalName($this->_account->getId(), $folderName);
197                 Felamimail_Controller_Cache_Message::getInstance()->clear($folder);
198             }
199         }
200         
201         // sieve cleanup
202         if ($this->_testSieveScriptName !== NULL) {
203             Felamimail_Controller_Sieve::getInstance()->setScriptName($this->_testSieveScriptName);
204             try {
205                 Felamimail_Controller_Sieve::getInstance()->deleteScript($this->_account->getId());
206             } catch (Zend_Mail_Protocol_Exception $zmpe) {
207                 // do not delete script if active
208             }
209             Felamimail_Controller_Account::getInstance()->setVacationActive($this->_account, $this->_oldSieveVacationActiveState);
210             
211             if ($this->_oldSieveData !== NULL) {
212                 $this->_oldSieveData->save();
213             }
214         }
215         if ($this->_oldActiveSieveScriptName !== NULL) {
216             Felamimail_Controller_Sieve::getInstance()->setScriptName($this->_oldActiveSieveScriptName);
217             Felamimail_Controller_Sieve::getInstance()->activateScript($this->_account->getId());
218         }
219         
220         // vfs cleanup
221         foreach ($this->_pathsToDelete as $path) {
222             $webdavRoot = new DAV\ObjectTree(new Tinebase_WebDav_Root());
223             //echo "delete $path";
224             $webdavRoot->delete($path);
225         }
226         
227         Tinebase_TransactionManager::getInstance()->rollBack();
228     }
229
230     /************************ test functions *********************************/
231     
232     /*********************** folder tests ****************************/
233     
234     /**
235      * test search folders (check order of folders as well)
236      */
237     public function testSearchFolders()
238     {
239         $filter = $this->_getFolderFilter();
240         $result = $this->_json->searchFolders($filter);
241         
242         $this->assertGreaterThan(1, $result['totalcount']);
243         $expectedFolders = array('INBOX', $this->_testFolderName, $this->_account->trash_folder, $this->_account->sent_folder);
244         
245         $foundCount = 0;
246         foreach ($result['results'] as $index => $folder) {
247             if (in_array($folder['localname'], $expectedFolders)) {
248                 $foundCount++;
249             }
250         }
251         $this->assertEquals(count($expectedFolders), $foundCount);
252     }
253     
254     /**
255      * clear test folder
256      */
257     public function testClearFolder()
258     {
259         $folderName = $this->_testFolderName;
260         $folder = $this->_getFolder($this->_testFolderName);
261         $folder = Felamimail_Controller_Folder::getInstance()->emptyFolder($folder->getId());
262
263         $filter = $this->_getMessageFilter($folder->getId());
264         $result = $this->_json->searchMessages($filter, '');
265         
266         $this->assertEquals(0, $result['totalcount'], 'Found too many messages in folder ' . $this->_testFolderName);
267         $this->assertEquals(0, $folder->cache_totalcount);
268     }
269
270     /**
271      * try to create some folders
272      */
273     public function testCreateFolders()
274     {
275         $filter = $this->_getFolderFilter();
276         $result = $this->_json->searchFolders($filter);
277         
278         $foldernames = array('test' => 'test', 'Schlüssel' => 'Schlüssel', 'test//1' => 'test1', 'test\2' => 'test2');
279         
280         foreach ($foldernames as $foldername => $expected) {
281             $result = $this->_json->addFolder($foldername, $this->_testFolderName, $this->_account->getId());
282             $globalname = $this->_testFolderName . $this->_account->delimiter . $expected;
283             $this->_createdFolders[] = $globalname;
284             $this->assertEquals($expected, $result['localname']);
285             $this->assertEquals($globalname, $result['globalname']);
286             $this->assertEquals(Felamimail_Model_Folder::CACHE_STATUS_EMPTY, $result['cache_status']);
287         }
288     }
289     
290     /**
291      * test emtpy folder (with subfolder)
292      */
293     public function testEmptyFolderWithSubfolder()
294     {
295         $folderName = $this->_testFolderName;
296         $folder = $this->_getFolder($this->_testFolderName);
297         $this->testCreateFolders();
298         
299         $folderArray = $this->_json->emptyFolder($folder->getId());
300         $this->assertEquals(0, $folderArray['has_children']);
301         
302         $result = $this->_json->updateFolderCache($this->_account->getId(), $this->_testFolderName);
303         $this->assertEquals(0, count($result));
304     }
305     
306     /**
307      * testUpdateFolderCache
308      */
309     public function testUpdateFolderCache()
310     {
311         $result = $this->_json->updateFolderCache($this->_account->getId(), '');
312         
313         // create folders directly on imap server
314         $this->_imap->createFolder('test', $this->_testFolderName, $this->_account->delimiter);
315         $this->_imap->createFolder('testsub', $this->_testFolderName . $this->_account->delimiter . 'test', $this->_account->delimiter);
316         // if something goes wrong, we need to delete these folders in tearDown
317         $this->_createdFolders[] = $this->_testFolderName . $this->_account->delimiter . 'test' . $this->_account->delimiter . 'testsub';
318         $this->_createdFolders[] = $this->_testFolderName . $this->_account->delimiter . 'test';
319         
320         if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__
321             . ' Update cache and check if folder is found');
322         
323         $result = $this->_json->updateFolderCache($this->_account->getId(), $this->_testFolderName);
324         $testfolder = $result[0];
325         $this->assertGreaterThan(0, count($result));
326         $this->assertEquals($this->_testFolderName . $this->_account->delimiter . 'test', $testfolder['globalname']);
327         $this->assertEquals(TRUE, (bool)$testfolder['has_children'], 'should have children');
328         
329         if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__
330             . ' Delete subfolder directly on imap server');
331         
332         $this->_imap->removeFolder($this->_testFolderName . $this->_account->delimiter . 'test' . $this->_account->delimiter . 'testsub');
333         array_shift($this->_createdFolders);
334         
335         if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__
336             . ' Check if has_children got updated and folder is removed from cache');
337         
338         $this->_json->updateFolderCache($this->_account->getId(), '');
339         $testfolder = $this->_getFolder($this->_testFolderName . $this->_account->delimiter . 'test');
340         $this->assertEquals(FALSE, (bool)$testfolder['has_children'], 'should have no children');
341
342         return $testfolder;
343     }
344     
345     /**
346      * testUpdateFolderCacheOfNonexistantFolder
347      *
348      * @see 0009800: unselectable folder with subfolders disappears
349      */
350     public function testUpdateFolderCacheOfNonexistantFolder()
351     {
352         $testfolder = $this->testUpdateFolderCache();
353         
354         try {
355             $folderName = $this->_testFolderName . $this->_account->delimiter . 'test' . $this->_account->delimiter . 'testsub';
356             if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__
357                 . ' Trying to fetch deleted folder ' . $folderName);
358             
359             $testfoldersub = Felamimail_Controller_Folder::getInstance()->getByBackendAndGlobalName($this->_account->getId(), $folderName);
360             $this->fail('Tinebase_Exception_NotFound expected when looking for folder ' . $folderName);
361         } catch (Tinebase_Exception_NotFound $tenf) {
362         }
363         
364         $this->_imap->removeFolder($this->_testFolderName . $this->_account->delimiter . 'test');
365         array_shift($this->_createdFolders);
366         
367         // try to update message cache of nonexistant folder
368         $removedTestfolder = $this->_json->updateMessageCache($testfolder['id'], 1);
369         $this->assertEquals(0, $removedTestfolder['is_selectable'], 'Folder should not be selectable');
370         
371         // update cache and check if folder is deleted
372         $result = $this->_json->updateFolderCache($this->_account->getId(), $this->_testFolderName);
373         $this->assertEquals(0, count($result));
374     }
375     
376     /*********************** accounts tests **************************/
377     
378     /**
379      * test search for accounts and check default account from config
380      */
381     public function testSearchAccounts()
382     {
383         $system = $this->_getSystemAccount();
384         
385         $this->assertTrue(! empty($system), 'no accounts found');
386         if (TestServer::getInstance()->getConfig()->mailserver) {
387             $this->assertEquals(TestServer::getInstance()->getConfig()->mailserver, $system['host']);
388             $this->assertEquals(TestServer::getInstance()->getConfig()->mailserver, $system['sieve_hostname']);
389         }
390     }
391     
392     /**
393      * get system account
394      *
395      * @return array
396      */
397     protected function _getSystemAccount()
398     {
399         $results = $this->_json->searchAccounts(array());
400         
401         $this->assertGreaterThan(0, $results['totalcount']);
402         $system = array();
403         foreach ($results['results'] as $result) {
404             if ($result['name'] == Tinebase_Core::getUser()->accountLoginName . '@' . $this->_mailDomain) {
405                 $system = $result;
406             }
407         }
408         
409         return $system;
410     }
411     
412     /**
413      * test change / delete of account
414      */
415     public function testChangeDeleteAccount()
416     {
417         $system = $this->_getSystemAccount();
418         unset($system['id']);
419         $system['type'] = Felamimail_Model_Account::TYPE_USER;
420         $account = $this->_json->saveAccount($system);
421         
422         $accountRecord = new Felamimail_Model_Account($account, TRUE);
423         $accountRecord->resolveCredentials(FALSE);
424         if (TestServer::getInstance()->getConfig()->mailserver) {
425             $this->assertEquals(TestServer::getInstance()->getConfig()->mailserver, $account['host']);
426         }
427         
428         $this->_json->changeCredentials($account['id'], $accountRecord->user, 'neuespasswort');
429         $account = $this->_json->getAccount($account['id']);
430         
431         $accountRecord = new Felamimail_Model_Account($account, TRUE);
432         $accountRecord->resolveCredentials(FALSE);
433         $this->assertEquals('neuespasswort', $accountRecord->password);
434         
435         $this->_json->deleteAccounts($account['id']);
436     }
437     
438     /*********************** message tests ****************************/
439     
440     /**
441      * test update message cache
442      */
443     public function testUpdateMessageCache()
444     {
445         $message = $this->_sendMessage();
446         $inbox = $this->_getFolder('INBOX');
447         // update message cache and check result
448         $result = $this->_json->updateMessageCache($inbox['id'], 30);
449         
450         if ($result['cache_status'] == Felamimail_Model_Folder::CACHE_STATUS_COMPLETE) {
451             $this->assertEquals($result['imap_totalcount'], $result['cache_totalcount'], 'totalcounts should be equal');
452         } else if ($result['cache_status'] == Felamimail_Model_Folder::CACHE_STATUS_INCOMPLETE) {
453             $this->assertNotEquals(0, $result['cache_job_actions_est']);
454         }
455     }
456     
457     /**
458      * test folder status
459      */
460     public function testGetFolderStatus()
461     {
462         $filter = $this->_getFolderFilter();
463         $result = $this->_json->searchFolders($filter);
464         $this->assertGreaterThan(1, $result['totalcount']);
465         $expectedFolders = array('INBOX', $this->_testFolderName, $this->_account->trash_folder, $this->_account->sent_folder);
466         
467         foreach ($result['results'] as $folder) {
468             $this->_json->updateMessageCache($folder['id'], 30);
469         }
470         
471         $message = $this->_sendMessage();
472         
473         $status = $this->_json->getFolderStatus(array(array('field' => 'account_id', 'operator' => 'equals', 'value' => $this->_account->getId())));
474         $this->assertEquals(1, count($status));
475         $this->assertEquals($this->_account->sent_folder, $status[0]['localname']);
476     }
477
478     /**
479      * test folder status of deleted folder
480      *
481      * @see 0007134: getFolderStatus should ignore non-existent folders
482      */
483     public function testGetFolderStatusOfDeletedFolder()
484     {
485         $this->testCreateFolders();
486         // remove one of the created folders
487         $removedFolder = $this->_createdFolders[0];
488         $this->_imap->removeFolder(Felamimail_Model_Folder::encodeFolderName($removedFolder));
489         
490         $status = $this->_json->getFolderStatus(array(array('field' => 'account_id', 'operator' => 'equals', 'value' => $this->_account->getId())));
491         $this->assertGreaterThan(2, count($status), 'Expected more than 2 folders that need an update: ' . print_r($status, TRUE));
492         foreach ($status as $folder) {
493             if ($folder['globalname'] == $removedFolder) {
494                 $this->fail('removed folder should not appear in status array!');
495             }
496         }
497     }
498     
499     /**
500      * test send message
501      */
502     public function testSendMessage()
503     {
504         // set email to unittest@tine20.org
505         $contactFilter = new Addressbook_Model_ContactFilter(array(
506             array('field' => 'n_family', 'operator' => 'equals', 'value' => 'Clever')
507         ));
508         $contactIds = Addressbook_Controller_Contact::getInstance()->search($contactFilter, NULL, FALSE, TRUE);
509         $this->assertTrue(count($contactIds) > 0, 'sclever not found in addressbook');
510
511         $contact = Addressbook_Controller_Contact::getInstance()->get($contactIds[0]);
512         $originalEmail =  $contact->email;
513         $contact->email = $this->_account->email;
514         $contact = Addressbook_Controller_Contact::getInstance()->update($contact, FALSE);
515
516         // send email
517         $messageToSend = $this->_getMessageData('unittestalias@' . $this->_mailDomain);
518         $messageToSend['note'] = 1;
519         $messageToSend['bcc']  = array(Tinebase_Core::getUser()->accountEmailAddress);
520         //print_r($messageToSend);
521         $returned = $this->_json->saveMessage($messageToSend);
522         $this->_foldersToClear = array('INBOX', $this->_account->sent_folder);
523         
524         // check if message is in sent folder
525         $message = $this->_searchForMessageBySubject($messageToSend['subject'], $this->_account->sent_folder);
526         $this->assertEquals($message['from_email'], $messageToSend['from_email']);
527         $this->assertTrue(isset($message['to'][0]));
528         $this->assertEquals($message['to'][0],      $messageToSend['to'][0], 'recipient not found');
529         $this->assertEquals($message['bcc'][0],     $messageToSend['bcc'][0], 'bcc recipient not found');
530         $this->assertEquals($message['subject'],    $messageToSend['subject']);
531         
532         $this->_checkEmailNote($contact, $messageToSend['subject']);
533         
534         // reset sclevers original email address
535         $contact->email = $originalEmail;
536         Addressbook_Controller_Contact::getInstance()->update($contact, FALSE);
537     }
538     
539     /**
540      * check email note
541      *
542      * @param Addressbook_Model_Contact $contact
543      * @param string $subject
544      */
545     protected function _checkEmailNote($contact, $subject)
546     {
547         // check if email note has been added to contact(s)
548         $contact = Addressbook_Controller_Contact::getInstance()->get($contact->getId());
549         $emailNoteType = Tinebase_Notes::getInstance()->getNoteTypeByName('email');
550         
551         // check / delete notes
552         $emailNotes = new Tinebase_Record_RecordSet('Tinebase_Model_Note');
553         foreach ($contact->notes as $note) {
554             if ($note->note_type_id == $emailNoteType->getId()) {
555                 $this->assertContains($subject, $note->note, 'did not find note subject');
556                 $this->assertEquals(Tinebase_Core::getUser()->getId(), $note->created_by);
557                 $this->assertContains('aaaaaä', $note->note);
558                 $emailNotes->addRecord($note);
559             }
560         }
561         $this->assertGreaterThan(0, $emailNotes->count(), 'no email notes found');
562         Tinebase_Notes::getInstance()->deleteNotes($emailNotes);
563     }
564
565     /**
566      * test send message to invalid recipient
567      */
568     public function testSendMessageToInvalidRecipient()
569     {
570         $messageToSend = $this->_getMessageData($this->_account->email);
571         $invalidEmail = 'invaliduser@' . $this->_mailDomain;
572         $messageToSend['to'] = array($invalidEmail);
573         
574         try {
575             $returned = $this->_json->saveMessage($messageToSend);
576             $this->fail('Tinebase_Exception_SystemGeneric expected');
577         } catch (Tinebase_Exception_SystemGeneric $tesg) {
578             $this->assertContains('550 5.1.1 <' . $invalidEmail . '>: Recipient address rejected', $tesg->getMessage(),
579                 'exception message did not match: ' . $tesg->getMessage());
580         }
581     }
582     
583     /**
584      * try to get a message from imap server (with complete body, attachments, etc)
585      *
586      * @see 0006300: add unique message-id header to new messages (for message-id check)
587      */
588     public function testGetMessage()
589     {
590         $message = $this->_sendMessage();
591         
592         // get complete message
593         $message = $this->_json->getMessage($message['id']);
594         
595         // check
596         $this->assertTrue(isset($message['headers']) && $message['headers']['message-id']);
597         $this->assertContains('@' . $this->_mailDomain, $message['headers']['message-id']);
598         $this->assertGreaterThan(0, preg_match('/aaaaaä/', $message['body']));
599         
600         // delete message on imap server and check if correct exception is thrown when trying to get it
601         $this->_imap->selectFolder('INBOX');
602         $this->_imap->removeMessage($message['messageuid']);
603         Tinebase_Core::getCache()->clean();
604         $this->setExpectedException('Felamimail_Exception_IMAPMessageNotFound');
605         $message = $this->_json->getMessage($message['id']);
606     }
607     
608     /**
609      * try to get a message as plain/text
610      */
611     public function testGetPlainTextMessage()
612     {
613         $accountBackend = new Felamimail_Backend_Account();
614         $message = $this->_sendMessage();
615         
616         // get complete message
617         $this->_account->display_format = Felamimail_Model_Account::DISPLAY_PLAIN;
618         $accountBackend->update($this->_account);
619         $message = $this->_json->getMessage($message['id']);
620         $this->_account->display_format = Felamimail_Model_Account::DISPLAY_HTML;
621         $accountBackend->update($this->_account);
622         
623         // check
624         $this->assertEquals("aaaaaä \n\r\n", $message['body']);
625     }
626     
627     /**
628      * try search for a message with path filter
629      */
630     public function testSearchMessageWithPathFilter()
631     {
632         $sentMessage = $this->_sendMessage();
633         $filter = array(array(
634             'field' => 'path', 'operator' => 'in', 'value' => '/' . $this->_account->getId()
635         ));
636         $result = $this->_json->searchMessages($filter, '');
637         $message = $this->_getMessageFromSearchResult($result, $sentMessage['subject']);
638         $this->assertTrue(! empty($message), 'Sent message not found with account path filter');
639
640         $inbox = $this->_getFolder('INBOX');
641         $filter = array(array(
642             'field' => 'path', 'operator' => 'in', 'value' => '/' . $this->_account->getId() . '/' . $inbox->getId()
643         ));
644         $result = $this->_json->searchMessages($filter, '');
645         $message = $this->_getMessageFromSearchResult($result, $sentMessage['subject']);
646         $this->assertTrue(! empty($message), 'Sent message not found with path filter');
647         foreach ($result['results'] as $mail) {
648             $this->assertEquals($inbox->getId(), $mail['folder_id'], 'message is in wrong folder: ' . print_r($mail, TRUE));
649         }
650     }
651     
652     /**
653      * try search for a message with all inboxes and flags filter
654      */
655     public function testSearchMessageWithAllInboxesFilter()
656     {
657         $sentMessage = $this->_sendMessage();
658         $filter = array(
659             array('field' => 'path',  'operator' => 'in',       'value' => Felamimail_Model_MessageFilter::PATH_ALLINBOXES),
660             array('field' => 'flags', 'operator' => 'notin',    'value' => Zend_Mail_Storage::FLAG_FLAGGED),
661         );
662         $result = $this->_json->searchMessages($filter, '');
663         $this->assertGreaterThan(0, $result['totalcount']);
664         $this->assertEquals($result['totalcount'], count($result['results']));
665         
666         $message = $this->_getMessageFromSearchResult($result, $sentMessage['subject']);
667         $this->assertTrue(! empty($message), 'Sent message not found with all inboxes filter');
668     }
669     
670     /**
671      * try search for a message with three cache filters to force a foreign relation join with at least 2 tables
672      */
673     public function testSearchMessageWithThreeCacheFilter()
674     {
675         $filter = array(
676             array('field' => 'flags',   'operator' => 'in',       'value' => Zend_Mail_Storage::FLAG_ANSWERED),
677             array('field' => 'to',      'operator' => 'contains', 'value' => 'testDOESNOTEXIST'),
678             array('field' => 'subject', 'operator' => 'contains', 'value' => 'testDOESNOTEXIST'),
679         );
680         $result = $this->_json->searchMessages($filter, '');
681         $this->assertEquals(0, $result['totalcount']);
682     }
683     
684     /**
685      * try search for a message with empty path filter
686      */
687     public function testSearchMessageEmptyPath()
688     {
689         $sentMessage = $this->_sendMessage();
690         
691         $filter = array(
692             array('field' => 'path',  'operator' => 'equals',   'value' => ''),
693         );
694         $result = $this->_json->searchMessages($filter, '');
695         
696         $this->assertEquals(0, $result['totalcount']);
697         $accountFilterFound = FALSE;
698         
699         foreach ($result['filter'] as $filter) {
700             if ($filter['field'] === 'account_id' && empty($filter['value'])) {
701                 $accountFilterFound = TRUE;
702                 break;
703             }
704         }
705         $this->assertTrue($accountFilterFound);
706     }
707     
708     /**
709      * test flags (add + clear + deleted)
710      */
711     public function testAddAndClearFlags()
712     {
713         $message = $this->_sendMessage();
714         $inboxBefore = $this->_getFolder('INBOX');
715         
716         $this->_json->addFlags($message['id'], Zend_Mail_Storage::FLAG_SEEN);
717         
718         // check if unread count got decreased
719         $inboxAfter = $this->_getFolder('INBOX');
720         $this->assertTrue($inboxBefore->cache_unreadcount - 1 == $inboxAfter->cache_unreadcount, 'wrong cache unreadcount');
721         
722         $message = $this->_json->getMessage($message['id']);
723         $this->assertTrue(in_array(Zend_Mail_Storage::FLAG_SEEN, $message['flags']), 'seen flag not set');
724         
725         // try with a filter
726         $filter = array(
727             array('field' => 'id', 'operator' => 'in', array($message['id']))
728         );
729         $this->_json->clearFlags($filter, Zend_Mail_Storage::FLAG_SEEN);
730         
731         $message = $this->_json->getMessage($message['id']);
732         $this->assertFalse(in_array(Zend_Mail_Storage::FLAG_SEEN, $message['flags']), 'seen flag should not be set');
733
734         $this->setExpectedException('Tinebase_Exception_NotFound');
735         $this->_json->addFlags(array($message['id']), Zend_Mail_Storage::FLAG_DELETED);
736         $this->_json->getMessage($message['id']);
737     }
738     
739     /**
740      * testMarkFolderRead
741      *
742      * @see 0009812: mark folder as read does not work with pgsql
743      */
744     public function testMarkFolderRead()
745     {
746         $inboxBefore = $this->_getFolder('INBOX');
747         $filter = array(array(
748             'field' => 'folder_id', 'operator' => 'equals', 'value' => $inboxBefore->getId()
749         ), array(
750             'field' => 'flags', 'operator' => 'notin', 'value' => array(Zend_Mail_Storage::FLAG_SEEN)
751         ));
752         $this->_json->addFlags($filter, Zend_Mail_Storage::FLAG_SEEN);
753         
754         $inboxAfter = $this->_getFolder('INBOX');
755         $this->assertEquals(0, $inboxAfter->cache_unreadcount);
756     }
757     
758     /**
759      * test delete from trash
760      */
761     public function testDeleteFromTrashWithFilter()
762     {
763         $message = $this->_sendMessage();
764         $this->_foldersToClear = array('INBOX', $this->_account->sent_folder, $this->_account->trash_folder);
765         
766         $trash = $this->_getFolder($this->_account->trash_folder);
767         $result = $this->_json->moveMessages(array(array(
768             'field' => 'id', 'operator' => 'in', 'value' => array($message['id'])
769         )), $trash->getId());
770
771         $messageInTrash = $this->_searchForMessageBySubject($message['subject'], $this->_account->trash_folder);
772         
773         // delete messages in trash with filter
774         $this->_json->addFlags(array(array(
775             'field' => 'folder_id', 'operator' => 'equals', 'value' => $trash->getId()
776         ), array(
777             'field' => 'id', 'operator' => 'in', 'value' => array($messageInTrash['id'])
778         )), Zend_Mail_Storage::FLAG_DELETED);
779         
780         $this->setExpectedException('Tinebase_Exception_NotFound');
781         $this->_json->getMessage($messageInTrash['id']);
782     }
783     
784     /**
785      * move message to trash with trash folder constant (Felamimail_Model_Folder::FOLDER_TRASH)
786      */
787     public function testMoveMessagesToTrash()
788     {
789         $message = $this->_sendMessage();
790         $this->_foldersToClear = array('INBOX', $this->_account->sent_folder, $this->_account->trash_folder);
791         
792         $result = $this->_json->moveMessages(array(array(
793             'field' => 'id', 'operator' => 'in', 'value' => array($message['id'])
794         )), Felamimail_Model_Folder::FOLDER_TRASH);
795
796         $messageInTrash = $this->_searchForMessageBySubject($message['subject'], $this->_account->trash_folder);
797     }
798     
799     /**
800      * test reply mail and check some headers
801      *
802      * @see 0006106: Add References header / https://forge.tine20.org/mantisbt/view.php?id=6106
803      */
804     public function testReplyMessage()
805     {
806         $message = $this->_sendMessage();
807         
808         $replyMessage = $this->_getReply($message);
809         $returned = $this->_json->saveMessage($replyMessage);
810         
811         $result = $this->_getMessages();
812         
813         $replyMessageFound = array();
814         $originalMessage = array();
815         foreach ($result['results'] as $mail) {
816             if ($mail['subject'] == $replyMessage['subject']) {
817                 $replyMessageFound = $mail;
818             }
819             if ($mail['subject'] == $message['subject']) {
820                 $originalMessage = $mail;
821             }
822         }
823         $replyMessageFound = $this->_json->getMessage($replyMessageFound['id']);
824         $originalMessage = $this->_json->getMessage($originalMessage['id']);
825         
826         $this->assertTrue(! empty($replyMessageFound), 'replied message not found');
827         $this->assertTrue(! empty($originalMessage), 'original message not found');
828         
829         // check headers
830         $this->assertTrue(isset($replyMessageFound['headers']['in-reply-to']));
831         $this->assertEquals($originalMessage['headers']['message-id'], $replyMessageFound['headers']['in-reply-to']);
832         $this->assertTrue(isset($replyMessageFound['headers']['references']));
833         $this->assertEquals($originalMessage['headers']['message-id'], $replyMessageFound['headers']['references']);
834         
835         // check answered flag
836         $this->assertTrue(in_array(Zend_Mail_Storage::FLAG_ANSWERED, $originalMessage['flags'], 'could not find flag'));
837     }
838     
839     /**
840      * get reply message data
841      *
842      * @param array $_original
843      * @return array
844      */
845     protected function _getReply($_original)
846     {
847         $replyMessage               = $this->_getMessageData();
848         $replyMessage['subject']    = 'Re: ' . $_original['subject'];
849         $replyMessage['original_id']= $_original['id'];
850         $replyMessage['flags']      = Zend_Mail_Storage::FLAG_ANSWERED;
851         
852         return $replyMessage;
853     }
854
855     /**
856      * test reply mail in sent folder
857      */
858     public function testReplyMessageInSentFolder()
859     {
860         $messageInSent = $this->_sendMessage($this->_account->sent_folder);
861         $replyMessage = $this->_getReply($messageInSent);
862         $returned = $this->_json->saveMessage($replyMessage);
863         
864         $result = $this->_getMessages();
865         $sentMessage = $this->_getMessageFromSearchResult($result, $replyMessage['subject']);
866         $this->assertTrue(! empty($sentMessage));
867     }
868
869     /**
870      * test reply mail with long references header
871      *
872      * @see 0006644: "At least one mail header line is too long"
873      */
874     public function testReplyMessageWithLongHeader()
875     {
876         $messageInSent = $this->_sendMessage($this->_account->sent_folder, array(
877             'references' => '<c95d8187-2c71-437e-adb8-5e1dcdbdc507@email.test.org>
878    <2601bbfa-566e-4490-a3db-aad005733d32@email.test.org>
879    <20120530154350.1854610131@ganymed.de>
880    <7e393ce1-d193-44fc-bf5f-30c61a271fe6@email.test.org>
881    <4FC8B49C.8040704@funk.de>
882    <dba2ad5c-6726-4171-8710-984847c010a1@email.test.org>
883    <20120601123551.5E98610131@ganymed.de>
884    <f1cc3195-8641-46e3-8f20-f60f3e16b107@email.test.org>
885    <20120619093658.37E4210131@ganymed.de>
886    <CA+6Rn2PX2Q3tOk2tCQfCjcaC8zYS5XZX327OoyJfUb+w87vCLQ@mail.net.com>
887    <20120619130652.03DD310131@ganymed.de>
888    <37616c6a-4c47-4b54-9ca6-56875bc9205d@email.test.org>
889    <20120620074843.42E2010131@ganymed.de>
890    <CA+6Rn2MAb2x0qeSfcaW6F=0S7LEQL442Sx2ha9RtwMs4B0esBg@mail.net.com>
891    <20120620092902.88C8C10131@ganymed.de>
892    <c95d8187-2c71-437e-adb8-5e1dcdbdc507@email.test.org>
893    <2601bbfa-566e-4490-a3db-aad005733d32@email.test.org>
894    <20120530154350.1854610131@ganymed.de>
895    <7e393ce1-d193-44fc-bf5f-30c61a271fe6@email.test.org>
896    <4FC8B49C.8040704@funk.de>
897    <dba2ad5c-6726-4171-8710-984847c010a1@email.test.org>
898    <20120601123551.5E98610131@ganymed.de>
899    <f1cc3195-8641-46e3-8f20-f60f3e16b107@email.test.org>
900    <20120619093658.37E4210131@ganymed.de>
901    <CA+6Rn2PX2Q3tOk2tCQfCjcaC8zYS5XZX327OoyJfUb+w87vCLQ@mail.net.com>
902    <20120619130652.03DD310131@ganymed.de>
903    <37616c6a-4c47-4b54-9ca6-56875bc9205d@email.test.org>
904    <20120620074843.42E2010131@ganymed.de>
905    <CA+6Rn2MAb2x0qeSfcaW6F=0S7LEQL442Sx2ha9RtwMs4B0esBg@mail.net.com>
906    <20120620092902.88C8C10131@ganymed.de>'
907         ));
908         $replyMessage = $this->_getReply($messageInSent);
909         $returned = $this->_json->saveMessage($replyMessage);
910         
911         $result = $this->_getMessages();
912         $sentMessage = $this->_getMessageFromSearchResult($result, $replyMessage['subject']);
913         $this->assertTrue(! empty($sentMessage));
914     }
915     
916     /**
917      * test move
918      */
919     public function testMoveMessage()
920     {
921         $message = $this->_sendMessage();
922         $this->_foldersToClear = array('INBOX', $this->_account->sent_folder, $this->_testFolderName);
923         
924         $inbox = $this->_getFolder('INBOX');
925         $inboxBefore = $this->_json->updateMessageCache($inbox['id'], 30);
926         
927         // move
928         $testFolder = $this->_getFolder($this->_testFolderName);
929         $result = $this->_json->moveMessages(array(array(
930             'field' => 'id', 'operator' => 'in', 'value' => array($message['id'])
931         )), $testFolder->getId());
932
933         // sleep for 2 secs because mailserver may be slower than expected
934         sleep(2);
935
936         $inboxAfter = $this->_getFolder('INBOX');
937         
938         // check if count was decreased correctly
939         $this->assertEquals($inboxBefore['cache_unreadcount'] - 1, $inboxAfter['cache_unreadcount']);
940         $this->assertEquals($inboxBefore['cache_totalcount'] - 1, $inboxAfter['cache_totalcount']);
941         
942         $result = $this->_getMessages($this->_testFolderName);
943         $movedMessage = array();
944         foreach ($result['results'] as $mail) {
945             if ($mail['subject'] == $message['subject']) {
946                 $movedMessage = $mail;
947             }
948         }
949         $this->assertTrue(! empty($movedMessage), 'moved message not found');
950     }
951     
952     /**
953      * forward message test
954      *
955      * @see 0007624: losing umlauts in attached filenames
956      */
957     public function testForwardMessageWithAttachment()
958     {
959         $testFolder = $this->_getFolder($this->_testFolderName);
960         $message = fopen(dirname(__FILE__) . '/../files/multipart_related.eml', 'r');
961         Felamimail_Controller_Message::getInstance()->appendMessage($testFolder, $message);
962         
963         $subject = 'Tine 2.0 bei Metaways - Verbessurngsvorschlag';
964         $message = $this->_searchForMessageBySubject($subject, $this->_testFolderName);
965         
966         $fwdSubject = 'Fwd: ' . $subject;
967         $forwardMessageData = array(
968             'account_id'    => $this->_account->getId(),
969             'subject'       => $fwdSubject,
970             'to'            => array($this->_getEmailAddress()),
971             'body'          => "aaaaaä <br>",
972             'headers'       => array('X-Tine20TestMessage' => 'jsontest'),
973             'original_id'   => $message['id'],
974             'attachments'   => array(new Tinebase_Model_TempFile(array(
975                 'type'  => Felamimail_Model_Message::CONTENT_TYPE_MESSAGE_RFC822,
976                 'name'  => 'Verbessurüngsvorschlag',
977             ), TRUE)),
978             'flags'         => Zend_Mail_Storage::FLAG_PASSED,
979         );
980         
981         $this->_foldersToClear[] = 'INBOX';
982         $this->_json->saveMessage($forwardMessageData);
983         $forwardMessage = $this->_searchForMessageBySubject($fwdSubject);
984         
985         // check attachment name
986         $forwardMessageComplete = $this->_json->getMessage($forwardMessage['id']);
987         $this->assertEquals(1, count($forwardMessageComplete['attachments']));
988         $this->assertEquals('Verbessurüngsvorschlag.eml', $forwardMessageComplete['attachments'][0]['filename'], 'umlaut missing from attachment filename');
989         
990         $forwardMessage = $this->_json->getMessage($forwardMessage['id']);
991         $this->assertTrue((isset($forwardMessage['structure']) || array_key_exists('structure', $forwardMessage)), 'structure should be set when fetching complete message: ' . print_r($forwardMessage, TRUE));
992         $this->assertEquals(Felamimail_Model_Message::CONTENT_TYPE_MESSAGE_RFC822, $forwardMessage['structure']['parts'][2]['contentType']);
993         
994         $message = $this->_json->getMessage($message['id']);
995         $this->assertTrue(in_array(Zend_Mail_Storage::FLAG_PASSED, $message['flags']), 'forwarded flag missing in flags: ' . print_r($message, TRUE));
996     }
997     
998     /**
999      * testSendMessageWithAttachmentWithoutExtension
1000      *
1001      * @see 0008328: email attachment without file extension is not sent properly
1002      */
1003     public function testSendMessageWithAttachmentWithoutExtension()
1004     {
1005         $subject = 'attachment test';
1006         $messageToSend = $this->_getMessageData('unittestalias@' . $this->_mailDomain, $subject);
1007         $tempfileName = 'jsontest' . Tinebase_Record_Abstract::generateUID(10);
1008         $tempfilePath = Tinebase_Core::getTempDir() . DIRECTORY_SEPARATOR . $tempfileName;
1009         file_put_contents($tempfilePath, 'some content');
1010         $tempFile = Tinebase_TempFile::getInstance()->createTempFile($tempfilePath, $tempfileName);
1011         $messageToSend['attachments'] = array(array('tempFile' => array('id' => $tempFile->getId())));
1012         $this->_json->saveMessage($messageToSend);
1013         $forwardMessage = $this->_searchForMessageBySubject($subject);
1014         $this->_foldersToClear = array('INBOX', $this->_account->sent_folder);
1015         
1016         $fullMessage = $this->_json->getMessage($forwardMessage['id']);
1017         $this->assertTrue(count($fullMessage['attachments']) === 1);
1018         $attachment = $fullMessage['attachments'][0];
1019         $this->assertContains($tempfileName, $attachment['filename'], 'wrong attachment filename: ' . print_r($attachment, TRUE));
1020         $this->assertEquals(16, $attachment['size'], 'wrong attachment size: ' . print_r($attachment, TRUE));
1021     }
1022     
1023     /**
1024      * save message in folder (draft) test
1025      *
1026      * @see 0007178: BCC does not save the draft message
1027      */
1028     public function testSaveMessageInFolder()
1029     {
1030         $messageToSave = $this->_getMessageData();
1031         $messageToSave['bcc'] = array('bccaddress@email.org', 'bccaddress2@email.org');
1032         
1033         $draftsFolder = $this->_getFolder($this->_account->drafts_folder);
1034         $returned = $this->_json->saveMessageInFolder($this->_account->drafts_folder, $messageToSave);
1035         $this->_foldersToClear = array($this->_account->drafts_folder);
1036         
1037         // check if message is in drafts folder and recipients are present
1038         $message = $this->_searchForMessageBySubject($messageToSave['subject'], $this->_account->drafts_folder);
1039         $this->assertEquals($messageToSave['subject'],  $message['subject']);
1040         $this->assertEquals($messageToSave['to'][0],    $message['to'][0], 'recipient not found');
1041         $this->assertEquals(2, count($message['bcc']), 'bcc recipient not found: ' . print_r($message, TRUE));
1042         $this->assertEquals($messageToSave['bcc'][0],   $message['bcc'][0], '1st bcc recipient not found');
1043         $this->assertEquals($messageToSave['bcc'][1],   $message['bcc'][1], '2nd bcc recipient not found');
1044     }
1045     
1046     /**
1047      * testSendReadingConfirmation
1048      *
1049      * @see 0007736: ask user before sending reading confirmation
1050      * @see 0008402: Wrong recipient with read confirmation
1051      */
1052     public function testSendReadingConfirmation()
1053     {
1054         $messageToSave = $this->_getMessageData();
1055         $messageToSave['headers']['disposition-notification-to'] = '"' . Tinebase_Core::getUser()->accountFullName . '" <' . $this->_account->email . '>';
1056         $returned = $this->_json->saveMessageInFolder($this->_testFolderName, $messageToSave);
1057         $messageWithReadingConfirmationHeader = $this->_searchForMessageBySubject($messageToSave['subject'], $this->_testFolderName);
1058         $this->_messageIds[] = $messageWithReadingConfirmationHeader['id'];
1059         $this->_json->sendReadingConfirmation($messageWithReadingConfirmationHeader['id']);
1060         
1061         $translate = Tinebase_Translation::getTranslation('Felamimail');
1062         $subject = $translate->_('Reading Confirmation:') . ' '. $messageToSave['subject'];
1063         $message = $this->_searchForMessageBySubject($subject);
1064         $this->_messageIds[] = $message['id'];
1065         
1066         $complete = $this->_json->getMessage($message['id']);
1067         $this->assertContains($translate->_('Was read by:') . ' ' . $this->_account->from, $complete['body']);
1068     }
1069
1070     /**
1071      * save message in non-existant folder (templates) test
1072      *
1073      * @see 0008476: Drafts are not working
1074      */
1075     public function testSaveMessageInNonExistantTemplatesFolder()
1076     {
1077         $messageToSave = $this->_getMessageData();
1078         
1079         $templatesFolder = $this->_getFolder($this->_account->templates_folder, FALSE);
1080         if ($templatesFolder) {
1081             $this->_json->deleteFolder($templatesFolder['id'], $this->_account->getId());
1082         }
1083         $returned = $this->_json->saveMessageInFolder($this->_account->templates_folder, $messageToSave);
1084         $this->_foldersToClear = array($this->_account->templates_folder);
1085         
1086         // check if message is in templates folder
1087         $message = $this->_searchForMessageBySubject($messageToSave['subject'], $this->_account->templates_folder);
1088         $this->assertEquals($messageToSave['subject'],  $message['subject']);
1089         $this->assertEquals($messageToSave['to'][0],    $message['to'][0], 'recipient not found');
1090     }
1091     
1092     /**
1093      * testSaveMessageNoteWithInvalidChar
1094      *
1095      * @see 0008644: error when sending mail with note (wrong charset)
1096      */
1097     public function testSaveMessageNoteWithInvalidChar()
1098     {
1099         $subject = Tinebase_Core::filterInputForDatabase("\xF0\x9F\x98\x8A\xC2"); // :-) emoji &nbsp;
1100         $messageData = $this->_getMessageData('', $subject);
1101         $messageData['note'] = true;
1102         $messageData['body'] .= "&nbsp;";
1103         
1104         $this->_foldersToClear[] = 'INBOX';
1105         $this->_json->saveMessage($messageData);
1106         $message = $this->_searchForMessageBySubject($subject);
1107         
1108         $contact = Addressbook_Controller_Contact::getInstance()->getContactByUserId(Tinebase_Core::getUser()->getId());
1109         $this->_checkEmailNote($contact, $subject);
1110     }
1111     
1112     /**
1113      * testSaveMessageNoteWithInvalidChar
1114      *
1115      * @see 0008644: error when sending mail with note (wrong charset)
1116      */
1117     public function testSaveMessageWithInvalidChar()
1118     {
1119         $subject = "\xF0\x9F\x98\x8A"; // :-) emoji
1120         $messageData = $this->_getMessageData('', $subject);
1121         $this->_foldersToClear[] = 'INBOX';
1122         $this->_json->saveMessage($messageData);
1123         $message = $this->_searchForMessageBySubject(Tinebase_Core::filterInputForDatabase($subject));
1124     }
1125     
1126     /**
1127      * testMessageWithInvalidICS
1128      *
1129      * @see 0008786: broken ics causes js error when showing details
1130      */
1131     public function testMessageWithInvalidICS()
1132     {
1133         $inbox = $this->_getFolder('INBOX');
1134         $mailAsString = file_get_contents(dirname(__FILE__) . '/../files/invalidimip.eml');
1135         Felamimail_Controller_Message::getInstance()->appendMessage($inbox, $mailAsString);
1136         
1137         $this->_foldersToClear = array('INBOX');
1138         $message = $this->_searchForMessageBySubject('test invalid imip');
1139         
1140         $fullMessage = $this->_json->getMessage($message['id']);
1141         $this->assertTrue(empty($fullMessage->preparedParts));
1142     }
1143     
1144     /*********************** sieve tests ****************************/
1145     
1146     /**
1147      * set and get vacation sieve script
1148      *
1149      * @see 0007768: Sieve - Vacation notify frequency not being set (Cyrus)
1150      */
1151     public function testGetSetVacation()
1152     {
1153         $vacationData = $this->_getVacationData();
1154         $this->_sieveTestHelper($vacationData);
1155         
1156         // check if script was activated
1157         $activeScriptName = Felamimail_Controller_Sieve::getInstance()->getActiveScriptName($this->_account->getId());
1158         $this->assertEquals($this->_testSieveScriptName, $activeScriptName);
1159         $updatedAccount = Felamimail_Controller_Account::getInstance()->get($this->_account->getId());
1160         $this->assertTrue((bool) $updatedAccount->sieve_vacation_active);
1161         
1162         $result = $this->_json->getVacation($this->_account->getId());
1163
1164         $this->assertEquals($this->_account->email, $result['addresses'][0]);
1165         
1166         $sieveBackend = Felamimail_Backend_SieveFactory::factory($this->_account->getId());
1167         if (preg_match('/dbmail/i', $sieveBackend->getImplementation())) {
1168             $translate = Tinebase_Translation::getTranslation('Felamimail');
1169             $vacationData['subject'] = sprintf($translate->_('Out of Office reply from %1$s'), Tinebase_Core::getUser()->accountFullName);
1170         }
1171         
1172         foreach (array('reason', 'enabled', 'subject', 'from', 'days') as $field) {
1173             $this->assertEquals($vacationData[$field], $result[$field], 'vacation data mismatch: ' . $field);
1174         }
1175     }
1176     
1177     /**
1178      * get vacation data
1179      *
1180      * @return array
1181      */
1182     protected function _getVacationData()
1183     {
1184         return array(
1185             'id'                    => $this->_account->getId(),
1186             'subject'               => 'unittest vacation subject',
1187             'from'                  => $this->_account->from . ' <' . $this->_account->email . '>',
1188             'days'                  => 3,
1189             'enabled'               => TRUE,
1190             'reason'                => 'unittest vacation message<br /><br />signature',
1191             'mime'                  => NULL,
1192         );
1193     }
1194     
1195     /**
1196      * test mime vacation sieve script
1197      */
1198     public function testMimeVacation()
1199     {
1200         $vacationData = $this->_getVacationData();
1201         $vacationData['reason'] = "\n<html><body><h1>unittest vacation&nbsp;message</h1></body></html>";
1202         
1203         $_sieveBackend = Felamimail_Backend_SieveFactory::factory($this->_account->getId());
1204         if (! in_array('mime', $_sieveBackend->capability())) {
1205             $vacationData['mime'] = 'text/html';
1206         }
1207         
1208         $this->_sieveTestHelper($vacationData, TRUE);
1209     }
1210     
1211     /**
1212      * test get/set of rules sieve script
1213      */
1214     public function testGetSetRules()
1215     {
1216         $ruleData = $this->_getRuleData();
1217         
1218         $this->_sieveTestHelper($ruleData);
1219         
1220         // check getRules
1221         $result = $this->_json->getRules($this->_account->getId());
1222         $this->assertEquals($result['totalcount'], count($ruleData));
1223         
1224         // check by sending mail
1225         $messageData = $this->_getMessageData('', 'viagra');
1226         $returned = $this->_json->saveMessage($messageData);
1227         $this->_foldersToClear = array('INBOX', $this->_testFolderName);
1228         // check if message is in test folder
1229         $message = $this->_searchForMessageBySubject($messageData['subject'], $this->_testFolderName);
1230     }
1231     
1232     /**
1233      * testRemoveRules
1234      *
1235      * @see 0006490: can not delete single filter rule
1236      */
1237     public function testRemoveRules()
1238     {
1239         $this->testGetSetRules();
1240         $this->_json->saveRules($this->_account->getId(), array());
1241         
1242         $result = $this->_json->getRules($this->_account->getId());
1243         $this->assertEquals(0, $result['totalcount'], 'found rules: ' . print_r($result, TRUE));
1244     }
1245     
1246     /**
1247      * get sieve rule data
1248      *
1249      * @return array
1250      */
1251     protected function _getRuleData()
1252     {
1253         return array(array(
1254             'id'            => 1,
1255             'action_type'   => Felamimail_Sieve_Rule_Action::FILEINTO,
1256             'action_argument' => $this->_testFolderName,
1257             'conjunction'  => 'allof',
1258             'conditions'    => array(array(
1259                 'test'          => Felamimail_Sieve_Rule_Condition::TEST_ADDRESS,
1260                 'comperator'    => Felamimail_Sieve_Rule_Condition::COMPERATOR_CONTAINS,
1261                 'header'        => 'From',
1262                 'key'           => '"abcd" <info@example.org>',
1263             )),
1264             'enabled'       => 1,
1265         ), array(
1266             'id'            => 2,
1267             'action_type'   => Felamimail_Sieve_Rule_Action::FILEINTO,
1268             'action_argument' => $this->_testFolderName,
1269             'conjunction'  => 'allof',
1270             'conditions'    => array(array(
1271                 'test'          => Felamimail_Sieve_Rule_Condition::TEST_ADDRESS,
1272                 'comperator'    => Felamimail_Sieve_Rule_Condition::COMPERATOR_CONTAINS,
1273                 'header'        => 'From',
1274                 'key'           => 'info@example.org',
1275             )),
1276             'enabled'       => 0,
1277         ), array(
1278             'id'            => 3,
1279             'action_type'   => Felamimail_Sieve_Rule_Action::FILEINTO,
1280             'action_argument' => $this->_testFolderName,
1281             'conjunction'  => 'allof',
1282             'conditions'    => array(array(
1283                 'test'          => Felamimail_Sieve_Rule_Condition::TEST_HEADER,
1284                 'comperator'    => Felamimail_Sieve_Rule_Condition::COMPERATOR_REGEX,
1285                 'header'        => 'subject',
1286                 'key'           => '[vV]iagra|cyalis',
1287             )),
1288             'enabled'       => 1,
1289         ));
1290     }
1291     
1292     /**
1293      * test to set a forward rule to this accounts email address
1294      * -> should throw exception to prevent mail cycling
1295      */
1296     public function testSetForwardRuleToSelf()
1297     {
1298         $ruleData = array(array(
1299             'id'            => 1,
1300             'action_type'   => Felamimail_Sieve_Rule_Action::REDIRECT,
1301             'action_argument' => $this->_account->email,
1302             'conjunction'     => 'allof',
1303             'conditions'    => array(array(
1304                 'test'          => Felamimail_Sieve_Rule_Condition::TEST_ADDRESS,
1305                 'comperator'    => Felamimail_Sieve_Rule_Condition::COMPERATOR_CONTAINS,
1306                 'header'        => 'From',
1307                 'key'           => 'info@example.org',
1308             )),
1309             'enabled'       => 1,
1310         ));
1311         
1312         try {
1313             $this->_sieveTestHelper($ruleData);
1314             $this->assertTrue(FALSE, 'it is not allowed to set own email address for redirect!');
1315         } catch (Felamimail_Exception_Sieve $e) {
1316             $this->assertTrue(TRUE);
1317         }
1318
1319         // this should work
1320         $ruleData[0]['enabled'] = 0;
1321         $this->_sieveTestHelper($ruleData);
1322     }
1323     
1324     /**
1325      * testGetVacationTemplates
1326      *
1327      * @return array
1328      */
1329     public function testGetVacationTemplates()
1330     {
1331         $this->markTestSkipped('0010194: fix felamimail webdav tests');
1332         
1333         $this->_addVacationTemplateFile();
1334         $result = $this->_json->getVacationMessageTemplates();
1335         
1336         $this->assertTrue($result['totalcount'] > 0, 'no templates found');
1337         $found = FALSE;
1338         foreach ($result['results'] as $template) {
1339             if ($template['name'] === $this->_sieveVacationTemplateFile) {
1340                 $found = TRUE;
1341                 break;
1342             }
1343         }
1344         
1345         $this->assertTrue($found, 'wrong templates: ' . print_r($result['results'], TRUE));
1346         
1347         return $template;
1348     }
1349     
1350     /**
1351      * add vacation template file to vfs
1352      */
1353     protected function _addVacationTemplateFile()
1354     {
1355         $webdavRoot = new DAV\ObjectTree(new Tinebase_WebDav_Root());
1356         $path = '/webdav/Felamimail/shared/Vacation Templates';
1357         $node = $webdavRoot->getNodeForPath($path);
1358         $this->_pathsToDelete[] = $path . '/' . $this->_sieveVacationTemplateFile;
1359         $node->createFile($this->_sieveVacationTemplateFile, fopen(dirname(__FILE__) . '/../files/' . $this->_sieveVacationTemplateFile, 'r'));
1360     }
1361     
1362     /**
1363      * testGetVacationMessage
1364      */
1365     public function testGetVacationMessage()
1366     {
1367         $this->markTestSkipped('0010194: fix felamimail webdav tests');
1368         
1369         $result = $this->_getVacationMessageWithTemplate();
1370         $sclever = Tinebase_User::getInstance()->getFullUserByLoginName('sclever');
1371         $pwulf = Tinebase_User::getInstance()->getFullUserByLoginName('pwulf');
1372         $this->assertEquals("Ich bin vom 18.04.2012 bis zum 20.04.2012 im Urlaub. Bitte kontaktieren Sie<br /> Paul Wulf (" .
1373             $pwulf->accountEmailAddress . ") oder Susan Clever (" .
1374             $sclever->accountEmailAddress . ").<br /><br />I am on vacation until Apr 20, 2012. Please contact Paul Wulf<br />(" .
1375             $pwulf->accountEmailAddress . ") or Susan Clever (" .
1376             $sclever->accountEmailAddress . ") instead.<br /><br />" .
1377             Addressbook_Controller_Contact::getInstance()->getContactByUserId(Tinebase_Core::getUser()->getId())->n_fn, $result['message']);
1378     }
1379     
1380     /**
1381      * get vacation message with template
1382      *
1383      * @return array
1384      */
1385     protected function _getVacationMessageWithTemplate()
1386     {
1387         $template = $this->testGetVacationTemplates();
1388         $sclever = Tinebase_User::getInstance()->getFullUserByLoginName('sclever');
1389         $result = $this->_json->getVacationMessage(array(
1390             'start_date' => '2012-04-18',
1391             'end_date'   => '2012-04-20',
1392             'contact_ids' => array(
1393                 Tinebase_User::getInstance()->getFullUserByLoginName('pwulf')->contact_id,
1394                 $sclever->contact_id,
1395             ),
1396             'template_id' => $template['id'],
1397             'signature' => $this->_account->signature
1398         ));
1399         
1400         return $result;
1401     }
1402     
1403     /**
1404      * testGetVacationWithSignature
1405      *
1406      * @see 0006866: check signature linebreaks in vacation message from template
1407      */
1408     public function testGetVacationWithSignature()
1409     {
1410         $this->markTestSkipped('0010194: fix felamimail webdav tests');
1411         
1412         $this->_sieveVacationTemplateFile = 'vacation_template_sig.tpl';
1413         
1414         // set signature with <br> + linebreaks
1415         $this->_account->signature = "llalala<br>\nxyz<br>\nblubb<br>";
1416         
1417         $result = $this->_getVacationMessageWithTemplate();
1418         $this->assertContains('-- <br />llalala<br />xyz<br />blubb<br />', $result['message'], 'wrong linebreaks or missing signature');
1419     }
1420     
1421     /**
1422     * testSetVacationWithStartAndEndDate
1423     *
1424     * @see 0006266: automatic deactivation of vacation message
1425     */
1426     public function testSetVacationWithStartAndEndDate()
1427     {
1428         $vacationData = $this->_getVacationData();
1429         $vacationData['start_date'] = '2012-04-18';
1430         $vacationData['end_date'] = '2012-04-20';
1431         $result = $this->_sieveTestHelper($vacationData);
1432         
1433         $this->assertContains($vacationData['start_date'], $result['start_date']);
1434         $this->assertContains($vacationData['end_date'], $result['end_date']);
1435     }
1436     
1437     /**
1438      * testSieveRulesOrder
1439      *
1440      * @see 0007240: order of sieve rules changes when vacation message is saved
1441      */
1442     public function testSieveRulesOrder()
1443     {
1444         $this->_setTestScriptname();
1445         
1446         // disable vacation first
1447         $this->_setDisabledVacation();
1448         
1449         $sieveBackend = Felamimail_Backend_SieveFactory::factory($this->_account->getId());
1450         
1451         $ruleData = $this->_getRuleData();
1452         $ruleData[0]['id'] = $ruleData[2]['id'];
1453         $ruleData[2]['id'] = 11;
1454         $resultSet = $this->_json->saveRules($this->_account->getId(), $ruleData);
1455         $sieveScriptRules = $sieveBackend->getScript($this->_testSieveScriptName);
1456         
1457         $this->_setDisabledVacation();
1458         $sieveScriptVacation = $sieveBackend->getScript($this->_testSieveScriptName);
1459         
1460         // compare sieve scripts
1461         $this->assertContains($sieveScriptRules, $sieveScriptVacation, 'rule order changed');
1462     }
1463     
1464     /**
1465      * use another name for test sieve script
1466      */
1467     protected function _setTestScriptname()
1468     {
1469         $this->_oldActiveSieveScriptName = Felamimail_Controller_Sieve::getInstance()->getActiveScriptName($this->_account->getId());
1470         $this->_testSieveScriptName = 'Felamimail_Unittest';
1471         Felamimail_Controller_Sieve::getInstance()->setScriptName($this->_testSieveScriptName);
1472     }
1473     
1474     /**
1475      * set disabled vacation message
1476      */
1477     protected function _setDisabledVacation()
1478     {
1479         $vacationData = $this->_getVacationData();
1480         $vacationData['enabled'] = FALSE;
1481         $resultSet = $this->_json->saveVacation($vacationData);
1482     }
1483     
1484     /**
1485      * get folder filter
1486      *
1487      * @return array
1488      */
1489     protected function _getFolderFilter()
1490     {
1491         return array(array(
1492             'field' => 'globalname', 'operator' => 'equals', 'value' => ''
1493         ));
1494     }
1495
1496     /**
1497      * get message filter
1498      *
1499      * @param string $_folderId
1500      * @return array
1501      */
1502     protected function _getMessageFilter($_folderId)
1503     {
1504         $result = array(array(
1505             'field' => 'folder_id', 'operator' => 'equals', 'value' => $_folderId
1506         ));
1507         
1508         return $result;
1509     }
1510     
1511     /**
1512      * get mailbox
1513      *
1514      * @param string $name
1515      * @param boolean $createFolder
1516      * @return Felamimail_Model_Folder|NULL
1517      */
1518     protected function _getFolder($name, $createFolder = TRUE)
1519     {
1520         Felamimail_Controller_Cache_Folder::getInstance()->update($this->_account->getId());
1521         try {
1522             $folder = Felamimail_Controller_Folder::getInstance()->getByBackendAndGlobalName($this->_account->getId(), $name);
1523         } catch (Tinebase_Exception_NotFound $tenf) {
1524             $folder = ($createFolder) ? Felamimail_Controller_Folder::getInstance()->create($this->_account, $name) : NULL;
1525         }
1526         
1527         return $folder;
1528     }
1529
1530     /**
1531      * get message data
1532      *
1533      * @return array
1534      */
1535     protected function _getMessageData($_emailFrom = '', $_subject = 'test')
1536     {
1537         return array(
1538             'account_id'    => $this->_account->getId(),
1539             'subject'       => $_subject,
1540             'to'            => array(Tinebase_Core::getUser()->accountEmailAddress),
1541             'body'          => 'aaaaaä <br>',
1542             'headers'       => array('X-Tine20TestMessage' => 'jsontest'),
1543             'from_email'    => $_emailFrom,
1544             'content_type'  => Felamimail_Model_Message::CONTENT_TYPE_HTML,
1545         );
1546     }
1547
1548     /**
1549      * send message and return message array
1550      *
1551      * @param string $folderName
1552      * @param array $addtionalHeaders
1553      * @return array
1554      */
1555     protected function _sendMessage($folderName = 'INBOX', $addtionalHeaders = array())
1556     {
1557         $messageToSend = $this->_getMessageData();
1558         $messageToSend['headers'] = array_merge($messageToSend['headers'], $addtionalHeaders);
1559         $returned = $this->_json->saveMessage($messageToSend);
1560         $this->_foldersToClear = array('INBOX', $this->_account->sent_folder);
1561
1562         // sleep for 2 secs because mailserver may be slower than expected
1563         sleep(2);
1564         
1565         $result = $this->_getMessages($folderName);
1566         $message = $this->_getMessageFromSearchResult($result, $messageToSend['subject']);
1567         
1568         $this->assertTrue(! empty($message), 'Sent message not found.');
1569         
1570         return $message;
1571     }
1572     
1573     /**
1574      * returns message array from result
1575      *
1576      * @param array $_result
1577      * @param string $_subject
1578      * @return array
1579      */
1580     protected function _getMessageFromSearchResult($_result, $_subject)
1581     {
1582         $message = array();
1583         foreach ($_result['results'] as $mail) {
1584             if ($mail['subject'] == $_subject) {
1585                 $message = $mail;
1586             }
1587         }
1588         
1589         return $message;
1590     }
1591     
1592     /**
1593      * get messages from folder
1594      *
1595      * @param string $_folderName
1596      * @return array
1597      */
1598     protected function _getMessages($_folderName = 'INBOX')
1599     {
1600         $folder = $this->_getFolder($_folderName);
1601         $filter = $this->_getMessageFilter($folder->getId());
1602         // update cache
1603         $folder = Felamimail_Controller_Cache_Message::getInstance()->updateCache($folder, 10, 1);
1604         $i = 0;
1605         while ($folder->cache_status != Felamimail_Model_Folder::CACHE_STATUS_COMPLETE && $i < 10) {
1606             $folder = Felamimail_Controller_Cache_Message::getInstance()->updateCache($folder, 10);
1607             $i++;
1608         }
1609         $result = $this->_json->searchMessages($filter, '');
1610
1611         return $result;
1612     }
1613     
1614     /**
1615      * search for message defined by subject in folder
1616      *
1617      * @param string $_subject
1618      * @param string $_folderName
1619      * @return string message data
1620      */
1621     protected function _searchForMessageBySubject($_subject, $_folderName = 'INBOX')
1622     {
1623         // give server some time to send and receive messages
1624         sleep(1);
1625
1626         $result = $this->_getMessages($_folderName);
1627         
1628         $message = array();
1629         foreach ($result['results'] as $mail) {
1630             if ($mail['subject'] == $_subject) {
1631                 $message = $mail;
1632             }
1633         }
1634         $this->assertGreaterThan(0, $result['totalcount'], 'folder is empty');
1635         $this->assertTrue(! empty($message), 'Message not found');
1636         
1637         return $message;
1638     }
1639     
1640     /**
1641      * sieve test helper
1642      *
1643      * @param array $_sieveData
1644      * @return array
1645      */
1646     protected function _sieveTestHelper($_sieveData, $_isMime = FALSE)
1647     {
1648         $this->_setTestScriptname();
1649         
1650         // check which save fn to use
1651         if ((isset($_sieveData['reason']) || array_key_exists('reason', $_sieveData))) {
1652             $resultSet = $this->_json->saveVacation($_sieveData);
1653             $this->assertEquals($this->_account->email, $resultSet['addresses'][0]);
1654             
1655             $_sieveBackend = Felamimail_Backend_SieveFactory::factory($this->_account->getId());
1656             
1657             if (preg_match('/dbmail/i', $_sieveBackend->getImplementation())) {
1658                 $translate = Tinebase_Translation::getTranslation('Felamimail');
1659                 $this->assertEquals(sprintf(
1660                     $translate->_('Out of Office reply from %1$s'), Tinebase_Core::getUser()->accountFullName),
1661                     $resultSet['subject']
1662                 );
1663             } else {
1664                 $this->assertEquals($_sieveData['subject'], $resultSet['subject']);
1665             }
1666             
1667             if ($_isMime) {
1668                 $this->assertEquals(html_entity_decode('unittest vacation&nbsp;message', ENT_NOQUOTES, 'UTF-8'), $resultSet['reason']);
1669             } else {
1670                 $this->assertEquals($_sieveData['reason'], $resultSet['reason']);
1671             }
1672             
1673         } else if ((isset($_sieveData[0]['action_type']) || array_key_exists('action_type', $_sieveData[0]))) {
1674             $resultSet = $this->_json->saveRules($this->_account->getId(), $_sieveData);
1675             $this->assertEquals($_sieveData, $resultSet);
1676         }
1677         
1678         return $resultSet;
1679     }
1680
1681     /**
1682      * search preferences by application felamimail
1683      *
1684      */
1685     public function testSearchFelamimailPreferences()
1686     {
1687         // search prefs
1688         $result = $this->_frontend->searchPreferencesForApplication('Felamimail', '');
1689         
1690         // check results
1691         $this->assertTrue(isset($result['results']));
1692         $this->assertGreaterThan(0, $result['totalcount']);
1693     }
1694     
1695     /**
1696      * testGetRegistryData
1697      *
1698      * @see 0010251: do not send unused config data to client
1699      */
1700     public function testGetRegistryData()
1701     {
1702         $regData = $this->_json->getRegistryData();
1703
1704         $this->assertFalse(isset($regData['defaults']));
1705         $supportedFlags = Felamimail_Config::getInstance()->featureEnabled(Felamimail_Config::FEATURE_TINE20_FLAG)
1706             ? 6
1707             : 5;
1708         $this->assertEquals($supportedFlags, $regData['supportedFlags']['totalcount']);
1709     }
1710 }