From a52df5cad2f9853514988b1bb43b07e812bb9316 Mon Sep 17 00:00:00 2001 From: Bernhard Posselt Date: Tue, 21 Oct 2014 11:11:20 +0200 Subject: move config class into config folder --- appinfo/app.php | 5 +- appinfo/application.php | 3 +- appinfo/install.php | 22 +++ articleenhancer/xpatharticleenhancer.php | 4 +- config/config.php | 196 +++++++++++++++++++ fetcher/feedfetcher.php | 2 +- service/feedservice.php | 2 +- service/folderservice.php | 2 +- service/itemservice.php | 2 +- .../articleenhancer/XPathArticleEnhancerTest.php | 2 +- tests/unit/config/ConfigTest.php | 215 +++++++++++++++++++++ tests/unit/fetcher/FeedFetcherTest.php | 2 +- tests/unit/service/FeedServiceTest.php | 2 +- tests/unit/service/FolderServiceTest.php | 8 +- tests/unit/service/ItemServiceTest.php | 2 +- tests/unit/utility/ConfigTest.php | 215 --------------------- utility/config.php | 196 ------------------- 17 files changed, 450 insertions(+), 430 deletions(-) create mode 100644 appinfo/install.php create mode 100644 config/config.php create mode 100644 tests/unit/config/ConfigTest.php delete mode 100644 tests/unit/utility/ConfigTest.php delete mode 100644 utility/config.php diff --git a/appinfo/app.php b/appinfo/app.php index 077d0b7c4..a6de66ce2 100644 --- a/appinfo/app.php +++ b/appinfo/app.php @@ -28,7 +28,4 @@ $container = new Application(); $config = $container->getAppConfig(); $config->registerNavigation(); $config->registerBackgroundJobs(); -$config->registerHooks(); - -// check for correct app dependencies and fail if possible -$config->testDependencies(); +$config->registerHooks(); \ No newline at end of file diff --git a/appinfo/application.php b/appinfo/application.php index 5d27fe5dd..cdbaa82d6 100644 --- a/appinfo/application.php +++ b/appinfo/application.php @@ -19,6 +19,7 @@ use \OCP\Util; use \OCP\User; use \OCA\News\Config\AppConfig; +use \OCA\News\Config\Config; use \OCA\News\Controller\PageController; use \OCA\News\Controller\FolderController; @@ -39,7 +40,6 @@ use \OCA\News\Db\FeedMapper; use \OCA\News\Db\StatusFlag; use \OCA\News\Db\MapperFactory; -use \OCA\News\Utility\Config; use \OCA\News\Utility\OPMLExporter; use \OCA\News\Utility\Updater; use \OCA\News\Utility\SimplePieAPIFactory; @@ -470,5 +470,6 @@ class Application extends App { } + public function dispatchPart($controller, $) } diff --git a/appinfo/install.php b/appinfo/install.php new file mode 100644 index 000000000..572f21fff --- /dev/null +++ b/appinfo/install.php @@ -0,0 +1,22 @@ + + * @author Bernhard Posselt + * @copyright Alessandro Cosentino 2012 + * @copyright Bernhard Posselt 2012, 2014 + */ + +namespace OCA\News\AppInfo; + +use \OCA\News\Config\DependencyException; + + +// check for correct app dependencies and fail if possible +$container = new Application(); +$config = $container->getAppConfig(); +$config->testDependencies(); \ No newline at end of file diff --git a/articleenhancer/xpatharticleenhancer.php b/articleenhancer/xpatharticleenhancer.php index fa704e301..623b42bfc 100644 --- a/articleenhancer/xpatharticleenhancer.php +++ b/articleenhancer/xpatharticleenhancer.php @@ -20,7 +20,7 @@ use \ZendXml\Security; use \OCA\News\Db\Item; use \OCA\News\Utility\SimplePieAPIFactory; -use \OCA\News\Utility\Config; +use \OCA\News\Config\Config; @@ -38,7 +38,7 @@ class XPathArticleEnhancer implements ArticleEnhancer { * @param array $regexXPathPair an associative array containing regex to * match the url and the xpath that should be used for it to extract the * page - * @param \OCA\News\Utility\Config $config + * @param \OCA\News\Config\Config $config */ public function __construct(SimplePieAPIFactory $fileFactory, array $regexXPathPair, diff --git a/config/config.php b/config/config.php new file mode 100644 index 000000000..d84e4076c --- /dev/null +++ b/config/config.php @@ -0,0 +1,196 @@ + + * @author Bernhard Posselt + * @copyright Alessandro Cosentino 2012 + * @copyright Bernhard Posselt 2012, 2014 + */ + +namespace OCA\News\Config; + +use \OCP\ILogger; + + +class Config { + + private $fileSystem; + private $autoPurgeMinimumInterval; // seconds, used to define how + // long deleted folders and feeds + // should still be kept for an + // undo actions + private $autoPurgeCount; // number of allowed unread articles per feed + private $simplePieCacheDuration; // seconds + private $feedFetcherTimeout; // seconds + private $useCronUpdates; // turn off updates run by owncloud cronjob + private $proxyHost; + private $proxyPort; + private $proxyUser; + private $proxyPassword; + private $logger; + private $loggerParams; + + + public function __construct($fileSystem, ILogger $logger, $loggerParams) { + $this->fileSystem = $fileSystem; + $this->autoPurgeMinimumInterval = 60; + $this->autoPurgeCount = 200; + $this->simplePieCacheDuration = 30*60; + $this->feedFetcherTimeout = 60; + $this->useCronUpdates = true; + $this->logger = $logger; + $this->proxyHost = ''; + $this->proxyPort = 8080; + $this->proxyUser = ''; + $this->proxyPassword = ''; + $this->loggerParams = $loggerParams; + } + + public function getProxyPort() { + return $this->proxyPort; + } + + public function getProxyHost() { + return $this->proxyHost; + } + + public function getProxyAuth() { + if($this->proxyUser === '') { + return null; + } else { + return $this->proxyUser . ':' . $this->proxyPassword; + } + } + + public function getProxyUser() { + return $this->proxyUser; + } + + public function getProxyPassword() { + return $this->proxyPassword; + } + + public function getAutoPurgeMinimumInterval() { + if ($this->autoPurgeMinimumInterval > 60) { + return $this->autoPurgeMinimumInterval; + } else { + return 60; + } + } + + + public function getAutoPurgeCount() { + return $this->autoPurgeCount; + } + + + public function getSimplePieCacheDuration() { + return $this->simplePieCacheDuration; + } + + + public function getFeedFetcherTimeout() { + return $this->feedFetcherTimeout; + } + + + public function getUseCronUpdates() { + return $this->useCronUpdates; + } + + + public function setAutoPurgeMinimumInterval($value) { + $this->autoPurgeMinimumInterval = $value; + } + + + public function setAutoPurgeCount($value) { + $this->autoPurgeCount = $value; + } + + + public function setSimplePieCacheDuration($value) { + $this->simplePieCacheDuration = $value; + } + + + public function setFeedFetcherTimeout($value) { + $this->feedFetcherTimeout = $value; + } + + + public function setUseCronUpdates($value) { + $this->useCronUpdates = $value; + } + + + public function setProxyPort($value) { + $this->proxyPort = $value; + } + + public function setProxyHost($value) { + $this->proxyHost = $value; + } + + public function setProxyUser($value) { + $this->proxyUser = $value; + } + + public function setProxyPassword($value) { + $this->proxyPassword = $value; + } + + + public function read($configPath, $createIfNotExists=false) { + if($createIfNotExists && !$this->fileSystem->file_exists($configPath)) { + + $this->write($configPath); + + } else { + + $content = $this->fileSystem->file_get_contents($configPath); + $configValues = parse_ini_string($content); + + if($configValues === false || count($configValues) === 0) { + $this->logger->warning('Configuration invalid. Ignoring values.', + $this->loggerParams); + } else { + + foreach($configValues as $key => $value) { + if(property_exists($this, $key)) { + $type = gettype($this->$key); + settype($value, $type); + $this->$key = $value; + } else { + $this->logger->warning('Configuration value "' . $key . + '" does not exist. Ignored value.' , $this->loggerParams); + } + } + + } + } + } + + + public function write($configPath) { + $ini = + "autoPurgeMinimumInterval = " . $this->autoPurgeMinimumInterval . "\n" . + "autoPurgeCount = " . $this->autoPurgeCount . "\n" . + "simplePieCacheDuration = " . $this->simplePieCacheDuration . "\n" . + "feedFetcherTimeout = " . $this->feedFetcherTimeout . "\n" . + "useCronUpdates = " . var_export($this->useCronUpdates, true) . "\n" . + "proxyHost = " . $this->proxyHost . "\n" . + "proxyPort = " . $this->proxyPort . "\n" . + "proxyUser = " . $this->proxyUser . "\n" . + "proxyPassword = " . $this->proxyPassword; + ; + + $this->fileSystem->file_put_contents($configPath, $ini); + } + + +} \ No newline at end of file diff --git a/fetcher/feedfetcher.php b/fetcher/feedfetcher.php index feeabb2bb..c08bf4606 100644 --- a/fetcher/feedfetcher.php +++ b/fetcher/feedfetcher.php @@ -17,7 +17,7 @@ use \OCA\News\Db\Item; use \OCA\News\Db\Feed; use \OCA\News\Utility\FaviconFetcher; use \OCA\News\Utility\SimplePieAPIFactory; -use \OCA\News\Utility\Config; +use \OCA\News\Config\Config; use \OCA\News\Config\AppConfig; diff --git a/service/feedservice.php b/service/feedservice.php index cb8d4f7cc..ce1c48af9 100644 --- a/service/feedservice.php +++ b/service/feedservice.php @@ -24,7 +24,7 @@ use \OCA\News\Db\ItemMapper; use \OCA\News\Fetcher\Fetcher; use \OCA\News\Fetcher\FetcherException; use \OCA\News\ArticleEnhancer\Enhancer; -use \OCA\News\Utility\Config; +use \OCA\News\Config\Config; class FeedService extends Service { diff --git a/service/folderservice.php b/service/folderservice.php index f6d45c7e5..88cc8355e 100644 --- a/service/folderservice.php +++ b/service/folderservice.php @@ -17,7 +17,7 @@ use \OCP\IL10N; use \OCA\News\Db\Folder; use \OCA\News\Db\FolderMapper; -use \OCA\News\Utility\Config; +use \OCA\News\Config\Config; class FolderService extends Service { diff --git a/service/itemservice.php b/service/itemservice.php index dcabb2f11..792fcc987 100644 --- a/service/itemservice.php +++ b/service/itemservice.php @@ -18,7 +18,7 @@ use \OCP\AppFramework\Db\DoesNotExistException; use \OCA\News\Db\ItemMapper; use \OCA\News\Db\StatusFlag; use \OCA\News\Db\FeedType; -use \OCA\News\Utility\Config; +use \OCA\News\Config\Config; class ItemService extends Service { diff --git a/tests/unit/articleenhancer/XPathArticleEnhancerTest.php b/tests/unit/articleenhancer/XPathArticleEnhancerTest.php index 8a99e9d5a..082a2030c 100644 --- a/tests/unit/articleenhancer/XPathArticleEnhancerTest.php +++ b/tests/unit/articleenhancer/XPathArticleEnhancerTest.php @@ -37,7 +37,7 @@ class XPathArticleEnhancerTest extends \PHPUnit_Framework_TestCase { $this->proxyPort = 3; $this->proxyAuth = 'hi'; $this->config = $this->getMockBuilder( - '\OCA\News\Utility\Config') + '\OCA\News\Config\Config') ->disableOriginalConstructor() ->getMock(); $this->config->expects($this->any()) diff --git a/tests/unit/config/ConfigTest.php b/tests/unit/config/ConfigTest.php new file mode 100644 index 000000000..b03eeb325 --- /dev/null +++ b/tests/unit/config/ConfigTest.php @@ -0,0 +1,215 @@ + + * @author Bernhard Posselt + * @copyright Alessandro Cosentino 2012 + * @copyright Bernhard Posselt 2012, 2014 + */ + +namespace OCA\News\Config; + + +class ConfigTest extends \PHPUnit_Framework_TestCase { + + private $fileSystem; + private $config; + private $configPath; + private $loggerParams; + + public function setUp() { + $this->logger = $this->getMockBuilder( + '\OCP\ILogger') + ->disableOriginalConstructor() + ->getMock(); + $this->fileSystem = $this->getMock('FileSystem', [ + 'file_get_contents', + 'file_put_contents', + 'file_exists' + ]); + $this->loggerParams = ['hi']; + $this->config = new Config($this->fileSystem, $this->logger, $this->loggerParams); + $this->configPath = 'config.json'; + } + + + public function testDefaults() { + $this->assertEquals(60, $this->config->getAutoPurgeMinimumInterval()); + $this->assertEquals(200, $this->config->getAutoPurgeCount()); + $this->assertEquals(30*60, $this->config->getSimplePieCacheDuration()); + $this->assertEquals(60, $this->config->getFeedFetcherTimeout()); + $this->assertEquals(true, $this->config->getUseCronUpdates()); + $this->assertEquals(8080, $this->config->getProxyPort()); + $this->assertEquals('', $this->config->getProxyHost()); + $this->assertEquals(null, $this->config->getProxyAuth()); + $this->assertEquals('', $this->config->getProxyUser()); + $this->assertEquals('', $this->config->getProxyPassword()); + } + + + public function testRead () { + $this->fileSystem->expects($this->once()) + ->method('file_get_contents') + ->with($this->equalTo($this->configPath)) + ->will($this->returnValue("autoPurgeCount = 3\nuseCronUpdates = true")); + + $this->config->read($this->configPath); + + $this->assertSame(3, $this->config->getAutoPurgeCount()); + $this->assertSame(true, $this->config->getUseCronUpdates()); + } + + + public function testReadIgnoresVeryLowPurgeInterval () { + $this->fileSystem->expects($this->once()) + ->method('file_get_contents') + ->with($this->equalTo($this->configPath)) + ->will($this->returnValue("autoPurgeMinimumInterval = 59")); + + $this->config->read($this->configPath); + + $this->assertSame(60, $this->config->getAutoPurgeMinimumInterval()); + } + + + + public function testReadBool () { + $this->fileSystem->expects($this->once()) + ->method('file_get_contents') + ->with($this->equalTo($this->configPath)) + ->will($this->returnValue("autoPurgeCount = 3\nuseCronUpdates = false")); + + $this->config->read($this->configPath); + + $this->assertSame(3, $this->config->getAutoPurgeCount()); + $this->assertSame(false, $this->config->getUseCronUpdates()); + } + + + public function testReadLogsInvalidValue() { + $this->fileSystem->expects($this->once()) + ->method('file_get_contents') + ->with($this->equalTo($this->configPath)) + ->will($this->returnValue('autoPurgeCounts = 3')); + $this->logger->expects($this->once()) + ->method('warning') + ->with($this->equalTo('Configuration value "autoPurgeCounts" ' . + 'does not exist. Ignored value.'), + $this->equalTo($this->loggerParams)); + + $this->config->read($this->configPath); + } + + + public function testReadLogsInvalidINI() { + $this->fileSystem->expects($this->once()) + ->method('file_get_contents') + ->with($this->equalTo($this->configPath)) + ->will($this->returnValue('')); + $this->logger->expects($this->once()) + ->method('warning') + ->with($this->equalTo('Configuration invalid. Ignoring values.'), + $this->equalTo($this->loggerParams)); + + $this->config->read($this->configPath); + } + + + public function testWrite () { + $json = "autoPurgeMinimumInterval = 60\n" . + "autoPurgeCount = 3\n" . + "simplePieCacheDuration = 1800\n" . + "feedFetcherTimeout = 60\n" . + "useCronUpdates = true\n" . + "proxyHost = yo man\n" . + "proxyPort = 12\n" . + "proxyUser = this is a test\n". + "proxyPassword = se"; + $this->config->setAutoPurgeCount(3); + $this->config->setProxyHost("yo man"); + $this->config->setProxyPort(12); + $this->config->setProxyUser("this is a test"); + $this->config->setProxyPassword("se"); + + $this->fileSystem->expects($this->once()) + ->method('file_put_contents') + ->with($this->equalTo($this->configPath), + $this->equalTo($json)); + + $this->config->write($this->configPath); + } + + + public function testNoProxyAuthReturnsNull() { + $this->assertNull($this->config->getProxyAuth()); + } + + + public function testReadingNonExistentConfigWillWriteDefaults() { + $this->fileSystem->expects($this->once()) + ->method('file_exists') + ->with($this->equalTo($this->configPath)) + ->will($this->returnValue(false)); + + $this->config->setUseCronUpdates(false); + + $json = "autoPurgeMinimumInterval = 60\n" . + "autoPurgeCount = 200\n" . + "simplePieCacheDuration = 1800\n" . + "feedFetcherTimeout = 60\n" . + "useCronUpdates = false\n" . + "proxyHost = \n" . + "proxyPort = 8080\n" . + "proxyUser = \n" . + "proxyPassword = "; + + $this->fileSystem->expects($this->once()) + ->method('file_put_contents') + ->with($this->equalTo($this->configPath), + $this->equalTo($json)); + + $this->config->read($this->configPath, true); + } + + + public function testEncodesUserAndPasswordInHTTPBasicAuth() { + $this->config->setProxyUser("this is a test"); + $this->config->setProxyPassword("se"); + + $this->assertEquals('this is a test:se', $this->config->getProxyAuth()); + } + + + public function testNoLowMinimumAutoPurgeInterval() { + $this->config->setAutoPurgeMinimumInterval(59); + $interval = $this->config->getAutoPurgeMinimumInterval(); + + $this->assertSame(60, $interval); + } + + + public function testMinimumAutoPurgeInterval() { + $this->config->setAutoPurgeMinimumInterval(61); + $interval = $this->config->getAutoPurgeMinimumInterval(); + + $this->assertSame(61, $interval); + } + + public function testCacheDuration() { + $this->config->setSimplePieCacheDuration(21); + $duration = $this->config->getSimplePieCacheDuration(); + + $this->assertSame(21, $duration); + } + + public function testFeedFetcherTimeout() { + $this->config->setFeedFetcherTimeout(2); + $timout = $this->config->getFeedFetcherTimeout(); + + $this->assertSame(2, $timout); + } +} \ No newline at end of file diff --git a/tests/unit/fetcher/FeedFetcherTest.php b/tests/unit/fetcher/FeedFetcherTest.php index 893fb7b2a..948e25e9b 100644 --- a/tests/unit/fetcher/FeedFetcherTest.php +++ b/tests/unit/fetcher/FeedFetcherTest.php @@ -102,7 +102,7 @@ class FeedFetcherTest extends \PHPUnit_Framework_TestCase { $this->proxyAuth = 'hi'; $this->fetchTimeout = 40; $this->config = $this->getMockBuilder( - '\OCA\News\Utility\Config') + '\OCA\News\Config\Config') ->disableOriginalConstructor() ->getMock(); $this->config->expects($this->any()) diff --git a/tests/unit/service/FeedServiceTest.php b/tests/unit/service/FeedServiceTest.php index a4e722109..cc8d3018e 100644 --- a/tests/unit/service/FeedServiceTest.php +++ b/tests/unit/service/FeedServiceTest.php @@ -69,7 +69,7 @@ class FeedServiceTest extends \PHPUnit_Framework_TestCase { ->getMock(); $this->purifier = $this->getMock('purifier', ['purify']); $config = $this->getMockBuilder( - '\OCA\News\Utility\Config') + '\OCA\News\Config\Config') ->disableOriginalConstructor() ->getMock(); $config->expects($this->any()) diff --git a/tests/unit/service/FolderServiceTest.php b/tests/unit/service/FolderServiceTest.php index 144705b26..bbc89c99d 100644 --- a/tests/unit/service/FolderServiceTest.php +++ b/tests/unit/service/FolderServiceTest.php @@ -40,7 +40,7 @@ class FolderServiceTest extends \PHPUnit_Framework_TestCase { ->getMock(); $this->autoPurgeMinimumInterval = 10; $config = $this->getMockBuilder( - '\OCA\News\Utility\Config') + '\OCA\News\Config\Config') ->disableOriginalConstructor() ->getMock(); $config->expects($this->any()) @@ -147,14 +147,14 @@ class FolderServiceTest extends \PHPUnit_Framework_TestCase { $this->folderService->rename(3, 'bogus', ''); - $this->assertEquals('bogus', $folder->getName()); + $this->assertEquals('bogus', $folder->getName()); } public function testRenameThrowsExWhenFolderNameExists(){ $folderName = 'hihi'; $rows = [['id' => 1]]; - + $this->l10n->expects($this->once()) ->method('t'); $this->folderMapper->expects($this->once()) @@ -169,7 +169,7 @@ class FolderServiceTest extends \PHPUnit_Framework_TestCase { public function testRenameThrowsExWhenFolderNameEmpty(){ $folderName = ''; - + $this->folderMapper->expects($this->once()) ->method('findByName') ->with($this->equalTo($folderName)) diff --git a/tests/unit/service/ItemServiceTest.php b/tests/unit/service/ItemServiceTest.php index 2f5f31cba..03d644eb9 100644 --- a/tests/unit/service/ItemServiceTest.php +++ b/tests/unit/service/ItemServiceTest.php @@ -49,7 +49,7 @@ class ItemServiceTest extends \PHPUnit_Framework_TestCase { ->will($this->returnValue($this->status)); $this->threshold = 2; $config = $this->getMockBuilder( - '\OCA\News\Utility\Config') + '\OCA\News\Config\Config') ->disableOriginalConstructor() ->getMock(); $config->expects($this->any()) diff --git a/tests/unit/utility/ConfigTest.php b/tests/unit/utility/ConfigTest.php deleted file mode 100644 index a8f7be1be..000000000 --- a/tests/unit/utility/ConfigTest.php +++ /dev/null @@ -1,215 +0,0 @@ - - * @author Bernhard Posselt - * @copyright Alessandro Cosentino 2012 - * @copyright Bernhard Posselt 2012, 2014 - */ - -namespace OCA\News\Utility; - - -class ConfigTest extends \PHPUnit_Framework_TestCase { - - private $fileSystem; - private $config; - private $configPath; - private $loggerParams; - - public function setUp() { - $this->logger = $this->getMockBuilder( - '\OCP\ILogger') - ->disableOriginalConstructor() - ->getMock(); - $this->fileSystem = $this->getMock('FileSystem', [ - 'file_get_contents', - 'file_put_contents', - 'file_exists' - ]); - $this->loggerParams = ['hi']; - $this->config = new Config($this->fileSystem, $this->logger, $this->loggerParams); - $this->configPath = 'config.json'; - } - - - public function testDefaults() { - $this->assertEquals(60, $this->config->getAutoPurgeMinimumInterval()); - $this->assertEquals(200, $this->config->getAutoPurgeCount()); - $this->assertEquals(30*60, $this->config->getSimplePieCacheDuration()); - $this->assertEquals(60, $this->config->getFeedFetcherTimeout()); - $this->assertEquals(true, $this->config->getUseCronUpdates()); - $this->assertEquals(8080, $this->config->getProxyPort()); - $this->assertEquals('', $this->config->getProxyHost()); - $this->assertEquals(null, $this->config->getProxyAuth()); - $this->assertEquals('', $this->config->getProxyUser()); - $this->assertEquals('', $this->config->getProxyPassword()); - } - - - public function testRead () { - $this->fileSystem->expects($this->once()) - ->method('file_get_contents') - ->with($this->equalTo($this->configPath)) - ->will($this->returnValue("autoPurgeCount = 3\nuseCronUpdates = true")); - - $this->config->read($this->configPath); - - $this->assertSame(3, $this->config->getAutoPurgeCount()); - $this->assertSame(true, $this->config->getUseCronUpdates()); - } - - - public function testReadIgnoresVeryLowPurgeInterval () { - $this->fileSystem->expects($this->once()) - ->method('file_get_contents') - ->with($this->equalTo($this->configPath)) - ->will($this->returnValue("autoPurgeMinimumInterval = 59")); - - $this->config->read($this->configPath); - - $this->assertSame(60, $this->config->getAutoPurgeMinimumInterval()); - } - - - - public function testReadBool () { - $this->fileSystem->expects($this->once()) - ->method('file_get_contents') - ->with($this->equalTo($this->configPath)) - ->will($this->returnValue("autoPurgeCount = 3\nuseCronUpdates = false")); - - $this->config->read($this->configPath); - - $this->assertSame(3, $this->config->getAutoPurgeCount()); - $this->assertSame(false, $this->config->getUseCronUpdates()); - } - - - public function testReadLogsInvalidValue() { - $this->fileSystem->expects($this->once()) - ->method('file_get_contents') - ->with($this->equalTo($this->configPath)) - ->will($this->returnValue('autoPurgeCounts = 3')); - $this->logger->expects($this->once()) - ->method('warning') - ->with($this->equalTo('Configuration value "autoPurgeCounts" ' . - 'does not exist. Ignored value.'), - $this->equalTo($this->loggerParams)); - - $this->config->read($this->configPath); - } - - - public function testReadLogsInvalidINI() { - $this->fileSystem->expects($this->once()) - ->method('file_get_contents') - ->with($this->equalTo($this->configPath)) - ->will($this->returnValue('')); - $this->logger->expects($this->once()) - ->method('warning') - ->with($this->equalTo('Configuration invalid. Ignoring values.'), - $this->equalTo($this->loggerParams)); - - $this->config->read($this->configPath); - } - - - public function testWrite () { - $json = "autoPurgeMinimumInterval = 60\n" . - "autoPurgeCount = 3\n" . - "simplePieCacheDuration = 1800\n" . - "feedFetcherTimeout = 60\n" . - "useCronUpdates = true\n" . - "proxyHost = yo man\n" . - "proxyPort = 12\n" . - "proxyUser = this is a test\n". - "proxyPassword = se"; - $this->config->setAutoPurgeCount(3); - $this->config->setProxyHost("yo man"); - $this->config->setProxyPort(12); - $this->config->setProxyUser("this is a test"); - $this->config->setProxyPassword("se"); - - $this->fileSystem->expects($this->once()) - ->method('file_put_contents') - ->with($this->equalTo($this->configPath), - $this->equalTo($json)); - - $this->config->write($this->configPath); - } - - - public function testNoProxyAuthReturnsNull() { - $this->assertNull($this->config->getProxyAuth()); - } - - - public function testReadingNonExistentConfigWillWriteDefaults() { - $this->fileSystem->expects($this->once()) - ->method('file_exists') - ->with($this->equalTo($this->configPath)) - ->will($this->returnValue(false)); - - $this->config->setUseCronUpdates(false); - - $json = "autoPurgeMinimumInterval = 60\n" . - "autoPurgeCount = 200\n" . - "simplePieCacheDuration = 1800\n" . - "feedFetcherTimeout = 60\n" . - "useCronUpdates = false\n" . - "proxyHost = \n" . - "proxyPort = 8080\n" . - "proxyUser = \n" . - "proxyPassword = "; - - $this->fileSystem->expects($this->once()) - ->method('file_put_contents') - ->with($this->equalTo($this->configPath), - $this->equalTo($json)); - - $this->config->read($this->configPath, true); - } - - - public function testEncodesUserAndPasswordInHTTPBasicAuth() { - $this->config->setProxyUser("this is a test"); - $this->config->setProxyPassword("se"); - - $this->assertEquals('this is a test:se', $this->config->getProxyAuth()); - } - - - public function testNoLowMinimumAutoPurgeInterval() { - $this->config->setAutoPurgeMinimumInterval(59); - $interval = $this->config->getAutoPurgeMinimumInterval(); - - $this->assertSame(60, $interval); - } - - - public function testMinimumAutoPurgeInterval() { - $this->config->setAutoPurgeMinimumInterval(61); - $interval = $this->config->getAutoPurgeMinimumInterval(); - - $this->assertSame(61, $interval); - } - - public function testCacheDuration() { - $this->config->setSimplePieCacheDuration(21); - $duration = $this->config->getSimplePieCacheDuration(); - - $this->assertSame(21, $duration); - } - - public function testFeedFetcherTimeout() { - $this->config->setFeedFetcherTimeout(2); - $timout = $this->config->getFeedFetcherTimeout(); - - $this->assertSame(2, $timout); - } -} \ No newline at end of file diff --git a/utility/config.php b/utility/config.php deleted file mode 100644 index 94e71442b..000000000 --- a/utility/config.php +++ /dev/null @@ -1,196 +0,0 @@ - - * @author Bernhard Posselt - * @copyright Alessandro Cosentino 2012 - * @copyright Bernhard Posselt 2012, 2014 - */ - -namespace OCA\News\Utility; - -use \OCP\ILogger; - - -class Config { - - private $fileSystem; - private $autoPurgeMinimumInterval; // seconds, used to define how - // long deleted folders and feeds - // should still be kept for an - // undo actions - private $autoPurgeCount; // number of allowed unread articles per feed - private $simplePieCacheDuration; // seconds - private $feedFetcherTimeout; // seconds - private $useCronUpdates; // turn off updates run by owncloud cronjob - private $proxyHost; - private $proxyPort; - private $proxyUser; - private $proxyPassword; - private $logger; - private $loggerParams; - - - public function __construct($fileSystem, ILogger $logger, $loggerParams) { - $this->fileSystem = $fileSystem; - $this->autoPurgeMinimumInterval = 60; - $this->autoPurgeCount = 200; - $this->simplePieCacheDuration = 30*60; - $this->feedFetcherTimeout = 60; - $this->useCronUpdates = true; - $this->logger = $logger; - $this->proxyHost = ''; - $this->proxyPort = 8080; - $this->proxyUser = ''; - $this->proxyPassword = ''; - $this->loggerParams = $loggerParams; - } - - public function getProxyPort() { - return $this->proxyPort; - } - - public function getProxyHost() { - return $this->proxyHost; - } - - public function getProxyAuth() { - if($this->proxyUser === '') { - return null; - } else { - return $this->proxyUser . ':' . $this->proxyPassword; - } - } - - public function getProxyUser() { - return $this->proxyUser; - } - - public function getProxyPassword() { - return $this->proxyPassword; - } - - public function getAutoPurgeMinimumInterval() { - if ($this->autoPurgeMinimumInterval > 60) { - return $this->autoPurgeMinimumInterval; - } else { - return 60; - } - } - - - public function getAutoPurgeCount() { - return $this->autoPurgeCount; - } - - - public function getSimplePieCacheDuration() { - return $this->simplePieCacheDuration; - } - - - public function getFeedFetcherTimeout() { - return $this->feedFetcherTimeout; - } - - - public function getUseCronUpdates() { - return $this->useCronUpdates; - } - - - public function setAutoPurgeMinimumInterval($value) { - $this->autoPurgeMinimumInterval = $value; - } - - - public function setAutoPurgeCount($value) { - $this->autoPurgeCount = $value; - } - - - public function setSimplePieCacheDuration($value) { - $this->simplePieCacheDuration = $value; - } - - - public function setFeedFetcherTimeout($value) { - $this->feedFetcherTimeout = $value; - } - - - public function setUseCronUpdates($value) { - $this->useCronUpdates = $value; - } - - - public function setProxyPort($value) { - $this->proxyPort = $value; - } - - public function setProxyHost($value) { - $this->proxyHost = $value; - } - - public function setProxyUser($value) { - $this->proxyUser = $value; - } - - public function setProxyPassword($value) { - $this->proxyPassword = $value; - } - - - public function read($configPath, $createIfNotExists=false) { - if($createIfNotExists && !$this->fileSystem->file_exists($configPath)) { - - $this->write($configPath); - - } else { - - $content = $this->fileSystem->file_get_contents($configPath); - $configValues = parse_ini_string($content); - - if($configValues === false || count($configValues) === 0) { - $this->logger->warning('Configuration invalid. Ignoring values.', - $this->loggerParams); - } else { - - foreach($configValues as $key => $value) { - if(property_exists($this, $key)) { - $type = gettype($this->$key); - settype($value, $type); - $this->$key = $value; - } else { - $this->logger->warning('Configuration value "' . $key . - '" does not exist. Ignored value.' , $this->loggerParams); - } - } - - } - } - } - - - public function write($configPath) { - $ini = - "autoPurgeMinimumInterval = " . $this->autoPurgeMinimumInterval . "\n" . - "autoPurgeCount = " . $this->autoPurgeCount . "\n" . - "simplePieCacheDuration = " . $this->simplePieCacheDuration . "\n" . - "feedFetcherTimeout = " . $this->feedFetcherTimeout . "\n" . - "useCronUpdates = " . var_export($this->useCronUpdates, true) . "\n" . - "proxyHost = " . $this->proxyHost . "\n" . - "proxyPort = " . $this->proxyPort . "\n" . - "proxyUser = " . $this->proxyUser . "\n" . - "proxyPassword = " . $this->proxyPassword; - ; - - $this->fileSystem->file_put_contents($configPath, $ini); - } - - -} \ No newline at end of file -- cgit v1.2.3