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

Add tests for Social Auth #8

Open
wants to merge 43 commits into
base: 8.x-2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
86185e2
Add tests for class AuthManager and AuthManagerInterface
neerajp99 Jun 5, 2019
9a82acf
Add tests for class BeforeRedirectEvent
neerajp99 Jun 6, 2019
6d22cb0
Add tests for class FailedAuthenticationEvent
neerajp99 Jun 6, 2019
ce63a0c
Add tests for class SocialAuthEvents
neerajp99 Jun 6, 2019
173fb28
Add tests for class UserEvent
neerajp99 Jun 6, 2019
fb0979f
Add tests for class UserFieldsEvent
neerajp99 Jun 6, 2019
b8c5b45
Add tests for class SocialAuthDataHandler
neerajp99 Jun 6, 2019
c99601c
Add tests for class SettingsBase
neerajp99 Jun 6, 2019
333910d
Add tests for class SettingsInterface and edit class SocialAuthDataha…
neerajp99 Jun 6, 2019
91e1014
Add tests for Plugin
neerajp99 Jun 10, 2019
433bf77
Edit tests for AuthManager
neerajp99 Jun 10, 2019
ca4ffdc
Edit tests for Settings
neerajp99 Jun 10, 2019
f08797c
Edit tests for SocialAuthDataHandler
neerajp99 Jun 10, 2019
842ce6e
Modified tests for AuthManager
neerajp99 Jun 14, 2019
89708b1
Add tests for Controller
neerajp99 Jun 14, 2019
29cb45c
Add tests for class SocialAuth
neerajp99 Jun 14, 2019
5d09a70
Edit tests for AuthManager
neerajp99 Jun 16, 2019
e6eecdb
Edit tests for Controller
neerajp99 Jun 16, 2019
22609e1
Edit tests for Events
neerajp99 Jun 16, 2019
a3a4211
Edit tests for AuthManager
neerajp99 Jun 16, 2019
bdc5c46
Edit tests for Controller
neerajp99 Jun 16, 2019
cbb2e26
Edit tests for Entity
neerajp99 Jun 16, 2019
31dc508
Edit tests for class NetworkBase and SocialAuthLoginBlock
neerajp99 Jun 16, 2019
d0d7761
Edit tests for SettingsBase class
neerajp99 Jun 16, 2019
93b2d1a
remove extra test for class SocialAuthDataHandler
neerajp99 Jun 16, 2019
39f4f82
Fixed coding standard violations in AuthManager test
neerajp99 Jun 16, 2019
4bb4702
Fixed coding standard violations in Controller test
neerajp99 Jun 16, 2019
75de6bc
Fixed coding standard violations in EntityTest Class
neerajp99 Jun 16, 2019
c70a601
Fixed coding standard violations in Event
neerajp99 Jun 16, 2019
acb347f
Fixed coding standard violations in test class NetworkTest
neerajp99 Jun 16, 2019
af0f424
Fixed coding standard violations in SettingsTest class
neerajp99 Jun 16, 2019
05ff252
Edit test class EventTest
neerajp99 Jun 16, 2019
32d9ecb
Fixed typo in test class AuthManagerTest
neerajp99 Jun 17, 2019
f173f7b
Edit tests class EventTest
neerajp99 Jun 17, 2019
18992e0
Edit SocialAuth Controller tests
neerajp99 Jun 21, 2019
a368682
Edit test class SocialAuthControllerTest.php
neerajp99 Jun 21, 2019
77ba678
Edit test class SocialAuthEntityTest
neerajp99 Jun 21, 2019
25712dc
Edit test class SocialAuthEvent
neerajp99 Jun 21, 2019
e195896
Edit test class SocialAuthMaagerTest
neerajp99 Jun 21, 2019
368e5a4
Edit test class SocialAuthPluginTest
neerajp99 Jun 21, 2019
206b7cc
Edit test class SocialAuthSettingsTest
neerajp99 Jun 21, 2019
6e145cb
Reame the classNames and file
neerajp99 Jun 21, 2019
a4d3afc
Keep teh fork upto date
neerajp99 Jun 27, 2019
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
88 changes: 88 additions & 0 deletions tests/src/Unit/SocialAuthControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

