Phuedx has uploaded a new change for review.
https://gerrit.wikimedia.org/r/141248
Change subject: Configure logged in session length independantly
......................................................................
Configure logged in session length independantly
* Add the $wgLoginCookieExpiration configuration variable, which
dictates when the cookies set by User#setCookies expire
* Default $wgLoginCookieExpiration to 0 so that existing behaviour isn't
affected
Bug: 66699
Change-Id: I0cc24524e4d7d9d1d21c9fa8a28c7c76b677b96c
---
M includes/DefaultSettings.php
M includes/User.php
M tests/TestsAutoLoader.php
M tests/phpunit/includes/UserTest.php
A tests/phpunit/mocks/MockWebRequest.php
5 files changed, 128 insertions(+), 1 deletion(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core
refs/changes/48/141248/1
diff --git a/includes/DefaultSettings.php b/includes/DefaultSettings.php
index bc7959b..6e61d20 100644
--- a/includes/DefaultSettings.php
+++ b/includes/DefaultSettings.php
@@ -4850,6 +4850,12 @@
$wgCookieExpiration = 180 * 86400;
/**
+ * Default login cookie lifetime, in seconds. If $wgLoginCookieExpiration is 0
+ * then $wgCookieExpiration is used.
+ */
+$wgLoginCookieExpiration = 0;
+
+/**
* Set to set an explicit domain on the login cookies eg, "justthis.domain.org"
* or ".any.subdomain.net"
*/
diff --git a/includes/User.php b/includes/User.php
index f2d3d9b..1675b65 100644
--- a/includes/User.php
+++ b/includes/User.php
@@ -3307,7 +3307,7 @@
* @param string $name Name of the cookie to set
* @param string $value Value to set
* @param int $exp Expiration time, as a UNIX time value;
- * if 0 or not specified, use the default
$wgCookieExpiration
+ * if 0 or not specified, use the default
$wgLoginCookieExpiration
* @param bool $secure
* true: Force setting the secure attribute when setting the cookie
* false: Force NOT setting the secure attribute when setting the
cookie
@@ -3315,6 +3315,12 @@
* @param array $params Array of options sent passed to
WebResponse::setcookie()
*/
protected function setCookie( $name, $value, $exp = 0, $secure = null,
$params = array() ) {
+ global $wgLoginCookieExpiration;
+
+ if ( !$exp && $wgLoginCookieExpiration ) {
+ $exp = time() + $wgLoginCookieExpiration;
+ }
+
$params['secure'] = $secure;
$this->getRequest()->response()->setcookie( $name, $value,
$exp, $params );
}
diff --git a/tests/TestsAutoLoader.php b/tests/TestsAutoLoader.php
index 37cf68f..10956c8 100644
--- a/tests/TestsAutoLoader.php
+++ b/tests/TestsAutoLoader.php
@@ -95,6 +95,7 @@
'MockImageHandler' =>
"$testDir/phpunit/mocks/media/MockImageHandler.php",
'MockSvgHandler' => "$testDir/phpunit/mocks/media/MockSvgHandler.php",
'MockDjVuHandler' => "$testDir/phpunit/mocks/media/MockDjVuHandler.php",
+ 'MockWebRequest' => "$testDir/phpunit/mocks/MockWebRequest.php",
# tests/parser
'NewParserTest' => "$testDir/phpunit/includes/parser/NewParserTest.php",
diff --git a/tests/phpunit/includes/UserTest.php
b/tests/phpunit/includes/UserTest.php
index a19d035..0b807a8 100644
--- a/tests/phpunit/includes/UserTest.php
+++ b/tests/phpunit/includes/UserTest.php
@@ -7,6 +7,12 @@
* @group Database
*/
class UserTest extends MediaWikiTestCase {
+
+ /**
+ * @var WebRequest
+ */
+ protected $request;
+
/**
* @var User
*/
@@ -295,4 +301,86 @@
$this->assertFalse( $user->checkPasswordValidity( 'Passpass'
)->isGood() );
$this->assertEquals( 'password-login-forbidden',
$user->getPasswordValidity( 'Passpass' ) );
}
+
+ public static function setCookieDataProvider() {
+ $data = array();
+
+ // If exp is zero and $wgLoginCookieExpiration is non-zero,
then the
+ // expiry passed to setcookie is time() +
$wgLoginCookieExpiration.
+ $data[] = array(
+ 0,
+ 1234,
+ time() + 1234,
+ );
+
+ // If exp is non-zero, then exp is passed to setcookie
regardless of the
+ // value of $wgLoginCookieExpiration.
+ $data[] = array(
+ 123456789,
+ 1234,
+ 123456789,
+ );
+
+ // If exp and $wgLoginCookieExpiration are zero, then the
expiry passed
+ // to setcookie is zero.
+ $data[] = array(
+ 0,
+ 0,
+ 0,
+ true,
+ );
+
+ return $data;
+ }
+
+ /**
+ * @dataProvider setCookieDataProvider
+ *
+ * @covers User::getRequest
+ * @covers User::setCookie
+ */
+ public function testSetCookie($expiry, $actualWgLoginCookieExpiration,
$expectedExpiry) {
+ global $wgLoginCookieExpiration;
+
+ // XXX (phuedx, 2014/09/20): The following spies on a call to
+ // WebResponse#setcookie from User#setCookie and records the
"exp"
+ // argument.
+ $actualExpiry = null;
+ $response = $this->getMock( 'WebResponse' );
+ $response->expects( $this->once() )
+ ->method( 'setcookie' )
+ ->will( $this->returnCallback( function () use (
&$actualExpiry ) {
+ $arguments = func_get_args();
+ $actualExpiry = $arguments[ 2 ];
+ } ) );
+ $request = new MockWebRequest( $response );
+ $user = new UserProxy( User::newFromSession( $request ) );
+
+ $backupWgLoginCookieExpiration = $wgLoginCookieExpiration;
+ $wgLoginCookieExpiration = $actualWgLoginCookieExpiration;
+
+ $user->setCookie( 'name', 'value', $expiry );
+
+ $wgLoginCookieExpiration = $backupWgLoginCookieExpiration;
+
+ // TODO (phuedx, 2014/09/21): ± 2 seconds compensates for
slow-running
+ // tests. Find out if this value is appropriate for the CI
environment.
+ $this->assertEquals( $expectedExpiry, $actualExpiry, '', 2 );
+ }
+}
+
+class UserProxy extends User {
+
+ /**
+ * @var User
+ */
+ protected $user;
+
+ public function __construct( User $user ) {
+ $this->user = $user;
+ }
+
+ public function setCookie( $name, $value, $exp = 0, $secure = null,
$params = array() ) {
+ $this->user->setCookie( $name, $value, $exp, $secure, $params );
+ }
}
diff --git a/tests/phpunit/mocks/MockWebRequest.php
b/tests/phpunit/mocks/MockWebRequest.php
new file mode 100644
index 0000000..ca79bb9
--- /dev/null
+++ b/tests/phpunit/mocks/MockWebRequest.php
@@ -0,0 +1,26 @@
+<?php
+
+/**
+ * A mock WebRequest.
+ *
+ * If the code under test accesses the response via the request (see
+ * WebRequest#response), then you might be able to use this mock to simplify
+ * your tests.
+ */
+class MockWebRequest extends WebRequest
+{
+ /**
+ * @var WebResponse
+ */
+ protected $response;
+
+ public function __construct( WebResponse $response ) {
+ parent::__construct();
+
+ $this->response = $response;
+ }
+
+ public function response() {
+ return $this->response;
+ }
+}
--
To view, visit https://gerrit.wikimedia.org/r/141248
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I0cc24524e4d7d9d1d21c9fa8a28c7c76b677b96c
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Phuedx <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits