U
    {h(                    @   s  d dl 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
 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mZmZ d dlmZ d dlmZmZmZmZm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z' d dl(m)Z) d dl*m+Z+ G dd de)e
Z,G dd de)e
Z-G dd de)e
Z.G dd de)e
Z/G dd de)e
Z0G dd de)e
Z1G dd de)e
Z2G dd de)e
Z3G dd  d e)e
Z4ed!d"G d#d$ d$e)e
Z5ed!d"G d%d& d&e)e
Z6G d'd( d(e)e
Z7G d)d* d*e)e
Z8dS )+    N)mock)Group
Permission)HttpRequestHttpResponse)TestCase)override_settings)reverse)timezone)gettext_lazy)GroupPagePermissionLocalePageRevision)page_published)BusinessChildBusinessIndexBusinessSubIndexDefaultStreamPage
PersonPageSimpleChildPage
SimplePageSimpleParentPageSingletonPageSingletonPageViaMaxCountStandardChildStandardIndex)WagtailTestUtils)submittable_timestampc                   @   s  e Zd 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ed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d0d1 Zd2d3 Zd4d5 Zd6d7 Zd8d9 Zd:d; Z d<d= Z!d>d? Z"d@dA Z#dBdC Z$dDdE Z%dFdG Z&dHdI Z'dJdK Z(dLdM Z)dNdO Z*dPdQ Z+dRdS Z,dTdU Z-dVdW Z.dXdY Z/dZd[ Z0d\d] Z1d^d_ Z2eddd`da Z3dbdc Z4ddS )eTestPageCreationzdata-side-panel-toggle="status"[^<]+<svg[^<]+<use[^<]+</use[^<]+</svg[^<]+<div data-side-panel-toggle-counter[^>]+w-bg-critical-200[^>]+>\s*%(num_errors)s\s*</div>c                 C   s   t jjdd| _|  | _d S N   idr   objectsget	root_pageloginuserself r,   r/var/www/dating/data/www/fatepal.com/env/lib/python3.8/site-packages/wagtail/admin/tests/pages/test_create_page.pysetUp&   s    zTestPageCreation.setUpc                 C   s   | j td| jjfd}| |jd | |d tddd| jjfd}| |d|  | |d	 | |d
 | |d | |d d S )Nwagtailadmin_pages:add_subpageargs   Simple pagewagtailadmin_pages:addtests
simplepagez	href="%s"zA simple page descriptionzMTI base pagezAbstract pageBusiness child	clientr&   r	   r'   r#   assertEqualstatus_codeassertContainsassertNotContainsr+   response
target_urlr,   r,   r-   test_add_subpage-   s     z!TestPageCreation.test_add_subpagec                 C   sh   t ddd}| jj|d | jtd|jfd}| |jd | 	|d | 	|d	 | 
|d
 d S )NHello world!hello-worldtitlesluginstancer/   r0   r2   r7   z&A lazy business child page descriptionr3   )r   r'   	add_childr9   r&   r	   r#   r:   r;   r<   r=   r+   business_indexr?   r,   r,   r-   #test_add_subpage_with_subpage_types@   s    z4TestPageCreation.test_add_subpage_with_subpage_typesc                 C   st   t ddd}| jj|d tddd}|j|d | jtd|jfd}| |j	d | 
|d | |d	 d S )
NzHello World!rC   rD   rG   r/   r0   r2   z1Sorry, you cannot create a page at this location.z/Choose which type of page you'd like to create.)r   r'   rI   r   r9   r&   r	   r#   r:   r;   r<   r=   )r+   Zsimple_parent_pageZsimple_child_pager?   r,   r,   r-   (test_no_subpage_type_available_to_createR   s,      z9TestPageCreation.test_no_subpage_type_available_to_createc                 C   sj   t ddd}| jj|d tddd}|j|d | jtd|jfd}| |tddd	|jfd d S )
NrB   rC   rD   rG   r/   r0   r4   r5   businesschild)	r   r'   rI   r   r9   r&   r	   r#   assertRedirects)r+   rK   business_subindexr?   r,   r,   r-   ,test_add_subpage_with_one_valid_subpage_typej   s(    
z=TestPageCreation.test_add_subpage_with_one_valid_subpage_typec                 C   sX   d| j _| j jtjjddd | j   | jt	d| j
jfd}| |jd d S )NFwagtailadminaccess_adminZcontent_type__app_labelcodenamer/   r0   .  r)   is_superuseruser_permissionsaddr   r%   r&   saver9   r	   r'   r#   r:   r;   r+   r?   r,   r,   r-    test_add_subpage_bad_permissions   s     
z1TestPageCreation.test_add_subpage_bad_permissionsc                 C   s&   | j tddd}| |jd d S )Nr/   )順 r0     r9   r&   r	   r:   r;   r\   r,   r,   r-   "test_add_subpage_nonexistantparent   s    
z3TestPageCreation.test_add_subpage_nonexistantparentc                 C   sd   | j td| jjfdddi}| |jd | |d tddd	| jjfd}| |d
|  d S )Nr/   r0   nextz/admin/users/r2   r3   r4   r5   r6   zhref="%s?next=/admin/users/"r9   r&   r	   r'   r#   r:   r;   r<   r>   r,   r,   r-    test_add_subpage_with_next_param   s     z1TestPageCreation.test_add_subpage_with_next_paramc                 C   s   | j tddd| jjfd}| |jd | |d d | |d | |d	 | |d
 | |d | |d | |d d S )Nr4   r5   r6   r0   r2   zContent-Typeztext/html; charset=utf-8r<a id="tab-label-content" href="#tab-content" class="w-tabs__tab " role="tab" aria-selected="false" tabindex="-1">r<a id="tab-label-promote" href="#tab-promote" class="w-tabs__tab " role="tab" aria-selected="false" tabindex="-1">zW<button type="submit" name="action-panic" value="Panic!" class="button">Panic!</button>ztestapp/js/siren.jszW<button type="submit" name="action-relax" value="Relax." class="button">Relax.</button>zX<button type="submit" name="action-submit" value="Submit for moderation" class="button">rc   r\   r,   r,   r-   test_create_simplepage   s:    z'TestPageCreation.test_create_simplepageF)ZWAGTAIL_WORKFLOW_ENABLEDc                 C   s<   | j tddd| jjfd}| |jd | |d d S )Nr4   r5   r6   r0   r2   zvalue="Submit for moderation")r9   r&   r	   r'   r#   r:   r;   r=   r\   r,   r,   r-   6test_workflow_buttons_not_shown_when_workflow_disabled   s    zGTestPageCreation.test_workflow_buttons_not_shown_when_workflow_disabledc                 C   s   | j tddd| jjfd}| |jd | |d | |d | j tddd| jjfd}| |jd | 	|d d	S )
