From 670242c731ac8d832773ac0fe86813061dcf0768 Mon Sep 17 00:00:00 2001 From: kondou Date: Fri, 2 Aug 2013 11:46:47 +0200 Subject: [PATCH 1/4] Add \OC_Appconfig Unittest --- tests/lib/appconfig.php | 126 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 126 insertions(+) create mode 100644 tests/lib/appconfig.php diff --git a/tests/lib/appconfig.php b/tests/lib/appconfig.php new file mode 100644 index 00000000000..c73e528a277 --- /dev/null +++ b/tests/lib/appconfig.php @@ -0,0 +1,126 @@ + + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +class Test_Appconfig extends PHPUnit_Framework_TestCase { + private function fillDb() { + $query = \OC_DB::prepare('INSERT INTO `*PREFIX*appconfig` VALUES (?, ?, ?)'); + + $query->execute(array('testapp', 'enabled', 'true')); + $query->execute(array('testapp', 'installed_version', '1.2.3')); + $query->execute(array('testapp', 'depends_on', 'someapp')); + $query->execute(array('testapp', 'deletethis', 'deletethis')); + $query->execute(array('testapp', 'key', 'value')); + + $query->execute(array('someapp', 'key', 'value')); + $query->execute(array('someapp', 'otherkey', 'othervalue')); + + $query->execute(array('123456', 'key', 'value')); + $query->execute(array('123456', 'enabled', 'false')); + + $query->execute(array('anotherapp', 'key', 'value')); + $query->execute(array('anotherapp', 'enabled', 'false')); + } + + public function testGetApps() { + $this->fillDb(); + + $query = \OC_DB::prepare('SELECT DISTINCT `appid` FROM `*PREFIX*appconfig`'); + $result = $query->execute(); + $expected = array(); + while ($row = $result->fetchRow()) { + $expected[] = $row['appid']; + } + $apps = \OC_Appconfig::getApps(); + $this->assertEquals($expected, $apps); + } + + public function testGetKeys() { + $query = \OC_DB::prepare('SELECT `configkey` FROM `*PREFIX*appconfig` WHERE `appid` = ?'); + $result = $query->execute(array('testapp')); + $expected = array(); + while($row = $result->fetchRow()) { + $expected[] = $row["configkey"]; + } + $keys = \OC_Appconfig::getKeys('testapp'); + $this->assertEquals($expected, $keys); + } + + public function testGetValue() { + $query = \OC_DB::prepare('SELECT `configvalue` FROM `*PREFIX*appconfig` WHERE `appid` = ? AND `configkey` = ?'); + $result = $query->execute(array('testapp', 'installed_version')); + $expected = $result->fetchRow(); + $value = \OC_Appconfig::getValue('testapp', 'installed_version'); + $this->assertEquals($expected['configvalue'], $value); + + $value = \OC_Appconfig::getValue('testapp', 'nonexistant'); + $this->assertNull($value); + + $value = \OC_Appconfig::getValue('testapp', 'nonexistant', 'default'); + $this->assertEquals('default', $value); + } + + public function testHasKey() { + $value = \OC_Appconfig::hasKey('testapp', 'installed_version'); + $this->assertTrue($value); + + $value = \OC_Appconfig::hasKey('nonexistant', 'nonexistant'); + $this->assertFalse($value); + } + + public function testSetValue() { + \OC_Appconfig::setValue('testapp', 'installed_version', '1.33.7'); + $query = \OC_DB::prepare('SELECT `configvalue` FROM `*PREFIX*appconfig` WHERE `appid` = ? AND `configkey` = ?'); + $result = $query->execute(array('testapp', 'installed_version')); + $value = $result->fetchRow(); + $this->assertEquals('1.33.7', $value['configvalue']); + + \OC_Appconfig::setValue('someapp', 'somekey', 'somevalue'); + $query = \OC_DB::prepare('SELECT `configvalue` FROM `*PREFIX*appconfig` WHERE `appid` = ? AND `configkey` = ?'); + $result = $query->execute(array('someapp', 'somekey')); + $value = $result->fetchRow(); + $this->assertEquals('somevalue', $value['configvalue']); + } + + public function testDeleteKey() { + \OC_Appconfig::deleteKey('testapp', 'deletethis'); + $query = \OC_DB::prepare('SELECT `configvalue` FROM `*PREFIX*appconfig` WHERE `appid` = ? AND `configkey` = ?'); + $query->execute(array('testapp', 'deletethis')); + $result = (bool)$query->fetchRow(); + $this->assertFalse($result); + } + + public function testDeleteApp() { + \OC_Appconfig::deleteApp('someapp'); + $query = \OC_DB::prepare('SELECT `configkey` FROM `*PREFIX*appconfig` WHERE `appid` = ?'); + $query->execute(array('someapp')); + $result = (bool)$query->fetchRow(); + $this->assertFalse($result); + } + + public function testGetValues() { + $this->assertFalse(\OC_Appconfig::getValues('testapp', 'enabled')); + + $query = \OC_DB::prepare('SELECT `configkey`, `configvalue` FROM `*PREFIX*appconfig` WHERE `appid` = ?'); + $query->execute(array('testapp')); + $expected = array(); + while ($row = $query->fetchRow()) { + $expected[$row['configkey']] = $row['configvalue']; + } + $values = \OC_Appconfig::getValues('testapp', false); + $this->assertEquals($expected, $values); + + $query = \OC_DB::prepare('SELECT `appid`, `configvalue` FROM `*PREFIX*appconfig` WHERE `configkey` = ?'); + $query->execute(array('enabled')); + $expected = array(); + while ($row = $query->fetchRow()) { + $expected[$row['appid']] = $row['configvalue']; + } + $values = \OC_Appconfig::getValues(false, 'enabled'); + $this->assertEquals($expected, $values); + } +} From c74f3d0b90227f95c1b6d21bbb7ee28561b67446 Mon Sep 17 00:00:00 2001 From: kondou Date: Fri, 2 Aug 2013 15:59:33 +0200 Subject: [PATCH 2/4] Add null and emptystring tests to check NOT NULL --- tests/lib/appconfig.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tests/lib/appconfig.php b/tests/lib/appconfig.php index c73e528a277..ae08877bd7f 100644 --- a/tests/lib/appconfig.php +++ b/tests/lib/appconfig.php @@ -86,6 +86,20 @@ class Test_Appconfig extends PHPUnit_Framework_TestCase { $this->assertEquals('somevalue', $value['configvalue']); } + /** + * @expectedException \Doctrine\DBAL\DBALException + */ + public function testSetValueNull() { + \OC_Appconfig::setValue('testapp', 'installed_version', null); + } + + /** + * @expectedException \Doctrine\DBAL\DBALException + */ + public function testSetValueEmptyString() { + \OC_Appconfig::setValue('testapp', '', '1.33.7'); + } + public function testDeleteKey() { \OC_Appconfig::deleteKey('testapp', 'deletethis'); $query = \OC_DB::prepare('SELECT `configvalue` FROM `*PREFIX*appconfig` WHERE `appid` = ? AND `configkey` = ?'); From 56549dafce474d74f32d33f6bf510ec7514283e1 Mon Sep 17 00:00:00 2001 From: kondou Date: Fri, 2 Aug 2013 21:27:33 +0200 Subject: [PATCH 3/4] Revert "Add null and emptystring tests to check NOT NULL" This reverts commit c74f3d0b90227f95c1b6d21bbb7ee28561b67446. --- tests/lib/appconfig.php | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/tests/lib/appconfig.php b/tests/lib/appconfig.php index ae08877bd7f..c73e528a277 100644 --- a/tests/lib/appconfig.php +++ b/tests/lib/appconfig.php @@ -86,20 +86,6 @@ class Test_Appconfig extends PHPUnit_Framework_TestCase { $this->assertEquals('somevalue', $value['configvalue']); } - /** - * @expectedException \Doctrine\DBAL\DBALException - */ - public function testSetValueNull() { - \OC_Appconfig::setValue('testapp', 'installed_version', null); - } - - /** - * @expectedException \Doctrine\DBAL\DBALException - */ - public function testSetValueEmptyString() { - \OC_Appconfig::setValue('testapp', '', '1.33.7'); - } - public function testDeleteKey() { \OC_Appconfig::deleteKey('testapp', 'deletethis'); $query = \OC_DB::prepare('SELECT `configvalue` FROM `*PREFIX*appconfig` WHERE `appid` = ? AND `configkey` = ?'); From d70a4a960da243a490c9b3211fb91f59864f5ba5 Mon Sep 17 00:00:00 2001 From: kondou Date: Tue, 6 Aug 2013 17:30:58 +0200 Subject: [PATCH 4/4] Use setUpBeforeClass() and tearDownAfterClass() --- tests/lib/appconfig.php | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/tests/lib/appconfig.php b/tests/lib/appconfig.php index c73e528a277..4d82cd5ba7b 100644 --- a/tests/lib/appconfig.php +++ b/tests/lib/appconfig.php @@ -7,7 +7,7 @@ */ class Test_Appconfig extends PHPUnit_Framework_TestCase { - private function fillDb() { + public static function setUpBeforeClass() { $query = \OC_DB::prepare('INSERT INTO `*PREFIX*appconfig` VALUES (?, ?, ?)'); $query->execute(array('testapp', 'enabled', 'true')); @@ -26,9 +26,15 @@ class Test_Appconfig extends PHPUnit_Framework_TestCase { $query->execute(array('anotherapp', 'enabled', 'false')); } - public function testGetApps() { - $this->fillDb(); + public static function tearDownAfterClass() { + $query = \OC_DB::prepare('DELETE FROM `*PREFIX*appconfig` WHERE `appid` = ?'); + $query->execute(array('testapp')); + $query->execute(array('someapp')); + $query->execute(array('123456')); + $query->execute(array('anotherapp')); + } + public function testGetApps() { $query = \OC_DB::prepare('SELECT DISTINCT `appid` FROM `*PREFIX*appconfig`'); $result = $query->execute(); $expected = array();