diff --git a/.tests/php/integration/AMainTest.php b/.tests/php/integration/AMainTest.php index e33e5ba9..245a5951 100644 --- a/.tests/php/integration/AMainTest.php +++ b/.tests/php/integration/AMainTest.php @@ -508,6 +508,9 @@ public function test_print_inline_styles() { padding: 0; clear: both; } + #hcaptcha-options .h-captcha { + margin-bottom: 0; + } #af-wrapper div.editor-row.editor-row-hcaptcha { display: flex; flex-direction: row-reverse; @@ -705,6 +708,7 @@ function load() { const t = document.getElementsByTagName( \'script\' )[0]; const s = document.createElement(\'script\'); s.type = \'text/javascript\'; + s.id = \'hcaptcha-api\'; s[\'src\'] = \'' . $expected_script_src . '\'; s.async = true; t.parentNode.insertBefore( s, t ); @@ -737,10 +741,11 @@ function delayedLoad() { } )(); '; - $config_params = '{}'; + $config_params = 'on' === $custom_themes ? '' : null; $expected_extra = [ 'group' => 1, - 'data' => 'var HCaptchaMainObject = {"params":"' . $config_params . '"};', + // phpcs:ignore WordPress.WP.AlternativeFunctions.json_encode_json_encode + 'data' => 'var HCaptchaMainObject = {"params":' . json_encode( $config_params ) . '};', ]; update_option( diff --git a/.tests/php/integration/DelayedScript/DelayedScriptTest.php b/.tests/php/integration/DelayedScript/DelayedScriptTest.php index 60dc1a85..d6e7255d 100644 --- a/.tests/php/integration/DelayedScript/DelayedScriptTest.php +++ b/.tests/php/integration/DelayedScript/DelayedScriptTest.php @@ -115,6 +115,7 @@ function load() { const t = document.getElementsByTagName( \'script\' )[0]; const s = document.createElement(\'script\'); s.type = \'text/javascript\'; + s.id = \'hcaptcha-api\'; s[\'src\'] = \'https://js.hcaptcha.com/1/api.js\'; s.async = true; t.parentNode.insertBefore( s, t ); diff --git a/.tests/php/unit/HCaptchaTestCase.php b/.tests/php/unit/HCaptchaTestCase.php index 6b45b7ef..650db53f 100644 --- a/.tests/php/unit/HCaptchaTestCase.php +++ b/.tests/php/unit/HCaptchaTestCase.php @@ -372,7 +372,12 @@ protected function get_test_general_form_fields() { 'type' => 'password', 'section' => General::SECTION_KEYS, ], - 'check_config' => [ + 'sample_hcaptcha' => [ + 'label' => 'Sample hCaptcha', + 'type' => 'hcaptcha', + 'section' => General::SECTION_KEYS, + ], + 'check_config' => [ 'label' => 'Check Site Config', 'type' => 'button', 'text' => 'Check', diff --git a/.tests/php/unit/Settings/Abstracts/SettingsBaseTest.php b/.tests/php/unit/Settings/Abstracts/SettingsBaseTest.php index 88182386..9a62dc8d 100644 --- a/.tests/php/unit/Settings/Abstracts/SettingsBaseTest.php +++ b/.tests/php/unit/Settings/Abstracts/SettingsBaseTest.php @@ -1051,6 +1051,7 @@ public function test_setup_fields_not_on_options_screen() { * * @dataProvider dp_test_field_callback * @noinspection PhpUnusedParameterInspection + * @throws ReflectionException ReflectionException. */ public function test_field_callback( $arguments, $expected ) { $option_name = 'hcaptcha_settings'; @@ -1060,6 +1061,21 @@ public function test_field_callback( $arguments, $expected ) { $subject->shouldReceive( 'option_name' )->andReturn( $option_name ); $subject->shouldReceive( 'get' )->with( $arguments['field_id'] )->andReturn( $arguments['default'] ); + $fields = [ + 'text' => [ $subject, 'print_text_field' ], + 'password' => [ $subject, 'print_text_field' ], + 'number' => [ $subject, 'print_number_field' ], + 'textarea' => [ $subject, 'print_textarea_field' ], + 'checkbox' => [ $subject, 'print_checkbox_field' ], + 'radio' => [ $subject, 'print_radio_field' ], + 'select' => [ $subject, 'print_select_field' ], + 'multiple' => [ $subject, 'print_multiple_select_field' ], + 'table' => [ $subject, 'print_table_field' ], + 'button' => [ $subject, 'print_button_field' ], + ]; + + $this->set_protected_property( $subject, 'fields', $fields ); + WP_Mock::passthruFunction( 'wp_kses_post' ); WP_Mock::passthruFunction( 'wp_kses' ); diff --git a/.tests/php/unit/Settings/GeneralTest.php b/.tests/php/unit/Settings/GeneralTest.php index bc58413a..177cf033 100644 --- a/.tests/php/unit/Settings/GeneralTest.php +++ b/.tests/php/unit/Settings/GeneralTest.php @@ -12,8 +12,10 @@ namespace HCaptcha\Tests\Unit\Settings; +use HCaptcha\Main; use HCaptcha\Settings\Abstracts\SettingsBase; use HCaptcha\Settings\General; +use HCaptcha\Settings\Settings; use HCaptcha\Tests\Unit\HCaptchaTestCase; use Mockery; use ReflectionException; @@ -239,6 +241,13 @@ public function test_admin_enqueue_scripts() { $min_prefix = '.min'; $ajax_url = 'https://test.test/wp-admin/admin-ajax.php'; $nonce = 'some_nonce'; + $site_key = 'some key'; + + $settings = Mockery::mock( Settings::class )->makePartial(); + $settings->shouldReceive( 'get_site_key' )->andReturn( $site_key ); + + $main = Mockery::mock( Main::class )->makePartial(); + $main->shouldReceive( 'settings' )->andReturn( $settings ); $subject = Mockery::mock( General::class )->makePartial(); $subject->shouldAllowMockingProtectedMethods(); @@ -260,6 +269,8 @@ static function ( $name ) use ( $plugin_url, $plugin_version ) { } ); + WP_Mock::userFunction( 'hcaptcha' )->with()->once()->andReturn( $main ); + WP_Mock::userFunction( 'wp_enqueue_script' ) ->with( General::HANDLE, @@ -285,9 +296,17 @@ static function ( $name ) use ( $plugin_url, $plugin_version ) { General::HANDLE, General::OBJECT, [ - 'ajaxUrl' => $ajax_url, - 'action' => General::CHECK_CONFIG_ACTION, - 'nonce' => $nonce, + 'ajaxUrl' => $ajax_url, + 'action' => General::CHECK_CONFIG_ACTION, + 'nonce' => $nonce, + 'modeLive' => General::MODE_LIVE, + 'modeTestPublisher' => General::MODE_TEST_PUBLISHER, + 'modeTestEnterpriseSafeEndUser' => General::MODE_TEST_ENTERPRISE_SAFE_END_USER, + 'modeTestEnterpriseBotDetected' => General::MODE_TEST_ENTERPRISE_BOT_DETECTED, + 'siteKey' => $site_key, + 'modeTestPublisherSiteKey' => General::MODE_TEST_PUBLISHER_SITE_KEY, + 'modeTestEnterpriseSafeEndUserSiteKey' => General::MODE_TEST_ENTERPRISE_SAFE_END_USER_SITE_KEY, + 'modeTestEnterpriseBotDetectedSiteKey' => General::MODE_TEST_ENTERPRISE_BOT_DETECTED_SITE_KEY, ] ) ->once(); diff --git a/src/php/Settings/Abstracts/SettingsBase.php b/src/php/Settings/Abstracts/SettingsBase.php index ca0e0c27..12bf8dfe 100644 --- a/src/php/Settings/Abstracts/SettingsBase.php +++ b/src/php/Settings/Abstracts/SettingsBase.php @@ -59,7 +59,7 @@ abstract class SettingsBase { * * @var array */ - private $fields; + protected $fields; /** * Get screen id. @@ -689,7 +689,7 @@ public function setup_fields() { * * @param array $arguments Field arguments. */ - private function print_text_field( array $arguments ) { + protected function print_text_field( array $arguments ) { $value = $this->get( $arguments['field_id'] ); $autocomplete = ''; $lp_ignore = 'false'; @@ -718,7 +718,7 @@ private function print_text_field( array $arguments ) { * * @param array $arguments Field arguments. */ - private function print_number_field( array $arguments ) { + protected function print_number_field( array $arguments ) { $value = $this->get( $arguments['field_id'] ); $min = $arguments['min']; $max = $arguments['max']; @@ -744,7 +744,7 @@ private function print_number_field( array $arguments ) { * * @param array $arguments Field arguments. */ - private function print_textarea_field( array $arguments ) { + protected function print_textarea_field( array $arguments ) { $value = $this->get( $arguments['field_id'] ); printf( @@ -764,7 +764,7 @@ private function print_textarea_field( array $arguments ) { * * @noinspection HtmlUnknownAttribute */ - private function print_checkbox_field( array $arguments ) { + protected function print_checkbox_field( array $arguments ) { $value = (array) $this->get( $arguments['field_id'] ); if ( empty( $arguments['options'] ) || ! is_array( $arguments['options'] ) ) { @@ -829,7 +829,7 @@ private function print_checkbox_field( array $arguments ) { * * @noinspection HtmlUnknownAttribute */ - private function print_radio_field( array $arguments ) { + protected function print_radio_field( array $arguments ) { $value = $this->get( $arguments['field_id'] ); if ( empty( $arguments['options'] ) || ! is_array( $arguments['options'] ) ) { @@ -894,7 +894,7 @@ private function print_radio_field( array $arguments ) { * * @noinspection HtmlUnknownAttribute */ - private function print_select_field( array $arguments ) { + protected function print_select_field( array $arguments ) { $value = $this->get( $arguments['field_id'] ); if ( empty( $arguments['options'] ) || ! is_array( $arguments['options'] ) ) { @@ -944,7 +944,7 @@ private function print_select_field( array $arguments ) { * * @noinspection HtmlUnknownAttribute */ - private function print_multiple_select_field( array $arguments ) { + protected function print_multiple_select_field( array $arguments ) { $value = $this->get( $arguments['field_id'] ); if ( empty( $arguments['options'] ) || ! is_array( $arguments['options'] ) ) { @@ -998,7 +998,7 @@ private function print_multiple_select_field( array $arguments ) { * * @param array $arguments Field arguments. */ - private function print_table_field( array $arguments ) { + protected function print_table_field( array $arguments ) { $value = $this->get( $arguments['field_id'] ); if ( ! is_array( $value ) ) { @@ -1045,7 +1045,7 @@ private function print_table_field( array $arguments ) { * * @param array $arguments Field arguments. */ - private function print_button_field( array $arguments ) { + protected function print_button_field( array $arguments ) { $disabled = $arguments['disabled'] ?? ''; $field_id = $arguments['field_id'] ?? ''; $text = $arguments['text'] ?? ''; @@ -1261,7 +1261,7 @@ protected function is_options_screen(): bool { * * @return void */ - private function print_helper( string $helper ) { + protected function print_helper( string $helper ) { if ( ! $helper ) { return; } @@ -1279,7 +1279,7 @@ private function print_helper( string $helper ) { * * @return void */ - private function print_supplemental( string $supplemental ) { + protected function print_supplemental( string $supplemental ) { if ( ! $supplemental ) { return; }