2017-08-11 04:35:18 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class Customer_Download_Log.
|
|
|
|
* @since 3.3.0
|
|
|
|
* @package WooCommerce\Tests\Customer
|
|
|
|
*/
|
|
|
|
class WC_Tests_Customer_Download_Log extends WC_Unit_Test_Case {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test: get_id
|
|
|
|
*/
|
2018-05-09 10:45:17 +00:00
|
|
|
public function test_get_id() {
|
2018-04-18 07:43:24 +00:00
|
|
|
$customer_id = wc_create_new_customer( 'test@example.com', 'testuser', 'testpassword' );
|
|
|
|
$download = new WC_Customer_Download();
|
|
|
|
$download->set_user_id( $customer_id );
|
|
|
|
$download->set_order_id( 1 );
|
|
|
|
$download->save();
|
|
|
|
|
2017-08-11 04:35:18 +00:00
|
|
|
$object = new WC_Customer_Download_Log();
|
2018-04-18 07:43:24 +00:00
|
|
|
$object->set_permission_id( $download->get_id() );
|
|
|
|
$object->set_user_id( $customer_id );
|
|
|
|
$object->set_user_ip_address( '1.2.3.4' );
|
2017-08-11 04:35:18 +00:00
|
|
|
$id = $object->save();
|
|
|
|
$this->assertEquals( $id, $object->get_id() );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test: get_timestamp
|
|
|
|
*/
|
2018-05-09 10:45:17 +00:00
|
|
|
public function test_get_timestamp() {
|
2017-08-11 04:35:18 +00:00
|
|
|
$object = new WC_Customer_Download_Log();
|
2019-12-20 17:23:05 +00:00
|
|
|
$set_to = time();
|
2017-08-11 04:50:44 +00:00
|
|
|
|
2017-08-23 03:23:25 +00:00
|
|
|
// Convert timestamp to WC_DateTime using ISO 8601 for PHP 5.2 compat.
|
2018-05-09 10:45:17 +00:00
|
|
|
$dt_str = date( 'c', $set_to );
|
2017-11-13 15:58:22 +00:00
|
|
|
$wc_timestamp = new WC_DateTime( $dt_str );
|
2017-08-11 04:50:44 +00:00
|
|
|
|
2017-08-11 04:35:18 +00:00
|
|
|
$object->set_timestamp( $set_to );
|
|
|
|
$this->assertEquals( $wc_timestamp, $object->get_timestamp() );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test: get_permission_id
|
|
|
|
*/
|
2018-05-09 10:45:17 +00:00
|
|
|
public function test_get_permission_id() {
|
2017-08-11 04:35:18 +00:00
|
|
|
$object = new WC_Customer_Download_Log();
|
|
|
|
$set_to = 10;
|
|
|
|
$object->set_permission_id( $set_to );
|
|
|
|
$this->assertEquals( $set_to, $object->get_permission_id() );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test: get_user_id
|
|
|
|
*/
|
2018-05-09 10:45:17 +00:00
|
|
|
public function test_get_user_id() {
|
2017-08-11 04:35:18 +00:00
|
|
|
$object = new WC_Customer_Download_Log();
|
|
|
|
$set_to = 10;
|
|
|
|
$object->set_user_id( $set_to );
|
|
|
|
$this->assertEquals( $set_to, $object->get_user_id() );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test: get_user_ip_address
|
|
|
|
*/
|
2018-05-09 10:45:17 +00:00
|
|
|
public function test_get_user_ip_address() {
|
2017-08-11 04:35:18 +00:00
|
|
|
$object = new WC_Customer_Download_Log();
|
|
|
|
$set_to = '1.2.3.4';
|
|
|
|
$object->set_user_ip_address( $set_to );
|
|
|
|
$this->assertEquals( $set_to, $object->get_user_ip_address() );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test creating a new download log manually.
|
|
|
|
*/
|
|
|
|
public function test_create_download_log() {
|
2017-08-23 03:23:25 +00:00
|
|
|
// First create a download permission to test against.
|
2017-08-11 04:35:18 +00:00
|
|
|
$customer_id_1 = wc_create_new_customer( 'test@example.com', 'testuser', 'testpassword' );
|
|
|
|
$customer_id_2 = wc_create_new_customer( 'test2@example.com', 'testuser2', 'testpassword2' );
|
|
|
|
|
2018-05-09 10:45:17 +00:00
|
|
|
$download_1 = new WC_Customer_Download();
|
2017-08-11 04:35:18 +00:00
|
|
|
$download_1->set_user_id( $customer_id_1 );
|
|
|
|
$download_1->set_order_id( 1 );
|
|
|
|
$download_1->save();
|
|
|
|
|
2017-08-23 03:23:25 +00:00
|
|
|
// Create download log.
|
2019-12-20 17:23:05 +00:00
|
|
|
$timestamp = time();
|
2017-08-11 04:50:44 +00:00
|
|
|
|
2017-08-23 03:23:25 +00:00
|
|
|
// Convert timestamp to WC_DateTime using ISO 8601 for PHP 5.2 compat.
|
2018-05-09 10:45:17 +00:00
|
|
|
$dt_str = date( 'c', $timestamp );
|
2017-11-13 15:58:22 +00:00
|
|
|
$wc_timestamp = new WC_DateTime( $dt_str );
|
2017-08-11 04:35:18 +00:00
|
|
|
|
2018-05-09 10:45:17 +00:00
|
|
|
$download_log = new WC_Customer_Download_Log();
|
2017-08-11 04:35:18 +00:00
|
|
|
$download_log->set_timestamp( $timestamp );
|
|
|
|
$download_log->set_permission_id( $download_1->get_id() );
|
|
|
|
$download_log->set_user_id( $customer_id_2 );
|
|
|
|
$download_log->set_user_ip_address( '1.2.3.4' );
|
|
|
|
$download_log->save();
|
|
|
|
|
2017-08-23 03:23:25 +00:00
|
|
|
// Pull the download log back from data store.
|
2017-08-11 04:35:18 +00:00
|
|
|
$db_download_log = new WC_Customer_Download_Log( $download_log->get_id() );
|
|
|
|
|
2017-08-23 03:23:25 +00:00
|
|
|
// Check that created log matches data from data store.
|
|
|
|
$this->assertNotEquals( 0, $db_download_log->get_id(), 'New download log ID not set to non-zero value.' );
|
|
|
|
$this->assertEquals( $wc_timestamp, $db_download_log->get_timestamp(), 'New download log timestamp set incorrectly.' );
|
|
|
|
$this->assertEquals( $download_1->get_id(), $db_download_log->get_permission_id(), 'New download log permission ID set incorrectly.' );
|
|
|
|
$this->assertEquals( $customer_id_2, $db_download_log->get_user_id(), 'New download log user ID set incorrectly.' );
|
|
|
|
$this->assertEquals( '1.2.3.4', $db_download_log->get_user_ip_address(), 'New download log IP address set incorrectly.' );
|
2017-08-11 04:35:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test creating a new download log automatically from customer download.
|
|
|
|
*/
|
|
|
|
public function test_track_download() {
|
2017-08-23 03:23:25 +00:00
|
|
|
// First create a download permission to test against.
|
2017-08-11 04:35:18 +00:00
|
|
|
$customer_id_1 = wc_create_new_customer( 'test@example.com', 'testuser', 'testpassword' );
|
|
|
|
$customer_id_2 = wc_create_new_customer( 'test2@example.com', 'testuser2', 'testpassword2' );
|
|
|
|
|
2018-05-09 10:45:17 +00:00
|
|
|
$download_1 = new WC_Customer_Download();
|
2017-08-11 04:35:18 +00:00
|
|
|
$download_1->set_user_id( $customer_id_1 );
|
|
|
|
$download_1->set_order_id( 1 );
|
|
|
|
$download_1->set_downloads_remaining( 10 );
|
|
|
|
$download_1->save();
|
|
|
|
|
|
|
|
$ip_address = '1.2.3.4';
|
|
|
|
|
2017-08-23 03:23:25 +00:00
|
|
|
// Initially download count should be zero, and remaining should be 10.
|
|
|
|
$this->assertEquals( 0, $download_1->get_download_count(), 'New permission download count should be zero.' );
|
|
|
|
$this->assertEquals( 10, $download_1->get_downloads_remaining(), 'New permission downloads remaining should be 10.' );
|
2017-08-11 04:35:18 +00:00
|
|
|
|
2017-08-23 03:23:25 +00:00
|
|
|
// Track the download in logs and change remaining/counts.
|
2017-08-11 04:35:18 +00:00
|
|
|
$download_1->track_download( $customer_id_2, $ip_address );
|
|
|
|
|
2017-08-23 03:23:25 +00:00
|
|
|
// Ensure download count iterates properly.
|
|
|
|
$this->assertEquals( 1, $download_1->get_download_count(), 'After download, permission download count should be 1.' );
|
|
|
|
$this->assertEquals( 9, $download_1->get_downloads_remaining(), 'After download, permission downloads remaining should be 9.' );
|
2017-08-11 04:35:18 +00:00
|
|
|
|
2017-08-23 03:23:25 +00:00
|
|
|
// Make sure download log was recorded properly.
|
2018-05-09 10:45:17 +00:00
|
|
|
$data_store = WC_Data_Store::load( 'customer-download-log' );
|
2019-05-01 22:05:00 +00:00
|
|
|
$download_logs = $data_store->get_download_logs(
|
|
|
|
array(
|
|
|
|
'permission_id' => $download_1->get_id(),
|
|
|
|
)
|
|
|
|
);
|
2017-08-11 04:35:18 +00:00
|
|
|
|
2017-08-23 03:23:25 +00:00
|
|
|
$this->assertEquals( 1, count( $download_logs ), 'After single download, permission should have one download log in database.' );
|
2017-08-11 04:35:18 +00:00
|
|
|
|
|
|
|
$download_log = current( $download_logs );
|
|
|
|
|
|
|
|
// Ensure log contains appropriate data for the user, etc.
|
2017-08-23 03:23:25 +00:00
|
|
|
$this->assertNotEquals( 0, $download_log->get_id(), 'Tracked download log ID should not be zero.' );
|
|
|
|
$this->assertEquals( $download_1->get_id(), $download_log->get_permission_id(), 'Tracked download log permission ID did not match.' );
|
|
|
|
$this->assertEquals( $customer_id_2, $download_log->get_user_id(), 'Tracked download log user ID did not match.' );
|
|
|
|
$this->assertEquals( $ip_address, $download_log->get_user_ip_address(), 'Tracked download log IP address did not match.' );
|
2017-08-11 04:35:18 +00:00
|
|
|
}
|
|
|
|
}
|