Skip to content

Commit 4d6b59f

Browse files
committed
New. BaseCall. Default params class.
1 parent becd642 commit 4d6b59f

3 files changed

Lines changed: 287 additions & 21 deletions

File tree

inc/cleantalk-common.php

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
use Cleantalk\Antispam\CleantalkRequest;
55
use Cleantalk\Antispam\CleantalkResponse;
66
use Cleantalk\ApbctWP\API;
7+
use Cleantalk\ApbctWP\BaseCall\BaseCallDefaultParams;
78
use Cleantalk\ApbctWP\CleantalkSettingsTemplates;
89
use Cleantalk\ApbctWP\Cron;
910
use Cleantalk\ApbctWP\DB;
@@ -12,14 +13,14 @@
1213
use Cleantalk\ApbctWP\GetFieldsAny;
1314
use Cleantalk\ApbctWP\Helper;
1415
use Cleantalk\ApbctWP\Honeypot;
16+
use Cleantalk\ApbctWP\RequestParameters\RequestParameters;
17+
use Cleantalk\ApbctWP\RequestParameters\SubmitTimeHandler;
1518
use Cleantalk\ApbctWP\Sanitize;
1619
use Cleantalk\ApbctWP\Variables\AltSessions;
1720
use Cleantalk\ApbctWP\Variables\Cookie;
1821
use Cleantalk\ApbctWP\Variables\Get;
1922
use Cleantalk\ApbctWP\Variables\Post;
2023
use Cleantalk\ApbctWP\Variables\Server;
21-
use Cleantalk\ApbctWP\RequestParameters\RequestParameters;
22-
use Cleantalk\ApbctWP\RequestParameters\SubmitTimeHandler;
2324
use Cleantalk\Common\TT;
2425

2526
// Prevent direct call
@@ -182,25 +183,8 @@ function apbct_base_call($params = array(), $reg_flag = false)
182183
);
183184
}
184185

185-
$default_params = array(
186-
187-
// IPs
188-
'sender_ip' => defined('CT_TEST_IP')
189-
? CT_TEST_IP
190-
: \Cleantalk\ApbctWP\Helper::ipGet('remote_addr', false),
191-
'x_forwarded_for' => \Cleantalk\ApbctWP\Helper::ipGet('x_forwarded_for', false),
192-
'x_real_ip' => \Cleantalk\ApbctWP\Helper::ipGet('x_real_ip', false),
193-
194-
// Misc
195-
'auth_key' => $apbct->api_key,
196-
'js_on' => apbct_js_test(Sanitize::cleanTextField(Cookie::getString('ct_checkjs')), true)
197-
? 1
198-
: apbct_js_test(Post::getString('ct_checkjs')),
199-
200-
'agent' => APBCT_AGENT,
201-
'sender_info' => $sender_info,
202-
'submit_time' => SubmitTimeHandler::getFromRequest(),
203-
);
186+
$default_params_getter = new BaseCallDefaultParams($apbct->api_key, $sender_info);
187+
$default_params = $default_params_getter->get();
204188

