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_Products_Test file.
|
|
|
|
*
|
|
|
|
* @package WooCommerce\Tests\Settings
|
|
|
|
*/
|
|
|
|
|
|
|
|
// phpcs:ignore Squiz.Commenting.FileComment.Missing
|
|
|
|
|
|
|
|
require_once __DIR__ . '/class-wc-settings-unit-test-case.php';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unit tests for the WC_Settings_Products class.
|
|
|
|
*/
|
|
|
|
class WC_Settings_Products_Test extends WC_Settings_Unit_Test_Case {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @testdox get_sections should get all the existing sections.
|
|
|
|
*/
|
|
|
|
public function test_get_sections() {
|
|
|
|
$sut = new WC_Settings_Products();
|
|
|
|
|
|
|
|
$section_names = array_keys( $sut->get_sections() );
|
|
|
|
|
|
|
|
$expected = array(
|
|
|
|
'',
|
|
|
|
'inventory',
|
|
|
|
'downloadable',
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals( $expected, $section_names );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get_settings should trigger the appropriate filter(s) depending on the requested section name.
|
|
|
|
*
|
|
|
|
* @testWith ["", ["woocommerce_products_general_settings", "woocommerce_product_settings"]]
|
|
|
|
* ["inventory", ["woocommerce_inventory_settings"]]
|
|
|
|
* ["downloadable", ["woocommerce_downloadable_products_settings"]]
|
|
|
|
*
|
|
|
|
* @param string $section_name The section name to test getting the settings for.
|
|
|
|
* @param string $filter_names The name of the filter that is expected to be triggered.
|
|
|
|
*/
|
|
|
|
public function test_get_settings_triggers_filter( $section_name, $filter_names ) {
|
|
|
|
$actual_settings_via_filter = array();
|
|
|
|
|
|
|
|
foreach ( $filter_names as $filter_name ) {
|
|
|
|
add_filter(
|
|
|
|
$filter_name,
|
|
|
|
function ( $settings ) use ( $filter_name, &$actual_settings_via_filter ) {
|
|
|
|
$actual_settings_via_filter[ $filter_name ] = $settings;
|
|
|
|
|
|
|
|
return $settings;
|
|
|
|
},
|
|
|
|
10,
|
|
|
|
1
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$sut = new WC_Settings_Products();
|
|
|
|
|
2021-04-12 16:04:41 +00:00
|
|
|
$actual_settings_returned = $sut->get_settings_for_section( $section_name );
|
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
|
|
|
|
|
|
|
foreach ( $filter_names as $filter_name ) {
|
|
|
|
remove_all_filters( $filter_name );
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ( $filter_names as $filter_name ) {
|
|
|
|
$this->assertSame( $actual_settings_returned, $actual_settings_via_filter[ $filter_name ] );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @testdox get_settings('') should return all the settings for the default section.
|
|
|
|
*/
|
|
|
|
public function test_get_default_settings_returns_all_settings() {
|
|
|
|
$sut = new WC_Settings_Products();
|
|
|
|
|
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(
|
|
|
|
'catalog_options' => array( 'title', 'sectionend' ),
|
|
|
|
'woocommerce_shop_page_id' => 'single_select_page',
|
|
|
|
'woocommerce_cart_redirect_after_add' => 'checkbox',
|
|
|
|
'woocommerce_enable_ajax_add_to_cart' => 'checkbox',
|
|
|
|
'woocommerce_placeholder_image' => 'text',
|
|
|
|
'product_measurement_options' => array( 'title', 'sectionend' ),
|
|
|
|
'woocommerce_weight_unit' => 'select',
|
|
|
|
'woocommerce_dimension_unit' => 'select',
|
|
|
|
'product_rating_options' => array( 'title', 'sectionend' ),
|
|
|
|
'woocommerce_enable_reviews' => 'checkbox',
|
|
|
|
'woocommerce_review_rating_verification_label' => 'checkbox',
|
|
|
|
'woocommerce_review_rating_verification_required' => 'checkbox',
|
|
|
|
'woocommerce_enable_review_rating' => 'checkbox',
|
|
|
|
'woocommerce_review_rating_required' => 'checkbox',
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals( $expected, $settings_ids_and_types );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @testdox get_settings('inventory') should return all the settings for the inventory section.
|
|
|
|
*/
|
|
|
|
public function test_get_inventory_settings_returns_all_settings() {
|
|
|
|
$sut = new WC_Settings_Products();
|
|
|
|
|
2021-04-12 16:04:41 +00:00
|
|
|
$settings = $sut->get_settings_for_section( 'inventory' );
|
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(
|
|
|
|
'product_inventory_options' => array( 'title', 'sectionend' ),
|
|
|
|
'woocommerce_manage_stock' => 'checkbox',
|
|
|
|
'woocommerce_hold_stock_minutes' => 'number',
|
|
|
|
'woocommerce_notify_low_stock' => 'checkbox',
|
|
|
|
'woocommerce_notify_no_stock' => 'checkbox',
|
|
|
|
'woocommerce_stock_email_recipient' => 'text',
|
|
|
|
'woocommerce_notify_low_stock_amount' => 'number',
|
|
|
|
'woocommerce_notify_no_stock_amount' => 'number',
|
|
|
|
'woocommerce_hide_out_of_stock_items' => 'checkbox',
|
|
|
|
'woocommerce_stock_format' => 'select',
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals( $expected, $settings_ids_and_types );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @testdox get_settings('downloadable') should return all the settings for the inventory section.
|
|
|
|
*/
|
|
|
|
public function test_get_downloadable_settings_returns_all_settings() {
|
|
|
|
$sut = new WC_Settings_Products();
|
|
|
|
|
2021-04-12 16:04:41 +00:00
|
|
|
$settings = $sut->get_settings_for_section( 'downloadable' );
|
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(
|
2021-07-16 19:27:23 +00:00
|
|
|
'digital_download_options' => array( 'title', 'sectionend' ),
|
|
|
|
'woocommerce_file_download_method' => 'select',
|
|
|
|
'woocommerce_downloads_redirect_fallback_allowed' => 'checkbox',
|
|
|
|
'woocommerce_downloads_require_login' => 'checkbox',
|
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
|
|
|
'woocommerce_downloads_grant_access_after_payment' => 'checkbox',
|
2021-07-16 19:27:23 +00:00
|
|
|
'woocommerce_downloads_add_hash_to_filename' => 'checkbox',
|
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
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals( $expected, $settings_ids_and_types );
|
|
|
|
}
|
2021-02-02 08:31:11 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @testdox 'save' flushes the term count cache.
|
|
|
|
*/
|
|
|
|
public function test_save_does_recount_terms() {
|
2021-03-12 08:50:21 +00:00
|
|
|
$wc_recount_all_terms_called = false;
|
2021-02-02 08:31:11 +00:00
|
|
|
|
2021-03-12 08:50:21 +00:00
|
|
|
$this->register_legacy_proxy_function_mocks(
|
2021-02-02 08:31:11 +00:00
|
|
|
array(
|
2021-03-12 08:50:21 +00:00
|
|
|
'wc_recount_all_terms' => function() use ( &$wc_recount_all_terms_called ) {
|
|
|
|
$wc_recount_all_terms_called = true;
|
|
|
|
},
|
2021-02-02 08:31:11 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$sut = new WC_Settings_Products();
|
|
|
|
$sut->save();
|
|
|
|
|
2021-03-12 08:50:21 +00:00
|
|
|
$this->assertTrue( $wc_recount_all_terms_called );
|
2021-02-02 08:31:11 +00:00
|
|
|
}
|
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
|
|
|
}
|