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\ChromeImport
;
12 use Wallabag\ImportBundle\Redis\Producer
;
13 use Wallabag\UserBundle\Entity\User
;
15 class ChromeImportTest
extends TestCase
19 protected $logHandler;
20 protected $contentProxy;
21 protected $tagsAssigner;
23 public function testInit()
25 $chromeImport = $this->getChromeImport();
27 $this->assertSame('Chrome', $chromeImport->getName());
28 $this->assertNotEmpty($chromeImport->getUrl());
29 $this->assertSame('import.chrome.description', $chromeImport->getDescription());
32 public function testImport()
34 $chromeImport = $this->getChromeImport(false, 1);
35 $chromeImport->setFilepath(__DIR__
. '/../fixtures/chrome-bookmarks');
37 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
38 ->disableOriginalConstructor()
41 $entryRepo->expects($this->exactly(1))
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(1))
56 ->method('updateEntry')
59 $res = $chromeImport->import();
61 $this->assertTrue($res);
62 $this->assertSame(['skipped' => 0, 'imported' => 1, 'queued' => 0], $chromeImport->getSummary());
65 public function testImportAndMarkAllAsRead()
67 $chromeImport = $this->getChromeImport(false, 1);
68 $chromeImport->setFilepath(__DIR__
. '/../fixtures/chrome-bookmarks');
70 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
71 ->disableOriginalConstructor()
74 $entryRepo->expects($this->exactly(1))
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 = $chromeImport->setMarkAsRead(true)->import();
98 $this->assertTrue($res);
100 $this->assertSame(['skipped' => 0, 'imported' => 1, 'queued' => 0], $chromeImport->getSummary());
103 public function testImportWithRabbit()
105 $chromeImport = $this->getChromeImport();
106 $chromeImport->setFilepath(__DIR__
. '/../fixtures/chrome-bookmarks');
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 $chromeImport->setProducer($producer);
137 $res = $chromeImport->setMarkAsRead(true)->import();
139 $this->assertTrue($res);
140 $this->assertSame(['skipped' => 0, 'imported' => 0, 'queued' => 1], $chromeImport->getSummary());
143 public function testImportWithRedis()
145 $chromeImport = $this->getChromeImport();
146 $chromeImport->setFilepath(__DIR__
. '/../fixtures/chrome-bookmarks');
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, 'chrome');
171 $producer = new Producer($queue);
173 $chromeImport->setProducer($producer);
175 $res = $chromeImport->setMarkAsRead(true)->import();
177 $this->assertTrue($res);
178 $this->assertSame(['skipped' => 0, 'imported' => 0, 'queued' => 1], $chromeImport->getSummary());
180 $this->assertNotEmpty($redisMock->lpop('chrome'));
183 public function testImportBadFile()
185 $chromeImport = $this->getChromeImport();
186 $chromeImport->setFilepath(__DIR__
. '/../fixtures/wallabag-v1.jsonx');
188 $res = $chromeImport->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 $chromeImport = $this->getChromeImport(true);
200 $chromeImport->setFilepath(__DIR__
. '/../fixtures/chrome-bookmarks');
202 $res = $chromeImport->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 getChromeImport($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 ChromeImport($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
);