205189
if (!isset($params['post_info']['post_url'])) {
206190
$params['post_info']['post_url'] = Server::get('HTTP_REFERER');
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
<?php
2+
3+
namespace Cleantalk\ApbctWP\BaseCall;
4+
5+
use Cleantalk\ApbctWP\RequestParameters\SubmitTimeHandler;
6+
use Cleantalk\ApbctWP\Sanitize;
7+
use Cleantalk\ApbctWP\Variables\Cookie;
8+
use Cleantalk\ApbctWP\Variables\Post;
9+
10+
class BaseCallDefaultParams
11+
{
12+
private $auth_key = '';
13+
private $sender_info = [];
14+
15+
public function __construct($auth_key, $sender_info = [])
16+
{
17+
$this->auth_key = $auth_key;
18+
$this->sender_info = $sender_info;
19+
}
20+
21+
public function get()
22+
{
23+
$test_ip = $this->getTestIp();
24+
25+
return [
26+
'sender_ip' => $this->getSenderIP($test_ip),
27+
'x_forwarded_for' => $this->getXForwardedForIP(),
28+
'x_real_ip' => $this->getXRealIP(),
29+
'auth_key' => $this->auth_key,
30+
'js_on' => $this->getJsOn(),
31+
'agent' => $this->getAgent(),
32+
'sender_info' => $this->sender_info,
33+
'submit_time' => $this->getSubmittime(),
34+
];
35+
}
36+
37+
public function getSenderIP($test_ip = null)
38+
{
39+
return $test_ip ?? $this->ipGet('remote_addr');
40+
}
41+
42+
public function getXForwardedForIP()
43+
{
44+
return $this->ipGet('x_forwarded_for');
45+
}
46+
47+
public function getXRealIP()
48+
{
49+
return $this->ipGet('x_real_ip');
50+
}
51+
52+
public function getJsOn()
53+
{
54+
$cookieValue = Sanitize::cleanTextField(
55+
$this->getCookieValue('ct_checkjs')
56+
);
57+
58+
return $this->jsTest($cookieValue, true)
59+
? 1
60+
: $this->jsTest($this->getPostValue('ct_checkjs'));
61+
}
62+
63+
public function getSubmittime()
64+
{
65+
return $this->getSubmitTimeFromRequest();
66+
}
67+
68+
/**
69+
* ---- Wrapper methods for testability ----
70+
*/
71+
72+
protected function ipGet($type)
73+
{
74+
return \Cleantalk\ApbctWP\Helper::ipGet($type, false);
75+
}
76+
77+
protected function jsTest($value, $is_cookie = false)
78+
{
79+
return apbct_js_test($value, $is_cookie);
80+
}
81+
82+
protected function getCookieValue($key)
83+
{
84+
return Cookie::getString($key);
85+
}
86+
87+
protected function getPostValue($key)
88+
{
89+
return Post::getString($key);
90+
}
91+
92+
protected function getSubmitTimeFromRequest()
93+
{
94+
return SubmitTimeHandler::getFromRequest();
95+
}
96+
97+
protected function getAgent()
98+
{
99+
return APBCT_AGENT;
100+
}
101+
102+
protected function getTestIp()
103+
{
104+
return defined('CT_TEST_IP') && !empty(CT_TEST_IP)
105+
? CT_TEST_IP
106+
: null;
107+
}
108+
}
Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
<?php
2+
3+
namespace tests\Cleantalk\ApbctWP\BaseCall;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use Cleantalk\ApbctWP\BaseCall\BaseCallDefaultParams;
7+
8+
class BaseCallDefaultParamsTest extends TestCase
9+
{
10+
/**
11+
* Базовый helper для создания мока
12+
*/
13+
private function createMockedInstance($authKey = 'test_key', $senderInfo = [])
14+
{
15+
return $this->getMockBuilder(BaseCallDefaultParams::class)
16+
->setConstructorArgs([$authKey, $senderInfo])
17+
->onlyMethods([
18+
'ipGet',
19+
'jsTest',
20+
'getSubmitTimeFromRequest',
21+
'getAgent',
22+
'getTestIp',
23+
'getCookieValue',
24+
'getPostValue',
25+
])
26+
->getMock();
27+
}
28+
29+
public function testGetSenderIPWithTestIp()
30+
{
31+
$obj = $this->createMockedInstance();
32+
33+
$this->assertEquals('1.2.3.4', $obj->getSenderIP('1.2.3.4'));
34+
}
35+
36+
public function testGetSenderIPWithoutTestIp()
37+
{
38+
$obj = $this->createMockedInstance();
39+
40+
$obj->method('ipGet')
41+
->with('remote_addr')
42+
->willReturn('5.6.7.8');
43+
44+
$this->assertEquals('5.6.7.8', $obj->getSenderIP(null));
45+
}
46+
47+
public function testGetXForwardedForIP()
48+
{
49+
$obj = $this->createMockedInstance();
50+
51+
$obj->method('ipGet')
52+
->with('x_forwarded_for')
53+
->willReturn('10.0.0.1');
54+
55+
$this->assertEquals('10.0.0.1', $obj->getXForwardedForIP());
56+
}
57+
58+
public function testGetXRealIP()
59+
{
60+
$obj = $this->createMockedInstance();
61+
62+
$obj->method('ipGet')
63+
->with('x_real_ip')
64+
->willReturn('10.0.0.2');
65+
66+
$this->assertEquals('10.0.0.2', $obj->getXRealIP());
67+
}
68+
69+
public function testGetJsOnFromCookie()
70+
{
71+
$obj = $this->createMockedInstance();
72+
73+
$obj->method('getCookieValue')->willReturn('valid');
74+
75+
$obj->method('jsTest')
76+
->with('valid', true)
77+
->willReturn(true);
78+
79+
$this->assertEquals(1, $obj->getJsOn());
80+
}
81+
82+
public function testGetJsOnFromPost()
83+
{
84+
$obj = $this->createMockedInstance();
85+
86+
$obj->method('getCookieValue')->willReturn('invalid');
87+
88+
$obj->method('jsTest')->willReturnMap([
89+
['invalid', true, false],
90+
['valid', false, true],
91+
]);
92+
93+
$obj->method('getPostValue')->willReturn('valid');
94+
95+
$this->assertEquals(1, $obj->getJsOn());
96+
}
97+
98+
public function testGetJsOnFail()
99+
{
100+
$obj = $this->createMockedInstance();
101+
102+
$obj->method('getCookieValue')->willReturn('invalid');
103+
104+
$obj->method('getPostValue')->willReturn('invalid');
105+
106+
$obj->method('jsTest')->willReturn(false);
107+
108+
$this->assertEquals(0, $obj->getJsOn());
109+
}
110+
111+
public function testGetSubmittime()
112+
{
113+
$obj = $this->createMockedInstance();
114+
115+
$obj->method('getSubmitTimeFromRequest')
116+
->willReturn(123);
117+
118+
$this->assertEquals(123, $obj->getSubmittime());
119+
}
120+
121+
public function testGetFullData()
122+
{
123+
$senderInfo = ['foo' => 'bar'];
124+
125+
$obj = $this->createMockedInstance('test_key', $senderInfo);
126+
127+
$obj->method('getTestIp')->willReturn('1.2.3.4');
128+
$obj->method('getAgent')->willReturn('test-agent');
129+
$obj->method('getSubmitTimeFromRequest')->willReturn(999);
130+
131+
$obj->method('ipGet')->willReturnMap([
132+
['x_forwarded_for', '10.0.0.1'],
133+
['x_real_ip', '10.0.0.2'],
134+
]);
135+
136+
$obj->method('getCookieValue')->willReturn('valid');
137+
$obj->method('jsTest')->willReturn(true);
138+
139+
$result = $obj->get();
140+
141+
$this->assertEquals([
142+
'sender_ip' => '1.2.3.4',
143+
'x_forwarded_for' => '10.0.0.1',
144+
'x_real_ip' => '10.0.0.2',
145+
'auth_key' => 'test_key',
146+
'js_on' => 1,
147+
'agent' => 'test-agent',
148+
'sender_info' => $senderInfo,
149+
'submit_time' => 999,
150+
], $result);
151+
}
152+
153+
// public function testGetUsesRealIpIfNoTestIp()
154+
// {
155+
// $obj = $this->createMockedInstance();
156+
//
157+
// $obj->method('getTestIp')->willReturn(null);
158+
//
159+
// $obj->method('ipGet')
160+
// ->with('remote_addr')
161+
// ->willReturn('8.8.8.8');
162+
//
163+
// // остальные зависимости просто заглушим
164+
// $obj->method('getAgent')->willReturn('agent');
165+
// $obj->method('getSubmitTimeFromRequest')->willReturn(0);
166+
// $obj->method('getCookieValue')->willReturn(null);
167+
// $obj->method('getPostValue')->willReturn(null);
168+
// $obj->method('jsTest')->willReturn(false);
169+
//
170+
// $result = $obj->get();
171+
//
172+
// $this->assertEquals('8.8.8.8', $result['sender_ip']);
173+
// }
174+
}

0 commit comments

Comments
 (0)