summaryrefslogtreecommitdiffstats
path: root/tests/unit/config/ConfigTest.php
pre { line-height: 125%; } td.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; } span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; } td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; } span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; } .highlight .hll { background-color: #ffffcc } .highlight .c { color: #888888 } /* Comment */ .highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */ .highlight .k { color: #008800; font-weight: bold } /* Keyword */ .highlight .ch { color: #888888 } /* Comment.Hashbang */ .highlight .cm { color: #888888 } /* Comment.Multiline */ .highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */ .highlight .cpf { color: #888888 } /* Comment.PreprocFile */ .highlight .c1 { color: #888888 } /* Comment.Single */ .highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */ .highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */ .highlight .ge { font-style: italic } /* Generic.Emph */ .highlight .ges { font-weight: bold; font-style: italic } /* Generic.EmphStrong */ .highlight .gr { color: #aa0000 } /* Generic.Error */ .highlight .gh { color: #333333 } /* Generic.Heading */ .highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */ .highlight .go { color: #888888 } /* Generic.Output */ .highlight .gp { color: #555555 } /* Generic.Prompt */ .highlight .gs { font-weight: bold } /* Generic.Strong */ .highlight .gu { color: #666666 } /* Generic.Subheading */ .highlight .gt { color: #aa0000 } /* Generic.Traceback */ .highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */ .highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */ .highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */ .highlight .kp { color: #008800 } /* Keyword.Pseudo */ .highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */ .highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */ .highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */ .highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */ .highlight .na { color: #336699 } /* Name.Attribute */ .highlight .nb { color: #003388 } /* Name.Builtin */ .highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */ .highlight .no { color: #003366; font-weight: bold } /* Name.Constant */ .highlight .nd { color: #555555 } /* Name.Decorator */ .highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */ .highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */ .highlight .nl { color: #336699; font-style: italic } /* Name.Label */ .highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */ .highlight .py { color: #336699; font-weight: bold } /* Name.Property */ .highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */ .highlight .nv { color: #336699 } /* Name.Variable */ .highlight .ow { color: #008800 } /* Operator.Word */ .highlight .w { color: #bbbbbb } /* Text.Whitespace */ .highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */ .highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */ .highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */ .highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */ .highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */ .highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */ .highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */ .highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */ .highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */ .highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */ .highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */ .highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */ .highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */ .highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */ .highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */ .highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */ .highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */ .highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */ .highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #336699 } /* Name.Variable.Class */ .highlight .vg { color: #dd7700 } /* Name.Variable.Global */ .highlight .vi { color: #3333bb } /* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
# This file contains a list of commits that are not likely what you
# are looking for in a blame, such as mass reformatting or renaming.
# You can set this file as a default ignore file for blame by running
# the following command.
#
# $ git config blame.ignoreRevsFile .git-blame-ignore-revs
#
# To temporarily not use this file add
# --ignore-revs-file=""
# to your blame command.
#
# The ignoreRevsFile can't be set globally due to blame failing if the file isn't present.
# To not have to set the option in every repository it is needed in,
# save the following script in your path with the name "git-bblame"
# now you can run
# $ git bblame $FILE
# to use the .git-blame-ignore-revs file if it is present.
#
# #!/usr/bin/env bash
# repo_root=$(git rev-parse --show-toplevel)
# if [[ -e $repo_root/.git-blame-ignore-revs ]]; then
#     git blame --ignore-revs-file="$repo_root/.git-blame-ignore-revs" $@
# else
#     git blame $@
# fi


# nixos/modules/rename: Sort alphabetically
1f71224fe86605ef4cd23ed327b3da7882dad382

# manual: fix typos
feddd5e7f8c6f8167b48a077fa2a5394dc008999

# nixos: fix module paths in rename.nix
d08ede042b74b8199dc748323768227b88efcf7c

# fix indentation in mk-python-derivation.nix
d1c1a0c656ccd8bd3b25d3c4287f2d075faf3cf3

# fix indentation in meteor default.nix
a37a6de881ec4c6708e6b88fd16256bbc7f26bbd
09 210 211 212 213 214 215 216 217
<?php
/**
 * ownCloud - News
 *
 * This file is licensed under the Affero General Public License version 3 or
 * later. See the COPYING file.
 *
 * @author Alessandro Cosentino <cosenal@gmail.com>
 * @author Bernhard Posselt <dev@bernhard-posselt.com>
 * @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(10, $this->config->getMaxRedirects());
        $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());
        $this->assertEquals(1024*1024*100, $this->config->getMaxSize());
    }


    public function testRead () {
        $this->fileSystem->expects($this->once())
            ->method('file_get_contents')
            ->with($this->equalTo($this->configPath))
            ->will($this->returnValue(
                'autoPurgeCount = 3' . "\n" . 'useCronUpdates = 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' . "\n" . 'useCronUpdates = 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" .
            'maxRedirects = 10' . "\n" .
            'maxSize = 399' . "\n" .
            'feedFetcherTimeout = 60' . "\n" .
            'useCronUpdates = true';
        $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->config->setMaxSize(399);

        $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" .
            'maxRedirects = 10' . "\n" .
            'maxSize = 104857600' . "\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);
    }


    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 testMaxRedirects() {
        $this->config->setMaxRedirects(21);
        $redirects = $this->config->getMaxRedirects();

        $this->assertSame(21, $redirects);
    }

    public function testFeedFetcherTimeout() {
        $this->config->setFeedFetcherTimeout(2);
        $timout = $this->config->getFeedFetcherTimeout();

        $this->assertSame(2, $timout);
    }
}