2016-01-20 14:37:01 +01:00
|
|
|
<?php
|
|
|
|
|
2023-12-31 18:21:09 +01:00
|
|
|
namespace Tests\Wallabag\CoreBundle\Import;
|
2016-01-20 14:37:01 +01:00
|
|
|
|
2022-09-01 20:54:56 +02:00
|
|
|
use Doctrine\ORM\EntityManager;
|
|
|
|
use Doctrine\ORM\UnitOfWork;
|
2017-07-01 09:52:38 +02:00
|
|
|
use M6Web\Component\RedisMock\RedisMockFactory;
|
2016-01-20 14:37:01 +01:00
|
|
|
use Monolog\Handler\TestHandler;
|
2017-07-01 09:52:38 +02:00
|
|
|
use Monolog\Logger;
|
2017-12-16 22:17:42 +01:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2022-09-01 20:54:56 +02:00
|
|
|
use Predis\Client;
|
2016-09-09 21:02:03 +02:00
|
|
|
use Simpleue\Queue\RedisQueue;
|
2022-09-01 20:54:56 +02:00
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcher;
|
2017-07-01 09:52:38 +02:00
|
|
|
use Wallabag\CoreBundle\Entity\Entry;
|
2023-12-30 23:32:36 +01:00
|
|
|
use Wallabag\CoreBundle\Entity\User;
|
2022-09-01 20:54:56 +02:00
|
|
|
use Wallabag\CoreBundle\Helper\ContentProxy;
|
|
|
|
use Wallabag\CoreBundle\Helper\TagsAssigner;
|
2023-12-31 18:21:09 +01:00
|
|
|
use Wallabag\CoreBundle\Import\WallabagV2Import;
|
2023-12-31 21:13:25 +01:00
|
|
|
use Wallabag\CoreBundle\Redis\Producer;
|
2022-09-01 20:54:56 +02:00
|
|
|
use Wallabag\CoreBundle\Repository\EntryRepository;
|
2016-01-20 14:37:01 +01:00
|
|
|
|
2017-12-16 22:17:42 +01:00
|
|
|
class WallabagV2ImportTest extends TestCase
|
2016-01-20 14:37:01 +01:00
|
|
|
{
|
|
|
|
protected $user;
|
|
|
|
protected $em;
|
|
|
|
protected $logHandler;
|
2016-02-11 13:27:17 +01:00
|
|
|
protected $contentProxy;
|
2017-05-27 22:08:14 +02:00
|
|
|
protected $tagsAssigner;
|
|
|
|
protected $uow;
|
2016-01-20 14:37:01 +01:00
|
|
|
|
|
|
|
public function testInit()
|
|
|
|
{
|
|
|
|
$wallabagV2Import = $this->getWallabagV2Import();
|
|
|
|
|
2017-07-01 09:52:38 +02:00
|
|
|
$this->assertSame('wallabag v2', $wallabagV2Import->getName());
|
2016-01-20 14:37:01 +01:00
|
|
|
$this->assertNotEmpty($wallabagV2Import->getUrl());
|
2017-07-01 09:52:38 +02:00
|
|
|
$this->assertSame('import.wallabag_v2.description', $wallabagV2Import->getDescription());
|
2016-01-20 14:37:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testImport()
|
|
|
|
{
|
2016-11-02 07:10:23 +01:00
|
|
|
$wallabagV2Import = $this->getWallabagV2Import(false, 2);
|
2023-12-31 18:21:09 +01:00
|
|
|
$wallabagV2Import->setFilepath(__DIR__ . '/../fixtures/Import/wallabag-v2.json');
|
2016-01-20 14:37:01 +01:00
|
|
|
|
2022-09-01 20:54:56 +02:00
|
|
|
$entryRepo = $this->getMockBuilder(EntryRepository::class)
|
2016-01-20 14:37:01 +01:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
2017-05-16 21:17:10 +02:00
|
|
|
$entryRepo->expects($this->exactly(6))
|
2016-01-20 14:37:01 +01:00
|
|
|
->method('findByUrlAndUserId')
|
|
|
|
->will($this->onConsecutiveCalls(false, true, false));
|
|
|
|
|
|
|
|
$this->em
|
|
|
|
->expects($this->any())
|
|
|
|
->method('getRepository')
|
|
|
|
->willReturn($entryRepo);
|
|
|
|
|
2016-03-27 23:32:55 +02:00
|
|
|
$this->contentProxy
|
|
|
|
->expects($this->exactly(2))
|
|
|
|
->method('updateEntry')
|
|
|
|
->willReturn(new Entry($this->user));
|
|
|
|
|
2016-01-20 14:37:01 +01:00
|
|
|
$res = $wallabagV2Import->import();
|
|
|
|
|
|
|
|
$this->assertTrue($res);
|
2017-07-01 09:52:38 +02:00
|
|
|
$this->assertSame(['skipped' => 4, 'imported' => 2, 'queued' => 0], $wallabagV2Import->getSummary());
|
2016-02-13 16:20:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testImportAndMarkAllAsRead()
|
|
|
|
{
|
2016-11-02 07:10:23 +01:00
|
|
|
$wallabagV2Import = $this->getWallabagV2Import(false, 2);
|
2023-12-31 18:21:09 +01:00
|
|
|
$wallabagV2Import->setFilepath(__DIR__ . '/../fixtures/Import/wallabag-v2-read.json');
|
2016-02-13 16:20:00 +01:00
|
|
|
|
2022-09-01 20:54:56 +02:00
|
|
|
$entryRepo = $this->getMockBuilder(EntryRepository::class)
|
2016-02-13 16:20:00 +01:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$entryRepo->expects($this->exactly(2))
|
|
|
|
->method('findByUrlAndUserId')
|
|
|
|
->will($this->onConsecutiveCalls(false, false));
|
|
|
|
|
|
|
|
$this->em
|
|
|
|
->expects($this->any())
|
|
|
|
->method('getRepository')
|
|
|
|
->willReturn($entryRepo);
|
|
|
|
|
2016-03-27 23:32:55 +02:00
|
|
|
$this->contentProxy
|
|
|
|
->expects($this->exactly(2))
|
|
|
|
->method('updateEntry')
|
|
|
|
->willReturn(new Entry($this->user));
|
|
|
|
|
2016-03-04 10:04:51 +01:00
|
|
|
// check that every entry persisted are archived
|
|
|
|
$this->em
|
|
|
|
->expects($this->any())
|
|
|
|
->method('persist')
|
2016-03-08 15:22:35 +01:00
|
|
|
->with($this->callback(function ($persistedEntry) {
|
2020-06-15 13:37:50 +02:00
|
|
|
return (bool) $persistedEntry->isArchived();
|
2016-03-04 10:04:51 +01:00
|
|
|
}));
|
|
|
|
|
2016-02-13 16:20:00 +01:00
|
|
|
$res = $wallabagV2Import->setMarkAsRead(true)->import();
|
|
|
|
|
|
|
|
$this->assertTrue($res);
|
|
|
|
|
2017-07-01 09:52:38 +02:00
|
|
|
$this->assertSame(['skipped' => 0, 'imported' => 2, 'queued' => 0], $wallabagV2Import->getSummary());
|
2016-01-20 14:37:01 +01:00
|
|
|
}
|
|
|
|
|
2016-09-09 18:02:29 +02:00
|
|
|
public function testImportWithRabbit()
|
|
|
|
{
|
|
|
|
$wallabagV2Import = $this->getWallabagV2Import();
|
2023-12-31 18:21:09 +01:00
|
|
|
$wallabagV2Import->setFilepath(__DIR__ . '/../fixtures/Import/wallabag-v2.json');
|
2016-09-09 18:02:29 +02:00
|
|
|
|
2022-09-01 20:54:56 +02:00
|
|
|
$entryRepo = $this->getMockBuilder(EntryRepository::class)
|
2016-09-09 18:02:29 +02:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$entryRepo->expects($this->never())
|
|
|
|
->method('findByUrlAndUserId');
|
|
|
|
|
|
|
|
$this->em
|
|
|
|
->expects($this->never())
|
|
|
|
->method('getRepository');
|
|
|
|
|
|
|
|
$this->contentProxy
|
|
|
|
->expects($this->never())
|
|
|
|
->method('updateEntry');
|
|
|
|
|
2022-09-01 20:54:56 +02:00
|
|
|
$producer = $this->getMockBuilder(\OldSound\RabbitMqBundle\RabbitMq\Producer::class)
|
2016-09-09 18:02:29 +02:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$producer
|
2017-05-16 21:17:10 +02:00
|
|
|
->expects($this->exactly(6))
|
2016-09-09 18:02:29 +02:00
|
|
|
->method('publish');
|
|
|
|
|
2016-09-09 21:02:03 +02:00
|
|
|
$wallabagV2Import->setProducer($producer);
|
2016-09-09 18:02:29 +02:00
|
|
|
|
|
|
|
$res = $wallabagV2Import->setMarkAsRead(true)->import();
|
|
|
|
|
|
|
|
$this->assertTrue($res);
|
2017-07-01 09:52:38 +02:00
|
|
|
$this->assertSame(['skipped' => 0, 'imported' => 0, 'queued' => 6], $wallabagV2Import->getSummary());
|
2016-09-09 18:02:29 +02:00
|
|
|
}
|
|
|
|
|
2016-09-09 21:02:03 +02:00
|
|
|
public function testImportWithRedis()
|
|
|
|
{
|
|
|
|
$wallabagV2Import = $this->getWallabagV2Import();
|
2023-12-31 18:21:09 +01:00
|
|
|
$wallabagV2Import->setFilepath(__DIR__ . '/../fixtures/Import/wallabag-v2.json');
|
2016-09-09 21:02:03 +02:00
|
|
|
|
2022-09-01 20:54:56 +02:00
|
|
|
$entryRepo = $this->getMockBuilder(EntryRepository::class)
|
2016-09-09 21:02:03 +02:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$entryRepo->expects($this->never())
|
|
|
|
->method('findByUrlAndUserId');
|
|
|
|
|
|
|
|
$this->em
|
|
|
|
->expects($this->never())
|
|
|
|
->method('getRepository');
|
|
|
|
|
|
|
|
$this->contentProxy
|
|
|
|
->expects($this->never())
|
|
|
|
->method('updateEntry');
|
|
|
|
|
|
|
|
$factory = new RedisMockFactory();
|
2022-09-01 20:54:56 +02:00
|
|
|
$redisMock = $factory->getAdapter(Client::class, true);
|
2016-09-09 21:02:03 +02:00
|
|
|
|
|
|
|
$queue = new RedisQueue($redisMock, 'wallabag_v2');
|
|
|
|
$producer = new Producer($queue);
|
|
|
|
|
|
|
|
$wallabagV2Import->setProducer($producer);
|
|
|
|
|
|
|
|
$res = $wallabagV2Import->setMarkAsRead(true)->import();
|
|
|
|
|
|
|
|
$this->assertTrue($res);
|
2017-07-01 09:52:38 +02:00
|
|
|
$this->assertSame(['skipped' => 0, 'imported' => 0, 'queued' => 6], $wallabagV2Import->getSummary());
|
2016-09-09 21:02:03 +02:00
|
|
|
|
|
|
|
$this->assertNotEmpty($redisMock->lpop('wallabag_v2'));
|
|
|
|
}
|
|
|
|
|
2016-01-20 14:37:01 +01:00
|
|
|
public function testImportBadFile()
|
|
|
|
{
|
|
|
|
$wallabagV1Import = $this->getWallabagV2Import();
|
2023-12-31 18:21:09 +01:00
|
|
|
$wallabagV1Import->setFilepath(__DIR__ . '/../fixtures/Import/wallabag-v2.jsonx');
|
2016-01-20 14:37:01 +01:00
|
|
|
|
|
|
|
$res = $wallabagV1Import->import();
|
|
|
|
|
|
|
|
$this->assertFalse($res);
|
|
|
|
|
|
|
|
$records = $this->logHandler->getRecords();
|
2020-06-15 13:37:50 +02:00
|
|
|
$this->assertStringContainsString('WallabagImport: unable to read file', $records[0]['message']);
|
2017-07-01 09:52:38 +02:00
|
|
|
$this->assertSame('ERROR', $records[0]['level_name']);
|
2016-01-20 14:37:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testImportUserNotDefined()
|
|
|
|
{
|
|
|
|
$wallabagV1Import = $this->getWallabagV2Import(true);
|
2023-12-31 18:21:09 +01:00
|
|
|
$wallabagV1Import->setFilepath(__DIR__ . '/../fixtures/Import/wallabag-v2.json');
|
2016-01-20 14:37:01 +01:00
|
|
|
|
|
|
|
$res = $wallabagV1Import->import();
|
|
|
|
|
|
|
|
$this->assertFalse($res);
|
|
|
|
|
|
|
|
$records = $this->logHandler->getRecords();
|
2020-06-15 13:37:50 +02:00
|
|
|
$this->assertStringContainsString('WallabagImport: user is not defined', $records[0]['message']);
|
2017-07-01 09:52:38 +02:00
|
|
|
$this->assertSame('ERROR', $records[0]['level_name']);
|
2016-01-20 14:37:01 +01:00
|
|
|
}
|
2016-08-19 23:52:19 +02:00
|
|
|
|
|
|
|
public function testImportEmptyFile()
|
|
|
|
{
|
|
|
|
$wallabagV2Import = $this->getWallabagV2Import();
|
2023-12-31 18:21:09 +01:00
|
|
|
$wallabagV2Import->setFilepath(__DIR__ . '/../fixtures/Import/wallabag-v2-empty.json');
|
2016-08-19 23:52:19 +02:00
|
|
|
|
|
|
|
$res = $wallabagV2Import->import();
|
|
|
|
|
|
|
|
$this->assertFalse($res);
|
2017-07-01 09:52:38 +02:00
|
|
|
$this->assertSame(['skipped' => 0, 'imported' => 0, 'queued' => 0], $wallabagV2Import->getSummary());
|
2016-08-19 23:52:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testImportWithExceptionFromGraby()
|
|
|
|
{
|
2016-11-02 07:10:23 +01:00
|
|
|
$wallabagV2Import = $this->getWallabagV2Import(false, 2);
|
2023-12-31 18:21:09 +01:00
|
|
|
$wallabagV2Import->setFilepath(__DIR__ . '/../fixtures/Import/wallabag-v2.json');
|
2016-08-19 23:52:19 +02:00
|
|
|
|
2022-09-01 20:54:56 +02:00
|
|
|
$entryRepo = $this->getMockBuilder(EntryRepository::class)
|
2016-08-19 23:52:19 +02:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
2017-05-16 21:17:10 +02:00
|
|
|
$entryRepo->expects($this->exactly(6))
|
2016-08-19 23:52:19 +02:00
|
|
|
->method('findByUrlAndUserId')
|
|
|
|
->will($this->onConsecutiveCalls(false, true, false));
|
|
|
|
|
|
|
|
$this->em
|
|
|
|
->expects($this->any())
|
|
|
|
->method('getRepository')
|
|
|
|
->willReturn($entryRepo);
|
|
|
|
|
|
|
|
$this->contentProxy
|
|
|
|
->expects($this->exactly(2))
|
|
|
|
->method('updateEntry')
|
|
|
|
->will($this->throwException(new \Exception()));
|
|
|
|
|
|
|
|
$res = $wallabagV2Import->import();
|
|
|
|
|
|
|
|
$this->assertTrue($res);
|
2017-07-01 09:52:38 +02:00
|
|
|
$this->assertSame(['skipped' => 4, 'imported' => 2, 'queued' => 0], $wallabagV2Import->getSummary());
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getWallabagV2Import($unsetUser = false, $dispatched = 0)
|
|
|
|
{
|
|
|
|
$this->user = new User();
|
|
|
|
|
2022-09-01 20:54:56 +02:00
|
|
|
$this->em = $this->getMockBuilder(EntityManager::class)
|
2017-07-01 09:52:38 +02:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
2022-09-01 20:54:56 +02:00
|
|
|
$this->uow = $this->getMockBuilder(UnitOfWork::class)
|
2017-07-01 09:52:38 +02:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$this->em
|
|
|
|
->expects($this->any())
|
|
|
|
->method('getUnitOfWork')
|
|
|
|
->willReturn($this->uow);
|
|
|
|
|
|
|
|
$this->uow
|
|
|
|
->expects($this->any())
|
|
|
|
->method('getScheduledEntityInsertions')
|
|
|
|
->willReturn([]);
|
|
|
|
|
2022-09-01 20:54:56 +02:00
|
|
|
$this->contentProxy = $this->getMockBuilder(ContentProxy::class)
|
2017-07-01 09:52:38 +02:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
2022-09-01 20:54:56 +02:00
|
|
|
$this->tagsAssigner = $this->getMockBuilder(TagsAssigner::class)
|
2017-07-01 09:52:38 +02:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
2022-09-01 20:54:56 +02:00
|
|
|
$dispatcher = $this->getMockBuilder(EventDispatcher::class)
|
2017-07-01 09:52:38 +02:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$dispatcher
|
|
|
|
->expects($this->exactly($dispatched))
|
|
|
|
->method('dispatch');
|
|
|
|
|
|
|
|
$this->logHandler = new TestHandler();
|
|
|
|
$logger = new Logger('test', [$this->logHandler]);
|
2022-08-27 20:57:18 +02:00
|
|
|
|
|
|
|
$wallabag = new WallabagV2Import($this->em, $this->contentProxy, $this->tagsAssigner, $dispatcher, $logger);
|
2017-07-01 09:52:38 +02:00
|
|
|
|
|
|
|
if (false === $unsetUser) {
|
|
|
|
$wallabag->setUser($this->user);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $wallabag;
|
2016-08-19 23:52:19 +02:00
|
|
|
}
|
2016-01-20 14:37:01 +01:00
|
|
|
}
|