U
    {hP                     @   s   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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 G dd deeZG dd deZG dd deZG dd deZG dd deZ dS )    )AbstractBaseUserGroup)SimpleTestCaseTestCase)reverse)	urlencode)hooks)widgets)page_header_buttonspage_listing_more_buttons)Button)Page)
SimplePage)WagtailTestUtils)RemovedInWagtail70Warningc                   @   s   e Zd Zdd ZdS )TestButtonsHooksc                 C   s8   |   | _tjjdd| _| jjtddddd| _d S )N   )idzPublic pageZhelloT)titlecontentlive)instance)	loginuserr   objectsget	root_pageZ	add_childr   Z
child_pageself r   n/var/www/dating/data/www/fatepal.com/env/lib/python3.8/site-packages/wagtail/admin/tests/test_buttons_hooks.pysetUp   s    
zTestButtonsHooks.setUpN)__name__
__module____qualname__r!   r   r   r   r    r      s   r   c                   @   s   e Zd Zdd Zdd ZdS )TestPageListingButtonsHooksc              
   C   s   ddd}t d|8 | td  | jtd| jjfd}W 5 Q R X W 5 Q R X | 	|j
d | |d | |d	 | |d
 d S )Nc                 s   s   t jddddV  d S )N#Another useless page listing button/custom-url
   priority)wagtailadmin_widgetsPageListingButtonpage
page_permsnext_urlr   r   r    "page_listing_buttons_old_signature    s
      zxTestPageListingButtonsHooks.test_register_page_listing_buttons_old_signature.<locals>.page_listing_buttons_old_signatureregister_page_listing_buttonszf`register_page_listing_buttons` hook functions should accept a `user` argument instead of `page_perms`wagtailadmin_exploreargs   5wagtailadmin/pages/listing/_button_with_dropdown.html wagtailadmin/shared/buttons.htmlr&   )Nr   register_temporarilyassertWarnsMessager   clientr   r   r   r   assertEqualstatus_codeassertTemplateUsedassertContains)r   r1   responser   r   r    0test_register_page_listing_buttons_old_signature   s&    
  zLTestPageListingButtonsHooks.test_register_page_listing_buttons_old_signaturec              	   C   sr   d
dd}t d|  | jtd| jjfd}W 5 Q R X | |jd | 	|d | 	|d | 
|d	 d S )Nc                 s   s(   t |tstdtjddddV  d S )Nexpected a user instancer&   r'   r(   r)   )
isinstancer   	TypeErrorr+   r,   r.   r   r0   r   r   r    "page_listing_buttons_new_signature9   s    
  zxTestPageListingButtonsHooks.test_register_page_listing_buttons_new_signature.<locals>.page_listing_buttons_new_signaturer2   r3   r4   r6   r7   r8   r&   )Nr   r:   r<   r   r   r   r   r=   r>   r?   r@   )r   rG   rA   r   r   r    0test_register_page_listing_buttons_new_signature8   s    
  zLTestPageListingButtonsHooks.test_register_page_listing_buttons_new_signatureN)r"   r#   r$   rB   rI   r   r   r   r    r%      s   r%   c                   @   sL   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S )TestPageListingMoreButtonsHooksc              
   C   s   ddd}t d|8 | td  | jtd| jjfd}W 5 Q R X W 5 Q R X | 	|j
d | |d | |d	 | |d
 d S )Nc                 s   s   t jddddV  d S )N1Another useless button in default "More" dropdownr'   r(   r)   r+   r   r-   r   r   r    r   S   s
    z}TestPageListingMoreButtonsHooks.test_register_page_listing_more_buttons_with_old_signature.<locals>.page_listing_more_buttons"register_page_listing_more_buttonszk`register_page_listing_more_buttons` hook functions should accept a `user` argument instead of `page_perms`r3   r4   r6   r7   r8   ;Another useless button in default &quot;More&quot; dropdown)Nr9   r   r   rA   r   r   r    :test_register_page_listing_more_buttons_with_old_signatureR   s.    
   zZTestPageListingMoreButtonsHooks.test_register_page_listing_more_buttons_with_old_signaturec              	   C   sr   d
