woocommerce/tests/php/includes/settings/class-wc-settings-shipping-...

318 lines
9.2 KiB
PHP
Raw Normal View History

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_Shipping_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_Shipping class.
*/
class WC_Settings_Shipping_Test extends WC_Settings_Unit_Test_Case {
/**
* @testDox 'get_sections' returns a fixed set of sections, plus one section for each shipping method having settings.
*/
public function test_get_sections_returns_fixed_sections_and_one_section_per_shipping_method_with_settings() {
$methods = array(
//phpcs:disable Squiz.Commenting
new class() {
public $id;
public $method_title;
public function has_settings() {
return false;
}
public function __construct() {
$this->id = 'method_with_no_settings';
}
},
new class() {
public $id;
public $method_title;
public function has_settings() {
return true;
}
public function __construct() {
$this->id = 'method_1_id';
$this->method_title = null;
}
},
new class() {
public $id;
public $method_title;
public function has_settings() {
return true;
}
public function __construct() {
$this->id = 'method_2_id';
$this->method_title = 'method_2_title';
}
},
//phpcs:enable
);
$sut = $this->getMockBuilder( WC_Settings_Shipping::class )
->setMethods( array( 'get_shipping_methods', 'wc_is_installing' ) )
->getMock();
$sut->method( 'get_shipping_methods' )->willReturn( $methods );
$sut->method( 'wc_is_installing' )->willReturn( false );
$sections = $sut->get_sections();
$expected = array(
'' => 'Shipping zones',
'options' => 'Shipping options',
'classes' => 'Shipping classes',
'method_1_id' => 'Method_1_id',
'method_2_id' => 'method_2_title',
);
$this->assertEquals( $expected, $sections );
}
/**
* @testDox 'get_settings' returns proper default settings, also 'options' is an alias for the default section.
*
* @testWith [""]
* ["options"]
*
* @param string $section_name The name of the section to get the settings for.
*/
public function test_get_default_and_options_settings_returns_all_settings( $section_name ) {
$sut = new WC_Settings_Shipping();
$settings = $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
$settings_ids_and_types = $this->get_ids_and_types( $settings );
$expected = array(
'shipping_options' => array( 'title', 'sectionend' ),
'woocommerce_enable_shipping_calc' => 'checkbox',
'woocommerce_shipping_cost_requires_address' => 'checkbox',
'woocommerce_ship_to_destination' => 'radio',
'woocommerce_shipping_debug_mode' => 'checkbox',
);
$this->assertEquals( $expected, $settings_ids_and_types );
}
/**
* @testDox 'output' for a predefined section invokes the appropriate internal method.
*
* @testWith ["", "output_zones_screen"]
* ["classes", "output_shipping_class_screen"]
*
* @param string $section_name Current section name.
* @param string $expected_method_invoked Name of the internal method expected to be invoked.
*/
public function test_output_for_predefined_section( $section_name, $expected_method_invoked ) {
global $current_section;
$current_section = $section_name;
$method_invoked = null;
$sut = $this->getMockBuilder( WC_Settings_Shipping::class )
->setMethods( array( 'get_shipping_methods', 'output_zones_screen', 'output_fields', 'output_shipping_class_screen' ) )
->getMock();
$sut->method( 'get_shipping_methods' )->willReturn( array() );
$sut->method( 'output_zones_screen' )->will(
$this->returnCallback(
function() use ( &$method_invoked ) {
$method_invoked = 'output_zones_screen';
}
)
);
$sut->method( 'output_shipping_class_screen' )->will(
$this->returnCallback(
function() use ( &$method_invoked ) {
$method_invoked = 'output_shipping_class_screen';
}
)
);
$sut->output();
$this->assertEquals( $expected_method_invoked, $method_invoked );
}
/**
* @testDox 'output' invokes 'admin_options' of the method if section is the id of a shipping method with settings.
*/
public function test_output_for_shipping_method_id_with_settings() {
global $current_section;
$current_section = 'method_id';
//phpcs:disable Squiz.Commenting
$method =
new class() {
public $id;
public $method_title;
public $admin_options_invoked;
public function has_settings() {
return true;
}
public function admin_options() {
$this->admin_options_invoked = true;
}
public function __construct() {
$this->id = 'method_id';
$this->admin_options_invoked = false;
}
};
//phpcs:enable
$sut = $this->getMockBuilder( WC_Settings_Shipping::class )
->setMethods( array( 'get_shipping_methods' ) )
->getMock();
$sut->method( 'get_shipping_methods' )->willReturn( array( $method ) );
$sut->output();
$this->assertTrue( $method->admin_options_invoked );
}
/**
* @testDox 'output' fallbacks to default behavior when section is for a shipping method without settings, or not a known shipping method.
*
* @testWith ["method_name"]
* ["foobar"]
*
* @param string $section_name Current section name.
*/
public function test_output_for_shipping_method_without_settings_or_unknown_section( $section_name ) {
global $current_section;
$current_section = $section_name;
$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;
},
),
)
);
//phpcs:disable Squiz.Commenting
$method =
new class() {
public $id;
public $method_title;
public $admin_options_invoked;
public function has_settings() {
return false;
}
public function admin_options() {
$this->admin_options_invoked = true;
}
public function __construct() {
$this->id = 'method_id';
$this->admin_options_invoked = false;
}
};
//phpcs:enable
$sut = $this->getMockBuilder( WC_Settings_Shipping::class )
->setMethods( array( 'get_shipping_methods' ) )
->getMock();
$sut->method( 'get_shipping_methods' )->willReturn( array( $method ) );
$sut->output();
$this->assertFalse( $method->admin_options_invoked );
$this->assertTrue( $output_fields_in_admin_settings_invoked );
}
/**
* @testDox 'save' triggers the appropriate action, and also 'save_settings_for_current_section' if needed, when the current section is a predefined one.
*
* @testWith ["", false, false]
* ["options", true, true]
* ["classes", false, true]
*
* @param string $section_name Current section name.
* @param bool $expect_save_settings_for_current_section_invoked Is 'save_settings_for_current_section' expected to be invoked?.
* @param bool $expect_update_options_action_invoked Is the 'woocommerce_update_options_' action expected to be triggered?.
*/
public function test_save_predefined_section( $section_name, $expect_save_settings_for_current_section_invoked, $expect_update_options_action_invoked ) {
global $current_section;
$current_section = $section_name;
$save_settings_for_current_section_invoked = false;
$sut = $this->getMockBuilder( WC_Settings_Shipping::class )
->setMethods( array( 'get_shipping_methods', 'save_settings_for_current_section' ) )
->getMock();
$sut->method( 'get_shipping_methods' )->willReturn( array() );
$sut->method( 'save_settings_for_current_section' )->will(
$this->returnCallback(
function() use ( &$save_settings_for_current_section_invoked ) {
$save_settings_for_current_section_invoked = true;
}
)
);
$sut->save();
$this->assertEquals( $expect_save_settings_for_current_section_invoked, $save_settings_for_current_section_invoked );
$this->assertEquals( $expect_update_options_action_invoked ? 1 : 0, did_action( 'woocommerce_update_options_shipping_' . $section_name ) );
}
/**
* @testDox 'save' triggers 'woocommerce_update_options_shipping_{method_id}' when the current section is the id of a shipping method with settings.
*/
public function test_save_for_shipping_method_triggers_appropriate_action() {
global $current_section;
$current_section = 'method_id';
//phpcs:disable Squiz.Commenting
$method =
new class() {
public $id;
public $method_title;
public $admin_options_invoked;
public function has_settings() {
return true;
}
public function __construct() {
$this->id = 'method_id';
}
};
//phpcs:enable
$sut = $this->getMockBuilder( WC_Settings_Shipping::class )
->setMethods( array( 'get_shipping_methods' ) )
->getMock();
$sut->method( 'get_shipping_methods' )->willReturn( array( $method ) );
$sut->save();
$this->assertEquals( 1, did_action( 'woocommerce_update_options_shipping_method_id' ) );
}
}