a
    DgE                     @   sR  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
 d dlmZmZmZ d dlmZmZmZ d dlmZ d d	lmZ d d
lmZmZ d dlmZmZ d dlmZ G dd deZedddgdd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$G dd deZ%G d d! d!eZ&G d"d# d#eZ'd$S )%    )patch)modelform_factory)TestCaseoverride_settings)translation)RichTextField)LocalePageSite)RichTextRichTextMaxLengthValidatorexpand_db_html)FeatureRegistry)PageLinkHandler)LinkRewriterextract_attrs)
EventIndex	EventPage)	rich_textc                   @   s*   e Zd ZdgZdd Zdd Zdd ZdS )	TestPageLinktypeHandler	test.jsonc                 C   s*   t dtjjddji}| |d d S )Nid/home/events/christmas/Zurl_pathz<a href="/events/christmas/">)r   expand_db_attributesr	   objectsgetr   assertEqualselfresult r!   [/var/www/lab.imftr.de/x/nb_venv/lib/python3.9/site-packages/wagtail/tests/test_rich_text.pytest_expand_db_attributes   s    z1TestPageLinktypeHandler.test_expand_db_attributesc                 C   s   t ddi}| |d d S )Nr   r   <a>r   r   r   r   r!   r!   r"   -test_expand_db_attributes_page_does_not_exist   s    zETestPageLinktypeHandler.test_expand_db_attributes_page_does_not_existc                 C   s   t ddi}| |d d S )Nr      z<a href="None">r%   r   r!   r!   r"   (test_expand_db_attributes_not_for_editor   s    z@TestPageLinktypeHandler.test_expand_db_attributes_not_for_editorN)__name__
__module____qualname__fixturesr#   r&   r(   r!   r!   r!   r"   r      s   r   T)enZEnglish)frZFrenchzwagtail.test.urls_multilang)ZWAGTAIL_I18N_ENABLEDZWAGTAIL_CONTENT_LANGUAGESZROOT_URLCONFc                   @   s2   e Zd ZdgZdd Zdd Zdd Zdd	 Zd
S )TestPageLinktypeHandlerWithI18Nr   c                 C   s^   t jjdd| _tjjdd| _| jj| jdd| _d| j_	| jj
dgd	 | j   d S )
Nr.   )Zlanguage_coder   r   T)Zcopy_parentsZnoelslug)Zupdate_fields)r   r   createZ	fr_localer	   r   
event_pageZcopy_for_translationfr_event_pager0   savesave_revisionpublishr   r!   r!   r"   setUp.   s    z%TestPageLinktypeHandlerWithI18N.setUpc                 C   s"   t d| jji}| |d d S )Nr    <a href="/en/events/christmas/">)r   r   r2   r   r   r   r!   r!   r"   r#   8   s    z9TestPageLinktypeHandlerWithI18N.test_expand_db_attributesc                 C   sL   t d. td| jji}| |d W d    n1 s>0    Y  d S )Nr.   r   z<a href="/fr/events/noel/">)r   overrider   r   r2   r   r   r   r!   r!   r"   'test_expand_db_attributes_autolocalizes<   s    zGTestPageLinktypeHandlerWithI18N.test_expand_db_attributes_autolocalizesc                 C   s`   | j   | j   td. td| jji}| 	|d W d    n1 sR0    Y  d S )Nr.   r   r9   )
r3   Z	unpublishr4   r   r:   r   r   r2   r   r   r   r!   r!   r"   >test_expand_db_attributes_doesnt_autolocalize_unpublished_pageC   s
    

z^TestPageLinktypeHandlerWithI18N.test_expand_db_attributes_doesnt_autolocalize_unpublished_pageN)r)   r*   r+   r,   r8   r#   r;   r<   r!   r!   r!   r"   r/   #   s
   	
