| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * @author Christoph Wurst <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * @author Robin McCorkell <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * @author Scrutinizer Auto-Fixer <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * @author Thomas Müller <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * ownCloud - Mail | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * This code is free software: you can redistribute it and/or modify | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * it under the terms of the GNU Affero General Public License, version 3, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * as published by the Free Software Foundation. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * This program is distributed in the hope that it will be useful, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * GNU Affero General Public License for more details. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * You should have received a copy of the GNU Affero General Public License, version 3, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  * along with this program.  If not, see <http://www.gnu.org/licenses/> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | namespace OCA\Mail\Tests\Imap; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | use OCA\Mail\Account; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | use OCA\Mail\Db\MailAccount; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | use OCA\Mail\Mailbox; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | abstract class AbstractTest extends \PHPUnit_Framework_TestCase { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | 	 * @var Account | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | 	private static $account; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | 	 * @var string[] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | 	private static $createdMailboxes = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  | 	public static function setUpBeforeClass() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  | 		$user = '[email protected]'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  | 		$password = 'mypassword'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  | 		$password = \OC::$server->getCrypto()->encrypt($password); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | 		$a = new MailAccount(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  | 		$a->setId(-1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | 		$a->setName('ownCloudMail'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | 		$a->setInboundHost('localhost'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  | 		$a->setInboundPort(993); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | 		$a->setInboundUser($user); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  | 		$a->setInboundPassword($password); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | 		$a->setInboundSslMode('ssl'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  | 		$a->setEmail($user); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | 		$a->setOutboundHost('localhost'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  | 		$a->setOutboundPort(465); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | 		$a->setOutboundUser($user); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | 		$a->setOutboundPassword($password); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | 		$a->setOutboundSslMode('none'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  | 		self::$account = new Account($a); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | 		self::$account->getImapConnection(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | 	public static function tearDownAfterClass() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | 		foreach(self::$createdMailboxes as $createdMailbox) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  | 			try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  | 				self::deleteMailbox($createdMailbox); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  | 			} catch(\Exception $ex) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  | 	 * @param string $name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  | 	 * @return \OCA\Mail\Mailbox | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  | 	public function createMailBox($name) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  | 		try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  | 			$this->getTestAccount()->getMailbox($name); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  | 			$this->deleteMailbox($name); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  | 		} catch (\Exception $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  | 			// Ignore mailbox not found | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  | 		$mailbox = $this->getTestAccount()->createMailbox($name); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  | 		self::$createdMailboxes[$name]= $mailbox; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  | 		return $mailbox; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  | 	 * @return Account | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  | 	protected function getTestAccount() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  | 		return self::$account; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  | 	 * @param string $mailbox | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  | 	private static function deleteMailbox($mailbox) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  | 		self::$account->deleteMailbox($mailbox); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  | 	 * @param string $name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  | 	public function existsMailBox($name) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  | 		$mb = $this->getTestAccount()->getMailbox($name); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  | 		try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  | 			$mb->getStatus(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  | 			return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  | 		} catch (\Exception $ex) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  | 			return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  | 	 * @param string $name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  | 	protected function assertMailBoxExists($name) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  | 		$this->assertTrue($this->existsMailBox($name)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  | 	 * @param string $name | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 128 |  |  | 	 */ | 
            
                                                                        
                            
            
                                    
            
            
                | 129 |  |  | 	protected function assertMailBoxNotExists($name) { | 
            
                                                                        
                            
            
                                    
            
            
                | 130 |  |  | 		$this->assertFalse($this->existsMailBox($name)); | 
            
                                                                        
                            
            
                                    
            
            
                | 131 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  | 	protected function createTestMessage( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  | 		Mailbox $mailbox, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  | 		$subject = 'Don\'t panic!', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  | 		$contents = 'Don\'t forget your towel', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  | 		$from = '[email protected]', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  | 		$to = '[email protected]' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  | 	) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  | 		$message = "From: $from | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  | Subject: $subject | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  | To: $to | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  | Message-ID: <20150415133206.Horde.M8uzSs0lxFX6uUE2sc6_rw5@localhost> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  | User-Agent: Horde Application Framework 5 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  | Date: Wed, 15 Apr 2015 13:32:06 +0000 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  | Content-Type: text/plain; charset=UTF-8; format=flowed; DelSp=Yes | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  | MIME-Version: 1.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  | $contents"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  | 		$mailbox->saveMessage($message); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 154 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 155 |  |  |  |