123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419 |
- <?php
- /**
- * Copyright (c) 2014 Morris Jobke <hey@morrisjobke.de>
- * This file is licensed under the Affero General Public License version 3 or
- * later.
- * See the COPYING-README file.
- */
- namespace Test;
- /**
- * Class TestAllConfig
- *
- * @group DB
- *
- * @package Test
- */
- class TestAllConfig extends \Test\TestCase {
- /** @var \OCP\IDBConnection */
- protected $connection;
- protected function getConfig($systemConfig = null, $connection = null) {
- if($this->connection === null) {
- $this->connection = \OC::$server->getDatabaseConnection();
- }
- if($connection === null) {
- $connection = $this->connection;
- }
- if($systemConfig === null) {
- $systemConfig = $this->getMockBuilder('\OC\SystemConfig')
- ->disableOriginalConstructor()
- ->getMock();
- }
- return new \OC\AllConfig($systemConfig, $connection);
- }
- public function testDeleteUserValue() {
- $config = $this->getConfig();
- // preparation - add something to the database
- $this->connection->executeUpdate(
- 'INSERT INTO `*PREFIX*preferences` (`userid`, `appid`, ' .
- '`configkey`, `configvalue`) VALUES (?, ?, ?, ?)',
- array('userDelete', 'appDelete', 'keyDelete', 'valueDelete')
- );
- $config->deleteUserValue('userDelete', 'appDelete', 'keyDelete');
- $result = $this->connection->executeQuery(
- 'SELECT COUNT(*) AS `count` FROM `*PREFIX*preferences` WHERE `userid` = ?',
- array('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.');
- }
- public function testSetUserValue() {
- $selectAllSQL = 'SELECT `userid`, `appid`, `configkey`, `configvalue` FROM `*PREFIX*preferences` WHERE `userid` = ?';
- $config = $this->getConfig();
- $config->setUserValue('userSet', 'appSet', 'keySet', 'valueSet');
- $result = $this->connection->executeQuery($selectAllSQL, array('userSet'))->fetchAll();
- $this->assertEquals(1, count($result));
- $this->assertEquals(array(
- 'userid' => 'userSet',
- 'appid' => 'appSet',
- 'configkey' => 'keySet',
- 'configvalue' => 'valueSet'
- ), $result[0]);
- // test if the method overwrites existing database entries
- $config->setUserValue('userSet', 'appSet', 'keySet', 'valueSet2');
- $result = $this->connection->executeQuery($selectAllSQL, array('userSet'))->fetchAll();
- $this->assertEquals(1, count($result));
- $this->assertEquals(array(
- 'userid' => 'userSet',
- 'appid' => 'appSet',
- 'configkey' => 'keySet',
- 'configvalue' => 'valueSet2'
- ), $result[0]);
- // cleanup - it therefore relies on the successful execution of the previous test
- $config->deleteUserValue('userSet', 'appSet', 'keySet');
- }
- public function testSetUserValueWithPreCondition() {
- $config = $this->getConfig();
- $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() {
- $config = $this->getConfig();
- $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() {
- // TODO - FIXME until the dependency injection is handled properly (in AllConfig)
- $this->markTestSkipped('Skipped because this is just testable if database connection can be injected');
- $resultMock = $this->getMockBuilder('\Doctrine\DBAL\Driver\Statement')
- ->disableOriginalConstructor()->getMock();
- $resultMock->expects($this->once())
- ->method('fetchColumn')
- ->will($this->returnValue('valueSetUnchanged'));
- $connectionMock = $this->getMock('\OCP\IDBConnection');
- $connectionMock->expects($this->once())
- ->method('executeQuery')
- ->with($this->equalTo('SELECT `configvalue` FROM `*PREFIX*preferences` '.
- 'WHERE `userid` = ? AND `appid` = ? AND `configkey` = ?'),
- $this->equalTo(array('userSetUnchanged', 'appSetUnchanged', 'keySetUnchanged')))
- ->will($this->returnValue($resultMock));
- $connectionMock->expects($this->never())
- ->method('executeUpdate');
- $config = $this->getConfig(null, $connectionMock);
- $config->setUserValue('userSetUnchanged', 'appSetUnchanged', 'keySetUnchanged', 'valueSetUnchanged');
- }
- public function testGetUserValue() {
- $config = $this->getConfig();
- // setup - it therefore relies on the successful execution of the previous test
- $config->setUserValue('userGet', 'appGet', 'keyGet', 'valueGet');
- $value = $config->getUserValue('userGet', 'appGet', 'keyGet');
- $this->assertEquals('valueGet', $value);
- $result = $this->connection->executeQuery(
- 'SELECT `userid`, `appid`, `configkey`, `configvalue` FROM `*PREFIX*preferences` WHERE `userid` = ?',
- array('userGet')
- )->fetchAll();
- $this->assertEquals(1, count($result));
- $this->assertEquals(array(
- 'userid' => 'userGet',
- 'appid' => 'appGet',
- 'configkey' => 'keyGet',
- 'configvalue' => 'valueGet'
- ), $result[0]);
- // drop data from database - but the config option should be cached in the config object
- $this->connection->executeUpdate('DELETE FROM `*PREFIX*preferences` WHERE `userid` = ?', array('userGet'));
- // testing the caching mechanism
- $value = $config->getUserValue('userGet', 'appGet', 'keyGet');
- $this->assertEquals('valueGet', $value);
- $result = $this->connection->executeQuery(
- 'SELECT `userid`, `appid`, `configkey`, `configvalue` FROM `*PREFIX*preferences` WHERE `userid` = ?',
- array('userGet')
- )->fetchAll();
- $this->assertEquals(0, count($result));
- }
- public function testGetUserKeys() {
- $config = $this->getConfig();
- // preparation - add something to the database
- $data = array(
- array('userFetch', 'appFetch1', 'keyFetch1', 'value1'),
- array('userFetch', 'appFetch1', 'keyFetch2', 'value2'),
- array('userFetch', 'appFetch2', 'keyFetch3', 'value3'),
- array('userFetch', 'appFetch1', 'keyFetch4', 'value4'),
- array('userFetch', 'appFetch4', 'keyFetch1', 'value5'),
- array('userFetch', 'appFetch5', 'keyFetch1', 'value6'),
- array('userFetch2', 'appFetch', 'keyFetch1', 'value7')
- );
- foreach ($data as $entry) {
- $this->connection->executeUpdate(
- 'INSERT INTO `*PREFIX*preferences` (`userid`, `appid`, ' .
- '`configkey`, `configvalue`) VALUES (?, ?, ?, ?)',
- $entry
- );
- }
- $value = $config->getUserKeys('userFetch', 'appFetch1');
- $this->assertEquals(array('keyFetch1', 'keyFetch2', 'keyFetch4'), $value);
- $value = $config->getUserKeys('userFetch2', 'appFetch');
- $this->assertEquals(array('keyFetch1'), $value);
- // cleanup
- $this->connection->executeUpdate('DELETE FROM `*PREFIX*preferences`');
- }
- public function testGetUserValueDefault() {
- $config = $this->getConfig();
- $this->assertEquals('', $config->getUserValue('userGetUnset', 'appGetUnset', 'keyGetUnset'));
- $this->assertEquals(null, $config->getUserValue('userGetUnset', 'appGetUnset', 'keyGetUnset', null));
- $this->assertEquals('foobar', $config->getUserValue('userGetUnset', 'appGetUnset', 'keyGetUnset', 'foobar'));
- }
- public function testGetUserValueForUsers() {
- $config = $this->getConfig();
- // preparation - add something to the database
- $data = array(
- array('userFetch1', 'appFetch2', 'keyFetch1', 'value1'),
- array('userFetch2', 'appFetch2', 'keyFetch1', 'value2'),
- array('userFetch3', 'appFetch2', 'keyFetch1', 3),
- array('userFetch4', 'appFetch2', 'keyFetch1', 'value4'),
- array('userFetch5', 'appFetch2', 'keyFetch1', 'value5'),
- array('userFetch6', 'appFetch2', 'keyFetch1', 'value6'),
- array('userFetch7', 'appFetch2', 'keyFetch1', 'value7')
- );
- foreach ($data as $entry) {
- $this->connection->executeUpdate(
- 'INSERT INTO `*PREFIX*preferences` (`userid`, `appid`, ' .
- '`configkey`, `configvalue`) VALUES (?, ?, ?, ?)',
- $entry
- );
- }
- $value = $config->getUserValueForUsers('appFetch2', 'keyFetch1',
- array('userFetch1', 'userFetch2', 'userFetch3', 'userFetch5'));
- $this->assertEquals(array(
- 'userFetch1' => 'value1',
- 'userFetch2' => 'value2',
- 'userFetch3' => 3,
- 'userFetch5' => 'value5'
- ), $value);
- $value = $config->getUserValueForUsers('appFetch2', 'keyFetch1',
- array('userFetch1', 'userFetch4', 'userFetch9'));
- $this->assertEquals(array(
- 'userFetch1' => 'value1',
- 'userFetch4' => 'value4'
- ), $value, 'userFetch9 is an non-existent user and should not be shown.');
- // cleanup
- $this->connection->executeUpdate('DELETE FROM `*PREFIX*preferences`');
- }
- public function testDeleteAllUserValues() {
- $config = $this->getConfig();
- // preparation - add something to the database
- $data = array(
- array('userFetch3', 'appFetch1', 'keyFetch1', 'value1'),
- array('userFetch3', 'appFetch1', 'keyFetch2', 'value2'),
- array('userFetch3', 'appFetch2', 'keyFetch3', 'value3'),
- array('userFetch3', 'appFetch1', 'keyFetch4', 'value4'),
- array('userFetch3', 'appFetch4', 'keyFetch1', 'value5'),
- array('userFetch3', 'appFetch5', 'keyFetch1', 'value6'),
- array('userFetch4', 'appFetch2', 'keyFetch1', 'value7')
- );
- foreach ($data as $entry) {
- $this->connection->executeUpdate(
- 'INSERT INTO `*PREFIX*preferences` (`userid`, `appid`, ' .
- '`configkey`, `configvalue`) VALUES (?, ?, ?, ?)',
- $entry
- );
- }
- $config->deleteAllUserValues('userFetch3');
- $result = $this->connection->executeQuery(
- 'SELECT COUNT(*) AS `count` FROM `*PREFIX*preferences`'
- )->fetch();
- $actualCount = $result['count'];
- $this->assertEquals(1, $actualCount, 'After removing `userFetch3` there should be exactly 1 entry left.');
- // cleanup
- $this->connection->executeUpdate('DELETE FROM `*PREFIX*preferences`');
- }
- public function testDeleteAppFromAllUsers() {
- $config = $this->getConfig();
- // preparation - add something to the database
- $data = array(
- array('userFetch5', 'appFetch1', 'keyFetch1', 'value1'),
- array('userFetch5', 'appFetch1', 'keyFetch2', 'value2'),
- array('userFetch5', 'appFetch2', 'keyFetch3', 'value3'),
- array('userFetch5', 'appFetch1', 'keyFetch4', 'value4'),
- array('userFetch5', 'appFetch4', 'keyFetch1', 'value5'),
- array('userFetch5', 'appFetch5', 'keyFetch1', 'value6'),
- array('userFetch6', 'appFetch2', 'keyFetch1', 'value7')
- );
- foreach ($data as $entry) {
- $this->connection->executeUpdate(
- 'INSERT INTO `*PREFIX*preferences` (`userid`, `appid`, ' .
- '`configkey`, `configvalue`) VALUES (?, ?, ?, ?)',
- $entry
- );
- }
- $config->deleteAppFromAllUsers('appFetch1');
- $result = $this->connection->executeQuery(
- 'SELECT COUNT(*) AS `count` FROM `*PREFIX*preferences`'
- )->fetch();
- $actualCount = $result['count'];
- $this->assertEquals(4, $actualCount, 'After removing `appFetch1` there should be exactly 4 entries left.');
- $config->deleteAppFromAllUsers('appFetch2');
- $result = $this->connection->executeQuery(
- 'SELECT COUNT(*) AS `count` FROM `*PREFIX*preferences`'
- )->fetch();
- $actualCount = $result['count'];
- $this->assertEquals(2, $actualCount, 'After removing `appFetch2` there should be exactly 2 entries left.');
- // cleanup
- $this->connection->executeUpdate('DELETE FROM `*PREFIX*preferences`');
- }
- public function testGetUsersForUserValue() {
- // mock the check for the database to run the correct SQL statements for each database type
- $systemConfig = $this->getMockBuilder('\OC\SystemConfig')
- ->disableOriginalConstructor()
- ->getMock();
- $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);
- // preparation - add something to the database
- $data = array(
- array('user1', 'appFetch9', 'keyFetch9', 'value9'),
- array('user2', 'appFetch9', 'keyFetch9', 'value9'),
- array('user3', 'appFetch9', 'keyFetch9', 'value8'),
- array('user4', 'appFetch9', 'keyFetch8', 'value9'),
- array('user5', 'appFetch8', 'keyFetch9', 'value9'),
- array('user6', 'appFetch9', 'keyFetch9', 'value9'),
- );
- foreach ($data as $entry) {
- $this->connection->executeUpdate(
- 'INSERT INTO `*PREFIX*preferences` (`userid`, `appid`, ' .
- '`configkey`, `configvalue`) VALUES (?, ?, ?, ?)',
- $entry
- );
- }
- $value = $config->getUsersForUserValue('appFetch9', 'keyFetch9', 'value9');
- $this->assertEquals(array('user1', 'user2', 'user6'), $value);
- // cleanup
- $this->connection->executeUpdate('DELETE FROM `*PREFIX*preferences`');
- }
- }
|