]>
Commit | Line | Data |
---|---|---|
6785f4aa NL |
1 | <?php |
2 | ||
23634d5d | 3 | namespace Tests\Wallabag\ImportBundle\Import; |
6785f4aa NL |
4 | |
5 | use Wallabag\ImportBundle\Import\WallabagV2Import; | |
6 | use Wallabag\UserBundle\Entity\User; | |
8f336fda | 7 | use Wallabag\CoreBundle\Entity\Entry; |
6785f4aa NL |
8 | use Monolog\Logger; |
9 | use Monolog\Handler\TestHandler; | |
10 | ||
11 | class WallabagV2ImportTest extends \PHPUnit_Framework_TestCase | |
12 | { | |
13 | protected $user; | |
14 | protected $em; | |
15 | protected $logHandler; | |
0783c99a | 16 | protected $contentProxy; |
6785f4aa NL |
17 | |
18 | private function getWallabagV2Import($unsetUser = false) | |
19 | { | |
20 | $this->user = new User(); | |
21 | ||
22 | $this->em = $this->getMockBuilder('Doctrine\ORM\EntityManager') | |
23 | ->disableOriginalConstructor() | |
24 | ->getMock(); | |
25 | ||
0783c99a TC |
26 | $this->contentProxy = $this->getMockBuilder('Wallabag\CoreBundle\Helper\ContentProxy') |
27 | ->disableOriginalConstructor() | |
28 | ->getMock(); | |
29 | ||
da0a9e01 | 30 | $wallabag = new WallabagV2Import($this->em, $this->contentProxy); |
6785f4aa NL |
31 | |
32 | $this->logHandler = new TestHandler(); | |
4094ea47 | 33 | $logger = new Logger('test', [$this->logHandler]); |
6785f4aa NL |
34 | $wallabag->setLogger($logger); |
35 | ||
36 | if (false === $unsetUser) { | |
37 | $wallabag->setUser($this->user); | |
38 | } | |
39 | ||
40 | return $wallabag; | |
41 | } | |
42 | ||
43 | public function testInit() | |
44 | { | |
45 | $wallabagV2Import = $this->getWallabagV2Import(); | |
46 | ||
47 | $this->assertEquals('wallabag v2', $wallabagV2Import->getName()); | |
48 | $this->assertNotEmpty($wallabagV2Import->getUrl()); | |
0d42217e | 49 | $this->assertEquals('import.wallabag_v2.description', $wallabagV2Import->getDescription()); |
6785f4aa NL |
50 | } |
51 | ||
52 | public function testImport() | |
53 | { | |
54 | $wallabagV2Import = $this->getWallabagV2Import(); | |
55 | $wallabagV2Import->setFilepath(__DIR__.'/../fixtures/wallabag-v2.json'); | |
56 | ||
57 | $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository') | |
58 | ->disableOriginalConstructor() | |
59 | ->getMock(); | |
60 | ||
23d24b17 | 61 | $entryRepo->expects($this->exactly(24)) |
6785f4aa NL |
62 | ->method('findByUrlAndUserId') |
63 | ->will($this->onConsecutiveCalls(false, true, false)); | |
64 | ||
65 | $this->em | |
66 | ->expects($this->any()) | |
67 | ->method('getRepository') | |
68 | ->willReturn($entryRepo); | |
69 | ||
8f336fda JB |
70 | $this->contentProxy |
71 | ->expects($this->exactly(2)) | |
72 | ->method('updateEntry') | |
73 | ->willReturn(new Entry($this->user)); | |
74 | ||
6785f4aa NL |
75 | $res = $wallabagV2Import->import(); |
76 | ||
77 | $this->assertTrue($res); | |
23d24b17 | 78 | $this->assertEquals(['skipped' => 22, 'imported' => 2], $wallabagV2Import->getSummary()); |
c32ae320 TC |
79 | } |
80 | ||
81 | public function testImportAndMarkAllAsRead() | |
82 | { | |
83 | $wallabagV2Import = $this->getWallabagV2Import(); | |
84 | $wallabagV2Import->setFilepath(__DIR__.'/../fixtures/wallabag-v2-read.json'); | |
85 | ||
86 | $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository') | |
87 | ->disableOriginalConstructor() | |
88 | ->getMock(); | |
89 | ||
90 | $entryRepo->expects($this->exactly(2)) | |
91 | ->method('findByUrlAndUserId') | |
92 | ->will($this->onConsecutiveCalls(false, false)); | |
93 | ||
94 | $this->em | |
95 | ->expects($this->any()) | |
96 | ->method('getRepository') | |
97 | ->willReturn($entryRepo); | |
98 | ||
8f336fda JB |
99 | $this->contentProxy |
100 | ->expects($this->exactly(2)) | |
101 | ->method('updateEntry') | |
102 | ->willReturn(new Entry($this->user)); | |
103 | ||
79d0e38e JB |
104 | // check that every entry persisted are archived |
105 | $this->em | |
106 | ->expects($this->any()) | |
107 | ->method('persist') | |
cebb4223 | 108 | ->with($this->callback(function ($persistedEntry) { |
79d0e38e JB |
109 | return $persistedEntry->isArchived(); |
110 | })); | |
111 | ||
c32ae320 TC |
112 | $res = $wallabagV2Import->setMarkAsRead(true)->import(); |
113 | ||
114 | $this->assertTrue($res); | |
115 | ||
c32ae320 | 116 | $this->assertEquals(['skipped' => 0, 'imported' => 2], $wallabagV2Import->getSummary()); |
6785f4aa NL |
117 | } |
118 | ||
13470c35 JB |
119 | public function testImportWithRabbit() |
120 | { | |
121 | $wallabagV2Import = $this->getWallabagV2Import(); | |
122 | $wallabagV2Import->setFilepath(__DIR__.'/../fixtures/wallabag-v2.json'); | |
123 | ||
124 | $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository') | |
125 | ->disableOriginalConstructor() | |
126 | ->getMock(); | |
127 | ||
128 | $entryRepo->expects($this->never()) | |
129 | ->method('findByUrlAndUserId'); | |
130 | ||
131 | $this->em | |
132 | ->expects($this->never()) | |
133 | ->method('getRepository'); | |
134 | ||
135 | $this->contentProxy | |
136 | ->expects($this->never()) | |
137 | ->method('updateEntry'); | |
138 | ||
139 | $producer = $this->getMockBuilder('OldSound\RabbitMqBundle\RabbitMq\Producer') | |
140 | ->disableOriginalConstructor() | |
141 | ->getMock(); | |
142 | ||
143 | $producer | |
144 | ->expects($this->exactly(24)) | |
145 | ->method('publish'); | |
146 | ||
147 | $wallabagV2Import->setRabbitmqProducer($producer); | |
148 | ||
149 | $res = $wallabagV2Import->setMarkAsRead(true)->import(); | |
150 | ||
151 | $this->assertTrue($res); | |
152 | $this->assertEquals(['skipped' => 0, 'imported' => 24], $wallabagV2Import->getSummary()); | |
153 | } | |
154 | ||
6785f4aa NL |
155 | public function testImportBadFile() |
156 | { | |
157 | $wallabagV1Import = $this->getWallabagV2Import(); | |
158 | $wallabagV1Import->setFilepath(__DIR__.'/../fixtures/wallabag-v2.jsonx'); | |
159 | ||
160 | $res = $wallabagV1Import->import(); | |
161 | ||
162 | $this->assertFalse($res); | |
163 | ||
164 | $records = $this->logHandler->getRecords(); | |
165 | $this->assertContains('WallabagImport: unable to read file', $records[0]['message']); | |
166 | $this->assertEquals('ERROR', $records[0]['level_name']); | |
167 | } | |
168 | ||
169 | public function testImportUserNotDefined() | |
170 | { | |
171 | $wallabagV1Import = $this->getWallabagV2Import(true); | |
172 | $wallabagV1Import->setFilepath(__DIR__.'/../fixtures/wallabag-v2.json'); | |
173 | ||
174 | $res = $wallabagV1Import->import(); | |
175 | ||
176 | $this->assertFalse($res); | |
177 | ||
178 | $records = $this->logHandler->getRecords(); | |
179 | $this->assertContains('WallabagImport: user is not defined', $records[0]['message']); | |
180 | $this->assertEquals('ERROR', $records[0]['level_name']); | |
181 | } | |
19d9efab JB |
182 | |
183 | public function testImportEmptyFile() | |
184 | { | |
185 | $wallabagV2Import = $this->getWallabagV2Import(); | |
186 | $wallabagV2Import->setFilepath(__DIR__.'/../fixtures/wallabag-v2-empty.json'); | |
187 | ||
188 | $res = $wallabagV2Import->import(); | |
189 | ||
190 | $this->assertFalse($res); | |
191 | $this->assertEquals(['skipped' => 0, 'imported' => 0], $wallabagV2Import->getSummary()); | |
192 | } | |
193 | ||
194 | public function testImportWithExceptionFromGraby() | |
195 | { | |
196 | $wallabagV2Import = $this->getWallabagV2Import(); | |
197 | $wallabagV2Import->setFilepath(__DIR__.'/../fixtures/wallabag-v2.json'); | |
198 | ||
199 | $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository') | |
200 | ->disableOriginalConstructor() | |
201 | ->getMock(); | |
202 | ||
203 | $entryRepo->expects($this->exactly(24)) | |
204 | ->method('findByUrlAndUserId') | |
205 | ->will($this->onConsecutiveCalls(false, true, false)); | |
206 | ||
207 | $this->em | |
208 | ->expects($this->any()) | |
209 | ->method('getRepository') | |
210 | ->willReturn($entryRepo); | |
211 | ||
212 | $this->contentProxy | |
213 | ->expects($this->exactly(2)) | |
214 | ->method('updateEntry') | |
215 | ->will($this->throwException(new \Exception())); | |
216 | ||
217 | $res = $wallabagV2Import->import(); | |
218 | ||
219 | $this->assertTrue($res); | |
220 | $this->assertEquals(['skipped' => 24, 'imported' => 0], $wallabagV2Import->getSummary()); | |
221 | } | |
6785f4aa | 222 | } |