wordpress-develop/tests/phpunit/tests/customize/control.php
Sergey Biryukov e72fff9cef Code Modernization: Replace dirname( __FILE__ ) calls with __DIR__ magic constant.
This avoids the performance overhead of the function call every time `dirname( __FILE__ )` was used instead of `__DIR__`.

This commit also includes:

* Removing unnecessary parentheses from `include`/`require` statements. These are language constructs, not function calls.
* Replacing `include` statements for several files with `require_once`, for consistency:
 * `wp-admin/admin-header.php`
 * `wp-admin/admin-footer.php`
 * `wp-includes/version.php`

Props ayeshrajans, desrosj, valentinbora, jrf, joostdevalk, netweb.
Fixes #48082.

git-svn-id: https://develop.svn.wordpress.org/trunk@47198 602fd350-edb4-49c9-b593-d223f7449a82
2020-02-06 06:31:22 +00:00

181 lines
4.9 KiB
PHP

<?php
/**
* Test_WP_Customize_Control tests.
*
* @package WordPress
*/
/**
* Tests for the Test_WP_Customize_Control class.
*
* @todo This is missing dedicated tests for all but one of the methods.
*
* @group customize
*/
class Test_WP_Customize_Control extends WP_UnitTestCase {
/**
* Manager.
*
* @var WP_Customize_Manager
*/
public $wp_customize;
/**
* Set up.
*/
function setUp() {
parent::setUp();
wp_set_current_user( $this->factory()->user->create( array( 'role' => 'administrator' ) ) );
require_once ABSPATH . WPINC . '/class-wp-customize-manager.php';
$GLOBALS['wp_customize'] = new WP_Customize_Manager();
$this->wp_customize = $GLOBALS['wp_customize'];
}
/**
* Test WP_Customize_Control::check_capabilities().
*
* @see WP_Customize_Control::check_capabilities()
*/
function test_check_capabilities() {
do_action( 'customize_register', $this->wp_customize );
$control = new WP_Customize_Control(
$this->wp_customize,
'blogname',
array(
'settings' => array( 'blogname' ),
)
);
$this->assertTrue( $control->check_capabilities() );
$control = new WP_Customize_Control(
$this->wp_customize,
'blogname',
array(
'settings' => array( 'blogname', 'non_existing' ),
)
);
$this->assertFalse( $control->check_capabilities() );
$this->wp_customize->add_setting(
'top_secret_message',
array(
'capability' => 'top_secret_clearance',
)
);
$control = new WP_Customize_Control(
$this->wp_customize,
'blogname',
array(
'settings' => array( 'blogname', 'top_secret_clearance' ),
)
);
$this->assertFalse( $control->check_capabilities() );
$control = new WP_Customize_Control(
$this->wp_customize,
'no_setting',
array(
'settings' => array(),
)
);
$this->assertTrue( $control->check_capabilities() );
$control = new WP_Customize_Control(
$this->wp_customize,
'no_setting',
array(
'settings' => array(),
'capability' => 'top_secret_clearance',
)
);
$this->assertFalse( $control->check_capabilities() );
$control = new WP_Customize_Control(
$this->wp_customize,
'no_setting',
array(
'settings' => array(),
'capability' => 'edit_theme_options',
)
);
$this->assertTrue( $control->check_capabilities() );
}
/**
* @ticket 38164
*/
function test_dropdown_pages() {
do_action( 'customize_register', $this->wp_customize );
$this->assertInstanceOf( 'WP_Customize_Nav_Menus', $this->wp_customize->nav_menus );
$nav_menus_created_posts_setting = $this->wp_customize->get_setting( 'nav_menus_created_posts' );
$this->assertInstanceOf( 'WP_Customize_Filter_Setting', $nav_menus_created_posts_setting );
$page_on_front_control = $this->wp_customize->get_control( 'page_on_front' );
// Ensure the add-new-toggle is absent if allow_addition param is not set.
$page_on_front_control->allow_addition = false;
ob_start();
$page_on_front_control->maybe_render();
$content = ob_get_clean();
$this->assertNotContains( 'add-new-toggle', $content );
// Ensure the add-new-toggle is absent if allow_addition param is set.
$page_on_front_control->allow_addition = true;
ob_start();
$page_on_front_control->maybe_render();
$content = ob_get_clean();
$this->assertContains( 'add-new-toggle', $content );
// Ensure that dropdown-pages delect is rendered even if there are no pages published (yet).
foreach ( get_pages() as $page ) {
wp_delete_post( $page->ID );
}
$page_on_front_control->allow_addition = true;
ob_start();
$page_on_front_control->maybe_render();
$content = ob_get_clean();
$this->assertContains( '<option value="0">', $content, 'Dropdown-pages renders select even without any pages published.' );
// Ensure that auto-draft pages are included if they are among the nav_menus_created_posts.
$auto_draft_page_id = $this->factory()->post->create(
array(
'post_type' => 'page',
'post_status' => 'auto-draft',
'post_title' => 'Auto Draft Page',
)
);
$this->factory()->post->create(
array(
'post_type' => 'page',
'post_status' => 'auto-draft',
'post_title' => 'Orphan Auto Draft Page',
)
);
$auto_draft_post_id = $this->factory()->post->create(
array(
'post_type' => 'post',
'post_status' => 'auto-draft',
'post_title' => 'Auto Draft Post',
)
);
$this->wp_customize->set_post_value( $nav_menus_created_posts_setting->id, array( $auto_draft_page_id, $auto_draft_post_id ) );
$nav_menus_created_posts_setting->preview();
ob_start();
$page_on_front_control->maybe_render();
$content = ob_get_clean();
$this->assertContains( sprintf( '<option value="%d">Auto Draft Page</option>', $auto_draft_page_id ), $content );
$this->assertNotContains( 'Auto Draft Post', $content );
$this->assertNotContains( 'Orphan Auto Draft Page', $content );
}
/**
* Tear down.
*/
function tearDown() {
$this->wp_customize = null;
unset( $GLOBALS['wp_customize'] );
parent::tearDown();
}
}