Add support for tag in Instapaper import

This commit is contained in:
Jeremy Benoist 2017-05-31 10:38:00 +02:00
parent 7aa5607f29
commit 7a8ed3cee1
No known key found for this signature in database
GPG key ID: BCA73962457ACC3C
4 changed files with 60 additions and 13 deletions

View file

@ -68,6 +68,14 @@ class InstapaperImport extends AbstractImport
continue;
}
// last element in the csv is the folder where the content belong
// BUT it can also be the status (since status = folder in Instapaper)
// and we don't want archive, unread & starred to become a tag
$tags = null;
if (false === in_array($data[3], ['Archive', 'Unread', 'Starred'])) {
$tags = [$data[3]];
}
$entries[] = [
'url' => $data[0],
'title' => $data[1],
@ -75,6 +83,7 @@ class InstapaperImport extends AbstractImport
'is_archived' => $data[3] === 'Archive' || $data[3] === 'Starred',
'is_starred' => $data[3] === 'Starred',
'html' => false,
'tags' => $tags,
];
}
fclose($handle);
@ -118,6 +127,14 @@ class InstapaperImport extends AbstractImport
// update entry with content (in case fetching failed, the given entry will be return)
$entry = $this->fetchContent($entry, $importedEntry['url'], $importedEntry);
if (!empty($importedEntry['tags'])) {
$this->tagsAssigner->assignTagsToEntry(
$entry,
$importedEntry['tags'],
$this->em->getUnitOfWork()->getScheduledEntityInsertions()
);
}
$entry->setArchived($importedEntry['is_archived']);
$entry->setStarred($importedEntry['is_starred']);

View file

@ -107,6 +107,9 @@ class InstapaperControllerTest extends WallabagCoreTestCase
$crawler = $client->followRedirect();
$this->assertGreaterThan(1, $body = $crawler->filter('body')->extract(['_text']));
$this->assertContains('flashes.import.notice.summary', $body[0]);
$content = $client->getContainer()
->get('doctrine.orm.entity_manager')
->getRepository('WallabagCoreBundle:Entry')
@ -115,14 +118,25 @@ class InstapaperControllerTest extends WallabagCoreTestCase
$this->getLoggedInUserId()
);
$this->assertGreaterThan(1, $body = $crawler->filter('body')->extract(['_text']));
$this->assertContains('flashes.import.notice.summary', $body[0]);
$this->assertNotEmpty($content->getMimetype(), 'Mimetype for http://www.liberation.fr is ok');
$this->assertNotEmpty($content->getPreviewPicture(), 'Preview picture for http://www.liberation.fr is ok');
$this->assertNotEmpty($content->getLanguage(), 'Language for http://www.liberation.fr is ok');
$this->assertContains('foot', $content->getTags(), 'It includes the "foot" tag');
$this->assertEquals(1, count($content->getTags()));
$this->assertInstanceOf(\DateTime::class, $content->getCreatedAt());
$content = $client->getContainer()
->get('doctrine.orm.entity_manager')
->getRepository('WallabagCoreBundle:Entry')
->findByUrlAndUserId(
'http://www.20minutes.fr/high-tech/2077615-20170531-dis-donc-donald-trump-quoi-exactement-covfefe',
$this->getLoggedInUserId()
);
$this->assertContains('foot', $content->getTags());
$this->assertContains('test_tag', $content->getTags());
$this->assertEquals(2, count($content->getTags()));
}
public function testImportInstapaperWithFileAndMarkAllAsRead()

View file

