3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
10 * Copyright © 2017-2023 The Galette Team
12 * This file is part of Galette (http://galette.tuxfamily.org).
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.
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.
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/>.
28 * @package GaletteTests
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 * @link http://galette.tuxfamily.org
37 namespace Galette\IO\test\units
;
39 use PHPUnit\Framework\TestCase
;
46 * @package GaletteTests
47 * @author Johan Cwiklinski <johan@x-tnd.be>
48 * @copyright 2017-2023 The Galette Team
49 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
50 * @link http://galette.tuxfamily.org
53 class News
extends TestCase
55 private string $local_url;
56 private \Galette\Core\I18n
$i18n;
63 public function setUp(): void
65 $this->i18n
= new \Galette\Core\
I18n();
68 $this->local_url
= 'file:///' . realpath(GALETTE_ROOT
. '../tests/feed.xml');
76 public function testLoadNews()
78 //ensure allow_url_fopen is on
79 ini_set('allow_url_fopen', true);
80 //load news without caching
81 $news = new \Galette\IO\
News($this->local_url
, true);
82 $posts = $news->getPosts();
83 $this->assertGreaterThan(0, count($posts));
91 public function testCacheNews()
93 //will use default lang to build RSS URL
94 $file = GALETTE_CACHE_DIR
. md5($this->local_url
) . '.cache';
96 //ensure file does not exist
97 $this->assertFalse(file_exists($file));
99 //load news with caching
100 $news = new \Galette\IO\
News($this->local_url
);
102 $posts = $news->getPosts();
103 $this->assertGreaterThan(0, count($posts));
105 //ensure file does exists
106 $this->assertTrue(file_exists($file));
108 $dformat = 'Y-m-d H:i:s';
109 $mdate = \DateTime
::createFromFormat(
117 $expired = $mdate->sub(
118 new \
DateInterval('PT25H')
120 $touched = touch($file, $expired->getTimestamp());
121 $this->assertTrue($touched);
123 $news = new \Galette\IO\
News($this->local_url
);
124 $mnewdate = \DateTime
::createFromFormat(
131 $isnewdate = $mnewdate > $mdate;
132 $this->assertTrue($isnewdate);
140 * Test news loading with allow_url_fopen off
144 public function testLoadNewsWExeption()
146 $news = $this->getMockBuilder(\Galette\IO\News
::class)
147 ->setConstructorArgs(array($this->local_url
, true))
148 ->onlyMethods(array('allowURLFOpen'))
150 $news->method('allowURLFOpen')->willReturn(false);
152 $this->assertCount(0, $news->getPosts());