diff options
Diffstat (limited to 'tests/Wallabag/CoreBundle/Helper/ContentProxyTest.php')
-rw-r--r-- | tests/Wallabag/CoreBundle/Helper/ContentProxyTest.php | 375 |
1 files changed, 254 insertions, 121 deletions
diff --git a/tests/Wallabag/CoreBundle/Helper/ContentProxyTest.php b/tests/Wallabag/CoreBundle/Helper/ContentProxyTest.php index 4f70ed0c..95dd75ba 100644 --- a/tests/Wallabag/CoreBundle/Helper/ContentProxyTest.php +++ b/tests/Wallabag/CoreBundle/Helper/ContentProxyTest.php | |||
@@ -3,10 +3,17 @@ | |||
3 | namespace Tests\Wallabag\CoreBundle\Helper; | 3 | namespace Tests\Wallabag\CoreBundle\Helper; |
4 | 4 | ||
5 | use Psr\Log\NullLogger; | 5 | use Psr\Log\NullLogger; |
6 | use Monolog\Logger; | ||
7 | use Monolog\Handler\TestHandler; | ||
6 | use Wallabag\CoreBundle\Helper\ContentProxy; | 8 | use Wallabag\CoreBundle\Helper\ContentProxy; |
7 | use Wallabag\CoreBundle\Entity\Entry; | 9 | use Wallabag\CoreBundle\Entity\Entry; |
8 | use Wallabag\CoreBundle\Entity\Tag; | 10 | use Wallabag\CoreBundle\Entity\Tag; |
9 | use Wallabag\UserBundle\Entity\User; | 11 | use Wallabag\UserBundle\Entity\User; |
12 | use Wallabag\CoreBundle\Helper\RuleBasedTagger; | ||
13 | use Graby\Graby; | ||
14 | use Symfony\Component\Validator\Validator\RecursiveValidator; | ||
15 | use Symfony\Component\Validator\ConstraintViolationList; | ||
16 | use Symfony\Component\Validator\ConstraintViolation; | ||
10 | 17 | ||
11 | class ContentProxyTest extends \PHPUnit_Framework_TestCase | 18 | class ContentProxyTest extends \PHPUnit_Framework_TestCase |
12 | { | 19 | { |
@@ -33,8 +40,9 @@ 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->assertEquals('http://user@:80', $entry->getUrl()); |
40 | $this->assertEmpty($entry->getTitle()); | 48 | $this->assertEmpty($entry->getTitle()); |
@@ -67,8 +75,9 @@ 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->assertEquals('http://0.0.0.0', $entry->getUrl()); |
74 | $this->assertEmpty($entry->getTitle()); | 83 | $this->assertEmpty($entry->getTitle()); |
@@ -106,12 +115,13 @@ 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->assertEquals('http://domain.io', $entry->getUrl()); |
113 | $this->assertEquals('my title', $entry->getTitle()); | 123 | $this->assertEquals('my title', $entry->getTitle()); |
114 | $this->assertEquals($this->fetchingErrorMessage . '<p><i>But we found a short description: </i></p>desc', $entry->getContent()); | 124 | $this->assertEquals($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()); |
@@ -147,8 +157,9 @@ 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->assertEquals('http://1.1.1.1', $entry->getUrl()); |
154 | $this->assertEquals('this is my title', $entry->getTitle()); | 165 | $this->assertEquals('this is my title', $entry->getTitle()); |
@@ -184,17 +195,18 @@ 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->assertEquals('http://1.1.1.1', $entry->getUrl()); |
195 | $this->assertEquals('this is my title', $entry->getTitle()); | 207 | $this->assertEquals('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->assertEmpty($entry->getPreviewPicture()); |
198 | $this->assertEquals('text/html', $entry->getMimetype()); | 210 | $this->assertEquals('text/html', $entry->getMimetype()); |
199 | $this->assertEquals('fr', $entry->getLanguage()); | 211 | $this->assertEquals('fr', $entry->getLanguage()); |
200 | $this->assertEquals('200', $entry->getHttpStatus()); | 212 | $this->assertEquals('200', $entry->getHttpStatus()); |
@@ -202,187 +214,308 @@ class ContentProxyTest extends \PHPUnit_Framework_TestCase | |||
202 | $this->assertEquals('1.1.1.1', $entry->getDomainName()); | 214 | $this->assertEquals('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(); |
224 | $validator->expects($this->exactly(2)) | ||
225 | ->method('validate') | ||
226 | ->will($this->onConsecutiveCalls( | ||
227 | new ConstraintViolationList([new ConstraintViolation('oops', 'oops', [], 'oops', 'language', 'dontexist')]), | ||
228 | new ConstraintViolationList() | ||
229 | )); | ||
230 | |||
231 | $graby = $this->getMockBuilder('Graby\Graby') | ||
232 | ->setMethods(['fetchContent']) | ||
233 | ->disableOriginalConstructor() | ||
234 | ->getMock(); | ||
212 | 235 | ||
213 | $proxy = new ContentProxy($graby, $tagger, $this->getTagRepositoryMock(), $this->getLogger(), $this->fetchingErrorMessage); | 236 | $graby->expects($this->any()) |
214 | $entry = $proxy->updateEntry(new Entry(new User()), 'http://0.0.0.0', [ | 237 | ->method('fetchContent') |
215 | 'html' => str_repeat('this is my content', 325), | 238 | ->willReturn([ |
216 | 'title' => 'this is my title', | 239 | 'html' => str_repeat('this is my content', 325), |
217 | 'url' => 'http://1.1.1.1', | 240 | 'title' => 'this is my title', |
218 | 'content_type' => 'text/html', | 241 | 'url' => 'http://1.1.1.1', |
219 | 'language' => 'fr', | 242 | 'content_type' => 'text/html', |
220 | ]); | 243 | 'language' => 'dontexist', |
244 | 'status' => '200', | ||
245 | ]); | ||
246 | |||
247 | $proxy = new ContentProxy($graby, $tagger, $validator, $this->getLogger(), $this->fetchingErrorMessage); | ||
248 | $entry = new Entry(new User()); | ||
249 | $proxy->updateEntry($entry, 'http://0.0.0.0'); | ||
221 | 250 | ||
222 | $this->assertEquals('http://1.1.1.1', $entry->getUrl()); | 251 | $this->assertEquals('http://1.1.1.1', $entry->getUrl()); |
223 | $this->assertEquals('this is my title', $entry->getTitle()); | 252 | $this->assertEquals('this is my title', $entry->getTitle()); |
224 | $this->assertContains('this is my content', $entry->getContent()); | 253 | $this->assertContains('this is my content', $entry->getContent()); |
225 | $this->assertEquals('text/html', $entry->getMimetype()); | 254 | $this->assertEquals('text/html', $entry->getMimetype()); |
226 | $this->assertEquals('fr', $entry->getLanguage()); | 255 | $this->assertEmpty($entry->getLanguage()); |
256 | $this->assertEquals('200', $entry->getHttpStatus()); | ||
227 | $this->assertEquals(4.0, $entry->getReadingTime()); | 257 | $this->assertEquals(4.0, $entry->getReadingTime()); |
228 | $this->assertEquals('1.1.1.1', $entry->getDomainName()); | 258 | $this->assertEquals('1.1.1.1', $entry->getDomainName()); |
229 | } | 259 | } |
230 | 260 | ||
231 | public function testTaggerThrowException() | 261 | public function testWithContentAndBadOgImage() |
232 | { | 262 | { |
233 | $graby = $this->getMockBuilder('Graby\Graby') | ||
234 | ->disableOriginalConstructor() | ||
235 | ->getMock(); | ||
236 | |||
237 | $tagger = $this->getTaggerMock(); | 263 | $tagger = $this->getTaggerMock(); |
238 | $tagger->expects($this->once()) | 264 | $tagger->expects($this->once()) |
239 | ->method('tag') | 265 | ->method('tag'); |
240 | ->will($this->throwException(new \Exception())); | ||
241 | |||
242 | $tagRepo = $this->getTagRepositoryMock(); | ||
243 | $proxy = new ContentProxy($graby, $tagger, $tagRepo, $this->getLogger(), $this->fetchingErrorMessage); | ||
244 | |||
245 | $entry = $proxy->updateEntry(new Entry(new User()), 'http://0.0.0.0', [ | ||
246 | 'html' => str_repeat('this is my content', 325), | ||
247 | 'title' => 'this is my title', | ||
248 | 'url' => 'http://1.1.1.1', | ||
249 | 'content_type' => 'text/html', | ||
250 | 'language' => 'fr', | ||
251 | ]); | ||
252 | 266 | ||
253 | $this->assertCount(0, $entry->getTags()); | 267 | $validator = $this->getValidator(); |
254 | } | 268 | $validator->expects($this->exactly(2)) |
269 | ->method('validate') | ||
270 | ->will($this->onConsecutiveCalls( | ||
271 | new ConstraintViolationList(), | ||
272 | new ConstraintViolationList([new ConstraintViolation('oops', 'oops', [], 'oops', 'url', 'https://')]) | ||
273 | )); | ||
255 | 274 | ||
256 | public function testAssignTagsWithArrayAndExtraSpaces() | ||
257 | { | ||
258 | $graby = $this->getMockBuilder('Graby\Graby') | 275 | $graby = $this->getMockBuilder('Graby\Graby') |
276 | ->setMethods(['fetchContent']) | ||
259 | ->disableOriginalConstructor() | 277 | ->disableOriginalConstructor() |
260 | ->getMock(); | 278 | ->getMock(); |
261 | 279 | ||
262 | $tagRepo = $this->getTagRepositoryMock(); | 280 | $graby->expects($this->any()) |
263 | $proxy = new ContentProxy($graby, $this->getTaggerMock(), $tagRepo, $this->getLogger(), $this->fetchingErrorMessage); | 281 | ->method('fetchContent') |
282 | ->willReturn([ | ||
283 | 'html' => str_repeat('this is my content', 325), | ||
284 | 'title' => 'this is my title', | ||
285 | 'url' => 'http://1.1.1.1', | ||
286 | 'content_type' => 'text/html', | ||
287 | 'language' => 'fr', | ||
288 | 'status' => '200', | ||
289 | 'open_graph' => [ | ||
290 | 'og_title' => 'my OG title', | ||
291 | 'og_description' => 'OG desc', | ||
292 | 'og_image' => 'https://', | ||
293 | ], | ||
294 | ]); | ||
264 | 295 | ||
296 | $proxy = new ContentProxy($graby, $tagger, $validator, $this->getLogger(), $this->fetchingErrorMessage); | ||
265 | $entry = new Entry(new User()); | 297 | $entry = new Entry(new User()); |
298 | $proxy->updateEntry($entry, 'http://0.0.0.0'); | ||
266 | 299 | ||
267 | $proxy->assignTagsToEntry($entry, [' tag1', 'tag2 ']); | 300 | $this->assertEquals('http://1.1.1.1', $entry->getUrl()); |
268 | 301 | $this->assertEquals('this is my title', $entry->getTitle()); | |
269 | $this->assertCount(2, $entry->getTags()); | 302 | $this->assertContains('this is my content', $entry->getContent()); |
270 | $this->assertEquals('tag1', $entry->getTags()[0]->getLabel()); | 303 | $this->assertEmpty($entry->getPreviewPicture()); |
271 | $this->assertEquals('tag2', $entry->getTags()[1]->getLabel()); | 304 | $this->assertEquals('text/html', $entry->getMimetype()); |
305 | $this->assertEquals('fr', $entry->getLanguage()); | ||
306 | $this->assertEquals('200', $entry->getHttpStatus()); | ||
307 | $this->assertEquals(4.0, $entry->getReadingTime()); | ||
308 | $this->assertEquals('1.1.1.1', $entry->getDomainName()); | ||
272 | } | 309 | } |
273 | 310 | ||
274 | public function testAssignTagsWithString() | 311 | public function testWithForcedContent() |
275 | { | 312 | { |
276 | $graby = $this->getMockBuilder('Graby\Graby') | 313 | $tagger = $this->getTaggerMock(); |
277 | ->disableOriginalConstructor() | 314 | $tagger->expects($this->once()) |
278 | ->getMock(); | 315 | ->method('tag'); |
279 | |||
280 | $tagRepo = $this->getTagRepositoryMock(); | ||
281 | $proxy = new ContentProxy($graby, $this->getTaggerMock(), $tagRepo, $this->getLogger(), $this->fetchingErrorMessage); | ||
282 | 316 | ||
317 | $proxy = new ContentProxy((new Graby()), $tagger, $this->getValidator(), $this->getLogger(), $this->fetchingErrorMessage); | ||
283 | $entry = new Entry(new User()); | 318 | $entry = new Entry(new User()); |
319 | $proxy->updateEntry( | ||
320 | $entry, | ||
321 | 'http://0.0.0.0', | ||
322 | [ | ||
323 | 'html' => str_repeat('this is my content', 325), | ||
324 | 'title' => 'this is my title', | ||
325 | 'url' => 'http://1.1.1.1', | ||
326 | 'content_type' => 'text/html', | ||
327 | 'language' => 'fr', | ||
328 | 'date' => '1395635872', | ||
329 | 'authors' => ['Jeremy', 'Nico', 'Thomas'], | ||
330 | 'all_headers' => [ | ||
331 | 'Cache-Control' => 'no-cache', | ||
332 | ], | ||
333 | ] | ||
334 | ); | ||
284 | 335 | ||
285 | $proxy->assignTagsToEntry($entry, 'tag1, tag2'); | 336 | $this->assertEquals('http://1.1.1.1', $entry->getUrl()); |
286 | 337 | $this->assertEquals('this is my title', $entry->getTitle()); | |
287 | $this->assertCount(2, $entry->getTags()); | 338 | $this->assertContains('this is my content', $entry->getContent()); |
288 | $this->assertEquals('tag1', $entry->getTags()[0]->getLabel()); | 339 | $this->assertEquals('text/html', $entry->getMimetype()); |
289 | $this->assertEquals('tag2', $entry->getTags()[1]->getLabel()); | 340 | $this->assertEquals('fr', $entry->getLanguage()); |
341 | $this->assertEquals(4.0, $entry->getReadingTime()); | ||
342 | $this->assertEquals('1.1.1.1', $entry->getDomainName()); | ||
343 | $this->assertEquals('24/03/2014', $entry->getPublishedAt()->format('d/m/Y')); | ||
344 | $this->assertContains('Jeremy', $entry->getPublishedBy()); | ||
345 | $this->assertContains('Nico', $entry->getPublishedBy()); | ||
346 | $this->assertContains('Thomas', $entry->getPublishedBy()); | ||
347 | $this->assertContains('no-cache', $entry->getHeaders()); | ||
290 | } | 348 | } |
291 | 349 | ||
292 | public function testAssignTagsWithEmptyArray() | 350 | public function testWithForcedContentAndDatetime() |
293 | { | 351 | { |
294 | $graby = $this->getMockBuilder('Graby\Graby') | 352 | $tagger = $this->getTaggerMock(); |
295 | ->disableOriginalConstructor() | 353 | $tagger->expects($this->once()) |
296 | ->getMock(); | 354 | ->method('tag'); |
297 | 355 | ||
298 | $tagRepo = $this->getTagRepositoryMock(); | 356 | $logHandler = new TestHandler(); |
299 | $proxy = new ContentProxy($graby, $this->getTaggerMock(), $tagRepo, $this->getLogger(), $this->fetchingErrorMessage); | 357 | $logger = new Logger('test', [$logHandler]); |
300 | 358 | ||
359 | $proxy = new ContentProxy((new Graby()), $tagger, $this->getValidator(), $logger, $this->fetchingErrorMessage); | ||
301 | $entry = new Entry(new User()); | 360 | $entry = new Entry(new User()); |
361 | $proxy->updateEntry( | ||
362 | $entry, | ||
363 | 'http://1.1.1.1', | ||
364 | [ | ||
365 | 'html' => str_repeat('this is my content', 325), | ||
366 | 'title' => 'this is my title', | ||
367 | 'url' => 'http://1.1.1.1', | ||
368 | 'content_type' => 'text/html', | ||
369 | 'language' => 'fr', | ||
370 | 'date' => '2016-09-08T11:55:58+0200', | ||
371 | ] | ||
372 | ); | ||
302 | 373 | ||
303 | $proxy->assignTagsToEntry($entry, []); | 374 | $this->assertEquals('http://1.1.1.1', $entry->getUrl()); |
304 | 375 | $this->assertEquals('this is my title', $entry->getTitle()); | |
305 | $this->assertCount(0, $entry->getTags()); | 376 | $this->assertContains('this is my content', $entry->getContent()); |
377 | $this->assertEquals('text/html', $entry->getMimetype()); | ||
378 | $this->assertEquals('fr', $entry->getLanguage()); | ||
379 | $this->assertEquals(4.0, $entry->getReadingTime()); | ||
380 | $this->assertEquals('1.1.1.1', $entry->getDomainName()); | ||
381 | $this->assertEquals('08/09/2016', $entry->getPublishedAt()->format('d/m/Y')); | ||
306 | } | 382 | } |
307 | 383 | ||
308 | public function testAssignTagsWithEmptyString() | 384 | public function testWithForcedContentAndBadDate() |
309 | { | 385 | { |
310 | $graby = $this->getMockBuilder('Graby\Graby') | 386 | $tagger = $this->getTaggerMock(); |
311 | ->disableOriginalConstructor() | 387 | $tagger->expects($this->once()) |
312 | ->getMock(); | 388 | ->method('tag'); |
313 | 389 | ||
314 | $tagRepo = $this->getTagRepositoryMock(); | 390 | $logger = new Logger('foo'); |
315 | $proxy = new ContentProxy($graby, $this->getTaggerMock(), $tagRepo, $this->getLogger(), $this->fetchingErrorMessage); | 391 | $handler = new TestHandler(); |
392 | $logger->pushHandler($handler); | ||
316 | 393 | ||
394 | $proxy = new ContentProxy((new Graby()), $tagger, $this->getValidator(), $logger, $this->fetchingErrorMessage); | ||
317 | $entry = new Entry(new User()); | 395 | $entry = new Entry(new User()); |
396 | $proxy->updateEntry( | ||
397 | $entry, | ||
398 | 'http://1.1.1.1', | ||
399 | [ | ||
400 | 'html' => str_repeat('this is my content', 325), | ||
401 | 'title' => 'this is my title', | ||
402 | 'url' => 'http://1.1.1.1', | ||
403 | 'content_type' => 'text/html', | ||
404 | 'language' => 'fr', | ||
405 | 'date' => '01 02 2012', | ||
406 | ] | ||
407 | ); | ||
408 | |||
409 | $this->assertEquals('http://1.1.1.1', $entry->getUrl()); | ||
410 | $this->assertEquals('this is my title', $entry->getTitle()); | ||
411 | $this->assertContains('this is my content', $entry->getContent()); | ||
412 | $this->assertEquals('text/html', $entry->getMimetype()); | ||
413 | $this->assertEquals('fr', $entry->getLanguage()); | ||
414 | $this->assertEquals(4.0, $entry->getReadingTime()); | ||
415 | $this->assertEquals('1.1.1.1', $entry->getDomainName()); | ||
416 | $this->assertNull($entry->getPublishedAt()); | ||
318 | 417 | ||
319 | $proxy->assignTagsToEntry($entry, ''); | 418 | $records = $handler->getRecords(); |
320 | 419 | ||
321 | $this->assertCount(0, $entry->getTags()); | 420 | $this->assertCount(1, $records); |
421 | $this->assertContains('Error while defining date', $records[0]['message']); | ||
322 | } | 422 | } |
323 | 423 | ||
324 | public function testAssignTagsAlreadyAssigned() | 424 | public function testTaggerThrowException() |
325 | { | 425 | { |
326 | $graby = $this->getMockBuilder('Graby\Graby') | 426 | $tagger = $this->getTaggerMock(); |
327 | ->disableOriginalConstructor() | 427 | $tagger->expects($this->once()) |
328 | ->getMock(); | 428 | ->method('tag') |
329 | 429 | ->will($this->throwException(new \Exception())); | |
330 | $tagRepo = $this->getTagRepositoryMock(); | ||
331 | $proxy = new ContentProxy($graby, $this->getTaggerMock(), $tagRepo, $this->getLogger(), $this->fetchingErrorMessage); | ||
332 | |||
333 | $tagEntity = new Tag(); | ||
334 | $tagEntity->setLabel('tag1'); | ||
335 | 430 | ||
431 | $proxy = new ContentProxy((new Graby()), $tagger, $this->getValidator(), $this->getLogger(), $this->fetchingErrorMessage); | ||
336 | $entry = new Entry(new User()); | 432 | $entry = new Entry(new User()); |
337 | $entry->addTag($tagEntity); | 433 | $proxy->updateEntry( |
338 | 434 | $entry, | |
339 | $proxy->assignTagsToEntry($entry, 'tag1, tag2'); | 435 | 'http://1.1.1.1', |
436 | [ | ||
437 | 'html' => str_repeat('this is my content', 325), | ||
438 | 'title' => 'this is my title', | ||
439 | 'url' => 'http://1.1.1.1', | ||
440 | 'content_type' => 'text/html', | ||
441 | 'language' => 'fr', | ||
442 | ] | ||
443 | ); | ||
340 | 444 | ||
341 | $this->assertCount(2, $entry->getTags()); | 445 | $this->assertCount(0, $entry->getTags()); |
342 | $this->assertEquals('tag1', $entry->getTags()[0]->getLabel()); | ||
343 | $this->assertEquals('tag2', $entry->getTags()[1]->getLabel()); | ||
344 | } | 446 | } |
345 | 447 | ||
346 | public function testAssignTagsNotFlushed() | 448 | public function dataForCrazyHtml() |
347 | { | 449 | { |
348 | $graby = $this->getMockBuilder('Graby\Graby') | 450 | return [ |
349 | ->disableOriginalConstructor() | 451 | 'script and comment' => [ |
350 | ->getMock(); | 452 | '<strong>Script inside:</strong> <!--[if gte IE 4]><script>alert(\'lol\');</script><![endif]--><br />', |
351 | 453 | 'lol', | |
352 | $tagRepo = $this->getTagRepositoryMock(); | 454 | ], |
353 | $tagRepo->expects($this->never()) | 455 | 'script' => [ |
354 | ->method('__call'); | 456 | '<strong>Script inside:</strong><script>alert(\'lol\');</script>', |
355 | 457 | 'script', | |
356 | $proxy = new ContentProxy($graby, $this->getTaggerMock(), $tagRepo, $this->getLogger(), $this->fetchingErrorMessage); | 458 | ], |
459 | ]; | ||
460 | } | ||
357 | 461 | ||
358 | $tagEntity = new Tag(); | 462 | /** |
359 | $tagEntity->setLabel('tag1'); | 463 | * @dataProvider dataForCrazyHtml |
464 | */ | ||
465 | public function testWithCrazyHtmlContent($html, $escapedString) | ||
466 | { | ||
467 | $tagger = $this->getTaggerMock(); | ||
468 | $tagger->expects($this->once()) | ||
469 | ->method('tag'); | ||
360 | 470 | ||
471 | $proxy = new ContentProxy((new Graby()), $tagger, $this->getValidator(), $this->getLogger(), $this->fetchingErrorMessage); | ||
361 | $entry = new Entry(new User()); | 472 | $entry = new Entry(new User()); |
473 | $proxy->updateEntry( | ||
474 | $entry, | ||
475 | 'http://1.1.1.1', | ||
476 | [ | ||
477 | 'html' => $html, | ||
478 | 'title' => 'this is my title', | ||
479 | 'url' => 'http://1.1.1.1', | ||
480 | 'content_type' => 'text/html', | ||
481 | 'language' => 'fr', | ||
482 | 'status' => '200', | ||
483 | 'open_graph' => [ | ||
484 | 'og_title' => 'my OG title', | ||
485 | 'og_description' => 'OG desc', | ||
486 | 'og_image' => 'http://3.3.3.3/cover.jpg', | ||
487 | ], | ||
488 | ] | ||
489 | ); | ||
362 | 490 | ||
363 | $proxy->assignTagsToEntry($entry, 'tag1', [$tagEntity]); | 491 | $this->assertEquals('http://1.1.1.1', $entry->getUrl()); |
364 | 492 | $this->assertEquals('this is my title', $entry->getTitle()); | |
365 | $this->assertCount(1, $entry->getTags()); | 493 | $this->assertNotContains($escapedString, $entry->getContent()); |
366 | $this->assertEquals('tag1', $entry->getTags()[0]->getLabel()); | 494 | $this->assertEquals('http://3.3.3.3/cover.jpg', $entry->getPreviewPicture()); |
495 | $this->assertEquals('text/html', $entry->getMimetype()); | ||
496 | $this->assertEquals('fr', $entry->getLanguage()); | ||
497 | $this->assertEquals('200', $entry->getHttpStatus()); | ||
498 | $this->assertEquals('1.1.1.1', $entry->getDomainName()); | ||
367 | } | 499 | } |
368 | 500 | ||
369 | private function getTaggerMock() | 501 | private function getTaggerMock() |
370 | { | 502 | { |
371 | return $this->getMockBuilder('Wallabag\CoreBundle\Helper\RuleBasedTagger') | 503 | return $this->getMockBuilder(RuleBasedTagger::class) |
372 | ->setMethods(['tag']) | 504 | ->setMethods(['tag']) |
373 | ->disableOriginalConstructor() | 505 | ->disableOriginalConstructor() |
374 | ->getMock(); | 506 | ->getMock(); |
375 | } | 507 | } |
376 | 508 | ||
377 | private function getTagRepositoryMock() | 509 | private function getLogger() |
378 | { | 510 | { |
379 | return $this->getMockBuilder('Wallabag\CoreBundle\Repository\TagRepository') | 511 | return new NullLogger(); |
380 | ->disableOriginalConstructor() | ||
381 | ->getMock(); | ||
382 | } | 512 | } |
383 | 513 | ||
384 | private function getLogger() | 514 | private function getValidator() |
385 | { | 515 | { |
386 | return new NullLogger(); | 516 | return $this->getMockBuilder(RecursiveValidator::class) |
517 | ->setMethods(['validate']) | ||
518 | ->disableOriginalConstructor() | ||
519 | ->getMock(); | ||
387 | } | 520 | } |
388 | } | 521 | } |