Fix #25900 - image size customisation controls not shown

- Added the `ThemeSupport class`, with methods to add and get
  theme support options.
  - It also has a new `add_default_options` method that adds the
    options under a `_defaults` key.
- The `WC_Twenty_*` classes now use `ThemeSupport` instead of
  the `add_theme_support` function to define image and thumbnail sizes.
  - The values are defined as default options.
- The `WC_Shop_Customizer` class now uses `ThemeSupport` instead of
  `wc_get_theme_support` to check if image and thumbnail sizes UI
  should be rendered.
  - The check is made excluding default values.

With these changes the UI to change the image and thumbnail sizes
is hidden only if the options are added as non-defaults elsewhere.

Additional changes:

- The code of the `wc_get_theme_support` function is replaced with
  a simple call to `get_option` in `ThemeSupport`.
- Added the utility class `ArrayUtil`.
This commit is contained in:
Nestor Soriano 2020-11-04 12:56:42 +01:00
parent cd443581cf
commit d620f1d232
20 changed files with 631 additions and 58 deletions

View File

@ -6,6 +6,8 @@
* @package WooCommerce * @package WooCommerce
*/ */
use Automattic\WooCommerce\ThemeManagement\ThemeSupport;
defined( 'ABSPATH' ) || exit; defined( 'ABSPATH' ) || exit;
/** /**
@ -13,10 +15,19 @@ defined( 'ABSPATH' ) || exit;
*/ */
class WC_Shop_Customizer { class WC_Shop_Customizer {
/**
* Holds the instance of ThemeSupport to use.
*
* @var ThemeSupport $theme_support The instance of ThemeSupport to use.
*/
private $theme_support;
/** /**
* Constructor. * Constructor.
*/ */
public function __construct() { public function __construct() {
$this->theme_support = wc_get_container()->get( ThemeSupport::class );
add_action( 'customize_register', array( $this, 'add_sections' ) ); add_action( 'customize_register', array( $this, 'add_sections' ) );
add_action( 'customize_controls_print_styles', array( $this, 'add_styles' ) ); add_action( 'customize_controls_print_styles', array( $this, 'add_styles' ) );
add_action( 'customize_controls_print_scripts', array( $this, 'add_scripts' ), 30 ); add_action( 'customize_controls_print_scripts', array( $this, 'add_scripts' ), 30 );
@ -545,11 +556,11 @@ class WC_Shop_Customizer {
) )
); );
if ( ! wc_get_theme_support( 'single_image_width' ) ) { if ( ! $this->theme_support->has_option( 'single_image_width', false ) ) {
$wp_customize->add_setting( $wp_customize->add_setting(
'woocommerce_single_image_width', 'woocommerce_single_image_width',
array( array(
'default' => 600, 'default' => $this->theme_support->get_option( 'single_image_width', 600 ),
'type' => 'option', 'type' => 'option',
'capability' => 'manage_woocommerce', 'capability' => 'manage_woocommerce',
'sanitize_callback' => 'absint', 'sanitize_callback' => 'absint',
@ -573,11 +584,11 @@ class WC_Shop_Customizer {
); );
} }
if ( ! wc_get_theme_support( 'thumbnail_image_width' ) ) { if ( ! $this->theme_support->has_option( 'thumbnail_image_width', false ) ) {
$wp_customize->add_setting( $wp_customize->add_setting(
'woocommerce_thumbnail_image_width', 'woocommerce_thumbnail_image_width',
array( array(
'default' => 300, 'default' => $this->theme_support->get_option( 'thumbnail_image_width', 300 ),
'type' => 'option', 'type' => 'option',
'capability' => 'manage_woocommerce', 'capability' => 'manage_woocommerce',
'sanitize_callback' => 'absint', 'sanitize_callback' => 'absint',

View File

@ -6,6 +6,8 @@
* @package WooCommerce\Classes * @package WooCommerce\Classes
*/ */
use Automattic\WooCommerce\ThemeManagement\ThemeSupport;
defined( 'ABSPATH' ) || exit; defined( 'ABSPATH' ) || exit;
/** /**
@ -29,8 +31,7 @@ class WC_Twenty_Eleven {
add_theme_support( 'wc-product-gallery-zoom' ); add_theme_support( 'wc-product-gallery-zoom' );
add_theme_support( 'wc-product-gallery-lightbox' ); add_theme_support( 'wc-product-gallery-lightbox' );
add_theme_support( 'wc-product-gallery-slider' ); add_theme_support( 'wc-product-gallery-slider' );
add_theme_support( wc_get_container()->get( ThemeSupport::class )->add_default_options(
'woocommerce',
array( array(
'thumbnail_image_width' => 150, 'thumbnail_image_width' => 150,
'single_image_width' => 300, 'single_image_width' => 300,

View File

@ -7,6 +7,8 @@
* @package WooCommerce\Classes * @package WooCommerce\Classes
*/ */
use Automattic\WooCommerce\ThemeManagement\ThemeSupport;
defined( 'ABSPATH' ) || exit; defined( 'ABSPATH' ) || exit;
/** /**
@ -30,8 +32,7 @@ class WC_Twenty_Fifteen {
add_theme_support( 'wc-product-gallery-zoom' ); add_theme_support( 'wc-product-gallery-zoom' );
add_theme_support( 'wc-product-gallery-lightbox' ); add_theme_support( 'wc-product-gallery-lightbox' );
add_theme_support( 'wc-product-gallery-slider' ); add_theme_support( 'wc-product-gallery-slider' );
add_theme_support( wc_get_container()->get( ThemeSupport::class )->add_default_options(
'woocommerce',
array( array(
'thumbnail_image_width' => 200, 'thumbnail_image_width' => 200,
'single_image_width' => 350, 'single_image_width' => 350,

View File

@ -7,6 +7,8 @@
* @package WooCommerce\Classes * @package WooCommerce\Classes
*/ */
use Automattic\WooCommerce\ThemeManagement\ThemeSupport;
defined( 'ABSPATH' ) || exit; defined( 'ABSPATH' ) || exit;
/** /**
@ -30,8 +32,7 @@ class WC_Twenty_Fourteen {
add_theme_support( 'wc-product-gallery-zoom' ); add_theme_support( 'wc-product-gallery-zoom' );
add_theme_support( 'wc-product-gallery-lightbox' ); add_theme_support( 'wc-product-gallery-lightbox' );
add_theme_support( 'wc-product-gallery-slider' ); add_theme_support( 'wc-product-gallery-slider' );
add_theme_support( wc_get_container()->get( ThemeSupport::class )->add_default_options(
'woocommerce',
array( array(
'thumbnail_image_width' => 150, 'thumbnail_image_width' => 150,
'single_image_width' => 300, 'single_image_width' => 300,

View File

@ -7,6 +7,7 @@
*/ */
use Automattic\Jetpack\Constants; use Automattic\Jetpack\Constants;
use Automattic\WooCommerce\ThemeManagement\ThemeSupport;
defined( 'ABSPATH' ) || exit; defined( 'ABSPATH' ) || exit;
@ -37,8 +38,7 @@ class WC_Twenty_Nineteen {
add_theme_support( 'wc-product-gallery-zoom' ); add_theme_support( 'wc-product-gallery-zoom' );
add_theme_support( 'wc-product-gallery-lightbox' ); add_theme_support( 'wc-product-gallery-lightbox' );
add_theme_support( 'wc-product-gallery-slider' ); add_theme_support( 'wc-product-gallery-slider' );
add_theme_support( wc_get_container()->get( ThemeSupport::class )->add_default_options(
'woocommerce',
array( array(
'thumbnail_image_width' => 300, 'thumbnail_image_width' => 300,
'single_image_width' => 450, 'single_image_width' => 450,

View File

@ -7,6 +7,7 @@
*/ */
use Automattic\Jetpack\Constants; use Automattic\Jetpack\Constants;
use Automattic\WooCommerce\ThemeManagement\ThemeSupport;
defined( 'ABSPATH' ) || exit; defined( 'ABSPATH' ) || exit;
@ -30,8 +31,7 @@ class WC_Twenty_Seventeen {
add_theme_support( 'wc-product-gallery-zoom' ); add_theme_support( 'wc-product-gallery-zoom' );
add_theme_support( 'wc-product-gallery-lightbox' ); add_theme_support( 'wc-product-gallery-lightbox' );
add_theme_support( 'wc-product-gallery-slider' ); add_theme_support( 'wc-product-gallery-slider' );
add_theme_support( wc_get_container()->get( ThemeSupport::class )->add_default_options(
'woocommerce',
array( array(
'thumbnail_image_width' => 250, 'thumbnail_image_width' => 250,
'single_image_width' => 350, 'single_image_width' => 350,

View File

@ -6,6 +6,8 @@
* @package WooCommerce\Classes * @package WooCommerce\Classes
*/ */
use Automattic\WooCommerce\ThemeManagement\ThemeSupport;
defined( 'ABSPATH' ) || exit; defined( 'ABSPATH' ) || exit;
/** /**
@ -29,8 +31,7 @@ class WC_Twenty_Sixteen {
add_theme_support( 'wc-product-gallery-zoom' ); add_theme_support( 'wc-product-gallery-zoom' );
add_theme_support( 'wc-product-gallery-lightbox' ); add_theme_support( 'wc-product-gallery-lightbox' );
add_theme_support( 'wc-product-gallery-slider' ); add_theme_support( 'wc-product-gallery-slider' );
add_theme_support( wc_get_container()->get( ThemeSupport::class )->add_default_options(
'woocommerce',
array( array(
'thumbnail_image_width' => 250, 'thumbnail_image_width' => 250,
'single_image_width' => 400, 'single_image_width' => 400,

View File

@ -6,6 +6,8 @@
* @package WooCommerce\Classes * @package WooCommerce\Classes
*/ */
use Automattic\WooCommerce\ThemeManagement\ThemeSupport;
defined( 'ABSPATH' ) || exit; defined( 'ABSPATH' ) || exit;
/** /**
@ -29,8 +31,7 @@ class WC_Twenty_Ten {
add_theme_support( 'wc-product-gallery-zoom' ); add_theme_support( 'wc-product-gallery-zoom' );
add_theme_support( 'wc-product-gallery-lightbox' ); add_theme_support( 'wc-product-gallery-lightbox' );
add_theme_support( 'wc-product-gallery-slider' ); add_theme_support( 'wc-product-gallery-slider' );
add_theme_support( wc_get_container()->get( ThemeSupport::class )->add_default_options(
'woocommerce',
array( array(
'thumbnail_image_width' => 200, 'thumbnail_image_width' => 200,
'single_image_width' => 300, 'single_image_width' => 300,

View File

@ -7,6 +7,8 @@
* @package WooCommerce\Classes * @package WooCommerce\Classes
*/ */
use Automattic\WooCommerce\ThemeManagement\ThemeSupport;
defined( 'ABSPATH' ) || exit; defined( 'ABSPATH' ) || exit;
/** /**
@ -30,8 +32,7 @@ class WC_Twenty_Thirteen {
add_theme_support( 'wc-product-gallery-zoom' ); add_theme_support( 'wc-product-gallery-zoom' );
add_theme_support( 'wc-product-gallery-lightbox' ); add_theme_support( 'wc-product-gallery-lightbox' );
add_theme_support( 'wc-product-gallery-slider' ); add_theme_support( 'wc-product-gallery-slider' );
add_theme_support( wc_get_container()->get( ThemeSupport::class )->add_default_options(
'woocommerce',
array( array(
'thumbnail_image_width' => 200, 'thumbnail_image_width' => 200,
'single_image_width' => 300, 'single_image_width' => 300,

View File

@ -7,6 +7,8 @@
* @package WooCommerce\Classes * @package WooCommerce\Classes
*/ */
use Automattic\WooCommerce\ThemeManagement\ThemeSupport;
defined( 'ABSPATH' ) || exit; defined( 'ABSPATH' ) || exit;
/** /**
@ -30,8 +32,7 @@ class WC_Twenty_Twelve {
add_theme_support( 'wc-product-gallery-zoom' ); add_theme_support( 'wc-product-gallery-zoom' );
add_theme_support( 'wc-product-gallery-lightbox' ); add_theme_support( 'wc-product-gallery-lightbox' );
add_theme_support( 'wc-product-gallery-slider' ); add_theme_support( 'wc-product-gallery-slider' );
add_theme_support( wc_get_container()->get( ThemeSupport::class )->add_default_options(
'woocommerce',
array( array(
'thumbnail_image_width' => 200, 'thumbnail_image_width' => 200,
'single_image_width' => 300, 'single_image_width' => 300,

View File

@ -7,6 +7,7 @@
*/ */
use Automattic\Jetpack\Constants; use Automattic\Jetpack\Constants;
use Automattic\WooCommerce\ThemeManagement\ThemeSupport;
defined( 'ABSPATH' ) || exit; defined( 'ABSPATH' ) || exit;
@ -37,8 +38,7 @@ class WC_Twenty_Twenty {
add_theme_support( 'wc-product-gallery-zoom' ); add_theme_support( 'wc-product-gallery-zoom' );
add_theme_support( 'wc-product-gallery-lightbox' ); add_theme_support( 'wc-product-gallery-lightbox' );
add_theme_support( 'wc-product-gallery-slider' ); add_theme_support( 'wc-product-gallery-slider' );
add_theme_support( wc_get_container()->get( ThemeSupport::class )->add_default_options(
'woocommerce',
array( array(
'thumbnail_image_width' => 450, 'thumbnail_image_width' => 450,
'single_image_width' => 600, 'single_image_width' => 600,

View File

@ -9,6 +9,7 @@
*/ */
use Automattic\Jetpack\Constants; use Automattic\Jetpack\Constants;
use Automattic\WooCommerce\ThemeManagement\ThemeSupport;
use Automattic\WooCommerce\Utilities\NumberUtil; use Automattic\WooCommerce\Utilities\NumberUtil;
if ( ! defined( 'ABSPATH' ) ) { if ( ! defined( 'ABSPATH' ) ) {
@ -879,38 +880,7 @@ function wc_mail( $to, $subject, $message, $headers = "Content-Type: text/html\r
* @return mixed Value of prop(s). * @return mixed Value of prop(s).
*/ */
function wc_get_theme_support( $prop = '', $default = null ) { function wc_get_theme_support( $prop = '', $default = null ) {
$theme_support = get_theme_support( 'woocommerce' ); return wc_get_container()->get( ThemeSupport::class )->get_option( $prop, $default );
$theme_support = is_array( $theme_support ) ? $theme_support[0] : false;
if ( ! $theme_support ) {
return $default;
}
if ( $prop ) {
$prop_stack = explode( '::', $prop );
$prop_key = array_shift( $prop_stack );
if ( isset( $theme_support[ $prop_key ] ) ) {
$value = $theme_support[ $prop_key ];
if ( count( $prop_stack ) ) {
foreach ( $prop_stack as $prop_key ) {
if ( is_array( $value ) && isset( $value[ $prop_key ] ) ) {
$value = $value[ $prop_key ];
} else {
$value = $default;
break;
}
}
}
} else {
$value = $default;
}
return $value;
}
return $theme_support;
} }
/** /**

View File

@ -6,12 +6,13 @@
namespace Automattic\WooCommerce; namespace Automattic\WooCommerce;
use Automattic\WooCommerce\Internal\DependencyManagement\ServiceProviders\ProxiesServiceProvider; use Automattic\WooCommerce\Internal\DependencyManagement\ServiceProviders\ProxiesServiceProvider;
use Automattic\WooCommerce\Internal\DependencyManagement\ServiceProviders\ThemeManagementServiceProvider;
use Automattic\WooCommerce\Internal\DependencyManagement\ExtendedContainer; use Automattic\WooCommerce\Internal\DependencyManagement\ExtendedContainer;
/** /**
* PSR11 compliant dependency injection container for WooCommerce. * PSR11 compliant dependency injection container for WooCommerce.
* *
* Classes in the `src` directory should specify dependencies from that directory via constructor arguments * Classes in the `src` directory should specify dependencies from that directory via an 'init' method having arguments
* with type hints. If an instance of the container itself is needed, the type hint to use is \Psr\Container\ContainerInterface. * with type hints. If an instance of the container itself is needed, the type hint to use is \Psr\Container\ContainerInterface.
* *
* Classes in the `src` directory should interact with anything outside (especially code in the `includes` directory * Classes in the `src` directory should interact with anything outside (especially code in the `includes` directory
@ -33,6 +34,7 @@ final class Container implements \Psr\Container\ContainerInterface {
*/ */
private $service_providers = array( private $service_providers = array(
ProxiesServiceProvider::class, ProxiesServiceProvider::class,
ThemeManagementServiceProvider::class,
); );
/** /**

View File

@ -0,0 +1,31 @@
<?php
/**
* ThemeManagementServiceProvider class file.
*/
namespace Automattic\WooCommerce\Internal\DependencyManagement\ServiceProviders;
use Automattic\WooCommerce\ThemeManagement\ThemeSupport;
use Automattic\WooCommerce\Internal\DependencyManagement\AbstractServiceProvider;
/**
* Service provider for the classes in the Automattic\WooCommerce\ThemeManagement namespace.
*/
class ThemeManagementServiceProvider extends AbstractServiceProvider {
/**
* The classes/interfaces that are serviced by this service provider.
*
* @var array
*/
protected $provides = array(
ThemeSupport::class,
);
/**
* Register the classes.
*/
public function register() {
$this->share_with_auto_arguments( ThemeSupport::class );
}
}

View File

@ -0,0 +1,118 @@
<?php
/**
* ThemeSupport class file.
*
* @package Automattic\WooCommerce\ThemeManagement
*/
namespace Automattic\WooCommerce\ThemeManagement;
use Automattic\WooCommerce\Proxies\LegacyProxy;
use Automattic\WooCommerce\Utilities\ArrayUtil;
/**
* Provides methods for theme support.
*/
class ThemeSupport {
const DEFAULTS_KEY = '_defaults';
/**
* The instance of LegacyProxy to use.
*
* @var LegacyProxy
*/
private $legacy_proxy;
/**
* ThemeSupport constructor.
*
* @internal
* @param LegacyProxy $legacy_proxy The instance of LegacyProxy to use.
*/
final public function init( LegacyProxy $legacy_proxy ) {
$this->legacy_proxy = $legacy_proxy;
}
/**
* Adds theme support options for the current theme.
*
* @param array $options The options to be added.
*/
public function add_options( $options ) {
$this->legacy_proxy->call_function( 'add_theme_support', 'woocommerce', $options );
}
/**
* Adds default theme support options for the current theme.
*
* @param array $options The options to be added.
*/
public function add_default_options( $options ) {
$default_options = $this->get_option( self::DEFAULTS_KEY, array() );
$default_options = array_merge( $default_options, $options );
$this->add_options( array( self::DEFAULTS_KEY => $default_options ) );
}
/**
* Gets "theme support" options from the current theme, if set.
*
* @param string $option_name Option name, possibly nested (key::subkey), to get specific value. Blank to get all the existing options as an array.
* @param mixed $default_value Value to return if the specified option doesn't exist.
* @return mixed The retrieved option or the default value.
*/
public function get_option( $option_name = '', $default_value = null ) {
$theme_support_options = $this->get_all_options();
if ( ! $theme_support_options ) {
return $default_value;
}
if ( $option_name ) {
$value = ArrayUtil::get_nested_value( $theme_support_options, $option_name );
if ( is_null( $value ) ) {
$value = ArrayUtil::get_nested_value( $theme_support_options, self::DEFAULTS_KEY . '::' . $option_name, $default_value );
}
return $value;
}
return $theme_support_options;
}
/**
* Checks whether a given theme support option has been defined.
*
* @param string $option_name The (possibly nested) name of the option to check.
* @param bool $include_defaults True to include the default values in the check, false otherwise.
*
* @return bool True if the specified theme support option has been defined, false otherwise.
*/
public function has_option( $option_name, $include_defaults = true ) {
$theme_support_options = $this->get_all_options();
if ( ! $theme_support_options ) {
return false;
}
$value = ArrayUtil::get_nested_value( $theme_support_options, $option_name );
if ( ! is_null( $value ) ) {
return true;
}
if ( ! $include_defaults ) {
return false;
}
$value = ArrayUtil::get_nested_value( $theme_support_options, self::DEFAULTS_KEY . '::' . $option_name );
return ! is_null( $value );
}
/**
* Get all the defined theme support options for the 'woocommerce' feature.
*
* @return array An array with all the theme support options defined for the 'woocommerce' feature, or false if nothing has been defined for that feature.
*/
private function get_all_options() {
$theme_support = $this->legacy_proxy->call_function( 'get_theme_support', 'woocommerce' );
return is_array( $theme_support ) ? $theme_support[0] : false;
}
}

View File

@ -0,0 +1,48 @@
<?php
/**
* A class of utilities for dealing with arrays.
*/
namespace Automattic\WooCommerce\Utilities;
/**
* A class of utilities for dealing with arrays.
*/
class ArrayUtil {
/**
* Get a value from an nested array by specifying the entire key hierarchy with '::' as separator.
*
* E.g. for [ 'foo' => [ 'bar' => [ 'fizz' => 'buzz' ] ] ] the value for key 'foo::bar::fizz' would be 'buzz'.
*
* @param array $array The array to get the value from.
* @param string $key The complete key hierarchy, using '::' as separator.
* @param mixed $default The value to return if the key doesn't exist in the array.
*
* @return mixed The retrieved value, or the supplied default value.
* @throws \Exception $array is not an array.
*/
public static function get_nested_value( array $array, string $key, $default = null ) {
$key_stack = explode( '::', $key );
$subkey = array_shift( $key_stack );
if ( isset( $array[ $subkey ] ) ) {
$value = $array[ $subkey ];
if ( count( $key_stack ) ) {
foreach ( $key_stack as $subkey ) {
if ( is_array( $value ) && isset( $value[ $subkey ] ) ) {
$value = $value[ $subkey ];
} else {
$value = $default;
break;
}
}
}
} else {
$value = $default;
}
return $value;
}
}

View File

@ -103,7 +103,14 @@ class WC_Unit_Tests_Bootstrap {
* @throws Exception Error when initializing one of the hacks. * @throws Exception Error when initializing one of the hacks.
*/ */
private function initialize_code_hacker() { private function initialize_code_hacker() {
CodeHacker::initialize( array( __DIR__ . '/../../includes/' ) ); $wp_dir = getenv( 'WP_TESTS_WP_DIR' ) ? getenv( 'WP_TESTS_WP_DIR' ) : sys_get_temp_dir() . '/wordpress';
CodeHacker::initialize(
array(
$this->plugin_dir . '/includes/',
$wp_dir . '/wp-includes/class-wp-customize-manager.php',
)
);
$replaceable_functions = include_once __DIR__ . '/mockable-functions.php'; $replaceable_functions = include_once __DIR__ . '/mockable-functions.php';
if ( ! empty( $replaceable_functions ) ) { if ( ! empty( $replaceable_functions ) ) {
FunctionsMockerHack::initialize( $replaceable_functions ); FunctionsMockerHack::initialize( $replaceable_functions );

View File

@ -0,0 +1,59 @@
<?php
/**
* Tests for WC_Shop_Customizer
*/
use Automattic\WooCommerce\ThemeManagement\ThemeSupport;
require_once ABSPATH . WPINC . '/class-wp-customize-manager.php';
require_once ABSPATH . WPINC . '/class-wp-customize-control.php';
/**
* Tests for WC_Shop_Customizer
*/
class WC_Shop_Customizer_Test extends WC_Unit_Test_Case {
/**
* Runs before each test of the class.
*/
public function setUp() {
remove_theme_support( 'woocommerce' );
}
/**
* @testdox add_sections should add controls for image size settings only if these sizes haven't been explicitly declared as theme support (not counting default declarations).
*
* @testWith ["single_image_width", true, false]
* ["single_image_width", false, true]
* ["thumbnail_image_width", true, false]
* ["thumbnail_image_width", false, true]
*
* @param string $option_name The option name to test, either 'single_image_width' or 'thumbnail_image_width'.
* @param bool $add_explicit_theme_support True to test when theme support is added explicitly (not as a default value).
* @param bool $expected_to_have_added_customization True to expect the customization to have been added, false to expect it no to have been added.
*/
public function test_add_sections_should_add_image_controls_only_if_no_theme_support_for_image_sizes_is_defined( $option_name, $add_explicit_theme_support, $expected_to_have_added_customization ) {
$customize_manager = $this->createMock( WP_Customize_Manager::class );
$added_settings = array();
$added_controls = array();
$add_setting_callback = function( $id, $args = array() ) use ( &$added_settings ) {
array_push( $added_settings, $id );
};
$add_control_callback = function( $id, $args = array() ) use ( &$added_controls ) {
array_push( $added_controls, $id );
};
$customize_manager->method( 'add_setting' )->will( $this->returnCallback( $add_setting_callback ) );
$customize_manager->method( 'add_control' )->will( $this->returnCallback( $add_control_callback ) );
$theme_support = $this->get_instance_of( ThemeSupport::class );
$add_support_method = $add_explicit_theme_support ? 'add_options' : 'add_default_options';
$theme_support->$add_support_method( array( $option_name => 1234 ) );
$sut = $this->get_legacy_instance_of( WC_Shop_Customizer::class );
$sut->add_sections( $customize_manager );
$this->assertEquals( $expected_to_have_added_customization, in_array( 'woocommerce_' . $option_name, $added_settings, true ) );
$this->assertEquals( $expected_to_have_added_customization, in_array( 'woocommerce_' . $option_name, $added_controls, true ) );
}
}

View File

@ -0,0 +1,254 @@
<?php
/**
* Tests for ThemeSupport
*
* @package Automattic\WooCommerce\Tests\ThemeSupport
*/
namespace Automattic\WooCommerce\Tests\ThemeManagement;
use Automattic\WooCommerce\ThemeManagement\ThemeSupport;
/**
* Tests for ThemeSupport
*/
class ThemeSupportTest extends \WC_Unit_Test_Case {
/**
* The system under test.
*
* @var ThemeSupport
*/
private $sut;
/**
* Runs before each test.
*/
public function setUp() {
$this->sut = $this->get_instance_of( ThemeSupport::class );
remove_theme_support( 'woocommerce' );
}
/**
* @testdox add_options should add the supplied options under the 'woocommerce' feature.
*/
public function test_add_options() {
$actual_added_feature = null;
$actual_added_options = null;
$this->register_legacy_proxy_function_mocks(
array(
'add_theme_support' => function( $feature, ...$args ) use ( &$actual_added_feature, &$actual_added_options ) {
$actual_added_feature = $feature;
$actual_added_options = $args;
},
)
);
$options = array( 'foo' => 'bar' );
$this->sut->add_options( $options );
$this->assertEquals( 'woocommerce', $actual_added_feature );
$this->assertEquals( $options, $actual_added_options[0] );
$this->reset_legacy_proxy_mocks();
$this->sut->add_options( $options );
$actual_retrieved_options = get_theme_support( 'woocommerce' )[0];
$this->assertEquals( $options, $actual_retrieved_options );
}
/**
* @testdox add_default_options should add the supplied options under the 'woocommerce' feature on a '_defaults' key.
*/
public function test_2_add_default_options() {
$actual_added_options = array();
$this->register_legacy_proxy_function_mocks(
array(
'add_theme_support' => function( $feature, ...$args ) use ( &$actual_added_options ) {
array_push( $actual_added_options, $args );
},
)
);
$this->sut->add_default_options( array( 'foo' => 'bar' ) );
$this->sut->add_default_options( array( 'fizz' => 'buzz' ) );
$expected_added_options = array(
array(
array(
ThemeSupport::DEFAULTS_KEY =>
array(
'foo' => 'bar',
),
),
),
array(
array(
ThemeSupport::DEFAULTS_KEY =>
array(
'fizz' => 'buzz',
),
),
),
);
$this->assertEquals( $expected_added_options, $actual_added_options );
$this->reset_legacy_proxy_mocks();
$this->sut->add_default_options( array( 'foo' => 'bar' ) );
$this->sut->add_default_options( array( 'fizz' => 'buzz' ) );
$actual_retrieved_options = get_theme_support( 'woocommerce' )[0];
$expected_retrieved_options = array(
ThemeSupport::DEFAULTS_KEY => array(
'foo' => 'bar',
'fizz' => 'buzz',
),
);
$this->assertEquals( $expected_retrieved_options, $actual_retrieved_options );
}
/**
* @testdox add_default_options should add the supplied options under the 'woocommerce' feature on a '_defaults' key.
*/
public function test_add_default_options() {
$this->sut->add_default_options( array( 'foo' => 'bar' ) );
$this->sut->add_default_options( array( 'fizz' => 'buzz' ) );
$actual = get_theme_support( 'woocommerce' )[0];
$expected = array(
ThemeSupport::DEFAULTS_KEY => array(
'foo' => 'bar',
'fizz' => 'buzz',
),
);
$this->assertEquals( $expected, $actual );
}
/**
* @testdox get_option should return all the options under the 'woocommerce' feature when invoked with blank option name.
*/
public function test_get_option_with_no_option_name() {
$options = array( 'foo' => 'bar' );
$this->sut->add_options( $options );
$actual = $this->sut->get_option();
$this->assertEquals( $options, $actual );
}
/**
* @testdox get_option should return null if no 'woocommerce' feature exists and no default value is supplied.
*/
public function test_get_option_with_no_option_name_when_no_options_exist_and_no_default_value_supplied() {
$actual = $this->sut->get_option();
$this->assertNull( $actual );
}
/**
* @testdox get_option should return the supplied default value if no 'woocommerce' feature exists.
*/
public function test_get_option_with_no_option_name_when_no_options_exist_and_default_value_supplied() {
$actual = $this->sut->get_option( '', 'DEFAULT' );
$this->assertEquals( 'DEFAULT', $actual );
}
/**
* @testdox get_theme_support should return the value of the requested option if it exists.
*/
public function test_get_theme_support_with_option_name() {
$options = array( 'foo' => array( 'bar' => 'fizz' ) );
$this->sut->add_options( $options );
$actual = $this->sut->get_option( 'foo::bar' );
$this->assertEquals( 'fizz', $actual );
}
/**
* @testdox get_option should return null if the requested option doesn't exist and no default value is supplied.
*/
public function test_get_option_with_option_name_when_option_does_not_exist_and_no_default_value_supplied() {
$options = array( 'foo' => array( 'bar' => 'fizz' ) );
$this->sut->add_options( $options );
$actual = $this->sut->get_option( 'buzz' );
$this->assertNull( $actual );
}
/**
* @testdox get_option should return the supplied default value if the requested option doesn't exist.
*/
public function test_get_option_with_option_name_when_option_does_not_exist_and_default_value_supplied() {
$options = array( 'foo' => array( 'bar' => 'fizz' ) );
$this->sut->add_options( $options );
$actual = $this->sut->get_option( 'buzz', 'DEFAULT' );
$this->assertEquals( 'DEFAULT', $actual );
}
/**
* @testdox get_option should return the value of the requested option if it has been defined as a default.
*/
public function test_get_option_with_option_name_and_option_defined_as_default() {
$options = array( 'foo' => array( 'bar' => 'fizz' ) );
$this->sut->add_default_options( $options );
$actual = $this->sut->get_option( 'foo::bar' );
$this->assertEquals( 'fizz', $actual );
}
/**
* @testdox has_option should return false if no 'woocommerce' feature exists.
*
* @testWith [true]
* [false]
*
* @param bool $include_defaults Whether to include defaults in the search or not.
*/
public function test_has_option_when_no_woocommerce_feature_is_defined( $include_defaults ) {
$this->assertFalse( $this->sut->has_option( 'foo::bar', $include_defaults ) );
}
/**
* @testdox has_option should return false if the specified option has not been defined.
*
* @testWith [true]
* [false]
*
* @param bool $include_defaults Whether to include defaults in the search or not.
*/
public function test_has_option_when_option_is_not_defined( $include_defaults ) {
$this->sut->add_options( array( 'foo' => 'bar' ) );
$this->assertFalse( $this->sut->has_option( 'fizz::buzz', $include_defaults ) );
}
/**
* @testdox has_option should return true if the specified option has been defined.
*
* @testWith [true]
* [false]
*
* @param bool $include_defaults Whether to include defaults in the search or not.
*/
public function test_has_option_when_option_is_defined( $include_defaults ) {
$this->sut->add_options( array( 'foo' => 'bar' ) );
$this->assertTrue( $this->sut->has_option( 'foo', $include_defaults ) );
}
/**
* @testdox If an option has been defined as a default, has_theme_support should return true if $include_defaults is passed as true, should return false otherwise.
*
* @testWith [true, true]
* [false, false]
*
* @param bool $include_defaults Whether to include defaults in the search or not.
* @param bool $expected_result The expected return value from the tested method.
*/
public function test_has_option_when_option_is_defined_as_default( $include_defaults, $expected_result ) {
$this->sut->add_default_options( array( 'foo' => 'bar' ) );
$this->assertEquals( $expected_result, $this->sut->has_option( 'foo', $include_defaults ) );
}
}

View File

@ -0,0 +1,66 @@
<?php
namespace Automattic\WooCommerce\Tests\Utilities;
use Automattic\WooCommerce\Utilities\ArrayUtil;
/**
* A collection of tests for the array utility class.
*/
class ArrayUtilTest extends \WC_Unit_Test_Case {
/**
* @testdox `get_nested_value` should return null if the requested key doesn't exist and no default value is supplied.
*
* @testWith ["foo"]
* ["foo::bar"]
*
* @param string $key The key to test.
*/
public function test_get_nested_value_returns_null_if_non_existing_key_and_default_not_supplied( $key ) {
$array = array( 'fizz' => 'buzz' );
$actual = ArrayUtil::get_nested_value( $array, $key );
$this->assertNull( $actual );
}
/**
* @testdox `get_nested_value` should return the supplied default value if the requested key doesn't exist.
*
* @testWith ["foo"]
* ["foo::bar"]
*
* @param string $key The key to test.
*/
public function test_get_nested_value_returns_supplied_default_if_non_existing_key( $key ) {
$array = array( 'fizz' => 'buzz' );
$actual = ArrayUtil::get_nested_value( $array, $key, 'DEFAULT' );
$this->assertEquals( 'DEFAULT', $actual );
}
/**
* @testdox `get_nested_value` should return the proper value when a simple key is passed.
*/
public function test_get_nested_value_works_for_simple_keys() {
$array = array( 'foo' => 'bar' );
$actual = ArrayUtil::get_nested_value( $array, 'foo' );
$this->assertEquals( 'bar', $actual );
}
/**
* @testdox `get_nested_value` should return the proper value when a nested key is passed.
*/
public function test_get_nested_value_works_for_nested_keys() {
$array = array(
'foo' => array(
'bar' => array(
'fizz' => 'buzz',
),
),
);
$actual = ArrayUtil::get_nested_value( $array, 'foo::bar::fizz' );
$this->assertEquals( 'buzz', $actual );
}
}