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\FirefoxImport
;
12 use Wallabag\ImportBundle\Redis\Producer
;
13 use Wallabag\UserBundle\Entity\User
;
15 class FirefoxImportTest
extends TestCase
19 protected $logHandler;
20 protected $contentProxy;
21 protected $tagsAssigner;
23 public function testInit()
25 $firefoxImport = $this->getFirefoxImport();
27 $this->assertSame('Firefox', $firefoxImport->getName());
28 $this->assertNotEmpty($firefoxImport->getUrl());
29 $this->assertSame('import.firefox.description', $firefoxImport->getDescription());
32 public function testImport()
34 $firefoxImport = $this->getFirefoxImport(false, 2);
35 $firefoxImport->setFilepath(__DIR__
. '/../fixtures/firefox-bookmarks.json');
37 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
38 ->disableOriginalConstructor()
41 $entryRepo->expects($this->exactly(2))
42 ->method('findByUrlAndUserId')
46 ->expects($this->any())
47 ->method('getRepository')
48 ->willReturn($entryRepo);
50 $entry = $this->getMockBuilder('Wallabag\CoreBundle\Entity\Entry')
51 ->disableOriginalConstructor()
55 ->expects($this->exactly(2))
56 ->method('updateEntry')
59 $res = $firefoxImport->import();
61 $this->assertTrue($res);
62 $this->assertSame(['skipped' => 0, 'imported' => 2, 'queued' => 0], $firefoxImport->getSummary());
65 public function testImportAndMarkAllAsRead()
67 $firefoxImport = $this->getFirefoxImport(false, 1);
68 $firefoxImport->setFilepath(__DIR__
. '/../fixtures/firefox-bookmarks.json');
70 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
71 ->disableOriginalConstructor()
74 $entryRepo->expects($this->exactly(2))
75 ->method('findByUrlAndUserId')
76 ->will($this->onConsecutiveCalls(false, true));
79 ->expects($this->any())
80 ->method('getRepository')
81 ->willReturn($entryRepo);
84 ->expects($this->exactly(1))
85 ->method('updateEntry')
86 ->willReturn(new Entry($this->user
));
88 // check that every entry persisted are archived
90 ->expects($this->any())
92 ->with($this->callback(function ($persistedEntry) {
93 return $persistedEntry->isArchived();
96 $res = $firefoxImport->setMarkAsRead(true)->import();
98 $this->assertTrue($res);
100 $this->assertSame(['skipped' => 1, 'imported' => 1, 'queued' => 0], $firefoxImport->getSummary());
103 public function testImportWithRabbit()
105 $firefoxImport = $this->getFirefoxImport();
106 $firefoxImport->setFilepath(__DIR__
. '/../fixtures/firefox-bookmarks.json');
108 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
109 ->disableOriginalConstructor()
112 $entryRepo->expects($this->never())
113 ->method('findByUrlAndUserId');
116 ->expects($this->never())
117 ->method('getRepository');
119 $entry = $this->getMockBuilder('Wallabag\CoreBundle\Entity\Entry')
120 ->disableOriginalConstructor()
124 ->expects($this->never())
125 ->method('updateEntry');
127 $producer = $this->getMockBuilder('OldSound\RabbitMqBundle\RabbitMq\Producer')
128 ->disableOriginalConstructor()
132 ->expects($this->exactly(1))
135 $firefoxImport->setProducer($producer);
137 $res = $firefoxImport->setMarkAsRead(true)->import();
139 $this->assertTrue($res);
140 $this->assertSame(['skipped' => 0, 'imported' => 0, 'queued' => 1], $firefoxImport->getSummary());
143 public function testImportWithRedis()
145 $firefoxImport = $this->getFirefoxImport();
146 $firefoxImport->setFilepath(__DIR__
. '/../fixtures/firefox-bookmarks.json');
148 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
149 ->disableOriginalConstructor()
152 $entryRepo->expects($this->never())
153 ->method('findByUrlAndUserId');
156 ->expects($this->never())
157 ->method('getRepository');
159 $entry = $this->getMockBuilder('Wallabag\CoreBundle\Entity\Entry')
160 ->disableOriginalConstructor()
164 ->expects($this->never())
165 ->method('updateEntry');
167 $factory = new RedisMockFactory();
168 $redisMock = $factory->getAdapter('Predis\Client', true);
170 $queue = new RedisQueue($redisMock, 'firefox');
171 $producer = new Producer($queue);
173 $firefoxImport->setProducer($producer);
175 $res = $firefoxImport->setMarkAsRead(true)->import();
177 $this->assertTrue($res);
178 $this->assertSame(['skipped' => 0, 'imported' => 0, 'queued' => 1], $firefoxImport->getSummary());
180 $this->assertNotEmpty($redisMock->lpop('firefox'));
183 public function testImportBadFile()
185 $firefoxImport = $this->getFirefoxImport();
186 $firefoxImport->setFilepath(__DIR__
. '/../fixtures/wallabag-v1.jsonx');
188 $res = $firefoxImport->import();
190 $this->assertFalse($res);
192 $records = $this->logHandler
->getRecords();
193 $this->assertContains('Wallabag Browser Import: unable to read file', $records[0]['message']);
194 $this->assertSame('ERROR', $records[0]['level_name']);
197 public function testImportUserNotDefined()
199 $firefoxImport = $this->getFirefoxImport(true);
200 $firefoxImport->setFilepath(__DIR__
. '/../fixtures/firefox-bookmarks.json');
202 $res = $firefoxImport->import();
204 $this->assertFalse($res);
206 $records = $this->logHandler
->getRecords();
207 $this->assertContains('Wallabag Browser Import: user is not defined', $records[0]['message']);
208 $this->assertSame('ERROR', $records[0]['level_name']);
211 private function getFirefoxImport($unsetUser = false, $dispatched = 0)
213 $this->user
= new User();
215 $this->em
= $this->getMockBuilder('Doctrine\ORM\EntityManager')
216 ->disableOriginalConstructor()
219 $this->contentProxy
= $this->getMockBuilder('Wallabag\CoreBundle\Helper\ContentProxy')
220 ->disableOriginalConstructor()
223 $this->tagsAssigner
= $this->getMockBuilder('Wallabag\CoreBundle\Helper\TagsAssigner')
224 ->disableOriginalConstructor()
227 $dispatcher = $this->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcher')
228 ->disableOriginalConstructor()
232 ->expects($this->exactly($dispatched))
233 ->method('dispatch');
235 $wallabag = new FirefoxImport($this->em
, $this->contentProxy
, $this->tagsAssigner
, $dispatcher);
237 $this->logHandler
= new TestHandler();
238 $logger = new Logger('test', [$this->logHandler
]);
239 $wallabag->setLogger($logger);
241 if (false === $unsetUser) {
242 $wallabag->setUser($this->user
);