Break “legacy” setting to “new” conversion into it’s own method.

This commit is contained in:
Jeff Stieler 2016-06-13 11:37:35 -06:00 committed by Justin Shreve
parent 6c53cfa167
commit 1fc539992c
2 changed files with 128 additions and 22 deletions

View File

@ -56,33 +56,49 @@ class WC_Register_Legacy_Settings {
foreach ( $legacy_sections as $legacy_section => $legacy_section_label ) {
$legacy_settings = $this->page->get_settings( $legacy_section );
foreach ( $legacy_settings as $legacy_setting ) {
if ( ! isset( $legacy_setting['id'] ) ) {
continue;
$new_setting = $this->new_setting_from_legacy( $legacy_setting );
if ( $new_setting ) {
$settings[] = $new_setting;
}
$new_setting = array(
'id' => $legacy_setting['id'],
'label' => ( ! empty( $legacy_setting['title'] ) ? $legacy_setting['title'] : '' ),
'description' => ( ! empty( $legacy_setting['desc'] ) ? $legacy_setting['desc'] : '' ),
'type' => $legacy_setting['type'],
);
if ( isset( $legacy_setting['default'] ) ) {
$new_setting['default'] = $legacy_setting['default'];
}
if ( isset( $legacy_setting['options'] ) ) {
$new_setting['options'] = $legacy_setting['options'];
}
if ( isset( $legacy_setting['desc_tip'] ) ) {
if ( true === $legacy_setting['desc_tip'] ) {
$new_setting['tip'] = $legacy_setting['desc'];
} else if ( ! empty( $legacy_setting['desc_tip'] ) ) {
$new_setting['tip'] = $legacy_setting['desc_tip'];
}
}
$settings[] = $new_setting;
}
}
return $settings;
}
/**
* Convert a "legacy" setting (WC_Settings_Page::get_settings()) into the format expected
* for the REST API Settings Controller
*
* @param $legacy_setting Settings array, as produced by a subclass of WC_Settings_Page.
*
* @return array|bool Boolean false if legacy setting has no ID, Array of converted new setting otherwise.
*/
public function new_setting_from_legacy( $legacy_setting ) {
if ( ! isset( $legacy_setting['id'] ) ) {
return false;
}
$new_setting = array(
'id' => $legacy_setting['id'],
'label' => ( ! empty( $legacy_setting['title'] ) ? $legacy_setting['title'] : '' ),
'description' => ( ! empty( $legacy_setting['desc'] ) ? $legacy_setting['desc'] : '' ),
'type' => $legacy_setting['type'],
);
if ( isset( $legacy_setting['default'] ) ) {
$new_setting['default'] = $legacy_setting['default'];
}
if ( isset( $legacy_setting['options'] ) ) {
$new_setting['options'] = $legacy_setting['options'];
}
if ( isset( $legacy_setting['desc_tip'] ) ) {
if ( true === $legacy_setting['desc_tip'] ) {
$new_setting['tip'] = $legacy_setting['desc'];
} else if ( ! empty( $legacy_setting['desc_tip'] ) ) {
$new_setting['tip'] = $legacy_setting['desc_tip'];
}
}
return $new_setting;
}
}
/**

View File

@ -54,4 +54,94 @@ class WC_Tests_Register_Legacy_Settings extends WC_Unit_Test_Case {
$this->assertEquals( $expected, $actual );
}
public function new_setting_from_legacy_provider() {
return array(
// No "id" case
array(
array(
'type' => 'some-type-with-no-id',
),
false,
),
// All optional properties except 'desc_tip'
array(
array(
'id' => 'setting-id',
'type' => 'select',
'title' => 'Setting Name',
'desc' => 'Setting Description',
'default' => 'one',
'options' => array( 'one', 'two' ),
),
array(
'id' => 'setting-id',
'type' => 'select',
'label' => 'Setting Name',
'description' => 'Setting Description',
'default' => 'one',
'options' => array( 'one', 'two' ),
),
),
// Boolean 'desc_tip' defaulting to 'desc' value
array(
array(
'id' => 'setting-id',
'type' => 'select',
'title' => 'Setting Name',
'desc' => 'Setting Description',
'desc_tip' => true,
),
array(
'id' => 'setting-id',
'type' => 'select',
'label' => 'Setting Name',
'description' => 'Setting Description',
'tip' => 'Setting Description',
),
),
// String 'desc_tip'
array(
array(
'id' => 'setting-id',
'type' => 'select',
'title' => 'Setting Name',
'desc' => 'Setting Description',
'desc_tip' => 'Setting Tip',
),
array(
'id' => 'setting-id',
'type' => 'select',
'label' => 'Setting Name',
'description' => 'Setting Description',
'tip' => 'Setting Tip',
),
),
// Empty 'title' and 'desc'
array(
array(
'id' => 'setting-id',
'type' => 'select',
),
array(
'id' => 'setting-id',
'type' => 'select',
'label' => '',
'description' => '',
),
),
);
}
/**
* @dataProvider new_setting_from_legacy_provider
* @covers WC_Register_Legacy_Settings::new_setting_from_legacy
*/
public function test_new_setting_from_legacy( $input, $expected ) {
$legacy_settings = new WC_Register_Legacy_Settings( $this->page );
$actual = $legacy_settings->new_setting_from_legacy( $input );
$this->assertEquals( $expected, $actual );
}
}