U
    {h                    @   sJ  d dl mZm Z mZ d dlmZ d dlmZmZmZm	Z	 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mZ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)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7 d dl8m9Z9 d dl:m;Z;m<Z<m=Z= d dl>m?Z? d dl@mAZA d dlBmCZC d dlDmEZE d dlFmGZGmHZHmIZImJZJ d dlKmLZL d dlMmNZNmOZOmPZPmQZQmRZRmSZSmTZTmUZUmVZVmWZWmXZXmYZY d dlZm[Z[ G dd deZ\dd Z]G dd  d eZ^G d!d" d"eZ_G d#d$ d$e[eZ`G d%d& d&e[eZaG d'd( d(eZbG d)d* d*eZcG d+d, d,eZdG d-d. d.eZeG d/d0 d0eZfG d1d2 d2eZgG d3d4 d4e[eZhG d5d6 d6e[eZiG d7d8 d8eZjG d9d: d:eZkG d;d< d<e[eZlG d=d> d>e[eZmG d?d@ d@e[eZnG dAdB dBeZoG dCdD dDe[eZpG dEdF dFe[eZqdGS )H    )datedatetimetimezonewraps)AnyListMappingOptional)mock)forms)settings)get_user_model)AnonymousUser
Permission)checks)FieldDoesNotExistImproperlyConfigured)RequestFactoryTestCaseoverride_settings)reverse)escapejson_script)freeze_time)WagtailAdminModelFormWagtailAdminPageForm)CommentPanel
FieldPanelFieldRowPanel	HelpPanelInlinePanelMultiFieldPanelMultipleChooserPanel
ObjectListPageChooserPanelPanelPublishingPanelTabbedInterfaceTitleFieldPanel*extract_panel_definitions_from_model_classget_form_for_model)DraftailRichTextArea)AdminAutoHeightTextInputAdminDateInputAdminPageChooser)FormSubmission)FormSubmissionsPanel)get_dummy_request)get_image_model)CommentCommentReplyPageSite)ValidatedPageForm)Advert	EventPageEventPageChooserModelEventPageSpeakerFormPageWithRedirectGalleryPagePageChooserModel
PersonPageRestaurantPageRestaurantTag
SimplePageValidatedPage)WagtailTestUtilsc                   @   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 )TestGetFormForModelc              	   C   s*   t  }| td |  W 5 Q R X d S )Nz]ObjectList is not bound to a model yet. Use `.bind_to_model(model)` before using this method.)r$   ZassertRaisesMessageAttributeErrorget_form_class)selfedit_handler rK   n/var/www/dating/data/www/fatepal.com/env/lib/python3.8/site-packages/wagtail/admin/tests/test_edit_handlers.pytest_get_form_without_modelA   s    z/TestGetFormForModel.test_get_form_without_modelc                 C   sB   t ttd}| t|t | }| d|j | d|j d S )N)
form_classtitlepath)r+   r:   r   
assertTrue
issubclassassertNotInfieldsrI   EventPageFormformrK   rK   rL   /test_get_form_for_model_without_explicit_fieldsJ   s    zCTestGetFormForModel.test_get_form_for_model_without_explicit_fieldsc                 C   s   t ttddddgd}| }| t|t | t|jd t	j
 | t|jd t	j | t|jd jt | d|j d S )NrO   slug	date_fromdate_torN   rT   rP   )r+   r:   r   rQ   rR   r   assertEqualtyperT   r   Z	CharField	DateFieldwidgetr.   rS   rU   rK   rK   rL   (test_get_form_for_model_without_formsetsV   s    
z<TestGetFormForModel.test_get_form_for_model_without_formsetsc                 C   sB   t ttddddgddgd}| }| d|j | d|j d S )NrO   rY   rZ   r[   speakersrelated_linksrN   rT   formsets)r+   r:   r   assertInre   rU   rK   rK   rL   %test_get_form_for_model_with_formsetsj   s    
z9TestGetFormForModel.test_get_form_for_model_with_formsetsc                 C   sx   t ttdddgd}| t|t | }| t|jd jt	 t t
tdddgd}| }| t|jd jt d S )NrO   rY   contentr\   body)r+   rC   r   rQ   rR   r]   r^   rT   r`   r-   r:   r,   )rI   ZSimplePageFormZsimple_formrV   Z
event_formrK   rK   rL    test_direct_form_field_overridesw   s$       z4TestGetFormForModel.test_direct_form_field_overridesc                 C   sx   t ttdgdgd}| }| t|jd tj | t|jd jt	 | 
d|j | d|j | 
d|j d S )NrZ   rb   rd   rO   rc   r+   r:   r   r]   r^   rT   r   r_   r`   r.   rS   rf   re   rU   rK   rK   rL   ,test_get_form_for_model_with_specific_fields   s    z@TestGetFormForModel.test_get_form_for_model_with_specific_fieldsc                 C   s6   t ttdgd}| }| d|j | d|j d S )NrZ   r\   rb   rc   )r+   r:   r   rS   re   rU   rK   rK   rL   1test_get_form_for_model_without_explicit_formsets   s    zETestGetFormForModel.test_get_form_for_model_without_explicit_formsetsc                 C   sx   t ttdgdgd}| }| t|jd tj | t|jd jt	 | 
d|j | d|j | 
d|j d S )NrO   rc   )rN   excludeZexclude_formsetsrZ   rb   rk   rU   rK   rK   rL   ,test_get_form_for_model_with_excluded_fields   s    z@TestGetFormForModel.test_get_form_for_model_with_excluded_fieldsc                 C   sV   t ttddgdtjid}| }| t|jd tj | t|jd j	tj d S Nr[   rZ   )rN   rT   widgets
r+   r:   r   r   ZPasswordInputr]   r^   rT   r_   r`   rU   rK   rK   rL   6test_get_form_for_model_with_widget_overrides_by_class   s    zJTestGetFormForModel.test_get_form_for_model_with_widget_overrides_by_classc                 C   sX   t ttddgdt id}| }| t|jd tj | t|jd j	tj d S rp   rr   rU   rK   rK   rL   9test_get_form_for_model_with_widget_overrides_by_instance   s    
zMTestGetFormForModel.test_get_form_for_model_with_widget_overrides_by_instancec                 C   s   t ttdddgd}|  }| d| | td| tjjddd	 tjjd
dd	 |dddd}| 	|
  | |jd dg d S )NrO   rY   tagsr\   zCdata-w-tag-url-value="/admin/tag-autocomplete/tests/restauranttag/"z"autocompleteOnly": trueZItalianitalian)namerY   ZIndianZindianZ	BuonaseraZ	buonaserazItalian, delicious)rO   rY   ru   )r+   rA   r   Zas_prf   r   rB   objectscreaterQ   is_validr]   Zcleaned_data)rI   ZRestaurantPageFormZ	form_htmlrW   rK   rK   rL   #test_tag_widget_is_passed_tag_model   s*    
z7TestGetFormForModel.test_tag_widget_is_passed_tag_modelN)__name__
__module____qualname__rM   rX   ra   rg   rj   rl   rm   ro   rs   rt   r{   rK   rK   rK   rL   rF   @   s   	rF   c                    s    fdd}|S )Nc                    s   t   fdd}|S )Nc                     s*   j   z | | W 5 j   X d S N)get_edit_handlercache_clear)argskwargs)fnpage_clsrK   rL   	decorated   s    
z8clear_edit_handler.<locals>.decorator.<locals>.decoratedr   )r   r   r   )r   rL   	decorator   s    	z%clear_edit_handler.<locals>.decoratorrK   )r   r   rK   r   rL   clear_edit_handler   s    r   c                   @   s\   e Zd Zeedd Zeedd Zeedd Zeedd Z	eed	d
 Z
dS )TestPageEditHandlersc                 C   s$   t  }| }| t|t dS )zS
        Forms for pages should have a base class of WagtailAdminPageForm.
        N)r:   r   rH   rQ   rR   r   )rI   rJ   rV   rK   rK   rL   test_get_edit_handler
  s    z*TestPageEditHandlers.test_get_edit_handlerc                 C   s$   t  }| }| t|t dS )z
        ValidatedPage sets a custom base_form_class. This should be used as the
        base class when constructing a form for ValidatedPages
        N)rD   r   rH   rQ   rR   r8   )rI   rJ   ZGeneratedValidatedPageFormrK   rK   rL   'test_get_form_for_page_with_custom_base  s    z<TestPageEditHandlers.test_get_form_for_page_with_custom_basec              	   C   s   G dd d}t jddtdd}t jddtd	d}tjjtd
|d< t  }dd |D }|jdd d | |||g W 5 Q R X d S )Nc                   @   s   e Zd ZdS )zMTestPageEditHandlers.test_check_invalid_base_form_class.<locals>.BadFormClassN)r|   r}   r~   rK   rK   rK   rL   BadFormClass$  s   r   zBValidatedPage.base_form_class does not extend WagtailAdminPageFormz\Ensure that wagtail.admin.tests.test_edit_handlers.BadFormClass extends WagtailAdminPageFormzwagtailadmin.E001hintobjidzVValidatedPage.get_edit_handler().get_form_class() does not extend WagtailAdminPageFormz]Ensure that the panel definition for ValidatedPage creates a subclass of WagtailAdminPageFormzwagtailadmin.E002Zbase_form_class)newc                 S   s   g | ]}|j tjkr|qS rK   )levelr   ERROR).0erK   rK   rL   
<listcomp>9  s      zKTestPageEditHandlers.test_check_invalid_base_form_class.<locals>.<listcomp>c                 S   s   | j S r   r   )r   rK   rK   rL   <lambda><      zITestPageEditHandlers.test_check_invalid_base_form_class.<locals>.<lambda>)key)	r   ErrorrD   r   patchobject
run_checkssortr]   )rI   r   Zinvalid_base_formZinvalid_edit_handlererrorsrK   rK   rL   "test_check_invalid_base_form_class"  s$    z7TestPageEditHandlers.test_check_invalid_base_form_classc              	   C   s^   t  t_tjjtdt  dd6 t  }| t	|t
 t }| |g  W 5 Q R X dS )z
        Set a custom edit handler on a Page class, but dont customise
        ValidatedPage.base_form_class, or provide a custom form class for the
        edit handler. Check the generated form class is of the correct type.
        rJ   T)r   ry   N)r(   rD   rJ   r   r   r   r   rH   rQ   rR   r   checkr]   )rI   rN   r   rK   rK   rL   #test_custom_edit_handler_form_class@  s       z8TestPageEditHandlers.test_custom_edit_handler_form_classc                 C   sh   t  }t|}| d| |jd d d d}t|}| d| | d| | d| | d| d S )Nz9model=<class 'wagtail.test.testapp.models.ValidatedPage'>)instancerequestrW   zinstance=Nonezrequest=Nonez	form=None)rD   r   reprrf   get_bound_panel)rI   rJ   Zhandler_reprZbound_handlerZbound_handler_reprrK   rK   rL   	test_reprP  s&      zTestPageEditHandlers.test_reprN)r|   r}   r~   r   r:   r   rD   r   r   r   r   rK   rK   rK   rL   r   	  s   




r   c                   @   s$   e Zd Zdd Zdd Zdd ZdS ))TestExtractPanelDefinitionsFromModelClassc                 C   s4   t t}| t|d | tdd |D  d S )N   c                 s   s   | ]}t |tV  qd S r   )
isinstancer"   r   panelrK   rK   rL   	<genexpr>n  s     z\TestExtractPanelDefinitionsFromModelClass.test_can_extract_panel_property.<locals>.<genexpr>)r*   r<   r]   lenrQ   anyrI   resultrK   rK   rL   test_can_extract_panel_propertyj  s    zITestExtractPanelDefinitionsFromModelClass.test_can_extract_panel_propertyc                 C   s*   t tdgd}|D ]}| |jd qd S )Nhostname)rn   )r*   r7   ZassertNotEqual
field_name)rI   panelsr   rK   rK   rL   test_excludep  s    z6TestExtractPanelDefinitionsFromModelClass.test_excludec                 C   s$   t t}| tdd |D  d S )Nc                 s   s"   | ]}t |to|jd kV  qdS )rZ   N)r   r   r   r   rK   rK   rL   r   z  s   zVTestExtractPanelDefinitionsFromModelClass.test_can_build_panel_list.<locals>.<genexpr>)r*   r:   rQ   r   )rI   r   rK   rK   rL   test_can_build_panel_listu  s    zCTestExtractPanelDefinitionsFromModelClass.test_can_build_panel_listN)r|   r}   r~   r   r   r   rK   rK   rK   rL   r   i  s   r   c                   @   s   e Zd Zdd Zdd ZdS )TestPanelAttributesc                 C   s   t  d| _| jdd}|| j_|  | _tttdddidt	dt
jd	tt	d
t	dddidgddidgddddidttddddidgddtttdddidt	dtt	dt	dddidgddidgddidgddgddidt| _d S )N/adminusernamez)Double-check event details before submit.zdata-panel-typehelpattrsrO   r`   rZ   r[   fieldz	field-rowEvent detailsshinyzobject-listheading	classnamer   rb   Speakersinline)labelr   r   z(Double-check cost details before submit.z	help-costcostz.nested-object_list-multi_field-field_row-fieldz(nested-object_list-multi_field-field_rowzmulti-fieldSecrettabs)r   getr   create_superuseruserloginr(   r$   r    r   r   Textarear   r!   r"   bind_to_modelr:   event_page_tabbed_interfacerI   r   rK   rK   rL   setUp  s    
 
 
 >@zTestPanelAttributes.setUpc                 C   s   | j  }tdd}||d}| j j||| jd}| }| |dd | |dd | |dd | |d	d | |d
