]> git.agnieray.net Git - galette.git/blob - tests/Galette/IO/tests/units/News.php
Migrate to phpunit; closes #1674
[galette.git] / tests / Galette / IO / tests / units / News.php
1 <?php
2
3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4
5 /**
6 * News tests
7 *
8 * PHP version 5
9 *
10 * Copyright © 2017-2023 The Galette Team
11 *
12 * This file is part of Galette (http://galette.tuxfamily.org).
13 *
14 * Galette is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation, either version 3 of the License, or
17 * (at your option) any later version.
18 *
19 * Galette is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with Galette. If not, see <http://www.gnu.org/licenses/>.
26 *
27 * @category Core
28 * @package GaletteTests
29 *
30 * @author Johan Cwiklinski <johan@x-tnd.be>
31 * @copyright 2017-2023 The Galette Team
32 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
33 * @version SVN: $Id$
34 * @link http://galette.tuxfamily.org
35 * @since 2017-03-07
36 */
37
38 namespace Galette\IO\test\units;
39
40 use PHPUnit\Framework\TestCase;
41
42 /**
43 * News tests class
44 *
45 * @category Core
46 * @name News
47 * @package GaletteTests
48 * @author Johan Cwiklinski <johan@x-tnd.be>
49 * @copyright 2017-2023 The Galette Team
50 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
51 * @link http://galette.tuxfamily.org
52 * @since 2017-03-07
53 */
54 class News extends TestCase
55 {
56 private \Galette\Core\I18n $i18n;
57
58 /**
59 * Set up tests
60 *
61 * @return void
62 */
63 public function setUp(): void
64 {
65 $this->i18n = new \Galette\Core\I18n();
66 global $i18n;
67 $i18n = $this->i18n;
68 }
69
70 /**
71 * Test news loading
72 *
73 * @return void
74 */
75 public function testLoadNews()
76 {
77 //ensure allow_url_fopen is on
78 ini_set('allow_url_fopen', true);
79 //load news without caching
80 $news = new \Galette\IO\News('https://galette.eu/site/feed.xml', true);
81 $posts = $news->getPosts();
82 $this->assertGreaterThan(0, count($posts));
83 }
84
85 /**
86 * Test news caching
87 *
88 * @return void
89 */
90 public function testCacheNews()
91 {
92 //will use default lang to build RSS URL
93 $file = GALETTE_CACHE_DIR . md5('https://galette.eu/site/feed.xml') . '.cache';
94
95 //ensure file does not exist
96 $this->assertFalse(file_exists($file));
97
98 //load news with caching
99 $news = new \Galette\IO\News('https://galette.eu/site/feed.xml');
100
101 $posts = $news->getPosts();
102 $this->assertGreaterThan(0, count($posts));
103
104 //ensure file does exists
105 $this->assertTrue(file_exists($file));
106
107 $dformat = 'Y-m-d H:i:s';
108 $mdate = \DateTime::createFromFormat(
109 $dformat,
110 date(
111 $dformat,
112 filemtime($file)
113 )
114 );
115
116 $expired = $mdate->sub(
117 new \DateInterval('PT25H')
118 );
119 $touched = touch($file, $expired->getTimestamp());
120 $this->assertTrue($touched);
121
122 $news = new \Galette\IO\News('https://galette.eu/site/feed.xml');
123 $mnewdate = \DateTime::createFromFormat(
124 $dformat,
125 date(
126 $dformat,
127 filemtime($file)
128 )
129 );
130 $isnewdate = $mnewdate > $mdate;
131 $this->assertTrue($isnewdate);
132
133 //drop file finally
134 unlink($file);
135 }
136
137
138 /**
139 * Test news loading with allow_url_fopen off
140 *
141 * @return void
142 */
143 public function testLoadNewsWExeption()
144 {
145 $news = $this->getMockBuilder(\Galette\IO\News::class)
146 ->setConstructorArgs(array('https://galette.eu/site/feed.xml', true))
147 ->onlyMethods(array('allowURLFOpen'))
148 ->getMock();
149 $news->method('allowURLFOpen')->willReturn(false);
150
151 $this->assertCount(0, $news->getPosts());
152 }
153 }