o
    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 pagehelloT)titlecontentlive)instance)	loginuserr   objectsget	root_page	add_childr   
child_pageself r"   f/var/www/html/ndineBlogger/venv/lib/python3.10/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      ddd}t d|+ | td | jtd| jjfd}W d    n1 s*w   Y  W d    n1 s9w   Y  | 	|j
d | |d | |d	 | |d
 d S )Nc                 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       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!   r6   responser"   r"   r#   0test_register_page_listing_buttons_old_signature   s(   
zLTestPageListingButtonsHooks.test_register_page_listing_buttons_old_signaturec                 C      d
dd}t d| | jtd| jjfd}W d    n1 s#w   Y  | |jd | 	|d | 	|d | 
|d	 d S )Nc                 s   *    t |ts
tdtjddddV  d S )Nexpected a user instancer+   r,   r-   r.   )
isinstancer   	TypeErrorr0   r1   r3   r   r5   r"   r"   r#   "page_listing_buttons_new_signature9   s   
zxTestPageListingButtonsHooks.test_register_page_listing_buttons_new_signature.<locals>.page_listing_buttons_new_signaturer8   r9   r:   r<   r=   r>   r+   r?   r   rA   rC   r   r   r   r   rD   rE   rF   rG   )r!   rP   rH   r"   r"   r#   0test_register_page_listing_buttons_new_signature8   s   
zLTestPageListingButtonsHooks.test_register_page_listing_buttons_new_signatureN)r%   r&   r'   rI   rR   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   r)   )Nc                 s   r*   )N1Another useless button in default "More" dropdownr,   r-   r.   r0   r   r2   r"   r"   r#   r   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`r9   r:   r<   r=   r>   ;Another useless button in default &quot;More&quot; dropdownr?   r@   r!   r   rH   r"   r"   r#   :test_register_page_listing_more_buttons_with_old_signatureR   s.   
 
zZTestPageListingMoreButtonsHooks.test_register_page_listing_more_buttons_with_old_signaturec                 C   rJ   )Nc                 s   rK   )NrL   rT   r,   r-   r.   rM   r   rN   r0   r   rO   r"   r"   r#   r   o      
z}TestPageListingMoreButtonsHooks.test_register_page_listing_more_buttons_with_new_signature.<locals>.page_listing_more_buttonsrW   r9   r:   r<   r=   r>   rX   r?   rQ   rY   r"   r"   r#   :test_register_page_listing_more_buttons_with_new_signaturen   s    

zZTestPageListingMoreButtonsHooks.test_register_page_listing_more_buttons_with_new_signaturec              
   C   sl   | j }| td tjdd||| jdddddd	}W d    n1 s'w   Y  | |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_namer3   r4   r5   attrsr/   )r   rB   r   r0   ButtonWithDropdownFromHookpermissions_for_userr   rD   )r!   r3   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|A t d|+ | td | jtd| jjfd	}W d    n1 s6w   Y  W d    n1 sEw   Y  W d    n1 sTw   Y  | 	|j