use Drupal\Tests\UnitTestCase;
use Drupal\social_auth\Controller\SocialAuthController;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\social_api\Plugin\NetworkManager;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\social_auth\Controller\OAuth2ControllerBase;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\Render\RendererInterface;
use Drupal\social_auth\AuthManager\OAuth2ManagerInterface;
use Drupal\social_auth\SocialAuthDataHandler;
use Drupal\social_auth\User\UserAuthenticator;
use Symfony\Component\HttpFoundation\RequestStack;

/**
* Defines Controller Test Class.
*/
class SocialAuthControllerTest extends UnitTestCase {

/**
* Tests for class SocialAuthController.
*/
public function testSocialAuthController() {
$namespaces = $this->createMock(Traversable::class);
$cache_backend = $this->createMock(CacheBackendInterface::class);
$module_handler = $this->createMock(ModuleHandlerInterface::class);
$container = $this->createMock(ContainerInterface::class);

$networkManager = $this->getMockBuilder(NetworkManager::class)
->setConstructorArgs([$namespaces, $cache_backend, $module_handler])
->getMock();

$socialAuthController = $this->getMockBuilder(SocialAuthController::class)
->setConstructorArgs([$networkManager])
->setMethods(null)
->getMock();

$this->assertTrue(
method_exists($socialAuthController, 'setLoginButtonSettings'),
'SocialAuthController does not have setLoginButtonSettings function/method'
);

$this->assertTrue(
method_exists($socialAuthController, 'deleteLoginButtonSettings'),
'SocialAuthController does not have deleteLoginButtonSettings function/method'
);
}

/**
* Tests for class OAuth2ControllerBase.
*/
public function testOAuth2ControllerBase() {
$messenger = $this->createMock(MessengerInterface::class);
$network_manager = $this->createMock(NetworkManager::class);
$user_authenticator = $this->createMock(UserAuthenticator::class);
$provider_manager = $this->createMock(OAuth2ManagerInterface::class);
$data_handler = $this->createMock(SocialAuthDataHandler::class);
$renderer = $this->createMock(RendererInterface::class);
$request = $this->createMock(RequestStack::class);

$oAuth2ControllerBase = $this->getMockBuilder(OAuth2ControllerBase::class)
->setConstructorArgs(['moduleName',
'pluginId',
$messenger,
$network_manager,
$user_authenticator,
$provider_manager,
$request,
$data_handler,
$renderer,
])
->setMethods(null)
->getMock();

$this->assertTrue(
method_exists($oAuth2ControllerBase, 'processCallback'),
'OAuth2ControllerBase does not implements processCallback function/method'
);

$this->assertTrue(
method_exists($oAuth2ControllerBase, 'redirectToProvider'),
'OAuth2ControllerBase does not implements redirectToProvider function/method'
);
}

}
96 changes: 96 additions & 0 deletions tests/src/Unit/SocialAuthEntityTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php

use Drupal\Tests\UnitTestCase;
use Drupal\social_auth\Entity\SocialAuth;

