From 0f617bfbc4850c6ac1c92df36346740aa9cfeede Mon Sep 17 00:00:00 2001 From: Rodrigue Tusse Date: Thu, 3 Aug 2023 15:21:29 +0200 Subject: [PATCH 1/2] make get_options a bit more robust. --- facebook-commerce-pixel-event.php | 30 ++++++--- tests/Unit/FacebookCommercePixelEventTest.php | 61 +++++++++++++++++++ 2 files changed, 82 insertions(+), 9 deletions(-) create mode 100644 tests/Unit/FacebookCommercePixelEventTest.php diff --git a/facebook-commerce-pixel-event.php b/facebook-commerce-pixel-event.php index 0e1ca5daf..2a78fd81b 100644 --- a/facebook-commerce-pixel-event.php +++ b/facebook-commerce-pixel-event.php @@ -657,15 +657,27 @@ private static function get_version_info() { * Get PixelID related settings. */ public static function get_options() { - $facebook_config = get_option( self::SETTINGS_KEY ); - return is_array( $facebook_config ) && ! empty( $facebook_config ) - ? $facebook_config - : array( - self::PIXEL_ID_KEY => '0', - self::USE_PII_KEY => 0, - self::USE_S2S_KEY => false, - self::ACCESS_TOKEN_KEY => '', - ); + + $default_options = array( + self::PIXEL_ID_KEY => '0', + self::USE_PII_KEY => 0, + self::USE_S2S_KEY => false, + self::ACCESS_TOKEN_KEY => '', + ); + + $fb_options = get_option( self::SETTINGS_KEY ); + + if ( ! is_array( $fb_options ) ) { + $fb_options = $default_options; + } else { + foreach ( $default_options as $key => $value ) { + if ( ! isset( $fb_options[ $key ] ) ) { + $fb_options[ $key ] = $value; + } + } + } + + return $fb_options; } /** diff --git a/tests/Unit/FacebookCommercePixelEventTest.php b/tests/Unit/FacebookCommercePixelEventTest.php new file mode 100644 index 000000000..0d2684aea --- /dev/null +++ b/tests/Unit/FacebookCommercePixelEventTest.php @@ -0,0 +1,61 @@ + '0', + WC_Facebookcommerce_Pixel::USE_PII_KEY => 0, + WC_Facebookcommerce_Pixel::USE_S2S_KEY => false, + WC_Facebookcommerce_Pixel::ACCESS_TOKEN_KEY => '', + ); + + $actual_options = WC_Facebookcommerce_Pixel::get_options(); + + $this->assertEquals($expected_options, $actual_options); + } + + public function test_get_options_returns_merged_options_when_options_exist() { + $default_options = array( + WC_Facebookcommerce_Pixel::PIXEL_ID_KEY => '0', + WC_Facebookcommerce_Pixel::USE_PII_KEY => 0, + WC_Facebookcommerce_Pixel::USE_S2S_KEY => false, + WC_Facebookcommerce_Pixel::ACCESS_TOKEN_KEY => '', + ); + + $existing_options = array( + WC_Facebookcommerce_Pixel::PIXEL_ID_KEY => '123456789', + WC_Facebookcommerce_Pixel::USE_PII_KEY => 1, + WC_Facebookcommerce_Pixel::USE_S2S_KEY => true, + WC_Facebookcommerce_Pixel::ACCESS_TOKEN_KEY => 'abc123', + ); + + $expected_options = array_merge($default_options, $existing_options); + + update_option(WC_Facebookcommerce_Pixel::SETTINGS_KEY, $existing_options); + + $actual_options = WC_Facebookcommerce_Pixel::get_options(); + + $this->assertEquals($expected_options, $actual_options); + } + + public function test_get_options_returns_default_options_when_options_are_not_an_array() { + update_option(WC_Facebookcommerce_Pixel::SETTINGS_KEY, 'not an array'); + + $expected_options = array( + WC_Facebookcommerce_Pixel::PIXEL_ID_KEY => '0', + WC_Facebookcommerce_Pixel::USE_PII_KEY => 0, + WC_Facebookcommerce_Pixel::USE_S2S_KEY => false, + WC_Facebookcommerce_Pixel::ACCESS_TOKEN_KEY => '', + ); + + $actual_options = WC_Facebookcommerce_Pixel::get_options(); + + $this->assertEquals($expected_options, $actual_options); + } + +} From ff66f484e7d0715de0ebbaa45d4cae989915f9c8 Mon Sep 17 00:00:00 2001 From: Kader Ibrahim S Date: Thu, 3 Aug 2023 19:37:44 +0530 Subject: [PATCH 2/2] Apply coding standards to the \`FacebookCommercePixelTest\` class. --- tests/Unit/FacebookCommercePixelEventTest.php | 36 +++++++++---------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/tests/Unit/FacebookCommercePixelEventTest.php b/tests/Unit/FacebookCommercePixelEventTest.php index 0d2684aea..23f74ee85 100644 --- a/tests/Unit/FacebookCommercePixelEventTest.php +++ b/tests/Unit/FacebookCommercePixelEventTest.php @@ -8,54 +8,54 @@ class FacebookCommercePixelTest extends WP_UnitTestCase { */ public function test_get_options_returns_default_options_when_no_options_exist() { $expected_options = array( - WC_Facebookcommerce_Pixel::PIXEL_ID_KEY => '0', - WC_Facebookcommerce_Pixel::USE_PII_KEY => 0, - WC_Facebookcommerce_Pixel::USE_S2S_KEY => false, + WC_Facebookcommerce_Pixel::PIXEL_ID_KEY => '0', + WC_Facebookcommerce_Pixel::USE_PII_KEY => 0, + WC_Facebookcommerce_Pixel::USE_S2S_KEY => false, WC_Facebookcommerce_Pixel::ACCESS_TOKEN_KEY => '', ); $actual_options = WC_Facebookcommerce_Pixel::get_options(); - $this->assertEquals($expected_options, $actual_options); + $this->assertEquals( $expected_options, $actual_options ); } public function test_get_options_returns_merged_options_when_options_exist() { $default_options = array( - WC_Facebookcommerce_Pixel::PIXEL_ID_KEY => '0', - WC_Facebookcommerce_Pixel::USE_PII_KEY => 0, - WC_Facebookcommerce_Pixel::USE_S2S_KEY => false, + WC_Facebookcommerce_Pixel::PIXEL_ID_KEY => '0', + WC_Facebookcommerce_Pixel::USE_PII_KEY => 0, + WC_Facebookcommerce_Pixel::USE_S2S_KEY => false, WC_Facebookcommerce_Pixel::ACCESS_TOKEN_KEY => '', ); $existing_options = array( - WC_Facebookcommerce_Pixel::PIXEL_ID_KEY => '123456789', - WC_Facebookcommerce_Pixel::USE_PII_KEY => 1, - WC_Facebookcommerce_Pixel::USE_S2S_KEY => true, + WC_Facebookcommerce_Pixel::PIXEL_ID_KEY => '123456789', + WC_Facebookcommerce_Pixel::USE_PII_KEY => 1, + WC_Facebookcommerce_Pixel::USE_S2S_KEY => true, WC_Facebookcommerce_Pixel::ACCESS_TOKEN_KEY => 'abc123', ); - $expected_options = array_merge($default_options, $existing_options); + $expected_options = array_merge( $default_options, $existing_options ); - update_option(WC_Facebookcommerce_Pixel::SETTINGS_KEY, $existing_options); + update_option( WC_Facebookcommerce_Pixel::SETTINGS_KEY, $existing_options ); $actual_options = WC_Facebookcommerce_Pixel::get_options(); - $this->assertEquals($expected_options, $actual_options); + $this->assertEquals( $expected_options, $actual_options ); } public function test_get_options_returns_default_options_when_options_are_not_an_array() { - update_option(WC_Facebookcommerce_Pixel::SETTINGS_KEY, 'not an array'); + update_option( WC_Facebookcommerce_Pixel::SETTINGS_KEY, 'not an array' ); $expected_options = array( - WC_Facebookcommerce_Pixel::PIXEL_ID_KEY => '0', - WC_Facebookcommerce_Pixel::USE_PII_KEY => 0, - WC_Facebookcommerce_Pixel::USE_S2S_KEY => false, + WC_Facebookcommerce_Pixel::PIXEL_ID_KEY => '0', + WC_Facebookcommerce_Pixel::USE_PII_KEY => 0, + WC_Facebookcommerce_Pixel::USE_S2S_KEY => false, WC_Facebookcommerce_Pixel::ACCESS_TOKEN_KEY => '', ); $actual_options = WC_Facebookcommerce_Pixel::get_options(); - $this->assertEquals($expected_options, $actual_options); + $this->assertEquals( $expected_options, $actual_options ); } }