aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorJeremy Benoist <jeremy.benoist@gmail.com>2016-10-30 11:27:09 +0100
committerJeremy Benoist <jeremy.benoist@gmail.com>2016-10-30 11:27:09 +0100
commit48656e0eaac006a80f21e9aec8900747fe76283a (patch)
tree8b685bd1b2b68b47a12b2bc17f076683c466603f
parent7f55941856549a3f5f45c42fdc171d66ff7ee297 (diff)
downloadwallabag-48656e0eaac006a80f21e9aec8900747fe76283a.tar.gz
wallabag-48656e0eaac006a80f21e9aec8900747fe76283a.tar.zst
wallabag-48656e0eaac006a80f21e9aec8900747fe76283a.zip
Fixing tests
-rw-r--r--src/Wallabag/CoreBundle/Event/Subscriber/DownloadImagesSubscriber.php31
-rw-r--r--src/Wallabag/CoreBundle/Helper/ContentProxy.php3
-rw-r--r--src/Wallabag/CoreBundle/Helper/DownloadImages.php21
-rw-r--r--tests/Wallabag/CoreBundle/Event/Listener/LocaleListenerTest.php (renamed from tests/Wallabag/CoreBundle/EventListener/LocaleListenerTest.php)4
-rw-r--r--tests/Wallabag/CoreBundle/Event/Listener/UserLocaleListenerTest.php (renamed from tests/Wallabag/CoreBundle/EventListener/UserLocaleListenerTest.php)4
-rw-r--r--tests/Wallabag/CoreBundle/Event/Subscriber/TablePrefixSubscriberTest.php (renamed from tests/Wallabag/CoreBundle/Subscriber/TablePrefixSubscriberTest.php)4
-rw-r--r--tests/Wallabag/CoreBundle/Helper/DownloadImagesTest.php19
7 files changed, 63 insertions, 23 deletions
diff --git a/src/Wallabag/CoreBundle/Event/Subscriber/DownloadImagesSubscriber.php b/src/Wallabag/CoreBundle/Event/Subscriber/DownloadImagesSubscriber.php
index 654edf31..09f8e911 100644
--- a/src/Wallabag/CoreBundle/Event/Subscriber/DownloadImagesSubscriber.php
+++ b/src/Wallabag/CoreBundle/Event/Subscriber/DownloadImagesSubscriber.php
@@ -49,22 +49,23 @@ class DownloadImagesSubscriber implements EventSubscriber
49 return; 49 return;
50 } 50 }
51 51
52 $em = $args->getEntityManager(); 52 $config = new $this->configClass();
53 $config->setEntityManager($args->getEntityManager());
53 54
54 // field content has been updated 55 // field content has been updated
55 if ($args->hasChangedField('content')) { 56 if ($args->hasChangedField('content')) {
56 $html = $this->downloadImages($em, $entity); 57 $html = $this->downloadImages($config, $entity);
57 58
58 if (null !== $html) { 59 if (false !== $html) {
59 $args->setNewValue('content', $html); 60 $args->setNewValue('content', $html);
60 } 61 }
61 } 62 }
62 63
63 // field preview picture has been updated 64 // field preview picture has been updated
64 if ($args->hasChangedField('previewPicture')) { 65 if ($args->hasChangedField('previewPicture')) {
65 $previewPicture = $this->downloadPreviewImage($em, $entity); 66 $previewPicture = $this->downloadPreviewImage($config, $entity);
66 67
67 if (null !== $previewPicture) { 68 if (false !== $previewPicture) {
68 $entity->setPreviewPicture($previewPicture); 69 $entity->setPreviewPicture($previewPicture);
69 } 70 }
70 } 71 }
@@ -88,17 +89,25 @@ class DownloadImagesSubscriber implements EventSubscriber
88 89
89 // update all images inside the html 90 // update all images inside the html
90 $html = $this->downloadImages($config, $entity); 91 $html = $this->downloadImages($config, $entity);
91 if (null !== $html) { 92 if (false !== $html) {
92 $entity->setContent($html); 93 $entity->setContent($html);
93 } 94 }
94 95
95 // update preview picture 96 // update preview picture
96 $previewPicture = $this->downloadPreviewImage($config, $entity); 97 $previewPicture = $this->downloadPreviewImage($config, $entity);
97 if (null !== $previewPicture) { 98 if (false !== $previewPicture) {
98 $entity->setPreviewPicture($previewPicture); 99 $entity->setPreviewPicture($previewPicture);
99 } 100 }
100 } 101 }
101 102
103 /**
104 * Download all images from the html.
105 *
106 * @param Config $config
107 * @param Entry $entry
108 *
109 * @return string|false False in case of async
110 */
102 public function downloadImages(Config $config, Entry $entry) 111 public function downloadImages(Config $config, Entry $entry)
103 { 112 {
104 // if ($config->get('download_images_with_rabbitmq')) { 113 // if ($config->get('download_images_with_rabbitmq')) {
@@ -113,6 +122,14 @@ class DownloadImagesSubscriber implements EventSubscriber
113 ); 122 );
114 } 123 }
115 124
125 /**
126 * Download the preview picture.
127 *
128 * @param Config $config
129 * @param Entry $entry
130 *
131 * @return string|false False in case of async
132 */
116 public function downloadPreviewImage(Config $config, Entry $entry) 133 public function downloadPreviewImage(Config $config, Entry $entry)
117 { 134 {
118 // if ($config->get('download_images_with_rabbitmq')) { 135 // if ($config->get('download_images_with_rabbitmq')) {
diff --git a/src/Wallabag/CoreBundle/Helper/ContentProxy.php b/src/Wallabag/CoreBundle/Helper/ContentProxy.php
index d90d3dc8..1986ab33 100644
--- a/src/Wallabag/CoreBundle/Helper/ContentProxy.php
+++ b/src/Wallabag/CoreBundle/Helper/ContentProxy.php
@@ -65,6 +65,7 @@ class ContentProxy
65 65
66 $entry->setUrl($content['url'] ?: $url); 66 $entry->setUrl($content['url'] ?: $url);
67 $entry->setTitle($title); 67 $entry->setTitle($title);
68 $entry->setContent($html);
68 69
69 $entry->setLanguage($content['language']); 70 $entry->setLanguage($content['language']);
70 $entry->setMimetype($content['content_type']); 71 $entry->setMimetype($content['content_type']);
@@ -75,8 +76,6 @@ class ContentProxy
75 $entry->setDomainName($domainName); 76 $entry->setDomainName($domainName);
76 } 77 }
77 78
78 $entry->setContent($html);
79
80 if (isset($content['open_graph']['og_image'])) { 79 if (isset($content['open_graph']['og_image'])) {
81 $entry->setPreviewPicture($content['open_graph']['og_image']); 80 $entry->setPreviewPicture($content['open_graph']['og_image']);
82 } 81 }
diff --git a/src/Wallabag/CoreBundle/Helper/DownloadImages.php b/src/Wallabag/CoreBundle/Helper/DownloadImages.php
index 426cbe48..004bb277 100644
--- a/src/Wallabag/CoreBundle/Helper/DownloadImages.php
+++ b/src/Wallabag/CoreBundle/Helper/DownloadImages.php
@@ -91,20 +91,23 @@ class DownloadImages
91 // build image path 91 // build image path
92 $absolutePath = $this->getAbsoluteLink($url, $imagePath); 92 $absolutePath = $this->getAbsoluteLink($url, $imagePath);
93 if (false === $absolutePath) { 93 if (false === $absolutePath) {
94 $this->logger->log('debug', 'Can not determine the absolute path for that image, skipping.'); 94 $this->logger->log('error', 'Can not determine the absolute path for that image, skipping.');
95 95
96 return false; 96 return false;
97 } 97 }
98 98
99 $res = $this->client->get( 99 try {
100 $absolutePath, 100 $res = $this->client->get($absolutePath);
101 ['exceptions' => false] 101 } catch (\Exception $e) {
102 ); 102 $this->logger->log('error', 'Can not retrieve image, skipping.', ['exception' => $e]);
103
104 return false;
105 }
103 106
104 $ext = $this->mimeGuesser->guess($res->getHeader('content-type')); 107 $ext = $this->mimeGuesser->guess($res->getHeader('content-type'));
105 $this->logger->log('debug', 'Checking extension', ['ext' => $ext, 'header' => $res->getHeader('content-type')]); 108 $this->logger->log('debug', 'Checking extension', ['ext' => $ext, 'header' => $res->getHeader('content-type')]);
106 if (!in_array($ext, ['jpeg', 'jpg', 'gif', 'png'])) { 109 if (!in_array($ext, ['jpeg', 'jpg', 'gif', 'png'], true)) {
107 $this->logger->log('debug', 'Processed image with not allowed extension. Skipping '.$imagePath); 110 $this->logger->log('error', 'Processed image with not allowed extension. Skipping '.$imagePath);
108 111
109 return false; 112 return false;
110 } 113 }
@@ -117,7 +120,7 @@ class DownloadImages
117 $im = false; 120 $im = false;
118 } 121 }
119 122
120 if ($im === false) { 123 if (false === $im) {
121 $this->logger->log('error', 'Error while regenerating image', ['path' => $localPath]); 124 $this->logger->log('error', 'Error while regenerating image', ['path' => $localPath]);
122 125
123 return false; 126 return false;
@@ -193,6 +196,8 @@ class DownloadImages
193 return $absolute->get_uri(); 196 return $absolute->get_uri();
194 } 197 }
195 198
199 $this->logger->log('error', 'Can not make an absolute link', ['base' => $base, 'url' => $url]);
200
196 return false; 201 return false;
197 } 202 }
198} 203}
diff --git a/tests/Wallabag/CoreBundle/EventListener/LocaleListenerTest.php b/tests/Wallabag/CoreBundle/Event/Listener/LocaleListenerTest.php
index 078bb69a..84a54d3a 100644
--- a/tests/Wallabag/CoreBundle/EventListener/LocaleListenerTest.php
+++ b/tests/Wallabag/CoreBundle/Event/Listener/LocaleListenerTest.php
@@ -1,6 +1,6 @@
1<?php 1<?php
2 2
3namespace Tests\Wallabag\CoreBundle\EventListener; 3namespace Tests\Wallabag\CoreBundle\Event\Listener;
4 4
5use Symfony\Component\EventDispatcher\EventDispatcher; 5use Symfony\Component\EventDispatcher\EventDispatcher;
6use Symfony\Component\HttpFoundation\Request; 6use Symfony\Component\HttpFoundation\Request;
@@ -9,7 +9,7 @@ use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
9use Symfony\Component\HttpKernel\Event\GetResponseEvent; 9use Symfony\Component\HttpKernel\Event\GetResponseEvent;
10use Symfony\Component\HttpKernel\HttpKernelInterface; 10use Symfony\Component\HttpKernel\HttpKernelInterface;
11use Symfony\Component\HttpKernel\KernelEvents; 11use Symfony\Component\HttpKernel\KernelEvents;
12use Wallabag\CoreBundle\EventListener\LocaleListener; 12use Wallabag\CoreBundle\Event\Listener\LocaleListener;
13 13
14class LocaleListenerTest extends \PHPUnit_Framework_TestCase 14class LocaleListenerTest extends \PHPUnit_Framework_TestCase
15{ 15{
diff --git a/tests/Wallabag/CoreBundle/EventListener/UserLocaleListenerTest.php b/tests/Wallabag/CoreBundle/Event/Listener/UserLocaleListenerTest.php
index e9ac7c1d..45aecc63 100644
--- a/tests/Wallabag/CoreBundle/EventListener/UserLocaleListenerTest.php
+++ b/tests/Wallabag/CoreBundle/Event/Listener/UserLocaleListenerTest.php
@@ -1,6 +1,6 @@
1<?php 1<?php
2 2
3namespace Tests\Wallabag\CoreBundle\EventListener; 3namespace Tests\Wallabag\CoreBundle\Event\Listener;
4 4
5use Symfony\Component\HttpFoundation\Request; 5use Symfony\Component\HttpFoundation\Request;
6use Symfony\Component\HttpFoundation\Session\Session; 6use Symfony\Component\HttpFoundation\Session\Session;
@@ -8,7 +8,7 @@ use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
8use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken; 8use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
9use Symfony\Component\Security\Http\Event\InteractiveLoginEvent; 9use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
10use Wallabag\CoreBundle\Entity\Config; 10use Wallabag\CoreBundle\Entity\Config;
11use Wallabag\CoreBundle\EventListener\UserLocaleListener; 11use Wallabag\CoreBundle\Event\Listener\UserLocaleListener;
12use Wallabag\UserBundle\Entity\User; 12use Wallabag\UserBundle\Entity\User;
13 13
14class UserLocaleListenerTest extends \PHPUnit_Framework_TestCase 14class UserLocaleListenerTest extends \PHPUnit_Framework_TestCase
diff --git a/tests/Wallabag/CoreBundle/Subscriber/TablePrefixSubscriberTest.php b/tests/Wallabag/CoreBundle/Event/Subscriber/TablePrefixSubscriberTest.php
index 4ae76703..b8cd0fad 100644
--- a/tests/Wallabag/CoreBundle/Subscriber/TablePrefixSubscriberTest.php
+++ b/tests/Wallabag/CoreBundle/Event/Subscriber/TablePrefixSubscriberTest.php
@@ -1,11 +1,11 @@
1<?php 1<?php
2 2
3namespace Tests\Wallabag\CoreBundle\Subscriber; 3namespace Tests\Wallabag\CoreBundle\Event\Subscriber;
4 4
5use Doctrine\Common\EventManager; 5use Doctrine\Common\EventManager;
6use Doctrine\ORM\Event\LoadClassMetadataEventArgs; 6use Doctrine\ORM\Event\LoadClassMetadataEventArgs;
7use Doctrine\ORM\Mapping\ClassMetadata; 7use Doctrine\ORM\Mapping\ClassMetadata;
8use Wallabag\CoreBundle\Subscriber\TablePrefixSubscriber; 8use Wallabag\CoreBundle\Event\Subscriber\TablePrefixSubscriber;
9 9
10class TablePrefixSubscriberTest extends \PHPUnit_Framework_TestCase 10class TablePrefixSubscriberTest extends \PHPUnit_Framework_TestCase
11{ 11{
diff --git a/tests/Wallabag/CoreBundle/Helper/DownloadImagesTest.php b/tests/Wallabag/CoreBundle/Helper/DownloadImagesTest.php
index 0273693e..e000d681 100644
--- a/tests/Wallabag/CoreBundle/Helper/DownloadImagesTest.php
+++ b/tests/Wallabag/CoreBundle/Helper/DownloadImagesTest.php
@@ -83,6 +83,25 @@ class DownloadImagesTest extends \PHPUnit_Framework_TestCase
83 $this->assertContains('/assets/images/4/2/4258f71e/ebe60399.'.$extension, $res); 83 $this->assertContains('/assets/images/4/2/4258f71e/ebe60399.'.$extension, $res);
84 } 84 }
85 85
86 public function testProcessSingleImageWithBadUrl()
87 {
88 $client = new Client();
89
90 $mock = new Mock([
91 new Response(404, []),
92 ]);
93
94 $client->getEmitter()->attach($mock);
95
96 $logHandler = new TestHandler();
97 $logger = new Logger('test', array($logHandler));
98
99 $download = new DownloadImages($client, sys_get_temp_dir().'/wallabag_test', $logger);
100 $res = $download->processSingleImage('T9qgcHc.jpg', 'http://imgur.com/gallery/WxtWY');
101
102 $this->assertFalse($res, 'Image can not be found, so it will not be replaced');
103 }
104
86 public function testProcessSingleImageWithBadImage() 105 public function testProcessSingleImageWithBadImage()
87 { 106 {
88 $client = new Client(); 107 $client = new Client();