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