d | |dd | |dd | |dd | |dd | |dd d S )NAbergavenny sheepdog trialsrO   r   r   rW   r   zdata-panel-type="tabs"   zdata-panel-type="multi-field"z:data-panel-type="nested-object_list-multi_field-field_row"z@data-panel-type="nested-object_list-multi_field-field_row-field"zdata-panel-type="help-cost"zdata-panel-type="inline"zdata-panel-type="object-list"zdata-panel-type="field-row"zdata-panel-type="field"zdata-panel-type="help")r   rH   r:   r   r   render_htmlr]   countrI   rV   eventrW   tabbed_interfacer   rK   rK   rL   test_render  s6    


zTestPanelAttributes.test_renderN)r|   r}   r~   r   r   rK   rK   rK   rL   r     s   Ir   c                   @   s<   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd ZdS )TestTabbedInterfacec              
   C   s   t  d| _| jdd}|| j_|  | _| jddd| _tj	jdd}| jj
| tttd	tjd
tdtdgdddttdddgddttdddgddttdgdddttdgdddgddidt| _d S ) Nr   r   r   Zadmin2ztest2@email.com)r   emailZcustom_see_panel_setting)codenamerO   r   rZ   r[   r   r   )r   r   rb   r   r   r   r   Z	superuser)
permissionr   ztests.custom_see_panel_settingzCustom Setting)r   r   z$tests.other_custom_see_panel_settingzOther Custom Settingdata-controllerzmy-tabbed-interfacer   )r   r   r   r   r   r   Zcreate_user
other_userr   rx   Zuser_permissionsaddr(   r$   r   r   r   r!   r   r:   r   )rI   r   prK   rK   rL   r     sV    
	