r/   c                   @   s   e Zd Zdd ZdS )TestExtractAttrsc                 C   s"   d}t |}| |ddd d S )Nz#<a foo="bar" baz="quux">snowman</a>barZquux)ZfooZbaz)r   r   r   htmlr    r!   r!   r"   test_extract_attrN   s    z"TestExtractAttrs.test_extract_attrN)r)   r*   r+   rA   r!   r!   r!   r"   r=   M   s   r=   c                   @   s   e Zd ZdgZdd Zdd Zdd Zdd	 Zd
d Zdd Z	e
ddd Zedddiiddd Zdd Zedddiiddd Zdd ZdS ) TestExpandDbHtmlr   c                 C   s   d}t |}| |d d S )Nz<a id="1">foo</a>r   r   r?   r!   r!   r"   test_expand_db_html_no_linktypeW   s    z0TestExpandDbHtml.test_expand_db_html_no_linktypec                 C   s   d}t |}| |d d S )Nz$<a id="1" linktype="invalid">foo</a>
<a>foo</a>rC   r?   r!   r!   r"   'test_invalid_linktype_set_to_empty_link\   s    z8TestExpandDbHtml.test_invalid_linktype_set_to_empty_linkc                 C   s   d}t |}| |d d S )Nz%<a id="1" linktype="document">foo</a>z'<a href="/documents/1/test.pdf">foo</a>rC   r?   r!   r!   r"   !test_valid_linktype_and_referencea   s    z2TestExpandDbHtml.test_valid_linktype_and_referencec                 C   s   d}t |}| |d d S )Nz(<a id="9999" linktype="document">foo</a>rE   rC   r?   r!   r!   r"   7test_valid_linktype_invalid_reference_set_to_empty_linkf   s    zHTestExpandDbHtml.test_valid_linktype_invalid_reference_set_to_empty_linkc                 C   s   |  tdd d S )Nz<embed id="1" /> r   r   r7   r!   r!   r"   test_no_embedtype_remove_tagk   s    z-TestExpandDbHtml.test_no_embedtype_remove_tagc                 C   s   |  tdd d S )Nz$<embed id="1" embedtype="invalid" />rI   rJ   r7   r!   r!   r"   !test_invalid_embedtype_remove_tagn   s    z2TestExpandDbHtml.test_invalid_embedtype_remove_tagzwagtail.embeds.embeds.get_embedc                 C   s4   ddl m} |dd|_d}t|}| d| d S )Nr   )Embedz	test htmlr@   z><embed embedtype="media" url="http://www.youtube.com/watch" />)Zwagtail.embeds.modelsrM   Zreturn_valuer   ZassertIn)r   Z	get_embedrM   r@   r    r!   r!   r"   test_expand_db_html_with_embedq   s
    z/TestExpandDbHtml.test_expand_db_html_with_embeddefaultZBACKENDz-django.core.cache.backends.locmem.LocMemCache)ZCACHESc                 C   s>   t   | d td W d    n1 s00    Y  d S )N   a}  
This rich text has 8 page links, and this test verifies that the code uses the
minimal number of database queries (5) to expand them.

All of these pages should be retrieved with 4 queries, one to do the base
Page table lookup and then 1 each for the EventIndex, EventPage, and
SimplePage tables.

<a linktype="page" id="3">This links to an EventIndex page.</a>
<a linktype="page" id="4">This links to an EventPage page.</a>
<a linktype="page" id="5">This links to an EventPage page.</a>
<a linktype="page" id="6">This links to an EventPage page.</a>
<a linktype="page" id="9">This links to an EventPage page.</a>
<a linktype="page" id="12">This links to an EventPage page.</a>
<a linktype="page" id="7">This links to a SimplePage page.</a>
<a linktype="page" id="11">This links to a SimplePage page.</a>

Finally there's one additional query needed to do the Site root paths lookup.
        )r
   Zclear_site_root_paths_cacheassertNumQueriesr   r7   r!   r!   r"   *test_expand_db_html_database_queries_pages|   s
    z;TestExpandDbHtml.test_expand_db_html_database_queries_pagesc                 C   s8   |  d tdd W d    n1 s*0    Y  d S )Nr'   aE  
This rich text has 2 document links, and this test verifies that the code uses
the minimal number of database queries (1) to expand them.

Both of these documents should be retrieved with 1 query:

<a linktype="document" id="1">This links to a document.</a>
<a linktype="document" id="2">This links to another document.</a>
rN   rR   r   r7   r!   r!   r"   .test_expand_db_html_database_queries_documents   s    z?TestExpandDbHtml.test_expand_db_html_database_queries_documentsZ
renditionsz+django.core.cache.backends.dummy.DummyCachec                 C   s6   |  d td W d    n1 s(0    Y  d S )N   a  
This rich text has 2 image links, and this test verifies that the code uses the
minimal number of database queries (3) to expand them.

Both of these images should be retrieved with 3 queries, one to fetch the
image objects in bulk and then one per image to fetch their renditions:

This is an image: <embed embedtype="image" id="1" format="left" />
This is another image: <embed embedtype="image" id="2" format="left" />
        rT   r7   r!   r!   r"   +test_expand_db_html_database_queries_images   s    z<TestExpandDbHtml.test_expand_db_html_database_queries_imagesc                 C   s$   |  tdd |  tdd d S )NzG<a href="https://wagtail.org/">foo</a><a linktype="page" id="3">bar</a>z@<a href="https://wagtail.org/">foo</a><a href="/events/">bar</a>zn<a linktype="page" id="3">page</a><a linktype="document" id="1">document</a><a linktype="page" id="3">page</a>zb<a href="/events/">page</a><a href="/documents/1/test.pdf">document</a><a href="/events/">page</a>rJ   r7   r!   r!   r"   $test_expand_db_html_mixed_link_types   s    z5TestExpandDbHtml.test_expand_db_html_mixed_link_typesN)r)   r*   r+   r,   rD   rF   rG   rH   rK   rL   r   rO   r   rS   rU   rW   rX   r!   r!   r!   r"   rB   T   s2   



