o
    h8)                     @   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 )Njinja2)r   engineself r   Y/var/www/html/ndineBlogger/venv/lib/python3.10/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)is_default_site)siterequest)r
   objectsgetr   r   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   richtextr   r   r   test_richtext   s
   z'TestCoreGlobalsAndFilters.test_richtextc                 C   ,   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   r$   )Nr%   r&   z{{ fullpageurl(page) }}r(   )r	   r   r   r!   r   full_urlr+   r   r   r   test_fullpageurl'      z*TestCoreGlobalsAndFilters.test_fullpageurlc                 C   r$   )Nr%   r&   z{{ slugurl(page.slug) }}r(   r)   r+   r   r   r   test_slugurl-   r/   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      z+TestCoreGlobalsAndFilters.test_wagtail_sitec                 C   s   |  | dt d S )Nz{{ wagtail_version() }})r!   r   r   r   r   r   r   test_wagtail_version;   r5   z.TestCoreGlobalsAndFilters.test_wagtail_version)NT)__name__
__module____qualname__r   r   r#   r,   r.   r0   r2   r4   r6   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   H   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.htmlr@   z<p>hello world</p>Nr   StreamBlock	CharBlock	to_pythonr   assertInr   stream_blockstream_valueresultr   r   r    test_block_render_result_is_safeB   s   z2TestJinjaEscaping.test_block_render_result_is_safec                 C   r;   )
z
        Ensure that RichText values are marked safe
        so that they don't get double-escaped when inserted into a parent template (#2542)
        r<   ztests/jinja2/rich_text.htmlr=   z5<p>Merry <a linktype="page" id="4">Christmas</a>!</p>r>   rA   r@   z8<p>Merry <a href="/events/christmas/">Christmas</a>!</p>N)r   rC   RichTextBlockrE   r   rF   rG   r   r   r   test_rich_text_is_safeZ   s(   
	z(TestJinjaEscaping.test_rich_text_is_safeN)r7   r8   r9   fixturesrK   rM   r   r   r   r   r:   ?   s    r:   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.htmlr=   bonjour$tests/jinja2/include_block_test.htmlfr
test_blocklanguage'<body><h1 lang="fr">bonjour</h1></body>Nr   rD   bindr   rF   r   blockbound_blockrJ   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bodyrR   rS   rT   z<<body><h1 lang="fr">Bonjour</h1>monde <i>italique</i></body>N)r   rE   r   rF   )r   r[   struct_valuerJ   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
        headingrP   r=   r<   z&tests/jinja2/stream_with_language.htmlr^   r>   rR   rS   rT   z?<div class="heading" lang="fr"><h1 lang="fr">Bonjour</h1></div>NrB   )r   r[   rI   rJ   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
        rR   rU   *   z<body>42</body>N)r   rF   )r   rJ   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 %}
        rP   r=   rQ   z0tests/jinja2/include_block_test_with_filter.htmlrS   rT   rW   Nz<body>999</body>rX   rZ   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.htmlr=   rQ   z<tests/jinja2/include_block_tag_with_additional_variable.htmlrU   z/<body><h1 class="important">bonjour</h1></body>NrX   rZ   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> HTMLrR   rU   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   rD   rY   rE   r   rF   RawHTMLBlockr   rZ   r   r   r    test_include_block_html_escaping   sP   z4TestIncludeBlockTag.test_include_block_html_escapingN)
r7   r8   r9   r]   rb   rd   rf   rg   rh   rj   r   r   r   r   rO   |   s    rO   N)django.templater   django.template.loaderr   django.testr   django.utils.safestringr   wagtailr   r   wagtail.coreutilsr   wagtail.modelsr	   r
   wagtail.test.testapp.blocksr   r   r:   rO   r   r   r   r   <module>   s    3=