Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make get_options a bit more robust. #2603

Merged
merged 2 commits into from
Aug 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 21 additions & 9 deletions facebook-commerce-pixel-event.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
61 changes: 61 additions & 0 deletions tests/Unit/FacebookCommercePixelEventTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php
declare( strict_types=1 );

class FacebookCommercePixelTest extends WP_UnitTestCase {

/**
* Unit tests for WC_Facebookcommerce_Pixel class.
*/
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::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 );
}

}