%'zTestTabbedInterface.setUpc                 C   s<   | j  }| }| d|j | t|jd jtj	 d S Nrb   rO   )
r   rH   rf   re   r]   r^   rT   r`   r   r   rU   rK   rK   rL   test_get_form_class!  s    
z'TestTabbedInterface.test_get_form_classc                 C   s   | j  }tdd}||d}| j j||| jd}| }| d| | d| | d| | d| | d	| | d
| | d| d S )Nr   r   r   r   <a id="tab-label-event_details" href="#tab-event_details" class="w-tabs__tab shiny" role="tab" aria-selected="false" tabindex="-1">t<a id="tab-label-speakers" href="#tab-speakers" class="w-tabs__tab " role="tab" aria-selected="false" tabindex="-1">z)aria-labelledby="tab-label-event_details"z$aria-labelledby="tab-label-speakers"&Abergavenny sheepdog trials</textarea>z%data-controller="my-tabbed-interface"signup_link)r   rH   r:   r   r   r   rf   rS   r   rK   rK   rL   r   +  s,    


zTestTabbedInterface.test_renderc                 C   s*   t | j d }| |ddddh d S )NrT   rO   rZ   r[   r   )setr   get_form_optionsr]   r   rK   rK   rL   test_required_fieldsO  s    z(TestTabbedInterface.test_required_fieldsc                 C   sV   | j  }tdd}||d}| j j||| jd}| }| d| | d| d S )Nr   r   r   r   r   r   )r   rH   r:   r   r   render_form_contentrf   rS   r   rK   rK   rL   test_render_form_contentT  s    


z,TestTabbedInterface.test_render_form_contentc              	   C   s  | j  }tdd}||d}| d^ | j j||| jd}| }| d| | d| | d| | d	| | d
| W 5 Q R X | dh | j| j_	| j j||| jd}| }| d| | d| | 
d| | d	| | 
d| W 5 Q R X | dl t }|| j_	| j j||| jd}| }| d| | d| | 
d| | 
d	| | 
d| W 5 Q R X dS )z
        test that three tabs show when the current user has permission to see all three
        test that two tabs show when the current user does not have permission to see all three
        r   r   r   zSuper user testr   r   r   z,<a id="tab-label-secret" href="#tab-secret" z<<a id="tab-label-custom_setting" href="#tab-custom_setting" zH<a id="tab-label-other_custom_setting" href="#tab-other_custom_setting" zNot superuser permissionszH<a id="tab-label-other_custom_setting" href="#tab-other-custom_setting" zNon superuserN)r   rH   r:   subTestr   r   r   rf   r   r   rS   r   )rI   rV   r   rW   r   r   r   rK   rK   rL   test_tabs_permissionsf  s    



z)TestTabbedInterface.test_tabs_permissionsN)	r|   r}   r~   r   r   r   r   r   r   rK   rK   rK   rL   r     s   2
$r   c                   @   s$   e Zd Zdd Zdd Zdd ZdS )TestObjectListc                 C   s^   t  d| _t }|| j_ttdtjdtdtdt	dddgd	d
ddid
t| _d S )Nr   rO   r   rZ   r[   rb   r   r   r   r   r   zmy-object-listr   )r   r   r   r   r   r$   r   r   r   r!   r   r:   event_page_object_listr   rK   rK   rL   r     s    

zTestObjectList.setUpc                 C   s<   | j  }| }| d|j | t|jd jtj	 d S r   )
r   rH   rf   re   r]   r^   rT   r`   r   r   rU   rK   rK   rL   r     s    
z"TestObjectList.test_get_form_classc                 C   s   | j  }tdd}||d}| j j||| jd}| }| d| | d| | d| | d| | d	| | d
| d S )Nr   r   r   r   z<div class="w-panel__header">z data-controller="my-object-list"z2<label for="id_date_from" id="id_date_from-label">z@<div class="help">Not required if event is on a single day</div>r   r   )	r   rH   r:   r   r   r   rf   ZassertInHTMLrS   )rI   rV   r   rW   object_listr   rK   rK   rL   r     s*    


zTestObjectList.test_renderN)r|   r}   r~   r   r   r   rK   rK   rK   rL   r     s   
r   c                       s4   e Zd Z fddZdd Zdd Zdd Z  ZS )	TestFormatValueForDisplayc                    s8   t    t | _tdtdddtddddd| _d S )Nr              publicrO   rZ   r[   audience)superr   r&   r   r:   r   r   rI   	__class__rK   rL   r     s    


zTestFormatValueForDisplay.setUpc                 C   s$   | j | jj}| || jj d S r   )r   format_value_for_displayr   rO   assertIsr   rK   rK   rL   test_charfield_return_value  s    z5TestFormatValueForDisplay.test_charfield_return_valuec                 C   s$   | j | jj}| || jj d S r   )r   r	  r   rZ   r
  r   rK   rK   rL   test_datefield_return_value"  s    z5TestFormatValueForDisplay.test_datefield_return_valuec                 C   s"   | j tj }| |d d S )Nz'Root, Welcome to your new Wagtail site!)r   r	  r6   rx   allr]   r   rK   rK   rL   test_queryset_return_value&  s    z4TestFormatValueForDisplay.test_queryset_return_value)r|   r}   r~   r   r  r  r  __classcell__rK   rK   r  rL   r     s   
r   c                   @   s   e Zd Zdd Zd"eeeef  eee  e	dddZ
d#ee	ej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d Zdd Zdd Zdd Zd d! ZdS )$TestFieldPanelc                 C   s   t  d| _t }|| j_tdtdddtddddd| _td	d
d	t| _
td	dd	t| _tddd	t| _tddd	t| _dddd| _d S )Nr   r   r   r   r   r  r  r  r[   
full-widthr   TZ	read_onlyr  
feed_imagePontypridd sheepdog trialsz
2014-06-01z
2014-06-02rO   rZ   r[   )r   r   r   r   r   r:   r   r   r   r   end_date_panelread_only_end_date_panelread_only_audience_panelread_only_image_panelpontypridd_event_datar   rK   rK   rL   r   ,  sJ    

    zTestFieldPanel.setUpN)datarT   returnc                 C   s0   t tt|d k	r|ndddgg d}||| jdS )NrO   rY   r[   rd   )r  r   )r+   r:   r   r   )rI   r  rT   clsrK   rK   rL   	_get_formN  s    zTestFieldPanel._get_form)r   rW   r  c                 C   s,   |j s|t}|j|p|  | j| jdS )N)rW   r   r   )modelr   r:   r   r  r   r   )rI   r   rW   rK   rK   rL   _get_bound_panel[  s    

zTestFieldPanel._get_bound_panelc              	   C   s.   t dt}| t |j W 5 Q R X d S )NZbarbecue)r   r   r6   assertRaisesr   db_field)rI   field_panelrK   rK   rL   test_non_model_fieldf  s    z#TestFieldPanel.test_non_model_fieldc                 C   s0   | j }| }| d| | |d dg d S NrT   r[   )r  r   rf   r]   rI   r   r   rK   rK   rL   3test_get_form_options_includes_non_read_only_fieldso  s    zBTestFieldPanel.test_get_form_options_includes_non_read_only_fieldsc                 C   s   | j }| }| d| d S )NrT   )r  r   rS   r'  rK   rK   rL   7test_get_form_options_does_not_include_read_only_fieldsu  s    zFTestFieldPanel.test_get_form_options_does_not_include_read_only_fieldsc              
   C   sn   | j ddgd}dD ]T\}}}t||d}| j||d}| | d|  | | | W 5 Q R X qd S )Nri   rO   )rT   ))rO   TT)ri   TTr  rW   z, read_only=)r  r   r!  r   r
  Zis_shown)rI   rW   r   Zmake_read_onlyZexpected_valuer   bound_panelrK   rK   rL   test_boundpanel_is_shownz  s    z'TestFieldPanel.test_boundpanel_is_shownc                 C   sT   |  | j}| |j|jj |  tdddd}| |jd | |jjd d S )Nr[   r  zNew headingr   r   )r!  r  r]   r   Zbound_fieldr   r   )rI   r+  rK   rK   rL   test_override_heading  s    z$TestFieldPanel.test_override_headingc              
   C   s   d t | jjf| j| jd ffD ]\}}| j|dddgd}|  | j| j|d}| }| 	d| J | 
d| | 
d| d	| | 
d
| | 
d| | d| W 5 Q R X q d S )Nr[   rO   rY   r  rT   r*  form data = <inputzvalue=""zdata-field-wrapper(Not required if event is on a single dayerror-message)strr   r[   r  r  rz   r!  r  r   r   rf   rS   )rI   r  Zexpected_input_valuerW   r+  r   rK   rK   rL   test_render_html  s    zTestFieldPanel.test_render_htmlc              
   C   s   | j jd}| jd f| j| jffD ]x\}}| j|ddgd}|  | j||d}| d| 2 |	 }| 
d| | || | d| W 5 Q R X q$d S )	Nz
%B %-d, %YrO   rY   r/  r*  r0  r1  r3  )r   r[   strftimer  r  r  rz   r!  r   r   rS   rf   )rI   Zexpected_value_outputr   r  rW   r+  r   rK   rK   rL   test_render_html_when_read_only  s    z.TestFieldPanel.test_render_html_when_read_onlyc                 C   s    | j | jj}| |d d S )NZPublic)r  r	  r   r  r]   r   rK   rK   rL   .test_format_value_for_display_with_choicefield  s    z=TestFieldPanel.test_format_value_for_display_with_choicefieldc              	   C   s:   t  dd}| d | | j|| W 5 Q R X dS )aV  
        `ForeignKey.formfield()` returns a `ModelChoiceField`, which returns a
        `ModelChoiceIterator` instance when it's `choices` property is
        accessed. This test is to show that `format_value_for_display()` avoids
        evaluating `ModelChoiceIterator` instances, and the database query
        that would trigger.
        ZTitler   r   N)r3   ZassertNumQueriesr]   r  r	  )rI   imagerK   rK   rL   3test_format_value_for_display_with_modelchoicefield  s    
