Skip to content

Commit

Permalink
Update compatibility data output (#8935)
Browse files Browse the repository at this point in the history
  • Loading branch information
jessepearson committed Jun 18, 2024
1 parent 156221f commit 549100e
Show file tree
Hide file tree
Showing 4 changed files with 183 additions and 7 deletions.
4 changes: 4 additions & 0 deletions changelog/fix-5532-fix-compatibility-data
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: fix

Fix output for compatibility data.
21 changes: 17 additions & 4 deletions includes/class-compatibility-service.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,10 @@ public function add_compatibility_onboarding_data( $args ): array {
private function get_compatibility_data(): array {
$active_plugins = get_option( 'active_plugins', [] );
$post_types_count = $this->get_post_types_count();
$wc_permalinks = get_option( 'woocommerce_permalinks' );
$wc_shop_permalink = get_permalink( wc_get_page_id( 'shop' ) );
$wc_cart_permalink = get_permalink( wc_get_page_id( 'cart' ) );
$wc_checkout_permalink = get_permalink( wc_get_page_id( 'checkout' ) );
$wc_permalinks = get_option( 'woocommerce_permalinks', [] );
$wc_shop_permalink = $this->get_permalink_for_page_id( 'shop' );
$wc_cart_permalink = $this->get_permalink_for_page_id( 'cart' );
$wc_checkout_permalink = $this->get_permalink_for_page_id( 'checkout' );

return [
'woopayments_version' => WCPAY_VERSION_NUMBER,
Expand Down Expand Up @@ -114,4 +114,17 @@ private function get_post_types_count(): array {

return $post_types_count;
}

/**
* Gets the permalink for a page ID.
*
* @param string $page_id The page ID to get the permalink for.
*
* @return string The permalink for the page ID, or 'Not set' if the permalink is not available.
*/
private function get_permalink_for_page_id( string $page_id ): string {
$permalink = get_permalink( wc_get_page_id( $page_id ) );

return $permalink ? $permalink : 'Not set';
}
}
95 changes: 93 additions & 2 deletions tests/unit/test-class-compatibility-service.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class Compatibility_Service_Test extends WCPAY_UnitTestCase {
*/
private $post_types_count = [
'post' => 1,
'page' => 6,
'page' => 4,
'attachment' => 0,
'product' => 12,
];
Expand Down Expand Up @@ -158,6 +158,40 @@ public function test_update_compatibility_data_active_plugins_false() {
$this->fix_active_plugins_option();
}

/**
* Checks to make sure "Not set" is returned if a page id is not returned.
*
* @dataProvider provider_update_compatibility_data_permalinks_not_set
*/
public function test_update_compatibility_data_permalinks_not_set( $page_name ) {
// Arrange: Create the expected value to be passed to update_compatibility_data.
$expected = $this->get_mock_compatibility_data(
[
'woocommerce_' . $page_name => 'Not set',
]
);

// Arrange: Delete the page id reference from the database.
delete_option( 'woocommerce_' . $page_name . '_page_id' );

// Arrange/Assert: Set the expectations for update_compatibility_data.
$this->mock_api_client
->expects( $this->once() )
->method( 'update_compatibility_data' )
->with( $expected );

// Act: Call the method we're testing.
$this->compatibility_service->update_compatibility_data();
}

public function provider_update_compatibility_data_permalinks_not_set(): array {
return [
'shop' => [ 'shop' ],
'cart' => [ 'cart' ],
'checkout' => [ 'checkout' ],
];
}

public function test_add_compatibility_onboarding_data() {
// Arrange: Create the expected value.
$expected = [ 'compatibility_data' => $this->get_mock_compatibility_data() ];
Expand All @@ -178,7 +212,7 @@ private function get_mock_compatibility_data( array $args = [] ): array {
[
'woopayments_version' => WCPAY_VERSION_NUMBER,
'woocommerce_version' => WC_VERSION,
'woocommerce_permalinks' => get_option( 'woocommerce_permalinks' ),
'woocommerce_permalinks' => get_option( 'woocommerce_permalinks', [] ),
'woocommerce_shop' => get_permalink( wc_get_page_id( 'shop' ) ),
'woocommerce_cart' => get_permalink( wc_get_page_id( 'cart' ) ),
'woocommerce_checkout' => get_permalink( wc_get_page_id( 'checkout' ) ),
Expand Down Expand Up @@ -262,6 +296,11 @@ private function insert_test_posts( array $post_types = [] ): array {
$post_types = ! empty( $post_types ) ? $post_types : $this->post_types_count;
$post_ids = [];
foreach ( $post_types as $post_type => $count ) {
// Let's create the default WooCommerce pages for the test pages.
if ( 'page' === $post_type ) {
$post_ids = array_merge( $post_ids, $this->create_woocommerce_default_pages() );
continue;
}
$title_content = 'This is a ' . $post_type . ' test post';
for ( $i = 0; $i < $count; $i++ ) {
$post_ids[] = (int) wp_insert_post(
Expand Down Expand Up @@ -290,4 +329,56 @@ private function delete_test_posts( array $post_ids = [] ) {
wp_delete_post( (int) $post_id, true );
}
}

/**
* Creates the default WooCommerce pages for test purposes.
*
* @return array Array of post IDs that were created.
*/
private function create_woocommerce_default_pages(): array {
// Note: Inspired by WC_Install::create_pages().

$pages = [
'shop' => [
'name' => 'shop',
'title' => 'Shop',
'content' => '',
],
'cart' => [
'name' => 'cart',
'title' => 'Cart',
'content' => '',
],
'checkout' => [
'name' => 'checkout',
'title' => 'Checkout',
'content' => '',
],
'myaccount' => [
'name' => 'my-account',
'title' => 'My account',
'content' => '',
],
'refund_returns' => [
'name' => 'refund_returns',
'title' => 'Refund and Returns Policy',
'content' => '',
'post_status' => 'draft',
],
];

$page_ids = [];
foreach ( $pages as $key => $page ) {
$page_ids[] = wc_create_page(
esc_sql( $page['name'] ),
'woocommerce_' . $key . '_page_id',
$page['title'],
$page['content'],
'',
! empty( $page['post_status'] ) ? $page['post_status'] : 'publish'
);
}

return $page_ids;
}
}
70 changes: 69 additions & 1 deletion tests/unit/wc-payment-api/test-class-wc-payments-api-client.php
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,8 @@ public function test_get_onboarding_data() {
'f' => 6,
];

$default_wc_pages = $this->create_woocommerce_default_pages();

$this->mock_http_client
->expects( $this->once() )
->method( 'remote_request' )
Expand Down Expand Up @@ -326,6 +328,9 @@ function ( $data ): bool {

// Assert the response is correct.
$this->assertEquals( [ 'url' => false ], $result );

// Remove test pages created.
$this->delete_test_posts( $default_wc_pages );
}

/**
Expand Down Expand Up @@ -1267,12 +1272,75 @@ private function get_mock_compatibility_data( array $args = [] ): array {
'active_plugins' => [],
'post_types_count' => [
'post' => 0,
'page' => 0,
'page' => 4,
'attachment' => 0,
'product' => 0,
],
],
$args
);
}

/**
* Creates the default WooCommerce pages for test purposes.
*
* @return array Array of post IDs that were created.
*/
private function create_woocommerce_default_pages(): array {
// Note: Inspired by WC_Install::create_pages().

$pages = [
'shop' => [
'name' => 'shop',
'title' => 'Shop',
'content' => '',
],
'cart' => [
'name' => 'cart',
'title' => 'Cart',
'content' => '',
],
'checkout' => [
'name' => 'checkout',
'title' => 'Checkout',
'content' => '',
],
'myaccount' => [
'name' => 'my-account',
'title' => 'My account',
'content' => '',
],
'refund_returns' => [
'name' => 'refund_returns',
'title' => 'Refund and Returns Policy',
'content' => '',
'post_status' => 'draft',
],
];

$page_ids = [];
foreach ( $pages as $key => $page ) {
$page_ids[] = wc_create_page(
esc_sql( $page['name'] ),
'woocommerce_' . $key . '_page_id',
$page['title'],
$page['content'],
'',
! empty( $page['post_status'] ) ? $page['post_status'] : 'publish'
);
}

return $page_ids;
}

/**
* Delete test posts that were created during a unit test.
*
* @param array $post_ids Array of post IDs to delete.
*/
private function delete_test_posts( array $post_ids = [] ) {
foreach ( $post_ids as $post_id ) {
wp_delete_post( (int) $post_id, true );
}
}
}

0 comments on commit 549100e

Please sign in to comment.