Refactor the settings pages, and add unit tests for them.
This commit fixes some inconsistencies in the settings pages, and
makes all the existing pages extensible by adding new sections
(that was possible in some pages, but not in others). Main changes:
1. Modify the 'get_sections' method so that it invokes a new protected
'get_own_sections' method and then triggers the
'woocommerce_get_sections_' . id filter.
This way the filter is triggered only in the base class
and not in each of the derived classes too.
2. Change the get_settings() method so that it has its signature
changed to get_settings( $current_section = '' )
in the base class and in all the derived class.
Some derived classes were already using this signature, but others
(those not having multiple sections natively) weren't, making then
effectively impossible to define multiple sections for these pages
via filters.
With this change all the section pages act consistently and allow
both adding new settings to the default "General" section
and creating new sections via filters.
3. Change the implementation of 'get_settings' in the base class
so that it searches for a 'get_settings_for_{section_id}_section'
method in the class and executes it, otherwise it executes the new
protected method get_settings_for_section( $current_section ); then
it triggers the 'woocommerce_get_settings_' . id filter.
This makes it easier to separate the code that returns the list
of filters in multiple methods, one per section, instead of using
one big if-else-else... block.
So now instead of overriding get_settings($current_section='') derived
classes need to implement get_settings_for_{$current_section}_section
for each section, or override get_settings_for_section($current_section)
or both. 'get_settings_for_section' returns an empty array by default.
Also, 'woocommerce_get_settings_' . id is triggered in one single
place too.
Other improvements:
* Remove duplicated code from 'output' in 'WC_Settings_Page' children.
Some classes inherited from 'WC_Settings_Page' override the 'output'
method with custom code, which in all cases ended up repeating the code
of the original method as a fallback. These repetitions have been
replaced with 'parent::output()'.
* Fix inconsistencies for 'save' and 'output' in WC_Settings_Tax/Emails
The 'WC_Settings_Tax' and 'WC_Settings_Emails' classes had some
inconsistencies in their 'save' and 'output' methods that prevented the
proper creation new sections and the addition of new settings via the
'woocommerce_get_sections_' and 'woocommerce_get_settings_' filters.
Now they work as expected.
* Deduplicate parts of 'save' in 'WC_Settings_Page' and children.
Two methods have been added to 'WC_Settings_Page' class:
'save_settings_for_current_section' and 'do_update_options_action'.
These are intended to be invoked by derived classes in their 'save'
methods, in order to remove code repetition.
* Add some helper methods to WC_Unit_Test_Case.
Methods added:
- assertOutputsHTML
- assertEqualsHTML
- normalize_html
- capture_output_from
2020-09-16 10:27:05 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Class WC_Settings_Tax_Test file.
|
|
|
|
*
|
|
|
|
* @package WooCommerce\Tests\Settings
|
|
|
|
*/
|
|
|
|
|
|
|
|
use Automattic\WooCommerce\Testing\Tools\CodeHacking\Hacks\FunctionsMockerHack;
|
|
|
|
use Automattic\WooCommerce\Testing\Tools\CodeHacking\Hacks\StaticMockerHack;
|
|
|
|
|
|
|
|
require_once __DIR__ . '/class-wc-settings-unit-test-case.php';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unit tests for the WC_Settings_Tax class.
|
|
|
|
*/
|
|
|
|
class WC_Settings_Tax_Test extends WC_Settings_Unit_Test_Case {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @testDox 'get_sections' returns the predefined sections as well as one section per existing tax class.
|
|
|
|
*/
|
|
|
|
public function test_get_sections_returns_predefined_sections_and_one_section_per_tax_class() {
|
|
|
|
$tax_classes = array( 'tax_class_1', 'tax_class_2' );
|
|
|
|
|
|
|
|
StaticMockerHack::add_method_mocks(
|
|
|
|
array(
|
|
|
|
WC_Tax::class => array(
|
|
|
|
'get_tax_classes' => function() use ( $tax_classes ) {
|
|
|
|
return $tax_classes;
|
|
|
|
},
|
|
|
|
),
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$sut = new WC_Settings_Tax();
|
|
|
|
$sections = $sut->get_sections();
|
|
|
|
|
|
|
|
$expected = array(
|
|
|
|
'' => 'Tax options',
|
|
|
|
'standard' => 'Standard rates',
|
|
|
|
'tax_class_1' => 'tax_class_1 rates',
|
|
|
|
'tax_class_2' => 'tax_class_2 rates',
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals( $expected, $sections );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @testDox 'get_settings' returns the appropriate settings for the default section.
|
|
|
|
*/
|
|
|
|
public function test_get_settings_for_default_section() {
|
|
|
|
$sut = new WC_Settings_Tax();
|
|
|
|
|
2021-04-12 16:04:41 +00:00
|
|
|
$settings = $sut->get_settings_for_section( '' );
|
Refactor the settings pages, and add unit tests for them.
This commit fixes some inconsistencies in the settings pages, and
makes all the existing pages extensible by adding new sections
(that was possible in some pages, but not in others). Main changes:
1. Modify the 'get_sections' method so that it invokes a new protected
'get_own_sections' method and then triggers the
'woocommerce_get_sections_' . id filter.
This way the filter is triggered only in the base class
and not in each of the derived classes too.
2. Change the get_settings() method so that it has its signature
changed to get_settings( $current_section = '' )
in the base class and in all the derived class.
Some derived classes were already using this signature, but others
(those not having multiple sections natively) weren't, making then
effectively impossible to define multiple sections for these pages
via filters.
With this change all the section pages act consistently and allow
both adding new settings to the default "General" section
and creating new sections via filters.
3. Change the implementation of 'get_settings' in the base class
so that it searches for a 'get_settings_for_{section_id}_section'
method in the class and executes it, otherwise it executes the new
protected method get_settings_for_section( $current_section ); then
it triggers the 'woocommerce_get_settings_' . id filter.
This makes it easier to separate the code that returns the list
of filters in multiple methods, one per section, instead of using
one big if-else-else... block.
So now instead of overriding get_settings($current_section='') derived
classes need to implement get_settings_for_{$current_section}_section
for each section, or override get_settings_for_section($current_section)
or both. 'get_settings_for_section' returns an empty array by default.
Also, 'woocommerce_get_settings_' . id is triggered in one single
place too.
Other improvements:
* Remove duplicated code from 'output' in 'WC_Settings_Page' children.
Some classes inherited from 'WC_Settings_Page' override the 'output'
method with custom code, which in all cases ended up repeating the code
of the original method as a fallback. These repetitions have been
replaced with 'parent::output()'.
* Fix inconsistencies for 'save' and 'output' in WC_Settings_Tax/Emails
The 'WC_Settings_Tax' and 'WC_Settings_Emails' classes had some
inconsistencies in their 'save' and 'output' methods that prevented the
proper creation new sections and the addition of new settings via the
'woocommerce_get_sections_' and 'woocommerce_get_settings_' filters.
Now they work as expected.
* Deduplicate parts of 'save' in 'WC_Settings_Page' and children.
Two methods have been added to 'WC_Settings_Page' class:
'save_settings_for_current_section' and 'do_update_options_action'.
These are intended to be invoked by derived classes in their 'save'
methods, in order to remove code repetition.
* Add some helper methods to WC_Unit_Test_Case.
Methods added:
- assertOutputsHTML
- assertEqualsHTML
- normalize_html
- capture_output_from
2020-09-16 10:27:05 +00:00
|
|
|
$settings_ids_and_types = $this->get_ids_and_types( $settings );
|
|
|
|
|
|
|
|
$expected = array(
|
|
|
|
'tax_options' => array( 'title', 'sectionend' ),
|
|
|
|
'woocommerce_prices_include_tax' => 'radio',
|
|
|
|
'woocommerce_tax_based_on' => 'select',
|
|
|
|
'woocommerce_shipping_tax_class' => 'select',
|
|
|
|
'woocommerce_tax_round_at_subtotal' => 'checkbox',
|
|
|
|
'woocommerce_tax_classes' => 'textarea',
|
|
|
|
'woocommerce_tax_display_shop' => 'select',
|
|
|
|
'woocommerce_tax_display_cart' => 'select',
|
|
|
|
'woocommerce_price_display_suffix' => 'text',
|
|
|
|
'woocommerce_tax_total_display' => 'select',
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals( $expected, $settings_ids_and_types );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @testDox 'output' invokes 'output_tax_rates' for the 'standard' section and for sections named as a tax class.
|
|
|
|
*
|
|
|
|
* @testWith ["standard"]
|
|
|
|
* ["tax_class_slug"]
|
|
|
|
*
|
|
|
|
* @param string $section_name Current section name.
|
|
|
|
*/
|
|
|
|
public function test_output_for_standard_section_and_known_tax_class( $section_name ) {
|
|
|
|
global $current_section;
|
|
|
|
$current_section = $section_name;
|
|
|
|
|
|
|
|
$output_tax_rates_invoked = false;
|
|
|
|
|
|
|
|
StaticMockerHack::add_method_mocks(
|
|
|
|
array(
|
|
|
|
'WC_Tax' => array(
|
|
|
|
'get_tax_class_slugs' => function() {
|
|
|
|
return array( 'tax_class_slug' );
|
|
|
|
},
|
|
|
|
),
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$sut = $this->getMockBuilder( WC_Settings_Tax::class )
|
|
|
|
->setMethods( array( 'output_tax_rates' ) )
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$sut->method( 'output_tax_rates' )->will(
|
|
|
|
$this->returnCallback(
|
|
|
|
function() use ( &$output_tax_rates_invoked ) {
|
|
|
|
$output_tax_rates_invoked = true;
|
|
|
|
}
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$sut->output();
|
|
|
|
|
|
|
|
$this->assertTrue( $output_tax_rates_invoked );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @testDox 'output' fallbacks to 'output_fields' in WC_Admin_Settings for an unknown tax class.
|
|
|
|
*/
|
|
|
|
public function test_output_for_unknown_tax_class() {
|
|
|
|
global $current_section;
|
|
|
|
$current_section = 'foobar';
|
|
|
|
|
|
|
|
$output_fields_in_admin_settings_invoked = false;
|
|
|
|
|
|
|
|
StaticMockerHack::add_method_mocks(
|
|
|
|
array(
|
|
|
|
'WC_Admin_Settings' => array(
|
|
|
|
'output_fields' => function( $settings ) use ( &$output_fields_in_admin_settings_invoked ) {
|
|
|
|
$output_fields_in_admin_settings_invoked = true;
|
|
|
|
},
|
|
|
|
),
|
|
|
|
'WC_Tax' => array(
|
|
|
|
'get_tax_class_slugs' => function() {
|
|
|
|
return array( 'tax_class_slug' );
|
|
|
|
},
|
|
|
|
),
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$sut = new WC_Settings_Tax();
|
|
|
|
|
|
|
|
$sut->output();
|
|
|
|
|
|
|
|
$this->assertTrue( $output_fields_in_admin_settings_invoked );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @testDox 'save_tax_classes' appropriately creates or deletes the tax classes.
|
|
|
|
*/
|
|
|
|
public function test_save_tax_classes() {
|
|
|
|
$created = array();
|
|
|
|
$deleted = array();
|
|
|
|
|
|
|
|
StaticMockerHack::add_method_mocks(
|
|
|
|
array(
|
|
|
|
'WC_Tax' => array(
|
|
|
|
'get_tax_classes' => function() {
|
|
|
|
return array( 'tax_1', 'tax_2', 'tax_3' );
|
|
|
|
},
|
|
|
|
'delete_tax_class_by' => function( $field, $name ) use ( &$deleted ) {
|
|
|
|
$deleted[] = $name;
|
|
|
|
},
|
|
|
|
'create_tax_class' => function( $name ) use ( &$created ) {
|
|
|
|
$created[] = $name;
|
|
|
|
},
|
|
|
|
),
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$sut = new WC_Settings_Tax();
|
|
|
|
|
|
|
|
$sut->save_tax_classes( "tax_2\ntax_3\ntax_4" );
|
|
|
|
|
|
|
|
$this->assertEquals( array( 'tax_1' ), $deleted );
|
|
|
|
$this->assertEquals( array( 'tax_4' ), $created );
|
|
|
|
}
|
|
|
|
}
|