a
    DgA                     @   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 )NZjinja2z
Test image)titlefile)
r   enginer   objectscreater
   imager	   	bad_imagesaveself r   _/var/www/lab.imftr.de/x/nb_venv/lib/python3.9/site-packages/wagtail/images/tests/test_jinja2.pysetUp   s    
zJinjaImagesTestCase.setUpTc                 C   sN   |d u ri }|r8t jjdd}| jjd|jd}||d< | j|}||S )NT)Zis_default_sitez/test/)Z	HTTP_HOSTrequest)r   r   getclienthostnamer   Zfrom_stringrender)r   stringcontextrequest_contextsiter   templater   r   r   r      s    zJinjaImagesTestCase.render)NT)__name__
__module____qualname__ZmaxDiffr   r   r   r   r   r   r      s   r   c                   @   sT   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   s,   |  | 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   s    |  ddd i}| |d d S )Nz{{ image(myimage, "width-2") }}r)    r   assertEqualr   renderedr   r   r   test_no_image6   s    zTestImageJinja.test_no_imagec                 C   s,   |  | dd| jidt| jd d S )Nz@{{ image(myimage, "width-200", alt="alternate", class="test") }}r)   zD<img alt="alternate" src="{}" width="200" height="150" class="test">r+   r,   r   r   r   r   test_image_attributes:   s    
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: r+   r)   )r   r   r-   r   )r   r#   outputr   r   r   test_image_assignmentE   s    z$TestImageJinja.test_image_assignmentc                 C   s,   |  | dd| jidt| jd d S )Nz0{% set bg=image(myimage, "width-200") %}{{ bg }}r)   r*   r+   r,   r   r   r   r   "test_image_assignment_render_as_isM   s    
z1TestImageJinja.test_image_assignment_render_as_isc                 C   s   |  | dd| jid d S )Nr(   r)   zE<img alt="missing image" src="/media/not-found" width="0" height="0">)r-   r   r   r   r   r   r   test_missing_imageX   s    
z!TestImageJinja.test_missing_imagec                 C   sB   |  td" | dd| ji W d    n1 s40    Y  d S )Nz$filter specs in 'image' tag may onlyu%   {{ image(myimage, "fill-200×200") }}r)   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    n1 sP0    Y  d S )
Nwagtailimagesdefault_attrsasynclazyZdecodingZloadingnewr(   r)   ze<img alt="missing image" src="/media/not-found" width="0" height="0" decoding="async" loading="lazy">)	unittestmockpatchobjectr   get_app_configr-   r   r   r   r   r   r   test_custom_default_attrsf   s    
z(TestImageJinja.test_custom_default_attrsc                 C   s,   |  | dd| jidt| jd d S )Nz0{{ image(myimage, "width-200|jpegquality-40") }}r)   r*   width-200.jpegquality-40r,   r   r   r   r   test_chaining_filterspecss   s    
z(TestImageJinja.test_chaining_filterspecsN)r$   r%   r&   r/   r5   r6   r8   r9   r:   r=   rJ   rL   r   r   r   r   r'   -   s   r'   c                   @   s   e Zd Zdd Zdd ZdS )TestImageURLJinjac                 C   s4   |  | dd| jid| jjjdd  d S )Nz%{{ image_url(myimage, "width-200") }}r)   z/images/.*/width-200/{}/assertRegexr   r   r.   r   namesplitr   r   r   r   test_image_url   s    
z TestImageURLJinja.test_image_urlc                 C   s4   |  | dd| jid| jjjdd  d S )NzH{{ image_url(myimage, "width-200", "wagtailimages_serve_custom_view") }}r)   z'/testimages/custom_view/.*/width-200/{}rN   rO   rP   r   r   r   r   test_image_url_custom_view   s    z,TestImageURLJinja.test_image_url_custom_viewN)r$   r%   r&   rT   rU   r   r   r   r   rM      s   rM   c                   @   sT   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 )TestSrcsetImageJinjac                 C   sR   t | jd}t | jd}| dd| ji}d| d| d| d}| || d S )	Nr+   	width-400={{ srcset_image(myimage, "width-{200,400}", sizes="100vw") }}r)   E
            <img
                sizes="100vw"
                src=""
                srcset=" 200w, w 400w"
                alt="Test image"
                width="200"
                height="150"
            >
        r   r   r   r-   )r   Zfilename_200Zfilename_400r4   expectedr   r   r   test_srcset_image   s    
z&TestSrcsetImageJinja.test_srcset_imagec                 C   s    |  ddd i}| |d d S )Nz&{{ srcset_image(myimage, "width-2") }}r)   r0   r1   r3   r   r   r   r5      s    z"TestSrcsetImageJinja.test_no_imagec                 C   s,   |  | dd| ji| dd| ji d S )Nz({{ srcset_image(myimage, "width-200") }}r)   r(   )r-   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 }} r)   
            width: 200, url: r+   
            width: 400, url: rW   	
        r   r   r   r-   r   r#   r4   r^   r   r   r   test_srcset_image_assignment   s    