dd}t d|  | jtd| jjfd}W 5 Q R X | |jd | 	|d | 	|d | 
|d	 d S )Nc                 s   s(   t |tstdtjddddV  d S )NrC   rK   r'   r(   r)   rD   r   rE   r+   r   rF   r   r   r    r   o   s    
z}TestPageListingMoreButtonsHooks.test_register_page_listing_more_buttons_with_new_signature.<locals>.page_listing_more_buttonsrM   r3   r4   r6   r7   r8   rN   )NrH   rO   r   r   r    :test_register_page_listing_more_buttons_with_new_signaturen   s$    

   zZTestPageListingMoreButtonsHooks.test_register_page_listing_more_buttons_with_new_signaturec              
   C   sX   | j }| td, tjdd||| jdddddd	}W 5 Q R X | |j| j d S )
NzUButtonWithDropdownFromHook should be passed a `user` argument instead of `page_perms`One more more button+register_page_listing_one_more_more_buttonsr'   _blank
noreferrertargetrel2   )	hook_namer.   r/   r0   attrsr*   )r   r;   r   r+   ButtonWithDropdownFromHookZpermissions_for_userr   r=   )r   r.   buttonr   r   r    ?test_button_with_dropdown_from_hook_accepts_page_perms_argument   s    

z_TestPageListingMoreButtonsHooks.test_button_with_dropdown_from_hook_accepts_page_perms_argumentc                 C   s   ddd}ddd}t d|P t d|8 | td  | jtd| jjfd	}W 5 Q R X W 5 Q R X W 5 Q R X | 	|j
d
 | |d | |d | |d | |d d S )Nc              	   s   s$   t jdd| ||dddddV  d S NrS   rT   rU   rV   rW   rZ   )r[   r.   r   r0   r\   r*   r+   r]   rF   r   r   r    page_custom_listing_buttons   s    zxTestPageListingMoreButtonsHooks.test_custom_button_with_dropdown_with_old_signature.<locals>.page_custom_listing_buttonsc                 s   s   t jddddV  d S )NBAnother useless dropdown button in "One more more button" dropdownr'   r(   r)   rL   r-   r   r   r     page_custom_listing_more_buttons   s
    z}TestPageListingMoreButtonsHooks.test_custom_button_with_dropdown_with_old_signature.<locals>.page_custom_listing_more_buttonsr2   rT   zt`register_page_listing_one_more_more_buttons` hook functions should accept a `user` argument instead of `page_perms`r3   r4   r6   r7   r8   rS   LAnother useless dropdown button in &quot;One more more button&quot; dropdown)N)Nr9   r   rb   rd   rA   r   r   r    3test_custom_button_with_dropdown_with_old_signature   s<    

 	" zSTestPageListingMoreButtonsHooks.test_custom_button_with_dropdown_with_old_signaturec              
   C   s   ddd}ddd}t d|8 t d|  | jtd| jjfd}W 5 Q R X W 5 Q R X | |jd	 | 	|d
 | 	|d | 
