Skip to content
7 changes: 5 additions & 2 deletions tests/Unit/Admin/Settings_Screens/ConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use PHPUnit\Framework\TestCase;
use WC_Facebookcommerce;
use WooCommerce\Facebook\Admin\Settings_Screens\Connection;
use WooCommerce\Facebook\Tests\SafelyUpdateOptionsTestTrait;

/**
* Class ConnectionTest
Expand All @@ -12,6 +13,8 @@
*/
class ConnectionTest extends TestCase {

use SafelyUpdateOptionsTestTrait;

/**
* Helper method to invoke private/protected methods
*
Expand Down Expand Up @@ -143,8 +146,8 @@ public function test_renders_management_url_based_on_merchant_token() {
$connection = new Connection();

// Set up the merchant token
update_option('wc_facebook_merchant_access_token', 'test_token');

$this->set_option_safely_only_for_this_test('wc_facebook_merchant_access_token', 'test_token');
// Use output buffering to capture the iframe HTML
ob_start();
$this->invoke_method($connection, 'render_facebook_iframe');
Expand Down
5 changes: 4 additions & 1 deletion tests/Unit/Admin/Settings_Screens/ShopsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use PHPUnit\Framework\TestCase;
use WooCommerce\Facebook\Admin\Settings_Screens\Shops;
use WooCommerce\Facebook\Tests\SafelyUpdateOptionsTestTrait;

/**
* Class ShopsTest
Expand All @@ -11,6 +12,8 @@
*/
class ShopsTest extends TestCase {

use SafelyUpdateOptionsTestTrait;

/** @var Shops */
private $shops;

Expand Down Expand Up @@ -115,7 +118,7 @@ public function test_renders_management_url_based_on_merchant_token() {
->getMock();

// Set up the merchant token
update_option('wc_facebook_merchant_access_token', 'test_token');
$this->set_option_safely_only_for_this_test('wc_facebook_merchant_access_token', 'test_token');

// Start output buffering to capture the render output
ob_start();
Expand Down
27 changes: 15 additions & 12 deletions tests/Unit/Api/REST/RestAPITest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@
use WooCommerce\Facebook\API\Plugin\Settings\Handler;
use WooCommerce\Facebook\API\Plugin\Settings\Update\Request as UpdateRequest;
use PHPUnit\Framework\TestCase;
use WooCommerce\Facebook\Tests\SafelyUpdateOptionsTestTrait;

/**
* The REST API unit test class.
*/
class RestAPITest extends TestCase {

use SafelyUpdateOptionsTestTrait;

/**
* Test REST API routes are registered
*/
Expand All @@ -37,25 +40,21 @@ public function test_settings_update_succeeds_with_valid_data() {
}

// Mock the update_option function
if (!function_exists('update_option')) {
if (!function_exists('\Tests\Api\REST\update_option')) {
function update_option($option, $value) {
global $wp_options;
if (!isset($wp_options)) {
$wp_options = [];
}
$wp_options[$option] = $value;
global $test_instance;
$test_instance->set_option_safely_only_for_this_test($option, $value);
return true;
}
}

// Mock the get_option function
if (!function_exists('get_option')) {
if (!function_exists('\Tests\Api\REST\get_option')) {
function get_option($option, $default = false) {
global $wp_options;
if (!isset($wp_options)) {
$wp_options = [];
}
return isset($wp_options[$option]) ? $wp_options[$option] : $default;
// We can't directly access the original values here easily,
// so we'll rely on the trait's teardown to restore state.
// For the test logic, we return the WP option directly.
return \get_option($option, $default);
}
}

Expand All @@ -66,6 +65,10 @@ function wc_bool_to_string($bool) {
}
}

// Set the global test instance for the mocked update_option
global $test_instance;
$test_instance = $this;

// Create a handler instance
$handler = new Handler();

Expand Down
15 changes: 9 additions & 6 deletions tests/Unit/ExternalVersionUpdate/UpdateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,15 @@
use ReflectionObject;
use WC_Facebookcommerce_Utils;
use WP_Error;
use WooCommerce\Facebook\Tests\SafelyUpdateOptionsTestTrait;

/**
* The External version update unit test class.
*/
class UpdateTest extends WP_UnitTestCase {

use SafelyUpdateOptionsTestTrait;

/**
* Instance of the Update class that we are testing.
*
Expand Down Expand Up @@ -107,7 +110,7 @@ public function test_should_update_version() {
->getMock();
$mock_connection_handler->expects( $this->any() )->method( 'is_connected' )->willReturn( false );
$prop_connection_handler->setValue( $plugin, $mock_connection_handler );
update_option( 'facebook_for_woocommerce_latest_version_sent_to_server', '0.0.0' ); // Reset the option.
$this->set_option_safely_only_for_this_test( 'facebook_for_woocommerce_latest_version_sent_to_server', '0.0.0' ); // Reset the option.
$should_update2 = $this->update->should_update_version();
$this->assertFalse( $should_update2 );

Expand All @@ -118,11 +121,11 @@ public function test_should_update_version() {
->getMock();
$mock_connection_handler->expects( $this->any() )->method( 'is_connected' )->willReturn( true );
$prop_connection_handler->setValue( $plugin, $mock_connection_handler );
update_option( 'facebook_for_woocommerce_latest_version_sent_to_server', WC_Facebookcommerce_Utils::PLUGIN_VERSION );
$this->set_option_safely_only_for_this_test( 'facebook_for_woocommerce_latest_version_sent_to_server', WC_Facebookcommerce_Utils::PLUGIN_VERSION );
$should_update3 = $this->update->should_update_version();
$this->assertTrue( $should_update3 ); // Because the versions match.

update_option( 'facebook_for_woocommerce_latest_version_sent_to_server', '0.0.0' ); // Reset the option.
$this->set_option_safely_only_for_this_test( 'facebook_for_woocommerce_latest_version_sent_to_server', '0.0.0' ); // Reset the option.
$should_update4 = $this->update->should_update_version();
$this->assertTrue( $should_update4 );
}
Expand Down Expand Up @@ -198,7 +201,7 @@ public function test_maybe_update_external_plugin_version() {
$prop_api->setValue( $plugin, $mock_api2 );

// Assert handling failed API response.
update_option( 'facebook_for_woocommerce_latest_version_sent_to_server', '0.0.0' ); // Reset the version to pass the should_update_version check.
$this->set_option_safely_only_for_this_test( 'facebook_for_woocommerce_latest_version_sent_to_server', '0.0.0' ); // Reset the version to pass the should_update_version check.
$updated3 = $this->update->maybe_update_external_plugin_version();
$this->assertFalse( $updated3 ); // API failed response is handled.
$this->assertNotEquals( WC_Facebookcommerce_Utils::PLUGIN_VERSION, get_option( 'facebook_for_woocommerce_latest_version_sent_to_server' ) ); // API failed response should not update the option.
Expand All @@ -209,7 +212,7 @@ public function test_maybe_update_external_plugin_version() {
$prop_api->setValue( $plugin, $mock_api3 );

// Assert PluginException Handling.
update_option( 'facebook_for_woocommerce_latest_version_sent_to_server', '0.0.0' ); // Reset the version to pass the should_update_version check.
$this->set_option_safely_only_for_this_test( 'facebook_for_woocommerce_latest_version_sent_to_server', '0.0.0' ); // Reset the version to pass the should_update_version check.
$updated4 = $this->update->maybe_update_external_plugin_version();
$this->assertFalse( $updated4 ); // API failed response is handled.
$this->assertNotEquals( WC_Facebookcommerce_Utils::PLUGIN_VERSION, get_option( 'facebook_for_woocommerce_latest_version_sent_to_server' ) ); // API failed response should not update the option.
Expand All @@ -220,7 +223,7 @@ public function test_maybe_update_external_plugin_version() {
$prop_api->setValue( $plugin, $mock_api4 );

// Assert ApiException Handling.
update_option( 'facebook_for_woocommerce_latest_version_sent_to_server', '0.0.0' ); // Reset the version to pass the should_update_version check.
$this->set_option_safely_only_for_this_test( 'facebook_for_woocommerce_latest_version_sent_to_server', '0.0.0' ); // Reset the version to pass the should_update_version check.
$updated5 = $this->update->maybe_update_external_plugin_version();
$this->assertFalse( $updated5 ); // API failed response is handled.
$this->assertNotEquals( WC_Facebookcommerce_Utils::PLUGIN_VERSION, get_option( 'facebook_for_woocommerce_latest_version_sent_to_server' ) ); // API failed response should not update the option.
Expand Down
11 changes: 9 additions & 2 deletions tests/Unit/FacebookCommercePixelEventTest.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
<?php
declare( strict_types=1 );

use WooCommerce\Facebook\Tests\SafelyUpdateOptionsTestTrait;

class FacebookCommercePixelTest extends WP_UnitTestCase {

// Use the trait to manage options safely during tests.
use SafelyUpdateOptionsTestTrait;

/**
* Unit tests for WC_Facebookcommerce_Pixel class.
*/
Expand Down Expand Up @@ -36,15 +41,17 @@ public function test_get_options_returns_merged_options_when_options_exist() {

$expected_options = array_merge( $default_options, $existing_options );

update_option( WC_Facebookcommerce_Pixel::SETTINGS_KEY, $existing_options );
// Use the trait method to set the option for this test.
$this->set_option_safely_only_for_this_test( 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' );
// Use the trait method to set the option for this test.
$this->set_option_safely_only_for_this_test( WC_Facebookcommerce_Pixel::SETTINGS_KEY, 'not an array' );

$expected_options = array(
WC_Facebookcommerce_Pixel::PIXEL_ID_KEY => '0',
Expand Down
20 changes: 12 additions & 8 deletions tests/Unit/Feed/AbstractFeedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

use WP_UnitTestCase;
use WooCommerce\Facebook\Utilities\Heartbeat;
use WooCommerce\Facebook\Tests\SafelyUpdateOptionsTestTrait;

class TestFeed extends AbstractFeed {
public function __construct(FeedFileWriter $file_writer, AbstractFeedHandler $feed_handler, FeedGenerator $feed_generator) {
Expand Down Expand Up @@ -40,6 +41,9 @@ protected static function get_feed_gen_scheduling_interval(): string {
}

class AbstractFeedTest extends WP_UnitTestCase {

use SafelyUpdateOptionsTestTrait;

/**
* The test feed class.
*
Expand All @@ -57,17 +61,17 @@ public function setUp(): void {
}

public function testShouldSkipFeed() {
update_option( 'wc_facebook_commerce_partner_integration_id', '1841465350002849' );
update_option( 'wc_facebook_commerce_merchant_settings_id', '1352794439398752' );
$this->set_option_safely_only_for_this_test( 'wc_facebook_commerce_partner_integration_id', '1841465350002849' );
$this->set_option_safely_only_for_this_test( 'wc_facebook_commerce_merchant_settings_id', '1352794439398752' );
$this->assertFalse( $this->feed->should_skip_feed(), 'Feed should not be skipped when CPI ID and CMS ID are set.' );
update_option( 'wc_facebook_commerce_partner_integration_id', '' );
update_option( 'wc_facebook_commerce_merchant_settings_id', '1352794439398752' );
$this->set_option_safely_only_for_this_test( 'wc_facebook_commerce_partner_integration_id', '' );
$this->set_option_safely_only_for_this_test( 'wc_facebook_commerce_merchant_settings_id', '1352794439398752' );
$this->assertTrue( $this->feed->should_skip_feed(), 'Feed should be skipped when CPI ID is empty.' );
update_option( 'wc_facebook_commerce_partner_integration_id', '1841465350002849' );
update_option( 'wc_facebook_commerce_merchant_settings_id', '' );
$this->set_option_safely_only_for_this_test( 'wc_facebook_commerce_partner_integration_id', '1841465350002849' );
$this->set_option_safely_only_for_this_test( 'wc_facebook_commerce_merchant_settings_id', '' );
$this->assertTrue( $this->feed->should_skip_feed(), 'Feed should be skipped when CMS ID is empty.' );
update_option( 'wc_facebook_commerce_partner_integration_id', '' );
update_option( 'wc_facebook_commerce_merchant_settings_id', '' );
$this->set_option_safely_only_for_this_test( 'wc_facebook_commerce_partner_integration_id', '' );
$this->set_option_safely_only_for_this_test( 'wc_facebook_commerce_merchant_settings_id', '' );
$this->assertTrue( $this->feed->should_skip_feed(), 'Feed should be skipped when both CPI ID and CMS ID are empty.' );
}

Expand Down
16 changes: 10 additions & 6 deletions tests/Unit/Feed/FeedUploadUtilsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,17 @@

require_once __DIR__ . '/../../../includes/Feed/FeedUploadUtils.php';

use WooCommerce\Facebook\Tests\SafelyUpdateOptionsTestTrait;

/**
* Class FeedUploadUtilsTest
*
* Sets up environment to test various logic in FeedUploadUtils
*/
class FeedUploadUtilsTest extends \WooCommerce\Facebook\Tests\AbstractWPUnitTestWithSafeFiltering {

use SafelyUpdateOptionsTestTrait;

/** @var int Shop page ID */
protected static $shop_page_id;

Expand All @@ -31,7 +35,7 @@ public function setUp(): void {
return '/%postname%/';
});

update_option( 'permalink_structure', '/%postname%/' );
$this->set_option_safely_only_for_this_test( 'permalink_structure', '/%postname%/' );
global $wp_rewrite;
if ( ! ( $wp_rewrite instanceof WP_Rewrite ) ) {
$wp_rewrite = new WP_Rewrite();
Expand All @@ -41,10 +45,10 @@ public function setUp(): void {
flush_rewrite_rules();

// Set basic site options.
update_option( 'blogname', 'Test Store' );
update_option( 'wc_facebook_commerce_merchant_settings_id', '123456789' );
update_option( 'siteurl', 'https://example.com' );
update_option( 'home', 'https://example.com' );
$this->set_option_safely_only_for_this_test( 'blogname', 'Test Store' );
$this->set_option_safely_only_for_this_test( 'wc_facebook_commerce_merchant_settings_id', '123456789' );
$this->set_option_safely_only_for_this_test( 'siteurl', 'https://example.com' );
$this->set_option_safely_only_for_this_test( 'home', 'https://example.com' );

// Create and register the Shop page.
self::$shop_page_id = self::factory()->post->create( [
Expand All @@ -53,7 +57,7 @@ public function setUp(): void {
'post_title' => 'Shop',
'post_name' => 'shop'
] );
update_option( 'woocommerce_shop_page_id', self::$shop_page_id );
$this->set_option_safely_only_for_this_test( 'woocommerce_shop_page_id', self::$shop_page_id );
flush_rewrite_rules();

// Add high–priority filters to force URLs.
Expand Down
5 changes: 4 additions & 1 deletion tests/Unit/Handlers/MetaExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@

use WooCommerce\Facebook\Handlers\MetaExtension;
use WP_UnitTestCase;
use WooCommerce\Facebook\Tests\SafelyUpdateOptionsTestTrait;

/**
* The Meta Extension unit test class.
*/
class MetaExtensionTest extends \WP_UnitTestCase {

use SafelyUpdateOptionsTestTrait;

/**
* Instance of the MetaExtension class that we are testing.
*
Expand Down Expand Up @@ -48,7 +51,7 @@ public function test_generate_iframe_splash_url() {
* Test generate_iframe_management_url
*/
public function test_generate_iframe_management_url() {
update_option( 'wc_facebook_access_token', 'test_merchant_token' );
$this->set_option_safely_only_for_this_test( 'wc_facebook_access_token', 'test_merchant_token' );

// Test with empty business ID (should return empty string)
$url = MetaExtension::generate_iframe_management_url('');
Expand Down
Loading