z|
        Test checks if 'enctype="multipart/form-data"' is added and only to forms that require multipart encoding.
        r4   r5   r6   r0   r2   zenctype="multipart/form-data"wagtailadmin/pages/create.htmlZfilepageN)
r9   r&   r	   r'   r#   r:   r;   r=   assertTemplateUsedr<   r\   r,   r,   r-   test_create_multipart   s"     z&TestPageCreation.test_create_multipartc                 C   sH   | j tddd| jjfd}| |jd | |d | |d dS )	zd
        Test that the Promote tab is not rendered for page classes that define it as empty
        r4   r5   Zstandardindexr0   r2   re   ztab-promoteNr8   r\   r,   r,   r-   $test_create_page_without_promote_tab   s    z5TestPageCreation.test_create_page_without_promote_tabc                 C   sT   | j tddd| jjfd}| |jd | |d | |d | |d d	S )
z=
        Test that custom edit handlers are rendered
        r4   r5   standardchildr0   r2   re   rf   zv<a id="tab-label-dinosaurs" href="#tab-dinosaurs" class="w-tabs__tab " role="tab" aria-selected="false" tabindex="-1">Nrc   r\   r,   r,   r-   !test_create_page_with_custom_tabs   s&    z2TestPageCreation.test_create_page_with_custom_tabsc                 C   sH   | j tddd| jjfd}| |jd | |d | |d dS )	zq
        Test that additional fields defined on the form rather than the model are accepted and rendered
        r4   r5   Zformclassadditionalfieldpager0   r2   ri   zEnter SMS authentication codeN)	r9   r&   r	   r'   r#   r:   r;   rj   r<   r\   r,   r,   r-   %test_create_page_with_non_model_field  s    z6TestPageCreation.test_create_page_with_non_model_fieldc                 C   s\   d| j _| j jtjjddd | j   | jt	ddd| j
jfd}| |jd	 d S )
NFrR   rS   rT   r4   r5   r6   r0   rV   rW   r\   r,   r,   r-   &test_create_simplepage_bad_permissions$  s$     
z7TestPageCreation.test_create_simplepage_bad_permissionsc                 C   s.   | j tddd| jjfd}| |d d S )Nr4   r5   Zmtibasepager0   /admin/r9   r&   r	   r'   r#   rO   r\   r,   r,   r-   /test_cannot_create_page_with_is_creatable_false=  s    z@TestPageCreation.test_cannot_create_page_with_is_creatable_falsec                 C   s   t dtjjtjj| jjgd}| t| j | j	
|}| |jd | jjtdddd | t| j | j	
|}| |d d S Nr4   r0   r2   Z	singletonrD   rG   rq   )r	   r   _meta	app_label
model_namer'   pk
assertTruecan_create_atr9   r&   r:   r;   rI   assertFalserO   r+   Zadd_urlr?   r,   r,   r-   8test_cannot_create_page_when_can_create_at_returns_falseH  s     

zITestPageCreation.test_cannot_create_page_when_can_create_at_returns_falsec                 C   s   t dtjjtjj| jjgd}| t| j | j	
|}| |jd | jjtdddd | t| j | j	
|}| |d d S rt   )r	   r   ru   rv   rw   r'   rx   ry   rz   r9   r&   r:   r;   rI   r{   rO   r|   r,   r,   r-   0test_cannot_create_singleton_page_with_max_countl  s     

zATestPageCreation.test_cannot_create_singleton_page_with_max_countc                 C   s.   | j tddd| jjfd}| |d d S )Nr4   r5   rN   r0   rq   rr   r\   r,   r,   r-   4test_cannot_create_page_with_wrong_parent_page_types  s    zETestPageCreation.test_cannot_create_page_with_wrong_parent_page_typesc                 C   sF   t ddd}| jj|d | jtddd|jfd}| |d	 d S )
NrB   rC   rD   rG   r4   r5   r6   r0   rq   )r   r'   rI   r9   r&   r	   r#   rO   rJ   r,   r,   r-   0test_cannot_create_page_with_wrong_subpage_types  s    
zATestPageCreation.test_cannot_create_page_with_wrong_subpage_typesc                 C   s   dddd}| j tddd| jjfd|}tjj| jjdd	j	}| 
|td
|jfd | |j|d  | |j|d  | |t | |j | |j | jtt dd d S )N	New page!Some contentrC   rE   contentrF   r4   r5   r6   r0   Zpath__startswithrF   wagtailadmin_pages:editrE   $treebeard found consistency problemsmsg)r9   postr	   r'   r#   r   r%   r&   pathspecificrO   r:   rE   draft_titleassertIsInstancer   r{   livefirst_published_atanyfind_problemsr+   	post_datar?   pager,   r,   r-   test_create_simplepage_post  s8    	  
 z,TestPageCreation.test_create_simplepage_postc                 C   s   t  tjdd }t  tjdd }dddt|t|d}| jtdd	d
