wiki.techinc.nl/tests/phpunit/unit/includes/libs/ArrayUtilsTest.php

358 lines
5 KiB
PHP
Raw Normal View History

<?php
/**
* Test class for ArrayUtils class
*/
class ArrayUtilsTest extends PHPUnit\Framework\TestCase {
use MediaWikiCoversValidator;
/**
* @covers ArrayUtils::findLowerBound
* @dataProvider provideFindLowerBound
*/
public function testFindLowerBound(
$valueCallback, $valueCount, $comparisonCallback, $target, $expected
) {
$this->assertSame(
$expected,
ArrayUtils::findLowerBound(
$valueCallback, $valueCount, $comparisonCallback, $target
)
);
}
public function provideFindLowerBound() {
$indexValueCallback = function ( $size ) {
return function ( $val ) use ( $size ) {
$this->assertTrue( $val >= 0 );
$this->assertTrue( $val < $size );
return $val;
};
};
$comparisonCallback = static function ( $a, $b ) {
return $a - $b;
};
return [
[
$indexValueCallback( 0 ),
0,
$comparisonCallback,
1,
false,
],
[
$indexValueCallback( 1 ),
1,
$comparisonCallback,
-1,
false,
],
[
$indexValueCallback( 1 ),
1,
$comparisonCallback,
0,
0,
],
[
$indexValueCallback( 1 ),
1,
$comparisonCallback,
1,
0,
],
[
$indexValueCallback( 2 ),
2,
$comparisonCallback,
-1,
false,
],
[
$indexValueCallback( 2 ),
2,
$comparisonCallback,
0,
0,
],
[
$indexValueCallback( 2 ),
2,
$comparisonCallback,
0.5,
0,
],
[
$indexValueCallback( 2 ),
2,
$comparisonCallback,
1,
1,
],
[
$indexValueCallback( 2 ),
2,
$comparisonCallback,
1.5,
1,
],
[
$indexValueCallback( 3 ),
3,
$comparisonCallback,
1,
1,
],
[
$indexValueCallback( 3 ),
3,
$comparisonCallback,
1.5,
1,
],
[
$indexValueCallback( 3 ),
3,
$comparisonCallback,
2,
2,
],
[
$indexValueCallback( 3 ),
3,
$comparisonCallback,
3,
2,
],
];
}
/**
* @covers ArrayUtils::arrayDiffAssocRecursive
* @dataProvider provideArrayDiffAssocRecursive
*/
public function testArrayDiffAssocRecursive( $expected, ...$args ) {
$this->assertEquals( $expected, ArrayUtils::arrayDi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0 => 2 ] ] ],
],
[
[ 1 => [ 1 => 2 ] ],
[ 1, [ [ 2, 3 ], 2 ] ],
[ 1, [ [ 1 => 3 ] ] ],
[ 1 => [ [ 2 ] ] ],
],
[
[],
[ 1, [ [ 2, 3 ], 2 ] ],
[ 1 => [ 1 => 2, 0 => [ 1 => 3, 0 => 2 ] ], 0 => 1 ],
],
[
[],
[ 1, [ [ 2, 3 ], 2 ] ],
[ 1 => [ 1 => 2 ] ],
[ 1 => [ [ 1 => 3 ] ] ],
[ 1 => [ [ 2 ] ] ],
[ 1 ],
],
];
}
Introduce $wgForceHTTPS Add $wgForceHTTPS. When set to true: * It makes the HTTP 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 ]
],
];
}
}