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
|
|
|
|
*/
|
|
|
|
function test_get_id() {
|
|
|
|
$object = new WC_Customer_Download_Log();
|
|
|
|
$id = $object->save();
|
|
|
|
$this->assertEquals( $id, $object->get_id() );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test: get_timestamp
|
|
|
|
*/
|
|
|
|
function test_get_timestamp() {
|
|
|
|
$object = new WC_Customer_Download_Log();
|
|
|
|
$set_to = current_time( 'timestamp', true );
|
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.
|
2017-08-11 04:50:44 +00:00
|
|
|
$dtStr = date("c", $set_to);
|
|
|
|
$wc_timestamp = new WC_DateTime($dtStr);
|
|
|
|
|
2017-08-11 04:35:18 +00:00
|
|
|
$object->set_timestamp( $set_to );
|
|
|
|
$this->assertEquals( $wc_timestamp, $object->get_timestamp() );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test: get_permission_id
|
|
|
|
*/
|
|
|
|
function test_get_permission_id() {
|
|
|
|
$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
|
|
|
|
*/
|
|
|
|
function test_get_user_id() {
|
|
|
|
$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
|
|
|
|
*/
|
|
|
|
function test_get_user_ip_address() {
|
|
|
|
$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' );
|
|
|
|
|
|
|
|
$download_1 = new WC_Customer_Download;
|
|
|
|
$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.
|
2017-08-11 04:35:18 +00:00
|
|
|
$timestamp = current_time( 'timestamp', true );
|
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.
|
2017-08-11 04:50:44 +00:00
|
|
|
$dtStr = date("c", $timestamp);
|
|
|
|
$wc_timestamp = new WC_DateTime($dtStr);
|
2017-08-11 04:35:18 +00:00
|
|
|
|
|
|
|
$download_log = new WC_Customer_Download_Log;
|
|
|
|
$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' );
|
|
|
|
|
|
|
|
$download_1 = new WC_Customer_Download;
|
|
|
|
$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.
|
2017-08-11 04:35:18 +00:00
|
|
|
$data_store = WC_Data_Store::load( 'customer-download-log' );
|
|
|
|
$download_logs = $data_store->get_download_logs( array(
|
|
|
|
'permission_id' => $download_1->get_id()
|
|
|
|
) );
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|