o
    hE                     @   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/url_pathz<a href="/events/christmas/">)r   expand_db_attributesr	   objectsgetr   assertEqualselfresult r"   \/var/www/html/ndineBlogger/venv/lib/python3.10/site-packages/wagtail/tests/test_rich_text.pytest_expand_db_attributes   s   z1TestPageLinktypeHandler.test_expand_db_attributesc                 C      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      zETestPageLinktypeHandler.test_expand_db_attributes_page_does_not_existc                 C   r%   )Nr      z<a href="None">r'   r   r"   r"   r#   (test_expand_db_attributes_not_for_editor   r)   z@TestPageLinktypeHandler.test_expand_db_attributes_not_for_editorN)__name__
__module____qualname__fixturesr$   r(   r+   r"   r"   r"   r#   r      s
    r   T)enEnglish)frFrenchzwagtail.test.urls_multilang)WAGTAIL_I18N_ENABLEDWAGTAIL_CONTENT_LANGUAGESROOT_URLCONFc                   @   2   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 )
Nr2   )language_coder   r   T)copy_parentsnoelslug)update_fields)r   r   create	fr_localer	   r   
event_pagecopy_for_translationfr_event_pager<   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   r@   r   r   r   r"   r"   r#   r$   8   s   z9TestPageLinktypeHandlerWithI18N.test_expand_db_attributesc                 C   sN   t d td| jji}| |d W d    d S 1 s w   Y  d S )Nr2   r   z<a href="/fr/events/noel/">)r   overrider   r   r@   r   r   r   r"   r"   r#   'test_expand_db_attributes_autolocalizes<   s   "zGTestPageLinktypeHandlerWithI18N.test_expand_db_attributes_autolocalizesc                 C   sb   | j   | j   td td| jji}| 	|d W d    d S 1 s*w   Y  d S )Nr2   r   rH   )
rB   	unpublishrC   r   rI   r   r   r@   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/   rG   r$   rJ   rL   r"   r"   r"   r#   r8   #   s    	
r8   c                   @      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>barquux)foobaz)r   r   r    htmlr!   r"   r"   r#   test_extract_attrN   s   z"TestExtractAttrs.test_extract_attrN)r,   r-   r.   rU   r"   r"   r"   r#   rN   M       rN   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   rS   r"   r"   r#   test_expand_db_html_no_linktypeW      z0TestExpandDbHtml.test_expand_db_html_no_linktypec                 C      d}t |}| |d d S )Nz$<a id="1" linktype="invalid">foo</a>
<a>foo</a>rX   rS   r"   r"   r#   'test_invalid_linktype_set_to_empty_link\   rZ   z8TestExpandDbHtml.test_invalid_linktype_set_to_empty_linkc                 C   r[   )Nz%<a id="1" linktype="document">foo</a>z'<a href="/documents/1/test.pdf">foo</a>rX   rS   r"   r"   r#   !test_valid_linktype_and_referencea   rZ   z2TestExpandDbHtml.test_valid_linktype_and_referencec                 C   r[   )Nz(<a id="9999" linktype="document">foo</a>r\   rX   rS   r"   r"   r#   7test_valid_linktype_invalid_reference_set_to_empty_linkf   rZ   zHTestExpandDbHtml.test_valid_linktype_invalid_reference_set_to_empty_linkc                 C      |  tdd d S )Nz<embed id="1" /> r   r   rF   r"   r"   r#   test_no_embedtype_remove_tagk      z-TestExpandDbHtml.test_no_embedtype_remove_tagc                 C   r`   )Nz$<embed id="1" embedtype="invalid" />ra   rb   rF   r"   r"   r#   !test_invalid_embedtype_remove_tagn   rd   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rT   z><embed embedtype="media" url="http://www.youtube.com/watch" />)wagtail.embeds.modelsrf   return_valuer   assertIn)r    	get_embedrf   rT   r!   r"   r"   r#   test_expand_db_html_with_embedq   s
   z/TestExpandDbHtml.test_expand_db_html_with_embeddefaultBACKENDz-django.core.cache.backends.locmem.LocMemCache)CACHESc                 C   s@   t   | d td W d    d S 1 sw   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
   clear_site_root_paths_cacheassertNumQueriesr   rF   r"   r"   r#   *test_expand_db_html_database_queries_pages|   s   "z;TestExpandDbHtml.test_expand_db_html_database_queries_pagesc                 C   s:   |  d tdd W d    d S 1 sw   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>
