Skip to content

Commit

Permalink
Add tests for error log clear action
Browse files Browse the repository at this point in the history
  • Loading branch information
joshcanhelp committed Dec 18, 2019
1 parent 864d7b2 commit 2735a7d
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 14 deletions.
28 changes: 14 additions & 14 deletions lib/WP_Auth0_ErrorLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,20 @@ public function delete() {
return delete_option( self::OPTION_NAME );
}

/**
* Update the error log with an array and enforcing the length limit.
*
* @param array $log - Log array to update.
*
* @return bool
*/
private function update( array $log ) {
if ( count( $log ) > self::ERROR_LOG_ENTRY_LIMIT ) {
array_pop( $log );
}
return update_option( self::OPTION_NAME, $log );
}

/**
* Create a row in the error log.
*
Expand Down Expand Up @@ -133,18 +147,4 @@ public static function insert_error( $section, $error ) {

return ( new self )->add( $new_entry );
}

/**
* Update the error log with an array and enforcing the length limit.
*
* @param array $log - Log array to update.
*
* @return bool
*/
private function update( array $log ) {
if ( count( $log ) > self::ERROR_LOG_ENTRY_LIMIT ) {
array_pop( $log );
}
return update_option( self::OPTION_NAME, $log );
}
}
76 changes: 76 additions & 0 deletions tests/testErrorLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@
*/
class TestErrorLog extends WP_Auth0_Test_Case {

use HookHelpers;

use RedirectHelpers;

use WpDieHelper;

/**
* Test log entry section.
*/
Expand Down Expand Up @@ -42,6 +48,19 @@ public static function setUpBeforeClass() {
];
}

/**
* Test that the error log option name did not change.
*/
public function testThatClearAdminActionFunctionIsHooked() {
$expect_hooked = [
'wp_auth0_errorlog_clear_error_log' => [
'priority' => 10,
'accepted_args' => 1,
],
];
$this->assertHookedFunction( 'admin_action_wpauth0_clear_error_log', $expect_hooked );
}

/**
* Test that the error log option name did not change.
*/
Expand Down Expand Up @@ -209,4 +228,61 @@ public function testLogDelete() {

$this->assertFalse( get_option( WP_Auth0_ErrorLog::OPTION_NAME ) );
}

public function testThatBadNonceStopsProcess() {
$this->startWpDieHalting();
$error_log = new WP_Auth0_ErrorLog();
$error_log::insert_error( uniqid(), uniqid() );

$this->assertCount( 1, $error_log->get() );

try {
wp_auth0_errorlog_clear_error_log();
$caught = 'Nothing caught';
} catch ( \Exception $e ) {
$caught = $e->getMessage();
}

$this->assertEquals( 'Not allowed.', $caught );
$this->assertCount( 1, $error_log->get() );
}

public function testThatNonAdminStopsProcess() {
$this->startWpDieHalting();
$_POST['nonce'] = wp_create_nonce( 'clear_error_log' );
$error_log = new WP_Auth0_ErrorLog();
$error_log::insert_error( uniqid(), uniqid() );

$this->assertCount( 1, $error_log->get() );

try {
wp_auth0_errorlog_clear_error_log();
$caught = 'Nothing caught';
} catch ( \Exception $e ) {
$caught = $e->getMessage();
}

$this->assertEquals( 'Not authorized.', $caught );
$this->assertCount( 1, $error_log->get() );
}

public function testThatErrorLogCanBeCleared() {
$this->startRedirectHalting();
$_POST['nonce'] = wp_create_nonce( 'clear_error_log' );
$error_log = new WP_Auth0_ErrorLog();
$error_log::insert_error( uniqid(), uniqid() );

$this->assertCount( 1, $error_log->get() );

try {
wp_auth0_errorlog_clear_error_log();
$caught = [ 'Nothing caught' ];
} catch ( \Exception $e ) {
$caught = unserialize( $e->getMessage() );
}

$this->assertEquals( 'http://example.org/wp-admin/admin.php?page=wpa0-errors&cleared=1', $caught['location'] );
$this->assertEquals( 302, $caught['status'] );
$this->assertEmpty( $error_log->get() );
}
}

0 comments on commit 2735a7d

Please # to comment.