summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2019-06-27 22:56:05 +0200
committerChristian Grothoff <christian@grothoff.org>2019-06-27 22:56:05 +0200
commitf4789b97ab54a0887481e4521907ed0842ace062 (patch)
treef57d575b17c4151fd8fa6a93703e340ed6b925e7
parentba8022368a35aed990225ff76f7e00544c038391 (diff)
downloadwoocommerce-taler-f4789b97ab54a0887481e4521907ed0842ace062.tar.gz
woocommerce-taler-f4789b97ab54a0887481e4521907ed0842ace062.tar.bz2
woocommerce-taler-f4789b97ab54a0887481e4521907ed0842ace062.zip
import testcases
-rw-r--r--src/GNU-taler-payment/functions/functions.php224
-rw-r--r--src/GNU-taler-payment/functions/functionsTest.php157
2 files changed, 381 insertions, 0 deletions
diff --git a/src/GNU-taler-payment/functions/functions.php b/src/GNU-taler-payment/functions/functions.php
new file mode 100644
index 0000000..1a8dd6f
--- /dev/null
+++ b/src/GNU-taler-payment/functions/functions.php
@@ -0,0 +1,224 @@
+<?php
+
+/**
+ * Sends a request to a url via HTTP.
+ *
+ * Sends a request to a GNU Taler Backend over HTTP and returns the result.
+ * The request can be sent as POST, GET, PUT or another method.
+ *
+ * @param $method - POST, GET, PUT or another method.
+ * @param $backend_url - URL to the GNU Taler Backend.
+ * @param $body - The content of the request.
+ * @param $purpose - What return value is to be expected.
+ * @param $api_key
+ * @return array The return array will either have the successful return value or a detailed error message.
+ * @since 0.6.0
+ */
+function call_api( $method, $backend_url, $body, $purpose, $api_key ): array
+{
+ //create_url
+ $url = create_api_url( $backend_url, $purpose, $body );
+
+ //Initialize curl request
+ $curl = curl_init_request( $method, $body, $url, $api_key);
+
+ // EXECUTE:
+ $result = curl_exec( $curl );
+
+ //HTTP Status Error handling
+ $message_array = curl_error_handling( $curl, $result );
+ curl_close( $curl );
+ return $message_array;
+}
+
+/**
+ * Checks if the return http code is a success and if not what kind of error status it is.
+ *
+ * If the request was successful an array will be returned with the boolean value true, the http code and the result of the response.
+ * If the request failed an array will be returned with the boolean value false, the http code and a detailed error message.
+ *
+ * @param $curl - Created curl request for error handling.
+ * @param $result - The response from the backend, that will be returned if the request was successful
+ * @return array - Array with a boolean, a http code and a message will be returned
+ * @since 0.6.0
+ *
+ */
+function curl_error_handling( $curl, $result ): array
+{
+ $http_code = curl_getinfo( $curl, CURLINFO_HTTP_CODE );
+ if ( curl_error( $curl ) ) {
+ $error_msg = curl_error( $curl );
+ }
+ if ( isset( $error_msg ) ) {
+ return array(
+ 'result' => false,
+ 'http_code' => $http_code,
+ 'message' => $error_msg,
+ );
+ }
+ if ( $http_code === 200 ) {
+ return array(
+ 'result' => true,
+ 'http_code' => $http_code,
+ 'message' => $result,
+ );
+ }
+ if ( preg_match( '(4[0-9]{2})', $http_code ) ) {
+ switch ($http_code) {
+ case 400:
+ return array(
+ 'result' => false,
+ 'http_code' => $http_code,
+ 'message' => 'Bad request',
+ );
+ break;
+ case 401:
+ return array(
+ 'result' => false,
+ 'http_code' => $http_code,
+ 'message' => 'Unauthorized',
+ );
+ break;
+ case 403:
+ return array(
+ 'result' => false,
+ 'http_code' => $http_code,
+ 'message' => 'Forbidden',
+ );
+ break;
+ case 404:
+ return array(
+ 'result' => false,
+ 'http_code' => $http_code,
+ 'message' => 'Page Not Found',
+ );
+ break;
+ default:
+ return array(
+ 'result' => false,
+ 'http_code' => $http_code,
+ 'message' => '4xx Client Error',
+ );
+ break;
+ }
+ } elseif ( preg_match( '(5[0-9]{2})', $http_code ) ) {
+ switch ( $http_code ) {
+ case '500':
+ return array(
+ 'result' => false,
+ 'http_code' => $http_code,
+ 'message' => 'Internal Server Error',
+ );
+ break;
+ case '502':
+ return array(
+ 'result' => false,
+ 'http_code' => $http_code,
+ 'message' => 'Bad Gateway',
+ );
+ break;
+ case '503':
+ return array(
+ 'result' => false,
+ 'http_code' => $http_code,
+ 'message' => 'Service Unavailable',
+ );
+ break;
+ case '504':
+ return array(
+ 'result' => false,
+ 'http_code' => $http_code,
+ 'message' => 'Gateway Timeout',
+ );
+ break;
+ default:
+ return array(
+ 'result' => false,
+ 'http_code' => $http_code,
+ 'message' => '5xx Client Error',
+ );
+ break;
+ }
+ } else {
+ return array(
+ 'result' => false,
+ 'http_code' => $http_code,
+ 'message' => 'http status error',
+ );
+ }
+}
+
+/**
+ * Initialises the curl request and sets some necessary options depending on the method.
+ *
+ * Depending of the method chosen different options for the curl request will be set.
+ * Not depending on the method the settings for a return value, Authorization and Content-Type are being set.
+ *
+ * @param $method - POST, GET, PUT or another method.
+ * @param $body - Content of the request.
+ * @param $url - URL where the request will be send
+ * @param $api_key
+ * @return false|resource - Either the configured curl request will be returned or false if an error appears.
+ * @since 0.6.0
+ */
+function curl_init_request( $method, $body, $url, $api_key )
+{
+ $curl = curl_init();
+
+ switch ( $method ) {
+ case 'POST':
+ curl_setopt( $curl, CURLOPT_POST, 1 );
+ if ( $body ) {
+ curl_setopt( $curl, CURLOPT_POSTFIELDS, $body );
+ }
+ break;
+ case 'PUT':
+ curl_setopt( $curl, CURLOPT_CUSTOMREQUEST, 'PUT' );
+ if ( $body ) {
+ curl_setopt( $curl, CURLOPT_POSTFIELDS, $body );
+ }
+ break;
+ case 'GET':
+ curl_setopt( $curl, CURLOPT_VERBOSE, 1 );
+ break;
+ default:
+ curl_setopt( $curl, CURLOPT_CUSTOMREQUEST, $method );
+ break;
+ }
+
+ // OPTIONS:
+ curl_setopt( $curl, CURLOPT_URL, $url );
+ curl_setopt( $curl, CURLOPT_HTTPHEADER, array(
+ 'Authorization: ' . $api_key,
+ 'Content-Type: application/json',
+ ) );
+ curl_setopt( $curl, CURLOPT_RETURNTRANSFER, 1 );
+ curl_setopt ($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC );
+
+ return $curl;
+}
+
+/**
+ * Creates the final url depending on the purpose.
+ *
+ * @param $url - URL where the request will be send.
+ * @param $purpose - What will be added to the url.
+ * @param $body - Content of the request.
+ * @return string - return the final url.
+ * @since 0.6.0
+ */
+function create_api_url ($url, $purpose, $body ): string
+{
+ if ( $purpose === 'create_order' ) {
+ return $url . '/order';
+ }
+ if ( $purpose === 'confirm_payment' ) {
+ return $url . '/check-payment?order_id=' . $body;
+ }
+ if ( $purpose === 'create_refund' ) {
+ return $url . '/refund';
+ }
+ return $url;
+}
+
+
diff --git a/src/GNU-taler-payment/functions/functionsTest.php b/src/GNU-taler-payment/functions/functionsTest.php
new file mode 100644
index 0000000..a18dab2
--- /dev/null
+++ b/src/GNU-taler-payment/functions/functionsTest.php
@@ -0,0 +1,157 @@
+<?php
+
+
+use PHPUnit\Framework\Assert;
+use PHPUnit\Framework\TestCase;
+require 'functions.php';
+
+
+/**
+ * Class functionsTest
+ */
+class functionsTest extends TestCase
+{
+ /**
+ * Tests the call_api function
+ */
+ public function test_call_api(): void
+ {
+ $method_post = 'POST';
+ $method_get = 'GET';
+ $method_different = '1234';
+ $wc_order_test_request = '';
+ $backend_url = 'https://backend.demo.taler.net';
+ $api_key = 'ApiKey sandbox';
+
+ try {
+ $wc_order_test_request = 'wc_test_' . random_int(0, 1000);
+ } catch (Exception $e) {
+ }
+ $body_request_1 = array(
+ 'order' => array(
+ 'amount' => 'KUDOS:0.1',
+ 'fulfillment_url' => 'http://gnutaler.hofmd.ch',
+ 'summary' => 'Test_order',
+ 'order_id' => $wc_order_test_request,
+ )
+ );
+ $purpose_1 = 'create_order';
+
+ $body_request_2 = $wc_order_test_request;
+ $purpose_2 = 'confirm_payment';
+
+ $result_create_order = call_api( $method_post, $backend_url, json_encode($body_request_1), $purpose_1, $api_key );
+ $result_confirm_payment = call_api( $method_get, $backend_url, $body_request_2, $purpose_2, $api_key );
+ $result_verify_backend_url = call_api( $method_get, $backend_url, '', '', $api_key );
+ $result_method_different = call_api( $method_different, $backend_url, json_encode($body_request_1), $purpose_1, $api_key );
+
+ Assert::assertTrue($result_create_order['result']);
+ Assert::assertEquals($wc_order_test_request, json_decode($result_create_order['message'], true)['order_id']);
+ Assert::assertTrue($result_confirm_payment['result']);
+ Assert::assertEquals(false, json_decode($result_confirm_payment['message'], true)['paid']);
+ Assert::assertTrue($result_verify_backend_url['result']);
+ Assert::assertEquals('Hello, I\'m a merchant\'s Taler backend. This HTTP server is not for humans.', trim($result_verify_backend_url['message']));
+ Assert::assertFalse($result_method_different['result']);
+ Assert::assertEquals('Bad request', $result_method_different['message']);
+ }
+
+
+ /**
+ * Tests the create_api_url function
+ */
+ public function test_create_api_url(): void
+ {
+ $url_test = 'https://backend.demo.taler.net';
+ $wc_test_order = '';
+ try {
+ $wc_test_order = 'wc_test_' . random_int(0, 1000);
+ } catch (Exception $e) {
+ }
+ $purpose_create_order = 'create_order';
+ $purpose_confirm_payment = 'confirm_payment';
+ $purpose_create_refund = 'create_refund';
+
+ $create_order_url = create_api_url($url_test, $purpose_create_order, '');
+ $confirm_payment_url = create_api_url($url_test, $purpose_confirm_payment, $wc_test_order);
+ $create_refund_url = create_api_url($url_test, $purpose_create_refund, '');
+
+ Assert::assertEquals('https://backend.demo.taler.net/order', $create_order_url);
+ Assert::assertEquals('https://backend.demo.taler.net/check-payment?order_id=' . $wc_test_order, $confirm_payment_url);
+ Assert::assertEquals('https://backend.demo.taler.net/refund', $create_refund_url);
+ }
+
+ /**
+ * Tests the curl_error_handling function with the http status code 200
+ */
+ public function test_curl_error_handling_code_200(): void
+ {
+ $api_key = 'ApiKey sandbox';
+ $test_url = 'https://backend.demo.taler.net';
+
+ $curl_error_message_array = call_api('GET', $test_url, '', '', $api_key);
+
+ Assert::assertTrue($curl_error_message_array['result']);
+ Assert::assertEquals(200, $curl_error_message_array['http_code']);
+ }
+
+ /**
+ * Tests the curl_error_handling function with the http status code 400
+ */
+ public function test_curl_error_handling_code_400(): void
+ {
+ $api_key = 'ApiKey sandbox';
+ $api_key_wrong = 'ApiKey ____***££££èèè';
+ $test_url = 'https://backend.demo.taler.net';
+ $test_url_wrong = 'https://backend.demo.taler.net/test_if_this_exits';
+ $body = json_encode(array(
+ 'order' => array(
+ 'wrong_field' => 'Wrong value',
+ )
+ ));
+
+ $curl_error_message_array_400 = call_api('POST', $test_url, $body, 'create_order', $api_key);
+ $curl_error_message_array_401 = call_api('GET', $test_url, '', '', $api_key_wrong);
+ $curl_error_message_array_403 = call_api('GET', 'https://httpstat.us/403', '', '', '');
+ $curl_error_message_array_404 = call_api('GET', $test_url_wrong, '', '', $api_key);
+
+ Assert::assertFalse($curl_error_message_array_400['result']);
+ Assert::assertEquals(400, $curl_error_message_array_400['http_code']);
+ Assert::assertFalse($curl_error_message_array_401['result']);
+ Assert::assertEquals(401, $curl_error_message_array_401['http_code']);
+ Assert::assertFalse($curl_error_message_array_403['result']);
+ Assert::assertEquals(403, $curl_error_message_array_403['http_code']);
+ Assert::assertFalse($curl_error_message_array_404['result']);
+ Assert::assertEquals(404, $curl_error_message_array_404['http_code']);
+
+ }
+
+
+ /**
+ * Tests the curl_error_handling function with the http status code 500
+ */
+ public function test_curl_error_handling_code_500(): void
+ {
+ $api_key = 'ApiKey sandbox';
+ $test_url = 'https://backend.demo.taler.net';
+ $test_url_500 = 'https://httpstat.us/500';
+ $test_url_502 = 'https://httpstat.us/502';
+ $test_url_503 = 'https://httpstat.us/503';
+ $test_url_504 = 'https://httpstat.us/504';
+
+ $curl_error_message_array_500 = call_api('GET', $test_url_500, '', '', $api_key);
+ $curl_error_message_array_502 = call_api('GET', $test_url_502, '', '', $api_key);
+ $curl_error_message_array_503 = call_api('GET', $test_url_503, '', '', $api_key);
+ $curl_error_message_array_504 = call_api('GET', $test_url_504, '', '', $api_key);
+
+ Assert::assertFalse($curl_error_message_array_500['result']);
+ Assert::assertEquals(500, $curl_error_message_array_500['http_code']);
+ Assert::assertFalse($curl_error_message_array_502['result']);
+ Assert::assertEquals(502, $curl_error_message_array_502['http_code']);
+ Assert::assertFalse($curl_error_message_array_503['result']);
+ Assert::assertEquals(503, $curl_error_message_array_503['http_code']);
+ Assert::assertFalse($curl_error_message_array_504['result']);
+ Assert::assertEquals(504, $curl_error_message_array_504['http_code']);
+
+ }
+
+}