]> git.agnieray.net Git - galette.git/blob - tests/Galette/IO/tests/units/News.php
Do not rely on website for tests; closes #1721
[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 * @link http://galette.tuxfamily.org
34 * @since 2017-03-07
35 */
36
37 namespace Galette\IO\test\units;
38
39 use PHPUnit\Framework\TestCase;
40
41 /**
42 * News tests class
43 *
44 * @category Core
45 * @name News
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
51 * @since 2017-03-07
52 */
53 class News extends TestCase
54 {
55 private string $local_url;
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 $this->local_url = 'file:///' . realpath(GALETTE_ROOT . '../tests/feed.xml');
69 }
70
71 /**
72 * Test news loading
73 *
74 * @return void
75 */
76 public function testLoadNews()
77 {
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));
84 }
85
86 /**
87 * Test news caching
88 *
89 * @return void
90 */
91 public function testCacheNews()
92 {
93 //will use default lang to build RSS URL
94 $file = GALETTE_CACHE_DIR . md5($this->local_url) . '.cache';
95
96 //ensure file does not exist
97 $this->assertFalse(file_exists($file));
98
99 //load news with caching
100 $news = new \Galette\IO\News($this->local_url);
101
102 $posts = $news->getPosts();
103 $this->assertGreaterThan(0, count($posts));
104
105 //ensure file does exists
106 $this->assertTrue(file_exists($file));
107
108 $dformat = 'Y-m-d H:i:s';
109 $mdate = \DateTime::createFromFormat(
110 $dformat,
111 date(
112 $dformat,
113 filemtime($file)
114 )
115 );
116
117 $expired = $mdate->sub(
118 new \DateInterval('PT25H')
119 );
120 $touched = touch($file, $expired->getTimestamp());
121 $this->assertTrue($touched);
122
123 $news = new \Galette\IO\News($this->local_url);
124 $mnewdate = \DateTime::createFromFormat(
125 $dformat,
126 date(
127 $dformat,
128 filemtime($file)
129 )
130 );
131 $isnewdate = $mnewdate > $mdate;
132 $this->assertTrue($isnewdate);
133
134 //drop file finally
135 unlink($file);
136 }
137
138
139 /**
140 * Test news loading with allow_url_fopen off
141 *
142 * @return void
143 */
144 public function testLoadNewsWExeption()
145 {
146 $news = $this->getMockBuilder(\Galette\IO\News::class)
147 ->setConstructorArgs(array($this->local_url, true))
148 ->onlyMethods(array('allowURLFOpen'))
149 ->getMock();
150 $news->method('allowURLFOpen')->willReturn(false);
151
152 $this->assertCount(0, $news->getPosts());
153 }
154 }