diff options
author | Antoine Musso <hashar@free.fr> | 2013-05-22 16:39:31 +0200 |
---|---|---|
committer | Tyler Anthony Romeo <tylerromeo@gmail.com> | 2014-01-24 11:09:11 -0800 |
commit | ff355e87e2f7a41a87c37f43232a44113cb033e0 (patch) | |
tree | 5d557acfa26ccd28b8255dd6cfded7c0bfd60d2f /tests/phpunit/includes/UserTest.php | |
parent | c0d3e85b06d628e5c4a7c93cfe5fb7587f9118f0 (diff) | |
download | mediawikicore-ff355e87e2f7a41a87c37f43232a44113cb033e0.tar.gz mediawikicore-ff355e87e2f7a41a87c37f43232a44113cb033e0.zip |
User::saveOptions() optimization
Since we only want to save non default user options, we have to strip
out any user option that match the default ones. We did that by calling
User::getDefaultOption( 'some option name' ); on each of the option.
Since the User mOptions property is a merge of the default option, we
end up doing a lot of unneeded processing. The loop roughly looks like:
User::getDefaultOption()
User::getDefaultOptions()
Language->getCode()
SearchEngine::searchableNamespaces()
language->getNamespaces()
wfRunHooks('SearcheableNamespaces')
wfRunHooks('UserGetDefaultOptions')
For EACH of the mOptions.
Instead this patch does an array_diff to strip out from mObjects any
default option. We still skip options whose value is false or null.
Test provided to make sure we only save what we want.
Change-Id: Ie98d3a17edab74401ed32f759ba11f723b56e376
Diffstat (limited to 'tests/phpunit/includes/UserTest.php')
-rw-r--r-- | tests/phpunit/includes/UserTest.php | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/tests/phpunit/includes/UserTest.php b/tests/phpunit/includes/UserTest.php index ff33e825ee31..b7df3f617de7 100644 --- a/tests/phpunit/includes/UserTest.php +++ b/tests/phpunit/includes/UserTest.php @@ -234,4 +234,49 @@ class UserTest extends MediaWikiTestCase { $this->assertEquals( $wgDefaultUserOptions['cols'], $this->user->getOption( 'cols' ) ); $this->assertEquals( 'test', $this->user->getOption( 'someoption' ) ); } + + /** + * Helper, fetch user properties from the database. + * @param int $userId + */ + function dbUserProperties( $userId ) { + $res = wfGetDB( DB_SLAVE )->select( + 'user_properties', + array( 'up_property', 'up_value' ), + array( 'up_user' => $userId ), + __METHOD__ + ); + $ret = array(); + foreach( $res as $row ) { + $ret[$row->up_property] = $row->up_value; + } + return $ret; + } + + public function testOnlySaveChangedOptions() { + $user = User::newFromName( 'UnitTestUser2' ); + $user->addToDatabase(); + + // Fresh user only has default, so nothing should be in the DB + $dbProps = $this->dbUserProperties( $user->getId() ); + $this->assertEmpty( $dbProps, + "A new user should not have any user property saved in the DB" ); + + // Make sure we only save the altered option + $user->setOption( 'changed_opt', 'alix_20281' ); + $user->setOption( 'switch', 1 ); + $user->setOption( 'anotherswitch', 1 ); + $user->saveSettings(); + + $expected = array ( + 'changed_opt' => 'alix_20281', + 'switch' => '1', + 'anotherswitch' => '1', + ); + $dbProps = $this->dbUserProperties( $user->getId() ); + + $this->assertEquals( $expected, $dbProps, + "non default options should be saved, and default ones should not" ); + + } } |