summaryrefslogtreecommitdiffstats
path: root/tests/unit/config/ConfigTest.php
blob: 5baeae3ba325d84b309fab11d176e61b4958b4e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
<?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('', $this->config->getExploreUrl());
        $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" .
            'exploreUrl = http://google.de' . "\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->config->setExploreUrl('http://google.de');

        $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" .
            'exploreUrl = ' . "\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);
    }
}