diff options
Diffstat (limited to 'tests/Wallabag/CoreBundle/Helper')
6 files changed, 581 insertions, 182 deletions
diff --git a/tests/Wallabag/CoreBundle/Helper/ContentProxyTest.php b/tests/Wallabag/CoreBundle/Helper/ContentProxyTest.php index 4f70ed0c..f94c2137 100644 --- a/tests/Wallabag/CoreBundle/Helper/ContentProxyTest.php +++ b/tests/Wallabag/CoreBundle/Helper/ContentProxyTest.php | |||
@@ -2,10 +2,17 @@ | |||
2 | 2 | ||
3 | namespace Tests\Wallabag\CoreBundle\Helper; | 3 | namespace Tests\Wallabag\CoreBundle\Helper; |
4 | 4 | ||
5 | use Graby\Graby; | ||
6 | use Monolog\Handler\TestHandler; | ||
7 | use Monolog\Logger; | ||
5 | use Psr\Log\NullLogger; | 8 | use Psr\Log\NullLogger; |
6 | use Wallabag\CoreBundle\Helper\ContentProxy; | 9 | use Symfony\Component\Validator\ConstraintViolation; |
10 | use Symfony\Component\Validator\ConstraintViolationList; | ||
11 | use Symfony\Component\Validator\Validator\RecursiveValidator; | ||
7 | use Wallabag\CoreBundle\Entity\Entry; | 12 | use Wallabag\CoreBundle\Entity\Entry; |
8 | use Wallabag\CoreBundle\Entity\Tag; | 13 | use Wallabag\CoreBundle\Entity\Tag; |
14 | use Wallabag\CoreBundle\Helper\ContentProxy; | ||
15 | use Wallabag\CoreBundle\Helper\RuleBasedTagger; | ||
9 | use Wallabag\UserBundle\Entity\User; | 16 | use Wallabag\UserBundle\Entity\User; |
10 | 17 | ||
11 | class ContentProxyTest extends \PHPUnit_Framework_TestCase | 18 | class ContentProxyTest extends \PHPUnit_Framework_TestCase |
@@ -33,17 +40,18 @@ class ContentProxyTest extends \PHPUnit_Framework_TestCase | |||
33 | 'language' => '', | 40 | 'language' => '', |
34 | ]); | 41 | ]); |
35 | 42 | ||
36 | $proxy = new ContentProxy($graby, $tagger, $this->getTagRepositoryMock(), $this->getLogger(), $this->fetchingErrorMessage); | 43 | $proxy = new ContentProxy($graby, $tagger, $this->getValidator(), $this->getLogger(), $this->fetchingErrorMessage); |
37 | $entry = $proxy->updateEntry(new Entry(new User()), 'http://user@:80'); | 44 | $entry = new Entry(new User()); |
45 | $proxy->updateEntry($entry, 'http://user@:80'); | ||
38 | 46 | ||
39 | $this->assertEquals('http://user@:80', $entry->getUrl()); | 47 | $this->assertSame('http://user@:80', $entry->getUrl()); |
40 | $this->assertEmpty($entry->getTitle()); | 48 | $this->assertEmpty($entry->getTitle()); |
41 | $this->assertEquals($this->fetchingErrorMessage, $entry->getContent()); | 49 | $this->assertSame($this->fetchingErrorMessage, $entry->getContent()); |
42 | $this->assertEmpty($entry->getPreviewPicture()); | 50 | $this->assertEmpty($entry->getPreviewPicture()); |
43 | $this->assertEmpty($entry->getMimetype()); | 51 | $this->assertEmpty($entry->getMimetype()); |
44 | $this->assertEmpty($entry->getLanguage()); | 52 | $this->assertEmpty($entry->getLanguage()); |
45 | $this->assertEquals(0.0, $entry->getReadingTime()); | 53 | $this->assertSame(0.0, $entry->getReadingTime()); |
46 | $this->assertEquals(false, $entry->getDomainName()); | 54 | $this->assertSame(null, $entry->getDomainName()); |
47 | } | 55 | } |
48 | 56 | ||
49 | public function testWithEmptyContent() | 57 | public function testWithEmptyContent() |
@@ -67,17 +75,18 @@ class ContentProxyTest extends \PHPUnit_Framework_TestCase | |||
67 | 'language' => '', | 75 | 'language' => '', |
68 | ]); | 76 | ]); |
69 | 77 | ||
70 | $proxy = new ContentProxy($graby, $tagger, $this->getTagRepositoryMock(), $this->getLogger(), $this->fetchingErrorMessage); | 78 | $proxy = new ContentProxy($graby, $tagger, $this->getValidator(), $this->getLogger(), $this->fetchingErrorMessage); |
71 | $entry = $proxy->updateEntry(new Entry(new User()), 'http://0.0.0.0'); | 79 | $entry = new Entry(new User()); |
80 | $proxy->updateEntry($entry, 'http://0.0.0.0'); | ||
72 | 81 | ||
73 | $this->assertEquals('http://0.0.0.0', $entry->getUrl()); | 82 | $this->assertSame('http://0.0.0.0', $entry->getUrl()); |
74 | $this->assertEmpty($entry->getTitle()); | 83 | $this->assertEmpty($entry->getTitle()); |
75 | $this->assertEquals($this->fetchingErrorMessage, $entry->getContent()); | 84 | $this->assertSame($this->fetchingErrorMessage, $entry->getContent()); |
76 | $this->assertEmpty($entry->getPreviewPicture()); | 85 | $this->assertEmpty($entry->getPreviewPicture()); |
77 | $this->assertEmpty($entry->getMimetype()); | 86 | $this->assertEmpty($entry->getMimetype()); |
78 | $this->assertEmpty($entry->getLanguage()); | 87 | $this->assertEmpty($entry->getLanguage()); |
79 | $this->assertEquals(0.0, $entry->getReadingTime()); | 88 | $this->assertSame(0.0, $entry->getReadingTime()); |
80 | $this->assertEquals('0.0.0.0', $entry->getDomainName()); | 89 | $this->assertSame('0.0.0.0', $entry->getDomainName()); |
81 | } | 90 | } |
82 | 91 | ||
83 | public function testWithEmptyContentButOG() | 92 | public function testWithEmptyContentButOG() |
@@ -106,18 +115,19 @@ class ContentProxyTest extends \PHPUnit_Framework_TestCase | |||
106 | ], | 115 | ], |
107 | ]); | 116 | ]); |
108 | 117 | ||
109 | $proxy = new ContentProxy($graby, $tagger, $this->getTagRepositoryMock(), $this->getLogger(), $this->fetchingErrorMessage); | 118 | $proxy = new ContentProxy($graby, $tagger, $this->getValidator(), $this->getLogger(), $this->fetchingErrorMessage); |
110 | $entry = $proxy->updateEntry(new Entry(new User()), 'http://domain.io'); | 119 | $entry = new Entry(new User()); |
120 | $proxy->updateEntry($entry, 'http://domain.io'); | ||
111 | 121 | ||
112 | $this->assertEquals('http://domain.io', $entry->getUrl()); | 122 | $this->assertSame('http://domain.io', $entry->getUrl()); |
113 | $this->assertEquals('my title', $entry->getTitle()); | 123 | $this->assertSame('my title', $entry->getTitle()); |
114 | $this->assertEquals($this->fetchingErrorMessage . '<p><i>But we found a short description: </i></p>desc', $entry->getContent()); | 124 | $this->assertSame($this->fetchingErrorMessage . '<p><i>But we found a short description: </i></p>desc', $entry->getContent()); |
115 | $this->assertEmpty($entry->getPreviewPicture()); | 125 | $this->assertEmpty($entry->getPreviewPicture()); |
116 | $this->assertEmpty($entry->getLanguage()); | 126 | $this->assertEmpty($entry->getLanguage()); |
117 | $this->assertEmpty($entry->getHttpStatus()); | 127 | $this->assertEmpty($entry->getHttpStatus()); |
118 | $this->assertEmpty($entry->getMimetype()); | 128 | $this->assertEmpty($entry->getMimetype()); |
119 | $this->assertEquals(0.0, $entry->getReadingTime()); | 129 | $this->assertSame(0.0, $entry->getReadingTime()); |
120 | $this->assertEquals('domain.io', $entry->getDomainName()); | 130 | $this->assertSame('domain.io', $entry->getDomainName()); |
121 | } | 131 | } |
122 | 132 | ||
123 | public function testWithContent() | 133 | public function testWithContent() |
@@ -147,18 +157,19 @@ class ContentProxyTest extends \PHPUnit_Framework_TestCase | |||
147 | ], | 157 | ], |
148 | ]); | 158 | ]); |
149 | 159 | ||
150 | $proxy = new ContentProxy($graby, $tagger, $this->getTagRepositoryMock(), $this->getLogger(), $this->fetchingErrorMessage); | 160 | $proxy = new ContentProxy($graby, $tagger, $this->getValidator(), $this->getLogger(), $this->fetchingErrorMessage); |
151 | $entry = $proxy->updateEntry(new Entry(new User()), 'http://0.0.0.0'); | 161 | $entry = new Entry(new User()); |
162 | $proxy->updateEntry($entry, 'http://0.0.0.0'); | ||
152 | 163 | ||
153 | $this->assertEquals('http://1.1.1.1', $entry->getUrl()); | 164 | $this->assertSame('http://1.1.1.1', $entry->getUrl()); |
154 | $this->assertEquals('this is my title', $entry->getTitle()); | 165 | $this->assertSame('this is my title', $entry->getTitle()); |
155 | $this->assertContains('this is my content', $entry->getContent()); | 166 | $this->assertContains('this is my content', $entry->getContent()); |
156 | $this->assertEquals('http://3.3.3.3/cover.jpg', $entry->getPreviewPicture()); | 167 | $this->assertSame('http://3.3.3.3/cover.jpg', $entry->getPreviewPicture()); |
157 | $this->assertEquals('text/html', $entry->getMimetype()); | 168 | $this->assertSame('text/html', $entry->getMimetype()); |
158 | $this->assertEquals('fr', $entry->getLanguage()); | 169 | $this->assertSame('fr', $entry->getLanguage()); |
159 | $this->assertEquals('200', $entry->getHttpStatus()); | 170 | $this->assertSame('200', $entry->getHttpStatus()); |
160 | $this->assertEquals(4.0, $entry->getReadingTime()); | 171 | $this->assertSame(4.0, $entry->getReadingTime()); |
161 | $this->assertEquals('1.1.1.1', $entry->getDomainName()); | 172 | $this->assertSame('1.1.1.1', $entry->getDomainName()); |
162 | } | 173 | } |
163 | 174 | ||
164 | public function testWithContentAndNoOgImage() | 175 | public function testWithContentAndNoOgImage() |
@@ -184,205 +195,359 @@ class ContentProxyTest extends \PHPUnit_Framework_TestCase | |||
184 | 'open_graph' => [ | 195 | 'open_graph' => [ |
185 | 'og_title' => 'my OG title', | 196 | 'og_title' => 'my OG title', |
186 | 'og_description' => 'OG desc', | 197 | 'og_description' => 'OG desc', |
187 | 'og_image' => false, | 198 | 'og_image' => null, |
188 | ], | 199 | ], |
189 | ]); | 200 | ]); |
190 | 201 | ||
191 | $proxy = new ContentProxy($graby, $tagger, $this->getTagRepositoryMock(), $this->getLogger(), $this->fetchingErrorMessage); | 202 | $proxy = new ContentProxy($graby, $tagger, $this->getValidator(), $this->getLogger(), $this->fetchingErrorMessage); |
192 | $entry = $proxy->updateEntry(new Entry(new User()), 'http://0.0.0.0'); | 203 | $entry = new Entry(new User()); |
204 | $proxy->updateEntry($entry, 'http://0.0.0.0'); | ||
193 | 205 | ||
194 | $this->assertEquals('http://1.1.1.1', $entry->getUrl()); | 206 | $this->assertSame('http://1.1.1.1', $entry->getUrl()); |
195 | $this->assertEquals('this is my title', $entry->getTitle()); | 207 | $this->assertSame('this is my title', $entry->getTitle()); |
196 | $this->assertContains('this is my content', $entry->getContent()); | 208 | $this->assertContains('this is my content', $entry->getContent()); |
197 | $this->assertNull($entry->getPreviewPicture()); | 209 | $this->assertNull($entry->getPreviewPicture()); |
198 | $this->assertEquals('text/html', $entry->getMimetype()); | 210 | $this->assertSame('text/html', $entry->getMimetype()); |
199 | $this->assertEquals('fr', $entry->getLanguage()); | 211 | $this->assertSame('fr', $entry->getLanguage()); |
200 | $this->assertEquals('200', $entry->getHttpStatus()); | 212 | $this->assertSame('200', $entry->getHttpStatus()); |
201 | $this->assertEquals(4.0, $entry->getReadingTime()); | 213 | $this->assertSame(4.0, $entry->getReadingTime()); |
202 | $this->assertEquals('1.1.1.1', $entry->getDomainName()); | 214 | $this->assertSame('1.1.1.1', $entry->getDomainName()); |
203 | } | 215 | } |
204 | 216 | ||
205 | public function testWithForcedContent() | 217 | public function testWithContentAndBadLanguage() |
206 | { | 218 | { |
207 | $tagger = $this->getTaggerMock(); | 219 | $tagger = $this->getTaggerMock(); |
208 | $tagger->expects($this->once()) | 220 | $tagger->expects($this->once()) |
209 | ->method('tag'); | 221 | ->method('tag'); |
210 | 222 | ||
211 | $graby = $this->getMockBuilder('Graby\Graby')->getMock(); | 223 | $validator = $this->getValidator(); |
212 | 224 | $validator->expects($this->once()) | |
213 | $proxy = new ContentProxy($graby, $tagger, $this->getTagRepositoryMock(), $this->getLogger(), $this->fetchingErrorMessage); | 225 | ->method('validate') |
214 | $entry = $proxy->updateEntry(new Entry(new User()), 'http://0.0.0.0', [ | 226 | ->willReturn(new ConstraintViolationList([new ConstraintViolation('oops', 'oops', [], 'oops', 'language', 'dontexist')])); |
215 | 'html' => str_repeat('this is my content', 325), | ||
216 | 'title' => 'this is my title', | ||
217 | 'url' => 'http://1.1.1.1', | ||
218 | 'content_type' => 'text/html', | ||
219 | 'language' => 'fr', | ||
220 | ]); | ||
221 | |||
222 | $this->assertEquals('http://1.1.1.1', $entry->getUrl()); | ||
223 | $this->assertEquals('this is my title', $entry->getTitle()); | ||
224 | $this->assertContains('this is my content', $entry->getContent()); | ||
225 | $this->assertEquals('text/html', $entry->getMimetype()); | ||
226 | $this->assertEquals('fr', $entry->getLanguage()); | ||
227 | $this->assertEquals(4.0, $entry->getReadingTime()); | ||
228 | $this->assertEquals('1.1.1.1', $entry->getDomainName()); | ||
229 | } | ||
230 | 227 | ||
231 | public function testTaggerThrowException() | ||
232 | { | ||
233 | $graby = $this->getMockBuilder('Graby\Graby') | 228 | $graby = $this->getMockBuilder('Graby\Graby') |
229 | ->setMethods(['fetchContent']) | ||
234 | ->disableOriginalConstructor() | 230 | ->disableOriginalConstructor() |
235 | ->getMock(); | 231 | ->getMock(); |
236 | 232 | ||
237 | $tagger = $this->getTaggerMock(); | 233 | $graby->expects($this->any()) |
238 | $tagger->expects($this->once()) | 234 | ->method('fetchContent') |
239 | ->method('tag') | 235 | ->willReturn([ |
240 | ->will($this->throwException(new \Exception())); | 236 | 'html' => str_repeat('this is my content', 325), |
241 | 237 | 'title' => 'this is my title', | |
242 | $tagRepo = $this->getTagRepositoryMock(); | 238 | 'url' => 'http://1.1.1.1', |
243 | $proxy = new ContentProxy($graby, $tagger, $tagRepo, $this->getLogger(), $this->fetchingErrorMessage); | 239 | 'content_type' => 'text/html', |
240 | 'language' => 'dontexist', | ||
241 | 'status' => '200', | ||
242 | ]); | ||
244 | 243 | ||
245 | $entry = $proxy->updateEntry(new Entry(new User()), 'http://0.0.0.0', [ | 244 | $proxy = new ContentProxy($graby, $tagger, $validator, $this->getLogger(), $this->fetchingErrorMessage); |
246 | 'html' => str_repeat('this is my content', 325), | 245 | $entry = new Entry(new User()); |
247 | 'title' => 'this is my title', | 246 | $proxy->updateEntry($entry, 'http://0.0.0.0'); |
248 | 'url' => 'http://1.1.1.1', | ||
249 | 'content_type' => 'text/html', | ||
250 | 'language' => 'fr', | ||
251 | ]); | ||
252 | 247 | ||
253 | $this->assertCount(0, $entry->getTags()); | 248 | $this->assertSame('http://1.1.1.1', $entry->getUrl()); |
249 | $this->assertSame('this is my title', $entry->getTitle()); | ||
250 | $this->assertContains('this is my content', $entry->getContent()); | ||
251 | $this->assertSame('text/html', $entry->getMimetype()); | ||
252 | $this->assertNull($entry->getLanguage()); | ||
253 | $this->assertSame('200', $entry->getHttpStatus()); | ||
254 | $this->assertSame(4.0, $entry->getReadingTime()); | ||
255 | $this->assertSame('1.1.1.1', $entry->getDomainName()); | ||
254 | } | 256 | } |
255 | 257 | ||
256 | public function testAssignTagsWithArrayAndExtraSpaces() | 258 | public function testWithContentAndBadOgImage() |
257 | { | 259 | { |
260 | $tagger = $this->getTaggerMock(); | ||
261 | $tagger->expects($this->once()) | ||
262 | ->method('tag'); | ||
263 | |||
264 | $validator = $this->getValidator(); | ||
265 | $validator->expects($this->exactly(2)) | ||
266 | ->method('validate') | ||
267 | ->will($this->onConsecutiveCalls( | ||
268 | new ConstraintViolationList(), | ||
269 | new ConstraintViolationList([new ConstraintViolation('oops', 'oops', [], 'oops', 'url', 'https://')]) | ||
270 | )); | ||
271 | |||
258 | $graby = $this->getMockBuilder('Graby\Graby') | 272 | $graby = $this->getMockBuilder('Graby\Graby') |
273 | ->setMethods(['fetchContent']) | ||
259 | ->disableOriginalConstructor() | 274 | ->disableOriginalConstructor() |
260 | ->getMock(); | 275 | ->getMock(); |
261 | 276 | ||
262 | $tagRepo = $this->getTagRepositoryMock(); | 277 | $graby->expects($this->any()) |
263 | $proxy = new ContentProxy($graby, $this->getTaggerMock(), $tagRepo, $this->getLogger(), $this->fetchingErrorMessage); | 278 | ->method('fetchContent') |
279 | ->willReturn([ | ||
280 | 'html' => str_repeat('this is my content', 325), | ||
281 | 'title' => 'this is my title', | ||
282 | 'url' => 'http://1.1.1.1', | ||
283 | 'content_type' => 'text/html', | ||
284 | 'language' => 'fr', | ||
285 | 'status' => '200', | ||
286 | 'open_graph' => [ | ||
287 | 'og_title' => 'my OG title', | ||
288 | 'og_description' => 'OG desc', | ||
289 | 'og_image' => 'https://', | ||
290 | ], | ||
291 | ]); | ||
264 | 292 | ||
293 | $proxy = new ContentProxy($graby, $tagger, $validator, $this->getLogger(), $this->fetchingErrorMessage); | ||
265 | $entry = new Entry(new User()); | 294 | $entry = new Entry(new User()); |
295 | $proxy->updateEntry($entry, 'http://0.0.0.0'); | ||
266 | 296 | ||
267 | $proxy->assignTagsToEntry($entry, [' tag1', 'tag2 ']); | 297 | $this->assertSame('http://1.1.1.1', $entry->getUrl()); |
268 | 298 | $this->assertSame('this is my title', $entry->getTitle()); | |
269 | $this->assertCount(2, $entry->getTags()); | 299 | $this->assertContains('this is my content', $entry->getContent()); |
270 | $this->assertEquals('tag1', $entry->getTags()[0]->getLabel()); | 300 | $this->assertNull($entry->getPreviewPicture()); |
271 | $this->assertEquals('tag2', $entry->getTags()[1]->getLabel()); | 301 | $this->assertSame('text/html', $entry->getMimetype()); |
302 | $this->assertSame('fr', $entry->getLanguage()); | ||
303 | $this->assertSame('200', $entry->getHttpStatus()); | ||
304 | $this->assertSame(4.0, $entry->getReadingTime()); | ||
305 | $this->assertSame('1.1.1.1', $entry->getDomainName()); | ||
272 | } | 306 | } |
273 | 307 | ||
274 | public function testAssignTagsWithString() | 308 | public function testWithForcedContent() |
275 | { | 309 | { |
276 | $graby = $this->getMockBuilder('Graby\Graby') | 310 | $tagger = $this->getTaggerMock(); |
277 | ->disableOriginalConstructor() | 311 | $tagger->expects($this->once()) |
278 | ->getMock(); | 312 | ->method('tag'); |
279 | |||
280 | $tagRepo = $this->getTagRepositoryMock(); | ||
281 | $proxy = new ContentProxy($graby, $this->getTaggerMock(), $tagRepo, $this->getLogger(), $this->fetchingErrorMessage); | ||
282 | 313 | ||
314 | $proxy = new ContentProxy((new Graby()), $tagger, $this->getValidator(), $this->getLogger(), $this->fetchingErrorMessage); | ||
283 | $entry = new Entry(new User()); | 315 | $entry = new Entry(new User()); |
316 | $proxy->updateEntry( | ||
317 | $entry, | ||
318 | 'http://0.0.0.0', | ||
319 | [ | ||
320 | 'html' => str_repeat('this is my content', 325), | ||
321 | 'title' => 'this is my title', | ||
322 | 'url' => 'http://1.1.1.1', | ||
323 | 'content_type' => 'text/html', | ||
324 | 'language' => 'fr', | ||
325 | 'date' => '1395635872', | ||
326 | 'authors' => ['Jeremy', 'Nico', 'Thomas'], | ||
327 | 'all_headers' => [ | ||
328 | 'Cache-Control' => 'no-cache', | ||
329 | ], | ||
330 | ] | ||
331 | ); | ||
284 | 332 | ||
285 | $proxy->assignTagsToEntry($entry, 'tag1, tag2'); | 333 | $this->assertSame('http://1.1.1.1', $entry->getUrl()); |
286 | 334 | $this->assertSame('this is my title', $entry->getTitle()); | |
287 | $this->assertCount(2, $entry->getTags()); | 335 | $this->assertContains('this is my content', $entry->getContent()); |
288 | $this->assertEquals('tag1', $entry->getTags()[0]->getLabel()); | 336 | $this->assertSame('text/html', $entry->getMimetype()); |
289 | $this->assertEquals('tag2', $entry->getTags()[1]->getLabel()); | 337 | $this->assertSame('fr', $entry->getLanguage()); |
338 | $this->assertSame(4.0, $entry->getReadingTime()); | ||
339 | $this->assertSame('1.1.1.1', $entry->getDomainName()); | ||
340 | $this->assertSame('24/03/2014', $entry->getPublishedAt()->format('d/m/Y')); | ||
341 | $this->assertContains('Jeremy', $entry->getPublishedBy()); | ||
342 | $this->assertContains('Nico', $entry->getPublishedBy()); | ||
343 | $this->assertContains('Thomas', $entry->getPublishedBy()); | ||
344 | $this->assertContains('no-cache', $entry->getHeaders()); | ||
290 | } | 345 | } |
291 | 346 | ||
292 | public function testAssignTagsWithEmptyArray() | 347 | public function testWithForcedContentAndDatetime() |
293 | { | 348 | { |
294 | $graby = $this->getMockBuilder('Graby\Graby') | 349 | $tagger = $this->getTaggerMock(); |
295 | ->disableOriginalConstructor() | 350 | $tagger->expects($this->once()) |
296 | ->getMock(); | 351 | ->method('tag'); |
297 | 352 | ||
298 | $tagRepo = $this->getTagRepositoryMock(); | 353 | $logHandler = new TestHandler(); |
299 | $proxy = new ContentProxy($graby, $this->getTaggerMock(), $tagRepo, $this->getLogger(), $this->fetchingErrorMessage); | 354 | $logger = new Logger('test', [$logHandler]); |
300 | 355 | ||
356 | $proxy = new ContentProxy((new Graby()), $tagger, $this->getValidator(), $logger, $this->fetchingErrorMessage); | ||
301 | $entry = new Entry(new User()); | 357 | $entry = new Entry(new User()); |
358 | $proxy->updateEntry( | ||
359 | $entry, | ||
360 | 'http://1.1.1.1', | ||
361 | [ | ||
362 | 'html' => str_repeat('this is my content', 325), | ||
363 | 'title' => 'this is my title', | ||
364 | 'url' => 'http://1.1.1.1', | ||
365 | 'content_type' => 'text/html', | ||
366 | 'language' => 'fr', | ||
367 | 'date' => '2016-09-08T11:55:58+0200', | ||
368 | ] | ||
369 | ); | ||
302 | 370 | ||
303 | $proxy->assignTagsToEntry($entry, []); | 371 | $this->assertSame('http://1.1.1.1', $entry->getUrl()); |
304 | 372 | $this->assertSame('this is my title', $entry->getTitle()); | |
305 | $this->assertCount(0, $entry->getTags()); | 373 | $this->assertContains('this is my content', $entry->getContent()); |
374 | $this->assertSame('text/html', $entry->getMimetype()); | ||
375 | $this->assertSame('fr', $entry->getLanguage()); | ||
376 | $this->assertSame(4.0, $entry->getReadingTime()); | ||
377 | $this->assertSame('1.1.1.1', $entry->getDomainName()); | ||
378 | $this->assertSame('08/09/2016', $entry->getPublishedAt()->format('d/m/Y')); | ||
306 | } | 379 | } |
307 | 380 | ||
308 | public function testAssignTagsWithEmptyString() | 381 | public function testWithForcedContentAndBadDate() |
309 | { | 382 | { |
310 | $graby = $this->getMockBuilder('Graby\Graby') | 383 | $tagger = $this->getTaggerMock(); |
311 | ->disableOriginalConstructor() | 384 | $tagger->expects($this->once()) |
312 | ->getMock(); | 385 | ->method('tag'); |
313 | 386 | ||
314 | $tagRepo = $this->getTagRepositoryMock(); | 387 | $logger = new Logger('foo'); |
315 | $proxy = new ContentProxy($graby, $this->getTaggerMock(), $tagRepo, $this->getLogger(), $this->fetchingErrorMessage); | 388 | $handler = new TestHandler(); |
389 | $logger->pushHandler($handler); | ||
316 | 390 | ||
391 | $proxy = new ContentProxy((new Graby()), $tagger, $this->getValidator(), $logger, $this->fetchingErrorMessage); | ||
317 | $entry = new Entry(new User()); | 392 | $entry = new Entry(new User()); |
393 | $proxy->updateEntry( | ||
394 | $entry, | ||
395 | 'http://1.1.1.1', | ||
396 | [ | ||
397 | 'html' => str_repeat('this is my content', 325), | ||
398 | 'title' => 'this is my title', | ||
399 | 'url' => 'http://1.1.1.1', | ||
400 | 'content_type' => 'text/html', | ||
401 | 'language' => 'fr', | ||
402 | 'date' => '01 02 2012', | ||
403 | ] | ||
404 | ); | ||
318 | 405 | ||
319 | $proxy->assignTagsToEntry($entry, ''); | 406 | $this->assertSame('http://1.1.1.1', $entry->getUrl()); |
407 | $this->assertSame('this is my title', $entry->getTitle()); | ||
408 | $this->assertContains('this is my content', $entry->getContent()); | ||
409 | $this->assertSame('text/html', $entry->getMimetype()); | ||
410 | $this->assertSame('fr', $entry->getLanguage()); | ||
411 | $this->assertSame(4.0, $entry->getReadingTime()); | ||
412 | $this->assertSame('1.1.1.1', $entry->getDomainName()); | ||
413 | $this->assertNull($entry->getPublishedAt()); | ||
320 | 414 | ||
321 | $this->assertCount(0, $entry->getTags()); | 415 | $records = $handler->getRecords(); |
416 | |||
417 | $this->assertCount(1, $records); | ||
418 | $this->assertContains('Error while defining date', $records[0]['message']); | ||
322 | } | 419 | } |
323 | 420 | ||
324 | public function testAssignTagsAlreadyAssigned() | 421 | public function testTaggerThrowException() |
325 | { | 422 | { |
326 | $graby = $this->getMockBuilder('Graby\Graby') | 423 | $tagger = $this->getTaggerMock(); |
327 | ->disableOriginalConstructor() | 424 | $tagger->expects($this->once()) |
328 | ->getMock(); | 425 | ->method('tag') |
426 | ->will($this->throwException(new \Exception())); | ||
329 | 427 | ||
330 | $tagRepo = $this->getTagRepositoryMock(); | 428 | $proxy = new ContentProxy((new Graby()), $tagger, $this->getValidator(), $this->getLogger(), $this->fetchingErrorMessage); |
331 | $proxy = new ContentProxy($graby, $this->getTaggerMock(), $tagRepo, $this->getLogger(), $this->fetchingErrorMessage); | 429 | $entry = new Entry(new User()); |
430 | $proxy->updateEntry( | ||
431 | $entry, | ||
432 | 'http://1.1.1.1', | ||
433 | [ | ||
434 | 'html' => str_repeat('this is my content', 325), | ||
435 | 'title' => 'this is my title', | ||
436 | 'url' => 'http://1.1.1.1', | ||
437 | 'content_type' => 'text/html', | ||
438 | 'language' => 'fr', | ||
439 | ] | ||
440 | ); | ||
332 | 441 | ||
333 | $tagEntity = new Tag(); | 442 | $this->assertCount(0, $entry->getTags()); |
334 | $tagEntity->setLabel('tag1'); | 443 | } |
335 | 444 | ||
336 | $entry = new Entry(new User()); | 445 | public function dataForCrazyHtml() |
337 | $entry->addTag($tagEntity); | 446 | { |
447 | return [ | ||
448 | 'script and comment' => [ | ||
449 | '<strong>Script inside:</strong> <!--[if gte IE 4]><script>alert(\'lol\');</script><![endif]--><br />', | ||
450 | 'lol', | ||
451 | ], | ||
452 | 'script' => [ | ||
453 | '<strong>Script inside:</strong><script>alert(\'lol\');</script>', | ||
454 | 'script', | ||
455 | ], | ||
456 | ]; | ||
457 | } | ||
338 | 458 | ||
339 | $proxy->assignTagsToEntry($entry, 'tag1, tag2'); | 459 | /** |
460 | * @dataProvider dataForCrazyHtml | ||
461 | */ | ||
462 | public function testWithCrazyHtmlContent($html, $escapedString) | ||
463 | { | ||
464 | $tagger = $this->getTaggerMock(); | ||
465 | $tagger->expects($this->once()) | ||
466 | ->method('tag'); | ||
340 | 467 | ||
341 | $this->assertCount(2, $entry->getTags()); | 468 | $proxy = new ContentProxy((new Graby()), $tagger, $this->getValidator(), $this->getLogger(), $this->fetchingErrorMessage); |
342 | $this->assertEquals('tag1', $entry->getTags()[0]->getLabel()); | 469 | $entry = new Entry(new User()); |
343 | $this->assertEquals('tag2', $entry->getTags()[1]->getLabel()); | 470 | $proxy->updateEntry( |
471 | $entry, | ||
472 | 'http://1.1.1.1', | ||
473 | [ | ||
474 | 'html' => $html, | ||
475 | 'title' => 'this is my title', | ||
476 | 'url' => 'http://1.1.1.1', | ||
477 | 'content_type' => 'text/html', | ||
478 | 'language' => 'fr', | ||
479 | 'status' => '200', | ||
480 | 'open_graph' => [ | ||
481 | 'og_title' => 'my OG title', | ||
482 | 'og_description' => 'OG desc', | ||
483 | 'og_image' => 'http://3.3.3.3/cover.jpg', | ||
484 | ], | ||
485 | ] | ||
486 | ); | ||
487 | |||
488 | $this->assertSame('http://1.1.1.1', $entry->getUrl()); | ||
489 | $this->assertSame('this is my title', $entry->getTitle()); | ||
490 | $this->assertNotContains($escapedString, $entry->getContent()); | ||
491 | $this->assertSame('http://3.3.3.3/cover.jpg', $entry->getPreviewPicture()); | ||
492 | $this->assertSame('text/html', $entry->getMimetype()); | ||
493 | $this->assertSame('fr', $entry->getLanguage()); | ||
494 | $this->assertSame('200', $entry->getHttpStatus()); | ||
495 | $this->assertSame('1.1.1.1', $entry->getDomainName()); | ||
344 | } | 496 | } |
345 | 497 | ||
346 | public function testAssignTagsNotFlushed() | 498 | public function testWithImageAsContent() |
347 | { | 499 | { |
500 | $tagger = $this->getTaggerMock(); | ||
501 | $tagger->expects($this->once()) | ||
502 | ->method('tag'); | ||
503 | |||
348 | $graby = $this->getMockBuilder('Graby\Graby') | 504 | $graby = $this->getMockBuilder('Graby\Graby') |
505 | ->setMethods(['fetchContent']) | ||
349 | ->disableOriginalConstructor() | 506 | ->disableOriginalConstructor() |
350 | ->getMock(); | 507 | ->getMock(); |
351 | 508 | ||
352 | $tagRepo = $this->getTagRepositoryMock(); | 509 | $graby->expects($this->any()) |
353 | $tagRepo->expects($this->never()) | 510 | ->method('fetchContent') |
354 | ->method('__call'); | 511 | ->willReturn([ |
355 | 512 | 'html' => '<p><img src="http://1.1.1.1/image.jpg" /></p>', | |
356 | $proxy = new ContentProxy($graby, $this->getTaggerMock(), $tagRepo, $this->getLogger(), $this->fetchingErrorMessage); | 513 | 'title' => 'this is my title', |
357 | 514 | 'url' => 'http://1.1.1.1/image.jpg', | |
358 | $tagEntity = new Tag(); | 515 | 'content_type' => 'image/jpeg', |
359 | $tagEntity->setLabel('tag1'); | 516 | 'status' => '200', |
517 | 'open_graph' => [], | ||
518 | ]); | ||
360 | 519 | ||
520 | $proxy = new ContentProxy($graby, $tagger, $this->getValidator(), $this->getLogger(), $this->fetchingErrorMessage); | ||
361 | $entry = new Entry(new User()); | 521 | $entry = new Entry(new User()); |
362 | 522 | $proxy->updateEntry($entry, 'http://0.0.0.0'); | |
363 | $proxy->assignTagsToEntry($entry, 'tag1', [$tagEntity]); | 523 | |
364 | 524 | $this->assertSame('http://1.1.1.1/image.jpg', $entry->getUrl()); | |
365 | $this->assertCount(1, $entry->getTags()); | 525 | $this->assertSame('this is my title', $entry->getTitle()); |
366 | $this->assertEquals('tag1', $entry->getTags()[0]->getLabel()); | 526 | $this->assertContains('http://1.1.1.1/image.jpg', $entry->getContent()); |
527 | $this->assertSame('http://1.1.1.1/image.jpg', $entry->getPreviewPicture()); | ||
528 | $this->assertSame('image/jpeg', $entry->getMimetype()); | ||
529 | $this->assertSame('200', $entry->getHttpStatus()); | ||
530 | $this->assertSame('1.1.1.1', $entry->getDomainName()); | ||
367 | } | 531 | } |
368 | 532 | ||
369 | private function getTaggerMock() | 533 | private function getTaggerMock() |
370 | { | 534 | { |
371 | return $this->getMockBuilder('Wallabag\CoreBundle\Helper\RuleBasedTagger') | 535 | return $this->getMockBuilder(RuleBasedTagger::class) |
372 | ->setMethods(['tag']) | 536 | ->setMethods(['tag']) |
373 | ->disableOriginalConstructor() | 537 | ->disableOriginalConstructor() |
374 | ->getMock(); | 538 | ->getMock(); |
375 | } | 539 | } |
376 | 540 | ||
377 | private function getTagRepositoryMock() | 541 | private function getLogger() |
378 | { | 542 | { |
379 | return $this->getMockBuilder('Wallabag\CoreBundle\Repository\TagRepository') | 543 | return new NullLogger(); |
380 | ->disableOriginalConstructor() | ||
381 | ->getMock(); | ||
382 | } | 544 | } |
383 | 545 | ||
384 | private function getLogger() | 546 | private function getValidator() |
385 | { | 547 | { |
386 | return new NullLogger(); | 548 | return $this->getMockBuilder(RecursiveValidator::class) |
549 | ->setMethods(['validate']) | ||
550 | ->disableOriginalConstructor() | ||
551 | ->getMock(); | ||
387 | } | 552 | } |
388 | } | 553 | } |
diff --git a/tests/Wallabag/CoreBundle/Helper/CryptoProxyTest.php b/tests/Wallabag/CoreBundle/Helper/CryptoProxyTest.php new file mode 100644 index 00000000..782c29c3 --- /dev/null +++ b/tests/Wallabag/CoreBundle/Helper/CryptoProxyTest.php | |||
@@ -0,0 +1,40 @@ | |||
1 | <?php | ||
2 | |||
3 | namespace Tests\Wallabag\CoreBundle\Helper; | ||
4 | |||
5 | use Monolog\Handler\TestHandler; | ||
6 | use Monolog\Logger; | ||
7 | use Psr\Log\NullLogger; | ||
8 | use Wallabag\CoreBundle\Helper\CryptoProxy; | ||
9 | |||
10 | class CryptoProxyTest extends \PHPUnit_Framework_TestCase | ||
11 | { | ||
12 | public function testCrypto() | ||
13 | { | ||
14 | $logHandler = new TestHandler(); | ||
15 | $logger = new Logger('test', [$logHandler]); | ||
16 | |||
17 | $crypto = new CryptoProxy(sys_get_temp_dir() . '/' . uniqid('', true) . '.txt', $logger); | ||
18 | $crypted = $crypto->crypt('test'); | ||
19 | $decrypted = $crypto->decrypt($crypted); | ||
20 | |||
21 | $this->assertSame('test', $decrypted); | ||
22 | |||
23 | $records = $logHandler->getRecords(); | ||
24 | $this->assertCount(2, $records); | ||
25 | $this->assertContains('Crypto: crypting value', $records[0]['message']); | ||
26 | $this->assertContains('Crypto: decrypting value', $records[1]['message']); | ||
27 | } | ||
28 | |||
29 | /** | ||
30 | * @expectedException \RuntimeException | ||
31 | * @expectedExceptionMessage Decrypt fail | ||
32 | * | ||
33 | * @return [type] [description] | ||
34 | */ | ||
35 | public function testDecryptBadValue() | ||
36 | { | ||
37 | $crypto = new CryptoProxy(sys_get_temp_dir() . '/' . uniqid('', true) . '.txt', new NullLogger()); | ||
38 | $crypto->decrypt('badvalue'); | ||
39 | } | ||
40 | } | ||
diff --git a/tests/Wallabag/CoreBundle/Helper/DownloadImagesTest.php b/tests/Wallabag/CoreBundle/Helper/DownloadImagesTest.php index 85f12d87..c61f65d0 100644 --- a/tests/Wallabag/CoreBundle/Helper/DownloadImagesTest.php +++ b/tests/Wallabag/CoreBundle/Helper/DownloadImagesTest.php | |||
@@ -2,34 +2,52 @@ | |||
2 | 2 | ||
3 | namespace Tests\Wallabag\CoreBundle\Helper; | 3 | namespace Tests\Wallabag\CoreBundle\Helper; |
4 | 4 | ||
5 | use Wallabag\CoreBundle\Helper\DownloadImages; | ||
6 | use Monolog\Logger; | ||
7 | use Monolog\Handler\TestHandler; | ||
8 | use GuzzleHttp\Client; | 5 | use GuzzleHttp\Client; |
9 | use GuzzleHttp\Subscriber\Mock; | ||
10 | use GuzzleHttp\Message\Response; | 6 | use GuzzleHttp\Message\Response; |
11 | use GuzzleHttp\Stream\Stream; | 7 | use GuzzleHttp\Stream\Stream; |
8 | use GuzzleHttp\Subscriber\Mock; | ||
9 | use Monolog\Handler\TestHandler; | ||
10 | use Monolog\Logger; | ||
11 | use Wallabag\CoreBundle\Helper\DownloadImages; | ||
12 | 12 | ||
13 | class DownloadImagesTest extends \PHPUnit_Framework_TestCase | 13 | class DownloadImagesTest extends \PHPUnit_Framework_TestCase |
14 | { | 14 | { |
15 | public function testProcessHtml() | 15 | public function dataForSuccessImage() |
16 | { | ||
17 | return [ | ||
18 | 'imgur' => [ | ||
19 | '<div><img src="http://i.imgur.com/T9qgcHc.jpg" /></div>', | ||
20 | 'http://imgur.com/gallery/WxtWY', | ||
21 | ], | ||
22 | 'image with &' => [ | ||
23 | '<div><img src="https://i2.wp.com/www.tvaddons.ag/wp-content/uploads/2017/01/Screen-Shot-2017-01-07-at-10.17.40-PM.jpg?w=640&ssl=1" /></div>', | ||
24 | 'https://www.tvaddons.ag/realdebrid-kodi-jarvis/', | ||
25 | ], | ||
26 | ]; | ||
27 | } | ||
28 | |||
29 | /** | ||
30 | * @dataProvider dataForSuccessImage | ||
31 | */ | ||
32 | public function testProcessHtml($html, $url) | ||
16 | { | 33 | { |
17 | $client = new Client(); | 34 | $client = new Client(); |
18 | 35 | ||
19 | $mock = new Mock([ | 36 | $mock = new Mock([ |
20 | new Response(200, ['content-type' => 'image/png'], Stream::factory(file_get_contents(__DIR__.'/../fixtures/unnamed.png'))), | 37 | new Response(200, ['content-type' => 'image/png'], Stream::factory(file_get_contents(__DIR__ . '/../fixtures/unnamed.png'))), |
21 | ]); | 38 | ]); |
22 | 39 | ||
23 | $client->getEmitter()->attach($mock); | 40 | $client->getEmitter()->attach($mock); |
24 | 41 | ||
25 | $logHandler = new TestHandler(); | 42 | $logHandler = new TestHandler(); |
26 | $logger = new Logger('test', array($logHandler)); | 43 | $logger = new Logger('test', [$logHandler]); |
27 | 44 | ||
28 | $download = new DownloadImages($client, sys_get_temp_dir().'/wallabag_test', 'http://wallabag.io/', $logger); | 45 | $download = new DownloadImages($client, sys_get_temp_dir() . '/wallabag_test', 'http://wallabag.io/', $logger); |
29 | 46 | ||
30 | $res = $download->processHtml(123, '<div><img src="http://i.imgur.com/T9qgcHc.jpg" /></div>', 'http://imgur.com/gallery/WxtWY'); | 47 | $res = $download->processHtml(123, $html, $url); |
31 | 48 | ||
32 | $this->assertContains('http://wallabag.io/assets/images/9/b/9b0ead26/c638b4c2.png', $res); | 49 | // this the base path of all image (since it's calculated using the entry id: 123) |
50 | $this->assertContains('http://wallabag.io/assets/images/9/b/9b0ead26/', $res); | ||
33 | } | 51 | } |
34 | 52 | ||
35 | public function testProcessHtmlWithBadImage() | 53 | public function testProcessHtmlWithBadImage() |
@@ -43,9 +61,9 @@ class DownloadImagesTest extends \PHPUnit_Framework_TestCase | |||
43 | $client->getEmitter()->attach($mock); | 61 | $client->getEmitter()->attach($mock); |
44 | 62 | ||
45 | $logHandler = new TestHandler(); | 63 | $logHandler = new TestHandler(); |
46 | $logger = new Logger('test', array($logHandler)); | 64 | $logger = new Logger('test', [$logHandler]); |
47 | 65 | ||
48 | $download = new DownloadImages($client, sys_get_temp_dir().'/wallabag_test', 'http://wallabag.io/', $logger); | 66 | $download = new DownloadImages($client, sys_get_temp_dir() . '/wallabag_test', 'http://wallabag.io/', $logger); |
49 | $res = $download->processHtml(123, '<div><img src="http://i.imgur.com/T9qgcHc.jpg" /></div>', 'http://imgur.com/gallery/WxtWY'); | 67 | $res = $download->processHtml(123, '<div><img src="http://i.imgur.com/T9qgcHc.jpg" /></div>', 'http://imgur.com/gallery/WxtWY'); |
50 | 68 | ||
51 | $this->assertContains('http://i.imgur.com/T9qgcHc.jpg', $res, 'Image were not replace because of content-type'); | 69 | $this->assertContains('http://i.imgur.com/T9qgcHc.jpg', $res, 'Image were not replace because of content-type'); |
@@ -69,18 +87,18 @@ class DownloadImagesTest extends \PHPUnit_Framework_TestCase | |||
69 | $client = new Client(); | 87 | $client = new Client(); |
70 | 88 | ||
71 | $mock = new Mock([ | 89 | $mock = new Mock([ |
72 | new Response(200, ['content-type' => $header], Stream::factory(file_get_contents(__DIR__.'/../fixtures/unnamed.png'))), | 90 | new Response(200, ['content-type' => $header], Stream::factory(file_get_contents(__DIR__ . '/../fixtures/unnamed.png'))), |
73 | ]); | 91 | ]); |
74 | 92 | ||
75 | $client->getEmitter()->attach($mock); | 93 | $client->getEmitter()->attach($mock); |
76 | 94 | ||
77 | $logHandler = new TestHandler(); | 95 | $logHandler = new TestHandler(); |
78 | $logger = new Logger('test', array($logHandler)); | 96 | $logger = new Logger('test', [$logHandler]); |
79 | 97 | ||
80 | $download = new DownloadImages($client, sys_get_temp_dir().'/wallabag_test', 'http://wallabag.io/', $logger); | 98 | $download = new DownloadImages($client, sys_get_temp_dir() . '/wallabag_test', 'http://wallabag.io/', $logger); |
81 | $res = $download->processSingleImage(123, 'T9qgcHc.jpg', 'http://imgur.com/gallery/WxtWY'); | 99 | $res = $download->processSingleImage(123, 'T9qgcHc.jpg', 'http://imgur.com/gallery/WxtWY'); |
82 | 100 | ||
83 | $this->assertContains('/assets/images/9/b/9b0ead26/ebe60399.'.$extension, $res); | 101 | $this->assertContains('/assets/images/9/b/9b0ead26/ebe60399.' . $extension, $res); |
84 | } | 102 | } |
85 | 103 | ||
86 | public function testProcessSingleImageWithBadUrl() | 104 | public function testProcessSingleImageWithBadUrl() |
@@ -94,9 +112,9 @@ class DownloadImagesTest extends \PHPUnit_Framework_TestCase | |||
94 | $client->getEmitter()->attach($mock); | 112 | $client->getEmitter()->attach($mock); |
95 | 113 | ||
96 | $logHandler = new TestHandler(); | 114 | $logHandler = new TestHandler(); |
97 | $logger = new Logger('test', array($logHandler)); | 115 | $logger = new Logger('test', [$logHandler]); |
98 | 116 | ||
99 | $download = new DownloadImages($client, sys_get_temp_dir().'/wallabag_test', 'http://wallabag.io/', $logger); | 117 | $download = new DownloadImages($client, sys_get_temp_dir() . '/wallabag_test', 'http://wallabag.io/', $logger); |
100 | $res = $download->processSingleImage(123, 'T9qgcHc.jpg', 'http://imgur.com/gallery/WxtWY'); | 118 | $res = $download->processSingleImage(123, 'T9qgcHc.jpg', 'http://imgur.com/gallery/WxtWY'); |
101 | 119 | ||
102 | $this->assertFalse($res, 'Image can not be found, so it will not be replaced'); | 120 | $this->assertFalse($res, 'Image can not be found, so it will not be replaced'); |
@@ -113,9 +131,9 @@ class DownloadImagesTest extends \PHPUnit_Framework_TestCase | |||
113 | $client->getEmitter()->attach($mock); | 131 | $client->getEmitter()->attach($mock); |
114 | 132 | ||
115 | $logHandler = new TestHandler(); | 133 | $logHandler = new TestHandler(); |
116 | $logger = new Logger('test', array($logHandler)); | 134 | $logger = new Logger('test', [$logHandler]); |
117 | 135 | ||
118 | $download = new DownloadImages($client, sys_get_temp_dir().'/wallabag_test', 'http://wallabag.io/', $logger); | 136 | $download = new DownloadImages($client, sys_get_temp_dir() . '/wallabag_test', 'http://wallabag.io/', $logger); |
119 | $res = $download->processSingleImage(123, 'http://i.imgur.com/T9qgcHc.jpg', 'http://imgur.com/gallery/WxtWY'); | 137 | $res = $download->processSingleImage(123, 'http://i.imgur.com/T9qgcHc.jpg', 'http://imgur.com/gallery/WxtWY'); |
120 | 138 | ||
121 | $this->assertFalse($res, 'Image can not be loaded, so it will not be replaced'); | 139 | $this->assertFalse($res, 'Image can not be loaded, so it will not be replaced'); |
@@ -126,17 +144,42 @@ class DownloadImagesTest extends \PHPUnit_Framework_TestCase | |||
126 | $client = new Client(); | 144 | $client = new Client(); |
127 | 145 | ||
128 | $mock = new Mock([ | 146 | $mock = new Mock([ |
129 | new Response(200, ['content-type' => 'image/png'], Stream::factory(file_get_contents(__DIR__.'/../fixtures/unnamed.png'))), | 147 | new Response(200, ['content-type' => 'image/png'], Stream::factory(file_get_contents(__DIR__ . '/../fixtures/unnamed.png'))), |
130 | ]); | 148 | ]); |
131 | 149 | ||
132 | $client->getEmitter()->attach($mock); | 150 | $client->getEmitter()->attach($mock); |
133 | 151 | ||
134 | $logHandler = new TestHandler(); | 152 | $logHandler = new TestHandler(); |
135 | $logger = new Logger('test', array($logHandler)); | 153 | $logger = new Logger('test', [$logHandler]); |
136 | 154 | ||
137 | $download = new DownloadImages($client, sys_get_temp_dir().'/wallabag_test', 'http://wallabag.io/', $logger); | 155 | $download = new DownloadImages($client, sys_get_temp_dir() . '/wallabag_test', 'http://wallabag.io/', $logger); |
138 | $res = $download->processSingleImage(123, '/i.imgur.com/T9qgcHc.jpg', 'imgur.com/gallery/WxtWY'); | 156 | $res = $download->processSingleImage(123, '/i.imgur.com/T9qgcHc.jpg', 'imgur.com/gallery/WxtWY'); |
139 | 157 | ||
140 | $this->assertFalse($res, 'Absolute image can not be determined, so it will not be replaced'); | 158 | $this->assertFalse($res, 'Absolute image can not be determined, so it will not be replaced'); |
141 | } | 159 | } |
160 | |||
161 | public function testProcessRealImage() | ||
162 | { | ||
163 | $client = new Client(); | ||
164 | |||
165 | $mock = new Mock([ | ||
166 | new Response(200, ['content-type' => null], Stream::factory(file_get_contents(__DIR__ . '/../fixtures/image-no-content-type.jpg'))), | ||
167 | ]); | ||
168 | |||
169 | $client->getEmitter()->attach($mock); | ||
170 | |||
171 | $logHandler = new TestHandler(); | ||
172 | $logger = new Logger('test', [$logHandler]); | ||
173 | |||
174 | $download = new DownloadImages($client, sys_get_temp_dir() . '/wallabag_test', 'http://wallabag.io/', $logger); | ||
175 | |||
176 | $res = $download->processSingleImage( | ||
177 | 123, | ||
178 | 'https://cdn.theconversation.com/files/157200/article/width926/gsj2rjp2-1487348607.jpg', | ||
179 | 'https://theconversation.com/conversation-avec-gerald-bronner-ce-nest-pas-la-post-verite-qui-nous-menace-mais-lextension-de-notre-credulite-73089' | ||
180 | ); | ||
181 | |||
182 | $this->assertContains('http://wallabag.io/assets/images/9/b/9b0ead26/', $res, 'Content-Type was empty but data is ok for an image'); | ||
183 | $this->assertContains('DownloadImages: Checking extension (alternative)', $logHandler->getRecords()[3]['message']); | ||
184 | } | ||
142 | } | 185 | } |
diff --git a/tests/Wallabag/CoreBundle/Helper/RedirectTest.php b/tests/Wallabag/CoreBundle/Helper/RedirectTest.php index 0539f20a..7fd2ea2b 100644 --- a/tests/Wallabag/CoreBundle/Helper/RedirectTest.php +++ b/tests/Wallabag/CoreBundle/Helper/RedirectTest.php | |||
@@ -2,11 +2,11 @@ | |||
2 | 2 | ||
3 | namespace Tests\Wallabag\CoreBundle\Helper; | 3 | namespace Tests\Wallabag\CoreBundle\Helper; |
4 | 4 | ||
5 | use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage; | ||
6 | use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken; | ||
5 | use Wallabag\CoreBundle\Entity\Config; | 7 | use Wallabag\CoreBundle\Entity\Config; |
6 | use Wallabag\UserBundle\Entity\User; | ||
7 | use Wallabag\CoreBundle\Helper\Redirect; | 8 | use Wallabag\CoreBundle\Helper\Redirect; |
8 | use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken; | 9 | use Wallabag\UserBundle\Entity\User; |
9 | use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage; | ||
10 | 10 | ||
11 | class RedirectTest extends \PHPUnit_Framework_TestCase | 11 | class RedirectTest extends \PHPUnit_Framework_TestCase |
12 | { | 12 | { |
@@ -56,21 +56,21 @@ class RedirectTest extends \PHPUnit_Framework_TestCase | |||
56 | { | 56 | { |
57 | $redirectUrl = $this->redirect->to(null, 'fallback'); | 57 | $redirectUrl = $this->redirect->to(null, 'fallback'); |
58 | 58 | ||
59 | $this->assertEquals('fallback', $redirectUrl); | 59 | $this->assertSame('fallback', $redirectUrl); |
60 | } | 60 | } |
61 | 61 | ||
62 | public function testRedirectToNullWithoutFallback() | 62 | public function testRedirectToNullWithoutFallback() |
63 | { | 63 | { |
64 | $redirectUrl = $this->redirect->to(null); | 64 | $redirectUrl = $this->redirect->to(null); |
65 | 65 | ||
66 | $this->assertEquals($this->routerMock->generate('homepage'), $redirectUrl); | 66 | $this->assertSame($this->routerMock->generate('homepage'), $redirectUrl); |
67 | } | 67 | } |
68 | 68 | ||
69 | public function testRedirectToValidUrl() | 69 | public function testRedirectToValidUrl() |
70 | { | 70 | { |
71 | $redirectUrl = $this->redirect->to('/unread/list'); | 71 | $redirectUrl = $this->redirect->to('/unread/list'); |
72 | 72 | ||
73 | $this->assertEquals('/unread/list', $redirectUrl); | 73 | $this->assertSame('/unread/list', $redirectUrl); |
74 | } | 74 | } |
75 | 75 | ||
76 | public function testWithNotLoggedUser() | 76 | public function testWithNotLoggedUser() |
@@ -78,7 +78,7 @@ class RedirectTest extends \PHPUnit_Framework_TestCase | |||
78 | $redirect = new Redirect($this->routerMock, new TokenStorage()); | 78 | $redirect = new Redirect($this->routerMock, new TokenStorage()); |
79 | $redirectUrl = $redirect->to('/unread/list'); | 79 | $redirectUrl = $redirect->to('/unread/list'); |
80 | 80 | ||
81 | $this->assertEquals('/unread/list', $redirectUrl); | 81 | $this->assertSame('/unread/list', $redirectUrl); |
82 | } | 82 | } |
83 | 83 | ||
84 | public function testUserForRedirectToHomepage() | 84 | public function testUserForRedirectToHomepage() |
@@ -87,6 +87,24 @@ class RedirectTest extends \PHPUnit_Framework_TestCase | |||
87 | 87 | ||
88 | $redirectUrl = $this->redirect->to('/unread/list'); | 88 | $redirectUrl = $this->redirect->to('/unread/list'); |
89 | 89 | ||
90 | $this->assertEquals($this->routerMock->generate('homepage'), $redirectUrl); | 90 | $this->assertSame($this->routerMock->generate('homepage'), $redirectUrl); |
91 | } | ||
92 | |||
93 | public function testUserForRedirectWithIgnoreActionMarkAsRead() | ||
94 | { | ||
95 | $this->token->getUser()->getConfig()->setActionMarkAsRead(Config::REDIRECT_TO_HOMEPAGE); | ||
96 | |||
97 | $redirectUrl = $this->redirect->to('/unread/list', '', true); | ||
98 | |||
99 | $this->assertSame('/unread/list', $redirectUrl); | ||
100 | } | ||
101 | |||
102 | public function testUserForRedirectNullWithFallbackWithIgnoreActionMarkAsRead() | ||
103 | { | ||
104 | $this->token->getUser()->getConfig()->setActionMarkAsRead(Config::REDIRECT_TO_HOMEPAGE); | ||
105 | |||
106 | $redirectUrl = $this->redirect->to(null, 'fallback', true); | ||
107 | |||
108 | $this->assertSame('fallback', $redirectUrl); | ||
91 | } | 109 | } |
92 | } | 110 | } |
diff --git a/tests/Wallabag/CoreBundle/Helper/RuleBasedTaggerTest.php b/tests/Wallabag/CoreBundle/Helper/RuleBasedTaggerTest.php index 17b08c2a..c31af680 100644 --- a/tests/Wallabag/CoreBundle/Helper/RuleBasedTaggerTest.php +++ b/tests/Wallabag/CoreBundle/Helper/RuleBasedTaggerTest.php | |||
@@ -2,6 +2,8 @@ | |||
2 | 2 | ||
3 | namespace Tests\Wallabag\CoreBundle\Helper; | 3 | namespace Tests\Wallabag\CoreBundle\Helper; |
4 | 4 | ||
5 | use Monolog\Handler\TestHandler; | ||
6 | use Monolog\Logger; | ||
5 | use Wallabag\CoreBundle\Entity\Config; | 7 | use Wallabag\CoreBundle\Entity\Config; |
6 | use Wallabag\CoreBundle\Entity\Entry; | 8 | use Wallabag\CoreBundle\Entity\Entry; |
7 | use Wallabag\CoreBundle\Entity\Tag; | 9 | use Wallabag\CoreBundle\Entity\Tag; |
@@ -15,14 +17,19 @@ class RuleBasedTaggerTest extends \PHPUnit_Framework_TestCase | |||
15 | private $tagRepository; | 17 | private $tagRepository; |
16 | private $entryRepository; | 18 | private $entryRepository; |
17 | private $tagger; | 19 | private $tagger; |
20 | private $logger; | ||
21 | private $handler; | ||
18 | 22 | ||
19 | public function setUp() | 23 | public function setUp() |
20 | { | 24 | { |
21 | $this->rulerz = $this->getRulerZMock(); | 25 | $this->rulerz = $this->getRulerZMock(); |
22 | $this->tagRepository = $this->getTagRepositoryMock(); | 26 | $this->tagRepository = $this->getTagRepositoryMock(); |
23 | $this->entryRepository = $this->getEntryRepositoryMock(); | 27 | $this->entryRepository = $this->getEntryRepositoryMock(); |
28 | $this->logger = $this->getLogger(); | ||
29 | $this->handler = new TestHandler(); | ||
30 | $this->logger->pushHandler($this->handler); | ||
24 | 31 | ||
25 | $this->tagger = new RuleBasedTagger($this->rulerz, $this->tagRepository, $this->entryRepository); | 32 | $this->tagger = new RuleBasedTagger($this->rulerz, $this->tagRepository, $this->entryRepository, $this->logger); |
26 | } | 33 | } |
27 | 34 | ||
28 | public function testTagWithNoRule() | 35 | public function testTagWithNoRule() |
@@ -32,6 +39,8 @@ class RuleBasedTaggerTest extends \PHPUnit_Framework_TestCase | |||
32 | $this->tagger->tag($entry); | 39 | $this->tagger->tag($entry); |
33 | 40 | ||
34 | $this->assertTrue($entry->getTags()->isEmpty()); | 41 | $this->assertTrue($entry->getTags()->isEmpty()); |
42 | $records = $this->handler->getRecords(); | ||
43 | $this->assertCount(0, $records); | ||
35 | } | 44 | } |
36 | 45 | ||
37 | public function testTagWithNoMatchingRule() | 46 | public function testTagWithNoMatchingRule() |
@@ -49,6 +58,8 @@ class RuleBasedTaggerTest extends \PHPUnit_Framework_TestCase | |||
49 | $this->tagger->tag($entry); | 58 | $this->tagger->tag($entry); |
50 | 59 | ||
51 | $this->assertTrue($entry->getTags()->isEmpty()); | 60 | $this->assertTrue($entry->getTags()->isEmpty()); |
61 | $records = $this->handler->getRecords(); | ||
62 | $this->assertCount(0, $records); | ||
52 | } | 63 | } |
53 | 64 | ||
54 | public function testTagWithAMatchingRule() | 65 | public function testTagWithAMatchingRule() |
@@ -70,6 +81,9 @@ class RuleBasedTaggerTest extends \PHPUnit_Framework_TestCase | |||
70 | $tags = $entry->getTags(); | 81 | $tags = $entry->getTags(); |
71 | $this->assertSame('foo', $tags[0]->getLabel()); | 82 | $this->assertSame('foo', $tags[0]->getLabel()); |
72 | $this->assertSame('bar', $tags[1]->getLabel()); | 83 | $this->assertSame('bar', $tags[1]->getLabel()); |
84 | |||
85 | $records = $this->handler->getRecords(); | ||
86 | $this->assertCount(1, $records); | ||
73 | } | 87 | } |
74 | 88 | ||
75 | public function testTagWithAMixOfMatchingRules() | 89 | public function testTagWithAMixOfMatchingRules() |
@@ -90,6 +104,8 @@ class RuleBasedTaggerTest extends \PHPUnit_Framework_TestCase | |||
90 | 104 | ||
91 | $tags = $entry->getTags(); | 105 | $tags = $entry->getTags(); |
92 | $this->assertSame('foo', $tags[0]->getLabel()); | 106 | $this->assertSame('foo', $tags[0]->getLabel()); |
107 | $records = $this->handler->getRecords(); | ||
108 | $this->assertCount(1, $records); | ||
93 | } | 109 | } |
94 | 110 | ||
95 | public function testWhenTheTagExists() | 111 | public function testWhenTheTagExists() |
@@ -118,6 +134,8 @@ class RuleBasedTaggerTest extends \PHPUnit_Framework_TestCase | |||
118 | 134 | ||
119 | $tags = $entry->getTags(); | 135 | $tags = $entry->getTags(); |
120 | $this->assertSame($tag, $tags[0]); | 136 | $this->assertSame($tag, $tags[0]); |
137 | $records = $this->handler->getRecords(); | ||
138 | $this->assertCount(1, $records); | ||
121 | } | 139 | } |
122 | 140 | ||
123 | public function testSameTagWithDifferentfMatchingRules() | 141 | public function testSameTagWithDifferentfMatchingRules() |
@@ -138,6 +156,8 @@ class RuleBasedTaggerTest extends \PHPUnit_Framework_TestCase | |||
138 | 156 | ||
139 | $tags = $entry->getTags(); | 157 | $tags = $entry->getTags(); |
140 | $this->assertCount(1, $tags); | 158 | $this->assertCount(1, $tags); |
159 | $records = $this->handler->getRecords(); | ||
160 | $this->assertCount(2, $records); | ||
141 | } | 161 | } |
142 | 162 | ||
143 | public function testTagAllEntriesForAUser() | 163 | public function testTagAllEntriesForAUser() |
@@ -162,7 +182,7 @@ class RuleBasedTaggerTest extends \PHPUnit_Framework_TestCase | |||
162 | $tags = $entry->getTags(); | 182 | $tags = $entry->getTags(); |
163 | 183 | ||
164 | $this->assertCount(1, $tags); | 184 | $this->assertCount(1, $tags); |
165 | $this->assertEquals('hey', $tags[0]->getLabel()); | 185 | $this->assertSame('hey', $tags[0]->getLabel()); |
166 | } | 186 | } |
167 | } | 187 | } |
168 | 188 | ||
@@ -209,4 +229,9 @@ class RuleBasedTaggerTest extends \PHPUnit_Framework_TestCase | |||
209 | ->disableOriginalConstructor() | 229 | ->disableOriginalConstructor() |
210 | ->getMock(); | 230 | ->getMock(); |
211 | } | 231 | } |
232 | |||
233 | private function getLogger() | ||
234 | { | ||
235 | return new Logger('foo'); | ||
236 | } | ||
212 | } | 237 | } |
diff --git a/tests/Wallabag/CoreBundle/Helper/TagsAssignerTest.php b/tests/Wallabag/CoreBundle/Helper/TagsAssignerTest.php new file mode 100644 index 00000000..475cd349 --- /dev/null +++ b/tests/Wallabag/CoreBundle/Helper/TagsAssignerTest.php | |||
@@ -0,0 +1,108 @@ | |||
1 | <?php | ||
2 | |||
3 | namespace Tests\Wallabag\CoreBundle\Helper; | ||
4 | |||
5 | use Wallabag\CoreBundle\Entity\Entry; | ||
6 | use Wallabag\CoreBundle\Entity\Tag; | ||
7 | use Wallabag\CoreBundle\Helper\TagsAssigner; | ||
8 | use Wallabag\CoreBundle\Repository\TagRepository; | ||
9 | use Wallabag\UserBundle\Entity\User; | ||
10 | |||
11 | class TagsAssignerTest extends \PHPUnit_Framework_TestCase | ||
12 | { | ||
13 | public function testAssignTagsWithArrayAndExtraSpaces() | ||
14 | { | ||
15 | $tagRepo = $this->getTagRepositoryMock(); | ||
16 | $tagsAssigner = new TagsAssigner($tagRepo); | ||
17 | |||
18 | $entry = new Entry(new User()); | ||
19 | |||
20 | $tagsAssigner->assignTagsToEntry($entry, [' tag1', 'tag2 ']); | ||
21 | |||
22 | $this->assertCount(2, $entry->getTags()); | ||
23 | $this->assertSame('tag1', $entry->getTags()[0]->getLabel()); | ||
24 | $this->assertSame('tag2', $entry->getTags()[1]->getLabel()); | ||
25 | } | ||
26 | |||
27 | public function testAssignTagsWithString() | ||
28 | { | ||
29 | $tagRepo = $this->getTagRepositoryMock(); | ||
30 | $tagsAssigner = new TagsAssigner($tagRepo); | ||
31 | |||
32 | $entry = new Entry(new User()); | ||
33 | |||
34 | $tagsAssigner->assignTagsToEntry($entry, 'tag1, tag2'); | ||
35 | |||
36 | $this->assertCount(2, $entry->getTags()); | ||
37 | $this->assertSame('tag1', $entry->getTags()[0]->getLabel()); | ||
38 | $this->assertSame('tag2', $entry->getTags()[1]->getLabel()); | ||
39 | } | ||
40 | |||
41 | public function testAssignTagsWithEmptyArray() | ||
42 | { | ||
43 | $tagRepo = $this->getTagRepositoryMock(); | ||
44 | $tagsAssigner = new TagsAssigner($tagRepo); | ||
45 | |||
46 | $entry = new Entry(new User()); | ||
47 | |||
48 | $tagsAssigner->assignTagsToEntry($entry, []); | ||
49 | |||
50 | $this->assertCount(0, $entry->getTags()); | ||
51 | } | ||
52 | |||
53 | public function testAssignTagsWithEmptyString() | ||
54 | { | ||
55 | $tagRepo = $this->getTagRepositoryMock(); | ||
56 | $tagsAssigner = new TagsAssigner($tagRepo); | ||
57 | |||
58 | $entry = new Entry(new User()); | ||
59 | |||
60 | $tagsAssigner->assignTagsToEntry($entry, ''); | ||
61 | |||
62 | $this->assertCount(0, $entry->getTags()); | ||
63 | } | ||
64 | |||
65 | public function testAssignTagsAlreadyAssigned() | ||
66 | { | ||
67 | $tagRepo = $this->getTagRepositoryMock(); | ||
68 | $tagsAssigner = new TagsAssigner($tagRepo); | ||
69 | |||
70 | $tagEntity = new Tag(); | ||
71 | $tagEntity->setLabel('tag1'); | ||
72 | |||
73 | $entry = new Entry(new User()); | ||
74 | $entry->addTag($tagEntity); | ||
75 | |||
76 | $tagsAssigner->assignTagsToEntry($entry, 'tag1, tag2'); | ||
77 | |||
78 | $this->assertCount(2, $entry->getTags()); | ||
79 | $this->assertSame('tag1', $entry->getTags()[0]->getLabel()); | ||
80 | $this->assertSame('tag2', $entry->getTags()[1]->getLabel()); | ||
81 | } | ||
82 | |||
83 | public function testAssignTagsNotFlushed() | ||
84 | { | ||
85 | $tagRepo = $this->getTagRepositoryMock(); | ||
86 | $tagRepo->expects($this->never()) | ||
87 | ->method('__call'); | ||
88 | |||
89 | $tagsAssigner = new TagsAssigner($tagRepo); | ||
90 | |||
91 | $tagEntity = new Tag(); | ||
92 | $tagEntity->setLabel('tag1'); | ||
93 | |||
94 | $entry = new Entry(new User()); | ||
95 | |||
96 | $tagsAssigner->assignTagsToEntry($entry, 'tag1', [$tagEntity]); | ||
97 | |||
98 | $this->assertCount(1, $entry->getTags()); | ||
99 | $this->assertSame('tag1', $entry->getTags()[0]->getLabel()); | ||
100 | } | ||
101 | |||
102 | private function getTagRepositoryMock() | ||
103 | { | ||
104 | return $this->getMockBuilder(TagRepository::class) | ||
105 | ->disableOriginalConstructor() | ||
106 | ->getMock(); | ||
107 | } | ||
108 | } | ||