aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--src/Wallabag/ApiBundle/Controller/EntryRestController.php2
-rw-r--r--src/Wallabag/CoreBundle/Helper/ContentProxy.php9
-rw-r--r--tests/Wallabag/CoreBundle/Helper/ContentProxyTest.php81
3 files changed, 90 insertions, 2 deletions
diff --git a/src/Wallabag/ApiBundle/Controller/EntryRestController.php b/src/Wallabag/ApiBundle/Controller/EntryRestController.php
index cc2cca64..c3ba1858 100644
--- a/src/Wallabag/ApiBundle/Controller/EntryRestController.php
+++ b/src/Wallabag/ApiBundle/Controller/EntryRestController.php
@@ -286,7 +286,7 @@ class EntryRestController extends WallabagRestController
286 * {"name"="content", "dataType"="string", "required"=false, "description"="Content of the entry"}, 286 * {"name"="content", "dataType"="string", "required"=false, "description"="Content of the entry"},
287 * {"name"="language", "dataType"="string", "required"=false, "description"="Language of the entry"}, 287 * {"name"="language", "dataType"="string", "required"=false, "description"="Language of the entry"},
288 * {"name"="preview_picture", "dataType"="string", "required"=false, "description"="Preview picture of the entry"}, 288 * {"name"="preview_picture", "dataType"="string", "required"=false, "description"="Preview picture of the entry"},
289 * {"name"="published_at", "dataType"="datetime", "format"="YYYY-MM-DDTHH:II:SS+TZ", "required"=false, "description"="Published date of the entry"}, 289 * {"name"="published_at", "dataType"="datetime|integer", "format"="YYYY-MM-DDTHH:II:SS+TZ or a timestamp", "required"=false, "description"="Published date of the entry"},
290 * {"name"="authors", "dataType"="string", "format"="Name Firstname,author2,author3", "required"=false, "description"="Authors of the entry"}, 290 * {"name"="authors", "dataType"="string", "format"="Name Firstname,author2,author3", "required"=false, "description"="Authors of the entry"},
291 * } 291 * }
292 * ) 292 * )
diff --git a/src/Wallabag/CoreBundle/Helper/ContentProxy.php b/src/Wallabag/CoreBundle/Helper/ContentProxy.php
index 90d0c50d..8ba77ca9 100644
--- a/src/Wallabag/CoreBundle/Helper/ContentProxy.php
+++ b/src/Wallabag/CoreBundle/Helper/ContentProxy.php
@@ -82,8 +82,15 @@ class ContentProxy
82 $entry->setHttpStatus(isset($content['status']) ? $content['status'] : ''); 82 $entry->setHttpStatus(isset($content['status']) ? $content['status'] : '');
83 83
84 if (!empty($content['date'])) { 84 if (!empty($content['date'])) {
85 $date = $content['date'];
86
87 // is it a timestamp?
88 if (filter_var($date, FILTER_VALIDATE_INT) !== false) {
89 $date = '@'.$content['date'];
90 }
91
85 try { 92 try {
86 $entry->setPublishedAt(new \DateTime($content['date'])); 93 $entry->setPublishedAt(new \DateTime($date));
87 } catch (\Exception $e) { 94 } catch (\Exception $e) {
88 $this->logger->warning('Error while defining date', ['e' => $e, 'url' => $url, 'date' => $content['date']]); 95 $this->logger->warning('Error while defining date', ['e' => $e, 'url' => $url, 'date' => $content['date']]);
89 } 96 }
diff --git a/tests/Wallabag/CoreBundle/Helper/ContentProxyTest.php b/tests/Wallabag/CoreBundle/Helper/ContentProxyTest.php
index 11f1d410..103acf50 100644
--- a/tests/Wallabag/CoreBundle/Helper/ContentProxyTest.php
+++ b/tests/Wallabag/CoreBundle/Helper/ContentProxyTest.php
@@ -9,6 +9,8 @@ use Wallabag\CoreBundle\Entity\Tag;
9use Wallabag\UserBundle\Entity\User; 9use Wallabag\UserBundle\Entity\User;
10use Wallabag\CoreBundle\Helper\RuleBasedTagger; 10use Wallabag\CoreBundle\Helper\RuleBasedTagger;
11use Graby\Graby; 11use Graby\Graby;
12use Monolog\Handler\TestHandler;
13use Monolog\Logger;
12 14
13class ContentProxyTest extends \PHPUnit_Framework_TestCase 15class ContentProxyTest extends \PHPUnit_Framework_TestCase
14{ 16{
@@ -220,6 +222,11 @@ class ContentProxyTest extends \PHPUnit_Framework_TestCase
220 'url' => 'http://1.1.1.1', 222 'url' => 'http://1.1.1.1',
221 'content_type' => 'text/html', 223 'content_type' => 'text/html',
222 'language' => 'fr', 224 'language' => 'fr',
225 'date' => '1395635872',
226 'authors' => ['Jeremy', 'Nico', 'Thomas'],
227 'all_headers' => [
228 'Cache-Control' => 'no-cache',
229 ]
223 ] 230 ]
224 ); 231 );
225 232
@@ -230,6 +237,80 @@ class ContentProxyTest extends \PHPUnit_Framework_TestCase
230 $this->assertEquals('fr', $entry->getLanguage()); 237 $this->assertEquals('fr', $entry->getLanguage());
231 $this->assertEquals(4.0, $entry->getReadingTime()); 238 $this->assertEquals(4.0, $entry->getReadingTime());
232 $this->assertEquals('1.1.1.1', $entry->getDomainName()); 239 $this->assertEquals('1.1.1.1', $entry->getDomainName());
240 $this->assertEquals('24/03/2014', $entry->getPublishedAt()->format('d/m/Y'));
241 $this->assertContains('Jeremy', $entry->getPublishedBy());
242 $this->assertContains('Nico', $entry->getPublishedBy());
243 $this->assertContains('Thomas', $entry->getPublishedBy());
244 $this->assertContains('no-cache', $entry->getHeaders());
245 }
246
247 public function testWithForcedContentAndDatetime()
248 {
249 $tagger = $this->getTaggerMock();
250 $tagger->expects($this->once())
251 ->method('tag');
252
253 $proxy = new ContentProxy((new Graby()), $tagger, $this->getTagRepositoryMock(), $this->getLogger(), $this->fetchingErrorMessage);
254 $entry = $proxy->updateEntry(
255 new Entry(new User()),
256 'http://0.0.0.0',
257 [
258 'html' => str_repeat('this is my content', 325),
259 'title' => 'this is my title',
260 'url' => 'http://1.1.1.1',
261 'content_type' => 'text/html',
262 'language' => 'fr',
263 'date' => '2016-09-08T11:55:58+0200',
264 ]
265 );
266
267 $this->assertEquals('http://1.1.1.1', $entry->getUrl());
268 $this->assertEquals('this is my title', $entry->getTitle());
269 $this->assertContains('this is my content', $entry->getContent());
270 $this->assertEquals('text/html', $entry->getMimetype());
271 $this->assertEquals('fr', $entry->getLanguage());
272 $this->assertEquals(4.0, $entry->getReadingTime());
273 $this->assertEquals('1.1.1.1', $entry->getDomainName());
274 $this->assertEquals('08/09/2016', $entry->getPublishedAt()->format('d/m/Y'));
275 }
276
277 public function testWithForcedContentAndBadDate()
278 {
279 $tagger = $this->getTaggerMock();
280 $tagger->expects($this->once())
281 ->method('tag');
282
283 $logger = new Logger('foo');
284 $handler = new TestHandler();
285 $logger->pushHandler($handler);
286
287 $proxy = new ContentProxy((new Graby()), $tagger, $this->getTagRepositoryMock(), $logger, $this->fetchingErrorMessage);
288 $entry = $proxy->updateEntry(
289 new Entry(new User()),
290 'http://0.0.0.0',
291 [
292 'html' => str_repeat('this is my content', 325),
293 'title' => 'this is my title',
294 'url' => 'http://1.1.1.1',
295 'content_type' => 'text/html',
296 'language' => 'fr',
297 'date' => '01 02 2012',
298 ]
299 );
300
301 $this->assertEquals('http://1.1.1.1', $entry->getUrl());
302 $this->assertEquals('this is my title', $entry->getTitle());
303 $this->assertContains('this is my content', $entry->getContent());
304 $this->assertEquals('text/html', $entry->getMimetype());
305 $this->assertEquals('fr', $entry->getLanguage());
306 $this->assertEquals(4.0, $entry->getReadingTime());
307 $this->assertEquals('1.1.1.1', $entry->getDomainName());
308 $this->assertNull($entry->getPublishedAt());
309
310 $records = $handler->getRecords();
311
312 $this->assertCount(1, $records);
313 $this->assertContains('Error while defining date', $records[0]['message']);
233 } 314 }
234 315
235 public function testTaggerThrowException() 316 public function testTaggerThrowException()