diff --git a/.tests/php/integration/AMainTest.php b/.tests/php/integration/AMainTest.php index 0f6a8e35..feb19b5a 100644 --- a/.tests/php/integration/AMainTest.php +++ b/.tests/php/integration/AMainTest.php @@ -13,6 +13,7 @@ namespace HCaptcha\Tests\Integration; +use HCaptcha\Admin\Notifications; use HCaptcha\AutoVerify\AutoVerify; use HCaptcha\BBPress\NewTopic; use HCaptcha\BBPress\Reply; @@ -27,17 +28,21 @@ use HCaptcha\ElementorPro\HCaptchaHandler; use HCaptcha\NF\NF; use HCaptcha\Quform\Quform; +use HCaptcha\Sendinblue\Sendinblue; use HCaptcha\WC\Checkout; use HCaptcha\WC\OrderTracking; use HCaptcha\WCWishlists\CreateList; use HCaptcha\WP\Comment; use HCaptcha\WP\Login; use HCaptcha\WP\LostPassword; +use HCaptcha\WP\PasswordProtected; use HCaptcha\WP\Register; use HCaptcha\WPDiscuz\Subscribe; use Mockery; use ReflectionException; use stdClass; +use tad\FunctionMocker\FunctionMocker; +use function PHPUnit\Framework\assertSame; /** * Test Main class. @@ -62,9 +67,9 @@ class AMainTest extends HCaptchaWPTestCase { * @throws ReflectionException ReflectionException. */ public function tearDown(): void { // phpcs:ignore PHPCompatibility.FunctionDeclarations.NewReturnTypeDeclarations.voidFound - $hcaptcha_wordpress_plugin = hcaptcha(); + $hcaptcha = hcaptcha(); - $loaded_classes = $this->get_protected_property( $hcaptcha_wordpress_plugin, 'loaded_classes' ); + $loaded_classes = $this->get_protected_property( $hcaptcha, 'loaded_classes' ); // phpcs:disable WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing unset( @@ -79,7 +84,7 @@ public function tearDown(): void { // phpcs:ignore PHPCompatibility.FunctionDecl ); // phpcs:enable WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing - $this->set_protected_property( $hcaptcha_wordpress_plugin, 'loaded_classes', $loaded_classes ); + $this->set_protected_property( $hcaptcha, 'loaded_classes', $loaded_classes ); wp_dequeue_script( 'hcaptcha' ); wp_deregister_script( 'hcaptcha' ); @@ -87,7 +92,7 @@ public function tearDown(): void { // phpcs:ignore PHPCompatibility.FunctionDecl wp_dequeue_script( 'jquery' ); wp_deregister_script( 'jquery' ); - $hcaptcha_wordpress_plugin->form_shown = false; + $hcaptcha->form_shown = false; parent::tearDown(); } @@ -98,26 +103,18 @@ public function tearDown(): void { // phpcs:ignore PHPCompatibility.FunctionDecl * @return void */ public function test_init() { - $hcaptcha_wordpress_plugin = hcaptcha(); + $hcaptcha = hcaptcha(); // Plugin was loaded by codeception. - self::assertSame( - - PHP_INT_MAX, - has_action( 'plugins_loaded', [ $hcaptcha_wordpress_plugin, 'init_hooks' ] ) - ); + self::assertSame( - PHP_INT_MAX, has_action( 'plugins_loaded', [ $hcaptcha, 'init_hooks' ] ) ); - remove_action( 'plugins_loaded', [ $hcaptcha_wordpress_plugin, 'init_hooks' ], -PHP_INT_MAX ); + remove_action( 'plugins_loaded', [ $hcaptcha, 'init_hooks' ], -PHP_INT_MAX ); - self::assertFalse( - has_action( 'plugins_loaded', [ $hcaptcha_wordpress_plugin, 'init_hooks' ] ) - ); + self::assertFalse( has_action( 'plugins_loaded', [ $hcaptcha, 'init_hooks' ] ) ); - $hcaptcha_wordpress_plugin->init(); + $hcaptcha->init(); - self::assertSame( - - PHP_INT_MAX, - has_action( 'plugins_loaded', [ $hcaptcha_wordpress_plugin, 'init_hooks' ] ) - ); + self::assertSame( - PHP_INT_MAX, has_action( 'plugins_loaded', [ $hcaptcha, 'init_hooks' ] ) ); } /** @@ -129,13 +126,21 @@ public function test_init() { * @param boolean $hcaptcha_active Plugin should be active. * * @dataProvider dp_test_init - * @noinspection PhpUnitTestsInspection * @throws ReflectionException ReflectionException. + * @noinspection PhpUnitTestsInspection + * @noinspection UnnecessaryAssertionInspection + * @noinspection UnusedFunctionResultInspection */ public function test_init_and_init_hooks( bool $logged_in, string $hcaptcha_off_when_logged_in, $whitelisted, bool $hcaptcha_active ) { global $current_user; - $hcaptcha_wordpress_plugin = hcaptcha(); + $hcaptcha = hcaptcha(); + + update_option( 'hcaptcha_settings', [ 'site_key' => 'some site key' ] ); + update_option( 'hcaptcha_settings', [ 'secret_key' => 'some secret key' ] ); + + // Init plugin to update settings. + do_action( 'plugins_loaded' ); add_filter( 'hcap_whitelist_ip', @@ -153,43 +158,21 @@ static function () use ( $whitelisted ) { ) ); - self::assertSame( - - PHP_INT_MAX, - has_action( 'plugins_loaded', [ $hcaptcha_wordpress_plugin, 'init_hooks' ] ) - ); + self::assertSame( - PHP_INT_MAX, has_action( 'plugins_loaded', [ $hcaptcha, 'init_hooks' ] ) ); - self::assertSame( - - PHP_INT_MAX + 1, - has_action( 'plugins_loaded', [ $hcaptcha_wordpress_plugin, 'load_modules' ] ) - ); - self::assertSame( - 10, - has_action( 'plugins_loaded', [ $hcaptcha_wordpress_plugin, 'load_textdomain' ] ) - ); + self::assertSame( - PHP_INT_MAX + 1, has_action( 'plugins_loaded', [ $hcaptcha, 'load_modules' ] ) ); + self::assertSame( 10, has_action( 'plugins_loaded', [ $hcaptcha, 'load_textdomain' ] ) ); - self::assertSame( - 10, - has_filter( - 'wp_resource_hints', - [ $hcaptcha_wordpress_plugin, 'prefetch_hcaptcha_dns' ] - ) - ); - self::assertSame( - 10, - has_action( 'wp_head', [ $hcaptcha_wordpress_plugin, 'print_inline_styles' ] ) - ); - self::assertSame( - 10, - has_action( 'login_head', [ $hcaptcha_wordpress_plugin, 'login_head' ] ) - ); - self::assertSame( - 0, - has_action( 'wp_print_footer_scripts', [ $hcaptcha_wordpress_plugin, 'print_footer_scripts' ] ) - ); + self::assertSame( 10, has_filter( 'wp_resource_hints', [ $hcaptcha, 'prefetch_hcaptcha_dns' ] ) ); + self::assertSame( 10, has_filter( 'wp_headers', [ $hcaptcha, 'csp_headers' ] ) ); + self::assertSame( 10, has_action( 'wp_head', [ $hcaptcha, 'print_inline_styles' ] ) ); + self::assertSame( 10, has_action( 'login_head', [ $hcaptcha, 'login_head' ] ) ); + self::assertSame( 0, has_action( 'wp_print_footer_scripts', [ $hcaptcha, 'print_footer_scripts' ] ) ); - self::assertInstanceOf( AutoVerify::class, $this->get_protected_property( $hcaptcha_wordpress_plugin, 'auto_verify' ) ); + self::assertInstanceOf( AutoVerify::class, $this->get_protected_property( $hcaptcha, 'auto_verify' ) ); unset( $current_user ); + if ( $logged_in ) { wp_set_current_user( 1 ); } @@ -203,6 +186,8 @@ static function () use ( $whitelisted ) { $subject = new Main(); $subject->init_hooks(); + self::assertInstanceOf( Notifications::class, $subject->notifications() ); + self::assertSame( - PHP_INT_MAX + 1, has_action( 'plugins_loaded', [ $subject, 'load_modules' ] ) @@ -220,43 +205,19 @@ static function () use ( $whitelisted ) { ); if ( $hcaptcha_active ) { - self::assertSame( - 10, - has_filter( - 'wp_resource_hints', - [ $subject, 'prefetch_hcaptcha_dns' ] - ) - ); - self::assertSame( - 10, - has_action( 'wp_head', [ $subject, 'print_inline_styles' ] ) - ); - self::assertSame( - 10, - has_action( 'login_head', [ $subject, 'login_head' ] ) - ); - self::assertSame( - 0, - has_action( 'wp_print_footer_scripts', [ $subject, 'print_footer_scripts' ] ) - ); + self::assertSame( 10, has_filter( 'wp_resource_hints', [ $subject, 'prefetch_hcaptcha_dns' ] ) ); + self::assertSame( 10, has_filter( 'wp_headers', [ $subject, 'csp_headers' ] ) ); + self::assertSame( 10, has_action( 'wp_head', [ $subject, 'print_inline_styles' ] ) ); + self::assertSame( 10, has_action( 'login_head', [ $subject, 'login_head' ] ) ); + self::assertSame( 0, has_action( 'wp_print_footer_scripts', [ $subject, 'print_footer_scripts' ] ) ); self::assertInstanceOf( AutoVerify::class, $this->get_protected_property( $subject, 'auto_verify' ) ); } else { - self::assertFalse( - has_filter( - 'wp_resource_hints', - [ $subject, 'prefetch_hcaptcha_dns' ] - ) - ); - self::assertFalse( - has_action( 'wp_head', [ $subject, 'print_inline_styles' ] ) - ); - self::assertFalse( - has_action( 'login_head', [ $subject, 'login_head' ] ) - ); - self::assertFalse( - has_action( 'wp_print_footer_scripts', [ $subject, 'print_footer_scripts' ] ) - ); + self::assertFalse( has_filter( 'wp_resource_hints', [ $subject, 'prefetch_hcaptcha_dns' ] ) ); + self::assertFalse( has_filter( 'wp_headers', [ $subject, 'csp_headers' ] ) ); + self::assertFalse( has_action( 'wp_head', [ $subject, 'print_inline_styles' ] ) ); + self::assertFalse( has_action( 'login_head', [ $subject, 'login_head' ] ) ); + self::assertFalse( has_action( 'wp_print_footer_scripts', [ $subject, 'print_footer_scripts' ] ) ); self::assertNull( $this->get_protected_property( $subject, 'auto_verify' ) ); } @@ -292,6 +253,7 @@ public function dp_test_init(): array { * @dataProvider dp_test_init_and_init_hooks_on_elementor_pro_edit_page * @noinspection PhpUnitTestsInspection * @throws ReflectionException ReflectionException. + * @noinspection UnusedFunctionResultInspection */ public function test_init_and_init_hooks_on_elementor_pro_edit_page( string $elementor_pro_status, @@ -344,43 +306,19 @@ static function () { ); if ( $hcaptcha_active ) { - self::assertSame( - 10, - has_filter( - 'wp_resource_hints', - [ $subject, 'prefetch_hcaptcha_dns' ] - ) - ); - self::assertSame( - 10, - has_action( 'wp_head', [ $subject, 'print_inline_styles' ] ) - ); - self::assertSame( - 10, - has_action( 'login_head', [ $subject, 'login_head' ] ) - ); - self::assertSame( - 0, - has_action( 'wp_print_footer_scripts', [ $subject, 'print_footer_scripts' ] ) - ); + self::assertSame( 10, has_filter( 'wp_resource_hints', [ $subject, 'prefetch_hcaptcha_dns' ] ) ); + self::assertSame( 10, has_filter( 'wp_headers', [ $subject, 'csp_headers' ] ) ); + self::assertSame( 10, has_action( 'wp_head', [ $subject, 'print_inline_styles' ] ) ); + self::assertSame( 10, has_action( 'login_head', [ $subject, 'login_head' ] ) ); + self::assertSame( 0, has_action( 'wp_print_footer_scripts', [ $subject, 'print_footer_scripts' ] ) ); self::assertInstanceOf( AutoVerify::class, $this->get_protected_property( $subject, 'auto_verify' ) ); } else { - self::assertFalse( - has_filter( - 'wp_resource_hints', - [ $subject, 'prefetch_hcaptcha_dns' ] - ) - ); - self::assertFalse( - has_action( 'wp_head', [ $subject, 'print_inline_styles' ] ) - ); - self::assertFalse( - has_action( 'login_head', [ $subject, 'login_head' ] ) - ); - self::assertFalse( - has_action( 'wp_print_footer_scripts', [ $subject, 'print_footer_scripts' ] ) - ); + self::assertFalse( has_filter( 'wp_resource_hints', [ $subject, 'prefetch_hcaptcha_dns' ] ) ); + self::assertFalse( has_filter( 'wp_headers', [ $subject, 'csp_headers' ] ) ); + self::assertFalse( has_action( 'wp_head', [ $subject, 'print_inline_styles' ] ) ); + self::assertFalse( has_action( 'login_head', [ $subject, 'login_head' ] ) ); + self::assertFalse( has_action( 'wp_print_footer_scripts', [ $subject, 'print_footer_scripts' ] ) ); self::assertNull( $this->get_protected_property( $subject, 'auto_verify' ) ); } @@ -445,28 +383,14 @@ public function test_init_and_init_hooks_on_xml_rpc_request() { $subject->init(); - self::assertFalse( - has_action( 'plugins_loaded', [ $subject, 'load_modules' ] ) - ); - self::assertFalse( - has_action( 'plugins_loaded', [ $subject, 'load_textdomain' ] ) - ); + self::assertFalse( has_action( 'plugins_loaded', [ $subject, 'load_modules' ] ) ); + self::assertFalse( has_action( 'plugins_loaded', [ $subject, 'load_textdomain' ] ) ); - self::assertFalse( - has_filter( - 'wp_resource_hints', - [ $subject, 'prefetch_hcaptcha_dns' ] - ) - ); - self::assertFalse( - has_action( 'wp_head', [ $subject, 'print_inline_styles' ] ) - ); - self::assertFalse( - has_action( 'login_head', [ $subject, 'login_head' ] ) - ); - self::assertFalse( - has_action( 'wp_print_footer_scripts', [ $subject, 'print_footer_scripts' ] ) - ); + self::assertFalse( has_filter( 'wp_resource_hints', [ $subject, 'prefetch_hcaptcha_dns' ] ) ); + self::assertFalse( has_filter( 'wp_headers', [ $subject, 'csp_headers' ] ) ); + self::assertFalse( has_action( 'wp_head', [ $subject, 'print_inline_styles' ] ) ); + self::assertFalse( has_action( 'login_head', [ $subject, 'login_head' ] ) ); + self::assertFalse( has_action( 'wp_print_footer_scripts', [ $subject, 'print_footer_scripts' ] ) ); self::assertNull( $this->get_protected_property( $subject, 'auto_verify' ) ); } @@ -489,6 +413,30 @@ public function test_prefetch_hcaptcha_dns() { self::assertSame( $expected, $subject->prefetch_hcaptcha_dns( $urls, 'dns-prefetch' ) ); } + /** + * Test csp_headers(). + * + * @return void + */ + public function test_csp_headers() { + $headers = [ 'some_header' => 'some header content' ]; + $expected = $headers; + $hcap_csp = "'self' https://hcaptcha.com https://*.hcaptcha.com"; + + $expected['X-Content-Security-Policy'] = + "default-src 'self'; " . + "script-src $hcap_csp; " . + "frame-src $hcap_csp; " . + "style-src $hcap_csp; " . + "connect-src $hcap_csp; " . + "unsafe-eval $hcap_csp; " . + "unsafe-inline $hcap_csp;"; + + $subject = new Main(); + + self::assertSame( $expected, $subject->csp_headers( $headers ) ); + } + /** * Test print_inline_styles(). */ @@ -666,9 +614,9 @@ public function test_login_head() { * @noinspection BadExpressionStatementJS */ public function test_print_footer_scripts( $compat, $language, $custom_themes, string $expected_script_src ) { - $hcaptcha_wordpress_plugin = hcaptcha(); + $hcaptcha = hcaptcha(); - $hcaptcha_wordpress_plugin->form_shown = true; + $hcaptcha->form_shown = true; $expected_scripts = '