aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorJérémy Benoist <j0k3r@users.noreply.github.com>2017-12-14 11:19:44 +0100
committerGitHub <noreply@github.com>2017-12-14 11:19:44 +0100
commita68a80f654be86ae25d49420b86099302ce55bb6 (patch)
tree158cb4743920e91f87f2194287b81095f527bad2
parent70265817aee257e7e635eda79ce3e037e3b4a242 (diff)
parentb457d7bd32b0eb228f6802d92630d8f0524f042f (diff)
downloadwallabag-a68a80f654be86ae25d49420b86099302ce55bb6.tar.gz
wallabag-a68a80f654be86ae25d49420b86099302ce55bb6.tar.zst
wallabag-a68a80f654be86ae25d49420b86099302ce55bb6.zip
Merge pull request #3442 from wallabag/empty-entry
Fix empty title and domain_name when exception is thrown during fetch
-rw-r--r--CHANGELOG.md9
-rw-r--r--src/Wallabag/ApiBundle/Controller/EntryRestController.php16
-rw-r--r--src/Wallabag/CoreBundle/Controller/EntryController.php8
-rw-r--r--src/Wallabag/CoreBundle/Helper/ContentProxy.php37
-rw-r--r--tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php2
-rw-r--r--tests/Wallabag/CoreBundle/Controller/EntryControllerTest.php49
-rw-r--r--tests/Wallabag/CoreBundle/WallabagCoreTestCase.php5
7 files changed, 121 insertions, 5 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index eb8b1f70..59d7f2d2 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,6 +1,13 @@
1# Changelog 1# Changelog
2 2
3## [2.3.0](https://github.com/wallabag/wallabag/tree/2.3.0) (2017-10-21) 3## [unreleased](https://github.com/wallabag/wallabag/tree/master)
4 [Full Changelog](https://github.com/wallabag/wallabag/compare/2.3.0...master)
5
6### Fixes
7
8 - Fix empty title and domain_name when exception is thrown during fetch [#3442](https://github.com/wallabag/wallabag/pull/3442)
9
10## [2.3.0](https://github.com/wallabag/wallabag/tree/2.3.0) (2017-12-11)
4 [Full Changelog](https://github.com/wallabag/wallabag/compare/2.2.3...2.3.0) 11 [Full Changelog](https://github.com/wallabag/wallabag/compare/2.2.3...2.3.0)
5 12
6### API 13### API
diff --git a/src/Wallabag/ApiBundle/Controller/EntryRestController.php b/src/Wallabag/ApiBundle/Controller/EntryRestController.php
index 7d820c7e..acca219f 100644
--- a/src/Wallabag/ApiBundle/Controller/EntryRestController.php
+++ b/src/Wallabag/ApiBundle/Controller/EntryRestController.php
@@ -381,6 +381,14 @@ class EntryRestController extends WallabagRestController
381 } 381 }
382 } 382 }
383 383
384 if (empty($entry->getDomainName())) {
385 $this->get('wallabag_core.content_proxy')->setEntryDomainName($entry);
386 }
387
388 if (empty($entry->getTitle())) {
389 $this->get('wallabag_core.content_proxy')->setDefaultEntryTitle($entry);
390 }
391
384 $em = $this->getDoctrine()->getManager(); 392 $em = $this->getDoctrine()->getManager();
385 $em->persist($entry); 393 $em->persist($entry);
386 $em->flush(); 394 $em->flush();
@@ -490,6 +498,14 @@ class EntryRestController extends WallabagRestController
490 $entry->setOriginUrl($data['origin_url']); 498 $entry->setOriginUrl($data['origin_url']);
491 } 499 }
492 500
501 if (empty($entry->getDomainName())) {
502 $this->get('wallabag_core.content_proxy')->setEntryDomainName($entry);
503 }
504
505 if (empty($entry->getTitle())) {
506 $this->get('wallabag_core.content_proxy')->setDefaultEntryTitle($entry);
507 }
508
493 $em = $this->getDoctrine()->getManager(); 509 $em = $this->getDoctrine()->getManager();
494 $em->persist($entry); 510 $em->persist($entry);
495 $em->flush(); 511 $em->flush();
diff --git a/src/Wallabag/CoreBundle/Controller/EntryController.php b/src/Wallabag/CoreBundle/Controller/EntryController.php
index 840dc254..b7fdea27 100644
--- a/src/Wallabag/CoreBundle/Controller/EntryController.php
+++ b/src/Wallabag/CoreBundle/Controller/EntryController.php
@@ -502,6 +502,14 @@ class EntryController extends Controller
502 $message = 'flashes.entry.notice.' . $prefixMessage . '_failed'; 502 $message = 'flashes.entry.notice.' . $prefixMessage . '_failed';
503 } 503 }
504 504
505 if (empty($entry->getDomainName())) {
506 $this->get('wallabag_core.content_proxy')->setEntryDomainName($entry);
507 }
508
509 if (empty($entry->getTitle())) {
510 $this->get('wallabag_core.content_proxy')->setDefaultEntryTitle($entry);
511 }
512
505 $this->get('session')->getFlashBag()->add('notice', $message); 513 $this->get('session')->getFlashBag()->add('notice', $message);
506 } 514 }
507 515
diff --git a/src/Wallabag/CoreBundle/Helper/ContentProxy.php b/src/Wallabag/CoreBundle/Helper/ContentProxy.php
index 4cc20c9c..fe795d42 100644
--- a/src/Wallabag/CoreBundle/Helper/ContentProxy.php
+++ b/src/Wallabag/CoreBundle/Helper/ContentProxy.php
@@ -145,6 +145,38 @@ class ContentProxy
145 } 145 }
146 146
147 /** 147 /**
148 * Helper to extract and save host from entry url.
149 *
150 * @param Entry $entry
151 */
152 public function setEntryDomainName(Entry $entry)
153 {
154 $domainName = parse_url($entry->getUrl(), PHP_URL_HOST);
155 if (false !== $domainName) {
156 $entry->setDomainName($domainName);
157 }
158 }
159
160 /**
161 * Helper to set a default title using:
162 * - url basename, if applicable
163 * - hostname.
164 *
165 * @param Entry $entry
166 */
167 public function setDefaultEntryTitle(Entry $entry)
168 {
169 $url = parse_url($entry->getUrl());
170 $path = pathinfo($url['path'], PATHINFO_BASENAME);
171
172 if (empty($path)) {
173 $path = $url['host'];
174 }
175
176 $entry->setTitle($path);
177 }
178
179 /**
148 * Stock entry with fetched or imported content. 180 * Stock entry with fetched or imported content.
149 * Will fall back to OpenGraph data if available. 181 * Will fall back to OpenGraph data if available.
150 * 182 *
@@ -155,10 +187,7 @@ class ContentProxy
155 { 187 {
156 $entry->setUrl($content['url']); 188 $entry->setUrl($content['url']);
157 189
158 $domainName = parse_url($entry->getUrl(), PHP_URL_HOST); 190 $this->setEntryDomainName($entry);
159 if (false !== $domainName) {
160 $entry->setDomainName($domainName);
161 }
162 191
163 if (!empty($content['title'])) { 192 if (!empty($content['title'])) {
164 $entry->setTitle($content['title']); 193 $entry->setTitle($content['title']);
diff --git a/tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php b/tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php
index b0d4c4e1..5c7b988c 100644
--- a/tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php
+++ b/tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php
@@ -501,6 +501,8 @@ class EntryRestControllerTest extends WallabagApiTestCase
501 $content = json_decode($this->client->getResponse()->getContent(), true); 501 $content = json_decode($this->client->getResponse()->getContent(), true);
502 $this->assertGreaterThan(0, $content['id']); 502 $this->assertGreaterThan(0, $content['id']);
503 $this->assertSame('http://www.example.com/', $content['url']); 503 $this->assertSame('http://www.example.com/', $content['url']);
504 $this->assertSame('www.example.com', $content['domain_name']);
505 $this->assertSame('www.example.com', $content['title']);
504 } finally { 506 } finally {
505 // Remove the created entry to avoid side effects on other tests 507 // Remove the created entry to avoid side effects on other tests
506 if (isset($content['id'])) { 508 if (isset($content['id'])) {
diff --git a/tests/Wallabag/CoreBundle/Controller/EntryControllerTest.php b/tests/Wallabag/CoreBundle/Controller/EntryControllerTest.php
index 4ac4548b..a02f9699 100644
--- a/tests/Wallabag/CoreBundle/Controller/EntryControllerTest.php
+++ b/tests/Wallabag/CoreBundle/Controller/EntryControllerTest.php
@@ -6,6 +6,7 @@ use Tests\Wallabag\CoreBundle\WallabagCoreTestCase;
6use Wallabag\CoreBundle\Entity\Config; 6use Wallabag\CoreBundle\Entity\Config;
7use Wallabag\CoreBundle\Entity\Entry; 7use Wallabag\CoreBundle\Entity\Entry;
8use Wallabag\CoreBundle\Entity\SiteCredential; 8use Wallabag\CoreBundle\Entity\SiteCredential;
9use Wallabag\CoreBundle\Helper\ContentProxy;
9 10
10class EntryControllerTest extends WallabagCoreTestCase 11class EntryControllerTest extends WallabagCoreTestCase
11{ 12{
@@ -1429,4 +1430,52 @@ class EntryControllerTest extends WallabagCoreTestCase
1429 1430
1430 $client->getContainer()->get('craue_config')->set('restricted_access', 0); 1431 $client->getContainer()->get('craue_config')->set('restricted_access', 0);
1431 } 1432 }
1433
1434 public function testPostEntryWhenFetchFails()
1435 {
1436 $url = 'http://example.com/papers/email_tracking.pdf';
1437 $this->logInAs('admin');
1438 $client = $this->getClient();
1439
1440 $container = $client->getContainer();
1441 $contentProxy = $this->getMockBuilder(ContentProxy::class)
1442 ->disableOriginalConstructor()
1443 ->setMethods(['updateEntry'])
1444 ->getMock();
1445 $contentProxy->expects($this->any())
1446 ->method('updateEntry')
1447 ->willThrowException(new \Exception('Test Fetch content fails'));
1448
1449 $crawler = $client->request('GET', '/new');
1450
1451 $this->assertSame(200, $client->getResponse()->getStatusCode());
1452
1453 $form = $crawler->filter('form[name=entry]')->form();
1454
1455 $data = [
1456 'entry[url]' => $url,
1457 ];
1458
1459 /**
1460 * We generate a new client to be able to use Mock ContentProxy
1461 * Also we reinject the cookie from the previous client to keep the
1462 * session.
1463 */
1464 $cookie = $client->getCookieJar()->all();
1465 $client = $this->getNewClient();
1466 $client->getCookieJar()->set($cookie[0]);
1467 $client->getContainer()->set('wallabag_core.content_proxy', $contentProxy);
1468 $client->submit($form, $data);
1469
1470 $this->assertSame(302, $client->getResponse()->getStatusCode());
1471
1472 $content = $client->getContainer()
1473 ->get('doctrine.orm.entity_manager')
1474 ->getRepository('WallabagCoreBundle:Entry')
1475 ->findByUrlAndUserId($url, $this->getLoggedInUserId());
1476
1477 $authors = $content->getPublishedBy();
1478 $this->assertSame('email_tracking.pdf', $content->getTitle());
1479 $this->assertSame('example.com', $content->getDomainName());
1480 }
1432} 1481}
diff --git a/tests/Wallabag/CoreBundle/WallabagCoreTestCase.php b/tests/Wallabag/CoreBundle/WallabagCoreTestCase.php
index 1eda5199..6e1163c5 100644
--- a/tests/Wallabag/CoreBundle/WallabagCoreTestCase.php
+++ b/tests/Wallabag/CoreBundle/WallabagCoreTestCase.php
@@ -25,6 +25,11 @@ abstract class WallabagCoreTestCase extends WebTestCase
25 $this->client = static::createClient(); 25 $this->client = static::createClient();
26 } 26 }
27 27
28 public function getNewClient()
29 {
30 return $this->client = static::createClient();
31 }
32
28 public function getClient() 33 public function getClient()
29 { 34 {
30 return $this->client; 35 return $this->client;