Skip to content

Commit

Permalink
Move WP_Auth0_InitialSetup::init() actions to functions; clean up not…
Browse files Browse the repository at this point in the history
…ices
  • Loading branch information
joshcanhelp committed Dec 20, 2019
1 parent a39cc3e commit d9dfbb2
Show file tree
Hide file tree
Showing 8 changed files with 191 additions and 71 deletions.
56 changes: 51 additions & 5 deletions WP_Auth0.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@

define( 'WPA0_LANG', 'wp-auth0' ); // deprecated; do not use for translations

require_once WPA0_PLUGIN_DIR.'vendor/autoload.php';
require_once WPA0_PLUGIN_DIR.'functions.php';
require_once WPA0_PLUGIN_DIR . 'vendor/autoload.php';
require_once WPA0_PLUGIN_DIR . 'functions.php';

/*
* Localization
Expand Down Expand Up @@ -123,9 +123,6 @@ public function init() {

add_filter( 'plugin_action_links_' . $this->basename, [ $this, 'wp_add_plugin_settings_link' ] );

$initial_setup = new WP_Auth0_InitialSetup( $this->a0_options );
$initial_setup->init();

$this->router = new WP_Auth0_Routes( $this->a0_options );
}

Expand Down Expand Up @@ -477,6 +474,55 @@ function wp_auth0_db_check_update() {
* Core WP hooks
*/

function wp_auth0_setup_error_admin_notices() {
if ( empty( $_REQUEST['error'] ) ) {
return false;
}

$initial_setup = new WP_Auth0_InitialSetup( WP_Auth0_Options::Instance() );

switch ( $_REQUEST['error'] ) {

case 'cant_create_client':
$initial_setup->cant_create_client_message();
break;

case 'cant_create_client_grant':
$initial_setup->cant_create_client_grant_message();
break;

case 'cant_exchange_token':
$initial_setup->cant_exchange_token_message();
break;

case 'rejected':
$initial_setup->rejected_message();
break;

case 'access_denied':
$initial_setup->access_denied_message();
break;

default:
$initial_setup->notify_error();
}

return true;
}
add_action( 'admin_notices', 'wp_auth0_setup_error_admin_notices' );

function wp_auth0_setup_callback_step1() {
$setup_conn = new WP_Auth0_InitialSetup_ConnectionProfile( WP_Auth0_Options::Instance() );
$setup_conn->callback();
}
add_action( 'admin_action_wpauth0_callback_step1', 'wp_auth0_setup_callback_step1' );

function wp_auth0_setup_callback_step3_social() {
$setup_admin = new WP_Auth0_InitialSetup_AdminUser( WP_Auth0_Options::Instance() );
$setup_admin->callback();
}
add_action( 'admin_action_wpauth0_callback_step3_social', 'wp_auth0_setup_callback_step3_social' );

/**
* Function to call the method that clears out the error log.
*
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
"phpcbf": "\"vendor/bin/phpcbf\"",
"phpcbf-tests": "\"vendor/bin/phpcbf\" --standard=phpcs-test-ruleset.xml -s ./tests/",
"sniffs": "\"vendor/bin/phpcs\" -e",
"test": "\"vendor/bin/phpunit\" --coverage-text",
"test": "\"vendor/bin/phpunit\"",
"test-cov": "\"vendor/bin/phpunit\" --coverage-text",
"test-group": "\"vendor/bin/phpunit\" --coverage-text --group",
"test-ci": "\"vendor/bin/phpunit\" --coverage-clover=coverage.xml",
"pre-commit-no-tests": [ "@phpcbf", "@phpcbf-tests", "@phpcs-tests", "@compat", "@phpcs-i18n" ],
Expand Down
82 changes: 22 additions & 60 deletions lib/initial-setup/WP_Auth0_InitialSetup.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,45 +19,8 @@ public function __construct( WP_Auth0_Options $a0_options ) {
$this->end_step = new WP_Auth0_InitialSetup_End( $this->a0_options );
}

/**
* @deprecated - 3.10.0, will move add_action calls out of this class in the next major.
*
* @codeCoverageIgnore - Deprecated.
*/
public function init() {

add_action( 'admin_action_wpauth0_callback_step1', [ $this->connection_profile, 'callback' ] );
add_action( 'admin_action_wpauth0_callback_step3_social', [ $this->adminuser_step, 'callback' ] );

if ( isset( $_REQUEST['page'] ) && 'wpa0-setup' === $_REQUEST['page'] ) {
if ( isset( $_REQUEST['error'] ) ) {
add_action( 'admin_notices', [ $this, 'notify_error' ] );
}
}

if ( isset( $_REQUEST['error'] ) && 'cant_create_client' == $_REQUEST['error'] ) {
add_action( 'admin_notices', [ $this, 'cant_create_client_message' ] );
}

if ( isset( $_REQUEST['error'] ) && 'cant_create_client_grant' == $_REQUEST['error'] ) {
add_action( 'admin_notices', [ $this, 'cant_create_client_grant_message' ] );
}

if ( isset( $_REQUEST['error'] ) && 'cant_exchange_token' == $_REQUEST['error'] ) {
add_action( 'admin_notices', [ $this, 'cant_exchange_token_message' ] );
}

if ( isset( $_REQUEST['error'] ) && 'rejected' == $_REQUEST['error'] ) {
add_action( 'admin_notices', [ $this, 'rejected_message' ] );
}

if ( isset( $_REQUEST['error'] ) && 'access_denied' == $_REQUEST['error'] ) {
add_action( 'admin_notices', [ $this, 'access_denied' ] );
}
}

