U
    |h0                    @   s  d dl Z d dlZd dlZd dlZ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 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$ 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/m0Z0 d dl1m2Z3 d dl1m4Z4 d dl5m6Z6m7Z7 d dl8m9Z9 d dl:m;Z; G dd dej<Z=G dd dej>Z?G dd deZ@G d d! d!e9eZAG d"d# d#ejZBG d$d% d%ejZCG d&d' d'ejZDG d(d) d)ejZEG d*d+ d+eZFG d,d- d-eZGG d.d/ d/eZHG d0d1 d1eZIG d2d3 d3e9eZJG d4d5 d5e9eZKG d6d7 d7ejZLG d8d9 d9ejZMG d:d; d;eZNG d<d= d=eZOG d>d? d?e9eZPG d@dA dAeZQG dBdC dCe9eZRG dDdE dEeZSG dFdG dGeZTG dHdI dIeZUG dJdK dKeZVG dLdM dMejZWG dNdO dOeZXG dPdQ dQeZYG dRdS dSeZZG dTdU dUeZ[G dVdW dWeZ\G dXdY dYeZ]G dZd[ d[eZ^G d\d] d]eZ_G d^d_ d_eZ`dS )`    N)Decimal)formsValidationError)DjangoJSONEncoder)	ErrorList)render_to_string)SimpleTestCaseTestCase)SafeData	mark_safe)gettext_lazy)blocks)get_error_json_data)BlockDefinitionLookup)FieldBlockAdapter)ListBlockAdapterListBlockValidationError)StaticBlockAdapter)StreamBlockAdapterStreamBlockValidationError)StructBlockAdapterStructBlockValidationError)Page)RichText)	LinkBlock)SectionBlock)	EventPage
SimplePage)WagtailTestUtils)RemovedInWagtail70Warningc                       s(   e Zd Ze ZdZ fddZ  ZS )FooStreamBlockz!At least one block must say "foo"c                    s6   t  |}tdd |D s2tjt| jgd|S )Nc                 s   s   | ]}|j d kV  qdS )fooNvalue.0block r(   a/var/www/dating/data/www/fatepal.com/env/lib/python3.8/site-packages/wagtail/tests/test_blocks.py	<genexpr>)   s     z'FooStreamBlock.clean.<locals>.<genexpr>non_block_errors)supercleananyr   r   r   errorselfr$   	__class__r(   r)   r.   '   s    
zFooStreamBlock.clean)	__name__
__module____qualname__r   	CharBlocktextr0   r.   __classcell__r(   r(   r3   r)   r!   #   s   r!   c                       s   e Zd Zd fdd	Z  ZS )ContextCharBlockNc                    s    t | }ttj| ||S N)strupperr-   r   r8   get_context)r2   r$   Zparent_contextr3   r(   r)   r?   1   s    zContextCharBlock.get_context)N)r5   r6   r7   r?   r:   r(   r(   r3   r)   r;   0   s   r;   c                   @   s   e Zd Zdd ZdS )	TestBlockc                 C   s    t  }| t || dS )zFThe base normalize implementation should return its argument unchangedN)objectassertIsr   Block	normalize)r2   objr(   r(   r)   test_normalize7   s    zTestBlock.test_normalizeN)r5   r6   r7   rF   r(   r(   r(   r)   r@   6   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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 )TestFieldBlockc                 C   s"   t  }|d}| |d d S NHello world!r   r8   renderassertEqualr2   r'   htmlr(   r(   r)   test_charfield_render>   s    
z$TestFieldBlock.test_charfield_renderc                 C   s&   t jdd}|d}| |d d S )Ntests/blocks/heading_block.htmltemplaterI   <h1>Hello world!</h1>rJ   rM   r(   r(   r)   #test_charfield_render_with_templateD   s    
z2TestFieldBlock.test_charfield_render_with_templatec                 C   sl   t jdd}|d t |}| |d d | |d tj | |d dddd	d
dddid d S )NzSome helpful text	help_text
test_blockr         z
Test blockTplaceholderz/w-field w-field--char_field w-field--text_inputADD_COMMENTAdd Comment)labelhelpTextrequiredicon	classnameshowAddCommentButtonstrings)	r   r8   set_namer   js_argsrL   assertIsInstancer   Z	TextInputr2   r'   re   r(   r(   r)   test_charblock_adapterJ   s     
z%TestFieldBlock.test_charblock_adapterc                 C   sp   t jdd}|d t |}| d|d d  t jdd}|d t |}| d	|d d  d
S )z
        Meta data test for FormField; this checks if both the meta values
        form_classname and classname are accepted and are rendered
        in the form
        zspecial-char-formclassnameform_classnamerW   z special-char-formclassnamerY   ra   zspecial-char-classname)ra   z special-char-classnameN)r   r8   rd   r   re   assertIn)r2   r'   re   Zblock_with_classnamer(   r(   r)   %test_charblock_adapter_form_classname_   s    

z4TestFieldBlock.test_charblock_adapter_form_classnamec                 C   s,   t dd}|jdddid}| |d d S )NrP   rQ   Bonjour le monde!languagefrcontextz$<h1 lang="fr">BONJOUR LE MONDE!</h1>)r;   rK   rL   rM   r(   r(   r)   6test_charfield_render_with_template_with_extra_contextq   s    
 zETestFieldBlock.test_charfield_render_with_template_with_extra_contextc                 C   s"   t  }|d}| |d d S rH   r   r8   get_form_staterL   r2   r'   
form_stater(   r(   r)   test_charfield_get_form_state|   s    
z,TestFieldBlock.test_charfield_get_form_statec                 C   s$   t  }|d}| |dg d S rH   r   r8   get_searchable_contentrL   r2   r'   contentr(   r(   r)   !test_charfield_searchable_content   s    
z0TestFieldBlock.test_charfield_searchable_contentc                 C   s&   t jdd}|d}| |g  d S )NFsearch_indexrI   rx   rz   r(   r(   r)   $test_search_index_searchable_content   s    
z3TestFieldBlock.test_search_index_searchable_contentc              	   C   s:   dd }t j|gd}| t |d W 5 Q R X d S )Nc                 S   s   | dkrt dd S Nr"   zValue must be 'foo'r   r#   r(   r(   r)   validate_is_foo   s    zETestFieldBlock.test_charfield_with_validator.<locals>.validate_is_foo
validatorsbar)r   r8   assertRaisesr   r.   r2   r   r'   r(   r(   r)   test_charfield_with_validator   s    z,TestFieldBlock.test_charfield_with_validatorc                 C   s2   G dd dt j}| }|d}| |d d S )Nc                   @   s   e Zd ZejddZdS )z;TestFieldBlock.test_choicefield_render.<locals>.ChoiceBlockchoice-1Choice 1choice-2Choice 2choicesNr5   r6   r7   r   ZChoiceFieldfieldr(   r(   r(   r)   ChoiceBlock   s   r   r   )r   
FieldBlockrK   rL   )r2   r   r'   rN   r(   r(   r)   test_choicefield_render   s    
z&TestFieldBlock.test_choicefield_renderc              
   C   s   G dd dt j}| }|d t |}| |d d | |d tj | |d j	ddg | |d d	d
ddd
ddid d S )Nc                   @   s   e Zd ZejddZdS )zATestFieldBlock.test_adapt_custom_choicefield.<locals>.ChoiceBlockr   r   Nr   r(   r(   r(   r)   r      s   r   test_choiceblockr   rX   r   r   rY   Test choiceblockTrZ   -w-field w-field--choice_field w-field--selectr[   r\   r]   r_   r`   ra   rb   rc   )
r   r   rd   r   re   rL   rf   r   Selectr   )r2   r   r'   re   r(   r(   r)   test_adapt_custom_choicefield   s,    
z,TestFieldBlock.test_adapt_custom_choicefieldc                 C   s.   G dd dt j}| }| |dg  dS )z
        FieldBlock should not return anything for `get_searchable_content` by
        default. Subclasses are free to override it and provide relevant
        content.
        c                   @   s   e Zd ZejddZdS )z;TestFieldBlock.test_searchable_content.<locals>.CustomBlockTr_   N)r5   r6   r7   r   Z	CharFieldr   r(   r(   r(   r)   CustomBlock   s   r   zfoo barN)r   r   rL   ry   )r2   r   r'   r(   r(   r)   test_searchable_content   s    z&TestFieldBlock.test_searchable_contentc                 C   s2   G dd dt j}| }|d}| |d d S )Nc                   @   s    e Zd ZdZdd Zdd ZdS )zbTestFieldBlock.test_form_handling_is_independent_of_serialisation.<locals>.Base64EncodingCharBlockzA CharBlock with a deliberately perverse JSON (de)serialisation format
            so that it visibly blows up if we call to_python / get_prep_value where we shouldn't
            c                 S   s
   t |S r<   )base64	b64decode)r2   jsonish_valuer(   r(   r)   	to_python   s    zlTestFieldBlock.test_form_handling_is_independent_of_serialisation.<locals>.Base64EncodingCharBlock.to_pythonc                 S   s
   t |S r<   )r   	b64encode)r2   Znative_valuer(   r(   r)   get_prep_value   s    zqTestFieldBlock.test_form_handling_is_independent_of_serialisation.<locals>.Base64EncodingCharBlock.get_prep_valueN)r5   r6   r7   __doc__r   r   r(   r(   r(   r)   Base64EncodingCharBlock   s   r   zhello worldrs   )r2   r   r'   rv   r(   r(   r)   2test_form_handling_is_independent_of_serialisation   s    
zATestFieldBlock.test_form_handling_is_independent_of_serialisationc                    s   G dd dG fdddt j G  fdddtj}| }d}||}| |d d	d
i}|||i d	}| |d dS )a  
        Check that Field.prepare_value is called before sending the value to
        the widget for rendering.

        Actual real-world use case: A Youtube field that produces YoutubeVideo
        instances from IDs, but videos are entered using their full URLs.
        c                   @   s4   e Zd ZdZdd Zdd Zedd Zdd	 Zd
S )z?TestFieldBlock.test_prepare_value_called.<locals>.PrefixWrapperhttp://example.com/c                 S   s
   || _ d S r<   r#   r1   r(   r(   r)   __init__   s    zHTestFieldBlock.test_prepare_value_called.<locals>.PrefixWrapper.__init__c                 S   s   | j | j S r<   )prefixr$   r2   r(   r(   r)   with_prefix   s    zKTestFieldBlock.test_prepare_value_called.<locals>.PrefixWrapper.with_prefixc                 S   s&   | | jst| |t| jd  S r<   )
startswithr   
ValueErrorlen)clsr$   r(   r(   r)   from_prefixed   s    zMTestFieldBlock.test_prepare_value_called.<locals>.PrefixWrapper.from_prefixedc                 S   s   | j |j kS r<   r#   )r2   otherr(   r(   r)   __eq__   s    zFTestFieldBlock.test_prepare_value_called.<locals>.PrefixWrapper.__eq__N)	r5   r6   r7   r   r   r   classmethodr   r   r(   r(   r(   r)   PrefixWrapper   s   
r   c                       s&   e Zd Z fddZdd Z  ZS )z=TestFieldBlock.test_prepare_value_called.<locals>.PrefixFieldc                    s   t  |} |S r<   )r-   r.   r   r1   )r   r4   r(   r)   r.     s    zCTestFieldBlock.test_prepare_value_called.<locals>.PrefixField.cleanc                 S   s   |  S r<   )r   r1   r(   r(   r)   prepare_value  s    zKTestFieldBlock.test_prepare_value_called.<locals>.PrefixField.prepare_value)r5   r6   r7   r.   r   r:   r(   )r   r3   r)   PrefixField  s   r   c                       s    e Zd Zd fdd	Z  ZS )z?TestFieldBlock.test_prepare_value_called.<locals>.PrefixedBlockT c                    s    t  jf |  ||d| _d S )N)r_   rV   )r-   r   r   )r2   r_   rV   kwargs)r   r4   r(   r)   r     s    zHTestFieldBlock.test_prepare_value_called.<locals>.PrefixedBlock.__init__)Tr   )r5   r6   r7   r   r:   r(   )r   r3   r)   PrefixedBlock  s   r   r"   zhttp://example.com/foourlzhttp://example.com/barr   N)r   Fieldr   r   rt   rL   r.   value_from_datadict)r2   r   r'   r$   rv   data	new_valuer(   )r   r   r)   test_prepare_value_called   s    	
z(TestFieldBlock.test_prepare_value_calledN)r5   r6   r7   rO   rT   rh   rl   rr   rw   r|   r   r   r   r   r   r   r   r(   r(   r(   r)   rG   =   s   
#rG   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 )TestIntegerBlockc                 C   s&   t  }|d}| t|t d S Ni  )r   IntegerBlockvalue_from_formrL   typeintr2   r'   digitr(   r(   r)   	test_type  s    
zTestIntegerBlock.test_typec                 C   s"   t  }|d}| |d d S r   )r   r   r   rL   r   r(   r(   r)   test_render%  s    
zTestIntegerBlock.test_renderc              	   C   s,   t  }| t |d W 5 Q R X d S Nr   r   r   r   r   r.   r2   r'   r(   r(   r)   test_render_required_error+  s    z+TestIntegerBlock.test_render_required_errorc              	   C   s0   t jdd}| t |d W 5 Q R X d S )N   Z	max_value   r   r   r(   r(   r)    test_render_max_value_validation1  s    z1TestIntegerBlock.test_render_max_value_validationc              	   C   s0   t jdd}| t |d W 5 Q R X d S )Nr   Z	min_value
   r   r   r(   r(   r)    test_render_min_value_validation7  s    z1TestIntegerBlock.test_render_min_value_validationc              	   C   s:   dd }t j|gd}| t |d W 5 Q R X d S )Nc                 S   s   | d dkrt dd S NrY   r   zValue must be evenr   r#   r(   r(   r)   validate_is_even>  s    zETestIntegerBlock.test_render_with_validator.<locals>.validate_is_evenr      r   r2   r   r'   r(   r(   r)   test_render_with_validator=  s    z+TestIntegerBlock.test_render_with_validatorN)	r5   r6   r7   r   r   r   r   r   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	S )
TestEmailBlockc                 C   s"   t  }|d}| |d d S )Nzexample@email.com)r   
EmailBlockrK   rL   )r2   r'   emailr(   r(   r)   r   I  s    
zTestEmailBlock.test_renderc              	   C   s,   t  }| t |d W 5 Q R X d S r   r   r   r   r   r.   r   r(   r(   r)   r   O  s    z)TestEmailBlock.test_render_required_errorc              	   C   s,   t  }| t |d W 5 Q R X d S )Nzexample.email.comr   r   r(   r(   r)   test_format_validationU  s    z%TestEmailBlock.test_format_validationc              	   C   s:   dd }t j|gd}| t |d W 5 Q R X d S )Nc                 S   s   |  dstdd S )Nz@example.comz'E-mail address must end in @example.com)endswithr   r#   r(   r(   r)   validate_is_example_domain\  s    
zMTestEmailBlock.test_render_with_validator.<locals>.validate_is_example_domainr   zfoo@example.netr   )r2   r   r'   r(   r(   r)   r   [  s    z)TestEmailBlock.test_render_with_validatorN)r5   r6   r7   r   r   r   r   r(   r(   r(   r)   r   H  s   r   c                   @   s   e Zd Zdd ZdS )TestBooleanBlockc                 C   s<   t jdd}|d}| |d |d}| |d d S )NFr   T)r   BooleanBlockrt   rB   ru   r(   r(   r)   test_get_form_stateg  s
    

z$TestBooleanBlock.test_get_form_stateN)r5   r6   r7   r   r(   r(   r(   r)   r   f  s   r   c                   @   s   e Zd Zdd Zdd ZdS )TestBlockQuoteBlockc                 C   s"   t  }|d}| |d d S )NzNow is the time...z+<blockquote>Now is the time...</blockquote>)r   BlockQuoteBlockrK   rL   )r2   r'   quoter(   r(   r)   r   p  s    
zTestBlockQuoteBlock.test_renderc              	   C   s:   dd }t j|gd}| t |d W 5 Q R X d S )Nc                 S   s   |  dstdd S )NzOnce upon a timezValue must be a proper story)r   r   r#   r(   r(   r)   validate_is_proper_storyw  s    
zPTestBlockQuoteBlock.test_render_with_validator.<locals>.validate_is_proper_storyr   zA long, long time ago)r   r   r   r   r.   )r2   r   r'   r(   r(   r)   r   v  s    z.TestBlockQuoteBlock.test_render_with_validatorN)r5   r6   r7   r   r   r(   r(   r(   r)   r   o  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 )TestFloatBlockc                 C   s*   t  }|td}| t|t d S NGz?)r   
FloatBlockr   floatrL   r   r2   r'   	block_valr(   r(   r)   r     s    zTestFloatBlock.test_typec                 C   s*   t  }td}||}| || d S r   )r   r   r   r   rL   r2   r'   Ztest_valr   r(   r(   r)   r     s    
zTestFloatBlock.test_renderc              	   C   s,   t  }| t |d W 5 Q R X d S r   r   r   r   r   r.   r   r(   r(   r)   test_raises_required_error  s    z)TestFloatBlock.test_raises_required_errorc              	   C   s0   t jdd}| t |d W 5 Q R X d S Nr   r   z20.01r   r   r(   r(   r)   &test_raises_max_value_validation_error  s    z5TestFloatBlock.test_raises_max_value_validation_errorc              	   C   s0   t jdd}| t |d W 5 Q R X d S Nr   r   z19.99r   r   r(   r(   r)   &test_raises_min_value_validation_error  s    z5TestFloatBlock.test_raises_min_value_validation_errorc              	   C   s:   dd }t j|gd}| t |d W 5 Q R X d S )Nc                 S   s   | d dkrt dd S r   r   r#   r(   r(   r)   r     s    zCTestFloatBlock.test_render_with_validator.<locals>.validate_is_evenr   3.0r   r   r(   r(   r)   r     s    z)TestFloatBlock.test_render_with_validatorN)	r5   r6   r7   r   r   r   r   r   r   r(   r(   r(   r)   r     s   r   c                   @   sT   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S )TestDecimalBlockc                 C   s*   t  }|td}| t|t d S Nz1.63)r   DecimalBlockr   r   rL   r   r   r(   r(   r)   r     s    zTestDecimalBlock.test_typec                 C   s&   t  }|d}| t|t d S r   )r   r   r   rL   r   r   r   r(   r(   r)   test_type_to_python  s
    z$TestDecimalBlock.test_type_to_pythonc                 C   s    t  }|d }| | d S r<   )r   r   r   assertIsNoner   r(   r(   r)   &test_type_to_python_decimal_none_value  s    
z7TestDecimalBlock.test_type_to_python_decimal_none_valuec                 C   s*   t  }td}||}| || d S r   )r   r   r   r   rL   r   r(   r(   r)   r     s    
zTestDecimalBlock.test_renderc              	   C   s,   t  }| t |d W 5 Q R X d S r   r   r   r   r   r.   r   r(   r(   r)   r     s    z+TestDecimalBlock.test_raises_required_errorc              	   C   s0   t jdd}| t |d W 5 Q R X d S r   r   r   r(   r(   r)   r     s    z7TestDecimalBlock.test_raises_max_value_validation_errorc              	   C   s0   t jdd}| t |d W 5 Q R X d S r   r   r   r(   r(   r)   r     s    z7TestDecimalBlock.test_raises_min_value_validation_errorc              	   C   s:   dd }t j|gd}| t |d W 5 Q R X d S )Nc                 S   s   | d dkrt dd S r   r   r#   r(   r(   r)   r     s    zETestDecimalBlock.test_render_with_validator.<locals>.validate_is_evenr   r   r   r   r(   r(   r)   r     s    z+TestDecimalBlock.test_render_with_validatorc                 C   sT   t  }td}tj||td}|t|}| 	t
|t | 	|| d S )Nr   r   )r   r   r   jsondumpsr   r   r   loadsrL   r   )r2   r'   original_valueZdb_valueZrestored_valuer(   r(   r)   $test_round_trip_to_db_preserves_type  s     z5TestDecimalBlock.test_round_trip_to_db_preserves_typeN)r5   r6   r7   r   r   r   r   r   r   r   r   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 )TestRegexBlockc                 C   s*   t jdd}d}||}| || d S )N
^[0-9]{3}$regexZ123)r   
RegexBlockr   rL   r   r(   r(   r)   r     s    
zTestRegexBlock.test_renderc              	   C   s@   t jdd}| t}|d W 5 Q R X | d|jj d S )Nr  r  r   This field is required.r   r	  r   r   r.   rk   	exceptionmessagesr2   r'   rq   r(   r(   r)   r     s    z)TestRegexBlock.test_raises_required_errorc              	   C   sJ   d}t jdd|id}| t}|d W 5 Q R X | ||jj d S )NzOops, you missed a bit.r  r_   r  Zerror_messagesr   r  r2   Ztest_messager'   rq   r(   r(   r)   !test_raises_custom_required_error  s     z0TestRegexBlock.test_raises_custom_required_errorc              	   C   s@   t jdd}| t}|d W 5 Q R X | d|jj d S )Nr  r  [/]zEnter a valid value.r  r  r(   r(   r)   test_raises_validation_error  s    z+TestRegexBlock.test_raises_validation_errorc              	   C   sJ   d}t jdd|id}| t}|d W 5 Q R X | ||jj d S )Nz Not a valid library card number.r  invalidr  r  r  r  r(   r(   r)    test_raises_custom_error_message  s     z/TestRegexBlock.test_raises_custom_error_messagec              	   C   s<   dd }t jd|gd}| t |d W 5 Q R X d S )Nc                 S   s   | dkrt dd S r   r   r#   r(   r(   r)   r     s    zBTestRegexBlock.test_render_with_validator.<locals>.validate_is_fooz^.*$)r  r   r   )r   r	  r   r   r.   r   r(   r(   r)   r     s    z)TestRegexBlock.test_render_with_validatorN)	r5   r6   r7   r   r   r  r  r  r   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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 )'TestRichTextBlock	test.jsonc                 C   s*   t   }| |t | |jd d S r   r   RichTextBlockget_defaultrf   r   rL   sourcer2   default_valuer(   r(   r)   $test_get_default_with_fallback_value)  s    z6TestRichTextBlock.test_get_default_with_fallback_valuec                 C   s.   t jd d }| |t | |jd d S Ndefaultr   r  r  r(   r(   r)   "test_get_default_with_default_none.  s    z4TestRichTextBlock.test_get_default_with_default_nonec                 C   s.   t jdd }| |t | |jd d S Nr   r   r  r  r(   r(   r)   "test_get_default_with_empty_string3  s    z4TestRichTextBlock.test_get_default_with_empty_stringc                 C   s.   t jdd }| |t | |jd d S Nz
<p>foo</p>r   r  r  r(   r(   r)   %test_get_default_with_nonempty_string8  s    z7TestRichTextBlock.test_get_default_with_nonempty_stringc                 C   s2   t jtdd }| |t | |jd d S r%  )r   r  r   r  rf   rL   r  r  r(   r(   r)   $test_get_default_with_richtext_value=  s
    
z6TestRichTextBlock.test_get_default_with_richtext_valuec                 C   s*   t  }td}||}| |d d S )Nz5<p>Merry <a linktype="page" id="4">Christmas</a>!</p>z8<p>Merry <a href="/events/christmas/">Christmas</a>!</p>)r   r  r   rK   rL   r2   r'   r$   resultr(   r(   r)   r   D  s    
 zTestRichTextBlock.test_renderc              
   C   st   ddl m} tjdd}|d t |}| |d d | |d | | |d dd	d
ddddid d S )Nr   )CustomRichTextAreaZcustom)Zeditortest_richtextblockrX   rY   z:w-field w-field--char_field w-field--custom_rich_text_areapilcrowTest richtextblockTr[   r\   )ra   r`   r]   r_   rb   rc   )	Zwagtail.test.testapp.rich_textr*  r   r  rd   r   re   rL   rf   )r2   r*  r'   re   r(   r(   r)   test_adapterL  s     
zTestRichTextBlock.test_adapterc              
   C   sp   ddl m} t }|d t |}| |d d | |d | | |d dddd	d
