Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions lib/Cleantalk/ApbctWP/RequestParameters/SubmitTimeHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
final class SubmitTimeHandler
{
const DEFAULT_VALUE = null; // Default value to return when calculation is disabled or invalid
const REQUEST_PARAM_NAME = 'apbct_timestamp'; // Name of the request parameter for the timestamp
const REQUEST_PARAM_NAME = 'ct_ps_timestamp'; // Name of the request parameter for the timestamp

/**
* Retrieves the time difference between the current time and the timestamp
Expand Down Expand Up @@ -78,6 +78,8 @@ final public static function isCalculationDisabled()
global $apbct;

// Return the value of the bot detector setting
return (bool)$apbct->settings['data__bot_detector_enabled'];
return $apbct->settings['data__bot_detector_enabled']
? !RequestParameters::get('ct_gathering_loaded')
: false;
}
}
62 changes: 10 additions & 52 deletions tests/ApbctWP/TestSubmitTimeHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,80 +8,38 @@ class TestSubmitTimeHandler extends TestCase
public function testGetFromRequestReturnsNullWhenCalculationDisabled()
{
global $apbct;
$apbct = (object) ['settings' => ['data__bot_detector_enabled' => true]];
$apbct = (object) [
'settings' => ['data__bot_detector_enabled' => true],
'data' => ['cookies_type' => 'alternative']
];

$result = SubmitTimeHandler::getFromRequest();

$this->assertNull($result);
}

public function testGetFromRequestReturnsNullWhenTimestampIsZero()
{
global $apbct;
$apbct = (object) ['settings' => ['data__bot_detector_enabled' => false]];

$mock = \Mockery::mock('alias:Cleantalk\ApbctWP\RequestParameters\RequestParameters');
$mock->shouldReceive('get')->with(SubmitTimeHandler::REQUEST_PARAM_NAME, true)->andReturn(0);

Mockery::mock('alias:apbct_cookies_test')->shouldReceive('__invoke')->andReturn(1);

$result = SubmitTimeHandler::getFromRequest();

$this->assertNull($result);
}

public function testGetFromRequestReturnsTimeDifferenceWhenValid()
public function testSetToRequestDoesNotModifyWhenCalculationDisabled()
{
global $apbct;
$apbct = (object) [
'settings' => ['data__bot_detector_enabled' => false],
'settings' => ['data__bot_detector_enabled' => true],
'data' => ['cookies_type' => 'alternative']
];

$timestamp = time() - 100;

$mock = \Mockery::mock('alias:Cleantalk\ApbctWP\RequestParameters\RequestParameters');
$mock->shouldReceive('get')->with(SubmitTimeHandler::REQUEST_PARAM_NAME, true)->andReturn($timestamp);

Mockery::mock('alias:apbct_cookies_test')->shouldReceive('__invoke')->andReturn(1);

$result = SubmitTimeHandler::getFromRequest();

$this->assertEquals(100, $result);
}

public function testSetToRequestDoesNotModifyWhenCalculationDisabled()
{
global $apbct;
$apbct = (object) ['settings' => ['data__bot_detector_enabled' => true]];

$cookie_test_value = [];
SubmitTimeHandler::setToRequest(time(), $cookie_test_value);

$this->assertEmpty($cookie_test_value);
}

public function testSetToRequestAddsTimestampToRequestAndCookieTestValue()
{
global $apbct;
$apbct = (object) ['settings' => ['data__bot_detector_enabled' => false]];

$current_timestamp = time();
$cookie_test_value = ['cookies_names' => [], 'check_value' => ''];

$mock = \Mockery::mock('alias:Cleantalk\ApbctWP\RequestParameters\RequestParameters');
$mock->shouldReceive('set')->with(SubmitTimeHandler::REQUEST_PARAM_NAME, (string)$current_timestamp, true)->andReturnNull();

SubmitTimeHandler::setToRequest($current_timestamp, $cookie_test_value);

$this->assertContains(SubmitTimeHandler::REQUEST_PARAM_NAME, $cookie_test_value['cookies_names']);
$this->assertStringContainsString((string)$current_timestamp, $cookie_test_value['check_value']);
}

public function testIsCalculationDisabledReturnsTrueWhenBotDetectorEnabled()
{
global $apbct;
$apbct = (object) ['settings' => ['data__bot_detector_enabled' => true]];
$apbct = (object) [
'settings' => ['data__bot_detector_enabled' => true],
'data' => ['cookies_type' => 'alternative']
];

$result = SubmitTimeHandler::isCalculationDisabled();

Expand Down