Skip to content

Commit

Permalink
Fix tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
kagg-design committed Jul 30, 2023
1 parent f7b04b3 commit d5ad761
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 18 deletions.
9 changes: 7 additions & 2 deletions .tests/php/integration/AMainTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 );
Expand Down Expand Up @@ -737,10 +741,11 @@ function delayedLoad() {
} )();
</script>';

$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(
Expand Down
1 change: 1 addition & 0 deletions .tests/php/integration/DelayedScript/DelayedScriptTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
7 changes: 6 additions & 1 deletion .tests/php/unit/HCaptchaTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
16 changes: 16 additions & 0 deletions .tests/php/unit/Settings/Abstracts/SettingsBaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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' );

Expand Down
25 changes: 22 additions & 3 deletions .tests/php/unit/Settings/GeneralTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand All @@ -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,
Expand All @@ -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();
Expand Down
24 changes: 12 additions & 12 deletions src/php/Settings/Abstracts/SettingsBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ abstract class SettingsBase {
*
* @var array
*/
private $fields;
protected $fields;

/**
* Get screen id.
Expand Down Expand Up @@ -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';
Expand Down Expand Up @@ -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'];
Expand All @@ -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(
Expand All @@ -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'] ) ) {
Expand Down Expand Up @@ -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'] ) ) {
Expand Down Expand Up @@ -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'] ) ) {
Expand Down Expand Up @@ -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'] ) ) {
Expand Down Expand Up @@ -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 ) ) {
Expand Down Expand Up @@ -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'] ?? '';
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand Down

0 comments on commit d5ad761

Please sign in to comment.