| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Craft; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use \PHPUnit_Framework_MockObject_MockObject as mock; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * @author    Nerds & Company | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * @copyright Copyright (c) 2015, Nerds & Company | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * @license   MIT | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * @link      http://www.nerds.company | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * @coversDefaultClass Craft\ApiAuthController | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * @covers ::<!public> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | class ApiAuthControllerTest extends BaseTest | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     public static function setUpBeforeClass() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |         parent::setUpBeforeClass(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         //Plugin classes | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |         require_once __DIR__ . '/../../controllers/ApiAuthController.php'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         require_once __DIR__ . '/../../services/ApiAuthService.php'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     //============================================================================================================== | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     //=================================================  TESTS  ==================================================== | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     //============================================================================================================== | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      * @covers ::actionAuthenticate | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     public function testApiAuthControllerAuthenticateShouldReturnErrorWhenNotPostRequest() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         $this->setSimpleMockApiAuthService(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         $this->setMockRequestService('GET'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         $apiAuthController = $this->getMockApiAuthController('returnErrorJson', ''); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         $apiAuthController->actionAuthenticate(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      * @covers ::actionAuthenticate | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     public function testApiAuthControllerAuthenticateShouldReturnBadCredentialsWhenLoginFails() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         $username = 'username'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         $password = 'test123'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         $errorMessage = Craft::t('Invalid username or password'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         $this->setSimpleMockApiAuthService(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         $this->setMockRequestService('POST', $username, $password); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         $this->setMockUserSessionService($username, $password, false); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         $apiAuthController = $this->getMockApiAuthController('returnErrorJson', $errorMessage); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         $apiAuthController->actionAuthenticate(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      * @covers ::actionAuthenticate | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 68 |  | View Code Duplication |     public function testApiAuthControllerAuthenticateShouldReturnInternalServerErrorWhenKeyCannotBeSaved() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         $username = 'username'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         $password = 'test123'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         $key = 'averynicekey'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         $errorMessage = Craft::t('Something went wrong'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         $mockUser = $this->getMockUser(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         $this->setMockRequestService('POST', $username, $password); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         $this->setMockUserSessionService($username, $password, true, $mockUser); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         $this->setMockApiAuthService($key, $mockUser, false); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         $apiAuthController = $this->getMockApiAuthController('returnErrorJson', $errorMessage); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         $apiAuthController->actionAuthenticate(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |      * @covers ::actionAuthenticate | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 87 |  |  |      */ | 
            
                                                                        
                            
            
                                                                    
                                                                                                        
            
            
                | 88 |  | View Code Duplication |     public function testApiAuthControllerAuthenticateShouldReturnKey() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |         $username = 'username'; | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  |         $password = 'test123'; | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  |         $key = 'averynicekey'; | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |         $mockUser = $this->getMockUser(); | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  |         $this->setMockRequestService('POST', $username, $password); | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  |         $this->setMockUserSessionService($username, $password, true, $mockUser); | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  |         $this->setMockApiAuthService($key, $mockUser, true); | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |         $apiAuthController = $this->getMockApiAuthController('returnJson', array('key' => $key)); | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  |         $apiAuthController->actionAuthenticate(); | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |      * @covers ::actionResetPassword | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     public function testApiAuthControllerResetPasswordShouldReturnErrorWhenNotPostRequest() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         $this->setSimpleMockApiAuthService(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         $this->setMockRequestService('GET'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         $apiAuthController = $this->getMockApiAuthController('returnErrorJson', ''); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         $apiAuthController->actionResetPassword(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |      * @covers ::actionResetPassword | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |     public function testApiAuthControllerResetPasswordShouldReturnSuccessMessageWhenUserNotFound() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |         $username = 'username'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |         $message = array('message' => Craft::t('Email has been sent if address exists')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |         $this->setSimpleMockApiAuthService(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |         $this->setMockUsersService($username); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |         $this->setMockRequestService('POST', $username, null, 1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |         $apiAuthController = $this->getMockApiAuthController('returnJson', $message); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |         $apiAuthController->actionResetPassword(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |      * @covers ::actionResetPassword | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |     public function testApiAuthControllerResetPasswordShouldSendMailWhenUserFound() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |         $username = 'username'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |         $message = array('message' => Craft::t('Email has been sent if address exists')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |         $this->setSimpleMockApiAuthService(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |         $this->setMockRequestService('POST', $username, null, 1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |         $mockUser = $this->getMockUser(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |         $mockUsersService = $this->setMockUsersService($username, $mockUser); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |         $mockUsersService->expects($this->exactly(1)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |             ->method('sendPasswordResetEmail') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |             ->with($mockUser); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |         $apiAuthController = $this->getMockApiAuthController('returnJson', $message); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |         $apiAuthController->actionResetPassword(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |     //============================================================================================================== | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |     //=================================================  MOCKS  ==================================================== | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |     //============================================================================================================== | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |      * @param string $method | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |      * @param mixed $param | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |      * @param mixed $return | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |      * @return ApiAuthController|mock | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |     private function getMockApiAuthController($method, $param, $return = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |         $apiAuthController = $this->getMockBuilder('Craft\ApiAuthController') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |             ->disableOriginalConstructor() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |             ->setMethods(array($method)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |             ->getMock(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |         $apiAuthController->expects($this->exactly(1)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |             ->method($method) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |             ->with($param) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |             ->willReturn($return); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |         return $apiAuthController; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |      * @return UserModel|mock | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |     private function getMockUser() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |         $mockUser = $this->getMockBuilder('Craft\UserModel') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  |             ->disableOriginalConstructor() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  |             ->getMock(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  |         return $mockUser; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 |  |  |      * @param string $requestType | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 |  |  |      * @param string $username | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 |  |  |      * @param string $password | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 |  |  |      * @param int $count | 
            
                                                                                                            
                            
            
                                    
            
            
                | 199 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 200 |  |  |      * @return UserPermissionsService|mock | 
            
                                                                                                            
                            
            
                                    
            
            
                | 201 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 202 |  |  |     private function setMockRequestService($requestType, $username = null, $password = null, $count = 2) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 203 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 204 |  |  |         $mockRequestService = $this->getMockBuilder('Craft\HttpRequestService') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 205 |  |  |             ->disableOriginalConstructor() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 206 |  |  |             ->getMock(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 207 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 208 |  |  |         $mockRequestService->expects($this->exactly(1)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 209 |  |  |             ->method('getRequestType') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 210 |  |  |             ->willReturn($requestType); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 211 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 212 |  |  |         if ($username !== null || $password !== null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 213 |  |  |             $mockRequestService->expects($this->exactly($count)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 214 |  |  |                 ->method('getRequiredPost') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 215 |  |  |                 ->willReturnMap(array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 216 |  |  |                     array('username', $username), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 217 |  |  |                     array('password', $password), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 218 |  |  |                 )); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 219 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 220 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 221 |  |  |         $this->setComponent(craft(), 'request', $mockRequestService); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 222 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 223 |  |  |         return $mockRequestService; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 224 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 225 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 226 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 227 |  |  |      * @param string $username | 
            
                                                                                                            
                            
            
                                    
            
            
                | 228 |  |  |      * @param string $password | 
            
                                                                                                            
                            
            
                                    
            
            
                | 229 |  |  |      * @param bool $success | 
            
                                                                                                            
                            
            
                                    
            
            
                | 230 |  |  |      * @param UserModel $mockUser | 
            
                                                                                                            
                            
            
                                    
            
            
                | 231 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 232 |  |  |      * @return UserSessionService|mock | 
            
                                                                                                            
                            
            
                                    
            
            
                | 233 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 234 |  |  |     private function setMockUserSessionService($username, $password, $success = true, UserModel $mockUser = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 235 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 236 |  |  |         $mockUserSessionService = $this->getMockBuilder('Craft\UserSessionService') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 237 |  |  |             ->disableOriginalConstructor() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 238 |  |  |             ->getMock(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 239 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 240 |  |  |         $mockUserSessionService->expects($this->exactly(1)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 241 |  |  |             ->method('login') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 242 |  |  |             ->with($username, $password) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 243 |  |  |             ->willReturn($success); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 244 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 245 |  |  |         if ($mockUser) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 246 |  |  |             $mockUserSessionService->expects($this->exactly(1)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 247 |  |  |                 ->method('getUser') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 248 |  |  |                 ->willReturn($mockUser); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 249 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 250 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 251 |  |  |         $this->setComponent(craft(), 'userSession', $mockUserSessionService); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 252 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 253 |  |  |         return $mockUserSessionService; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 254 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 255 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 256 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 257 |  |  |      * @return mock|ApiAuthService | 
            
                                                                                                            
                            
            
                                    
            
            
                | 258 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 259 |  |  |     private function setSimpleMockApiAuthService() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 260 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 261 |  |  |         $mockApiAuthService = $this->getMockBuilder('Craft\ApiAuthService') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 262 |  |  |             ->disableOriginalConstructor() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 263 |  |  |             ->getMock(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 264 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 265 |  |  |         $this->setComponent(craft(), 'apiAuth', $mockApiAuthService); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 266 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 267 |  |  |         return $mockApiAuthService; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 268 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 269 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 270 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 271 |  |  |      * @param string $key | 
            
                                                                                                            
                            
            
                                    
            
            
                | 272 |  |  |      * @param UserModel $mockUser | 
            
                                                                                                            
                            
            
                                    
            
            
                | 273 |  |  |      * @param bool $success | 
            
                                                                                                            
                            
            
                                    
            
            
                | 274 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 275 |  |  |      * @return mock|ApiAuthService | 
            
                                                                                                            
                            
            
                                    
            
            
                | 276 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 277 |  |  |     private function setMockApiAuthService($key, UserModel $mockUser, $success) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 278 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 279 |  |  |         $mockApiAuthService = $this->setSimpleMockApiAuthService(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 280 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 281 |  |  |         $mockApiAuthService->expects($this->exactly(1)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 282 |  |  |             ->method('generateKey') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 283 |  |  |             ->willReturn($key); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 284 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 285 |  |  |         $mockApiAuthService->expects($this->exactly(1)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 286 |  |  |             ->method('saveKey') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 287 |  |  |             ->with($mockUser, $key) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 288 |  |  |             ->willReturn($success); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 289 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 290 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 291 |  |  |         return $mockApiAuthService; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 292 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 293 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 294 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 295 |  |  |      * @param string $username | 
            
                                                                                                            
                            
            
                                    
            
            
                | 296 |  |  |      * @param UserModel $user | 
            
                                                                                                            
                            
            
                                    
            
            
                | 297 |  |  |      * @return mock|UsersService | 
            
                                                                                                            
                            
            
                                    
            
            
                | 298 |  |  |      */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 299 |  | View Code Duplication |     private function setMockUsersService($username, UserModel $user = null) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 300 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 301 |  |  |         $mockUsersService = $this->getMockBuilder('Craft\UsersService') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 302 |  |  |             ->disableOriginalConstructor() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 303 |  |  |             ->getMock(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 304 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 305 |  |  |         $mockUsersService->expects($this->exactly(1)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 306 |  |  |             ->method('getUserByUsernameOrEmail') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 307 |  |  |             ->with($username) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 308 |  |  |             ->willReturn($user); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 309 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 310 |  |  |         $this->setComponent(craft(), 'users', $mockUsersService); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 311 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 312 |  |  |         return $mockUsersService; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 313 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 314 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 315 |  |  |  | 
            
                        
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.