ddid d S )Nr   DraftailRichTextArear+  rX   rY   r-  Tr,  <w-field w-field--char_field w-field--draftail_rich_text_areaFr[   r\   r   	Zwagtail.admin.rich_textr0  r   r  rd   r   re   rL   rf   r2   r0  r'   re   r(   r(   r)   test_adapter_with_draftailb  s     
z,TestRichTextBlock.test_adapter_with_draftailc                 C   sv   ddl m} tjdd}|d t |}| |d d | |d | | |d dd	d
ddddidd d S )Nr   r/  i  
max_lengthr+  rX   rY   r-  Tr,  r1  Fr[   r\   )r]   r_   r`   ra   rb   rc   Z	maxLengthr2  r3  r(   r(   r)   test_adapter_with_max_lengthx  s"    
z.TestRichTextBlock.test_adapter_with_max_lengthc              	   C   s0   t  }| t |td W 5 Q R X d S r   r   r  r   r   r.   r   r   r(   r(   r)   %test_validate_required_richtext_block  s    z7TestRichTextBlock.test_validate_required_richtext_blockc                 C   s8   t jdd}|td}| |t | |jd d S )NFr   r   )r   r  r.   r   rf   rL   r  r2   r'   r)  r(   r(   r)   )test_validate_non_required_richtext_block  s    z;TestRichTextBlock.test_validate_non_required_richtext_blockc              	   C   s>   dd }t j|gd}| t |td W 5 Q R X d S )Nc                 S   s   d| krt dd S Nr"   zValue must contain 'foo'r   r#   r(   r(   r)   validate_contains_foo  s    zKTestRichTextBlock.test_render_with_validator.<locals>.validate_contains_foor   
<p>bar</p>r8  r2   r=  r'   r(   r(   r)   r     s    z,TestRichTextBlock.test_render_with_validatorc              	   C   sP   t jdd}|td | t |td W 5 Q R X |td d S )Nr   r5  z<p>short</p>z*<p>this exceeds the 20 character limit</p>zJ<p><a href="http://really-long-domain-name.example.com">also</a> short</p>)r   r  r.   r   r   r   r   r(   r(   r)   test_validate_max_length  s    z*TestRichTextBlock.test_validate_max_lengthc                 C   s,   t  }td}||}| |dg d S )N<p>Merry <a linktype="page" id="4">Christmas</a>! &amp; a happy new year</p>
<p>Our Santa pet <b>Wagtail</b> has some cool stuff in store for you all!</p>zdMerry Christmas! & a happy new year 
Our Santa pet Wagtail has some cool stuff in store for you all!r   r  r   ry   rL   r(  r(   r(   r)   test_get_searchable_content  s    
z-TestRichTextBlock.test_get_searchable_contentc                 C   s.   t jdd}td}||}| |g  d S )NFr}   rA  rB  r(  r(   r(   r)   (test_search_index_get_searchable_content  s    
z:TestRichTextBlock.test_search_index_get_searchable_contentc                 C   s,   t  }td}||}| |dg d S )Nz#<p>mashed</p><p>po<i>ta</i>toes</p>zmashed potatoesrB  r(  r(   r(   r)   &test_get_searchable_content_whitespace  s    
z8TestRichTextBlock.test_get_searchable_content_whitespacec                 C   s4   t  }td}| t||tdddfg d S )Nz6<a linktype="page" id="1">Link to an internal page</a>1r   )r   r  r   rL   listextract_referencesr   r2   r'   r$   r(   r(   r)   test_extract_references  s    z)TestRichTextBlock.test_extract_referencesc              
   C   sZ   t  }dtdfD ]@}| j|d* ||}| |t | |jd W 5 Q R X qd S )NHello, worldr#   )r   r  r   subTestrD   rf   rL   r  r2   r'   r$   
normalizedr(   r(   r)   rF     s    
z TestRichTextBlock.test_normalizeN)r5   r6   r7   fixturesr  r"  r$  r&  r'  r   r.  r4  r7  r9  r;  r   r@  rC  rD  rE  rJ  rF   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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 )0TestChoiceBlockc                 C   s   ddl m} |d d | _d S Nr   )BLANK_CHOICE_DASHrX   Zdjango.db.models.fieldsrR  Zblank_choice_dash_labelr2   rR  r(   r(   r)   setUp  s    zTestChoiceBlock.setUpc              
   C   s   t jddgd}|d t |}| |d d | |d tj | t	|d j
dddg | |d d	d
ddd
ddid d S )NteaTeacoffeeZCoffeer   r   r   rX   r   z	---------rY   r   TrZ   r   r[   r\   r   )r   r   rd   r   re   rL   rf   r   r   rG  r   rg   r(   r(   r)   test_adapt_choice_block  s&    
z'TestChoiceBlock.test_adapt_choice_blockc                 C   s&   t jddgdd}| | d d S NrV  rY  rW  )r   r!  )r   r   rL   r  r   r(   r(   r)   test_choice_block_with_default  s
     z.TestChoiceBlock.test_choice_block_with_defaultc                 C   s\   dd }t j|d}|d t |}| |d tj | t	|d j
dddg d S )	Nc                   S   s   ddgS NrV  rY  r(   r(   r(   r(   r)   callable_choices  s    zWTestChoiceBlock.test_adapt_choice_block_with_callable_choices.<locals>.callable_choicesr   r   rX   r[  rV  rY  )r   r   rd   r   re   rf   r   r   rL   rG  r   r2   r`  r'   re   r(   r(   r)   -test_adapt_choice_block_with_callable_choices  s    
z=TestChoiceBlock.test_adapt_choice_block_with_callable_choicesc              	   C   s   t jddgd}| |dd | t |d W 5 Q R X | t |d W 5 Q R X | t |d  W 5 Q R X d S NrV  rY  r   rZ  whiskyr   r   r   rL   r.   r   r   r   r(   r(   r)   #test_validate_required_choice_block  s    z3TestChoiceBlock.test_validate_required_choice_blockc                 C   s>   t jddgdd}|d t |}| |d d  d S NrV  rY  Fr   r_   r   rY   r_   )r   r   rd   r   re   assertFalserg   r(   r(   r)   $test_adapt_non_required_choice_block#  s     
z4TestChoiceBlock.test_adapt_non_required_choice_blockc              	   C   sl   t jddgdd}| |dd | t |d W 5 Q R X | |dd | |d d d S NrV  rY  Frh  rZ  rd  r   re  r   r(   r(   r)   'test_validate_non_required_choice_block-  s     z7TestChoiceBlock.test_validate_non_required_choice_blockc                 C   sJ   t jdddgdd}|d t |}| t|d jdddg d S NrV  rY  r   z	No thanksFrh  r   rX   r   r   rd   r   re   rL   rG  r   rg   r(   r(   r)   2test_adapt_choice_block_with_existing_blank_choice9  s    
zBTestChoiceBlock.test_adapt_choice_block_with_existing_blank_choicec                 C   sL   dd }t j|dd}|d t |}| t|d jddd	g d S )
Nc                   S   s
   dddgS NrV  rY  rn  r(   r(   r(   r(   r)   r`  J  s    zvTestChoiceBlock.test_adapt_choice_block_with_existing_blank_choice_and_with_callable_choices.<locals>.callable_choicesFrh  r   rX   rV  rY  rn  ro  ra  r(   r(   r)   Ltest_adapt_choice_block_with_existing_blank_choice_and_with_callable_choicesG  s    
z\TestChoiceBlock.test_adapt_choice_block_with_existing_blank_choice_and_with_callable_choicesc                 C   sf   t jdddgfdddgfgd}|d t |}| t|d	 jd
dddgfdddgfg d S )N	AlcoholicZginZGinrd  ZWhiskyNon-alcoholicrV  rY  r   r   rX   r[  ro  rg   r(   r(   r)   &test_named_groups_without_blank_optionW  s<    
z6TestChoiceBlock.test_named_groups_without_blank_optionc                 C   sv   t jdddgfdddgfddgfgd	d
}|d t |}| t|d jdddgfdddgfddgfg d S Nrs  rt  ru  rv  rV  rY  zNot thirstyrn  Frh  r   rX   ro  rg   r(   r(   r)   #test_named_groups_with_blank_option  s@    
z3TestChoiceBlock.test_named_groups_with_blank_optionc                 C   sr   G dd dt j}|dd}|d t |}| t|d jddd	g | | d
g dd	gddf d S )Nc                   @   s   e Zd ZddgZdS )z=TestChoiceBlock.test_subclassing.<locals>.BeverageChoiceBlockrV  rY  Nr5   r6   r7   r   r(   r(   r(   r)   BeverageChoiceBlock  s   r{  Fr   r   rX   r[  rV  rY  wagtail.blocks.ChoiceBlockrh  	r   r   rd   r   re   rL   rG  r   deconstruct)r2   r{  r'   re   r(   r(   r)   test_subclassing  s(    


z TestChoiceBlock.test_subclassingc                 C   s(   t jddgd}| |ddg d S Nr   r   r   r   r   r   r   rL   ry   r   r(   r(   r)   r     s    z'TestChoiceBlock.test_searchable_contentc                 C   s(   t jddgdd}| |dg  d S Nr   r   F)r   r~   r   r  r   r(   r(   r)   r     s    z4TestChoiceBlock.test_search_index_searchable_contentc                 C   s,   dd }t j|d}| |ddg d S )Nc                   S   s   ddgS Nr   r   r(   r(   r(   r(   r)   r`    s    zWTestChoiceBlock.test_searchable_content_with_callable_choices.<locals>.callable_choicesr   r   r   r  r2   r`  r'   r(   r(   r)   -test_searchable_content_with_callable_choices  s    z=TestChoiceBlock.test_searchable_content_with_callable_choicesc                 C   s:   t jdddgfdddgfgd}| |ddd	g d S 
N	Section 1)1-1Block 1)1-2Block 2	Section 2)2-1r  )2-2r  r   r  r  r  r   r(   r(   r)    test_optgroup_searchable_content  s    z0TestChoiceBlock.test_optgroup_searchable_contentc                 C   s&   t jddgd}| |dg  d S N)oneZOne)twoZTwor   threer  r   r(   r(   r)   test_invalid_searchable_content  s    z/TestChoiceBlock.test_invalid_searchable_contentc                 C   sL   t jdtdfdtdfgd}|d}tt|}| |dg d S Nr   r   r   r   r   r   r   _ry   r   r  r  rL   r:  r(   r(   r)   -test_searchable_content_with_lazy_translation  s    


z=TestChoiceBlock.test_searchable_content_with_lazy_translationc                 C   sv   t jtddtdfdtdfgftddtdfdtdfgfgd	}|d}tt|}| |ddg d S 
Nr  r  r  r  r  r  r  r  r   r  r:  r(   r(   r)   6test_optgroup_searchable_content_with_lazy_translation  s     




zFTestChoiceBlock.test_optgroup_searchable_content_with_lazy_translationc                 C   sh   dd }t j|dd}|d t |}| t|d jddd	g | | d
g |ddf d S )Nc                   S   s   ddgS r_  r(   r(   r(   r(   r)   r`  6  s    zPTestChoiceBlock.test_deconstruct_with_callable_choices.<locals>.callable_choicesFrh  r   rX   r[  rV  rY  r|  r}  ra  r(   r(   r)   &test_deconstruct_with_callable_choices5  s(    
	z6TestChoiceBlock.test_deconstruct_with_callable_choicesc              	   C   sD   ddg}dd }t j||gd}| t |d W 5 Q R X d S )NrV  rY  c                 S   s   t dd S NzYou must select 'tea'r   r#   r(   r(   r)   validate_tea_is_selected\  s    zLTestChoiceBlock.test_render_with_validator.<locals>.validate_tea_is_selectedr   r   rZ  )r   r   r   r   r.   r2   r   r  r'   r(   r(   r)   r   V  s     z*TestChoiceBlock.test_render_with_validatorc                 C   s,   t jddgd}|d}| |dg d S )NrV  rY  r   rW  )r   r   rt   rL   ru   r(   r(   r)   r   f  s    
z#TestChoiceBlock.test_get_form_statec                 C   s0   t jddgtjd}|d}| |dg d S )NrV  rY  r   ZwidgetrW  )r   r   r   ZRadioSelectrt   rL   ru   r(   r(   r)   %test_get_form_state_with_radio_widgetk  s     
z5TestChoiceBlock.test_get_form_state_with_radio_widgetN)r5   r6   r7   rU  r\  r^  rb  rf  rj  rl  rp  rr  rw  ry  r  r   r   r  r  r  r  r  r  r   r   r  r(   r(   r(   r)   rP    s.   
,/"	

	!rP  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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 )0TestMultipleChoiceBlockc                 C   s   ddl m} |d d | _d S rQ  rS  rT  r(   r(   r)   rU  t  s    zTestMultipleChoiceBlock.setUpc              
   C   s   t jddgd}|d t |}| |d d | |d tj | t	|d j
ddg | |d dd	d
dd	ddid d S )NrV  rY  r   r   r   rX   rY   r   TrZ   z?w-field w-field--multiple_choice_field w-field--select_multipler[   r\   r   )r   MultipleChoiceBlockrd   r   re   rL   rf   r   r   rG  r   rg   r(   r(   r)    test_adapt_multiple_choice_blocky  s*    
 z8TestMultipleChoiceBlock.test_adapt_multiple_choice_blockc                 C   s&   t jddgdd}| | d d S r]  )r   r  rL   r  r   r(   r(   r)   'test_multiple_choice_block_with_default  s
     z?TestMultipleChoiceBlock.test_multiple_choice_block_with_defaultc                 C   sZ   dd }t j|d}|d t |}| |d tj | t	|d j
ddg d S )Nc                   S   s   ddgS r_  r(   r(   r(   r(   r)   r`    s    zhTestMultipleChoiceBlock.test_adapt_multiple_choice_block_with_callable_choices.<locals>.callable_choicesr   r   rX   rV  rY  )r   r  rd   r   re   rf   r   r   rL   rG  r   ra  r(   r(   r)   6test_adapt_multiple_choice_block_with_callable_choices  s    
 zNTestMultipleChoiceBlock.test_adapt_multiple_choice_block_with_callable_choicesc              	   C   s   t jddgd}| |dgdg | t |dg W 5 Q R X | t |d W 5 Q R X | t |d  W 5 Q R X d S rc  r   r  rL   r.   r   r   r   r(   r(   r)   ,test_validate_required_multiple_choice_block  s    zDTestMultipleChoiceBlock.test_validate_required_multiple_choice_blockc                 C   s>   t jddgdd}|d t |}| |d d  d S rg  )r   r  rd   r   re   ri  rg   r(   r(   r)   -test_adapt_non_required_multiple_choice_block  s     
zETestMultipleChoiceBlock.test_adapt_non_required_multiple_choice_blockc              	   C   sr   t jddgdd}| |dgdg | t |dg W 5 Q R X | |dg  | |d g  d S rk  r  r   r(   r(   r)   0test_validate_non_required_multiple_choice_block  s     zHTestMultipleChoiceBlock.test_validate_non_required_multiple_choice_blockc                 C   sJ   t jdddgdd}|d t |}| t|d jdddg d S rm  r   r  rd   r   re   rL   rG  r   rg   r(   r(   r)   ;test_adapt_multiple_choice_block_with_existing_blank_choice  s    
zSTestMultipleChoiceBlock.test_adapt_multiple_choice_block_with_existing_blank_choicec                 C   sL   dd }t j|dd}|d t |}| t|d jddd	g d S )
Nc                   S   s
   dddgS rq  r(   r(   r(   r(   r)   r`    s    zTestMultipleChoiceBlock.test_adapt_multiple_choice_block_with_existing_blank_choice_and_with_callable_choices.<locals>.callable_choicesFrh  r   rX   rV  rY  rn  r  ra  r(   r(   r)   Utest_adapt_multiple_choice_block_with_existing_blank_choice_and_with_callable_choices  s    
zmTestMultipleChoiceBlock.test_adapt_multiple_choice_block_with_existing_blank_choice_and_with_callable_choicesc                 C   sd   t jdddgfdddgfgd}|d t |}| t|d	 jdddgfdddgfg d S )
Nrs  rt  ru  rv  rV  rY  r   r   rX   r  rg   r(   r(   r)   rw    s:    
z>TestMultipleChoiceBlock.test_named_groups_without_blank_optionc                 C   sv   t jdddgfdddgfddgfgd	d
}|d t |}| t|d jdddgfdddgfddgfg d S rx  r  rg   r(   r(   r)   ry    s@    
z;TestMultipleChoiceBlock.test_named_groups_with_blank_optionc                 C   sp   G dd dt j}|dd}|d t |}| t|d jddg | | d	g ddgdd
f d S )Nc                   @   s   e Zd ZddgZdS )zMTestMultipleChoiceBlock.test_subclassing.<locals>.BeverageMultipleChoiceBlockrV  rY  Nrz  r(   r(   r(   r)   BeverageMultipleChoiceBlockC  s   r  Fr   r   rX   rV  rY  "wagtail.blocks.MultipleChoiceBlockrh  	r   r  rd   r   re   rL   rG  r   r~  )r2   r  r'   re   r(   r(   r)   r  B  s&    

	z(TestMultipleChoiceBlock.test_subclassingc                 C   s(   t jddgd}| |ddg d S r  r   r  rL   ry   r   r(   r(   r)   r   c  s    z/TestMultipleChoiceBlock.test_searchable_contentc                 C   s(   t jddgdd}| |dg  d S r  r  r   r(   r(   r)   r   l  s    z<TestMultipleChoiceBlock.test_search_index_searchable_contentc                 C   s,   dd }t j|d}| |ddg d S )Nc                   S   s   ddgS r  r(   r(   r(   r(   r)   r`  w  s    z_TestMultipleChoiceBlock.test_searchable_content_with_callable_choices.<locals>.callable_choicesr   r   r   r  r  r(   r(   r)   r  v  s    zETestMultipleChoiceBlock.test_searchable_content_with_callable_choicesc                 C   s:   t jdddgfdddgfgd}| |ddd	g d S r  r  r   r(   r(   r)   r    s    z8TestMultipleChoiceBlock.test_optgroup_searchable_contentc                 C   s&   t jddgd}| |dg  d S r  r  r   r(   r(   r)   r    s    z7TestMultipleChoiceBlock.test_invalid_searchable_contentc                 C   sL   t jdtdfdtdfgd}|d}tt|}| |dg d S r  r   r  r  ry   r   r  r  rL   r:  r(   r(   r)   r    s    


zETestMultipleChoiceBlock.test_searchable_content_with_lazy_translationc                 C   sv   t jtddtdfdtdfgftddtdfdtdfgfgd	}|d}tt|}| |ddg d S r  r  r:  r(   r(   r)   r    s     




zNTestMultipleChoiceBlock.test_optgroup_searchable_content_with_lazy_translationc                 C   sf   dd }t j|dd}|d t |}| t|d jddg | | d	g |ddf d S )
Nc                   S   s   ddgS r_  r(   r(   r(   r(   r)   r`    s    zXTestMultipleChoiceBlock.test_deconstruct_with_callable_choices.<locals>.callable_choicesFrh  r   rX   rV  rY  r  r  ra  r(   r(   r)   r    s&    
z>TestMultipleChoiceBlock.test_deconstruct_with_callable_choicesc              	   C   sD   ddg}dd }t j||gd}| t |d W 5 Q R X d S )NrV  rY  c                 S   s   t dd S r  r   r#   r(   r(   r)   r    s    zTTestMultipleChoiceBlock.test_render_with_validator.<locals>.validate_tea_is_selectedr  rZ  )r   r  r   r   r.   r  r(   r(   r)   r     s     z2TestMultipleChoiceBlock.test_render_with_validatorc                 C   s2   t jddgd}|ddg}| |ddg d S )NrV  rY  r   rW  rZ  )r   r  rt   rL   ru   r(   r(   r)   r     s
    z+TestMultipleChoiceBlock.test_get_form_statec                 C   s6   t jddgtjd}|ddg}| |ddg d S )NrV  rY  r  rW  rZ  )r   r   r   ZCheckboxSelectMultiplert   rL   ru   r(   r(   r)   (test_get_form_state_with_checkbox_widget  s    z@TestMultipleChoiceBlock.test_get_form_state_with_checkbox_widgetN)r5   r6   r7   rU  r  r  r  r  r  r  r  r  rw  ry  r  r   r   r  r  r  r  r  r  r   r   r  r(   r(   r(   r)   r  s  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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 )TestRawHTMLBlockc                 C   s(   t   }| |d | |t d S r   r   RawHTMLBlockr  rL   rf   r   r  r(   r(   r)   r    s    z5TestRawHTMLBlock.test_get_default_with_fallback_valuec                 C   s,   t jd d }| |d | |t d S r  r  r  r(   r(   r)   test_get_default_with_none
  s    z+TestRawHTMLBlock.test_get_default_with_nonec                 C   s,   t jdd }| |d | |t d S r#  r  r  r(   r(   r)   r$    s    z3TestRawHTMLBlock.test_get_default_with_empty_stringc                 C   s,   t jdd }| |d | |t d S )N   <blink>BÖÖM</blink>r   r  r  r(   r(   r)   r&    s    z6TestRawHTMLBlock.test_get_default_with_nonempty_stringc                 C   s2   t  }|td}| |d | |t d S Nr  )r   r  r   r   rL   ZassertNotIsInstancer   r:  r(   r(   r)   test_serialize  s    zTestRawHTMLBlock.test_serializec                 C   s.   t  }|d}| |d | |t d S r  )r   r  r   rL   rf   r   r:  r(   r(   r)   test_deserialize  s    
z!TestRawHTMLBlock.test_deserializec                 C   s2   t  }|td}| |d | |t d S r  )r   r  rK   r   rL   rf   r   r:  r(   r(   r)   r   %  s    zTestRawHTMLBlock.test_renderc                 C   s"   t  }|d}| |d d S r  )r   r  rt   rL   ru   r(   r(   r)   r   +  s    
z$TestRawHTMLBlock.test_get_form_statec              
   C   s~   t  }|d t |}| |d d | |d tj | |d j	ddd | |d dd	d
