2019-06-07 15:32:42 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Data REST API Test
|
|
|
|
*
|
|
|
|
* @package WooCommerce Admin\Tests\API
|
|
|
|
*/
|
|
|
|
|
2019-06-10 11:36:11 +00:00
|
|
|
namespace WooCommerce\RestApi\UnitTests\Tests\Version4;
|
|
|
|
|
|
|
|
defined( 'ABSPATH' ) || exit;
|
|
|
|
|
2019-06-10 14:08:29 +00:00
|
|
|
use \WooCommerce\RestApi\UnitTests\AbstractReportsTest;
|
2019-06-10 11:36:11 +00:00
|
|
|
|
2019-06-07 15:32:42 +00:00
|
|
|
/**
|
|
|
|
* WC Tests API Data
|
|
|
|
*/
|
2019-06-10 14:08:29 +00:00
|
|
|
class Data extends AbstractReportsTest {
|
2019-06-07 15:32:42 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Endpoints.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $endpoint = '/wc/v4/data';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that the list of data endpoints includes download-ips.
|
|
|
|
*/
|
|
|
|
public function test_get_items_contains_download_ips() {
|
2019-06-10 14:08:29 +00:00
|
|
|
$request = new \WP_REST_Request( 'GET', $this->endpoint );
|
2019-06-07 15:32:42 +00:00
|
|
|
$response = $this->server->dispatch( $request );
|
|
|
|
$data = $response->get_data();
|
|
|
|
|
|
|
|
$this->assertEquals( 200, $response->get_status() );
|
|
|
|
$this->assertEquals( 4, count( $data ) );
|
|
|
|
$this->assertEquals( 'download-ips', $data[3]['slug'] );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test download-ips match searching.
|
|
|
|
*/
|
|
|
|
public function test_download_ips() {
|
2019-06-10 14:08:29 +00:00
|
|
|
$prod_download = new \WC_Product_Download();
|
2019-06-07 15:32:42 +00:00
|
|
|
$prod_download->set_file( plugin_dir_url( __FILE__ ) . '/assets/images/help.png' );
|
|
|
|
$prod_download->set_id( 1 );
|
|
|
|
|
2019-06-10 14:08:29 +00:00
|
|
|
$product = new \WC_Product_Simple();
|
2019-06-07 15:32:42 +00:00
|
|
|
$product->set_name( 'Test Product' );
|
|
|
|
$product->set_downloadable( 'yes' );
|
|
|
|
$product->set_downloads( array( $prod_download ) );
|
|
|
|
$product->set_regular_price( 25 );
|
|
|
|
$product->save();
|
|
|
|
|
2019-06-10 11:36:11 +00:00
|
|
|
$order = \WooCommerce\RestApi\UnitTests\Helpers\OrderHelper::create_order( 1, $product );
|
2019-06-07 15:32:42 +00:00
|
|
|
$order->set_status( 'completed' );
|
|
|
|
$order->set_total( 100 );
|
|
|
|
$order->save();
|
|
|
|
|
2019-06-10 14:08:29 +00:00
|
|
|
$download = new \WC_Customer_Download();
|
2019-06-07 15:32:42 +00:00
|
|
|
$download->set_user_id( $this->user );
|
|
|
|
$download->set_order_id( $order->get_id() );
|
|
|
|
$download->set_product_id( $product->get_id() );
|
|
|
|
$download->set_download_id( $prod_download->get_id() );
|
|
|
|
$download->save();
|
|
|
|
|
2019-06-10 14:08:29 +00:00
|
|
|
$object = new \WC_Customer_Download_Log();
|
2019-06-07 15:32:42 +00:00
|
|
|
$object->set_permission_id( $download->get_id() );
|
|
|
|
$object->set_user_id( $this->user );
|
|
|
|
$object->set_user_ip_address( '1.2.3.4' );
|
|
|
|
$id = $object->save();
|
|
|
|
|
2019-06-10 14:08:29 +00:00
|
|
|
$object = new \WC_Customer_Download_Log();
|
2019-06-07 15:32:42 +00:00
|
|
|
$object->set_permission_id( $download->get_id() );
|
|
|
|
$object->set_user_id( $this->user );
|
|
|
|
$object->set_user_ip_address( '54.2.1.3' );
|
|
|
|
$id = $object->save();
|
|
|
|
|
|
|
|
// Save a second log for the same IP -- only one result for this IP should be returned.
|
2019-06-10 14:08:29 +00:00
|
|
|
$object = new \WC_Customer_Download_Log();
|
2019-06-07 15:32:42 +00:00
|
|
|
$object->set_permission_id( $download->get_id() );
|
|
|
|
$object->set_user_id( $this->user );
|
|
|
|
$object->set_user_ip_address( '54.2.1.3' );
|
|
|
|
$id = $object->save();
|
|
|
|
|
2019-06-10 14:08:29 +00:00
|
|
|
$object = new \WC_Customer_Download_Log();
|
2019-06-07 15:32:42 +00:00
|
|
|
$object->set_permission_id( $download->get_id() );
|
|
|
|
$object->set_user_id( $this->user );
|
|
|
|
$object->set_user_ip_address( '54.5.1.7' );
|
|
|
|
$id = $object->save();
|
|
|
|
|
2019-06-10 14:08:29 +00:00
|
|
|
$request = new \WP_REST_Request( 'GET', $this->endpoint . '/download-ips' );
|
2019-06-07 15:32:42 +00:00
|
|
|
$request->set_query_params(
|
|
|
|
array(
|
|
|
|
'match' => '54',
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$response = $this->server->dispatch( $request );
|
|
|
|
$addresses = $response->get_data();
|
|
|
|
|
|
|
|
$this->assertEquals( 200, $response->get_status() );
|
|
|
|
$this->assertEquals( 2, count( $addresses ) );
|
|
|
|
|
|
|
|
$this->assertEquals( '54.2.1.3', $addresses[0]['user_ip_address'] );
|
|
|
|
$this->assertEquals( '54.5.1.7', $addresses[1]['user_ip_address'] );
|
|
|
|
}
|
|
|
|
}
|