diff options
author | Jeremy Benoist <jeremy.benoist@gmail.com> | 2017-06-08 21:51:46 +0200 |
---|---|---|
committer | Jeremy Benoist <jeremy.benoist@gmail.com> | 2017-06-08 21:51:46 +0200 |
commit | 0d349ea67073c535e1aa7f19f3cf842a54458bfe (patch) | |
tree | c5449f49c941ea7cc09f7606e87464c3d03367cf /tests/Wallabag/CoreBundle | |
parent | 3f474025d889c3eff20b481f005f4d292f1ef29d (diff) | |
download | wallabag-0d349ea67073c535e1aa7f19f3cf842a54458bfe.tar.gz wallabag-0d349ea67073c535e1aa7f19f3cf842a54458bfe.tar.zst wallabag-0d349ea67073c535e1aa7f19f3cf842a54458bfe.zip |
Validate language & preview picture fields
Instead of saving the value of each field right into the content without any validation, it seems better to validate them.
This might sounds obvious now we say that.
Diffstat (limited to 'tests/Wallabag/CoreBundle')
-rw-r--r-- | tests/Wallabag/CoreBundle/Helper/ContentProxyTest.php | 129 |
1 files changed, 117 insertions, 12 deletions
diff --git a/tests/Wallabag/CoreBundle/Helper/ContentProxyTest.php b/tests/Wallabag/CoreBundle/Helper/ContentProxyTest.php index a3570125..95dd75ba 100644 --- a/tests/Wallabag/CoreBundle/Helper/ContentProxyTest.php +++ b/tests/Wallabag/CoreBundle/Helper/ContentProxyTest.php | |||
@@ -11,6 +11,9 @@ use Wallabag\CoreBundle\Entity\Tag; | |||
11 | use Wallabag\UserBundle\Entity\User; | 11 | use Wallabag\UserBundle\Entity\User; |
12 | use Wallabag\CoreBundle\Helper\RuleBasedTagger; | 12 | use Wallabag\CoreBundle\Helper\RuleBasedTagger; |
13 | use Graby\Graby; | 13 | use Graby\Graby; |
14 | use Symfony\Component\Validator\Validator\RecursiveValidator; | ||
15 | use Symfony\Component\Validator\ConstraintViolationList; | ||
16 | use Symfony\Component\Validator\ConstraintViolation; | ||
14 | 17 | ||
15 | class ContentProxyTest extends \PHPUnit_Framework_TestCase | 18 | class ContentProxyTest extends \PHPUnit_Framework_TestCase |
16 | { | 19 | { |
@@ -37,7 +40,7 @@ class ContentProxyTest extends \PHPUnit_Framework_TestCase | |||
37 | 'language' => '', | 40 | 'language' => '', |
38 | ]); | 41 | ]); |
39 | 42 | ||
40 | $proxy = new ContentProxy($graby, $tagger, $this->getLogger(), $this->fetchingErrorMessage); | 43 | $proxy = new ContentProxy($graby, $tagger, $this->getValidator(), $this->getLogger(), $this->fetchingErrorMessage); |
41 | $entry = new Entry(new User()); | 44 | $entry = new Entry(new User()); |
42 | $proxy->updateEntry($entry, 'http://user@:80'); | 45 | $proxy->updateEntry($entry, 'http://user@:80'); |
43 | 46 | ||
@@ -72,7 +75,7 @@ class ContentProxyTest extends \PHPUnit_Framework_TestCase | |||
72 | 'language' => '', | 75 | 'language' => '', |
73 | ]); | 76 | ]); |
74 | 77 | ||
75 | $proxy = new ContentProxy($graby, $tagger, $this->getLogger(), $this->fetchingErrorMessage); | 78 | $proxy = new ContentProxy($graby, $tagger, $this->getValidator(), $this->getLogger(), $this->fetchingErrorMessage); |
76 | $entry = new Entry(new User()); | 79 | $entry = new Entry(new User()); |
77 | $proxy->updateEntry($entry, 'http://0.0.0.0'); | 80 | $proxy->updateEntry($entry, 'http://0.0.0.0'); |
78 | 81 | ||
@@ -112,7 +115,7 @@ class ContentProxyTest extends \PHPUnit_Framework_TestCase | |||
112 | ], | 115 | ], |
113 | ]); | 116 | ]); |
114 | 117 | ||
115 | $proxy = new ContentProxy($graby, $tagger, $this->getLogger(), $this->fetchingErrorMessage); | 118 | $proxy = new ContentProxy($graby, $tagger, $this->getValidator(), $this->getLogger(), $this->fetchingErrorMessage); |
116 | $entry = new Entry(new User()); | 119 | $entry = new Entry(new User()); |
117 | $proxy->updateEntry($entry, 'http://domain.io'); | 120 | $proxy->updateEntry($entry, 'http://domain.io'); |
118 | 121 | ||
@@ -154,7 +157,7 @@ class ContentProxyTest extends \PHPUnit_Framework_TestCase | |||
154 | ], | 157 | ], |
155 | ]); | 158 | ]); |
156 | 159 | ||
157 | $proxy = new ContentProxy($graby, $tagger, $this->getLogger(), $this->fetchingErrorMessage); | 160 | $proxy = new ContentProxy($graby, $tagger, $this->getValidator(), $this->getLogger(), $this->fetchingErrorMessage); |
158 | $entry = new Entry(new User()); | 161 | $entry = new Entry(new User()); |
159 | $proxy->updateEntry($entry, 'http://0.0.0.0'); | 162 | $proxy->updateEntry($entry, 'http://0.0.0.0'); |
160 | 163 | ||
@@ -192,18 +195,112 @@ class ContentProxyTest extends \PHPUnit_Framework_TestCase | |||
192 | 'open_graph' => [ | 195 | 'open_graph' => [ |
193 | 'og_title' => 'my OG title', | 196 | 'og_title' => 'my OG title', |
194 | 'og_description' => 'OG desc', | 197 | 'og_description' => 'OG desc', |
195 | 'og_image' => false, | 198 | 'og_image' => null, |
196 | ], | 199 | ], |
197 | ]); | 200 | ]); |
198 | 201 | ||
199 | $proxy = new ContentProxy($graby, $tagger, $this->getLogger(), $this->fetchingErrorMessage); | 202 | $proxy = new ContentProxy($graby, $tagger, $this->getValidator(), $this->getLogger(), $this->fetchingErrorMessage); |
200 | $entry = new Entry(new User()); | 203 | $entry = new Entry(new User()); |
201 | $proxy->updateEntry($entry, 'http://0.0.0.0'); | 204 | $proxy->updateEntry($entry, 'http://0.0.0.0'); |
202 | 205 | ||
203 | $this->assertEquals('http://1.1.1.1', $entry->getUrl()); | 206 | $this->assertEquals('http://1.1.1.1', $entry->getUrl()); |
204 | $this->assertEquals('this is my title', $entry->getTitle()); | 207 | $this->assertEquals('this is my title', $entry->getTitle()); |
205 | $this->assertContains('this is my content', $entry->getContent()); | 208 | $this->assertContains('this is my content', $entry->getContent()); |
206 | $this->assertNull($entry->getPreviewPicture()); | 209 | $this->assertEmpty($entry->getPreviewPicture()); |
210 | $this->assertEquals('text/html', $entry->getMimetype()); | ||
211 | $this->assertEquals('fr', $entry->getLanguage()); | ||
212 | $this->assertEquals('200', $entry->getHttpStatus()); | ||
213 | $this->assertEquals(4.0, $entry->getReadingTime()); | ||
214 | $this->assertEquals('1.1.1.1', $entry->getDomainName()); | ||
215 | } | ||
216 | |||
217 | public function testWithContentAndBadLanguage() | ||
218 | { | ||
219 | $tagger = $this->getTaggerMock(); | ||
220 | $tagger->expects($this->once()) | ||
221 | ->method('tag'); | ||
222 | |||
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(); | ||
235 | |||
236 | $graby->expects($this->any()) | ||
237 | ->method('fetchContent') | ||
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'); | ||
250 | |||
251 | $this->assertEquals('http://1.1.1.1', $entry->getUrl()); | ||
252 | $this->assertEquals('this is my title', $entry->getTitle()); | ||
253 | $this->assertContains('this is my content', $entry->getContent()); | ||
254 | $this->assertEquals('text/html', $entry->getMimetype()); | ||
255 | $this->assertEmpty($entry->getLanguage()); | ||
256 | $this->assertEquals('200', $entry->getHttpStatus()); | ||
257 | $this->assertEquals(4.0, $entry->getReadingTime()); | ||
258 | $this->assertEquals('1.1.1.1', $entry->getDomainName()); | ||
259 | } | ||
260 | |||
261 | public function testWithContentAndBadOgImage() | ||
262 | { | ||
263 | $tagger = $this->getTaggerMock(); | ||
264 | $tagger->expects($this->once()) | ||
265 | ->method('tag'); | ||
266 | |||
267 | $validator = $this->getValidator(); | ||
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 | )); | ||
274 | |||
275 | $graby = $this->getMockBuilder('Graby\Graby') | ||
276 | ->setMethods(['fetchContent']) | ||
277 | ->disableOriginalConstructor() | ||
278 | ->getMock(); | ||
279 | |||
280 | $graby->expects($this->any()) | ||
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 | ]); | ||
295 | |||
296 | $proxy = new ContentProxy($graby, $tagger, $validator, $this->getLogger(), $this->fetchingErrorMessage); | ||
297 | $entry = new Entry(new User()); | ||
298 | $proxy->updateEntry($entry, 'http://0.0.0.0'); | ||
299 | |||
300 | $this->assertEquals('http://1.1.1.1', $entry->getUrl()); | ||
301 | $this->assertEquals('this is my title', $entry->getTitle()); | ||
302 | $this->assertContains('this is my content', $entry->getContent()); | ||
303 | $this->assertEmpty($entry->getPreviewPicture()); | ||
207 | $this->assertEquals('text/html', $entry->getMimetype()); | 304 | $this->assertEquals('text/html', $entry->getMimetype()); |
208 | $this->assertEquals('fr', $entry->getLanguage()); | 305 | $this->assertEquals('fr', $entry->getLanguage()); |
209 | $this->assertEquals('200', $entry->getHttpStatus()); | 306 | $this->assertEquals('200', $entry->getHttpStatus()); |
@@ -217,7 +314,7 @@ class ContentProxyTest extends \PHPUnit_Framework_TestCase | |||
217 | $tagger->expects($this->once()) | 314 | $tagger->expects($this->once()) |
218 | ->method('tag'); | 315 | ->method('tag'); |
219 | 316 | ||
220 | $proxy = new ContentProxy((new Graby()), $tagger, $this->getLogger(), $this->fetchingErrorMessage); | 317 | $proxy = new ContentProxy((new Graby()), $tagger, $this->getValidator(), $this->getLogger(), $this->fetchingErrorMessage); |
221 | $entry = new Entry(new User()); | 318 | $entry = new Entry(new User()); |
222 | $proxy->updateEntry( | 319 | $proxy->updateEntry( |
223 | $entry, | 320 | $entry, |
@@ -259,7 +356,7 @@ class ContentProxyTest extends \PHPUnit_Framework_TestCase | |||
259 | $logHandler = new TestHandler(); | 356 | $logHandler = new TestHandler(); |
260 | $logger = new Logger('test', [$logHandler]); | 357 | $logger = new Logger('test', [$logHandler]); |
261 | 358 | ||
262 | $proxy = new ContentProxy((new Graby()), $tagger, $logger, $this->fetchingErrorMessage); | 359 | $proxy = new ContentProxy((new Graby()), $tagger, $this->getValidator(), $logger, $this->fetchingErrorMessage); |
263 | $entry = new Entry(new User()); | 360 | $entry = new Entry(new User()); |
264 | $proxy->updateEntry( | 361 | $proxy->updateEntry( |
265 | $entry, | 362 | $entry, |
@@ -294,7 +391,7 @@ class ContentProxyTest extends \PHPUnit_Framework_TestCase | |||
294 | $handler = new TestHandler(); | 391 | $handler = new TestHandler(); |
295 | $logger->pushHandler($handler); | 392 | $logger->pushHandler($handler); |
296 | 393 | ||
297 | $proxy = new ContentProxy((new Graby()), $tagger, $logger, $this->fetchingErrorMessage); | 394 | $proxy = new ContentProxy((new Graby()), $tagger, $this->getValidator(), $logger, $this->fetchingErrorMessage); |
298 | $entry = new Entry(new User()); | 395 | $entry = new Entry(new User()); |
299 | $proxy->updateEntry( | 396 | $proxy->updateEntry( |
300 | $entry, | 397 | $entry, |
@@ -331,7 +428,7 @@ class ContentProxyTest extends \PHPUnit_Framework_TestCase | |||
331 | ->method('tag') | 428 | ->method('tag') |
332 | ->will($this->throwException(new \Exception())); | 429 | ->will($this->throwException(new \Exception())); |
333 | 430 | ||
334 | $proxy = new ContentProxy((new Graby()), $tagger, $this->getLogger(), $this->fetchingErrorMessage); | 431 | $proxy = new ContentProxy((new Graby()), $tagger, $this->getValidator(), $this->getLogger(), $this->fetchingErrorMessage); |
335 | $entry = new Entry(new User()); | 432 | $entry = new Entry(new User()); |
336 | $proxy->updateEntry( | 433 | $proxy->updateEntry( |
337 | $entry, | 434 | $entry, |
@@ -371,7 +468,7 @@ class ContentProxyTest extends \PHPUnit_Framework_TestCase | |||
371 | $tagger->expects($this->once()) | 468 | $tagger->expects($this->once()) |
372 | ->method('tag'); | 469 | ->method('tag'); |
373 | 470 | ||
374 | $proxy = new ContentProxy((new Graby()), $tagger, $this->getLogger(), $this->fetchingErrorMessage); | 471 | $proxy = new ContentProxy((new Graby()), $tagger, $this->getValidator(), $this->getLogger(), $this->fetchingErrorMessage); |
375 | $entry = new Entry(new User()); | 472 | $entry = new Entry(new User()); |
376 | $proxy->updateEntry( | 473 | $proxy->updateEntry( |
377 | $entry, | 474 | $entry, |
@@ -413,4 +510,12 @@ class ContentProxyTest extends \PHPUnit_Framework_TestCase | |||
413 | { | 510 | { |
414 | return new NullLogger(); | 511 | return new NullLogger(); |
415 | } | 512 | } |
513 | |||
514 | private function getValidator() | ||
515 | { | ||
516 | return $this->getMockBuilder(RecursiveValidator::class) | ||
517 | ->setMethods(['validate']) | ||
518 | ->disableOriginalConstructor() | ||
519 | ->getMock(); | ||
520 | } | ||
416 | } | 521 | } |