3 namespace Tests\Wallabag\ImportBundle\Import
;
5 use Wallabag\ImportBundle\Import\WallabagV1Import
;
6 use Wallabag\UserBundle\Entity\User
;
7 use Wallabag\CoreBundle\Entity\Entry
;
8 use Wallabag\ImportBundle\Redis\Producer
;
10 use Monolog\Handler\TestHandler
;
11 use Simpleue\Queue\RedisQueue
;
12 use M6Web\Component\RedisMock\RedisMockFactory
;
14 class WallabagV1ImportTest
extends \PHPUnit_Framework_TestCase
18 protected $logHandler;
19 protected $contentProxy;
20 protected $tagsAssigner;
22 protected $fetchingErrorMessageTitle = 'No title found';
23 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>.';
25 private function getWallabagV1Import($unsetUser = false, $dispatched = 0)
27 $this->user
= new User();
29 $this->em
= $this->getMockBuilder('Doctrine\ORM\EntityManager')
30 ->disableOriginalConstructor()
33 $this->uow
= $this->getMockBuilder('Doctrine\ORM\UnitOfWork')
34 ->disableOriginalConstructor()
38 ->expects($this->any())
39 ->method('getUnitOfWork')
40 ->willReturn($this->uow
);
43 ->expects($this->any())
44 ->method('getScheduledEntityInsertions')
47 $this->contentProxy
= $this->getMockBuilder('Wallabag\CoreBundle\Helper\ContentProxy')
48 ->disableOriginalConstructor()
51 $this->tagsAssigner
= $this->getMockBuilder('Wallabag\CoreBundle\Helper\TagsAssigner')
52 ->disableOriginalConstructor()
55 $dispatcher = $this->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcher')
56 ->disableOriginalConstructor()
60 ->expects($this->exactly($dispatched))
63 $wallabag = new WallabagV1Import(
68 $this->fetchingErrorMessageTitle
,
69 $this->fetchingErrorMessage
72 $this->logHandler
= new TestHandler();
73 $logger = new Logger('test', [$this->logHandler
]);
74 $wallabag->setLogger($logger);
76 if (false === $unsetUser) {
77 $wallabag->setUser($this->user
);
83 public function testInit()
85 $wallabagV1Import = $this->getWallabagV1Import();
87 $this->assertEquals('wallabag v1', $wallabagV1Import->getName());
88 $this->assertNotEmpty($wallabagV1Import->getUrl());
89 $this->assertEquals('import.wallabag_v1.description', $wallabagV1Import->getDescription());
92 public function testImport()
94 $wallabagV1Import = $this->getWallabagV1Import(false, 1);
95 $wallabagV1Import->setFilepath(__DIR__
.'/../fixtures/wallabag-v1.json');
97 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
98 ->disableOriginalConstructor()
101 $entryRepo->expects($this->exactly(2))
102 ->method('findByUrlAndUserId')
103 ->will($this->onConsecutiveCalls(false, true, false, false));
106 ->expects($this->any())
107 ->method('getRepository')
108 ->willReturn($entryRepo);
110 $entry = $this->getMockBuilder('Wallabag\CoreBundle\Entity\Entry')
111 ->disableOriginalConstructor()
115 ->expects($this->exactly(1))
116 ->method('importEntry')
117 ->willReturn($entry);
119 $res = $wallabagV1Import->import();
121 $this->assertTrue($res);
122 $this->assertEquals(['skipped' => 1, 'imported' => 1, 'queued' => 0], $wallabagV1Import->getSummary());
125 public function testImportAndMarkAllAsRead()
127 $wallabagV1Import = $this->getWallabagV1Import(false, 3);
128 $wallabagV1Import->setFilepath(__DIR__
.'/../fixtures/wallabag-v1-read.json');
130 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
131 ->disableOriginalConstructor()
134 $entryRepo->expects($this->exactly(3))
135 ->method('findByUrlAndUserId')
136 ->will($this->onConsecutiveCalls(false, false, false));
139 ->expects($this->any())
140 ->method('getRepository')
141 ->willReturn($entryRepo);
144 ->expects($this->exactly(3))
145 ->method('importEntry')
146 ->willReturn(new Entry($this->user
));
148 // check that every entry persisted are archived
150 ->expects($this->any())
152 ->with($this->callback(function ($persistedEntry) {
153 return $persistedEntry->isArchived();
156 $res = $wallabagV1Import->setMarkAsRead(true)->import();
158 $this->assertTrue($res);
160 $this->assertEquals(['skipped' => 0, 'imported' => 3, 'queued' => 0], $wallabagV1Import->getSummary());
163 public function testImportWithRabbit()
165 $wallabagV1Import = $this->getWallabagV1Import();
166 $wallabagV1Import->setFilepath(__DIR__
.'/../fixtures/wallabag-v1.json');
168 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
169 ->disableOriginalConstructor()
172 $entryRepo->expects($this->never())
173 ->method('findByUrlAndUserId');
176 ->expects($this->never())
177 ->method('getRepository');
179 $entry = $this->getMockBuilder('Wallabag\CoreBundle\Entity\Entry')
180 ->disableOriginalConstructor()
184 ->expects($this->never())
185 ->method('importEntry');
187 $producer = $this->getMockBuilder('OldSound\RabbitMqBundle\RabbitMq\Producer')
188 ->disableOriginalConstructor()
192 ->expects($this->exactly(2))
195 $wallabagV1Import->setProducer($producer);
197 $res = $wallabagV1Import->setMarkAsRead(true)->import();
199 $this->assertTrue($res);
200 $this->assertEquals(['skipped' => 0, 'imported' => 0, 'queued' => 2], $wallabagV1Import->getSummary());
203 public function testImportWithRedis()
205 $wallabagV1Import = $this->getWallabagV1Import();
206 $wallabagV1Import->setFilepath(__DIR__
.'/../fixtures/wallabag-v1.json');
208 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
209 ->disableOriginalConstructor()
212 $entryRepo->expects($this->never())
213 ->method('findByUrlAndUserId');
216 ->expects($this->never())
217 ->method('getRepository');
219 $entry = $this->getMockBuilder('Wallabag\CoreBundle\Entity\Entry')
220 ->disableOriginalConstructor()
224 ->expects($this->never())
225 ->method('importEntry');
227 $factory = new RedisMockFactory();
228 $redisMock = $factory->getAdapter('Predis\Client', true);
230 $queue = new RedisQueue($redisMock, 'wallabag_v1');
231 $producer = new Producer($queue);
233 $wallabagV1Import->setProducer($producer);
235 $res = $wallabagV1Import->setMarkAsRead(true)->import();
237 $this->assertTrue($res);
238 $this->assertEquals(['skipped' => 0, 'imported' => 0, 'queued' => 2], $wallabagV1Import->getSummary());
240 $this->assertNotEmpty($redisMock->lpop('wallabag_v1'));
243 public function testImportBadFile()
245 $wallabagV1Import = $this->getWallabagV1Import();
246 $wallabagV1Import->setFilepath(__DIR__
.'/../fixtures/wallabag-v1.jsonx');
248 $res = $wallabagV1Import->import();
250 $this->assertFalse($res);
252 $records = $this->logHandler
->getRecords();
253 $this->assertContains('WallabagImport: unable to read file', $records[0]['message']);
254 $this->assertEquals('ERROR', $records[0]['level_name']);
257 public function testImportUserNotDefined()
259 $wallabagV1Import = $this->getWallabagV1Import(true);
260 $wallabagV1Import->setFilepath(__DIR__
.'/../fixtures/wallabag-v1.json');
262 $res = $wallabagV1Import->import();
264 $this->assertFalse($res);
266 $records = $this->logHandler
->getRecords();
267 $this->assertContains('WallabagImport: user is not defined', $records[0]['message']);
268 $this->assertEquals('ERROR', $records[0]['level_name']);