zBTestFieldPanel.test_format_value_for_display_with_modelchoicefieldc                 C   s    | j  d }| |dg d S r&  )r  r   r]   r   rK   rK   rL   r     s    z#TestFieldPanel.test_required_fieldsc                 C   sB   | j ddddd}|  | | j|}| }| d| d S )Nr  
2014-07-20
2014-07-33r  )r  Enter a valid date.)r  rz   r!  r  r   rf   )rI   rW   r+  r   rK   rK   rL   test_error_message_is_rendered  s    z-TestFieldPanel.test_error_message_is_renderedc                 C   sH   |  | j}t|}| d| | d| | d| | d| d S )Nz5model=<class 'wagtail.test.testapp.models.EventPage'>z$instance=Abergavenny sheepdog trialszrequest=<WSGIRequest: GET '/'>zform=EventPageForm)r!  r  r   rf   )rI   r+  Zfield_panel_reprrK   rK   rL   r     s     zTestFieldPanel.test_repr)NN)N)r|   r}   r~   r   r
   r	   r5  r   r   r   r  r   Z
BoundPanelr!  r%  r(  r)  r,  r.  r6  r8  r9  r;  r   r?  r   rK   rK   rK   rL   r  +  s4   $  
  	r  c                   @   s$   e Zd Zdd Zdd Zdd ZdS )TestFieldRowPanelc                 C   s   t  d| _t }|| j_tttddddgg d| _tdt	dd	d
t	dd	dd| _
ttddddtdddgddt| _d S )Nr   rO   rY   rZ   r[   rd   r   r   r   r   r  r  Zcol4ZStartr-  Zcoltwor  Confirmed event dates only)	help_textr   r   r   r   r   r+   r:   r   rV   r   r   r   r   r   dates_panelr   rK   rK   rL   r     s,    



zTestFieldRowPanel.setUpc                 C   s~   | j dddd| jd}|  | jj| j|| jd}| }| d| | d| | d	| | d
| | d| d S )Nr  r<  
2014-07-22r  r   r   zE<label class="w-field__label" for="id_date_to" id="id_date_to-label">r3  rA  zvalue="2014-07-22"r4  	rV   r   rz   rD  r   r   r   rf   rS   rI   rW   r$  r   rK   rK   rL   r6    s,    	z"TestFieldRowPanel.test_render_htmlc                 C   sN   | j dddd| jd}|  | jj| j|| jd}| }| d| d S )Nr  r<  r=  r  r   r   r>  )rV   r   rz   rD  r   r   r   rf   rG  rK   rK   rL   r?  =  s    	z0TestFieldRowPanel.test_error_message_is_renderedN)r|   r}   r~   r   r6  r?  rK   rK   rK   rL   r@    s   %r@  c                   @   s   e Zd Zdd Zdd ZdS )TestFieldRowPanelWithChooserc                 C   sv   t  d| _t }|| j_tttddddgg d| _tdt	dd	d
t	dd	dd| _
ttdtdgt| _d S )Nr   rO   rY   rZ   r[   rd   r   r   r      r  r  r  rC  r   rK   rK   rL   r   T  s*    


z"TestFieldRowPanelWithChooser.setUpc                 C   sZ   | j dddd| jd}|  | jj| j|| jd}| }| d| | d| d S )	Nr  r<  rE  r  r   r   zvalue="2014-07-20"r4  rF  rG  rK   rK   rL   r6  l  s     	z-TestFieldRowPanelWithChooser.test_render_htmlN)r|   r}   r~   r   r6  rK   rK   rK   rL   rH  S  s   rH  c                   @   sr   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d Z
dd Zdd Zdd Zdd Zdd ZdS )TestPageChooserPanel	test.jsonc                 C   s   t  d| _t }|| j_t}ttdgt| _	| j	j
d | _| j	 | _tjjdd| _tjjdd| _|jj| jd| _| j| jd| _| jj| j| j| jd	| _d S )
Nr   pager   	christmasrY   eventsrL  r   r   )r   r   r   r   r   r?   r$   r%   r   rJ   childrenmy_page_chooser_panelrH   PageChooserFormr6   rx   christmas_pageevents_index_pagery   test_instancerW   r   page_chooser_panel)rI   r   r   rK   rK   rL   r     s$      zTestPageChooserPanel.setUpc                 C   s   |  t| jjd jt d S )NrL  )r]   r^   rW   rT   r`   r/   r  rK   rK   rL   %test_page_chooser_uses_correct_widget  s    z:TestPageChooserPanel.test_page_chooser_uses_correct_widgetc                 C   s.   | j  }djdd| jjd}| || d S )Nnew PageChooser("{id}", {{"modelNames": ["{model}"], "canChooseRoot": false, "userPerms": null, "modalUrl": "/admin/choose-page/", "parentId": {parent}}});id_pagewagtailcore.pager   r   parent)rW  r   formatrU  r   rf   )rI   r   expected_jsrK   rK   rL   test_render_js_init  s    
  z(TestPageChooserPanel.test_render_js_initc                 C   sv   t tdddgt}|jd }| }|| jd}|j| j|| jd}|	 }dj
dd	| jjd
}| || d S )NrL  T)Zcan_choose_rootr   r   r   znew PageChooser("{id}", {{"modelNames": ["{model}"], "canChooseRoot": true, "userPerms": null, "modalUrl": "/admin/choose-page/", "parentId": {parent}}});rZ  r[  r\  )r$   r%   r   r?   rQ  rH   rV  r   r   r   r^  rU  r   rf   rI   Zmy_page_object_listrR  rS  rW   rW  r   r_  rK   rK   rL   -test_render_js_init_with_can_choose_root_true  s(    
    zBTestPageChooserPanel.test_render_js_init_with_can_choose_root_truec                 C   s:   | j  }| d| | d| | d| jj | d S )N!<div class="help">help text</div>zQ<div class="chooser__title" data-chooser-title id="id_page-title">Christmas</div>zW<a data-chooser-edit-link href="/admin/pages/%d/edit/" aria-describedby="id_page-title")rW  r   rf   rT  r   r   rK   rK   rL   r6    s    
z%TestPageChooserPanel.test_render_htmlc                 C   sV   t  }| j|d}| jj||| jd}| }| d| | d| | d| d S )Nr   r   rc  zH<div class="chooser__title" data-chooser-title id="id_page-title"></div>zChoose a page)r?   rS  rR  r   r   r   rf   )rI   rV  rW   rW  r   rK   rK   rL   test_render_as_empty_field  s      z/TestPageChooserPanel.test_render_as_empty_fieldc                 C   sL   | j ddi| jd}| |  | jj| j|| jd}| d|  d S )NrL   r   r   r4  )	rS  rV  assertFalserz   rR  r   r   rf   r   )rI   rW   rW  rK   rK   rL   test_render_error  s      z&TestPageChooserPanel.test_render_errorc                 C   st   t tddgt}|jd }| }|| jd}|j| j|| jd}|	 }dj
dd| jjd	}| || d S )
NrL  ztests.EventPager   r   r   rY  rZ  tests.eventpager\  r$   r%   r   r;   rQ  rH   rV  r   r   r   r^  rU  r   rf   ra  rK   rK   rL   test_override_page_type  s(    

    z,TestPageChooserPanel.test_override_page_typec                 C   sr   t tdgt}|jd }| }|| jd}|j| j|| jd}|	 }dj
dd| jjd}| || d S )	NrL  r   r   r   rY  rZ  rh  r\  ri  ra  rK   rK   rL   test_autodetect_page_type  s$    
    z.TestPageChooserPanel.test_autodetect_page_typec                 C   s4   t ddt}| d d }| |jtg d S )NrL  zwagtailcore.siterq   )r%   r   r?   r   r]   Ztarget_modelsr7   )rI   r   r`   rK   rK   rL   test_target_models  s
    
