View = $this->getMockBuilder('Cake\View\View') ->setMethods(['append']) ->getMock(); //Assuming all these url's are authorized $this->AuthLink = $this->getMockBuilder('CakeDC\Users\View\Helper\AuthLinkHelper') ->setMethods(['isAuthorized']) ->setConstructorArgs([$this->View]) ->getMock(); $this->AuthLink->expects($this->any()) ->method('isAuthorized') ->will($this->returnValue(true)); $this->User = new UserHelper($this->View); $this->User->AuthLink = $this->AuthLink; $this->request = new Request(); } /** * tearDown method * * @return void */ public function tearDown() { unset($this->User); parent::tearDown(); } /** * Test twitterLogin * * @return void */ public function testLogout() { $result = $this->User->logout(); $expected = 'Logout'; $this->assertEquals($expected, $result); } /** * Test twitterLogin * * @return void */ public function testLogoutDifferentMessage() { $result = $this->User->logout('Sign Out'); $expected = 'Sign Out'; $this->assertEquals($expected, $result); } /** * Test twitterLogin * * @return void */ public function testLogoutWithOptions() { $result = $this->User->logout('Sign Out', ['class' => 'logout']); $expected = 'Sign Out'; $this->assertEquals($expected, $result); } /** * Test link * * @return void */ public function testWelcome() { $session = $this->getMockBuilder('Cake\Network\Session') ->setMethods(['read']) ->getMock(); $session->expects($this->at(0)) ->method('read') ->with('Auth.User.id') ->will($this->returnValue(2)); $session->expects($this->at(1)) ->method('read') ->with('Auth.User.first_name') ->will($this->returnValue('david')); $this->User->request = $this->getMockBuilder('Cake\Network\Request') ->setMethods(['session']) ->getMock(); $this->User->request->expects($this->any()) ->method('session') ->will($this->returnValue($session)); $expected = 'Welcome, david'; $result = $this->User->welcome(); $this->assertEquals($expected, $result); } /** * Test link * * @return void */ public function testWelcomeNotLoggedInUser() { $session = $this->getMockBuilder('Cake\Network\Session') ->setMethods(['read']) ->getMock(); $session->expects($this->at(0)) ->method('read') ->with('Auth.User.id') ->will($this->returnValue(null)); $this->User->request = $this->getMockBuilder('Cake\Network\Request') ->setMethods(['session']) ->getMock(); $this->User->request->expects($this->any()) ->method('session') ->will($this->returnValue($session)); $result = $this->User->welcome(); $this->assertEmpty($result); } /** * Test add ReCaptcha field * * @return void */ public function testAddReCaptcha() { Configure::write('Users.reCaptcha.key', 'testKey'); $result = $this->User->addReCaptcha(); $this->assertEquals('
', $result); } /** * Test add ReCaptcha field * * @return void */ public function testAddReCaptchaEmpty() { $result = $this->User->addReCaptcha(); $expected = 'reCaptcha is not configured! Please configure Users.reCaptcha.key
'; $this->assertEquals($expected, $result); } /** * Test add ReCaptcha field * * @return void */ public function testAddReCaptchaScript() { $this->View->expects($this->at(0)) ->method('append') ->with('script', $this->stringContains('https://www.google.com/recaptcha/api.js')); $this->User->addReCaptchaScript(); } /** * Test social login link * * @return void */ public function testSocialLoginLink() { $result = $this->User->socialLogin('facebook'); $this->assertEquals('Sign in with Facebook', $result); $result = $this->User->socialLogin('twitter', ['label' => 'Register with']); $this->assertEquals('Register with Twitter', $result); } /** * test * * @return void */ public function testSocialLoginTranslation() { I18n::locale('es_ES'); $result = $this->User->socialLogin('facebook'); $this->assertEquals('Iniciar sesión con Facebook', $result); I18n::locale('en_US'); } }