Skip to content

Commit

Permalink
chore: added unit test for the modified sanitization method
Browse files Browse the repository at this point in the history
  • Loading branch information
preda-bogdan committed Mar 30, 2023
1 parent 69cd74c commit 3d337a8
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ logs.log
/playwright-report/
/playwright/.cache/
/playwright/.auth/
.phpunit.result.cache
11 changes: 11 additions & 0 deletions header-footer-grid/Traits/Core.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'] );
}
}
}
Expand Down
9 changes: 3 additions & 6 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,11 @@
convertWarningsToExceptions="true"
>

<testsuites name="General Unit tests ( Requires PHP 5.4) ">
<testsuite >
<testsuites>
<testsuite name="General Unit tests ( Requires PHP 5.4) ">
<directory phpVersion="5.4.0" phpVersionOperator=">=" prefix="test-" suffix=".php">./tests/</directory>
</testsuite>
</testsuites>

<testsuites name="Bail lower php versions( For PHP lower than 5.4) ">
<testsuite>
<testsuite name="Bail lower php versions( For PHP lower than 5.4) ">
<directory phpVersion="5.4.0" phpVersionOperator="lt" prefix="old-" suffix=".php">./tests/old/</directory>
</testsuite>
</testsuites>
Expand Down
143 changes: 143 additions & 0 deletions tests/test-neve-sanitization.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
<?php
/**
* Description test-neve-sanitization.php
*
* Author: Bogdan Preda <[email protected]>
* 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 );
}
}

0 comments on commit 3d337a8

Please sign in to comment.