dd	ddid d S )NZtest_rawhtmlblockr   rX   Z4010)colsrowsrY   zTest rawhtmlblockTcodez-w-field w-field--char_field w-field--textarear[   r\   r   )
r   r  rd   r   re   rL   rf   r   ZTextareaattrsrg   r(   r(   r)   
test_adapt1  s     
zTestRawHTMLBlock.test_adaptc                 C   s8   t  }|jddii dd}| |d | |t d S )Nrawhtmlr  r   )r   r  r   rL   rf   r   r:  r(   r(   r)   test_form_responseF  s      z#TestRawHTMLBlock.test_form_responsec                 C   sT   t  }| |ddii d | |ddii d | |ddii d d S )Nr  Zohair   nothing-herenope)r   r  ri  value_omitted_from_data
assertTruer   r(   r(   r)   test_value_omitted_from_dataN  s    z-TestRawHTMLBlock.test_value_omitted_from_datac              	   C   sV   t  }|td}| |d | |t | t |td W 5 Q R X d S )Nr  r   )	r   r  r.   r   rL   rf   r   r   r   r:  r(   r(   r)   test_clean_required_fieldX  s    z*TestRawHTMLBlock.test_clean_required_fieldc                 C   s\   t jdd}|td}| |d | |t |td}| |d | |t d S )NFr   r  r   )r   r  r.   r   rL   rf   r   r:  r(   r(   r)   test_clean_nonrequired_fielda  s    z-TestRawHTMLBlock.test_clean_nonrequired_fieldc              	   C   s>   dd }t j|gd}| t |td W 5 Q R X d S )Nc                 S   s   d| krt dd S r<  r   r#   r(   r(   r)   r=  l  s    zJTestRawHTMLBlock.test_render_with_validator.<locals>.validate_contains_foor   r>  )r   r  r   r   r.   r   r?  r(   r(   r)   r   k  s    z+TestRawHTMLBlock.test_render_with_validatorN)r5   r6   r7   r  r  r$  r&  r  r  r   r   r  r  r  r  r  r   r(   r(   r(   r)   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 )TestMetac                 C   s,   G dd dt j}| }| |jjd d S )Nc                   @   s   e Zd ZG dd dZdS )z:TestMeta.test_set_template_with_meta.<locals>.HeadingBlockc                   @   s   e Zd ZdZdS )z?TestMeta.test_set_template_with_meta.<locals>.HeadingBlock.Metaheading.htmlNr5   r6   r7   rR   r(   r(   r(   r)   Metay  s   r  Nr5   r6   r7   r  r(   r(   r(   r)   HeadingBlockx  s   r  r  r   r8   rL   metarR   r2   r  r'   r(   r(   r)   test_set_template_with_metaw  s    z$TestMeta.test_set_template_with_metac                 C   s    t jdd}| |jjd d S )Nr  rQ   r  r   r(   r(   r)   "test_set_template_with_constructor  s    z+TestMeta.test_set_template_with_constructorc                 C   s0   G dd dt j}|dd}| |jjd d S )Nc                   @   s   e Zd ZG dd dZdS )zPTestMeta.test_set_template_with_constructor_overrides_meta.<locals>.HeadingBlockc                   @   s   e Zd ZdZdS )zUTestMeta.test_set_template_with_constructor_overrides_meta.<locals>.HeadingBlock.Metar  Nr  r(   r(   r(   r)   r    s   r  Nr  r(   r(   r(   r)   r    s   r  subheading.htmlrQ   r  r  r(   r(   r)   1test_set_template_with_constructor_overrides_meta  s    
z:TestMeta.test_set_template_with_constructor_overrides_metac                 C   sL   G dd dt j}G dd d|}| }| |jjd | |jjd dS )zI
        Check that having a multi-level inheritance chain works
        c                   @   s   e Zd ZG dd dZdS )z;TestMeta.test_meta_nested_inheritance.<locals>.HeadingBlockc                   @   s   e Zd ZdZdZdS )z@TestMeta.test_meta_nested_inheritance.<locals>.HeadingBlock.Metar  FooN)r5   r6   r7   rR   testr(   r(   r(   r)   r    s   r  Nr  r(   r(   r(   r)   r    s   r  c                   @   s   e Zd ZG dd dZdS )z>TestMeta.test_meta_nested_inheritance.<locals>.SubHeadingBlockc                   @   s   e Zd ZdZdS )zCTestMeta.test_meta_nested_inheritance.<locals>.SubHeadingBlock.Metar  Nr  r(   r(   r(   r)   r    s   r  Nr  r(   r(   r(   r)   SubHeadingBlock  s   r  r  r  N)r   r8   rL   r  rR   r  )r2   r  r  r'   r(   r(   r)   test_meta_nested_inheritance  s
    z%TestMeta.test_meta_nested_inheritancec                 C   s   G dd dt j}G dd dt j}G dd d||}| }| |jjd | |jjd | |jjd	 | |jjd
 dS )zM
        Check that multi-inheritance and Meta classes work together
        c                   @   s   e Zd ZG dd dZdS )z7TestMeta.test_meta_multi_inheritance.<locals>.LeftBlockc                   @   s   e Zd ZdZdZdZdS )z<TestMeta.test_meta_multi_inheritance.<locals>.LeftBlock.Metatemplate.htmlthe bandz
Left blockN)r5   r6   r7   rR   clashr]   r(   r(   r(   r)   r    s   r  Nr  r(   r(   r(   r)   	LeftBlock  s   r  c                   @   s   e Zd ZG dd dZdS )z8TestMeta.test_meta_multi_inheritance.<locals>.RightBlockc                   @   s   e Zd ZdZdZdZdS )z=TestMeta.test_meta_multi_inheritance.<locals>.RightBlock.Metahelloz	the albumzRight blockN)r5   r6   r7   r!  r  r]   r(   r(   r(   r)   r    s   r  Nr  r(   r(   r(   r)   
RightBlock  s   r  c                   @   s   e Zd ZG dd dZdS )z8TestMeta.test_meta_multi_inheritance.<locals>.ChildBlockc                   @   s   e Zd ZdZdS )z=TestMeta.test_meta_multi_inheritance.<locals>.ChildBlock.MetaChild blockNr5   r6   r7   r]   r(   r(   r(   r)   r    s   r  Nr  r(   r(   r(   r)   
ChildBlock  s   r  r  r  r  r  N)r   r8   rL   r  rR   r!  r  r]   )r2   r  r  r  r'   r(   r(   r)   test_meta_multi_inheritance  s    z$TestMeta.test_meta_multi_inheritanceN)r5   r6   r7   r  r  r  r  r  r(   r(   r(   r)   r  v  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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/d0 Zd1d2 Zd3d4 Zd5d6 Zd7d8 Zd9d: Zd;d< Z d=d> Z!d?d@ Z"dAS )BTestStructBlockc                 C   s<   t dt  fdt  fg}| t|j ddg d S )Ntitlelink)r   StructBlockr8   URLBlockrL   rG  child_blockskeysr   r(   r(   r)   test_initialisation  s    

z#TestStructBlock.test_initialisationc                 C   s6   G dd dt j}| }| t|j ddg d S )Nc                   @   s   e Zd Ze Ze ZdS )zDTestStructBlock.test_initialisation_from_subclass.<locals>.LinkBlockNr5   r6   r7   r   r8   r  r  r  r(   r(   r(   r)   r     s   r   r  r  r   r  rL   rG  r  r  r2   r   r'   r(   r(   r)   !test_initialisation_from_subclass  s    z1TestStructBlock.test_initialisation_from_subclassc                 C   sD   G dd dt j}|dt  fg}| t|j dddg d S )Nc                   @   s   e Zd Ze Ze ZdS )zOTestStructBlock.test_initialisation_from_subclass_with_extra.<locals>.LinkBlockNr  r(   r(   r(   r)   r     s   r   ra   r  r  )r   r  r8   rL   rG  r  r  r  r(   r(   r)   ,test_initialisation_from_subclass_with_extra  s     z<TestStructBlock.test_initialisation_from_subclass_with_extrac                 C   sH   G dd dt j}G dd d|}| }| t|j dddg d S )Nc                   @   s   e Zd Ze Ze ZdS )zPTestStructBlock.test_initialisation_with_multiple_subclassses.<locals>.LinkBlockNr  r(   r(   r(   r)   r     s   r   c                   @   s   e Zd Ze ZdS )zVTestStructBlock.test_initialisation_with_multiple_subclassses.<locals>.StyledLinkBlockNr5   r6   r7   r   r8   ra   r(   r(   r(   r)   StyledLinkBlock  s   r  r  r  ra   r  )r2   r   r  r'   r(   r(   r)   -test_initialisation_with_multiple_subclassses  s     z=TestStructBlock.test_initialisation_with_multiple_subclasssesc                 C   s^   G dd dt j}G dd dt j}G dd d||}| }| t|j ddd	d
g dS )a'  
        The order of fields of classes with multiple parent classes is slightly
        surprising at first. Child fields are inherited in a bottom-up order,
        by traversing the MRO in reverse. In the example below,
        ``StyledLinkBlock`` will have an MRO of::

            [StyledLinkBlock, StylingMixin, LinkBlock, StructBlock, ...]

        This will result in ``classname`` appearing *after* ``title`` and
        ``link`` in ``StyleLinkBlock`.child_blocks`, even though
        ``StylingMixin`` appeared before ``LinkBlock``.
        c                   @   s   e Zd Ze Ze ZdS )zBTestStructBlock.test_initialisation_with_mixins.<locals>.LinkBlockNr  r(   r(   r(   r)   r     s   r   c                   @   s   e Zd Ze ZdS )zETestStructBlock.test_initialisation_with_mixins.<locals>.StylingMixinNr  r(   r(   r(   r)   StylingMixin  s   r  c                   @   s   e Zd Ze ZdS )zHTestStructBlock.test_initialisation_with_mixins.<locals>.StyledLinkBlockNr5   r6   r7   r   r8   r  r(   r(   r(   r)   r    s   r  r  r  ra   r  Nr  )r2   r   r  r  r'   r(   r(   r)   test_initialisation_with_mixins  s     
z/TestStructBlock.test_initialisation_with_mixinsc                 C   sT   G dd dt j}| }||ddd}dddd	d
ddg}| || d S )Nc                   @   s   e Zd Ze Ze ZdS )z.TestStructBlock.test_render.<locals>.LinkBlockNr  r(   r(   r(   r)   r     s   r   Wagtail sitehttp://www.wagtail.orgr  r  
z<dl><dt>title</dt><dd>Wagtail site</dd><dt>link</dt><dd>http://www.wagtail.org</dd>z</dl>)r   r  rK   r   joinZassertHTMLEqual)r2   r   r'   rN   Zexpected_htmlr(   r(   r)   r     s&    zTestStructBlock.test_renderc                    s^   G dd dt j G  fdddt j}| }|jddddd	d
d}| |dd	d dS )z
        The get_api_representation method of a StructBlock should invoke
        the block's get_api_representation method on each field and the
        context should be passed on.
        c                   @   s   e Zd ZdddZdS )zjTestStructBlock.test_get_api_representation_calls_same_method_on_fields_with_context.<locals>.ContextBlockNc                 S   s   || S r<   r(   r2   r$   rq   r(   r(   r)   get_api_representation.  s    zTestStructBlock.test_get_api_representation_calls_same_method_on_fields_with_context.<locals>.ContextBlock.get_api_representation)Nr5   r6   r7   r  r(   r(   r(   r)   ContextBlock-  s   r  c                       s   e Zd Z  Z  ZdS )ziTestStructBlock.test_get_api_representation_calls_same_method_on_fields_with_context.<locals>.AuthorBlockN)r5   r6   r7   rn   authorr(   r  r(   r)   AuthorBlock1  s   r	  enwagtail)rn   r  EnglishWagtail!r
  r  rp   N)r   r8   r  r  ZassertDictEqual)r2   r	  r'   api_representationr(   r  r)   Dtest_get_api_representation_calls_same_method_on_fields_with_context&  s     zTTestStructBlock.test_get_api_representation_calls_same_method_on_fields_with_contextc                 C   sp   G dd dt j}| }||dddd}| d| | d| | d	| | d
| | d| d S )Nc                   @   s   e Zd Ze Ze ZdS )z<TestStructBlock.test_render_unknown_field.<locals>.LinkBlockNr  r(   r(   r(   r)   r   C  s   r   r  r  r   )r  r  imager  r  r   r  z<dt>image</dt>)r   r  rK   r   rk   assertNotIn)r2   r   r'   rN   r(   r(   r)   test_render_unknown_fieldB  s    
z)TestStructBlock.test_render_unknown_fieldc                 C   sJ   G dd dt j}| }|ddd}|jd }d}| t|| d S )Nc                   @   s   e Zd Ze Ze ZdS )z=TestStructBlock.test_render_bound_block.<locals>.SectionBlockN)r5   r6   r7   r   r8   r  r  bodyr(   r(   r(   r)   r   ]  s   r   r  <b>world</b>r  r  r  )r   r  r   bound_blocksrL   r=   )r2   r   r'   struct_valueZbody_bound_blockexpectedr(   r(   r)   test_render_bound_blockZ  s    
z'TestStructBlock.test_render_bound_blockc                 C   s   G dd dt j}| }|j|ddddd}| |d tj | t|d d	 | |d d
 t j	 | |d d
 j
d | |d d t j	 | |d d j
d | |d | | |d d d S )Nc                   @   s   e Zd Ze Ze ZdS )z8TestStructBlock.test_get_form_context.<locals>.LinkBlockNr  r(   r(   r(   r)   r   m  s   r   r  r  r  mylinkr  childrenrY   r  r  Zblock_definitionr   )r   r  Zget_form_contextr   rf   collectionsOrderedDictrL   r   Z
BoundBlockr$   )r2   r   r'   rq   r(   r(   r)   test_get_form_contextl  s$    
z%TestStructBlock.test_get_form_contextc                 C   s   G dd dt j}| }|d t |}| |d d | |d dddd	d
 | t|d d |d \}}| ||jd  | ||jd  d S )Nc                   @   s$   e Zd ZejddZejddZdS )z-TestStructBlock.test_adapt.<locals>.LinkBlockFr   Nr  r(   r(   r(   r)   r     s   r   test_structblockr   rY   Test structblockFrZ   struct-block)r]   r_   r`   ra   rX   r  r  )r   r  rd   r   re   rL   r   r  )r2   r   r'   re   Ztitle_fieldZ
link_fieldr(   r(   r)   r    s"    

zTestStructBlock.test_adaptc              	   C   sN   G dd dt j}| }|d t |}| |d ddddd	d
 d S )Nc                   @   s2   e Zd ZejddZejddZG dd dZdS )z@TestStructBlock.test_adapt_with_form_template.<locals>.LinkBlockFr   c                   @   s   e Zd ZdZdS )zETestStructBlock.test_adapt_with_form_template.<locals>.LinkBlock.Metaz1tests/block_forms/struct_block_form_template.htmlNr5   r6   r7   Zform_templater(   r(   r(   r)   r    s   r  N	r5   r6   r7   r   r8   r  r  r  r  r(   r(   r(   r)   r     s   r   r   rY   r!  FrZ   r"  <div>Hello</div>r]   r_   r`   ra   ZformTemplater   r  rd   r   re   rL   r2   r   r'   re   r(   r(   r)   test_adapt_with_form_template  s    
z-TestStructBlock.test_adapt_with_form_templatec              	   C   sN   G dd dt j}| }|d t |}| |d ddddd	d
 d S )Nc                   @   s2   e Zd ZejddZejddZG dd dZdS )zFTestStructBlock.test_adapt_with_form_template_jinja.<locals>.LinkBlockFr   c                   @   s   e Zd ZdZdS )zKTestStructBlock.test_adapt_with_form_template_jinja.<locals>.LinkBlock.Metaz,tests/jinja2/struct_block_form_template.htmlNr#  r(   r(   r(   r)   r    s   r  Nr$  r(   r(   r(   r)   r     s   r   r   rY   r!  FrZ   r"  r%  r&  r'  r(  r(   r(   r)   #test_adapt_with_form_template_jinja  s    
z3TestStructBlock.test_adapt_with_form_template_jinjac                 C   s6   G dd dt j}| }| }| |dd d S )Nc                   @   s$   e Zd ZejddZejddZdS )z3TestStructBlock.test_get_default.<locals>.LinkBlockTorchboxr   http://www.torchbox.comNr  r(   r(   r(   r)   r     s   r   r  r+  )r   r  r  rL   get)r2   r   r'   Zdefault_valr(   r(   r)   test_get_default  s    z TestStructBlock.test_get_defaultc              
   C   sP   G dd dt j}| }|d t |}| |d ddddd	d
