-
Notifications
You must be signed in to change notification settings - Fork 8
New. Code. Amelia integration #797
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
alexander-b-clean
wants to merge
6
commits into
dev
Choose a base branch
from
new-code-amelia-integrations.ab
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
1ad16b8
New. Code. Amelia integration
datorik 6087ee7
New. Code. Amelia integration
datorik 2141481
Code. Code review
datorik 007a6e8
Code. Code review
datorik 47c0e92
Code. Unit testing
datorik ea03786
Code. Unit testing
datorik File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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'); | ||
| 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')); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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')); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.There was a problem hiding this comment.
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.