| jj	fd|}| 
|jd tjj| jjddj}| 
|j |  | 
|j |  | |jd | 
|jd | tjj|j	djdd  d S )N   daysr!   r   r   rC   rE   r   rF   
go_live_at	expire_atr4   r5   r6   r0   rV   r   FZdraftZ	object_idTZapproved_go_live_at__isnull)r
   nowdatetime	timedeltar   r9   r   r	   r'   r#   r:   r;   r   r%   r&   r   r   r   dater   assertIsexpiredstatus_stringr{   r   page_revisionsfilterexcludeexistsr+   r   r   r   r?   r   r,   r,   r-    test_create_simplepage_scheduled  s:    	 z1TestPageCreation.test_create_simplepage_scheduledc              	   C   s   dddt t tjdd t t tjdd d}| jtdd	d
| jj	fd|}| 
|jd | |jd dd | |jd dd | j|ddd d}| |j | jd|i  | |d d S )Nr   r   rC   r!   r   r   r   r4   r5   r6   r0   r2   formr   z1Go live date/time must be before expiry date/timer   <<div class="w-label-3 w-text-primary">Invalid schedule</div>Thtml
num_errors!data-w-unsaved-force-value="true"r   r
   r   r   r   r9   r   r	   r'   r#   r:   r;   assertFormErrorcontextr<   assertRegexr   decodeSTATUS_TOGGLE_BADGE_REGEXr+   r   r?   r   r,   r,   r-   6test_create_simplepage_scheduled_go_live_before_expiry  sN    zGTestPageCreation.test_create_simplepage_scheduled_go_live_before_expiryc                 C   s   dddt t tjdd d}| jtddd	| jj	fd
|}| 
|jd | |jd dd | j|ddd d}| |j | jd|i  | |d d S )Nr   r   rC   r   )rE   r   rF   r   r4   r5   r6   r0   r2   r   r   z&Expiry date/time must be in the futurer   Tr   r   r   r   r   r   r,   r,   r-   3test_create_simplepage_scheduled_expire_in_the_past#  s>    zDTestPageCreation.test_create_simplepage_scheduled_expire_in_the_pastc              	   C   s6  t  }t| zddddd}| jtddd| jj	fd	|}t
jj| jjdd
j}| |td| jj	fd	 | |j|d  | |j|d  | |t | |j | |j | |jd |jd d }| |d |j | |d | | |d |j | jtt
 dd W 5 t| X d S )Nr   r   rC   PublishrE   r   rF   action-publishr4   r5   r6   r0   r   wagtailadmin_explorerE   r   r   r!   ZsenderrH   r   r   )r   Z	MagicMockr   connectZ
disconnectr9   r   r	   r'   r#   r   r%   r&   r   r   rO   r:   rE   r   r   r   ry   r   r   Z
call_countZ
mock_callsZspecific_classr{   r   r   )r+   Zmock_handlerr   r?   r   Z	mock_callr,   r,   r-   #test_create_simplepage_post_publishN  sL    
	  
 
z4TestPageCreation.test_create_simplepage_post_publishc                 C   s  t  tjdd }t  tjdd }ddddt|t|d}| jtd	d
d| jj	fd|}| 
|jd tjj| jjddj}| 
|j |  | 
|j |  | |jd | tjj|j	djdd  | |j | |j | 
|jd d S )Nr   r   r!   r   r   rC   r   )rE   r   rF   r   r   r   r4   r5   r6   r0   rV   r   Fr   Tr   Z	scheduled) r
   r   r   r   r   r9   r   r	   r'   r#   r:   r;   r   r%   r&   r   r   r   r   r   r   r   ry   r   r   r   r   r   r{   r   r   r   r   r,   r,   r-   -test_create_simplepage_post_publish_scheduled  s@    	 z>TestPageCreation.test_create_simplepage_post_publish_scheduledc                 C   s   |  ddd ddddd}| jtd	d
d| jjfd|}tjj| jj	ddj
}| |td| jjfd | |j|d  | |t | |j | |j | |jj|jj d S )NZ	moderatorzmoderator@email.compasswordr   r   rC   SubmitrE   r   rF   action-submitr4   r5   r6   r0   r   r   rE   )Zcreate_superuserr9   r   r	   r'   r#   r   r%   r&   r   r   rO   r:   rE   r   r   r{   r   r   Zcurrent_workflow_statestatusZSTATUS_IN_PROGRESSr   r,   r,   r-   "test_create_simplepage_post_submit  s:    	  z3TestPageCreation.test_create_simplepage_post_submitc                 C   s   t dddd| _| jj| jd ddddd	}| jtd
dd| jjfd|}| |j	d | 
|jd dd | |d d S )NrB   rC   Zhello)rE   rF   r   rG   r   r   r   r   r4   r5   r6   r0   r2   r   rF   z?The slug 'hello-world' is already in use within the parent pager   )r   Z
child_pager'   rI   r9   r   r	   r#   r:   r;   r   r   r<   r+   r   r?   r,   r,   r-   )test_create_simplepage_post_existing_slug  s2      	z:TestPageCreation.test_create_simplepage_post_existing_slugc                 C   s&   | j tddd}| |jd d S )Nr4   )r5   r6   r^   r0   r_   r`   r\   r,   r,   r-   test_create_nonexistantparent  s    
z.TestPageCreation.test_create_nonexistantparentc                 C   s0   | j tddd| jjfd}| |jd d S )Nr4   Zwagtailimagesimager0   r_   )r9   r&   r	   r'   r#   r:   r;   r\   r,   r,   r-   test_create_nonpagetype  s    z(TestPageCreation.test_create_nonpagetypec                 C   s   | j tddd| jjfddddd}| |jd	 | |jd
 dd | 	t