z'TestPageChooserPanel.test_target_modelsc                 C   s"   t ddt}| t|j d S )NrL  snowmanr%   r   r?   r"  r   r   rI   r   rK   rK   rL   !test_target_models_malformed_type  s    z6TestPageChooserPanel.test_target_models_malformed_typec                 C   s"   t ddt}| t|j d S )NrL  zsnowman.lorryrn  ro  rK   rK   rL   #test_target_models_nonexistent_type  s    
z8TestPageChooserPanel.test_target_models_nonexistent_typeN)r|   r}   r~   fixturesr   rX  r`  rb  r6  rd  rg  rj  rk  rl  rp  rq  rK   rK   rK   rL   rJ    s   	rJ  c                   @   sF   e Zd ZdgZdd Zdd Zdd Zeddd	d
d Zdd Z	dS )TestInlinePanelrK  c                 C   s    t  d| _t }|| j_d S Nr   r   r   r   r   r   r   rK   rK   rL   r   %  s    zTestInlinePanel.setUpc                 C   s  t tdddddidgt}| }| dgt|j  tj	j
dd}||d	}|j||| jd
}| }| d| | d| | d| | d| | d| | jd|dd | jd|dd | jd|dd | jd|dd | d| | d| | d| dS )
        Check that the inline panel renders the panels set on the model
        when no 'panels' parameter is passed in the InlinePanel definition
        rb   r   zclassname-for-speakersr   test)r   r   r   rM  rN  r   r   a<label class="w-field__label" for="id_speakers-0-first_name" id="id_speakers-0-first_name-label">zvalue="Father"_<label class="w-field__label" for="id_speakers-0-last_name" id="id_speakers-0-last_name-label">W<label class="w-field__label" for="id_speakers-0-image" id="id_speakers-0-image-label">Choose an image@<input id="id_speakers-0-id" name="speakers-0-id" type="hidden">Tallow_extra_attrsH<input id="id_speakers-0-DELETE" name="speakers-0-DELETE" type="hidden">F<input id="id_speakers-0-ORDER" name="speakers-0-ORDER" type="hidden">N<input id="id_speakers-TOTAL_FORMS" name="speakers-TOTAL_FORMS" type="hidden">var panel = new InlinePanel({zdata-contentpath-disabledzdata-controller="test"N)r$   r!   r   r:   rH   r]   listre   keysrx   r   r   r   r   rf   assertTagInHTML)rI   speaker_object_listrV   
event_pagerW   r   r   rK   rK   rL   r   *  st    	

  zTestInlinePanel.test_renderc                 C   s*  t tddtdtjdtdgdgt}|jd }| }| 	dgt
|j  tjjdd	}||d
}|j||| jd}| }| d| | d| | d| | jd|ddd | d| | d| | jd|dd | jd|dd | jd|dd | jd|dd | d|  dS )z
        Check that inline panel renders the panels listed in the InlinePanel definition
        where one is specified
        rb   r   
first_namer   r:  r   r   r   rM  rN  r   r   rx  zFather</textarea>ry  z$<input id="id_speakers-0-last_name">T)r   r~  rz  r{  r|  r}  r  r  r  r  N)r$   r!   r   r   r   r   r:   rQ  rH   r]   r  re   r  rx   r   r   r   r   rf   rS   r  )rI   r  speaker_inline_panelrV   r  rW   r   r   rK   rK   rL    test_render_with_panel_overridesy  s    

  z0TestInlinePanel.test_render_with_panel_overridesT)ZUSE_L10NZUSE_THOUSAND_SEPARATORc                 C   s|   t tddtdtjdtdgdgt}|jd }| }tj	j
dd	}||d
}|j||| jd}| d|  dS )a  
        Test that the USE_THOUSAND_SEPARATOR setting does not screw up the rendering of numbers
        (specifically maxForms=1000) in the JS initializer:
        https://github.com/wagtail/wagtail/pull/2699
        https://github.com/wagtail/wagtail/issues/3227
        rb   r   r  r   r:  r  r   rM  rN  r   r   zmaxForms: 1000N)r$   r!   r   r   r   r   r:   rQ  rH   rx   r   r   r   rf   r   )rI   r  r  rV   r  rW   r   rK   rK   rL   !test_no_thousand_separators_in_js  s,    

  z1TestInlinePanel.test_no_thousand_separators_in_jsc              	   C   s8   |   & | tdd  | tdd  W 5 Q R X d S )Nc                   S   s
   t ddS )Nr   r   )r!   rK   rK   rK   rL   r     r   zFTestInlinePanel.test_invalid_inlinepanel_declaration.<locals>.<lambda>c                   S   s   t tddddS )Nrb   r   Zchunky)r   bacon)r!   r:   rK   rK   rK   rL   r     s
      )Zignore_deprecation_warningsr"  	TypeErrorr  rK   rK   rL   $test_invalid_inlinepanel_declaration  s    
z4TestInlinePanel.test_invalid_inlinepanel_declarationN)
r|   r}   r~   rr  r   r   r  r   r  r  rK   rK   rK   rL   rs  "  s   OT

rs  c                   @   s"   e Zd ZdgZdd Zdd ZdS )TestNonOrderableInlinePanelrK  c                 C   s   t  | _t }|| j_d S r   )r2   r   r   r   r   rK   rK   rL   r     s    z!TestNonOrderableInlinePanel.setUpc                 C   sZ   t tdddgt}| }t }||d}|j||| jd}| }| d| dS )rv  Zsocial_linkszSocial Linksr   r   r   z%id="id_social_links-__prefix__-ORDER"N)	r$   r!   r   r@   rH   r   r   r   ZassertNotInHTML)rI   Zsocial_link_object_listZPersonPageFormZperson_pagerW   r   r   rK   rK   rL   r     s,    
  z'TestNonOrderableInlinePanel.test_renderN)r|   r}   r~   rr  r   r   rK   rK   rK   rL   r    s   r  c                   @   s"   e Zd ZdgZdd Zdd ZdS )TestInlinePanelGetComparisonrK  c                 C   s    t  d| _t }|| j_d S rt  ru  r   rK   rK   rL   r     s    z"TestInlinePanelGetComparison.setUpc                    sX   t jjddj   j | jd } fdd|D }dd |D }| d| d S )N   r   r   r   c                    s   g | ]}|  qS rK   rK   r   comprP  rK   rL   r   ,  s     zDTestInlinePanelGetComparison.test_get_comparison.<locals>.<listcomp>c                 S   s   g | ]}|  qS rK   Zfield_labelr  rK   rK   rL   r   -  s     r   )	r6   rx   r   Zspecificr   r   r   get_comparisonrf   rI   Z
comparisonZfield_labelsrK   rP  rL   test_get_comparison"  s     z0TestInlinePanelGetComparison.test_get_comparisonNr|   r}   r~   rr  r   r  rK   rK   rK   rL   r    s   r  c                   @   s4   e Zd Zdd Zdd Zdd Zdd Zd	d
 ZdS ),TestInlinePanelRelatedModelPanelConfigChecksc                 C   s*   t j| _tt d dd }d| _|| _d S )Nr   c                  S   s   t jdgd} dd | D S )Nr   )ru   c                 S   s   g | ]}|j tkr|qS rK   )r   r<   )r   warningrK   rK   rL   r   9  s    
 zaTestInlinePanelRelatedModelPanelConfigChecks.setUp.<locals>.get_checks_result.<locals>.<listcomp>)r   r   )Zchecks_resultrK   rK   rL   get_checks_result6  s    zMTestInlinePanelRelatedModelPanelConfigChecks.setUp.<locals>.get_checks_resultzwagtailadmin.W002)r<   r   original_panelsdelattr
warning_idr  )rI   r  rK   rK   rL   r   2  s
    
z2TestInlinePanelRelatedModelPanelConfigChecks.setUpc                 C   s   | j t_d S r   )r  r<   r   r  rK   rK   rL   tearDown@  s    z5TestInlinePanelRelatedModelPanelConfigChecks.tearDownc                 C   sH   t dt dgt_tjddt| jd}|  }| || ttd dS )zSTest that checks will warn against setting single tabbed panel on InlinePanel modelr  	last_namezQEventPageSpeaker.settings_panels will have no effect on InlinePanel model editingzEnsure that EventPageSpeaker uses `panels` instead of `settings_panels`.
There are no tabs on non-Page model editing within InlinePanels.r   settings_panelsN)	r   r<   r  r   Warningr  r  rf   r  )rI   r  checks_resultsrK   rK   rL   2test_page_with_inline_model_with_tabbed_panel_onlyC  s    z_TestInlinePanelRelatedModelPanelConfigChecks.test_page_with_inline_model_with_tabbed_panel_onlyc                 C   sx   t dgt_t dgt_tjddt| jd}tjddt| jd}|  }| || | || t	td t	td	 d
S )zOTest that checks will warn against multiple tabbed panels on InlinePanel modelsr  r  zPEventPageSpeaker.content_panels will have no effect on InlinePanel model editingzEnsure that EventPageSpeaker uses `panels` instead of `content_panels`.
There are no tabs on non-Page model editing within InlinePanels.r   zPEventPageSpeaker.promote_panels will have no effect on InlinePanel model editingzEnsure that EventPageSpeaker uses `panels` instead of `promote_panels`.
There are no tabs on non-Page model editing within InlinePanels.content_panelspromote_panelsN)
r   r<   r  r  r   r  r  r  rf   r  )rI   Z	warning_1Z	warning_2r  rK   rK   rL   2test_page_with_inline_model_with_two_tabbed_panelsY  s&    
z_TestInlinePanelRelatedModelPanelConfigChecks.test_page_with_inline_model_with_two_tabbed_panelsc                 C   sN   t dgt_ttt dgddgt_| |  g  ttd ttd dS )zOChecks should NOT warn if InlinePanel models use tabbed panels AND edit_handlerr  r  rw  r   rJ   r  N)	r   r<   r  r(   r$   rJ   r]   r  r  r  rK   rK   rL   -test_page_with_inline_model_with_edit_handlerv  s    
zZTestInlinePanelRelatedModelPanelConfigChecks.test_page_with_inline_model_with_edit_handlerN)r|   r}   r~   r   r  r  r  r  rK   rK   rK   rL   r  1  s
   r  c                   @   st   e Zd ZdgZdd Zdd Zedddd	 Zd
d Zdd Z	dd Z
dd Zdd Zeddd Zdd ZdS )TestCommentPanelrK  c                 C   s   t  jjdd| _t  jjdd| _t d| _| j| j_tt	 g}|
t| _t|g
t| _| j | _tjjdd| _tjj| jd| jdd	| _tjj| jd
| jd| _tjj| jd| jd| _d S )Nr   pk   r   rM  rN  rw  location)rL  textr   contentpathreply_1)commentr  r   reply_2)r   rx   r   commenting_userr   r   r   r   r$   r   r   r:   r   r(   r   rH   rV   r  r4   ry   r  r5   r  r  rI   Zunbound_object_listrK   rK   rL   r     s6    

    zTestCommentPanel.setUpc                 C   sR   | j  }| }| |j tt| jjgt	}| }| }| 
|j dS )z
        Test that the comments toggle is enabled for a TabbedInterface containing CommentPanel, and disabled otherwise
        N)r   rH   rQ   show_comments_toggler(   r$   r  r  r   r:   rf  )rI   rN   rW   Z&tabbed_interface_without_content_panelrK   rK   rL   test_comments_toggle_enabled  s    
z-TestCommentPanel.test_comments_toggle_enabledF)ZWAGTAILADMIN_COMMENTS_ENABLEDc                 C   s<   |  tdd tjD  t  }| }|  |j dS )z_
        Test that the comment panel is missing if WAGTAILADMIN_COMMENTS_ENABLED=False
        c                 s   s   | ]}t |tV  qd S r   r   r   r   rK   rK   rL   r     s     zBTestCommentPanel.test_comments_disabled_setting.<locals>.<genexpr>N)rf  r   r6   r  r   rH   r  rI   rN   rW   rK   rK   rL   test_comments_disabled_setting  s    z/TestCommentPanel.test_comments_disabled_settingc                 C   s<   |  tdd tjD  t  }| }|  |j dS )zC
        Test that the comment panel is present by default
        c                 s   s   | ]}t |tV  qd S r   r  r   rK   rK   rL   r     s     zATestCommentPanel.test_comments_enabled_setting.<locals>.<genexpr>N)rQ   r   r6   r  r   rH   r  r  rK   rK   rL   test_comments_enabled_setting  s    z.TestCommentPanel.test_comments_enabled_settingc                 C   sH  | j | jd}| jj| j| j|djd }| d }| |d | jj	 | t
|d d | |d d d | jjj	 | t
|d d d d	 | |d d d d d | jjj	 | |d d d d d | jjj	 | t| jj	|d
  | t| jj	|d
  zt|d W n  tk
rB   | d Y nX dS )z|
        Test that the context contains the data about existing comments necessary to initialize the commenting app
        r   )r   r   rW   r   Zcomments_datar   commentsr   replies   Zauthorszcomments-datazhFailed to serialize comments data. This is likely due to a custom user model using an unsupported field.N)rV   r  r   r   r   rQ  Zget_context_datar]   r  r  r   r  r   r  r  rf   r5  r   r   r  Zfail)rI   rW   r   r  rK   rK   rL   test_context  s:        zTestCommentPanel.test_contextc                 C   s   | j | j| jd}| d|j |jd }| t|jd | |jd j| j |jd jd }| t|jd | |jd j| j dS )z
        Check that the form has the comments/replies formsets, and that the
        user has been set on each CommentForm/CommentReplyForm instance
        r   for_userr  r   r   r  r  N)	rV   r  r  rf   re   r]   r   r   r  )rI   rW   Zcomments_formsetZreplies_formsetrK   rK   rL   	test_form  s     
zTestCommentPanel.test_formc                 C   s   | j dddd| jjd| jjdddddddddddd	| j| jd
}|jd jd }| |	  |jd jd }| 
|	  | |jj| j | j dddd| jj| jj| jjdddddd| j| jd
}|jd jd }| |	  d S )Nr  r   r     zedited textre  znew commentznew.path)comments-TOTAL_FORMScomments-INITIAL_FORMScomments-MIN_NUM_FORMScomments-MAX_NUM_FORMScomments-0-idcomments-0-textcomments-0-contentpathcomments-0-replies-TOTAL_FORMS comments-0-replies-INITIAL_FORMS comments-0-replies-MIN_NUM_FORMS comments-0-replies-MAX_NUM_FORMSzcomments-1-idzcomments-1-textzcomments-1-contentpathzcomments-1-replies-TOTAL_FORMSz comments-1-replies-INITIAL_FORMSz comments-1-replies-MIN_NUM_FORMSz comments-1-replies-MAX_NUM_FORMSr  r  )r  r  r  r  r  r  r  comments-0-DELETEr  r  r  r  )rV   r  r  r  r  r  re   r   rf  rz   rQ   r]   r   r   r  rI   rW   comment_formrK   rK   rL   test_comment_form_validation  s^    z-TestCommentPanel.test_comment_form_validationc                 C   s^   | j dddd| jj| jj| jjddddddd| j| jd}|jd jd }| 	|
  d S )Nr   r   r  Za_new_position)r  r  r  r  r  r  r  zcomments-0-positionr  r  r  r  r  r  r  )rV   r  r  r  r  r  r  re   r   rQ   rz   r  rK   rK   rL   %test_users_can_edit_comment_positions8  s(    z6TestCommentPanel.test_users_can_edit_comment_positionsz2017-01-01 12:00:00c                 C   s   | j dddd| jj| jj| jjdddddd| j| jd}|jd jd }| 	|
  |  tjj| jjd}| |j| j tjr| |jtdddd	ddtjd
 n| |jtdddd	dd d S )Nr   r   r  )r  r  r  r  r  r  r  zcomments-0-resolvedr  r  r  r  r  r  r  i     )tzinfo)rV   r  r  r  r  r  r  re   r   rQ   rz   saver4   rx   r   r]   Zresolved_byr   ZUSE_TZZresolved_atr   r   utc)rI   rW   r  Zresolved_commentrK   rK   rL   test_comment_resolveR  s>     z%TestCommentPanel.test_comment_resolvec                 C   sH  | j dddd| jj| jj| jjdddd| jjd| jjddd	d
| j| jd}|j	d j
d }|j	d j
}| |d   | |d   | |d   | |d jj| j | j dddd| jj| jj| jjdddd| jj| jjd| jjddd| j| jd}|j	d j
d }|j	d j
}| |d   | |d   d S )Nr   r   r     r  Zedited_textzEdited text 2re  z	New reply)r  r  r  r  r  r  r  r  r  r  r  comments-0-replies-0-idcomments-0-replies-0-textcomments-0-replies-1-idcomments-0-replies-1-textzcomments-0-replies-2-idzcomments-0-replies-2-textr  r  r  )r  r  r  r  r  r  r  r  r  r  r  r  r  zcomments-0-replies-0-DELETEr  r  zcomments-0-replies-1-DELETE)rV   r  r  r  r  r  r  r  r  re   r   rf  rz   rQ   r]   r   r   )rI   rW   r  Zreply_formsrK   rK   rL   "test_comment_reply_form_validationv  sl    z3TestCommentPanel.test_comment_reply_form_validationN)r|   r}   r~   rr  r   r  r   r  r  r  r  r  r  r   r  r  rK   rK   rK   rL   r    s   
!:
#r  c                   @   s2   e Zd ZdgZdd Zdd Zdd Zdd	 Zd
S )TestPublishingPanelrK  c                 C   sT   |   | _tt g}|t| _t|gt| _| j	 | _
tjjdd| _d S )NrM  rN  )r   r   r$   r'   r   r:   r   r(   r   rH   rV   rx   r   r  r  rK   rK   rL   r     s    

zTestPublishingPanel.setUpc                 C   sR   | j  }| }| |j tt| jjgt	}| }| }| 
|j dS )z
        Test that the schedule publishing toggle is shown for a TabbedInterface containing PublishingPanel, and disabled otherwise
        N)r   rH   rQ   show_schedule_publishing_toggler(   r$   r  r  r   r:   rf  )rI   rN   rW   Z)tabbed_interface_without_publishing_panelrK   rK   rL   ,test_schedule_publishing_toggle_toggle_shown  s    
z@TestPublishingPanel.test_schedule_publishing_toggle_toggle_shownc                 C   s   |  tdd tjD  t  }| }|  |j |jd j}|j	
dd}|j	
dd}|j	
dd}| |d | |d	 | |d
 dS )zF
        Test that the publishing panel is present by default
        c                 s   s   | ]}t |tV  qd S r   )r   r'   r   rK   rK   rL   r     s     zMTestPublishingPanel.test_publishing_panel_shown_by_default.<locals>.<genexpr>	expire_atr   Ndata-actionzdata-w-dialog-targetzw-actionzw-dialog:hidden->w-action#resetnotify)rQ   r   r6   r  r   rH   r  rT   r`   r   r   r]   )rI   rN   rW   Zexpire_at_inputZdata_controllerZdata_actionZdata_w_dialog_targetrK   rK   rL   &test_publishing_panel_shown_by_default  s    z:TestPublishingPanel.test_publishing_panel_shown_by_defaultc                 C   s2   | j | j| jd}| d|j | d|j dS )zI
        Check that the form has the scheduled publishing fields
        r  Z
