Skip to content

Commit

Permalink
Payments Network: Add tests for JPM wires
Browse files Browse the repository at this point in the history
  • Loading branch information
iandunn committed Jul 24, 2023
1 parent 84a25e4 commit ff68919
Show file tree
Hide file tree
Showing 6 changed files with 200 additions and 2 deletions.
4 changes: 3 additions & 1 deletion phpunit-bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
const SITE_ID_CURRENT_SITE = WORDCAMP_NETWORK_ID;

define( 'WP_PLUGIN_DIR', __DIR__ . '/public_html/wp-content/plugins' );
define( 'SUT_WPMU_PLUGIN_DIR', __DIR__ . '/public_html/wp-content/mu-plugins' ); // WPMU_PLUGIN_DIR will be in `WP_TESTS_DIR`.
define( 'SUT_WP_CONTENT_DIR', __DIR__ . '/public_html/wp-content/' ); // WP_CONTENT_DIR will be in `WP_TESTS_DIR`.
define( 'SUT_WPMU_PLUGIN_DIR', SUT_WP_CONTENT_DIR . '/mu-plugins' ); // WPMU_PLUGIN_DIR will be in `WP_TESTS_DIR`.

$core_tests_directory = getenv( 'WP_TESTS_DIR' );

Expand Down Expand Up @@ -46,6 +47,7 @@
require_once WP_PLUGIN_DIR . '/wcpt/tests/bootstrap.php';
require_once( WP_PLUGIN_DIR . '/wordcamp-remote-css/tests/bootstrap.php' );
require_once WP_PLUGIN_DIR . '/wordcamp-speaker-feedback/tests/bootstrap.php';
require_once WP_PLUGIN_DIR . '/wordcamp-payments-network/tests/bootstrap.php';
require_once SUT_WPMU_PLUGIN_DIR . '/tests/bootstrap.php';

/*
Expand Down
1 change: 1 addition & 0 deletions phpunit-watcher.yml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ watch:
- ./public_html/wp-content/mu-plugins/
- ./public_html/wp-content/plugins/camptix/
- ./public_html/wp-content/plugins/wordcamp-organizer-reminders/
- ./public_html/wp-content/plugins/wordcamp-payments-network/
- ./public_html/wp-content/plugins/wc-post-types/
- ./public_html/wp-content/plugins/wcpt/
- ./public_html/wp-content/plugins/wordcamp-remote-css/
Expand Down
8 changes: 8 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@
</directory>
</testsuite>

<testsuite name="WordCamp Budgets Dashboard">
<directory prefix="test-" suffix=".php">
./public_html/wp-content/plugins/wordcamp-payments-network/tests/
</directory>
</testsuite>

<testsuite name="WordCamp Post Types">
<directory prefix="test-" suffix=".php">
./public_html/wp-content/plugins/wc-post-types/tests/
Expand Down Expand Up @@ -64,6 +70,8 @@
<directory suffix=".php">./public_html/wp-content/plugins/wc-post-types</directory>
<directory suffix=".php">./public_html/wp-content/plugins/wcpt</directory>
<directory suffix=".php">./public_html/wp-content/plugins/wordcamp-organizer-reminders</directory>
<directory suffix=".php">./public_html/wp-content/plugins/wordcamp-payments/</directory>
<directory suffix=".php">./public_html/wp-content/plugins/wordcamp-payments-network/</directory>
<directory suffix=".php">./public_html/wp-content/plugins/wordcamp-remote-css</directory>
<directory suffix=".php">./public_html/wp-content/plugins/wordcamp-speaker-feedback</directory>
<file>./public_html/wp-content/sunrise.php</file>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use WordCamp\Budgets_Dashboard\Reimbursement_Requests as Reimbursements_Dashboard;
use DateTimeInterface;
use WP_CLI;
use WP_Error;

defined( 'WPINC' ) || die();

Expand Down Expand Up @@ -468,7 +469,7 @@ function process_export_request() {
}
}

/*
/**
* Generate and return the raw payment report contents
*
* @param array $args
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace WordCamp\Budgets_Dashboard\Tests;

if ( 'cli' !== php_sapi_name() ) {
return;
}

/**
* Load the plugins that we'll need to be active for the tests.
*/
function manually_load_plugin() {
// @todo switch to `require_once` once it's accessible in all local environments.
// @link https://github.com/WordPress/wordcamp.org/issues/769
include_once SUT_WP_CONTENT_DIR . '/mu-plugins-private/wporg-mu-plugins/pub-sync/utilities/class-export-csv.php';

require_once WP_PLUGIN_DIR . '/wordcamp-payments/includes/wordcamp-budgets.php';
require_once WP_PLUGIN_DIR . '/wordcamp-payments/includes/payment-request.php';
require_once WP_PLUGIN_DIR . '/wordcamp-payments/includes/encryption.php';

require_once dirname( __DIR__ ) . '/includes/payment-requests-dashboard.php';
require_once dirname( __DIR__ ) . '/includes/wordcamp-budgets-dashboard.php';
}

tests_add_filter( 'muplugins_loaded', __NAMESPACE__ . '\manually_load_plugin' );
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
<?php

namespace WordCamp\Budgets_Dashboard\Tests;

use Payment_Requests_Dashboard;
use WCP_Encryption;
use WP_UnitTestCase;
use function WordCamp\Budgets_Dashboard\{ generate_payment_report };

defined( 'WPINC' ) || die();

