o
    hA                     @   s   d dl Zd dlmZ d dlmZmZ d dlmZ d dl	m
Z
 ddlmZmZmZmZ G dd	 d	eZG d
d deZG dd deZG dd deZG dd deZdS )    N)apps)TemplateSyntaxErrorengines)TestCase)Site   )Imageget_test_bad_imageget_test_image_fileget_test_image_filenamec                   @   s"   e Zd ZdZdd ZdddZdS )JinjaImagesTestCaseNc                 C   s4   t d | _tjjdt d| _t | _| j	  d S )Njinja2z
Test image)titlefile)
r   enginer   objectscreater
   imager	   	bad_imagesaveself r   `/var/www/html/ndineBlogger/venv/lib/python3.10/site-packages/wagtail/images/tests/test_jinja2.pysetUp   s   
zJinjaImagesTestCase.setUpTc                 C   sN   |d u ri }|rt jjdd}| jjd|jd}||d< | j|}||S )NT)is_default_sitez/test/)	HTTP_HOSTrequest)r   r   getclienthostnamer   from_stringrender)r   stringcontextrequest_contextsiter   templater   r   r   r"      s   
zJinjaImagesTestCase.render)NT)__name__
__module____qualname__maxDiffr   r"   r   r   r   r   r      s    r   c                   @   T   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd ZdS )TestImageJinjac                 C   ,   |  | dd| jidt| jd d S )N!{{ image(myimage, "width-200") }}myimage8<img alt="Test image" src="{}" width="200" height="150">	width-200assertHTMLEqualr"   r   formatr   r   r   r   r   
test_image.   s   
zTestImageJinja.test_imagec                 C       |  ddd i}| |d d S )Nz{{ image(myimage, "width-2") }}r0    r"   assertEqualr   renderedr   r   r   test_no_image6      zTestImageJinja.test_no_imagec                 C   r.   )Nz@{{ image(myimage, "width-200", alt="alternate", class="test") }}r0   zD<img alt="alternate" src="{}" width="200" height="150" class="test">r2   r3   r   r   r   r   test_image_attributes:      
z$TestImageJinja.test_image_attributesc                 C   s2   d}dt | jd }| | |d| ji| d S )NzP{% set bg=image(myimage, "width-200") %}width: {{ bg.width }}, url: {{ bg.url }}zwidth: 200, url: r2   r0   )r   r   r4   r"   )r   r'   outputr   r   r   test_image_assignmentE   s   z$TestImageJinja.test_image_assignmentc                 C   r.   )Nz0{% set bg=image(myimage, "width-200") %}{{ bg }}r0   r1   r2   r3   r   r   r   r   "test_image_assignment_render_as_isM   r@   z1TestImageJinja.test_image_assignment_render_as_isc                 C   s   |  | dd| jid d S )Nr/   r0   zE<img alt="missing image" src="/media/not-found" width="0" height="0">)r4   r"   r   r   r   r   r   test_missing_imageX   s   
z!TestImageJinja.test_missing_imagec                 C   D   |  td | dd| ji W d    d S 1 sw   Y  d S )Nz$filter specs in 'image' tag may onlyu%   {{ image(myimage, "fill-200×200") }}r0   assertRaisesRegexr   r"   r   r   r   r   r   test_invalid_character`   s
   "z%TestImageJinja.test_invalid_characterc                 C   s`   t jjjtdddddd | | dd| jid	 W d    d S 1 s)w   Y  d S )
Nwagtailimagesdefault_attrsasynclazydecodingloadingnewr/   r0   ze<img alt="missing image" src="/media/not-found" width="0" height="0" decoding="async" loading="lazy">)	unittestmockpatchobjectr   get_app_configr4   r"   r   r   r   r   r   test_custom_default_attrsf   s   
"z(TestImageJinja.test_custom_default_attrsc                 C   r.   )Nz0{{ image(myimage, "width-200|jpegquality-40") }}r0   r1   width-200.jpegquality-40r3   r   r   r   r   test_chaining_filterspecss   r@   z(TestImageJinja.test_chaining_filterspecsN)r(   r)   r*   r6   r=   r?   rB   rC   rD   rH   rW   rY   r   r   r   r   r-   -   s    r-   c                   @   s   e Zd Zdd Zdd ZdS )TestImageURLJinjac                 C   4   |  | dd| jid| jjjdd  d S )Nz%{{ image_url(myimage, "width-200") }}r0   z/images/.*/width-200/{}/assertRegexr"   r   r5   r   namesplitr   r   r   r   test_image_url   s   
z TestImageURLJinja.test_image_urlc                 C   r[   )NzH{{ image_url(myimage, "width-200", "wagtailimages_serve_custom_view") }}r0   z'/testimages/custom_view/.*/width-200/{}r\   r]   r^   r   r   r   r   test_image_url_custom_view   s   z,TestImageURLJinja.test_image_url_custom_viewN)r(   r)   r*   rb   rc   r   r   r   r   rZ      s    rZ   c                   @   r,   )TestSrcsetImageJinjac                 C   sR   t | jd}t | jd}| dd| ji}d| d| d| d}| || d S )	Nr2   	width-400={{ srcset_image(myimage, "width-{200,400}", sizes="100vw") }}r0   E
            <img
                sizes="100vw"
                src=""
                srcset=" 200w, w 400w"
                alt="Test image"
                width="200"
                height="150"
            >
        r   r   r"   r4   )r   filename_200filename_400r<   expectedr   r   r   test_srcset_image   s   
