o
    hm#                     @   sV   d dl 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ZdS )	    N)contextmanager)UnionBeautifulSoup)get_user_model)assert_and_parse_htmlc                   @   s   e Zd Zedeeef defddZedd Z	d&d	d
Z
ed'ddZed'ddZeedd Zed(ddZdd Zdd Zd)ddZdd Zdd Z	d*d d!Zd+d"d#Zd,d$d%ZdS )-WagtailTestUtilsmarkupreturnc                 C   s
   t | dS )Nzhtml.parserr   )r	    r   `/var/www/html/ndineBlogger/venv/lib/python3.10/site-packages/wagtail/test/utils/wagtail_tests.pyget_soup   s   
zWagtailTestUtils.get_soupc                  C   sF   t  } | jdddddi}| jD ]
}||vr|||< q| jjdi |S )zV
        Override this method to return an instance of your custom user model
        ztest@email.comemailpasswordNr   )r   USERNAME_FIELDREQUIRED_FIELDSobjectscreate_superuser)
user_model	user_datafieldr   r   r   create_test_user   s   
z!WagtailTestUtils.create_test_userNr   c                 C   sj   t  }|d u r|d u r|  }t||j}|jdkr"d|vr"d| }| | jjdd|i|j|i |S )Nr   @%s@example.comr   r   )r   r   getattrr   
assertTrueclientlogin)selfuserusernamer   r   r   r   r   r   "   s   
zWagtailTestUtils.loginc                 K   D   t  }|pd|  |d< ||d< |jdkr| ||j< |jjdi |S Nr   r   r   r   )r   r   r   create_userr    r   r   kwargsUserr   r   r   r#   ;      

zWagtailTestUtils.create_userc                 K   r!   r"   )r   r   r   r   r$   r   r   r   r   H   r'   z!WagtailTestUtils.create_superuserc               	   c   st    t jdd} d V  W d    n1 sw   Y  | D ]}t|jttfs7t j|j|j|j|j	|j
|jd qd S )NT)record)messagecategoryfilenamelinenofileline)warningscatch_warnings
issubclassr*   DeprecationWarningPendingDeprecationWarningshowwarningr)   r+   r,   r-   r.   )warning_listwr   r   r   ignore_deprecation_warningsU   s$   
z,WagtailTestUtils.ignore_deprecation_warningsr   c              
   c   sT    ddl m} |||| zd V  W |j| ||f d S |j| ||f w )Nr   )hooks)wagtailr8   register_hooksremove)r   	hook_namefnorderr8   r   r   r   register_hooki   s   .zWagtailTestUtils.register_hookc                 C   s   t |dr
t |dsdS |j|jkrdS t|jt|jkr dS |j|jkrUtt|jD ]'}|j| \}}|j| \}}|d u rC|}|d u rI|}||ksQ||krT dS q-dS NnameFT)hasattrrB   len
attributesrange)r   tag1tag2iattrvalue
other_attrother_valuer   r   r   _tag_is_equals   s$   zWagtailTestUtils._tag_is_equalc                 C   s|   t |dr
t |dsdS |j|jkrdS |jD ]$\}}|d u r1|d f|jvr0||f|jvr0 dS q||f|jvr; dS qdS rA   )rC   rB   rE   )r   thin_tagfat_tagrJ   rK   r   r   r   _tag_matches_with_extra_attrs   s$   z.WagtailTestUtils._tag_matches_with_extra_attrsFc                    s`   d} r |r|d7 }n
|r|d7 }t|dr.|t fdd|jD 7 }|S )Nr      childrenc                 3   s     | ]}j | d V  qdS )allow_extra_attrsN)_count_tag_occurrences).0childrU   needler   r   r   	<genexpr>   s    
z:WagtailTestUtils._count_tag_occurrences.<locals>.<genexpr>)rQ   rN   rC   sumrS   )r   rZ   haystackrU   countr   rY   r   rV      s   

z'WagtailTestUtils._count_tag_occurrencesc                 C   s"   |j dkrdS tdd |jD S )NscriptFc                 s   s    | ]}|d kV  qdS ))typeztext/templateNr   )rW   rJ   r   r   r   r[      s    z;WagtailTestUtils._tag_is_template_script.<locals>.<genexpr>)rB   anyrE   )r   tagr   r   r   _tag_is_template_script   s   
z(WagtailTestUtils._tag_is_template_scriptc                 c   sD    t |dsd S | |r|V  d S |jD ]
}| |E d H  qd S )NrB   )rC   rc   rS   _find_template_script_tags)r   r]   rX   r   r   r   rd      s   



z+WagtailTestUtils._find_template_script_tags c              	   C   sn   t | |d d}t | |d d}| j|||d}|d ur*| |||d|||f   d S | |d|d|   d S )N!First argument is not valid HTML:"Second argument is not valid HTML:rT   z4Found %d instances of '%s' in response (expected %d)r   zCouldn't find '%s' in response)r   rV   assertEqualassertNotEqual)r   rZ   r]   r^   
msg_prefixrU   
real_countr   r   r   assertTagInHTML   s,   z WagtailTestUtils.assertTagInHTMLc                 C   s   | j ||d|d d S )Nr   )r^   rj   )assertInHTML)r   rZ   r]   rj   r   r   r   assertNotInHTML   s   z WagtailTestUtils.assertNotInHTMLc              	   C   s   t | |d d}t | |d d}d}| |D ] }|jr5| t|jd t | |jd d d}|| ||7 }q|d urJ| |||d|||f   d S | |d|d|   d S )Nrf   rg   r   rR   z%Script tag content is not valid HTML:z;Found %d instances of '%s' in template script (expected %d)z%Couldn't find '%s' in template script)r   rd   rS   rh   rD   rV   ri   )r   rZ   r]   r^   rj   rk   
script_tagscript_htmlr   r   r   assertTagInTemplateScript   sB   
z*WagtailTestUtils.assertTagInTemplateScript)NNr   )NN)r   )F)Nre   F)re   )Nre   )__name__
__module____qualname__staticmethodr   strbytesr   r   r   r   r#   r   r   r7   r@   rN   rQ   rV   rc   rd   rl   rn   rq   r   r   r   r   r   
   s0    

	


r   )r/   
contextlibr   typingr   bs4r   django.contrib.authr   django.test.testcasesr   r   r   r   r   r   <module>   s    