oauthConfig === null) { $this->oauthConfig = (array)Configure::read('OAuth'); $this->socialLogin = Configure::read('Users.Social.login'); } parent::setUp(); $this->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 ServerRequest(); } /** * tearDown method * * @return void */ public function tearDown() { Configure::write('OAuth', $this->oauthConfig); Configure::write('Users.Social.login', $this->socialLogin); 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\Http\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')); $request = $this->getMockBuilder('Cake\Http\ServerRequest') ->setMethods(['getSession']) ->getMock(); $request->expects($this->any()) ->method('getSession') ->will($this->returnValue($session)); $this->User->getView()->setRequest($request); $expected = 'Welcome, david'; $result = $this->User->welcome(); $this->assertEquals($expected, $result); } /** * Test link * * @return void */ public function testWelcomeNotLoggedInUser() { $session = $this->getMockBuilder('Cake\Http\Session') ->setMethods(['read']) ->getMock(); $session->expects($this->at(0)) ->method('read') ->with('Auth.User.id') ->will($this->returnValue(null)); $request = $this->getMockBuilder('Cake\Http\ServerRequest') ->setMethods(['getSession']) ->getMock(); $request->expects($this->any()) ->method('getSession') ->will($this->returnValue($session)); $this->User->getView()->setRequest($request); $result = $this->User->welcome(); $this->assertEmpty($result); } /** * Test add ReCaptcha field * * @return void */ public function testAddReCaptcha() { Configure::write('Users.reCaptcha.key', 'testKey'); Configure::write('Users.reCaptcha.theme', 'light'); Configure::write('Users.reCaptcha.size', 'normal'); Configure::write('Users.reCaptcha.tabindex', '3'); $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::setLocale('es_ES'); $result = $this->User->socialLogin('facebook'); $this->assertEquals('Iniciar sesión con Facebook', $result); I18n::setLocale('en_US'); } /** * Test social connect link list * * @return void */ public function testSocialConnectLinkList() { Configure::write('Users.Social.login', true); Configure::write('OAuth.providers.facebook.options.clientId', 'testclientidtestclientid'); Configure::write('OAuth.providers.facebook.options.clientSecret', 'testclientsecrettestclientsecret'); Configure::write('OAuth.providers.google.options.clientId', 'testclientidgoogtestclientidgoog'); Configure::write('OAuth.providers.google.options.clientSecret', 'testclientsecretgoogtestclientsecretgoog'); $actual = $this->User->socialConnectLinkList(); $expected = ' Connect with Facebook'; $expected .= ' Connect with Google'; $this->assertEquals($expected, $actual); } /** * Test social connect link list, user is connected with facebook * * @return void */ public function testSocialConnectLinkListIsConnectedWithFacebook() { Configure::write('Users.Social.login', true); Configure::write('OAuth.providers.facebook.options.clientId', 'testclientidtestclientid'); Configure::write('OAuth.providers.facebook.options.clientSecret', 'testclientsecrettestclientsecret'); Configure::write('OAuth.providers.google.options.clientId', 'testclientidgoogtestclientidgoog'); Configure::write('OAuth.providers.google.options.clientSecret', 'testclientsecretgoogtestclientsecretgoog'); $socialAccounts = [ new SocialAccount([ 'id' => '00000000-0000-0000-0000-000000000001', 'user_id' => '00000000-0000-0000-0000-000000000001', 'provider' => 'Facebook', 'username' => 'user-1-fb', 'reference' => 'reference-1-1234', 'avatar' => 'Lorem ipsum dolor sit amet', 'description' => 'Lorem ipsum dolor sit amet, aliquet feugiat. Convallis morbi fringilla gravida, phasellus feugiat dapibus velit nunc, pulvinar eget sollicitudin venenatis cum nullam, vivamus ut a sed, mollitia lectus. Nulla vestibulum massa neque ut et, id hendrerit sit, feugiat in taciti enim proin nibh, tempor dignissim, rhoncus duis vestibulum nunc mattis convallis.', 'token' => 'token-1234', 'token_secret' => 'Lorem ipsum dolor sit amet', 'token_expires' => '2015-05-22 21:52:44', 'active' => false, 'data' => '', 'created' => '2015-05-22 21:52:44', 'modified' => '2015-05-22 21:52:44' ]) ]; $actual = $this->User->socialConnectLinkList($socialAccounts); $expected = ' Connected with Facebook'; $expected .= ' Connect with Google'; $this->assertEquals($expected, $actual); } /** * Test social connect link list, social is not enabled * * @return void */ public function testSocialConnectLinkListSocialIsNotEnabled() { Configure::write('Users.Social.login', false); Configure::write('OAuth.providers.facebook.options.clientId', 'testclientidtestclientid'); Configure::write('OAuth.providers.facebook.options.clientSecret', 'testclientsecrettestclientsecret'); Configure::write('OAuth.providers.google.options.clientId', 'testclientidgoogtestclientidgoog'); Configure::write('OAuth.providers.google.options.clientSecret', 'testclientsecretgoogtestclientsecretgoog'); $socialAccounts = [ new SocialAccount([ 'id' => '00000000-0000-0000-0000-000000000001', 'user_id' => '00000000-0000-0000-0000-000000000001', 'provider' => 'Facebook', 'username' => 'user-1-fb', 'reference' => 'reference-1-1234', 'avatar' => 'Lorem ipsum dolor sit amet', 'description' => 'Lorem ipsum dolor sit amet, aliquet feugiat. Convallis morbi fringilla gravida, phasellus feugiat dapibus velit nunc, pulvinar eget sollicitudin venenatis cum nullam, vivamus ut a sed, mollitia lectus. Nulla vestibulum massa neque ut et, id hendrerit sit, feugiat in taciti enim proin nibh, tempor dignissim, rhoncus duis vestibulum nunc mattis convallis.', 'token' => 'token-1234', 'token_secret' => 'Lorem ipsum dolor sit amet', 'token_expires' => '2015-05-22 21:52:44', 'active' => false, 'data' => '', 'created' => '2015-05-22 21:52:44', 'modified' => '2015-05-22 21:52:44' ]) ]; $actual = $this->User->socialConnectLinkList($socialAccounts); $expected = ''; $this->assertEquals($expected, $actual); } /** * Test social connect link list, social is enabled but any provider was configured * * @return void */ public function testSocialConnectLinkListSocialEnabledButNotConfiguredProvider() { Configure::write('Users.Social.login', true); $socialAccounts = [ new SocialAccount([ 'id' => '00000000-0000-0000-0000-000000000001', 'user_id' => '00000000-0000-0000-0000-000000000001', 'provider' => 'Facebook', 'username' => 'user-1-fb', 'reference' => 'reference-1-1234', 'avatar' => 'Lorem ipsum dolor sit amet', 'description' => 'Lorem ipsum dolor sit amet, aliquet feugiat. Convallis morbi fringilla gravida, phasellus feugiat dapibus velit nunc, pulvinar eget sollicitudin venenatis cum nullam, vivamus ut a sed, mollitia lectus. Nulla vestibulum massa neque ut et, id hendrerit sit, feugiat in taciti enim proin nibh, tempor dignissim, rhoncus duis vestibulum nunc mattis convallis.', 'token' => 'token-1234', 'token_secret' => 'Lorem ipsum dolor sit amet', 'token_expires' => '2015-05-22 21:52:44', 'active' => false, 'data' => '', 'created' => '2015-05-22 21:52:44', 'modified' => '2015-05-22 21:52:44' ]) ]; $actual = $this->User->socialConnectLinkList($socialAccounts); $expected = ''; $this->assertEquals($expected, $actual); } }