Skip to content
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

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/User/UserAuthenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,16 @@ public function getPluginId() {
return $this->pluginId;
}

/**
* 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.
*
Expand Down
169 changes: 169 additions & 0 deletions tests/src/Unit/SocialApiUserTest.php
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'
);
}
Copy link
Contributor

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


/**
* @covers Drupal\social_api\User\UserManager::setPluginId
*/
public function testSetPluginId() {
$this->assertEquals(NULL, $this->userManager->getPluginId());
$this->userManager->setPluginId('social_auth_mixer');
Copy link
Contributor

Choose a reason for hiding this comment

The 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');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'social_auth_test2'

$this->assertNotEquals('social_auth_reddit', $this->userManager->getPluginId());
Copy link
Contributor

Choose a reason for hiding this comment

The 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'
);
Copy link
Contributor

Choose a reason for hiding this comment

The 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());
Copy link
Contributor

Choose a reason for hiding this comment

The 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. assertNotEquals is not convenient since the session keys might be NULL and that's still no equal.

}

/**
* @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());
}

}
Copy link
Contributor

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.