3 namespace Tests\Wallabag\ImportBundle\Import
;
5 use M6Web\Component\RedisMock\RedisMockFactory
;
6 use Monolog\Handler\TestHandler
;
8 use PHPUnit\Framework\TestCase
;
9 use Simpleue\Queue\RedisQueue
;
10 use Wallabag\CoreBundle\Entity\Entry
;
11 use Wallabag\ImportBundle\Import\WallabagV1Import
;
12 use Wallabag\ImportBundle\Redis\Producer
;
13 use Wallabag\UserBundle\Entity\User
;
15 class WallabagV1ImportTest
extends TestCase
19 protected $logHandler;
20 protected $contentProxy;
21 protected $tagsAssigner;
23 protected $fetchingErrorMessageTitle = 'No title found';
24 protected $fetchingErrorMessage = 'wallabag can\'t retrieve contents for this article. Please <a href="http://doc.wallabag.org/en/master/user/errors_during_fetching.html#how-can-i-help-to-fix-that">troubleshoot this issue</a>.';
26 public function testInit()
28 $wallabagV1Import = $this->getWallabagV1Import();
30 $this->assertSame('wallabag v1', $wallabagV1Import->getName());
31 $this->assertNotEmpty($wallabagV1Import->getUrl());
32 $this->assertSame('import.wallabag_v1.description', $wallabagV1Import->getDescription());
35 public function testImport()
37 $wallabagV1Import = $this->getWallabagV1Import(false, 1);
38 $wallabagV1Import->setFilepath(__DIR__
. '/../fixtures/wallabag-v1.json');
40 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
41 ->disableOriginalConstructor()
44 $entryRepo->expects($this->exactly(2))
45 ->method('findByUrlAndUserId')
46 ->will($this->onConsecutiveCalls(false, true, false, false));
49 ->expects($this->any())
50 ->method('getRepository')
51 ->willReturn($entryRepo);
53 $entry = $this->getMockBuilder('Wallabag\CoreBundle\Entity\Entry')
54 ->disableOriginalConstructor()
58 ->expects($this->exactly(1))
59 ->method('updateEntry')
62 $res = $wallabagV1Import->import();
64 $this->assertTrue($res);
65 $this->assertSame(['skipped' => 1, 'imported' => 1, 'queued' => 0], $wallabagV1Import->getSummary());
68 public function testImportAndMarkAllAsRead()
70 $wallabagV1Import = $this->getWallabagV1Import(false, 3);
71 $wallabagV1Import->setFilepath(__DIR__
. '/../fixtures/wallabag-v1-read.json');
73 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
74 ->disableOriginalConstructor()
77 $entryRepo->expects($this->exactly(3))
78 ->method('findByUrlAndUserId')
79 ->will($this->onConsecutiveCalls(false, false, false));
82 ->expects($this->any())
83 ->method('getRepository')
84 ->willReturn($entryRepo);
87 ->expects($this->exactly(3))
88 ->method('updateEntry')
89 ->willReturn(new Entry($this->user
));
91 // check that every entry persisted are archived
93 ->expects($this->any())
95 ->with($this->callback(function ($persistedEntry) {
96 return $persistedEntry->isArchived();
99 $res = $wallabagV1Import->setMarkAsRead(true)->import();
101 $this->assertTrue($res);
103 $this->assertSame(['skipped' => 0, 'imported' => 3, 'queued' => 0], $wallabagV1Import->getSummary());
106 public function testImportWithRabbit()
108 $wallabagV1Import = $this->getWallabagV1Import();
109 $wallabagV1Import->setFilepath(__DIR__
. '/../fixtures/wallabag-v1.json');
111 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
112 ->disableOriginalConstructor()
115 $entryRepo->expects($this->never())
116 ->method('findByUrlAndUserId');
119 ->expects($this->never())
120 ->method('getRepository');
122 $entry = $this->getMockBuilder('Wallabag\CoreBundle\Entity\Entry')
123 ->disableOriginalConstructor()
127 ->expects($this->never())
128 ->method('updateEntry');
130 $producer = $this->getMockBuilder('OldSound\RabbitMqBundle\RabbitMq\Producer')
131 ->disableOriginalConstructor()
135 ->expects($this->exactly(2))
138 $wallabagV1Import->setProducer($producer);
140 $res = $wallabagV1Import->setMarkAsRead(true)->import();
142 $this->assertTrue($res);
143 $this->assertSame(['skipped' => 0, 'imported' => 0, 'queued' => 2], $wallabagV1Import->getSummary());
146 public function testImportWithRedis()
148 $wallabagV1Import = $this->getWallabagV1Import();
149 $wallabagV1Import->setFilepath(__DIR__
. '/../fixtures/wallabag-v1.json');
151 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
152 ->disableOriginalConstructor()
155 $entryRepo->expects($this->never())
156 ->method('findByUrlAndUserId');
159 ->expects($this->never())
160 ->method('getRepository');
162 $entry = $this->getMockBuilder('Wallabag\CoreBundle\Entity\Entry')
163 ->disableOriginalConstructor()
167 ->expects($this->never())
168 ->method('updateEntry');
170 $factory = new RedisMockFactory();
171 $redisMock = $factory->getAdapter('Predis\Client', true);
173 $queue = new RedisQueue($redisMock, 'wallabag_v1');
174 $producer = new Producer($queue);
176 $wallabagV1Import->setProducer($producer);
178 $res = $wallabagV1Import->setMarkAsRead(true)->import();
180 $this->assertTrue($res);
181 $this->assertSame(['skipped' => 0, 'imported' => 0, 'queued' => 2], $wallabagV1Import->getSummary());
183 $this->assertNotEmpty($redisMock->lpop('wallabag_v1'));
186 public function testImportBadFile()
188 $wallabagV1Import = $this->getWallabagV1Import();
189 $wallabagV1Import->setFilepath(__DIR__
. '/../fixtures/wallabag-v1.jsonx');
191 $res = $wallabagV1Import->import();
193 $this->assertFalse($res);
195 $records = $this->logHandler
->getRecords();
196 $this->assertContains('WallabagImport: unable to read file', $records[0]['message']);
197 $this->assertSame('ERROR', $records[0]['level_name']);
200 public function testImportUserNotDefined()
202 $wallabagV1Import = $this->getWallabagV1Import(true);
203 $wallabagV1Import->setFilepath(__DIR__
. '/../fixtures/wallabag-v1.json');
205 $res = $wallabagV1Import->import();
207 $this->assertFalse($res);
209 $records = $this->logHandler
->getRecords();
210 $this->assertContains('WallabagImport: user is not defined', $records[0]['message']);
211 $this->assertSame('ERROR', $records[0]['level_name']);
214 private function getWallabagV1Import($unsetUser = false, $dispatched = 0)
216 $this->user
= new User();
218 $this->em
= $this->getMockBuilder('Doctrine\ORM\EntityManager')
219 ->disableOriginalConstructor()
222 $this->uow
= $this->getMockBuilder('Doctrine\ORM\UnitOfWork')
223 ->disableOriginalConstructor()
227 ->expects($this->any())
228 ->method('getUnitOfWork')
229 ->willReturn($this->uow
);
232 ->expects($this->any())
233 ->method('getScheduledEntityInsertions')
236 $this->contentProxy
= $this->getMockBuilder('Wallabag\CoreBundle\Helper\ContentProxy')
237 ->disableOriginalConstructor()
240 $this->tagsAssigner
= $this->getMockBuilder('Wallabag\CoreBundle\Helper\TagsAssigner')
241 ->disableOriginalConstructor()
244 $dispatcher = $this->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcher')
245 ->disableOriginalConstructor()
249 ->expects($this->exactly($dispatched))
250 ->method('dispatch');
252 $wallabag = new WallabagV1Import(
257 $this->fetchingErrorMessageTitle
,
258 $this->fetchingErrorMessage
261 $this->logHandler
= new TestHandler();
262 $logger = new Logger('test', [$this->logHandler
]);
263 $wallabag->setLogger($logger);
265 if (false === $unsetUser) {
266 $wallabag->setUser($this->user
);