z&TestSrcsetImageJinja.test_srcset_imagec                 C   r7   )Nz&{{ srcset_image(myimage, "width-2") }}r0   r8   r9   r;   r   r   r   r=      s   z"TestSrcsetImageJinja.test_no_imagec                 C   s,   |  | dd| ji| dd| ji d S )Nz({{ srcset_image(myimage, "width-200") }}r0   r/   )r4   r"   r   r   r   r   r   test_srcset_output_single_image   s   z4TestSrcsetImageJinja.test_srcset_output_single_imagec                 C   sH   d}|  |d| ji}dt| jd dt| jd d}| || d S )Nz{% set bg=srcset_image(myimage, "width-{200,400}") %}width: {{ bg.renditions[0].width }}, url: {{ bg.renditions[0].url }} width: {{ bg.renditions[1].width }}, url: {{ bg.renditions[1].url }} r0   
            width: 200, url: r2   
            width: 400, url: re   	
        r"   r   r   r4   r   r'   r<   rn   r   r   r   test_srcset_image_assignment   s   

z1TestSrcsetImageJinja.test_srcset_image_assignmentc                 C   4   |  dd| ji}|  dd| ji}| || d S )Nz={% set bg=srcset_image(myimage, "width-{200,400}") %}{{ bg }}r0   z.{{ srcset_image(myimage, "width-{200,400}") }}r"   r   r4   r   r<   rn   r   r   r   )test_srcset_image_assignment_render_as_is      z>TestSrcsetImageJinja.test_srcset_image_assignment_render_as_isc                 C   &   |  dd| ji}d}| || d S )Nrf   r0   a  
            <img
                sizes="100vw"
                src="/media/not-found"
                srcset="/media/not-found 0w, /media/not-found 0w"
                alt="missing image"
                width="0"
                height="0"
            >
        r"   r   r4   ry   r   r   r   test_missing_srcset_image   s   
z.TestSrcsetImageJinja.test_missing_srcset_imagec                 C   rE   )Nz+filter specs in 'srcset_image' tag may onlyuB   {{ srcset_image(myimage, "fill-{20×20,40×40}", sizes="100vw") }}r0   rF   r   r   r   r   rH         "z+TestSrcsetImageJinja.test_invalid_characterc                 C   h   t jjjtdddddd | dd| ji}d	}| || W d    d S 1 s-w   Y  d S )
NrI   rJ   rK   rL   rM   rP   z;{{ srcset_image(myimage, "width-{20,40}", sizes="100vw") }}r0   at  
                <img
                    sizes="100vw"
                    src="/media/not-found"
                    srcset="/media/not-found 0w, /media/not-found 0w"
                    alt="missing image"
                    width="0"
                    height="0"
                    decoding="async"
                    loading="lazy"
                >
            	rR   rS   rT   rU   r   rV   r"   r   r4   ry   r   r   r   rW      s   "z.TestSrcsetImageJinja.test_custom_default_attrsc                 C   s^   t | jdt | jdg}| dd| ji}d|d  d|d  d|d	  d
}| || d S )NrX   zwidth-400.jpegquality-40zL{{ srcset_image(myimage, "width-{200,400}|jpegquality-40", sizes="100vw") }}r0   rg   r   rh   ri   r   rj   rk   r   	filenamesr<   rn   r   r   r   rY     s   


