From 3d337a87158f7081168f16a13ae4fa717db31d5d Mon Sep 17 00:00:00 2001 From: Bogdan Preda Date: Thu, 30 Mar 2023 13:35:19 +0300 Subject: [PATCH] chore: added unit test for the modified sanitization method --- .gitignore | 1 + header-footer-grid/Traits/Core.php | 11 +++ phpunit.xml | 9 +- tests/test-neve-sanitization.php | 143 +++++++++++++++++++++++++++++ 4 files changed, 158 insertions(+), 6 deletions(-) create mode 100644 tests/test-neve-sanitization.php diff --git a/.gitignore b/.gitignore index 17cdd95e29..25562a2937 100644 --- a/.gitignore +++ b/.gitignore @@ -24,3 +24,4 @@ logs.log /playwright-report/ /playwright/.cache/ /playwright/.auth/ +.phpunit.result.cache diff --git a/header-footer-grid/Traits/Core.php b/header-footer-grid/Traits/Core.php index 952649a162..198f073ad0 100644 --- a/header-footer-grid/Traits/Core.php +++ b/header-footer-grid/Traits/Core.php @@ -93,8 +93,19 @@ public function sanitize_responsive_int_json( $input ) { // Optional if a suffix is present. We sanitize the array of suffixes below. if ( 'suffix' === $key && is_array( $value ) ) { + $default_suffix = array( + 'mobile' => 'px', + 'tablet' => 'px', + 'desktop' => 'px', + ); + $sanitized_devices = array(); foreach ( $value as $device => $suffix ) { $filtered['suffix'][ $device ] = in_array( $suffix, array( 'px', 'em', 'rem', '%' ), true ) ? $suffix : 'px'; + $sanitized_devices[] = $device; + } + + if ( array_diff( array_keys( $default_suffix ), $sanitized_devices ) ) { + $filtered['suffix'] = array_merge( $default_suffix, $filtered['suffix'] ); } } } diff --git a/phpunit.xml b/phpunit.xml index ec5472d4ac..ef1249b5d1 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -7,14 +7,11 @@ convertWarningsToExceptions="true" > - - + + ./tests/ - - - - + ./tests/old/ diff --git a/tests/test-neve-sanitization.php b/tests/test-neve-sanitization.php new file mode 100644 index 0000000000..ab50eed393 --- /dev/null +++ b/tests/test-neve-sanitization.php @@ -0,0 +1,143 @@ + + * Created on: 30-03-{2023} + * + * @package neve + */ + +use HFG\Traits\Core; + +/** + * Class SanitizationWrapperTraitClass + */ +class SanitizationWrapperTraitClass { + use Core; +} + +/** + * Class TestSanitization + */ +class TestSanitization extends WP_UnitTestCase { + + /** + * Sanitization wrapper trait class. + * + * @var SanitizationWrapperTraitClass + */ + private $sanitization; + + /** + * Setup. + */ + public function setUp(): void { + parent::setUp(); + + $this->sanitization = new SanitizationWrapperTraitClass(); + } + + /** + * Test sanitize responsive int json. + */ + public function test_sanitize_responsive_int_json() { + // Test value responsive int json. w/o. suffix. + $input_value = [ + 'mobile' => 1, + 'tablet' => 2, + 'desktop' => 3, + ]; + + $this->do_assertion_for_sanitize_responsive_int_json( $input_value ); + + // Test value responsive int json. w. suffix. + $input_value['suffix'] = [ + 'mobile' => 'px', + 'tablet' => 'em', + 'desktop' => 'rem', + ]; + + $this->do_assertion_for_sanitize_responsive_int_json( $input_value ); + + // Test sanitization with string values. + $input_value = [ + 'mobile' => '1', + 'tablet' => '2', + 'desktop' => '3', + ]; + $expected_value = [ + 'mobile' => 1, + 'tablet' => 2, + 'desktop' => 3, + ]; + + $this->do_assertion_for_sanitize_responsive_int_json( $input_value, wp_json_encode( $expected_value ) ); + + // Test sanitization with string values and suffix. + $input_value = [ + 'mobile' => '1', + 'tablet' => '2', + 'desktop' => '3', + 'suffix' => [ + 'mobile' => 'px', + 'tablet' => 'em', + 'desktop' => 'rem', + ], + ]; + $expected_value = [ + 'mobile' => 1, + 'tablet' => 2, + 'desktop' => 3, + 'suffix' => [ + 'mobile' => 'px', + 'tablet' => 'em', + 'desktop' => 'rem', + ], + ]; + + $this->do_assertion_for_sanitize_responsive_int_json( $input_value, wp_json_encode( $expected_value ) ); + + // Test that for invalid input or failed sanitization the default value is returned. + $expected_default = '{"mobile":0,"tablet":0,"desktop":0}'; + $input_value = 'invalid'; + $this->do_assertion_for_sanitize_responsive_int_json( $input_value, $expected_default ); + + $input_value = []; + $this->do_assertion_for_sanitize_responsive_int_json( $input_value, $expected_default ); + + // Test partial valid input. + $expected_value = '{"mobile":1,"tablet":0,"desktop":0}'; + $input_value = [ + 'mobile' => 1, + ]; + $this->do_assertion_for_sanitize_responsive_int_json( $input_value, $expected_value ); + + // Test partial valid input with suffix. + $expected_value = '{"mobile":1,"tablet":0,"desktop":0,"suffix":{"mobile":"rem","tablet":"px","desktop":"px"}}'; + $input_value = [ + 'mobile' => 1, + 'suffix' => [ + 'mobile' => 'rem', + ], + ]; + $this->do_assertion_for_sanitize_responsive_int_json( $input_value, $expected_value ); + } + + /** + * Private reusable function for the assertion of sanitize responsive int json. + * + * @param array $input_value Input value. + * @param string $expected_value Expected value. + */ + private function do_assertion_for_sanitize_responsive_int_json( $input_value, $expected_value = null ) { + $inout_json = wp_json_encode( $input_value ); + $sanitized_value = $this->sanitization->sanitize_responsive_int_json( $inout_json ); + + if ( $expected_value === null ) { + $expected_value = $inout_json; + } + + $this->assertEquals( $expected_value, $sanitized_value ); + } +}