Skip to content

Commit 2b141d8

Browse files
committed
New. BaseCall. Default params class.
1 parent becd642 commit 2b141d8

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

0 commit comments

Comments
 (0)