From f71f097994bdd62a31ce8daee40156c45efeca55 Mon Sep 17 00:00:00 2001 From: brianvarskonst Date: Sun, 26 Jun 2022 14:21:30 +0000 Subject: [PATCH 01/11] Remove outdated Tests --- tests/Unit/FieldTypeTest.php | 126 ----------- tests/Unit/NonceAbstractTest.php | 116 ---------- tests/Unit/NonceManagerBuildTest.php | 147 ------------- tests/Unit/NonceManagerConfigurationTest.php | 98 --------- tests/Unit/NonceManagerCreateTest.php | 118 ---------- tests/Unit/NonceTest.php | 95 --------- tests/Unit/UrlTypeTest.php | 103 --------- tests/Unit/VerifierTest.php | 213 ------------------- 8 files changed, 1016 deletions(-) delete mode 100755 tests/Unit/FieldTypeTest.php delete mode 100644 tests/Unit/NonceAbstractTest.php delete mode 100644 tests/Unit/NonceManagerBuildTest.php delete mode 100755 tests/Unit/NonceManagerConfigurationTest.php delete mode 100644 tests/Unit/NonceManagerCreateTest.php delete mode 100755 tests/Unit/NonceTest.php delete mode 100755 tests/Unit/UrlTypeTest.php delete mode 100755 tests/Unit/VerifierTest.php diff --git a/tests/Unit/FieldTypeTest.php b/tests/Unit/FieldTypeTest.php deleted file mode 100755 index 9a6ee55..0000000 --- a/tests/Unit/FieldTypeTest.php +++ /dev/null @@ -1,126 +0,0 @@ -alias('sha1'); - - // we mock wp_nonce_field. - Functions::expect('wp_nonce_field')->andReturnUsing(function ($action, $request_name, $referer, $echo) { - $string = $action . $request_name; - if ($referer) { - $string .= 'referer'; - } - - // Should never be true, since we call wp_nonce_field with $echo false always and do echo by ourselfs. - if ($echo) { - $string .= 'echo'; - } - - return $string; - }); - - // we mock wp_kses. - Functions::expect('wp_kses')->andReturnUsing(function ($string, $array) { - return $string; - }); - - $this->action = 'action'; - $this->request = 'request'; - $this->lifetime = 213; - $this->configuration = new Configuration($this->action, $this->request, $this->lifetime); - } - - /** - * Test Field generation & output - * - * @covers \NoncesManager\Nonces\Types\FieldType::generate - */ - public function testGenerateNonceField(): void - { - $create = new FieldType($this->configuration); - $create->generate(); - $field = $create->getField(); - - self::assertSame($field, $this->action . $this->request); - - $field = $create->generate(true); - self::assertSame($field, $this->action . $this->request . 'referer'); - - // Test echo. - ob_start(); - $create->generate(false, true); - $field = $create->getField(); - $echo_output = ob_get_contents(); - ob_end_clean(); - - self::assertSame($echo_output, $this->action . $this->request . 'echo'); - self::assertSame($echo_output, $field); - } - - /** - * Test FieldType set & get - * - * @covers \NoncesManager\Nonces\Types\UrlType::SetUrl - * @covers \NoncesManager\Nonces\Types\UrlType::GetUrl - */ - public function testSetNewNonceField(): void { - $create = new FieldType($this->configuration); - $create->generate(); - $field = $create->getField(); - - // Check if is setted and in util class get back the reflected class - $setUrl = PHPUnitUtility::callSetterMethod($create, 'setField', $this->action . $this->request); - $getSettedFieldValue = PHPUnitUtility::callGetterMethod($setUrl, $create, 'getField'); - - // $create->setUrl('abc'); - self::assertSame($getSettedFieldValue, $field); - } -} diff --git a/tests/Unit/NonceAbstractTest.php b/tests/Unit/NonceAbstractTest.php deleted file mode 100644 index f9ddf13..0000000 --- a/tests/Unit/NonceAbstractTest.php +++ /dev/null @@ -1,116 +0,0 @@ -alias('sha1'); - - $this->action = 'action'; - $this->request = 'request'; - $this->lifetime = 213; - $this->configuration = new BaseConfiguration($this->action, $this->request, $this->lifetime); - - // Get mock, without the constructor being called - $this->nonce = new BaseNonce($this->configuration); - } - - /** - * Test Nonce is an instance of NonceAbstract Class - * TODO: Maybe make a better Test - */ - public function testNonceAbstractClassNonceInstance(): void { - $this->assertInstanceOf(NonceAbstract::class, $this->nonce); - } - - /** - * Test Create setted & get an Nonce - * - * @covers \NoncesManager\Nonces\NonceAbstract::setNonce - * @covers \NoncesManager\Nonces\NonceAbstract::getNonce - */ - public function testNonceAbstractClassNonce(): void { - $create = new Nonce($this->configuration); - - $this->assertSame($create->getNonce(), $this->nonce->getNonce()); - } - - /** - * Test setted and get Nonce Lifetime - * - * @covers \NoncesManager\Nonces\NonceAbstract::setLifetime - * @covers \NoncesManager\Nonces\NonceAbstract::getLifetime - */ - public function testNonceAbstractClassNonceLifetime(): void { - //Handle ticks - $lifetime = $this->lifetime * 2; - - $this->assertSame($lifetime, $this->nonce->getLifetime()); - } - - /** - * Test setted and get Nonce Action - * - * @covers \NoncesManager\Nonces\NonceAbstract::setAction - * @covers \NoncesManager\Nonces\NonceAbstract::getAction - */ - public function testNonceAbstractClassNonceAction(): void { - $this->assertSame($this->action, $this->nonce->getAction()); - } - - /** - * Test setted and get Nonce RequestName - * - * @covers \NoncesManager\Nonces\NonceAbstract::setRequestName - * @covers \NoncesManager\Nonces\NonceAbstract::getRequestName - */ - public function testNonceAbstractClassNonceRequestName(): void { - $this->assertSame($this->request, $this->nonce->getRequestName()); - } -} \ No newline at end of file diff --git a/tests/Unit/NonceManagerBuildTest.php b/tests/Unit/NonceManagerBuildTest.php deleted file mode 100644 index ef41922..0000000 --- a/tests/Unit/NonceManagerBuildTest.php +++ /dev/null @@ -1,147 +0,0 @@ -action = 'action'; - $this->request = 'request'; - $this->lifetime = 213; - - // The filter should be added once. - Filters::expectAdded('nonce_life')->once(); - - $this->configuration = new BaseConfiguration($this->action, $this->request, $this->lifetime); - - $this->nonceManager = NonceManager::build($this->configuration); - } - - /** - * Test create a new Nonce Manager through the static create method and inject the required Dependencies - * - * @covers \NoncesManager\NonceManager::build - */ - public function testBuildedNonceManager(): void - { - self::assertInstanceOf(NonceManager::class, $this->nonceManager); - } - - /** - * Test the builded Configuration - * - * @covers \NoncesManager\NonceManager::Configuration - */ - public function testGetConfiguration(): void - { - self::assertInstanceOf(BaseConfiguration::class, $this->nonceManager->Configuration()); - } - - /** - * Test the builded Nonce - * - * @covers \NoncesManager\NonceManager::Nonce - */ - public function testGetNonce(): void - { - self::assertInstanceOf(Nonce::class, $this->nonceManager->Nonce()); - } - - /** - * Test the builded FieldType - * - * @covers \NoncesManager\NonceManager::Field - */ - public function testGetField(): void - { - self::assertInstanceOf(FieldType::class, $this->nonceManager->Field()); - } - - /** - * Test the builded UrlType - * - * @covers \NoncesManager\NonceManager::Url - */ - public function testGetUrl(): void - { - self::assertInstanceOf(UrlType::class, $this->nonceManager->Url()); - } - - /** - * Test the builded Verifier - * - * @covers \NoncesManager\NonceManager::Verifier - */ - public function testGetVerifier(): void - { - self::assertInstanceOf(NonceVerifier::class, $this->nonceManager->Verifier()); - } -} \ No newline at end of file diff --git a/tests/Unit/NonceManagerConfigurationTest.php b/tests/Unit/NonceManagerConfigurationTest.php deleted file mode 100755 index cb9c5c1..0000000 --- a/tests/Unit/NonceManagerConfigurationTest.php +++ /dev/null @@ -1,98 +0,0 @@ -action = 'action'; - $this->request = 'request'; - } - - /** - * Check if NonceConfig stores the data correctly. - * - * @covers \NoncesManager\Configuration::getAction - * @covers \NoncesManager\Configuration::getRequestName - * @covers \NoncesManager\Configuration::getLifetime - * - * @covers \NoncesManager\Configuration::nonceLifetime - */ - public function testCreateConfiguration(): void - { - $this->lifetime = 213; - - // The filter should be added once. - Filters::expectAdded('nonce_life')->once(); - - $this->configuration = new BaseConfiguration($this->action, $this->request, $this->lifetime); - - self::assertSame($this->configuration->getAction(), $this->action); - self::assertSame($this->configuration->getRequestName(), $this->request); - - // Not the same as setted, because 1 tick is not one second - self::assertNotSame($this->configuration->getLifetime(), $this->lifetime); - - // Handle Tick Time Interval - $lifetime = $this->lifetime * 2; - - // Check if nonceLifetime returns the right value. - self::assertSame($this->configuration->nonceLifetime(DAY_IN_SECONDS), $lifetime); - } - - /** - * Check if filter is not added, when lifetime is not set. - **/ - public function testNoFilterAdded(): void - { - $this->lifetime = null; - - // The filter should be added once. - Filters::expectAdded('nonce_life')->never(); - - $this->configuration = new BaseConfiguration($this->action, $this->request, $this->lifetime); - - self::assertNotSame($this->configuration->nonceLifetime(DAY_IN_SECONDS), $this->lifetime); - } -} diff --git a/tests/Unit/NonceManagerCreateTest.php b/tests/Unit/NonceManagerCreateTest.php deleted file mode 100644 index 93da5be..0000000 --- a/tests/Unit/NonceManagerCreateTest.php +++ /dev/null @@ -1,118 +0,0 @@ -action = 'action'; - $this->request = 'request'; - $this->lifetime = 213; - - // The filter should be added once. - Filters::expectAdded('nonce_life')->once(); - - $this->configuration = new Configuration($this->action, $this->request, $this->lifetime); - - $this->fieldType = new FieldType($this->configuration); - $this->urlType = new UrlType($this->configuration); - $this->verifier = new NonceVerifier($this->configuration); - } - - /** - * Test create a new Nonce Manager through the static create method and inject the required Dependencies - * - * @covers \NoncesManager\NonceManager::create - */ - public function testCreatedNonceManager(): void { - $this->nonceManager = NonceManager::create($this->configuration, $this->fieldType, $this->urlType, $this->verifier); - - self::assertInstanceOf(NonceManager::class, $this->nonceManager); - } - - /** - * Test the created Nonce Manager and test the inject Dependencies - * - * @covers \NoncesManager\NonceManager::Configuration - * @covers \NoncesManager\NonceManager::Nonce - * @covers \NoncesManager\NonceManager::Field - * @covers \NoncesManager\NonceManager::Url - * @covers \NoncesManager\NonceManager::Verifier - */ - public function testServicesAreInjected(): void { - $this->nonceManager = NonceManager::create($this->configuration, $this->fieldType, $this->urlType, $this->verifier); - - self::assertInstanceOf(Configuration::class, $this->nonceManager->Configuration()); - self::assertInstanceOf(Nonce::class, $this->nonceManager->Nonce()); - self::assertInstanceOf(FieldType::class, $this->nonceManager->Field()); - self::assertInstanceOf(UrlNonce::class, $this->nonceManager->Url()); - self::assertInstanceOf(Verifier::class, $this->nonceManager->Verifier()); - } -} \ No newline at end of file diff --git a/tests/Unit/NonceTest.php b/tests/Unit/NonceTest.php deleted file mode 100755 index 07ad016..0000000 --- a/tests/Unit/NonceTest.php +++ /dev/null @@ -1,95 +0,0 @@ -alias('sha1'); - - $this->action = 'action'; - $this->request = 'request'; - $this->lifetime = 213; - $this->configuration = new BaseConfiguration($this->action, $this->request, $this->lifetime); - } - - /** - * Test Create new Nonce & get the new Nonce - * - * @covers \NoncesManager\Nonces\Nonce::create - * @covers \NoncesManager\Nonces\Nonce::getAction - */ - public function testCreateNonce(): void - { - $create = new Nonce($this->configuration); - $create->create(); - $nonce = $create->getNonce(); - - // Check if nonce is stored correctly. - self::assertSame($nonce, $create->getNonce()); - } - - /** - * Test the new created Nonce Magic Method __toString - * - * @covers \NoncesManager\Nonces\Nonce::__toString - */ - public function testMagicMethodToString() { - $create = new Nonce($this->configuration); - $create->create(); - $nonceToString = (string) $create; - - $nonce = $create->getNonce(); - - self::assertSame($nonceToString, $nonce); - } -} diff --git a/tests/Unit/UrlTypeTest.php b/tests/Unit/UrlTypeTest.php deleted file mode 100755 index fa56485..0000000 --- a/tests/Unit/UrlTypeTest.php +++ /dev/null @@ -1,103 +0,0 @@ -alias('sha1'); - - // We mock wp_nonce_url. - Functions::expect('wp_nonce_url')->andReturnUsing(function ($url, $action, $request_name) { - return $url . $action . $request_name; - }); - - $this->action = 'action'; - $this->request = 'request'; - $this->lifetime = 213; - $this->configuration = new Configuration($this->action, $this->request, $this->lifetime); - } - - /** - * Test URL generation - * - * @covers \NoncesManager\Nonces\Types\UrlType::generate - */ - public function testCreateNonceURL(): void - { - $create = new UrlType($this->configuration); - $url = 'http://example.com/'; - $url_with_nonce = $create->generate($url); - - self::assertSame($url_with_nonce, $url . $this->action . $this->request); - } - - /** - * Test UrlType set & get - * - * @covers \NoncesManager\Nonces\Types\UrlType::SetUrl - * @covers \NoncesManager\Nonces\Types\UrlType::GetUrl - */ - public function testSetNewNonceUrl(): void { - $create = new UrlType($this->configuration); - $url = 'http://example.com/'; - $url_with_nonce = $create->generate($url); - - self::assertSame($url_with_nonce, $create->getUrl()); - - // Check if is setted and in util class get back the reflected class - $setUrl = PHPUnitUtility::callSetterMethod($create, 'setUrl', 'abc'); - $getSettedUrlValue = PHPUnitUtility::callGetterMethod($setUrl, $create, 'getUrl'); - - // $create->setUrl('abc'); - self::assertSame($getSettedUrlValue, 'abc'); - } -} diff --git a/tests/Unit/VerifierTest.php b/tests/Unit/VerifierTest.php deleted file mode 100755 index 4d9fde8..0000000 --- a/tests/Unit/VerifierTest.php +++ /dev/null @@ -1,213 +0,0 @@ -alias('sha1'); - - // we mock wp_verify_nonce. - Functions::expect('wp_verify_nonce')->andReturnUsing(function ($nonce, $action) { - return sha1($action) === $nonce; - }); - - // we mock wp_unslash. - Functions::expect('wp_unslash')->andReturnUsing(function ($string) { - return $string; - }); - - // we mock sanitize_text_field. - Functions::expect('sanitize_text_field')->andReturnUsing(function ($string) { - return $string; - }); - - $this->action = 'action'; - $this->request = 'request'; - $this->lifetime = 213; - $this->configuration = new Configuration($this->action, $this->request, $this->lifetime); - } - - /** - * Check validation - * - * @covers \NoncesManager\Nonces\Verification\Verifier::verify - */ - public function testValidity(): void - { - $create = new Nonce($this->configuration); - $create->create(); - $nonce = $create->getNonce(); - - $verify = new Verifier($this->configuration); - $valid = $verify->verify($nonce); - - // Check if nonce is valid. - self::assertTrue($valid); - - // Check if nonce is not valid. - $not_valid = $verify->verify('not-valid' . $nonce); - self::assertFalse($not_valid); - - // Check auto-nonce assignment. - $_REQUEST[$this->request] = $nonce; - $verify = new NonceVerifier($this->configuration); - $valid = $verify->verify(); - self::assertTrue($valid); - } - - /** - * Test Get Age - * - * @covers \NoncesManager\Nonces\Verification\Verifier::getAge - */ - public function testAge(): void - { - self::markTestSkipped('wp_verify_nonce() needs a better mockup to test this functionality.'); - - $create = new Nonce($this->configuration); - $create->create(); - - $verify = new NonceVerifier($this->configuration); - $age = $verify->getAge(); - - self::assertSame(1, $age); - } - - /** - * Test check Admin Referer valid - * TODO: Make better Test - * - * @covers \NoncesManager\Nonces\Verification\Verifier::isAdminReferer - */ - public function testCheckAdminRefererValid(): void { - $create = new Nonce($this->configuration); - $create->create(); - - // mock check_admin_referer success - Functions::expect('check_admin_referer') - ->once() - ->with($this->action, $this->request) - ->andReturn(true); - - $verify = new Verifier($this->configuration); - $valid = $verify->isAdminReferer(); - - // Check if nonce is valid. - self::assertTrue($valid); - } - - /** - * Test check Admin Referer invalid - * TODO: Make better Test - * - * @covers \NoncesManager\Nonces\Verification\Verifier::isAdminReferer - */ - public function testCheckAdminRefererInvalid(): void { - $create = new Nonce($this->configuration); - $create->create(); - - // mock check_admin_referer success - Functions::expect('check_admin_referer') - ->once() - ->with($this->action, $this->request) - ->andReturn(false); - - $verify = new Verifier($this->configuration); - $valid = $verify->isAdminReferer(); - - // Check if nonce is invalid. - self::assertFalse($valid); - } - - /** - * Test check Ajax Referer valid - * TODO: Make better Test - * - * @covers \NoncesManager\Nonces\Verification\Verifier::isAjaxReferer - */ - public function testCheckAjaxRefererValid(): void { - $create = new Nonce($this->configuration); - $create->create(); - - // mock check_admin_referer success - Functions::expect('check_ajax_referer') - ->once() - ->with($this->action, $this->request, false) - ->andReturn(true); - - $verify = new Verifier($this->configuration); - $valid = $verify->isAjaxReferer(); - - // Check if nonce is valid. - self::assertTrue($valid); - } - - /** - * Test check Ajax Referer valid - * TODO: Make better Test - * - * @covers \NoncesManager\Nonces\Verification\Verifier::isAjaxReferer - */ - public function testCheckAjaxRefererInvalid(): void { - $create = new Nonce($this->configuration); - $create->create(); - - // mock check_admin_referer success - Functions::expect('check_ajax_referer') - ->once() - ->with($this->action, $this->request, false) - ->andReturn(false); - - $verify = new Verifier($this->configuration); - $valid = $verify->isAjaxReferer(); - - // Check if nonce is valid. - self::assertFalse($valid); - } -} From b05de30e014b0b024fff6b9ef4f250c2325fe4b9 Mon Sep 17 00:00:00 2001 From: brianvarskonst Date: Sun, 26 Jun 2022 19:07:24 +0000 Subject: [PATCH 02/11] Add NonceManager Tests --- src/NonceManager.php | 2 +- src/Nonces/Factory/DefaultNonceProperties.php | 2 +- src/Nonces/FieldNonce.php | 4 + tests/Unit/NonceManagerTest.php | 90 +++++++++++++++++++ 4 files changed, 96 insertions(+), 2 deletions(-) create mode 100644 tests/Unit/NonceManagerTest.php diff --git a/src/NonceManager.php b/src/NonceManager.php index bb0bb41..b2571e7 100644 --- a/src/NonceManager.php +++ b/src/NonceManager.php @@ -40,7 +40,7 @@ public static function createFromDefaults(): NonceManager ); } - public function createNonce(string $type, array $data): Nonce + public function createNonce(string $type, array $data = []): Nonce { return $this->nonceFactory->create($type, $data); } diff --git a/src/Nonces/Factory/DefaultNonceProperties.php b/src/Nonces/Factory/DefaultNonceProperties.php index 5cef5c4..463a4c1 100644 --- a/src/Nonces/Factory/DefaultNonceProperties.php +++ b/src/Nonces/Factory/DefaultNonceProperties.php @@ -8,5 +8,5 @@ interface DefaultNonceProperties public const REQUEST_NAME = '_wpnonce'; - public const LIFETIME = (int) DAY_IN_SECONDS; + public const LIFETIME = DAY_IN_SECONDS; } diff --git a/src/Nonces/FieldNonce.php b/src/Nonces/FieldNonce.php index 971c432..d60e987 100644 --- a/src/Nonces/FieldNonce.php +++ b/src/Nonces/FieldNonce.php @@ -63,6 +63,10 @@ public function render(): void public function getField(): string { + if ($this->field === null) { + $this->generate(); + } + return $this->field; } } diff --git a/tests/Unit/NonceManagerTest.php b/tests/Unit/NonceManagerTest.php new file mode 100644 index 0000000..fef1d37 --- /dev/null +++ b/tests/Unit/NonceManagerTest.php @@ -0,0 +1,90 @@ +buildTestee(); + + $this->assertInstanceOf(NonceManager::class, $testee); + } + + public function testCreateSimpleNonceWithNonceManager(): void + { + $testee = $this->buildTestee(); + + $token = 'fooBar'; + + 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); + } + + public function testCreateFieldNonceWithNonceManager(): void + { + $testee = $this->buildTestee(); + + $token = 'fooBar'; + $hiddenInput = ''; + + 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()); + } + + public function testCreateUrlNonceWithNonceManager(): void + { + $testee = $this->buildTestee(); + + $token = 'fooBar'; + $url = 'https://example.com'; + + 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()); + } + + private function buildTestee(): NonceManager + { + return NonceManager::createFromDefaults(); + } +} \ No newline at end of file From d15606803ba2bbc6274d138457c241d85fdb01cb Mon Sep 17 00:00:00 2001 From: brianvarskonst Date: Sun, 26 Jun 2022 19:09:44 +0000 Subject: [PATCH 03/11] Add NonceManager Verifier Test --- tests/Unit/NonceManagerTest.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/tests/Unit/NonceManagerTest.php b/tests/Unit/NonceManagerTest.php index fef1d37..a15c3ed 100644 --- a/tests/Unit/NonceManagerTest.php +++ b/tests/Unit/NonceManagerTest.php @@ -83,6 +83,24 @@ public function testCreateUrlNonceWithNonceManager(): void $this->assertSame($url, $nonce->getUrl()); } + public function testVerifyNonceWithNonceManager(): void + { + $testee = $this->buildTestee(); + + $token = 'fooBar'; + + 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)); + } + private function buildTestee(): NonceManager { return NonceManager::createFromDefaults(); From 0b9081a63e16a3904f33a3ba35075c1953cefe80 Mon Sep 17 00:00:00 2001 From: brianvarskonst Date: Sun, 26 Jun 2022 19:16:19 +0000 Subject: [PATCH 04/11] Add creational Testee Tests --- tests/Unit/NonceManagerTest.php | 71 ++++++++++++++++++++++++++++++++- 1 file changed, 70 insertions(+), 1 deletion(-) diff --git a/tests/Unit/NonceManagerTest.php b/tests/Unit/NonceManagerTest.php index a15c3ed..3535cc1 100644 --- a/tests/Unit/NonceManagerTest.php +++ b/tests/Unit/NonceManagerTest.php @@ -6,15 +6,25 @@ use Bvsk\WordPress\NonceManager\NonceManager; use Bvsk\WordPress\NonceManager\Nonces\Factory\DefaultNonceProperties; +use Bvsk\WordPress\NonceManager\Nonces\Factory\NonceFactory; 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\UnitTestCase; +use Bvsk\WordPress\NonceManager\Verification\Verifier; use function Brain\Monkey\Functions\expect; class NonceManagerTest extends UnitTestCase { - public function testCreateNonceManager(): void + public function testCreateNewInstanceOfNonceManagerWithDependencyInjection(): void + { + $testee = $this->buildTesteeWithAnonymousDependencies(); + + $this->assertInstanceOf(NonceManager::class, $testee); + } + + public function testCreateNewInstanceOfNonceManagerFromDefaults(): void { $testee = $this->buildTestee(); @@ -105,4 +115,63 @@ private function buildTestee(): NonceManager { return NonceManager::createFromDefaults(); } + + private function buildTesteeWithAnonymousDependencies(): NonceManager + { + return new NonceManager( + new class implements NonceFactory { + + public function accepts(string $type, array $data): bool + { + return true; + } + + public function getSupportedType(): string + { + return 'FooBar'; + } + + public function create(string $type, array $data = []): Nonce + { + return new class implements Nonce { + + public function getAction(): string + { + return 'foo'; + } + + public function getLifetime(bool $actualLifetime = true): int + { + return 1; + } + + public function getRequestName(): string + { + return 'bar'; + } + + public function getToken(): string + { + return 'fooBar'; + } + }; + } + }, + new class implements Verifier { + + public function verify(Nonce $nonce): bool + { + return true; + } + + public function getAge(Nonce $nonce): string + { + return '1'; + } + + public function renderMessageHasExpired(Nonce $nonce): void + {} + } + ); + } } \ No newline at end of file From 2b33f5af7f8eecbf2edbec3a042ca1ae5c0d459e Mon Sep 17 00:00:00 2001 From: brianvarskonst Date: Tue, 5 Jul 2022 18:09:06 +0000 Subject: [PATCH 05/11] Add Stub unit tests and integration tests --- src/Nonces/Factory/UrlNonceFactory.php | 1 - .../NonceManagerTest.php | 82 ++++-------------- tests/Integration/NonceVerifierTest.php | 12 +++ tests/Unit/NonceFactoryTest.php | 57 ++++++++++++ tests/Unit/NonceTest.php | 54 ++++++++++++ tests/Unit/NonceVerifierTest.php | 62 +++++++++++++ tests/src/Stubs/FooBarNonce.php | 86 +++++++++++++++++++ tests/src/Stubs/FooBarNonceFactory.php | 33 +++++++ tests/src/Stubs/FooBarVerifier.php | 39 +++++++++ 9 files changed, 359 insertions(+), 67 deletions(-) rename tests/{Unit => Integration}/NonceManagerTest.php (62%) create mode 100644 tests/Integration/NonceVerifierTest.php create mode 100644 tests/Unit/NonceFactoryTest.php create mode 100644 tests/Unit/NonceTest.php create mode 100644 tests/Unit/NonceVerifierTest.php create mode 100644 tests/src/Stubs/FooBarNonce.php create mode 100644 tests/src/Stubs/FooBarNonceFactory.php create mode 100644 tests/src/Stubs/FooBarVerifier.php diff --git a/src/Nonces/Factory/UrlNonceFactory.php b/src/Nonces/Factory/UrlNonceFactory.php index b66cf00..25f4186 100644 --- a/src/Nonces/Factory/UrlNonceFactory.php +++ b/src/Nonces/Factory/UrlNonceFactory.php @@ -25,7 +25,6 @@ public function getSupportedType(): string public function create(string $type, array $data = []): Nonce { - return new UrlNonce( $data['url'], $data['action'] ?? DefaultNonceProperties::ACTION, diff --git a/tests/Unit/NonceManagerTest.php b/tests/Integration/NonceManagerTest.php similarity index 62% rename from tests/Unit/NonceManagerTest.php rename to tests/Integration/NonceManagerTest.php index 3535cc1..6f70ead 100644 --- a/tests/Unit/NonceManagerTest.php +++ b/tests/Integration/NonceManagerTest.php @@ -2,33 +2,34 @@ declare(strict_types=1); -namespace Bvsk\WordPress\NonceManager\Tests\Unit; +namespace Bvsk\WordPress\NonceManager\Tests\Integration; use Bvsk\WordPress\NonceManager\NonceManager; use Bvsk\WordPress\NonceManager\Nonces\Factory\DefaultNonceProperties; -use Bvsk\WordPress\NonceManager\Nonces\Factory\NonceFactory; 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 Bvsk\WordPress\NonceManager\Verification\Verifier; use function Brain\Monkey\Functions\expect; class NonceManagerTest extends UnitTestCase { - public function testCreateNewInstanceOfNonceManagerWithDependencyInjection(): void + public function testCreateStubInstance(): void { - $testee = $this->buildTesteeWithAnonymousDependencies(); - - $this->assertInstanceOf(NonceManager::class, $testee); + $this->assertInstanceOf( + NonceManager::class, + $this->buildTesteeWithStubs() + ); } - public function testCreateNewInstanceOfNonceManagerFromDefaults(): void + public function testCreateInstanceFromDefaults(): void { - $testee = $this->buildTestee(); - - $this->assertInstanceOf(NonceManager::class, $testee); + $this->assertInstanceOf( + NonceManager::class, + $this->buildTestee() + ); } public function testCreateSimpleNonceWithNonceManager(): void @@ -116,62 +117,11 @@ private function buildTestee(): NonceManager return NonceManager::createFromDefaults(); } - private function buildTesteeWithAnonymousDependencies(): NonceManager + private function buildTesteeWithStubs(): NonceManager { return new NonceManager( - new class implements NonceFactory { - - public function accepts(string $type, array $data): bool - { - return true; - } - - public function getSupportedType(): string - { - return 'FooBar'; - } - - public function create(string $type, array $data = []): Nonce - { - return new class implements Nonce { - - public function getAction(): string - { - return 'foo'; - } - - public function getLifetime(bool $actualLifetime = true): int - { - return 1; - } - - public function getRequestName(): string - { - return 'bar'; - } - - public function getToken(): string - { - return 'fooBar'; - } - }; - } - }, - new class implements Verifier { - - public function verify(Nonce $nonce): bool - { - return true; - } - - public function getAge(Nonce $nonce): string - { - return '1'; - } - - public function renderMessageHasExpired(Nonce $nonce): void - {} - } + new FooBarNonceFactory(), + new FooBarVerifier() ); } } \ No newline at end of file diff --git a/tests/Integration/NonceVerifierTest.php b/tests/Integration/NonceVerifierTest.php new file mode 100644 index 0000000..c19baad --- /dev/null +++ b/tests/Integration/NonceVerifierTest.php @@ -0,0 +1,12 @@ +assertInstanceOf( + NonceFactory::class, + $this->buildTestee($type) + ); + } + + /** + * @dataProvider defaultDataProvider + */ + public function testStubInstance(string $type): void + { + $nonceFactory = $this->buildTestee($type); + + $this->assertTrue($nonceFactory->accepts($type, [])); + $this->assertSame($type, $nonceFactory->getSupportedType()); + + $nonce = $nonceFactory->create($type, []); + + $this->assertInstanceOf(Nonce::class, $nonce); + $this->assertInstanceOf(FooBarNonce::class, $nonce); + } + + public function defaultDataProvider(): iterable + { + yield 'test' => [ + 'type' => 'fooBar' + ]; + + yield 'test2' => [ + 'type' => 'fizzBuzz' + ]; + } + + private function buildTestee(string $type): FooBarNonceFactory + { + return new FooBarNonceFactory($type); + } +} \ No newline at end of file diff --git a/tests/Unit/NonceTest.php b/tests/Unit/NonceTest.php new file mode 100644 index 0000000..24b8c19 --- /dev/null +++ b/tests/Unit/NonceTest.php @@ -0,0 +1,54 @@ +assertInstanceOf( + Nonce::class, + $this->buildTestee() + ); + } + + /** + * @dataProvider defaultDataProvider + */ + public function testGetterOfStubInstance( + string $action, + int $lifetime, + int $lifetimeManipulated, + string $requestName, + string $token + ): void { + + $nonce = $this->buildTestee(); + + $this->assertSame($action, $nonce->getAction()); + $this->assertSame($lifetime, $nonce->getLifetime()); + $this->assertSame($requestName, $nonce->getRequestName()); + $this->assertSame($token, $nonce->getToken()); + + $this->assertNotSame($lifetime, $nonce->getLifetime(false)); + + $this->assertSame($lifetimeManipulated, $nonce->getLifetime(false)); + } + + public function defaultDataProvider(): iterable + { + yield 'test' => FooBarNonce::getDefaults(); + } + + private function buildTestee(): Nonce + { + return FooBarNonce::createFromDefaults(); + } +} \ No newline at end of file diff --git a/tests/Unit/NonceVerifierTest.php b/tests/Unit/NonceVerifierTest.php new file mode 100644 index 0000000..d059547 --- /dev/null +++ b/tests/Unit/NonceVerifierTest.php @@ -0,0 +1,62 @@ +assertInstanceOf( + Verifier::class, + $this->buildtestee($callback, $age) + ); + } + + /** + * @dataProvider defaultDataProvider + */ + public function testStubInstance( + callable $callback, + string $age, + Nonce $nonce + ): void { + $verifier = $this->buildtestee($callback, $age); + + $this->assertTrue($verifier->verify($nonce)); + $this->assertSame($age, $verifier->getAge($nonce)); + + $this->expectOutputString($age); + $verifier->renderMessageHasExpired($nonce); + } + + public function defaultDataProvider(): iterable + { + yield 'test' => [ + 'callback' => static fn(Nonce $nonce): bool => true, + 'age' => 'fooBar', + 'nonce' => FooBarNonce::createFromDefaults(), + ]; + } + + private function buildtestee( + callable $callback, + string $age + ): FooBarVerifier { + + return new FooBarVerifier($callback, $age); + } +} \ No newline at end of file diff --git a/tests/src/Stubs/FooBarNonce.php b/tests/src/Stubs/FooBarNonce.php new file mode 100644 index 0000000..8d00eeb --- /dev/null +++ b/tests/src/Stubs/FooBarNonce.php @@ -0,0 +1,86 @@ +action = $action; + $this->lifetime = $lifetime; + $this->lifetimeManipulated = $lifetimeManipulated; + $this->requestName = $requestName; + $this->token = $token; + } + + public static function createFromDefaults(): Nonce + { + $data = self::getDefaults(); + + return new self( + $data['action'], + $data['lifetime'], + $data['lifetimeManipulated'], + $data['requestName'], + $data['token'] + ); + } + + public static function getDefaults(): array + { + $fooBar = 'fooBar'; + $lifetime = 1; + + return [ + 'action' => $fooBar, + 'lifetime' => $lifetime, + 'lifetimeManipulated' => $lifetime * 2, + 'requestName' => $fooBar, + 'token' => $fooBar + ]; + } + + public function getAction(): string + { + return $this->action; + } + + public function getLifetime(bool $actualLifetime = true): int + { + if (!$actualLifetime) { + return $this->lifetimeManipulated; + } + + return $this->lifetime; + } + + public function getRequestName(): string + { + return $this->requestName; + } + + public function getToken(): string + { + return $this->token; + } +} \ No newline at end of file diff --git a/tests/src/Stubs/FooBarNonceFactory.php b/tests/src/Stubs/FooBarNonceFactory.php new file mode 100644 index 0000000..9a0add2 --- /dev/null +++ b/tests/src/Stubs/FooBarNonceFactory.php @@ -0,0 +1,33 @@ +type = $type; + } + + public function accepts(string $type, array $data): bool + { + return $this->type === $type; + } + + public function getSupportedType(): string + { + return $this->type; + } + + public function create(string $type, array $data = []): Nonce + { + return FooBarNonce::createFromDefaults(); + } +} \ No newline at end of file diff --git a/tests/src/Stubs/FooBarVerifier.php b/tests/src/Stubs/FooBarVerifier.php new file mode 100644 index 0000000..3d6b594 --- /dev/null +++ b/tests/src/Stubs/FooBarVerifier.php @@ -0,0 +1,39 @@ +callback = $callback; + $this->age = $age; + } + + public function verify(Nonce $nonce): bool + { + return ($this->callback)($nonce); + } + + public function getAge(Nonce $nonce): string + { + return $this->age; + } + + public function renderMessageHasExpired(Nonce $nonce): void + { + echo $this->age; + } +} \ No newline at end of file From f191b259f4fdb714577fe8553218ddeb3247a075 Mon Sep 17 00:00:00 2001 From: brianvarskonst Date: Tue, 5 Jul 2022 18:28:54 +0000 Subject: [PATCH 06/11] Add Nonce verifier Integration Test --- tests/Integration/NonceVerifierTest.php | 97 +++++++++++++++++++++++++ 1 file changed, 97 insertions(+) diff --git a/tests/Integration/NonceVerifierTest.php b/tests/Integration/NonceVerifierTest.php index c19baad..c48bbda 100644 --- a/tests/Integration/NonceVerifierTest.php +++ b/tests/Integration/NonceVerifierTest.php @@ -4,9 +4,106 @@ namespace Bvsk\WordPress\NonceManager\Tests\Integration; +use Bvsk\WordPress\NonceManager\Nonces\Factory\DefaultNonceProperties; +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 function Brain\Monkey\Functions\expect; +use InvalidArgumentException; class NonceVerifierTest extends UnitTestCase { + public function testCreateNonceVerifier(): void + { + $this->assertInstanceOf( + Verifier::class, + $this->buildTestee() + ); + } + + /** + * @dataProvider defaultDataProvider + */ + public function testSuccessfulNonceVerifier(string $token, Nonce $nonce): void + { + $verifier = $this->buildTestee(); + + expect('add_filter')->once(); + expect('apply_filters')->once()->andReturn(DefaultNonceProperties::LIFETIME); + expect('wp_create_nonce')->once()->andReturn($token); + + 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(string $token, Nonce $nonce): void + { + $verifier = $this->buildTestee(); + + expect('add_filter')->once(); + expect('apply_filters')->once()->andReturn(DefaultNonceProperties::LIFETIME); + expect('wp_create_nonce')->once()->andReturn($token); + + 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 + { + $token = 'fooBar'; + + yield 'testSimpleNonce' => [ + 'token' => $token, + 'nonce' => (new SimpleNonceFactory())->create('simple') + ]; + + yield 'testFieldNonce' => [ + 'token' => $token, + 'nonce' => (new FieldNonceFactory())->create('field', ['referer' => false]) + ]; + + yield 'testUrlNonce' => [ + 'token' => $token, + 'nonce' => (new UrlNonceFactory())->create('url', ['url' => 'https://www.example.com']) + ]; + } + + private function buildTestee(): NonceVerifier + { + return new NonceVerifier(); + } } \ No newline at end of file From a2116d16fd20887f0cadee6352aa8307ccc5f425 Mon Sep 17 00:00:00 2001 From: brianvarskonst Date: Tue, 5 Jul 2022 18:38:39 +0000 Subject: [PATCH 07/11] include Integration Tests and fix them --- phpunit.xml.dist | 6 +++++- tests/Integration/NonceManagerTest.php | 10 ++++++++-- tests/Integration/NonceVerifierTest.php | 11 +++-------- 3 files changed, 16 insertions(+), 11 deletions(-) diff --git a/phpunit.xml.dist b/phpunit.xml.dist index e17727e..615c8a0 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -14,7 +14,11 @@ - + + tests/Integration/ + + + tests/Unit/ diff --git a/tests/Integration/NonceManagerTest.php b/tests/Integration/NonceManagerTest.php index 6f70ead..6be60aa 100644 --- a/tests/Integration/NonceManagerTest.php +++ b/tests/Integration/NonceManagerTest.php @@ -7,6 +7,7 @@ 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; @@ -119,9 +120,14 @@ private function buildTestee(): NonceManager private function buildTesteeWithStubs(): NonceManager { + $foobar = 'fooBar'; + return new NonceManager( - new FooBarNonceFactory(), - new FooBarVerifier() + new FooBarNonceFactory($foobar), + new FooBarVerifier( + fn(Nonce $nonce): bool => true, + $foobar + ) ); } } \ No newline at end of file diff --git a/tests/Integration/NonceVerifierTest.php b/tests/Integration/NonceVerifierTest.php index c48bbda..43bd9c5 100644 --- a/tests/Integration/NonceVerifierTest.php +++ b/tests/Integration/NonceVerifierTest.php @@ -35,10 +35,6 @@ public function testSuccessfulNonceVerifier(string $token, Nonce $nonce): void { $verifier = $this->buildTestee(); - expect('add_filter')->once(); - expect('apply_filters')->once()->andReturn(DefaultNonceProperties::LIFETIME); - expect('wp_create_nonce')->once()->andReturn($token); - expect('wp_verify_nonce')->once()->andReturn(true); if ($nonce instanceof FieldNonce) { @@ -59,10 +55,6 @@ public function testFailingNonceVerifier(string $token, Nonce $nonce): void { $verifier = $this->buildTestee(); - expect('add_filter')->once(); - expect('apply_filters')->once()->andReturn(DefaultNonceProperties::LIFETIME); - expect('wp_create_nonce')->once()->andReturn($token); - expect('wp_verify_nonce')->once()->andReturn(false); if ($nonce instanceof UrlNonce) { @@ -86,6 +78,9 @@ public function defaultDataProvider(): iterable { $token = 'fooBar'; + expect('add_filter')->times(3); + expect('wp_create_nonce')->times(3)->andReturn($token); + yield 'testSimpleNonce' => [ 'token' => $token, 'nonce' => (new SimpleNonceFactory())->create('simple') From c4b190e2f0eb13499d3734b41a39366b8d1c3de4 Mon Sep 17 00:00:00 2001 From: brianvarskonst Date: Tue, 5 Jul 2022 18:42:32 +0000 Subject: [PATCH 08/11] Fix CS on test code --- tests/Integration/NonceManagerTest.php | 3 ++- tests/Integration/NonceVerifierTest.php | 11 ++++++----- tests/Unit/NonceFactoryTest.php | 6 +++--- tests/Unit/NonceTest.php | 3 +-- tests/Unit/NonceVerifierTest.php | 3 ++- tests/src/Stubs/FooBarNonce.php | 4 ++-- tests/src/Stubs/FooBarNonceFactory.php | 2 +- tests/src/Stubs/FooBarVerifier.php | 2 +- tests/src/UnitTestCase.php | 1 - 9 files changed, 18 insertions(+), 17 deletions(-) diff --git a/tests/Integration/NonceManagerTest.php b/tests/Integration/NonceManagerTest.php index 6be60aa..c147816 100644 --- a/tests/Integration/NonceManagerTest.php +++ b/tests/Integration/NonceManagerTest.php @@ -13,6 +13,7 @@ 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 @@ -130,4 +131,4 @@ private function buildTesteeWithStubs(): NonceManager ) ); } -} \ No newline at end of file +} diff --git a/tests/Integration/NonceVerifierTest.php b/tests/Integration/NonceVerifierTest.php index 43bd9c5..5d6eaf3 100644 --- a/tests/Integration/NonceVerifierTest.php +++ b/tests/Integration/NonceVerifierTest.php @@ -14,12 +14,13 @@ use Bvsk\WordPress\NonceManager\Tests\UnitTestCase; use Bvsk\WordPress\NonceManager\Verification\NonceVerifier; use Bvsk\WordPress\NonceManager\Verification\Verifier; + use function Brain\Monkey\Functions\expect; + use InvalidArgumentException; class NonceVerifierTest extends UnitTestCase { - public function testCreateNonceVerifier(): void { $this->assertInstanceOf( @@ -83,17 +84,17 @@ public function defaultDataProvider(): iterable yield 'testSimpleNonce' => [ 'token' => $token, - 'nonce' => (new SimpleNonceFactory())->create('simple') + 'nonce' => (new SimpleNonceFactory())->create('simple'), ]; yield 'testFieldNonce' => [ 'token' => $token, - 'nonce' => (new FieldNonceFactory())->create('field', ['referer' => false]) + 'nonce' => (new FieldNonceFactory())->create('field', ['referer' => false]), ]; yield 'testUrlNonce' => [ 'token' => $token, - 'nonce' => (new UrlNonceFactory())->create('url', ['url' => 'https://www.example.com']) + 'nonce' => (new UrlNonceFactory())->create('url', ['url' => 'https://www.example.com']), ]; } @@ -101,4 +102,4 @@ private function buildTestee(): NonceVerifier { return new NonceVerifier(); } -} \ No newline at end of file +} diff --git a/tests/Unit/NonceFactoryTest.php b/tests/Unit/NonceFactoryTest.php index 1aacf6b..d9ce081 100644 --- a/tests/Unit/NonceFactoryTest.php +++ b/tests/Unit/NonceFactoryTest.php @@ -42,11 +42,11 @@ public function testStubInstance(string $type): void public function defaultDataProvider(): iterable { yield 'test' => [ - 'type' => 'fooBar' + 'type' => 'fooBar', ]; yield 'test2' => [ - 'type' => 'fizzBuzz' + 'type' => 'fizzBuzz', ]; } @@ -54,4 +54,4 @@ private function buildTestee(string $type): FooBarNonceFactory { return new FooBarNonceFactory($type); } -} \ No newline at end of file +} diff --git a/tests/Unit/NonceTest.php b/tests/Unit/NonceTest.php index 24b8c19..3d084fb 100644 --- a/tests/Unit/NonceTest.php +++ b/tests/Unit/NonceTest.php @@ -10,7 +10,6 @@ class NonceTest extends UnitTestCase { - public function testCreateStubInstance(): void { $this->assertInstanceOf( @@ -51,4 +50,4 @@ private function buildTestee(): Nonce { return FooBarNonce::createFromDefaults(); } -} \ No newline at end of file +} diff --git a/tests/Unit/NonceVerifierTest.php b/tests/Unit/NonceVerifierTest.php index d059547..cb8ff92 100644 --- a/tests/Unit/NonceVerifierTest.php +++ b/tests/Unit/NonceVerifierTest.php @@ -34,6 +34,7 @@ public function testStubInstance( string $age, Nonce $nonce ): void { + $verifier = $this->buildtestee($callback, $age); $this->assertTrue($verifier->verify($nonce)); @@ -59,4 +60,4 @@ private function buildtestee( return new FooBarVerifier($callback, $age); } -} \ No newline at end of file +} diff --git a/tests/src/Stubs/FooBarNonce.php b/tests/src/Stubs/FooBarNonce.php index 8d00eeb..ae1e95e 100644 --- a/tests/src/Stubs/FooBarNonce.php +++ b/tests/src/Stubs/FooBarNonce.php @@ -56,7 +56,7 @@ public static function getDefaults(): array 'lifetime' => $lifetime, 'lifetimeManipulated' => $lifetime * 2, 'requestName' => $fooBar, - 'token' => $fooBar + 'token' => $fooBar, ]; } @@ -83,4 +83,4 @@ public function getToken(): string { return $this->token; } -} \ No newline at end of file +} diff --git a/tests/src/Stubs/FooBarNonceFactory.php b/tests/src/Stubs/FooBarNonceFactory.php index 9a0add2..e3a5516 100644 --- a/tests/src/Stubs/FooBarNonceFactory.php +++ b/tests/src/Stubs/FooBarNonceFactory.php @@ -30,4 +30,4 @@ public function create(string $type, array $data = []): Nonce { return FooBarNonce::createFromDefaults(); } -} \ No newline at end of file +} diff --git a/tests/src/Stubs/FooBarVerifier.php b/tests/src/Stubs/FooBarVerifier.php index 3d6b594..952224a 100644 --- a/tests/src/Stubs/FooBarVerifier.php +++ b/tests/src/Stubs/FooBarVerifier.php @@ -36,4 +36,4 @@ public function renderMessageHasExpired(Nonce $nonce): void { echo $this->age; } -} \ No newline at end of file +} diff --git a/tests/src/UnitTestCase.php b/tests/src/UnitTestCase.php index 2e7c7b4..d588a33 100644 --- a/tests/src/UnitTestCase.php +++ b/tests/src/UnitTestCase.php @@ -6,5 +6,4 @@ class UnitTestCase extends TestCase { - } From 6648b27426e8875b3df463a97962fb2b5e2ae45c Mon Sep 17 00:00:00 2001 From: brianvarskonst Date: Tue, 5 Jul 2022 18:46:42 +0000 Subject: [PATCH 09/11] Fix CS and include Test Code --- phpcs.xml | 3 ++- tests/Integration/NonceVerifierTest.php | 4 +--- tests/{src => }/Stubs/FooBarNonce.php | 0 tests/{src => }/Stubs/FooBarNonceFactory.php | 0 tests/{src => }/Stubs/FooBarVerifier.php | 8 +++----- tests/{src => }/TestCase.php | 0 tests/Unit/NonceVerifierTest.php | 7 ++----- tests/{src => }/UnitTestCase.php | 0 8 files changed, 8 insertions(+), 14 deletions(-) rename tests/{src => }/Stubs/FooBarNonce.php (100%) rename tests/{src => }/Stubs/FooBarNonceFactory.php (100%) rename tests/{src => }/Stubs/FooBarVerifier.php (81%) rename tests/{src => }/TestCase.php (100%) rename tests/{src => }/UnitTestCase.php (100%) diff --git a/phpcs.xml b/phpcs.xml index 2e38625..1ef62aa 100644 --- a/phpcs.xml +++ b/phpcs.xml @@ -6,6 +6,7 @@ ./src + ./tests */vendor/* @@ -21,7 +22,7 @@ + value="Bvsk\WordPress\NonceManager=>src,Bvsk\WordPress\NonceManager\Tests=>tests" /> diff --git a/tests/Integration/NonceVerifierTest.php b/tests/Integration/NonceVerifierTest.php index 5d6eaf3..e629c6d 100644 --- a/tests/Integration/NonceVerifierTest.php +++ b/tests/Integration/NonceVerifierTest.php @@ -4,7 +4,6 @@ namespace Bvsk\WordPress\NonceManager\Tests\Integration; -use Bvsk\WordPress\NonceManager\Nonces\Factory\DefaultNonceProperties; use Bvsk\WordPress\NonceManager\Nonces\Factory\FieldNonceFactory; use Bvsk\WordPress\NonceManager\Nonces\Factory\SimpleNonceFactory; use Bvsk\WordPress\NonceManager\Nonces\Factory\UrlNonceFactory; @@ -14,11 +13,10 @@ 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; -use InvalidArgumentException; - class NonceVerifierTest extends UnitTestCase { public function testCreateNonceVerifier(): void diff --git a/tests/src/Stubs/FooBarNonce.php b/tests/Stubs/FooBarNonce.php similarity index 100% rename from tests/src/Stubs/FooBarNonce.php rename to tests/Stubs/FooBarNonce.php diff --git a/tests/src/Stubs/FooBarNonceFactory.php b/tests/Stubs/FooBarNonceFactory.php similarity index 100% rename from tests/src/Stubs/FooBarNonceFactory.php rename to tests/Stubs/FooBarNonceFactory.php diff --git a/tests/src/Stubs/FooBarVerifier.php b/tests/Stubs/FooBarVerifier.php similarity index 81% rename from tests/src/Stubs/FooBarVerifier.php rename to tests/Stubs/FooBarVerifier.php index 952224a..bd0b41e 100644 --- a/tests/src/Stubs/FooBarVerifier.php +++ b/tests/Stubs/FooBarVerifier.php @@ -13,11 +13,8 @@ class FooBarVerifier implements Verifier private string $age; - public function __construct( - callable $callback, - string $age - ) { - + public function __construct(callable $callback, string $age) + { $this->callback = $callback; $this->age = $age; } @@ -34,6 +31,7 @@ public function getAge(Nonce $nonce): string public function renderMessageHasExpired(Nonce $nonce): void { + // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped echo $this->age; } } diff --git a/tests/src/TestCase.php b/tests/TestCase.php similarity index 100% rename from tests/src/TestCase.php rename to tests/TestCase.php diff --git a/tests/Unit/NonceVerifierTest.php b/tests/Unit/NonceVerifierTest.php index cb8ff92..f78acc7 100644 --- a/tests/Unit/NonceVerifierTest.php +++ b/tests/Unit/NonceVerifierTest.php @@ -15,11 +15,8 @@ class NonceVerifierTest extends UnitTestCase /** * @dataProvider defaultDataProvider */ - public function testCreateStubInstance( - callable $callback, - string $age - ): void { - + public function testCreateStubInstance(callable $callback, string $age): void + { $this->assertInstanceOf( Verifier::class, $this->buildtestee($callback, $age) diff --git a/tests/src/UnitTestCase.php b/tests/UnitTestCase.php similarity index 100% rename from tests/src/UnitTestCase.php rename to tests/UnitTestCase.php From de8af4f026f1cce655d5ad000a49c0cbf7e85d62 Mon Sep 17 00:00:00 2001 From: brianvarskonst Date: Tue, 5 Jul 2022 20:02:55 +0000 Subject: [PATCH 10/11] Fix Dev Autoload and use Data provider for Nonce Manager Test --- composer.json | 8 +-- tests/Integration/NonceManagerTest.php | 68 +++++++++++++++++-------- tests/Integration/NonceVerifierTest.php | 11 ++-- 3 files changed, 55 insertions(+), 32 deletions(-) diff --git a/composer.json b/composer.json index daa85e5..6a39a39 100644 --- a/composer.json +++ b/composer.json @@ -39,7 +39,7 @@ }, "autoload-dev": { "psr-4": { - "Bvsk\\WordPress\\NonceManager\\Tests\\": "tests/src" + "Bvsk\\WordPress\\NonceManager\\Tests\\": "tests" } }, "scripts": { @@ -48,8 +48,10 @@ "psalm": "@php ./vendor/vimeo/psalm/psalm --no-cache --no-diff --no-progress --output-format=compact", "tests": "@php ./vendor/bin/phpunit", "tests:no-cov": "@php ./vendor/bin/phpunit --no-coverage", - "tests:unit": "@php ./vendor/bin/phpunit --testsuite=PHPUnit", - "tests:unit:no-cov": "@php ./vendor/bin/phpunit --testsuite=PHPUnit --no-coverage", + "tests:unit": "@php ./vendor/bin/phpunit --testsuite=Unit", + "tests:unit:no-cov": "@php ./vendor/bin/phpunit --testsuite=Unit --no-coverage", + "tests:integration": "@php ./vendor/bin/phpunit --testsuite=Integration", + "tests:integration:no-cov": "@php ./vendor/bin/phpunit --testsuite=Integration --no-coverage", "qa": [ "@cs", "@psalm", diff --git a/tests/Integration/NonceManagerTest.php b/tests/Integration/NonceManagerTest.php index c147816..a218173 100644 --- a/tests/Integration/NonceManagerTest.php +++ b/tests/Integration/NonceManagerTest.php @@ -18,11 +18,14 @@ class NonceManagerTest extends UnitTestCase { - public function testCreateStubInstance(): void + /** + * @dataProvider defaultDataProvider + */ + public function testCreateStubInstance(string $token): void { $this->assertInstanceOf( NonceManager::class, - $this->buildTesteeWithStubs() + $this->buildTesteeWithStubs($token) ); } @@ -34,12 +37,13 @@ public function testCreateInstanceFromDefaults(): void ); } - public function testCreateSimpleNonceWithNonceManager(): void + /** + * @dataProvider defaultDataProvider + */ + public function testCreateSimpleNonceWithNonceManager(string $token): void { $testee = $this->buildTestee(); - $token = 'fooBar'; - expect('add_filter')->once(); expect('apply_filters')->once()->andReturn(DefaultNonceProperties::LIFETIME); expect('wp_create_nonce')->once()->andReturn($token); @@ -53,13 +57,13 @@ public function testCreateSimpleNonceWithNonceManager(): void $this->assertSame($nonce->getRequestName(), DefaultNonceProperties::REQUEST_NAME); } - public function testCreateFieldNonceWithNonceManager(): void + /** + * @dataProvider fieldNonceDataProvider + */ + public function testCreateFieldNonceWithNonceManager(string $token, string $hiddenInput): void { $testee = $this->buildTestee(); - $token = 'fooBar'; - $hiddenInput = ''; - expect('add_filter')->once(); expect('apply_filters')->once()->andReturn(DefaultNonceProperties::LIFETIME); expect('wp_create_nonce')->once()->andReturn($token); @@ -75,13 +79,13 @@ public function testCreateFieldNonceWithNonceManager(): void $this->assertSame(DefaultNonceProperties::REQUEST_NAME, $nonce->getRequestName()); } - public function testCreateUrlNonceWithNonceManager(): void + /** + * @dataProvider urlNonceDataProvider + */ + public function testCreateUrlNonceWithNonceManager(string $token, string $url): void { $testee = $this->buildTestee(); - $token = 'fooBar'; - $url = 'https://example.com'; - expect('add_filter')->once(); expect('apply_filters')->once()->andReturn(DefaultNonceProperties::LIFETIME); expect('wp_create_nonce')->once()->andReturn($token); @@ -96,12 +100,13 @@ public function testCreateUrlNonceWithNonceManager(): void $this->assertSame($url, $nonce->getUrl()); } - public function testVerifyNonceWithNonceManager(): void + /** + * @dataProvider defaultDataProvider + */ + public function testVerifyNonceWithNonceManager(string $token): void { $testee = $this->buildTestee(); - $token = 'fooBar'; - expect('add_filter')->once(); expect('apply_filters')->once()->andReturn(DefaultNonceProperties::LIFETIME); expect('wp_create_nonce')->once()->andReturn($token); @@ -114,20 +119,41 @@ public function testVerifyNonceWithNonceManager(): void $this->assertTrue($testee->verify($nonce)); } + public function defaultDataProvider(): iterable + { + yield 'test' => [ + 'token' => 'fooBar', + ]; + } + + public function fieldNonceDataProvider(): iterable + { + yield 'test' => [ + 'token' => 'fooBar', + 'hiddenInput' => '', + ]; + } + + public function urlNonceDataProvider(): iterable + { + yield 'test' => [ + 'token' => 'fooBar', + 'url' => 'https://example.com', + ]; + } + private function buildTestee(): NonceManager { return NonceManager::createFromDefaults(); } - private function buildTesteeWithStubs(): NonceManager + private function buildTesteeWithStubs(string $fooBar): NonceManager { - $foobar = 'fooBar'; - return new NonceManager( - new FooBarNonceFactory($foobar), + new FooBarNonceFactory($fooBar), new FooBarVerifier( fn(Nonce $nonce): bool => true, - $foobar + $fooBar ) ); } diff --git a/tests/Integration/NonceVerifierTest.php b/tests/Integration/NonceVerifierTest.php index e629c6d..bf40e05 100644 --- a/tests/Integration/NonceVerifierTest.php +++ b/tests/Integration/NonceVerifierTest.php @@ -30,7 +30,7 @@ public function testCreateNonceVerifier(): void /** * @dataProvider defaultDataProvider */ - public function testSuccessfulNonceVerifier(string $token, Nonce $nonce): void + public function testSuccessfulNonceVerifier(Nonce $nonce): void { $verifier = $this->buildTestee(); @@ -50,7 +50,7 @@ public function testSuccessfulNonceVerifier(string $token, Nonce $nonce): void /** * @dataProvider defaultDataProvider */ - public function testFailingNonceVerifier(string $token, Nonce $nonce): void + public function testFailingNonceVerifier(Nonce $nonce): void { $verifier = $this->buildTestee(); @@ -75,23 +75,18 @@ public function testFailingNonceVerifier(string $token, Nonce $nonce): void public function defaultDataProvider(): iterable { - $token = 'fooBar'; - expect('add_filter')->times(3); - expect('wp_create_nonce')->times(3)->andReturn($token); + expect('wp_create_nonce')->times(3)->andReturn('fooBar'); yield 'testSimpleNonce' => [ - 'token' => $token, 'nonce' => (new SimpleNonceFactory())->create('simple'), ]; yield 'testFieldNonce' => [ - 'token' => $token, 'nonce' => (new FieldNonceFactory())->create('field', ['referer' => false]), ]; yield 'testUrlNonce' => [ - 'token' => $token, 'nonce' => (new UrlNonceFactory())->create('url', ['url' => 'https://www.example.com']), ]; } From 605fb3b504160035bf82a3e1c824a9481ef9fdb9 Mon Sep 17 00:00:00 2001 From: brianvarskonst Date: Tue, 5 Jul 2022 20:05:22 +0000 Subject: [PATCH 11/11] Add run phpunit tests in testing workflow --- .github/workflows/testing.yaml | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/.github/workflows/testing.yaml b/.github/workflows/testing.yaml index 26a86a6..97b5ed1 100644 --- a/.github/workflows/testing.yaml +++ b/.github/workflows/testing.yaml @@ -8,12 +8,11 @@ jobs: runs-on: ubuntu-latest steps: - # Notice: Ubuntu will already ship composer 2 which currently breaks the used tools. - uses: shivammathur/setup-php@v2 with: php-version: '7.4' tools: composer:v1 - # Save the current branch name in an environment variable, to be used for conditional logic below + # Save the current branch name in an environment variable to use it for the conditional logic below - name: Extract branch name into env id: git run: echo "::set-output name=branch::$(echo ${GITHUB_REF##*/})" @@ -37,4 +36,7 @@ jobs: run: composer install --prefer-dist --no-progress --no-suggest - name: Run Codesniffer - run: composer cs \ No newline at end of file + run: composer cs + + - name: Run PHPUnit Tests + run: composer tests \ No newline at end of file