You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

71 lines
3.2 KiB

  1. <?php
  2. use Symfony\Component\HttpFoundation\Request;
  3. use Symfony\Component\HttpFoundation\Response;
  4. class SanitizeTest extends PHPUnit_Framework_TestCase {
  5. private $http;
  6. public function setUp() {
  7. $this->client = new Parse();
  8. $this->client->http = new p3k\HTTPTest(dirname(__FILE__).'/data/');
  9. }
  10. private function parse($params) {
  11. $request = new Request($params);
  12. $response = new Response();
  13. return $this->client->parse($request, $response);
  14. }
  15. public function testAllowsWhitelistedTags() {
  16. $url = 'http://sanitize.example/entry-with-valid-tags';
  17. $response = $this->parse(['url' => $url]);
  18. $body = $response->getContent();
  19. $this->assertEquals(200, $response->getStatusCode());
  20. $data = json_decode($body, true);
  21. $html = $data['data']['content']['html'];
  22. $this->assertEquals('entry', $data['data']['type']);
  23. $this->assertContains('This content has only valid tags.', $html);
  24. $this->assertContains('<a href="http://sanitize.example/example">links</a>,', $html, '<a> missing');
  25. $this->assertContains('<abbr>abbreviations</abbr>,', $html, '<abbr> missing');
  26. $this->assertContains('<b>bold</b>,', $html, '<b> missing');
  27. $this->assertContains('<code>inline code</code>,', $html, '<code> missing');
  28. $this->assertContains('<del>delete</del>,', $html, '<del> missing');
  29. $this->assertContains('<em>emphasis</em>,', $html, '<em> missing');
  30. $this->assertContains('<i>italics</i>,', $html, '<i> missing');
  31. $this->assertContains('<img alt="images are allowed" src="http://sanitize.example/example.jpg" />', $html, '<img> missing');
  32. $this->assertContains('<q>inline quote</q>,', $html, '<q> missing');
  33. $this->assertContains('<strike>strikethrough</strike>,', $html, '<strike> missing');
  34. $this->assertContains('<strong>strong text</strong>,', $html, '<strong> missing');
  35. $this->assertContains('<time datetime="2016-01-01">time elements</time>', $html, '<time> missing');
  36. $this->assertContains('<blockquote>Blockquote tags are okay</blockquote>', $html);
  37. $this->assertContains('<pre>preformatted text is okay too', $html, '<pre> missing');
  38. $this->assertContains('for code examples and such</pre>', $html, '<pre> missing');
  39. $this->assertContains('<h1>One</h1>', $html, '<h1> missing');
  40. $this->assertContains('<h2>Two</h2>', $html, '<h2> missing');
  41. $this->assertContains('<h3>Three</h3>', $html, '<h3> missing');
  42. $this->assertContains('<h4>Four</h4>', $html, '<h4> missing');
  43. $this->assertContains('<h5>Five</h5>', $html, '<h5> missing');
  44. $this->assertContains('<h6>Six</h6>', $html, '<h6> missing');
  45. }
  46. public function testRemovesUnsafeTags() {
  47. $url = 'http://sanitize.example/entry-with-unsafe-tags';
  48. $response = $this->parse(['url' => $url]);
  49. $body = $response->getContent();
  50. $this->assertEquals(200, $response->getStatusCode());
  51. $data = json_decode($body, true);
  52. $html = $data['data']['content']['html'];
  53. $this->assertEquals('entry', $data['data']['type']);
  54. $this->assertNotContains('<p>', $html);
  55. $this->assertNotContains('<script>', $html);
  56. $this->assertNotContains('<style>', $html);
  57. $this->assertNotContains('visiblity', $html); // from the CSS
  58. $this->assertNotContains('alert', $html); // from the JS
  59. }
  60. }