/**
* Defines Test Class for Social Auth Entity.
*/
class SocialAuthEntityTest extends UnitTestCase {

/**
* Tests for class SocialAuth.
*/
public function testSocialAuth() {
$socialAuth = $this->createMock(SocialAuth::class);

$socialAuth->method('setAdditionalData')
->with('data')
->will($this->returnValue('additionalData'));

$socialAuth->method('setToken')
->with('token')
->will($this->returnValue('tokenEntity'));

$socialAuth->method('setCreatedTime')
->with(1560161171)
->will($this->returnValue('2019-06-10T10:06:11+00:00'));

$socialAuth->method('setChangedTime')
->with(1560161171)
->will($this->returnValue('2019-06-10T10:06:11+00:00'));

$socialAuth->method('getAdditionalData')
->will($this->returnValue($socialAuth->setAdditionalData('data')));

$socialAuth->method('getToken')
->will($this->returnValue($socialAuth->setToken('token')));

$socialAuth->method('getCreatedTime')
->will($this->returnValue($socialAuth->setCreatedTime(1560161171)));

$socialAuth->method('getChangedTime')
->will($this->returnValue($socialAuth->setChangedTime(1560161171)));

$this->assertTrue(
method_exists($socialAuth, 'getUserId'),
'SocialAuth does not implements getUserId function/method'
);

$this->assertTrue(
method_exists($socialAuth, 'setAdditionalData'),
'SocialAuth does not implements setAdditionalData function/method'
);

$this->assertTrue(
method_exists($socialAuth, 'getAdditionalData'),
'SocialAuth does not implements getAdditionalData function/method'
);

$this->assertTrue(
method_exists($socialAuth, 'setToken'),
'SocialAuth does not implements setToken function/method'
);

$this->assertTrue(
method_exists($socialAuth, 'getToken'),
'SocialAuth does not implements getToken function/method'
);

$this->assertTrue(
method_exists($socialAuth, 'setCreatedTime'),
'SocialAuth does not implements setCreatedTime function/method'
);

$this->assertTrue(
method_exists($socialAuth, 'getCreatedTime'),
'SocialAuth does not implements getCreatedTime function/method'
);

$this->assertTrue(
method_exists($socialAuth, 'setChangedTime'),
'SocialAuth does not implements setChangedTime function/method'
);

$this->assertTrue(
method_exists($socialAuth, 'getChangedTime'),
'SocialAuth does not implements getChangedTime function/method'
);

$this->assertEquals('additionalData', $socialAuth->getAdditionalData());
$this->assertEquals('tokenEntity', $socialAuth->getToken());
$this->assertSame('2019-06-10T10:06:11+00:00', $socialAuth->getCreatedTime());
$this->assertEquals('2019-06-10T10:06:11+00:00', $socialAuth->getChangedTime());
}

}
206 changes: 206 additions & 0 deletions tests/src/Unit/SocialAuthEventTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
<?php

use Drupal\Tests\UnitTestCase;
use Drupal\social_auth\Event\BeforeRedirectEvent;
use Drupal\social_auth\SocialAuthDataHandler;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Drupal\social_auth\Event\FailedAuthenticationEvent;
use Drupal\social_auth\Event\SocialAuthEvents;
use Drupal\user\UserInterface;
use Drupal\social_auth\Event\UserEvent;
use Drupal\social_auth\Event\UserFieldsEvent;

