From 8cffc0b9d74f56060c9d8bded01763f5b4eb41c9 Mon Sep 17 00:00:00 2001 From: agrochal Date: Mon, 30 Dec 2019 14:19:50 +0100 Subject: [PATCH 01/10] Bug fix in UserManager.php and tests for it --- src/User/UserManager.php | 12 ++- tests/src/Unit/SocialPostUserTest.php | 141 ++++++++++++++++++++++++++ 2 files changed, 152 insertions(+), 1 deletion(-) create mode 100644 tests/src/Unit/SocialPostUserTest.php diff --git a/src/User/UserManager.php b/src/User/UserManager.php index 9b276a7..f2d4f45 100755 --- a/src/User/UserManager.php +++ b/src/User/UserManager.php @@ -210,6 +210,16 @@ public function addRecord($name, $provider_user_id, $token, $additional_data = N return FALSE; } + /** + * Gets the session keys. + * + * @return array + * The session keys array + */ + public function getSessionKeys() { + return $this->sessionKeys; + } + /** * Sets the session keys to nullify if user could not logged in. * @@ -226,7 +236,7 @@ public function setSessionKeysToNullify(array $session_keys) { public function nullifySessionKeys() { if (!empty($this->sessionKeys)) { array_walk($this->sessionKeys, function ($session_key) { - $this->dataHandler->set($this->dataHandler->getSessionPrefix() . $session_key, NULL); + $this->dataHandler->set($session_key, NULL); }); } } diff --git a/tests/src/Unit/SocialPostUserTest.php b/tests/src/Unit/SocialPostUserTest.php new file mode 100644 index 0000000..25bb67a --- /dev/null +++ b/tests/src/Unit/SocialPostUserTest.php @@ -0,0 +1,141 @@ +createMock(AccountProxy::class); + $entity_type_manager = $this->createMock(EntityTypeManagerInterface::class); + $logger_factory = $this->createMock(LoggerChannelFactoryInterface::class); + + $this->dataHandler = $this->getMockBuilder(SocialPostDataHandler::class) + ->disableOriginalConstructor() + ->setMethods(['get', 'set', 'getSessionPrefix']) + ->getMock(); + + $this->sessionKeys = []; + + $this->userManager = $this->getMockBuilder(UserManager::class) + ->setConstructorArgs([$entity_type_manager, + $current_user, + $this->dataHandler, + $logger_factory, + ]) + ->setMethods(NULL) + ->getMock(); + + } + + /** + * @covers Drupal\social_post\User\UserManager::setPluginId + */ + public function testSetPluginId() { + $this->assertEquals(NULL, $this->userManager->getPluginId()); + $this->userManager->setPluginId('social_post_test'); + $this->assertEquals('social_post_test', $this->userManager->getPluginId()); + } + + /** + * @covers Drupal\social_post\User\UserManager::getPluginId + */ + public function testGetPluginId() { + $this->userManager->setPluginId('social_post_test2'); + $this->assertEquals('social_post_test2', $this->userManager->getPluginId()); + } + + /** + * @covers Drupal\social_post\User\UserManager::getSessionKeys + */ + public function testGetSessionKeys() { + $sample_session = ['x1Sn2lPZZ' => 'ikSn2AZj3', 'pL2bxA2xz' => 'l2AYxbA9a']; + + $this->userManager->setSessionKeysToNullify(array_keys($sample_session)); + $this->assertEquals(array_keys($sample_session), $this->userManager->getSessionKeys()); + } + + /** + * @covers Drupal\social_post\User\UserManager::setSessionKeysToNullify + */ + public function testSetSessionKeysToNullify() { + $sample_session = ['x1Sn2lPZZ' => 'ikSn2AZj3', 'pL2bxA2xz' => 'l2AYxbA9a']; + + $this->assertNotEquals(array_keys($sample_session), $this->userManager->getSessionKeys()); + $this->userManager->setSessionKeysToNullify(array_keys($sample_session)); + $this->assertEquals(array_keys($sample_session), $this->userManager->getSessionKeys()); + } + + /** + * @covers Drupal\social_post\User\UserManager::nullifySessionKeys + */ + public function testNullifySessionKeys() { + $sample_session = ['x1Sn2lPZZ' => 'ikSn2AZj3']; + + $this->dataHandler->expects($this->any()) + ->method('getSessionPrefix') + ->will($this->returnCallback(function () { + return 'xB2g22_'; + })); + + $this->dataHandler->expects($this->any()) + ->method('get') + ->with($this->isType('string')) + ->will($this->returnCallback(function ($key) { + return $this->sessionKeys[$this->dataHandler->getSessionPrefix() . $key]; + })); + + $this->dataHandler->expects($this->any()) + ->method('set') + ->with($this->isType('string'), $this->anything()) + ->will($this->returnCallback(function ($key, $value) { + $this->sessionKeys[$this->dataHandler->getSessionPrefix() . $key] = $value; + })); + + $this->dataHandler->set('x1Sn2lPZZ', 'ikSn2AZj3'); + $this->assertEquals('ikSn2AZj3', $this->dataHandler->get('x1Sn2lPZZ')); + + $this->userManager->setSessionKeysToNullify(array_keys($sample_session)); + $this->assertEquals(array_keys($sample_session), $this->userManager->getSessionKeys()); + + $this->userManager->nullifySessionKeys(); + + $this->assertEquals(NULL, $this->dataHandler->get('x1Sn2lPZZ')); + } + +} From e862a674d11e92c733f2a6a3b98028a8439b2933 Mon Sep 17 00:00:00 2001 From: agrochal Date: Mon, 30 Dec 2019 17:38:46 +0100 Subject: [PATCH 02/10] Update SocialPostUserTest.php --- tests/src/Unit/SocialPostUserTest.php | 84 ++++++++++++++++++++++++--- 1 file changed, 75 insertions(+), 9 deletions(-) diff --git a/tests/src/Unit/SocialPostUserTest.php b/tests/src/Unit/SocialPostUserTest.php index 25bb67a..88382ca 100644 --- a/tests/src/Unit/SocialPostUserTest.php +++ b/tests/src/Unit/SocialPostUserTest.php @@ -2,9 +2,11 @@ namespace Drupal\Tests\social_post\Unit; +use Drupal\Core\Entity\EntityStorageInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Logger\LoggerChannelFactoryInterface; use Drupal\Core\Session\AccountProxy; +use Drupal\social_post\Entity\SocialPost; use Drupal\social_post\SocialPostDataHandler; use Drupal\social_post\User\UserManager; use Drupal\Tests\UnitTestCase; @@ -30,6 +32,13 @@ class SocialPostUserTest extends UnitTestCase { */ protected $dataHandler; + /** + * The mocked Entity Type Manager. + * + * @var \Drupal\Core\Entity\EntityTypeManagerInterface + */ + protected $entityTypeManager; + /** * The mocked array of the session keys. * @@ -37,12 +46,42 @@ class SocialPostUserTest extends UnitTestCase { */ protected $sessionKeys; + /** + * The test plugin id + * + * @var string + */ + protected $pluginId = 'social_post_test'; + + /** + * The test provider user id. + * + * @var string + */ + protected $provider_user_id = 'some_id'; + + /** + * The mocked Social Post + * + * @var \Drupal\social_post\Entity\SocialPost + */ + protected $socialPost; + /** * {@inheritdoc} */ public function setUp() { $current_user = $this->createMock(AccountProxy::class); - $entity_type_manager = $this->createMock(EntityTypeManagerInterface::class); + $this->userStorage = $this->createMock(EntityStorageInterface::class); + + $this->socialPost = $this->createMock(SocialPost::class); + + $this->entityTypeManager = $this->createMock(EntityTypeManagerInterface::class); + $this->entityTypeManager->expects($this->any()) + ->method('getStorage') + ->with('social_post') + ->will($this->returnValue($this->userStorage)); + $logger_factory = $this->createMock(LoggerChannelFactoryInterface::class); $this->dataHandler = $this->getMockBuilder(SocialPostDataHandler::class) @@ -52,14 +91,7 @@ public function setUp() { $this->sessionKeys = []; - $this->userManager = $this->getMockBuilder(UserManager::class) - ->setConstructorArgs([$entity_type_manager, - $current_user, - $this->dataHandler, - $logger_factory, - ]) - ->setMethods(NULL) - ->getMock(); + $this->userManager = new UserManager($this->entityTypeManager,$current_user,$this->dataHandler,$logger_factory); } @@ -138,4 +170,38 @@ public function testNullifySessionKeys() { $this->assertEquals(NULL, $this->dataHandler->get('x1Sn2lPZZ')); } + /** + * Tests the authenticate method with no account returned. + * + * @covers Drupal\social_post\User\UserManager::checkIfUserExists + */ + public function testCheckIfUserExistsWithNoUserReturned() { + $this->userStorage->expects($this->once()) + ->method('loadByProperties') + ->with(['plugin_id' => $this->pluginId, 'provider_user_id' => $this->provider_user_id]) + ->will($this->returnValue([])); + $this->userManager->setPluginId($this->pluginId); + $this->assertFalse($this->userManager->checkIfUserExists($this->provider_user_id)); + } + + /** + * Tests the authenticate method with account returned. + * + * @covers Drupal\social_post\User\UserManager::checkIfUserExists + */ + public function testCheckIfUserExistsWithUserReturned() { + $this->socialPost->expects($this->any()) + ->method('getUserId') + ->will($this->returnValue(97212)); + + $this->userStorage->expects($this->once()) + ->method('loadByProperties') + ->with(['plugin_id' => $this->pluginId, 'provider_user_id' => $this->provider_user_id]) + ->will($this->returnValue(array($this->socialPost))); + + $this->userManager->setPluginId($this->pluginId); + $this->assertEquals(97212, $this->userManager->checkIfUserExists($this->provider_user_id)); + } + + } From d21bb8c8a8d763b4accf5a6289b374fcd209d072 Mon Sep 17 00:00:00 2001 From: agrochal Date: Mon, 30 Dec 2019 18:10:58 +0100 Subject: [PATCH 03/10] Created tests for getAccountsByUserId and checkIfUserExists --- tests/src/Unit/SocialPostUserTest.php | 100 +++++++++++++++++--------- 1 file changed, 68 insertions(+), 32 deletions(-) diff --git a/tests/src/Unit/SocialPostUserTest.php b/tests/src/Unit/SocialPostUserTest.php index 88382ca..2a69d1d 100644 --- a/tests/src/Unit/SocialPostUserTest.php +++ b/tests/src/Unit/SocialPostUserTest.php @@ -47,21 +47,28 @@ class SocialPostUserTest extends UnitTestCase { protected $sessionKeys; /** - * The test plugin id + * The test plugin id. * * @var string */ protected $pluginId = 'social_post_test'; + /** + * The test user id. + * + * @var int + */ + protected $userId = 21353; + /** * The test provider user id. * * @var string */ - protected $provider_user_id = 'some_id'; + protected $providerUserId = 'some_id'; /** - * The mocked Social Post + * The mocked Social Post. * * @var \Drupal\social_post\Entity\SocialPost */ @@ -91,7 +98,7 @@ public function setUp() { $this->sessionKeys = []; - $this->userManager = new UserManager($this->entityTypeManager,$current_user,$this->dataHandler,$logger_factory); + $this->userManager = new UserManager($this->entityTypeManager, $current_user, $this->dataHandler, $logger_factory); } @@ -171,37 +178,66 @@ public function testNullifySessionKeys() { } /** - * Tests the authenticate method with no account returned. + * Tests the checkIfUserExists method with no account returned. + * + * @covers Drupal\social_post\User\UserManager::checkIfUserExists + */ + public function testCheckIfUserExistsWithNoUserReturned() { + $this->userStorage->expects($this->once()) + ->method('loadByProperties') + ->with(['plugin_id' => $this->pluginId, 'provider_user_id' => $this->providerUserId]) + ->will($this->returnValue([])); + $this->userManager->setPluginId($this->pluginId); + $this->assertFalse($this->userManager->checkIfUserExists($this->providerUserId)); + } + + /** + * Tests the checkIfUserExists method with account returned. * * @covers Drupal\social_post\User\UserManager::checkIfUserExists */ - public function testCheckIfUserExistsWithNoUserReturned() { - $this->userStorage->expects($this->once()) - ->method('loadByProperties') - ->with(['plugin_id' => $this->pluginId, 'provider_user_id' => $this->provider_user_id]) - ->will($this->returnValue([])); - $this->userManager->setPluginId($this->pluginId); - $this->assertFalse($this->userManager->checkIfUserExists($this->provider_user_id)); - } - - /** - * Tests the authenticate method with account returned. - * - * @covers Drupal\social_post\User\UserManager::checkIfUserExists - */ - public function testCheckIfUserExistsWithUserReturned() { - $this->socialPost->expects($this->any()) - ->method('getUserId') - ->will($this->returnValue(97212)); - - $this->userStorage->expects($this->once()) - ->method('loadByProperties') - ->with(['plugin_id' => $this->pluginId, 'provider_user_id' => $this->provider_user_id]) - ->will($this->returnValue(array($this->socialPost))); - - $this->userManager->setPluginId($this->pluginId); - $this->assertEquals(97212, $this->userManager->checkIfUserExists($this->provider_user_id)); - } + public function testCheckIfUserExistsWithUserReturned() { + $this->socialPost->expects($this->any()) + ->method('getUserId') + ->will($this->returnValue(97212)); + + $this->userStorage->expects($this->once()) + ->method('loadByProperties') + ->with(['plugin_id' => $this->pluginId, 'provider_user_id' => $this->providerUserId]) + ->will($this->returnValue([$this->socialPost])); + + $this->userManager->setPluginId($this->pluginId); + $this->assertEquals(97212, $this->userManager->checkIfUserExists($this->providerUserId)); + } + + /** + * Tests the getAccountsByUserId method with no account returned. + * + * @covers Drupal\social_post\User\UserManager::getAccountsByUserId + */ + public function testGetAccountsByUserIdWithNoAccountReturned() { + $this->userStorage->expects($this->once()) + ->method('loadByProperties') + ->with(['user_id' => $this->userId, 'plugin_id' => $this->pluginId]) + ->will($this->returnValue([])); + + $this->userManager->setPluginId($this->pluginId); + $this->assertEquals([], $this->userManager->getAccountsByUserId($this->userId)); + } + /** + * Tests the getAccountsByUserId method with account returned. + * + * @covers Drupal\social_post\User\UserManager::getAccountsByUserId + */ + public function testGetAccountsByUserIdWithAccountReturned() { + $this->userStorage->expects($this->once()) + ->method('loadByProperties') + ->with(['user_id' => $this->userId, 'plugin_id' => $this->pluginId]) + ->will($this->returnValue(['test'])); + + $this->userManager->setPluginId($this->pluginId); + $this->assertEquals(['test'], $this->userManager->getAccountsByUserId($this->userId)); + } } From 4c8c26e80b6206632c18126e2d755c583c8c5118 Mon Sep 17 00:00:00 2001 From: agrochal Date: Fri, 3 Jan 2020 02:06:47 +0100 Subject: [PATCH 04/10] Update SocialPostUserTest.php --- tests/src/Unit/SocialPostUserTest.php | 230 +++++++++++++++++++++++++- 1 file changed, 222 insertions(+), 8 deletions(-) diff --git a/tests/src/Unit/SocialPostUserTest.php b/tests/src/Unit/SocialPostUserTest.php index 2a69d1d..eed8ece 100644 --- a/tests/src/Unit/SocialPostUserTest.php +++ b/tests/src/Unit/SocialPostUserTest.php @@ -32,6 +32,13 @@ class SocialPostUserTest extends UnitTestCase { */ protected $dataHandler; + /** + * The mocked current logged in Drupal user. + * + * @var \Drupal\Core\Session\AccountProxy + */ + protected $currentUser; + /** * The mocked Entity Type Manager. * @@ -78,10 +85,11 @@ class SocialPostUserTest extends UnitTestCase { * {@inheritdoc} */ public function setUp() { - $current_user = $this->createMock(AccountProxy::class); - $this->userStorage = $this->createMock(EntityStorageInterface::class); + $this->currentUser = $this->createMock(AccountProxy::class); + $logger_factory = $this->createMock(LoggerChannelFactoryInterface::class); $this->socialPost = $this->createMock(SocialPost::class); + $this->userStorage = $this->createMock(EntityStorageInterface::class); $this->entityTypeManager = $this->createMock(EntityTypeManagerInterface::class); $this->entityTypeManager->expects($this->any()) @@ -89,8 +97,6 @@ public function setUp() { ->with('social_post') ->will($this->returnValue($this->userStorage)); - $logger_factory = $this->createMock(LoggerChannelFactoryInterface::class); - $this->dataHandler = $this->getMockBuilder(SocialPostDataHandler::class) ->disableOriginalConstructor() ->setMethods(['get', 'set', 'getSessionPrefix']) @@ -98,8 +104,14 @@ public function setUp() { $this->sessionKeys = []; - $this->userManager = new UserManager($this->entityTypeManager, $current_user, $this->dataHandler, $logger_factory); - + $this->userManager = $this->getMockBuilder(UserManager::class) + ->setConstructorArgs([$this->entityTypeManager, + $this->currentUser, + $this->dataHandler, + $logger_factory, + ]) + ->setMethods(NULL) + ->getMock(); } /** @@ -187,6 +199,7 @@ public function testCheckIfUserExistsWithNoUserReturned() { ->method('loadByProperties') ->with(['plugin_id' => $this->pluginId, 'provider_user_id' => $this->providerUserId]) ->will($this->returnValue([])); + $this->userManager->setPluginId($this->pluginId); $this->assertFalse($this->userManager->checkIfUserExists($this->providerUserId)); } @@ -234,10 +247,211 @@ public function testGetAccountsByUserIdWithAccountReturned() { $this->userStorage->expects($this->once()) ->method('loadByProperties') ->with(['user_id' => $this->userId, 'plugin_id' => $this->pluginId]) - ->will($this->returnValue(['test'])); + ->will($this->returnValue([$this->socialPost])); + + $this->userManager->setPluginId($this->pluginId); + $this->assertEquals([$this->socialPost], $this->userManager->getAccountsByUserId($this->userId)); + } + + /** + * @covers Drupal\social_post\User\UserManager::getCurrentUser + */ + public function testGetCurrentUser() { + $this->currentUser->expects($this->once()) + ->method('id') + ->will($this->returnValue(123)); + + $this->assertEquals(123, $this->userManager->getCurrentUser()); + } + + /** + * Tests the addRecord method when user exists. + * + * @covers Drupal\social_post\User\UserManager::addRecord + */ + public function testAddRecordExist() { + $this->socialPost->expects($this->any()) + ->method('getUserId') + ->will($this->returnValue(97212)); + + $this->currentUser->expects($this->once()) + ->method('id') + ->will($this->returnValue(456)); + + $this->userStorage->expects($this->once()) + ->method('loadByProperties') + ->with(['plugin_id' => $this->pluginId, 'provider_user_id' => $this->providerUserId]) + ->will($this->returnValue([$this->socialPost])); + + $this->userManager->setPluginId($this->pluginId); + + $this->assertFalse($this->userManager->addRecord('test', $this->providerUserId, 'f31a2f3SA', NULL)); + + } + + /** + * Tests the addRecord method user doesn't exist and new created successfully. + * + * @covers Drupal\social_post\User\UserManager::addRecord + */ + public function testAddRecordNoExistSuccess() { + $this->currentUser->expects($this->once()) + ->method('id') + ->will($this->returnValue(456)); + + $this->userStorage->expects($this->once()) + ->method('loadByProperties') + ->with(['plugin_id' => $this->pluginId, 'provider_user_id' => $this->providerUserId]) + ->will($this->returnValue([])); + + $this->userStorage->expects($this->once()) + ->method('create') + ->with($this->isType('array')) + ->will($this->returnValue($this->socialPost)); + + $this->socialPost->expects($this->once()) + ->method('setToken') + ->with($this->isType('string')); + + $this->socialPost->expects($this->once()) + ->method('save'); + + $this->userManager->setPluginId($this->pluginId); + $this->assertTrue($this->userManager->addRecord('test', $this->providerUserId, 'jnh3q3q', NULL)); + + } + + /** + * Tests the addRecord method user doesn't exist and failure creating new. + * + * @covers Drupal\social_post\User\UserManager::addRecord + */ + public function testAddRecordNoExistFailure() { + $this->currentUser->expects($this->once()) + ->method('id') + ->will($this->returnValue(456)); + + $this->userStorage->expects($this->once()) + ->method('loadByProperties') + ->with(['plugin_id' => $this->pluginId, 'provider_user_id' => $this->providerUserId]) + ->will($this->returnValue([])); + + $this->userStorage->expects($this->once()) + ->method('create') + ->with($this->isType('array')) + ->will($this->returnValue($this->socialPost)); + + $this->socialPost->expects($this->once()) + ->method('setToken') + ->with($this->isType('string')); + + $this->socialPost->expects($this->once()) + ->method('save') + ->will($this->returnCallback(function () { + unset($this->socialPost); + })); + + $this->userManager->setPluginId($this->pluginId); + $this->assertFalse($this->userManager->addRecord('test', $this->providerUserId, 'jnh3q3q', NULL)); + } + + /** + * Tests the updateToken method with no account returned. + * + * @covers Drupal\social_post\User\UserManager::updateToken + */ + public function testUpdateTokenWithNoAccountReturned() { + $this->userStorage->expects($this->once()) + ->method('loadByProperties') + ->with(['plugin_id' => $this->pluginId, 'provider_user_id' => $this->providerUserId]) + ->will($this->returnValue([])); + + $this->userManager->setPluginId($this->pluginId); + $this->assertFalse($this->userManager->updateToken($this->pluginId, $this->providerUserId, 'ba2w8gf2a68f')); + } + + /** + * Tests the updateToken method account returned and successfully updating. + * + * @covers Drupal\social_post\User\UserManager::updateToken + */ + public function testUpdateTokenWithAccountReturnedSuccess() { + $this->userStorage->expects($this->once()) + ->method('loadByProperties') + ->with(['plugin_id' => $this->pluginId, 'provider_user_id' => $this->providerUserId]) + ->will($this->returnValue([$this->socialPost])); + + $this->socialPost->expects($this->once()) + ->method('setToken') + ->with($this->isType('string')) + ->will($this->returnValue($this->socialPost)); + + $this->socialPost->expects($this->once()) + ->method('save'); + + $this->userManager->setPluginId($this->pluginId); + $this->assertTrue($this->userManager->updateToken($this->pluginId, $this->providerUserId, 'ba2w8gf2a68f')); + } + + /** + * Tests the updateToken method with account returned and failure updating. + * + * @covers Drupal\social_post\User\UserManager::updateToken + */ + public function testUpdateTokenWithAccountReturnedFailure() { + $this->userStorage->expects($this->once()) + ->method('loadByProperties') + ->with(['plugin_id' => $this->pluginId, 'provider_user_id' => $this->providerUserId]) + ->will($this->returnValue([$this->socialPost])); + + $this->socialPost->expects($this->once()) + ->method('setToken') + ->with($this->isType('string')) + ->will($this->returnValue($this->socialPost)); + + $this->socialPost->expects($this->once()) + ->method('save') + ->will($this->returnCallback(function () { + throw new \Exception('test'); + })); + + $this->userManager->setPluginId($this->pluginId); + $this->expectException("Exception"); + $this->assertFalse($this->userManager->updateToken($this->pluginId, $this->providerUserId, 'ba2w8gf2a68f')); + } + + /** + * Tests the getToken method with no account returned. + * + * @covers Drupal\social_post\User\UserManager::getToken + */ + public function testGetTokenWithNoAccountReturned() { + $this->userStorage->expects($this->once()) + ->method('loadByProperties') + ->with(['plugin_id' => $this->pluginId, 'provider_user_id' => $this->providerUserId]) + ->will($this->returnValue([])); + + $this->userManager->setPluginId($this->pluginId); + $this->assertNull($this->userManager->getToken($this->providerUserId)); + } + + /** + * Tests the getToken method with token returned. + * + * @covers Drupal\social_post\User\UserManager::getToken + */ + public function testGetTokenWithTokenReturned() { + $this->userStorage->expects($this->once()) + ->method('loadByProperties') + ->with(['plugin_id' => $this->pluginId, 'provider_user_id' => $this->providerUserId]) + ->will($this->returnValue([$this->socialPost])); + + $this->socialPost->expects($this->once()) + ->method('getToken') + ->will($this->returnValue('cn7a2ASh2')); $this->userManager->setPluginId($this->pluginId); - $this->assertEquals(['test'], $this->userManager->getAccountsByUserId($this->userId)); + $this->assertEquals('cn7a2ASh2', $this->userManager->getToken($this->providerUserId)); } } From 2904cc8eecfa105ec918fa3f438f069fc6e7c49b Mon Sep 17 00:00:00 2001 From: agrochal Date: Fri, 3 Jan 2020 10:55:56 +0100 Subject: [PATCH 05/10] Proposal --- src/User/UserManager.php | 13 ++++++++----- tests/src/Unit/SocialPostUserTest.php | 12 +----------- 2 files changed, 9 insertions(+), 16 deletions(-) diff --git a/src/User/UserManager.php b/src/User/UserManager.php index f2d4f45..ba1fcb2 100755 --- a/src/User/UserManager.php +++ b/src/User/UserManager.php @@ -198,13 +198,16 @@ public function addRecord($name, $provider_user_id, $token, $additional_data = N ]; $user_info = $this->entityTypeManager->getStorage('social_post')->create($values); - $user_info->setToken($token); + if ($user_info) { + $user_info->setToken($token); - // Saves the entity. - $user_info->save(); + // Saves the entity. + $user_info->save(); + + if ($user_info) { + return TRUE; + } - if ($user_info) { - return TRUE; } return FALSE; diff --git a/tests/src/Unit/SocialPostUserTest.php b/tests/src/Unit/SocialPostUserTest.php index eed8ece..a4fe07a 100644 --- a/tests/src/Unit/SocialPostUserTest.php +++ b/tests/src/Unit/SocialPostUserTest.php @@ -339,17 +339,7 @@ public function testAddRecordNoExistFailure() { $this->userStorage->expects($this->once()) ->method('create') ->with($this->isType('array')) - ->will($this->returnValue($this->socialPost)); - - $this->socialPost->expects($this->once()) - ->method('setToken') - ->with($this->isType('string')); - - $this->socialPost->expects($this->once()) - ->method('save') - ->will($this->returnCallback(function () { - unset($this->socialPost); - })); + ->will($this->returnValue(NULL)); $this->userManager->setPluginId($this->pluginId); $this->assertFalse($this->userManager->addRecord('test', $this->providerUserId, 'jnh3q3q', NULL)); From f2963de8cf60e0ee709550b56b0f5263137d2128 Mon Sep 17 00:00:00 2001 From: agrochal Date: Fri, 3 Jan 2020 17:29:32 +0100 Subject: [PATCH 06/10] try catch construction in addRecord function and test for it --- src/User/UserManager.php | 17 +++++++-- tests/src/Unit/SocialPostUserTest.php | 53 ++++++++++++++++++++++++++- 2 files changed, 64 insertions(+), 6 deletions(-) diff --git a/src/User/UserManager.php b/src/User/UserManager.php index ba1fcb2..7fc729f 100755 --- a/src/User/UserManager.php +++ b/src/User/UserManager.php @@ -198,19 +198,28 @@ public function addRecord($name, $provider_user_id, $token, $additional_data = N ]; $user_info = $this->entityTypeManager->getStorage('social_post')->create($values); - if ($user_info) { + + if (!$user_info) { + return FALSE; + } + + try { $user_info->setToken($token); // Saves the entity. $user_info->save(); - if ($user_info) { - return TRUE; - } + return TRUE; } + catch (\Exception $ex) { + $this->loggerFactory + ->get($this->getPluginId()) + ->error('Failed to add record. Exception: @message', ['@message' => $ex->getMessage()]); + } return FALSE; + } /** diff --git a/tests/src/Unit/SocialPostUserTest.php b/tests/src/Unit/SocialPostUserTest.php index a4fe07a..70ef8fb 100644 --- a/tests/src/Unit/SocialPostUserTest.php +++ b/tests/src/Unit/SocialPostUserTest.php @@ -5,6 +5,7 @@ use Drupal\Core\Entity\EntityStorageInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Logger\LoggerChannelFactoryInterface; +use Drupal\Core\Logger\LoggerChannelInterface; use Drupal\Core\Session\AccountProxy; use Drupal\social_post\Entity\SocialPost; use Drupal\social_post\SocialPostDataHandler; @@ -39,6 +40,13 @@ class SocialPostUserTest extends UnitTestCase { */ protected $currentUser; + /** + * The mocked LoggerChannelFactoryInterface. + * + * @var \Drupal\Core\Logger\LoggerChannelFactoryInterface + */ + protected $loggerFactory; + /** * The mocked Entity Type Manager. * @@ -87,7 +95,7 @@ class SocialPostUserTest extends UnitTestCase { public function setUp() { $this->currentUser = $this->createMock(AccountProxy::class); - $logger_factory = $this->createMock(LoggerChannelFactoryInterface::class); + $this->loggerFactory = $this->createMock(LoggerChannelFactoryInterface::class); $this->socialPost = $this->createMock(SocialPost::class); $this->userStorage = $this->createMock(EntityStorageInterface::class); @@ -108,7 +116,7 @@ public function setUp() { ->setConstructorArgs([$this->entityTypeManager, $this->currentUser, $this->dataHandler, - $logger_factory, + $this->loggerFactory, ]) ->setMethods(NULL) ->getMock(); @@ -345,6 +353,47 @@ public function testAddRecordNoExistFailure() { $this->assertFalse($this->userManager->addRecord('test', $this->providerUserId, 'jnh3q3q', NULL)); } + /** + * Tests the addRecord method user doesn't exist and exception while creating. + * + * @covers Drupal\social_post\User\UserManager::addRecord + */ + public function testAddRecordNoExistException() { + $logger = $this->createMock(LoggerChannelInterface::class); + $this->currentUser->expects($this->once()) + ->method('id') + ->will($this->returnValue(456)); + + $this->userStorage->expects($this->once()) + ->method('loadByProperties') + ->with(['plugin_id' => $this->pluginId, 'provider_user_id' => $this->providerUserId]) + ->will($this->returnValue([])); + + $this->userStorage->expects($this->once()) + ->method('create') + ->with($this->isType('array')) + ->will($this->returnValue($this->socialPost)); + + $this->socialPost->expects($this->once()) + ->method('setToken') + ->with($this->isType('string')) + ->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->userManager->setPluginId($this->pluginId); + $this->assertFalse($this->userManager->addRecord('test', $this->providerUserId, 'jnh3q3q', NULL)); + } + /** * Tests the updateToken method with no account returned. * From 83654be2be0e32cb86b39be582f5427cfd97c3b8 Mon Sep 17 00:00:00 2001 From: agrochal Date: Sat, 4 Jan 2020 17:37:38 +0100 Subject: [PATCH 07/10] Changed catching exception and added setup for addRecord --- src/User/UserManager.php | 4 +- tests/src/Unit/SocialPostUserTest.php | 94 +++++++++++++++++---------- 2 files changed, 60 insertions(+), 38 deletions(-) diff --git a/src/User/UserManager.php b/src/User/UserManager.php index 7fc729f..8a56e89 100755 --- a/src/User/UserManager.php +++ b/src/User/UserManager.php @@ -212,10 +212,10 @@ public function addRecord($name, $provider_user_id, $token, $additional_data = N return TRUE; } - catch (\Exception $ex) { + catch (EntityStorageException $ex) { $this->loggerFactory ->get($this->getPluginId()) - ->error('Failed to add record. Exception: @message', ['@message' => $ex->getMessage()]); + ->error('Failed to add a record for the user with id: @user_id. Exception: @message', ['@user_id' => $user_id, '@message' => $ex->getMessage()]); } return FALSE; diff --git a/tests/src/Unit/SocialPostUserTest.php b/tests/src/Unit/SocialPostUserTest.php index 70ef8fb..391659e 100644 --- a/tests/src/Unit/SocialPostUserTest.php +++ b/tests/src/Unit/SocialPostUserTest.php @@ -2,6 +2,7 @@ namespace Drupal\Tests\social_post\Unit; +use Drupal\Core\Entity\EntityStorageException; use Drupal\Core\Entity\EntityStorageInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Logger\LoggerChannelFactoryInterface; @@ -22,7 +23,7 @@ class SocialPostUserTest extends UnitTestCase { /** * The tested Social Post UserManager. * - * @var \Drupal\social_post\User\UserManager + * @var \Drupal\social_post\User\UserManager|\PHPUnit_Framework_MockObject_MockObject */ protected $userManager; @@ -278,21 +279,18 @@ public function testGetCurrentUser() { * @covers Drupal\social_post\User\UserManager::addRecord */ public function testAddRecordExist() { - $this->socialPost->expects($this->any()) - ->method('getUserId') - ->will($this->returnValue(97212)); + $this->prepareAddRecord(); - $this->currentUser->expects($this->once()) - ->method('id') - ->will($this->returnValue(456)); + $this->userManager->expects($this->once()) + ->method('getCurrentUser') + ->will($this->returnValue(12345)); - $this->userStorage->expects($this->once()) - ->method('loadByProperties') - ->with(['plugin_id' => $this->pluginId, 'provider_user_id' => $this->providerUserId]) - ->will($this->returnValue([$this->socialPost])); + $this->userManager->expects($this->once()) + ->method('checkIfUserExists') + ->with($this->providerUserId) + ->will($this->returnValue(12345)); $this->userManager->setPluginId($this->pluginId); - $this->assertFalse($this->userManager->addRecord('test', $this->providerUserId, 'f31a2f3SA', NULL)); } @@ -303,14 +301,16 @@ public function testAddRecordExist() { * @covers Drupal\social_post\User\UserManager::addRecord */ public function testAddRecordNoExistSuccess() { - $this->currentUser->expects($this->once()) - ->method('id') - ->will($this->returnValue(456)); + $this->prepareAddRecord(); - $this->userStorage->expects($this->once()) - ->method('loadByProperties') - ->with(['plugin_id' => $this->pluginId, 'provider_user_id' => $this->providerUserId]) - ->will($this->returnValue([])); + $this->userManager->expects($this->once()) + ->method('getCurrentUser') + ->will($this->returnValue(12345)); + + $this->userManager->expects($this->once()) + ->method('checkIfUserExists') + ->with($this->providerUserId) + ->will($this->returnValue(FALSE)); $this->userStorage->expects($this->once()) ->method('create') @@ -335,14 +335,16 @@ public function testAddRecordNoExistSuccess() { * @covers Drupal\social_post\User\UserManager::addRecord */ public function testAddRecordNoExistFailure() { - $this->currentUser->expects($this->once()) - ->method('id') - ->will($this->returnValue(456)); + $this->prepareAddRecord(); - $this->userStorage->expects($this->once()) - ->method('loadByProperties') - ->with(['plugin_id' => $this->pluginId, 'provider_user_id' => $this->providerUserId]) - ->will($this->returnValue([])); + $this->userManager->expects($this->once()) + ->method('getCurrentUser') + ->will($this->returnValue(12345)); + + $this->userManager->expects($this->once()) + ->method('checkIfUserExists') + ->with($this->providerUserId) + ->will($this->returnValue(FALSE)); $this->userStorage->expects($this->once()) ->method('create') @@ -359,15 +361,17 @@ public function testAddRecordNoExistFailure() { * @covers Drupal\social_post\User\UserManager::addRecord */ public function testAddRecordNoExistException() { + $this->prepareAddRecord(); $logger = $this->createMock(LoggerChannelInterface::class); - $this->currentUser->expects($this->once()) - ->method('id') - ->will($this->returnValue(456)); - $this->userStorage->expects($this->once()) - ->method('loadByProperties') - ->with(['plugin_id' => $this->pluginId, 'provider_user_id' => $this->providerUserId]) - ->will($this->returnValue([])); + $this->userManager->expects($this->once()) + ->method('getCurrentUser') + ->will($this->returnValue(12345)); + + $this->userManager->expects($this->once()) + ->method('checkIfUserExists') + ->with($this->providerUserId) + ->will($this->returnValue(FALSE)); $this->userStorage->expects($this->once()) ->method('create') @@ -376,9 +380,12 @@ public function testAddRecordNoExistException() { $this->socialPost->expects($this->once()) ->method('setToken') - ->with($this->isType('string')) + ->with($this->isType('string')); + + $this->socialPost->expects($this->once()) + ->method('save') ->will($this->returnCallback(function () { - throw new \Exception('test'); + throw new EntityStorageException('Message'); })); $this->loggerFactory->expects($this->once()) @@ -388,7 +395,7 @@ public function testAddRecordNoExistException() { $logger->expects($this->once()) ->method('error') - ->with($this->anything()); + ->with('Failed to add a record for the user with id: @user_id. Exception: @message'); $this->userManager->setPluginId($this->pluginId); $this->assertFalse($this->userManager->addRecord('test', $this->providerUserId, 'jnh3q3q', NULL)); @@ -493,4 +500,19 @@ public function testGetTokenWithTokenReturned() { $this->assertEquals('cn7a2ASh2', $this->userManager->getToken($this->providerUserId)); } + /** + * UserManager with mocked methods for addRecord tests. + */ + protected function prepareAddRecord() { + unset($this->userManager); + $this->userManager = $this->getMockBuilder(UserManager::class) + ->setConstructorArgs([$this->entityTypeManager, + $this->currentUser, + $this->dataHandler, + $this->loggerFactory, + ]) + ->setMethods(['getCurrentUser', 'checkIfUserExists']) + ->getMock(); + } + } From cf4a622837123fd71aeff7f926223f93ac4ef8db Mon Sep 17 00:00:00 2001 From: agrochal Date: Sun, 5 Jan 2020 20:17:34 +0100 Subject: [PATCH 08/10] Minor change --- tests/src/Unit/SocialPostUserTest.php | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/tests/src/Unit/SocialPostUserTest.php b/tests/src/Unit/SocialPostUserTest.php index 391659e..9acf2a1 100644 --- a/tests/src/Unit/SocialPostUserTest.php +++ b/tests/src/Unit/SocialPostUserTest.php @@ -384,9 +384,7 @@ public function testAddRecordNoExistException() { $this->socialPost->expects($this->once()) ->method('save') - ->will($this->returnCallback(function () { - throw new EntityStorageException('Message'); - })); + ->will($this->throwException(new EntityStorageException("Message"))); $this->loggerFactory->expects($this->once()) ->method('get') @@ -457,9 +455,7 @@ public function testUpdateTokenWithAccountReturnedFailure() { $this->socialPost->expects($this->once()) ->method('save') - ->will($this->returnCallback(function () { - throw new \Exception('test'); - })); + ->will($this->throwException(new \Exception("test"))); $this->userManager->setPluginId($this->pluginId); $this->expectException("Exception"); From f1d666f9967da028fefca8e2f36f282cdb0021ac Mon Sep 17 00:00:00 2001 From: agrochal Date: Sun, 5 Jan 2020 22:39:32 +0100 Subject: [PATCH 09/10] Changes requested in comments --- src/User/UserManager.php | 5 +++-- tests/src/Unit/SocialPostUserTest.php | 4 ---- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/src/User/UserManager.php b/src/User/UserManager.php index 8a56e89..c7cbc09 100755 --- a/src/User/UserManager.php +++ b/src/User/UserManager.php @@ -181,13 +181,14 @@ public function getCurrentUser() { * True if User record was created or False otherwise */ public function addRecord($name, $provider_user_id, $token, $additional_data = NULL) { - // Get User ID of logged in user. - $user_id = $this->getCurrentUser(); if ($this->checkIfUserExists($provider_user_id)) { return FALSE; } + // Get User ID of logged in user. + $user_id = $this->getCurrentUser(); + // Adds user record. $values = [ 'user_id' => $user_id, diff --git a/tests/src/Unit/SocialPostUserTest.php b/tests/src/Unit/SocialPostUserTest.php index 9acf2a1..15ae521 100644 --- a/tests/src/Unit/SocialPostUserTest.php +++ b/tests/src/Unit/SocialPostUserTest.php @@ -281,10 +281,6 @@ public function testGetCurrentUser() { public function testAddRecordExist() { $this->prepareAddRecord(); - $this->userManager->expects($this->once()) - ->method('getCurrentUser') - ->will($this->returnValue(12345)); - $this->userManager->expects($this->once()) ->method('checkIfUserExists') ->with($this->providerUserId) From 2c4c146c603420151cf63d67f2d508fb31612581 Mon Sep 17 00:00:00 2001 From: agrochal Date: Fri, 10 Jan 2020 15:25:35 +0100 Subject: [PATCH 10/10] Update SocialPostUserTest.php --- tests/src/Unit/SocialPostUserTest.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/src/Unit/SocialPostUserTest.php b/tests/src/Unit/SocialPostUserTest.php index 15ae521..3839f63 100644 --- a/tests/src/Unit/SocialPostUserTest.php +++ b/tests/src/Unit/SocialPostUserTest.php @@ -30,28 +30,28 @@ class SocialPostUserTest extends UnitTestCase { /** * The mocked Social Post Data Handler. * - * @var \Drupal\social_post\SocialPostDataHandler + * @var \Drupal\social_post\SocialPostDataHandler|\PHPUnit_Framework_MockObject_MockObject */ protected $dataHandler; /** * The mocked current logged in Drupal user. * - * @var \Drupal\Core\Session\AccountProxy + * @var \Drupal\Core\Session\AccountProxy|\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 Entity Type Manager. * - * @var \Drupal\Core\Entity\EntityTypeManagerInterface + * @var \Drupal\Core\Entity\EntityTypeManagerInterface|\PHPUnit_Framework_MockObject_MockObject */ protected $entityTypeManager; @@ -86,7 +86,7 @@ class SocialPostUserTest extends UnitTestCase { /** * The mocked Social Post. * - * @var \Drupal\social_post\Entity\SocialPost + * @var \Drupal\social_post\Entity\SocialPost|\PHPUnit_Framework_MockObject_MockObject */ protected $socialPost;