d
 | |d | |d | |d | |d d S )Nc              	   s   &    t jdd| ||dddddV  d S Nr^   r_   r`   ra   rb   re   )rf   r3   r   r5   rg   r/   r0   rh   rO   r"   r"   r#   page_custom_listing_buttons      zxTestPageListingMoreButtonsHooks.test_custom_button_with_dropdown_with_old_signature.<locals>.page_custom_listing_buttonsc                 s   r*   )NBAnother useless dropdown button in "One more more button" dropdownr,   r-   r.   rU   r2   r"   r"   r#    page_custom_listing_more_buttons   rV   z}TestPageListingMoreButtonsHooks.test_custom_button_with_dropdown_with_old_signature.<locals>.page_custom_listing_more_buttonsr8   r_   zt`register_page_listing_one_more_more_buttons` hook functions should accept a `user` argument instead of `page_perms`r9   r:   r<   r=   r>   r^   LAnother useless dropdown button in &quot;One more more button&quot; dropdownr?   r@   r!   ro   rr   rH   r"   r"   r#   3test_custom_button_with_dropdown_with_old_signature   sB   

	  zSTestPageListingMoreButtonsHooks.test_custom_button_with_dropdown_with_old_signaturec              	   C   s   ddd}ddd}t d|+ t d| | jtd| jjfd}W d    n1 s/w   Y  W d    n1 s>w   Y  | |jd	 | 	|d
 | 	|d | 
|d | 
|d d S )Nc              	   s   rl   rm   rn   rO   r"   r"   r#   ro      rp   zxTestPageListingMoreButtonsHooks.test_custom_button_with_dropdown_with_new_signature.<locals>.page_custom_listing_buttonsc                 s   rK   )NrL   rq   r,   r-   r.   r[   rO   r"   r"   r#   rr      r\   z}TestPageListingMoreButtonsHooks.test_custom_button_with_dropdown_with_new_signature.<locals>.page_custom_listing_more_buttonsr8   r_   r9   r:   r<   r=   r>   r^   rs   r?   rQ   rt   r"   r"   r#   3test_custom_button_with_dropdown_with_new_signature   s4   


 
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:deleter:   a/random/url/?nextr   r5   c                 s       | ]
}|j d kr|V  qdS DeleteNlabel.0rj   r"   r"   r#   	<genexpr>      zSTestPageListingMoreButtonsHooks.test_delete_button_with_next_url.<locals>.<genexpr>N)	r   r   r   r   r   r   r{   rD   urlr!   r3   base_urlr5   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.
        rx   r:   r9   r|   c                 s   r}   r~   r   r   r"   r"   r#   r     r   z[TestPageListingMoreButtonsHooks.test_delete_button_with_invalid_next_url.<locals>.<genexpr>wagtailadmin_pages:unpublishc                 s   r}   	UnpublishNr   r   r"   r"   r#   r         rz   r{   N)	r   r   r   r   r   r{   rD   r   r   	r!   r3   r   r5   r   r   unpublish_base_urlunpublish_buttonr   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   Editors)namec                 S      g | ]}|j r|qS r"   showr   r"   r"   r#   
<listcomp>)      zRTestPageListingMoreButtonsHooks.test_reorder_button_visibility.<locals>.<listcomp>)r   c                 S   s   g | ]	}|j d kr|qS )Sort menu orderr   r   r"   r"   r#   r   /  s    r   	publisher
Moderatorsc                 S   r   r"   r   r   r"   r"   r#   r   7  r   c                 s   r}   )r   Nr   r   r"   r"   r#   r   <  r   zQTestPageListingMoreButtonsHooks.test_reorder_button_visibility.<locals>.<genexpr>z/admin/pages/%d/?ordering=ord)r   create_usergroupsaddr   r   r   r   rD   lenr{   r   r   )r!   r3   r   r   r   reorder_buttonr"   r"   r#   test_reorder_button_visibility!  s$   

z>TestPageListingMoreButtonsHooks.test_reorder_button_visibilityN)r%   r&   r'   rZ   r]   rk   ru   rv   r   r   r   r"   r"   r"   r#   rS   Q   s    ,,rS   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|+ | td | jtd| jjfd}W d    n1 s*w   Y  W d    n1 s9w   Y  | 	|j
d | |d | |d	 d S )Nc                 s   r*   )NAnother useless header buttonr,   r-   r.   rU   r2   r"   r"   r#   custom_page_header_buttonsE  r7   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:editr:   r<   4wagtailadmin/pages/listing/_page_header_buttons.htmlr   r?   r@   r!   r   rH   r"   r"   r#   /test_register_page_header_buttons_old_signatureD  s(   
 
zJTestPageHeaderButtonsHooks.test_register_page_header_buttons_old_signaturec                 C   sz   d	dd}t d| | jtd| jjfd}W d    n1 s#w   Y  | |jd | 	|d | 
|d d S )
Nc                 s   s:    t |ts
td|dkrtdtjddddV  d S )NrL   editzexpected view_name to be 'edit'r   r,   r-   r.   )rM   r   rN   
ValueErrorr0   r   )r3   r   	view_namer5   r"   r"   r#   r   \  s   
znTestPageHeaderButtonsHooks.test_register_page_header_buttons_new_signature.<locals>.custom_page_header_buttonsr   r   r:   r<   r   r   r?   rQ   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 )rw   rx   r:   ry   rz   r{   indexr   r5   c                 s   r}   r~   r   r   r"   r"   r#   r     r   zNTestPageHeaderButtonsHooks.test_delete_button_with_next_url.<locals>.<genexpr>N)	r   r   r   r   r
   r   r{   rD   r   r   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.
        rx   r:   r9   r   r   c                 s   r}   r~   r   r   r"   r"   r#   r     r   zVTestPageHeaderButtonsHooks.test_delete_button_with_invalid_next_url.<locals>.<genexpr>r   c                 s   r}   r~   r   r   r"   r"   r#   r     r   r   c                 s   r}   r   r   r   r"   r"   r#   r     r   rz   r{   N)	r   r   r   r
   r   r{   rD   r   r   r   r"   r"   r#   r     s*   

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 class3r<   z/url3i,  )r   button1button2button3button4r    r"   r"   r#   r$     s   zButtonComparisonTestCase.setUpc                 C   sJ   |  | j| jk | | j| jk | | j| jk | | jdk d S N	Something)
assertTruer   r   assertFalser   r   r    r"   r"   r#   test_eq  s   z ButtonComparisonTestCase.test_eqc                 C   sp   |  | j| jk  |  | j| jk  | | j| jk  | t | jdk  W d    d S 1 s1w   Y  d S r   )r   r   r   r   r   assertRaisesrN   r    r"   r"   r#   test_lt     "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 d    d S 1 sCw   Y  d S r   )r   r   r   r   r   r   r   rN   r    r"   r"   r#   test_le  s   "z ButtonComparisonTestCase.test_lec                 C   sp   |  | j| jk |  | j| jk | | j| jk | t | jdk W d    d S 1 s1w   Y  d S r   )r   r   r   r   r   r   rN   r    r"   r"   r#   test_gt  r   z ButtonComparisonTestCase.test_gtc                 C   s^   |  | j| jk |  | j| jk |  | j| jk |  | j| jk | | j| jk d S r?   )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)!django.contrib.auth.modelsr   r   django.testr   r   django.urlsr   django.utils.httpr   wagtailr   wagtail.adminr	   r0   wagtail.admin.wagtail_hooksr
   r   wagtail.admin.widgets.buttonr   wagtail.modelsr   wagtail.test.testapp.modelsr   wagtail.test.utilsr   wagtail.utils.deprecationr   r   r(   rS   r   r   r"   r"   r"   r#   <module>   s$    3 sn