62 lines
2.4 KiB
PHP
62 lines
2.4 KiB
PHP
<?php
|
|
declare( strict_types=1 );
|
|
|
|
namespace {
|
|
if ( ! function_exists( 'get_option' ) ) {
|
|
function get_option( $k, $d = false ) { return array_key_exists( $k, $GLOBALS['sg_test_opts'] ) ? $GLOBALS['sg_test_opts'][ $k ] : $d; }
|
|
}
|
|
if ( ! function_exists( 'update_option' ) ) {
|
|
function update_option( $k, $v, $a = null ) { $GLOBALS['sg_test_opts'][ $k ] = $v; $GLOBALS['sg_test_writes'] = ( $GLOBALS['sg_test_writes'] ?? 0 ) + 1; return true; }
|
|
}
|
|
if ( ! function_exists( 'current_user_can' ) ) {
|
|
function current_user_can( $cap ) { return (bool) ( $GLOBALS['sg_test_can'] ?? false ); }
|
|
}
|
|
}
|
|
|
|
namespace StreamGoals\Tests\Unit {
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
use StreamGoals\Core\Settings;
|
|
use StreamGoals\Rest\OverlayController;
|
|
|
|
/** L5: the overlay token is never created by an unauthenticated request. */
|
|
final class OverlayTokenTest extends TestCase {
|
|
|
|
protected function setUp(): void {
|
|
$GLOBALS['sg_test_opts'] = array();
|
|
$GLOBALS['sg_test_writes'] = 0;
|
|
$GLOBALS['sg_test_can'] = false;
|
|
}
|
|
|
|
public function test_public_verify_with_no_token_is_false_and_writes_nothing(): void {
|
|
$this->assertFalse( OverlayController::verify_token( 'anything' ) );
|
|
$this->assertFalse( OverlayController::verify_token( '' ) );
|
|
$this->assertSame( 0, $GLOBALS['sg_test_writes'], 'unauthenticated request must not write' );
|
|
$this->assertSame( '', OverlayController::current_token() );
|
|
}
|
|
|
|
public function test_ensure_token_refuses_without_capability(): void {
|
|
$this->assertSame( '', OverlayController::ensure_token() );
|
|
$this->assertSame( 0, $GLOBALS['sg_test_writes'] );
|
|
}
|
|
|
|
public function test_admin_generates_once_and_public_compare_then_works(): void {
|
|
$GLOBALS['sg_test_can'] = true;
|
|
$t = OverlayController::ensure_token();
|
|
$this->assertSame( 32, strlen( $t ) );
|
|
$this->assertSame( $t, OverlayController::ensure_token(), 'stable' );
|
|
$this->assertSame( 1, $GLOBALS['sg_test_writes'] );
|
|
$GLOBALS['sg_test_can'] = false;
|
|
$this->assertTrue( OverlayController::verify_token( $t ) );
|
|
$this->assertFalse( OverlayController::verify_token( str_repeat( 'a', 32 ) ) );
|
|
}
|
|
|
|
public function test_activation_helper_generates_when_missing_and_keeps_existing(): void {
|
|
$t = Settings::ensure_overlay_token();
|
|
$this->assertSame( 32, strlen( $t ) );
|
|
$this->assertSame( $t, Settings::ensure_overlay_token() );
|
|
$this->assertSame( 1, $GLOBALS['sg_test_writes'] );
|
|
}
|
|
}
|
|
}
|