Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[stable12] Trigger changeUser hook only on real changes #5216

Merged
merged 1 commit into from
Jun 2, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions lib/private/User/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,9 @@ public function setEMailAddress($mailAddress) {
} else {
$this->config->setUserValue($this->uid, 'settings', 'email', $mailAddress);
}
$this->triggerChange('eMailAddress', $mailAddress, $oldMailAddress);
if($oldMailAddress !== $mailAddress) {
$this->triggerChange('eMailAddress', $mailAddress, $oldMailAddress);
}
}

/**
Expand Down Expand Up @@ -388,12 +390,15 @@ public function getQuota() {
* @since 9.0.0
*/
public function setQuota($quota) {
$oldQuota = $this->config->getUserValue($this->uid, 'files', 'quota', '');
if($quota !== 'none' and $quota !== 'default') {
$quota = OC_Helper::computerFileSize($quota);
$quota = OC_Helper::humanFileSize($quota);
}
$this->config->setUserValue($this->uid, 'files', 'quota', $quota);
$this->triggerChange('quota', $quota);
if($quota !== $oldQuota) {
$this->triggerChange('quota', $quota);
}
}

/**
Expand Down
96 changes: 95 additions & 1 deletion tests/lib/User/UserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use OCP\IUser;
use OCP\Notification\IManager as INotificationManager;
use OCP\Notification\INotification;
use OCP\UserInterface;
use Test\TestCase;

/**
Expand Down Expand Up @@ -614,7 +615,7 @@ public function testSetEMailAddressEmpty() {

public function testSetEMailAddress() {
/**
* @var Backend | \PHPUnit_Framework_MockObject_MockObject $backend
* @var UserInterface | \PHPUnit_Framework_MockObject_MockObject $backend
*/
$backend = $this->createMock(\Test\Util\User\Dummy::class);

Expand Down Expand Up @@ -649,6 +650,99 @@ public function testSetEMailAddress() {
$user->setEMailAddress('foo@bar.com');
}

public function testSetEMailAddressNoChange() {
/**
* @var UserInterface | \PHPUnit_Framework_MockObject_MockObject $backend
*/
$backend = $this->createMock(\Test\Util\User\Dummy::class);

/** @var PublicEmitter|\PHPUnit_Framework_MockObject_MockObject $emitter */
$emitter = $this->createMock(PublicEmitter::class);
$emitter->expects($this->never())
->method('emit');

$config = $this->createMock(IConfig::class);
$config->expects($this->any())
->method('getUserValue')
->willReturn('foo@bar.com');
$config->expects($this->once())
->method('setUserValue')
->with(
'foo',
'settings',
'email',
'foo@bar.com'
);

$user = new User('foo', $backend, $emitter, $config);
$user->setEMailAddress('foo@bar.com');
}

public function testSetQuota() {
/**
* @var UserInterface | \PHPUnit_Framework_MockObject_MockObject $backend
*/
$backend = $this->createMock(\Test\Util\User\Dummy::class);

$test = $this;
$hooksCalled = 0;

/**
* @param IUser $user
* @param string $feature
* @param string $value
*/
$hook = function (IUser $user, $feature, $value) use ($test, &$hooksCalled) {
$hooksCalled++;
$test->assertEquals('quota', $feature);
$test->assertEquals('23 TB', $value);
};

$emitter = new PublicEmitter();
$emitter->listen('\OC\User', 'changeUser', $hook);

$config = $this->createMock(IConfig::class);
$config->expects($this->once())
->method('setUserValue')
->with(
'foo',
'files',
'quota',
'23 TB'
);

$user = new User('foo', $backend, $emitter, $config);
$user->setQuota('23 TB');
}

public function testSetQuotaAddressNoChange() {
/**
* @var UserInterface | \PHPUnit_Framework_MockObject_MockObject $backend
*/
$backend = $this->createMock(\Test\Util\User\Dummy::class);

/** @var PublicEmitter|\PHPUnit_Framework_MockObject_MockObject $emitter */
$emitter = $this->createMock(PublicEmitter::class);
$emitter->expects($this->never())
->method('emit');

$config = $this->createMock(IConfig::class);
$config->expects($this->any())
->method('getUserValue')
->willReturn('23 TB');
$config->expects($this->once())
->method('setUserValue')
->with(
'foo',
'files',
'quota',
'23 TB'
);

$user = new User('foo', $backend, $emitter, $config);
$user->setQuota('23 TB');
}

public function testGetLastLogin() {
/**
* @var Backend | \PHPUnit_Framework_MockObject_MockObject $backend
Expand Down