d d S )Nc                   @   s*   e Zd Ze Ze ZG dd dZdS )zDTestStructBlock.test_adapt_with_help_text_on_meta.<locals>.LinkBlockc                   @   s   e Zd ZdZdS )zITestStructBlock.test_adapt_with_help_text_on_meta.<locals>.LinkBlock.MetaSelf-promotion is encouragedN)r5   r6   r7   rV   r(   r(   r(   r)   r    s   r  Nr$  r(   r(   r(   r)   r     s   r   r   rY   r!  FrZ   r"  Z<svg class="icon icon-help default" aria-hidden="true"><use href="#icon-help"></use></svg>r/  r]   r_   r`   ra   ZhelpIconr^   r'  r(  r(   r(   r)   !test_adapt_with_help_text_on_meta  s    
z1TestStructBlock.test_adapt_with_help_text_on_metac              
   C   sT   G dd dt j}|dd}|d t |}| |d ddd	d
ddd d S )Nc                   @   s   e Zd Ze Ze ZdS )zHTestStructBlock.test_adapt_with_help_text_as_argument.<locals>.LinkBlockNr  r(   r(   r(   r)   r     s   r   r/  rU   r   rY   r!  FrZ   r"  r0  r1  r'  r(  r(   r(   r)   %test_adapt_with_help_text_as_argument  s    

z5TestStructBlock.test_adapt_with_help_text_as_argumentc                 C   s@   G dd dt j}| }||ddd}| |dg d S )Nc                   @   s   e Zd Ze Ze ZdS )z:TestStructBlock.test_searchable_content.<locals>.LinkBlockNr  r(   r(   r(   r)   r     s   r   r  r  r  )r   r  ry   r   rL   r2   r   r'   r{   r(   r(   r)   r     s    	z'TestStructBlock.test_searchable_contentc                 C   sz   t dt  fdt  fg}|dddi d}| |d d | |d d | |t j | |jd j	t j d S )Nr  r  r+  r,  mylink-titlezmylink-linkr  )
r   r  r8   r  r   rL   rf   StructValuer  r'   )r2   r'   Z
struct_valr(   r(   r)   test_value_from_datadict  s    

z(TestStructBlock.test_value_from_datadictc                 C   sR   t dt  fdt  fg}| |ddii d | |ddii d d S )Nr  r  r6  r+  r  r  r  )r   r  r8   r  ri  r  r  r   r(   r(   r)   r  2  s    

z,TestStructBlock.test_value_omitted_from_datac                    sf   G dd dt j G  fdddt j}| }|ddi}| |d d d	 | |d t j d
S )zWhen returning the default value of a StructBlock (e.g. because it's
        a child of another StructBlock, and the outer value is missing that key)
        we should receive it as a StructValue, not just a plain dictc                   @   s   e Zd Ze Ze ZdS )zLTestStructBlock.test_default_is_returned_as_structvalue.<locals>.PersonBlockNr5   r6   r7   r   r8   
first_namesurnamer(   r(   r(   r)   PersonBlockG  s   r<  c                       s$   e Zd Ze Z ddddZdS )zKTestStructBlock.test_default_is_returned_as_structvalue.<locals>.EventBlockEdBallsr:  r;  r   Nr5   r6   r7   r   r8   r  guest_speakerr(   r<  r(   r)   
EventBlockK  s   rC  r  Birthday partyrA  r:  r=  N)r   r  r   rL   rf   r7  )r2   rC  event_blockeventr(   rB  r)   'test_default_is_returned_as_structvalueB  s    z7TestStructBlock.test_default_is_returned_as_structvaluec                    s   G dd dt j G  fdddt j}| }|ddi}|ddi}d|d	 d
< | |d	 d
 d | |d	 d
 d dS )z
        Whenever the default value of a StructBlock is invoked, it should be a distinct
        instance of the dict so that modifying it doesn't modify other places where the
        default value appears.
        c                   @   s   e Zd Ze Ze ZdS )zLTestStructBlock.test_default_value_is_distinct_instance.<locals>.PersonBlockNr9  r(   r(   r(   r)   r<  _  s   r<  c                       s$   e Zd Ze Z ddddZdS )zKTestStructBlock.test_default_value_is_distinct_instance.<locals>.EventBlockr=  r>  r?  r   Nr@  r(   rB  r(   r)   rC  c  s   rC  r  rD  zChristmas partyZMilibandrA  r;  r>  N)r   r  r   rL   )r2   rC  rE  Zevent1Zevent2r(   rB  r)   'test_default_value_is_distinct_instanceX  s    z7TestStructBlock.test_default_value_is_distinct_instancec                 C   sz   G dd dt j}| }|ddiddig}|d d d | t|d d d	dg | t|d
 d d	g dS )a  
        Whenever StructBlock.bulk_to_python invokes a child block's get_default method to
        fill in missing fields, it should use a separate invocation for each record so that
        we don't end up with the same instance of a mutable value on multiple records
        c                   @   s&   e Zd Ze ZeejddZdS )zaTestStructBlock.test_bulk_to_python_returns_distinct_default_instances.<locals>.ShoppingListBlock	chocolater   Nr5   r6   r7   r   r8   shop	ListBlockitemsr(   r(   r(   r)   ShoppingListBlock~  s   rN  rK  TescoAsdar   rM  cakerI  rX   N)r   r  bulk_to_pythonappendrL   rG  )r2   rN  r'   Zshopping_listsr(   r(   r)   6test_bulk_to_python_returns_distinct_default_instancesw  s     	zFTestStructBlock.test_bulk_to_python_returns_distinct_default_instancesc              	   C   s   t dt  fdt  fg}|ddd}||}| |t j | |d d |ddd}| 	t
 || W 5 Q R X d S )Nr  r  r+  http://www.torchbox.com/r  	not a url)r   r  r8   r  r   r.   rf   r7  rL   r   r   )r2   r'   r$   clean_valuer(   r(   r)   
test_clean  s    


zTestStructBlock.test_cleanc              	   C   sb   G dd dt j}| }d dd}| t || W 5 Q R X d dd}| ||| d S )Nc                       s4   e Zd ZejddZejddZ fddZ  Z	S )zBTestStructBlock.test_non_block_validation_error.<locals>.LinkBlockFr   c                    s0   t  |}|d s,|d s,ttdgd|S )Npager   z$Either page or URL must be specifiedr+   )r-   r.   r   r   )r2   r$   r)  r3   r(   r)   r.     s    zHTestStructBlock.test_non_block_validation_error.<locals>.LinkBlock.clean)
r5   r6   r7   r   PageChooserBlockrY  r  r   r.   r:   r(   r(   r3   r)   r     s   r   r   )rY  r   zhttps://wagtail.org/)r   r  r   r   r.   rL   )r2   r   r'   Zbad_dataZ	good_datar(   r(   r)   test_non_block_validation_error  s    

z/TestStructBlock.test_non_block_validation_errorc                 C   s0   t  }|ddd}||}| |d dS )z
        Test that we are able to use value.bound_blocks within templates
        to access a child block's own HTML rendering
        Hello<i>italic</i> worldr  !<h1>Hello</h1><i>italic</i> worldNr   r   rK   rL   r(  r(   r(   r)   +test_bound_blocks_are_available_on_template  s    
z;TestStructBlock.test_bound_blocks_are_available_on_templatec                 C   s8   t  }|ddd}|j|ddid}| |d d S NBonjourmonde <i>italique</i>r  rn   ro   rp   z/<h1 lang="fr">Bonjour</h1>monde <i>italique</i>r_  r(  r(   r(   r)   $test_render_block_with_extra_context  s    z4TestStructBlock.test_render_block_with_extra_contextc                 C   sB   t  }|ddd}| }| |d | }| |d dS )zZ
        The HTML representation of a StructValue should use the block's template
        r\  r]  r  r^  N)r   r   Z__html__rL   render_as_blockr(  r(   r(   r)   test_render_structvalue  s    z'TestStructBlock.test_render_structvaluec                 C   sR   t  }|ddd}t|}| d| | d| | d| | d| dS )a  
        The str() representation of a StructValue should NOT render the template, as that's liable
        to cause an infinite loop if any debugging / logging code attempts to log the fact that
        it rendered a template with this object in the context:
        https://github.com/wagtail/wagtail/issues/2874
        https://github.com/jazzband/django-debug-toolbar/issues/950
        r\  r]  r  z<h1>r7  r  N)r   r   r=   r  rk   r(  r(   r(   r)   test_str_structvalue  s    z$TestStructBlock.test_str_structvaluec                 C   s6   t  }|ddd}|jddid}| |d d S ra  )r   r   re  rL   r(  r(   r(   r)   *test_render_structvalue_with_extra_context  s    z:TestStructBlock.test_render_structvalue_with_extra_contextc                 C   sZ   t  }|ddd}t|}| || | |tj | |j|j | 	|| d S )Nr\  worldr  )
r   r   copyassertIsNotrf   r   r7  rB   r'   rL   )r2   r'   r$   copiedr(   r(   r)   test_copy_structvalue  s    
z%TestStructBlock.test_copy_structvaluec                 C   s^   t dt  fg}| |ddi|ddi | ||ddi|ddi dS )z9Test the trivially recursive and already normalized casesr  r  N)r   r  r8   rL   rD   Z_to_struct_valuer   r(   r(   r)   test_normalize_base_cases  s     z)TestStructBlock.test_normalize_base_casesc              	   C   s   t dt dt  fdt  fgfdt t  fg}ddgddd	d
gd}||}| |t j | |d t j	 | |d d t j	j
 | |d d t j	j
 | |d t jj | |d d t dS )z?StructBlock.normalize should recursively normalize all childreninner_stream
inner_char	inner_intlist_of_ints)rp  rK  )rq  *               )ro  rr  r   rX   N)r   r  StreamBlockr8   r   rL  rD   rf   r7  StreamValueZStreamChild
list_block	ListValuer   rM  r(   r(   r)   test_recursive_normalize
	  s6    

	


 
 z(TestStructBlock.test_recursive_normalizeN)#r5   r6   r7   r  r  r  r  r  r   r  r  r  r  r  r)  r*  r.  r2  r3  r   r8  r  rG  rH  rT  rX  r[  r`  rd  rf  rg  rh  rm  rn  r|  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 )$TestStructBlockWithCustomStructValuec              	   C   s  G dd dt j}t jdt  fdt  fg|d}| t|j ddg |	ddd}| 
|| | }| 
|| |d	d
di d}| 
|| |	d	dd}||}| 
|| | |d d	 |	d	dd}| t || W 5 Q R X d S )Nc                   @   s   e Zd Zdd ZdS )zSTestStructBlockWithCustomStructValue.test_initialisation.<locals>.CustomStructValuec                 S   s   |  dd|  dd S )Nr  r   r  r-  r   r(   r(   r)   joined2	  s    zZTestStructBlockWithCustomStructValue.test_initialisation.<locals>.CustomStructValue.joinedN)r5   r6   r7   r  r(   r(   r(   r)   CustomStructValue1	  s   r  r  r  )value_classrD  zhttps://myparty.co.ukr  r+  r,  r5  r  rU  rV  )r   r7  r  r8   r  rL   rG  r  r  r   rf   r  r   r.   r   r   )r2   r  r'   block_valuer  r   r$   rW  r(   r(   r)   r  0	  s:    


z8TestStructBlockWithCustomStructValue.test_initialisationc                    s~   G dd dt j G  fdddt j}| }| t|j dddg |dd	d
}| |  |	 }| |  d S )Nc                   @   s   e Zd Zdd ZdS )z_TestStructBlockWithCustomStructValue.test_initialisation_from_subclass.<locals>.LinkStructValuec                 S   s   |  dp|  dS NrY  r  r~  r   r(   r(   r)   r   \	  s    zcTestStructBlockWithCustomStructValue.test_initialisation_from_subclass.<locals>.LinkStructValue.urlNr5   r6   r7   r   r(   r(   r(   r)   LinkStructValue[	  s   r  c                       s>   e Zd Ze ZejddZejddZ	G  fdddZ
dS )zYTestStructBlockWithCustomStructValue.test_initialisation_from_subclass.<locals>.LinkBlockFr   c                       s   e Zd Z ZdS )z^TestStructBlockWithCustomStructValue.test_initialisation_from_subclass.<locals>.LinkBlock.MetaNr5   r6   r7   r  r(   r  r(   r)   r  d	  s   r  Nr5   r6   r7   r   r8   r  rZ  rY  r  r  r  r(   r  r(   r)   r   _	  s   r   r  rY  r  Websitehttps://website.comr  )
r   r7  r  rL   rG  r  r  r   rf   r  )r2   r   r'   r  r  r(   r  r)   r  Z	  s    zFTestStructBlockWithCustomStructValue.test_initialisation_from_subclassc                    s   G dd dt j G  fdddt j}G dd d|}| }| t|j ddd	d
g |ddddi d}| |  d S )Nc                   @   s   e Zd Zdd ZdS )zkTestStructBlockWithCustomStructValue.test_initialisation_with_multiple_subclassses.<locals>.LinkStructValuec                 S   s   |  dp|  dS r  r~  r   r(   r(   r)   r   u	  s    zoTestStructBlockWithCustomStructValue.test_initialisation_with_multiple_subclassses.<locals>.LinkStructValue.urlNr  r(   r(   r(   r)   r  t	  s   r  c                       s>   e Zd Ze ZejddZejddZ	G  fdddZ
dS )zeTestStructBlockWithCustomStructValue.test_initialisation_with_multiple_subclassses.<locals>.LinkBlockFr   c                       s   e Zd Z ZdS )zjTestStructBlockWithCustomStructValue.test_initialisation_with_multiple_subclassses.<locals>.LinkBlock.MetaNr  r(   r  r(   r)   r  }	  s   r  Nr  r(   r  r(   r)   r   x	  s   r   c                   @   s   e Zd Ze ZdS )zkTestStructBlockWithCustomStructValue.test_initialisation_with_multiple_subclassses.<locals>.StyledLinkBlockNr  r(   r(   r(   r)   r  	  s   r  r  rY  r  ra   r+  r,  Zfullsize)zqueen-titlez
queen-linkzqueen-classnameZqueen)	r   r7  r  rL   rG  r  r  r   rf   )r2   r   r  r'   r   r(   r  r)   r  s	  s"     

zRTestStructBlockWithCustomStructValue.test_initialisation_with_multiple_subclasssesc                    s   G dd dt j G  fdddt j}G dd dt j}G dd d||}| }| t|j d	d
ddg |ddddd}| |  d S )Nc                   @   s   e Zd ZdS )z]TestStructBlockWithCustomStructValue.test_initialisation_with_mixins.<locals>.LinkStructValueNr5   r6   r7   r(   r(   r(   r)   r  	  s   r  c                       s.   e Zd Ze Ze ZG  fdddZdS )zWTestStructBlockWithCustomStructValue.test_initialisation_with_mixins.<locals>.LinkBlockc                       s   e Zd Z ZdS )z\TestStructBlockWithCustomStructValue.test_initialisation_with_mixins.<locals>.LinkBlock.MetaNr  r(   r  r(   r)   r  	  s   r  Nr$  r(   r  r(   r)   r   	  s   r   c                   @   s   e Zd Ze ZdS )zZTestStructBlockWithCustomStructValue.test_initialisation_with_mixins.<locals>.StylingMixinNr  r(   r(   r(   r)   r  	  s   r  c                   @   s   e Zd Ze ZdS )z]TestStructBlockWithCustomStructValue.test_initialisation_with_mixins.<locals>.StyledLinkBlockNr  r(   r(   r(   r)   r  	  s   r  r  r  ra   r  r  r  Zgooglez	full-size)r  r  r  ra   )	r   r7  r  rL   rG  r  r  r   rf   )r2   r   r  r  r'   r  r(   r  r)   r  	  s"     
zDTestStructBlockWithCustomStructValue.test_initialisation_with_mixinsc                    sT   G dd dt j G  fdddt j}| }|ddd}|j}| |d d S )	Nc                   @   s   e Zd Zedd ZdS )zTTestStructBlockWithCustomStructValue.test_value_property.<locals>.SectionStructValuec                 S   s   d|  dd S )Nzbar %sr  r   r~  r   r(   r(   r)   r"   	  s    zXTestStructBlockWithCustomStructValue.test_value_property.<locals>.SectionStructValue.fooN)r5   r6   r7   propertyr"   r(   r(   r(   r)   SectionStructValue	  s   r  c                       s.   e Zd Ze Ze ZG  fdddZdS )zNTestStructBlockWithCustomStructValue.test_value_property.<locals>.SectionBlockc                       s   e Zd Z ZdS )zSTestStructBlockWithCustomStructValue.test_value_property.<locals>.SectionBlock.MetaNr  r(   r  r(   r)   r  	  s   r  N)	r5   r6   r7   r   r8   r  r  r  r  r(   r  r(   r)   r   	  s   r   r  r  r  z	bar hello)r   r7  r  r   r"   rL   )r2   r   r'   r  r$   r(   r  r)   test_value_property	  s    z8TestStructBlockWithCustomStructValue.test_value_propertyc                    sz   G dd dt j G  fdddt j}|dd}|ddi}||}| |d	 |i }||}| |d
 d S )Nc                   @   s   e Zd Zdd ZdS )zZTestStructBlockWithCustomStructValue.test_render_with_template.<locals>.SectionStructValuec                 S   s   |  d}|rd| S dS )Nr  z	SUFFIX %szEMPTY TITLEr~  )r2   r  r(   r(   r)   title_with_suffix	  s    
zlTestStructBlockWithCustomStructValue.test_render_with_template.<locals>.SectionStructValue.title_with_suffixN)r5   r6   r7   r  r(   r(   r(   r)   r  	  s   r  c                       s*   e Zd ZejddZG  fdddZdS )zTTestStructBlockWithCustomStructValue.test_render_with_template.<locals>.SectionBlockFr   c                       s   e Zd Z ZdS )zYTestStructBlockWithCustomStructValue.test_render_with_template.<locals>.SectionBlock.MetaNr  r(   r  r(   r)   r  	  s   r  N)r5   r6   r7   r   r8   r  r  r(   r  r(   r)   r   	  s   r   z+tests/blocks/struct_block_custom_value.htmlrQ   r  r  z<div>SUFFIX hello</div>
z<div>EMPTY TITLE</div>
)r   r7  r  r   rK   rL   )r2   r   r'   r  rN   r(   r  r)   test_render_with_template	  s    



z>TestStructBlockWithCustomStructValue.test_render_with_templatec                    sD   G dd dt j G  fdddt j}| | ddi  dS )zUA normalized StructBlock value should be an instance of the StructBlock's value_classc                   @   s   e Zd ZdS )zNTestStructBlockWithCustomStructValue.test_normalize.<locals>.CustomStructValueNr  r(   r(   r(   r)   r  	  s   r  c                       s&   e Zd Ze ZG  fdddZdS )zNTestStructBlockWithCustomStructValue.test_normalize.<locals>.CustomStructBlockc                       s   e Zd Z ZdS )zSTestStructBlockWithCustomStructValue.test_normalize.<locals>.CustomStructBlock.MetaNr  r(   r  r(   r)   r  	  s   r  Nr5   r6   r7   r   	TextBlockr9   r  r(   r  r(   r)   CustomStructBlock	  s   r  r9   zShe sells sea shellsNr   r7  r  rf   rD   )r2   r  r(   r  r)   rF   	  s    z3TestStructBlockWithCustomStructValue.test_normalizec                    sT   G dd dt j G  fdddt j}| }t |ddi}| ||  dS )z
        If StructBlock.normalize is passed a StructValue instance that doesn't
        match the StructBlock's `value_class', it should convert the value
        to the correct class.
        c                   @   s   e Zd ZdS )zdTestStructBlockWithCustomStructValue.test_normalize_incorrect_value_class.<locals>.CustomStructValueNr  r(   r(   r(   r)   r  	  s   r  c                       s&   e Zd Ze ZG  fdddZdS )zdTestStructBlockWithCustomStructValue.test_normalize_incorrect_value_class.<locals>.CustomStructBlockc                       s   e Zd Z ZdS )ziTestStructBlockWithCustomStructValue.test_normalize_incorrect_value_class.<locals>.CustomStructBlock.MetaNr  r(   r  r(   r)   r  	  s   r  Nr  r(   r  r(   r)   r  	  s   r  r9   zThe quick brown foxNr  )r2   r  r'   r$   r(   r  r)   $test_normalize_incorrect_value_class	  s
    zITestStructBlockWithCustomStructValue.test_normalize_incorrect_value_classN)r5   r6   r7   r  r  r  r  r  r  rF   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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/d0 Zd1d2 Zd3d4 Zd5d6 Zd7d8 Zd9d: Zd;d< Z d=d> Z!d?d@ Z"dAS )BTestListBlockc                 C   s   |  t|t| d S r<   )rL   rG  )r2   pqr(   r(   r)   assert_eq_list_values
  s    z#TestListBlock.assert_eq_list_valuesc                 C   s    t t j}| |jt j d S r<   )r   rL  r8   rf   child_blockr   r(   r(   r)   test_initialise_with_class
  s    z(TestListBlock.test_initialise_with_classc                 C   s$   t  }t |}| |j| d S r<   )r   r8   rL  rL   r  )r2   r  r'   r(   r(   r)   test_initialise_with_instance
  s    