public function notify_error() {
printf( '<div class="notice notice-error">%s</div>', strip_tags( $_REQUEST['error'] ) );
printf( '<div class="notice notice-error"><p><strong>%s</strong></p></div>', strip_tags( $_REQUEST['error'] ) );
}

public function render_setup_page() {
Expand Down Expand Up @@ -103,14 +66,13 @@ public function render_setup_page() {

public function cant_create_client_message() {
?>
<div id="message" class="error">
<div class="notice notice-error">
<p>
<strong>
<?php echo __( 'There was an error creating the Auth0 App. Check the ', 'wp-auth0' ); ?>
<a target="_blank" href="<?php echo admin_url( 'admin.php?page=wpa0-errors' ); ?>"><?php echo __( 'Error log', 'wp-auth0' ); ?></a>
<?php echo __( ' for more information. If the problem persists, please create it manually in the ', 'wp-auth0' ); ?>
<a target="_blank" href="https://manage.auth0.com/#/applications"><?php echo __( 'Auth0 Dashboard', 'wp-auth0' ); ?></a>
<?php echo __( ' and copy the Client ID and Client Secret.', 'wp-auth0' ); ?>
<?php _e( 'There was an error creating the Auth0 App. Check the ', 'wp-auth0' ); ?>
<a target="_blank" href="<?php echo admin_url( 'admin.php?page=wpa0-errors' ); ?>"><?php _e( 'error log', 'wp-auth0' ); ?></a>
<?php _e( ' for more information. If the problem persists, please follow the ', 'wp-auth0' ); ?>
<a target="_blank" href="https://auth0.com/docs/cms/wordpress/installation#manual-setup"><?php _e( 'manual setup instructions', 'wp-auth0' ); ?></a>.
</strong>
</p>
</div>
Expand All @@ -119,21 +81,21 @@ public function cant_create_client_message() {

public function cant_create_client_grant_message() {
?>
<div id="message" class="error">
<div class="notice notice-error">
<p>
<strong>
<?php echo __( 'There was an error creating the necessary client grants. ', 'wp-auth0' ); ?>
<?php _e( 'There was an error creating the necessary client grants. ', 'wp-auth0' ); ?>
<?php
echo __(
_e(
'Go to your Auth0 dashboard > APIs > Auth0 Management API > Machine to Machine Applications tab and authorize this Application. ',
'wp-auth0'
);
?>
<?php echo __( 'Make sure to add the following scopes: ', 'wp-auth0' ); ?>
<?php _e( 'Make sure to add the following scopes: ', 'wp-auth0' ); ?>
<code><?php echo implode( '</code>, <code>', WP_Auth0_Api_Client::get_required_scopes() ); ?></code>
<?php echo __( 'You can also check the ', 'wp-auth0' ); ?>
<a target="_blank" href="<?php echo admin_url( 'admin.php?page=wpa0-errors' ); ?>"><?php echo __( 'Error log', 'wp-auth0' ); ?></a>
<?php echo __( ' for more information.', 'wp-auth0' ); ?>
<?php _e( 'You can also check the ', 'wp-auth0' ); ?>
<a target="_blank" href="<?php echo admin_url( 'admin.php?page=wpa0-errors' ); ?>"><?php _e( 'Error log', 'wp-auth0' ); ?></a>
<?php _e( ' for more information.', 'wp-auth0' ); ?>
</strong>
</p>
</div>
Expand All @@ -142,13 +104,13 @@ public function cant_create_client_grant_message() {

public function cant_exchange_token_message() {
?>
<div id="message" class="error">
<div class="notice notice-error">
<p>
<strong>
<?php echo __( 'There was an error retrieving your Auth0 credentials. Check the ', 'wp-auth0' ); ?>
<a target="_blank" href="<?php echo admin_url( 'admin.php?page=wpa0-errors' ); ?>"><?php echo __( 'Error log', 'wp-auth0' ); ?></a>
<?php echo __( ' for more information.', 'wp-auth0' ); ?>
<?php echo __( 'Please check that your server has internet access and can reach ', 'wp-auth0' ); ?>
<?php _e( 'There was an error retrieving your Auth0 credentials. Check the ', 'wp-auth0' ); ?>
<a target="_blank" href="<?php echo admin_url( 'admin.php?page=wpa0-errors' ); ?>"><?php _e( 'Error log', 'wp-auth0' ); ?></a>
<?php _e( ' for more information.', 'wp-auth0' ); ?>
<?php _e( 'Please check that your server has internet access and can reach ', 'wp-auth0' ); ?>
<code>https://<?php echo $this->a0_options->get( 'domain' ); ?></code>
</strong>
</p>
Expand All @@ -158,22 +120,22 @@ public function cant_exchange_token_message() {

public function rejected_message() {
?>
<div id="message" class="error">
<div class="notice notice-error">
<p>
<strong>
<?php echo __( 'The required scoped were rejected.', 'wp-auth0' ); ?>
<?php _e( 'The required scopes were rejected.', 'wp-auth0' ); ?>
</strong>
</p>
</div>
<?php
}

public function access_denied() {
public function access_denied_message() {
?>
<div class="notice notice-error">
<p>
<strong>
<?php echo __( 'Please create your Auth0 account first at ', 'wp-auth0' ); ?>
<?php _e( 'Please create your Auth0 account first at ', 'wp-auth0' ); ?>
<a href="https://manage.auth0.com">https://manage.auth0.com</a>
</strong>
</p>
Expand Down
4 changes: 2 additions & 2 deletions lib/initial-setup/WP_Auth0_InitialSetup_Consent.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public function consent_callback( $name ) {
$client_response = WP_Auth0_Api_Client::create_client( $domain, $this->access_token, $name );

if ( $client_response === false ) {
wp_redirect( admin_url( 'admin.php?page=wpa0&error=cant_create_client' ) );
wp_redirect( admin_url( 'admin.php?page=wpa0-setup&error=cant_create_client' ) );
exit;
}

Expand Down Expand Up @@ -192,7 +192,7 @@ public function consent_callback( $name ) {
$grant_response = WP_Auth0_Api_Client::create_client_grant( $this->access_token, $client_id );

if ( false === $grant_response ) {
wp_redirect( admin_url( 'admin.php?page=wpa0&error=cant_create_client_grant' ) );
wp_redirect( admin_url( 'admin.php?page=wpa0-setup&error=cant_create_client_grant' ) );
exit;
}

Expand Down
2 changes: 1 addition & 1 deletion templates/initial-setup/connection_profile.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@
<h4><?php _e( 'Manual Setup', 'wp-auth0' ); ?></h4>
<p><?php _e( 'If you already have an Application or want to use an existing database connection, please follow the steps below.', 'wp-auth0' ); ?></p>
<p><a class="a0-button primary" href="https://auth0.com/docs/cms/wordpress/installation#manual-setup"
target="_blank"><?php _e( 'Manual Setup Instructions', 'wp-auth0' ); ?></a></p>
target="_blank"><?php _e( 'manual Setup Instructions', 'wp-auth0' ); ?></a></p>
<br>
</div>
</div>
Expand Down
3 changes: 3 additions & 0 deletions tests/testErrorLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ class TestErrorLog extends WP_Auth0_Test_Case {

use RedirectHelpers;

use UsersHelper;

use WpDieHelper;

/**
Expand Down Expand Up @@ -268,6 +270,7 @@ public function testThatNonAdminStopsProcess() {

public function testThatErrorLogCanBeCleared() {
$this->startRedirectHalting();
$this->setGlobalUser();
$_POST['nonce'] = wp_create_nonce( 'clear_error_log' );
$error_log = new WP_Auth0_ErrorLog();
$error_log::insert_error( uniqid(), uniqid() );
Expand Down
108 changes: 108 additions & 0 deletions tests/testInitialSetup.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php
/**
* Contains Class TestInitialSetup.
*
* @package WP-Auth0
*
* @since 4.0.0
*/

/**
* Class TestInitialSetup.
*/
class TestInitialSetup extends WP_Auth0_Test_Case {

use HookHelpers;

public function testThatClearAdminActionFunctionsAreHooked() {
$expect_hooked = [
'wp_auth0_setup_callback_step3_social' => [
'priority' => 10,
'accepted_args' => 1,
],
];
$this->assertHookedFunction( 'admin_action_wpauth0_callback_step3_social', $expect_hooked );

$expect_hooked = [
'wp_auth0_setup_callback_step1' => [
'priority' => 10,
'accepted_args' => 1,
],
];
$this->assertHookedFunction( 'admin_action_wpauth0_callback_step1', $expect_hooked );

$expect_hooked = [
'wp_auth0_setup_error_admin_notices' => [
'priority' => 10,
'accepted_args' => 1,
],
];
$this->assertHookedFunction( 'admin_notices', $expect_hooked );
}

public function testThatNoErrorReturnsFalseWithNoOutput() {
ob_start();
$this->assertFalse( wp_auth0_setup_error_admin_notices() );
$this->assertEmpty( ob_get_clean() );
}

public function testThatCantCreateClientHasCorrectNotice() {
$_REQUEST['error'] = 'cant_create_client';
ob_start();
$this->assertTrue( wp_auth0_setup_error_admin_notices() );
$notice_html = ob_get_clean();

$this->assertContains( '<div class="notice notice-error">', $notice_html );
$this->assertContains( 'There was an error creating the Auth0 App', $notice_html );
}

public function testThatCantCreateGrantHasCorrectNotice() {
$_REQUEST['error'] = 'cant_create_client_grant';
ob_start();
$this->assertTrue( wp_auth0_setup_error_admin_notices() );
$notice_html = ob_get_clean();

$this->assertContains( '<div class="notice notice-error">', $notice_html );
$this->assertContains( 'There was an error creating the necessary client grants', $notice_html );
}

public function testThatCantExchangeTokenHasCorrectNotice() {
$_REQUEST['error'] = 'cant_exchange_token';
ob_start();
$this->assertTrue( wp_auth0_setup_error_admin_notices() );
$notice_html = ob_get_clean();

$this->assertContains( '<div class="notice notice-error">', $notice_html );
$this->assertContains( 'There was an error retrieving your Auth0 credentials', $notice_html );
}

public function testThatRejectedHasCorrectNotice() {
$_REQUEST['error'] = 'rejected';
ob_start();
$this->assertTrue( wp_auth0_setup_error_admin_notices() );
$notice_html = ob_get_clean();

$this->assertContains( '<div class="notice notice-error">', $notice_html );
$this->assertContains( 'The required scopes were rejected', $notice_html );
}

public function testThatAccessDeniedHasCorrectNotice() {
$_REQUEST['error'] = 'access_denied';
ob_start();
$this->assertTrue( wp_auth0_setup_error_admin_notices() );
$notice_html = ob_get_clean();

$this->assertContains( '<div class="notice notice-error">', $notice_html );
$this->assertContains( 'Please create your Auth0 account first', $notice_html );
}

public function testThatUnknownErrorHasCorrectNotice() {
$_REQUEST['error'] = '__test_unknown_error__';
ob_start();
$this->assertTrue( wp_auth0_setup_error_admin_notices() );
$notice_html = ob_get_clean();

$this->assertContains( '<div class="notice notice-error">', $notice_html );
$this->assertContains( '__test_unknown_error__', $notice_html );
}
}
4 changes: 2 additions & 2 deletions tests/testInitialSetupConsent.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public function testThatClientCreationFailureIsRedirected() {
$redirect_url = parse_url( $caught_redirect['location'] );

$this->assertEquals( '/wp-admin/admin.php', $redirect_url['path'] );
$this->assertContains( 'page=wpa0', $redirect_url['query'] );
$this->assertContains( 'page=wpa0-setup', $redirect_url['query'] );
$this->assertContains( 'error=cant_create_client', $redirect_url['query'] );

$this->assertCount( 1, self::$error_log->get() );
Expand Down Expand Up @@ -192,7 +192,7 @@ public function testThatNewConnectionIsCreatedAndFailedClientGrantRedirects() {
$redirect_url = parse_url( $caught_redirect['location'] );

$this->assertEquals( '/wp-admin/admin.php', $redirect_url['path'] );
$this->assertContains( 'page=wpa0', $redirect_url['query'] );
$this->assertContains( 'page=wpa0-setup', $redirect_url['query'] );
$this->assertContains( 'error=cant_create_client_grant', $redirect_url['query'] );

$this->assertEquals( 'TEST_CLIENT_ID', self::$opts->get( 'client_id' ) );
Expand Down

0 comments on commit d9dfbb2

Please sign in to comment.