jj| jjdd  | j tddd| jjfddddd}t
jj| jjdd}| |td|jfd d S )Nr4   r5   validatedpager0   r   not barrC   )rE   foorF   r2   r   r   zField foo must be barr   superbarr   )r9   r   r	   r'   r#   r:   r;   r   r   r{   r   r%   r   r   r   r&   rO   )r+   r?   r   r,   r,   r-   test_custom_validation  sH       z'TestPageCreation.test_custom_validationc                 C   s   ddddd}t ddd| jjfd	}| j||}| |jd
 | |j	 ddd | j
|}| |jd
 | |d | |d | |jd j| jjd  | |jd j| jj | |jd  | j | |d d S )Nr   r   rC   r   r   !wagtailadmin_pages:preview_on_addr5   r6   r0   r2   TZis_validZis_availableztests/simple_page.htmlr+   r   zEdit this page)r	   r'   r#   r9   r   r:   r;   assertJSONEqualr   r   r&   rj   r<   r   depthry   r   
startswithZ
get_parentr=   r+   r   Zpreview_urlr?   r,   r,   r-   test_preview_on_create0  s.    z'TestPageCreation.test_preview_on_createc                 C   s   ddddd}t ddd| jjfd	}| j||}| |jd
 | |j	 ddd ddddd}t ddd| jjfd	}| j||}| |jd
 | |j	 ddd | j
|}| |jd
 | |d | |d d S )Nr   r   rC   r   )rE   r   rF   r   r   r5   r   r0   r2   Fr   r   Tztests/validated_page.htmlzfoo = superbar)r	   r'   r#   r9   r   r:   r;   r   r   r   r&   rj   r<   r   r,   r,   r-   #test_preview_with_custom_validationS  sD    z4TestPageCreation.test_preview_with_custom_validationc                 C   sF   ddddd}| j tddd| jjfd	|}| |jd
 dd d S )N r   rC   r   r   r4   r5   r6   r0   r   rE   This field is required.r9   r   r	   r'   r#   r   r   r   r,   r,   r-   test_whitespace_titles  s"    	  z'TestPageCreation.test_whitespace_titlesc                 C   sF   ddddd}| j tddd| jjfd	|}| |jd
 dd d S )N	r   rC   r   r   r4   r5   r6   r0   r   rE   r   r   r   r,   r,   r-   test_whitespace_titles_with_tab  s"    	  z0TestPageCreation.test_whitespace_titles_with_tabc                 C   s`   dddddd}| j tddd	| jjfd
|}| |jd tj	d
 }| |jd d S )NHellor   rC   r   r   rE   r   rF   r   	seo_titler4   r5   r6   r0   rV   -id )r9   r   r	   r'   r#   r:   r;   r   r%   order_byfirstr   r   r,   r,   r-   ,test_whitespace_titles_with_tab_in_seo_title  s     	z=TestPageCreation.test_whitespace_titles_with_tab_in_seo_titlec                 C   s|   dddddd}| j tddd	| jjfd
|}| |jd tj	d
 }| |jd | |jd | |jd d S )Nz   Hello   r   rC   r   z   hello SEO   r   r4   r5   r6   r0   rV   r   r   z	hello SEO)r9   r   r	   r'   r#   r:   r;   r   r%   r   r   rE   r   r   r   r,   r,   r-   'test_whitespace_is_stripped_from_titles  s$    	z8TestPageCreation.test_whitespace_is_stripped_from_titlesc                 C   sT   ddddd}| j tddd| jjfd	|}| |jd
 | |jd dd d S )NzHello worldr   a  hello-world-hello-world-hello-world-hello-world-hello-world-hello-world-hello-world-hello-world-hello-world-hello-world-hello-world-hello-world-hello-world-hello-world-hello-world-hello-world-hello-world-hello-world-hello-world-hello-world-hello-world-hello-world-hello-world-hello-worldr   r   r4   r5   r6   r0   r2   r   rF   z:Ensure this value has at most 255 characters (it has 287).)	r9   r   r	   r'   r#   r:   r;   r   r   r   r,   r,   r-   test_long_slug  s$    		zTestPageCreation.test_long_slugc                 C   sf   | j tddd| jjfd}| |j}|dddij}dd	d
dddddddd
}| 	|| dS )z
        Should correctly add the sync field and placeholder attributes to the title field.
        Note: Many test Page models use a FieldPanel for 'title', StandardChild does not
        override content_panels (uses the default).
        r4   r5   rm   r0   inputnamerE   z(panel-child-content-child-title-helptextzOfocus->w-sync#check blur->w-sync#apply change->w-sync#apply keyup->w-sync#applyzw-syncz#id_slugZid_titleZ255zPage title*r   text)
zaria-describedbyzdata-actiondata-controllerzdata-w-sync-target-valuer#   Z	maxlengthr   placeholderrequiredtypeN)
r9   r&   r	   r'   r#   get_soupr   findattrsr:   )r+   r?   r   Zactual_attrsZexpected_attrsr,   r,   r-   test_title_field_attrs  s(    z'TestPageCreation.test_title_field_attrsc              	      sb    fdd}  d|$  jtddd jjfd}W 5 Q R X  |jd  |jd	 d S )
Nc                    s2     | t  |j jj  |t tdS NOverridden!r   r   r:   r#   r'   r   r   requestZparent_pageZ
page_classr*   r,   r-   	hook_func  s    z@TestPageCreation.test_before_create_page_hook.<locals>.hook_funcbefore_create_pager4   r5   r6   r0   r2      Overridden!)	register_hookr9   r&   r	   r'   r#   r:   r;   r   )r+   r   r?   r,   r*   r-   test_before_create_page_hook  s    z-TestPageCreation.test_before_create_page_hookc              	      s    fdd}  d|2 dddd} jtdd	d
 jjfd|}W 5 Q R X  |jd  |jd  	t
