a
    Dg8)                     @   s   d dl mZ d dlmZ d dlmZ d dlmZ d dlm	Z	m
Z
 d dlmZ d dlmZmZ d dlmZ G d	d
 d
eZG dd deZG dd deZdS )    )engines)render_to_string)TestCase)	mark_safe)__version__blocks)get_dummy_request)PageSite)SectionBlockc                   @   sV   e Zd Zdd Zd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 )TestCoreGlobalsAndFiltersc                 C   s   t d | _d S )NZjinja2)r   engineself r   X/var/www/lab.imftr.de/x/nb_venv/lib/python3.9/site-packages/wagtail/tests/test_jinja2.pysetUp   s    zTestCoreGlobalsAndFilters.setUpNTc                 C   sB   |d u ri }|r,t jjdd}t|d|d< | j|}||S )NT)Zis_default_site)siterequest)r
   objectsgetr   r   Zfrom_stringrender)r   stringcontextrequest_contextr   templater   r   r   r      s    z TestCoreGlobalsAndFilters.renderc                 C   s    d}|  | dd|id d S )Nz5<p>Merry <a linktype="page" id="2">Christmas</a>!</p>z{{ text|richtext }}textz'<p>Merry <a href="/">Christmas</a>!</p>assertEqualr   )r   Zrichtextr   r   r   test_richtext   s
    z'TestCoreGlobalsAndFilters.test_richtextc                 C   s,   t jjdd}| | dd|i|j d S )N   pkz{{ pageurl(page) }}pager	   r   r   r   r   urlr   r#   r   r   r   test_pageurl#   s    z&TestCoreGlobalsAndFilters.test_pageurlc                 C   s,   t jjdd}| | dd|i|j d S )Nr    r!   z{{ fullpageurl(page) }}r#   )r	   r   r   r   r   full_urlr&   r   r   r   test_fullpageurl'   s    z*TestCoreGlobalsAndFilters.test_fullpageurlc                 C   s,   t jjdd}| | dd|i|j d S )Nr    r!   z{{ slugurl(page.slug) }}r#   r$   r&   r   r   r   test_slugurl-   s    z&TestCoreGlobalsAndFilters.test_slugurlc                 C   s   |  | di d d S )Nz&{{ slugurl("bad-slug-doesnt-exist") }}Noner   r   r   r   r   test_bad_slugurl3   s    z*TestCoreGlobalsAndFilters.test_bad_slugurlc                 C   s   |  | dd d S )Nz{{ wagtail_site().hostname }}	localhostr   r   r   r   r   test_wagtail_site8   s    z+TestCoreGlobalsAndFilters.test_wagtail_sitec                 C   s   |  | dt d S )Nz{{ wagtail_version() }})r   r   r   r   r   r   r   test_wagtail_version;   s    z.TestCoreGlobalsAndFilters.test_wagtail_version)NT)__name__
__module____qualname__r   r   r   r'   r)   r*   r,   r.   r/   r   r   r   r   r      s   
r   c                   @   s"   e Zd ZdgZdd Zdd ZdS )TestJinjaEscapingz	test.jsonc                 C   sH   t dt jddfg}|dddg}tdd|i}| d| d	S )
z
        Ensure that any results of template rendering in block.render are marked safe
        so that they don't get double-escaped when inserted into a parent template (#2541)
        	paragraphztests/jinja2/paragraph.htmlr   zhello worldtypevaluetests/jinja2/stream.htmlr8   z<p>hello world</p>Nr   StreamBlock	CharBlock	to_pythonr   assertInr   Zstream_blockstream_valueresultr   r   r    test_block_render_result_is_safeB   s    z2TestJinjaEscaping.test_block_render_result_is_safec                 C   sH   t dt jddfg}|dddg}tdd|i}| d| d	S )
z
        Ensure that RichText values are marked safe
        so that they don't get double-escaped when inserted into a parent template (#2542)
        r4   ztests/jinja2/rich_text.htmlr5   z5<p>Merry <a linktype="page" id="4">Christmas</a>!</p>r6   r9   r8   z8<p>Merry <a href="/events/christmas/">Christmas</a>!</p>N)r   r;   ZRichTextBlockr=   r   r>   r?   r   r   r   test_rich_text_is_safeZ   s(    
	z(TestJinjaEscaping.test_rich_text_is_safeN)r0   r1   r2   ZfixturesrB   rC   r   r   r   r   r3   ?   s   r3   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S )TestIncludeBlockTagc                 C   s6   t jdd}|d}td|dd}| d| dS )	z
        The include_block tag should be able to render a BoundBlock's template
        while keeping the parent template's context
        tests/jinja2/heading_block.htmlr5   bonjour$tests/jinja2/include_block_test.htmlfr
