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

[stable10] Changes updated by rewriting config.php should sent events #31107

Merged
merged 1 commit into from
Apr 16, 2018
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
48 changes: 33 additions & 15 deletions lib/private/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,13 +167,22 @@ public function setValue($key, $value) {
* @return bool True if the file needs to be updated, false otherwise
*/
protected function set($key, $value) {
if (!isset($this->cache[$key]) || $this->cache[$key] !== $value) {
// Add change
$this->cache[$key] = $value;
return true;
}
return $this->emittingCall(function (&$afterArray) use (&$key, &$value) {
if (!isset($this->cache[$key]) || $this->cache[$key] !== $value) {
if (isset($this->cache[$key])) {
$afterArray['update'] = true;
$afterArray['oldvalue'] = $this->cache[$key];
}
// Add change
$this->cache[$key] = $value;
return true;
}

return false;
return false;
},[
'before' => ['key' => $key, 'value' => $value],
'after' => ['key' => $key, 'value' => $value, 'update' => false, 'oldvalue' => null]
], 'config', 'setvalue');
}

/**
Expand All @@ -184,15 +193,18 @@ public function deleteKey($key) {
if ($this->isReadOnly()) {
throw new \Exception('Config file is read only.');
}
$this->emittingCall(function () use (&$key) {
$this->emittingCall(function (&$afterArray) use (&$key) {
if (isset($this->cache[$key])) {
$afterArray['value'] = $this->cache[$key];
}
if ($this->delete($key)) {
// Write changes
$this->writeData();
}
return true;
}, [
'before' => ['key' => $key],
'after' => ['key' => $key]
'before' => ['key' => $key, 'value' => null],
'after' => ['key' => $key, 'value' => null]
], 'config', 'deletevalue');
}

Expand All @@ -203,12 +215,18 @@ public function deleteKey($key) {
* @return bool True if the file needs to be updated, false otherwise
*/
protected function delete($key) {
if (isset($this->cache[$key])) {
// Delete key from cache
unset($this->cache[$key]);
return true;
}
return false;
return $this->emittingCall(function (&$afterArray) use (&$key) {
if (isset($this->cache[$key])) {
$afterArray['value'] = $this->cache[$key];
// Delete key from cache
unset($this->cache[$key]);
return true;
}
return false;
}, [
'before' => ['key' => $key, 'value' => null],
'after' => ['key' => $key, 'value' => null]
], 'config', 'deletevalue');
}

/**
Expand Down
57 changes: 57 additions & 0 deletions tests/lib/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,18 @@ public function testSetValues() {
$this->assertAttributeEquals($this->initialConfig, 'cache', $this->config);
$this->assertStringEqualsFile($this->configFile, self::TESTCONTENT);

$calledBeforeUpdate = [];
\OC::$server->getEventDispatcher()->addListener('config.beforesetvalue',
function (GenericEvent $event) use (&$calledBeforeUpdate) {
$calledBeforeUpdate[] = 'config.beforesetvalue';
$calledBeforeUpdate[] = $event;
});
$calledAfterUpdate = [];
\OC::$server->getEventDispatcher()->addListener('config.aftersetvalue',
function (GenericEvent $event) use (&$calledAfterUpdate) {
$calledAfterUpdate[] = 'config.aftersetvalue';
$calledAfterUpdate[] = $event;
});
$this->config->setValues([
'foo' => 'moo',
'alcohol_free' => null,
Expand All @@ -128,10 +140,55 @@ public function testSetValues() {
$expectedConfig['foo'] = 'moo';
unset($expectedConfig['alcohol_free']);
$this->assertAttributeEquals($expectedConfig, 'cache', $this->config);
$this->assertInstanceOf(GenericEvent::class, $calledBeforeUpdate[1]);
$this->assertInstanceOf(GenericEvent::class, $calledAfterUpdate[1]);
$this->assertEquals('config.beforesetvalue', $calledBeforeUpdate[0]);
$this->assertEquals('config.aftersetvalue', $calledAfterUpdate[0]);
$this->assertArrayHasKey('key', $calledBeforeUpdate[1]);
$this->assertEquals('foo', $calledBeforeUpdate[1]->getArgument('key'));
$this->assertArrayHasKey('value', $calledBeforeUpdate[1]);
$this->assertEquals('moo', $calledBeforeUpdate[1]->getArgument('value'));
$this->assertArrayHasKey('key', $calledAfterUpdate[1]);
$this->assertEquals('foo', $calledAfterUpdate[1]->getArgument('key'));
$this->assertArrayHasKey('value', $calledAfterUpdate[1]);
$this->assertEquals('moo', $calledAfterUpdate[1]->getArgument('value'));
$this->assertArrayHasKey('update', $calledAfterUpdate[1]);
$this->assertTrue($calledAfterUpdate[1]->getArgument('update'));
$this->assertArrayHasKey('oldvalue', $calledAfterUpdate[1]);
$this->assertEquals('bar', $calledAfterUpdate[1]->getArgument('oldvalue'));

$expected = "<?php\n\$CONFIG = array (\n 'foo' => 'moo',\n 'beers' => \n array (\n 0 => 'Appenzeller',\n " .
" 1 => 'Guinness',\n 2 => 'Kölsch',\n ),\n);\n";
$this->assertStringEqualsFile($this->configFile, $expected);

$calledBeforeDelete = [];
\OC::$server->getEventDispatcher()->addListener('config.beforedeletevalue',
function (GenericEvent $event) use (&$calledBeforeDelete) {
$calledBeforeDelete[] = 'config.beforedeletevalue';
$calledBeforeDelete[] = $event;
});
$calledAfterDelete = [];
\OC::$server->getEventDispatcher()->addListener('config.afterdeletevalue',
function (GenericEvent $event) use (&$calledAfterDelete) {
$calledAfterDelete[] = 'config.afterdeletevalue';
$calledAfterDelete[] = $event;
});

$this->config->setValues([
'foo' => null
]);
$this->assertInstanceOf(GenericEvent::class, $calledBeforeDelete[1]);
$this->assertInstanceOf(GenericEvent::class, $calledAfterDelete[1]);
$this->assertEquals('config.beforedeletevalue', $calledBeforeDelete[0]);
$this->assertEquals('config.afterdeletevalue', $calledAfterDelete[0]);
$this->assertArrayHasKey('key', $calledBeforeDelete[1]);
$this->assertEquals('foo', $calledBeforeDelete[1]->getArgument('key'));
$this->assertArrayHasKey('value', $calledBeforeDelete[1]);
$this->assertNull($calledBeforeDelete[1]->getArgument('value'));
$this->assertArrayHasKey('key', $calledAfterDelete[1]);
$this->assertEquals('foo', $calledAfterDelete[1]->getArgument('key'));
$this->assertArrayHasKey('value', $calledAfterDelete[1]);
$this->assertEquals('moo', $calledAfterDelete[1]->getArgument('value'));
}

public function testDeleteKey() {
Expand Down