-
Notifications
You must be signed in to change notification settings - Fork 6
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
Unit tests for User #11
Changes from 2 commits
fed2dde
d014a42
15f0f1d
4e46c13
06c9f24
d28f68a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
<?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; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function setUp() { | ||
$current_user = $this->createMock(AccountProxyInterface::class); | ||
$data_handler = $this->createMock(SocialApiDataHandler::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); | ||
|
||
$entity_type = 'users'; | ||
|
||
$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, | ||
$data_handler, | ||
]) | ||
->setMethods(NULL) | ||
->getMock(); | ||
|
||
} | ||
|
||
/** | ||
* Tests for class UserManager. | ||
*/ | ||
public function testUserManager() { | ||
$this->assertTrue( | ||
method_exists($this->userManager, 'getPluginId'), | ||
'UserManager class does not implements getPluginId function/method' | ||
); | ||
$this->assertTrue( | ||
method_exists($this->userManager, 'setPluginId'), | ||
'UserManager class does not implements setPluginId function/method' | ||
); | ||
$this->assertTrue( | ||
method_exists($this->userManager, 'getDrupalUserId'), | ||
'UserManager class does not implements getDrupalUserId function/method' | ||
); | ||
} | ||
|
||
/** | ||
* @covers Drupal\social_api\User\UserManager::setPluginId | ||
*/ | ||
public function testSetPluginId() { | ||
$this->assertEquals(NULL, $this->userManager->getPluginId()); | ||
$this->userManager->setPluginId('social_auth_mixer'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's change it to 'social_auth_test' |
||
$this->assertEquals('social_auth_mixer', $this->userManager->getPluginId()); | ||
} | ||
|
||
/** | ||
* @covers Drupal\social_api\User\UserManager::getPluginId | ||
*/ | ||
public function testGetPluginId() { | ||
$this->userManager->setPluginId('social_auth_mixer'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 'social_auth_test2' |
||
$this->assertNotEquals('social_auth_reddit', $this->userManager->getPluginId()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just test that you're getting the correct value (again) |
||
} | ||
|
||
/** | ||
* Tests for class UserAuthenticator. | ||
*/ | ||
public function testUserAuthenticator() { | ||
$this->assertTrue( | ||
method_exists($this->userAuthenticator, 'getPluginId'), | ||
'UserAuthenticator class does not implements getPluginId function/method' | ||
); | ||
$this->assertTrue( | ||
method_exists($this->userAuthenticator, 'setPluginId'), | ||
'UserAuthenticator class does not implements setPluginId function/method' | ||
); | ||
$this->assertTrue( | ||
method_exists($this->userAuthenticator, 'getSessionKeys'), | ||
'UserAuthenticator class does not implements getSessionKeys function/method' | ||
); | ||
$this->assertTrue( | ||
method_exists($this->userAuthenticator, 'setSessionKeysToNullify'), | ||
'UserAuthenticator class does not implements setSessionKeysToNullify function/method' | ||
); | ||
$this->assertTrue( | ||
method_exists($this->userAuthenticator, 'nullifySessionKeys'), | ||
'UserAuthenticator class does not implements nullifySessionKeys function/method' | ||
); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as what I mentioned before |
||
} | ||
|
||
/** | ||
* @covers Drupal\social_api\User\UserAuthenticator::setPluginId | ||
*/ | ||
public function testSetPluginIdAuthenticator() { | ||
$this->assertEquals(NULL, $this->userAuthenticator->getPluginId()); | ||
$this->userAuthenticator->setPluginId('social_auth_mixer'); | ||
$this->assertEquals('social_auth_mixer', $this->userAuthenticator->getPluginId()); | ||
} | ||
|
||
/** | ||
* @covers Drupal\social_api\User\UserAuthenticator::getPluginId | ||
*/ | ||
public function testGetPluginIdAuthenticator() { | ||
$this->userAuthenticator->setPluginId('social_auth_mixer'); | ||
$this->assertNotEquals('social_auth_reddit', $this->userAuthenticator->getPluginId()); | ||
} | ||
|
||
/** | ||
* @covers Drupal\social_api\User\UserAuthenticator::getSessionKeys | ||
*/ | ||
public function testGetSessionKeys() { | ||
$sample_session = ['h78323' => '78t2gq2g7q', 'pawdwadawd' => 'cbzhzxc']; | ||
$another_session = ['mpau82ass' => 'lplaw2b21', 'caholaaw' => '018y23czs']; | ||
|
||
$this->userAuthenticator->setSessionKeysToNullify($sample_session); | ||
$this->assertNotEquals($another_session, $this->userAuthenticator->getSessionKeys()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You're better off checking that what you set is what you get in this cases. |
||
} | ||
|
||
/** | ||
* @covers Drupal\social_api\User\UserAuthenticator::setSessionKeysToNullify | ||
*/ | ||
public function testSetSessionKeysToNullify() { | ||
$sample_session = ['h78323' => '78t2gq2g7q', 'pawdwadawd' => 'cbzhzxc']; | ||
|
||
$this->userAuthenticator->setSessionKeysToNullify($sample_session); | ||
$this->assertEquals($sample_session, $this->userAuthenticator->getSessionKeys()); | ||
} | ||
|
||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You might wanna add a test for |
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.
Unless the class is inheriting from an abstract class, we don't need to check this. I don't know if that's the case off the top of my head