z.TestSrcsetImageJinja.test_chaining_filterspecsN)r(   r)   r*   ro   r=   rp   rv   rz   r~   rH   rW   rY   r   r   r   r   rd      s    	rd   c                   @   sd   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd Zdd Zdd ZdS )TestPictureJinjac                 C   s   t | jdt | jdt | jdt | jdt | jdt | jdg}| dd| ji}d	|d
  d|d  d|d  d|d  d|d  d|d  d|d  d}| || d S )Nwidth-200.format-jpegwidth-400.format-jpegwidth-200.format-webpwidth-400.format-webpzwidth-200.format-gifzwidth-400.format-gifO{{ picture(myimage, "width-{200,400}|format-{jpeg,webp,gif}", sizes="100vw") }}r0   3
            <picture>
            <source srcset="   ri      zD 400w" sizes="100vw" type="image/webp">
            <source srcset="r   r   zl 400w" sizes="100vw" type="image/jpeg">
            <img
                sizes="100vw"
                src="   rh      z 400w"
                alt="Test image"
                width="200"
                height="150"
            >
            </picture>
        rk   r   r   r   r    test_picture_formats_multi_sizes  s6   





	z1TestPictureJinja.test_picture_formats_multi_sizesc                 C   L   t | jd}t | jd}| dd| ji}d| d| d}| || d S )Nzformat-jpegzformat-webp,{{ picture(myimage, "format-{jpeg,webp}") }}r0   r   ;" type="image/webp">
            <img
                src=""
                alt="Test image"
                width="640"
                height="480"
            >
            </picture>
        rk   r   filename_jpegfilename_webpr<   rn   r   r   r   test_picture_formats_only9  s   z*TestPictureJinja.test_picture_formats_onlyc                 C   rw   )Nz8{{ picture(myimage, "width-{200,400}", sizes="100vw") }}r0   zP<picture>{{ srcset_image(myimage, "width-{200,400}", sizes="100vw") }}</picture>rx   ry   r   r   r   test_picture_sizes_onlyN  r{   z(TestPictureJinja.test_picture_sizes_onlyc                 C   rw   )Nz%{{ picture(myimage, "format-jpeg") }}r0   z6<picture>{{ image(myimage, "format-jpeg") }}</picture>rx   ry   r   r   r   test_picture_single_formatY  r{   z+TestPictureJinja.test_picture_single_formatc                 C   r7   )Nz!{{ picture(myimage, "width-2") }}r0   r8   r9   r;   r   r   r   r=   d  r>   zTestPictureJinja.test_no_imagec              
   C   sd   d}|  |d| ji}dt| jd dt| jd dt| jd dt| jd d		}| || d S )
Na  {% set bg=picture(myimage, "width-{200,400}|format-{jpeg,webp}") %}width: {{ bg.formats['jpeg'][0].width }}, url: {{ bg.formats['jpeg'][0].url }} width: {{ bg.formats['jpeg'][1].width }}, url: {{ bg.formats['jpeg'][1].url }} width: {{ bg.formats['webp'][0].width }}, url: {{ bg.formats['webp'][0].url }} width: {{ bg.formats['webp'][1].width }}, url: {{ bg.formats['webp'][1].url }} r0   rq   r   rr   r   r   r   rs   rt   ru   r   r   r   test_picture_assignmenth  s   



z(TestPictureJinja.test_picture_assignmentc                 C   rw   )Nz^{% set bg=picture(myimage, "width-{200,400}|format-{jpeg,webp,gif}", sizes="100vw") %}{{ bg }}r0   r   rx   ry   r   r   r   $test_picture_assignment_render_as_isy  r{   z5TestPictureJinja.test_picture_assignment_render_as_isc                 C   r|   )Nr   r0   a2  
            <picture>
                <source srcset="/media/not-found" type="image/webp">
                <img
                    src="/media/not-found"
                    alt="missing image"
                    width="0"
                    height="0"
                >
            </picture>
        r}   ry   r   r   r   test_missing_picture  s   z%TestPictureJinja.test_missing_picturec                 C   rE   )Nz&filter specs in 'picture' tag may onlyu=   {{ picture(myimage, "fill-{20×20,40×40}", sizes="100vw") }}r0   rF   r   r   r   r   rH     r   z'TestPictureJinja.test_invalid_characterc                 C   r   )
NrI   rJ   rK   rL   rM   rP   r   r0   a  
                <picture>
                    <source srcset="/media/not-found" type="image/webp">
                    <img
                        src="/media/not-found"
                        alt="missing image"
                        width="0"
                        height="0"
                        decoding="async"
                        loading="lazy"
                    >
                </picture>
            r   ry   r   r   r   rW     s   "z*TestPictureJinja.test_custom_default_attrsc                 C   r   )Nz)format-jpeg.jpegquality-40.webpquality-40z)format-webp.jpegquality-40.webpquality-40zJ{{ picture(myimage, "format-{jpeg,webp}|jpegquality-40|webpquality-40") }}r0   r   r   r   rk   r   r   r   r   rY     s    z*TestPictureJinja.test_chaining_filterspecsN)r(   r)   r*   r   r   r   r   r=   r   r   r   rH   rW   rY   r   r   r   r   r     s    	r   )unittest.mockrR   django.appsr   django.templater   r   django.testr   wagtail.modelsr   utilsr   r	   r
   r   r   r-   rZ   rd   r   r   r   r   r   <module>   s    R 