2017-12-14 10:45:00 +00:00
|
|
|
<?php
|
2018-01-22 12:59:09 +00:00
|
|
|
/**
|
|
|
|
* WC_Customer_Download tests file.
|
|
|
|
*
|
|
|
|
* @package WooCommerce\Tests\Customer
|
|
|
|
*/
|
2017-12-14 10:45:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class WC_Customer_Download.
|
2018-01-22 12:59:09 +00:00
|
|
|
*
|
2017-12-14 10:45:00 +00:00
|
|
|
* @since 3.3.0
|
|
|
|
* @package WooCommerce\Tests\Customer
|
|
|
|
*/
|
|
|
|
class WC_Tests_Customer_Download extends WC_Unit_Test_Case {
|
|
|
|
|
2018-01-22 16:43:28 +00:00
|
|
|
/**
|
|
|
|
* Download object used for testing.
|
|
|
|
*
|
|
|
|
* @var WC_Customer_Download
|
|
|
|
*/
|
|
|
|
private $download;
|
|
|
|
|
|
|
|
/**
|
2018-01-26 14:05:42 +00:00
|
|
|
* ID of the fake customer used for the tests.
|
2018-01-22 16:43:28 +00:00
|
|
|
*
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
private $customer_id;
|
|
|
|
|
2018-01-26 14:05:42 +00:00
|
|
|
/**
|
|
|
|
* E-mail of the fake customer used for the tests.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $customer_email;
|
|
|
|
|
2018-01-22 16:43:28 +00:00
|
|
|
/**
|
|
|
|
* Tests set up.
|
|
|
|
*/
|
|
|
|
public function setUp() {
|
2018-01-26 14:05:42 +00:00
|
|
|
$this->customer_id = 1;
|
|
|
|
$this->customer_email = 'test@example.com';
|
2018-01-22 16:43:28 +00:00
|
|
|
|
|
|
|
$this->download = new WC_Customer_Download();
|
|
|
|
$this->download->set_user_id( $this->customer_id );
|
2018-01-26 14:05:42 +00:00
|
|
|
$this->download->set_user_email( $this->customer_email );
|
2018-01-22 16:43:28 +00:00
|
|
|
$this->download->set_order_id( 1 );
|
2018-01-22 19:15:02 +00:00
|
|
|
$this->download->set_access_granted( '2018-01-22 00:00:00' );
|
2018-01-22 16:43:28 +00:00
|
|
|
$this->download->save();
|
|
|
|
}
|
|
|
|
|
2018-01-22 12:59:09 +00:00
|
|
|
/**
|
|
|
|
* Test WC_Customer_Download_Data_Store::delete()
|
|
|
|
*/
|
2017-12-14 10:45:00 +00:00
|
|
|
public function test_delete() {
|
|
|
|
$data_store = WC_Data_Store::load( 'customer-download' );
|
2018-01-22 16:43:28 +00:00
|
|
|
$data_store->delete( $this->download );
|
|
|
|
$this->assertEquals( 0, $this->download->get_id() );
|
2017-12-14 10:45:00 +00:00
|
|
|
}
|
|
|
|
|
2018-01-22 12:59:09 +00:00
|
|
|
/**
|
|
|
|
* Test WC_Customer_Download_Data_Store::delete_by_id()
|
|
|
|
*/
|
2017-12-14 10:45:00 +00:00
|
|
|
public function test_delete_by_id() {
|
|
|
|
$data_store = WC_Data_Store::load( 'customer-download' );
|
2018-01-22 16:43:28 +00:00
|
|
|
$data_store->delete_by_id( $this->download->get_id() );
|
2017-12-14 10:45:00 +00:00
|
|
|
$this->assertEquals( 0, $data_store->get_id() );
|
|
|
|
}
|
|
|
|
|
2018-01-22 12:59:09 +00:00
|
|
|
/**
|
|
|
|
* Test WC_Customer_Download_Data_Store::delete_by_download_id()
|
|
|
|
*/
|
2017-12-14 10:45:00 +00:00
|
|
|
public function test_delete_by_download_id() {
|
2018-01-22 16:43:28 +00:00
|
|
|
$download_id = $this->download->get_download_id();
|
2018-01-22 12:59:09 +00:00
|
|
|
$data_store = WC_Data_Store::load( 'customer-download' );
|
2018-01-22 16:43:28 +00:00
|
|
|
$downloads = $data_store->get_downloads_for_customer( $this->customer_id );
|
2017-12-14 10:45:00 +00:00
|
|
|
$this->assertInstanceOf( 'StdClass', $downloads[0] );
|
|
|
|
$data_store->delete_by_download_id( $download_id );
|
2018-01-22 16:43:28 +00:00
|
|
|
$downloads = $data_store->get_downloads_for_customer( $this->customer_id );
|
2017-12-14 10:45:00 +00:00
|
|
|
$this->assertEquals( array(), $downloads );
|
|
|
|
}
|
|
|
|
|
2018-01-22 12:59:09 +00:00
|
|
|
/**
|
|
|
|
* Test WC_Customer_Download_Data_Store::get_downloads()
|
|
|
|
*/
|
2017-12-14 10:45:00 +00:00
|
|
|
public function test_get_downloads() {
|
2018-01-22 12:59:09 +00:00
|
|
|
$download_2 = new WC_Customer_Download();
|
2018-01-22 16:43:28 +00:00
|
|
|
$download_2->set_user_id( $this->customer_id );
|
2018-01-26 14:05:42 +00:00
|
|
|
$download_2->set_user_email( $this->customer_email );
|
2018-02-28 16:34:11 +00:00
|
|
|
$download_2->set_order_id( 2 );
|
2018-01-22 19:15:02 +00:00
|
|
|
$download_2->set_access_granted( '2018-01-22 00:00:00' );
|
2017-12-14 10:45:00 +00:00
|
|
|
$download_2->save();
|
|
|
|
|
|
|
|
$data_store = WC_Data_Store::load( 'customer-download' );
|
2018-02-28 16:34:11 +00:00
|
|
|
$downloads = $data_store->get_downloads(
|
|
|
|
array(
|
|
|
|
'user_email' => $this->customer_email,
|
|
|
|
'orderby' => 'order_id',
|
|
|
|
'order' => 'DESC',
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$this->assertEquals( array( $download_2, $this->download ), $downloads );
|
2018-01-22 13:01:00 +00:00
|
|
|
|
2017-12-14 10:45:00 +00:00
|
|
|
$downloads = $data_store->get_downloads( array( 'user_email' => 'test2@example.com' ) );
|
|
|
|
$this->assertEquals( array(), $downloads );
|
|
|
|
|
2018-01-22 16:43:28 +00:00
|
|
|
$expected_result = array( $this->download->get_id(), $download_2->get_id() );
|
2018-01-22 13:01:00 +00:00
|
|
|
$downloads = $data_store->get_downloads(
|
|
|
|
array(
|
2018-01-26 14:05:42 +00:00
|
|
|
'user_email' => $this->customer_email,
|
2018-01-22 13:01:00 +00:00
|
|
|
'return' => 'ids',
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$this->assertEquals( $expected_result, $downloads );
|
2018-01-22 19:15:02 +00:00
|
|
|
|
|
|
|
$expected_result = array(
|
|
|
|
array(
|
2018-01-26 14:05:42 +00:00
|
|
|
'user_email' => $this->customer_email,
|
2018-01-22 19:15:02 +00:00
|
|
|
'permission_id' => $this->download->get_id(),
|
|
|
|
),
|
|
|
|
array(
|
2018-01-26 14:05:42 +00:00
|
|
|
'user_email' => $this->customer_email,
|
2018-01-22 19:15:02 +00:00
|
|
|
'permission_id' => $download_2->get_id(),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
$downloads = $data_store->get_downloads(
|
|
|
|
array(
|
2018-01-26 14:05:42 +00:00
|
|
|
'user_email' => $this->customer_email,
|
2018-01-22 19:15:02 +00:00
|
|
|
'return' => 'permission_id,user_email',
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$this->assertEquals( $expected_result, $downloads );
|
2017-12-14 10:45:00 +00:00
|
|
|
}
|
|
|
|
}
|