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.

61 lines
2.3 KiB

  1. # Copyright 2016-2017 Jairo Llopis <jairo.llopis@tecnativa.com>
  2. # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
  3. from odoo.tools import mute_logger
  4. from odoo.tests.common import TransactionCase
  5. class ExtractorCase(TransactionCase):
  6. def setUp(self):
  7. super(ExtractorCase, self).setUp()
  8. # Shortcut
  9. self.imgs_from_html = self.env["ir.fields.converter"].imgs_from_html
  10. def test_mixed_images_found(self):
  11. """Images correctly found in <img> elements and backgrounds."""
  12. content = u"""
  13. <div>
  14. <!-- src-less img -->
  15. <img/>
  16. <p/>
  17. <img src="/path/0"/>
  18. <img src="/path/1"/>
  19. <img src="/path/2"/>
  20. <img src="/path/3"/>
  21. <section style="background : URL('/path/4');;background;ö;">
  22. <div style='BACKGROUND-IMAGE:url(/path/5)'>
  23. <p style="background:uRl(&quot;/path/6&quot;)">
  24. <img src="/path/7"/>
  25. </p>
  26. </div>
  27. </section>
  28. </div>
  29. """
  30. # Read all images
  31. for n, url in enumerate(self.imgs_from_html(content)):
  32. self.assertEqual("/path/%d" % n, url)
  33. self.assertEqual(n, 7)
  34. # Read only first image
  35. for n, url in enumerate(self.imgs_from_html(content, 1)):
  36. self.assertEqual("/path/%d" % n, url)
  37. self.assertEqual(n, 0)
  38. @mute_logger("odoo.addons.html_image_url_extractor" +
  39. ".models.ir_fields_converter")
  40. def test_empty_html(self):
  41. """Empty HTML handled correctly."""
  42. for laps, text in self.imgs_from_html(""):
  43. self.assertTrue(False) # You should never get here
  44. with self.assertRaises(Exception):
  45. list(self.imgs_from_html("", fail=True))
  46. @mute_logger("odoo.addons.html_image_url_extractor" +
  47. ".models.ir_fields_converter")
  48. def test_false_html(self):
  49. """``False`` HTML handled correctly."""
  50. for laps, text in self.imgs_from_html(False):
  51. self.assertTrue(False) # You should never get here
  52. with self.assertRaises(Exception):
  53. list(self.imgs_from_html(False, fail=True))