2016-06-13 16:09:31 +00:00
|
|
|
<?php
|
2021-04-12 16:04:41 +00:00
|
|
|
/**
|
|
|
|
* WC_Tests_Register_WP_Admin_Settings class file.
|
|
|
|
*
|
|
|
|
* @package @package WooCommerce\Tests\Settings
|
|
|
|
*/
|
2016-06-13 16:09:31 +00:00
|
|
|
|
|
|
|
/**
|
2016-07-26 18:08:29 +00:00
|
|
|
* Settings API: WP-Admin Helper Tests
|
|
|
|
* Tests the helper class that makes settings (currently present in wp-admin)
|
2017-07-10 05:56:28 +00:00
|
|
|
* available to the REST API.
|
2016-07-19 18:24:05 +00:00
|
|
|
*
|
2016-06-13 16:09:31 +00:00
|
|
|
* @package WooCommerce\Tests\Settings
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0.0
|
2016-06-13 16:09:31 +00:00
|
|
|
*/
|
2016-07-26 18:08:29 +00:00
|
|
|
class WC_Tests_Register_WP_Admin_Settings extends WC_Unit_Test_Case {
|
2016-06-13 16:09:31 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var WC_Settings_Page $page
|
|
|
|
*/
|
|
|
|
protected $page;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize a WC_Settings_Page for testing
|
|
|
|
*/
|
|
|
|
public function setUp() {
|
|
|
|
parent::setUp();
|
|
|
|
|
2017-01-03 12:19:03 +00:00
|
|
|
$mock_page = $this->getMockBuilder( 'WC_Settings_General' )->getMock();
|
2016-06-13 18:07:02 +00:00
|
|
|
|
|
|
|
$mock_page
|
|
|
|
->expects( $this->any() )
|
|
|
|
->method( 'get_id' )
|
|
|
|
->will( $this->returnValue( 'page-id' ) );
|
|
|
|
|
|
|
|
$mock_page
|
|
|
|
->expects( $this->any() )
|
|
|
|
->method( 'get_label' )
|
|
|
|
->will( $this->returnValue( 'Page Label' ) );
|
|
|
|
|
|
|
|
$this->page = $mock_page;
|
2016-06-13 16:09:31 +00:00
|
|
|
}
|
|
|
|
|
2016-06-13 16:38:30 +00:00
|
|
|
/**
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0.0
|
2016-07-26 18:08:29 +00:00
|
|
|
* @covers WC_Register_WP_Admin_Settings::__construct
|
2016-06-13 16:38:30 +00:00
|
|
|
*/
|
2016-06-13 16:09:31 +00:00
|
|
|
public function test_constructor() {
|
2016-09-08 22:38:58 +00:00
|
|
|
$settings = new WC_Register_WP_Admin_Settings( $this->page, 'page' );
|
2016-07-19 18:24:05 +00:00
|
|
|
|
2016-09-08 22:38:58 +00:00
|
|
|
$this->assertEquals( has_filter( 'woocommerce_settings_groups', array( $settings, 'register_page_group' ) ), 10 );
|
|
|
|
$this->assertEquals( has_filter( 'woocommerce_settings-' . $this->page->get_id(), array( $settings, 'register_page_settings' ) ), 10 );
|
2016-06-13 16:09:31 +00:00
|
|
|
}
|
|
|
|
|
2016-06-13 16:38:30 +00:00
|
|
|
/**
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0.0
|
2016-09-12 16:33:36 +00:00
|
|
|
* @covers WC_Register_WP_Admin_Settings::register_page_group
|
2016-06-13 16:38:30 +00:00
|
|
|
*/
|
2016-07-26 18:08:29 +00:00
|
|
|
public function test_register_group() {
|
2016-09-08 22:38:58 +00:00
|
|
|
$settings = new WC_Register_WP_Admin_Settings( $this->page, 'page' );
|
2016-06-13 16:38:30 +00:00
|
|
|
|
|
|
|
$existing = array(
|
|
|
|
'id' => 'existing-id',
|
|
|
|
'label' => 'Existing Group',
|
|
|
|
);
|
|
|
|
$initial = array( $existing );
|
|
|
|
$expected = array(
|
|
|
|
$existing,
|
|
|
|
array(
|
|
|
|
'id' => $this->page->get_id(),
|
|
|
|
'label' => $this->page->get_label(),
|
|
|
|
),
|
|
|
|
);
|
2019-05-01 22:05:00 +00:00
|
|
|
$actual = $settings->register_page_group( $initial );
|
2016-06-13 16:38:30 +00:00
|
|
|
|
|
|
|
$this->assertEquals( $expected, $actual );
|
|
|
|
}
|
|
|
|
|
2016-07-19 18:24:05 +00:00
|
|
|
/**
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0.0
|
2016-07-19 18:24:05 +00:00
|
|
|
*/
|
2016-07-26 18:08:29 +00:00
|
|
|
public function register_setting_provider() {
|
2016-06-13 17:37:35 +00:00
|
|
|
return array(
|
2021-04-12 16:04:41 +00:00
|
|
|
// No "id" case.
|
2016-06-13 17:37:35 +00:00
|
|
|
array(
|
|
|
|
array(
|
2016-09-08 22:38:58 +00:00
|
|
|
'type' => 'some-type-with-no-id',
|
|
|
|
'option_key' => '',
|
2016-06-13 17:37:35 +00:00
|
|
|
),
|
|
|
|
false,
|
|
|
|
),
|
2021-04-12 16:04:41 +00:00
|
|
|
// All optional properties except 'desc_tip'.
|
2016-06-13 17:37:35 +00:00
|
|
|
array(
|
|
|
|
array(
|
2016-09-08 22:38:58 +00:00
|
|
|
'id' => 'setting-id',
|
|
|
|
'type' => 'select',
|
|
|
|
'title' => 'Setting Name',
|
|
|
|
'desc' => 'Setting Description',
|
|
|
|
'default' => 'one',
|
|
|
|
'options' => array( 'one', 'two' ),
|
|
|
|
'option_key' => '',
|
2016-06-13 17:37:35 +00:00
|
|
|
),
|
|
|
|
array(
|
|
|
|
'id' => 'setting-id',
|
|
|
|
'type' => 'select',
|
|
|
|
'label' => 'Setting Name',
|
|
|
|
'description' => 'Setting Description',
|
|
|
|
'default' => 'one',
|
|
|
|
'options' => array( 'one', 'two' ),
|
2016-09-08 22:38:58 +00:00
|
|
|
'option_key' => '',
|
2016-06-13 17:37:35 +00:00
|
|
|
),
|
|
|
|
),
|
2021-04-12 16:04:41 +00:00
|
|
|
// Boolean 'desc_tip' defaulting to 'desc' value.
|
2016-06-13 17:37:35 +00:00
|
|
|
array(
|
|
|
|
array(
|
2019-05-01 22:05:00 +00:00
|
|
|
'id' => 'setting-id',
|
|
|
|
'type' => 'select',
|
|
|
|
'title' => 'Setting Name',
|
|
|
|
'desc' => 'Setting Description',
|
|
|
|
'desc_tip' => true,
|
2016-09-08 22:38:58 +00:00
|
|
|
'option_key' => '',
|
2016-06-13 17:37:35 +00:00
|
|
|
),
|
|
|
|
array(
|
|
|
|
'id' => 'setting-id',
|
|
|
|
'type' => 'select',
|
|
|
|
'label' => 'Setting Name',
|
|
|
|
'description' => 'Setting Description',
|
|
|
|
'tip' => 'Setting Description',
|
2016-09-08 22:38:58 +00:00
|
|
|
'option_key' => '',
|
2016-06-13 17:37:35 +00:00
|
|
|
),
|
|
|
|
),
|
2021-04-12 16:04:41 +00:00
|
|
|
// String 'desc_tip'.
|
2016-06-13 17:37:35 +00:00
|
|
|
array(
|
|
|
|
array(
|
2016-09-08 22:38:58 +00:00
|
|
|
'id' => 'setting-id',
|
|
|
|
'type' => 'select',
|
|
|
|
'title' => 'Setting Name',
|
|
|
|
'desc' => 'Setting Description',
|
|
|
|
'desc_tip' => 'Setting Tip',
|
|
|
|
'option_key' => '',
|
2016-06-13 17:37:35 +00:00
|
|
|
),
|
|
|
|
array(
|
|
|
|
'id' => 'setting-id',
|
|
|
|
'type' => 'select',
|
|
|
|
'label' => 'Setting Name',
|
|
|
|
'description' => 'Setting Description',
|
|
|
|
'tip' => 'Setting Tip',
|
2016-09-08 22:38:58 +00:00
|
|
|
'option_key' => '',
|
2016-06-13 17:37:35 +00:00
|
|
|
),
|
|
|
|
),
|
2021-04-12 16:04:41 +00:00
|
|
|
// phpcs:ignore Squiz.PHP.CommentedOutCode.Found
|
|
|
|
// Empty 'title' and empty 'desc'.
|
2016-06-13 17:37:35 +00:00
|
|
|
array(
|
|
|
|
array(
|
2016-09-08 22:38:58 +00:00
|
|
|
'id' => 'setting-id',
|
|
|
|
'type' => 'select',
|
|
|
|
'option_key' => '',
|
2016-06-13 17:37:35 +00:00
|
|
|
),
|
|
|
|
array(
|
|
|
|
'id' => 'setting-id',
|
|
|
|
'type' => 'select',
|
|
|
|
'label' => '',
|
|
|
|
'description' => '',
|
2016-09-08 22:38:58 +00:00
|
|
|
'option_key' => '',
|
2016-06-13 17:37:35 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0.0
|
2016-07-26 18:08:29 +00:00
|
|
|
* @dataProvider register_setting_provider
|
|
|
|
* @covers WC_Register_WP_Admin_Settings::register_setting
|
2021-04-12 16:04:41 +00:00
|
|
|
* @param array $input Array of settings.
|
|
|
|
* @param bool $expected Expected result of the setting registering operation.
|
2016-06-13 17:37:35 +00:00
|
|
|
*/
|
2016-07-26 18:08:29 +00:00
|
|
|
public function test_register_setting( $input, $expected ) {
|
2016-09-08 22:38:58 +00:00
|
|
|
$settings = new WC_Register_WP_Admin_Settings( $this->page, 'page' );
|
2016-06-13 17:37:35 +00:00
|
|
|
|
2016-07-26 18:08:29 +00:00
|
|
|
$actual = $settings->register_setting( $input );
|
2016-06-13 17:37:35 +00:00
|
|
|
|
|
|
|
$this->assertEquals( $expected, $actual );
|
|
|
|
}
|
|
|
|
|
2016-06-13 18:07:02 +00:00
|
|
|
/**
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0.0
|
2016-09-12 20:51:32 +00:00
|
|
|
* @covers WC_Register_WP_Admin_Settings::register_page_settings
|
2016-06-13 18:07:02 +00:00
|
|
|
*/
|
2021-04-13 10:45:42 +00:00
|
|
|
public function test_register_settings_default_section_no_settings() {
|
2016-06-13 18:07:02 +00:00
|
|
|
$this->page
|
|
|
|
->expects( $this->any() )
|
|
|
|
->method( 'get_sections' )
|
|
|
|
->will( $this->returnValue( array() ) );
|
|
|
|
|
|
|
|
$this->page
|
|
|
|
->expects( $this->once() )
|
2021-04-13 10:45:42 +00:00
|
|
|
->method( 'get_settings' )
|
|
|
|
->with( $this->equalTo( '' ) )
|
2016-06-13 18:07:02 +00:00
|
|
|
->will( $this->returnValue( array() ) );
|
|
|
|
|
2016-09-08 22:38:58 +00:00
|
|
|
$settings = new WC_Register_WP_Admin_Settings( $this->page, 'page' );
|
2016-06-13 18:07:02 +00:00
|
|
|
|
|
|
|
$expected = array();
|
2016-09-08 22:38:58 +00:00
|
|
|
$actual = $settings->register_page_settings( array() );
|
2016-06-13 18:07:02 +00:00
|
|
|
|
|
|
|
$this->assertEquals( $expected, $actual );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0.0
|
2016-09-12 20:51:32 +00:00
|
|
|
* @covers WC_Register_WP_Admin_Settings::register_page_settings
|
2016-06-13 18:07:02 +00:00
|
|
|
*/
|
2021-04-13 10:45:42 +00:00
|
|
|
public function test_register_settings_default_section_with_settings() {
|
2016-06-13 18:07:02 +00:00
|
|
|
$this->page
|
|
|
|
->expects( $this->any() )
|
|
|
|
->method( 'get_sections' )
|
|
|
|
->will( $this->returnValue( array() ) );
|
|
|
|
|
|
|
|
$settings = array(
|
|
|
|
array(
|
2019-05-01 22:05:00 +00:00
|
|
|
'id' => 'setting-1',
|
|
|
|
'type' => 'text',
|
|
|
|
'option_key' => '',
|
2016-06-13 18:07:02 +00:00
|
|
|
),
|
|
|
|
array(
|
2019-05-01 22:05:00 +00:00
|
|
|
'type' => 'no-id',
|
|
|
|
'option_key' => '',
|
2016-06-13 18:07:02 +00:00
|
|
|
),
|
|
|
|
array(
|
2019-05-01 22:05:00 +00:00
|
|
|
'id' => 'setting-2',
|
|
|
|
'type' => 'textarea',
|
|
|
|
'option_key' => '',
|
2016-06-13 18:07:02 +00:00
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->page
|
|
|
|
->expects( $this->any() )
|
2021-04-13 10:45:42 +00:00
|
|
|
->method( 'get_settings' )
|
2016-06-13 18:07:02 +00:00
|
|
|
->will( $this->returnValue( $settings ) );
|
|
|
|
|
2016-09-08 22:38:58 +00:00
|
|
|
$settings = new WC_Register_WP_Admin_Settings( $this->page, 'page' );
|
2016-06-13 18:07:02 +00:00
|
|
|
|
|
|
|
$expected = array(
|
|
|
|
array(
|
|
|
|
'id' => 'setting-1',
|
|
|
|
'type' => 'text',
|
|
|
|
'label' => '',
|
|
|
|
'description' => '',
|
2016-09-08 22:38:58 +00:00
|
|
|
'option_key' => 'setting-1',
|
2016-06-13 18:07:02 +00:00
|
|
|
),
|
|
|
|
array(
|
|
|
|
'id' => 'setting-2',
|
|
|
|
'type' => 'textarea',
|
|
|
|
'label' => '',
|
|
|
|
'description' => '',
|
2016-09-08 22:38:58 +00:00
|
|
|
'option_key' => 'setting-2',
|
2016-06-13 18:07:02 +00:00
|
|
|
),
|
|
|
|
);
|
2019-05-01 22:05:00 +00:00
|
|
|
$actual = $settings->register_page_settings( array() );
|
2016-06-13 18:07:02 +00:00
|
|
|
|
|
|
|
$this->assertEquals( $expected, $actual );
|
|
|
|
}
|
2016-07-19 18:24:05 +00:00
|
|
|
}
|