]> git.immae.eu Git - github/wallabag/wallabag.git/blob - tests/Wallabag/ImportBundle/Import/ChromeImportTest.php
Merge pull request #4232 from wallabag/dependabot/composer/php-http/mock-client-1.3.1
[github/wallabag/wallabag.git] / tests / Wallabag / ImportBundle / Import / ChromeImportTest.php
1 <?php
2
3 namespace Tests\Wallabag\ImportBundle\Import;
4
5 use M6Web\Component\RedisMock\RedisMockFactory;
6 use Monolog\Handler\TestHandler;
7 use Monolog\Logger;
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;
14
15 class ChromeImportTest extends TestCase
16 {
17 protected $user;
18 protected $em;
19 protected $logHandler;
20 protected $contentProxy;
21 protected $tagsAssigner;
22
23 public function testInit()
24 {
25 $chromeImport = $this->getChromeImport();
26
27 $this->assertSame('Chrome', $chromeImport->getName());
28 $this->assertNotEmpty($chromeImport->getUrl());
29 $this->assertSame('import.chrome.description', $chromeImport->getDescription());
30 }
31
32 public function testImport()
33 {
34 $chromeImport = $this->getChromeImport(false, 1);
35 $chromeImport->setFilepath(__DIR__ . '/../fixtures/chrome-bookmarks');
36
37 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
38 ->disableOriginalConstructor()
39 ->getMock();
40
41 $entryRepo->expects($this->exactly(1))
42 ->method('findByUrlAndUserId')
43 ->willReturn(false);
44
45 $this->em
46 ->expects($this->any())
47 ->method('getRepository')
48 ->willReturn($entryRepo);
49
50 $entry = $this->getMockBuilder('Wallabag\CoreBundle\Entity\Entry')
51 ->disableOriginalConstructor()
52 ->getMock();
53
54 $this->contentProxy
55 ->expects($this->exactly(1))
56 ->method('updateEntry')
57 ->willReturn($entry);
58
59 $res = $chromeImport->import();
60
61 $this->assertTrue($res);
62 $this->assertSame(['skipped' => 0, 'imported' => 1, 'queued' => 0], $chromeImport->getSummary());
63 }
64
65 public function testImportAndMarkAllAsRead()
66 {
67 $chromeImport = $this->getChromeImport(false, 1);
68 $chromeImport->setFilepath(__DIR__ . '/../fixtures/chrome-bookmarks');
69
70 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
71 ->disableOriginalConstructor()
72 ->getMock();
73
74 $entryRepo->expects($this->exactly(1))
75 ->method('findByUrlAndUserId')
76 ->will($this->onConsecutiveCalls(false, true));
77
78 $this->em
79 ->expects($this->any())
80 ->method('getRepository')
81 ->willReturn($entryRepo);
82
83 $this->contentProxy
84 ->expects($this->exactly(1))
85 ->method('updateEntry')
86 ->willReturn(new Entry($this->user));
87
88 // check that every entry persisted are archived
89 $this->em
90 ->expects($this->any())
91 ->method('persist')
92 ->with($this->callback(function ($persistedEntry) {
93 return $persistedEntry->isArchived();
94 }));
95
96 $res = $chromeImport->setMarkAsRead(true)->import();
97
98 $this->assertTrue($res);
99
100 $this->assertSame(['skipped' => 0, 'imported' => 1, 'queued' => 0], $chromeImport->getSummary());
101 }
102
103 public function testImportWithRabbit()
104 {
105 $chromeImport = $this->getChromeImport();
106 $chromeImport->setFilepath(__DIR__ . '/../fixtures/chrome-bookmarks');
107
108 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
109 ->disableOriginalConstructor()
110 ->getMock();
111
112 $entryRepo->expects($this->never())
113 ->method('findByUrlAndUserId');
114
115 $this->em
116 ->expects($this->never())
117 ->method('getRepository');
118
119 $entry = $this->getMockBuilder('Wallabag\CoreBundle\Entity\Entry')
120 ->disableOriginalConstructor()
121 ->getMock();
122
123 $this->contentProxy
124 ->expects($this->never())
125 ->method('updateEntry');
126
127 $producer = $this->getMockBuilder('OldSound\RabbitMqBundle\RabbitMq\Producer')
128 ->disableOriginalConstructor()
129 ->getMock();
130
131 $producer
132 ->expects($this->exactly(1))
133 ->method('publish');
134
135 $chromeImport->setProducer($producer);
136
137 $res = $chromeImport->setMarkAsRead(true)->import();
138
139 $this->assertTrue($res);
140 $this->assertSame(['skipped' => 0, 'imported' => 0, 'queued' => 1], $chromeImport->getSummary());
141 }
142
143 public function testImportWithRedis()
144 {
145 $chromeImport = $this->getChromeImport();
146 $chromeImport->setFilepath(__DIR__ . '/../fixtures/chrome-bookmarks');
147
148 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
149 ->disableOriginalConstructor()
150 ->getMock();
151
152 $entryRepo->expects($this->never())
153 ->method('findByUrlAndUserId');
154
155 $this->em
156 ->expects($this->never())
157 ->method('getRepository');
158
159 $entry = $this->getMockBuilder('Wallabag\CoreBundle\Entity\Entry')
160 ->disableOriginalConstructor()
161 ->getMock();
162
163 $this->contentProxy
164 ->expects($this->never())
165 ->method('updateEntry');
166
167 $factory = new RedisMockFactory();
168 $redisMock = $factory->getAdapter('Predis\Client', true);
169
170 $queue = new RedisQueue($redisMock, 'chrome');
171 $producer = new Producer($queue);
172
173 $chromeImport->setProducer($producer);
174
175 $res = $chromeImport->setMarkAsRead(true)->import();
176
177 $this->assertTrue($res);
178 $this->assertSame(['skipped' => 0, 'imported' => 0, 'queued' => 1], $chromeImport->getSummary());
179
180 $this->assertNotEmpty($redisMock->lpop('chrome'));
181 }
182
183 public function testImportBadFile()
184 {
185 $chromeImport = $this->getChromeImport();
186 $chromeImport->setFilepath(__DIR__ . '/../fixtures/wallabag-v1.jsonx');
187
188 $res = $chromeImport->import();
189
190 $this->assertFalse($res);
191
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']);
195 }
196
197 public function testImportUserNotDefined()
198 {
199 $chromeImport = $this->getChromeImport(true);
200 $chromeImport->setFilepath(__DIR__ . '/../fixtures/chrome-bookmarks');
201
202 $res = $chromeImport->import();
203
204 $this->assertFalse($res);
205
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']);
209 }
210
211 private function getChromeImport($unsetUser = false, $dispatched = 0)
212 {
213 $this->user = new User();
214
215 $this->em = $this->getMockBuilder('Doctrine\ORM\EntityManager')
216 ->disableOriginalConstructor()
217 ->getMock();
218
219 $this->contentProxy = $this->getMockBuilder('Wallabag\CoreBundle\Helper\ContentProxy')
220 ->disableOriginalConstructor()
221 ->getMock();
222
223 $this->tagsAssigner = $this->getMockBuilder('Wallabag\CoreBundle\Helper\TagsAssigner')
224 ->disableOriginalConstructor()
225 ->getMock();
226
227 $dispatcher = $this->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcher')
228 ->disableOriginalConstructor()
229 ->getMock();
230
231 $dispatcher
232 ->expects($this->exactly($dispatched))
233 ->method('dispatch');
234
235 $wallabag = new ChromeImport($this->em, $this->contentProxy, $this->tagsAssigner, $dispatcher);
236
237 $this->logHandler = new TestHandler();
238 $logger = new Logger('test', [$this->logHandler]);
239 $wallabag->setLogger($logger);
240
241 if (false === $unsetUser) {
242 $wallabag->setUser($this->user);
243 }
244
245 return $wallabag;
246 }
247 }