Skip to content

Commit

Permalink
Add tests for Themes_Screens::redirect_to_theme_install() (#226)
Browse files Browse the repository at this point in the history
  • Loading branch information
costdev authored Dec 1, 2024
1 parent fb7314e commit f1f7ed5
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 2 deletions.
4 changes: 2 additions & 2 deletions includes/class-themes-screens.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public function admin_enqueue_scripts( $hook ) {
public function redirect_to_theme_install() {

$nonce = isset( $_REQUEST['_wpnonce'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['_wpnonce'] ) ) : false;
if ( $nonce && ! wp_verify_nonce( $nonce, 'query-themes' ) ) {
if ( ! $nonce || ! wp_verify_nonce( $nonce, 'query-themes' ) ) {
return;
}

Expand All @@ -105,7 +105,7 @@ public function redirect_to_theme_install() {
$admin_settings = Admin_Settings::get_instance();
if ( $admin_settings->get_setting( 'enable', false ) ) {
wp_safe_redirect( admin_url( 'theme-install.php' ) );
exit;
! defined( 'AP_RUN_TESTS' ) && exit;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
<?php
/**
* Class ThemesScreens_RedirectToThemeInstallTest
*
* @package AspireUpdate
*/

/**
* Tests for Themes_Screens::redirect_to_theme_install()
*
* These tests cause constants to be defined and also occur after headers are sent.
* They must run in separate processes and must not preserve global state.
*
* @runTestsInSeparateProcesses
* @preserveGlobalState disabled
*
* @covers \AspireUpdate\Themes_Screens::redirect_to_theme_install
*/
class ThemesScreens_RedirectToThemeInstallTest extends WP_UnitTestCase {
/**
* Test that a redirect is not performed when $_REQUEST['_wpnonce'] is not set.
*/
public function test_should_not_redirect_when_request_wpnonce_is_not_set() {
unset( $_REQUEST['_wpnonce'] );
$_GET['browse'] = 'favorites';
define( 'AP_ENABLE', true );

$redirect = new MockAction();
add_filter( 'wp_redirect', [ $redirect, 'filter' ] );

$themes_screens = new AspireUpdate\Themes_Screens();
$themes_screens->redirect_to_theme_install();

$this->assertSame( 0, $redirect->get_call_count() );
}

/**
* Test that a redirect is not performed when nonce verification fails.
*/
public function test_should_not_redirect_when_nonce_verification_fails() {
$_REQUEST['_wpnonce'] = 'incorrect_value';
$_GET['browse'] = 'favorites';
define( 'AP_ENABLE', true );

$redirect = new MockAction();
add_filter( 'wp_redirect', [ $redirect, 'filter' ] );

$themes_screens = new AspireUpdate\Themes_Screens();
$themes_screens->redirect_to_theme_install();

$this->assertSame( 0, $redirect->get_call_count() );
}

/**
* Test that a redirect is not performed when not viewing an unsupported filter.
*/
public function test_should_not_redirect_when_not_viewing_an_unsupported_filter() {
$_REQUEST['_wpnonce'] = wp_create_nonce( 'query-themes' );
$_GET['browse'] = 'some-filter';
define( 'AP_ENABLE', true );

$redirect = new MockAction();
add_filter( 'wp_redirect', [ $redirect, 'filter' ] );

$themes_screens = new AspireUpdate\Themes_Screens();
$themes_screens->redirect_to_theme_install();

$this->assertSame( 0, $redirect->get_call_count() );
}

/**
* Test that a redirect is not performed when AP_ENABLE is false.
*/
public function test_should_not_redirect_when_ap_enable_is_false() {
$_REQUEST['_wpnonce'] = wp_create_nonce( 'query-themes' );
$_GET['browse'] = 'favorites';
define( 'AP_ENABLE', false );

$redirect = new MockAction();
add_filter( 'wp_redirect', [ $redirect, 'filter' ] );

$themes_screens = new AspireUpdate\Themes_Screens();
$themes_screens->redirect_to_theme_install();

$this->assertSame( 0, $redirect->get_call_count() );
}

/**
* Test that a redirect is performed when viewing an unsupported filter.
*
* @dataProvider data_unsupported_filters
*
* @param string $filter The unsupported filter.
*/
public function test_should_redirect_when_viewing_an_unsupported_filter( $filter ) {
$_REQUEST['_wpnonce'] = wp_create_nonce( 'query-themes' );
$_GET['browse'] = $filter;
define( 'AP_ENABLE', true );

$redirect = new MockAction();
add_filter( 'wp_redirect', [ $redirect, 'filter' ] );

$themes_screens = new AspireUpdate\Themes_Screens();
$themes_screens->redirect_to_theme_install();

$this->assertSame( 1, $redirect->get_call_count() );
}

/**
* Data provider.
*
* @return array[]
*/
public function data_unsupported_filters() {
return self::text_array_to_dataprovider(
[
'favorites',
]
);
}
}

0 comments on commit f1f7ed5

Please sign in to comment.