@ -18,6 +18,7 @@ class InstapaperImportTest extends \PHPUnit_Framework_TestCase
protected $logHandler;
protected $contentProxy;
protected $tagsAssigner;
protected $uow;
private function getInstapaperImport($unsetUser = false, $dispatched = 0)
{
@ -27,6 +28,20 @@ class InstapaperImportTest extends \PHPUnit_Framework_TestCase
->disableOriginalConstructor()
->getMock();
$this->uow = $this->getMockBuilder('Doctrine\ORM\UnitOfWork')
->disableOriginalConstructor()
->getMock();
$this->em
->expects($this->any())
->method('getUnitOfWork')
->willReturn($this->uow);
$this->uow
->expects($this->any())
->method('getScheduledEntityInsertions')
->willReturn([]);
$this->contentProxy = $this->getMockBuilder('Wallabag\CoreBundle\Helper\ContentProxy')
->disableOriginalConstructor()
->getMock();
@ -67,14 +82,14 @@ class InstapaperImportTest extends \PHPUnit_Framework_TestCase
public function testImport()
{
$instapaperImport = $this->getInstapaperImport(false, 3);
$instapaperImport = $this->getInstapaperImport(false, 4);
$instapaperImport->setFilepath(__DIR__.'/../fixtures/instapaper-export.csv');
$entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
->disableOriginalConstructor()
->getMock();
$entryRepo->expects($this->exactly(3))
$entryRepo->expects($this->exactly(4))
->method('findByUrlAndUserId')
->willReturn(false);
@ -88,14 +103,14 @@ class InstapaperImportTest extends \PHPUnit_Framework_TestCase
->getMock();
$this->contentProxy
->expects($this->exactly(3))
->expects($this->exactly(4))
->method('updateEntry')
->willReturn($entry);
$res = $instapaperImport->import();
$this->assertTrue($res);
$this->assertEquals(['skipped' => 0, 'imported' => 3, 'queued' => 0], $instapaperImport->getSummary());
$this->assertEquals(['skipped' => 0, 'imported' => 4, 'queued' => 0], $instapaperImport->getSummary());
}
public function testImportAndMarkAllAsRead()
@ -107,9 +122,9 @@ class InstapaperImportTest extends \PHPUnit_Framework_TestCase
->disableOriginalConstructor()
->getMock();
$entryRepo->expects($this->exactly(3))
$entryRepo->expects($this->exactly(4))
->method('findByUrlAndUserId')
->will($this->onConsecutiveCalls(false, true, true));
->will($this->onConsecutiveCalls(false, true, true, true));
$this->em
->expects($this->any())
@ -133,7 +148,7 @@ class InstapaperImportTest extends \PHPUnit_Framework_TestCase
$this->assertTrue($res);
$this->assertEquals(['skipped' => 2, 'imported' => 1, 'queued' => 0], $instapaperImport->getSummary());
$this->assertEquals(['skipped' => 3, 'imported' => 1, 'queued' => 0], $instapaperImport->getSummary());
}
public function testImportWithRabbit()
@ -165,7 +180,7 @@ class InstapaperImportTest extends \PHPUnit_Framework_TestCase
->getMock();
$producer
->expects($this->exactly(3))
->expects($this->exactly(4))
->method('publish');
$instapaperImport->setProducer($producer);
@ -173,7 +188,7 @@ class InstapaperImportTest extends \PHPUnit_Framework_TestCase
$res = $instapaperImport->setMarkAsRead(true)->import();
$this->assertTrue($res);
$this->assertEquals(['skipped' => 0, 'imported' => 0, 'queued' => 3], $instapaperImport->getSummary());
$this->assertEquals(['skipped' => 0, 'imported' => 0, 'queued' => 4], $instapaperImport->getSummary());
}
public function testImportWithRedis()
@ -211,7 +226,7 @@ class InstapaperImportTest extends \PHPUnit_Framework_TestCase
$res = $instapaperImport->setMarkAsRead(true)->import();
$this->assertTrue($res);
$this->assertEquals(['skipped' => 0, 'imported' => 0, 'queued' => 3], $instapaperImport->getSummary());
$this->assertEquals(['skipped' => 0, 'imported' => 0, 'queued' => 4], $instapaperImport->getSummary());
$this->assertNotEmpty($redisMock->lpop('instapaper'));
}

View file

@ -2,3 +2,4 @@ URL,Title,Selection,Folder
http://www.liberation.fr/societe/2012/12/06/baumettes-un-tour-en-cellule_865551,Baumettes : un tour en cellule,,Unread
https://redditblog.com/2016/09/20/amp-and-reactredux/,AMP and React+Redux: Why Not?,,Archive
https://medium.com/@the_minh/why-foursquare-swarm-is-still-my-favourite-social-network-e38228493e6c,Why Foursquare / Swarm is still my favourite social network,,Starred
http://www.20minutes.fr/high-tech/2077615-20170531-dis-donc-donald-trump-quoi-exactement-covfefe,"Dis donc Donald Trump, c'est quoi exactement «covfefe»?",,test_tag

1 URL Title Selection Folder
2 http://www.liberation.fr/societe/2012/12/06/baumettes-un-tour-en-cellule_865551 Baumettes : un tour en cellule Unread
3 https://redditblog.com/2016/09/20/amp-and-reactredux/ AMP and React+Redux: Why Not? Archive
4 https://medium.com/@the_minh/why-foursquare-swarm-is-still-my-favourite-social-network-e38228493e6c Why Foursquare / Swarm is still my favourite social network Starred
5 http://www.20minutes.fr/high-tech/2077615-20170531-dis-donc-donald-trump-quoi-exactement-covfefe Dis donc Donald Trump, c'est quoi exactement «covfefe»? test_tag