jjdd  d S )Nc                    s2     | t  |j jj  |t tdS r   r   r   r*   r,   r-   r   (  s    zETestPageCreation.test_before_create_page_hook_post.<locals>.hook_funcr   r   r   rC   r   r4   r5   r6   r0   r2   r   rE   )r   r9   r   r	   r'   r#   r:   r;   r   r{   r   r%   r   r   r+   r   r   r?   r,   r*   r-   !test_before_create_page_hook_post'  s     z2TestPageCreation.test_before_create_page_hook_postc              	      s    fdd}  d|2 dddd} jtdd	d
 jjfd|}W 5 Q R X  |jd  |jd  	t
jjdd  d S )Nc                    s8     | t   |t  |j  |j tdS r   )r   r   r   assertIsNoner   last_published_atr   r   r   r*   r,   r-   r   D  s
    z?TestPageCreation.test_after_create_page_hook.<locals>.hook_funcafter_create_pager   r   rC   r   r4   r5   r6   r0   r2   r   r   r   r9   r   r	   r'   r#   r:   r;   r   ry   r   r%   r   r   r  r,   r*   r-   test_after_create_page_hookC  s     
z,TestPageCreation.test_after_create_page_hookc              	      s    fdd}  d|4 ddddd} jtd	d
d jjfd|}W 5 Q R X  |jd  |jd  	t
jjdd  d S )Nc                    s8     | t   |t  |j  |j tdS r   )r   r   r   assertIsNotNoner   r  r   r  r*   r,   r-   r   c  s
    zQTestPageCreation.test_after_create_page_hook_with_page_publish.<locals>.hook_funcr  r   r   rC   r   r   r4   r5   r6   r0   r2   r   r   r  r  r,   r*   r-   -test_after_create_page_hook_with_page_publishb  s"    	z>TestPageCreation.test_after_create_page_hook_with_page_publishc              	      s    fdd}  d|4 ddddd} jtd	d
d jjfd|}W 5 Q R X  |jd  |jd  j	    j
 d jtd d S )Nc                    s:     | t  |jd  |j  |j tdS Nr   r   )r   r   r:   rE   r	  r   r  r   r  r*   r,   r-   r     s
    z;TestPageCreation.test_after_publish_page.<locals>.hook_funcZafter_publish_pager   r   rC   r   r   r4   r5   r6   r0   r2   r   r   r   r   r9   r   r	   r'   r#   r:   r;   r   Zrefresh_from_dbZget_childrenr   _r  r,   r*   r-   test_after_publish_page  s$    	
z(TestPageCreation.test_after_publish_pagec              	      s    fdd}  d|4 ddddd} jtd	d
d jjfd|}W 5 Q R X  |jd  |jd  j	    j
 d jtd d S )Nc                    s:     | t  |jd  |j  |j tdS r  )r   r   r:   rE   r  r   r  r   r  r*   r,   r-   r     s
    z<TestPageCreation.test_before_publish_page.<locals>.hook_funcZbefore_publish_pager   r   rC   r   r   r4   r5   r6   r0   r2   r   r   zlive + draftr  r  r,   r*   r-   test_before_publish_page  s*    	
 z)TestPageCreation.test_before_publish_pagec                 C   s.   | j tddd| jjfd}| |d dS )zg
        Tests that by default the "Submit for Moderation" button is shown in the action menu.
        r4   r5   r6   r0   z<button type="submit" name="action-submit" value="Submit for moderation" class="button"><svg class="icon icon-resubmit icon" aria-hidden="true"><use href="#icon-resubmit"></use></svg>Submit for moderation</button>N)r9   r&   r	   r'   r#   r<   r\   r,   r,   r-   )test_display_moderation_button_by_default  s    z:TestPageCreation.test_display_moderation_button_by_defaultc                 C   s.   | j tddd| jjfd}| |d dS )zz
        Tests that if WAGTAIL_WORKFLOW_ENABLED is set to False, the "Submit for Moderation" button is not shown.
        r4   r5   r6   r0   zv<button type="submit" name="action-submit" value="Submit for moderation" class="button">Submit for moderation</button>Nr9   r&   r	   r'   r#   r=   r\   r,   r,   r-   test_hide_moderation_button  s    z,TestPageCreation.test_hide_moderation_buttonc                 C   sZ   t jjdd}| jjtdd|dd}| jtddd	|j	fd
}| 
|jd j| d S )NfrZlanguage_codeZHomezhome-fr)rE   rF   localerG   r4   r5   r6   r0   r   )r   r%   creater'   rI   r   r9   r&   r	   r#   r:   r   r  )r+   	fr_localeZfr_homepager?   r,   r,   r-   (test_create_sets_locale_to_parent_locale  s     
z9TestPageCreation.test_create_sets_locale_to_parent_localeN)5__name__
__module____qualname__r   r.   rA   rL   rM   rQ   r]   ra   rd   rg   r   rh   rk   rl   rn   ro   rp   rs   r}   r~   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r
  r  r  r  r  r  r,   r,   r,   r-   r       sh   "

$#%3+4)(%	)#.! 
r   c                   @   s"   e Zd ZdgZdd Zdd ZdS )TestPermissionedFieldPanels	test.jsonc                 C   s2   t jjdd| _tjjtjjdd| jdd d S )Nr!   r"   zSite-wide editorsr   rZ   groupr   Zpermission_type)r   r%   r&   r'   r   r  r   r*   r,   r,   r-   r.     s    z!TestPermissionedFieldPanels.setUpc                 C   s   | j ddd | jtddd| jjfd}| |jd | |d	 | 	|d
 | j ddd | jtddd| jjfd}| |jd | |d	 | |d
 dS )zI
        Test that permission rules on field panels are honoured
        Z
