summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorMorris Jobke <hey@morrisjobke.de>2014-12-03 21:31:29 +0100
committerMorris Jobke <hey@morrisjobke.de>2014-12-08 22:33:36 +0100
commitaf91ee97c981d32bcd531e71d31e16f1232c44ce (patch)
treea38253e8311aa0056f90af6adc89d9b791d1dc00 /tests
parentf0b10324caf1637d8ad5a9ed94dfed084f65407d (diff)
downloadnextcloud-server-af91ee97c981d32bcd531e71d31e16f1232c44ce.tar.gz
nextcloud-server-af91ee97c981d32bcd531e71d31e16f1232c44ce.zip
introduce preCondition for setUserValue to provide atomic check-and-update
Diffstat (limited to 'tests')
-rw-r--r--tests/lib/allconfig.php85
1 files changed, 85 insertions, 0 deletions
diff --git a/tests/lib/allconfig.php b/tests/lib/allconfig.php
index a2da46a6105..63ee60f2078 100644
--- a/tests/lib/allconfig.php
+++ b/tests/lib/allconfig.php
@@ -80,6 +80,91 @@ class TestAllConfig extends \Test\TestCase {
$config->deleteUserValue('userSet', 'appSet', 'keySet');
}
+ public function testSetUserValueWithPreCondition() {
+ // mock the check for the database to run the correct SQL statements for each database type
+ $systemConfig = $this->getMock('\OC\SystemConfig');
+ $systemConfig->expects($this->once())
+ ->method('getValue')
+ ->with($this->equalTo('dbtype'),
+ $this->equalTo('sqlite'))
+ ->will($this->returnValue(\OC::$server->getConfig()->getSystemValue('dbtype', 'sqlite')));
+ $config = $this->getConfig($systemConfig);
+
+ $selectAllSQL = 'SELECT `userid`, `appid`, `configkey`, `configvalue` FROM `*PREFIX*preferences` WHERE `userid` = ?';
+
+ $config->setUserValue('userPreCond', 'appPreCond', 'keyPreCond', 'valuePreCond');
+
+ $result = $this->connection->executeQuery($selectAllSQL, array('userPreCond'))->fetchAll();
+
+ $this->assertEquals(1, count($result));
+ $this->assertEquals(array(
+ 'userid' => 'userPreCond',
+ 'appid' => 'appPreCond',
+ 'configkey' => 'keyPreCond',
+ 'configvalue' => 'valuePreCond'
+ ), $result[0]);
+
+ // test if the method overwrites existing database entries with valid precond
+ $config->setUserValue('userPreCond', 'appPreCond', 'keyPreCond', 'valuePreCond2', 'valuePreCond');
+
+ $result = $this->connection->executeQuery($selectAllSQL, array('userPreCond'))->fetchAll();
+
+ $this->assertEquals(1, count($result));
+ $this->assertEquals(array(
+ 'userid' => 'userPreCond',
+ 'appid' => 'appPreCond',
+ 'configkey' => 'keyPreCond',
+ 'configvalue' => 'valuePreCond2'
+ ), $result[0]);
+
+ // cleanup
+ $config->deleteUserValue('userPreCond', 'appPreCond', 'keyPreCond');
+ }
+
+ /**
+ * @expectedException \OCP\PreConditionNotMetException
+ */
+ public function testSetUserValueWithPreConditionFailure() {
+ // mock the check for the database to run the correct SQL statements for each database type
+ $systemConfig = $this->getMock('\OC\SystemConfig');
+ $systemConfig->expects($this->once())
+ ->method('getValue')
+ ->with($this->equalTo('dbtype'),
+ $this->equalTo('sqlite'))
+ ->will($this->returnValue(\OC::$server->getConfig()->getSystemValue('dbtype', 'sqlite')));
+ $config = $this->getConfig($systemConfig);
+
+ $selectAllSQL = 'SELECT `userid`, `appid`, `configkey`, `configvalue` FROM `*PREFIX*preferences` WHERE `userid` = ?';
+
+ $config->setUserValue('userPreCond1', 'appPreCond', 'keyPreCond', 'valuePreCond');
+
+ $result = $this->connection->executeQuery($selectAllSQL, array('userPreCond1'))->fetchAll();
+
+ $this->assertEquals(1, count($result));
+ $this->assertEquals(array(
+ 'userid' => 'userPreCond1',
+ 'appid' => 'appPreCond',
+ 'configkey' => 'keyPreCond',
+ 'configvalue' => 'valuePreCond'
+ ), $result[0]);
+
+ // test if the method overwrites existing database entries with valid precond
+ $config->setUserValue('userPreCond1', 'appPreCond', 'keyPreCond', 'valuePreCond2', 'valuePreCond3');
+
+ $result = $this->connection->executeQuery($selectAllSQL, array('userPreCond1'))->fetchAll();
+
+ $this->assertEquals(1, count($result));
+ $this->assertEquals(array(
+ 'userid' => 'userPreCond1',
+ 'appid' => 'appPreCond',
+ 'configkey' => 'keyPreCond',
+ 'configvalue' => 'valuePreCond'
+ ), $result[0]);
+
+ // cleanup
+ $config->deleteUserValue('userPreCond1', 'appPreCond', 'keyPreCond');
+ }
+
public function testSetUserValueUnchanged() {
$resultMock = $this->getMockBuilder('\Doctrine\DBAL\Driver\Statement')
->disableOriginalConstructor()->getMock();