3 namespace Tests\Wallabag\ImportBundle\Import
;
5 use Wallabag\ImportBundle\Import\FirefoxImport
;
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 FirefoxImportTest
extends \PHPUnit_Framework_TestCase
18 protected $logHandler;
19 protected $contentProxy;
20 protected $tagsAssigner;
22 private function getFirefoxImport($unsetUser = false, $dispatched = 0)
24 $this->user
= new User();
26 $this->em
= $this->getMockBuilder('Doctrine\ORM\EntityManager')
27 ->disableOriginalConstructor()
30 $this->contentProxy
= $this->getMockBuilder('Wallabag\CoreBundle\Helper\ContentProxy')
31 ->disableOriginalConstructor()
34 $this->tagsAssigner
= $this->getMockBuilder('Wallabag\CoreBundle\Helper\TagsAssigner')
35 ->disableOriginalConstructor()
38 $dispatcher = $this->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcher')
39 ->disableOriginalConstructor()
43 ->expects($this->exactly($dispatched))
46 $wallabag = new FirefoxImport($this->em
, $this->contentProxy
, $this->tagsAssigner
, $dispatcher);
48 $this->logHandler
= new TestHandler();
49 $logger = new Logger('test', [$this->logHandler
]);
50 $wallabag->setLogger($logger);
52 if (false === $unsetUser) {
53 $wallabag->setUser($this->user
);
59 public function testInit()
61 $firefoxImport = $this->getFirefoxImport();
63 $this->assertEquals('Firefox', $firefoxImport->getName());
64 $this->assertNotEmpty($firefoxImport->getUrl());
65 $this->assertEquals('import.firefox.description', $firefoxImport->getDescription());
68 public function testImport()
70 $firefoxImport = $this->getFirefoxImport(false, 2);
71 $firefoxImport->setFilepath(__DIR__
.'/../fixtures/firefox-bookmarks.json');
73 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
74 ->disableOriginalConstructor()
77 $entryRepo->expects($this->exactly(2))
78 ->method('findByUrlAndUserId')
82 ->expects($this->any())
83 ->method('getRepository')
84 ->willReturn($entryRepo);
86 $entry = $this->getMockBuilder('Wallabag\CoreBundle\Entity\Entry')
87 ->disableOriginalConstructor()
91 ->expects($this->exactly(2))
92 ->method('updateEntry')
95 $res = $firefoxImport->import();
97 $this->assertTrue($res);
98 $this->assertEquals(['skipped' => 0, 'imported' => 2, 'queued' => 0], $firefoxImport->getSummary());
101 public function testImportAndMarkAllAsRead()
103 $firefoxImport = $this->getFirefoxImport(false, 1);
104 $firefoxImport->setFilepath(__DIR__
.'/../fixtures/firefox-bookmarks.json');
106 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
107 ->disableOriginalConstructor()
110 $entryRepo->expects($this->exactly(2))
111 ->method('findByUrlAndUserId')
112 ->will($this->onConsecutiveCalls(false, true));
115 ->expects($this->any())
116 ->method('getRepository')
117 ->willReturn($entryRepo);
120 ->expects($this->exactly(1))
121 ->method('updateEntry')
122 ->willReturn(new Entry($this->user
));
124 // check that every entry persisted are archived
126 ->expects($this->any())
128 ->with($this->callback(function ($persistedEntry) {
129 return $persistedEntry->isArchived();
132 $res = $firefoxImport->setMarkAsRead(true)->import();
134 $this->assertTrue($res);
136 $this->assertEquals(['skipped' => 1, 'imported' => 1, 'queued' => 0], $firefoxImport->getSummary());
139 public function testImportWithRabbit()
141 $firefoxImport = $this->getFirefoxImport();
142 $firefoxImport->setFilepath(__DIR__
.'/../fixtures/firefox-bookmarks.json');
144 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
145 ->disableOriginalConstructor()
148 $entryRepo->expects($this->never())
149 ->method('findByUrlAndUserId');
152 ->expects($this->never())
153 ->method('getRepository');
155 $entry = $this->getMockBuilder('Wallabag\CoreBundle\Entity\Entry')
156 ->disableOriginalConstructor()
160 ->expects($this->never())
161 ->method('updateEntry');
163 $producer = $this->getMockBuilder('OldSound\RabbitMqBundle\RabbitMq\Producer')
164 ->disableOriginalConstructor()
168 ->expects($this->exactly(1))
171 $firefoxImport->setProducer($producer);
173 $res = $firefoxImport->setMarkAsRead(true)->import();
175 $this->assertTrue($res);
176 $this->assertEquals(['skipped' => 0, 'imported' => 0, 'queued' => 1], $firefoxImport->getSummary());
179 public function testImportWithRedis()
181 $firefoxImport = $this->getFirefoxImport();
182 $firefoxImport->setFilepath(__DIR__
.'/../fixtures/firefox-bookmarks.json');
184 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
185 ->disableOriginalConstructor()
188 $entryRepo->expects($this->never())
189 ->method('findByUrlAndUserId');
192 ->expects($this->never())
193 ->method('getRepository');
195 $entry = $this->getMockBuilder('Wallabag\CoreBundle\Entity\Entry')
196 ->disableOriginalConstructor()
200 ->expects($this->never())
201 ->method('updateEntry');
203 $factory = new RedisMockFactory();
204 $redisMock = $factory->getAdapter('Predis\Client', true);
206 $queue = new RedisQueue($redisMock, 'firefox');
207 $producer = new Producer($queue);
209 $firefoxImport->setProducer($producer);
211 $res = $firefoxImport->setMarkAsRead(true)->import();
213 $this->assertTrue($res);
214 $this->assertEquals(['skipped' => 0, 'imported' => 0, 'queued' => 1], $firefoxImport->getSummary());
216 $this->assertNotEmpty($redisMock->lpop('firefox'));
219 public function testImportBadFile()
221 $firefoxImport = $this->getFirefoxImport();
222 $firefoxImport->setFilepath(__DIR__
.'/../fixtures/wallabag-v1.jsonx');
224 $res = $firefoxImport->import();
226 $this->assertFalse($res);
228 $records = $this->logHandler
->getRecords();
229 $this->assertContains('Wallabag Browser Import: unable to read file', $records[0]['message']);
230 $this->assertEquals('ERROR', $records[0]['level_name']);
233 public function testImportUserNotDefined()
235 $firefoxImport = $this->getFirefoxImport(true);
236 $firefoxImport->setFilepath(__DIR__
.'/../fixtures/firefox-bookmarks.json');
238 $res = $firefoxImport->import();
240 $this->assertFalse($res);
242 $records = $this->logHandler
->getRecords();
243 $this->assertContains('Wallabag Browser Import: user is not defined', $records[0]['message']);
244 $this->assertEquals('ERROR', $records[0]['level_name']);