siteeditorr   )usernamer   r4   r5   Z
secretpager0   r2   z"boring_data"z"secret_data"Z	superuserN)
r(   r9   r&   r	   r'   r#   r:   r;   r<   r=   r\   r,   r,   r-   .test_create_page_with_permissioned_field_panel  s(    zJTestPermissionedFieldPanels.test_create_page_with_permissioned_field_panelN)r  r  r  fixturesr.   r"  r,   r,   r,   r-   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 )TestSubpageBusinessRulesc                 C   s   t jjdd| _t | _d| j_d| j_| jj| jd t	 | _
d| j
_d| j
_| jj| j
d t | _d| j_d	| j_| j
j| jd t | _d
| j_d| j_| j
j| jd |   d S )Nr!   r"   zStandard Indexzstandard-indexrG   zBusiness Indexzbusiness-indexzBusiness Childzbusiness-childzBusiness Subindexzbusiness-subindex)r   r%   r&   r'   r   standard_indexrE   rF   rI   r   rK   r   business_childr   rP   r(   r*   r,   r,   r-   r.     s$    zTestSubpageBusinessRules.setUpc                 C   s   t d| jjfd}| jt d| jjfd}| |jd | || | j|}| |jd | |t	  | |t
	  | |t	  | |t	  d S Nr/   r0   r   r2   )r	   r%  r#   r9   r&   r:   r;   r<   r   get_verbose_namer   r=   r   r   r+   Zadd_subpage_urlr?   r,   r,   r-   test_standard_subpage>  s     z.TestSubpageBusinessRules.test_standard_subpagec                 C   s   t d| jjfd}| jt d| jjfd}| |jd | || | j|}| |jd | |t	
  | |t
  | |t
  | |t
  d S r'  )r	   rK   r#   r9   r&   r:   r;   r<   r=   r   r(  r   r   r   r)  r,   r,   r-   test_business_subpageS  s     z.TestSubpageBusinessRules.test_business_subpagec                 C   sr   t d| jjfd}| jt d| jjfd}| |jd | || | jt d| jjfd}| |jd d S )Nr/   r0   r   r2   rV   )r	   r&  r#   r9   r&   r:   r;   r=   r)  r,   r,   r-   test_business_child_subpageg  s     z4TestSubpageBusinessRules.test_business_child_subpagec                 C   s   | j tddd| jjfd}| |d | j tddd| jjfd}| |d | j tddd| jjfd}| |d | j tddd| jjfd}| |j	d d S )Nr4   r5   rm   r0   rq   rN   r2   )
r9   r&   r	   rK   r#   rO   r&  r%  r:   r;   r\   r,   r,   r-   $test_cannot_add_invalid_subpage_typey  s8    z=TestSubpageBusinessRules.test_cannot_add_invalid_subpage_typec                 C   s<   | j td| jjfd}| |tddd| jjfd d S )Nr/   r0   r4   r5   rN   )r9   r&   r	   rP   r#   rO   r\   r,   r,   r-   4test_not_prompted_for_page_type_when_only_one_choice  s    zMTestSubpageBusinessRules.test_not_prompted_for_page_type_when_only_one_choiceN)	r  r  r  r.   r*  r+  r,  r-  r.  r,   r,   r,   r-   r$    s   %r$  c                   @   s   e Zd ZdZdd ZdS )TestInlinePanelMediaz_
    Test that form media required by InlinePanels is correctly pulled in to the edit page
    c                 C   s   t jjdd}|   | jtddd|jfd}| |jd | 	|d | jtddd	|jfd}| |jd | 
|d d S )
Nr!   r"   r4   r5   r6   r0   r2   zwagtailadmin/js/draftail.jsZsectionedrichtextpage)r   r%   r&   r(   r9   r	   r#   r:   r;   r=   r<   )r+   homepager?   r,   r,   r-   test_inline_panel_media  s    
z,TestInlinePanelMedia.test_inline_panel_mediaN)r  r  r  __doc__r1  r,   r,   r,   r-   r/    s   r/  c                   @   s   e Zd ZdZdd ZdS )TestInlineStreamFieldz<
    Test that streamfields inside an inline child work
    c                 C   s   t jjdd}|   | jtddd|jfd}| |jd | 	|d | 
|j}|d	d
di}| | | |d | |d d S )Nr!   r"   r4   r5   Zinlinestreampager0   r2   z-<div id="sections-__prefix__-body" data-blockdivr   zw-blockzdata-w-block-arguments-valuezdata-w-block-data-value)r   r%   r&   r(   r9   r	   r#   r:   r;   r<   r   r   r   r	  ry   Zhas_attr)r+   r0  r?   soupZblockDivr,   r,   r-   test_inline_streamfield  s    

z-TestInlineStreamField.test_inline_streamfieldN)r  r  r  r2  r6  r,   r,   r,   r-   r3    s   r3  c                   @   s    e Zd ZdZdd Zdd ZdS )TestIssue2994aG  
    In contrast to most "standard" form fields, StreamField form widgets generally won't
    provide a postdata field with a name exactly matching the field name. To prevent Django
    from wrongly interpreting this as the field being omitted from the form,
    we need to provide a custom value_omitted_from_data method.
    c                 C   s   t jjdd| _|  | _d S r    r$   r*   r,   r,   r-   r.     s    zTestIssue2994.setUpc              	   C   sn   ddddddddd	}| j td
dd| jjfd| tjjdd}| dt	|j
 | d|j