/**
* Class Test_Budgets_Dashboard
*
* @group budgets-dashboard
*/
class Test_Budgets_Dashboard extends WP_UnitTestCase {
/**
* Set up shared fixtures for these tests.
*
* @param WP_UnitTest_Factory $factory
*/
public static function wpSetUpBeforeClass( $factory ) {
define( 'WORDCAMP_PAYMENTS_ENCRYPTION_KEY', 'key' );
define( 'WORDCAMP_PAYMENTS_HMAC_KEY', 'hmac' );

$factory->post->create( array(
'post_type' => 'wcp_payment_request',
'post_status' => 'wcb-approved',

'meta_input' => array(
'_wcb_updated_timestamp' => strtotime( 'Yesterday 10am' ),
'_camppayments_description' => 'Test Request',
'_camppayments_due_by' => strtotime( 'Next Tuesday' ),
'_camppayments_payment_amount' => '500',
'_camppayments_currency' => 'USD',
'_camppayments_payment_method' => 'Wire',
'_camppayments_invoice_number' => 'Invoice 1234',
'_camppayments_payment_category' => 'audio-visual',

'_camppayments_bank_name' => WCP_Encryption::encrypt( 'A Bank' ),
'_camppayments_bank_street_address' => WCP_Encryption::encrypt( '1234 Bank St' ),
'_camppayments_bank_city' => WCP_Encryption::encrypt( 'Bankersville' ),
'_camppayments_bank_state' => WCP_Encryption::encrypt( 'New Bankswick' ),
'_camppayments_bank_zip_code' => WCP_Encryption::encrypt( '12345' ),
'_camppayments_bank_country_iso3166' => WCP_Encryption::encrypt( 'US' ),
'_camppayments_bank_bic' => WCP_Encryption::encrypt( '123456' ),

'_camppayments_beneficiary_name' => WCP_Encryption::encrypt( 'Jane Beneficiary' ),
'_camppayments_beneficiary_street_address' => WCP_Encryption::encrypt( '9876 Beneficiary St' ),
'_camppayments_beneficiary_city' => WCP_Encryption::encrypt( 'Benficiaryville' ),
'_camppayments_beneficiary_state' => WCP_Encryption::encrypt( 'New Bennieswick' ),
'_camppayments_beneficiary_zip_code' => WCP_Encryption::encrypt( '98765' ),
'_camppayments_beneficiary_country_iso3166' => WCP_Encryption::encrypt( 'Test' ),
'_camppayments_beneficiary_account_number' => WCP_Encryption::encrypt( '987654' ),
),
) );

Payment_Requests_Dashboard::upgrade(); // Create index table.
Payment_Requests_Dashboard::aggregate(); // Populate index table.
}

/**
* @covers WordCamp\Budgets_Dashboard\generate_payment_report
* @covers WordCamp\Budgets_Dashboard\_generate_payment_report_jpm_wires
* @covers WCP_Payment_Request::_generate_payment_report_jpm_wires
*
* @dataProvider data_generate_payment_report
*/
public function test_generate_payment_report( array $args, string $expected ) : void {
if ( ! class_exists( 'WordPressdotorg\MU_Plugins\Utilities\Export_CSV' ) ) {
$this->markTestSkipped( 'Export_CSV class not found.' );
}

$actual = generate_payment_report( $args );

if ( is_wp_error( $actual ) ) {
$actual = $actual->get_error_message();
}

$this->assertSame( $expected, $actual );
}

/**
* Test cases for `test_generate_payment_report()`.
*/
public function data_generate_payment_report() : array {
// This isn't guaranteed to match the value used in `_generate_payment_report_jpm_wires()` due to the time
// between when this case is generated and when the test is ran. It seems to work most of the time though.
$export_date = gmdate( 'YmdHis' );

$cases = array(
'vendor payment wire' => array(
'args' => array(
'status' => 'wcb-approved',
'start_date' => strtotime( '3 days ago' ),
'end_date' => time(),
'post_type' => 'wcp_payment_request',

'export_type' => array(
'label' => 'JP Morgan Access - Wire Payments',
'mime_type' => 'text/csv',
'callback' => 'WordCamp\Budgets_Dashboard\_generate_payment_report_jpm_wires',
'filename' => 'wordcamp-payments-%s-%s-jpm-wires.csv',
),
),

'expected' => <<<EOD
HEADER,$export_date,1
P,WIRES,,,N,USD,500.00,,,,,,,ACCT,987654,"Jane Beneficiary","9876 Beneficiary St",,"Benficiaryville New Bennieswick ",,Test,,,SWIFT,123456,"A Bank","1234 Bank St",,"Bankersville New Bankswick 12345",US,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,"Invoice 1234",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,OUR,,,wcb-1-4
TRAILER,1,500
EOD
,
),

'no matching posts' => array(
'args' => array(
'status' => 'wcb-approved',
'start_date' => strtotime( '8 days ago' ),
'end_date' => strtotime( '5 days ago' ),
'post_type' => 'wcp_payment_request',

'export_type' => array(
'label' => 'JP Morgan Access - Wire Payments',
'mime_type' => 'text/csv',
'callback' => 'WordCamp\Budgets_Dashboard\_generate_payment_report_jpm_wires',
'filename' => 'wordcamp-payments-%s-%s-jpm-wires.csv',
),
),

'expected' => <<<EOD
HEADER,$export_date,1
TRAILER,0,0
EOD
,
),

'Invalid date' => array(
'args' => array(
'status' => 'wcb-approved',
'start_date' => 'invalid date',
'end_date' => strtotime( '5 days ago' ),
'post_type' => 'wcp_payment_request',

'export_type' => array(
'label' => 'JP Morgan Access - Wire Payments',
'mime_type' => 'text/csv',
'callback' => 'WordCamp\Budgets_Dashboard\_generate_payment_report_jpm_wires',
'filename' => 'wordcamp-payments-%s-%s-jpm-wires.csv',
),
),

'expected' => 'Invalid start or end date.',
),
);

return $cases;
}
}

0 comments on commit ff68919

Please sign in to comment.