From da3d4998c0972557952c83b610f8f45fdcd31b72 Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Mon, 28 Sep 2015 19:35:33 +0200 Subject: Move readingTime & domainName in ContentProxy So, everything is centralized in one place when we save a new entry. --- src/Wallabag/CoreBundle/DataFixtures/ORM/LoadEntryData.php | 12 ++++++++++++ src/Wallabag/CoreBundle/Entity/Entry.php | 3 --- src/Wallabag/CoreBundle/Helper/ContentProxy.php | 3 +++ src/Wallabag/CoreBundle/Tests/Helper/ContentProxyTest.php | 14 ++++++++++---- 4 files changed, 25 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/Wallabag/CoreBundle/DataFixtures/ORM/LoadEntryData.php b/src/Wallabag/CoreBundle/DataFixtures/ORM/LoadEntryData.php index dd316194..b4b685f9 100644 --- a/src/Wallabag/CoreBundle/DataFixtures/ORM/LoadEntryData.php +++ b/src/Wallabag/CoreBundle/DataFixtures/ORM/LoadEntryData.php @@ -17,6 +17,8 @@ class LoadEntryData extends AbstractFixture implements OrderedFixtureInterface { $entry1 = new Entry($this->getReference('admin-user')); $entry1->setUrl('http://0.0.0.0'); + $entry1->setReadingTime(11); + $entry1->setDomainName('domain.io'); $entry1->setTitle('test title entry1'); $entry1->setContent('This is my content /o/'); $entry1->setLanguage('en'); @@ -27,6 +29,8 @@ class LoadEntryData extends AbstractFixture implements OrderedFixtureInterface $entry2 = new Entry($this->getReference('admin-user')); $entry2->setUrl('http://0.0.0.0'); + $entry2->setReadingTime(1); + $entry2->setDomainName('domain.io'); $entry2->setTitle('test title entry2'); $entry2->setContent('This is my content /o/'); $entry2->setLanguage('fr'); @@ -37,6 +41,8 @@ class LoadEntryData extends AbstractFixture implements OrderedFixtureInterface $entry3 = new Entry($this->getReference('bob-user')); $entry3->setUrl('http://0.0.0.0'); + $entry3->setReadingTime(1); + $entry3->setDomainName('domain.io'); $entry3->setTitle('test title entry3'); $entry3->setContent('This is my content /o/'); $entry3->setLanguage('en'); @@ -55,6 +61,8 @@ class LoadEntryData extends AbstractFixture implements OrderedFixtureInterface $entry4 = new Entry($this->getReference('admin-user')); $entry4->setUrl('http://0.0.0.0'); + $entry4->setReadingTime(12); + $entry4->setDomainName('domain.io'); $entry4->setTitle('test title entry4'); $entry4->setContent('This is my content /o/'); $entry4->setLanguage('en'); @@ -73,6 +81,8 @@ class LoadEntryData extends AbstractFixture implements OrderedFixtureInterface $entry5 = new Entry($this->getReference('admin-user')); $entry5->setUrl('http://0.0.0.0'); + $entry5->setReadingTime(12); + $entry5->setDomainName('domain.io'); $entry5->setTitle('test title entry5'); $entry5->setContent('This is my content /o/'); $entry5->setStarred(true); @@ -84,6 +94,8 @@ class LoadEntryData extends AbstractFixture implements OrderedFixtureInterface $entry6 = new Entry($this->getReference('admin-user')); $entry6->setUrl('http://0.0.0.0'); + $entry6->setReadingTime(12); + $entry6->setDomainName('domain.io'); $entry6->setTitle('test title entry6'); $entry6->setContent('This is my content /o/'); $entry6->setArchived(true); diff --git a/src/Wallabag/CoreBundle/Entity/Entry.php b/src/Wallabag/CoreBundle/Entity/Entry.php index 7108889e..9e81ba12 100644 --- a/src/Wallabag/CoreBundle/Entity/Entry.php +++ b/src/Wallabag/CoreBundle/Entity/Entry.php @@ -7,7 +7,6 @@ use Doctrine\ORM\Mapping as ORM; use Symfony\Component\Validator\Constraints as Assert; use Hateoas\Configuration\Annotation as Hateoas; use JMS\Serializer\Annotation\XmlRoot; -use Wallabag\CoreBundle\Tools\Utils; /** * Entry. @@ -279,8 +278,6 @@ class Entry public function setContent($content) { $this->content = $content; - $this->readingTime = Utils::getReadingTime($content); - $this->domainName = parse_url($this->url, PHP_URL_HOST); return $this; } diff --git a/src/Wallabag/CoreBundle/Helper/ContentProxy.php b/src/Wallabag/CoreBundle/Helper/ContentProxy.php index 3de8828f..7fb41393 100644 --- a/src/Wallabag/CoreBundle/Helper/ContentProxy.php +++ b/src/Wallabag/CoreBundle/Helper/ContentProxy.php @@ -4,6 +4,7 @@ namespace Wallabag\CoreBundle\Helper; use Graby\Graby; use Wallabag\CoreBundle\Entity\Entry; +use Wallabag\CoreBundle\Tools\Utils; /** * This kind of proxy class take care of getting the content from an url @@ -51,6 +52,8 @@ class ContentProxy $entry->setContent($html); $entry->setLanguage($content['language']); $entry->setMimetype($content['content_type']); + $entry->setReadingTime(Utils::getReadingTime($html)); + $entry->setDomainName(parse_url($entry->getUrl(), PHP_URL_HOST)); if (isset($content['open_graph']['og_image'])) { $entry->setPreviewPicture($content['open_graph']['og_image']); diff --git a/src/Wallabag/CoreBundle/Tests/Helper/ContentProxyTest.php b/src/Wallabag/CoreBundle/Tests/Helper/ContentProxyTest.php index 30065d6b..0d338389 100644 --- a/src/Wallabag/CoreBundle/Tests/Helper/ContentProxyTest.php +++ b/src/Wallabag/CoreBundle/Tests/Helper/ContentProxyTest.php @@ -35,6 +35,8 @@ class ContentProxyTest extends KernelTestCase $this->assertEmpty($entry->getPreviewPicture()); $this->assertEmpty($entry->getMimetype()); $this->assertEmpty($entry->getLanguage()); + $this->assertEquals(0.0, $entry->getReadingTime()); + $this->assertEquals('0.0.0.0', $entry->getDomainName()); } public function testWithEmptyContentButOG() @@ -59,14 +61,16 @@ class ContentProxyTest extends KernelTestCase )); $proxy = new ContentProxy($graby); - $entry = $proxy->updateEntry(new Entry(new User()), 'http://0.0.0.0'); + $entry = $proxy->updateEntry(new Entry(new User()), 'http://domain.io'); - $this->assertEquals('http://0.0.0.0', $entry->getUrl()); + $this->assertEquals('http://domain.io', $entry->getUrl()); $this->assertEquals('my title', $entry->getTitle()); $this->assertEquals('

Unable to retrieve readable content.

But we found a short description:

desc', $entry->getContent()); $this->assertEmpty($entry->getPreviewPicture()); $this->assertEmpty($entry->getLanguage()); $this->assertEmpty($entry->getMimetype()); + $this->assertEquals(0.0, $entry->getReadingTime()); + $this->assertEquals('domain.io', $entry->getDomainName()); } public function testWithContent() @@ -79,7 +83,7 @@ class ContentProxyTest extends KernelTestCase $graby->expects($this->any()) ->method('fetchContent') ->willReturn(array( - 'html' => 'this is my content', + 'html' => str_repeat('this is my content', 325), 'title' => 'this is my title', 'url' => 'http://1.1.1.1', 'content_type' => 'text/html', @@ -96,9 +100,11 @@ class ContentProxyTest extends KernelTestCase $this->assertEquals('http://1.1.1.1', $entry->getUrl()); $this->assertEquals('this is my title', $entry->getTitle()); - $this->assertEquals('this is my content', $entry->getContent()); + $this->assertContains('this is my content', $entry->getContent()); $this->assertEquals('http://3.3.3.3/cover.jpg', $entry->getPreviewPicture()); $this->assertEquals('text/html', $entry->getMimetype()); $this->assertEquals('fr', $entry->getLanguage()); + $this->assertEquals(4.0, $entry->getReadingTime()); + $this->assertEquals('1.1.1.1', $entry->getDomainName()); } } -- cgit v1.2.3 From 159986c4fbf63dd93136ea5c52ff0be705aefaf3 Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Mon, 28 Sep 2015 19:35:55 +0200 Subject: Fix Postgres tests --- .../CoreBundle/Repository/EntryRepository.php | 36 ++++++++++++++++++++++ .../Tests/Controller/EntryControllerTest.php | 32 ++++++++----------- .../Tests/Controller/TagControllerTest.php | 6 ++-- 3 files changed, 52 insertions(+), 22 deletions(-) (limited to 'src') diff --git a/src/Wallabag/CoreBundle/Repository/EntryRepository.php b/src/Wallabag/CoreBundle/Repository/EntryRepository.php index 87b9befe..0e82b9b2 100644 --- a/src/Wallabag/CoreBundle/Repository/EntryRepository.php +++ b/src/Wallabag/CoreBundle/Repository/EntryRepository.php @@ -161,4 +161,40 @@ class EntryRepository extends EntityRepository return $languages; } + + /** + * Used only in test case to get the right entry associated to the right user + * + * @param string $username + * + * @return Entry + */ + public function findOneByUsernameAndNotStarred($username) + { + return $this->createQueryBuilder('e') + ->leftJoin('e.user', 'u') + ->where('u.username = :username')->setParameter('username', $username) + ->andWhere('e.isStarred = false') + ->setMaxResults(1) + ->getQuery() + ->getSingleResult(); + } + + /** + * Used only in test case to get the right entry associated to the right user + * + * @param string $username + * + * @return Entry + */ + public function findOneByUsernameAndNotArchived($username) + { + return $this->createQueryBuilder('e') + ->leftJoin('e.user', 'u') + ->where('u.username = :username')->setParameter('username', $username) + ->andWhere('e.isArchived = false') + ->setMaxResults(1) + ->getQuery() + ->getSingleResult(); + } } diff --git a/src/Wallabag/CoreBundle/Tests/Controller/EntryControllerTest.php b/src/Wallabag/CoreBundle/Tests/Controller/EntryControllerTest.php index cbd84a97..456c747a 100644 --- a/src/Wallabag/CoreBundle/Tests/Controller/EntryControllerTest.php +++ b/src/Wallabag/CoreBundle/Tests/Controller/EntryControllerTest.php @@ -101,7 +101,7 @@ class EntryControllerTest extends WallabagCoreTestCase $content = $client->getContainer() ->get('doctrine.orm.entity_manager') ->getRepository('WallabagCoreBundle:Entry') - ->findOneByIsArchived(false); + ->findOneByUsernameAndNotArchived('admin'); $client->request('GET', '/view/'.$content->getId()); @@ -117,7 +117,7 @@ class EntryControllerTest extends WallabagCoreTestCase $content = $client->getContainer() ->get('doctrine.orm.entity_manager') ->getRepository('WallabagCoreBundle:Entry') - ->findOneByIsArchived(false); + ->findOneByUsernameAndNotArchived('admin'); $crawler = $client->request('GET', '/edit/'.$content->getId()); @@ -135,7 +135,7 @@ class EntryControllerTest extends WallabagCoreTestCase $content = $client->getContainer() ->get('doctrine.orm.entity_manager') ->getRepository('WallabagCoreBundle:Entry') - ->findOneByIsArchived(false); + ->findOneByUsernameAndNotArchived('admin'); $crawler = $client->request('GET', '/edit/'.$content->getId()); @@ -165,7 +165,7 @@ class EntryControllerTest extends WallabagCoreTestCase $content = $client->getContainer() ->get('doctrine.orm.entity_manager') ->getRepository('WallabagCoreBundle:Entry') - ->findOneByIsArchived(false); + ->findOneByUsernameAndNotArchived('admin'); $client->request('GET', '/archive/'.$content->getId()); @@ -174,7 +174,7 @@ class EntryControllerTest extends WallabagCoreTestCase $res = $client->getContainer() ->get('doctrine.orm.entity_manager') ->getRepository('WallabagCoreBundle:Entry') - ->findOneById($content->getId()); + ->find($content->getId()); $this->assertEquals($res->isArchived(), true); } @@ -187,7 +187,7 @@ class EntryControllerTest extends WallabagCoreTestCase $content = $client->getContainer() ->get('doctrine.orm.entity_manager') ->getRepository('WallabagCoreBundle:Entry') - ->findOneByIsStarred(false); + ->findOneByUsernameAndNotStarred('admin'); $client->request('GET', '/star/'.$content->getId()); @@ -209,7 +209,7 @@ class EntryControllerTest extends WallabagCoreTestCase $content = $client->getContainer() ->get('doctrine.orm.entity_manager') ->getRepository('WallabagCoreBundle:Entry') - ->findOneByIsStarred(false); + ->findOneByUsernameAndNotStarred('admin'); $client->request('GET', '/delete/'.$content->getId()); @@ -222,21 +222,15 @@ class EntryControllerTest extends WallabagCoreTestCase public function testViewOtherUserEntry() { - $this->logInAs('bob'); + $this->logInAs('admin'); $client = $this->getClient(); $content = $client->getContainer() ->get('doctrine.orm.entity_manager') ->getRepository('WallabagCoreBundle:Entry') - ->createQueryBuilder('e') - ->select('e.id') - ->leftJoin('e.user', 'u') - ->where('u.username != :username')->setParameter('username', 'bob') - ->setMaxResults(1) - ->getQuery() - ->getSingleResult(AbstractQuery::HYDRATE_ARRAY); + ->findOneByUsernameAndNotArchived('bob'); - $client->request('GET', '/view/'.$content['id']); + $client->request('GET', '/view/'.$content->getId()); $this->assertEquals(403, $client->getResponse()->getStatusCode()); } @@ -360,7 +354,7 @@ class EntryControllerTest extends WallabagCoreTestCase $form['entry_filter[isStarred]']->untick(); $crawler = $client->submit($form); - $this->assertCount(2, $crawler->filter('div[class=entry]')); + $this->assertCount(1, $crawler->filter('div[class=entry]')); $form = $crawler->filter('button[id=submit-filter]')->form(); $form['entry_filter[isArchived]']->untick(); @@ -391,11 +385,11 @@ class EntryControllerTest extends WallabagCoreTestCase $crawler = $client->request('GET', '/unread/list'); $form = $crawler->filter('button[id=submit-filter]')->form(); $data = array( - 'entry_filter[language]' => 'de', + 'entry_filter[language]' => 'fr', ); $crawler = $client->submit($form, $data); - $this->assertCount(1, $crawler->filter('div[class=entry]')); + $this->assertCount(3, $crawler->filter('div[class=entry]')); $form = $crawler->filter('button[id=submit-filter]')->form(); $data = array( diff --git a/src/Wallabag/CoreBundle/Tests/Controller/TagControllerTest.php b/src/Wallabag/CoreBundle/Tests/Controller/TagControllerTest.php index d25b2db5..dc93dd6b 100644 --- a/src/Wallabag/CoreBundle/Tests/Controller/TagControllerTest.php +++ b/src/Wallabag/CoreBundle/Tests/Controller/TagControllerTest.php @@ -24,7 +24,7 @@ class TagControllerTest extends WallabagCoreTestCase $entry = $client->getContainer() ->get('doctrine.orm.entity_manager') ->getRepository('WallabagCoreBundle:Entry') - ->findOneBy(array()); + ->findOneByUsernameAndNotArchived('admin'); $crawler = $client->request('GET', '/view/'.$entry->getId()); @@ -46,7 +46,7 @@ class TagControllerTest extends WallabagCoreTestCase $newEntry = $client->getContainer() ->get('doctrine.orm.entity_manager') ->getRepository('WallabagCoreBundle:Entry') - ->findOneById($entry->getId()); + ->find($entry->getId()); $this->assertEquals(1, count($newEntry->getTags())); @@ -61,7 +61,7 @@ class TagControllerTest extends WallabagCoreTestCase $newEntry = $client->getContainer() ->get('doctrine.orm.entity_manager') ->getRepository('WallabagCoreBundle:Entry') - ->findOneById($entry->getId()); + ->find($entry->getId()); $this->assertEquals(2, count($newEntry->getTags())); } -- cgit v1.2.3 From 02d17813a11d27e0232c38d1adf037cefdb176c1 Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Mon, 28 Sep 2015 20:26:37 +0200 Subject: Fix tests for all --- .../CoreBundle/DataFixtures/ORM/LoadEntryData.php | 1 + .../CoreBundle/Repository/EntryRepository.php | 18 ---------------- .../Tests/Controller/EntryControllerTest.php | 24 ++++++++++++---------- 3 files changed, 14 insertions(+), 29 deletions(-) (limited to 'src') diff --git a/src/Wallabag/CoreBundle/DataFixtures/ORM/LoadEntryData.php b/src/Wallabag/CoreBundle/DataFixtures/ORM/LoadEntryData.php index b4b685f9..7e64c5e1 100644 --- a/src/Wallabag/CoreBundle/DataFixtures/ORM/LoadEntryData.php +++ b/src/Wallabag/CoreBundle/DataFixtures/ORM/LoadEntryData.php @@ -87,6 +87,7 @@ class LoadEntryData extends AbstractFixture implements OrderedFixtureInterface $entry5->setContent('This is my content /o/'); $entry5->setStarred(true); $entry5->setLanguage('fr'); + $entry5->setPreviewPicture('http://0.0.0.0/image.jpg'); $manager->persist($entry5); diff --git a/src/Wallabag/CoreBundle/Repository/EntryRepository.php b/src/Wallabag/CoreBundle/Repository/EntryRepository.php index 0e82b9b2..2286317c 100644 --- a/src/Wallabag/CoreBundle/Repository/EntryRepository.php +++ b/src/Wallabag/CoreBundle/Repository/EntryRepository.php @@ -162,24 +162,6 @@ class EntryRepository extends EntityRepository return $languages; } - /** - * Used only in test case to get the right entry associated to the right user - * - * @param string $username - * - * @return Entry - */ - public function findOneByUsernameAndNotStarred($username) - { - return $this->createQueryBuilder('e') - ->leftJoin('e.user', 'u') - ->where('u.username = :username')->setParameter('username', $username) - ->andWhere('e.isStarred = false') - ->setMaxResults(1) - ->getQuery() - ->getSingleResult(); - } - /** * Used only in test case to get the right entry associated to the right user * diff --git a/src/Wallabag/CoreBundle/Tests/Controller/EntryControllerTest.php b/src/Wallabag/CoreBundle/Tests/Controller/EntryControllerTest.php index 456c747a..e9c85a17 100644 --- a/src/Wallabag/CoreBundle/Tests/Controller/EntryControllerTest.php +++ b/src/Wallabag/CoreBundle/Tests/Controller/EntryControllerTest.php @@ -7,6 +7,8 @@ use Doctrine\ORM\AbstractQuery; class EntryControllerTest extends WallabagCoreTestCase { + public $url = 'http://www.lemonde.fr/pixels/article/2015/03/28/plongee-dans-l-univers-d-ingress-le-jeu-de-google-aux-frontieres-du-reel_4601155_4408996.html'; + public function testLogin() { $client = $this->getClient(); @@ -60,7 +62,7 @@ class EntryControllerTest extends WallabagCoreTestCase $form = $crawler->filter('button[type=submit]')->form(); $data = array( - 'entry[url]' => 'http://www.lemonde.fr/pixels/article/2015/03/28/plongee-dans-l-univers-d-ingress-le-jeu-de-google-aux-frontieres-du-reel_4601155_4408996.html', + 'entry[url]' => $this->url, ); $client->submit($form, $data); @@ -101,7 +103,7 @@ class EntryControllerTest extends WallabagCoreTestCase $content = $client->getContainer() ->get('doctrine.orm.entity_manager') ->getRepository('WallabagCoreBundle:Entry') - ->findOneByUsernameAndNotArchived('admin'); + ->findOneByUrl($this->url); $client->request('GET', '/view/'.$content->getId()); @@ -117,7 +119,7 @@ class EntryControllerTest extends WallabagCoreTestCase $content = $client->getContainer() ->get('doctrine.orm.entity_manager') ->getRepository('WallabagCoreBundle:Entry') - ->findOneByUsernameAndNotArchived('admin'); + ->findOneByUrl($this->url); $crawler = $client->request('GET', '/edit/'.$content->getId()); @@ -135,7 +137,7 @@ class EntryControllerTest extends WallabagCoreTestCase $content = $client->getContainer() ->get('doctrine.orm.entity_manager') ->getRepository('WallabagCoreBundle:Entry') - ->findOneByUsernameAndNotArchived('admin'); + ->findOneByUrl($this->url); $crawler = $client->request('GET', '/edit/'.$content->getId()); @@ -165,7 +167,7 @@ class EntryControllerTest extends WallabagCoreTestCase $content = $client->getContainer() ->get('doctrine.orm.entity_manager') ->getRepository('WallabagCoreBundle:Entry') - ->findOneByUsernameAndNotArchived('admin'); + ->findOneByUrl($this->url); $client->request('GET', '/archive/'.$content->getId()); @@ -187,7 +189,7 @@ class EntryControllerTest extends WallabagCoreTestCase $content = $client->getContainer() ->get('doctrine.orm.entity_manager') ->getRepository('WallabagCoreBundle:Entry') - ->findOneByUsernameAndNotStarred('admin'); + ->findOneByUrl($this->url); $client->request('GET', '/star/'.$content->getId()); @@ -209,7 +211,7 @@ class EntryControllerTest extends WallabagCoreTestCase $content = $client->getContainer() ->get('doctrine.orm.entity_manager') ->getRepository('WallabagCoreBundle:Entry') - ->findOneByUsernameAndNotStarred('admin'); + ->findOneByUrl($this->url); $client->request('GET', '/delete/'.$content->getId()); @@ -328,11 +330,11 @@ class EntryControllerTest extends WallabagCoreTestCase $crawler = $client->request('GET', '/unread/list'); $form = $crawler->filter('button[id=submit-filter]')->form(); $data = array( - 'entry_filter[domainName]' => 'monde', + 'entry_filter[domainName]' => 'domain', ); $crawler = $client->submit($form, $data); - $this->assertCount(1, $crawler->filter('div[class=entry]')); + $this->assertCount(5, $crawler->filter('div[class=entry]')); $form = $crawler->filter('button[id=submit-filter]')->form(); $data = array( @@ -361,7 +363,7 @@ class EntryControllerTest extends WallabagCoreTestCase $form['entry_filter[isStarred]']->tick(); $crawler = $client->submit($form); - $this->assertCount(2, $crawler->filter('div[class=entry]')); + $this->assertCount(1, $crawler->filter('div[class=entry]')); } public function testPreviewPictureFilter() @@ -389,7 +391,7 @@ class EntryControllerTest extends WallabagCoreTestCase ); $crawler = $client->submit($form, $data); - $this->assertCount(3, $crawler->filter('div[class=entry]')); + $this->assertCount(2, $crawler->filter('div[class=entry]')); $form = $crawler->filter('button[id=submit-filter]')->form(); $data = array( -- cgit v1.2.3