-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: added unit test for the modified sanitization method
- Loading branch information
1 parent
69cd74c
commit 3d337a8
Showing
4 changed files
with
158 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,3 +24,4 @@ logs.log | |
/playwright-report/ | ||
/playwright/.cache/ | ||
/playwright/.auth/ | ||
.phpunit.result.cache |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); | ||
} | ||
} |