go_live_atr  N)rV   r  r   rf   base_fields)rI   rW   rK   rK   rL   r    s    zTestPublishingPanel.test_formN)r|   r}   r~   rr  r   r  r  r  rK   rK   rK   rL   r    s
   r  c                   @   s"   e Zd ZdgZdd Zdd ZdS )TestMultipleChooserPanelrK  c                 C   s   t jjdd| _|  | _d S )Nr  r   )r6   rx   r   	root_pager   r   r  rK   rK   rL   r     s    zTestMultipleChooserPanel.setUpc                 C   sH   | j tddd| jjfd}| |jd | |d | |d d S )Nzwagtailadmin_pages:addZtestsZgallerypage)r      z!name="gallery_images-TOTAL_FORMS"zchooserFieldName: "image")clientr   r   r  r   r]   status_codeZassertContains)rI   responserK   rK   rL   test_can_render_panel  s    z.TestMultipleChooserPanel.test_can_render_panelN)r|   r}   r~   rr  r   r  rK   rK   rK   rL   r    s   r  c                   @   s"   e Zd ZdgZdd Zdd ZdS )%TestMultipleChooserPanelGetComparisonrK  c                 C   sH   t  d| _t }|| j_tdd| _tjjdd}|j	| jd d S )Nr   z	Test pager   r  r   r   )