z+TestListBlock.test_initialise_with_instancec                 C   s8   G dd dt j}t | }|ddddddgS )Nc                   @   s   e Zd Ze Ze ZdS )z'TestListBlock.render.<locals>.LinkBlockNr  r(   r(   r(   r)   r   
  s   r   Wagtailr  r  Djangohttp://www.djangoproject.com)r   r  rL  rK   r  r(   r(   r)   rK   
  s    zTestListBlock.renderc                 C   s$   |   }| d| | d| d S )Nz<ul>z</ul>rK   rk   r2   rN   r(   r(   r)   test_render_uses_ul*
  s    z!TestListBlock.test_render_uses_ulc                 C   s$   |   }| d| | d| d S )Nz<li>z</li>r  r  r(   r(   r)   test_render_uses_li0
  s    z!TestListBlock.test_render_uses_lic                 C   s<   t t jdd}|ddg}| d| | d| dS )z
        The default rendering of a ListBlock should invoke the block's render method
        on each child, rather than just outputting the child value as a string.
        rP   rQ   rI   zGoodbye world!rS   z<h1>Goodbye world!</h1>Nr   rL  r8   rK   rk   rM   r(   r(   r)   *test_render_calls_block_render_on_children6
  s    
z8TestListBlock.test_render_calls_block_render_on_childrenc                 C   sD   t t jdd}|jddgddid}| d| | d	| d
S )z
        Template context passed to the render method should be passed on
        to the render method of the child block.
        rP   rQ   rm   zAu revoir le monde!rn   ro   rp   z$<h1 lang="fr">Bonjour le monde!</h1>z&<h1 lang="fr">Au revoir le monde!</h1>Nr  rM   r(   r(   r)   &test_render_passes_context_to_childrenC
  s    
 z4TestListBlock.test_render_passes_context_to_childrenc                 C   sJ   G dd dt j}t | }|jddgdddd}| |ddg d	S )
z
        The get_api_representation method of a ListBlock should invoke
        the block's get_api_representation method on each child and
        the context should be passed on.
        c                   @   s   e Zd ZdddZdS )zjTestListBlock.test_get_api_representation_calls_same_method_on_children_with_context.<locals>.ContextBlockNc                 S   s   || S r<   r(   r  r(   r(   r)   r  ]
  s    zTestListBlock.test_get_api_representation_calls_same_method_on_children_with_context.<locals>.ContextBlock.get_api_representation)Nr  r(   r(   r(   r)   r  \
  s   r  r
  ro   rI   rm   )r
  ro   rp   N)r   r8   rL  r  rL   r2   r  r'   r  r(   r(   r)   Ftest_get_api_representation_calls_same_method_on_children_with_contextU
  s     zTTestListBlock.test_get_api_representation_calls_same_method_on_children_with_contextc                 C   s   G dd dt j}t |}|d t |}| |d d | |d | | |d d d d | |d d	d
d dddddddd d S )Nc                   @   s   e Zd Ze Ze ZdS )z+TestListBlock.test_adapt.<locals>.LinkBlockNr  r(   r(   r(   r)   r   h
  s   r   test_listblockr   rX   rY   r  r   Test listblockrZ   FDelete	Duplicate	Move downMove upAddDELETEZ	DUPLICATEZ	MOVE_DOWNZMOVE_UPZADDr]   r`   ra   	collapsedrc   r   r  rL  rd   r   re   rL   rf   r(  r(   r(   r)   r  g
  s*    

zTestListBlock.test_adaptc                 C   s   G dd dt j}t j|ddd}|d t |}| |d d | |d | | |d d d d	 | |d
 ddd dddddddddd d S )Nc                   @   s   e Zd Ze Ze ZdS )z@TestListBlock.test_adapt_with_min_num_max_num.<locals>.LinkBlockNr  r(   r(   r(   r)   r   
  s   r   rY   rt  )min_nummax_numr  r   rX   r  r   r  rZ   Fr  r  r  r  r  r  )r]   r`   ra   r  minNummaxNumrc   r  r(  r(   r(   r)   test_adapt_with_min_num_max_num
  s.    
z-TestListBlock.test_adapt_with_min_num_max_numc                 C   sL   G dd dt j}t | }|ddddddg}| |ddg d S )Nc                   @   s   e Zd Ze Ze ZdS )z8TestListBlock.test_searchable_content.<locals>.LinkBlockNr  r(   r(   r(   r)   r   
  s   r   r  r  r  r  r  )r   r  rL  ry   rL   r4  r(   r(   r)   r   
  s    z%TestListBlock.test_searchable_contentc              	   C   s`   t t  }| |ddii d | |dddddi d | |dd	ii d d S )
Nmylist-count0ZmylistrF  r  r   )r  zmylist-0-valuezmylist-0-deletedzmylist-0-orderr  r  )r   rL  r8   ri  r  r  r   r(   r(   r)   r  
  s$    z*TestListBlock.test_value_omitted_from_datac                 C   s   t t  }ddi}tddD ]:}|d| dd| t|d| d	| d
| d| i q ||i d}| |jd j	d | |jd j
d d S )Nshoppinglist-count3r   r   shoppinglist-%d-deletedr   shoppinglist-%d-ordershoppinglist-%d-valueitem %dshoppinglist-%d-id	0000000%dshoppinglistrX   zitem 1Z00000001)r   rL  r8   rangeupdater=   r   rL   r  r$   idr2   r'   	post_datair  r(   r(   r)   )test_id_from_form_submission_is_preserved
  s"        	z7TestListBlock.test_id_from_form_submission_is_preservedc                 C   s   t t  }ddi}tddD ]>}|d| dd| td| d	| d
| d| d| i q ||i d}| |d d d S )Nr  r  r   r   r  r   r  rY   r  r  r  r  r  zitem 0r   rL  r8   r  r  r=   r   rL   r  r(   r(   r)   1test_ordering_in_form_submission_uses_order_field
  s       
  	z?TestListBlock.test_ordering_in_form_submission_uses_order_fieldc                 C   s~   t t  }ddi}tddD ]:}|d| dd| t|d| d	| d
| d| i q ||i d}| |d d d S )Nr  12r      r  r   r  r  r  r  r  r  rY   zitem 2r  r  r(   r(   r)   +test_ordering_in_form_submission_is_numeric
  s         	z9TestListBlock.test_ordering_in_form_submission_is_numericc                 C   s6   t jt  dddgd}| t| dddg d S )NZpeasZbeansZcarrotsr   )r   rL  r8   rL   rG  r  r   r(   r(   r)   test_can_specify_default  s
     z&TestListBlock.test_can_specify_defaultc                 C   sR   t t jdd}| t| dg |d t |}| |d d dS )z
        if no explicit 'default' is set on the ListBlock, it should fall back on
        a single instance of the child block in its default state.
        rI  r   Ztest_shoppinglistblockrY   N)	r   rL  r8   rL   rG  r  rd   r   re   rg   r(   r(   r)   test_default_default  s
    
z"TestListBlock.test_default_defaultc                 C   st   G dd dt j}| }|ddi}|ddi}|d d | t|d ddg | t|d dg d	S )
z
        Whenever the default value of a ListBlock is invoked, it should be a distinct
        instance of the list so that modifying it doesn't modify other places where the
        default value appears.
        c                   @   s&   e Zd Ze ZeejddZdS )zPTestListBlock.test_default_value_is_distinct_instance.<locals>.ShoppingListBlockrI  r   NrJ  r(   r(   r(   r)   rN  "  s   rN  rK  rO  rP  rM  rQ  rI  N)r   r  r   rS  rL   rG  )r2   rN  r'   Ztesco_shoppingZasda_shoppingr(   r(   r)   rH    s    z5TestListBlock.test_default_value_is_distinct_instancec                 C   sb   G dd dt j}t j|dd}|d t |}| |d dddd	d
dddddd dS )zVform_classname from kwargs to be used as an additional class when rendering list blockc                   @   s   e Zd Ze Ze ZdS )zDTestListBlock.test_adapt_with_classname_via_kwarg.<locals>.LinkBlockNr  r(   r(   r(   r)   r   7  s   r   zspecial-list-classri   r  r   r  rZ   Fr  r  r  r  r  r  r  Nr   r  rL  rd   r   re   rL   r(  r(   r(   r)   #test_adapt_with_classname_via_kwarg4  s$    
z1TestListBlock.test_adapt_with_classname_via_kwargc                 C   sn   G dd dt j}G dd dt j}||}|d t |}| |d ddd	d
ddddddd dS )zTform_classname from meta to be used as an additional class when rendering list blockc                   @   s   e Zd Ze Ze ZdS )zITestListBlock.test_adapt_with_classname_via_class_meta.<locals>.LinkBlockNr  r(   r(   r(   r)   r   T  s   r   c                   @   s   e Zd ZG dd dZdS )zOTestListBlock.test_adapt_with_classname_via_class_meta.<locals>.CustomListBlockc                   @   s   e Zd ZdZdS )zTTestListBlock.test_adapt_with_classname_via_class_meta.<locals>.CustomListBlock.Metacustom-list-classNr5   r6   r7   rj   r(   r(   r(   r)   r  Y  s   r  Nr  r(   r(   r(   r)   CustomListBlockX  s   r  r  r   r  rZ   r  Fr  r  r  r  r  r  r  Nr  )r2   r   r  r'   re   r(   r(   r)   (test_adapt_with_classname_via_class_metaQ  s&    
z6TestListBlock.test_adapt_with_classname_via_class_metac                 C   sN   t t  }|ddddddddg}||}| |jd jd d S )Nitemr"   $11111111-1111-1111-1111-111111111111r   r$   r  r   $22222222-2222-2222-2222-222222222222r   )r   rL  r8   r   r.   rL   r  r  )r2   r'   r   Zcleaned_block_valr(   r(   r)   test_clean_preserves_block_idsr  s"    

 z,TestListBlock.test_clean_preserves_block_idsc              	   C   sx   t jt  dd}|dg}| t}|| W 5 Q R X | |j	 ddgi |ddg}| 
|| d S )NrY   r  r"   r   The minimum number of items is 2r   r   rL  r8   r   r   r   r.   rL   r  as_json_datar  r2   r'   r   catcherr(   r(   r)   test_min_num_validation_errors  s     z,TestListBlock.test_min_num_validation_errorsc              	   C   s|   t jt  dd}|dddg}| t}|| W 5 Q R X | |j	 ddgi |ddg}| 
|| d S )NrY   r  r"   r   bazr  z The maximum number of items is 2r  r  r(   r(   r)   test_max_num_validation_errors  s     z,TestListBlock.test_max_num_validation_errorsc                 C   sz   t t  }|ddg}| t|d | |d d | t|jd | |jd jd | |jd j	d d S )Nr"   r   rY   r   
[0-9a-f-]+)
r   rL  r8   r   rL   r   r  r$   assertRegexr  r2   r'   Zlist_valr(   r(   r)   test_unpack_old_database_format  s    z-TestListBlock.test_unpack_old_database_formatc                 C   s   t t  }|ddgdddgg\}}| t|d | t|d | |d d | |d d | t|jd | |jd jd | |jd j	d	 d S )
Nr"   r   ZxxxZyyyzzzrY   r   r   r  )
r   rL  r8   rR  rL   r   r  r$   r  r  r2   r'   Zlist_1Zlist_2r(   r(   r)   $test_bulk_unpack_old_database_format  s    z2TestListBlock.test_bulk_unpack_old_database_formatc                 C   s   t t  }|ddddddddg}| t|d | |d d | t|jd | |jd jd | |jd jd d S )	Nr  r"   r  r  r   r  rY   r   )	r   rL  r8   r   rL   r   r  r$   r  r  r(   r(   r)   test_unpack_new_database_format  s(    
 z-TestListBlock.test_unpack_new_database_formatc                 C   s   t t  }|ddddddddgddddgg\}}| t|d	 | t|d
 | |d d | |d d | t|jd	 | |jd jd | |jd jd d S )Nr  r"   r  r  r   r  r  z$33333333-3333-3333-3333-333333333333rY   rX   r   )	r   rL  r8   rR  rL   r   r  r$   r  r  r(   r(   r)   $test_bulk_unpack_new_database_format  s8    
 z2TestListBlock.test_bulk_unpack_new_database_formatc                 C   s   t dt t  fg}|g }|dddgf ||}||}| |d d d | t	|d d d | |d d d d d d S )NZbullet_listr"   r   r   r   r$   rY   )
r   rx  rL  r8   r   rS  r.   r   rL   r   )r2   stream_blockstream_valueZclean_stream_valuer)  r(   r(   r)   test_assign_listblock_with_list
  s    


z-TestListBlock.test_assign_listblock_with_listc              
   C   s   t t j}|dddddg}| |t jj | |dddddg |t j|dddddg}| |t jj | |dddddg dS )z?Test normalize when trivially recursive, or already a ListValuer   rX   rY   r   Nr   rL  r   rD   rf   rz  r{  r  r2   r'   rN  r(   r(   r)   test_normalize_base_case  s    z&TestListBlock.test_normalize_base_casec                 C   s8   t t  }|g }| |t jj | |g  d S r<   r  r  r(   r(   r)   test_normalize_empty&  s    
z"TestListBlock.test_normalize_emptyc                 C   sx   t t  }t |}dddggt j|dddggg}|D ]4}||}| |t jj | |d dddg q>dS )z{
        ListBlock.normalize should recursively normalize all values passed to
        it, and return a ListValue.
        rX   rY   r   r   N)r   rL  r   rz  r{  rD   rf   r  )r2   Zinner_list_blockr'   valuesr$   rN  r(   r(   r)   r|  ,  s    


z&TestListBlock.test_recursive_normalizeN)#r5   r6   r7   r  r  r  rK   r  r  r  r  r  r  r  r   r  r  r  r  r  r  rH  r  r  r  r  r  r  r  r  r  r  r  r  r|  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dS )	TestListBlockWithFixturesr  c              	   C   sX   ddddg}t jj|d}tt }| d ||}W 5 Q R X | || d S NrY   r      rt  )Zpk__inrX   )	r   objectsfilterr   rL  rZ  assertNumQueriesr   assertSequenceEqualr2   Zpage_idsZexpected_pagesr'   Zpagesr(   r(   r)   .test_calls_child_bulk_to_python_when_availableA  s    zHTestListBlockWithFixtures.test_calls_child_bulk_to_python_when_availablec              	   C   s   t t  }| d* |ddgg dgg}dd |D }W 5 Q R X | |tjjddtjjddgg tjjddgg d S )NrX   r  rt  rY   c                 S   s   g | ]}t |qS r(   rG  r&   valr(   r(   r)   
<listcomp>Q  s     zATestListBlockWithFixtures.test_bulk_to_python.<locals>.<listcomp>r  )	r   rL  rZ  r   rR  rL   r   r  r-  )r2   r'   r)  Zclean_resultr(   r(   r)   test_bulk_to_pythonK  s    z-TestListBlockWithFixtures.test_bulk_to_pythonc                 C   s   t t  }tjjdd}tjjdd}| t||	dd|j
ddd|j
dgtt|j
ddftt|j
ddfg d S )N	christmasZslugsaint-patrickblock1r  r  r   r$   block2)r   rL  rZ  r   r  r-  assertListEqualrG  rH  r   r  r=   r2   r'   christmas_pageZsaint_patrick_pager(   r(   r)   rJ  \  s.    z1TestListBlockWithFixtures.test_extract_referencesN)r5   r6   r7   rO  r  r	  rJ  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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/d0 Zd1d2 Zd3d4 Zd5d6 Zd7d8 Zd9d: Zd;d< Z d=d> Z!d?d@ Z"dAdB Z#dCdD Z$dEdF Z%dGdH Z&dIdJ Z'dKdL Z(dMdN Z)dOdP Z*dQdR Z+dSdT Z,dUdV Z-dWdX Z.dYdZ Z/d[d\ Z0d]d^ Z1d_d` Z2dadb Z3dcdd Z4deS )fTestStreamBlockc                 C   s<   t dt  fdt  fg}| t|j ddg d S )Nheading	paragraphr   rx  r8   rL   rG  r  r  r   r(   r(   r)   r  |  s    

z#TestStreamBlock.test_initialisationc                 C   s<   t dt  fdt  fg}| t|j ddg d S )N   heading	   paragraphr  r   r(   r(   r)   ,test_initialisation_with_binary_string_names  s    

z<TestStreamBlock.test_initialisation_with_binary_string_namesc                 C   s6   G dd dt j}| }| t|j ddg d S )Nc                   @   s   e Zd Ze Ze ZdS )zGTestStreamBlock.test_initialisation_from_subclass.<locals>.ArticleBlockNr5   r6   r7   r   r8   r  r  r(   r(   r(   r)   ArticleBlock  s   r  r  r  r   rx  rL   rG  r  r  r2   r  r'   r(   r(   r)   r    s    z1TestStreamBlock.test_initialisation_from_subclassc                 C   sD   G dd dt j}|dt  fg}| t|j dddg d S )Nc                   @   s   e Zd Ze Ze ZdS )zRTestStreamBlock.test_initialisation_from_subclass_with_extra.<locals>.ArticleBlockNr  r(   r(   r(   r)   r    s   r  intror  r  r  r  r(   r(   r)   r    s     z<TestStreamBlock.test_initialisation_from_subclass_with_extrac                 C   sH   G dd dt j}G dd d|}| }| t|j dddg d S )Nc                   @   s   e Zd Ze Ze ZdS )zSTestStreamBlock.test_initialisation_with_multiple_subclassses.<locals>.ArticleBlockNr  r(   r(   r(   r)   r    s   r  c                   @   s   e Zd Ze ZdS )z\TestStreamBlock.test_initialisation_with_multiple_subclassses.<locals>.ArticleWithIntroBlockNr5   r6   r7   r   r8   r  r(   r(   r(   r)   ArticleWithIntroBlock  s   r   r  r  r  r  )r2   r  r   r'   r(   r(   r)   r    s     z=TestStreamBlock.test_initialisation_with_multiple_subclasssesc                 C   s^   G dd dt j}G dd dt j}G dd d||}| }| t|j ddd	d
g dS )aO  
        The order of child blocks of a ``StreamBlock`` with multiple parent
        classes is slightly surprising at first. Child blocks are inherited in
        a bottom-up order, by traversing the MRO in reverse. In the example
        below, ``ArticleWithIntroBlock`` will have an MRO of::

            [ArticleWithIntroBlock, IntroMixin, ArticleBlock, StreamBlock, ...]

        This will result in ``intro`` appearing *after* ``heading`` and
        ``paragraph`` in ``ArticleWithIntroBlock.child_blocks``, even though
        ``IntroMixin`` appeared before ``ArticleBlock``.
        c                   @   s   e Zd Ze Ze ZdS )zETestStreamBlock.test_initialisation_with_mixins.<locals>.ArticleBlockNr  r(   r(   r(   r)   r    s   r  c                   @   s   e Zd Ze ZdS )zCTestStreamBlock.test_initialisation_with_mixins.<locals>.IntroMixinNr  r(   r(   r(   r)   
IntroMixin  s   r!  c                   @   s   e Zd Ze ZdS )zNTestStreamBlock.test_initialisation_with_mixins.<locals>.ArticleWithIntroBlockN)r5   r6   r7   r   r8   by_liner(   r(   r(   r)   r     s   r   r  r  r  r"  Nr  )r2   r  r!  r   r'   r(   r(   r)   r    s    
z/TestStreamBlock.test_initialisation_with_mixinsc                 C   s   t dt  fg}t |dg}d|d _t |dg}d|d _| t ||| t |dg}d|d _| t ||| d S )Nr  )r  r  ar   )r  z
not a test)	r   rx  r8   ry  r  ri  Z
BlockFieldZhas_changedr  )r2   r'   initial_valueZ
data_valueZchanged_data_valuer(   r(   r)   test_field_has_changed  s    


z&TestStreamBlock.test_field_has_changedc              	   C   sJ   t jdt  fgdd}t |g }| t j || W 5 Q R X d S )Nr  Tr   r   rx  r8   ry  r   r   r.   rI  r(   r(   r)   *test_required_raises_an_exception_if_empty  s    z:TestStreamBlock.test_required_raises_an_exception_if_emptyc                 C   sd   t jdt  fgdd}|dddg}z|| W n& t jk
r^   | dt j Y nX d S )Nr  Tr   r\  r   r$   %s was raised)r   rx  r8   r   r.   r   failureExceptionrI  r(   r(   r)   6test_required_does_not_raise_an_exception_if_not_empty  s    zFTestStreamBlock.test_required_does_not_raise_an_exception_if_not_emptyc                 C   s^   t jdt  fgdd}t |g }z|| W n& t jk
rX   | dt j Y nX d S )Nr  Fr   r)  )r   rx  r8   ry  r.   r   r*  rI  r(   r(   r)   6test_not_required_does_not_raise_an_exception_if_empty  s    zFTestStreamBlock.test_not_required_does_not_raise_an_exception_if_emptyc              	   C   sF   t dt  fg}t |g }| t j || W 5 Q R X d S )Nr  r&  rI  r(   r(   r)   test_required_by_default  s    z(TestStreamBlock.test_required_by_defaultc                 C   s,   G dd dt j}| }||}||S )Nc                   @   s   e Zd Ze Ze ZdS )z4TestStreamBlock.render_article.<locals>.ArticleBlockN)r5   r6   r7   r   r8   r  r  r  r(   r(   r(   r)   r    s   r  )r   rx  r   rK   )r2   r   r  r'   r$   r(   r(   r)   render_article
  s    
