-
Notifications
You must be signed in to change notification settings - Fork 6
Unit tests for User #11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
fed2dde
Create SocialApiUserTest.php
agr0chal d014a42
New tests and new function
agr0chal 15f0f1d
Update SocialApiUserTest.php
agr0chal 4e46c13
testNullifySessionKeys
agr0chal 06c9f24
Proposed refactor for the nullifySessionKeys
agr0chal d28f68a
Pass only keys from the array
agr0chal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,182 @@ | ||
| <?php | ||
|
|
||
| namespace Drupal\Tests\social_api\Unit; | ||
|
|
||
| use Drupal\Core\Entity\EntityTypeManagerInterface; | ||
| use Drupal\Core\Logger\LoggerChannelFactoryInterface; | ||
| use Drupal\Core\Messenger\MessengerInterface; | ||
| use Drupal\Core\Session\AccountProxyInterface; | ||
| use Drupal\social_api\SocialApiDataHandler; | ||
| use Drupal\social_api\User\UserAuthenticator; | ||
| use Drupal\social_api\User\UserManager; | ||
| use Drupal\social_api\User\UserManagerInterface; | ||
| use Drupal\Tests\UnitTestCase; | ||
|
|
||
| /** | ||
| * Tests social_api User. | ||
| * | ||
| * @group social_api | ||
| */ | ||
| class SocialApiUserTest extends UnitTestCase { | ||
|
|
||
| /** | ||
| * The tested Social Api UserManager. | ||
| * | ||
| * @var \Drupal\social_api\User\UserManager | ||
| */ | ||
| protected $userManager; | ||
|
|
||
| /** | ||
| * The tested Social Api UserAuthenticator. | ||
| * | ||
| * @var \Drupal\social_api\User\UserAuthenticator | ||
| */ | ||
| protected $userAuthenticator; | ||
|
|
||
| /** | ||
| * The mocked Social Api Data Handler. | ||
| * | ||
| * @var \Drupal\social_api\SocialApiDataHandler | ||
| */ | ||
| protected $dataHandler; | ||
|
|
||
| /** | ||
| * The mocked array of the session keys. | ||
| * | ||
| * @var array | ||
| */ | ||
| protected $sessionKeys; | ||
|
|
||
| /** | ||
| * {@inheritdoc} | ||
| */ | ||
| public function setUp() { | ||
| $current_user = $this->createMock(AccountProxyInterface::class); | ||
| $entity_type_manager = $this->createMock(EntityTypeManagerInterface::class); | ||
| $logger_factory = $this->createMock(LoggerChannelFactoryInterface::class); | ||
| $messenger = $this->createMock(MessengerInterface::class); | ||
| $user_manager = $this->createMock(UserManagerInterface::class); | ||
|
|
||
| $this->dataHandler = $this->getMockBuilder(SocialApiDataHandler::class) | ||
| ->disableOriginalConstructor() | ||
| ->setMethods(['get', 'set', 'getSessionPrefix']) | ||
| ->getMock(); | ||
|
|
||
| $entity_type = 'users'; | ||
|
|
||
| $this->sessionKeys = []; | ||
|
|
||
| $this->userManager = $this->getMockBuilder(UserManager::class) | ||
| ->setConstructorArgs([$entity_type, | ||
| $entity_type_manager, | ||
| $messenger, | ||
| $logger_factory, | ||
| ]) | ||
| ->setMethods(NULL) | ||
| ->getMock(); | ||
|
|
||
| $this->userAuthenticator = $this->getMockBuilder(UserAuthenticator::class) | ||
| ->setConstructorArgs([$current_user, | ||
| $messenger, | ||
| $logger_factory, | ||
| $user_manager, | ||
| $this->dataHandler, | ||
| ]) | ||
| ->setMethods(NULL) | ||
| ->getMock(); | ||
|
|
||
| } | ||
|
|
||
| /** | ||
| * @covers Drupal\social_api\User\UserManager::setPluginId | ||
| */ | ||
| public function testSetPluginId() { | ||
| $this->assertEquals(NULL, $this->userManager->getPluginId()); | ||
| $this->userManager->setPluginId('social_auth_test'); | ||
| $this->assertEquals('social_auth_test', $this->userManager->getPluginId()); | ||
| } | ||
|
|
||
| /** | ||
| * @covers Drupal\social_api\User\UserManager::getPluginId | ||
| */ | ||
| public function testGetPluginId() { | ||
| $this->userManager->setPluginId('social_auth_test2'); | ||
| $this->assertEquals('social_auth_test2', $this->userManager->getPluginId()); | ||
| } | ||
|
|
||
| /** | ||
| * @covers Drupal\social_api\User\UserAuthenticator::setPluginId | ||
| */ | ||
| public function testSetPluginIdAuthenticator() { | ||
| $this->assertEquals(NULL, $this->userAuthenticator->getPluginId()); | ||
| $this->userAuthenticator->setPluginId('social_auth_test'); | ||
| $this->assertEquals('social_auth_test', $this->userAuthenticator->getPluginId()); | ||
| } | ||
|
|
||
| /** | ||
| * @covers Drupal\social_api\User\UserAuthenticator::getPluginId | ||
| */ | ||
| public function testGetPluginIdAuthenticator() { | ||
| $this->userAuthenticator->setPluginId('social_auth_test2'); | ||
| $this->assertEquals('social_auth_test2', $this->userAuthenticator->getPluginId()); | ||
| } | ||
|
|
||
| /** | ||
| * @covers Drupal\social_api\User\UserAuthenticator::getSessionKeys | ||
| */ | ||
| public function testGetSessionKeys() { | ||
| $sample_session = ['h78323' => '78t2gq2g7q', 'pawdwadawd' => 'cbzhzxc']; | ||
|
|
||
| $this->userAuthenticator->setSessionKeysToNullify(array_keys($sample_session)); | ||
| $this->assertEquals(array_keys($sample_session), $this->userAuthenticator->getSessionKeys()); | ||
| } | ||
|
|
||
| /** | ||
| * @covers Drupal\social_api\User\UserAuthenticator::setSessionKeysToNullify | ||
| */ | ||
| public function testSetSessionKeysToNullify() { | ||
| $sample_session = ['h78323' => '78t2gq2g7q', 'pawdwadawd' => 'cbzhzxc']; | ||
|
|
||
| $this->assertNotEquals(array_keys($sample_session), $this->userAuthenticator->getSessionKeys()); | ||
| $this->userAuthenticator->setSessionKeysToNullify(array_keys($sample_session)); | ||
| $this->assertEquals(array_keys($sample_session), $this->userAuthenticator->getSessionKeys()); | ||
| } | ||
|
|
||
| /** | ||
| * @covers Drupal\social_api\User\UserAuthenticator::nullifySessionKeys | ||
| */ | ||
| public function testNullifySessionKeys() { | ||
| $sample_session = ['h78323' => '78t2gq2g7q']; | ||
|
|
||
| $this->dataHandler->expects($this->any()) | ||
| ->method('getSessionPrefix') | ||
| ->will($this->returnCallback(function () { | ||
| return 'xSn2ax_'; | ||
| })); | ||
|
|
||
| $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('h78323', '78t2gq2g7q'); | ||
| $this->assertEquals('78t2gq2g7q', $this->dataHandler->get('h78323')); | ||
|
|
||
| $this->userAuthenticator->setSessionKeysToNullify(array_keys($sample_session)); | ||
| $this->assertEquals(array_keys($sample_session), $this->userAuthenticator->getSessionKeys()); | ||
|
|
||
| $this->userAuthenticator->nullifySessionKeys(); | ||
|
|
||
| $this->assertEquals(NULL, $this->dataHandler->get('h78323')); | ||
| } | ||
|
|
||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You might wanna add a test for
nullifySessionKeys. That would probably require mocking the set and get methods of the data handler to use an arrray/map that you define in your test file.