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