zTestStreamBlock.render_articlec                 C   s|   G dd dt j}t d| fd| fg}|j|ddddddd	gd
ddd}| |dd
dd	dddd	g dS )z
        The get_api_representation method of a StreamBlock should invoke
        the block's get_api_representation method on each child and
        the context should be passed on.
        c                   @   s   e Zd ZdddZdS )zlTestStreamBlock.test_get_api_representation_calls_same_method_on_children_with_context.<locals>.ContextBlockNc                 S   s   || S r<   r(   r  r(   r(   r)   r    s    zTestStreamBlock.test_get_api_representation_calls_same_method_on_children_with_context.<locals>.ContextBlock.get_api_representation)Nr  r(   r(   r(   r)   r    s   r  rn   r  r
  r(  r  Z111111r  r  r  r  rp   N)r   r8   rx  r  r   r  r  r(   r(   r)   r    s(    



zVTestStreamBlock.test_get_api_representation_calls_same_method_on_children_with_contextc                 C   sJ   |  dddddddddg}| d| | d| | d	| d S )
Nr  My titler(  r  zMy <i>first</i> paragraphMy second paragraphz)<div class="block-heading">My title</div>z<<div class="block-paragraph">My <i>first</i> paragraph</div>z6<div class="block-paragraph">My second paragraph</div>)r.  rk   r  r(   r(   r)   r   7  s$     zTestStreamBlock.test_renderc                 C   sB   |  ddddddg}| d| | d| | d| d S )Nr"   r\  r(  r  My first paragraphz5<div class="block-paragraph">My first paragraph</div>)r.  r  rk   r  r(   r(   r)   test_render_unknown_typeO  s    z(TestStreamBlock.test_render_unknown_typec                 C   sb   t dt jddfdt  fg}|dddg}||}| d| | }| d| dS )	z
        The default rendering of a StreamBlock should invoke the block's render method
        on each child, rather than just outputting the child value as a string.
        r  rP   rQ   r  r\  r(  z/<div class="block-heading"><h1>Hello</h1></div>Nr   rx  r8   r   rK   rk   re  r2   r'   r$   rN   r(   r(   r)   r  a  s    

	
z:TestStreamBlock.test_render_calls_block_render_on_childrenc                 C   sr   t dt jddfdt  fg}|dddg}|j|ddid	}| d
| |jddid	}| d
| d S )Nr  rP   rQ   r  rb  r(  rn   ro   rp   z;<div class="block-heading"><h1 lang="fr">Bonjour</h1></div>r3  r4  r(   r(   r)   r  x  s6    

	    z6TestStreamBlock.test_render_passes_context_to_childrenc                 C   s   t dt jddfdt  fg}|dddg}|d  }| d| t|d }| d| |d  }| d| d	S )
z
        Accessing a child element of the stream (giving a StreamChild object) and rendering it
        should use the block template, not just render the value's string representation
        r  rP   rQ   r  r\  r(  r   z<h1>Hello</h1>N)r   rx  r8   r   rK   rL   r=   re  r4  r(   r(   r)   /test_render_on_stream_child_uses_child_template  s    

	z?TestStreamBlock.test_render_on_stream_child_uses_child_templatec                 C   sx   t dt jddfdt  fg}|dddg}|d jdd	id
}| d| |d jdd	id
}| d| d S )Nr  rP   rQ   r  rb  r(  r   rn   ro   rp   z<h1 lang="fr">Bonjour</h1>)r   rx  r8   r   rK   rL   re  r4  r(   r(   r)   .test_can_pass_context_to_stream_child_template  s    

	z>TestStreamBlock.test_can_pass_context_to_stream_child_templatec                 C   s  G dd dt j}| }|d t |}| |d d dd |d D }| t|d |d \}}| |d t|}| |d t j	 | |d j
d	 | |d t j	 | |d j
d
 | |d d d d | |d ddd dd d i dddddddd	 d S )Nc                   @   s   e Zd Ze Ze ZdS )z0TestStreamBlock.test_adapt.<locals>.ArticleBlockNr  r(   r(   r(   r)   r    s   r  test_streamblockr   c                 S   s   g | ]\}}|t |fqS r(   r  )r&   
group_nameZ
group_iterr(   r(   r)   r    s    z.TestStreamBlock.test_adapt.<locals>.<listcomp>rX   r   r  r  rY   )r  r  r   Test streamblockrZ   FTr  r  r  r  r  r  )	r]   r`   ra   r  r  r  blockCountsr_   rc   )r   rx  rd   r   re   rL   r   rG  rf   r8   name)r2   r  r'   re   Zgrouped_blocksr8  Z
block_iterZ
block_listr(   r(   r)   r    sF    
zTestStreamBlock.test_adaptc              
   C   sh   t dt  fg}| |ddii d | |ddddddi d | |d	d
ii d d S )Nr  mystream-countr  ZmystreamrF  r  r   )r<  zmystream-0-typezmystream-0-valuezmystream-0-deletedzmystream-0-orderr  r  )r   rx  r8   ri  r  r  r   r(   r(   r)   r    s,    
z,TestStreamBlock.test_value_omitted_from_datac              	   C   sx   G dd dt j}| }t |ddddg}| t}|| W 5 Q R X | |j ddd	gidd
gidi d S )Nc                   @   s   e Zd Ze Ze ZdS )z>TestStreamBlock.test_validation_errors.<locals>.ValidatedBlockNr5   r6   r7   r   r8   charr  r   r(   r(   r(   r)   ValidatedBlock  s   r?  )r>  r   r>  r"   r   r   )r   rV  blockErrorsr  r
  zEnter a valid URL.)r   r   )	r   rx  ry  r   r   r.   rL   r  r  r2   r?  r'   r$   r  r(   r(   r)   test_validation_errors  s(    
z&TestStreamBlock.test_validation_errorsc              	   C   s   G dd dt j}|dd}t |g }| t}|| W 5 Q R X | |j ddgi t |dg}| 	|| d S )Nc                   @   s   e Zd Ze Ze ZdS )zFTestStreamBlock.test_min_num_validation_errors.<locals>.ValidatedBlockNr=  r(   r(   r(   r)   r?  -  s   r?  rX   r  r  z The minimum number of items is 1r@  
r   rx  ry  r   r   r.   rL   r  r  r  rC  r(   r(   r)   r  ,  s    
z.TestStreamBlock.test_min_num_validation_errorsc              	   C   s   G dd dt j}|dd}t |ddddg}| t}|| W 5 Q R X | |j ddgi t |dg}| 	|| d S )	Nc                   @   s   e Zd Ze Ze ZdS )zFTestStreamBlock.test_max_num_validation_errors.<locals>.ValidatedBlockNr=  r(   r(   r(   r)   r?  A  s   r?  rX   r  r@  rA  r  z The maximum number of items is 1rE  rC  r(   r(   r)   r  @  s$    

z.TestStreamBlock.test_max_num_validation_errorsc              	   C   s   G dd dt j}|dddiid}t |ddg}| t}|| W 5 Q R X | |j dd	gi t |dd
dg}| 	|| d S )Nc                   @   s   e Zd Ze Ze ZdS )zOTestStreamBlock.test_block_counts_min_validation_errors.<locals>.ValidatedBlockNr=  r(   r(   r(   r)   r?  ]  s   r?  r>  r  rX   Zblock_countsrA  r  z&Char: The minimum number of items is 1r@  rE  rC  r(   r(   r)   'test_block_counts_min_validation_errors\  s,    z7TestStreamBlock.test_block_counts_min_validation_errorsc              	   C   s   G dd dt j}|dddiid}t |ddddg}| t}|| W 5 Q R X | |j d	d
gi t |dddg}| 	|| d S )Nc                   @   s   e Zd Ze Ze ZdS )zOTestStreamBlock.test_block_counts_max_validation_errors.<locals>.ValidatedBlockNr=  r(   r(   r(   r)   r?  ~  s   r?  r>  r  rX   rF  r@  rA  r  z&Char: The maximum number of items is 1rE  rC  r(   r(   r)   'test_block_counts_max_validation_errors}  s0    
z7TestStreamBlock.test_block_counts_max_validation_errorsc                 C   s   G dd dt j}| }ddi}tddD ]F}|d| dd	| td
| d| dd| d| d| d| i q*||i d}| |d
 jd | |d
 jd d S )Nc                   @   s   e Zd Ze Ze ZdS )zWTestStreamBlock.test_ordering_in_form_submission_uses_order_field.<locals>.ArticleBlockNr  r(   r(   r(   r)   r    s   r  article-countr  r   r   article-%d-deletedr   article-%d-orderrY   article-%d-typer  article-%d-value
heading %dzarticle-%d-idz000%darticlez	heading 00000)	r   rx  r  r  r=   r   rL   r$   r  r2   r  r'   r  r  r  r(   r(   r)   r    s(      
   
zATestStreamBlock.test_ordering_in_form_submission_uses_order_fieldc                 C   s   G dd dt j}| }ddi}tddD ]6}|d| dd	| t|d
| dd| d| i q*||i d}| |d jd d S )Nc                   @   s   e Zd Ze Ze ZdS )zQTestStreamBlock.test_ordering_in_form_submission_is_numeric.<locals>.ArticleBlockNr  r(   r(   r(   r)   r    s   r  rI  r  r   r  rJ  r   rK  rL  r  rM  rN  rO  rY   z	heading 2)r   rx  r  r  r=   r   rL   r$   rQ  r(   r(   r)   r    s"        	z;TestStreamBlock.test_ordering_in_form_submission_is_numericc                 C   sZ   G dd dt j}| }|dddddddddg}||}| |dddg d S )	Nc                   @   s   e Zd Ze Ze ZdS )z=TestStreamBlock.test_searchable_content.<locals>.ArticleBlockNr  r(   r(   r(   r)   r    s   r  r  r/  r(  r  r1  r0  )r   rx  r   ry   rL   )r2   r  r'   r$   r{   r(   r(   r)   r     s,    
z'TestStreamBlock.test_searchable_contentc                    s   G dd dt j G  fdddt j}| }|ddi}|d }| |t j | t|d | |d	 jd
 | |d	 j	d dS )zETest that we can specify a default value in the Meta of a StreamBlockc                   @   s*   e Zd Ze Ze ZG dd dZdS )z7TestStreamBlock.test_meta_default.<locals>.ArticleBlockc                   @   s   e Zd ZdgZdS )z<TestStreamBlock.test_meta_default.<locals>.ArticleBlock.Metar  A default headingNr5   r6   r7   r!  r(   r(   r(   r)   r    s   r  Nr5   r6   r7   r   r8   r  r  r  r(   r(   r(   r)   r    s   r  c                       s   e Zd Ze Z  ZdS )z@TestStreamBlock.test_meta_default.<locals>.ArticleContainerBlockNr5   r6   r7   r   r8   r  rO  r(   r  r(   r)   ArticleContainerBlock  s   rX  r  BobrO  rX   r   r  rS  N
r   rx  r  r   rf   ry  rL   r   
block_typer$   r2   rX  r'   r  r  r(   rW  r)   test_meta_default  s    	z!TestStreamBlock.test_meta_defaultc                    s   G dd dt j G  fdddt j}| }|ddi}|d }| |t j | t|d | |d	 jd
 | |d	 j	d dS )zLTest that we can specify a default value in the constructor of a StreamBlockc                   @   s*   e Zd Ze Ze ZG dd dZdS )z>TestStreamBlock.test_constructor_default.<locals>.ArticleBlockc                   @   s   e Zd ZdgZdS )zCTestStreamBlock.test_constructor_default.<locals>.ArticleBlock.MetarR  NrT  r(   r(   r(   r)   r    s   r  NrU  r(   r(   r(   r)   r    s   r  c                       s    e Zd Ze Z dgdZdS )zGTestStreamBlock.test_constructor_default.<locals>.ArticleContainerBlock)r  A different default headingr   NrV  r(   rW  r(   r)   rX    s   rX  r  rY  rO  rX   r   r  r^  NrZ  r\  r(   rW  r)   test_constructor_default	  s    	z(TestStreamBlock.test_constructor_defaultc                 C   sf   t dt  fg}|dddg}|dddg}|dddg}| || | || d S )Nr9   r  r(  goodbye)r   rx  r8   r   rL   ZassertNotEqual)r2   r'   Zvalue1Zvalue2Zvalue3r(   r(   r)   test_stream_value_equality"  s    
z*TestStreamBlock.test_stream_value_equalityc           	      C   s   G dd dt j}G dd dt j}G dd dt j}G dd dt j}G d	d
 d
t j}t d| fd| fd| fd| fd| fg}|d t |}t|d }| | dddh dS )z\If group attributes are set in Block Meta classes, make sure the blocks are grouped togetherc                   @   s   e Zd ZG dd dZdS )zJTestStreamBlock.test_adapt_considers_group_attribute.<locals>.Group1Block1c                   @   s   e Zd ZdZdS )zOTestStreamBlock.test_adapt_considers_group_attribute.<locals>.Group1Block1.Metagroup1Nr5   r6   r7   groupr(   r(   r(   r)   r  4  s   r  Nr  r(   r(   r(   r)   Group1Block13  s   re  c                   @   s   e Zd ZG dd dZdS )zJTestStreamBlock.test_adapt_considers_group_attribute.<locals>.Group1Block2c                   @   s   e Zd ZdZdS )zOTestStreamBlock.test_adapt_considers_group_attribute.<locals>.Group1Block2.Metarb  Nrc  r(   r(   r(   r)   r  8  s   r  Nr  r(   r(   r(   r)   Group1Block27  s   rf  c                   @   s   e Zd ZG dd dZdS )zJTestStreamBlock.test_adapt_considers_group_attribute.<locals>.Group2Block1c                   @   s   e Zd ZdZdS )zOTestStreamBlock.test_adapt_considers_group_attribute.<locals>.Group2Block1.Metagroup2Nrc  r(   r(   r(   r)   r  <  s   r  Nr  r(   r(   r(   r)   Group2Block1;  s   rh  c                   @   s   e Zd ZG dd dZdS )zJTestStreamBlock.test_adapt_considers_group_attribute.<locals>.Group2Block2c                   @   s   e Zd ZdZdS )zOTestStreamBlock.test_adapt_considers_group_attribute.<locals>.Group2Block2.Metarg  Nrc  r(   r(   r(   r)   r  @  s   r  Nr  r(   r(   r(   r)   Group2Block2?  s   ri  c                   @   s   e Zd ZdS )zJTestStreamBlock.test_adapt_considers_group_attribute.<locals>.NoGroupBlockNr  r(   r(   r(   r)   NoGroupBlockC  s   rj  b1b2Zb3Zb4Zngbr7  rX   r   rb  rg  N)	r   r8   rx  rd   r   re   dictrL   r  )	r2   re  rf  rh  ri  rj  r'   re   Zblockdefs_dictr(   r(   r)   $test_adapt_considers_group_attribute0  s"    

z4TestStreamBlock.test_adapt_considers_group_attributec                 C   s   G dd dt j}| }|jddddddd	d	dd
dddddddi dd}| t|d | |d jd | |d jd | |d jd | |d jd | |d jd | |d jd d S )Nc                   @   s   e Zd Ze Ze ZdS )z>TestStreamBlock.test_value_from_datadict.<locals>.ArticleBlockNr  r(   r(   r(   r)   r  W  s   r  r  r   2r  rP  this is my headingrF  0001za deleted headingr  r  <p>this is a paragraph</p>)z	foo-countzfoo-0-deletedzfoo-0-orderz
foo-0-typezfoo-0-idzfoo-0-valuezfoo-1-deletedzfoo-1-orderz
foo-1-typezfoo-1-idzfoo-1-valuezfoo-2-deletedzfoo-2-orderz
foo-2-typezfoo-2-idzfoo-2-valuer"   r  rY   r   rX   )r   rx  r   rL   r   r[  r  r$   )r2   r  r'   r$   r(   r(   r)   r8  V  s<    z(TestStreamBlock.test_value_from_datadictc                 C   s   G dd dt j}| }t j|||d}||}| t|d | |d dddd	 | |d
 d d | |d
 d d | |d
 d  ||}| |d d |d d  | |d
 d |d
 d  d S )Nc                   @   s   e Zd Ze Ze ZdS )z:TestStreamBlock.check_get_prep_value.<locals>.ArticleBlockNr  r(   r(   r(   r)   r  ~  s   r  is_lazyrY   r   r  rp  rP  r  rX   r   r  r$   rr  r  )r   rx  ry  r   rL   r   r  )r2   stream_datart  r  r'   r$   r   Zjsonish_value_againr(   r(   r)   check_get_prep_value}  s    


z$TestStreamBlock.check_get_prep_valuec                 C   s   ddg}| j |dd d S )N)r  rp  rP  )r  rr  Frs  rv  r2   ru  r(   r(   r)   test_get_prep_value_not_lazy  s    z,TestStreamBlock.test_get_prep_value_not_lazyc                 C   s(   dddddddg}| j |dd	 d S )
Nr  rp  rP  r  r  rr  r(  Trs  rw  rx  r(   r(   r)   test_get_prep_value_is_lazy  s    
z+TestStreamBlock.test_get_prep_value_is_lazyc                    s   G dd dt j}|   fdd| D } |}| t|d | |d ddd	d
g | t|d d |d d }| |d d | |d d | |d  d S )Nc                   @   s4   e Zd Zede fgZede fgZdS )zPTestStreamBlock.check_get_prep_value_nested_streamblocks.<locals>.TwoColumnBlockr9   N)r5   r6   r7   r   rx  r8   leftrightr(   r(   r(   r)   TwoColumnBlock  s   r}  c                    s(   i | ] \}}|t j j| |d qS )rs  )r   ry  r  )r&   kvr'   rt  r(   r)   
<dictcomp>  s    zLTestStreamBlock.check_get_prep_value_nested_streamblocks.<locals>.<dictcomp>rY   r{  r9   	some textrP  r  r|  rX   r   r   r$   some other textr  )r   r  rM  r   rL   r   r  )r2   ru  rt  r}  r$   r   Zright_blockr(   r  r)   (check_get_prep_value_nested_streamblocks  s     
z8TestStreamBlock.check_get_prep_value_nested_streamblocksc                 C   s    dgdgd}| j |dd d S )N)r9   r  rP  )r9   r  r{  r|  Frs  r  rx  r(   r(   r)   0test_get_prep_value_nested_streamblocks_not_lazy  s    z@TestStreamBlock.test_get_prep_value_nested_streamblocks_not_lazyc                 C   s.   ddddgdddgd}| j |dd	 d S )
Nr9   r  rP  r  r  r(  r  Trs  r  rx  r(   r(   r)   /test_get_prep_value_nested_streamblocks_is_lazy  s    	z?TestStreamBlock.test_get_prep_value_nested_streamblocks_is_lazyc                 C   sn   G dd dt j}| }|ddddddd	dg}d
|d _||}| |ddddddd
dg d S )Nc                   @   s   e Zd Ze Ze ZdS )zUTestStreamBlock.test_modifications_to_stream_child_id_are_saved.<locals>.ArticleBlockNr  r(   r(   r(   r)   r    s   r  r  r  rq  r  r  ri  00020003rX   )r   rx  r   r  r   rL   r2   r  r'   streamraw_datar(   r(   r)   /test_modifications_to_stream_child_id_are_saved  s    





z?TestStreamBlock.test_modifications_to_stream_child_id_are_savedc                 C   sn   G dd dt j}| }|ddddddd	dg}d
|d _||}| |dddddd
d	dg d S )Nc                   @   s   e Zd Ze Ze ZdS )zXTestStreamBlock.test_modifications_to_stream_child_value_are_saved.<locals>.ArticleBlockNr  r(   r(   r(   r)   r    s   r  r  r  rq  r  r  ri  r  earthrX   )r   rx  r   r$   r   rL   r  r(   r(   r)   2test_modifications_to_stream_child_value_are_saved  s    





zBTestStreamBlock.test_modifications_to_stream_child_value_are_savedc                 C   sl   G dd dt j}| }|ddddddd	dg}d
|d< ||}| |ddddddddg d S )Nc                   @   s   e Zd Ze Ze ZdS )z?TestStreamBlock.test_set_streamvalue_item.<locals>.ArticleBlockNr  r(   r(   r(   r)   r    s   r  r  r  rq  r  r  ri  r  )r  r`  r  rX   r`  r  r   rx  r   r   rL   r  r(   r(   r)   test_set_streamvalue_item  s    




z)TestStreamBlock.test_set_streamvalue_itemc                 C   s`   G dd dt j}| }|ddddddd	dg}|d
= ||}| |ddd	dg d S )Nc                   @   s   e Zd Ze Ze ZdS )zBTestStreamBlock.test_delete_streamvalue_item.<locals>.ArticleBlockNr  r(   r(   r(   r)   r    s   r  r  r  rq  r  r  ri  r  r   r  r  r(   r(   r)   test_delete_streamvalue_item  s    



z,TestStreamBlock.test_delete_streamvalue_itemc              	   C   sz   G dd dt j}| }|ddddddd	dg}|d
d ||}| |ddddddddddd	dg d S )Nc                   @   s   e Zd Ze Ze ZdS )zBTestStreamBlock.test_insert_streamvalue_item.<locals>.ArticleBlockNr  r(   r(   r(   r)   r  2  s   r  r  r  rq  r  r  ri  r  rX   )r  mutabler  r  r  )r   rx  r   insertr   rL   r  r(   r(   r)   test_insert_streamvalue_item1  s     





z,TestStreamBlock.test_insert_streamvalue_itemc              	   C   sx   G dd dt j}| }|ddddddd	dg}|d
 ||}| |ddddddd	dddddg d S )Nc                   @   s   e Zd Ze Ze ZdS )zBTestStreamBlock.test_append_streamvalue_item.<locals>.ArticleBlockNr  r(   r(   r(   r)   r  I  s   r  r  r  rq  r  r  ri  r  )r  of warcraftr  r  r  )r   rx  r   rS  r   rL   r  r(   r(   r)   test_append_streamvalue_itemH  s     






z,TestStreamBlock.test_append_streamvalue_itemc                 C   s  G dd dt j}| }|ddddddd	dg}| |jd
 dddd d|jd
 d< | |jd
 dddd ||}| |ddddddd	dg | |d
 jd d|jd
 d< | |jd
 d d | |d
 jd ||}| |ddddddd	dg dddd|jd