rg   rr   r   rF   r"   r"   r#   .test_expand_db_html_database_queries_documents   s
   "z?TestExpandDbHtml.test_expand_db_html_database_queries_documents
renditionsz+django.core.cache.backends.dummy.DummyCachec                 C   s8   |  d td W d    d S 1 sw   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   rF   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>rb   rF   r"   r"   r#   $test_expand_db_html_mixed_link_types   s   z5TestExpandDbHtml.test_expand_db_html_mixed_link_typesN)r,   r-   r.   r/   rY   r]   r^   r_   rc   re   r   rl   r   rs   ru   rx   ry   r"   r"   r"   r#   rW   T   s4    



rW   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 Nra   r   r   sourcer    valuer"   r"   r#   test_construct_with_none      z*TestRichTextValue.test_construct_with_nonec                 C      t d}| |jd d S r{   r|   r~   r"   r"   r#    test_construct_with_empty_string   r   z2TestRichTextValue.test_construct_with_empty_stringc                 C   r   )Nz<p>hello world</p>r|   r~   r"   r"   r#   #test_construct_with_nonempty_string   r   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 )Nr   z<p>django</p>i90  )r   assertNotEqualr   )r    value1value2value3r"   r"   r#   test_compare_value   s   z$TestRichTextValue.test_compare_valueN)
r,   r-   r.   r/   r   r   r   r   r   r   r"   r"   r"   r#   rz      s    rz   c                   @      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   js)r    featuresr   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 )Nmade_up_editor
blockquoter   made_up_feature)r   assertIsNoner   )r    r   r"   r"   r#   'test_missing_editor_plugin_returns_none  s   z;TestFeatureRegistry.test_missing_editor_plugin_returns_noneN)r,   r-   r.   r   r   r"   r"   r"   r#   r      s    r   c                   @   r   )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      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   r   )	r    rulesrewriterpage_type_linkexternal_type_linkemail_type_linkanchor_type_link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   r   r   r   r   r"   r"   r#   r   4  r   z\TestLinkRewriterTagReplacing.test_supported_type_should_follow_given_rules.<locals>.<lambda>c                 S   r   )Nz<a rel="nofollow" href="{}">hrefr   r   r"   r"   r#   r   5      c                 S   r   )Nz<a data-email="true" href="{}">r   r   r   r"   r"   r#   r   8  r   c                 S   r   )Nz <a data-anchor="true" href="{}">r   r   r   r"   r"   r#   r   ;  r   c                 S   r   )Nz<a data-phone="true" href="{}">r   r   r   r"   r"   r#   r   >  r   )r   externalemailanchor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   external_type_link_httpr   r   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#   r     s    #r   c                   @   r7   )TestRichTextFieldr   c                 C   N   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   rD   _meta	get_fieldvalue_from_objectget_searchable_contentr   r    christmas_page
body_fieldr   r!   r"   r"   r#   test_get_searchable_contenth     

z-TestRichTextField.test_get_searchable_contentc                 C   r   )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  r   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 )N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   r   is_validr   )r    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>1ra   )r   listr   extract_referencesr	   rF   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                   @   rM   )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>rp   N)r   r   clean)r    	validatorr"   r"   r#   test_count_characters  s   z4TestRichTextMaxLengthValidator.test_count_charactersN)r,   r-   r.   r   r"   r"   r"   r#   r     rV   r   N)(unittest.mockr   django.forms.modelsr   django.testr   r   django.utilsr   wagtail.fieldsr   wagtail.modelsr   r	   r
   wagtail.rich_textr   r   r   "wagtail.rich_text.feature_registryr   wagtail.rich_text.pagesr   wagtail.rich_text.rewritersr   r   wagtail.test.testapp.modelsr   r   wagtail.test.utils.form_datar   r   r8   rN   rW   rz   r   r   r   r   r"   r"   r"   r#   <module>   s:    " &X<