Add unit tests for DownloadPermissionsAdjuster::adjust_download_permissions

This commit is contained in:
Nestor Soriano 2021-01-13 12:11:15 +01:00
parent 2d83db8b66
commit 2ec002defa
2 changed files with 269 additions and 45 deletions

View File

@ -135,7 +135,6 @@ class DownloadPermissionsAdjuster {
'permission_data_by_file_order_user' => array(),
'download_ids_by_file_url' => array(),
);
$downloads = $product->get_downloads();
foreach ( $downloads as $download ) {
$result['download_ids_by_file_url'][ $download->get_file() ] = $download->get_id();
@ -144,6 +143,7 @@ class DownloadPermissionsAdjuster {
$permissions = $this->downloads_data_store->get_downloads( array( 'product_id' => $product->get_id() ) );
foreach ( $permissions as $permission ) {
$permission_data = (array) $permission->data;
if ( array_key_exists( $permission_data['download_id'], $downloads ) ) {
$file = $downloads[ $permission_data['download_id'] ]->get_file();
$data = array(
'file' => $file,
@ -151,6 +151,7 @@ class DownloadPermissionsAdjuster {
);
$result['permission_data_by_file_order_user'][ "${file}:${permission_data['user_id']}:${permission_data['order_id']}" ] = $data;
}
}
return $result;
}

View File

@ -11,8 +11,8 @@ use Automattic\WooCommerce\RestApi\UnitTests\Helpers\ProductHelper;
/**
* Tests for DownloadPermissionsAdjuster.
*/
class DownloadPermissionsAdjusterTest extends \WC_Unit_Test_Case
{
class DownloadPermissionsAdjusterTest extends \WC_Unit_Test_Case {
/**
* The system under test.
*
@ -26,13 +26,28 @@ class DownloadPermissionsAdjusterTest extends \WC_Unit_Test_Case
public function setUp() {
$this->sut = new DownloadPermissionsAdjuster();
$this->sut->init();
// This is needed for "product->set_downloads" to work without actual files.
add_filter(
'woocommerce_downloadable_file_allowed_mime_types',
function() {
return array( 'foo' => 'nonsense/foo' );
}
);
add_filter(
'woocommerce_downloadable_file_exists',
function( $exists, $filename ) {
return true;
},
2,
10
);
}
/**
* @testdox DownloadPermissionsAdjuster class hooks on 'adjust_download_permissions' on initialization.
*/
public function test_class_hooks_on_adjust_download_permissions()
{
public function test_class_hooks_on_adjust_download_permissions() {
remove_all_actions( 'adjust_download_permissions' );
$this->assertFalse( has_action( 'adjust_download_permissions' ) );
$this->setUp();
@ -53,7 +68,7 @@ class DownloadPermissionsAdjusterTest extends \WC_Unit_Test_Case
},
'as_schedule_single_action' => function( $timestamp, $hook, $args ) use ( &$as_schedule_single_action_invoked ) {
$as_schedule_single_action_invoked = true;
}
},
)
);
@ -79,7 +94,7 @@ class DownloadPermissionsAdjusterTest extends \WC_Unit_Test_Case
},
'as_schedule_single_action' => function( $timestamp, $hook, $args ) use ( &$as_schedule_single_action_invoked ) {
$as_schedule_single_action_invoked = true;
}
},
)
);
@ -89,7 +104,7 @@ class DownloadPermissionsAdjusterTest extends \WC_Unit_Test_Case
$expected_get_scheduled_actions_args = array(
'hook' => 'adjust_download_permissions',
'args' => array( $product->get_id() ),
'status' => \ActionScheduler_Store::STATUS_PENDING
'status' => \ActionScheduler_Store::STATUS_PENDING,
);
$this->assertEquals( $expected_get_scheduled_actions_args, $as_get_scheduled_actions_args );
$this->assertFalse( $as_schedule_single_action_invoked );
@ -111,7 +126,8 @@ class DownloadPermissionsAdjusterTest extends \WC_Unit_Test_Case
'as_schedule_single_action' => function( $timestamp, $hook, $args ) use ( &$as_schedule_single_action_args ) {
$as_schedule_single_action_args = array( $timestamp, $hook, $args );
},
'time' => function() { return 0; }
'time' => function() {
return 0; },
)
);
@ -121,15 +137,222 @@ class DownloadPermissionsAdjusterTest extends \WC_Unit_Test_Case
$expected_get_scheduled_actions_args = array(
'hook' => 'adjust_download_permissions',
'args' => array( $product->get_id() ),
'status' => \ActionScheduler_Store::STATUS_PENDING
'status' => \ActionScheduler_Store::STATUS_PENDING,
);
$this->assertEquals( $expected_get_scheduled_actions_args, $as_get_scheduled_actions_args );
$expected_as_schedule_single_action_args = array(
1,
'adjust_download_permissions',
array( $product->get_id() )
array( $product->get_id() ),
);
$this->assertEquals( $expected_as_schedule_single_action_args, $as_schedule_single_action_args );
}
/**
* @testdox 'adjust_download_permissions' creates child download permissions when they are missing (see method comment for details).
*/
public function test_adjust_download_permissions_creates_additional_permissions_if_not_exist() {
$download = array(
'name' => 'the_file',
'file' => 'the_file.foo',
);
$product = ProductHelper::create_variation_product();
$product->set_downloads( array( $download ) );
$product->save();
$parent_download_id = current( $product->get_downloads() )->get_id();
$child = wc_get_product( current( $product->get_children() ) );
$child->set_downloads( array( $download ) );
$child->save();
$child_download_id = current( $child->get_downloads() )->get_id();
$data_for_data_store =
array(
$product->get_id() =>
array(
(object) array(
'data' => array(
'download_id' => $parent_download_id,
'user_id' => 1234,
'order_id' => 5678,
'downloads_remaining' => 34,
'access_granted' => '2000-01-01',
'access_expires' => '2034-02-27',
),
),
),
);
$data_store = $this->create_mock_data_store( $data_for_data_store );
$this->setUp();
$this->sut->adjust_download_permissions( $product->get_id() );
$expected_created_data = array(
'download_id' => $child_download_id,
'user_id' => 1234,
'order_id' => 5678,
'product_id' => $child->get_id(),
'downloads_remaining' => 34,
'access_granted' => '2000-01-01',
'access_expires' => '2034-02-27',
);
$this->assertEquals( $expected_created_data, $data_store->created_data );
}
/**
* @testdox 'adjust_download_permissions' doesn't create child download permissions that already exist.
*/
public function test_adjust_download_permissions_dont_create_additional_permissions_if_already_exists() {
$download = array(
'name' => 'the_file',
'file' => 'the_file.foo',
);
$product = ProductHelper::create_variation_product();
$product->set_downloads( array( $download ) );
$product->save();
$parent_download_id = current( $product->get_downloads() )->get_id();
$child = wc_get_product( current( $product->get_children() ) );
$child->set_downloads( array( $download ) );
$child->save();
$child_download_id = current( $child->get_downloads() )->get_id();
$data_for_data_store =
array(
$product->get_id() =>
array(
(object) array(
'data' => array(
'download_id' => $parent_download_id,
'user_id' => 1234,
'order_id' => 5678,
),
),
),
$child->get_id() =>
array(
(object) array(
'data' => array(
'download_id' => $child_download_id,
'user_id' => 1234,
'order_id' => 5678,
),
),
),
);
$data_store = $this->create_mock_data_store( $data_for_data_store );
$this->setUp();
$this->sut->adjust_download_permissions( $product->get_id() );
$this->assertEmpty( $data_store->created_data );
}
/**
* @testdox 'adjust_download_permissions' creates child download permissions when one exists but for a different order or customer id.
*
* @testWith [9999, 5678]
* [1234, 9999]
* @param int $user_id User id the child download permission exists for.
* @param int $order_id Order id the child download permission exists for.
*/
public function test_adjust_download_permissions_creates_additional_permissions_if_exists_but_not_matching( $user_id, $order_id ) {
$download = array(
'name' => 'the_file',
'file' => 'the_file.foo',
);
$product = ProductHelper::create_variation_product();
$product->set_downloads( array( $download ) );
$product->save();
$parent_download_id = current( $product->get_downloads() )->get_id();
$child = wc_get_product( current( $product->get_children() ) );
$child->set_downloads( array( $download ) );
$child->save();
$child_download_id = current( $child->get_downloads() )->get_id();
$data_for_data_store =
array(
$product->get_id() =>
array(
(object) array(
'data' => array(
'download_id' => $parent_download_id,
'user_id' => 1234,
'order_id' => 5678,
),
),
),
$child->get_id() =>
array(
(object) array(
'data' => array(
'download_id' => $child_download_id,
'user_id' => $user_id,
'order_id' => $order_id,
),
),
),
);
$data_store = $this->create_mock_data_store( $data_for_data_store );
$this->setUp();
$this->sut->adjust_download_permissions( $product->get_id() );
$expected = array(
'download_id' => $child_download_id,
'user_id' => 1234,
'order_id' => 5678,
'product_id' => $child->get_id(),
);
$this->assertEquals( $expected, $data_store->created_data );
}
/**
* Create and register a mock customer downloads data store.
*
* @param array $data An array where keys are product ids, and values are what 'get_downloads' will return for that input.
* @return object An object that mocks the customer downloads data store.
*/
private function create_mock_data_store( $data ) {
// phpcs:disable Squiz.Commenting
$data_store = new class($data) {
private $data;
public $created_data = null;
public function __construct( $data ) {
$this->data = $data;
}
public function get_downloads( $params ) {
if ( array_key_exists( $params['product_id'], $this->data ) ) {
return $this->data[ $params['product_id'] ];
} else {
return array();
}
}
public function create_from_data( $data ) {
$this->created_data = $data;
}
};
// phpcs:enable Squiz.Commenting
$this->register_legacy_proxy_class_mocks(
array(
'WC_Data_Store' => $data_store,
)
);
return $data_store;
}
}