rB   c                   @   sB   e Zd ZdgZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dS )TestRichTextValuer   c                 C   s   t d }| |jd d S NrI   r   r   sourcer   valuer!   r!   r"   test_construct_with_none   s    z*TestRichTextValue.test_construct_with_nonec                 C   s   t d}| |jd d S rZ   r[   r]   r!   r!   r"    test_construct_with_empty_string   s    z2TestRichTextValue.test_construct_with_empty_stringc                 C   s   t d}| |jd d S )Nz<p>hello world</p>r[   r]   r!   r!   r"   #test_construct_with_nonempty_string   s    z5TestRichTextValue.test_construct_with_nonempty_stringc                 C   s    t d}t|}| |d d S )Nz5<p>Merry <a linktype="page" id="4">Christmas</a>!</p>z8<p>Merry <a href="/events/christmas/">Christmas</a>!</p>)r   strr   )r   r^   r    r!   r!   r"   test_render   s
    zTestRichTextValue.test_renderc                 C   s(   t d }| | t d}| | d S )N<p>wagtail</p>)r   assertFalse
assertTruer]   r!   r!   r"   test_evaluate_value   s    
z%TestRichTextValue.test_evaluate_valuec                 C   s@   t d}t d}t d}| || | |d | || d S )Nrd   z<p>django</p>i90  )r   assertNotEqualr   )r   Zvalue1Zvalue2Zvalue3r!   r!   r"   test_compare_value   s    z$TestRichTextValue.test_compare_valueN)
r)   r*   r+   r,   r_   r`   ra   rc   rg   ri   r!   r!   r!   r"   rY      s   rY   c                   @   s   e Zd Zdd Zdd ZdS )TestFeatureRegistryc                 C   s&   t  }|dd}| |jdg d S )Ndraftail	quotationz testapp/js/draftail-quotation.js)r   get_editor_pluginr   Zjs)r   featuresrl   r!   r!   r"   %test_register_rich_text_features_hook   s    z9TestFeatureRegistry.test_register_rich_text_features_hookc                 C   s.   t  }| |dd | |dd d S )NZmade_up_editor
blockquoterk   Zmade_up_feature)r   ZassertIsNonerm   )r   rn   r!   r!   r"   'test_missing_editor_plugin_returns_none  s    z;TestFeatureRegistry.test_missing_editor_plugin_returns_noneN)r)   r*   r+   ro   rq   r!   r!   r!   r"   rj      s   rj   c                   @   s   e Zd Zdd Zdd ZdS )TestLinkRewriterTagReplacingc           	      C   s   ddd i}t |}|d}| |d |d}| |d |d}| |d |d}| |d |d	}| |d	 |d
}| |d | |d | |dd d S )Npagec                 S   s   d | d S Nz<a href="/article/{}">r   formatattrsr!   r!   r"   <lambda>      zSTestLinkRewriterTagReplacing.test_should_follow_default_behaviour.<locals>.<lambda><a linktype="page" id="3"><a href="/article/3"><a href="https://wagtail.org/">"<a href="mailto:test@wagtail.org"><a href="#test">z#<a data-link="https://wagtail.org">z0<a linktype="custom" href="https://wagtail.org">z<a href="https://wagtail.org">r$   z9<a href="https://wagtail.org/"><a linktype="page" id="3">z4<a href="https://wagtail.org/"><a href="/article/3">)r   r   rh   )	r   rulesrewriterpage_type_linkexternal_type_linkemail_type_linkanchor_type_linkZlink_without_linktypelink_with_custom_linktyper!   r!   r"   $test_should_follow_default_behaviour  s*    zATestLinkRewriterTagReplacing.test_should_follow_default_behaviourc           
      C   s   dd dd dd dd dd d}t |}|d}| |d	 |d
}| |d |d}| |d |d}| |d |d}| |d |d}| |d |d}	| |	d d S )Nc                 S   s   d | d S rt   ru   rw   r!   r!   r"   ry   4  rz   z\TestLinkRewriterTagReplacing.test_supported_type_should_follow_given_rules.<locals>.<lambda>c                 S   s   d | d S )Nz<a rel="nofollow" href="{}">hrefru   rw   r!   r!   r"   ry   5  s   c                 S   s   d | d S )Nz<a data-email="true" href="{}">r   ru   rw   r!   r!   r"   ry   8  s   c                 S   s   d | d S )Nz <a data-anchor="true" href="{}">r   ru   rw   r!   r!   r"   ry   ;  s   c                 S   s   d | d S )Nz<a data-phone="true" href="{}">r   ru   rw   r!   r!   r"   ry   >  s   )rs   ZexternalemailanchorZcustomr{   r|   r}   z.<a rel="nofollow" href="https://wagtail.org/">z<a href="http://wagtail.org/">z-<a rel="nofollow" href="http://wagtail.org/">r~   z4<a data-email="true" href="mailto:test@wagtail.org">r   z#<a data-anchor="true" href="#test">z<a href="tel:+4917640206387">z/<a linktype="custom" href="tel:+4917640206387">z/<a data-phone="true" href="tel:+4917640206387">)r   r   )
r   r   r   r   r   Zexternal_type_link_httpr   r   Zlink_with_no_linktyper   r!   r!   r"   -test_supported_type_should_follow_given_rules1  s>    zJTestLinkRewriterTagReplacing.test_supported_type_should_follow_given_rulesN)r)   r*   r+   r   r   r!   r!   r!   r"   rr     s   #rr   c                   @   s2   e Zd ZdgZdd Zdd Zdd Zdd	 Zd
S )TestRichTextFieldr   c                 C   sN   t jjdd}d|_|  |jd}||}||}| 	|dg d S )Nr   r   zX<p><b>Merry Christmas from <a href="https://wagtail.org/">Wagtail!</a></b> &amp; co.</p>bodyz#Merry Christmas from Wagtail! & co.
r   r   r   r   r5   Z_meta	get_fieldZvalue_from_objectZget_searchable_contentr   r   Zchristmas_pageZ
body_fieldr^   r    r!   r!   r"   test_get_searchable_contenth  s    

z-TestRichTextField.test_get_searchable_contentc                 C   sN   t jjdd}d|_|  |jd}||}||}| 	|dg d S )Nr   r   z0<p>buttery<br />mashed</p><p>po<i>ta</i>toes</p>r   zbuttery mashed potatoesr   r   r!   r!   r"   &test_get_searchable_content_whitespacer  s    

z8TestRichTextField.test_get_searchable_content_whitespacec                 C   sl   t tdgd}|dtdi}| |  |dtdi}| |  |dtdi}| |  d S )NZintro)modelfieldsz%<p><i>less</i> than 50 characters</p>z]<p>a piece of text that is considerably longer than the limit of fifty characters of text</p>z<p><a href="http://a-domain-name-that-would-put-us-over-the-limit-if-we-were-counting-it.example.com/">less</a> than 50 characters</p>)r   r   r   rf   Zis_validre   )r   ZEventIndexFormformr!   r!   r"   test_max_length_validation|  s&    
z,TestRichTextField.test_max_length_validationc                 C   s&   |  tt dtdddfg d S )Nz6<a linktype="page" id="1">Link to an internal page</a>1rI   )r   listr   Zextract_referencesr	   r7   r!   r!   r"   test_extract_references  s    z)TestRichTextField.test_extract_referencesN)r)   r*   r+   r,   r   r   r   r   r!   r!   r!   r"   r   e  s
   

r   c                   @   s   e Zd Zdd ZdS )TestRichTextMaxLengthValidatorc                 C   s   t d}| |dd | |dd | |dd | |dd	 | |d
d	 | |dd | |dd | |dd | |dd dS )z=Keep those tests up-to-date with MaxLength tests client-side.2   z<p>Plain text</p>
   z<p>There&#x27;s quote</p>   z<p>Line<br/>break</p>	   z<p>Multi</p><p>blocks</p>   z <p>Empty</p><p></p><p>blocks</p>z<p>With</p><hr/><p>HR</p>   z<p>With</p><embed/><p>embed</p>u   <p>U+2764 U+FE0F ❤️</p>   u   <p>👨‍👨‍👧</p>rQ   N)r   r   clean)r   Z	validatorr!   r!   r"   test_count_characters  s    z4TestRichTextMaxLengthValidator.test_count_charactersN)r)   r*   r+   r   r!   r!   r!   r"   r     s   r   N)(Zunittest.mockr   Zdjango.forms.modelsr   Zdjango.testr   r   Zdjango.utilsr   Zwagtail.fieldsr   Zwagtail.modelsr   r	   r
   Zwagtail.rich_textr   r   r   Z"wagtail.rich_text.feature_registryr   Zwagtail.rich_text.pagesr   Zwagtail.rich_text.rewritersr   r   Zwagtail.test.testapp.modelsr   r   Zwagtail.test.utils.form_datar   r   r/   r=   rB   rY   rj   rr   r   r   r!   r!   r!   r"   <module>   s8   " &X<