d j d S )NzIssue 2994 testzissue-2994-test1r   0r   zhello worldr   )rE   rF   z
body-countzbody-0-deletedzbody-0-orderzbody-0-typezbody-0-valuer   r4   r5   Zdefaultstreampager0   rF   r   r   )r9   r   r	   r'   r#   r   r%   r&   r:   lenbodyvalue)r+   r   new_pager,   r,   r-   test_page_edit_post_publish_url  s&    
z-TestIssue2994.test_page_edit_post_publish_urlN)r  r  r  r2  r.   r?  r,   r,   r,   r-   r7    s   r7  c                   @   s   e Zd Zdd Zdd ZdS )TestInlinePanelWithTagsc                 C   s   t jjdd| _|  | _d S r    r$   r*   r,   r,   r-   r.   	  s    zTestInlinePanelWithTags.setUpc                 C   s   dddddddddd	d
ddddddddd}| j tddd| jjfd|}| |td| jjfd tjjdd}| 	|j
 j d d S )NMr Bennmr-bennWilliamBennr   r     z52 Festive Road, Londonzshopkeeper, bowler-hatr   )rE   rF   
first_name	last_nameaddresses-TOTAL_FORMSaddresses-INITIAL_FORMSaddresses-MIN_NUM_FORMSaddresses-MAX_NUM_FORMSzaddresses-0-addresszaddresses-0-tagsr   comments-TOTAL_FORMScomments-INITIAL_FORMScomments-MIN_NUM_FORMScomments-MAX_NUM_FORMSsocial_links-TOTAL_FORMSsocial_links-INITIAL_FORMSsocial_links-MIN_NUM_FORMSsocial_links-MAX_NUM_FORMSr4   r5   
personpager0   r   r:  r!   )r9   r   r	   r'   r#   rO   r   r%   r&   r:   	addressesr   tagscountr+   r   r?   r>  r,   r,   r-   test_create  sB     z#TestInlinePanelWithTags.test_createNr  r  r  r.   rY  r,   r,   r,   r-   r@    s   r@  c                   @   s   e Zd Zdd Zdd ZdS )TestNonOrderableInlinePanelc                 C   s   t jjdd| _|  | _d S r    r$   r*   r,   r,   r-   r.   4  s    z!TestNonOrderableInlinePanel.setUpc                 C   s   dddddddddddddddddd	d
d}| j tddd| jjfd|}| |td| jjfd tjjdd}| 	|j
 d d S )NrA  rB  rC  rD  r   rE  r   r   zhttps://twitter.com/mrbennZtwitter)rE   rF   rF  rG  rH  rI  rJ  rK  r   rL  rM  rN  rO  rP  rQ  rR  rS  zsocial_links-0-urlzsocial_links-0-kindr4   r5   rT  r0   r   r:  )r9   r   r	   r'   r#   rO   r   r%   r&   r:   Zsocial_linksrW  rX  r,   r,   r-   rY  8  sB     z'TestNonOrderableInlinePanel.test_createNrZ  r,   r,   r,   r-   r[  1  s   r[  c                   @   s&   e Zd ZdZdgZdd Zdd ZdS )TestInlinePanelNonFieldErrorszt
    Test that non field errors will render for InlinePanels
    https://github.com/wagtail/wagtail/issues/3890
    zdemosite.jsonc                 C   s   t jjdd| _|  | _d S r    r$   r*   r,   r,   r-   r.   d  s    z#TestInlinePanelNonFieldErrors.setUpc                 C   sz   dddddddddddddddddddd}| j td	d
