PluginProbe
ووسلام – همگام سازی ووکامرس و باسلام / 1.10.20
ووسلام – همگام سازی ووکامرس و باسلام v1.10.20
1.10.19 1.10.20 1.10.18 1.10.17 1.10.15 1.10.14 1.10.13 1.10.12 1.10.10 1.10.9 1.10.8 1.10.7 1.10.6 1.10.5 1.10.4 1.10.3 1.10.2 1.10.1 1.10.0 1.9.2 1.9.1 1.9.0 1.8.8 1.8.5 1.8.6 All 53 releases
sync-basalam / tests / Admin / OAuthManagerTest.php

OAuthManagerTest.php in ووسلام – همگام سازی ووکامرس و باسلام 1.10.20, at tests/Admin/OAuthManagerTest.php

125 lines 3.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace {
4 if (!function_exists('get_current_user_id')) {
5 function get_current_user_id()
6 {
7 return $GLOBALS['sync_basalam_oauth_test_user_id'] ?? 0;
8 }
9 }
10
11 if (!function_exists('wp_generate_password')) {
12 function wp_generate_password($length = 12, $specialChars = true, $extraSpecialChars = false)
13 {
14 return str_repeat('a', (int) $length);
15 }
16 }
17
18 if (!function_exists('wp_salt')) {
19 function wp_salt($scheme = 'auth')
20 {
21 return 'oauth-test-salt-' . $scheme;
22 }
23 }
24
25 if (!function_exists('is_ssl')) {
26 function is_ssl()
27 {
28 return true;
29 }
30 }
31 }
32
33 namespace SyncBasalam\Tests\Admin {
34 use PHPUnit\Framework\TestCase;
35 use ReflectionMethod;
36 use SyncBasalam\Admin\Settings\OAuthManager;
37
38 require_once dirname(__DIR__, 2) . '/includes/Admin/Settings/OAuthManager.php';
39
40 /**
41 * Cookie headers must be exercised before PHPUnit writes progress output.
42 *
43 * @runTestsInSeparateProcesses
44 * @preserveGlobalState disabled
45 */
46 class OAuthManagerTest extends TestCase
47 {
48 protected function setUp(): void
49 {
50 $_COOKIE = [];
51 $GLOBALS['sync_basalam_oauth_test_user_id'] = 42;
52 $GLOBALS['sync_basalam_jobs_runner_test_state'] = [
53 'events' => [],
54 ];
55 }
56
57 protected function tearDown(): void
58 {
59 $_COOKIE = [];
60 unset($GLOBALS['sync_basalam_oauth_test_user_id']);
61 }
62
63 public function testOauthProofDoesNotDependOnTransientStorage(): void
64 {
65 self::assertNotFalse(OAuthManager::issueOauthState());
66 self::assertArrayHasKey(OAuthManager::OAUTH_STATE_COOKIE, $_COOKIE);
67 self::assertNotContains('set_transient', $GLOBALS['sync_basalam_jobs_runner_test_state']['events']);
68
69 self::assertTrue($this->verifyOauthState());
70 self::assertArrayNotHasKey(OAuthManager::OAUTH_STATE_COOKIE, $_COOKIE);
71 }
72
73 public function testOauthProofIsSingleUse(): void
74 {
75 self::assertNotFalse(OAuthManager::issueOauthState());
76 self::assertTrue($this->verifyOauthState());
77 self::assertFalse($this->verifyOauthState());
78 }
79
80 public function testTamperedOauthProofIsRejectedAndConsumed(): void
81 {
82 self::assertNotFalse(OAuthManager::issueOauthState());
83 $_COOKIE[OAuthManager::OAUTH_STATE_COOKIE] .= 'tampered';
84
85 self::assertFalse($this->verifyOauthState());
86 self::assertArrayNotHasKey(OAuthManager::OAUTH_STATE_COOKIE, $_COOKIE);
87 }
88
89 public function testOauthProofIsBoundToTheStartingAdministrator(): void
90 {
91 self::assertNotFalse(OAuthManager::issueOauthState());
92 $GLOBALS['sync_basalam_oauth_test_user_id'] = 99;
93
94 self::assertFalse($this->verifyOauthState());
95 }
96
97 public function testExpiredOauthProofIsRejected(): void
98 {
99 $_COOKIE[OAuthManager::OAUTH_STATE_COOKIE] = $this->buildOauthStateCookieValue(
100 'expired-state',
101 42,
102 time() - 1
103 );
104
105 self::assertFalse($this->verifyOauthState());
106 }
107
108 private function buildOauthStateCookieValue(string $state, int $userId, int $expiresAt): string
109 {
110 $method = new ReflectionMethod(OAuthManager::class, 'buildOauthStateCookieValue');
111 $method->setAccessible(true);
112
113 return (string) $method->invoke(null, $state, $userId, $expiresAt);
114 }
115
116 private function verifyOauthState(): bool
117 {
118 $method = new ReflectionMethod(OAuthManager::class, 'verifyOauthState');
119 $method->setAccessible(true);
120
121 return (bool) $method->invoke(null);
122 }
123 }
124 }
125