| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * @author Christoph Wurst <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * ownCloud - Mail | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * This code is free software: you can redistribute it and/or modify | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * it under the terms of the GNU Affero General Public License, version 3, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * as published by the Free Software Foundation. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * This program is distributed in the hope that it will be useful, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * GNU Affero General Public License for more details. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * You should have received a copy of the GNU Affero General Public License, version 3, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * along with this program.  If not, see <http://www.gnu.org/licenses/> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use OCA\Mail\Controller\AccountsController; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | use OCA\Mail\Model\Message; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | use OC\AppFramework\Http; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | use OCP\AppFramework\Http\JSONResponse; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | class AccountsControllerTest extends \Test\TestCase { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | 	private $appName; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | 	private $request; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | 	private $accountService; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | 	private $userId; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | 	private $userFolder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | 	private $autoConfig; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | 	private $logger; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | 	private $l10n; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | 	private $crypto; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | 	private $controller; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | 	private $accountId; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | 	private $account; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | 	private $unifiedAccount; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  | 	protected function setUp() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  | 		parent::setUp(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  | 		$this->appName = 'mail'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | 		$this->request = $this->getMockBuilder('\OCP\IRequest') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  | 			->disableOriginalConstructor() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | 			->getMock(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | 		$this->accountService = $this->getMockBuilder('\OCA\Mail\Service\AccountService') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  | 			->disableOriginalConstructor() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | 			->getMock(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  | 		$this->userId = 'manfred'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | 		$this->userFolder = $this->getMockBuilder('\OCP\Files\Folder') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  | 			->disableOriginalConstructor() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | 			->getMock(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  | 		$this->autoConfig = $this->getMockBuilder('\OCA\Mail\Service\AutoConfig\AutoConfig') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | 			->disableOriginalConstructor() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | 			->getMock(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | 		$this->logger = $this->getMockBuilder('\OCA\Mail\Service\Logger') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  | 			->disableOriginalConstructor() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  | 			->getMock(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | 		$this->l10n = $this->getMockBuilder('\OCP\IL10N') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  | 			->disableOriginalConstructor() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | 			->getMock(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | 		$this->crypto = $this->getMockBuilder('\OCP\Security\ICrypto') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | 			->disableOriginalConstructor() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  | 			->getMock(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  | 		$this->collector = $this->getMockBuilder('\OCA\Mail\Service\AutoCompletion\AddressCollector') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  | 			->disableOriginalConstructor() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  | 			->getMock(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  | 		$this->controller = new AccountsController($this->appName, $this->request, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  | 			$this->accountService, $this->userId, $this->userFolder, $this->autoConfig, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  | 			$this->logger, $this->l10n, $this->crypto, $this->collector); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  | 		$this->account = $this->getMockBuilder('\OCA\Mail\Account') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  | 			->disableOriginalConstructor() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  | 			->getMock(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  | 		$this->unifiedAccount = $this->getMockBuilder('\OCA\Mail\Service\UnifiedAccount') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  | 			->disableOriginalConstructor() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  | 			->getMock(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  | 		$this->accountId = 123; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  | 	public function testIndex() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  | 		$this->account->expects($this->once()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  | 			->method('getConfiguration') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  | 			->will($this->returnValue('conf')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  | 		$this->accountService->expects($this->once()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  | 			->method('findByUserId') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  | 			->with($this->equalTo($this->userId)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  | 			->will($this->returnValue([$this->account])); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  | 		$response = $this->controller->index(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  | 		$expectedResponse = new JSONResponse(['conf']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  | 		$this->assertEquals($expectedResponse, $response); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 99 |  | View Code Duplication | 	public function testShow() { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  | 		$this->accountService->expects($this->once()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  | 			->method('find') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  | 			->with($this->equalTo($this->userId), $this->equalTo($this->accountId)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  | 			->will($this->returnValue($this->account)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  | 		$this->account->expects($this->once()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  | 			->method('getConfiguration') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  | 			->will($this->returnValue('conf')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  | 		$response = $this->controller->show($this->accountId); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  | 		$expectedResponse = new JSONResponse('conf'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  | 		$this->assertEquals($expectedResponse, $response); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 114 |  | View Code Duplication | 	public function testShowDoesNotExist() { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  | 		$this->accountService->expects($this->once()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  | 			->method('find') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  | 			->with($this->equalTo($this->userId), $this->equalTo($this->accountId)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  | 			->will($this->returnValue($this->account)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  | 		$this->account->expects($this->once()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  | 			->method('getConfiguration') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  | 			->will($this->throwException(new OCP\AppFramework\Db\DoesNotExistException('test123'))); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  | 		$response = $this->controller->show($this->accountId); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  | 		$expectedResponse = new JSONResponse([]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  | 		$expectedResponse->setStatus(404); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  | 		$this->assertEquals($expectedResponse, $response); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 130 |  | View Code Duplication | 	public function testDestroy() { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  | 		$this->accountService->expects($this->once()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  | 			->method('delete') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  | 			->with($this->equalTo($this->userId), $this->equalTo($this->accountId)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  | 		$response = $this->controller->destroy($this->accountId); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  | 		$expectedResponse = new JSONResponse(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  | 		$this->assertEquals($expectedResponse, $response); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 141 |  | View Code Duplication | 	public function testDestroyDoesNotExist() { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  | 		$this->accountService->expects($this->once()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  | 			->method('delete') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  | 			->with($this->equalTo($this->userId), $this->equalTo($this->accountId)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  | 			->will($this->throwException(new \OCP\AppFramework\Db\DoesNotExistException('test'))); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  | 		$response = $this->controller->destroy($this->accountId); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  | 		$expectedResponse = new JSONResponse(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  | 		$this->assertEquals($expectedResponse, $response); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  | 	public function testCreateAutoDetectSuccess() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  | 		$email = '[email protected]'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  | 		$password = '123456'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  | 		$accountName = 'John Doe'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  | 		$this->account->expects($this->exactly(2)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  | 			->method('getId') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  | 			->will($this->returnValue(135)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  | 		$this->autoConfig->expects($this->once()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  | 			->method('createAutoDetected') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  | 			->with($this->equalTo($email), $this->equalTo($password), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  | 				$this->equalTo($accountName)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  | 			->will($this->returnValue($this->account)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  | 		$this->accountService->expects($this->once()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  | 			->method('save') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  | 			->with($this->equalTo($this->account)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  | 		$response = $this->controller->create($accountName, $email, $password, null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  | 			null, null, null, null, null, null, null, null, null, true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  | 		$expectedResponse = new JSONResponse([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  | 			'data' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  | 				'id' => 135, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  | 			], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  | 			], Http::STATUS_CREATED); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  | 		$this->assertEquals($expectedResponse, $response); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  | 	public function testCreateAutoDetectFailure() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  | 		$email = '[email protected]'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  | 		$password = '123456'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  | 		$accountName = 'John Doe'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  | 		$this->autoConfig->expects($this->once()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  | 			->method('createAutoDetected') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  | 			->with($this->equalTo($email), $this->equalTo($password), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  | 				$this->equalTo($accountName)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  | 			->will($this->returnValue(null)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  | 		$this->l10n->expects($this->once()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  | 			->method('t') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  | 			->will($this->returnValue('fail')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 |  |  | 		$response = $this->controller->create($accountName, $email, $password, null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 |  |  | 			null, null, null, null, null, null, null, null, null, true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 |  |  | 		$expectedResponse = new JSONResponse([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 199 |  |  | 			'message' => 'fail', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 200 |  |  | 			], Http::STATUS_BAD_REQUEST); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 201 |  |  | 		$this->assertEquals($expectedResponse, $response); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 202 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 203 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 204 |  | View Code Duplication | 	public function newMessageDataProvider() { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 205 |  |  | 		return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 206 |  |  | 			[false, false], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 207 |  |  | 			[true, false], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 208 |  |  | 			[false, true], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 209 |  |  | 			[true, true], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 210 |  |  | 		]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 211 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 212 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 213 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 214 |  |  | 	 * @dataProvider newMessageDataProvider | 
            
                                                                                                            
                            
            
                                    
            
            
                | 215 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 216 |  |  | 	public function testSend($isUnifiedInbox, $isReply) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 217 |  |  | 		$account = $isUnifiedInbox ? $this->unifiedAccount : $this->account; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 218 |  |  | 		$folderId = base64_encode('My folder'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 219 |  |  | 		$subject = 'Hello'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 220 |  |  | 		$body = 'Hi!'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 221 |  |  | 		$from = '[email protected]'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 222 |  |  | 		$to = '[email protected]'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 223 |  |  | 		$cc = '"user2" <[email protected]>, [email protected]'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 224 |  |  | 		$bcc = '[email protected]'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 225 |  |  | 		$draftUID = 45; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 226 |  |  | 		$messageId = $isReply ? 123 : null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 227 |  |  | 		$attachmentName = 'kitten.png'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 228 |  |  | 		$attachments = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 229 |  |  | 			[ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 230 |  |  | 				'fileName' => $attachmentName | 
            
                                                                                                            
                            
            
                                    
            
            
                | 231 |  |  | 			], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 232 |  |  | 		]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 233 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 234 |  |  | 		$this->accountService->expects($this->once()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 235 |  |  | 			->method('find') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 236 |  |  | 			->with($this->userId, $this->accountId) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 237 |  |  | 			->will($this->returnValue($account)); | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 238 |  | View Code Duplication | 		if ($isUnifiedInbox) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 239 |  |  | 			$this->unifiedAccount->expects($this->once()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 240 |  |  | 				->method('resolve') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 241 |  |  | 				->with($messageId) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 242 |  |  | 				->will($this->returnValue([$this->account, $folderId, $messageId])); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 243 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 244 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 245 |  |  | 		if ($isReply) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 246 |  |  | 			$message = $this->getMockBuilder('OCA\Mail\Model\ReplyMessage') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 247 |  |  | 				->disableOriginalConstructor() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 248 |  |  | 				->getMock(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 249 |  |  | 			$this->account->expects($this->once()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 250 |  |  | 				->method('newReplyMessage') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 251 |  |  | 				->will($this->returnValue($message)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 252 |  |  | 			$mailbox = $this->getMockBuilder('OCA\Mail\Service\IMailBox') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 253 |  |  | 				->disableOriginalConstructor() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 254 |  |  | 				->getMock(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 255 |  |  | 			$this->account->expects($this->once()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 256 |  |  | 				->method('getMailbox') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 257 |  |  | 				->with(base64_decode($folderId)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 258 |  |  | 				->will($this->returnValue($mailbox)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 259 |  |  | 			$reply = new Message(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 260 |  |  | 			$mailbox->expects($this->once()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 261 |  |  | 				->method('getMessage') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 262 |  |  | 				->with($messageId) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 263 |  |  | 				->will($this->returnValue($reply)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 264 |  |  | 			$message->expects($this->once()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 265 |  |  | 				->method('setRepliedMessage') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 266 |  |  | 				->with($reply); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 267 |  |  | 		} else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 268 |  |  | 			$message = $this->getMockBuilder('OCA\Mail\Model\Message') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 269 |  |  | 				->disableOriginalConstructor() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 270 |  |  | 				->getMock(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 271 |  |  | 			$this->account->expects($this->once()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 272 |  |  | 				->method('newMessage') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 273 |  |  | 				->will($this->returnValue($message)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 274 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 275 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 276 |  |  | 		$message->expects($this->once()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 277 |  |  | 			->method('setTo') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 278 |  |  | 			->with(Message::parseAddressList($to)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 279 |  |  | 		$message->expects($this->once()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 280 |  |  | 			->method('setSubject') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 281 |  |  | 			->with($subject); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 282 |  |  | 		$message->expects($this->once()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 283 |  |  | 			->method('setFrom') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 284 |  |  | 			->with($from); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 285 |  |  | 		$message->expects($this->once()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 286 |  |  | 			->method('setCC') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 287 |  |  | 			->with(Message::parseAddressList($cc)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 288 |  |  | 		$message->expects($this->once()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 289 |  |  | 			->method('setBcc') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 290 |  |  | 			->with(Message::parseAddressList($bcc)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 291 |  |  | 		$message->expects($this->once()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 292 |  |  | 			->method('setContent') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 293 |  |  | 			->with($body); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 294 |  |  | 		$this->account->expects($this->once()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 295 |  |  | 			->method('getEMailAddress') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 296 |  |  | 			->will($this->returnValue($from)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 297 |  |  | 		$this->account->expects($this->once()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 298 |  |  | 			->method('sendMessage') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 299 |  |  | 			->with($message, $draftUID); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 300 |  |  | 		$this->userFolder->expects($this->at(0)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 301 |  |  | 			->method('nodeExists') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 302 |  |  | 			->with($attachmentName) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 303 |  |  | 			->will($this->returnValue(true)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 304 |  |  | 		$file = $this->getMockBuilder('OCP\Files\File') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 305 |  |  | 			->disableOriginalConstructor() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 306 |  |  | 			->getMock(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 307 |  |  | 		$this->userFolder->expects($this->once()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 308 |  |  | 			->method('get') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 309 |  |  | 			->with($attachmentName) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 310 |  |  | 			->will($this->returnValue($file)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 311 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 312 |  |  | 		$expected = new JSONResponse(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 313 |  |  | 		$actual = $this->controller->send($this->accountId, $folderId, $subject, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 314 |  |  | 			$body, $to, $cc, $bcc, $draftUID, $messageId, $attachments); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 315 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 316 |  |  | 		$this->assertEquals($expected, $actual); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 317 |  |  | 	} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 318 |  |  |  | 
            
                                                                        
                            
            
                                                                    
                                                                                                        
            
            
                | 319 |  | View Code Duplication | 	public function draftDataProvider() { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 320 |  |  | 		return [ | 
            
                                                                        
                            
            
                                    
            
            
                | 321 |  |  | 			[false, false], | 
            
                                                                        
                            
            
                                    
            
            
                | 322 |  |  | 			[true, true], | 
            
                                                                        
                            
            
                                    
            
            
                | 323 |  |  | 			[true, false], | 
            
                                                                        
                            
            
                                    
            
            
                | 324 |  |  | 			[true, true], | 
            
                                                                        
                            
            
                                    
            
            
                | 325 |  |  | 		]; | 
            
                                                                        
                            
            
                                    
            
            
                | 326 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 327 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 328 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 329 |  |  | 	 * @dataProvider newMessageDataProvider | 
            
                                                                                                            
                            
            
                                    
            
            
                | 330 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 331 |  |  | 	public function testDraft($isUnifiedInbox, $withPreviousDraft) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 332 |  |  | 		$account = $isUnifiedInbox ? $this->unifiedAccount : $this->account; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 333 |  |  | 		$subject = 'Hello'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 334 |  |  | 		$body = 'Hi!'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 335 |  |  | 		$from = '[email protected]'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 336 |  |  | 		$to = '[email protected]'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 337 |  |  | 		$cc = '"user2" <[email protected]>, [email protected]'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 338 |  |  | 		$bcc = '[email protected]'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 339 |  |  | 		$messageId = 123; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 340 |  |  | 		$uid = $withPreviousDraft ? 123 : null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 341 |  |  | 		$newUID = 124; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 342 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 343 |  |  | 		$this->accountService->expects($this->once()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 344 |  |  | 			->method('find') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 345 |  |  | 			->with($this->userId, $this->accountId) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 346 |  |  | 			->will($this->returnValue($account)); | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 347 |  | View Code Duplication | 		if ($isUnifiedInbox) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 348 |  |  | 			$this->unifiedAccount->expects($this->once()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 349 |  |  | 				->method('resolve') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 350 |  |  | 				->with($messageId) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 351 |  |  | 				->will($this->returnValue([$this->account])); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 352 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 353 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 354 |  |  | 		$message = $this->getMockBuilder('OCA\Mail\Model\Message') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 355 |  |  | 			->disableOriginalConstructor() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 356 |  |  | 			->getMock(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 357 |  |  | 		$this->account->expects($this->once()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 358 |  |  | 			->method('newMessage') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 359 |  |  | 			->will($this->returnValue($message)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 360 |  |  | 		$message->expects($this->once()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 361 |  |  | 			->method('setTo') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 362 |  |  | 			->with(Message::parseAddressList($to)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 363 |  |  | 		$message->expects($this->once()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 364 |  |  | 			->method('setSubject') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 365 |  |  | 			->with($subject); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 366 |  |  | 		$message->expects($this->once()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 367 |  |  | 			->method('setFrom') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 368 |  |  | 			->with($from); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 369 |  |  | 		$message->expects($this->once()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 370 |  |  | 			->method('setCC') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 371 |  |  | 			->with(Message::parseAddressList($cc)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 372 |  |  | 		$message->expects($this->once()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 373 |  |  | 			->method('setBcc') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 374 |  |  | 			->with(Message::parseAddressList($bcc)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 375 |  |  | 		$message->expects($this->once()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 376 |  |  | 			->method('setContent') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 377 |  |  | 			->with($body); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 378 |  |  | 		$this->account->expects($this->once()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 379 |  |  | 			->method('getEMailAddress') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 380 |  |  | 			->will($this->returnValue($from)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 381 |  |  | 		$this->account->expects($this->once()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 382 |  |  | 			->method('saveDraft') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 383 |  |  | 			->with($message, $uid) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 384 |  |  | 			->will($this->returnValue($newUID)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 385 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 386 |  |  | 		$expected = new JSONResponse([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 387 |  |  | 			'uid' => $newUID, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 388 |  |  | 		]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 389 |  |  | 		$actual = $this->controller->draft($this->accountId, $subject, $body, $to, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 390 |  |  | 			$cc, $bcc, $uid, $messageId); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 391 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 392 |  |  | 		$this->assertEquals($expected, $actual); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 393 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 394 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 395 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 396 |  |  |  | 
            
                        
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.