From 48656e0eaac006a80f21e9aec8900747fe76283a Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Sun, 30 Oct 2016 11:27:09 +0100 Subject: Fixing tests --- .../Event/Subscriber/DownloadImagesSubscriber.php | 31 ++++-- src/Wallabag/CoreBundle/Helper/ContentProxy.php | 3 +- src/Wallabag/CoreBundle/Helper/DownloadImages.php | 21 ++-- .../Event/Listener/LocaleListenerTest.php | 86 ++++++++++++++++ .../Event/Listener/UserLocaleListenerTest.php | 58 +++++++++++ .../Event/Subscriber/TablePrefixSubscriberTest.php | 114 +++++++++++++++++++++ .../EventListener/LocaleListenerTest.php | 86 ---------------- .../EventListener/UserLocaleListenerTest.php | 58 ----------- .../CoreBundle/Helper/DownloadImagesTest.php | 19 ++++ .../Subscriber/TablePrefixSubscriberTest.php | 114 --------------------- 10 files changed, 315 insertions(+), 275 deletions(-) create mode 100644 tests/Wallabag/CoreBundle/Event/Listener/LocaleListenerTest.php create mode 100644 tests/Wallabag/CoreBundle/Event/Listener/UserLocaleListenerTest.php create mode 100644 tests/Wallabag/CoreBundle/Event/Subscriber/TablePrefixSubscriberTest.php delete mode 100644 tests/Wallabag/CoreBundle/EventListener/LocaleListenerTest.php delete mode 100644 tests/Wallabag/CoreBundle/EventListener/UserLocaleListenerTest.php delete mode 100644 tests/Wallabag/CoreBundle/Subscriber/TablePrefixSubscriberTest.php 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 return; } - $em = $args->getEntityManager(); + $config = new $this->configClass(); + $config->setEntityManager($args->getEntityManager()); // field content has been updated if ($args->hasChangedField('content')) { - $html = $this->downloadImages($em, $entity); + $html = $this->downloadImages($config, $entity); - if (null !== $html) { + if (false !== $html) { $args->setNewValue('content', $html); } } // field preview picture has been updated if ($args->hasChangedField('previewPicture')) { - $previewPicture = $this->downloadPreviewImage($em, $entity); + $previewPicture = $this->downloadPreviewImage($config, $entity); - if (null !== $previewPicture) { + if (false !== $previewPicture) { $entity->setPreviewPicture($previewPicture); } } @@ -88,17 +89,25 @@ class DownloadImagesSubscriber implements EventSubscriber // update all images inside the html $html = $this->downloadImages($config, $entity); - if (null !== $html) { + if (false !== $html) { $entity->setContent($html); } // update preview picture $previewPicture = $this->downloadPreviewImage($config, $entity); - if (null !== $previewPicture) { + if (false !== $previewPicture) { $entity->setPreviewPicture($previewPicture); } } + /** + * Download all images from the html. + * + * @param Config $config + * @param Entry $entry + * + * @return string|false False in case of async + */ public function downloadImages(Config $config, Entry $entry) { // if ($config->get('download_images_with_rabbitmq')) { @@ -113,6 +122,14 @@ class DownloadImagesSubscriber implements EventSubscriber ); } + /** + * Download the preview picture. + * + * @param Config $config + * @param Entry $entry + * + * @return string|false False in case of async + */ public function downloadPreviewImage(Config $config, Entry $entry) { // 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 $entry->setUrl($content['url'] ?: $url); $entry->setTitle($title); + $entry->setContent($html); $entry->setLanguage($content['language']); $entry->setMimetype($content['content_type']); @@ -75,8 +76,6 @@ class ContentProxy $entry->setDomainName($domainName); } - $entry->setContent($html); - if (isset($content['open_graph']['og_image'])) { $entry->setPreviewPicture($content['open_graph']['og_image']); } 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 // build image path $absolutePath = $this->getAbsoluteLink($url, $imagePath); if (false === $absolutePath) { - $this->logger->log('debug', 'Can not determine the absolute path for that image, skipping.'); + $this->logger->log('error', 'Can not determine the absolute path for that image, skipping.'); return false; } - $res = $this->client->get( - $absolutePath, - ['exceptions' => false] - ); + try { + $res = $this->client->get($absolutePath); + } catch (\Exception $e) { + $this->logger->log('error', 'Can not retrieve image, skipping.', ['exception' => $e]); + + return false; + } $ext = $this->mimeGuesser->guess($res->getHeader('content-type')); $this->logger->log('debug', 'Checking extension', ['ext' => $ext, 'header' => $res->getHeader('content-type')]); - if (!in_array($ext, ['jpeg', 'jpg', 'gif', 'png'])) { - $this->logger->log('debug', 'Processed image with not allowed extension. Skipping '.$imagePath); + if (!in_array($ext, ['jpeg', 'jpg', 'gif', 'png'], true)) { + $this->logger->log('error', 'Processed image with not allowed extension. Skipping '.$imagePath); return false; } @@ -117,7 +120,7 @@ class DownloadImages $im = false; } - if ($im === false) { + if (false === $im) { $this->logger->log('error', 'Error while regenerating image', ['path' => $localPath]); return false; @@ -193,6 +196,8 @@ class DownloadImages return $absolute->get_uri(); } + $this->logger->log('error', 'Can not make an absolute link', ['base' => $base, 'url' => $url]); + return false; } } diff --git a/tests/Wallabag/CoreBundle/Event/Listener/LocaleListenerTest.php b/tests/Wallabag/CoreBundle/Event/Listener/LocaleListenerTest.php new file mode 100644 index 00000000..84a54d3a --- /dev/null +++ b/tests/Wallabag/CoreBundle/Event/Listener/LocaleListenerTest.php @@ -0,0 +1,86 @@ +getMockBuilder('Symfony\Component\HttpKernel\HttpKernelInterface') + ->disableOriginalConstructor() + ->getMock(); + + return new GetResponseEvent($kernel, $request, HttpKernelInterface::MASTER_REQUEST); + } + + public function testWithoutSession() + { + $request = Request::create('/'); + + $listener = new LocaleListener('fr'); + $event = $this->getEvent($request); + + $listener->onKernelRequest($event); + $this->assertEquals('en', $request->getLocale()); + } + + public function testWithPreviousSession() + { + $request = Request::create('/'); + // generate a previous session + $request->cookies->set('MOCKSESSID', 'foo'); + $request->setSession(new Session(new MockArraySessionStorage())); + + $listener = new LocaleListener('fr'); + $event = $this->getEvent($request); + + $listener->onKernelRequest($event); + $this->assertEquals('fr', $request->getLocale()); + } + + public function testLocaleFromRequestAttribute() + { + $request = Request::create('/'); + // generate a previous session + $request->cookies->set('MOCKSESSID', 'foo'); + $request->setSession(new Session(new MockArraySessionStorage())); + $request->attributes->set('_locale', 'es'); + + $listener = new LocaleListener('fr'); + $event = $this->getEvent($request); + + $listener->onKernelRequest($event); + $this->assertEquals('en', $request->getLocale()); + $this->assertEquals('es', $request->getSession()->get('_locale')); + } + + public function testSubscribedEvents() + { + $request = Request::create('/'); + // generate a previous session + $request->cookies->set('MOCKSESSID', 'foo'); + $request->setSession(new Session(new MockArraySessionStorage())); + + $listener = new LocaleListener('fr'); + $event = $this->getEvent($request); + + $dispatcher = new EventDispatcher(); + $dispatcher->addSubscriber($listener); + + $dispatcher->dispatch( + KernelEvents::REQUEST, + $event + ); + + $this->assertEquals('fr', $request->getLocale()); + } +} diff --git a/tests/Wallabag/CoreBundle/Event/Listener/UserLocaleListenerTest.php b/tests/Wallabag/CoreBundle/Event/Listener/UserLocaleListenerTest.php new file mode 100644 index 00000000..45aecc63 --- /dev/null +++ b/tests/Wallabag/CoreBundle/Event/Listener/UserLocaleListenerTest.php @@ -0,0 +1,58 @@ +setEnabled(true); + + $config = new Config($user); + $config->setLanguage('fr'); + + $user->setConfig($config); + + $userToken = new UsernamePasswordToken($user, '', 'test'); + $request = Request::create('/'); + $event = new InteractiveLoginEvent($request, $userToken); + + $listener->onInteractiveLogin($event); + + $this->assertEquals('fr', $session->get('_locale')); + } + + public function testWithoutLanguage() + { + $session = new Session(new MockArraySessionStorage()); + $listener = new UserLocaleListener($session); + + $user = new User(); + $user->setEnabled(true); + + $config = new Config($user); + + $user->setConfig($config); + + $userToken = new UsernamePasswordToken($user, '', 'test'); + $request = Request::create('/'); + $event = new InteractiveLoginEvent($request, $userToken); + + $listener->onInteractiveLogin($event); + + $this->assertEquals('', $session->get('_locale')); + } +} diff --git a/tests/Wallabag/CoreBundle/Event/Subscriber/TablePrefixSubscriberTest.php b/tests/Wallabag/CoreBundle/Event/Subscriber/TablePrefixSubscriberTest.php new file mode 100644 index 00000000..b8cd0fad --- /dev/null +++ b/tests/Wallabag/CoreBundle/Event/Subscriber/TablePrefixSubscriberTest.php @@ -0,0 +1,114 @@ +getMockBuilder('Doctrine\ORM\EntityManager') + ->disableOriginalConstructor() + ->getMock(); + + $subscriber = new TablePrefixSubscriber($prefix); + + $metaClass = new ClassMetadata($entityName); + $metaClass->setPrimaryTable(['name' => $tableName]); + + $metaDataEvent = new LoadClassMetadataEventArgs($metaClass, $em); + + $this->assertEquals($tableNameExpected, $metaDataEvent->getClassMetadata()->getTableName()); + + $subscriber->loadClassMetadata($metaDataEvent); + + $this->assertEquals($finalTableName, $metaDataEvent->getClassMetadata()->getTableName()); + $this->assertEquals($finalTableNameQuoted, $metaDataEvent->getClassMetadata()->getQuotedTableName($platform)); + } + + /** + * @dataProvider dataForPrefix + */ + public function testSubscribedEvents($prefix, $entityName, $tableName, $tableNameExpected, $finalTableName, $finalTableNameQuoted, $platform) + { + $em = $this->getMockBuilder('Doctrine\ORM\EntityManager') + ->disableOriginalConstructor() + ->getMock(); + + $metaClass = new ClassMetadata($entityName); + $metaClass->setPrimaryTable(['name' => $tableName]); + + $metaDataEvent = new LoadClassMetadataEventArgs($metaClass, $em); + + $subscriber = new TablePrefixSubscriber($prefix); + + $evm = new EventManager(); + $evm->addEventSubscriber($subscriber); + + $evm->dispatchEvent('loadClassMetadata', $metaDataEvent); + + $this->assertEquals($finalTableName, $metaDataEvent->getClassMetadata()->getTableName()); + $this->assertEquals($finalTableNameQuoted, $metaDataEvent->getClassMetadata()->getQuotedTableName($platform)); + } + + public function testPrefixManyToMany() + { + $em = $this->getMockBuilder('Doctrine\ORM\EntityManager') + ->disableOriginalConstructor() + ->getMock(); + + $subscriber = new TablePrefixSubscriber('yo_'); + + $metaClass = new ClassMetadata('Wallabag\UserBundle\Entity\Entry'); + $metaClass->setPrimaryTable(['name' => 'entry']); + $metaClass->mapManyToMany([ + 'fieldName' => 'tags', + 'joinTable' => ['name' => null, 'schema' => null], + 'targetEntity' => 'Tag', + 'mappedBy' => null, + 'inversedBy' => 'entries', + 'cascade' => ['persist'], + 'indexBy' => null, + 'orphanRemoval' => false, + 'fetch' => 2, + ]); + + $metaDataEvent = new LoadClassMetadataEventArgs($metaClass, $em); + + $this->assertEquals('entry', $metaDataEvent->getClassMetadata()->getTableName()); + + $subscriber->loadClassMetadata($metaDataEvent); + + $this->assertEquals('yo_entry', $metaDataEvent->getClassMetadata()->getTableName()); + $this->assertEquals('yo_entry_tag', $metaDataEvent->getClassMetadata()->associationMappings['tags']['joinTable']['name']); + $this->assertEquals('yo_entry', $metaDataEvent->getClassMetadata()->getQuotedTableName(new \Doctrine\DBAL\Platforms\MySqlPlatform())); + } +} diff --git a/tests/Wallabag/CoreBundle/EventListener/LocaleListenerTest.php b/tests/Wallabag/CoreBundle/EventListener/LocaleListenerTest.php deleted file mode 100644 index 078bb69a..00000000 --- a/tests/Wallabag/CoreBundle/EventListener/LocaleListenerTest.php +++ /dev/null @@ -1,86 +0,0 @@ -getMockBuilder('Symfony\Component\HttpKernel\HttpKernelInterface') - ->disableOriginalConstructor() - ->getMock(); - - return new GetResponseEvent($kernel, $request, HttpKernelInterface::MASTER_REQUEST); - } - - public function testWithoutSession() - { - $request = Request::create('/'); - - $listener = new LocaleListener('fr'); - $event = $this->getEvent($request); - - $listener->onKernelRequest($event); - $this->assertEquals('en', $request->getLocale()); - } - - public function testWithPreviousSession() - { - $request = Request::create('/'); - // generate a previous session - $request->cookies->set('MOCKSESSID', 'foo'); - $request->setSession(new Session(new MockArraySessionStorage())); - - $listener = new LocaleListener('fr'); - $event = $this->getEvent($request); - - $listener->onKernelRequest($event); - $this->assertEquals('fr', $request->getLocale()); - } - - public function testLocaleFromRequestAttribute() - { - $request = Request::create('/'); - // generate a previous session - $request->cookies->set('MOCKSESSID', 'foo'); - $request->setSession(new Session(new MockArraySessionStorage())); - $request->attributes->set('_locale', 'es'); - - $listener = new LocaleListener('fr'); - $event = $this->getEvent($request); - - $listener->onKernelRequest($event); - $this->assertEquals('en', $request->getLocale()); - $this->assertEquals('es', $request->getSession()->get('_locale')); - } - - public function testSubscribedEvents() - { - $request = Request::create('/'); - // generate a previous session - $request->cookies->set('MOCKSESSID', 'foo'); - $request->setSession(new Session(new MockArraySessionStorage())); - - $listener = new LocaleListener('fr'); - $event = $this->getEvent($request); - - $dispatcher = new EventDispatcher(); - $dispatcher->addSubscriber($listener); - - $dispatcher->dispatch( - KernelEvents::REQUEST, - $event - ); - - $this->assertEquals('fr', $request->getLocale()); - } -} diff --git a/tests/Wallabag/CoreBundle/EventListener/UserLocaleListenerTest.php b/tests/Wallabag/CoreBundle/EventListener/UserLocaleListenerTest.php deleted file mode 100644 index e9ac7c1d..00000000 --- a/tests/Wallabag/CoreBundle/EventListener/UserLocaleListenerTest.php +++ /dev/null @@ -1,58 +0,0 @@ -setEnabled(true); - - $config = new Config($user); - $config->setLanguage('fr'); - - $user->setConfig($config); - - $userToken = new UsernamePasswordToken($user, '', 'test'); - $request = Request::create('/'); - $event = new InteractiveLoginEvent($request, $userToken); - - $listener->onInteractiveLogin($event); - - $this->assertEquals('fr', $session->get('_locale')); - } - - public function testWithoutLanguage() - { - $session = new Session(new MockArraySessionStorage()); - $listener = new UserLocaleListener($session); - - $user = new User(); - $user->setEnabled(true); - - $config = new Config($user); - - $user->setConfig($config); - - $userToken = new UsernamePasswordToken($user, '', 'test'); - $request = Request::create('/'); - $event = new InteractiveLoginEvent($request, $userToken); - - $listener->onInteractiveLogin($event); - - $this->assertEquals('', $session->get('_locale')); - } -} 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 $this->assertContains('/assets/images/4/2/4258f71e/ebe60399.'.$extension, $res); } + public function testProcessSingleImageWithBadUrl() + { + $client = new Client(); + + $mock = new Mock([ + new Response(404, []), + ]); + + $client->getEmitter()->attach($mock); + + $logHandler = new TestHandler(); + $logger = new Logger('test', array($logHandler)); + + $download = new DownloadImages($client, sys_get_temp_dir().'/wallabag_test', $logger); + $res = $download->processSingleImage('T9qgcHc.jpg', 'http://imgur.com/gallery/WxtWY'); + + $this->assertFalse($res, 'Image can not be found, so it will not be replaced'); + } + public function testProcessSingleImageWithBadImage() { $client = new Client(); diff --git a/tests/Wallabag/CoreBundle/Subscriber/TablePrefixSubscriberTest.php b/tests/Wallabag/CoreBundle/Subscriber/TablePrefixSubscriberTest.php deleted file mode 100644 index 4ae76703..00000000 --- a/tests/Wallabag/CoreBundle/Subscriber/TablePrefixSubscriberTest.php +++ /dev/null @@ -1,114 +0,0 @@ -getMockBuilder('Doctrine\ORM\EntityManager') - ->disableOriginalConstructor() - ->getMock(); - - $subscriber = new TablePrefixSubscriber($prefix); - - $metaClass = new ClassMetadata($entityName); - $metaClass->setPrimaryTable(['name' => $tableName]); - - $metaDataEvent = new LoadClassMetadataEventArgs($metaClass, $em); - - $this->assertEquals($tableNameExpected, $metaDataEvent->getClassMetadata()->getTableName()); - - $subscriber->loadClassMetadata($metaDataEvent); - - $this->assertEquals($finalTableName, $metaDataEvent->getClassMetadata()->getTableName()); - $this->assertEquals($finalTableNameQuoted, $metaDataEvent->getClassMetadata()->getQuotedTableName($platform)); - } - - /** - * @dataProvider dataForPrefix - */ - public function testSubscribedEvents($prefix, $entityName, $tableName, $tableNameExpected, $finalTableName, $finalTableNameQuoted, $platform) - { - $em = $this->getMockBuilder('Doctrine\ORM\EntityManager') - ->disableOriginalConstructor() - ->getMock(); - - $metaClass = new ClassMetadata($entityName); - $metaClass->setPrimaryTable(['name' => $tableName]); - - $metaDataEvent = new LoadClassMetadataEventArgs($metaClass, $em); - - $subscriber = new TablePrefixSubscriber($prefix); - - $evm = new EventManager(); - $evm->addEventSubscriber($subscriber); - - $evm->dispatchEvent('loadClassMetadata', $metaDataEvent); - - $this->assertEquals($finalTableName, $metaDataEvent->getClassMetadata()->getTableName()); - $this->assertEquals($finalTableNameQuoted, $metaDataEvent->getClassMetadata()->getQuotedTableName($platform)); - } - - public function testPrefixManyToMany() - { - $em = $this->getMockBuilder('Doctrine\ORM\EntityManager') - ->disableOriginalConstructor() - ->getMock(); - - $subscriber = new TablePrefixSubscriber('yo_'); - - $metaClass = new ClassMetadata('Wallabag\UserBundle\Entity\Entry'); - $metaClass->setPrimaryTable(['name' => 'entry']); - $metaClass->mapManyToMany([ - 'fieldName' => 'tags', - 'joinTable' => ['name' => null, 'schema' => null], - 'targetEntity' => 'Tag', - 'mappedBy' => null, - 'inversedBy' => 'entries', - 'cascade' => ['persist'], - 'indexBy' => null, - 'orphanRemoval' => false, - 'fetch' => 2, - ]); - - $metaDataEvent = new LoadClassMetadataEventArgs($metaClass, $em); - - $this->assertEquals('entry', $metaDataEvent->getClassMetadata()->getTableName()); - - $subscriber->loadClassMetadata($metaDataEvent); - - $this->assertEquals('yo_entry', $metaDataEvent->getClassMetadata()->getTableName()); - $this->assertEquals('yo_entry_tag', $metaDataEvent->getClassMetadata()->associationMappings['tags']['joinTable']['name']); - $this->assertEquals('yo_entry', $metaDataEvent->getClassMetadata()->getQuotedTableName(new \Doctrine\DBAL\Platforms\MySqlPlatform())); - } -} -- cgit v1.2.3