|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace { |
| 4 | + if ( ! class_exists('NextendSocialProviderDummy', false) ) { |
| 5 | + class NextendSocialProviderDummy |
| 6 | + { |
| 7 | + private $auth_user_data = array(); |
| 8 | + |
| 9 | + public function __construct(array $auth_user_data = array()) |
| 10 | + { |
| 11 | + $this->auth_user_data = $auth_user_data; |
| 12 | + } |
| 13 | + |
| 14 | + public function getAuthUserData($key) |
| 15 | + { |
| 16 | + return $this->auth_user_data[$key] ?? null; |
| 17 | + } |
| 18 | + } |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +namespace Cleantalk\Antispam\Integrations { |
| 23 | + |
| 24 | + if ( ! function_exists(__NAMESPACE__ . '\\ct_die') ) { |
| 25 | + function ct_die($message = null, $code = null) |
| 26 | + { |
| 27 | + $GLOBALS['nextend_social_login_test_ct_die'] = array( |
| 28 | + 'message' => $message, |
| 29 | + 'code' => $code, |
| 30 | + ); |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + class TestNextendSocialLogin extends \ApbctTestCase |
| 35 | + { |
| 36 | + protected function setUp(): void |
| 37 | + { |
| 38 | + parent::setUp(); |
| 39 | + $GLOBALS['nextend_social_login_test_ct_die'] = null; |
| 40 | + $GLOBALS['ct_comment'] = null; |
| 41 | + } |
| 42 | + |
| 43 | + protected function tearDown(): void |
| 44 | + { |
| 45 | + unset($GLOBALS['nextend_social_login_test_ct_die']); |
| 46 | + $GLOBALS['ct_comment'] = null; |
| 47 | + parent::tearDown(); |
| 48 | + } |
| 49 | + |
| 50 | + public function testGetDataForCheckingReturnsEmailAndNicknameForNextendProvider() |
| 51 | + { |
| 52 | + $integration = new NextendSocialLogin(); |
| 53 | + $provider = new \NextendSocialProviderDummy(array( |
| 54 | + 'email' => 'user@example.com', |
| 55 | + 'name' => 'User Name', |
| 56 | + )); |
| 57 | + |
| 58 | + $this->assertSame( |
| 59 | + array( |
| 60 | + 'email' => 'user@example.com', |
| 61 | + 'nickname' => 'User Name', |
| 62 | + ), |
| 63 | + $integration->getDataForChecking($provider) |
| 64 | + ); |
| 65 | + } |
| 66 | + |
| 67 | + public function testGetDataForCheckingReturnsNullForUnsupportedArgument() |
| 68 | + { |
| 69 | + $integration = new NextendSocialLogin(); |
| 70 | + |
| 71 | + $this->assertNull($integration->getDataForChecking(new \stdClass())); |
| 72 | + } |
| 73 | + |
| 74 | + public function testDoBlockSetsGlobalCommentAndInvokesCtDie() |
| 75 | + { |
| 76 | + $integration = new NextendSocialLogin(); |
| 77 | + |
| 78 | + $integration->doBlock('blocked message'); |
| 79 | + |
| 80 | + $this->assertSame('blocked message', $GLOBALS['ct_comment']); |
| 81 | + $this->assertSame( |
| 82 | + array( |
| 83 | + 'message' => null, |
| 84 | + 'code' => null, |
| 85 | + ), |
| 86 | + $GLOBALS['nextend_social_login_test_ct_die'] |
| 87 | + ); |
| 88 | + } |
| 89 | + |
| 90 | + public function testIsOAuthProviderEnabledReturnsFalseForInvalidProviderId() |
| 91 | + { |
| 92 | + $this->assertFalse(NextendSocialLogin::isOAuthProviderEnabled('')); |
| 93 | + $this->assertFalse(NextendSocialLogin::isOAuthProviderEnabled(123)); |
| 94 | + $this->assertFalse(NextendSocialLogin::isOAuthProviderEnabled(null)); |
| 95 | + } |
| 96 | + |
| 97 | + public function testIsOAuthProviderEnabledReturnsTrueWhenClientSecretExists() |
| 98 | + { |
| 99 | + |
| 100 | + update_option('nextend_social_login_provider', serialize(array( |
| 101 | + 'client_secret' => 'secret-value', |
| 102 | + ))); |
| 103 | + |
| 104 | + $this->assertTrue(NextendSocialLogin::isOAuthProviderEnabled('nextend_social_login_provider')); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | +} |
0 commit comments