From 239e8e55dd157ec89efad82558419a79df89050f Mon Sep 17 00:00:00 2001 From: agrochal Date: Fri, 20 Dec 2019 23:48:20 +0100 Subject: [PATCH 1/8] Create SocialAuthUserTest.php --- tests/src/Unit/SocialAuthUserTest.php | 488 ++++++++++++++++++++++++++ 1 file changed, 488 insertions(+) create mode 100644 tests/src/Unit/SocialAuthUserTest.php diff --git a/tests/src/Unit/SocialAuthUserTest.php b/tests/src/Unit/SocialAuthUserTest.php new file mode 100644 index 0000000..2a2a00e --- /dev/null +++ b/tests/src/Unit/SocialAuthUserTest.php @@ -0,0 +1,488 @@ +createMock(ConfigFactoryInterface::class); + $current_user = $this->createMock(AccountProxyInterface::class); + $data_handler = $this->createMock(SocialAuthDataHandler::class); + $entity_field_manager = $this->createMock(EntityFieldManagerInterface::class); + $entity_type_manager = $this->createMock(EntityTypeManagerInterface::class); + $event_dispatcher = $this->createMock(EventDispatcherInterface::class); + $file_system = $this->createMock(FileSystemInterface::class); + $language_manager = $this->createMock(LanguageManagerInterface::class); + $logger_factory = $this->createMock(LoggerChannelFactoryInterface::class); + $messenger = $this->createMock(MessengerInterface::class); + $route_provider = $this->createMock(RouteProviderInterface::class); + $token = $this->createMock(Token::class); + $transliteration = $this->createMock(PhpTransliteration::class); + $user_manager = $this->createMock(UserManager::class); + + $name = 'agrochal'; + $email = 'test@gmail.com'; + $provider_user_id = 3141592; + $token_auth = '712gd21asda862fa2da2da'; + $picture_url = 'https://www.drupal.org/files/styles/grid-2/public/my-avatar.png'; + $additional_data = '{"age": 22}'; + + $this->socialAuthUser = $this->getMockBuilder(SocialAuthUser::class) + ->setConstructorArgs([$name, + $email, + $provider_user_id, + $token_auth, + $picture_url, + $additional_data, + ]) + ->setMethods(NULL) + ->getMock(); + + $this->userManager = $this->getMockBuilder(UserManager::class) + ->setConstructorArgs([$entity_type_manager, + $messenger, + $logger_factory, + $config_factory, + $entity_field_manager, + $transliteration, + $language_manager, + $event_dispatcher, + $token, + $file_system, + ]) + ->setMethods(NULL) + ->getMock(); + + $this->userAuthenticator = $this->getMockBuilder(UserAuthenticator::class) + ->setConstructorArgs([$current_user, + $messenger, + $logger_factory, + $user_manager, + $data_handler, + $config_factory, + $route_provider, + $event_dispatcher, + ]) + ->setMethods(NULL) + ->getMock(); + } + + /** + * Tests for class SocialAuthUser. + */ + public function testSocialAuthUser() { + + $this->assertTrue( + method_exists($this->socialAuthUser, 'getFirstName'), + 'SocialAuthUser class does not implements getFirstName function/method' + ); + $this->assertTrue( + method_exists($this->socialAuthUser, 'setFirstName'), + 'SocialAuthUser class does not implements setFirstName function/method' + ); + $this->assertTrue( + method_exists($this->socialAuthUser, 'getLastName'), + 'SocialAuthUser class does not implements getLastName function/method' + ); + $this->assertTrue( + method_exists($this->socialAuthUser, 'setLastName'), + 'SocialAuthUser class does not implements setLastName function/method' + ); + $this->assertTrue( + method_exists($this->socialAuthUser, 'getName'), + 'SocialAuthUser class does not implements getName function/method' + ); + $this->assertTrue( + method_exists($this->socialAuthUser, 'setName'), + 'SocialAuthUser class does not implements setName function/method' + ); + $this->assertTrue( + method_exists($this->socialAuthUser, 'getEmail'), + 'SocialAuthUser class does not implements getEmail function/method' + ); + $this->assertTrue( + method_exists($this->socialAuthUser, 'setEmail'), + 'SocialAuthUser class does not implements setEmail function/method' + ); + $this->assertTrue( + method_exists($this->socialAuthUser, 'getProviderId'), + 'SocialAuthUser class does not implements getProviderId function/method' + ); + $this->assertTrue( + method_exists($this->socialAuthUser, 'setProviderId'), + 'SocialAuthUser class does not implements setProviderId function/method' + ); + $this->assertTrue( + method_exists($this->socialAuthUser, 'getToken'), + 'SocialAuthUser class does not implements getToken function/method' + ); + $this->assertTrue( + method_exists($this->socialAuthUser, 'setToken'), + 'SocialAuthUser class does not implements setToken function/method' + ); + $this->assertTrue( + method_exists($this->socialAuthUser, 'getPictureUrl'), + 'SocialAuthUser class does not implements getPictureUrl function/method' + ); + $this->assertTrue( + method_exists($this->socialAuthUser, 'setPictureUrl'), + 'SocialAuthUser class does not implements setPictureUrl function/method' + ); + $this->assertTrue( + method_exists($this->socialAuthUser, 'getPicture'), + 'SocialAuthUser class does not implements getPicture function/method' + ); + $this->assertTrue( + method_exists($this->socialAuthUser, 'setPicture'), + 'SocialAuthUser class does not implements setPicture function/method' + ); + $this->assertTrue( + method_exists($this->socialAuthUser, 'getAdditionalData'), + 'SocialAuthUser class does not implements getAdditionalData function/method' + ); + $this->assertTrue( + method_exists($this->socialAuthUser, 'setAdditionalData'), + 'SocialAuthUser class does not implements setAdditionalData function/method' + ); + $this->assertTrue( + method_exists($this->socialAuthUser, 'addData'), + 'SocialAuthUser class does not implements addData function/method' + ); + $this->assertTrue( + method_exists($this->socialAuthUser, 'getData'), + 'SocialAuthUser class does not implements getData function/method' + ); + + } + + /** + * @covers Drupal\social_auth\User\SocialAuthUser::setFirstName + */ + public function testSetFirstName() { + $this->socialAuthUser->setFirstName('John'); + $this->assertEquals('John', $this->socialAuthUser->getFirstName()); + } + + /** + * @covers Drupal\social_auth\User\SocialAuthUser::getFirstName + */ + public function testGetFirstName() { + $this->socialAuthUser->setFirstName('John'); + $this->assertNotEquals('Arthur', $this->socialAuthUser->getFirstName()); + } + + /** + * @covers Drupal\social_auth\User\SocialAuthUser::setLastName + */ + public function testSetLastName() { + $this->socialAuthUser->setLastName('Doe'); + $this->assertEquals('Doe', $this->socialAuthUser->getLastName()); + } + + /** + * @covers Drupal\social_auth\User\SocialAuthUser::getLastName + */ + public function testGetLastName() { + $this->socialAuthUser->setLastName('Doe'); + $this->assertNotEquals('Smith', $this->socialAuthUser->getLastName()); + } + + /** + * @covers Drupal\social_auth\User\SocialAuthUser::setName + */ + public function testSetName() { + $this->assertEquals('agrochal', $this->socialAuthUser->getName()); + $this->socialAuthUser->setName('somename'); + $this->assertEquals('somename', $this->socialAuthUser->getName()); + } + + /** + * @covers Drupal\social_auth\User\SocialAuthUser::getName + */ + public function testGetName() { + $this->socialAuthUser->setName('agrochal'); + $this->assertNotEquals('helloworld', $this->socialAuthUser->getName()); + } + + /** + * @covers Drupal\social_auth\User\SocialAuthUser::setEmail + */ + public function testSetEmail() { + $this->assertEquals('test@gmail.com', $this->socialAuthUser->getEmail()); + $this->socialAuthUser->setEmail('true@gmail.com'); + $this->assertEquals('true@gmail.com', $this->socialAuthUser->getEmail()); + } + + /** + * @covers Drupal\social_auth\User\SocialAuthUser::getEmail + */ + public function testGetEmail() { + $this->socialAuthUser->setEmail('true@gmail.com'); + $this->assertNotEquals('false@gmail.com', $this->socialAuthUser->getEmail()); + } + + /** + * @covers Drupal\social_auth\User\SocialAuthUser::setProviderId + */ + public function testSetProviderId() { + $this->assertEquals(3141592, $this->socialAuthUser->getProviderId()); + $this->socialAuthUser->setProviderId(16180339); + $this->assertEquals(16180339, $this->socialAuthUser->getProviderId()); + } + + /** + * @covers Drupal\social_auth\User\SocialAuthUser::getProviderId + */ + public function testGetProviderId() { + $this->socialAuthUser->setProviderId(16180339); + $this->assertNotEquals(3141592, $this->socialAuthUser->getProviderId()); + } + + /** + * @covers Drupal\social_auth\User\SocialAuthUser::setToken + */ + public function testSetToken() { + $this->assertEquals('712gd21asda862fa2da2da', $this->socialAuthUser->getToken()); + $this->socialAuthUser->setToken('21h8dazsa092b'); + $this->assertEquals('21h8dazsa092b', $this->socialAuthUser->getToken()); + } + + /** + * @covers Drupal\social_auth\User\SocialAuthUser::getToken + */ + public function testGetToken() { + $this->socialAuthUser->setToken('21h8dazsa092b'); + $this->assertNotEquals('ndau21as812t17ajk', $this->socialAuthUser->getToken()); + } + + /** + * @covers Drupal\social_auth\User\SocialAuthUser::setPictureUrl + */ + public function testSetPictureUrl() { + $this->assertEquals('https://www.drupal.org/files/styles/grid-2/public/my-avatar.png', $this->socialAuthUser->getPictureUrl()); + $this->socialAuthUser->setPictureUrl('https://www.drupal.org/files/styles/grid-2/public/default-avatar.png'); + $this->assertEquals('https://www.drupal.org/files/styles/grid-2/public/default-avatar.png', $this->socialAuthUser->getPictureUrl()); + } + + /** + * @covers Drupal\social_auth\User\SocialAuthUser::getPictureUrl + */ + public function testGetPictureUrl() { + $this->socialAuthUser->setPictureUrl('https://www.drupal.org/files/styles/grid-2/public/default-avatar.png'); + $this->assertNotEquals('https://www.drupal.org/files/styles/grid-2/public/extra-avatar.png', $this->socialAuthUser->getPictureUrl()); + } + + /** + * @covers Drupal\social_auth\User\SocialAuthUser::setPicture + */ + public function testSetPicture() { + $this->socialAuthUser->setPicture('fileid123'); + $this->assertEquals('fileid123', $this->socialAuthUser->getPicture()); + } + + /** + * @covers Drupal\social_auth\User\SocialAuthUser::getPicture + */ + public function testGetPicture() { + $this->socialAuthUser->setPicture('fileid123'); + $this->assertNotEquals('filebadid123', $this->socialAuthUser->getPicture()); + } + + /** + * @covers Drupal\social_auth\User\SocialAuthUser::setAdditionalData + */ + public function testSetAdditionalData() { + $this->assertEquals('{"age": 22}', $this->socialAuthUser->getAdditionalData()); + $this->socialAuthUser->setAdditionalData('{"id": 1246534534}'); + $this->assertEquals('{"id": 1246534534}', $this->socialAuthUser->getAdditionalData()); + } + + /** + * @covers Drupal\social_auth\User\SocialAuthUser::getAdditionalData + */ + public function testGetAdditionalData() { + $this->socialAuthUser->setAdditionalData('{"id": 1246534534}'); + $this->assertNotEquals('{"id": 63333212}', $this->socialAuthUser->getAdditionalData()); + } + + /** + * @covers Drupal\social_auth\User\SocialAuthUser::addData + */ + public function testAddData() { + $this->socialAuthUser->addData('value', 'Information'); + $this->assertEquals('Information', $this->socialAuthUser->getData('value')); + } + + /** + * @covers Drupal\social_auth\User\SocialAuthUser::getData + */ + public function testGetData() { + $this->socialAuthUser->addData('value', 'Information'); + $this->assertNotEquals('SomeData', $this->socialAuthUser->getData('value')); + } + + /** + * Tests for class UserManager. + */ + public function testUserManager() { + $this->assertTrue( + method_exists($this->userManager, 'createNewUser'), + 'UserManager class does not implements createNewUser function/method' + ); + $this->assertTrue( + method_exists($this->userManager, 'createUser'), + 'UserManager class does not implements createUser function/method' + ); + $this->assertTrue( + method_exists($this->userManager, 'addUserRecord'), + 'UserManager class does not implements addUserRecord function/method' + ); + $this->assertTrue( + method_exists($this->userManager, 'loadUserByProperty'), + 'UserManager class does not implements loadUserByProperty function/method' + ); + $this->assertTrue( + method_exists($this->userManager, 'saveUser'), + 'UserManager class does not implements saveUser function/method' + ); + $this->assertTrue( + method_exists($this->userManager, 'setProfilePic'), + 'UserManager class does not implements setProfilePic function/method' + ); + $this->assertTrue( + method_exists($this->userManager, 'downloadProfilePic'), + 'UserManager class does not implements downloadProfilePic function/method' + ); + $this->assertTrue( + method_exists($this->userManager, 'generateUniqueUsername'), + 'UserManager class does not implements generateUniqueUsername function/method' + ); + $this->assertTrue( + method_exists($this->userManager, 'getUserFields'), + 'UserManager class does not implements getUserFields function/method' + ); + $this->assertTrue( + method_exists($this->userManager, 'userPictureEnabled'), + 'UserManager class does not implements userPictureEnabled function/method' + ); + $this->assertTrue( + method_exists($this->userManager, 'getPictureDirectory'), + 'UserManager class does not implements getPictureDirectory function/method' + ); + $this->assertTrue( + method_exists($this->userManager, 'userPassword'), + 'UserManager class does not implements userPassword function/method' + ); + $this->assertTrue( + method_exists($this->userManager, 'systemRetrieveFile'), + 'UserManager class does not implements systemRetrieveFile function/method' + ); + + } + + /** + * Tests for class UserAuthenticator. + */ + public function testUserAuthenticator() { + $this->assertTrue( + method_exists($this->userAuthenticator, 'setDestination'), + 'UserAuthenticator class does not implements setDestination function/method' + ); + $this->assertTrue( + method_exists($this->userAuthenticator, 'authenticateUser'), + 'UserAuthenticator class does not implements authenticateUser function/method' + ); + $this->assertTrue( + method_exists($this->userAuthenticator, 'associateNewProvider'), + 'UserAuthenticator class does not implements associateNewProvider function/method' + ); + $this->assertTrue( + method_exists($this->userAuthenticator, 'authenticateWithProvider'), + 'UserAuthenticator class does not implements authenticateWithProvider function/method' + ); + $this->assertTrue( + method_exists($this->userAuthenticator, 'authenticateWithEmail'), + 'UserAuthenticator class does not implements authenticateWithEmail function/method' + ); + $this->assertTrue( + method_exists($this->userAuthenticator, 'authenticateExistingUser'), + 'UserAuthenticator class does not implements authenticateExistingUser function/method' + ); + $this->assertTrue( + method_exists($this->userAuthenticator, 'authenticateNewUser'), + 'UserAuthenticator class does not implements authenticateNewUser function/method' + ); + $this->assertTrue( + method_exists($this->userAuthenticator, 'loginUser'), + 'UserAuthenticator class does not implements loginUser function/method' + ); + $this->assertTrue( + method_exists($this->userAuthenticator, 'checkProviderIsAssociated'), + 'UserAuthenticator class does not implements checkProviderIsAssociated function/method' + ); + $this->assertTrue( + method_exists($this->userAuthenticator, 'getLoginFormRedirection'), + 'UserAuthenticator class does not implements getLoginFormRedirection function/method' + ); + $this->assertTrue( + method_exists($this->userAuthenticator, 'userLoginFinalize'), + 'UserAuthenticator class does not implements userLoginFinalize function/method' + ); + $this->assertTrue( + method_exists($this->userAuthenticator, 'dispatchAuthenticationError'), + 'UserAuthenticator class does not implements dispatchAuthenticationError function/method' + ); + $this->assertTrue( + method_exists($this->userAuthenticator, 'dispatchBeforeRedirect'), + 'UserAuthenticator class does not implements dispatchBeforeRedirect function/method' + ); + } + +} From 6c968295a51bf2396689cefcd0f272591db6ec7b Mon Sep 17 00:00:00 2001 From: agrochal Date: Fri, 27 Dec 2019 19:09:20 +0100 Subject: [PATCH 2/8] Update SocialAuthUserTest.php --- tests/src/Unit/SocialAuthUserTest.php | 257 +++----------------------- 1 file changed, 28 insertions(+), 229 deletions(-) diff --git a/tests/src/Unit/SocialAuthUserTest.php b/tests/src/Unit/SocialAuthUserTest.php index 2a2a00e..d71c6b5 100644 --- a/tests/src/Unit/SocialAuthUserTest.php +++ b/tests/src/Unit/SocialAuthUserTest.php @@ -67,9 +67,9 @@ public function setUp() { $transliteration = $this->createMock(PhpTransliteration::class); $user_manager = $this->createMock(UserManager::class); - $name = 'agrochal'; + $name = 'test'; $email = 'test@gmail.com'; - $provider_user_id = 3141592; + $provider_user_id = 'some_id'; $token_auth = '712gd21asda862fa2da2da'; $picture_url = 'https://www.drupal.org/files/styles/grid-2/public/my-avatar.png'; $additional_data = '{"age": 22}'; @@ -114,98 +114,11 @@ public function setUp() { ->getMock(); } - /** - * Tests for class SocialAuthUser. - */ - public function testSocialAuthUser() { - - $this->assertTrue( - method_exists($this->socialAuthUser, 'getFirstName'), - 'SocialAuthUser class does not implements getFirstName function/method' - ); - $this->assertTrue( - method_exists($this->socialAuthUser, 'setFirstName'), - 'SocialAuthUser class does not implements setFirstName function/method' - ); - $this->assertTrue( - method_exists($this->socialAuthUser, 'getLastName'), - 'SocialAuthUser class does not implements getLastName function/method' - ); - $this->assertTrue( - method_exists($this->socialAuthUser, 'setLastName'), - 'SocialAuthUser class does not implements setLastName function/method' - ); - $this->assertTrue( - method_exists($this->socialAuthUser, 'getName'), - 'SocialAuthUser class does not implements getName function/method' - ); - $this->assertTrue( - method_exists($this->socialAuthUser, 'setName'), - 'SocialAuthUser class does not implements setName function/method' - ); - $this->assertTrue( - method_exists($this->socialAuthUser, 'getEmail'), - 'SocialAuthUser class does not implements getEmail function/method' - ); - $this->assertTrue( - method_exists($this->socialAuthUser, 'setEmail'), - 'SocialAuthUser class does not implements setEmail function/method' - ); - $this->assertTrue( - method_exists($this->socialAuthUser, 'getProviderId'), - 'SocialAuthUser class does not implements getProviderId function/method' - ); - $this->assertTrue( - method_exists($this->socialAuthUser, 'setProviderId'), - 'SocialAuthUser class does not implements setProviderId function/method' - ); - $this->assertTrue( - method_exists($this->socialAuthUser, 'getToken'), - 'SocialAuthUser class does not implements getToken function/method' - ); - $this->assertTrue( - method_exists($this->socialAuthUser, 'setToken'), - 'SocialAuthUser class does not implements setToken function/method' - ); - $this->assertTrue( - method_exists($this->socialAuthUser, 'getPictureUrl'), - 'SocialAuthUser class does not implements getPictureUrl function/method' - ); - $this->assertTrue( - method_exists($this->socialAuthUser, 'setPictureUrl'), - 'SocialAuthUser class does not implements setPictureUrl function/method' - ); - $this->assertTrue( - method_exists($this->socialAuthUser, 'getPicture'), - 'SocialAuthUser class does not implements getPicture function/method' - ); - $this->assertTrue( - method_exists($this->socialAuthUser, 'setPicture'), - 'SocialAuthUser class does not implements setPicture function/method' - ); - $this->assertTrue( - method_exists($this->socialAuthUser, 'getAdditionalData'), - 'SocialAuthUser class does not implements getAdditionalData function/method' - ); - $this->assertTrue( - method_exists($this->socialAuthUser, 'setAdditionalData'), - 'SocialAuthUser class does not implements setAdditionalData function/method' - ); - $this->assertTrue( - method_exists($this->socialAuthUser, 'addData'), - 'SocialAuthUser class does not implements addData function/method' - ); - $this->assertTrue( - method_exists($this->socialAuthUser, 'getData'), - 'SocialAuthUser class does not implements getData function/method' - ); - - } - /** * @covers Drupal\social_auth\User\SocialAuthUser::setFirstName */ public function testSetFirstName() { + $this->assertNotEquals('John', $this->socialAuthUser->getFirstName()); $this->socialAuthUser->setFirstName('John'); $this->assertEquals('John', $this->socialAuthUser->getFirstName()); } @@ -214,14 +127,15 @@ public function testSetFirstName() { * @covers Drupal\social_auth\User\SocialAuthUser::getFirstName */ public function testGetFirstName() { - $this->socialAuthUser->setFirstName('John'); - $this->assertNotEquals('Arthur', $this->socialAuthUser->getFirstName()); + $this->socialAuthUser->setFirstName('Arthur'); + $this->assertEquals('Arthur', $this->socialAuthUser->getFirstName()); } /** * @covers Drupal\social_auth\User\SocialAuthUser::setLastName */ public function testSetLastName() { + $this->assertNotEquals('Doe', $this->socialAuthUser->getLastName()); $this->socialAuthUser->setLastName('Doe'); $this->assertEquals('Doe', $this->socialAuthUser->getLastName()); } @@ -230,15 +144,15 @@ public function testSetLastName() { * @covers Drupal\social_auth\User\SocialAuthUser::getLastName */ public function testGetLastName() { - $this->socialAuthUser->setLastName('Doe'); - $this->assertNotEquals('Smith', $this->socialAuthUser->getLastName()); + $this->socialAuthUser->setLastName('Smith'); + $this->assertEquals('Smith', $this->socialAuthUser->getLastName()); } /** * @covers Drupal\social_auth\User\SocialAuthUser::setName */ public function testSetName() { - $this->assertEquals('agrochal', $this->socialAuthUser->getName()); + $this->assertEquals('test', $this->socialAuthUser->getName()); $this->socialAuthUser->setName('somename'); $this->assertEquals('somename', $this->socialAuthUser->getName()); } @@ -247,8 +161,8 @@ public function testSetName() { * @covers Drupal\social_auth\User\SocialAuthUser::getName */ public function testGetName() { - $this->socialAuthUser->setName('agrochal'); - $this->assertNotEquals('helloworld', $this->socialAuthUser->getName()); + $this->socialAuthUser->setName('test2'); + $this->assertEquals('test2', $this->socialAuthUser->getName()); } /** @@ -264,25 +178,25 @@ public function testSetEmail() { * @covers Drupal\social_auth\User\SocialAuthUser::getEmail */ public function testGetEmail() { - $this->socialAuthUser->setEmail('true@gmail.com'); - $this->assertNotEquals('false@gmail.com', $this->socialAuthUser->getEmail()); + $this->socialAuthUser->setEmail('somemail@gmail.com'); + $this->assertEquals('somemail@gmail.com', $this->socialAuthUser->getEmail()); } /** * @covers Drupal\social_auth\User\SocialAuthUser::setProviderId */ public function testSetProviderId() { - $this->assertEquals(3141592, $this->socialAuthUser->getProviderId()); - $this->socialAuthUser->setProviderId(16180339); - $this->assertEquals(16180339, $this->socialAuthUser->getProviderId()); + $this->assertEquals('some_id', $this->socialAuthUser->getProviderId()); + $this->socialAuthUser->setProviderId('another_id'); + $this->assertEquals('another_id', $this->socialAuthUser->getProviderId()); } /** * @covers Drupal\social_auth\User\SocialAuthUser::getProviderId */ public function testGetProviderId() { - $this->socialAuthUser->setProviderId(16180339); - $this->assertNotEquals(3141592, $this->socialAuthUser->getProviderId()); + $this->socialAuthUser->setProviderId('provider_id'); + $this->assertEquals('provider_id', $this->socialAuthUser->getProviderId()); } /** @@ -299,7 +213,7 @@ public function testSetToken() { */ public function testGetToken() { $this->socialAuthUser->setToken('21h8dazsa092b'); - $this->assertNotEquals('ndau21as812t17ajk', $this->socialAuthUser->getToken()); + $this->assertEquals('21h8dazsa092b', $this->socialAuthUser->getToken()); } /** @@ -316,13 +230,14 @@ public function testSetPictureUrl() { */ public function testGetPictureUrl() { $this->socialAuthUser->setPictureUrl('https://www.drupal.org/files/styles/grid-2/public/default-avatar.png'); - $this->assertNotEquals('https://www.drupal.org/files/styles/grid-2/public/extra-avatar.png', $this->socialAuthUser->getPictureUrl()); + $this->assertEquals('https://www.drupal.org/files/styles/grid-2/public/default-avatar.png', $this->socialAuthUser->getPictureUrl()); } /** * @covers Drupal\social_auth\User\SocialAuthUser::setPicture */ public function testSetPicture() { + $this->assertNotEquals('fileid123', $this->socialAuthUser->getPicture()); $this->socialAuthUser->setPicture('fileid123'); $this->assertEquals('fileid123', $this->socialAuthUser->getPicture()); } @@ -331,8 +246,8 @@ public function testSetPicture() { * @covers Drupal\social_auth\User\SocialAuthUser::getPicture */ public function testGetPicture() { - $this->socialAuthUser->setPicture('fileid123'); - $this->assertNotEquals('filebadid123', $this->socialAuthUser->getPicture()); + $this->socialAuthUser->setPicture('fileid9899'); + $this->assertEquals('fileid9899', $this->socialAuthUser->getPicture()); } /** @@ -348,14 +263,15 @@ public function testSetAdditionalData() { * @covers Drupal\social_auth\User\SocialAuthUser::getAdditionalData */ public function testGetAdditionalData() { - $this->socialAuthUser->setAdditionalData('{"id": 1246534534}'); - $this->assertNotEquals('{"id": 63333212}', $this->socialAuthUser->getAdditionalData()); + $this->socialAuthUser->setAdditionalData('{"id": 9876}'); + $this->assertEquals('{"id": 9876}', $this->socialAuthUser->getAdditionalData()); } /** * @covers Drupal\social_auth\User\SocialAuthUser::addData */ public function testAddData() { + $this->assertNotEquals('Information', $this->socialAuthUser->getData('value')); $this->socialAuthUser->addData('value', 'Information'); $this->assertEquals('Information', $this->socialAuthUser->getData('value')); } @@ -364,125 +280,8 @@ public function testAddData() { * @covers Drupal\social_auth\User\SocialAuthUser::getData */ public function testGetData() { - $this->socialAuthUser->addData('value', 'Information'); - $this->assertNotEquals('SomeData', $this->socialAuthUser->getData('value')); - } - - /** - * Tests for class UserManager. - */ - public function testUserManager() { - $this->assertTrue( - method_exists($this->userManager, 'createNewUser'), - 'UserManager class does not implements createNewUser function/method' - ); - $this->assertTrue( - method_exists($this->userManager, 'createUser'), - 'UserManager class does not implements createUser function/method' - ); - $this->assertTrue( - method_exists($this->userManager, 'addUserRecord'), - 'UserManager class does not implements addUserRecord function/method' - ); - $this->assertTrue( - method_exists($this->userManager, 'loadUserByProperty'), - 'UserManager class does not implements loadUserByProperty function/method' - ); - $this->assertTrue( - method_exists($this->userManager, 'saveUser'), - 'UserManager class does not implements saveUser function/method' - ); - $this->assertTrue( - method_exists($this->userManager, 'setProfilePic'), - 'UserManager class does not implements setProfilePic function/method' - ); - $this->assertTrue( - method_exists($this->userManager, 'downloadProfilePic'), - 'UserManager class does not implements downloadProfilePic function/method' - ); - $this->assertTrue( - method_exists($this->userManager, 'generateUniqueUsername'), - 'UserManager class does not implements generateUniqueUsername function/method' - ); - $this->assertTrue( - method_exists($this->userManager, 'getUserFields'), - 'UserManager class does not implements getUserFields function/method' - ); - $this->assertTrue( - method_exists($this->userManager, 'userPictureEnabled'), - 'UserManager class does not implements userPictureEnabled function/method' - ); - $this->assertTrue( - method_exists($this->userManager, 'getPictureDirectory'), - 'UserManager class does not implements getPictureDirectory function/method' - ); - $this->assertTrue( - method_exists($this->userManager, 'userPassword'), - 'UserManager class does not implements userPassword function/method' - ); - $this->assertTrue( - method_exists($this->userManager, 'systemRetrieveFile'), - 'UserManager class does not implements systemRetrieveFile function/method' - ); - - } - - /** - * Tests for class UserAuthenticator. - */ - public function testUserAuthenticator() { - $this->assertTrue( - method_exists($this->userAuthenticator, 'setDestination'), - 'UserAuthenticator class does not implements setDestination function/method' - ); - $this->assertTrue( - method_exists($this->userAuthenticator, 'authenticateUser'), - 'UserAuthenticator class does not implements authenticateUser function/method' - ); - $this->assertTrue( - method_exists($this->userAuthenticator, 'associateNewProvider'), - 'UserAuthenticator class does not implements associateNewProvider function/method' - ); - $this->assertTrue( - method_exists($this->userAuthenticator, 'authenticateWithProvider'), - 'UserAuthenticator class does not implements authenticateWithProvider function/method' - ); - $this->assertTrue( - method_exists($this->userAuthenticator, 'authenticateWithEmail'), - 'UserAuthenticator class does not implements authenticateWithEmail function/method' - ); - $this->assertTrue( - method_exists($this->userAuthenticator, 'authenticateExistingUser'), - 'UserAuthenticator class does not implements authenticateExistingUser function/method' - ); - $this->assertTrue( - method_exists($this->userAuthenticator, 'authenticateNewUser'), - 'UserAuthenticator class does not implements authenticateNewUser function/method' - ); - $this->assertTrue( - method_exists($this->userAuthenticator, 'loginUser'), - 'UserAuthenticator class does not implements loginUser function/method' - ); - $this->assertTrue( - method_exists($this->userAuthenticator, 'checkProviderIsAssociated'), - 'UserAuthenticator class does not implements checkProviderIsAssociated function/method' - ); - $this->assertTrue( - method_exists($this->userAuthenticator, 'getLoginFormRedirection'), - 'UserAuthenticator class does not implements getLoginFormRedirection function/method' - ); - $this->assertTrue( - method_exists($this->userAuthenticator, 'userLoginFinalize'), - 'UserAuthenticator class does not implements userLoginFinalize function/method' - ); - $this->assertTrue( - method_exists($this->userAuthenticator, 'dispatchAuthenticationError'), - 'UserAuthenticator class does not implements dispatchAuthenticationError function/method' - ); - $this->assertTrue( - method_exists($this->userAuthenticator, 'dispatchBeforeRedirect'), - 'UserAuthenticator class does not implements dispatchBeforeRedirect function/method' - ); + $this->socialAuthUser->addData('value2', 'AnotherInformation'); + $this->assertEquals('AnotherInformation', $this->socialAuthUser->getData('value2')); } } From 192334b2b1a1f5fb8a1951b0482d3b338146708a Mon Sep 17 00:00:00 2001 From: agrochal Date: Mon, 30 Dec 2019 23:07:16 +0100 Subject: [PATCH 3/8] Tests for associateNewProvider and new function in UserAuthenticator --- src/User/UserAuthenticator.php | 10 ++++ tests/src/Unit/SocialAuthUserTest.php | 82 ++++++++++++++++++++++++--- 2 files changed, 83 insertions(+), 9 deletions(-) diff --git a/src/User/UserAuthenticator.php b/src/User/UserAuthenticator.php index 2bed759..385910c 100644 --- a/src/User/UserAuthenticator.php +++ b/src/User/UserAuthenticator.php @@ -440,4 +440,14 @@ public function dispatchBeforeRedirect($destination = NULL) { $this->eventDispatcher->dispatch(SocialAuthEvents::BEFORE_REDIRECT, $event); } + /** + * Gets the response. + * + * @return string + * Response + */ + public function getResponse() { + return $this->response; + } + } diff --git a/tests/src/Unit/SocialAuthUserTest.php b/tests/src/Unit/SocialAuthUserTest.php index d71c6b5..7adfa6d 100644 --- a/tests/src/Unit/SocialAuthUserTest.php +++ b/tests/src/Unit/SocialAuthUserTest.php @@ -18,6 +18,7 @@ use Drupal\social_auth\User\UserAuthenticator; use Drupal\social_auth\User\UserManager; use Drupal\Tests\UnitTestCase; +use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\EventDispatcher\EventDispatcherInterface; /** @@ -48,12 +49,31 @@ class SocialAuthUserTest extends UnitTestCase { */ protected $userAuthenticator; + /** + * The mocked AccountProxyInterface. + * + * @var \Drupal\Core\Session\AccountProxyInterface + */ + protected $currentUser; + + /** + * The mocked Messenger service. + * + * @var \Drupal\Core\Messenger\MessengerInterface + */ + protected $messenger; + /** * {@inheritdoc} */ public function setUp() { + + /** @var \Symfony\Component\DependencyInjection\ContainerInterface|\PHPUnit_Framework_MockObject_MockObject $container */ + $container = $this->createMock(ContainerInterface::class); + \Drupal::setContainer($container); + $config_factory = $this->createMock(ConfigFactoryInterface::class); - $current_user = $this->createMock(AccountProxyInterface::class); + $this->currentUser = $this->createMock(AccountProxyInterface::class); $data_handler = $this->createMock(SocialAuthDataHandler::class); $entity_field_manager = $this->createMock(EntityFieldManagerInterface::class); $entity_type_manager = $this->createMock(EntityTypeManagerInterface::class); @@ -61,11 +81,10 @@ public function setUp() { $file_system = $this->createMock(FileSystemInterface::class); $language_manager = $this->createMock(LanguageManagerInterface::class); $logger_factory = $this->createMock(LoggerChannelFactoryInterface::class); - $messenger = $this->createMock(MessengerInterface::class); + $this->messenger = $this->createMock(MessengerInterface::class); $route_provider = $this->createMock(RouteProviderInterface::class); $token = $this->createMock(Token::class); $transliteration = $this->createMock(PhpTransliteration::class); - $user_manager = $this->createMock(UserManager::class); $name = 'test'; $email = 'test@gmail.com'; @@ -87,7 +106,7 @@ public function setUp() { $this->userManager = $this->getMockBuilder(UserManager::class) ->setConstructorArgs([$entity_type_manager, - $messenger, + $this->messenger, $logger_factory, $config_factory, $entity_field_manager, @@ -97,21 +116,25 @@ public function setUp() { $token, $file_system, ]) - ->setMethods(NULL) + ->setMethods(['addUserRecord']) ->getMock(); $this->userAuthenticator = $this->getMockBuilder(UserAuthenticator::class) - ->setConstructorArgs([$current_user, - $messenger, + ->setConstructorArgs([$this->currentUser, + $this->messenger, $logger_factory, - $user_manager, + $this->userManager, $data_handler, $config_factory, $route_provider, $event_dispatcher, ]) - ->setMethods(NULL) + ->setMethods(['getLoginFormRedirection', 'getPostLoginRedirection']) ->getMock(); + + $this->currentUser->expects($this->any()) + ->method('id') + ->will($this->returnValue(12345)); } /** @@ -284,4 +307,45 @@ public function testGetData() { $this->assertEquals('AnotherInformation', $this->socialAuthUser->getData('value2')); } + /** + * If statement giving true scenario. + * + * @covers Drupal\social_auth\User\UserAuthenticator::associateNewProvider + */ + public function testAssociateNewProviderTrue() { + $this->userManager->expects($this->any()) + ->method('addUserRecord') + ->with($this->currentUser->id(), $this->isType('string'), $this->isType('string'), $this->isType('array')) + ->will($this->returnValue(TRUE)); + + $this->userAuthenticator->expects($this->any()) + ->method('getPostLoginRedirection') + ->will($this->returnValue('post_redirect')); + + $this->userAuthenticator->associateNewProvider('social_auth_test', 'd278127t8', ['test']); + $this->assertEquals('post_redirect', $this->userAuthenticator->getResponse()); + } + + /** + * If statement giving false scenario. + * + * @covers Drupal\social_auth\User\UserAuthenticator::associateNewProvider + */ + public function testAssociateNewProviderFalse() { + $this->userManager->expects($this->any()) + ->method('addUserRecord') + ->with($this->currentUser->id(), $this->isType('string'), $this->isType('string'), $this->isType('array')) + ->will($this->returnValue(FALSE)); + + $this->userAuthenticator->expects($this->any()) + ->method('getLoginFormRedirection') + ->will($this->returnValue('form_redirect')); + + $this->messenger->expects($this->exactly(1)) + ->method('addError'); + + $this->userAuthenticator->associateNewProvider('social_auth_test2', 'vdsa2rrf', ['test2']); + $this->assertEquals('form_redirect', $this->userAuthenticator->getResponse()); + } + } From 71afa1f38b648298a61a77b43885486661b1d83f Mon Sep 17 00:00:00 2001 From: agrochal Date: Fri, 3 Jan 2020 19:41:52 +0100 Subject: [PATCH 4/8] Tests for SocialAuthUser and UserAuthenticator --- tests/src/Unit/SocialAuthUserTest.php | 718 +++++++++++++++++++++++++- 1 file changed, 707 insertions(+), 11 deletions(-) diff --git a/tests/src/Unit/SocialAuthUserTest.php b/tests/src/Unit/SocialAuthUserTest.php index 7adfa6d..dbdbb43 100644 --- a/tests/src/Unit/SocialAuthUserTest.php +++ b/tests/src/Unit/SocialAuthUserTest.php @@ -8,6 +8,7 @@ use Drupal\Core\File\FileSystemInterface; use Drupal\Core\Language\LanguageManagerInterface; use Drupal\Core\Logger\LoggerChannelFactoryInterface; +use Drupal\Core\Logger\LoggerChannelInterface; use Drupal\Core\Messenger\MessengerInterface; use Drupal\Core\Routing\RouteProviderInterface; use Drupal\Core\Session\AccountProxyInterface; @@ -18,8 +19,11 @@ use Drupal\social_auth\User\UserAuthenticator; use Drupal\social_auth\User\UserManager; use Drupal\Tests\UnitTestCase; +use Drupal\user\Entity\User; +use Drupal\user\UserInterface; use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\EventDispatcher\EventDispatcherInterface; +use Symfony\Component\HttpFoundation\RedirectResponse; /** * Tests social_auth User. @@ -56,6 +60,13 @@ class SocialAuthUserTest extends UnitTestCase { */ protected $currentUser; + /** + * The mocked LoggerChannelFactoryInterface. + * + * @var \Drupal\Core\Logger\LoggerChannelFactoryInterface + */ + protected $loggerFactory; + /** * The mocked Messenger service. * @@ -63,6 +74,20 @@ class SocialAuthUserTest extends UnitTestCase { */ protected $messenger; + /** + * The test provider user id. + * + * @var string + */ + protected $providerUserId = 'some_id'; + + /** + * The test plugin id. + * + * @var string + */ + protected $pluginId = 'social_auth_test'; + /** * {@inheritdoc} */ @@ -80,7 +105,7 @@ public function setUp() { $event_dispatcher = $this->createMock(EventDispatcherInterface::class); $file_system = $this->createMock(FileSystemInterface::class); $language_manager = $this->createMock(LanguageManagerInterface::class); - $logger_factory = $this->createMock(LoggerChannelFactoryInterface::class); + $this->loggerFactory = $this->createMock(LoggerChannelFactoryInterface::class); $this->messenger = $this->createMock(MessengerInterface::class); $route_provider = $this->createMock(RouteProviderInterface::class); $token = $this->createMock(Token::class); @@ -107,7 +132,7 @@ public function setUp() { $this->userManager = $this->getMockBuilder(UserManager::class) ->setConstructorArgs([$entity_type_manager, $this->messenger, - $logger_factory, + $this->loggerFactory, $config_factory, $entity_field_manager, $transliteration, @@ -116,20 +141,32 @@ public function setUp() { $token, $file_system, ]) - ->setMethods(['addUserRecord']) + ->setMethods(['addUserRecord', + 'getDrupalUserId', + 'loadUserByProperty', + 'createNewUser', + ]) ->getMock(); $this->userAuthenticator = $this->getMockBuilder(UserAuthenticator::class) ->setConstructorArgs([$this->currentUser, $this->messenger, - $logger_factory, + $this->loggerFactory, $this->userManager, $data_handler, $config_factory, $route_provider, $event_dispatcher, ]) - ->setMethods(['getLoginFormRedirection', 'getPostLoginRedirection']) + ->setMethods(['getLoginFormRedirection', + 'getPostLoginRedirection', + 'userLoginFinalize', + 'isRegistrationDisabled', + 'isApprovalRequired', + 'redirectToUserForm', + 'isAdminDisabled', + 'isUserRoleDisabled', + ]) ->getMock(); $this->currentUser->expects($this->any()) @@ -308,11 +345,12 @@ public function testGetData() { } /** - * If statement giving true scenario. + * Tests the associateNewProvider method with true returned. * * @covers Drupal\social_auth\User\UserAuthenticator::associateNewProvider */ public function testAssociateNewProviderTrue() { + $redirect = new RedirectResponse('https://drupal.org/'); $this->userManager->expects($this->any()) ->method('addUserRecord') ->with($this->currentUser->id(), $this->isType('string'), $this->isType('string'), $this->isType('array')) @@ -320,18 +358,19 @@ public function testAssociateNewProviderTrue() { $this->userAuthenticator->expects($this->any()) ->method('getPostLoginRedirection') - ->will($this->returnValue('post_redirect')); + ->will($this->returnValue($redirect)); $this->userAuthenticator->associateNewProvider('social_auth_test', 'd278127t8', ['test']); - $this->assertEquals('post_redirect', $this->userAuthenticator->getResponse()); + $this->assertEquals($redirect, $this->userAuthenticator->getResponse()); } /** - * If statement giving false scenario. + * Tests the associateNewProvider method with false returned. * * @covers Drupal\social_auth\User\UserAuthenticator::associateNewProvider */ public function testAssociateNewProviderFalse() { + $redirect = new RedirectResponse('https://drupal.org/'); $this->userManager->expects($this->any()) ->method('addUserRecord') ->with($this->currentUser->id(), $this->isType('string'), $this->isType('string'), $this->isType('array')) @@ -339,13 +378,670 @@ public function testAssociateNewProviderFalse() { $this->userAuthenticator->expects($this->any()) ->method('getLoginFormRedirection') - ->will($this->returnValue('form_redirect')); + ->will($this->returnValue($redirect)); $this->messenger->expects($this->exactly(1)) ->method('addError'); $this->userAuthenticator->associateNewProvider('social_auth_test2', 'vdsa2rrf', ['test2']); - $this->assertEquals('form_redirect', $this->userAuthenticator->getResponse()); + $this->assertEquals($redirect, $this->userAuthenticator->getResponse()); + } + + /** + * Tests the checkProviderIsAssociated method with Drupal user not existing. + * + * @covers Drupal\social_auth\User\UserAuthenticator::checkProviderIsAssociated + */ + public function testCheckProviderIsAssociatedNotExist() { + $this->userManager->expects($this->once()) + ->method('getDrupalUserId') + ->with($this->providerUserId) + ->will($this->returnValue(FALSE)); + + $this->assertFalse($this->userAuthenticator->checkProviderIsAssociated($this->providerUserId)); + } + + /** + * Tests the checkProviderIsAssociated method with Drupal user existing. + * + * @covers Drupal\social_auth\User\UserAuthenticator::checkProviderIsAssociated + */ + public function testCheckProviderIsAssociatedExist() { + $this->userManager->expects($this->once()) + ->method('getDrupalUserId') + ->with($this->providerUserId) + ->will($this->returnValue(6721234)); + + $this->assertEquals(6721234, $this->userAuthenticator->checkProviderIsAssociated($this->providerUserId)); + } + + /** + * Tests the loginUser method with not active account. + * + * @covers Drupal\social_auth\User\UserAuthenticator::loginUser + */ + public function testLoginUserNotActive() { + $user = $this->createMock(UserInterface::class); + $logger = $this->createMock(LoggerChannelInterface::class); + $user->expects($this->once()) + ->method('isActive') + ->will($this->returnValue(FALSE)); + + $this->loggerFactory->expects($this->once()) + ->method('get') + ->with($this->pluginId) + ->will($this->returnValue($logger)); + + $logger->expects($this->once()) + ->method('warning') + ->with($this->anything()); + + $this->userAuthenticator->setPluginId($this->pluginId); + $this->assertFalse($this->userAuthenticator->loginUser($user)); + } + + /** + * Tests the loginUser method with active account. + * + * @covers Drupal\social_auth\User\UserAuthenticator::loginUser + */ + public function testLoginUserActive() { + $user = $this->createMock(UserInterface::class); + + $user->expects($this->once()) + ->method('isActive') + ->will($this->returnValue(TRUE)); + + $this->userAuthenticator->expects($this->once()) + ->method('userLoginFinalize') + ->with($user); + + $this->userAuthenticator->setPluginId($this->pluginId); + $this->assertTrue($this->userAuthenticator->loginUser($user)); + } + + /** + * Tests the authenticateWithProvider method with successful authentication. + * + * @covers Drupal\social_auth\User\UserAuthenticator::authenticateWithProvider + */ + public function testAuthenticateWithProviderSuccess() { + $user = $this->createMock(User::class); + $redirect = new RedirectResponse('https://drupal.org/'); + + $this->userManager->expects($this->once()) + ->method('loadUserByProperty') + ->with('uid', 12345) + ->will($this->returnValue($user)); + + $this->userAuthenticator->expects($this->once()) + ->method('isAdminDisabled') + ->with($this->anything()) + ->will($this->returnValue(TRUE)); + + $this->messenger->expects($this->exactly(1)) + ->method('addError'); + + $this->userAuthenticator->expects($this->any()) + ->method('getLoginFormRedirection') + ->will($this->returnValue($redirect)); + + $this->assertTrue($this->userAuthenticator->authenticateWithProvider(12345)); + + } + + /** + * Tests the authenticateWithProvider method with failure while loading user. + * + * @covers Drupal\social_auth\User\UserAuthenticator::authenticateWithProvider + */ + public function testAuthenticateWithProviderFailLoading() { + $this->userManager->expects($this->once()) + ->method('loadUserByProperty') + ->with('uid', 12345) + ->will($this->returnValue(NULL)); + + $this->assertFalse($this->userAuthenticator->authenticateWithProvider(12345)); + } + + /** + * Tests the authenticateWithProvider method with exception thrown. + * + * @covers Drupal\social_auth\User\UserAuthenticator::authenticateWithProvider + */ + public function testAuthenticateWithProviderException() { + $logger = $this->createMock(LoggerChannelInterface::class); + $this->userManager->expects($this->once()) + ->method('loadUserByProperty') + ->with('uid', 12345) + ->will($this->returnCallback(function () { + throw new \Exception('test'); + })); + + $this->loggerFactory->expects($this->once()) + ->method('get') + ->with($this->pluginId) + ->will($this->returnValue($logger)); + + $logger->expects($this->once()) + ->method('error') + ->with($this->anything()); + + $this->userAuthenticator->setPluginId($this->pluginId); + $this->assertFalse($this->userAuthenticator->authenticateWithProvider(12345)); + + } + + /** + * Tests the authenticateNewUser method with not valid user. + * + * @covers Drupal\social_auth\User\UserAuthenticator::authenticateNewUser + */ + public function testAuthenticateNewUserNotValid() { + $redirect = new RedirectResponse('https://drupal.org/'); + + $this->userAuthenticator->expects($this->once()) + ->method('isRegistrationDisabled') + ->will($this->returnValue(TRUE)); + + $this->userAuthenticator->expects($this->any()) + ->method('getLoginFormRedirection') + ->will($this->returnValue($redirect)); + + $this->userAuthenticator->authenticateNewUser(NULL); + $this->assertEquals($redirect, $this->userAuthenticator->getResponse()); + } + + /** + * Tests the authenticateNewUser method valid user and need admin approval. + * + * @covers Drupal\social_auth\User\UserAuthenticator::authenticateNewUser + */ + public function testAuthenticateNewUserValidApproveNeed() { + $redirect = new RedirectResponse('https://drupal.org/'); + $user = $this->createMock(UserInterface::class); + + $this->userAuthenticator->expects($this->once()) + ->method('isApprovalRequired') + ->will($this->returnValue(TRUE)); + + $this->userAuthenticator->expects($this->any()) + ->method('getLoginFormRedirection') + ->will($this->returnValue($redirect)); + + $this->messenger->expects($this->exactly(1)) + ->method('addWarning'); + + $this->userAuthenticator->authenticateNewUser($user); + $this->assertEquals($redirect, $this->userAuthenticator->getResponse()); + } + + /** + * Tests the authenticateNewUser method with valid user and fail logging in. + * + * @covers Drupal\social_auth\User\UserAuthenticator::authenticateNewUser + */ + public function testAuthenticateNewUserValidFailLogin() { + $redirect = new RedirectResponse('https://drupal.org/'); + $user = $this->createMock(UserInterface::class); + $logger = $this->createMock(LoggerChannelInterface::class); + + $user->expects($this->once()) + ->method('isActive') + ->will($this->returnValue(FALSE)); + + $this->loggerFactory->expects($this->once()) + ->method('get') + ->with($this->pluginId) + ->will($this->returnValue($logger)); + + $logger->expects($this->once()) + ->method('warning') + ->with($this->anything()); + + $this->userAuthenticator->expects($this->once()) + ->method('isApprovalRequired') + ->will($this->returnValue(FALSE)); + + $this->userAuthenticator->expects($this->once()) + ->method('isRegistrationDisabled') + ->will($this->returnValue(TRUE)); + + $this->userAuthenticator->expects($this->any()) + ->method('getLoginFormRedirection') + ->will($this->returnValue($redirect)); + + $this->userAuthenticator->setPluginId($this->pluginId); + $this->userAuthenticator->authenticateNewUser($user); + $this->assertEquals($redirect, $this->userAuthenticator->getResponse()); + } + + /** + * Tests the authenticateNewUser method login and redirect to login form. + * + * @covers Drupal\social_auth\User\UserAuthenticator::authenticateNewUser + */ + public function testAuthenticateNewUserValidLoginFormRedirect() { + $redirect = new RedirectResponse('https://drupal.org/'); + $user = $this->createMock(UserInterface::class); + + $this->userAuthenticator->expects($this->once()) + ->method('isApprovalRequired') + ->will($this->returnValue(FALSE)); + + $user->expects($this->once()) + ->method('isActive') + ->will($this->returnValue(TRUE)); + + $this->userAuthenticator->expects($this->once()) + ->method('userLoginFinalize') + ->with($user); + + $this->userAuthenticator->expects($this->any()) + ->method('redirectToUserForm') + ->with($user) + ->will($this->returnValue($redirect)); + + $this->userAuthenticator->setPluginId($this->pluginId); + $this->userAuthenticator->authenticateNewUser($user); + $this->assertEquals($redirect, $this->userAuthenticator->getResponse()); + } + + /** + * Tests the authenticateNewUser method login and redirect to post login. + * + * @covers Drupal\social_auth\User\UserAuthenticator::authenticateNewUser + */ + public function testAuthenticateNewUserValidLoginPostRedirect() { + $redirect = new RedirectResponse('https://drupal.org/'); + $user = $this->createMock(UserInterface::class); + + $this->userAuthenticator->expects($this->once()) + ->method('isApprovalRequired') + ->will($this->returnValue(FALSE)); + + $user->expects($this->once()) + ->method('isActive') + ->will($this->returnValue(TRUE)); + + $this->userAuthenticator->expects($this->once()) + ->method('userLoginFinalize') + ->with($user); + + $this->userAuthenticator->expects($this->any()) + ->method('redirectToUserForm') + ->with($user) + ->will($this->returnValue(NULL)); + + $this->userAuthenticator->expects($this->any()) + ->method('getPostLoginRedirection') + ->will($this->returnValue($redirect)); + + $this->userAuthenticator->setPluginId($this->pluginId); + $this->userAuthenticator->authenticateNewUser($user); + $this->assertEquals($redirect, $this->userAuthenticator->getResponse()); + } + + /** + * Tests the authenticateWithEmail method with account existing. + * + * @covers Drupal\social_auth\User\UserAuthenticator::authenticateWithEmail + */ + public function testAuthenticateWithEmailAccountExist() { + $user = $this->createMock(User::class); + $redirect = new RedirectResponse('https://drupal.org/'); + $this->userManager->expects($this->once()) + ->method('loadUserByProperty') + ->with('mail', 'test@gmail.com') + ->will($this->returnValue($user)); + + $this->userAuthenticator->expects($this->once()) + ->method('isAdminDisabled') + ->with($this->anything()) + ->will($this->returnValue(TRUE)); + + $this->messenger->expects($this->exactly(1)) + ->method('addError'); + + $this->userAuthenticator->expects($this->any()) + ->method('getLoginFormRedirection') + ->will($this->returnValue($redirect)); + + $this->assertTrue($this->userAuthenticator->authenticateWithEmail('test@gmail.com', $this->providerUserId, '2873dgAS', ['test'])); + } + + /** + * Tests the authenticateWithEmail method with account not existing. + * + * @covers Drupal\social_auth\User\UserAuthenticator::authenticateWithEmail + */ + public function testAuthenticateWithEmailAccountNoExist() { + $logger = $this->createMock(LoggerChannelInterface::class); + $this->userManager->expects($this->once()) + ->method('loadUserByProperty') + ->with('mail', 'test@gmail.com') + ->will($this->returnCallback(function () { + throw new \Exception('test'); + })); + + $this->loggerFactory->expects($this->once()) + ->method('get') + ->with($this->pluginId) + ->will($this->returnValue($logger)); + + $logger->expects($this->once()) + ->method('error') + ->with($this->anything()); + + $this->userAuthenticator->setPluginId($this->pluginId); + $this->assertFalse($this->userAuthenticator->authenticateWithEmail('test@gmail.com', $this->providerUserId, '2873dgAS', ['test'])); + } + + /** + * Tests the authenticateExistingUser method with admin auth disabled. + * + * @covers Drupal\social_auth\User\UserAuthenticator::authenticateExistingUser + */ + public function testAuthenticateExistingUserAdminDisabled() { + $user = $this->createMock(UserInterface::class); + $redirect = new RedirectResponse('https://drupal.org/'); + + $this->userAuthenticator->expects($this->once()) + ->method('isAdminDisabled') + ->with($user) + ->will($this->returnValue(TRUE)); + + $this->messenger->expects($this->exactly(1)) + ->method('addError'); + + $this->userAuthenticator->expects($this->any()) + ->method('getLoginFormRedirection') + ->will($this->returnValue($redirect)); + + $this->userAuthenticator->authenticateExistingUser($user); + $this->assertEquals($redirect, $this->userAuthenticator->getResponse()); + } + + /** + * Tests the authenticateExistingUser method with user role auth disabled. + * + * @covers Drupal\social_auth\User\UserAuthenticator::authenticateExistingUser + */ + public function testAuthenticateExistingUserRoleDisabled() { + $user = $this->createMock(UserInterface::class); + $redirect = new RedirectResponse('https://drupal.org/'); + + $this->userAuthenticator->expects($this->once()) + ->method('isAdminDisabled') + ->with($user) + ->will($this->returnValue(FALSE)); + + $this->userAuthenticator->expects($this->once()) + ->method('isUserRoleDisabled') + ->with($user) + ->will($this->returnValue(TRUE)); + + $this->messenger->expects($this->exactly(1)) + ->method('addError'); + + $this->userAuthenticator->expects($this->any()) + ->method('getLoginFormRedirection') + ->will($this->returnValue($redirect)); + + $this->userAuthenticator->authenticateExistingUser($user); + $this->assertEquals($redirect, $this->userAuthenticator->getResponse()); + } + + /** + * Tests the authenticateExistingUser method with successful logging in. + * + * @covers Drupal\social_auth\User\UserAuthenticator::authenticateExistingUser + */ + public function testAuthenticateExistingUserSucessLogin() { + $user = $this->createMock(UserInterface::class); + $redirect = new RedirectResponse('https://drupal.org/'); + + $this->userAuthenticator->expects($this->once()) + ->method('isAdminDisabled') + ->with($user) + ->will($this->returnValue(FALSE)); + + $this->userAuthenticator->expects($this->once()) + ->method('isUserRoleDisabled') + ->with($user) + ->will($this->returnValue(FALSE)); + + $user->expects($this->once()) + ->method('isActive') + ->will($this->returnValue(TRUE)); + + $this->userAuthenticator->expects($this->once()) + ->method('userLoginFinalize') + ->with($user); + + $this->userAuthenticator->expects($this->any()) + ->method('getPostLoginRedirection') + ->will($this->returnValue($redirect)); + + $this->userAuthenticator->setPluginId($this->pluginId); + $this->userAuthenticator->authenticateExistingUser($user); + $this->assertEquals($redirect, $this->userAuthenticator->getResponse()); + } + + /** + * Tests the authenticateExistingUser method with failure logging in. + * + * @covers Drupal\social_auth\User\UserAuthenticator::authenticateExistingUser + */ + public function testAuthenticateExistingUserFailureLogin() { + $user = $this->createMock(UserInterface::class); + $logger = $this->createMock(LoggerChannelInterface::class); + $redirect = new RedirectResponse('https://drupal.org/'); + + $this->userAuthenticator->expects($this->once()) + ->method('isAdminDisabled') + ->with($user) + ->will($this->returnValue(FALSE)); + + $this->userAuthenticator->expects($this->once()) + ->method('isUserRoleDisabled') + ->with($user) + ->will($this->returnValue(FALSE)); + + $user->expects($this->once()) + ->method('isActive') + ->will($this->returnValue(FALSE)); + + $this->loggerFactory->expects($this->once()) + ->method('get') + ->with($this->pluginId) + ->will($this->returnValue($logger)); + + $logger->expects($this->once()) + ->method('warning') + ->with($this->anything()); + + $this->messenger->expects($this->exactly(1)) + ->method('addError'); + + $this->userAuthenticator->expects($this->any()) + ->method('getLoginFormRedirection') + ->will($this->returnValue($redirect)); + + $this->userAuthenticator->setPluginId($this->pluginId); + $this->userAuthenticator->authenticateExistingUser($user); + $this->assertEquals($redirect, $this->userAuthenticator->getResponse()); + } + + /** + * Tests the authenticateUser method - no record for provider exists. + * + * @covers Drupal\social_auth\User\UserAuthenticator::authenticateUser + */ + public function testAuthenticateUserNoRecord() { + $redirect = new RedirectResponse('https://drupal.org/'); + $this->userManager->expects($this->once()) + ->method('getDrupalUserId') + ->with($this->providerUserId) + ->will($this->returnValue(FALSE)); + + $this->currentUser->expects($this->once()) + ->method('isAuthenticated') + ->will($this->returnValue(TRUE)); + + $this->userManager->expects($this->any()) + ->method('addUserRecord') + ->with($this->currentUser->id(), $this->isType('string'), $this->isType('string'), $this->isType('array')) + ->will($this->returnValue(TRUE)); + + $this->userAuthenticator->expects($this->any()) + ->method('getPostLoginRedirection') + ->will($this->returnValue($redirect)); + + $this->assertEquals($redirect, $this->userAuthenticator->authenticateUser('username', 'test@gmail.com', $this->providerUserId, 'S92xzuwssa2', 'https://www.drupal.org/files/styles/grid-2/public/default-avatar.png', ['data'])); + } + + /** + * Tests the authenticateUser method - provider is associated. + * + * @covers Drupal\social_auth\User\UserAuthenticator::authenticateUser + */ + public function testAuthenticateUserProviderAssociated() { + $redirect = new RedirectResponse('https://drupal.org/'); + $this->userManager->expects($this->once()) + ->method('getDrupalUserId') + ->with($this->providerUserId) + ->will($this->returnValue(123456)); + + $this->currentUser->expects($this->once()) + ->method('isAuthenticated') + ->will($this->returnValue(TRUE)); + + $this->userAuthenticator->expects($this->any()) + ->method('getPostLoginRedirection') + ->will($this->returnValue($redirect)); + + $this->assertEquals($redirect, $this->userAuthenticator->authenticateUser('username', 'test@gmail.com', $this->providerUserId, 'S92xzuwssa2', 'https://www.drupal.org/files/styles/grid-2/public/default-avatar.png', ['data'])); + } + + /** + * Tests the authenticateUser method - auth with provider. + * + * @covers Drupal\social_auth\User\UserAuthenticator::authenticateUser + */ + public function testAuthenticateUserAuthProvider() { + $user = $this->createMock(UserInterface::class); + $redirect = new RedirectResponse('https://drupal.org/'); + + $this->userManager->expects($this->once()) + ->method('getDrupalUserId') + ->with($this->providerUserId) + ->will($this->returnValue(123456)); + + $this->currentUser->expects($this->once()) + ->method('isAuthenticated') + ->will($this->returnValue(FALSE)); + + $this->userManager->expects($this->once()) + ->method('loadUserByProperty') + ->with('uid', 123456) + ->will($this->returnValue($user)); + + $this->userAuthenticator->expects($this->once()) + ->method('isAdminDisabled') + ->with($user) + ->will($this->returnValue(TRUE)); + + $this->messenger->expects($this->exactly(1)) + ->method('addError'); + + $this->userAuthenticator->expects($this->any()) + ->method('getLoginFormRedirection') + ->will($this->returnValue($redirect)); + + $this->assertEquals($redirect, $this->userAuthenticator->authenticateUser('username', 'test@gmail.com', $this->providerUserId, 'S92xzuwssa2', 'https://www.drupal.org/files/styles/grid-2/public/default-avatar.png', ['data'])); + } + + /** + * Tests the authenticateUser method - auth with email. + * + * @covers Drupal\social_auth\User\UserAuthenticator::authenticateUser + */ + public function testAuthenticateUserAuthEmail() { + $user = $this->createMock(User::class); + $redirect = new RedirectResponse('https://drupal.org/'); + + $this->userManager->expects($this->once()) + ->method('getDrupalUserId') + ->with($this->providerUserId) + ->will($this->returnValue(NULL)); + + $this->currentUser->expects($this->once()) + ->method('isAuthenticated') + ->will($this->returnValue(FALSE)); + + $this->userManager->expects($this->once()) + ->method('loadUserByProperty') + ->with('mail', 'test@gmail.com') + ->will($this->returnValue($user)); + + $this->userAuthenticator->expects($this->once()) + ->method('isAdminDisabled') + ->with($this->anything()) + ->will($this->returnValue(TRUE)); + + $this->messenger->expects($this->exactly(1)) + ->method('addError'); + + $this->userAuthenticator->expects($this->any()) + ->method('getLoginFormRedirection') + ->will($this->returnValue($redirect)); + + $this->assertEquals($redirect, $this->userAuthenticator->authenticateUser('username', 'test@gmail.com', $this->providerUserId, 'S92xzuwssa2', 'https://www.drupal.org/files/styles/grid-2/public/default-avatar.png', ['data'])); + } + + /** + * Tests the authenticateUser method - create new user. + * + * @covers Drupal\social_auth\User\UserAuthenticator::authenticateUser + */ + public function testAuthenticateUserNewUser() { + $user = $this->createMock(UserInterface::class); + $redirect = new RedirectResponse('https://drupal.org/'); + + $this->userManager->expects($this->once()) + ->method('getDrupalUserId') + ->with($this->providerUserId) + ->will($this->returnValue(NULL)); + + $this->currentUser->expects($this->once()) + ->method('isAuthenticated') + ->will($this->returnValue(FALSE)); + + $this->userManager->expects($this->once()) + ->method('createNewUser') + ->with($this->anything()) + ->will($this->returnValue($user)); + + $this->userAuthenticator->expects($this->once()) + ->method('isApprovalRequired') + ->will($this->returnValue(FALSE)); + + $user->expects($this->once()) + ->method('isActive') + ->will($this->returnValue(TRUE)); + + $this->userAuthenticator->expects($this->once()) + ->method('userLoginFinalize') + ->with($user); + + $this->userAuthenticator->expects($this->any()) + ->method('redirectToUserForm') + ->with($user) + ->will($this->returnValue($redirect)); + + $this->userAuthenticator->setPluginId($this->pluginId); + $this->assertEquals($redirect, $this->userAuthenticator->authenticateUser('username', '', $this->providerUserId, 'S92xzuwssa2', 'https://www.drupal.org/files/styles/grid-2/public/default-avatar.png', ['data'])); } } From 07473ef15b9a11ca040068d69a26c68da5f03b6e Mon Sep 17 00:00:00 2001 From: agrochal Date: Sat, 4 Jan 2020 22:53:24 +0100 Subject: [PATCH 5/8] Tests and UserAuthenticator refactoring --- src/User/UserAuthenticator.php | 40 +-- tests/src/Unit/SocialAuthUserTest.php | 355 ++++++++++++++++---------- 2 files changed, 247 insertions(+), 148 deletions(-) diff --git a/src/User/UserAuthenticator.php b/src/User/UserAuthenticator.php index 385910c..a6f5267 100644 --- a/src/User/UserAuthenticator.php +++ b/src/User/UserAuthenticator.php @@ -126,7 +126,7 @@ public function authenticateUser($name, $email, $provider_user_id, $token, $pict if ($user_id === FALSE) { $this->associateNewProvider($provider_user_id, $token, $data); - return $this->response; + return $this->getResponse(); } // User is authenticated and provider is already associated. else { @@ -138,14 +138,14 @@ public function authenticateUser($name, $email, $provider_user_id, $token, $pict if ($user_id) { $this->authenticateWithProvider($user_id); - return $this->response; + return $this->getResponse(); } // Try to authenticate user using email address. if ($email) { // If authentication with email was successful. if ($this->authenticateWithEmail($email, $provider_user_id, $token, $data)) { - return $this->response; + return $this->getResponse(); } } @@ -156,7 +156,7 @@ public function authenticateUser($name, $email, $provider_user_id, $token, $pict $this->authenticateNewUser($drupal_user); - return $this->response; + return $this->getResponse(); } /** @@ -171,13 +171,13 @@ public function authenticateUser($name, $email, $provider_user_id, $token, $pict */ public function associateNewProvider($provider_user_id, $token, $data) { if ($this->userManager->addUserRecord($this->currentUser->id(), $provider_user_id, $token, $data)) { - $this->response = $this->getPostLoginRedirection(); + $this->setResponse($this->getPostLoginRedirection()); return; } $this->messenger->addError($this->t('New provider could not be associated.')); - $this->response = $this->getLoginFormRedirection(); + $this->setResponse($this->getLoginFormRedirection()); } /** @@ -266,7 +266,7 @@ public function authenticateExistingUser(UserInterface $drupal_user) { $this->nullifySessionKeys(); $this->messenger->addError($this->t('Authentication for Admin (user 1) is disabled.')); - $this->response = $this->getLoginFormRedirection(); + $this->setResponse($this->getLoginFormRedirection()); return; } @@ -276,20 +276,20 @@ public function authenticateExistingUser(UserInterface $drupal_user) { if ($disabled_role) { $this->messenger->addError($this->t("Authentication for '@role' role is disabled.", ['@role' => $disabled_role])); - $this->response = $this->getLoginFormRedirection(); + $this->setResponse($this->getLoginFormRedirection()); return; } // If user could be logged in. if ($this->loginUser($drupal_user)) { - $this->response = $this->getPostLoginRedirection(); + $this->setResponse($this->getPostLoginRedirection()); } else { $this->nullifySessionKeys(); $this->messenger->addError($this->t('Your account has not been approved yet or might have been canceled, please contact the administrator.')); - $this->response = $this->getLoginFormRedirection(); + $this->setResponse($this->getLoginFormRedirection()); } } @@ -309,7 +309,7 @@ public function authenticateNewUser(UserInterface $drupal_user = NULL) { $this->messenger->addWarning($this->t("Your account was created, but it needs administrator's approval.")); $this->nullifySessionKeys(); - $this->response = $this->getLoginFormRedirection(); + $this->setResponse($this->getLoginFormRedirection()); return; } @@ -320,12 +320,11 @@ public function authenticateNewUser(UserInterface $drupal_user = NULL) { $redirect = $this->redirectToUserForm($drupal_user); if ($redirect) { - $this->response = $redirect; + $this->setResponse($redirect); return; } - - $this->response = $this->getPostLoginRedirection(); + $this->setResponse($this->getPostLoginRedirection()); return; } @@ -336,8 +335,7 @@ public function authenticateNewUser(UserInterface $drupal_user = NULL) { } $this->nullifySessionKeys(); - - $this->response = $this->getLoginFormRedirection(); + $this->setResponse($this->getLoginFormRedirection()); } /** @@ -450,4 +448,14 @@ public function getResponse() { return $this->response; } + /** + * Sets the response. + * + * @var string + * Response + */ + public function setResponse($new_response) { + $this->response = $new_response; + } + } diff --git a/tests/src/Unit/SocialAuthUserTest.php b/tests/src/Unit/SocialAuthUserTest.php index dbdbb43..fe5c338 100644 --- a/tests/src/Unit/SocialAuthUserTest.php +++ b/tests/src/Unit/SocialAuthUserTest.php @@ -35,14 +35,14 @@ class SocialAuthUserTest extends UnitTestCase { /** * The tested Social Auth User. * - * @var \Drupal\social_auth\User\SocialAuthUser + * @var \Drupal\social_auth\User\SocialAuthUser|\PHPUnit_Framework_MockObject_MockObject */ protected $socialAuthUser; /** * The tested Social Auth UserManager. * - * @var \Drupal\social_auth\User\UserManager + * @var \Drupal\social_auth\User\UserManager|\PHPUnit_Framework_MockObject_MockObject */ protected $userManager; @@ -74,6 +74,34 @@ class SocialAuthUserTest extends UnitTestCase { */ protected $messenger; + /** + * The mocked Data Handler. + * + * @var \Drupal\social_auth\SocialAuthDataHandler + */ + protected $dataHandler; + + /** + * The mocked Config Factory. + * + * @var \Drupal\Core\Config\ConfigFactoryInterface + */ + protected $configFactory; + + /** + * The mocked Route Provider. + * + * @var \Drupal\Core\Routing\RouteProviderInterface + */ + protected $routeProvider; + + /** + * The mocked Event Dispatcher. + * + * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface + */ + protected $eventDispatcher; + /** * The test provider user id. * @@ -97,17 +125,17 @@ public function setUp() { $container = $this->createMock(ContainerInterface::class); \Drupal::setContainer($container); - $config_factory = $this->createMock(ConfigFactoryInterface::class); + $this->configFactory = $this->createMock(ConfigFactoryInterface::class); $this->currentUser = $this->createMock(AccountProxyInterface::class); - $data_handler = $this->createMock(SocialAuthDataHandler::class); + $this->dataHandler = $this->createMock(SocialAuthDataHandler::class); $entity_field_manager = $this->createMock(EntityFieldManagerInterface::class); $entity_type_manager = $this->createMock(EntityTypeManagerInterface::class); - $event_dispatcher = $this->createMock(EventDispatcherInterface::class); + $this->eventDispatcher = $this->createMock(EventDispatcherInterface::class); $file_system = $this->createMock(FileSystemInterface::class); $language_manager = $this->createMock(LanguageManagerInterface::class); $this->loggerFactory = $this->createMock(LoggerChannelFactoryInterface::class); $this->messenger = $this->createMock(MessengerInterface::class); - $route_provider = $this->createMock(RouteProviderInterface::class); + $this->routeProvider = $this->createMock(RouteProviderInterface::class); $token = $this->createMock(Token::class); $transliteration = $this->createMock(PhpTransliteration::class); @@ -133,11 +161,11 @@ public function setUp() { ->setConstructorArgs([$entity_type_manager, $this->messenger, $this->loggerFactory, - $config_factory, + $this->configFactory, $entity_field_manager, $transliteration, $language_manager, - $event_dispatcher, + $this->eventDispatcher, $token, $file_system, ]) @@ -153,10 +181,10 @@ public function setUp() { $this->messenger, $this->loggerFactory, $this->userManager, - $data_handler, - $config_factory, - $route_provider, - $event_dispatcher, + $this->dataHandler, + $this->configFactory, + $this->routeProvider, + $this->eventDispatcher, ]) ->setMethods(['getLoginFormRedirection', 'getPostLoginRedirection', @@ -466,8 +494,8 @@ public function testLoginUserActive() { * @covers Drupal\social_auth\User\UserAuthenticator::authenticateWithProvider */ public function testAuthenticateWithProviderSuccess() { + $this->prepareAuthenticateWithProvider(); $user = $this->createMock(User::class); - $redirect = new RedirectResponse('https://drupal.org/'); $this->userManager->expects($this->once()) ->method('loadUserByProperty') @@ -475,16 +503,8 @@ public function testAuthenticateWithProviderSuccess() { ->will($this->returnValue($user)); $this->userAuthenticator->expects($this->once()) - ->method('isAdminDisabled') - ->with($this->anything()) - ->will($this->returnValue(TRUE)); - - $this->messenger->expects($this->exactly(1)) - ->method('addError'); - - $this->userAuthenticator->expects($this->any()) - ->method('getLoginFormRedirection') - ->will($this->returnValue($redirect)); + ->method('authenticateExistingUser') + ->with($user); $this->assertTrue($this->userAuthenticator->authenticateWithProvider(12345)); @@ -538,6 +558,7 @@ public function testAuthenticateWithProviderException() { * @covers Drupal\social_auth\User\UserAuthenticator::authenticateNewUser */ public function testAuthenticateNewUserNotValid() { + $this->prepareAuthenticateNewUser(); $redirect = new RedirectResponse('https://drupal.org/'); $this->userAuthenticator->expects($this->once()) @@ -558,6 +579,7 @@ public function testAuthenticateNewUserNotValid() { * @covers Drupal\social_auth\User\UserAuthenticator::authenticateNewUser */ public function testAuthenticateNewUserValidApproveNeed() { + $this->prepareAuthenticateNewUser(); $redirect = new RedirectResponse('https://drupal.org/'); $user = $this->createMock(UserInterface::class); @@ -582,25 +604,18 @@ public function testAuthenticateNewUserValidApproveNeed() { * @covers Drupal\social_auth\User\UserAuthenticator::authenticateNewUser */ public function testAuthenticateNewUserValidFailLogin() { + $this->prepareAuthenticateNewUser(); + $redirect = new RedirectResponse('https://drupal.org/'); $user = $this->createMock(UserInterface::class); - $logger = $this->createMock(LoggerChannelInterface::class); - $user->expects($this->once()) - ->method('isActive') + $this->userAuthenticator->expects($this->once()) + ->method('isApprovalRequired') ->will($this->returnValue(FALSE)); - $this->loggerFactory->expects($this->once()) - ->method('get') - ->with($this->pluginId) - ->will($this->returnValue($logger)); - - $logger->expects($this->once()) - ->method('warning') - ->with($this->anything()); - $this->userAuthenticator->expects($this->once()) - ->method('isApprovalRequired') + ->method('loginUser') + ->with($user) ->will($this->returnValue(FALSE)); $this->userAuthenticator->expects($this->once()) @@ -622,6 +637,7 @@ public function testAuthenticateNewUserValidFailLogin() { * @covers Drupal\social_auth\User\UserAuthenticator::authenticateNewUser */ public function testAuthenticateNewUserValidLoginFormRedirect() { + $this->prepareAuthenticateNewUser(); $redirect = new RedirectResponse('https://drupal.org/'); $user = $this->createMock(UserInterface::class); @@ -629,13 +645,10 @@ public function testAuthenticateNewUserValidLoginFormRedirect() { ->method('isApprovalRequired') ->will($this->returnValue(FALSE)); - $user->expects($this->once()) - ->method('isActive') - ->will($this->returnValue(TRUE)); - $this->userAuthenticator->expects($this->once()) - ->method('userLoginFinalize') - ->with($user); + ->method('loginUser') + ->with($user) + ->will($this->returnValue(TRUE)); $this->userAuthenticator->expects($this->any()) ->method('redirectToUserForm') @@ -653,6 +666,7 @@ public function testAuthenticateNewUserValidLoginFormRedirect() { * @covers Drupal\social_auth\User\UserAuthenticator::authenticateNewUser */ public function testAuthenticateNewUserValidLoginPostRedirect() { + $this->prepareAuthenticateNewUser(); $redirect = new RedirectResponse('https://drupal.org/'); $user = $this->createMock(UserInterface::class); @@ -660,13 +674,10 @@ public function testAuthenticateNewUserValidLoginPostRedirect() { ->method('isApprovalRequired') ->will($this->returnValue(FALSE)); - $user->expects($this->once()) - ->method('isActive') - ->will($this->returnValue(TRUE)); - $this->userAuthenticator->expects($this->once()) - ->method('userLoginFinalize') - ->with($user); + ->method('loginUser') + ->with($user) + ->will($this->returnValue(TRUE)); $this->userAuthenticator->expects($this->any()) ->method('redirectToUserForm') @@ -688,24 +699,20 @@ public function testAuthenticateNewUserValidLoginPostRedirect() { * @covers Drupal\social_auth\User\UserAuthenticator::authenticateWithEmail */ public function testAuthenticateWithEmailAccountExist() { + $this->prepareAuthenticateWithEmailAccountExist(); $user = $this->createMock(User::class); - $redirect = new RedirectResponse('https://drupal.org/'); $this->userManager->expects($this->once()) ->method('loadUserByProperty') ->with('mail', 'test@gmail.com') ->will($this->returnValue($user)); - $this->userAuthenticator->expects($this->once()) - ->method('isAdminDisabled') - ->with($this->anything()) - ->will($this->returnValue(TRUE)); - - $this->messenger->expects($this->exactly(1)) - ->method('addError'); + $this->userManager->expects($this->once()) + ->method('addUserRecord') + ->with($this->anything()); - $this->userAuthenticator->expects($this->any()) - ->method('getLoginFormRedirection') - ->will($this->returnValue($redirect)); + $this->userAuthenticator->expects($this->once()) + ->method('authenticateExistingUser') + ->with($this->anything()); $this->assertTrue($this->userAuthenticator->authenticateWithEmail('test@gmail.com', $this->providerUserId, '2873dgAS', ['test'])); } @@ -716,6 +723,7 @@ public function testAuthenticateWithEmailAccountExist() { * @covers Drupal\social_auth\User\UserAuthenticator::authenticateWithEmail */ public function testAuthenticateWithEmailAccountNoExist() { + $this->prepareAuthenticateWithEmailAccountExist(); $logger = $this->createMock(LoggerChannelInterface::class); $this->userManager->expects($this->once()) ->method('loadUserByProperty') @@ -743,6 +751,7 @@ public function testAuthenticateWithEmailAccountNoExist() { * @covers Drupal\social_auth\User\UserAuthenticator::authenticateExistingUser */ public function testAuthenticateExistingUserAdminDisabled() { + $this->prepareAuthenticateExistingUser(); $user = $this->createMock(UserInterface::class); $redirect = new RedirectResponse('https://drupal.org/'); @@ -768,6 +777,7 @@ public function testAuthenticateExistingUserAdminDisabled() { * @covers Drupal\social_auth\User\UserAuthenticator::authenticateExistingUser */ public function testAuthenticateExistingUserRoleDisabled() { + $this->prepareAuthenticateExistingUser(); $user = $this->createMock(UserInterface::class); $redirect = new RedirectResponse('https://drupal.org/'); @@ -798,6 +808,7 @@ public function testAuthenticateExistingUserRoleDisabled() { * @covers Drupal\social_auth\User\UserAuthenticator::authenticateExistingUser */ public function testAuthenticateExistingUserSucessLogin() { + $this->prepareAuthenticateExistingUser(); $user = $this->createMock(UserInterface::class); $redirect = new RedirectResponse('https://drupal.org/'); @@ -811,13 +822,10 @@ public function testAuthenticateExistingUserSucessLogin() { ->with($user) ->will($this->returnValue(FALSE)); - $user->expects($this->once()) - ->method('isActive') - ->will($this->returnValue(TRUE)); - $this->userAuthenticator->expects($this->once()) - ->method('userLoginFinalize') - ->with($user); + ->method('loginUser') + ->with($user) + ->will($this->returnValue(TRUE)); $this->userAuthenticator->expects($this->any()) ->method('getPostLoginRedirection') @@ -834,8 +842,8 @@ public function testAuthenticateExistingUserSucessLogin() { * @covers Drupal\social_auth\User\UserAuthenticator::authenticateExistingUser */ public function testAuthenticateExistingUserFailureLogin() { + $this->prepareAuthenticateExistingUser(); $user = $this->createMock(UserInterface::class); - $logger = $this->createMock(LoggerChannelInterface::class); $redirect = new RedirectResponse('https://drupal.org/'); $this->userAuthenticator->expects($this->once()) @@ -848,19 +856,10 @@ public function testAuthenticateExistingUserFailureLogin() { ->with($user) ->will($this->returnValue(FALSE)); - $user->expects($this->once()) - ->method('isActive') + $this->userAuthenticator->expects($this->once()) + ->method('loginUser') ->will($this->returnValue(FALSE)); - $this->loggerFactory->expects($this->once()) - ->method('get') - ->with($this->pluginId) - ->will($this->returnValue($logger)); - - $logger->expects($this->once()) - ->method('warning') - ->with($this->anything()); - $this->messenger->expects($this->exactly(1)) ->method('addError'); @@ -879,6 +878,7 @@ public function testAuthenticateExistingUserFailureLogin() { * @covers Drupal\social_auth\User\UserAuthenticator::authenticateUser */ public function testAuthenticateUserNoRecord() { + $this->prepareAuthenticateUser(); $redirect = new RedirectResponse('https://drupal.org/'); $this->userManager->expects($this->once()) ->method('getDrupalUserId') @@ -889,16 +889,16 @@ public function testAuthenticateUserNoRecord() { ->method('isAuthenticated') ->will($this->returnValue(TRUE)); - $this->userManager->expects($this->any()) - ->method('addUserRecord') - ->with($this->currentUser->id(), $this->isType('string'), $this->isType('string'), $this->isType('array')) - ->will($this->returnValue(TRUE)); - $this->userAuthenticator->expects($this->any()) - ->method('getPostLoginRedirection') - ->will($this->returnValue($redirect)); + ->method('associateNewProvider') + ->with($this->anything()) + ->will($this->returnCallback(function () { + $redirect = new RedirectResponse('https://drupal.org/'); + $this->userAuthenticator->setResponse($redirect); + })); - $this->assertEquals($redirect, $this->userAuthenticator->authenticateUser('username', 'test@gmail.com', $this->providerUserId, 'S92xzuwssa2', 'https://www.drupal.org/files/styles/grid-2/public/default-avatar.png', ['data'])); + $this->userAuthenticator->authenticateUser('username', 'test@gmail.com', $this->providerUserId, 'S92xzuwssa2', 'https://www.drupal.org/files/styles/grid-2/public/default-avatar.png', ['data']); + $this->assertEquals($redirect, $this->userAuthenticator->getResponse()); } /** @@ -907,6 +907,7 @@ public function testAuthenticateUserNoRecord() { * @covers Drupal\social_auth\User\UserAuthenticator::authenticateUser */ public function testAuthenticateUserProviderAssociated() { + $this->prepareAuthenticateUser(); $redirect = new RedirectResponse('https://drupal.org/'); $this->userManager->expects($this->once()) ->method('getDrupalUserId') @@ -930,7 +931,7 @@ public function testAuthenticateUserProviderAssociated() { * @covers Drupal\social_auth\User\UserAuthenticator::authenticateUser */ public function testAuthenticateUserAuthProvider() { - $user = $this->createMock(UserInterface::class); + $this->prepareAuthenticateUser(); $redirect = new RedirectResponse('https://drupal.org/'); $this->userManager->expects($this->once()) @@ -942,24 +943,16 @@ public function testAuthenticateUserAuthProvider() { ->method('isAuthenticated') ->will($this->returnValue(FALSE)); - $this->userManager->expects($this->once()) - ->method('loadUserByProperty') - ->with('uid', 123456) - ->will($this->returnValue($user)); - $this->userAuthenticator->expects($this->once()) - ->method('isAdminDisabled') - ->with($user) - ->will($this->returnValue(TRUE)); - - $this->messenger->expects($this->exactly(1)) - ->method('addError'); - - $this->userAuthenticator->expects($this->any()) - ->method('getLoginFormRedirection') - ->will($this->returnValue($redirect)); + ->method('authenticateWithProvider') + ->with(123456) + ->will($this->returnCallback(function () { + $redirect = new RedirectResponse('https://drupal.org/'); + $this->userAuthenticator->setResponse($redirect); + })); - $this->assertEquals($redirect, $this->userAuthenticator->authenticateUser('username', 'test@gmail.com', $this->providerUserId, 'S92xzuwssa2', 'https://www.drupal.org/files/styles/grid-2/public/default-avatar.png', ['data'])); + $this->userAuthenticator->authenticateUser('username', 'test@gmail.com', $this->providerUserId, 'S92xzuwssa2', 'https://www.drupal.org/files/styles/grid-2/public/default-avatar.png', ['data']); + $this->assertEquals($redirect, $this->userAuthenticator->getResponse()); } /** @@ -968,7 +961,7 @@ public function testAuthenticateUserAuthProvider() { * @covers Drupal\social_auth\User\UserAuthenticator::authenticateUser */ public function testAuthenticateUserAuthEmail() { - $user = $this->createMock(User::class); + $this->prepareAuthenticateUser(); $redirect = new RedirectResponse('https://drupal.org/'); $this->userManager->expects($this->once()) @@ -980,24 +973,16 @@ public function testAuthenticateUserAuthEmail() { ->method('isAuthenticated') ->will($this->returnValue(FALSE)); - $this->userManager->expects($this->once()) - ->method('loadUserByProperty') - ->with('mail', 'test@gmail.com') - ->will($this->returnValue($user)); - $this->userAuthenticator->expects($this->once()) - ->method('isAdminDisabled') + ->method('authenticateWithEmail') ->with($this->anything()) - ->will($this->returnValue(TRUE)); - - $this->messenger->expects($this->exactly(1)) - ->method('addError'); - - $this->userAuthenticator->expects($this->any()) - ->method('getLoginFormRedirection') - ->will($this->returnValue($redirect)); + ->will($this->returnCallback(function () { + $redirect = new RedirectResponse('https://drupal.org/'); + $this->userAuthenticator->setResponse($redirect); + })); - $this->assertEquals($redirect, $this->userAuthenticator->authenticateUser('username', 'test@gmail.com', $this->providerUserId, 'S92xzuwssa2', 'https://www.drupal.org/files/styles/grid-2/public/default-avatar.png', ['data'])); + $this->userAuthenticator->authenticateUser('username', 'test@gmail.com', $this->providerUserId, 'S92xzuwssa2', 'https://www.drupal.org/files/styles/grid-2/public/default-avatar.png', ['data']); + $this->assertEquals($redirect, $this->userAuthenticator->getResponse()); } /** @@ -1006,6 +991,7 @@ public function testAuthenticateUserAuthEmail() { * @covers Drupal\social_auth\User\UserAuthenticator::authenticateUser */ public function testAuthenticateUserNewUser() { + $this->prepareAuthenticateUser(); $user = $this->createMock(UserInterface::class); $redirect = new RedirectResponse('https://drupal.org/'); @@ -1024,24 +1010,129 @@ public function testAuthenticateUserNewUser() { ->will($this->returnValue($user)); $this->userAuthenticator->expects($this->once()) - ->method('isApprovalRequired') - ->will($this->returnValue(FALSE)); + ->method('authenticateNewUser') + ->with($user) + ->will($this->returnCallback(function () { + $redirect = new RedirectResponse('https://drupal.org/'); + $this->userAuthenticator->setResponse($redirect); + })); - $user->expects($this->once()) - ->method('isActive') - ->will($this->returnValue(TRUE)); + $this->userAuthenticator->setPluginId($this->pluginId); + $this->userAuthenticator->authenticateUser('username', '', $this->providerUserId, 'S92xzuwssa2', 'https://www.drupal.org/files/styles/grid-2/public/default-avatar.png', ['data']); + $this->assertEquals($redirect, $this->userAuthenticator->getResponse()); + } - $this->userAuthenticator->expects($this->once()) - ->method('userLoginFinalize') - ->with($user); + /** + * UserAuthenticator with mocked methods for authenticateWithProvider tests. + */ + protected function prepareAuthenticateWithProvider() { + unset($this->userAuthenticator); + $this->userAuthenticator = $this->getMockBuilder(UserAuthenticator::class) + ->setConstructorArgs([$this->currentUser, + $this->messenger, + $this->loggerFactory, + $this->userManager, + $this->dataHandler, + $this->configFactory, + $this->routeProvider, + $this->eventDispatcher, + ]) + ->setMethods(['authenticateExistingUser']) + ->getMock(); + } - $this->userAuthenticator->expects($this->any()) - ->method('redirectToUserForm') - ->with($user) - ->will($this->returnValue($redirect)); + /** + * UserAuthenticator with mocked methods for authenticateNewUser tests. + */ + protected function prepareAuthenticateNewUser() { + unset($this->userAuthenticator); + $this->userAuthenticator = $this->getMockBuilder(UserAuthenticator::class) + ->setConstructorArgs([$this->currentUser, + $this->messenger, + $this->loggerFactory, + $this->userManager, + $this->dataHandler, + $this->configFactory, + $this->routeProvider, + $this->eventDispatcher, + ]) + ->setMethods(['loginUser', + 'isRegistrationDisabled', + 'isApprovalRequired', + 'getLoginFormRedirection', + 'redirectToUserForm', + 'getPostLoginRedirection', + ]) + ->getMock(); + } - $this->userAuthenticator->setPluginId($this->pluginId); - $this->assertEquals($redirect, $this->userAuthenticator->authenticateUser('username', '', $this->providerUserId, 'S92xzuwssa2', 'https://www.drupal.org/files/styles/grid-2/public/default-avatar.png', ['data'])); + /** + * UserAuthenticator with mocked methods for authenticateWithEmail tests. + */ + protected function prepareAuthenticateWithEmailAccountExist() { + unset($this->userAuthenticator); + $this->userAuthenticator = $this->getMockBuilder(UserAuthenticator::class) + ->setConstructorArgs([$this->currentUser, + $this->messenger, + $this->loggerFactory, + $this->userManager, + $this->dataHandler, + $this->configFactory, + $this->routeProvider, + $this->eventDispatcher, + ]) + ->setMethods(['addUserRecord', + 'authenticateExistingUser', + ]) + ->getMock(); + } + + /** + * UserAuthenticator with mocked methods for authenticateExistingUser tests. + */ + protected function prepareAuthenticateExistingUser() { + unset($this->userAuthenticator); + $this->userAuthenticator = $this->getMockBuilder(UserAuthenticator::class) + ->setConstructorArgs([$this->currentUser, + $this->messenger, + $this->loggerFactory, + $this->userManager, + $this->dataHandler, + $this->configFactory, + $this->routeProvider, + $this->eventDispatcher, + ]) + ->setMethods(['isAdminDisabled', + 'getLoginFormRedirection', + 'isUserRoleDisabled', + 'loginUser', + 'getPostLoginRedirection', + ]) + ->getMock(); + } + + /** + * UserAuthenticator with mocked methods for authenticateUser tests. + */ + protected function prepareAuthenticateUser() { + unset($this->userAuthenticator); + $this->userAuthenticator = $this->getMockBuilder(UserAuthenticator::class) + ->setConstructorArgs([$this->currentUser, + $this->messenger, + $this->loggerFactory, + $this->userManager, + $this->dataHandler, + $this->configFactory, + $this->routeProvider, + $this->eventDispatcher, + ]) + ->setMethods(['associateNewProvider', + 'getPostLoginRedirection', + 'authenticateWithProvider', + 'authenticateWithEmail', + 'authenticateNewUser', + ]) + ->getMock(); } } From dc829e8c9ffe559628b8ca197adcef03051e39a8 Mon Sep 17 00:00:00 2001 From: agrochal Date: Thu, 9 Jan 2020 13:53:52 +0100 Subject: [PATCH 6/8] Update SocialAuthUserTest.php --- tests/src/Unit/SocialAuthUserTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/src/Unit/SocialAuthUserTest.php b/tests/src/Unit/SocialAuthUserTest.php index fe5c338..f751ad9 100644 --- a/tests/src/Unit/SocialAuthUserTest.php +++ b/tests/src/Unit/SocialAuthUserTest.php @@ -49,7 +49,7 @@ class SocialAuthUserTest extends UnitTestCase { /** * The tested Social Auth UserAuthenticator. * - * @var \Drupal\social_auth\User\UserAuthenticator + * @var \Drupal\social_auth\User\UserAuthenticator|\PHPUnit_Framework_MockObject_MockObject */ protected $userAuthenticator; From 01c2580d9fc2ce991541faec60b26ad380791304 Mon Sep 17 00:00:00 2001 From: agrochal Date: Fri, 10 Jan 2020 12:59:57 +0100 Subject: [PATCH 7/8] Update SocialAuthUserTest.php --- tests/src/Unit/SocialAuthUserTest.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/tests/src/Unit/SocialAuthUserTest.php b/tests/src/Unit/SocialAuthUserTest.php index f751ad9..130d0bf 100644 --- a/tests/src/Unit/SocialAuthUserTest.php +++ b/tests/src/Unit/SocialAuthUserTest.php @@ -56,49 +56,49 @@ class SocialAuthUserTest extends UnitTestCase { /** * The mocked AccountProxyInterface. * - * @var \Drupal\Core\Session\AccountProxyInterface + * @var \Drupal\Core\Session\AccountProxyInterface|\PHPUnit_Framework_MockObject_MockObject */ protected $currentUser; /** * The mocked LoggerChannelFactoryInterface. * - * @var \Drupal\Core\Logger\LoggerChannelFactoryInterface + * @var \Drupal\Core\Logger\LoggerChannelFactoryInterface|\PHPUnit_Framework_MockObject_MockObject */ protected $loggerFactory; /** * The mocked Messenger service. * - * @var \Drupal\Core\Messenger\MessengerInterface + * @var \Drupal\Core\Messenger\MessengerInterface|\PHPUnit_Framework_MockObject_MockObject */ protected $messenger; /** * The mocked Data Handler. * - * @var \Drupal\social_auth\SocialAuthDataHandler + * @var \Drupal\social_auth\SocialAuthDataHandler|\PHPUnit_Framework_MockObject_MockObject */ protected $dataHandler; /** * The mocked Config Factory. * - * @var \Drupal\Core\Config\ConfigFactoryInterface + * @var \Drupal\Core\Config\ConfigFactoryInterface|\PHPUnit_Framework_MockObject_MockObject */ protected $configFactory; /** * The mocked Route Provider. * - * @var \Drupal\Core\Routing\RouteProviderInterface + * @var \Drupal\Core\Routing\RouteProviderInterface|\PHPUnit_Framework_MockObject_MockObject */ protected $routeProvider; /** * The mocked Event Dispatcher. * - * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface + * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface|\PHPUnit_Framework_MockObject_MockObject */ protected $eventDispatcher; From 3a75baa051cd82f210a4b5a32fc52de60da2a18b Mon Sep 17 00:00:00 2001 From: agrochal Date: Fri, 10 Jan 2020 15:33:20 +0100 Subject: [PATCH 8/8] Update SocialAuthUserTest.php --- tests/src/Unit/SocialAuthUserTest.php | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/tests/src/Unit/SocialAuthUserTest.php b/tests/src/Unit/SocialAuthUserTest.php index 130d0bf..1b26ff3 100644 --- a/tests/src/Unit/SocialAuthUserTest.php +++ b/tests/src/Unit/SocialAuthUserTest.php @@ -449,6 +449,7 @@ public function testCheckProviderIsAssociatedExist() { * @covers Drupal\social_auth\User\UserAuthenticator::loginUser */ public function testLoginUserNotActive() { + /** @var \Drupal\user\UserInterface||\PHPUnit\Framework\MockObject\MockObject $user */ $user = $this->createMock(UserInterface::class); $logger = $this->createMock(LoggerChannelInterface::class); $user->expects($this->once()) @@ -474,6 +475,7 @@ public function testLoginUserNotActive() { * @covers Drupal\social_auth\User\UserAuthenticator::loginUser */ public function testLoginUserActive() { + /** @var \Drupal\user\UserInterface||\PHPUnit\Framework\MockObject\MockObject $user */ $user = $this->createMock(UserInterface::class); $user->expects($this->once()) @@ -581,6 +583,7 @@ public function testAuthenticateNewUserNotValid() { public function testAuthenticateNewUserValidApproveNeed() { $this->prepareAuthenticateNewUser(); $redirect = new RedirectResponse('https://drupal.org/'); + /** @var \Drupal\user\UserInterface||\PHPUnit\Framework\MockObject\MockObject $user */ $user = $this->createMock(UserInterface::class); $this->userAuthenticator->expects($this->once()) @@ -607,6 +610,7 @@ public function testAuthenticateNewUserValidFailLogin() { $this->prepareAuthenticateNewUser(); $redirect = new RedirectResponse('https://drupal.org/'); + /** @var \Drupal\user\UserInterface||\PHPUnit\Framework\MockObject\MockObject $user */ $user = $this->createMock(UserInterface::class); $this->userAuthenticator->expects($this->once()) @@ -639,6 +643,7 @@ public function testAuthenticateNewUserValidFailLogin() { public function testAuthenticateNewUserValidLoginFormRedirect() { $this->prepareAuthenticateNewUser(); $redirect = new RedirectResponse('https://drupal.org/'); + /** @var \Drupal\user\UserInterface||\PHPUnit\Framework\MockObject\MockObject $user */ $user = $this->createMock(UserInterface::class); $this->userAuthenticator->expects($this->once()) @@ -668,6 +673,7 @@ public function testAuthenticateNewUserValidLoginFormRedirect() { public function testAuthenticateNewUserValidLoginPostRedirect() { $this->prepareAuthenticateNewUser(); $redirect = new RedirectResponse('https://drupal.org/'); + /** @var \Drupal\user\UserInterface||\PHPUnit\Framework\MockObject\MockObject $user */ $user = $this->createMock(UserInterface::class); $this->userAuthenticator->expects($this->once()) @@ -752,6 +758,7 @@ public function testAuthenticateWithEmailAccountNoExist() { */ public function testAuthenticateExistingUserAdminDisabled() { $this->prepareAuthenticateExistingUser(); + /** @var \Drupal\user\UserInterface||\PHPUnit\Framework\MockObject\MockObject $user */ $user = $this->createMock(UserInterface::class); $redirect = new RedirectResponse('https://drupal.org/'); @@ -778,6 +785,7 @@ public function testAuthenticateExistingUserAdminDisabled() { */ public function testAuthenticateExistingUserRoleDisabled() { $this->prepareAuthenticateExistingUser(); + /** @var \Drupal\user\UserInterface||\PHPUnit\Framework\MockObject\MockObject $user */ $user = $this->createMock(UserInterface::class); $redirect = new RedirectResponse('https://drupal.org/'); @@ -809,6 +817,7 @@ public function testAuthenticateExistingUserRoleDisabled() { */ public function testAuthenticateExistingUserSucessLogin() { $this->prepareAuthenticateExistingUser(); + /** @var \Drupal\user\UserInterface||\PHPUnit\Framework\MockObject\MockObject $user */ $user = $this->createMock(UserInterface::class); $redirect = new RedirectResponse('https://drupal.org/'); @@ -843,6 +852,7 @@ public function testAuthenticateExistingUserSucessLogin() { */ public function testAuthenticateExistingUserFailureLogin() { $this->prepareAuthenticateExistingUser(); + /** @var \Drupal\user\UserInterface||\PHPUnit\Framework\MockObject\MockObject $user */ $user = $this->createMock(UserInterface::class); $redirect = new RedirectResponse('https://drupal.org/'); @@ -992,6 +1002,7 @@ public function testAuthenticateUserAuthEmail() { */ public function testAuthenticateUserNewUser() { $this->prepareAuthenticateUser(); + /** @var \Drupal\user\UserInterface||\PHPUnit\Framework\MockObject\MockObject $user */ $user = $this->createMock(UserInterface::class); $redirect = new RedirectResponse('https://drupal.org/');