Skip to content

Commit

Permalink
Build/Test Tools: Splits and improves compat tests.
Browse files Browse the repository at this point in the history
Splits the tests in the `tests/phpunit/tests/compat.php` file up into individual test classes for each function being tested.

Improvements to individual test cases:
* Adds `@covers` tags.
* Adds visibility modifiers to all methods.
* Adds function availability test.
* Where relevant, fixes the assertion parameter order.
* Data provider:
   * Where relevant, reworks a test to use a data provider.
   * Where relevant, renames data provider methods to have a more obvious link to the test it applies to.
   * Makes the data provider more readable by adding keys within the data sets.
   * Moves the data provider below its associated tests.
   * Adds/removes data sets in data providers.
* Makes the actual test code more readable by using descriptive variables and multi-line function calls.
* Adds the `$message` parameter to all assertions when a test method contains more than one assertion.

Specifically for the `_mb_substr()` tests:
* Splits the `test_mb_substr_phpcore()` method into two test methods based on the PHP Core test files they are emulating.
* Makes the actual test code within the `test_mb_substr_phpcore_basic()` method more readable by using descriptive variables and multi-line function calls.
* Splits the data used for the second part of the `test_mb_substr_phpcore()` function, now `test_mb_substr_phpcore_input_type_handling()`, off into a separate data provider with named data sets.
* Removes duplicate data sets from the `data_mb_substr_phpcore_input_type_handling()`. 
   * Why? The PHP native tests test against upper/lowercase `false`, `true`, `null` and some other text string single quote/double quote variations. As things were, those differentiations had been undone when the coding standards were put in place, so in effect those weren't being tested anymore. And as this is userland code, there's no point in adding these differentiations back as they will be handled the same by PHP anyway (and that is safeguarded via the PHP native tests).
* Removes the "undefined variable" and "unset variable" test cases as, while those are relevant to the C code in which PHP is written, they are not relevant for testing userland code and will behave the same as the test passing `null`.

Follow-to [25002], [32364], [42228], [42343], [43034], [43036], [43220], [43571], [45607], [47122], [47198], [48937], [48996], [51415], [51563], [51594].

Props jrf, hellofromTonya.
See #39265, #53363.

git-svn-id: https://develop.svn.wordpress.org/trunk@51852 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
hellofromtonya committed Sep 22, 2021
1 parent e0f5dfd commit 237efb5
Show file tree
Hide file tree
Showing 7 changed files with 619 additions and 325 deletions.
325 changes: 0 additions & 325 deletions tests/phpunit/tests/compat.php

This file was deleted.

65 changes: 65 additions & 0 deletions tests/phpunit/tests/compat/hashHmac.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

/**
* @group compat
*
* @covers ::hash_hmac
* @covers ::_hash_hmac
*/
class Tests_Compat_hashHmac extends WP_UnitTestCase {

/**
* Test that hash_hmac() is always available (either from PHP or WP).
*/
public function test_hash_hmac_availability() {
$this->assertTrue( function_exists( 'hash_hmac' ) );
}

public function test_hash_hmac_simple() {
$data = 'simple';
$key = 'key';

$this->assertSame(
'140d1cb79fa12e2a31f32d35ad0a2723',
_hash_hmac( 'md5', $data, $key ),
'MD5 hash does not match'
);
$this->assertSame(
'993003b95758e0ac2eba451a4c5877eb1bb7b92a',
_hash_hmac( 'sha1', $data, $key ),
'sha1 hash does not match'
);
}

public function test_hash_hmac_padding() {
$data = 'simple';
$key = '65 character key 65 character key 65 character key 65 character k';

$this->assertSame(
'3c1399103807cf12ec38228614416a8c',
_hash_hmac( 'md5', $data, $key ),
'MD5 hash does not match'
);
$this->assertSame(
'4428826d20003e309d6c2a6515891370daf184ea',
_hash_hmac( 'sha1', $data, $key ),
'sha1 hash does not match'
);
}

public function test_hash_hmac_output() {
$data = 'simple';
$key = 'key';

$this->assertSame(
array( 1 => '140d1cb79fa12e2a31f32d35ad0a2723' ),
unpack( 'H32', _hash_hmac( 'md5', $data, $key, true ) ),
'unpacked MD5 hash does not match'
);
$this->assertSame(
array( 1 => '993003b95758e0ac2eba451a4c5877eb1bb7b92a' ),
unpack( 'H40', _hash_hmac( 'sha1', $data, $key, true ) ),
'unpacked sha1 hash does not match'
);
}
}
Loading

0 comments on commit 237efb5

Please sign in to comment.