2012-10-18 09:33:15 +00:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* Test class for ArrayUtils class
|
|
|
|
|
*/
|
2018-02-17 12:29:13 +00:00
|
|
|
class ArrayUtilsTest extends PHPUnit\Framework\TestCase {
|
2017-12-29 23:22:37 +00:00
|
|
|
|
|
|
|
|
use MediaWikiCoversValidator;
|
2012-10-18 09:33:15 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers ArrayUtils::findLowerBound
|
|
|
|
|
* @dataProvider provideFindLowerBound
|
|
|
|
|
*/
|
2019-10-09 18:24:07 +00:00
|
|
|
public function testFindLowerBound(
|
2012-10-18 09:33:15 +00:00
|
|
|
$valueCallback, $valueCount, $comparisonCallback, $target, $expected
|
|
|
|
|
) {
|
|
|
|
|
$this->assertSame(
|
2021-01-12 12:10:14 +00:00
|
|
|
$expected,
|
2012-10-18 09:33:15 +00:00
|
|
|
ArrayUtils::findLowerBound(
|
|
|
|
|
$valueCallback, $valueCount, $comparisonCallback, $target
|
2021-01-12 12:10:14 +00:00
|
|
|
)
|
2012-10-18 09:33:15 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-09 18:24:07 +00:00
|
|
|
public function provideFindLowerBound() {
|
2016-02-04 21:09:07 +00:00
|
|
|
$indexValueCallback = function ( $size ) {
|
|
|
|
|
return function ( $val ) use ( $size ) {
|
|
|
|
|
$this->assertTrue( $val >= 0 );
|
|
|
|
|
$this->assertTrue( $val < $size );
|
2012-10-18 09:33:15 +00:00
|
|
|
return $val;
|
|
|
|
|
};
|
|
|
|
|
};
|
2021-02-06 19:30:20 +00:00
|
|
|
$comparisonCallback = static function ( $a, $b ) {
|
2012-10-18 09:33:15 +00:00
|
|
|
return $a - $b;
|
|
|
|
|
};
|
|
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
return [
|
|
|
|
|
[
|
2012-10-18 09:33:15 +00:00
|
|
|
$indexValueCallback( 0 ),
|
|
|
|
|
0,
|
|
|
|
|
$comparisonCallback,
|
|
|
|
|
1,
|
|
|
|
|
false,
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
[
|
2012-10-18 09:33:15 +00:00
|
|
|
$indexValueCallback( 1 ),
|
|
|
|
|
1,
|
|
|
|
|
$comparisonCallback,
|
|
|
|
|
-1,
|
|
|
|
|
false,
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
[
|
2012-10-18 09:33:15 +00:00
|
|
|
$indexValueCallback( 1 ),
|
|
|
|
|
1,
|
|
|
|
|
$comparisonCallback,
|
|
|
|
|
0,
|
|
|
|
|
0,
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
[
|
2012-10-18 09:33:15 +00:00
|
|
|
$indexValueCallback( 1 ),
|
|
|
|
|
1,
|
|
|
|
|
$comparisonCallback,
|
|
|
|
|
1,
|
|
|
|
|
0,
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
[
|
2012-10-18 09:33:15 +00:00
|
|
|
$indexValueCallback( 2 ),
|
|
|
|
|
2,
|
|
|
|
|
$comparisonCallback,
|
|
|
|
|
-1,
|
|
|
|
|
false,
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
[
|
2012-10-18 09:33:15 +00:00
|
|
|
$indexValueCallback( 2 ),
|
|
|
|
|
2,
|
|
|
|
|
$comparisonCallback,
|
|
|
|
|
0,
|
|
|
|
|
0,
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
[
|
2012-10-18 09:33:15 +00:00
|
|
|
$indexValueCallback( 2 ),
|
|
|
|
|
2,
|
|
|
|
|
$comparisonCallback,
|
|
|
|
|
0.5,
|
|
|
|
|
0,
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
[
|
2012-10-18 09:33:15 +00:00
|
|
|
$indexValueCallback( 2 ),
|
|
|
|
|
2,
|
|
|
|
|
$comparisonCallback,
|
|
|
|
|
1,
|
|
|
|
|
1,
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
[
|
2012-10-18 09:33:15 +00:00
|
|
|
$indexValueCallback( 2 ),
|
|
|
|
|
2,
|
|
|
|
|
$comparisonCallback,
|
|
|
|
|
1.5,
|
|
|
|
|
1,
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
[
|
2012-10-18 09:33:15 +00:00
|
|
|
$indexValueCallback( 3 ),
|
|
|
|
|
3,
|
|
|
|
|
$comparisonCallback,
|
|
|
|
|
1,
|
|
|
|
|
1,
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
[
|
2012-10-18 09:33:15 +00:00
|
|
|
$indexValueCallback( 3 ),
|
|
|
|
|
3,
|
|
|
|
|
$comparisonCallback,
|
|
|
|
|
1.5,
|
|
|
|
|
1,
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
[
|
2012-10-18 09:33:15 +00:00
|
|
|
$indexValueCallback( 3 ),
|
|
|
|
|
3,
|
|
|
|
|
$comparisonCallback,
|
|
|
|
|
2,
|
|
|
|
|
2,
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
[
|
2012-10-18 09:33:15 +00:00
|
|
|
$indexValueCallback( 3 ),
|
|
|
|
|
3,
|
|
|
|
|
$comparisonCallback,
|
|
|
|
|
3,
|
|
|
|
|
2,
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
];
|
2012-10-18 09:33:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers ArrayUtils::arrayDiffAssocRecursive
|
|
|
|
|
* @dataProvider provideArrayDiffAssocRecursive
|
|
|
|
|
*/
|
2019-10-09 18:24:07 +00:00
|
|
|
public function testArrayDiffAssocRecursive( $expected, ...$args ) {
|
2020-05-29 06:46:30 +00:00
|
|
|
$this->assertEquals( $expected, ArrayUtils::arrayDiffAssocRecursive( ...$args ) );
|
2012-10-18 09:33:15 +00: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TP to HTTPS redirect unconditional and suppresses the
forceHTTPS cookie.
* It makes session cookies be secure.
* In the Action API, it triggers the existing deprecation warning and
avoids more expensive user/session checks.
* In login and signup, it suppresses the old hidden form fields for
protocol switching.
* It hides the prefershttps user preference.
Other changes:
* Factor out the HTTPS redirect in MediaWiki::main() into
maybeDoHttpsRedirect() and shouldDoHttpRedirect(). Improve
documentation.
* User::requiresHTTPS() reflects $wgForceHTTPS whereas the Session
concept of "force HTTPS" does not. The documentation of
User::requiresHTTPS() says that it includes configuration, and
retaining this definition was beneficial for some callers. Whereas
Session::shouldForceHTTPS() was used fairly narrowly as the value
of the forceHTTPS cookie, and injecting configuration into it is not
so easy or beneficial, so I left it as it was, except for clarifying
the documentation.
* Deprecate the following hooks: BeforeHttpsRedirect, UserRequiresHTTPS,
CanIPUseHTTPS. No known extension uses them, and they're not compatible
with the long-term goal of ending support for mixed-protocol wikis.
BeforeHttpsRedirect was documented as unstable from its inception.
CanIPUseHTTPS was a WMF config hack now superseded by GFOC's SNI
sniffing.
* For tests which failed with $wgForceHTTPS=true, I mostly split the
tests, testing each configuration value separately.
* Add ArrayUtils::cartesianProduct() as a helper for generating
combinations of boolean options in the session tests.
Bug: T256095
Change-Id: Iefb5ba55af35350dfc7c050f9fb8f4e8a79751cb
2020-06-24 00:56:46 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider provideCartesianProduct
|
|
|
|
|
* @covers ArrayUtils::cartesianProduct
|
|
|
|
|
*/
|
|
|
|
|
public function testCartesianProduct( $inputs, $expected ) {
|
|
|
|
|
$result = ArrayUtils::cartesianProduct( ...$inputs );
|
|
|
|
|
$this->assertSame( $expected, $result );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function provideCartesianProduct() {
|
|
|
|
|
$ab = [ 'a', 'b' ];
|
|
|
|
|
$cd = [ 'c', 'd' ];
|
|
|
|
|
$ac = [ 'a', 'c' ];
|
|
|
|
|
$ad = [ 'a', 'd' ];
|
|
|
|
|
$bc = [ 'b', 'c' ];
|
|
|
|
|
$bd = [ 'b', 'd' ];
|
|
|
|
|
|
|
|
|
|
return [
|
|
|
|
|
'no inputs' => [
|
|
|
|
|
[],
|
|
|
|
|
[]
|
|
|
|
|
],
|
|
|
|
|
'one empty input' => [
|
|
|
|
|
[ [] ],
|
|
|
|
|
[]
|
|
|
|
|
],
|
|
|
|
|
'one non-empty input' => [
|
|
|
|
|
[ $ab ],
|
|
|
|
|
[ [ 'a' ], [ 'b' ] ]
|
|
|
|
|
],
|
|
|
|
|
'non-empty times empty' => [
|
|
|
|
|
[ $ab, [] ],
|
|
|
|
|
[]
|
|
|
|
|
],
|
|
|
|
|
'empty times non-empty' => [
|
|
|
|
|
[ [], $ab ],
|
|
|
|
|
[]
|
|
|
|
|
],
|
|
|
|
|
'ab x cd' => [
|
|
|
|
|
[ $ab, $cd ],
|
|
|
|
|
[ $ac, $ad, $bc, $bd ]
|
|
|
|
|
],
|
|
|
|
|
'keys are ignored' => [
|
|
|
|
|
[
|
|
|
|
|
[ 99 => 'a', 98 => 'b' ],
|
|
|
|
|
[ 97 => 'c', 96 => 'd' ],
|
|
|
|
|
],
|
|
|
|
|
[ $ac, $ad, $bc, $bd ]
|
|
|
|
|
],
|
|
|
|
|
];
|
|
|
|
|
}
|
2012-10-18 09:33:15 +00:00
|
|
|
}
|