From a1a107705948c724aca7326f8550c336a25a6364 Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Thu, 1 Sep 2016 09:57:02 +0200 Subject: [PATCH] Add tests on ReadabilityImport --- .../ImportBundle/Import/ReadabilityImport.php | 2 - .../Controller/ReadabilityControllerTest.php | 122 ++++++++++++++ .../Import/ReadabilityImportTest.php | 150 ++++++++++++++++++ .../fixtures/readability-read.json | 25 +++ 4 files changed, 297 insertions(+), 2 deletions(-) create mode 100644 tests/Wallabag/ImportBundle/Controller/ReadabilityControllerTest.php create mode 100644 tests/Wallabag/ImportBundle/Import/ReadabilityImportTest.php create mode 100644 tests/Wallabag/ImportBundle/fixtures/readability-read.json diff --git a/src/Wallabag/ImportBundle/Import/ReadabilityImport.php b/src/Wallabag/ImportBundle/Import/ReadabilityImport.php index abea81a71..37b160c5c 100644 --- a/src/Wallabag/ImportBundle/Import/ReadabilityImport.php +++ b/src/Wallabag/ImportBundle/Import/ReadabilityImport.php @@ -8,7 +8,6 @@ use Wallabag\UserBundle\Entity\User; class ReadabilityImport extends AbstractImport { private $user; - private $client; private $skippedEntries = 0; private $importedEntries = 0; private $filepath; @@ -143,7 +142,6 @@ class ReadabilityImport extends AbstractImport $data = [ 'title' => $importedEntry['article__title'], - // 'html' => $importedEntry['article__excerpt'], 'url' => $importedEntry['article__url'], 'content_type' => '', 'language' => '', diff --git a/tests/Wallabag/ImportBundle/Controller/ReadabilityControllerTest.php b/tests/Wallabag/ImportBundle/Controller/ReadabilityControllerTest.php new file mode 100644 index 000000000..92cf4bfc8 --- /dev/null +++ b/tests/Wallabag/ImportBundle/Controller/ReadabilityControllerTest.php @@ -0,0 +1,122 @@ +logInAs('admin'); + $client = $this->getClient(); + + $crawler = $client->request('GET', '/import/readability'); + + $this->assertEquals(200, $client->getResponse()->getStatusCode()); + $this->assertEquals(1, $crawler->filter('form[name=upload_import_file] > button[type=submit]')->count()); + $this->assertEquals(1, $crawler->filter('input[type=file]')->count()); + } + + public function testImportReadabilityWithFile() + { + $this->logInAs('admin'); + $client = $this->getClient(); + + $crawler = $client->request('GET', '/import/readability'); + $form = $crawler->filter('form[name=upload_import_file] > button[type=submit]')->form(); + + $file = new UploadedFile(__DIR__.'/../fixtures/readability.json', 'readability.json'); + + $data = [ + 'upload_import_file[file]' => $file, + ]; + + $client->submit($form, $data); + + $this->assertEquals(302, $client->getResponse()->getStatusCode()); + + $crawler = $client->followRedirect(); + + $content = $client->getContainer() + ->get('doctrine.orm.entity_manager') + ->getRepository('WallabagCoreBundle:Entry') + ->findByUrlAndUserId( + 'https://venngage.com/blog/hashtags-are-worthless/', + $this->getLoggedInUserId() + ); + + $this->assertGreaterThan(1, $body = $crawler->filter('body')->extract(['_text'])); + $this->assertContains('flashes.import.notice.summary', $body[0]); + } + + public function testImportReadabilityWithFileAndMarkAllAsRead() + { + $this->logInAs('admin'); + $client = $this->getClient(); + + $crawler = $client->request('GET', '/import/readability'); + $form = $crawler->filter('form[name=upload_import_file] > button[type=submit]')->form(); + + $file = new UploadedFile(__DIR__.'/../fixtures/readability-read.json', 'readability-read.json'); + + $data = [ + 'upload_import_file[file]' => $file, + 'upload_import_file[mark_as_read]' => 1, + ]; + + $client->submit($form, $data); + + $this->assertEquals(302, $client->getResponse()->getStatusCode()); + + $crawler = $client->followRedirect(); + + $content1 = $client->getContainer() + ->get('doctrine.orm.entity_manager') + ->getRepository('WallabagCoreBundle:Entry') + ->findByUrlAndUserId( + 'https://blog.travis-ci.com/2016-07-28-what-we-learned-from-analyzing-2-million-travis-builds/', + $this->getLoggedInUserId() + ); + + $this->assertTrue($content1->isArchived()); + + $content2 = $client->getContainer() + ->get('doctrine.orm.entity_manager') + ->getRepository('WallabagCoreBundle:Entry') + ->findByUrlAndUserId( + 'https://facebook.github.io/graphql/', + $this->getLoggedInUserId() + ); + + $this->assertTrue($content2->isArchived()); + + $this->assertGreaterThan(1, $body = $crawler->filter('body')->extract(['_text'])); + $this->assertContains('flashes.import.notice.summary', $body[0]); + } + + public function testImportReadabilityWithEmptyFile() + { + $this->logInAs('admin'); + $client = $this->getClient(); + + $crawler = $client->request('GET', '/import/readability'); + $form = $crawler->filter('form[name=upload_import_file] > button[type=submit]')->form(); + + $file = new UploadedFile(__DIR__.'/../fixtures/test.txt', 'test.txt'); + + $data = [ + 'upload_import_file[file]' => $file, + ]; + + $client->submit($form, $data); + + $this->assertEquals(302, $client->getResponse()->getStatusCode()); + + $crawler = $client->followRedirect(); + + $this->assertGreaterThan(1, $body = $crawler->filter('body')->extract(['_text'])); + $this->assertContains('flashes.import.notice.failed', $body[0]); + } +} diff --git a/tests/Wallabag/ImportBundle/Import/ReadabilityImportTest.php b/tests/Wallabag/ImportBundle/Import/ReadabilityImportTest.php new file mode 100644 index 000000000..706d707b2 --- /dev/null +++ b/tests/Wallabag/ImportBundle/Import/ReadabilityImportTest.php @@ -0,0 +1,150 @@ +user = new User(); + + $this->em = $this->getMockBuilder('Doctrine\ORM\EntityManager') + ->disableOriginalConstructor() + ->getMock(); + + $this->contentProxy = $this->getMockBuilder('Wallabag\CoreBundle\Helper\ContentProxy') + ->disableOriginalConstructor() + ->getMock(); + + $wallabag = new ReadabilityImport($this->em, $this->contentProxy); + + $this->logHandler = new TestHandler(); + $logger = new Logger('test', [$this->logHandler]); + $wallabag->setLogger($logger); + + if (false === $unsetUser) { + $wallabag->setUser($this->user); + } + + return $wallabag; + } + + public function testInit() + { + $readabilityImport = $this->getReadabilityImport(); + + $this->assertEquals('Readability', $readabilityImport->getName()); + $this->assertNotEmpty($readabilityImport->getUrl()); + $this->assertEquals('import.readability.description', $readabilityImport->getDescription()); + } + + public function testImport() + { + $readabilityImport = $this->getReadabilityImport(); + $readabilityImport->setFilepath(__DIR__.'/../fixtures/readability.json'); + + $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository') + ->disableOriginalConstructor() + ->getMock(); + + $entryRepo->expects($this->exactly(2)) + ->method('findByUrlAndUserId') + ->will($this->onConsecutiveCalls(false, true)); + + $this->em + ->expects($this->any()) + ->method('getRepository') + ->willReturn($entryRepo); + + $entry = $this->getMockBuilder('Wallabag\CoreBundle\Entity\Entry') + ->disableOriginalConstructor() + ->getMock(); + + $this->contentProxy + ->expects($this->exactly(1)) + ->method('updateEntry') + ->willReturn($entry); + + $res = $readabilityImport->import(); + + $this->assertTrue($res); + $this->assertEquals(['skipped' => 1, 'imported' => 1], $readabilityImport->getSummary()); + } + + public function testImportAndMarkAllAsRead() + { + $readabilityImport = $this->getReadabilityImport(); + $readabilityImport->setFilepath(__DIR__.'/../fixtures/readability-read.json'); + + $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository') + ->disableOriginalConstructor() + ->getMock(); + + $entryRepo->expects($this->exactly(2)) + ->method('findByUrlAndUserId') + ->will($this->onConsecutiveCalls(false, false)); + + $this->em + ->expects($this->any()) + ->method('getRepository') + ->willReturn($entryRepo); + + $this->contentProxy + ->expects($this->exactly(2)) + ->method('updateEntry') + ->willReturn(new Entry($this->user)); + + // check that every entry persisted are archived + $this->em + ->expects($this->any()) + ->method('persist') + ->with($this->callback(function ($persistedEntry) { + return $persistedEntry->isArchived(); + })); + + $res = $readabilityImport->setMarkAsRead(true)->import(); + + $this->assertTrue($res); + + $this->assertEquals(['skipped' => 0, 'imported' => 2], $readabilityImport->getSummary()); + } + + public function testImportBadFile() + { + $readabilityImport = $this->getReadabilityImport(); + $readabilityImport->setFilepath(__DIR__.'/../fixtures/wallabag-v1.jsonx'); + + $res = $readabilityImport->import(); + + $this->assertFalse($res); + + $records = $this->logHandler->getRecords(); + $this->assertContains('ReadabilityImport: unable to read file', $records[0]['message']); + $this->assertEquals('ERROR', $records[0]['level_name']); + } + + public function testImportUserNotDefined() + { + $readabilityImport = $this->getReadabilityImport(true); + $readabilityImport->setFilepath(__DIR__.'/../fixtures/readability.json'); + + $res = $readabilityImport->import(); + + $this->assertFalse($res); + + $records = $this->logHandler->getRecords(); + $this->assertContains('ReadabilityImport: user is not defined', $records[0]['message']); + $this->assertEquals('ERROR', $records[0]['level_name']); + } +} diff --git a/tests/Wallabag/ImportBundle/fixtures/readability-read.json b/tests/Wallabag/ImportBundle/fixtures/readability-read.json new file mode 100644 index 000000000..c60767dc8 --- /dev/null +++ b/tests/Wallabag/ImportBundle/fixtures/readability-read.json @@ -0,0 +1,25 @@ +{ + "bookmarks": [ + { + "article__excerpt": "This is a guest post from Moritz Beller from the Delft University of Technology in The Netherlands. His team produced amazing research on several million Travis CI builds, creating invaluable…", + "favorite": false, + "date_archived": "2016-08-02T06:49:30", + "article__url": "https://blog.travis-ci.com/2016-07-28-what-we-learned-from-analyzing-2-million-travis-builds/", + "date_added": "2016-08-01T05:24:16", + "date_favorited": null, + "article__title": "Travis", + "archive": true + }, + { + "article__excerpt": "The GraphQL Type system describes the capabilities of a GraphQL server and is used to determine if a query is valid. The type system also describes the input types of query variables to determine if…", + "favorite": false, + "date_archived": "2016-07-19T06:48:31", + "article__url": "https://facebook.github.io/graphql/", + "date_added": "2016-06-24T17:50:16", + "date_favorited": null, + "article__title": "GraphQL", + "archive": true + } + ], + "recommendations": [] +}