d| jjfd|}| |jd | |d | j|ddd d S )NzIssue 3890 testzissue-3890-testr   r   rE  r   r   )rE   rF   zrelated_links-TOTAL_FORMSzrelated_links-INITIAL_FORMSzrelated_links-MIN_NUM_FORMSzrelated_links-MAX_NUM_FORMSzrelated_links-0-idzrelated_links-0-ORDERzrelated_links-0-link_pagezrelated_links-0-link_documentzrelated_links-0-link_externalzcarousel_items-INITIAL_FORMSzcarousel_items-MAX_NUM_FORMSzcarousel_items-TOTAL_FORMSr   rL  rM  rN  rO  r4   demositer0  r0   r2   z6The page could not be created due to validation errorszDYou must provide a related page, related document or an external URL)rW  )r9   r   r	   r'   r#   r:   r;   r<   r   r,   r,   r-   rY  h  sJ     z)TestInlinePanelNonFieldErrors.test_createN)r  r  r  r2  r#  r.   rY  r,   r,   r,   r-   r\  \  s   r\  TZWAGTAIL_I18N_ENABLEDc                   @   s<   e Zd ZdgZdd Zdd Zedddd	 Zd
d ZdS )TestLocaleSelectorr  c                 C   sB   t jjdd| _tjjdd| _| jj| jdd| _| 	 | _
d S )Nz/home/events/)Zurl_pathr  r  T)Zcopy_parents)r   r%   r&   events_pager   r  r  Zcopy_for_translationtranslated_events_pager(   r)   r*   r,   r,   r-   r.     s     zTestLocaleSelector.setUpc                 C   sX   | j tddd| jjgd}| |d tddd| jjgd}| |d| d d S )Nr4   r5   	eventpager0   id="status-sidebar-english"href="")r9   r&   r	   r`  r#   r<   ra  r+   r?   add_translation_urlr,   r,   r-   test_locale_selector  s    z'TestLocaleSelector.test_locale_selectorFr^  c                 C   sX   | j tddd| jjgd}| |d tddd| jjgd}| |d| d d S )Nr4   r5   rb  r0   Page Locale:rd  re  )r9   r&   r	   r`  r#   r=   ra  rf  r,   r,   r-   3test_locale_selector_not_present_when_i18n_disabled  s    zFTestLocaleSelector.test_locale_selector_not_present_when_i18n_disabledc                 C   s   t jjdd}tjj|| jdd d| j_| jj	t
jjddd | jj	| | j  | jtd	d
d| jjgd}| |d td	d
d| jjgd}| |d| d d S )NZ
Moderatorsr  rZ   r  FrR   rS   rT   r4   r5   rb  r0   rc  rd  re  )r   r%   r&   r   r  r`  r)   rX   rY   rZ   r   groupsr[   r9   r	   r#   r<   ra  r=   )r+   r   r?   rg  r,   r,   r-   :test_locale_selector_not_present_without_permission_to_add  s6     
zMTestLocaleSelector.test_locale_selector_not_present_without_permission_to_addN)	r  r  r  r#  r.   rh  r   rj  rl  r,   r,   r,   r-   r_    s   
r_  c                   @   s<   e Zd ZdgZdd Zdd Zdd Zedd	d
d ZdS )TestLocaleSelectorOnRootPager  c                 C   s.   t jjdd| _tjjdd| _|  | _d S )Nr   r"   r  r  )	r   r%   r&   r'   r   r  r  r(   r)   r*   r,   r,   r-   r.     s    z"TestLocaleSelectorOnRootPage.setUpc                 C   s   | j tddd| jjgd}| |d tddd| jjgdd }| |d| d tddd| jjgdd	 }| |d| d d S )
Nr4   r]  r0  r0   rc  
?locale=frrd  re  
?locale=enr9   r&   r	   r'   r#   r<   r=   r+   r?   rg  Zself_translation_urlr,   r,   r-   rh    s.    z1TestLocaleSelectorOnRootPage.test_locale_selectorc                 C   s   | j tddd| jjgdd }| |d | |d tddd| jjgdd }| |d	| d
 tddd| jjgdd }| |d	| d
 d S )Nr4   r]  r0  r0   rn  zid="status-sidebar-french"z.<input type="hidden" name="locale" value="fr">ro  rd  re  rp  rq  r,   r,   r-   test_locale_selector_selected  s4    z:TestLocaleSelectorOnRootPage.test_locale_selector_selectedFr^  c                 C   s\   | j tddd| jjgd}| |d tddd| jjgdd }| |d| d d S )	Nr4   r]  r0  r0   ri  rn  rd  re  r  rf  r,   r,   r-   rj  )  s    zPTestLocaleSelectorOnRootPage.test_locale_selector_not_present_when_i18n_disabledN)	r  r  r  r#  r.   rh  rr  r   rj  r,   r,   r,   r-   rm    s   "rm  c                   @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
TestPageSubscriptionSettingsc                 C   s   t jjdd| _|  | _d S r    r$   r*   r,   r,   r-   r.   ?  s    z"TestPageSubscriptionSettings.setUpc                 C   s<   | j tddd| jjgd}| |jd | |d d S )Nr4   r5   r6   r0   r2   zZ<input type="checkbox" name="comment_notifications" id="id_comment_notifications" checked>rc   r\   r,   r,   r-   1test_comment_notifications_switched_on_by_defaultF  s    zNTestPageSubscriptionSettings.test_comment_notifications_switched_on_by_defaultc                 C   s   ddddd}| j tddd| jjgd	|}tjj| jjdd
j	}| 
|td|jgd	 |j }| |j| j | |j d S )Nr   r   rC   on)rE   r   rF   comment_notificationsr4   r5   r6   r0   r   r   )r9   r   r	   r'   r#   r   r%   r&   r   r   rO   subscribersr:   r)   ry   rv  r+   r   r?   r   Zsubscriptionr,   r,   r-   0test_post_with_comment_notifications_switched_onT  s.      
zMTestPageSubscriptionSettings.test_post_with_comment_notifications_switched_onc                 C   s   dddd}| j tddd| jjgd|}tjj| jjdd	j	}| 
|td
|jgd |j }| |j| j | |j d S )Nr   r   rC   r   r4   r5   r6   r0   r   r   )r9   r   r	   r'   r#   r   r%   r&   r   r   rO   rw  r:   r)   r{   rv  rx  r,   r,   r-   1test_post_with_comment_notifications_switched_offq  s,      
zNTestPageSubscriptionSettings.test_post_with_comment_notifications_switched_offN)r  r  r  r.   rt  ry  rz  r,   r,   r,   r-   rs  >  s   rs  c                   @   s2   e Zd ZdZdd Zdd Zedddd	 Zd
S )TestCommentingzE
    Tests the commenting related logic of the create page view.
    c                 C   s   t jjdd| _|  | _d S r    r$   r*   r,   r,   r-   r.     s    zTestCommenting.setUpc                 C   sv   | j tddd| jjgd}| |jd | |j}|	d}| d|d  | 
d	|d
  | d|d  d S )Nr4   r5   r6   r0   r2   [data-edit-form]page-edit-formr#   w-initr   zw-comments:initdata-w-init-event-valuer9   r&   r	   r'   r#   r:   r;   r   r   Z
select_oneZassertInr+   r?   r5  r   r,   r,   r-    test_comments_enabled_by_default  s    
z/TestCommenting.test_comments_enabled_by_defaultF)ZWAGTAILADMIN_COMMENTS_ENABLEDc                 C   sv   | j tddd| jjgd}| |jd | |j}|	d}| d|d  | 
d	|d
  | d|d  d S )Nr4   r5   r6   r0   r2   r|  r}  r#   r~  r   r   r  r  r  r,   r,   r-   test_comments_disabled  s    
z%TestCommenting.test_comments_disabledN)r  r  r  r2  r.   r  r   r  r,   r,   r,   r-   r{    s
   r{  )9r   Zunittestr   Zdjango.contrib.auth.modelsr   r   Zdjango.httpr   r   Zdjango.testr   Zdjango.test.utilsr   Zdjango.urlsr	   Zdjango.utilsr
   Zdjango.utils.translationr   r  Zwagtail.modelsr   r   r   r   Zwagtail.signalsr   Zwagtail.test.testapp.modelsr   r   r   r   r   r   r   r   r   r   r   r   Zwagtail.test.utilsr   Zwagtail.test.utils.timestampsr   r   r  r$  r/  r3  r7  r@  r[  r\  r_  rm  rs  r{  r,   r,   r,   r-   <module>   sL   8         ^) $++5N]P