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.

83 lines
3.0 KiB

  1. # -*- coding: utf-8 -*-
  2. # © 2016 Grupo ESOC Ingeniería de Servicios, S.L.U. - Jairo Llopis
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  4. from lxml import etree
  5. from openerp.tools.misc import mute_logger
  6. from openerp.tests.common import TransactionCase
  7. class ExtractorCase(TransactionCase):
  8. def setUp(self):
  9. super(ExtractorCase, self).setUp()
  10. # Shortcut
  11. self.imgs_from_html = self.env["ir.fields.converter"].imgs_from_html
  12. self.logger = ('openerp.addons.html_image_url_extractor'
  13. '.models.ir_fields_converter')
  14. def test_mixed_images_found(self):
  15. """Images correctly found in <img> elements and backgrounds."""
  16. content = u"""
  17. <div>
  18. <!-- src-less img -->
  19. <img/>
  20. <p/>
  21. <img src="/path/0"/>
  22. <img src="/path/1"/>
  23. <img src="/path/2"/>
  24. <img src="/path/3"/>
  25. <section style="background : URL('/path/4');;background;ö;">
  26. <div style='BACKGROUND-IMAGE:url(/path/5)'>
  27. <p style="background:uRl(&quot;/path/6&quot;)">
  28. <img src="/path/7"/>
  29. </p>
  30. </div>
  31. </section>
  32. </div>
  33. """
  34. # Read all images
  35. for n, url in enumerate(self.imgs_from_html(content)):
  36. self.assertEqual("/path/%d" % n, url)
  37. self.assertEqual(n, 7)
  38. # Read only first image
  39. for n, url in enumerate(self.imgs_from_html(content, 1)):
  40. self.assertEqual("/path/%d" % n, url)
  41. self.assertEqual(n, 0)
  42. def test_empty_html(self):
  43. """Empty HTML handled correctly."""
  44. with mute_logger(self.logger):
  45. for laps, text in self.imgs_from_html(""):
  46. self.assertTrue(False) # You should never get here
  47. with self.assertRaises(etree.XMLSyntaxError):
  48. with mute_logger(self.logger):
  49. list(self.imgs_from_html("", fail=True))
  50. def test_false_html(self):
  51. """``False`` HTML handled correctly."""
  52. with mute_logger(self.logger):
  53. for laps, text in self.imgs_from_html(False):
  54. self.assertTrue(False) # You should never get here
  55. with self.assertRaises(TypeError):
  56. list(self.imgs_from_html(False, fail=True))
  57. def test_bad_html(self):
  58. """Bad HTML handled correctly."""
  59. with mute_logger(self.logger):
  60. for laps, text in self.imgs_from_html("<<bad>"):
  61. self.assertTrue(False) # You should never get here
  62. try:
  63. # Newer versions of lxml parse this as
  64. # '<html><body><p>&lt;<bad/></p></body></html>'
  65. # so the exception is not guaranteed
  66. with mute_logger(self.logger):
  67. images = list(self.imgs_from_html("<<bad>", fail=True))
  68. self.assertFalse(images)
  69. except etree.ParserError:
  70. pass