r   r   r   r   r   r>   rL  r6   rx   	add_child)rI   r   Zparent_pagerK   rK   rL   r     s    z+TestMultipleChooserPanelGetComparison.setUpc                    sL    j  j j  jd } fdd|D }dd |D } d| d S )Nr  c                    s   g | ]}| j  j qS rK   rP  r  r  rK   rL   r   (  s     zMTestMultipleChooserPanelGetComparison.test_get_comparison.<locals>.<listcomp>c                 S   s   g | ]}|  qS rK   r  r  rK   rK   rL   r   )  s     zGallery images)rL  r   r   r   r  rf   r  rK   r  rL   r    s    
 z9TestMultipleChooserPanelGetComparison.test_get_comparisonNr  rK   rK   rK   rL   r    s   r  c                   @   s<   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd ZdS )TestPanelIconsc                 C   s    |   | _t | _| j| j_d S r   r   r   r2   r   r  rK   rK   rL   r   .  s    
zTestPanelIcons.setUpc              
   C   s   t ddddft dd ddft ddddft dd	d	d
fg}tdd |D }|t}| }|j| j| d}| }t|D ]n\}\}}}	}
|j	| }|j
}t|jj}| j||jd, | |j| | |d|	 |
 W 5 Q R X qd S )Nr   link-externalr   r  placeholderri   pilcrowr  r:  r  c                 S   s   g | ]^}}|qS rK   rK   r   r   _rK   rK   rL   r   >  s     z?TestPanelIcons.test_default_fieldpanel_icon.<locals>.<listcomp>r   rW   
field_typer   #icon-)r   r$   r   r:   rH   r   r   r   	enumeraterQ  r   r^   r#  r|   r   r   r]   iconr   rI   casesrJ   rN   bound_edit_handlerhtmlir  expected_iconZrendered_defaultZdefault_countr+  r   r  rK   rK   rL   test_default_fieldpanel_icon3  s(    

 
z+TestPanelIcons.test_default_fieldpanel_iconc              
   C   s  t ddddddft ddddddft d	d
dd
ddft ddddddfg}tdd |D }|t}| }|j| j| d}| }t|D ]\}\}}}	}
|j	| }|j
}t|jj}| j||jd> | |j| | d| | | |d|	 |
 W 5 Q R X qd S )Nr   Zcogr  r  r   r  r   r  ri   cutr  r  Zsnippetr:  r   c                 S   s   g | ]^}}|qS rK   rK   r  rK   rK   rL   r   Z  s     z@TestPanelIcons.test_override_fieldpanel_icon.<locals>.<listcomp>r  r  r  )r   r$   r   r:   rH   r   r   r   r  rQ  r   r^   r#  r|   r   r   r]   r  rf   r   r  rK   rK   rL   test_override_fieldpanel_iconN  s*    
 
z,TestPanelIcons.test_override_fieldpanel_iconc           
   
   C   s   t tdtdfddddfttdtdfdd	dd	fg}td
d |D }|t}| }|j| j| d}|	 }t
|D ]R\}\}}|j| }	| jt|d& | |	j| | d| | W 5 Q R X qd S )NrZ   r[   ZDateyszcalendar-alt)r   r  Z	time_fromZtime_toZTimeyshistoryc                 S   s   g | ]^}}|qS rK   rK   r  rK   rK   rL   r   ~  s     z@TestPanelIcons.test_override_panelgroup_icon.<locals>.<listcomp>r  Z
panel_typer  )r"   r   r   r$   r   r:   rH   r   r   r   r  rQ  r   r^   r]   r  rf   
rI   r  rJ   rN   r   r  r  r   r  r+  rK   rK   rL   test_override_panelgroup_iconk  s8    	
 
z,TestPanelIcons.test_override_panelgroup_iconc           
   
   C   s   t dddddftddddd	dfg}td
d |D }|t}| }|j| j| d}| }t	|D ]R\}\}}|j
| }	| jt|d& | |	j| | d| | W 5 Q R X qld S )NZcarousel_itemsZCarouseyZcogs)r   r  rc   ZLinkyZ	link_pagepick)r   Zchooser_field_namer  c                 S   s   g | ]^}}|qS rK   rK   r  rK   rK   rL   r     s     zATestPanelIcons.test_override_inlinepanel_icon.<locals>.<listcomp>r  r	  r  )r!   r#   r$   r   r:   rH   r   r   r   r  rQ  r   r^   r]   r  rf   r
  rK   rK   rL   test_override_inlinepanel_icon  s2    
 
z-TestPanelIcons.test_override_inlinepanel_iconc              
   C   s   t jjdd}tdddddd}|j|d	}tjji |d
 tddddftdddfg}t	dd |D }|
t}| }|j| j| |d}| }t|D ]R\}\}	}
|j| }| jt|	d& | |j|
 | d|
 | W 5 Q R X qd S )Nr  r   z
Contact usz
contact-uszto@email.comzfrom@email.comzThe subject)rO   rY   Z
to_addressZfrom_addresssubjectr   )Z	form_datarL  Zthank_you_redirect_pageresetr  Z	thumbtackc                 S   s   g | ]^}}|qS rK   rK   r  rK   rK   rL   r     s     z@TestPanelIcons.test_override_misc_panel_icon.<locals>.<listcomp>r   rW   r   r	  r  )r6   rx   r   r=   r  r0   ry   r%   r1   r$   r   rH   r   r   r   r  rQ  r   r^   r]   r  rf   )rI   r  Z	form_pager  rJ   rN   r   r  r  r   r  r+  rK   rK   rL   test_override_misc_panel_icon  s6    
  
