. * */ namespace OCA\News\Utility; require_once(__DIR__ . "/../../classloader.php"); class ConfigFetcherTest extends \OCA\News\Utility\TestUtility { private $fileSystem; private $config; private $configPath; public function setUp() { $this->api = $this->getMockBuilder( '\OCA\News\Core\API') ->disableOriginalConstructor() ->getMock(); $this->fileSystem = $this->getMock('FileSystem', array( 'file_get_contents', 'file_put_contents', 'file_exists' )); $this->config = new Config($this->fileSystem, $this->api); $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()); } 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->assertTrue(3 === $this->config->getAutoPurgeCount()); $this->assertTrue(true === $this->config->getUseCronUpdates()); } 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->assertTrue(3 === $this->config->getAutoPurgeCount()); $this->assertTrue(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->api->expects($this->once()) ->method('log') ->with($this->equalTo('Configuration value "autoPurgeCounts" ' . 'does not exist. Ignored value.'), $this->equalTo('warn')); $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->api->expects($this->once()) ->method('log') ->with($this->equalTo('Configuration invalid. Ignoring values.'), $this->equalTo('warn')); $this->config->read($this->configPath); } public function testWrite () { $json = "autoPurgeMinimumInterval = 60\n" . "autoPurgeCount = 3\n" . "simplePieCacheDuration = 1800\n" . "feedFetcherTimeout = 60\n" . "useCronUpdates = true"; $this->config->setAutoPurgeCount(3); $this->fileSystem->expects($this->once()) ->method('file_put_contents') ->with($this->equalTo($this->configPath), $this->equalTo($json)); $this->config->write($this->configPath); } 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"; $this->fileSystem->expects($this->once()) ->method('file_put_contents') ->with($this->equalTo($this->configPath), $this->equalTo($json)); $this->config->read($this->configPath, true); } }