< ||}| |ddddddd	dg | |d
 jd |jd= |jd
dddd ||}| |ddddddddg d S )Nc                   @   s   e Zd Ze Ze ZdS )z?TestStreamBlock.test_streamvalue_raw_data.<locals>.ArticleBlockNr  r(   r(   r(   r)   r  `  s   r  r  r  rq  r  r  ri  r  r   bonjourr$   z	guten tagZ
konnichiwar  rX   zhello kitty saysZ0004)r   rx  r   rL   r  r   r$   r  r  r(   r(   r)   test_streamvalue_raw_data_  sn    

 
 



	


	


 



z)TestStreamBlock.test_streamvalue_raw_datac                 C   sl   t jdt  fdt  fgdd}|d t |}| |d ddd	d	i d
ddddddddd	 d	S )zXform_classname from kwargs to be used as an additional class when rendering stream blockr  r  zrocket-sectionri   r7  r   r9  rZ   NFTr  r  r  r  r  r  	r]   r`   r  r  r:  r  r_   ra   rc   )r   rx  r8   rd   r   re   rL   rg   r(   r(   r)   r    s4    


z3TestStreamBlock.test_adapt_with_classname_via_kwargc           	      C   s  G dd dt j}| }|dddddddddg}| }t|t jjsTtd	d
 | D }| 	|dgddgg d |jdd}| 	|| d  | 	t
|d |D ]}| 	|jd q| 	|jddg  | 	|jddg  |jdd}| 	|jd | 	|jd | |jddd  | |jddd  | }|d }| 	|jd | 	|jd | |d d  | |d d  d S )Nc                   @   s$   e Zd Ze Ze Ze Z	dS )z6TestStreamBlock.test_block_names.<locals>.ArticleBlockN)
r5   r6   r7   r   r8   r  r  r  	DateBlockdater(   r(   r(   r)   r    s   r  r  r/  r(  r  r1  r0  c                 S   s    i | ]\}}|d d |D qS )c                 S   s   g | ]
}|j qS r(   r#   r%   r(   r(   r)   r    s     z?TestStreamBlock.test_block_names.<locals>.<dictcomp>.<listcomp>r(   )r&   
block_namer   r(   r(   r)   r    s    z4TestStreamBlock.test_block_names.<locals>.<dictcomp>)r  r  r  )r  rY   r  invalid_type)r   rx  r   blocks_by_name
isinstancery  ZBlockNameLookupAssertionErrorrM  rL   r   r[  Zfirst_block_by_namer$   rB   )	r2   r  r'   r$   r  r)  Zparagraph_blocksZfirst_heading_blockZfirst_blocks_by_namer(   r(   r)   test_block_names  sX    	z TestStreamBlock.test_block_namesc                 C   sb   G dd dt j}| }|d t |}| |d ddddi dd	d
ddddddd	 dS )zVform_classname from meta to be used as an additional class when rendering stream blockc                   @   s"   e Zd Ze ZG dd dZdS )zNTestStreamBlock.test_adapt_with_classname_via_class_meta.<locals>.ProfileBlockc                   @   s   e Zd ZdZdS )zSTestStreamBlock.test_adapt_with_classname_via_class_meta.<locals>.ProfileBlock.Metaprofile-block-largeNr  r(   r(   r(   r)   r    s   r  N)r5   r6   r7   r   r8   usernamer  r(   r(   r(   r)   ProfileBlock  s   r  r7  r   r9  rZ   NFTr  r  r  r  r  r  r  r  )r   rx  rd   r   re   rL   )r2   r  r'   re   r(   r(   r)   r    s,    
z8TestStreamBlock.test_adapt_with_classname_via_class_metaN)5r5   r6   r7   r  r  r  r  r  r  r%  r'  r+  r,  r-  r.  r  r   r2  r  r  r5  r6  r  r  rD  r  r  rG  rH  r  r  r   r]  r_  ra  rn  r8  rv  ry  rz  r  r  r  r  r  r  r  r  r  r  r  r  r  r(   r(   r(   r)   r  {  sd   
	

# /!#"&'G#Cr  c                       s8   e Zd Ze fddZdd Zdd Zdd Z  ZS )	TestNormalizeStreamBlockc                    s   t    tdt fdt fg| _tdtdt fdt fdttjfgfdtdt	 fgfdttjfg| _
d S )Nnumberr9   ro  
inner_liststructboolrG  )r-   
setUpClassr   rx  r   r  simple_blockrL  r  r   recursive_blockr   r3   r(   r)   r  1  s"    



z#TestNormalizeStreamBlock.setUpClassc              
   C   sN   g dd g}|D ]:}| j |d$ | | j|t| jg  W 5 Q R X qd S )Nr   r#   )rL  rL   r  rD   r   ry  )r2   r  r$   r(   r(   r)   test_normalize_empty_streamH  s    

z4TestNormalizeStreamBlock.test_normalize_empty_streamc                 C   sB   ddg}t | j|}| || j| | || j| dS )zS
        Test normalize when trivially recursive, or already a StreamValue
        r  rX   )r9   ZichibanN)r   ry  r  rL   rD   )r2   r$   r  r(   r(   r)   r  Q  s    z1TestNormalizeStreamBlock.test_normalize_base_casec                 C   s  dddifdddddd	d	d
ddgfgfddd	d	d
ddgfgdddidddd	dddddddd	ddd	d
ddd
ddgdgddddd	ddd	d
ddd
ddgdgf}|D ]}| j |d | j|}| |d jtj | |d jd t | |d	 jtj | |d	 jd jt	 | |d	 jd	 jt
 | |d
 jd t	 | |d
 jtjj | |d	 jd
 jd t	 | |d	 jd
 jtjj W 5 Q R X qdS )zX
        A stream block is normalized iff all of its sub-blocks are normalized.
        r  r  Tro  r  )r9   r  r  r   rX   rY   r   rt  rG  r(  r  r9   r  r  r  r#   N)rL  r  rD   rf   r$   r   r7  r  ry  r   r=   rz  r{  )r2   r  r$   rN  r(   r(   r)   test_normalize_recursiveZ  sX    






. z1TestNormalizeStreamBlock.test_normalize_recursive)	r5   r6   r7   r   r  r  r  r  r:   r(   r(   r3   r)   r  0  s
   		r  c                   @   s"   e Zd ZdgZdd Zdd ZdS )TestStructBlockWithFixturesr  c              	   C   s   t dt jddfdt jddfg}| d. |dd	d
ddd
d dd
ddig}W 5 Q R X dd |D }| |t jgd  dd |D }| |d	dddg dd |D }| |tj	j
ddtj	j
ddd tj	j
ddg d S )NrY  Fr   	link_textmissing titler   rX   rY   zpage tworY  r  r   z
page threezno pager  c                 S   s   g | ]}t |qS r(   )r   r  r(   r(   r)   r    s     zCTestStructBlockWithFixtures.test_bulk_to_python.<locals>.<listcomp>c                 S   s   g | ]}|d  qS )r  r(   r  r(   r(   r)   r    s     c                 S   s   g | ]}|d  qS )rY  r(   r  r(   r(   r)   r    s     r  )r   r  rZ  r8   r   rR  rL   r7  r   r  r-  )r2   Zpage_link_blockr)  Zresult_typesZresult_titlesZresult_pagesr(   r(   r)   r	    s:    	 
z/TestStructBlockWithFixtures.test_bulk_to_pythonc                 C   sd   t dt jddfdt jddfg}tjjdd}| t|	|d	d
tt
|jddfg d S )NrY  Fr   r  r  r   r
  r  Z	Christmasr  )r   r  rZ  r8   r   r  r-  r  rG  rH  r=   r  r2   r'   r  r(   r(   r)   rJ    s    z3TestStructBlockWithFixtures.test_extract_referencesNr5   r6   r7   rO  r	  rJ  r(   r(   r(   r)   r    s   %r  c                   @   s"   e Zd ZdgZdd Zdd ZdS )TestStreamBlockWithFixturesr  c              	   C   s"  t dt  fdt  fg}| dN |dddddddddgdddd	d
dgddddddgg}W 5 Q R X | d dd |D }W 5 Q R X | |dddgdgddgg | d dd |D }W 5 Q R X | |dtjj	ddtjj	ddgdgdtjj	ddgg d S )NrY  r  rX   zinteresting pagesr(  rY   r   zpages written by dogsZwoofz	woof woofzboring pagesr  r   c                 S   s   g | ]}d d |D qS )c                 S   s   g | ]
}|j qS r(   )r[  r%   r(   r(   r)   r    s     NTestStreamBlockWithFixtures.test_bulk_to_python.<locals>.<listcomp>.<listcomp>r(   r&   r  r(   r(   r)   r    s     zCTestStreamBlockWithFixtures.test_bulk_to_python.<locals>.<listcomp>c                 S   s   g | ]}d d |D qS )c                 S   s   g | ]
}|j qS r(   r#   r%   r(   r(   r)   r    s     r  r(   r  r(   r(   r)   r    s     r  )
r   rx  rZ  r8   r   rR  rL   r   r  r-  )r2   r  resultsZblock_typesZblock_valuesr(   r(   r)   r	    sJ    

	z/TestStreamBlockWithFixtures.test_bulk_to_pythonc                 C   s   t dt  fdt  fg}tjjdd}tjjdd}| t|	|
ddddd	d|jdd
d|jdgtt|jdd	ftt|jdd
fg d S )NrY  r  r
  r  r  r  zSome events that you might liker  r  Zblock3)r   rx  rZ  r8   r   r  r-  r  rG  rH  r   r  r=   r  r(   r(   r)   rJ  %  s>    

z3TestStreamBlockWithFixtures.test_extract_referencesNr  r(   r(   r(   r)   r    s   Br  c                   @   s   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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 )/TestPageChooserBlockr  c                 C   s>   t  }tjjdd}| |||j | |d dS )zIThe value of a PageChooserBlock (a Page object) should serialize to an IDr
  r  N)	r   rZ  r   r  r-  rL   r   r  r   r  r(   r(   r)   r  R  s    z#TestPageChooserBlock.test_serializec                 C   s>   t  }tjjdd}| ||j| | |d dS )zVThe serialized value of a PageChooserBlock (an ID) should deserialize to a Page objectr
  r  N)	r   rZ  r   r  r-  rL   r   r  r   r  r(   r(   r)   r  \  s    z%TestPageChooserBlock.test_deserializec                 C   s   ddl m} tjdd}|d t |}| |d d | |d | | |d j	t
g | |d j | |d dd	d
ddd	ddid d S )Nr   )AdminPageChooserpick a page, any pagerU   test_pagechooserblockrX   rY   zTest pagechooserblockTzdoc-empty-inversez?w-field w-field--model_choice_field w-field--admin_page_chooserr[   r\   )r]   r_   r`   r^   ra   rb   rc   )Zwagtail.admin.widgets.chooserr  r   rZ  rd   r   re   rL   rf   target_modelsr   ri  can_choose_root)r2   r  r'   re   r(   r(   r)   r  f  s&    
zTestPageChooserBlock.test_adaptc                 C   s<   t jddd}|d t |}| |d jtg d S )Nr  tests.SimplePagerV   	page_typer  rX   )r   rZ  rd   r   re   rL   r  r   rg   r(   r(   r)   #test_adapt_with_target_model_string  s     
z8TestPageChooserBlock.test_adapt_with_target_model_stringc                 C   s<   t jdtd}|d t |}| |d jtg d S Nr  r  r  rX   )r   rZ  r   rd   r   re   rL   r  rg   r(   r(   r)   $test_adapt_with_target_model_literal  s     
z9TestPageChooserBlock.test_adapt_with_target_model_literalc                 C   sB   t jdddgd}|d t |}| |d jttg d S )Nr  r  tests.EventPager  r  rX   )	r   rZ  rd   r   re   rL   r  r   r   rg   r(   r(   r)   -test_adapt_with_target_model_multiple_strings  s    
zBTestPageChooserBlock.test_adapt_with_target_model_multiple_stringsc                 C   sB   t jdttgd}|d t |}| |d jttg d S r  )	r   rZ  r   r   rd   r   re   rL   r  rg   r(   r(   r)   .test_adapt_with_target_model_multiple_literals  s     
zCTestPageChooserBlock.test_adapt_with_target_model_multiple_literalsc                 C   s8   t jddd}|d t |}| |d j d S )Nr  T)rV   r  r  rX   )r   rZ  rd   r   re   r  r  rg   r(   r(   r)   test_adapt_with_can_choose_root  s     
z4TestPageChooserBlock.test_adapt_with_can_choose_rootc                 C   sZ   t  }tjjdd}|dt|jii d}| || |ddii d}| 	| d S )Nr
  r  rY  r   )
r   rZ  r   r  r-  r   r=   r  rL   r   )r2   r'   r  r$   Zempty_valuer(   r(   r)   r    s    z'TestPageChooserBlock.test_form_responsec              	   C   sz   t  }t jdd}tjjdd}| ||| | t |d  W 5 Q R X | ||| | 	|d  d S )NFr   r
  r  )
r   rZ  r   r  r-  rL   r.   r   r   r   )r2   Zrequired_blockZnonrequired_blockr  r(   r(   r)   rX    s    zTestPageChooserBlock.test_cleanc                 C   s   t  }| |jt d S r<   r   rZ  rL   target_modelr   r   r(   r(   r)   test_target_model_default  s    z.TestPageChooserBlock.test_target_model_defaultc                 C   s   t jdd}| |jt d S )Nr  r  )r   rZ  rL   r  r   r   r(   r(   r)   test_target_model_string  s    z-TestPageChooserBlock.test_target_model_stringc                 C   s   t jtd}| |jt d S Nr  )r   rZ  r   rL   r  r   r(   r(   r)   test_target_model_literal  s    z.TestPageChooserBlock.test_target_model_literalc                 C   s"   t jddgd}| |jt d S )Nr  r  r  r  r   r(   r(   r)   "test_target_model_multiple_strings  s    z7TestPageChooserBlock.test_target_model_multiple_stringsc                 C   s"   t jttgd}| |jt d S r  )r   rZ  r   r   rL   r  r   r   r(   r(   r)   #test_target_model_multiple_literals  s    z8TestPageChooserBlock.test_target_model_multiple_literalsc                 C   s"   t  }| | ddi f d S )Nwagtail.blocks.PageChooserBlockr(   r   rZ  rL   r~  r   r(   r(   r)   %test_deconstruct_target_model_default  s
     z:TestPageChooserBlock.test_deconstruct_target_model_defaultc                 C   s,   t jdd}| | ddddgif d S )Nr  r  r  r(   r  r  r   r(   r(   r)   $test_deconstruct_target_model_string  s    z9TestPageChooserBlock.test_deconstruct_target_model_stringc                 C   s,   t jtd}| | ddddgif d S )Nr  r  r(   r  r  )r   rZ  r   rL   r~  r   r(   r(   r)   %test_deconstruct_target_model_literal  s    z:TestPageChooserBlock.test_deconstruct_target_model_literalc                 C   s2   t jddgd}| | dddddgif d S )Nr  r  r  r  r(   r  r  r   r(   r(   r)   .test_deconstruct_target_model_multiple_strings  s    
zCTestPageChooserBlock.test_deconstruct_target_model_multiple_stringsc                 C   s2   t jttgd}| | dddddgif d S )Nr  r  r(   r  r  r  )r   rZ  r   r   rL   r~  r   r(   r(   r)   /test_deconstruct_target_model_multiple_literals  s    
zDTestPageChooserBlock.test_deconstruct_target_model_multiple_literalsc              	   C   sR   ddddg}t jj|d}t }| d ||}W 5 Q R X | || d S r  )r   r  r  r   rZ  r   rR  r  r  r(   r(   r)   r	    s    z(TestPageChooserBlock.test_bulk_to_pythonc                 C   sV   t  }tjjdd}| t||tt|j	ddfg | t|d g  d S )Nr
  r  r   )
r   rZ  r   r  r-  r  rG  rH  r=   r  r  r(   r(   r)   rJ    s    z,TestPageChooserBlock.test_extract_referencesN)r5   r6   r7   rO  r  r  r  r  r  r  r  r  r  rX  r  r  r  r  r  r  r  r  r  r  r	  rJ  r(   r(   r(   r)   r  O  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d Z	dd Z
dd Zdd ZdS )TestStaticBlockc                 C   sP   t jddd}|d t |}| |d d | |d dddd	 d S )
N[Latest posts - This block doesn't need to be configured, it will be displayed automatically$tests/blocks/posts_static_block.html
admin_textrR   posts_static_blockr   rX   rZ   Posts static blockr9   r`   r]   r   StaticBlockrd   r   re   rL   rg   r(   r(   r)   test_adapt_with_constructor*  s    
z+TestStaticBlock.test_adapt_with_constructorc                 C   sZ   G dd dt j}| }|d t |}| |d d | |d dddd	 d S )
Nc                   @   s   e Zd ZG dd dZdS )zBTestStaticBlock.test_adapt_with_subclass.<locals>.PostsStaticBlockc                   @   s   e Zd ZdZdZdS )zGTestStaticBlock.test_adapt_with_subclass.<locals>.PostsStaticBlock.Metar  r  N)r5   r6   r7   r  rR   r(   r(   r(   r)   r  ?  s   r  Nr  r(   r(   r(   r)   PostsStaticBlock>  s   r  r  r   rX   r  rZ   r  r  r  )r2   r  r'   re   r(   r(   r)   test_adapt_with_subclass=  s    
z(TestStaticBlock.test_adapt_with_subclassc                 C   sZ   G dd dt j}| }|d t |}| |d d | |d dddd	 d S )
Nc                   @   s   e Zd ZG dd dZdS )zmTestStaticBlock.test_adapt_with_subclass_displays_default_text_if_no_admin_text.<locals>.LabelOnlyStaticBlockc                   @   s   e Zd ZdZdS )zrTestStaticBlock.test_adapt_with_subclass_displays_default_text_if_no_admin_text.<locals>.LabelOnlyStaticBlock.MetaLatest postsNr  r(   r(   r(   r)   r  T  s   r  Nr  r(   r(   r(   r)   LabelOnlyStaticBlockS  s   r  r  r   rX   z(Latest posts: this block has no options.rZ   r  r  r  )r2   r  r'   re   r(   r(   r)   ?test_adapt_with_subclass_displays_default_text_if_no_admin_textR  s    
zOTestStaticBlock.test_adapt_with_subclass_displays_default_text_if_no_admin_textc                 C   sZ   G dd dt j}| }|d t |}| |d d | |d dddd	 d S )
Nc                   @   s   e Zd ZdS )zwTestStaticBlock.test_adapt_with_subclass_displays_default_text_if_no_admin_text_and_no_label.<locals>.NoMetaStaticBlockNr  r(   r(   r(   r)   NoMetaStaticBlocki  s   r  r  r   rX   z.Posts static block: this block has no options.rZ   r  r  r  )r2   r  r'   re   r(   r(   r)   Ltest_adapt_with_subclass_displays_default_text_if_no_admin_text_and_no_labelf  s    
z\TestStaticBlock.test_adapt_with_subclass_displays_default_text_if_no_admin_text_and_no_labelc                 C   sT   t jtddd}|d t |}| |d d | |d dddd	 d S )
Nzb<b>Latest posts</b> - This block doesn't need to be configured, it will be displayed automaticallyr  r  r  r   rX   rZ   r  )rN   r`   r]   )r   r  r   rd   r   re   rL   rg   r(   r(   r)   test_adapt_works_with_mark_safe{  s     
z/TestStaticBlock.test_adapt_works_with_mark_safec                 C   s   t  }| }| | d S r<   )r   r  r  r   )r2   r'   r  r(   r(   r)   r.    s    z TestStaticBlock.test_get_defaultc                 C   s&   t jdd}|d }| |d d S )Nr  rQ   z <p>PostsStaticBlock template</p>)r   r  rK   rL   r:  r(   r(   r)   r     s    
zTestStaticBlock.test_renderc                 C   s    t  }|d }| | d S r<   )r   r  r   r   r:  r(   r(   r)   r    s    
zTestStaticBlock.test_serializec                 C   s    t  }|d }| | d S r<   )r   r  r   r   r:  r(   r(   r)   r    s    
z TestStaticBlock.test_deserializec                 C   s   |  t d dS )zZ
        StaticBlock.normalize always returns None, as a StaticBlock has no value
           N)r   r   r  rD   r   r(   r(   r)   rF     s    zTestStaticBlock.test_normalizeN)r5   r6   r7   r  r  r  r  r  r.  r   r  r  rF   r(   r(   r(   r)   r  )  s   r  c                   @   s   e Zd Zdd Zdd ZdS )TestDateBlockc              
   C   s   ddl m} t }|d t |}| |d d | |d | | |d j	d | |d ddd	d