test_blocklanguage'<body><h1 lang="fr">bonjour</h1></body>Nr   r<   bindr   r>   r   blockZbound_blockrA   r   r   r   &test_include_block_tag_with_boundblock}   s    
z:TestIncludeBlockTag.test_include_block_tag_with_boundblockc                 C   s6   t  }|ddd}td|dd}| d| dS )	z
        The include_block tag should be able to render a StructValue's template
        while keeping the parent template's context
        Bonjourzmonde <i>italique</i>)titlebodyrG   rH   rI   z<<body><h1 lang="fr">Bonjour</h1>monde <i>italique</i></body>N)r   r=   r   r>   )r   rP   Zstruct_valuerA   r   r   r   'test_include_block_tag_with_structvalue   s    z;TestIncludeBlockTag.test_include_block_tag_with_structvaluec                 C   sX   t jdt jddfdt  fgdd}|dddg}td|d	d
}| d| dS )z
        The include_block tag should be able to render a StreamValue's template
        while keeping the parent template's context
        headingrE   r5   r4   z&tests/jinja2/stream_with_language.htmlrR   r6   rG   rH   rI   z?<div class="heading" lang="fr"><h1 lang="fr">Bonjour</h1></div>Nr:   )r   rP   r@   rA   r   r   r   'test_include_block_tag_with_streamvalue   s$    

z;TestIncludeBlockTag.test_include_block_tag_with_streamvaluec                 C   s   t dddi}| d| dS )z
        The include_block tag should be able to render a value without a render_as_block method
        by just rendering it as a string
        rG   rJ   *   z<body>42</body>N)r   r>   )r   rA   r   r   r   'test_include_block_tag_with_plain_value   s    z;TestIncludeBlockTag.test_include_block_tag_with_plain_valuec                 C   sR   t jdd}|d}td|dd}| d| tdddd}| d	| dS )
z
        The block parameter on include_block tag should support complex values including filters,
        e.g. {% include_block foo|default:123 %}
        rE   r5   rF   z0tests/jinja2/include_block_test_with_filter.htmlrH   rI   rL   Nz<body>999</body>rM   rO   r   r   r   *test_include_block_tag_with_filtered_value   s     
z>TestIncludeBlockTag.test_include_block_tag_with_filtered_valuec                 C   s4   t jdd}|d}tdd|i}| d| dS )z
        The include_block tag should be able to pass local variables from parent context to the
        child context
        ztests/blocks/heading_block.htmlr5   rF   z<tests/jinja2/include_block_tag_with_additional_variable.htmlrJ   z/<body><h1 class="important">bonjour</h1></body>NrM   rO   r   r   r   /test_include_block_tag_with_additional_variable   s    
zCTestIncludeBlockTag.test_include_block_tag_with_additional_variablec                 C   s   t  }||d}tdd|i}| d| tdd|i}| d| tdddi}| d| tdddi}| d| t  }||d}tdd|i}| d| tddtdi}| d| dS )	z\
        Output of include_block should be escaped as per Django autoescaping rules
        zsome <em>evil</em> HTMLrG   rJ   z0<body>some &lt;em&gt;evil&lt;/em&gt; HTML</body>z3tests/blocks/include_block_autoescape_off_test.htmlz$<body>some <em>evil</em> HTML</body>z3tests/jinja2/include_block_autoescape_off_test.htmlN)r   r<   rN   r=   r   r>   ZRawHTMLBlockr   rO   r   r   r    test_include_block_html_escaping   sP    z4TestIncludeBlockTag.test_include_block_html_escapingN)
r0   r1   r2   rQ   rU   rW   rY   rZ   r[   r\   r   r   r   r   rD   |   s   rD   N)Zdjango.templater   Zdjango.template.loaderr   Zdjango.testr   Zdjango.utils.safestringr   Zwagtailr   r   Zwagtail.coreutilsr   Zwagtail.modelsr	   r
   Zwagtail.test.testapp.blocksr   r   r3   rD   r   r   r   r   <module>   s   3=