/**
* Defines Test class for Event.
*/
class SocialAuthEventTest extends UnitTestCase {

/**
* Tests for class testBeforeRedirectEvent.
*/
public function testBeforeRedirectEvent() {

$pluginId = 'drupal123';
$destination = 'drupal123';
$data_handler = $this->createMock(SocialAuthDataHandler::class);

$beforeRedirectEvent = $this->getMockBuilder(BeforeRedirectEvent::class)
->setConstructorArgs([$data_handler,
$pluginId,
$destination,
])
->setMethods(null)
->getMock();


$this->assertTrue(
method_exists($beforeRedirectEvent, 'getDataHandler'),
'BeforeRedirectEvent class does not implements getDataHandler function/method'
);

$this->assertTrue(
method_exists($beforeRedirectEvent, 'getPluginId'),
'BeforeRedirectEvent class does not implements getPluginId function/method'
);

$this->assertTrue(
method_exists($beforeRedirectEvent, 'getDestination'),
'BeforeRedirectEvent class does not implements getDestination function/method'
);

$this->assertSame('drupal123', $beforeRedirectEvent->getPluginId());
$this->assertSame('drupal123', $beforeRedirectEvent->getDestination());
$this->assertEquals($data_handler, $beforeRedirectEvent->getDataHandler());
}

/**
* Tests for class FailedAuthenticationEvent.
*/
public function testFailedAuthenticationEvent() {

$error = "error404";
$pluginId = 'drupal123';
$data_handler = $this->createMock(SocialAuthDataHandler::class);
$response = $this->createMock(RedirectResponse::class);

$failedAuthenticationEvent = $this->getMockBuilder(FailedAuthenticationEvent::class)
->setConstructorArgs([$data_handler,
$pluginId,
$error,
])
->setMethods(null)
->getMock();

// parent::__construct($data_handler, $pluginId, $error);.
$failedAuthenticationEvent->setResponse($response);

$this->assertTrue(
method_exists($failedAuthenticationEvent, 'getDataHandler'),
'FailedAuthenticationEvent class does not implements getDataHandler function/method'
);

$this->assertTrue(
method_exists($failedAuthenticationEvent, 'getPluginId'),
'FailedAuthenticationEvent class does not implements getPluginId function/method'
);
$this->assertTrue(
method_exists($failedAuthenticationEvent, 'getError'),
'FailedAuthenticationEvent class does not implements getError function/method'
);

$this->assertTrue(
method_exists($failedAuthenticationEvent, 'getResponse'),
'FailedAuthenticationEvent class does not implements getResponse function/method'
);
$this->assertTrue(
method_exists($failedAuthenticationEvent, 'setResponse'),
'FailedAuthenticationEvent class does not implements setResponse function/method'
);

$this->assertTrue(
method_exists($failedAuthenticationEvent, 'hasResponse'),
'FailedAuthenticationEvent class does not implements hasResponse function/method'
);

$this->assertEquals($response, $failedAuthenticationEvent->getResponse());
$this->assertTrue($failedAuthenticationEvent->hasResponse());
$this->assertEquals('error404', $failedAuthenticationEvent->getError());
$this->assertEquals('drupal123', $failedAuthenticationEvent->getPluginId());
$this->assertEquals(TRUE, $failedAuthenticationEvent->hasResponse());
$this->assertEquals($data_handler, $failedAuthenticationEvent->getDataHandler());
}

/**
* Tests for class SocialAuthEvents.
*/
public function testSocialAuthEvents() {
$reflection = new ReflectionClass(SocialAuthEvents::class);

$user_fields = $reflection->getConstant('USER_FIELDS');
$user_created = $reflection->getConstant('USER_CREATED');
$user_login = $reflection->getConstant('USER_LOGIN');
$user_redirect = $reflection->getConstant('BEFORE_REDIRECT');
$faield_auth = $reflection->getConstant('FAILED_AUTH');

$this->assertEquals('social_auth.user.fields',
$reflection->getConstant('USER_FIELDS'),
'The constant values is not matched');

$this->assertEquals('social_auth.user.created',
$reflection->getConstant('USER_CREATED'),
'The constant values is not matched');

$this->assertEquals('social_auth.user.login',
$reflection->getConstant('USER_LOGIN'),
'The constant values is not matched');

$this->assertEquals('social_auth.before_redirect',
$reflection->getConstant('BEFORE_REDIRECT'),
'The constant values is not matched');

$this->assertEquals('social_auth.failed_authentication',
$reflection->getConstant('FAILED_AUTH'),
'The constant values is not matched');
}

/**
* Tests for class UserEvent.
*/
public function testUserEvent() {
$pluginId = 'drupal123';
$user = $this->createMock(UserInterface::class);

$userEvent = $this->getMockBuilder(UserEvent::class)
->setConstructorArgs([$user, $pluginId])
->setMethods(null)
->getMock();

$this->assertTrue(
method_exists($userEvent, 'getPluginId'),
'UserEvent class does not implements getPluginId function/method'
);

$this->assertTrue(
method_exists($userEvent, 'getUser'),
'UserEvent class does not implements getUser function/method'
);

$this->assertSame('drupal123', $userEvent->getPluginId());
$this->assertEquals($user, $userEvent->getUser());
}

/**
* Tests for class UserFieldsEvent.
*/
public function testUserFieldsEvent() {
// $this->user = $this->createMock(UserInterface::class);.
$pluginId = 'drupal123';
$user_fields = ['userfield', 'userfield2'];

$userFieldsEvent = $this->getMockBuilder(UserFieldsEvent::class)
->setConstructorArgs([$user_fields, $pluginId])
->setMethods(null)
->getMock();

$userFieldsEvent->setUserFields($user_fields);

$this->assertTrue(
method_exists($userFieldsEvent, 'getUserFields'),
'UserFieldsEvent does not implements getUserFields function/method'
);

$this->assertTrue(
method_exists($userFieldsEvent, 'setUserFields'),
'UserFieldsEvent does not implements setUserFields function/method'
);

$this->assertTrue(
method_exists($userFieldsEvent, 'getPluginId'),
'UserFieldsEvent does not implements getPluginId function/method'
);

$this->assertSame('drupal123', $userFieldsEvent->getPluginId());
$this->assertEquals($user_fields, $userFieldsEvent->getUserFields());
}

}
Loading