dddid d S )Nr   )AdminDateInputtest_dateblockrX   zY-m-drY   zTest dateblockTr  z5w-field w-field--date_field w-field--admin_date_inputr[   r\   r   )
wagtail.admin.widgets.datetimer  r   r  rd   r   re   rL   rf   	js_format)r2   r  r'   re   r(   r(   r)   r    s"    
zTestDateBlock.test_adaptc                 C   s8   t jdd}|d t |}| |d jd d S )Nz%d.%m.%Yformatr  rX   zd.m.Y)r   r  rd   r   re   rL   r  rg   r(   r(   r)   test_adapt_with_format  s    
z$TestDateBlock.test_adapt_with_formatNr5   r6   r7   r  r  r(   r(   r(   r)   r    s   r  c                   @   s   e Zd Zdd Zdd ZdS )TestTimeBlockc              
   C   s   ddl m} t }|d t |}| |d d | |d | | |d j	d | |d ddd	d
dddid d S )Nr   )AdminTimeInputtest_timeblockrX   zH:irY   zTest timeblockTtimez5w-field w-field--time_field w-field--admin_time_inputr[   r\   r   )
r  r  r   	TimeBlockrd   r   re   rL   rf   r  )r2   r  r'   re   r(   r(   r)   r    s"    
zTestTimeBlock.test_adaptc                 C   s8   t jdd}|d t |}| |d jd d S )Nz%H:%M:%Sr  r  rX   zH:i:s)r   r  rd   r   re   rL   r  rg   r(   r(   r)   r    s    
z$TestTimeBlock.test_adapt_with_formatNr  r(   r(   r(   r)   r    s   r  c                   @   s   e Zd Zdd Zdd ZdS )TestDateTimeBlockc              
   C   s   ddl m} t }|d t |}| |d d | |d | | |d j	d | |d ddd	d
dddid d S )Nr   )AdminDateTimeInputtest_datetimeblockrX   z	Y-m-d H:irY   zTest datetimeblockTr  z?w-field w-field--date_time_field w-field--admin_date_time_inputr[   r\   r   )
r  r  r   DateTimeBlockrd   r   re   rL   rf   r  )r2   r  r'   re   r(   r(   r)   r    s"    
zTestDateTimeBlock.test_adaptc                 C   s8   t jdd}|d t |}| |d jd d S )Nz%d.%m.%Y %H:%Mr  r   rX   z	d.m.Y H:i)r   r  rd   r   re   rL   r  rg   r(   r(   r)   r    s    
z(TestDateTimeBlock.test_adapt_with_formatNr  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 )TestSystemCheckc                 C   sv   t dt  fdt  fg}| }| t|d | |d jd | |d jd | |d j	|j
d  d S )Nr  z	rich+textrX   r   wagtailcore.E001zBlock names should follow standard Python conventions for variable names: alphanumeric and underscores, and cannot begin with a digitr   rx  r8   r  checkrL   r   r  hintrE   r  r2   r'   errorsr(   r(   r)   )test_name_cannot_contain_non_alphanumeric  s    

z9TestSystemCheck.test_name_cannot_contain_non_alphanumericc                 C   sv   t dt  fdt  fg}| }| t|d | |d jd | |d jd | |d j	|j
d  d S )Nr  r   rX   r   r  zBlock name cannot be emptyr  r  r(   r(   r)   test_name_must_be_nonempty   s    

z*TestSystemCheck.test_name_must_be_nonemptyc                 C   sv   t dt  fdt  fg}| }| t|d | |d jd | |d jd | |d j	|j
d  d S )Nr  	rich textrX   r   r  !Block names cannot contain spacesr  r  r(   r(   r)   test_name_cannot_contain_spaces.  s    

z/TestSystemCheck.test_name_cannot_contain_spacesc                 C   sv   t dt  fdt  fg}| }| t|d | |d jd | |d jd | |d j	|j
d  d S )Nr  z	rich-textrX   r   r  z!Block names cannot contain dashesr  r  r(   r(   r)   test_name_cannot_contain_dashes<  s    

z/TestSystemCheck.test_name_cannot_contain_dashesc                 C   sv   t dt  fdt  fg}| }| t|d | |d jd | |d jd | |d j	|j
d  d S )Nr  Z
99richtextrX   r   r  z%Block names cannot begin with a digitr  r  r(   r(   r)   !test_name_cannot_begin_with_digitJ  s    

z1TestSystemCheck.test_name_cannot_begin_with_digitc              
   C   s   t  }t dt t dt  fd|fgfg}| }| t|d | |d j	d | |d j
d | |d j| d S )NZparagraph_listr  r  rX   r   r  r  )r   r  rx  rL  r  r8   r  rL   r   r  r  rE   r2   Zfailing_blockr'   r  r(   r(   r)   %test_system_checks_recurse_into_listsX  s$    
z5TestSystemCheck.test_system_checks_recurse_into_listsc                 C   s   t  }t dt dt dt  fd|fgfgfg}| }| t|d | |d jd | |d j	d | |d j
| d S )	NZcarouselr9   r  r  rX   r   r  r  r   r  rx  r  r8   r  rL   r   r  r  rE   r  r(   r(   r)   'test_system_checks_recurse_into_streamsp  s*    
z7TestSystemCheck.test_system_checks_recurse_into_streamsc                 C   s   t  }t  }t dt dt dt  fd|fgfdt dt  fd|fgfgfg}| }| t|d | |d jd | |d j	d	 | |d j
| | |d
 jd | |d
 j	d	 | |d
 j
| d S )NZ
two_columnr{  r  r  r|  rY   r   r  r  rX   r  )r2   Zfailing_block_1Zfailing_block_2r'   r  r(   r(   r)   'test_system_checks_recurse_into_structs  s@    


z7TestSystemCheck.test_system_checks_recurse_into_structsN)r5   r6   r7   r	  r
  r  r  r  r  r  r  r(   r(   r(   r)   r    s   r  c                   @   s"   e Zd Zdd Zejdd ZdS )TestTemplateRenderingc                 C   s:   t  }|ddd}ddi}|||}| |d d S )Nr+  http://torchbox.com/r  r   ra   Z	importantz=<a href="http://torchbox.com/" class="important">Torchbox</a>)CustomLinkBlockr   rK   rL   )r2   r'   r$   rq   r)  r(   r(   r)   test_render_with_custom_context  s     z5TestTemplateRendering.test_render_with_custom_contextc                 C   s@   t  }|ddd}|j|dd}| d| | d| d S )Nr+  r  r  zmy-link-blockr  zdata-prefix="my-link-block"z#<p>Hello from get_form_context!</p>)r  r   Zrender_formrk   r(  r(   r(   r)   $test_render_with_custom_form_context  s
    z:TestTemplateRendering.test_render_with_custom_form_contextN)r5   r6   r7   r  unittestZexpectedFailurer  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 )TestIncludeBlockTagc                 C   s6   t jdd}|d}td|dd}| d| dS )	z
        The include_block tag should be able to render a BoundBlock's template
        while keeping the parent template's context
        rP   rQ   r  $tests/blocks/include_block_test.htmlro   rW   rn   '<body><h1 lang="fr">bonjour</h1></body>Nr   r8   bindr   rk   r2   r'   Zbound_blockr)  r(   r(   r)   &test_include_block_tag_with_boundblock  s    
z:TestIncludeBlockTag.test_include_block_tag_with_boundblockc                 C   s6   t  }|ddd}td|dd}| d| dS )	z
        The include_block tag should be able to render a StructValue's template
        while keeping the parent template's context
        rb  rc  r  r  ro   r  z<<body><h1 lang="fr">Bonjour</h1>monde <i>italique</i></body>N)r   r   r   rk   )r2   r'   r  r)  r(   r(   r)   'test_include_block_tag_with_structvalue  s     z;TestIncludeBlockTag.test_include_block_tag_with_structvaluec                 C   sX   t jdt jddfdt  fgdd}|dddg}td|d	d
}| d| dS )z
        The include_block tag should be able to render a StreamValue's template
        while keeping the parent template's context
        r  rP   rQ   r  z&tests/blocks/stream_with_language.htmlrb  r(  r  ro   r  z?<div class="heading" lang="fr"><h1 lang="fr">Bonjour</h1></div>N)r   rx  r8   r   r   rk   )r2   r'   r  r)  r(   r(   r)   'test_include_block_tag_with_streamvalue  s&    

 z;TestIncludeBlockTag.test_include_block_tag_with_streamvaluec                 C   s   t dddi}| d| dS )z
        The include_block tag should be able to render a value without a render_as_block method
        by just rendering it as a string
        r  rW   rs  z<body>42</body>N)r   rk   )r2   r)  r(   r(   r)   'test_include_block_tag_with_plain_value  s     z;TestIncludeBlockTag.test_include_block_tag_with_plain_valuec                 C   sR   t jdd}|d}td|dd}| d| tdddd}| d	| dS )
z
        The block parameter on include_block tag should support complex values including filters,
        e.g. {% include_block foo|default:123 %}
        rP   rQ   r  z0tests/blocks/include_block_test_with_filter.htmlro   r  r  Nz<body>999</body>r   r"  r(   r(   r)   *test_include_block_tag_with_filtered_value!  s     
z>TestIncludeBlockTag.test_include_block_tag_with_filtered_valuec                 C   s6   t jdd}|d}td|dd}| d| dS )	z
        Test that it's possible to pass extra context on an include_block tag using
        {% include_block foo with classname="bar" %}
        rP   rQ   r  z)tests/blocks/include_block_with_test.htmlro   r  z9<body><h1 lang="fr" class="important">bonjour</h1></body>Nr   r"  r(   r(   r)   )test_include_block_tag_with_extra_context;  s    
 z=TestIncludeBlockTag.test_include_block_tag_with_extra_contextc                 C   s6   t jdd}|d}td|dd}| d| dS )	z
        A tag such as {% include_block foo with classname="bar" only %}
        should not inherit the parent context
        rP   rQ   r  z)tests/blocks/include_block_only_test.htmlro   r  z/<body><h1 class="important">bonjour</h1></body>Nr   r"  r(   r(   r)   %test_include_block_tag_with_only_flagN  s    
z9TestIncludeBlockTag.test_include_block_tag_with_only_flagc                 C   s   t  }||d}tdd|i}| d| tdd|i}| d| tdddi}| d| tdddi}| d| t  }||d}tdd|i}| d| tddtdi}| d| dS )z\
        Output of include_block should be escaped as per Django autoescaping rules
        zsome <em>evil</em> HTMLr  rW   z0<body>some &lt;em&gt;evil&lt;/em&gt; HTML</body>z3tests/blocks/include_block_autoescape_off_test.htmlz$<body>some <em>evil</em> HTML</body>N)r   r8   r!  r   r   rk   r  r   r"  r(   r(   r)    test_include_block_html_escaping_  s\          z4TestIncludeBlockTag.test_include_block_html_escapingN)r5   r6   r7   r#  r$  r%  r&  r'  r(  r)  r*  r(   r(   r(   r)   r    s   r  c                   @   s   e Zd Zdd Zdd ZdS )!TestOverriddenGetTemplateBlockTagc              	   C   sN   G dd dt j}|dd}| td |d}W 5 Q R X | |d d S )Nc                   @   s   e Zd ZdZdddZdS )zfTestOverriddenGetTemplateBlockTag.test_get_template_old_signature.<locals>.BlockUsingGetTemplateMethodrP   Nc                 S   s   | j S r<   )my_new_template)r2   rq   r(   r(   r)   get_template  s    zsTestOverriddenGetTemplateBlockTag.test_get_template_old_signature.<locals>.BlockUsingGetTemplateMethod.get_template)N)r5   r6   r7   r,  r-  r(   r(   r(   r)   BlockUsingGetTemplateMethod  s   r.  z'tests/blocks/this_shouldnt_be_used.htmlrQ   z[BlockUsingGetTemplateMethod.get_template should accept a 'value' argument as first argumentHello Worldz<h1>Hello World</h1>)r   rC   ZassertWarnsMessager    rK   rL   )r2   r.  r'   rN   r(   r(   r)   test_get_template_old_signature  s    zATestOverriddenGetTemplateBlockTag.test_get_template_old_signaturec                 C   sH   G dd dt j}| }|d}| |d |d}| |d dS )z8verifies Block.render() passes the value to get_templatec                   @   s   e Zd ZdddZdS )zTestOverriddenGetTemplateBlockTag.test_block_render_passes_the_value_argument_to_get_template.<locals>.BlockChoosingTemplateBasedOnValueNc                 S   s   |dkrdS d S )NHEADINGrP   r(   r  r(   r(   r)   r-    s    zTestOverriddenGetTemplateBlockTag.test_block_render_passes_the_value_argument_to_get_template.<locals>.BlockChoosingTemplateBasedOnValue.get_template)NN)r5   r6   r7   r-  r(   r(   r(   r)   !BlockChoosingTemplateBasedOnValue  s   r2  r/  r1  z<h1>HEADING</h1>N)r   rC   rK   rL   )r2   r2  r'   rN   r(   r(   r)   ;test_block_render_passes_the_value_argument_to_get_template  s    

z]TestOverriddenGetTemplateBlockTag.test_block_render_passes_the_value_argument_to_get_templateN)r5   r6   r7   r0  r3  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d Zdd Zdd Zdd ZdS )TestValidationErrorAsJsonDatac                 C   s"   t d}| t|ddgi d S )Neverything is brokenr  r   rL   r   r2   r0   r(   r(   r)   test_plain_validation_error  s
     z9TestValidationErrorAsJsonData.test_plain_validation_errorc                 C   s0   t t dt dg}| t|dddgi d S )Nr5  zeven more broken than beforer  r6  r7  r(   r(   r)   ,test_validation_error_with_multiple_messages  s    
zJTestValidationErrorAsJsonData.test_validation_error_with_multiple_messagesc                 C   sJ   t dttdgittdgd}| t|dddgiidgd d S )Nr;  r
  3Either email or telephone number must be specified.block_errorsr,   r  rB  r  )r   r   r   rL   r   r7  r(   r(   r)   !test_structblock_validation_error  s(      z?TestValidationErrorAsJsonData.test_structblock_validation_errorc                 C   s*   t tdgd}| t|ddgi d S )Nr:  r+   r  r   r   rL   r   r7  r(   r(   r)   6test_structblock_validation_error_with_no_block_errors  s    zTTestValidationErrorAsJsonData.test_structblock_validation_error_with_no_block_errorsc                 C   s4   t dtdid}| t|ddddgiii d S )Nr;  r
  r<  rB  r  r?  r7  r(   r(   r)   :test_structblock_validation_error_with_no_non_block_errors  s      zXTestValidationErrorAsJsonData.test_structblock_validation_error_with_no_non_block_errorsc              	   C   s   t ttttdgdgttdgttdttdgidgdttdtdgd	}| t|d
dgid
dgiddd
dgiiidddgd d S )Nr:  r+   r
  r;  rA  )rY   r  ru  r   The maximum number of items is 5r;  r  rB  r=  )r   r   r   r   rL   r   r7  r(   r(   r)   !test_streamblock_validation_error  sd     # z?TestValidationErrorAsJsonData.test_streamblock_validation_errorc                 C   s*   t tdgd}| t|ddgi d S Nr  r+   r  r   r   rL   r   r7  r(   r(   r)   6test_streamblock_validation_error_with_no_block_errorsN  s    zTTestValidationErrorAsJsonData.test_streamblock_validation_error_with_no_block_errorsc                 C   sD   t tdgtddd}| t|dddgiddgidi d S )Nr
  )r  ru  rA  rB  r  rF  r7  r(   r(   r)   :test_streamblock_validation_error_with_no_non_block_errors]  s    zXTestValidationErrorAsJsonData.test_streamblock_validation_error_with_no_non_block_errorsc              	   C   s   t d ttttdgdgttdttdgidggttdtdgd}| t|d	dgid
dd	dgiiidddgd d S )Nr:  r+   r;  r
  rA  r  rC  r;  r  rB  rX   rY   r=  )r   r   r   r   rL   r   r7  r(   r(   r)   5test_listblock_validation_error_constructed_with_listn  sb     # zSTestValidationErrorAsJsonData.test_listblock_validation_error_constructed_with_listc                 C   s   t tttdgdgtdttdgiddttdtdgd	}| t|d
dgiddd
dgiiidddgd d S )Nr:  r+   r;  r
  rA  rI  r  rC  r;  r  rB  r=  )r   r   r   r   rL   r   r7  r(   r(   r)   5test_listblock_validation_error_constructed_with_dict  sR      zSTestValidationErrorAsJsonData.test_listblock_validation_error_constructed_with_dictc                 C   s4   t dtdid}| t|ddddgiii d S )NrY   r
  rA  rB  r  r   r   rL   r   r7  r(   r(   r)   8test_listblock_validation_error_with_no_non_block_errors  s    
 zVTestValidationErrorAsJsonData.test_listblock_validation_error_with_no_non_block_errorsc                 C   s*   t tdgd}| t|ddgi d S rE  rL  r7  r(   r(   r)   4test_listblock_validation_error_with_no_block_errors  s    zRTestValidationErrorAsJsonData.test_listblock_validation_error_with_no_block_errorsN)r5   r6   r7   r8  r9  r>  r@  rB  rD  rG  rH  rJ  rK  rM  rN  r(   r(   r(   r)   r4    s   =?6r4  c                   @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
TestBlockDefinitionLookupc                 C   s   t dg ddifdg i fd}|d}|d | |tj | |j |d}| |tj |d}|d	 | |tj | |j | 	|| | 
|jd | 
|jd	 d S )
Nwagtail.blocks.CharBlockr_   Twagtail.blocks.RichTextBlockr   rX   r   r  rX   subtitle)r   	get_blockrd   rf   r   r8   r  r_   r  rk  rL   r;  )r2   lookupZ
char_blockZrich_text_blockZchar_block_2r(   r(   r)   test_simple_lookup  s$    




z,TestBlockDefinitionLookup.test_simple_lookupc                 C   s   t dg ddifdg i fdddggi fd}|d	}| |tj |jd
 }| |tj | |j |jd }| |tj	 d S )NrP  r_   TrQ  zwagtail.blocks.StructBlock)r  r   )descriptionrX   r   rX   rY   rY   r  rW  )
r   rT  rf   r   r  r  r8   r  r_   r  )r2   rU  Zstruct_blocktitle_blockdescription_blockr(   r(   r)   test_structblock_lookup  s&    


z1TestBlockDefinitionLookup.test_structblock_lookupc                 C   s   t dg ddifdg i fdddggi fd}|d	}| |tj |jd
 }| |tj | |j |jd }| |tj	 d S )NrP  r_   TrQ  zwagtail.blocks.StreamBlock)r  r   )r  rX   rX  rY   r  r  )
r   rT  rf   r   rx  r  r8   r  r_   r  )r2   rU  r  rY  rZ  r(   r(   r)   test_streamblock_lookup3  s&    


z1TestBlockDefinitionLookup.test_streamblock_lookupc                 C   s   t dg ddifddgi fd}|d}| |tj |j}| |tj | |j t ddtjgi fi}|d}| |tj |j}| |tj d S )NrP  r_   Tzwagtail.blocks.ListBlockr   rR  rX   )	r   rT  rf   r   rL  r  r8   r  r_   )r2   rU  rz  Zlist_item_blockr(   r(   r)   test_listblock_lookupL  s&    

 
z/TestBlockDefinitionLookup.test_listblock_lookupN)r5   r6   r7   rV  r[  r\  r]  r(   r(   r(   r)   rO     s   rO  )ar   r  rj  r   r  decimalr   djangor   Zdjango.core.exceptionsr   Zdjango.core.serializers.jsonr   Zdjango.forms.utilsr   Zdjango.template.loaderr   Zdjango.testr	   r
   Zdjango.utils.safestringr   r   Zdjango.utils.translationr   r  r  r   Zwagtail.blocks.baser   Z wagtail.blocks.definition_lookupr   Zwagtail.blocks.field_blockr   Zwagtail.blocks.list_blockr   r   Zwagtail.blocks.static_blockr   Zwagtail.blocks.stream_blockr   r   Zwagtail.blocks.struct_blockr   r   Zwagtail.modelsr   Zwagtail.rich_textr   Zwagtail.test.testapp.blocksr   r  r   Zwagtail.test.testapp.modelsr   r   Zwagtail.test.utilsr   Zwagtail.utils.deprecationr    rx  r!   r8   r;   r@   rG   r   r   r   r   r   r   r  r  rP  r  r  r  r  r}  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r+  r4  rO  r(   r(   r(   r)   <module>   s    b*	)@< >      rJ    s X    <=         >r>o [ !!! + V%  ;