woocommerce/tests/unit-tests/api/payment-gateways.php

310 lines
10 KiB
PHP
Raw Normal View History

2016-08-31 21:16:52 +00:00
<?php
/**
* Tests for the Payment Gateways REST API.
*
* @package WooCommerce\Tests\API
2017-03-15 16:36:53 +00:00
* @since 3.0.0
2016-08-31 21:16:52 +00:00
*/
class Payment_Gateways extends WC_REST_Unit_Test_Case {
/**
* Setup our test server, endpoints, and user info.
*/
public function setUp() {
parent::setUp();
$this->endpoint = new WC_REST_Payment_Gateways_Controller();
$this->user = $this->factory->user->create( array(
'role' => 'administrator',
) );
}
/**
* Test route registration.
*
2017-03-15 16:36:53 +00:00
* @since 3.0.0
2016-08-31 21:16:52 +00:00
*/
public function test_register_routes() {
$routes = $this->server->get_routes();
2017-02-09 20:21:52 +00:00
$this->assertArrayHasKey( '/wc/v2/payment_gateways', $routes );
$this->assertArrayHasKey( '/wc/v2/payment_gateways/(?P<id>[\w-]+)', $routes );
2016-08-31 21:16:52 +00:00
}
/**
* Test getting all payment gateways.
*
2017-03-15 16:36:53 +00:00
* @since 3.0.0
2016-08-31 21:16:52 +00:00
*/
public function test_get_payment_gateways() {
wp_set_current_user( $this->user );
2017-02-09 20:21:52 +00:00
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/payment_gateways' ) );
2016-08-31 21:16:52 +00:00
$gateways = $response->get_data();
$this->assertEquals( 200, $response->get_status() );
$this->assertContains( array(
'id' => 'cheque',
'title' => 'Check payments',
2016-08-31 21:16:52 +00:00
'description' => 'Please send a check to Store Name, Store Street, Store Town, Store State / County, Store Postcode.',
'order' => '',
'enabled' => false,
'method_title' => 'Check payments',
2018-05-04 17:48:41 +00:00
'method_description' => 'Take payments in person via checks. This offline gateway can also be useful to test purchases.',
'settings' => array_diff_key( $this->get_settings( 'WC_Gateway_Cheque' ), array(
'enabled' => false,
'description' => false,
) ),
2016-08-31 21:16:52 +00:00
'_links' => array(
'self' => array(
array(
2017-02-09 20:21:52 +00:00
'href' => rest_url( '/wc/v2/payment_gateways/cheque' ),
2016-08-31 21:16:52 +00:00
),
),
'collection' => array(
array(
2017-02-09 20:21:52 +00:00
'href' => rest_url( '/wc/v2/payment_gateways' ),
2016-08-31 21:16:52 +00:00
),
),
),
), $gateways );
}
/**
* Tests to make sure payment gateways cannot viewed without valid permissions.
*
2017-03-15 16:36:53 +00:00
* @since 3.0.0
2016-08-31 21:16:52 +00:00
*/
public function test_get_payment_gateways_without_permission() {
wp_set_current_user( 0 );
2017-02-09 20:21:52 +00:00
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/payment_gateways' ) );
2016-08-31 21:16:52 +00:00
$this->assertEquals( 401, $response->get_status() );
}
/**
* Test getting a single payment gateway.
*
2017-03-15 16:36:53 +00:00
* @since 3.0.0
2016-08-31 21:16:52 +00:00
*/
public function test_get_payment_gateway() {
wp_set_current_user( $this->user );
2017-02-09 20:21:52 +00:00
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/payment_gateways/paypal' ) );
2016-08-31 21:16:52 +00:00
$paypal = $response->get_data();
$this->assertEquals( 200, $response->get_status() );
$this->assertEquals( array(
2017-03-24 15:11:21 +00:00
'id' => 'paypal',
'title' => 'PayPal',
'description' => "Pay via PayPal; you can pay with your credit card if you don't have a PayPal account.",
'order' => '',
'enabled' => false,
2017-03-24 15:11:21 +00:00
'method_title' => 'PayPal',
'method_description' => 'PayPal Standard redirects customers to PayPal to enter their payment information.',
2018-05-04 17:48:41 +00:00
'settings' => array_diff_key( $this->get_settings( 'WC_Gateway_Paypal' ), array(
'enabled' => false,
'description' => false,
) ),
2016-08-31 21:16:52 +00:00
), $paypal );
}
/**
* Test getting a payment gateway without valid permissions.
*
2017-03-15 16:36:53 +00:00
* @since 3.0.0
2016-08-31 21:16:52 +00:00
*/
public function test_get_payment_gateway_without_permission() {
wp_set_current_user( 0 );
2017-02-09 20:21:52 +00:00
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/payment_gateways/paypal' ) );
2016-08-31 21:16:52 +00:00
$this->assertEquals( 401, $response->get_status() );
}
/**
* Test getting a payment gateway with an invalid id.
*
2017-03-15 16:36:53 +00:00
* @since 3.0.0
2016-08-31 21:16:52 +00:00
*/
public function test_get_payment_gateway_invalid_id() {
wp_set_current_user( $this->user );
2017-02-09 20:21:52 +00:00
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/payment_gateways/totally_fake_method' ) );
2016-08-31 21:16:52 +00:00
$this->assertEquals( 404, $response->get_status() );
}
/**
* Test updating a single payment gateway.
*
2017-03-15 16:36:53 +00:00
* @since 3.0.0
2016-08-31 21:16:52 +00:00
*/
public function test_update_payment_gateway() {
wp_set_current_user( $this->user );
// Test defaults
2017-02-09 20:21:52 +00:00
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/payment_gateways/paypal' ) );
2016-08-31 21:16:52 +00:00
$paypal = $response->get_data();
$this->assertEquals( 'PayPal', $paypal['settings']['title']['value'] );
$this->assertEquals( 'admin@example.org', $paypal['settings']['email']['value'] );
$this->assertEquals( 'no', $paypal['settings']['testmode']['value'] );
// test updating single setting
2017-02-09 20:21:52 +00:00
$request = new WP_REST_Request( 'POST', '/wc/v2/payment_gateways/paypal' );
2016-08-31 21:16:52 +00:00
$request->set_body_params( array(
'settings' => array(
'email' => 'woo@woo.local',
),
) );
$response = $this->server->dispatch( $request );
$paypal = $response->get_data();
$this->assertEquals( 200, $response->get_status() );
$this->assertEquals( 'PayPal', $paypal['settings']['title']['value'] );
$this->assertEquals( 'woo@woo.local', $paypal['settings']['email']['value'] );
$this->assertEquals( 'no', $paypal['settings']['testmode']['value'] );
// test updating multiple settings
2017-02-09 20:21:52 +00:00
$request = new WP_REST_Request( 'POST', '/wc/v2/payment_gateways/paypal' );
2016-08-31 21:16:52 +00:00
$request->set_body_params( array(
'settings' => array(
'testmode' => 'yes',
'title' => 'PayPal - New Title',
),
) );
$response = $this->server->dispatch( $request );
$paypal = $response->get_data();
$this->assertEquals( 200, $response->get_status() );
$this->assertEquals( 'PayPal - New Title', $paypal['settings']['title']['value'] );
$this->assertEquals( 'woo@woo.local', $paypal['settings']['email']['value'] );
$this->assertEquals( 'yes', $paypal['settings']['testmode']['value'] );
// Test other parameters, and recheck settings
2017-02-09 20:21:52 +00:00
$request = new WP_REST_Request( 'POST', '/wc/v2/payment_gateways/paypal' );
2016-08-31 21:16:52 +00:00
$request->set_body_params( array(
'enabled' => false,
'order' => 2,
) );
$response = $this->server->dispatch( $request );
$paypal = $response->get_data();
$this->assertFalse( $paypal['enabled'] );
$this->assertEquals( 2, $paypal['order'] );
$this->assertEquals( 'PayPal - New Title', $paypal['settings']['title']['value'] );
$this->assertEquals( 'woo@woo.local', $paypal['settings']['email']['value'] );
$this->assertEquals( 'yes', $paypal['settings']['testmode']['value'] );
2016-09-08 22:14:40 +00:00
// test bogus
2017-02-09 20:21:52 +00:00
$request = new WP_REST_Request( 'POST', '/wc/v2/payment_gateways/paypal' );
2016-09-08 22:14:40 +00:00
$request->set_body_params( array(
'settings' => array(
'paymentaction' => 'afasfasf',
2016-09-09 16:39:09 +00:00
),
2016-09-08 22:14:40 +00:00
) );
$response = $this->server->dispatch( $request );
$this->assertEquals( 400, $response->get_status() );
2017-02-09 20:21:52 +00:00
$request = new WP_REST_Request( 'POST', '/wc/v2/payment_gateways/paypal' );
2016-09-08 22:14:40 +00:00
$request->set_body_params( array(
'settings' => array(
'paymentaction' => 'authorization',
2016-09-09 16:39:09 +00:00
),
2016-09-08 22:14:40 +00:00
) );
$response = $this->server->dispatch( $request );
$paypal = $response->get_data();
$this->assertEquals( 'authorization', $paypal['settings']['paymentaction']['value'] );
2016-08-31 21:16:52 +00:00
}
/**
* Test updating a payment gateway without valid permissions.
*
2017-03-15 16:36:53 +00:00
* @since 3.0.0
2016-08-31 21:16:52 +00:00
*/
public function test_update_payment_gateway_without_permission() {
wp_set_current_user( 0 );
2017-02-09 20:21:52 +00:00
$request = new WP_REST_Request( 'POST', '/wc/v2/payment_gateways/paypal' );
2016-08-31 21:16:52 +00:00
$request->set_body_params( array(
'settings' => array(
'testmode' => 'yes',
'title' => 'PayPal - New Title',
),
) );
$response = $this->server->dispatch( $request );
$this->assertEquals( 401, $response->get_status() );
}
/**
* Test updating a payment gateway with an invalid id.
*
2017-03-15 16:36:53 +00:00
* @since 3.0.0
2016-08-31 21:16:52 +00:00
*/
public function test_update_payment_gateway_invalid_id() {
wp_set_current_user( $this->user );
2017-02-09 20:21:52 +00:00
$request = new WP_REST_Request( 'POST', '/wc/v2/payment_gateways/totally_fake_method' );
2016-08-31 21:16:52 +00:00
$request->set_body_params( array(
'enabled' => true,
) );
$response = $this->server->dispatch( $request );
$this->assertEquals( 404, $response->get_status() );
}
/**
* Test the payment gateway schema.
*
2017-03-15 16:36:53 +00:00
* @since 3.0.0
2016-08-31 21:16:52 +00:00
*/
public function test_payment_gateway_schema() {
wp_set_current_user( $this->user );
2017-02-09 20:21:52 +00:00
$request = new WP_REST_Request( 'OPTIONS', '/wc/v2/payment_gateways' );
2016-08-31 21:16:52 +00:00
$response = $this->server->dispatch( $request );
$data = $response->get_data();
$properties = $data['schema']['properties'];
$this->assertEquals( 8, count( $properties ) );
$this->assertArrayHasKey( 'id', $properties );
$this->assertArrayHasKey( 'title', $properties );
$this->assertArrayHasKey( 'description', $properties );
$this->assertArrayHasKey( 'order', $properties );
$this->assertArrayHasKey( 'enabled', $properties );
$this->assertArrayHasKey( 'method_title', $properties );
$this->assertArrayHasKey( 'method_description', $properties );
$this->assertArrayHasKey( 'settings', $properties );
}
/**
2017-03-28 18:02:31 +00:00
* Loads a particular gateway's settings so we can correctly test API output.
2016-08-31 21:16:52 +00:00
*
2017-03-15 16:36:53 +00:00
* @since 3.0.0
2016-08-31 21:16:52 +00:00
* @param string $gateway_class Name of WC_Payment_Gateway class.
*/
private function get_settings( $gateway_class ) {
2018-05-04 17:48:41 +00:00
$gateway = new $gateway_class();
2016-08-31 21:16:52 +00:00
$settings = array();
$gateway->init_form_fields();
foreach ( $gateway->form_fields as $id => $field ) {
// Make sure we at least have a title and type
if ( empty( $field['title'] ) || empty( $field['type'] ) ) {
continue;
}
// Ignore 'title' settings/fields -- they are UI only
if ( 'title' === $field['type'] ) {
continue;
}
$data = array(
'id' => $id,
'label' => empty( $field['label'] ) ? $field['title'] : $field['label'],
'description' => empty( $field['description'] ) ? '' : $field['description'],
'type' => $field['type'],
'value' => $gateway->settings[ $id ],
'default' => empty( $field['default'] ) ? '' : $field['default'],
'tip' => empty( $field['description'] ) ? '' : $field['description'],
'placeholder' => empty( $field['placeholder'] ) ? '' : $field['placeholder'],
);
if ( ! empty( $field['options'] ) ) {
$data['options'] = $field['options'];
}
$settings[ $id ] = $data;
}
return $settings;
}
}