Skip to content

Commit

Permalink
Merge pull request #34820 from owncloud/stable10-sync-add-events-for-…
Browse files Browse the repository at this point in the history
…user-preferences

[stable10] Sync code for 'add events for user preference changes' 31266 and 31307
  • Loading branch information
phil-davis authored Mar 19, 2019
2 parents 053760b + e6337ee commit 1f38f9e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 25 deletions.
18 changes: 9 additions & 9 deletions lib/private/AllConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@
* Class to combine all the configuration options ownCloud offers
*/
class AllConfig implements IConfig {

/** @var SystemConfig */
private $systemConfig;

/** @var IDBConnection */
private $connection;

/** @var EventDispatcher */
/** @var EventDispatcher */
private $eventDispatcher;

/**
* 3 dimensional array with the following structure:
* [ $userId =>
Expand Down Expand Up @@ -306,7 +306,7 @@ public function deleteUserValue($userId, $appName, $key) {
$this->eventDispatcher->dispatch('userpreferences.beforeDeleteValue',
new GenericEvent(null, $arguments));

$sql = 'DELETE FROM `*PREFIX*preferences` '.
$sql = 'DELETE FROM `*PREFIX*preferences` '.
'WHERE `userid` = ? AND `appid` = ? AND `configkey` = ?';
$this->connection->executeUpdate($sql, [$userId, $appName, $key]);

Expand All @@ -332,7 +332,7 @@ public function deleteAllUserValues($userId) {
$arguments = ['uid' => $userId];
$this->eventDispatcher->dispatch('userpreferences.beforeDeleteUser', new GenericEvent(null, $arguments));

$sql = 'DELETE FROM `*PREFIX*preferences` '.
$sql = 'DELETE FROM `*PREFIX*preferences` '.
'WHERE `userid` = ?';
$this->connection->executeUpdate($sql, [$userId]);

Expand All @@ -356,7 +356,7 @@ public function deleteAppFromAllUsers($appName) {
$arguments = ['app' => $appName];
$this->eventDispatcher->dispatch('userpreferences.beforeDeleteApp', new GenericEvent(null, $arguments));

$sql = 'DELETE FROM `*PREFIX*preferences` '.
$sql = 'DELETE FROM `*PREFIX*preferences` '.
'WHERE `appid` = ?';
$this->connection->executeUpdate($sql, [$appName]);

Expand Down Expand Up @@ -426,10 +426,10 @@ public function getUserValueForUsers($appName, $key, $userIds) {

$placeholders = (\sizeof($chunk) === 50) ? $placeholders50 : \implode(',', \array_fill(0, \sizeof($chunk), '?'));

$query = 'SELECT `userid`, `configvalue` ' .
'FROM `*PREFIX*preferences` ' .
'WHERE `appid` = ? AND `configkey` = ? ' .
'AND `userid` IN (' . $placeholders . ')';
$query = 'SELECT `userid`, `configvalue` ' .
'FROM `*PREFIX*preferences` ' .
'WHERE `appid` = ? AND `configkey` = ? ' .
'AND `userid` IN (' . $placeholders . ')';
$result = $this->connection->executeQuery($query, $queryParams);

while ($row = $result->fetch()) {
Expand Down
38 changes: 22 additions & 16 deletions tests/lib/AllConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class AllConfigTest extends \Test\TestCase {
/** @var \OCP\IDBConnection */
protected $connection;

/** @var EventDispatcher */
protected $eventDispatcher;

protected function getConfig($systemConfig = null, $connection = null) {
Expand Down Expand Up @@ -54,9 +55,9 @@ public function testDeleteUserValue() {
$config->deleteUserValue('userDelete', 'appDelete', 'keyDelete');

$result = $this->connection->executeQuery(
'SELECT COUNT(*) AS `count` FROM `*PREFIX*preferences` WHERE `userid` = ?',
['userDelete']
)->fetch();
'SELECT COUNT(*) AS `count` FROM `*PREFIX*preferences` WHERE `userid` = ?',
['userDelete']
)->fetch();
$actualCount = $result['count'];

$this->assertEquals(0, $actualCount, 'There was one value in the database and after the tests there should be no entry left.');
Expand All @@ -66,14 +67,16 @@ public function testSetUserValue() {
$selectAllSQL = 'SELECT `userid`, `appid`, `configkey`, `configvalue` FROM `*PREFIX*preferences` WHERE `userid` = ?';
$config = $this->getConfig();

$event = new GenericEvent(null, [
'uid' => 'userSet', 'key' => 'keySet', 'value' => 'valueSet',
'app' => 'appSet', 'precondition' => null
]);
$event2 = new GenericEvent(null, [
'uid' => 'userSet', 'key' => 'keySet', 'value' => 'valueSet2',
'app' => 'appSet', 'precondition' => null
]);
$event = new GenericEvent(null,
[
'uid' => 'userSet', 'key' => 'keySet', 'value' => 'valueSet',
'app' => 'appSet', 'precondition' => null
]);
$event2 = new GenericEvent(null,
[
'uid' => 'userSet', 'key' => 'keySet', 'value' => 'valueSet2',
'app' => 'appSet', 'precondition' => null
]);
$event3 = new GenericEvent(null, [
'uid' => 'userSet', 'key' => 'keySet', 'app' => 'appSet'
]);
Expand All @@ -88,6 +91,7 @@ public function testSetUserValue() {
[$this->equalTo('userpreferences.beforeDeleteValue'), $this->equalTo($event3)],
[$this->equalTo('userpreferences.afterDeleteValue'), $this->equalTo($event3)]
);

$config->setUserValue('userSet', 'appSet', 'keySet', 'valueSet');

$result = $this->connection->executeQuery($selectAllSQL, ['userSet'])->fetchAll();
Expand Down Expand Up @@ -221,7 +225,7 @@ public function testSetUserValueUnchanged() {
$connectionMock->expects($this->once())
->method('executeQuery')
->with($this->equalTo('SELECT `configvalue` FROM `*PREFIX*preferences` '.
'WHERE `userid` = ? AND `appid` = ? AND `configkey` = ?'),
'WHERE `userid` = ? AND `appid` = ? AND `configkey` = ?'),
$this->equalTo(['userSetUnchanged', 'appSetUnchanged', 'keySetUnchanged']))
->will($this->returnValue($resultMock));
$connectionMock->expects($this->never())
Expand Down Expand Up @@ -333,10 +337,10 @@ public function testGetUserValueForUsers() {
$value = $config->getUserValueForUsers('appFetch2', 'keyFetch1',
['userFetch1', 'userFetch2', 'userFetch3', 'userFetch5']);
$this->assertEquals([
'userFetch1' => 'value1',
'userFetch2' => 'value2',
'userFetch3' => 3,
'userFetch5' => 'value5'
'userFetch1' => 'value1',
'userFetch2' => 'value2',
'userFetch3' => 3,
'userFetch5' => 'value5'
], $value);

$value = $config->getUserValueForUsers('appFetch2', 'keyFetch1',
Expand Down Expand Up @@ -378,6 +382,7 @@ public function testDeleteAllUserValues() {
[$this->equalTo('userpreferences.beforeDeleteUser'), $this->equalTo($event)],
[$this->equalTo('userpreferences.afterDeleteUser'), $this->equalTo($event)]
);

$config->deleteAllUserValues('userFetch3');

$result = $this->connection->executeQuery(
Expand Down Expand Up @@ -420,6 +425,7 @@ public function testDeleteAppFromAllUsers() {
[$this->equalTo('userpreferences.beforeDeleteApp'), $this->equalTo(new GenericEvent(null, ['app' => 'appFetch2']))],
[$this->equalTo('userpreferences.afterDeleteApp'), $this->equalTo(new GenericEvent(null, ['app' => 'appFetch2']))]
);

$config->deleteAppFromAllUsers('appFetch1');

$result = $this->connection->executeQuery(
Expand Down

0 comments on commit 1f38f9e

Please sign in to comment.