z1TestSrcsetImageJinja.test_srcset_image_assignmentc                 C   s4   |  dd| ji}|  dd| ji}| || d S )Nz={% set bg=srcset_image(myimage, "width-{200,400}") %}{{ bg }}r)   z.{{ srcset_image(myimage, "width-{200,400}") }}r   r   r-   r   r4   r^   r   r   r   )test_srcset_image_assignment_render_as_is   s    z>TestSrcsetImageJinja.test_srcset_image_assignment_render_as_isc                 C   s&   |  dd| ji}d}| || d S )NrX   r)   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   r-   rh   r   r   r   test_missing_srcset_image   s    
z.TestSrcsetImageJinja.test_missing_srcset_imagec                 C   sB   |  td" | dd| ji W d    n1 s40    Y  d S )Nz+filter specs in 'srcset_image' tag may onlyuB   {{ srcset_image(myimage, "fill-{20×20,40×40}", sizes="100vw") }}r)   r;   r   r   r   r   r=      s    z+TestSrcsetImageJinja.test_invalid_characterc                 C   sf   t jjjtdddddd2 | dd| ji}d	}| || W d    n1 sX0    Y  d S )
Nr>   r?   r@   rA   rB   rC   z;{{ srcset_image(myimage, "width-{20,40}", sizes="100vw") }}r)   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"
                >
            	rE   rF   rG   rH   r   rI   r   r   r-   rh   r   r   r   rJ      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 )NrK   zwidth-400.jpegquality-40zL{{ srcset_image(myimage, "width-{200,400}|jpegquality-40", sizes="100vw") }}r)   rY   r   rZ   r[   r   r\   r]   r   	filenamesr4   r^   r   r   r   rL     s    


z.TestSrcsetImageJinja.test_chaining_filterspecsN)r$   r%   r&   r_   r5   r`   rf   ri   rk   r=   rJ   rL   r   r   r   r   rV      s   	rV   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") }}r)   3
            <picture>
            <source srcset="   r[      zD 400w" sizes="100vw" type="image/webp">
            <source srcset="r   r   zl 400w" sizes="100vw" type="image/jpeg">
            <img
                sizes="100vw"
                src="   rZ      z 400w"
                alt="Test image"
                width="200"
                height="150"
            >
            </picture>
        r]   rm   r   r   r    test_picture_formats_multi_sizes  s6    





	z1TestPictureJinja.test_picture_formats_multi_sizesc                 C   sL   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}") }}r)   ru   ;" type="image/webp">
            <img
                src=""
                alt="Test image"
                width="640"
                height="480"
            >
            </picture>
        r]   r   Zfilename_jpegZfilename_webpr4   r^   r   r   r   test_picture_formats_only9  s    z*TestPictureJinja.test_picture_formats_onlyc                 C   s4   |  dd| ji}|  dd| ji}| || d S )Nz8{{ picture(myimage, "width-{200,400}", sizes="100vw") }}r)   zP<picture>{{ srcset_image(myimage, "width-{200,400}", sizes="100vw") }}</picture>rg   rh   r   r   r   test_picture_sizes_onlyN  s    z(TestPictureJinja.test_picture_sizes_onlyc                 C   s4   |  dd| ji}|  dd| ji}| || d S )Nz%{{ picture(myimage, "format-jpeg") }}r)   z6<picture>{{ image(myimage, "format-jpeg") }}</picture>rg   rh   r   r   r   test_picture_single_formatY  s    z+TestPictureJinja.test_picture_single_formatc                 C   s    |  ddd i}| |d d S )Nz!{{ picture(myimage, "width-2") }}r)   r0   r1   r3   r   r   r   r5   d  s    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 }} r)   ra   rp   rb   rq   rr   rs   rc   rd   re   r   r   r   test_picture_assignmenth  s    



z(TestPictureJinja.test_picture_assignmentc                 C   s4   |  dd| ji}|  dd| ji}| || d S )Nz^{% set bg=picture(myimage, "width-{200,400}|format-{jpeg,webp,gif}", sizes="100vw") %}{{ bg }}r)   rt   rg   rh   r   r   r   $test_picture_assignment_render_as_isy  s    z5TestPictureJinja.test_picture_assignment_render_as_isc                 C   s&   |  dd| ji}d}| || d S )Nr{   r)   a2  
            <picture>
                <source srcset="/media/not-found" type="image/webp">
                <img
                    src="/media/not-found"
                    alt="missing image"
                    width="0"
                    height="0"
                >
            </picture>
        rj   rh   r   r   r   test_missing_picture  s    z%TestPictureJinja.test_missing_picturec                 C   sB   |  td" | dd| ji W d    n1 s40    Y  d S )Nz&filter specs in 'picture' tag may onlyu=   {{ picture(myimage, "fill-{20×20,40×40}", sizes="100vw") }}r)   r;   r   r   r   r   r=     s    z'TestPictureJinja.test_invalid_characterc                 C   sf   t jjjtdddddd2 | dd| ji}d	}| || W d    n1 sX0    Y  d S )
Nr>   r?   r@   rA   rB   rC   r{   r)   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>
            rl   rh   r   r   r   rJ     s    z*TestPictureJinja.test_custom_default_attrsc                 C   sL   t | jd}t | jd}| dd| ji}d| d| d}| || d S )Nz)format-jpeg.jpegquality-40.webpquality-40z)format-webp.jpegquality-40.webpquality-40zJ{{ picture(myimage, "format-{jpeg,webp}|jpegquality-40|webpquality-40") }}r)   ru   r|   r}   r]   r~   r   r   r   rL     s     z*TestPictureJinja.test_chaining_filterspecsN)r$   r%   r&   rz   r   r   r   r5   r   r   r   r=   rJ   rL   r   r   r   r   ro     s   	ro   )Zunittest.mockrE   Zdjango.appsr   Zdjango.templater   r   Zdjango.testr   Zwagtail.modelsr   utilsr   r	   r
   r   r   r'   rM   rV   ro   r   r   r   r   <module>   s   R 