|d | 
|d d S )Nc              	   s   s$   t jdd| ||dddddV  d S r`   ra   rF   r   r   r    rb      s    zxTestPageListingMoreButtonsHooks.test_custom_button_with_dropdown_with_new_signature.<locals>.page_custom_listing_buttonsc                 s   s(   t |tstdtjddddV  d S )NrC   rc   r'   r(   r)   rQ   rF   r   r   r    rd      s    
z}TestPageListingMoreButtonsHooks.test_custom_button_with_dropdown_with_new_signature.<locals>.page_custom_listing_more_buttonsr2   rT   r3   r4   r6   r7   r8   rS   re   )N)NrH   rf   r   r   r    3test_custom_button_with_dropdown_with_new_signature   s2    


  zSTestPageListingMoreButtonsHooks.test_custom_button_with_dropdown_with_new_signaturec                 C   sb   | j }td|jgd}d}|d td|i }t|| j|d}tdd |D }| |j| d	S )
V
        Ensure that the built in delete button supports a next_url provided.
        wagtailadmin_pages:deleter4   a/random/url/?nextr   r0   c                 s   s   | ]}|j d kr|V  qdS DeleteNlabel.0r^   r   r   r    	<genexpr>  s     
 zSTestPageListingMoreButtonsHooks.test_delete_button_with_next_url.<locals>.<genexpr>N)	r   r   r   r   r   r   rm   r=   urlr   r.   base_urlr0   full_urlbuttonsdelete_buttonr   r   r     test_delete_button_with_next_url   s    z@TestPageListingMoreButtonsHooks.test_delete_button_with_next_urlc           	      C   s   | j }td|jgd}td|jgd}t|| j|d}tdd |D }| |j| td|jgd}tdd |D }|d	 td
|i }| |j| dS )z
        Ensure that the built in delete button on page listing will not use
        the next_url provided if that URL is directing the user to edit the page.
        As the page is now deleted and cannot be edited.
        rj   r4   r3   rn   c                 s   s   | ]}|j d kr|V  qdS ro   rq   rs   r   r   r    ru     s     
 z[TestPageListingMoreButtonsHooks.test_delete_button_with_invalid_next_url.<locals>.<genexpr>wagtailadmin_pages:unpublishc                 s   s   | ]}|j d kr|V  qdS Z	UnpublishNrq   rs   r   r   r    ru     s    
 rl   rm   N)	r   r   r   r   r   rm   r=   rv   r   	r   r.   rx   r0   rz   r{   Zunpublish_base_urlZunpublish_buttonry   r   r   r    (test_delete_button_with_invalid_next_url  s    zHTestPageListingMoreButtonsHooks.test_delete_button_with_invalid_next_urlc                 C   s   | j }| jddd}|jtjjdd dd t||dD }| t	d	d |D d
 | jddd}|jtjjdd dd t||dD }t
dd |D }| |jd|j  d S )Neditorpassword)usernamer   ZEditors)namec                 S   s   g | ]}|j r|qS r   showrs   r   r   r    
<listcomp>)  s   zRTestPageListingMoreButtonsHooks.test_reorder_button_visibility.<locals>.<listcomp>)r   c                 S   s   g | ]}|j d kr|qS )Sort menu orderrq   rs   r   r   r    r   /  s     
 r   	publisherZ
Moderatorsc                 S   s   g | ]}|j r|qS r   r   rs   r   r   r    r   7  s   c                 s   s   | ]}|j d kr|V  qdS )r   Nrq   rs   r   r   r    ru   <  s    
 zQTestPageListingMoreButtonsHooks.test_reorder_button_visibility.<locals>.<genexpr>z/admin/pages/%d/?ordering=ord)r   Zcreate_usergroupsaddr   r   r   r   r=   lenrm   rv   r   )r   r.   r   rz   r   Zreorder_buttonr   r   r    test_reorder_button_visibility!  s&    
 
z>TestPageListingMoreButtonsHooks.test_reorder_button_visibilityN)r"   r#   r$   rP   rR   r_   rg   rh   r|   r   r   r   r   r   r    rJ   Q   s   ,,rJ   c                   @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
TestPageHeaderButtonsHooksc              
   C   s~   d
dd}t d|8 | td  | jtd| jjfd}W 5 Q R X W 5 Q R X | 	|j
d | |d | |d	 d S )Nc                 s   s   t jddddV  d S )NAnother useless header buttonr'   r(   r)   rL   r-   r   r   r    custom_page_header_buttonsE  s
      znTestPageHeaderButtonsHooks.test_register_page_header_buttons_old_signature.<locals>.custom_page_header_buttonsregister_page_header_buttonsze`register_page_header_buttons` hook functions should accept a `user` argument instead of `page_perms`wagtailadmin_pages:editr4   r6   4wagtailadmin/pages/listing/_page_header_buttons.htmlr   )Nr9   r   r   rA   r   r   r    /test_register_page_header_buttons_old_signatureD  s&    
  zJTestPageHeaderButtonsHooks.test_register_page_header_buttons_old_signaturec              	   C   sf   d	dd}t d|  | jtd| jjfd}W 5 Q R X | |jd | 	|d | 
|d d S )
Nc                 s   s8   t |tstd|dkr"tdtjddddV  d S )NrC   Zeditzexpected view_name to be 'edit'r   r'   r(   r)   )rD   r   rE   
ValueErrorr+   r   )r.   r   	view_namer0   r   r   r    r   \  s    
  znTestPageHeaderButtonsHooks.test_register_page_header_buttons_new_signature.<locals>.custom_page_header_buttonsr   r   r4   r6   r   r   )NrH   r   r   r   r    /test_register_page_header_buttons_new_signature[  s    
  zJTestPageHeaderButtonsHooks.test_register_page_header_buttons_new_signaturec                 C   sd   | j }td|jgd}d}|d td|i }t|| jd|d}tdd	 |D }| |j| d
S )ri   rj   r4   rk   rl   rm   indexr   r0   c                 s   s   | ]}|j d kr|V  qdS ro   rq   rs   r   r   r    ru     s     
 zNTestPageHeaderButtonsHooks.test_delete_button_with_next_url.<locals>.<genexpr>N)	r   r   r   r   r
   r   rm   r=   rv   rw   r   r   r    r|   u  s       z;TestPageHeaderButtonsHooks.test_delete_button_with_next_urlc           	      C   s   | j }td|jgd}td|jgd}t|| jd|d}tdd |D }| |j| td|jgd}td|jgd}t|| jd|d}td	d |D }| |j| td
|jgd}tdd |D }|d td|i }| |j| dS )z
        Ensure that the built in delete button on page edit/home (header button) will not use
        the next_url provided if that URL is directing the user to edit the page.
        As the page is now deleted and cannot be edited.
        rj   r4   r3   r   r   c                 s   s   | ]}|j d kr|V  qdS ro   rq   rs   r   r   r    ru     s     
 zVTestPageHeaderButtonsHooks.test_delete_button_with_invalid_next_url.<locals>.<genexpr>r   c                 s   s   | ]}|j d kr|V  qdS ro   rq   rs   r   r   r    ru     s     
 r}   c                 s   s   | ]}|j d kr|V  qdS r~   rq   rs   r   r   r    ru     s    
 rl   rm   N)	r   r   r   r
   r   rm   r=   rv   r   r   r   r   r    r     s6          zCTestPageHeaderButtonsHooks.test_delete_button_with_invalid_next_urlN)r"   r#   r$   r   r   r|   r   r   r   r   r    r   C  s   r   c                   @   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S )ButtonComparisonTestCasezTests the comparison functions.c                 C   sL   t ddddd| _t dddd	d| _t dd
ddd| _t ddddd| _d S )NzLabel 1z/url1zclass1 class2d   )	classnamer*   zLabel 2z/url2zclass2 class3r6   z/url3i,  )r   button1button2button3button4r   r   r   r    r!     s0                zButtonComparisonTestCase.setUpc                 C   sJ   |  | j| jk | | j| jk | | j| jk | | jdk d S NZ	Something)
assertTruer   r   assertFalser   r   r   r   r   r    test_eq  s    z ButtonComparisonTestCase.test_eqc              	   C   sZ   |  | j| jk  |  | j| jk  | | j| jk  | t | jdk  W 5 Q R X d S r   )r   r   r   r   r   assertRaisesrE   r   r   r   r    test_lt  s
    z ButtonComparisonTestCase.test_ltc              	   C   s~   |  | j| jk |  | j| jk |  | j| jk |  | j| jk | | j| jk | t | jdk W 5 Q R X d S r   )r   r   r   r   r   r   r   rE   r   r   r   r    test_le  s    z ButtonComparisonTestCase.test_lec              	   C   sZ   |  | j| jk |  | j| jk | | j| jk | t | jdk W 5 Q R X d S r   )r   r   r   r   r   r   rE   r   r   r   r    test_gt  s
    z ButtonComparisonTestCase.test_gtc                 C   s^   |  | j| jk |  | j| jk |  | j| jk |  | j| jk | | j| jk d S )N)r   r   r   r   r   r   r   r   r   r    test_ge  s
    z ButtonComparisonTestCase.test_geN)
r"   r#   r$   __doc__r!   r   r   r   r   r   r   r   r   r    r     s   r   N)!Zdjango.contrib.auth.modelsr   r   Zdjango.testr   r   Zdjango.urlsr   Zdjango.utils.httpr   Zwagtailr   Zwagtail.adminr	   r+   Zwagtail.admin.wagtail_hooksr
   r   Zwagtail.admin.widgets.buttonr   Zwagtail.modelsr   Zwagtail.test.testapp.modelsr   Zwagtail.test.utilsr   Zwagtail.utils.deprecationr   r   r%   rJ   r   r   r   r   r   r    <module>   s"   3 sn