From 7920616b148e6eeb13e39b0296d797cd963d186f Mon Sep 17 00:00:00 2001 From: David Evbodaghe Date: Thu, 9 Jan 2025 23:15:39 +0000 Subject: [PATCH 1/4] Update catalog link to navigate to correct tab in Commerce Manager --- .../Admin/Settings_Screens/Connection.php | 32 +++++++- tests/Unit/ConnectionTest.php | 80 +++++++++++++++++++ 2 files changed, 108 insertions(+), 4 deletions(-) create mode 100644 tests/Unit/ConnectionTest.php diff --git a/includes/Admin/Settings_Screens/Connection.php b/includes/Admin/Settings_Screens/Connection.php index ccadcc47..0f7a8fcf 100644 --- a/includes/Admin/Settings_Screens/Connection.php +++ b/includes/Admin/Settings_Screens/Connection.php @@ -94,6 +94,22 @@ public function enqueue_assets() { } + /** + * Gets the catalog URL based on catalog ID and business ID. + * + * @param string $catalog_id The catalog ID + * @param string $business_id The business ID (optional) + * @return string The catalog URL + */ + private function get_catalog_url(string $catalog_id, string $business_id = ''): string + { + if ($business_id) { + return "https://www.facebook.com/commerce/catalogs/{$catalog_id}/products/?business_id={$business_id}"; + } + return "https://facebook.com/products/catalog/{$catalog_id}"; + } + + /** * Renders the screen. * @@ -159,16 +175,24 @@ public function render() { ); // if the catalog ID is set, update the URL and try to get its name for display - if ( $catalog_id = $static_items['catalog']['value'] ) { - - $static_items['catalog']['url'] = "https://facebook.com/products/catalogs/{$catalog_id}"; - + $catalog_id = $static_items['catalog']['value']; + $business_id = $static_items['business-manager']['value']; + if (!empty($catalog_id)) { + $static_items['catalog']['url'] = $this->get_catalog_url($catalog_id, $business_id); try { $response = facebook_for_woocommerce()->get_api()->get_catalog( $catalog_id ); if ( $name = $response->name ) { $static_items['catalog']['value'] = $name; } } catch ( ApiException $exception ) { + // Log the exception with additional information + facebook_for_woocommerce()->log( + sprintf( + 'Connection failed for catalog %s: %s ', + $catalog_id, + $exception->getMessage(), + ) + ); } } diff --git a/tests/Unit/ConnectionTest.php b/tests/Unit/ConnectionTest.php new file mode 100644 index 00000000..b6e61fe3 --- /dev/null +++ b/tests/Unit/ConnectionTest.php @@ -0,0 +1,80 @@ +connection = new Connection(); + } + + public function testGetCatalogUrl(): void { + $catalog_id = '123456' ; + $business_id = '789012'; + + $expected_url = "https://www.facebook.com/commerce/catalogs/{$catalog_id}/products/?business_id={$business_id}"; + + // Use reflection to access private method + $method = new \ReflectionMethod(Connection::class, 'get_catalog_url'); + $method->setAccessible(true); + + $actual_url = $method->invoke($this->connection, $catalog_id, $business_id); + + $this->assertEquals($expected_url, $actual_url); + } + + public function testGetCatalogUrlWithoutBusinessId(): void { + $catalog_id = '123456'; + + $expected_url = "https://facebook.com/products/catalog/{$catalog_id}"; + + // Use reflection to access private method + $method = new \ReflectionMethod(Connection::class, 'get_catalog_url'); + $method->setAccessible(true); + + $actual_url = $method->invoke($this->connection, $catalog_id, ''); + + $this->assertEquals($expected_url, $actual_url); + } + + public function testEnqueueAssetsWhenNotOnPage(): void { + // Mock is_current_screen_page to return false + $connection = $this->getMockBuilder(Connection::class) + ->onlyMethods(['is_current_screen_page']) + ->getMock(); + + $connection->method('is_current_screen_page') + ->willReturn(false); + + // No styles should be enqueued + $connection->enqueue_assets(); + + $this->assertFalse(wp_style_is('wc-facebook-admin-connection-settings')); + } + + public function testGetSettings(): void { + $settings = $this->connection->get_settings(); + + $this->assertIsArray($settings); + $this->assertNotEmpty($settings); + + // Check that the settings array has the expected structure + $this->assertArrayHasKey('type', $settings[0]); + $this->assertEquals('title', $settings[0]['type']); + + // Check debug mode setting + $debug_setting = $settings[1]; + $this->assertEquals('checkbox', $debug_setting['type']); + $this->assertEquals('no', $debug_setting['default']); + + // Check feed generator setting + $feed_setting = $settings[2]; + $this->assertEquals('checkbox', $feed_setting['type']); + $this->assertEquals('no', $feed_setting['default']); + } +} From 73add3d952b87620d2c0de35061006d4b0689e74 Mon Sep 17 00:00:00 2001 From: David Evbodaghe Date: Fri, 10 Jan 2025 10:54:20 +0000 Subject: [PATCH 2/4] Update catalog link to navigate to correct tab in Commerce Manager --- tests/Unit/{ => Admin/Settings}/ConnectionTest.php | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename tests/Unit/{ => Admin/Settings}/ConnectionTest.php (100%) diff --git a/tests/Unit/ConnectionTest.php b/tests/Unit/Admin/Settings/ConnectionTest.php similarity index 100% rename from tests/Unit/ConnectionTest.php rename to tests/Unit/Admin/Settings/ConnectionTest.php From b78360683fac0eb05da885cdc0a45228baf59954 Mon Sep 17 00:00:00 2001 From: David Evbodaghe Date: Fri, 10 Jan 2025 18:17:38 +0000 Subject: [PATCH 3/4] Update catalog link to navigate to correct tab in Commerce Manager --- .../Admin/Settings_Screens/Connection.php | 21 ++------------ tests/Unit/Admin/Settings/ConnectionTest.php | 29 ------------------- 2 files changed, 2 insertions(+), 48 deletions(-) diff --git a/includes/Admin/Settings_Screens/Connection.php b/includes/Admin/Settings_Screens/Connection.php index 0f7a8fcf..ebd32c3f 100644 --- a/includes/Admin/Settings_Screens/Connection.php +++ b/includes/Admin/Settings_Screens/Connection.php @@ -94,22 +94,6 @@ public function enqueue_assets() { } - /** - * Gets the catalog URL based on catalog ID and business ID. - * - * @param string $catalog_id The catalog ID - * @param string $business_id The business ID (optional) - * @return string The catalog URL - */ - private function get_catalog_url(string $catalog_id, string $business_id = ''): string - { - if ($business_id) { - return "https://www.facebook.com/commerce/catalogs/{$catalog_id}/products/?business_id={$business_id}"; - } - return "https://facebook.com/products/catalog/{$catalog_id}"; - } - - /** * Renders the screen. * @@ -176,9 +160,8 @@ public function render() { // if the catalog ID is set, update the URL and try to get its name for display $catalog_id = $static_items['catalog']['value']; - $business_id = $static_items['business-manager']['value']; - if (!empty($catalog_id)) { - $static_items['catalog']['url'] = $this->get_catalog_url($catalog_id, $business_id); + if ( !empty($catalog_id)) { + $static_items['catalog']['url'] = "https://www.facebook.com/commerce/catalogs/{$catalog_id}/products/"; try { $response = facebook_for_woocommerce()->get_api()->get_catalog( $catalog_id ); if ( $name = $response->name ) { diff --git a/tests/Unit/Admin/Settings/ConnectionTest.php b/tests/Unit/Admin/Settings/ConnectionTest.php index b6e61fe3..18a94e4d 100644 --- a/tests/Unit/Admin/Settings/ConnectionTest.php +++ b/tests/Unit/Admin/Settings/ConnectionTest.php @@ -13,35 +13,6 @@ protected function setUp(): void { $this->connection = new Connection(); } - public function testGetCatalogUrl(): void { - $catalog_id = '123456' ; - $business_id = '789012'; - - $expected_url = "https://www.facebook.com/commerce/catalogs/{$catalog_id}/products/?business_id={$business_id}"; - - // Use reflection to access private method - $method = new \ReflectionMethod(Connection::class, 'get_catalog_url'); - $method->setAccessible(true); - - $actual_url = $method->invoke($this->connection, $catalog_id, $business_id); - - $this->assertEquals($expected_url, $actual_url); - } - - public function testGetCatalogUrlWithoutBusinessId(): void { - $catalog_id = '123456'; - - $expected_url = "https://facebook.com/products/catalog/{$catalog_id}"; - - // Use reflection to access private method - $method = new \ReflectionMethod(Connection::class, 'get_catalog_url'); - $method->setAccessible(true); - - $actual_url = $method->invoke($this->connection, $catalog_id, ''); - - $this->assertEquals($expected_url, $actual_url); - } - public function testEnqueueAssetsWhenNotOnPage(): void { // Mock is_current_screen_page to return false $connection = $this->getMockBuilder(Connection::class) From 056d2783b6010f06308a0b9d80b0ee43978df877 Mon Sep 17 00:00:00 2001 From: David Date: Mon, 13 Jan 2025 15:21:30 +0000 Subject: [PATCH 4/4] Update catalog link to navigate to correct tab in Commerce Manager Co-authored-by: Marian Shymon --- includes/Admin/Settings_Screens/Connection.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/Admin/Settings_Screens/Connection.php b/includes/Admin/Settings_Screens/Connection.php index ebd32c3f..5555df2d 100644 --- a/includes/Admin/Settings_Screens/Connection.php +++ b/includes/Admin/Settings_Screens/Connection.php @@ -160,7 +160,7 @@ public function render() { // if the catalog ID is set, update the URL and try to get its name for display $catalog_id = $static_items['catalog']['value']; - if ( !empty($catalog_id)) { + if ( !empty( $catalog_id ) ) { $static_items['catalog']['url'] = "https://www.facebook.com/commerce/catalogs/{$catalog_id}/products/"; try { $response = facebook_for_woocommerce()->get_api()->get_catalog( $catalog_id );