z,TestPanelIcons.test_override_misc_panel_iconN)	r|   r}   r~   r   r  r  r  r  r  rK   rK   rK   rL   r  -  s   !r  c                   @   s   e Zd ZdgZdd ZedfddZee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d Zdd  Zd!d" Zd#d$ Zd%d& Zd'd( Zd)d* ZdS )+TestTitleFieldPanelrK  c                 C   s    |   | _t | _| j| j_d S r   r  r  rK   rK   rL   r     s    
zTestTitleFieldPanel.setUpNc                 C   s8   | |}| }|j| j| |d}| }| |S )Nr  )r   rH   r   r   r   Zget_soup)rI   rJ   r   r   rN   r   r  rK   rK   rL   get_edit_handler_html  s    
z)TestTitleFieldPanel.get_edit_handler_htmlc                 C   s,   t  }|jd jd }| t|t d S )Nr   )r6   r   rQ  rQ   r   r)   )rI   rJ   Zfirst_inner_panel_childrK   rK   rL   1test_default_page_content_panels_uses_title_field  s    zETestTitleFieldPanel.test_default_page_content_panels_uses_title_fieldc                 C   s   |  ttdtdg}| |jddid |dj}| |d d | |d d	 | |d
 d | |d d | |d d d S )NrO   rY   classw-panel titler   inputrw   r  zPage title*r   w-syncdata-w-sync-target-value#id_slugr  Ofocus->w-sync#check blur->w-sync#apply change->w-sync#apply keyup->w-sync#apply)r  r$   r)   r   assertIsNotNonefindr   r]   rI   r  r   rK   rK   rL   test_default_title_field_panel  s    z2TestTitleFieldPanel.test_default_title_field_panelc                 C   sJ   |  ttdg}| |jddid |dj}| |d d d S )NrO   r  r  r   r  r  re  )r  r$   r)   r  r  r   r]   r  rK   rK   rL   test_form_without_slugfield  s    z/TestTitleFieldPanel.test_form_without_slugfieldc                 C   sZ   | j ttdtdddgt d}| |jddid |d	j}| |d
 d d S )NrO   rY   Tr  r   r  r  r   r  r  re  )	r  r$   r)   r   r:   r  r  r   r]   r  rK   rK   rL   !test_form_with_readonly_slugfield	  s    z5TestTitleFieldPanel.test_form_with_readonly_slugfieldc                 C   s   t jjdd}| |jd | jttdtdg|d}| 	|
djd | jttddd	tdg|d}| |
djd d
S )z
        If the Page (or any model) has `live = True`, do not apply the actions by default.
        Allow this to be overridden though.
        rM  rN  TrO   rY   r   r  r  )apply_if_liveN)r:   rx   r   r]   liver  r$   r)   r   assertIsNoner  r   r  )rI   Z
event_liver  rK   rK   rL   $test_not_using_apply_actions_if_live	  s    z8TestTitleFieldPanel.test_not_using_apply_actions_if_livec                 C   sb   | j ttddgdtdgtd}|dj}| |d d | |d d	 | |d
  d S )Nr  urltargets)r   r  r   r  r  #id_urlr  )	r  r$   r)   r   r9   r  r   r]   r  r  rK   rK   rL   *test_using_apply_actions_if_non_page_model)	  s    z>TestTitleFieldPanel.test_using_apply_actions_if_non_page_modelc                 C   s   t dddd}d|_| jttddgdtdgt |d	}|d
j}| |d d | |d d | 	|
d | jttddgddtdgt |d	}|d
j}| |
d dS )z^
        Check for instance being live should be agnostic to how that is implemented.
        zFree sheepdogzhttps://example.comi  )r  r&  r   Tr  r&  r'  )r   r   r  r   r  r  r)  r  )r(  r"  N)r9   r#  r  r$   r)   r   r  r   r]   r$  r   r  )rI   Zadvert_liver  r   rK   rK   rL   =test_using_apply_actions_if_non_page_model_with_live_property5	  s4    
zQTestTitleFieldPanel.test_using_apply_actions_if_non_page_model_with_live_propertyc                 C   s>   |  ttdg dtdg}|dj}| |d d d S )NrO   r'  rY   r  r  re  r  r$   r)   r   r  r   r]   r  rK   rK   rL    test_targets_override_with_empty]	  s
    z4TestTitleFieldPanel.test_targets_override_with_emptyc                 C   sP   |  ttddgdtdg}|dj}| |d d | |d d d S )	Nr  rO   r'  r  r   r  r  z	#id_titler,  r  rK   rK   rL   )test_targets_override_with_non_slug_fieldf	  s    z=TestTitleFieldPanel.test_targets_override_with_non_slug_fieldc                 C   sX   |  ttdddgdtdtdg}|dj}| |d d | |d d	 d S )
NrO   r   r  r'  r  r   r  r  z#id_cost, #id_locationr,  r  rK   rK   rL   *test_targets_override_with_multiple_fieldsr	  s    
z>TestTitleFieldPanel.test_targets_override_with_multiple_fieldsc                 C   sN   |  ttdddtdg}| |jddid | |jddid d S )	NrO   zsuper-titler  rY   r  r  r   zw-panel super-title)r  r$   r)   r   r$  r  r  )rI   r  rK   rK   rL   test_classname_override	  s    z+TestTitleFieldPanel.test_classname_overridec                 C   s   t jdddddd}| ttd|dtd	g}|d
j}| |d d | |d d	ddg | |d d | |d d d S )Nzw-cleanz!w-clean#clean blur->w-clean#cleanz
trim upperz.will-be-ignored)r   r  data-w-clean-filters-valuer  r   rO   r   rY   r  r   zw-clean w-syncr   r  r  r  r1  )
r   	TextInputr  r$   r)   r   r  r   r]   join)rI   r`   r  r   rK   rK   rL   test_merging_data_attrs	  s,    	z+TestTitleFieldPanel.test_merging_data_attrsc                 C   sZ   |  ttdddtdg}|dj}| |d d | |d d | d	| d S )
NrO   Fr  rY   r  rw   r   r  r  r  r$   r)   r   r  r   r]   rS   r  rK   rK   rL   test_placeholder_override_false	  s    z3TestTitleFieldPanel.test_placeholder_override_falsec                 C   sZ   |  ttdd dtdg}|dj}| |d d | |d d | d| d S )	NrO   r6  rY   r  rw   r   r  r  r7  r  rK   rK   rL   test_placeholder_override_none	  s    z2TestTitleFieldPanel.test_placeholder_override_nonec                 C   sZ   |  ttdddtdg}|dj}| |d d | |d d | d	| d S )
NrO   re  r6  rY   r  rw   r   r  r  r7  r  rK   rK   rL   &test_placeholder_override_empty_string	  s    z:TestTitleFieldPanel.test_placeholder_override_empty_stringc                 C   sj   |  ttdtjddiddtdg}|dj}| |d d | |d	 d
 | |d d d S )NrO   r  zMy custom placeholderr   r   rY   r  rw   r   r  	r  r$   r)   r   r3  r   r  r   r]   r  rK   rK   rL   $test_placeholder_override_via_widget	  s     z8TestTitleFieldPanel.test_placeholder_override_via_widgetc              	   C   sl   |  ttddtjddiddtdg}|dj}| |d	 d | |d
 d | |d d d S )NrO   zPANEL placeholderr  WIDGET placeholderr   r  r`   rY   r  rw   r   r  r;  r  rK   rK   rL   /test_placeholder_override_via_widget_over_kwarg	  s"    zCTestTitleFieldPanel.test_placeholder_override_via_widget_over_kwargc              	   C   sl   |  ttddtjddiddtdg}|dj}| |d	 d | |d
 d | |d d d S )NrO   Fr  r=  r   r>  rY   r  rw   r   r  r;  r  rK   rK   rL   5test_placeholder_override_via_widget_over_false_kwarg
  s"    zITestTitleFieldPanel.test_placeholder_override_via_widget_over_false_kwarg)r|   r}   r~   rr  r   r:   r  r   r6   r  r  r   r!  r%  r*  r+  r-  r.  r/  r0  r5  r8  r9  r:  r<  r?  r@  rK   rK   rK   rL   r    s0   

(	$r  N)rr   r   r   	functoolsr   typingr   r   r	   r
   Zunittestr   djangor   Zdjango.confr   Zdjango.contrib.authr   Zdjango.contrib.auth.modelsr   r   Zdjango.corer   Zdjango.core.exceptionsr   r   Zdjango.testr   r   r   Zdjango.urlsr   Zdjango.utils.htmlr   r   Z	freezegunr   Zwagtail.admin.formsr   r   Zwagtail.admin.panelsr   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   Zwagtail.admin.rich_textr,   Zwagtail.admin.widgetsr-   r.   r/   Zwagtail.contrib.forms.modelsr0   Zwagtail.contrib.forms.panelsr1   Zwagtail.coreutilsr2   Zwagtail.imagesr3   Zwagtail.modelsr4   r5   r6   r7   Zwagtail.test.testapp.formsr8   Zwagtail.test.testapp.modelsr9   r:   r;   r<   r=   r>   r?   r@   rA   rB   rC   rD   Zwagtail.test.utilsrE   rF   r   r   r   r   r   r   r   r  r@  rH  rJ  rs  r  r  r  r  r  r  r  r  r  rK   rK   rK   rL   <module>   sn   D8 9`m dB TU3  U$U  <> 