aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/PluginManagerTest.php39
-rw-r--r--tests/container/ContainerBuilderTest.php5
-rw-r--r--tests/feed/CachedPageTest.php57
-rw-r--r--tests/helper/DailyPageHelperTest.php115
-rw-r--r--tests/plugins/test/test.php16
-rw-r--r--tests/plugins/test_route_invalid/test_route_invalid.php12
6 files changed, 217 insertions, 27 deletions
diff --git a/tests/PluginManagerTest.php b/tests/PluginManagerTest.php
index efef5e87..8947f679 100644
--- a/tests/PluginManagerTest.php
+++ b/tests/PluginManagerTest.php
@@ -120,4 +120,43 @@ class PluginManagerTest extends \Shaarli\TestCase
120 $this->assertEquals('test plugin', $meta[self::$pluginName]['description']); 120 $this->assertEquals('test plugin', $meta[self::$pluginName]['description']);
121 $this->assertEquals($expectedParameters, $meta[self::$pluginName]['parameters']); 121 $this->assertEquals($expectedParameters, $meta[self::$pluginName]['parameters']);
122 } 122 }
123
124 /**
125 * Test plugin custom routes - note that there is no check on callable functions
126 */
127 public function testRegisteredRoutes(): void
128 {
129 PluginManager::$PLUGINS_PATH = self::$pluginPath;
130 $this->pluginManager->load([self::$pluginName]);
131
132 $expectedParameters = [
133 [
134 'method' => 'GET',
135 'route' => '/test',
136 'callable' => 'getFunction',
137 ],
138 [
139 'method' => 'POST',
140 'route' => '/custom',
141 'callable' => 'postFunction',
142 ],
143 ];
144 $meta = $this->pluginManager->getRegisteredRoutes();
145 static::assertSame($expectedParameters, $meta[self::$pluginName]);
146 }
147
148 /**
149 * Test plugin custom routes with invalid route
150 */
151 public function testRegisteredRoutesInvalid(): void
152 {
153 $plugin = 'test_route_invalid';
154 $this->pluginManager->load([$plugin]);
155
156 $meta = $this->pluginManager->getRegisteredRoutes();
157 static::assertSame([], $meta);
158
159 $errors = $this->pluginManager->getErrors();
160 static::assertSame(['test_route_invalid [plugin incompatibility]: trying to register invalid route.'], $errors);
161 }
123} 162}
diff --git a/tests/container/ContainerBuilderTest.php b/tests/container/ContainerBuilderTest.php
index 3d43c344..04d4ef01 100644
--- a/tests/container/ContainerBuilderTest.php
+++ b/tests/container/ContainerBuilderTest.php
@@ -43,11 +43,15 @@ class ContainerBuilderTest extends TestCase
43 /** @var CookieManager */ 43 /** @var CookieManager */
44 protected $cookieManager; 44 protected $cookieManager;
45 45
46 /** @var PluginManager */
47 protected $pluginManager;
48
46 public function setUp(): void 49 public function setUp(): void
47 { 50 {
48 $this->conf = new ConfigManager('tests/utils/config/configJson'); 51 $this->conf = new ConfigManager('tests/utils/config/configJson');
49 $this->sessionManager = $this->createMock(SessionManager::class); 52 $this->sessionManager = $this->createMock(SessionManager::class);
50 $this->cookieManager = $this->createMock(CookieManager::class); 53 $this->cookieManager = $this->createMock(CookieManager::class);
54 $this->pluginManager = $this->createMock(PluginManager::class);
51 55
52 $this->loginManager = $this->createMock(LoginManager::class); 56 $this->loginManager = $this->createMock(LoginManager::class);
53 $this->loginManager->method('isLoggedIn')->willReturn(true); 57 $this->loginManager->method('isLoggedIn')->willReturn(true);
@@ -57,6 +61,7 @@ class ContainerBuilderTest extends TestCase
57 $this->sessionManager, 61 $this->sessionManager,
58 $this->cookieManager, 62 $this->cookieManager,
59 $this->loginManager, 63 $this->loginManager,
64 $this->pluginManager,
60 $this->createMock(LoggerInterface::class) 65 $this->createMock(LoggerInterface::class)
61 ); 66 );
62 } 67 }
diff --git a/tests/feed/CachedPageTest.php b/tests/feed/CachedPageTest.php
index 904db9dc..1decfaf3 100644
--- a/tests/feed/CachedPageTest.php
+++ b/tests/feed/CachedPageTest.php
@@ -40,10 +40,10 @@ class CachedPageTest extends \Shaarli\TestCase
40 */ 40 */
41 public function testConstruct() 41 public function testConstruct()
42 { 42 {
43 new CachedPage(self::$testCacheDir, '', true); 43 new CachedPage(self::$testCacheDir, '', true, null);
44 new CachedPage(self::$testCacheDir, '', false); 44 new CachedPage(self::$testCacheDir, '', false, null);
45 new CachedPage(self::$testCacheDir, 'http://shaar.li/feed/rss', true); 45 new CachedPage(self::$testCacheDir, 'http://shaar.li/feed/rss', true, null);
46 new CachedPage(self::$testCacheDir, 'http://shaar.li/feed/atom', false); 46 new CachedPage(self::$testCacheDir, 'http://shaar.li/feed/atom', false, null);
47 $this->addToAssertionCount(1); 47 $this->addToAssertionCount(1);
48 } 48 }
49 49
@@ -52,7 +52,7 @@ class CachedPageTest extends \Shaarli\TestCase
52 */ 52 */
53 public function testCache() 53 public function testCache()
54 { 54 {
55 $page = new CachedPage(self::$testCacheDir, self::$url, true); 55 $page = new CachedPage(self::$testCacheDir, self::$url, true, null);
56 56
57 $this->assertFileNotExists(self::$filename); 57 $this->assertFileNotExists(self::$filename);
58 $page->cache('<p>Some content</p>'); 58 $page->cache('<p>Some content</p>');
@@ -68,7 +68,7 @@ class CachedPageTest extends \Shaarli\TestCase
68 */ 68 */
69 public function testShouldNotCache() 69 public function testShouldNotCache()
70 { 70 {
71 $page = new CachedPage(self::$testCacheDir, self::$url, false); 71 $page = new CachedPage(self::$testCacheDir, self::$url, false, null);
72 72
73 $this->assertFileNotExists(self::$filename); 73 $this->assertFileNotExists(self::$filename);
74 $page->cache('<p>Some content</p>'); 74 $page->cache('<p>Some content</p>');
@@ -80,7 +80,7 @@ class CachedPageTest extends \Shaarli\TestCase
80 */ 80 */
81 public function testCachedVersion() 81 public function testCachedVersion()
82 { 82 {
83 $page = new CachedPage(self::$testCacheDir, self::$url, true); 83 $page = new CachedPage(self::$testCacheDir, self::$url, true, null);
84 84
85 $this->assertFileNotExists(self::$filename); 85 $this->assertFileNotExists(self::$filename);
86 $page->cache('<p>Some content</p>'); 86 $page->cache('<p>Some content</p>');
@@ -96,7 +96,7 @@ class CachedPageTest extends \Shaarli\TestCase
96 */ 96 */
97 public function testCachedVersionNoFile() 97 public function testCachedVersionNoFile()
98 { 98 {
99 $page = new CachedPage(self::$testCacheDir, self::$url, true); 99 $page = new CachedPage(self::$testCacheDir, self::$url, true, null);
100 100
101 $this->assertFileNotExists(self::$filename); 101 $this->assertFileNotExists(self::$filename);
102 $this->assertEquals( 102 $this->assertEquals(
@@ -110,7 +110,7 @@ class CachedPageTest extends \Shaarli\TestCase
110 */ 110 */
111 public function testNoCachedVersion() 111 public function testNoCachedVersion()
112 { 112 {
113 $page = new CachedPage(self::$testCacheDir, self::$url, false); 113 $page = new CachedPage(self::$testCacheDir, self::$url, false, null);
114 114
115 $this->assertFileNotExists(self::$filename); 115 $this->assertFileNotExists(self::$filename);
116 $this->assertEquals( 116 $this->assertEquals(
@@ -118,4 +118,43 @@ class CachedPageTest extends \Shaarli\TestCase
118 $page->cachedVersion() 118 $page->cachedVersion()
119 ); 119 );
120 } 120 }
121
122 /**
123 * Return a page's cached content within date period
124 */
125 public function testCachedVersionInDatePeriod()
126 {
127 $period = new \DatePeriod(
128 new \DateTime('yesterday'),
129 new \DateInterval('P1D'),
130 new \DateTime('tomorrow')
131 );
132 $page = new CachedPage(self::$testCacheDir, self::$url, true, $period);
133
134 $this->assertFileNotExists(self::$filename);
135 $page->cache('<p>Some content</p>');
136 $this->assertFileExists(self::$filename);
137 $this->assertEquals(
138 '<p>Some content</p>',
139 $page->cachedVersion()
140 );
141 }
142
143 /**
144 * Return a page's cached content outside of date period
145 */
146 public function testCachedVersionNotInDatePeriod()
147 {
148 $period = new \DatePeriod(
149 new \DateTime('yesterday noon'),
150 new \DateInterval('P1D'),
151 new \DateTime('yesterday midnight')
152 );
153 $page = new CachedPage(self::$testCacheDir, self::$url, true, $period);
154
155 $this->assertFileNotExists(self::$filename);
156 $page->cache('<p>Some content</p>');
157 $this->assertFileExists(self::$filename);
158 $this->assertNull($page->cachedVersion());
159 }
121} 160}
diff --git a/tests/helper/DailyPageHelperTest.php b/tests/helper/DailyPageHelperTest.php
index 5255b7b1..2d745800 100644
--- a/tests/helper/DailyPageHelperTest.php
+++ b/tests/helper/DailyPageHelperTest.php
@@ -4,6 +4,8 @@ declare(strict_types=1);
4 4
5namespace Shaarli\Helper; 5namespace Shaarli\Helper;
6 6
7use DateTimeImmutable;
8use DateTimeInterface;
7use Shaarli\Bookmark\Bookmark; 9use Shaarli\Bookmark\Bookmark;
8use Shaarli\TestCase; 10use Shaarli\TestCase;
9use Slim\Http\Request; 11use Slim\Http\Request;
@@ -32,7 +34,7 @@ class DailyPageHelperTest extends TestCase
32 string $type, 34 string $type,
33 string $input, 35 string $input,
34 ?Bookmark $bookmark, 36 ?Bookmark $bookmark,
35 \DateTimeInterface $expectedDateTime, 37 DateTimeInterface $expectedDateTime,
36 string $compareFormat = 'Ymd' 38 string $compareFormat = 'Ymd'
37 ): void { 39 ): void {
38 $dateTime = DailyPageHelper::extractRequestedDateTime($type, $input, $bookmark); 40 $dateTime = DailyPageHelper::extractRequestedDateTime($type, $input, $bookmark);
@@ -71,8 +73,8 @@ class DailyPageHelperTest extends TestCase
71 */ 73 */
72 public function testGetStartDatesByType( 74 public function testGetStartDatesByType(
73 string $type, 75 string $type,
74 \DateTimeImmutable $dateTime, 76 DateTimeImmutable $dateTime,
75 \DateTimeInterface $expectedDateTime 77 DateTimeInterface $expectedDateTime
76 ): void { 78 ): void {
77 $startDateTime = DailyPageHelper::getStartDateTimeByType($type, $dateTime); 79 $startDateTime = DailyPageHelper::getStartDateTimeByType($type, $dateTime);
78 80
@@ -84,7 +86,7 @@ class DailyPageHelperTest extends TestCase
84 $this->expectException(\Exception::class); 86 $this->expectException(\Exception::class);
85 $this->expectExceptionMessage('Unsupported daily format type'); 87 $this->expectExceptionMessage('Unsupported daily format type');
86 88
87 DailyPageHelper::getStartDateTimeByType('nope', new \DateTimeImmutable()); 89 DailyPageHelper::getStartDateTimeByType('nope', new DateTimeImmutable());
88 } 90 }
89 91
90 /** 92 /**
@@ -92,8 +94,8 @@ class DailyPageHelperTest extends TestCase
92 */ 94 */
93 public function testGetEndDatesByType( 95 public function testGetEndDatesByType(
94 string $type, 96 string $type,
95 \DateTimeImmutable $dateTime, 97 DateTimeImmutable $dateTime,
96 \DateTimeInterface $expectedDateTime 98 DateTimeInterface $expectedDateTime
97 ): void { 99 ): void {
98 $endDateTime = DailyPageHelper::getEndDateTimeByType($type, $dateTime); 100 $endDateTime = DailyPageHelper::getEndDateTimeByType($type, $dateTime);
99 101
@@ -105,7 +107,7 @@ class DailyPageHelperTest extends TestCase
105 $this->expectException(\Exception::class); 107 $this->expectException(\Exception::class);
106 $this->expectExceptionMessage('Unsupported daily format type'); 108 $this->expectExceptionMessage('Unsupported daily format type');
107 109
108 DailyPageHelper::getEndDateTimeByType('nope', new \DateTimeImmutable()); 110 DailyPageHelper::getEndDateTimeByType('nope', new DateTimeImmutable());
109 } 111 }
110 112
111 /** 113 /**
@@ -113,7 +115,7 @@ class DailyPageHelperTest extends TestCase
113 */ 115 */
114 public function testGeDescriptionsByType( 116 public function testGeDescriptionsByType(
115 string $type, 117 string $type,
116 \DateTimeImmutable $dateTime, 118 DateTimeImmutable $dateTime,
117 string $expectedDescription 119 string $expectedDescription
118 ): void { 120 ): void {
119 $description = DailyPageHelper::getDescriptionByType($type, $dateTime); 121 $description = DailyPageHelper::getDescriptionByType($type, $dateTime);
@@ -121,12 +123,25 @@ class DailyPageHelperTest extends TestCase
121 static::assertEquals($expectedDescription, $description); 123 static::assertEquals($expectedDescription, $description);
122 } 124 }
123 125
126 /**
127 * @dataProvider getDescriptionsByTypeNotIncludeRelative
128 */
129 public function testGeDescriptionsByTypeNotIncludeRelative(
130 string $type,
131 \DateTimeImmutable $dateTime,
132 string $expectedDescription
133 ): void {
134 $description = DailyPageHelper::getDescriptionByType($type, $dateTime, false);
135
136 static::assertEquals($expectedDescription, $description);
137 }
138
124 public function getDescriptionByTypeExceptionUnknownType(): void 139 public function getDescriptionByTypeExceptionUnknownType(): void
125 { 140 {
126 $this->expectException(\Exception::class); 141 $this->expectException(\Exception::class);
127 $this->expectExceptionMessage('Unsupported daily format type'); 142 $this->expectExceptionMessage('Unsupported daily format type');
128 143
129 DailyPageHelper::getDescriptionByType('nope', new \DateTimeImmutable()); 144 DailyPageHelper::getDescriptionByType('nope', new DateTimeImmutable());
130 } 145 }
131 146
132 /** 147 /**
@@ -147,6 +162,29 @@ class DailyPageHelperTest extends TestCase
147 } 162 }
148 163
149 /** 164 /**
165 * @dataProvider getCacheDatePeriodByType
166 */
167 public function testGetCacheDatePeriodByType(
168 string $type,
169 DateTimeImmutable $requested,
170 DateTimeInterface $start,
171 DateTimeInterface $end
172 ): void {
173 $period = DailyPageHelper::getCacheDatePeriodByType($type, $requested);
174
175 static::assertEquals($start, $period->getStartDate());
176 static::assertEquals($end, $period->getEndDate());
177 }
178
179 public function testGetCacheDatePeriodByTypeExceptionUnknownType(): void
180 {
181 $this->expectException(\Exception::class);
182 $this->expectExceptionMessage('Unsupported daily format type');
183
184 DailyPageHelper::getCacheDatePeriodByType('nope');
185 }
186
187 /**
150 * Data provider for testExtractRequestedType() test method. 188 * Data provider for testExtractRequestedType() test method.
151 */ 189 */
152 public function getRequestedTypes(): array 190 public function getRequestedTypes(): array
@@ -216,9 +254,9 @@ class DailyPageHelperTest extends TestCase
216 public function getStartDatesByType(): array 254 public function getStartDatesByType(): array
217 { 255 {
218 return [ 256 return [
219 [DailyPageHelper::DAY, new \DateTimeImmutable('2020-10-09 04:05:06'), new \DateTime('2020-10-09 00:00:00')], 257 [DailyPageHelper::DAY, new DateTimeImmutable('2020-10-09 04:05:06'), new \DateTime('2020-10-09 00:00:00')],
220 [DailyPageHelper::WEEK, new \DateTimeImmutable('2020-10-09 04:05:06'), new \DateTime('2020-10-05 00:00:00')], 258 [DailyPageHelper::WEEK, new DateTimeImmutable('2020-10-09 04:05:06'), new \DateTime('2020-10-05 00:00:00')],
221 [DailyPageHelper::MONTH, new \DateTimeImmutable('2020-10-09 04:05:06'), new \DateTime('2020-10-01 00:00:00')], 259 [DailyPageHelper::MONTH, new DateTimeImmutable('2020-10-09 04:05:06'), new \DateTime('2020-10-01 00:00:00')],
222 ]; 260 ];
223 } 261 }
224 262
@@ -228,9 +266,9 @@ class DailyPageHelperTest extends TestCase
228 public function getEndDatesByType(): array 266 public function getEndDatesByType(): array
229 { 267 {
230 return [ 268 return [
231 [DailyPageHelper::DAY, new \DateTimeImmutable('2020-10-09 04:05:06'), new \DateTime('2020-10-09 23:59:59')], 269 [DailyPageHelper::DAY, new DateTimeImmutable('2020-10-09 04:05:06'), new \DateTime('2020-10-09 23:59:59')],
232 [DailyPageHelper::WEEK, new \DateTimeImmutable('2020-10-09 04:05:06'), new \DateTime('2020-10-11 23:59:59')], 270 [DailyPageHelper::WEEK, new DateTimeImmutable('2020-10-09 04:05:06'), new \DateTime('2020-10-11 23:59:59')],
233 [DailyPageHelper::MONTH, new \DateTimeImmutable('2020-10-09 04:05:06'), new \DateTime('2020-10-31 23:59:59')], 271 [DailyPageHelper::MONTH, new DateTimeImmutable('2020-10-09 04:05:06'), new \DateTime('2020-10-31 23:59:59')],
234 ]; 272 ];
235 } 273 }
236 274
@@ -240,8 +278,22 @@ class DailyPageHelperTest extends TestCase
240 public function getDescriptionsByType(): array 278 public function getDescriptionsByType(): array
241 { 279 {
242 return [ 280 return [
243 [DailyPageHelper::DAY, $date = new \DateTimeImmutable(), 'Today - ' . $date->format('F j, Y')], 281 [DailyPageHelper::DAY, $date = new DateTimeImmutable(), 'Today - ' . $date->format('F j, Y')],
244 [DailyPageHelper::DAY, $date = new \DateTimeImmutable('-1 day'), 'Yesterday - ' . $date->format('F j, Y')], 282 [DailyPageHelper::DAY, $date = new DateTimeImmutable('-1 day'), 'Yesterday - ' . $date->format('F j, Y')],
283 [DailyPageHelper::DAY, new DateTimeImmutable('2020-10-09 04:05:06'), 'October 9, 2020'],
284 [DailyPageHelper::WEEK, new DateTimeImmutable('2020-10-09 04:05:06'), 'Week 41 (October 5, 2020)'],
285 [DailyPageHelper::MONTH, new DateTimeImmutable('2020-10-09 04:05:06'), 'October, 2020'],
286 ];
287 }
288
289 /**
290 * Data provider for testGeDescriptionsByTypeNotIncludeRelative() test method.
291 */
292 public function getDescriptionsByTypeNotIncludeRelative(): array
293 {
294 return [
295 [DailyPageHelper::DAY, $date = new \DateTimeImmutable(), $date->format('F j, Y')],
296 [DailyPageHelper::DAY, $date = new \DateTimeImmutable('-1 day'), $date->format('F j, Y')],
245 [DailyPageHelper::DAY, new \DateTimeImmutable('2020-10-09 04:05:06'), 'October 9, 2020'], 297 [DailyPageHelper::DAY, new \DateTimeImmutable('2020-10-09 04:05:06'), 'October 9, 2020'],
246 [DailyPageHelper::WEEK, new \DateTimeImmutable('2020-10-09 04:05:06'), 'Week 41 (October 5, 2020)'], 298 [DailyPageHelper::WEEK, new \DateTimeImmutable('2020-10-09 04:05:06'), 'Week 41 (October 5, 2020)'],
247 [DailyPageHelper::MONTH, new \DateTimeImmutable('2020-10-09 04:05:06'), 'October, 2020'], 299 [DailyPageHelper::MONTH, new \DateTimeImmutable('2020-10-09 04:05:06'), 'October, 2020'],
@@ -249,7 +301,7 @@ class DailyPageHelperTest extends TestCase
249 } 301 }
250 302
251 /** 303 /**
252 * Data provider for testGetDescriptionsByType() test method. 304 * Data provider for testGetRssLengthsByType() test method.
253 */ 305 */
254 public function getRssLengthsByType(): array 306 public function getRssLengthsByType(): array
255 { 307 {
@@ -259,4 +311,31 @@ class DailyPageHelperTest extends TestCase
259 [DailyPageHelper::MONTH], 311 [DailyPageHelper::MONTH],
260 ]; 312 ];
261 } 313 }
314
315 /**
316 * Data provider for testGetCacheDatePeriodByType() test method.
317 */
318 public function getCacheDatePeriodByType(): array
319 {
320 return [
321 [
322 DailyPageHelper::DAY,
323 new DateTimeImmutable('2020-10-09 04:05:06'),
324 new \DateTime('2020-10-09 00:00:00'),
325 new \DateTime('2020-10-09 23:59:59'),
326 ],
327 [
328 DailyPageHelper::WEEK,
329 new DateTimeImmutable('2020-10-09 04:05:06'),
330 new \DateTime('2020-10-05 00:00:00'),
331 new \DateTime('2020-10-11 23:59:59'),
332 ],
333 [
334 DailyPageHelper::MONTH,
335 new DateTimeImmutable('2020-10-09 04:05:06'),
336 new \DateTime('2020-10-01 00:00:00'),
337 new \DateTime('2020-10-31 23:59:59'),
338 ],
339 ];
340 }
262} 341}
diff --git a/tests/plugins/test/test.php b/tests/plugins/test/test.php
index 03be4f4e..34cd339e 100644
--- a/tests/plugins/test/test.php
+++ b/tests/plugins/test/test.php
@@ -27,3 +27,19 @@ function hook_test_error()
27{ 27{
28 new Unknown(); 28 new Unknown();
29} 29}
30
31function test_register_routes(): array
32{
33 return [
34 [
35 'method' => 'GET',
36 'route' => '/test',
37 'callable' => 'getFunction',
38 ],
39 [
40 'method' => 'POST',
41 'route' => '/custom',
42 'callable' => 'postFunction',
43 ],
44 ];
45}
diff --git a/tests/plugins/test_route_invalid/test_route_invalid.php b/tests/plugins/test_route_invalid/test_route_invalid.php
new file mode 100644
index 00000000..0c5a5101
--- /dev/null
+++ b/tests/plugins/test_route_invalid/test_route_invalid.php
@@ -0,0 +1,12 @@
1<?php
2
3function test_route_invalid_register_routes(): array
4{
5 return [
6 [
7 'method' => 'GET',
8 'route' => 'not a route',
9 'callable' => 'getFunction',
10 ],
11 ];
12}