From 86185e2e1041beabbdee4e96f642a2d571407c76 Mon Sep 17 00:00:00 2001 From: neerajp99 Date: Thu, 6 Jun 2019 03:35:42 +0530 Subject: [PATCH 01/42] Add tests for class AuthManager and AuthManagerInterface --- tests/src/Unit/AuthManagerTest.php | 116 +++++++++++++++++++++++++++++ 1 file changed, 116 insertions(+) create mode 100644 tests/src/Unit/AuthManagerTest.php diff --git a/tests/src/Unit/AuthManagerTest.php b/tests/src/Unit/AuthManagerTest.php new file mode 100644 index 0000000..7a9de08 --- /dev/null +++ b/tests/src/Unit/AuthManagerTest.php @@ -0,0 +1,116 @@ +logger_factory = $this->createMock(LoggerChannelFactoryInterface::class); + $this->settings = $this->createMock(Config::class); + $this->collection = $this->getMockBuilder('Drupal\social_auth\AuthManager\OAuth2Manager') + ->setConstructorArgs(array($this->settings, $this->logger_factory)) + ->setMethods(['getScopes', 'getEndPoints']) + ->getMockForAbstractClass(); + $this->collections = $this->getMock(OAuth2ManagerInterface::class); + // enable any other required module + $this->scopes = "drupal123"; + $this->endPoints = "drupal123"; + parent::setUp(); + } + + + public function testOAuth2Manager () { + $this->assertTrue( + method_exists($this->collection, 'getExtraDetails'), + 'OAuth2Manager does not have getExtraDetails function/method' + ); + $this->assertTrue( + method_exists($this->collection, 'getScopes'), + 'OAuth2Manager does not have getScopes function/method' + ); + $this->assertTrue( + method_exists($this->collection, 'getEndPoints'), + 'OAuth2Manager does not have ggetEndPoints function/method' + ); + + $this->collection->method('getScopes') + ->willReturn($this->scopes); + if ($this->scopes === FALSE){ + $this->scopes = $this->settings->get('scopes'); + } + $this->assertSame('drupal123', $this->collection->getScopes()); + + $this->collection->method('getEndPoints') + ->willReturn($this->endPoints); + if ($this->endPoints === FALSE) { + $this->endPoints = $this->settings->get('endpoints'); + } + $this->assertSame('drupal123', $this->collection->getEndPoints()); + } + + public function testGetExtraDetails () { + $endpoints = $this->collection->getEndPoints(); + $data = []; + $this->assertEmpty($data); + if ($endpoints) { + // Iterates through endpoints define in settings and retrieves them. + foreach (explode(PHP_EOL, $endpoints) as $endpoint) { + // Endpoint is set as path/to/endpoint|name. + $parts = explode('|', $endpoint); + $data[$parts[1]] = $this->collection->requestEndPoint($method, $parts[0], $domain); + } + return json_encode($data); + } + } + + public function testOAuth2ManagerInterface () { + $this->assertTrue( + method_exists($this->collections, 'getExtraDetails'), + 'OAuth2ManagerInterface does not have getExtraDetails function/method' + ); + $this->assertTrue( + method_exists($this->collections, 'requestEndPoint'), + 'OAuth2ManagerInterface does not have requestEndPoint function/method' + ); + $this->assertTrue( + method_exists($this->collections, 'getScopes'), + 'OAuth2ManagerInterface does not have getScopes function/method' + ); + $this->assertTrue( + method_exists($this->collections, 'getEndPoints'), + 'OAuth2ManagerInterface does not have getEndPoints function/method' + ); + $options = array(); + $this->collections->requestEndPoint($this->method, $this->path, $domain = NULL, $options = []); + } +} + + ?> From 9a82acf04e9235e0dae2ca92b8ddae4c3afe95ba Mon Sep 17 00:00:00 2001 From: neerajp99 Date: Thu, 6 Jun 2019 21:13:03 +0530 Subject: [PATCH 02/42] Add tests for class BeforeRedirectEvent --- tests/src/Unit/EventTest.php | 68 ++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 tests/src/Unit/EventTest.php diff --git a/tests/src/Unit/EventTest.php b/tests/src/Unit/EventTest.php new file mode 100644 index 0000000..fd9920c --- /dev/null +++ b/tests/src/Unit/EventTest.php @@ -0,0 +1,68 @@ +data_handler = $this->createMock(SocialAuthDataHandler::class); + $collection = $this->getMockBuilder('Drupal\social_auth\Event\BeforeRedirectEvent') + ->setConstructorArgs(array($this->data_handler, $this->pluginId, $this->destination)) + ->setMethods(array('getDataHandler', 'getPluginId', 'getDestination')) + ->getMock(); + + $this->assertTrue( + method_exists($collection, 'getDataHandler'), + 'BeforeRedirectEvent does not have getDataHandler function/method' + ); + $this->assertTrue( + method_exists($collection, 'getPluginId'), + 'BeforeRedirectEvent does not have getPluginId function/method' + ); + $this->assertTrue( + method_exists($collection, 'getDestination'), + 'BeforeRedirectEvent does not have getDestination function/method' + ); + $collection->method('getPluginId') + ->willReturn($this->pluginId); + $collection->method('getDestination') + ->willReturn($this->destination); + $collection->method('getDataHandler') + ->willReturn($this->data_handler); + $this->assertSame('drupal123', $collection->getPluginId()); + $this->assertSame('drupal123', $collection->getDestination()); + } + +} + + + ?> From 6d22cb069c06b00b280c2cf8503b0beba0e012e6 Mon Sep 17 00:00:00 2001 From: neerajp99 Date: Thu, 6 Jun 2019 23:43:33 +0530 Subject: [PATCH 03/42] Add tests for class FailedAuthenticationEvent --- tests/src/Unit/EventTest.php | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/tests/src/Unit/EventTest.php b/tests/src/Unit/EventTest.php index fd9920c..2ff3bc0 100644 --- a/tests/src/Unit/EventTest.php +++ b/tests/src/Unit/EventTest.php @@ -5,6 +5,8 @@ use Drupal\social_auth\Event\BeforeRedirectEvent; use Drupal\social_auth\SocialAuthDataHandler; use Symfony\Component\EventDispatcher\Event; +use Symfony\Component\HttpFoundation\RedirectResponse; +use Drupal\social_auth\Event\FailedAuthenticationEvent; @@ -12,6 +14,8 @@ class EventTest extends UnitTestCase { protected $data_handler; protected $pluginId = 'drupal123'; protected $destination = 'drupal123'; + protected $error = "error404"; + protected $response = 'drupal'; /** @@ -62,6 +66,35 @@ public function testBeforeRedirectEvent () { $this->assertSame('drupal123', $collection->getDestination()); } + /** + * test for class FailedAuthenticationEvent + */ + + public function testFailedAuthenticationEvent () { + $this->data_handler = $this->createMock(SocialAuthDataHandler::class); + $collection = $this->getMockBuilder('Drupal\social_auth\Event\FailedAuthenticationEvent') + ->setConstructorArgs(array($this->data_handler, $this->pluginId, $this->error)) + ->setMethods(array('getDataHandler', 'getPluginId', 'getError')) + ->getMock(); + // $reflector = new ReflectionClass( 'Drupal\social_auth\Event\FailedAuthenticationEvent' ); + // $method = $reflector->getMethod('setResponse'); + // $method->setAccessible( true ); + // $method->invokeArgs($collection, array($responses)); + $responses = $this->createMock(RedirectResponse::class); + $collection->setResponse($responses); + $this->assertEquals($responses, $collection->getResponse()); + // var_dump($collection->hasResponse()); + $this->assertEquals(true, $collection->hasResponse()); + $collection->method('getError') + ->willReturn($this->error); + $collection->method('getPluginId') + ->willReturn($this->pluginId); + $collection->method('getDataHandler') + ->willReturn($this->data_handler); + $this->assertEquals('error404', $collection->getError()); + $this->assertEquals('drupal123', $collection->getPluginId()); + } + } From ce63a0ca65c0c2d4d01418b3458934e726b53659 Mon Sep 17 00:00:00 2001 From: neerajp99 Date: Fri, 7 Jun 2019 01:57:50 +0530 Subject: [PATCH 04/42] Add tests for class SocialAuthEvents --- tests/src/Unit/EventTest.php | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/tests/src/Unit/EventTest.php b/tests/src/Unit/EventTest.php index 2ff3bc0..8ea5364 100644 --- a/tests/src/Unit/EventTest.php +++ b/tests/src/Unit/EventTest.php @@ -7,6 +7,7 @@ use Symfony\Component\EventDispatcher\Event; use Symfony\Component\HttpFoundation\RedirectResponse; use Drupal\social_auth\Event\FailedAuthenticationEvent; +use Drupal\social_auth\Event\SocialAuthEvents; @@ -95,6 +96,38 @@ public function testFailedAuthenticationEvent () { $this->assertEquals('drupal123', $collection->getPluginId()); } + /** + * tests for class SocialAuthEvents + */ + + public function testSocialAuthEvents () { + $reflection = new ReflectionClass('Drupal\social_auth\Event\SocialAuthEvents'); + $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'); + } + } From 173fb2835a8b0c2d46230625ab3850d69ee6396d Mon Sep 17 00:00:00 2001 From: neerajp99 Date: Fri, 7 Jun 2019 02:44:37 +0530 Subject: [PATCH 05/42] Add tests for class UserEvent --- tests/src/Unit/EventTest.php | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/tests/src/Unit/EventTest.php b/tests/src/Unit/EventTest.php index 8ea5364..279974d 100644 --- a/tests/src/Unit/EventTest.php +++ b/tests/src/Unit/EventTest.php @@ -8,6 +8,8 @@ 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; @@ -17,6 +19,7 @@ class EventTest extends UnitTestCase { protected $destination = 'drupal123'; protected $error = "error404"; protected $response = 'drupal'; + protected $user = 'drupaluser'; /** @@ -46,8 +49,8 @@ public function testBeforeRedirectEvent () { ->getMock(); $this->assertTrue( - method_exists($collection, 'getDataHandler'), - 'BeforeRedirectEvent does not have getDataHandler function/method' + method_exists($collection, 'getDataHandler'), + 'BeforeRedirectEvent does not have getDataHandler function/method' ); $this->assertTrue( method_exists($collection, 'getPluginId'), @@ -128,6 +131,21 @@ public function testSocialAuthEvents () { 'The constant values is not matched'); } + /** + * tests for class UserEvent + */ + + public function testUserEvent () { + $this->user = $this->createMock(UserInterface::class); + $collection = $this->getMockBuilder('Drupal\social_auth\Event\UserEvent') + ->setConstructorArgs(array($this->user, $this->pluginId)) + ->getMock(); + $collection->method('getPluginId') + ->willReturn($this->pluginId); + $collection->method('getUser') + ->willReturn($this->user); + $this->assertSame('drupal123', $collection->getPluginId()); + } } From fb0979fe52e51ce8e8cdc790f6d5c92e21b8df86 Mon Sep 17 00:00:00 2001 From: neerajp99 Date: Fri, 7 Jun 2019 04:33:55 +0530 Subject: [PATCH 06/42] Add tests for class UserFieldsEvent --- tests/src/Unit/EventTest.php | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/tests/src/Unit/EventTest.php b/tests/src/Unit/EventTest.php index 279974d..7b9a260 100644 --- a/tests/src/Unit/EventTest.php +++ b/tests/src/Unit/EventTest.php @@ -10,6 +10,7 @@ use Drupal\social_auth\Event\SocialAuthEvents; use Drupal\user\UserInterface; use Drupal\social_auth\Event\UserEvent; +use Drupal\social_auth\Event\UserFieldsEvent; @@ -20,6 +21,7 @@ class EventTest extends UnitTestCase { protected $error = "error404"; protected $response = 'drupal'; protected $user = 'drupaluser'; + protected $user_fields; /** @@ -146,6 +148,24 @@ public function testUserEvent () { ->willReturn($this->user); $this->assertSame('drupal123', $collection->getPluginId()); } + + /** + * tests for class UserFieldsEvent + */ + + public function testUserFieldsEvent () { + // $this->user = $this->createMock(UserInterface::class); + $this->user_fields = array(1,2,3); + $collection = $this->getMockBuilder('Drupal\social_auth\Event\UserFieldsEvent') + ->setConstructorArgs(array($this->user_fields, $this->pluginId)) + ->setMethods(array('getPluginId')) + ->getMock(); + $collection->method('getPluginId') + ->willReturn($this->pluginId); + $collection->setUserFields($this->user_fields); + $this->assertEquals($this->user_fields, $collection->getUserFields()); + $this->assertSame('drupal123', $collection->getPluginId()); + } } From b8c5b452e6bf7157ffba8b06726082841dbed8a5 Mon Sep 17 00:00:00 2001 From: neerajp99 Date: Fri, 7 Jun 2019 04:47:29 +0530 Subject: [PATCH 07/42] Add tests for class SocialAuthDataHandler --- tests/src/Unit/SocialAuthDataHandlerTest.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 tests/src/Unit/SocialAuthDataHandlerTest.php diff --git a/tests/src/Unit/SocialAuthDataHandlerTest.php b/tests/src/Unit/SocialAuthDataHandlerTest.php new file mode 100644 index 0000000..b5acbae --- /dev/null +++ b/tests/src/Unit/SocialAuthDataHandlerTest.php @@ -0,0 +1,18 @@ +getMockBuilder('Drupal\social_auth\SocialAuthDataHandler') + ->disableOriginalConstructor() + ->getMock(); + $this->assertTrue($collection instanceof SocialAuthDataHandler); + } +} + ?> From c99601c1d23f50062659efbef964514c12fbddc3 Mon Sep 17 00:00:00 2001 From: neerajp99 Date: Fri, 7 Jun 2019 05:07:37 +0530 Subject: [PATCH 08/42] Add tests for class SettingsBase --- tests/src/Unit/SettingsTest.php | 35 +++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 tests/src/Unit/SettingsTest.php diff --git a/tests/src/Unit/SettingsTest.php b/tests/src/Unit/SettingsTest.php new file mode 100644 index 0000000..91f6b34 --- /dev/null +++ b/tests/src/Unit/SettingsTest.php @@ -0,0 +1,35 @@ +storage = $this->createMock(StorageInterface::class); + $this->event_dispatcher = $this->createMock(EventDispatcherInterface::class); + $this->typed_config = $this->createMock(TypedConfigManagerInterface::class); + $this->configs = $this->getMockBuilder('Drupal\Core\Config\ImmutableConfig') + ->setConstructorArgs(array($this->config, $this->storage, $this->event_dispatcher, $this->typed_config)) + ->getMock(); + $collection = $this->getMockBuilder('Drupal\social_auth\Settings\SettingsBase') + ->setConstructorArgs(array($this->configs)) + ->getMock(); + $this->assertTrue($collection instanceof SettingsBase); + } +} + + ?> From 333910d28386f5b4ac0b9ddc696e3e510ada439a Mon Sep 17 00:00:00 2001 From: neerajp99 Date: Fri, 7 Jun 2019 05:27:27 +0530 Subject: [PATCH 09/42] Add tests for class SettingsInterface and edit class SocialAuthDatahandler --- tests/src/Unit/SettingsTest.php | 16 ++++++++++++++++ tests/src/Unit/SocialAuthDataHandlerTest.php | 6 +++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/tests/src/Unit/SettingsTest.php b/tests/src/Unit/SettingsTest.php index 91f6b34..6e8420a 100644 --- a/tests/src/Unit/SettingsTest.php +++ b/tests/src/Unit/SettingsTest.php @@ -8,6 +8,8 @@ use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Drupal\Core\Config\StorageInterface; use Drupal\Core\Config\TypedConfigManagerInterface; +use Drupal\social_auth\Settings\SettingsInterface; +use Drupal\social_api\Settings\SettingsInterface as SettingsInterfaceBase; class SettingsTest extends UnitTestCase { @@ -18,6 +20,10 @@ class SettingsTest extends UnitTestCase { protected $event_dispatcher; protected $config; + /** + * tests for class SettingsBase + */ + public function testSettingsBase () { $this->storage = $this->createMock(StorageInterface::class); $this->event_dispatcher = $this->createMock(EventDispatcherInterface::class); @@ -30,6 +36,16 @@ public function testSettingsBase () { ->getMock(); $this->assertTrue($collection instanceof SettingsBase); } + + /** + * tests for class SettingsInterface + */ + + public function testSettingsInterface () { + $collection = $this->createMock(SettingsInterface::class); + $this->assertTrue($collection instanceof SettingsInterface); + } + } ?> diff --git a/tests/src/Unit/SocialAuthDataHandlerTest.php b/tests/src/Unit/SocialAuthDataHandlerTest.php index b5acbae..126f3b9 100644 --- a/tests/src/Unit/SocialAuthDataHandlerTest.php +++ b/tests/src/Unit/SocialAuthDataHandlerTest.php @@ -7,7 +7,11 @@ class SocialAuthDataHandlerTest extends UnitTestCase { - public function testSocialAuthHandler () { + /** + * tests for class SocialAuthDataHandler + */ + + public function testSocialAuthDataHandler () { $collection = $this->getMockBuilder('Drupal\social_auth\SocialAuthDataHandler') ->disableOriginalConstructor() From 91e10142a870cdef037ff5c0db574c0ab9f969f2 Mon Sep 17 00:00:00 2001 From: neerajp99 Date: Tue, 11 Jun 2019 05:10:58 +0530 Subject: [PATCH 10/42] Add tests for Plugin --- tests/src/Unit/PluginTest.php | 87 +++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 tests/src/Unit/PluginTest.php diff --git a/tests/src/Unit/PluginTest.php b/tests/src/Unit/PluginTest.php new file mode 100644 index 0000000..9b0e646 --- /dev/null +++ b/tests/src/Unit/PluginTest.php @@ -0,0 +1,87 @@ +configuration = array(); + $this->social_auth_config = $this->createMock(ImmutableConfig::class); + $socialAuthLoginBlock = $this->getMockBuilder(SocialAuthLoginBlock::class) + ->setConstructorArgs(array($this->configuration, $this->plugin_id, $this->plugin_definition, $this->social_auth_config)) + ->getMock(); + $socialAuthLoginBlock->method('build') + ->willReturn(['#theme' => 'login_with', '#social_networks' => $this->social_auth_config->get('auth')]); + $this->assertTrue( + method_exists($socialAuthLoginBlock, 'create'), + 'OAuth2Manager does not implements create function/method' + ); + $this->assertTrue( + method_exists($socialAuthLoginBlock, 'build'), + 'OAuth2Manager does not implements build function/method' + ); + $this->assertEquals(['#theme' => 'login_with', '#social_networks' => $this->social_auth_config->get('auth')], $socialAuthLoginBlock->build()); + } + + /** + * tests for class NetWorkbase + */ + + public function testNetworkBase () { + $this->configuration = array(); + $this->plugin_definition = array(); + $setting = array(); + $entity_type_manager = $this->createMock(EntityTypeManagerInterface::class); + $config_factory = $this->createMock(ConfigFactoryInterface::class); + $logger_factory = $this->createMock(LoggerChannelFactoryInterface::class); + $settings = new Settings($setting); + // $settings = new ReflectionClass(Settings::class); + $networkBase = $this->getMockBuilder(NetworkBase::class) + ->setConstructorArgs(array($this->configuration, $this->plugin_id, $this->plugin_definition, $entity_type_manager, $config_factory, $logger_factory, $settings)) + ->getMockForAbstractClass(); + $this->assertTrue( + method_exists($networkBase, 'create'), + 'NetworkBase does not implements create function/method' + ); + } + +} + + ?> From 433bf7709b3fa94402f07a9a2e65b8bffdecd72b Mon Sep 17 00:00:00 2001 From: neerajp99 Date: Tue, 11 Jun 2019 05:13:51 +0530 Subject: [PATCH 11/42] Edit tests for AuthManager --- tests/src/Unit/AuthManagerTest.php | 32 ++++++++++++++++-------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/tests/src/Unit/AuthManagerTest.php b/tests/src/Unit/AuthManagerTest.php index 7a9de08..7939238 100644 --- a/tests/src/Unit/AuthManagerTest.php +++ b/tests/src/Unit/AuthManagerTest.php @@ -35,7 +35,7 @@ public function __construct() { public function setUp() { $this->logger_factory = $this->createMock(LoggerChannelFactoryInterface::class); $this->settings = $this->createMock(Config::class); - $this->collection = $this->getMockBuilder('Drupal\social_auth\AuthManager\OAuth2Manager') + $this->collection = $this->getMockBuilder(OAuth2Manager::class) ->setConstructorArgs(array($this->settings, $this->logger_factory)) ->setMethods(['getScopes', 'getEndPoints']) ->getMockForAbstractClass(); @@ -76,20 +76,22 @@ public function testOAuth2Manager () { $this->assertSame('drupal123', $this->collection->getEndPoints()); } - public function testGetExtraDetails () { - $endpoints = $this->collection->getEndPoints(); - $data = []; - $this->assertEmpty($data); - if ($endpoints) { - // Iterates through endpoints define in settings and retrieves them. - foreach (explode(PHP_EOL, $endpoints) as $endpoint) { - // Endpoint is set as path/to/endpoint|name. - $parts = explode('|', $endpoint); - $data[$parts[1]] = $this->collection->requestEndPoint($method, $parts[0], $domain); - } - return json_encode($data); - } - } + // public function testGetExtraDetails ($method = 'GET', $domain = NULL) { + // $this->collection->method('getEndPoints') + // ->willReturn($this->endPoints); + // $endpoints = $this->collection->getEndPoints(); + // $data = []; + // if ($endpoints) { + // // Iterates through endpoints define in settings and retrieves them. + // foreach (explode(PHP_EOL, $endpoints) as $endpoint) { + // // Endpoint is set as path/to/endpoint|name. + // $parts = explode('|', $endpoint); + // + // $data[$parts[0]] = $this->collection->requestEndPoint($method, $parts[0], $domain); + // } + // return json_encode($data); + // } + // } public function testOAuth2ManagerInterface () { $this->assertTrue( From ca4ffdcf6fb93f82486128b41f35cc800da0839b Mon Sep 17 00:00:00 2001 From: neerajp99 Date: Tue, 11 Jun 2019 05:14:05 +0530 Subject: [PATCH 12/42] Edit tests for Settings --- tests/src/Unit/SettingsTest.php | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/tests/src/Unit/SettingsTest.php b/tests/src/Unit/SettingsTest.php index 6e8420a..2373c2a 100644 --- a/tests/src/Unit/SettingsTest.php +++ b/tests/src/Unit/SettingsTest.php @@ -20,6 +20,21 @@ class SettingsTest extends UnitTestCase { protected $event_dispatcher; protected $config; + /** + * __construct function + */ + public function __construct() { + parent::__construct(); + } + + /** + * {@inheritdoc} + */ + + public function setUp() { + parent::setUp(); + } + /** * tests for class SettingsBase */ @@ -28,10 +43,10 @@ public function testSettingsBase () { $this->storage = $this->createMock(StorageInterface::class); $this->event_dispatcher = $this->createMock(EventDispatcherInterface::class); $this->typed_config = $this->createMock(TypedConfigManagerInterface::class); - $this->configs = $this->getMockBuilder('Drupal\Core\Config\ImmutableConfig') + $this->configs = $this->getMockBuilder(ImmutableConfig::class) ->setConstructorArgs(array($this->config, $this->storage, $this->event_dispatcher, $this->typed_config)) ->getMock(); - $collection = $this->getMockBuilder('Drupal\social_auth\Settings\SettingsBase') + $collection = $this->getMockBuilder(SettingsBase::class) ->setConstructorArgs(array($this->configs)) ->getMock(); $this->assertTrue($collection instanceof SettingsBase); From f08797c42cdcf386d872124ba44ca8023f8234df Mon Sep 17 00:00:00 2001 From: neerajp99 Date: Tue, 11 Jun 2019 05:14:26 +0530 Subject: [PATCH 13/42] Edit tests for SocialAuthDataHandler --- tests/src/Unit/SocialAuthDataHandlerTest.php | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/tests/src/Unit/SocialAuthDataHandlerTest.php b/tests/src/Unit/SocialAuthDataHandlerTest.php index 126f3b9..2678810 100644 --- a/tests/src/Unit/SocialAuthDataHandlerTest.php +++ b/tests/src/Unit/SocialAuthDataHandlerTest.php @@ -7,13 +7,28 @@ class SocialAuthDataHandlerTest extends UnitTestCase { + /** + * __construct function + */ + public function __construct() { + parent::__construct(); + } + + /** + * {@inheritdoc} + */ + + public function setUp() { + parent::setUp(); + } + /** * tests for class SocialAuthDataHandler */ public function testSocialAuthDataHandler () { - $collection = $this->getMockBuilder('Drupal\social_auth\SocialAuthDataHandler') + $collection = $this->getMockBuilder(SocialAuthDataHandler::class) ->disableOriginalConstructor() ->getMock(); $this->assertTrue($collection instanceof SocialAuthDataHandler); From 842ce6e059047c1315b749b7f06a70aa764e87c4 Mon Sep 17 00:00:00 2001 From: neerajp99 Date: Fri, 14 Jun 2019 05:37:16 +0530 Subject: [PATCH 14/42] Modified tests for AuthManager --- tests/src/Unit/AuthManagerTest.php | 74 +++++++++++++++--------------- 1 file changed, 36 insertions(+), 38 deletions(-) diff --git a/tests/src/Unit/AuthManagerTest.php b/tests/src/Unit/AuthManagerTest.php index 7939238..659a148 100644 --- a/tests/src/Unit/AuthManagerTest.php +++ b/tests/src/Unit/AuthManagerTest.php @@ -12,15 +12,6 @@ class AuthManagerTest extends UnitTestcase { - protected $settings; - protected $loggerFactory; - protected $scopes; - public $collection; - protected $endPoints; - public $collections; - protected $method; - protected $path; - /** * __construct function */ @@ -33,51 +24,55 @@ public function __construct() { */ public function setUp() { - $this->logger_factory = $this->createMock(LoggerChannelFactoryInterface::class); - $this->settings = $this->createMock(Config::class); - $this->collection = $this->getMockBuilder(OAuth2Manager::class) - ->setConstructorArgs(array($this->settings, $this->logger_factory)) - ->setMethods(['getScopes', 'getEndPoints']) - ->getMockForAbstractClass(); - $this->collections = $this->getMock(OAuth2ManagerInterface::class); - // enable any other required module - $this->scopes = "drupal123"; - $this->endPoints = "drupal123"; parent::setUp(); } public function testOAuth2Manager () { + $logger_factory = $this->createMock(LoggerChannelFactoryInterface::class); + $settings = $this->createMock(Config::class); + $collection = $this->getMockBuilder(OAuth2Manager::class) + ->setConstructorArgs(array($settings, $logger_factory)) + ->setMethods(['getScopes', 'getEndPoints', 'settings', 'get']) + ->getMockForAbstractClass(); + + $scopes = FALSE; + $endPoints = "drupal123"; $this->assertTrue( - method_exists($this->collection, 'getExtraDetails'), + method_exists($collection, 'getExtraDetails'), 'OAuth2Manager does not have getExtraDetails function/method' ); $this->assertTrue( - method_exists($this->collection, 'getScopes'), + method_exists($collection, 'getScopes'), 'OAuth2Manager does not have getScopes function/method' ); $this->assertTrue( - method_exists($this->collection, 'getEndPoints'), + method_exists($collection, 'getEndPoints'), 'OAuth2Manager does not have ggetEndPoints function/method' ); - $this->collection->method('getScopes') - ->willReturn($this->scopes); - if ($this->scopes === FALSE){ - $this->scopes = $this->settings->get('scopes'); + $settings->method('get') + ->willReturn('drupal123'); + + if ($scopes === FALSE){ + $scopes = $settings->get('scopes'); } - $this->assertSame('drupal123', $this->collection->getScopes()); - $this->collection->method('getEndPoints') - ->willReturn($this->endPoints); - if ($this->endPoints === FALSE) { - $this->endPoints = $this->settings->get('endpoints'); + $collection->method('getScopes') + ->willReturn($scopes); + + if ($endPoints === FALSE) { + $endPoints = $settings->get('endpoints'); } - $this->assertSame('drupal123', $this->collection->getEndPoints()); + $collection->method('getEndPoints') + ->willReturn($endPoints); + + $this->assertSame('drupal123', $collection->getScopes()); + $this->assertSame('drupal123', $collection->getEndPoints()); } // public function testGetExtraDetails ($method = 'GET', $domain = NULL) { - // $this->collection->method('getEndPoints') + // $collection->method('getEndPoints') // ->willReturn($this->endPoints); // $endpoints = $this->collection->getEndPoints(); // $data = []; @@ -94,24 +89,27 @@ public function testOAuth2Manager () { // } public function testOAuth2ManagerInterface () { + $method = "drupalmethod"; + $path = "drupalpath"; + $collections = $this->getMock(OAuth2ManagerInterface::class); $this->assertTrue( - method_exists($this->collections, 'getExtraDetails'), + method_exists($collections, 'getExtraDetails'), 'OAuth2ManagerInterface does not have getExtraDetails function/method' ); $this->assertTrue( - method_exists($this->collections, 'requestEndPoint'), + method_exists($collections, 'requestEndPoint'), 'OAuth2ManagerInterface does not have requestEndPoint function/method' ); $this->assertTrue( - method_exists($this->collections, 'getScopes'), + method_exists($collections, 'getScopes'), 'OAuth2ManagerInterface does not have getScopes function/method' ); $this->assertTrue( - method_exists($this->collections, 'getEndPoints'), + method_exists($collections, 'getEndPoints'), 'OAuth2ManagerInterface does not have getEndPoints function/method' ); $options = array(); - $this->collections->requestEndPoint($this->method, $this->path, $domain = NULL, $options = []); + $collections->requestEndPoint($method, $path, $domain = NULL, $options = []); } } From 89708b13e06bab6ad495f5831c10904f1463b991 Mon Sep 17 00:00:00 2001 From: neerajp99 Date: Fri, 14 Jun 2019 05:37:35 +0530 Subject: [PATCH 15/42] Add tests for Controller --- tests/src/Unit/ControllerTest.php | 62 +++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 tests/src/Unit/ControllerTest.php diff --git a/tests/src/Unit/ControllerTest.php b/tests/src/Unit/ControllerTest.php new file mode 100644 index 0000000..a7f1b93 --- /dev/null +++ b/tests/src/Unit/ControllerTest.php @@ -0,0 +1,62 @@ +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(array($namespaces, $cache_backend, $module_handler)) + ->getMock(); + $socialAuthController = $this->getMockBuilder(SocialAuthController::class) + ->setConstructorArgs(array($networkManager)) + ->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() { + $this->assertTrue(true); + } + +} From 29cb45c8f6eabdc98ee971dd280a5ee39b2d73a0 Mon Sep 17 00:00:00 2001 From: neerajp99 Date: Fri, 14 Jun 2019 05:37:48 +0530 Subject: [PATCH 16/42] Add tests for class SocialAuth --- tests/src/Unit/EntityTest.php | 102 ++++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 tests/src/Unit/EntityTest.php diff --git a/tests/src/Unit/EntityTest.php b/tests/src/Unit/EntityTest.php new file mode 100644 index 0000000..c76bdda --- /dev/null +++ b/tests/src/Unit/EntityTest.php @@ -0,0 +1,102 @@ +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()); + } +} + + ?> From 5d09a70478ab860e129e898bee0ce47a4c1ba68e Mon Sep 17 00:00:00 2001 From: neerajp99 Date: Sun, 16 Jun 2019 19:35:15 +0530 Subject: [PATCH 17/42] Edit tests for AuthManager --- tests/src/Unit/AuthManagerTest.php | 62 +++++++++++++----------------- 1 file changed, 26 insertions(+), 36 deletions(-) diff --git a/tests/src/Unit/AuthManagerTest.php b/tests/src/Unit/AuthManagerTest.php index 659a148..9533196 100644 --- a/tests/src/Unit/AuthManagerTest.php +++ b/tests/src/Unit/AuthManagerTest.php @@ -2,7 +2,6 @@ use Drupal\Tests\UnitTestCase; use PHPUnit\Framework\TestCase; -//OAuth2Manager use Drupal\social_auth\AuthManager\OAuth2Manager; use Drupal\Core\Config\Config; use Drupal\Core\Logger\LoggerChannelFactoryInterface; @@ -11,43 +10,30 @@ class AuthManagerTest extends UnitTestcase { - - /** - * __construct function - */ - public function __construct() { - parent::__construct(); - } - - /** - * {@inheritdoc} - */ - - public function setUp() { - parent::setUp(); - } - - public function testOAuth2Manager () { + $scopes = FALSE; + $endPoints = "drupal123"; + $logger_factory = $this->createMock(LoggerChannelFactoryInterface::class); $settings = $this->createMock(Config::class); - $collection = $this->getMockBuilder(OAuth2Manager::class) + + $authManager = $this->getMockBuilder(OAuth2Manager::class) ->setConstructorArgs(array($settings, $logger_factory)) ->setMethods(['getScopes', 'getEndPoints', 'settings', 'get']) ->getMockForAbstractClass(); - $scopes = FALSE; - $endPoints = "drupal123"; $this->assertTrue( - method_exists($collection, 'getExtraDetails'), + method_exists($authManager, 'getExtraDetails'), 'OAuth2Manager does not have getExtraDetails function/method' ); + $this->assertTrue( - method_exists($collection, 'getScopes'), + method_exists($authManager, 'getScopes'), 'OAuth2Manager does not have getScopes function/method' ); + $this->assertTrue( - method_exists($collection, 'getEndPoints'), + method_exists($authManager, 'getEndPoints'), 'OAuth2Manager does not have ggetEndPoints function/method' ); @@ -57,18 +43,17 @@ public function testOAuth2Manager () { if ($scopes === FALSE){ $scopes = $settings->get('scopes'); } - - $collection->method('getScopes') + $authManager->method('getScopes') ->willReturn($scopes); if ($endPoints === FALSE) { $endPoints = $settings->get('endpoints'); } - $collection->method('getEndPoints') + $authManager->method('getEndPoints') ->willReturn($endPoints); - $this->assertSame('drupal123', $collection->getScopes()); - $this->assertSame('drupal123', $collection->getEndPoints()); + $this->assertSame('drupal123', $authManager->getScopes()); + $this->assertSame('drupal123', $authManager->getEndPoints()); } // public function testGetExtraDetails ($method = 'GET', $domain = NULL) { @@ -91,25 +76,30 @@ public function testOAuth2Manager () { public function testOAuth2ManagerInterface () { $method = "drupalmethod"; $path = "drupalpath"; - $collections = $this->getMock(OAuth2ManagerInterface::class); + + $authManagerInterface = $this->getMock(OAuth2ManagerInterface::class); + + $authManagerInterface->requestEndPoint($method, $path, $domain = NULL, $options = []); + $this->assertTrue( - method_exists($collections, 'getExtraDetails'), + method_exists($authManagerInterface, 'getExtraDetails'), 'OAuth2ManagerInterface does not have getExtraDetails function/method' ); + $this->assertTrue( - method_exists($collections, 'requestEndPoint'), + method_exists($authManagerInterface, 'requestEndPoint'), 'OAuth2ManagerInterface does not have requestEndPoint function/method' ); + $this->assertTrue( - method_exists($collections, 'getScopes'), + method_exists($authManagerInterface, 'getScopes'), 'OAuth2ManagerInterface does not have getScopes function/method' ); + $this->assertTrue( - method_exists($collections, 'getEndPoints'), + method_exists($authManagerInterface, 'getEndPoints'), 'OAuth2ManagerInterface does not have getEndPoints function/method' ); - $options = array(); - $collections->requestEndPoint($method, $path, $domain = NULL, $options = []); } } From e6eecdb855334caddfe5e38661e70b2ca897be50 Mon Sep 17 00:00:00 2001 From: neerajp99 Date: Sun, 16 Jun 2019 19:35:28 +0530 Subject: [PATCH 18/42] Edit tests for Controller --- tests/src/Unit/ControllerTest.php | 39 +++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/tests/src/Unit/ControllerTest.php b/tests/src/Unit/ControllerTest.php index a7f1b93..0d4bbcc 100644 --- a/tests/src/Unit/ControllerTest.php +++ b/tests/src/Unit/ControllerTest.php @@ -7,6 +7,17 @@ use Drupal\social_api\Plugin\NetworkManager; use Drupal\Core\Cache\CacheBackendInterface; use Drupal\Core\Extension\ModuleHandlerInterface; +use Drupal\social_auth\Controller\OAuth2ControllerBase; +use Drupal\Component\Plugin\Exception\PluginException; +use Drupal\Core\Controller\ControllerBase; +use Drupal\Core\Messenger\MessengerInterface; +use Drupal\Core\Render\RenderContext; +use Drupal\Core\Render\RendererInterface; +use Drupal\Core\Routing\TrustedRedirectResponse; +use Drupal\social_auth\AuthManager\OAuth2ManagerInterface; +use Drupal\social_auth\SocialAuthDataHandler; +use Drupal\social_auth\User\UserAuthenticator; +use Symfony\Component\HttpFoundation\RequestStack; class ControllerTest extends UnitTestCase { @@ -57,6 +68,34 @@ public function testSocialAuthController () { */ public function testOAuth2ControllerBase() { $this->assertTrue(true); + $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(array('moduleName', + 'pluginId', + $messenger, + $network_manager, + $user_authenticator, + $provider_manager, + $request, + $data_handler, + $renderer, + )) + ->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' + ); } } From 22609e10b9d65322d2b72e929ace21152a78c57e Mon Sep 17 00:00:00 2001 From: neerajp99 Date: Sun, 16 Jun 2019 19:35:37 +0530 Subject: [PATCH 19/42] Edit tests for Events --- tests/src/Unit/EventTest.php | 224 +++++++++++++++++++++-------------- 1 file changed, 138 insertions(+), 86 deletions(-) diff --git a/tests/src/Unit/EventTest.php b/tests/src/Unit/EventTest.php index 7b9a260..6e542f3 100644 --- a/tests/src/Unit/EventTest.php +++ b/tests/src/Unit/EventTest.php @@ -12,106 +12,129 @@ use Drupal\social_auth\Event\UserEvent; use Drupal\social_auth\Event\UserFieldsEvent; - - class EventTest extends UnitTestCase { - protected $data_handler; - protected $pluginId = 'drupal123'; - protected $destination = 'drupal123'; - protected $error = "error404"; - protected $response = 'drupal'; - protected $user = 'drupaluser'; - protected $user_fields; - - /** - * __construct function - */ - public function __construct() { - parent::__construct(); - } - - /** - * {@inheritdoc} - */ - - public function setUp() { - parent::setUp(); - } + // public function __construct() { + // parent::__construct(...func_get_args()); + // } /** * test for class testBeforeRedirectEvent */ - public function testBeforeRedirectEvent () { - $this->data_handler = $this->createMock(SocialAuthDataHandler::class); - $collection = $this->getMockBuilder('Drupal\social_auth\Event\BeforeRedirectEvent') - ->setConstructorArgs(array($this->data_handler, $this->pluginId, $this->destination)) + $pluginId = 'drupal123'; + $destination = 'drupal123'; + $data_handler = $this->createMock(SocialAuthDataHandler::class); + + $beforeRedirectEvent = $this->getMockBuilder(BeforeRedirectEvent::class) + ->setConstructorArgs(array($data_handler, $pluginId, $destination)) ->setMethods(array('getDataHandler', 'getPluginId', 'getDestination')) ->getMock(); + $beforeRedirectEvent->method('getPluginId') + ->willReturn($pluginId); + + $beforeRedirectEvent->method('getDestination') + ->willReturn($destination); + + $beforeRedirectEvent->method('getDataHandler') + ->willReturn($data_handler); + $this->assertTrue( - method_exists($collection, 'getDataHandler'), - 'BeforeRedirectEvent does not have getDataHandler function/method' + method_exists($beforeRedirectEvent, 'getDataHandler'), + 'BeforeRedirectEvent class does not implements getDataHandler function/method' ); + $this->assertTrue( - method_exists($collection, 'getPluginId'), - 'BeforeRedirectEvent does not have getPluginId function/method' + method_exists($beforeRedirectEvent, 'getPluginId'), + 'BeforeRedirectEvent class does not implements getPluginId function/method' ); + $this->assertTrue( - method_exists($collection, 'getDestination'), - 'BeforeRedirectEvent does not have getDestination function/method' + method_exists($beforeRedirectEvent, 'getDestination'), + 'BeforeRedirectEvent class does not implements getDestination function/method' ); - $collection->method('getPluginId') - ->willReturn($this->pluginId); - $collection->method('getDestination') - ->willReturn($this->destination); - $collection->method('getDataHandler') - ->willReturn($this->data_handler); - $this->assertSame('drupal123', $collection->getPluginId()); - $this->assertSame('drupal123', $collection->getDestination()); + + $this->assertSame('drupal123', $beforeRedirectEvent->getPluginId()); + $this->assertSame('drupal123', $beforeRedirectEvent->getDestination()); + $this->assertEquals($data_handler, $beforeRedirectEvent->getDataHandler()); } /** * test for class FailedAuthenticationEvent */ - public function testFailedAuthenticationEvent () { - $this->data_handler = $this->createMock(SocialAuthDataHandler::class); - $collection = $this->getMockBuilder('Drupal\social_auth\Event\FailedAuthenticationEvent') - ->setConstructorArgs(array($this->data_handler, $this->pluginId, $this->error)) + $error = "error404"; + $pluginId = 'drupal123'; + + $data_handler = $this->createMock(SocialAuthDataHandler::class); + $response = $this->createMock(RedirectResponse::class); + + $failedAuthenticationEvent = $this->getMockBuilder(FailedAuthenticationEvent::class) + ->setConstructorArgs(array($data_handler, $pluginId, $error)) ->setMethods(array('getDataHandler', 'getPluginId', 'getError')) ->getMock(); - // $reflector = new ReflectionClass( 'Drupal\social_auth\Event\FailedAuthenticationEvent' ); - // $method = $reflector->getMethod('setResponse'); - // $method->setAccessible( true ); - // $method->invokeArgs($collection, array($responses)); - $responses = $this->createMock(RedirectResponse::class); - $collection->setResponse($responses); - $this->assertEquals($responses, $collection->getResponse()); - // var_dump($collection->hasResponse()); - $this->assertEquals(true, $collection->hasResponse()); - $collection->method('getError') - ->willReturn($this->error); - $collection->method('getPluginId') - ->willReturn($this->pluginId); - $collection->method('getDataHandler') - ->willReturn($this->data_handler); - $this->assertEquals('error404', $collection->getError()); - $this->assertEquals('drupal123', $collection->getPluginId()); + // parent::__construct($data_handler, $pluginId, $error); + + $failedAuthenticationEvent->setResponse($response); + + $failedAuthenticationEvent->method('getError') + ->willReturn($error); + + $failedAuthenticationEvent->method('getPluginId') + ->willReturn($pluginId); + + $failedAuthenticationEvent->method('getDataHandler') + ->willReturn($data_handler); + + $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('Drupal\social_auth\Event\SocialAuthEvents'); + $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'); @@ -136,37 +159,66 @@ public function testSocialAuthEvents () { /** * tests for class UserEvent */ - public function testUserEvent () { - $this->user = $this->createMock(UserInterface::class); - $collection = $this->getMockBuilder('Drupal\social_auth\Event\UserEvent') - ->setConstructorArgs(array($this->user, $this->pluginId)) + $pluginId = 'drupal123'; + $user = $this->createMock(UserInterface::class); + + $userEvent = $this->getMockBuilder(UserEvent::class) + ->setConstructorArgs(array($user, $pluginId)) ->getMock(); - $collection->method('getPluginId') - ->willReturn($this->pluginId); - $collection->method('getUser') - ->willReturn($this->user); - $this->assertSame('drupal123', $collection->getPluginId()); + + $userEvent->method('getPluginId') + ->willReturn($pluginId); + $userEvent->method('getUser') + ->willReturn($user); + + $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); - $this->user_fields = array(1,2,3); - $collection = $this->getMockBuilder('Drupal\social_auth\Event\UserFieldsEvent') - ->setConstructorArgs(array($this->user_fields, $this->pluginId)) + $pluginId = 'drupal123'; + $user_fields = array('userfield', 'userfield2'); + + $userFieldsEvent = $this->getMockBuilder(UserFieldsEvent::class) + ->setConstructorArgs(array($user_fields, $pluginId)) ->setMethods(array('getPluginId')) ->getMock(); - $collection->method('getPluginId') - ->willReturn($this->pluginId); - $collection->setUserFields($this->user_fields); - $this->assertEquals($this->user_fields, $collection->getUserFields()); - $this->assertSame('drupal123', $collection->getPluginId()); - } -} + $userFieldsEvent->method('getPluginId') + ->willReturn($pluginId); + $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()); + } +} From a3a42112f2dd356d5c20ec0f21cbd9d2419ce475 Mon Sep 17 00:00:00 2001 From: neerajp99 Date: Sun, 16 Jun 2019 19:37:22 +0530 Subject: [PATCH 20/42] Edit tests for AuthManager --- tests/src/Unit/AuthManagerTest.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/tests/src/Unit/AuthManagerTest.php b/tests/src/Unit/AuthManagerTest.php index 9533196..df90d73 100644 --- a/tests/src/Unit/AuthManagerTest.php +++ b/tests/src/Unit/AuthManagerTest.php @@ -8,8 +8,11 @@ use Drupal\social_api\AuthManager\OAuth2Manager as BaseOAuth2Manager; use Drupal\social_auth\AuthManager\OAuth2ManagerInterface; - class AuthManagerTest extends UnitTestcase { + + /** + * tests for class OAuth2Manager + */ public function testOAuth2Manager () { $scopes = FALSE; $endPoints = "drupal123"; @@ -73,6 +76,9 @@ public function testOAuth2Manager () { // } // } + /** + * tests for clas OAuth2ManagerInterface + */ public function testOAuth2ManagerInterface () { $method = "drupalmethod"; $path = "drupalpath"; @@ -102,5 +108,3 @@ public function testOAuth2ManagerInterface () { ); } } - - ?> From bdc5c4644f84f56dc6542fb1770f3c6563036f6e Mon Sep 17 00:00:00 2001 From: neerajp99 Date: Sun, 16 Jun 2019 19:38:56 +0530 Subject: [PATCH 21/42] Edit tests for Controller --- tests/src/Unit/ControllerTest.php | 22 +++++----------------- 1 file changed, 5 insertions(+), 17 deletions(-) diff --git a/tests/src/Unit/ControllerTest.php b/tests/src/Unit/ControllerTest.php index 0d4bbcc..379e6d4 100644 --- a/tests/src/Unit/ControllerTest.php +++ b/tests/src/Unit/ControllerTest.php @@ -22,33 +22,19 @@ class ControllerTest extends UnitTestCase { - /** - * __construct function - */ - public function __construct() { - parent::__construct(); - } - - /** - * {@inheritdoc} - */ - - public function setUp() { - parent::setUp(); - } - /** * 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(array($namespaces, $cache_backend, $module_handler)) ->getMock(); + $socialAuthController = $this->getMockBuilder(SocialAuthController::class) ->setConstructorArgs(array($networkManager)) ->getMock(); @@ -57,6 +43,7 @@ public function testSocialAuthController () { method_exists($socialAuthController, 'setLoginButtonSettings'), 'SocialAuthController does not have setLoginButtonSettings function/method' ); + $this->assertTrue( method_exists($socialAuthController, 'deleteLoginButtonSettings'), 'SocialAuthController does not have deleteLoginButtonSettings function/method' @@ -67,7 +54,6 @@ public function testSocialAuthController () { * tests for class OAuth2ControllerBase */ public function testOAuth2ControllerBase() { - $this->assertTrue(true); $messenger = $this->createMock(MessengerInterface::class); $network_manager = $this->createMock(NetworkManager::class); $user_authenticator = $this->createMock(UserAuthenticator::class); @@ -75,6 +61,7 @@ public function testOAuth2ControllerBase() { $data_handler = $this->createMock(SocialAuthDataHandler::class); $renderer = $this->createMock(RendererInterface::class); $request = $this->createMock(RequestStack::class); + $oAuth2ControllerBase = $this->getMockBuilder(OAuth2ControllerBase::class) ->setConstructorArgs(array('moduleName', 'pluginId', @@ -92,6 +79,7 @@ public function testOAuth2ControllerBase() { method_exists($oAuth2ControllerBase, 'processCallback'), 'OAuth2ControllerBase does not implements processCallback function/method' ); + $this->assertTrue( method_exists($oAuth2ControllerBase, 'redirectToProvider'), 'OAuth2ControllerBase does not implements redirectToProvider function/method' From cbb2e26490ddf7d4d02711de713e4aa73d0fba99 Mon Sep 17 00:00:00 2001 From: neerajp99 Date: Sun, 16 Jun 2019 19:41:45 +0530 Subject: [PATCH 22/42] Edit tests for Entity --- tests/src/Unit/EntityTest.php | 25 ++++++++----------------- 1 file changed, 8 insertions(+), 17 deletions(-) diff --git a/tests/src/Unit/EntityTest.php b/tests/src/Unit/EntityTest.php index c76bdda..c053dc9 100644 --- a/tests/src/Unit/EntityTest.php +++ b/tests/src/Unit/EntityTest.php @@ -9,21 +9,6 @@ use Drupal\Core\Entity\ContentEntityInterface; class EntityTest extends UnitTestCase { - protected $setting = array(); - /** - * __construct function - */ - public function __construct() { - parent::__construct(); - } - - /** - * {@inheritdoc} - */ - public function setUp() { - parent::setUp(); - } - public function testSocialAuth () { $socialAuth = $this->createMock(SocialAuth::class); @@ -59,34 +44,42 @@ public function testSocialAuth () { 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' @@ -98,5 +91,3 @@ public function testSocialAuth () { $this->assertEquals('2019-06-10T10:06:11+00:00', $socialAuth->getChangedTime()); } } - - ?> From 31dc5086b1fa3eb4de78c52ccedb58ef5a5a5340 Mon Sep 17 00:00:00 2001 From: neerajp99 Date: Sun, 16 Jun 2019 19:49:23 +0530 Subject: [PATCH 23/42] Edit tests for class NetworkBase and SocialAuthLoginBlock --- tests/src/Unit/PluginTest.php | 54 +++++++++++------------------------ 1 file changed, 17 insertions(+), 37 deletions(-) diff --git a/tests/src/Unit/PluginTest.php b/tests/src/Unit/PluginTest.php index 9b0e646..bddb502 100644 --- a/tests/src/Unit/PluginTest.php +++ b/tests/src/Unit/PluginTest.php @@ -15,73 +15,53 @@ class NetworkTest extends UnitTestCase { - - protected $current_user; - protected $plugin_id; - protected $plugin_definition; - protected $social_auth_config; - protected $configuration; - - /** - * __construct function - */ - public function __construct() { - parent::__construct(); - } - - /** - * {@inheritdoc} - */ - - public function setUp() { - parent::setUp(); - } - /** * tests for class SocialAuthLoginBlock */ - public function testSocialAuthLoginBlock () { - $this->configuration = array(); - $this->social_auth_config = $this->createMock(ImmutableConfig::class); + $configuration = array(); + $social_auth_config = $this->createMock(ImmutableConfig::class); + $socialAuthLoginBlock = $this->getMockBuilder(SocialAuthLoginBlock::class) - ->setConstructorArgs(array($this->configuration, $this->plugin_id, $this->plugin_definition, $this->social_auth_config)) + ->setConstructorArgs(array($configuration, 'drupalPlugin', 'definitionOfPlugin', $social_auth_config)) ->getMock(); + $socialAuthLoginBlock->method('build') - ->willReturn(['#theme' => 'login_with', '#social_networks' => $this->social_auth_config->get('auth')]); + ->willReturn(['#theme' => 'login_with', '#social_networks' => $social_auth_config->get('auth')]); + $this->assertTrue( method_exists($socialAuthLoginBlock, 'create'), - 'OAuth2Manager does not implements create function/method' + 'SocialAuthLoginBlock class does not implements create function/method' ); + $this->assertTrue( method_exists($socialAuthLoginBlock, 'build'), - 'OAuth2Manager does not implements build function/method' + 'SocialAuthLoginBlock class does not implements build function/method' ); - $this->assertEquals(['#theme' => 'login_with', '#social_networks' => $this->social_auth_config->get('auth')], $socialAuthLoginBlock->build()); + + $this->assertEquals(['#theme' => 'login_with', '#social_networks' => $social_auth_config->get('auth')], $socialAuthLoginBlock->build()); } /** * tests for class NetWorkbase */ - public function testNetworkBase () { - $this->configuration = array(); - $this->plugin_definition = array(); + $configuration = array(); + $plugin_definition = array(); $setting = array(); $entity_type_manager = $this->createMock(EntityTypeManagerInterface::class); $config_factory = $this->createMock(ConfigFactoryInterface::class); $logger_factory = $this->createMock(LoggerChannelFactoryInterface::class); $settings = new Settings($setting); // $settings = new ReflectionClass(Settings::class); + $networkBase = $this->getMockBuilder(NetworkBase::class) - ->setConstructorArgs(array($this->configuration, $this->plugin_id, $this->plugin_definition, $entity_type_manager, $config_factory, $logger_factory, $settings)) + ->setConstructorArgs(array($configuration, 'drupalPlugin123', $plugin_definition, $entity_type_manager, $config_factory, $logger_factory, $settings)) ->getMockForAbstractClass(); + $this->assertTrue( method_exists($networkBase, 'create'), 'NetworkBase does not implements create function/method' ); } - } - - ?> From d0d77616ee0040155a16cad374b33b77b91ad515 Mon Sep 17 00:00:00 2001 From: neerajp99 Date: Sun, 16 Jun 2019 20:05:43 +0530 Subject: [PATCH 24/42] Edit tests for SettingsBase class --- tests/src/Unit/SettingsTest.php | 55 +++++++-------------------------- 1 file changed, 11 insertions(+), 44 deletions(-) diff --git a/tests/src/Unit/SettingsTest.php b/tests/src/Unit/SettingsTest.php index 2373c2a..823cf43 100644 --- a/tests/src/Unit/SettingsTest.php +++ b/tests/src/Unit/SettingsTest.php @@ -8,59 +8,26 @@ use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Drupal\Core\Config\StorageInterface; use Drupal\Core\Config\TypedConfigManagerInterface; -use Drupal\social_auth\Settings\SettingsInterface; -use Drupal\social_api\Settings\SettingsInterface as SettingsInterfaceBase; - class SettingsTest extends UnitTestCase { - - protected $storage; - protected $event_dispatched; - protected $typed_config; - protected $event_dispatcher; - protected $config; - - /** - * __construct function - */ - public function __construct() { - parent::__construct(); - } - - /** - * {@inheritdoc} - */ - - public function setUp() { - parent::setUp(); - } - /** * tests for class SettingsBase */ - public function testSettingsBase () { - $this->storage = $this->createMock(StorageInterface::class); - $this->event_dispatcher = $this->createMock(EventDispatcherInterface::class); - $this->typed_config = $this->createMock(TypedConfigManagerInterface::class); - $this->configs = $this->getMockBuilder(ImmutableConfig::class) - ->setConstructorArgs(array($this->config, $this->storage, $this->event_dispatcher, $this->typed_config)) + $storage = $this->createMock(StorageInterface::class); + $event_dispatcher = $this->createMock(EventDispatcherInterface::class); + $typed_config = $this->createMock(TypedConfigManagerInterface::class); + + $configs = $this->getMockBuilder(ImmutableConfig::class) + ->setConstructorArgs(array('drupalConfig123', $storage, $event_dispatcher, $typed_config)) ->getMock(); + $collection = $this->getMockBuilder(SettingsBase::class) - ->setConstructorArgs(array($this->configs)) + ->setConstructorArgs(array($configs)) ->getMock(); - $this->assertTrue($collection instanceof SettingsBase); - } - /** - * tests for class SettingsInterface - */ - - public function testSettingsInterface () { - $collection = $this->createMock(SettingsInterface::class); - $this->assertTrue($collection instanceof SettingsInterface); + //writing this assertion, otherwise the test will throw a warning. And this test + //is quite important as its checking for parent methods. + $this->assertTrue($collection instanceof SettingsBase); } - } - - ?> From 93b2d1aa2c5a639d0702d87fc77fe90dffc37d1d Mon Sep 17 00:00:00 2001 From: neerajp99 Date: Sun, 16 Jun 2019 20:06:35 +0530 Subject: [PATCH 25/42] remove extra test for class SocialAuthDataHandler --- tests/src/Unit/SocialAuthDataHandlerTest.php | 37 -------------------- 1 file changed, 37 deletions(-) delete mode 100644 tests/src/Unit/SocialAuthDataHandlerTest.php diff --git a/tests/src/Unit/SocialAuthDataHandlerTest.php b/tests/src/Unit/SocialAuthDataHandlerTest.php deleted file mode 100644 index 2678810..0000000 --- a/tests/src/Unit/SocialAuthDataHandlerTest.php +++ /dev/null @@ -1,37 +0,0 @@ -getMockBuilder(SocialAuthDataHandler::class) - ->disableOriginalConstructor() - ->getMock(); - $this->assertTrue($collection instanceof SocialAuthDataHandler); - } -} - ?> From 39f4f829cad9ebcf9d4362ff0864c61b31ba1b46 Mon Sep 17 00:00:00 2001 From: neerajpandey Date: Sun, 16 Jun 2019 20:53:41 +0530 Subject: [PATCH 26/42] Fixed coding standard violations in AuthManager test --- tests/src/Unit/AuthManagerTest.php | 54 +++++++++++++----------------- 1 file changed, 24 insertions(+), 30 deletions(-) diff --git a/tests/src/Unit/AuthManagerTest.php b/tests/src/Unit/AuthManagerTest.php index df90d73..4a139c0 100644 --- a/tests/src/Unit/AuthManagerTest.php +++ b/tests/src/Unit/AuthManagerTest.php @@ -1,19 +1,21 @@ createMock(Config::class); $authManager = $this->getMockBuilder(OAuth2Manager::class) - ->setConstructorArgs(array($settings, $logger_factory)) - ->setMethods(['getScopes', 'getEndPoints', 'settings', 'get']) - ->getMockForAbstractClass(); + ->setConstructorArgs([$settings, + $logger_factory, + ]) + ->setMethods([( + 'getScopes', + 'getEndPoints', + 'settings', + 'get', + ]) + ->getMockForAbstractClass(); $this->assertTrue( method_exists($authManager, 'getExtraDetails'), @@ -41,45 +50,29 @@ public function testOAuth2Manager () { ); $settings->method('get') - ->willReturn('drupal123'); + ->willReturn('drupal123'); - if ($scopes === FALSE){ + if ($scopes === FALSE) { $scopes = $settings->get('scopes'); } $authManager->method('getScopes') - ->willReturn($scopes); + ->willReturn($scopes); if ($endPoints === FALSE) { $endPoints = $settings->get('endpoints'); } $authManager->method('getEndPoints') - ->willReturn($endPoints); + ->willReturn($endPoints); $this->assertSame('drupal123', $authManager->getScopes()); $this->assertSame('drupal123', $authManager->getEndPoints()); } - // public function testGetExtraDetails ($method = 'GET', $domain = NULL) { - // $collection->method('getEndPoints') - // ->willReturn($this->endPoints); - // $endpoints = $this->collection->getEndPoints(); - // $data = []; - // if ($endpoints) { - // // Iterates through endpoints define in settings and retrieves them. - // foreach (explode(PHP_EOL, $endpoints) as $endpoint) { - // // Endpoint is set as path/to/endpoint|name. - // $parts = explode('|', $endpoint); - // - // $data[$parts[0]] = $this->collection->requestEndPoint($method, $parts[0], $domain); - // } - // return json_encode($data); - // } - // } - /** - * tests for clas OAuth2ManagerInterface + * Tests for clas OAuth2ManagerInterface. */ - public function testOAuth2ManagerInterface () { + public function testOAuth2ManagerInterface() { + $method = "drupalmethod"; $path = "drupalpath"; @@ -107,4 +100,5 @@ public function testOAuth2ManagerInterface () { 'OAuth2ManagerInterface does not have getEndPoints function/method' ); } + } From 4bb4702c7939d30a4d0abab6135a80a982a60726 Mon Sep 17 00:00:00 2001 From: neerajpandey Date: Sun, 16 Jun 2019 20:56:29 +0530 Subject: [PATCH 27/42] Fixed coding standard violations in Controller test --- tests/src/Unit/ControllerTest.php | 43 ++++++++++++++----------------- 1 file changed, 20 insertions(+), 23 deletions(-) diff --git a/tests/src/Unit/ControllerTest.php b/tests/src/Unit/ControllerTest.php index 379e6d4..1c59028 100644 --- a/tests/src/Unit/ControllerTest.php +++ b/tests/src/Unit/ControllerTest.php @@ -2,41 +2,38 @@ use Drupal\Tests\UnitTestCase; use Drupal\social_auth\Controller\SocialAuthController; -use Drupal\social_api\Controller\SocialApiController; 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\Component\Plugin\Exception\PluginException; -use Drupal\Core\Controller\ControllerBase; use Drupal\Core\Messenger\MessengerInterface; -use Drupal\Core\Render\RenderContext; use Drupal\Core\Render\RendererInterface; -use Drupal\Core\Routing\TrustedRedirectResponse; 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 ControllerTest extends UnitTestCase { /** - * tests for class SocialAuthController + * Tests for class SocialAuthController. */ - public function testSocialAuthController () { + 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(array($namespaces, $cache_backend, $module_handler)) + ->setConstructorArgs([$namespaces, $cache_backend, $module_handler]) ->getMock(); $socialAuthController = $this->getMockBuilder(SocialAuthController::class) - ->setConstructorArgs(array($networkManager)) + ->setConstructorArgs([$networkManager]) ->getMock(); $this->assertTrue( @@ -51,7 +48,7 @@ public function testSocialAuthController () { } /** - * tests for class OAuth2ControllerBase + * Tests for class OAuth2ControllerBase. */ public function testOAuth2ControllerBase() { $messenger = $this->createMock(MessengerInterface::class); @@ -63,19 +60,19 @@ public function testOAuth2ControllerBase() { $request = $this->createMock(RequestStack::class); $oAuth2ControllerBase = $this->getMockBuilder(OAuth2ControllerBase::class) - ->setConstructorArgs(array('moduleName', - 'pluginId', - $messenger, - $network_manager, - $user_authenticator, - $provider_manager, - $request, - $data_handler, - $renderer, - )) - ->getMock(); + ->setConstructorArgs(['moduleName', + 'pluginId', + $messenger, + $network_manager, + $user_authenticator, + $provider_manager, + $request, + $data_handler, + $renderer, + ]) + ->getMock(); - $this->assertTrue( + $this->assertTrue( method_exists($oAuth2ControllerBase, 'processCallback'), 'OAuth2ControllerBase does not implements processCallback function/method' ); From 75de6bcc54b4163fb8f940716ff5b3b46854fa19 Mon Sep 17 00:00:00 2001 From: neerajpandey Date: Sun, 16 Jun 2019 20:59:17 +0530 Subject: [PATCH 28/42] Fixed coding standard violations in EntityTest Class --- tests/src/Unit/EntityTest.php | 175 +++++++++++++++++----------------- 1 file changed, 89 insertions(+), 86 deletions(-) diff --git a/tests/src/Unit/EntityTest.php b/tests/src/Unit/EntityTest.php index c053dc9..d890d20 100644 --- a/tests/src/Unit/EntityTest.php +++ b/tests/src/Unit/EntityTest.php @@ -1,93 +1,96 @@ 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()); - } + + /** + * 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()); + } + } From c70a601a0b3bb1ac4943f8bffbb0c75eb8fbd7ab Mon Sep 17 00:00:00 2001 From: neerajpandey Date: Sun, 16 Jun 2019 21:17:56 +0530 Subject: [PATCH 29/42] Fixed coding standard violations in Event --- tests/src/Unit/EventTest.php | 304 ++++++++++++++++++----------------- 1 file changed, 157 insertions(+), 147 deletions(-) diff --git a/tests/src/Unit/EventTest.php b/tests/src/Unit/EventTest.php index 6e542f3..af1757f 100644 --- a/tests/src/Unit/EventTest.php +++ b/tests/src/Unit/EventTest.php @@ -1,10 +1,8 @@ createMock(SocialAuthDataHandler::class); $beforeRedirectEvent = $this->getMockBuilder(BeforeRedirectEvent::class) - ->setConstructorArgs(array($data_handler, $pluginId, $destination)) - ->setMethods(array('getDataHandler', 'getPluginId', 'getDestination')) - ->getMock(); + ->setConstructorArgs([$data_handler, + $pluginId, + $destination, + ]) + ->setMethods(['getDataHandler', + 'getPluginId', + 'getDestination', + ]) + ->getMock(); $beforeRedirectEvent->method('getPluginId') - ->willReturn($pluginId); + ->willReturn($pluginId); $beforeRedirectEvent->method('getDestination') - ->willReturn($destination); + ->willReturn($destination); $beforeRedirectEvent->method('getDataHandler') - ->willReturn($data_handler); + ->willReturn($data_handler); $this->assertTrue( method_exists($beforeRedirectEvent, 'getDataHandler'), @@ -61,164 +65,170 @@ public function testBeforeRedirectEvent () { } /** - * test for class FailedAuthenticationEvent + * 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(array($data_handler, $pluginId, $error)) - ->setMethods(array('getDataHandler', 'getPluginId', 'getError')) - ->getMock(); - // parent::__construct($data_handler, $pluginId, $error); - - $failedAuthenticationEvent->setResponse($response); - - $failedAuthenticationEvent->method('getError') - ->willReturn($error); - - $failedAuthenticationEvent->method('getPluginId') - ->willReturn($pluginId); + public function testFailedAuthenticationEvent() { - $failedAuthenticationEvent->method('getDataHandler') - ->willReturn($data_handler); + $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(['getDataHandler', + 'getPluginId', + 'getError', + ]) + ->getMock(); + // parent::__construct($data_handler, $pluginId, $error);. + $failedAuthenticationEvent->setResponse($response); + + $failedAuthenticationEvent->method('getError') + ->willReturn($error); + + $failedAuthenticationEvent->method('getPluginId') + ->willReturn($pluginId); + + $failedAuthenticationEvent->method('getDataHandler') + ->willReturn($data_handler); - $this->assertTrue( - method_exists($failedAuthenticationEvent, 'getDataHandler'), - 'FailedAuthenticationEvent class does not implements getDataHandler function/method' - ); + $this->assertTrue( + method_exists($failedAuthenticationEvent, 'getDataHandler'), + 'FailedAuthenticationEvent class does not implements getDataHandler function/method' + ); - $this->assertTrue( + $this->assertTrue( method_exists($failedAuthenticationEvent, 'getPluginId'), 'FailedAuthenticationEvent class does not implements getPluginId function/method' ); - $this->assertTrue( + $this->assertTrue( method_exists($failedAuthenticationEvent, 'getError'), 'FailedAuthenticationEvent class does not implements getError function/method' ); - $this->assertTrue( + $this->assertTrue( method_exists($failedAuthenticationEvent, 'getResponse'), 'FailedAuthenticationEvent class does not implements getResponse function/method' ); - $this->assertTrue( + $this->assertTrue( method_exists($failedAuthenticationEvent, 'setResponse'), 'FailedAuthenticationEvent class does not implements setResponse function/method' ); - $this->assertTrue( + $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(array($user, $pluginId)) - ->getMock(); - - $userEvent->method('getPluginId') - ->willReturn($pluginId); - $userEvent->method('getUser') - ->willReturn($user); - - $this->assertTrue( - method_exists($userEvent, 'getPluginId'), - 'UserEvent class does not implements getPluginId 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()); + } - $this->assertTrue( - method_exists($userEvent, 'getUser'), - 'UserEvent class does not implements getUser function/method' - ); + /** + * 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'); + } - $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 = array('userfield', 'userfield2'); - - $userFieldsEvent = $this->getMockBuilder(UserFieldsEvent::class) - ->setConstructorArgs(array($user_fields, $pluginId)) - ->setMethods(array('getPluginId')) - ->getMock(); - - $userFieldsEvent->method('getPluginId') - ->willReturn($pluginId); - $userFieldsEvent->setUserFields($user_fields); - - $this->assertTrue( - method_exists($userFieldsEvent, 'getUserFields'), - 'UserFieldsEvent does not implements getUserFields function/method' - ); + /** + * Tests for class UserEvent. + */ + public function testUserEvent() { + $pluginId = 'drupal123'; + $user = $this->createMock(UserInterface::class); - $this->assertTrue( - method_exists($userFieldsEvent, 'setUserFields'), - 'UserFieldsEvent does not implements setUserFields function/method' - ); + $userEvent = $this->getMockBuilder(UserEvent::class) + ->setConstructorArgs([$user, $pluginId]) + ->getMock(); - $this->assertTrue( - method_exists($userFieldsEvent, 'getPluginId'), - 'UserFieldsEvent does not implements getPluginId function/method' - ); + $userEvent->method('getPluginId') + ->willReturn($pluginId); + $userEvent->method('getUser') + ->willReturn($user); + + $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(['getPluginId']) + ->getMock(); + + $userFieldsEvent->method('getPluginId') + ->willReturn($pluginId); + $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()); + } - $this->assertSame('drupal123', $userFieldsEvent->getPluginId()); - $this->assertEquals($user_fields, $userFieldsEvent->getUserFields()); - } } From acb347f77188929ee9cd455b10a5d9cc7395e1e4 Mon Sep 17 00:00:00 2001 From: neerajpandey Date: Sun, 16 Jun 2019 21:21:05 +0530 Subject: [PATCH 30/42] Fixed coding standard violations in test class NetworkTest --- tests/src/Unit/PluginTest.php | 49 +++++++++++++++++++++-------------- 1 file changed, 30 insertions(+), 19 deletions(-) diff --git a/tests/src/Unit/PluginTest.php b/tests/src/Unit/PluginTest.php index bddb502..ff5ff37 100644 --- a/tests/src/Unit/PluginTest.php +++ b/tests/src/Unit/PluginTest.php @@ -2,32 +2,35 @@ use Drupal\Tests\UnitTestCase; use Drupal\social_auth\Plugin\Block\SocialAuthLoginBlock; -use Drupal\Core\Block\BlockBase; use Drupal\Core\Config\ImmutableConfig; -use Drupal\Core\Plugin\ContainerFactoryPluginInterface; -use Symfony\Component\DependencyInjection\ContainerInterface; use Drupal\social_auth\Plugin\Network\NetworkBase; use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Logger\LoggerChannelFactoryInterface; use Drupal\Core\Site\Settings; -use Drupal\social_api\Plugin\NetworkBase as SocialApiNetworkBase; - +/** + * Defines Test Class for Plugin. + */ class NetworkTest extends UnitTestCase { + /** - * tests for class SocialAuthLoginBlock + * Tests for class SocialAuthLoginBlock. */ - public function testSocialAuthLoginBlock () { - $configuration = array(); + public function testSocialAuthLoginBlock() { + $configuration = []; $social_auth_config = $this->createMock(ImmutableConfig::class); $socialAuthLoginBlock = $this->getMockBuilder(SocialAuthLoginBlock::class) - ->setConstructorArgs(array($configuration, 'drupalPlugin', 'definitionOfPlugin', $social_auth_config)) - ->getMock(); + ->setConstructorArgs([$configuration, + 'drupalPlugin', + 'definitionOfPlugin', + $social_auth_config, + ]) + ->getMock(); $socialAuthLoginBlock->method('build') - ->willReturn(['#theme' => 'login_with', '#social_networks' => $social_auth_config->get('auth')]); + ->willReturn(['#theme' => 'login_with', '#social_networks' => $social_auth_config->get('auth')]); $this->assertTrue( method_exists($socialAuthLoginBlock, 'create'), @@ -43,25 +46,33 @@ public function testSocialAuthLoginBlock () { } /** - * tests for class NetWorkbase + * Tests for class NetWorkbase. */ - public function testNetworkBase () { - $configuration = array(); - $plugin_definition = array(); - $setting = array(); + public function testNetworkBase() { + $configuration = []; + $plugin_definition = []; + $setting = []; + $entity_type_manager = $this->createMock(EntityTypeManagerInterface::class); $config_factory = $this->createMock(ConfigFactoryInterface::class); $logger_factory = $this->createMock(LoggerChannelFactoryInterface::class); $settings = new Settings($setting); - // $settings = new ReflectionClass(Settings::class); $networkBase = $this->getMockBuilder(NetworkBase::class) - ->setConstructorArgs(array($configuration, 'drupalPlugin123', $plugin_definition, $entity_type_manager, $config_factory, $logger_factory, $settings)) - ->getMockForAbstractClass(); + ->setConstructorArgs([$configuration, + 'drupalPlugin123', + $plugin_definition, + $entity_type_manager, + $config_factory, + $logger_factory, + $settings, + ]) + ->getMockForAbstractClass(); $this->assertTrue( method_exists($networkBase, 'create'), 'NetworkBase does not implements create function/method' ); } + } From af0f424a7b0b4caca6bc32046868563028a19442 Mon Sep 17 00:00:00 2001 From: neerajpandey Date: Sun, 16 Jun 2019 21:23:39 +0530 Subject: [PATCH 31/42] Fixed coding standard violations in SettingsTest class --- tests/src/Unit/SettingsTest.php | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/tests/src/Unit/SettingsTest.php b/tests/src/Unit/SettingsTest.php index 823cf43..4a420c3 100644 --- a/tests/src/Unit/SettingsTest.php +++ b/tests/src/Unit/SettingsTest.php @@ -1,33 +1,40 @@ createMock(StorageInterface::class); $event_dispatcher = $this->createMock(EventDispatcherInterface::class); $typed_config = $this->createMock(TypedConfigManagerInterface::class); $configs = $this->getMockBuilder(ImmutableConfig::class) - ->setConstructorArgs(array('drupalConfig123', $storage, $event_dispatcher, $typed_config)) - ->getMock(); + ->setConstructorArgs(['drupalConfig123', + $storage, + $event_dispatcher, + $typed_config, + ]) + ->getMock(); $collection = $this->getMockBuilder(SettingsBase::class) - ->setConstructorArgs(array($configs)) - ->getMock(); + ->setConstructorArgs([$configs]) + ->getMock(); - //writing this assertion, otherwise the test will throw a warning. And this test - //is quite important as its checking for parent methods. + // Writing this assertion, otherwise the test will throw a warning. And this + // test is quite important as its checking for parent methods. $this->assertTrue($collection instanceof SettingsBase); } + } From 05ff25210efd90e5f314b8692391acfdcab40410 Mon Sep 17 00:00:00 2001 From: neerajp99 Date: Sun, 16 Jun 2019 21:26:32 +0530 Subject: [PATCH 32/42] Edit test class EventTest --- tests/src/Unit/EventTest.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/src/Unit/EventTest.php b/tests/src/Unit/EventTest.php index af1757f..1f8ed2e 100644 --- a/tests/src/Unit/EventTest.php +++ b/tests/src/Unit/EventTest.php @@ -178,6 +178,7 @@ public function testUserEvent() { $userEvent->method('getPluginId') ->willReturn($pluginId); + $userEvent->method('getUser') ->willReturn($user); @@ -210,6 +211,7 @@ public function testUserFieldsEvent() { $userFieldsEvent->method('getPluginId') ->willReturn($pluginId); + $userFieldsEvent->setUserFields($user_fields); $this->assertTrue( From 32d9ecbdd961e3a974e7c45f2fdce79ca5c5d89e Mon Sep 17 00:00:00 2001 From: neerajp99 Date: Mon, 17 Jun 2019 19:20:09 +0530 Subject: [PATCH 33/42] Fixed typo in test class AuthManagerTest --- tests/src/Unit/AuthManagerTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/src/Unit/AuthManagerTest.php b/tests/src/Unit/AuthManagerTest.php index 4a139c0..d5f6d3f 100644 --- a/tests/src/Unit/AuthManagerTest.php +++ b/tests/src/Unit/AuthManagerTest.php @@ -26,7 +26,7 @@ public function testOAuth2Manager() { ->setConstructorArgs([$settings, $logger_factory, ]) - ->setMethods([( + ->setMethods([ 'getScopes', 'getEndPoints', 'settings', From f173f7be91cb3c48f671058bd0d33c53269893fd Mon Sep 17 00:00:00 2001 From: neerajp99 Date: Mon, 17 Jun 2019 20:28:30 +0530 Subject: [PATCH 34/42] Edit tests class EventTest --- tests/src/Unit/EventTest.php | 40 +++++------------------------------- 1 file changed, 5 insertions(+), 35 deletions(-) diff --git a/tests/src/Unit/EventTest.php b/tests/src/Unit/EventTest.php index 1f8ed2e..bc3618d 100644 --- a/tests/src/Unit/EventTest.php +++ b/tests/src/Unit/EventTest.php @@ -29,20 +29,9 @@ public function testBeforeRedirectEvent() { $pluginId, $destination, ]) - ->setMethods(['getDataHandler', - 'getPluginId', - 'getDestination', - ]) + ->setMethods(null) ->getMock(); - $beforeRedirectEvent->method('getPluginId') - ->willReturn($pluginId); - - $beforeRedirectEvent->method('getDestination') - ->willReturn($destination); - - $beforeRedirectEvent->method('getDataHandler') - ->willReturn($data_handler); $this->assertTrue( method_exists($beforeRedirectEvent, 'getDataHandler'), @@ -79,23 +68,12 @@ public function testFailedAuthenticationEvent() { $pluginId, $error, ]) - ->setMethods(['getDataHandler', - 'getPluginId', - 'getError', - ]) + ->setMethods(null) ->getMock(); + // parent::__construct($data_handler, $pluginId, $error);. $failedAuthenticationEvent->setResponse($response); - $failedAuthenticationEvent->method('getError') - ->willReturn($error); - - $failedAuthenticationEvent->method('getPluginId') - ->willReturn($pluginId); - - $failedAuthenticationEvent->method('getDataHandler') - ->willReturn($data_handler); - $this->assertTrue( method_exists($failedAuthenticationEvent, 'getDataHandler'), 'FailedAuthenticationEvent class does not implements getDataHandler function/method' @@ -174,14 +152,9 @@ public function testUserEvent() { $userEvent = $this->getMockBuilder(UserEvent::class) ->setConstructorArgs([$user, $pluginId]) + ->setMethods(null) ->getMock(); - $userEvent->method('getPluginId') - ->willReturn($pluginId); - - $userEvent->method('getUser') - ->willReturn($user); - $this->assertTrue( method_exists($userEvent, 'getPluginId'), 'UserEvent class does not implements getPluginId function/method' @@ -206,12 +179,9 @@ public function testUserFieldsEvent() { $userFieldsEvent = $this->getMockBuilder(UserFieldsEvent::class) ->setConstructorArgs([$user_fields, $pluginId]) - ->setMethods(['getPluginId']) + ->setMethods(null) ->getMock(); - $userFieldsEvent->method('getPluginId') - ->willReturn($pluginId); - $userFieldsEvent->setUserFields($user_fields); $this->assertTrue( From 18992e0ec47be22152270f0abb60ea4a7dca16b1 Mon Sep 17 00:00:00 2001 From: neerajp99 Date: Fri, 21 Jun 2019 17:03:34 +0530 Subject: [PATCH 35/42] Edit SocialAuth Controller tests --- tests/src/Unit/SocialAuthControllerTest.php | 88 +++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 tests/src/Unit/SocialAuthControllerTest.php diff --git a/tests/src/Unit/SocialAuthControllerTest.php b/tests/src/Unit/SocialAuthControllerTest.php new file mode 100644 index 0000000..321d552 --- /dev/null +++ b/tests/src/Unit/SocialAuthControllerTest.php @@ -0,0 +1,88 @@ +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' + ); + } + +} From a368682154fa877839be0978e1f454656d0a6467 Mon Sep 17 00:00:00 2001 From: neerajp99 Date: Fri, 21 Jun 2019 17:04:56 +0530 Subject: [PATCH 36/42] Edit test class SocialAuthControllerTest.php --- tests/src/Unit/SocialAuthControllerTest.php | 2 +- tests/src/Unit/SocialAuthEntityTest.php | 96 +++++++++++++++++++++ 2 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 tests/src/Unit/SocialAuthEntityTest.php diff --git a/tests/src/Unit/SocialAuthControllerTest.php b/tests/src/Unit/SocialAuthControllerTest.php index 321d552..af1e4f9 100644 --- a/tests/src/Unit/SocialAuthControllerTest.php +++ b/tests/src/Unit/SocialAuthControllerTest.php @@ -17,7 +17,7 @@ /** * Defines Controller Test Class. */ -class ControllerTest extends UnitTestCase { +class SocialAuthControllerTest extends UnitTestCase { /** * Tests for class SocialAuthController. diff --git a/tests/src/Unit/SocialAuthEntityTest.php b/tests/src/Unit/SocialAuthEntityTest.php new file mode 100644 index 0000000..d890d20 --- /dev/null +++ b/tests/src/Unit/SocialAuthEntityTest.php @@ -0,0 +1,96 @@ +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()); + } + +} From 77ba678c33a1033353f248f6fcb965ad51a1d373 Mon Sep 17 00:00:00 2001 From: neerajp99 Date: Fri, 21 Jun 2019 17:05:12 +0530 Subject: [PATCH 37/42] Edit test class SocialAuthEntityTest --- tests/src/Unit/SocialAuthEntityTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/src/Unit/SocialAuthEntityTest.php b/tests/src/Unit/SocialAuthEntityTest.php index d890d20..f19cf25 100644 --- a/tests/src/Unit/SocialAuthEntityTest.php +++ b/tests/src/Unit/SocialAuthEntityTest.php @@ -6,7 +6,7 @@ /** * Defines Test Class for Social Auth Entity. */ -class EntityTest extends UnitTestCase { +class SocialAuthEntityTest extends UnitTestCase { /** * Tests for class SocialAuth. From 25712dc107b89170419ab66138925567b8fed02a Mon Sep 17 00:00:00 2001 From: neerajp99 Date: Fri, 21 Jun 2019 17:05:25 +0530 Subject: [PATCH 38/42] Edit test class SocialAuthEvent --- tests/src/Unit/SocialAuthEventTest.php | 206 +++++++++++++++++++++++++ 1 file changed, 206 insertions(+) create mode 100644 tests/src/Unit/SocialAuthEventTest.php diff --git a/tests/src/Unit/SocialAuthEventTest.php b/tests/src/Unit/SocialAuthEventTest.php new file mode 100644 index 0000000..2dc66c6 --- /dev/null +++ b/tests/src/Unit/SocialAuthEventTest.php @@ -0,0 +1,206 @@ +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()); + } + +} From e195896e8135ad84f8eae24f0a60ecef30108d01 Mon Sep 17 00:00:00 2001 From: neerajp99 Date: Fri, 21 Jun 2019 17:05:49 +0530 Subject: [PATCH 39/42] Edit test class SocialAuthMaagerTest --- tests/src/Unit/SocialAuthManagerTest.php | 106 +++++++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 tests/src/Unit/SocialAuthManagerTest.php diff --git a/tests/src/Unit/SocialAuthManagerTest.php b/tests/src/Unit/SocialAuthManagerTest.php new file mode 100644 index 0000000..1ec232a --- /dev/null +++ b/tests/src/Unit/SocialAuthManagerTest.php @@ -0,0 +1,106 @@ +createMock(LoggerChannelFactoryInterface::class); + $settings = $this->createMock(Config::class); + + $authManager = $this->getMockBuilder(OAuth2Manager::class) + ->setConstructorArgs([$settings, + $logger_factory, + ]) + ->setMethods([ + 'getScopes', + 'getEndPoints' + ]) + ->getMockForAbstractClass(); + + $this->assertTrue( + method_exists($authManager, 'getExtraDetails'), + 'OAuth2Manager does not have getExtraDetails function/method' + ); + + $this->assertTrue( + method_exists($authManager, 'getScopes'), + 'OAuth2Manager does not have getScopes function/method' + ); + + $this->assertTrue( + method_exists($authManager, 'getEndPoints'), + 'OAuth2Manager does not have ggetEndPoints function/method' + ); + + $this->assertEquals(NULL, $authManager->getExtraDetails()); + + $settings->method('get') + ->willReturn('drupal123'); + + if ($scopes === FALSE) { + $scopes = $settings->get('scopes'); + } + $authManager->method('getScopes') + ->willReturn($scopes); + + if ($endPoints === FALSE) { + $endPoints = $settings->get('endpoints'); + } + $authManager->method('getEndPoints') + ->willReturn($endPoints); + + $this->assertSame('drupal123', $authManager->getScopes()); + $this->assertSame('drupal123', $authManager->getEndPoints()); + // print_r(get_class_methods($settings)); + + } + + /** + * Tests for clas OAuth2ManagerInterface. + */ + public function testOAuth2ManagerInterface() { + + $method = "drupalmethod"; + $path = "drupalpath"; + + $authManagerInterface = $this->getMock(OAuth2ManagerInterface::class); + + $authManagerInterface->requestEndPoint($method, $path, $domain = NULL, $options = []); + + $this->assertTrue( + method_exists($authManagerInterface, 'getExtraDetails'), + 'OAuth2ManagerInterface does not have getExtraDetails function/method' + ); + + $this->assertTrue( + method_exists($authManagerInterface, 'requestEndPoint'), + 'OAuth2ManagerInterface does not have requestEndPoint function/method' + ); + + $this->assertTrue( + method_exists($authManagerInterface, 'getScopes'), + 'OAuth2ManagerInterface does not have getScopes function/method' + ); + + $this->assertTrue( + method_exists($authManagerInterface, 'getEndPoints'), + 'OAuth2ManagerInterface does not have getEndPoints function/method' + ); + } + +} From 368e5a40c0bdcaf3cd5ee1cb7bf6ac7bc0937654 Mon Sep 17 00:00:00 2001 From: neerajp99 Date: Fri, 21 Jun 2019 17:06:09 +0530 Subject: [PATCH 40/42] Edit test class SocialAuthPluginTest --- tests/src/Unit/SocialAuthPluginTest.php | 78 +++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 tests/src/Unit/SocialAuthPluginTest.php diff --git a/tests/src/Unit/SocialAuthPluginTest.php b/tests/src/Unit/SocialAuthPluginTest.php new file mode 100644 index 0000000..202df69 --- /dev/null +++ b/tests/src/Unit/SocialAuthPluginTest.php @@ -0,0 +1,78 @@ +createMock(ImmutableConfig::class); + + $socialAuthLoginBlock = $this->getMockBuilder(SocialAuthLoginBlock::class) + ->setConstructorArgs([$configuration, + 'drupalPlugin', + 'definitionOfPlugin', + $social_auth_config, + ]) + ->getMock(); + + $socialAuthLoginBlock->method('build') + ->willReturn(['#theme' => 'login_with', '#social_networks' => $social_auth_config->get('auth')]); + + $this->assertTrue( + method_exists($socialAuthLoginBlock, 'create'), + 'SocialAuthLoginBlock class does not implements create function/method' + ); + + $this->assertTrue( + method_exists($socialAuthLoginBlock, 'build'), + 'SocialAuthLoginBlock class does not implements build function/method' + ); + + $this->assertEquals(['#theme' => 'login_with', '#social_networks' => $social_auth_config->get('auth')], $socialAuthLoginBlock->build()); + } + + /** + * Tests for class NetWorkbase. + */ + public function testNetworkBase() { + $configuration = []; + $plugin_definition = []; + $setting = []; + + $entity_type_manager = $this->createMock(EntityTypeManagerInterface::class); + $config_factory = $this->createMock(ConfigFactoryInterface::class); + $logger_factory = $this->createMock(LoggerChannelFactoryInterface::class); + $settings = new Settings($setting); + + $networkBase = $this->getMockBuilder(NetworkBase::class) + ->setConstructorArgs([$configuration, + 'drupalPlugin123', + $plugin_definition, + $entity_type_manager, + $config_factory, + $logger_factory, + $settings, + ]) + ->getMockForAbstractClass(); + + $this->assertTrue( + method_exists($networkBase, 'create'), + 'NetworkBase does not implements create function/method' + ); + } + +} From 206b7ccb9ec72f06a57e74123eb30053c7e84c4d Mon Sep 17 00:00:00 2001 From: neerajp99 Date: Fri, 21 Jun 2019 17:06:52 +0530 Subject: [PATCH 41/42] Edit test class SocialAuthSettingsTest --- tests/src/Unit/SocialAuthSettingsTest.php | 40 +++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 tests/src/Unit/SocialAuthSettingsTest.php diff --git a/tests/src/Unit/SocialAuthSettingsTest.php b/tests/src/Unit/SocialAuthSettingsTest.php new file mode 100644 index 0000000..730e18e --- /dev/null +++ b/tests/src/Unit/SocialAuthSettingsTest.php @@ -0,0 +1,40 @@ +createMock(StorageInterface::class); + $event_dispatcher = $this->createMock(EventDispatcherInterface::class); + $typed_config = $this->createMock(TypedConfigManagerInterface::class); + + $configs = $this->getMockBuilder(ImmutableConfig::class) + ->setConstructorArgs(['drupalConfig123', + $storage, + $event_dispatcher, + $typed_config, + ]) + ->getMock(); + + $collection = $this->getMockBuilder(SettingsBase::class) + ->setConstructorArgs([$configs]) + ->getMock(); + + // Writing this assertion, otherwise the test will throw a warning. And this + // test is quite important as its checking for parent methods. + $this->assertTrue($collection instanceof SettingsBase); + } + +} From 6e145cb36f61ec3d69949599d10b1d262940a6bb Mon Sep 17 00:00:00 2001 From: neerajp99 Date: Fri, 21 Jun 2019 17:07:06 +0530 Subject: [PATCH 42/42] Reame the classNames and file --- tests/src/Unit/AuthManagerTest.php | 104 --------------- tests/src/Unit/ControllerTest.php | 86 ------------ tests/src/Unit/EntityTest.php | 96 -------------- tests/src/Unit/EventTest.php | 206 ----------------------------- tests/src/Unit/PluginTest.php | 78 ----------- tests/src/Unit/SettingsTest.php | 40 ------ 6 files changed, 610 deletions(-) delete mode 100644 tests/src/Unit/AuthManagerTest.php delete mode 100644 tests/src/Unit/ControllerTest.php delete mode 100644 tests/src/Unit/EntityTest.php delete mode 100644 tests/src/Unit/EventTest.php delete mode 100644 tests/src/Unit/PluginTest.php delete mode 100644 tests/src/Unit/SettingsTest.php diff --git a/tests/src/Unit/AuthManagerTest.php b/tests/src/Unit/AuthManagerTest.php deleted file mode 100644 index d5f6d3f..0000000 --- a/tests/src/Unit/AuthManagerTest.php +++ /dev/null @@ -1,104 +0,0 @@ -createMock(LoggerChannelFactoryInterface::class); - $settings = $this->createMock(Config::class); - - $authManager = $this->getMockBuilder(OAuth2Manager::class) - ->setConstructorArgs([$settings, - $logger_factory, - ]) - ->setMethods([ - 'getScopes', - 'getEndPoints', - 'settings', - 'get', - ]) - ->getMockForAbstractClass(); - - $this->assertTrue( - method_exists($authManager, 'getExtraDetails'), - 'OAuth2Manager does not have getExtraDetails function/method' - ); - - $this->assertTrue( - method_exists($authManager, 'getScopes'), - 'OAuth2Manager does not have getScopes function/method' - ); - - $this->assertTrue( - method_exists($authManager, 'getEndPoints'), - 'OAuth2Manager does not have ggetEndPoints function/method' - ); - - $settings->method('get') - ->willReturn('drupal123'); - - if ($scopes === FALSE) { - $scopes = $settings->get('scopes'); - } - $authManager->method('getScopes') - ->willReturn($scopes); - - if ($endPoints === FALSE) { - $endPoints = $settings->get('endpoints'); - } - $authManager->method('getEndPoints') - ->willReturn($endPoints); - - $this->assertSame('drupal123', $authManager->getScopes()); - $this->assertSame('drupal123', $authManager->getEndPoints()); - } - - /** - * Tests for clas OAuth2ManagerInterface. - */ - public function testOAuth2ManagerInterface() { - - $method = "drupalmethod"; - $path = "drupalpath"; - - $authManagerInterface = $this->getMock(OAuth2ManagerInterface::class); - - $authManagerInterface->requestEndPoint($method, $path, $domain = NULL, $options = []); - - $this->assertTrue( - method_exists($authManagerInterface, 'getExtraDetails'), - 'OAuth2ManagerInterface does not have getExtraDetails function/method' - ); - - $this->assertTrue( - method_exists($authManagerInterface, 'requestEndPoint'), - 'OAuth2ManagerInterface does not have requestEndPoint function/method' - ); - - $this->assertTrue( - method_exists($authManagerInterface, 'getScopes'), - 'OAuth2ManagerInterface does not have getScopes function/method' - ); - - $this->assertTrue( - method_exists($authManagerInterface, 'getEndPoints'), - 'OAuth2ManagerInterface does not have getEndPoints function/method' - ); - } - -} diff --git a/tests/src/Unit/ControllerTest.php b/tests/src/Unit/ControllerTest.php deleted file mode 100644 index 1c59028..0000000 --- a/tests/src/Unit/ControllerTest.php +++ /dev/null @@ -1,86 +0,0 @@ -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]) - ->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, - ]) - ->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' - ); - } - -} diff --git a/tests/src/Unit/EntityTest.php b/tests/src/Unit/EntityTest.php deleted file mode 100644 index d890d20..0000000 --- a/tests/src/Unit/EntityTest.php +++ /dev/null @@ -1,96 +0,0 @@ -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()); - } - -} diff --git a/tests/src/Unit/EventTest.php b/tests/src/Unit/EventTest.php deleted file mode 100644 index bc3618d..0000000 --- a/tests/src/Unit/EventTest.php +++ /dev/null @@ -1,206 +0,0 @@ -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()); - } - -} diff --git a/tests/src/Unit/PluginTest.php b/tests/src/Unit/PluginTest.php deleted file mode 100644 index ff5ff37..0000000 --- a/tests/src/Unit/PluginTest.php +++ /dev/null @@ -1,78 +0,0 @@ -createMock(ImmutableConfig::class); - - $socialAuthLoginBlock = $this->getMockBuilder(SocialAuthLoginBlock::class) - ->setConstructorArgs([$configuration, - 'drupalPlugin', - 'definitionOfPlugin', - $social_auth_config, - ]) - ->getMock(); - - $socialAuthLoginBlock->method('build') - ->willReturn(['#theme' => 'login_with', '#social_networks' => $social_auth_config->get('auth')]); - - $this->assertTrue( - method_exists($socialAuthLoginBlock, 'create'), - 'SocialAuthLoginBlock class does not implements create function/method' - ); - - $this->assertTrue( - method_exists($socialAuthLoginBlock, 'build'), - 'SocialAuthLoginBlock class does not implements build function/method' - ); - - $this->assertEquals(['#theme' => 'login_with', '#social_networks' => $social_auth_config->get('auth')], $socialAuthLoginBlock->build()); - } - - /** - * Tests for class NetWorkbase. - */ - public function testNetworkBase() { - $configuration = []; - $plugin_definition = []; - $setting = []; - - $entity_type_manager = $this->createMock(EntityTypeManagerInterface::class); - $config_factory = $this->createMock(ConfigFactoryInterface::class); - $logger_factory = $this->createMock(LoggerChannelFactoryInterface::class); - $settings = new Settings($setting); - - $networkBase = $this->getMockBuilder(NetworkBase::class) - ->setConstructorArgs([$configuration, - 'drupalPlugin123', - $plugin_definition, - $entity_type_manager, - $config_factory, - $logger_factory, - $settings, - ]) - ->getMockForAbstractClass(); - - $this->assertTrue( - method_exists($networkBase, 'create'), - 'NetworkBase does not implements create function/method' - ); - } - -} diff --git a/tests/src/Unit/SettingsTest.php b/tests/src/Unit/SettingsTest.php deleted file mode 100644 index 4a420c3..0000000 --- a/tests/src/Unit/SettingsTest.php +++ /dev/null @@ -1,40 +0,0 @@ -createMock(StorageInterface::class); - $event_dispatcher = $this->createMock(EventDispatcherInterface::class); - $typed_config = $this->createMock(TypedConfigManagerInterface::class); - - $configs = $this->getMockBuilder(ImmutableConfig::class) - ->setConstructorArgs(['drupalConfig123', - $storage, - $event_dispatcher, - $typed_config, - ]) - ->getMock(); - - $collection = $this->getMockBuilder(SettingsBase::class) - ->setConstructorArgs([$configs]) - ->getMock(); - - // Writing this assertion, otherwise the test will throw a warning. And this - // test is quite important as its checking for parent methods. - $this->assertTrue($collection instanceof SettingsBase); - } - -}