-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from brianvarskonst/feature/fix-tests
Feature/Rewrite Tests
- Loading branch information
Showing
25 changed files
with
584 additions
and
1,005 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Bvsk\WordPress\NonceManager\Tests\Integration; | ||
|
||
use Bvsk\WordPress\NonceManager\NonceManager; | ||
use Bvsk\WordPress\NonceManager\Nonces\Factory\DefaultNonceProperties; | ||
use Bvsk\WordPress\NonceManager\Nonces\FieldNonce; | ||
use Bvsk\WordPress\NonceManager\Nonces\Nonce; | ||
use Bvsk\WordPress\NonceManager\Nonces\SimpleNonce; | ||
use Bvsk\WordPress\NonceManager\Nonces\UrlNonce; | ||
use Bvsk\WordPress\NonceManager\Tests\Stubs\FooBarNonceFactory; | ||
use Bvsk\WordPress\NonceManager\Tests\Stubs\FooBarVerifier; | ||
use Bvsk\WordPress\NonceManager\Tests\UnitTestCase; | ||
|
||
use function Brain\Monkey\Functions\expect; | ||
|
||
class NonceManagerTest extends UnitTestCase | ||
{ | ||
/** | ||
* @dataProvider defaultDataProvider | ||
*/ | ||
public function testCreateStubInstance(string $token): void | ||
{ | ||
$this->assertInstanceOf( | ||
NonceManager::class, | ||
$this->buildTesteeWithStubs($token) | ||
); | ||
} | ||
|
||
public function testCreateInstanceFromDefaults(): void | ||
{ | ||
$this->assertInstanceOf( | ||
NonceManager::class, | ||
$this->buildTestee() | ||
); | ||
} | ||
|
||
/** | ||
* @dataProvider defaultDataProvider | ||
*/ | ||
public function testCreateSimpleNonceWithNonceManager(string $token): void | ||
{ | ||
$testee = $this->buildTestee(); | ||
|
||
expect('add_filter')->once(); | ||
expect('apply_filters')->once()->andReturn(DefaultNonceProperties::LIFETIME); | ||
expect('wp_create_nonce')->once()->andReturn($token); | ||
|
||
$nonce = $testee->createNonce(SimpleNonce::class); | ||
$this->assertInstanceOf(SimpleNonce::class, $nonce); | ||
|
||
$this->assertSame($nonce->getToken(), $token); | ||
$this->assertSame($nonce->getLifetime(), DefaultNonceProperties::LIFETIME); | ||
$this->assertSame($nonce->getAction(), DefaultNonceProperties::ACTION); | ||
$this->assertSame($nonce->getRequestName(), DefaultNonceProperties::REQUEST_NAME); | ||
} | ||
|
||
/** | ||
* @dataProvider fieldNonceDataProvider | ||
*/ | ||
public function testCreateFieldNonceWithNonceManager(string $token, string $hiddenInput): void | ||
{ | ||
$testee = $this->buildTestee(); | ||
|
||
expect('add_filter')->once(); | ||
expect('apply_filters')->once()->andReturn(DefaultNonceProperties::LIFETIME); | ||
expect('wp_create_nonce')->once()->andReturn($token); | ||
expect('wp_nonce_field')->once()->andReturn($hiddenInput); | ||
|
||
$nonce = $testee->createNonce(FieldNonce::class, ['referer' => false]); | ||
$this->assertInstanceOf(FieldNonce::class, $nonce); | ||
|
||
$this->assertSame($hiddenInput, $nonce->getField()); | ||
$this->assertSame($token, $nonce->getToken()); | ||
$this->assertSame(DefaultNonceProperties::LIFETIME, $nonce->getLifetime()); | ||
$this->assertSame(DefaultNonceProperties::ACTION, $nonce->getAction()); | ||
$this->assertSame(DefaultNonceProperties::REQUEST_NAME, $nonce->getRequestName()); | ||
} | ||
|
||
/** | ||
* @dataProvider urlNonceDataProvider | ||
*/ | ||
public function testCreateUrlNonceWithNonceManager(string $token, string $url): void | ||
{ | ||
$testee = $this->buildTestee(); | ||
|
||
expect('add_filter')->once(); | ||
expect('apply_filters')->once()->andReturn(DefaultNonceProperties::LIFETIME); | ||
expect('wp_create_nonce')->once()->andReturn($token); | ||
|
||
$nonce = $testee->createNonce(UrlNonce::class, ['url' => $url]); | ||
$this->assertInstanceOf(UrlNonce::class, $nonce); | ||
|
||
$this->assertSame($token, $nonce->getToken()); | ||
$this->assertSame(DefaultNonceProperties::LIFETIME, $nonce->getLifetime()); | ||
$this->assertSame(DefaultNonceProperties::ACTION, $nonce->getAction()); | ||
$this->assertSame(DefaultNonceProperties::REQUEST_NAME, $nonce->getRequestName()); | ||
$this->assertSame($url, $nonce->getUrl()); | ||
} | ||
|
||
/** | ||
* @dataProvider defaultDataProvider | ||
*/ | ||
public function testVerifyNonceWithNonceManager(string $token): void | ||
{ | ||
$testee = $this->buildTestee(); | ||
|
||
expect('add_filter')->once(); | ||
expect('apply_filters')->once()->andReturn(DefaultNonceProperties::LIFETIME); | ||
expect('wp_create_nonce')->once()->andReturn($token); | ||
|
||
$nonce = $testee->createNonce(SimpleNonce::class); | ||
$this->assertInstanceOf(SimpleNonce::class, $nonce); | ||
|
||
expect('wp_verify_nonce')->once()->andReturn(true); | ||
|
||
$this->assertTrue($testee->verify($nonce)); | ||
} | ||
|
||
public function defaultDataProvider(): iterable | ||
{ | ||
yield 'test' => [ | ||
'token' => 'fooBar', | ||
]; | ||
} | ||
|
||
public function fieldNonceDataProvider(): iterable | ||
{ | ||
yield 'test' => [ | ||
'token' => 'fooBar', | ||
'hiddenInput' => '<input type="hidden" />', | ||
]; | ||
} | ||
|
||
public function urlNonceDataProvider(): iterable | ||
{ | ||
yield 'test' => [ | ||
'token' => 'fooBar', | ||
'url' => 'https://example.com', | ||
]; | ||
} | ||
|
||
private function buildTestee(): NonceManager | ||
{ | ||
return NonceManager::createFromDefaults(); | ||
} | ||
|
||
private function buildTesteeWithStubs(string $fooBar): NonceManager | ||
{ | ||
return new NonceManager( | ||
new FooBarNonceFactory($fooBar), | ||
new FooBarVerifier( | ||
fn(Nonce $nonce): bool => true, | ||
$fooBar | ||
) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Bvsk\WordPress\NonceManager\Tests\Integration; | ||
|
||
use Bvsk\WordPress\NonceManager\Nonces\Factory\FieldNonceFactory; | ||
use Bvsk\WordPress\NonceManager\Nonces\Factory\SimpleNonceFactory; | ||
use Bvsk\WordPress\NonceManager\Nonces\Factory\UrlNonceFactory; | ||
use Bvsk\WordPress\NonceManager\Nonces\FieldNonce; | ||
use Bvsk\WordPress\NonceManager\Nonces\Nonce; | ||
use Bvsk\WordPress\NonceManager\Nonces\UrlNonce; | ||
use Bvsk\WordPress\NonceManager\Tests\UnitTestCase; | ||
use Bvsk\WordPress\NonceManager\Verification\NonceVerifier; | ||
use Bvsk\WordPress\NonceManager\Verification\Verifier; | ||
use InvalidArgumentException; | ||
|
||
use function Brain\Monkey\Functions\expect; | ||
|
||
class NonceVerifierTest extends UnitTestCase | ||
{ | ||
public function testCreateNonceVerifier(): void | ||
{ | ||
$this->assertInstanceOf( | ||
Verifier::class, | ||
$this->buildTestee() | ||
); | ||
} | ||
|
||
/** | ||
* @dataProvider defaultDataProvider | ||
*/ | ||
public function testSuccessfulNonceVerifier(Nonce $nonce): void | ||
{ | ||
$verifier = $this->buildTestee(); | ||
|
||
expect('wp_verify_nonce')->once()->andReturn(true); | ||
|
||
if ($nonce instanceof FieldNonce) { | ||
expect('check_ajax_referer')->once()->andReturn(true); | ||
} | ||
|
||
if ($nonce instanceof UrlNonce) { | ||
expect('check_admin_referer')->once()->andReturn(true); | ||
} | ||
|
||
$this->assertTrue($verifier->verify($nonce)); | ||
} | ||
|
||
/** | ||
* @dataProvider defaultDataProvider | ||
*/ | ||
public function testFailingNonceVerifier(Nonce $nonce): void | ||
{ | ||
$verifier = $this->buildTestee(); | ||
|
||
expect('wp_verify_nonce')->once()->andReturn(false); | ||
|
||
if ($nonce instanceof UrlNonce) { | ||
expect('check_admin_referer')->once()->andReturn(false); | ||
|
||
$this->expectException(InvalidArgumentException::class); | ||
$this->expectDeprecationMessage('Invalid UrlNonce was provided.'); | ||
} | ||
|
||
if ($nonce instanceof FieldNonce) { | ||
expect('check_ajax_referer')->once()->andReturn(false); | ||
|
||
$this->expectException(InvalidArgumentException::class); | ||
$this->expectDeprecationMessage('Invalid FieldNonce was provided.'); | ||
} | ||
|
||
$this->assertFalse($verifier->verify($nonce)); | ||
} | ||
|
||
public function defaultDataProvider(): iterable | ||
{ | ||
expect('add_filter')->times(3); | ||
expect('wp_create_nonce')->times(3)->andReturn('fooBar'); | ||
|
||
yield 'testSimpleNonce' => [ | ||
'nonce' => (new SimpleNonceFactory())->create('simple'), | ||
]; | ||
|
||
yield 'testFieldNonce' => [ | ||
'nonce' => (new FieldNonceFactory())->create('field', ['referer' => false]), | ||
]; | ||
|
||
yield 'testUrlNonce' => [ | ||
'nonce' => (new UrlNonceFactory())->create('url', ['url' => 'https://www.example.com']), | ||
]; | ||
} | ||
|
||
private function buildTestee(): NonceVerifier | ||
{ | ||
return new NonceVerifier(); | ||
} | ||
} |
Oops, something went wrong.