diff options
Diffstat (limited to 'tests/Wallabag/CoreBundle/Helper')
6 files changed, 491 insertions, 124 deletions
diff --git a/tests/Wallabag/CoreBundle/Helper/ContentProxyTest.php b/tests/Wallabag/CoreBundle/Helper/ContentProxyTest.php index 4f70ed0c..dbddbc5c 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,12 +195,13 @@ 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()); |
@@ -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 | )); | ||
212 | 230 | ||
213 | $proxy = new ContentProxy($graby, $tagger, $this->getTagRepositoryMock(), $this->getLogger(), $this->fetchingErrorMessage); | 231 | $graby = $this->getMockBuilder('Graby\Graby') |
214 | $entry = $proxy->updateEntry(new Entry(new User()), 'http://0.0.0.0', [ | 232 | ->setMethods(['fetchContent']) |
215 | 'html' => str_repeat('this is my content', 325), | 233 | ->disableOriginalConstructor() |
216 | 'title' => 'this is my title', | 234 | ->getMock(); |
217 | 'url' => 'http://1.1.1.1', | 235 | |
218 | 'content_type' => 'text/html', | 236 | $graby->expects($this->any()) |
219 | 'language' => 'fr', | 237 | ->method('fetchContent') |
220 | ]); | 238 | ->willReturn([ |
239 | 'html' => str_repeat('this is my content', 325), | ||
240 | 'title' => 'this is my title', | ||
241 | 'url' => 'http://1.1.1.1', | ||
242 | 'content_type' => 'text/html', | ||
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->assertNull($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->assertNull($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 | } |
diff --git a/tests/Wallabag/CoreBundle/Helper/CryptoProxyTest.php b/tests/Wallabag/CoreBundle/Helper/CryptoProxyTest.php new file mode 100644 index 00000000..cede8696 --- /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 Psr\Log\NullLogger; | ||
6 | use Monolog\Logger; | ||
7 | use Monolog\Handler\TestHandler; | ||
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..c02f9658 100644 --- a/tests/Wallabag/CoreBundle/Helper/DownloadImagesTest.php +++ b/tests/Wallabag/CoreBundle/Helper/DownloadImagesTest.php | |||
@@ -12,7 +12,24 @@ use GuzzleHttp\Stream\Stream; | |||
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 | ||
@@ -27,9 +44,10 @@ class DownloadImagesTest extends \PHPUnit_Framework_TestCase | |||
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() |
@@ -139,4 +157,29 @@ class DownloadImagesTest extends \PHPUnit_Framework_TestCase | |||
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', array($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..f420d06a 100644 --- a/tests/Wallabag/CoreBundle/Helper/RedirectTest.php +++ b/tests/Wallabag/CoreBundle/Helper/RedirectTest.php | |||
@@ -89,4 +89,22 @@ class RedirectTest extends \PHPUnit_Framework_TestCase | |||
89 | 89 | ||
90 | $this->assertEquals($this->routerMock->generate('homepage'), $redirectUrl); | 90 | $this->assertEquals($this->routerMock->generate('homepage'), $redirectUrl); |
91 | } | 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->assertEquals('/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->assertEquals('fallback', $redirectUrl); | ||
109 | } | ||
92 | } | 110 | } |
diff --git a/tests/Wallabag/CoreBundle/Helper/RuleBasedTaggerTest.php b/tests/Wallabag/CoreBundle/Helper/RuleBasedTaggerTest.php index 17b08c2a..1e21f400 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() |
@@ -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..6d6d6484 --- /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\UserBundle\Entity\User; | ||
9 | use Wallabag\CoreBundle\Repository\TagRepository; | ||
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->assertEquals('tag1', $entry->getTags()[0]->getLabel()); | ||
24 | $this->assertEquals('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->assertEquals('tag1', $entry->getTags()[0]->getLabel()); | ||
38 | $this->assertEquals('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->assertEquals('tag1', $entry->getTags()[0]->getLabel()); | ||
80 | $this->assertEquals('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->assertEquals('tag1', $entry->getTags()[0]->getLabel()); | ||
100 | } | ||
101 | |||
102 | private function getTagRepositoryMock() | ||
103 | { | ||
104 | return $this->getMockBuilder(TagRepository::class) | ||
105 | ->disableOriginalConstructor() | ||
106 | ->getMock(); | ||
107 | } | ||
108 | } | ||