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
5 changes: 5 additions & 0 deletions inc/cleantalk-integrations-by-hook.php
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,11 @@
'setting' => 'forms__contact_forms_test',
'ajax' => true
),
'Amelia' => array(
'hook' => 'wpamelia_api',
'setting' => 'forms__contact_forms_test',
'ajax' => true
),
);

add_action('plugins_loaded', function () use ($apbct_active_integrations) {
Expand Down
65 changes: 65 additions & 0 deletions lib/Cleantalk/Antispam/Integrations/Amelia.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

namespace Cleantalk\Antispam\Integrations;

use Cleantalk\ApbctWP\Variables\Get;

class Amelia extends IntegrationBase
{
public function doPrepareActions($argument)
{
$call = Get::getString('call');

if ( $call !== '/bookings' ) {
return false;
}

return true;
}

public function getDataForChecking($argument)
{
$raw = file_get_contents('php://input');
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why used the stream instead of $_POST? Explain, please.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Amelia sends data in the "application/json" format. $_POST does not intercept this.

if ( ! $raw ) {
return null;
}

$payload = json_decode($raw, true);
if ( ! is_array($payload) ) {
return null;
}

$email = '';
if ( ! empty($payload['customer']['email']) ) {
$email = (string) $payload['customer']['email'];
} elseif ( ! empty($payload['bookings'][0]['customer']['email']) ) {
$email = (string) $payload['bookings'][0]['customer']['email'];
} elseif ( ! empty($payload['email']) ) {
$email = (string) $payload['email'];
}

if ( ! $email ) {
return null;
}

return array('email' => $email);
}

public function doBlock($message)
{
remove_action('wp_ajax_wpamelia_api', array('AmeliaBooking\\Plugin', 'wpAmeliaApiCall'));
remove_action('wp_ajax_nopriv_wpamelia_api', array('AmeliaBooking\\Plugin', 'wpAmeliaApiCall'));
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this logic of removing hooks?


status_header(403);
nocache_headers();
header('Content-Type: application/json; charset=utf-8');
echo wp_json_encode(array(
'message' => $message,
'data' => array(
'message' => $message,
'reason' => $message,
),
));
exit;
}
}
142 changes: 142 additions & 0 deletions tests/Antispam/IntegrationsByHook/TestAmelia.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
<?php

namespace Cleantalk\Antispam\Integrations;

use Cleantalk\ApbctWP\Variables\Get;
use PHPUnit\Framework\TestCase;

class ApbctAmeliaPhpInputStub
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Every class must be in a separate file.

{
public static $data = '';
public $context;
private $position = 0;

public function stream_open($path, $mode, $options, &$opened_path)
{
$this->position = 0;
return true;
}

public function stream_read($count)
{
$chunk = substr(self::$data, $this->position, $count);
$this->position += strlen($chunk);
return $chunk;
}

public function stream_eof()
{
return $this->position >= strlen(self::$data);
}

public function stream_stat()
{
return array();
}

public function url_stat($path, $flags)
{
return array();
}

public function stream_set_option($option, $arg1, $arg2)
{
return true;
}
}

class TestAmelia extends TestCase
{
/** @var Amelia */
private $integration;

protected function setUp(): void
{
parent::setUp();
$this->integration = new Amelia();
}

protected function tearDown(): void
{
$_GET = array();
Get::getInstance()->variables = array();
ApbctAmeliaPhpInputStub::$data = '';
parent::tearDown();
}

/**
* Runs getDataForChecking() with a mocked php://input body.
*
* @param string $rawBody
* @return mixed
*/
private function getDataWithInput($rawBody)
{
ApbctAmeliaPhpInputStub::$data = $rawBody;
stream_wrapper_unregister('php');
stream_wrapper_register('php', ApbctAmeliaPhpInputStub::class);
try {
return $this->integration->getDataForChecking(null);
} finally {
stream_wrapper_restore('php');
}
}

public function testDoPrepareActionsReturnsTrueForBookingsCall()
{
$_GET['call'] = '/bookings';

$this->assertTrue($this->integration->doPrepareActions(null));
}

public function testDoPrepareActionsReturnsFalseForOtherCall()
{
$_GET['call'] = '/appointments';

$this->assertFalse($this->integration->doPrepareActions(null));
}

public function testDoPrepareActionsReturnsFalseWhenCallMissing()
{
$this->assertFalse($this->integration->doPrepareActions(null));
}

public function testDoPrepareActionsReturnsFalseForArrayCall()
{
$_GET['call'] = array('/bookings');
$this->assertFalse($this->integration->doPrepareActions(null));
}

public function testGetDataForCheckingReturnsCustomerEmail()
{
$result = $this->getDataWithInput('{"customer":{"email":"customer@example.com"}}');

$this->assertSame(array('email' => 'customer@example.com'), $result);
}

public function testGetDataForCheckingReturnsBookingsCustomerEmail()
{
$result = $this->getDataWithInput('{"bookings":[{"customer":{"email":"booking@example.com"}}]}');

$this->assertSame(array('email' => 'booking@example.com'), $result);
}

public function testGetDataForCheckingPrefersCustomerEmail()
{
$payload = '{"customer":{"email":"customer@example.com"},"email":"top@example.com"}';

$result = $this->getDataWithInput($payload);

$this->assertSame('customer@example.com', $result['email']);
}

public function testGetDataForCheckingReturnsNullForEmptyBody()
{
$this->assertNull($this->getDataWithInput(''));
}

public function testGetDataForCheckingReturnsNullForInvalidJson()
{
$this->assertNull($this->getDataWithInput('not a valid json'));
}
}
Loading