
    BhH                        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mZmZmZ d dlmZmZmZ d d	lmZ d d
lmZmZmZ d dlmZ d dlm Z m!Z! d dl"m#Z#m$Z$m%Z% d dl&m'Z'  G d de      Z( G d de%      Z) G d de%      Z*y)    )unescape)get_user_model)Site)cache)override)forms)GlobalPagePermissionAdminFormMovePageFormPagePermissionInlineAdminFormPageUserGroupFormViewRestrictionInlineAdminForm)assign_user_to_pagecreate_pagecreate_page_content)PageSelectFormField)get_page_choicesget_site_choicesupdate_site_and_page_choices)ApplicationConfigSelect)ACCESS_PAGEACCESS_PAGE_AND_CHILDREN)URL_CMS_PAGE_ADVANCED_CHANGEURL_CMS_PAGE_PERMISSIONSCMSTestCase)get_current_sitec                       e Zd ZddZy)Mock_PageSelectFormFieldc                 >    || _         i | _        d| j                  d<   y )NInvalid_pageinvalid_page)requirederror_messages)selfr!   s     D/home/dcms/DCMS/lib/python3.12/site-packages/cms/tests/test_forms.py__init__z!Mock_PageSelectFormField.__init__"   s#     ! .<N+    N)F)__name__
__module____qualname__r%    r&   r$   r   r   !   s    =r&   r   c                   r    e Zd Zd Zd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd Zd Zd Zd Zd Zy)FormsTestCasec                 ,    t        j                          y N)r   clearr#   s    r$   setUpzFormsTestCase.setUp+   s    r&   c                 <    t               }| j                  |g        y r.   r   assertEqualr#   results     r$   test_get_site_choicesz#FormsTestCase.test_get_site_choices.       !#$r&   c                 >    t               }| j                  |dg       y )N z----)r   r4   r5   s     r$   test_get_page_choicesz#FormsTestCase.test_get_page_choices2   s    !#,0r&   c           
      2   t               }t        ddd      t        ddd      t        ddd      t        ddd      g}d|j                  |D cg c]!  }|j                  |j	                  dd	      f# c}fg}| j                  t        d      |       y
c c}w )z>
        The page choices should always use draft ids
        0001nav_playground.htmlen000200030004r:   FfallbackN)r   r   namepk	get_titleassertSequenceEqualr   )r#   sitepagespageexpecteds        r$   test_page_choices_draft_onlyz*FormsTestCase.test_page_choices_draft_only6   s      ! 5t< 5t< 5t< 5t<	
 !499kp.qcgW\9]/^.q"rs  !1$!7B /rs   &Bc                    t               }t        ddd      t        ddd      t        ddd      t        ddd      g}ddg}|D ](  }|D ]!  }|j                  d      }t        |||	       # * dg|z   D ]k  }d
|j                  |D cg c]!  }|j
                  |j                  |d      f# c}fg}t        |      5  | j                  t               |       d d d        m y c c}w # 1 sw Y   }xY w)Nr>   r?   r@   rA   rB   rC   defr)rL   r:   FrD   )	r   r   rH   r   rF   rG   force_languagerI   r   )r#   rJ   rK   	languagesrL   languagetitlerM   s           r$   0test_get_page_choices_with_multiple_translationsz>FormsTestCase.test_get_page_choices_with_multiple_translationsE   s"   ! 5t< 5t< 5t< 5t<	
 4L	 	@D% @t,#He$?@	@
 * 	GH]bcUYdggt~~h~'OPcdH
  ) G(()9);XFG G	G dG Gs   &C#
=C((C1	c                 <    t               }| j                  |g        y r.   r3   r5   s     r$   'test_get_site_choices_without_moderatorz5FormsTestCase.test_get_site_choices_without_moderator]   r8   r&   c                    t               }t        dddd      }|j                  dk7  rd||j                  <    |di |}|j                  t	        ||j                               |j                          | j                  |      5  t        ddd|	       t               }| j                  |d
g       d d d        y # 1 sw Y   y xY w)NTsuper@super.comis_staff	is_activeis_superuseremailr_   superhomer?   r@   
created_by)   zexample.comr*   )
r   dictUSERNAME_FIELDset_passwordgetattrsavelogin_user_contextr   r   r4   )r#   Userfields
user_superr6   s        r$   6test_get_site_choices_without_moderator_with_superuserzDFormsTestCase.test_get_site_choices_without_moderator_with_superusera   s    tt$N_`')*1F4&&'^F^

D4G4G HI$$Z0 	; 5t
S%'FV&8%9:		; 	; 	;s   -B<<Cc                     d}	 t        d      }d}|j                  |       | j                  d       | j                  |       y # t        j                  $ r d}Y )w xY w)NFTr!   r   Nzcompress function didn't raise!)r   compressfailr   ValidationError
assertTrue)r#   raised
fake_field	data_lists       r$   /test_compress_function_raises_when_page_is_nonez=FormsTestCase.test_compress_function_raises_when_page_is_nones   s`    	14@J!I	*II78 	 $$ 	F	s   0A AAc                 f    t        d      }d}|j                  |      }| j                  |d        y )NFrp   rq   r   rr   r4   r#   rw   rx   r6   s       r$   5test_compress_function_returns_none_when_not_requiredzCFormsTestCase.test_compress_function_returns_none_when_not_required~   s1    -u=
	$$Y/&r&   c                 f    t        d      }d }|j                  |      }| j                  |d        y )NFrp   r{   r|   s       r$   5test_compress_function_returns_none_when_no_data_listzCFormsTestCase.test_compress_function_returns_none_when_no_data_list   s1    -u=
	$$Y/&r&   c                    t               }t        dddd      }|j                  dk7  rd||j                  <    |di |}|j                  t	        ||j                               |j                          | j                  |      5  t        ddd|	      }t               }d
|j                  f}|j                  |      }| j                  ||       d d d        y # 1 sw Y   y xY w)NTrZ   r[   r_   r`   ra   r?   r@   rb   r   r*   )r   re   rf   rg   rh   ri   rj   r   r   rG   rr   r4   )r#   rk   rl   rm   	home_pagerw   rx   r6   s           r$   2test_compress_function_gets_a_page_when_one_existsz@FormsTestCase.test_compress_function_gets_a_page_when_one_exists   s    tt$N_`')*1F4&&'^F^

D4G4G HI$$Z0 	0#F,A4T^_I13JILL)I((3FY/	0 	0 	0s   ACC#c           
      @   t         j                  j                         j                          t         j                  j	                  ddd      }t        ddd|      }t        d	dd
|      }t        ddd||      }t        ddd||      }d t        d      D        \  }}| j                  |d|j                  |j                  df|j                  df|j                  df|j                  d	fgfg       | j                  ||j                  |j                  fg       y )Nzhttp://www.django-cms.orgz
Django CMSrd   )domainrF   rG   zPage 1r?   r@   )rJ   zPage 2rP   zPage 3)rJ   parentz+Page 4<script>alert("bad-things");</script>c              3   2   K   | ]  }t        |        y wr.   )list).0bits     r$   	<genexpr>zBFormsTestCase.test_update_site_and_page_choices.<locals>.<genexpr>   s     %^Cd3i%^s   r:   z&nbsp;&nbsp;Page 3zM&nbsp;&nbsp;Page 4&lt;script&gt;alert(&quot;bad-things&quot;);&lt;/script&gt;)
r   objectsalldeletecreater   r   r4   rF   rG   )r#   rJ   page1page2page3page4site_choicespage_choicess           r$   !test_update_site_and_page_choicesz/FormsTestCase.test_update_site_and_page_choices   s   !!#||""*EL]^"_H&;TMH&;TMH&;TUZ[9;PRV]ajo
 &_;WX\;]%^"lII8,#78#rs8,		
 	$))(<'=>r&   c                 $    G d d      } G d d      } |d |dd       |dd	      g      } |d
 |dd       |dd      g      }||||i}t        |      }|j                  dd      }| j                  d|v        | j                  d|v        y )Nc                       e Zd Zd Zd Zy)DFormsTestCase.test_app_config_select_escaping.<locals>.FakeAppConfigc                      || _         || _        y r.   )rG   config)r#   rG   r   s      r$   r%   zMFormsTestCase.test_app_config_select_escaping.<locals>.FakeAppConfig.__init__   s    $r&   c                     | j                   S r.   )r   r0   s    r$   __str__zLFormsTestCase.test_app_config_select_escaping.<locals>.FakeAppConfig.__str__   s    {{"r&   N)r'   r(   r)   r%   r   r*   r&   r$   FakeAppConfigr      s    %#r&   r   c                   &    e Zd ZddZd Zd Zd Zy)>FormsTestCase.test_app_config_select_escaping.<locals>.FakeAppc                      || _         || _        y r.   )rF   configs)r#   rF   r   s      r$   r%   zGFormsTestCase.test_app_config_select_escaping.<locals>.FakeApp.__init__   s     	&r&   c                     | j                   S r.   )rF   r0   s    r$   r   zFFormsTestCase.test_app_config_select_escaping.<locals>.FakeApp.__str__   s    yy r&   c                     | j                   S r.   )r   r0   s    r$   get_configszJFormsTestCase.test_app_config_select_escaping.<locals>.FakeApp.get_configs   s    ||#r&   c                      y)Nz
/fake/url/r*   r0   s    r$   get_config_add_urlzQFormsTestCase.test_app_config_select_escaping.<locals>.FakeApp.get_config_add_url   s    #r&   N)r*   )r'   r(   r)   r%   r   r   r   r*   r&   r$   FakeAppr      s    '!$$r&   r   GoodApprd   zgood-app-one-config   zgood-app-two-configBadAppzbad-app-one-configz6bad-app-two-config<script>alert("bad-stuff");</script>)app_configsapplication_configurationsz$<script>alert("bad-stuff");</script>z:\u003Cscript\u003Ealert(\"bad-stuff\");\u003C/script\u003E)r   renderassertFalseru   )r#   r   r   r   r   r   app_config_selectoutputs           r$   test_app_config_select_escapingz-FormsTestCase.test_app_config_select_escaping   s    	# 	#	$ 	$ a!67a!67
 a!56a!YZ
 WF

 4L"))*FJ?6IJZ^dder&   c                 4   t               }t        ddd      }t        ddd      }t        ddd|      }|j                  d|j                  d}t        j                  |||	      }| j                  |j                         |j                         t        d
dd|      }|j                  d|j                  d}t        j                  |||	      }| j                  |j                         |j                         t        ddd|      }|j                  d|j                  d}t        j                  |||	      }| j                  |j                                | j                  dt        |j                  d                t        ddd      }	|	j                          |j                  d|j                  d}t        j                  ||	|	      }| j                  |j                                | j                  dt        |j                  d                y)z-Test the MovePageForm validation and behaviorParent 1r?   r@   Parent 2Child 1r   0targetpositionrJ   datarL   rJ   Child 2,You cannot have two pages with the same slug__all__Homez5You can&#x27;t move the home page inside another pager   N)r   r   rG   r   r
   ru   is_validerrorsr   assertInstrset_as_homepage)
r#   rJ   parent1parent2child1r   formchild2child3homepages
             r$   test_move_page_formz!FormsTestCase.test_move_page_form   s   ! j*?Fj*?FY(=tGT jjGG

 !!t&tD5 Y(=tGTjjGG

 !!t&tD5 Y(=tGTjjGG

 !!t&tD)Dc$++V_J`Fab v'<dC  "jjGG

 !!t(F)MsSWS^S^_gShOijr&   c                 J   t               }t        ddd      }t        ddd|      }t        ddd|      }d}|D ]g  \  }}|j                  ||j                  d}t        j                  |||	      }	| j                  |	j                         d
| d|	j                          i y)z/Test different position options in MovePageFormParentr?   r@   r   r   r   ))r   zfirst-child)1z
last-child)2left)3rightr   r   z"Form should be valid for position z
, errors: N)r   r   rG   r   r
   ru   r   r   )
r#   rJ   r   r   r   	positionsposition_idposition_namer   r   s
             r$   test_move_page_form_positionsz+FormsTestCase.test_move_page_form_positions   s    !X'<dCY(=tFSY(=tFS
	 +4 		&K ))'D
 %%4f4HDOO#Em_T^_c_j_j^k!l		r&   c                    t               }t        dddd      }t        dddd      }t        dddd	|
       t        dddd	|
      }|j                  d|j                  d}t        j                  |||      }|j                         }| j                  |d       | j                  dt        |j                  d         d       y)KTest that pages cannot be moved to create duplicate slugs at the same levelr   r?   r@   zparent-1slugr   zparent-2z	Test Pagetestr   r   r   r   r   ,Form should be invalid due to slug collisionr   r   'Form should report slug collision errorN
r   r   rG   r   r
   r   r   r   r   r   )r#   rJ   r   r   r   r   r   r   s           r$   *test_move_page_form_sibling_slug_collisionz8FormsTestCase.test_move_page_form_sibling_slug_collision9  s    ! j*?JWj*?JW 	K!66RYZ [*?F[bc jjGG
 !!t&tD==?#QR:I&'5	
r&   c                 p   t               }t        dddd      }t        dddd|      }t        d	ddd|      }|j                  d
|j                  d}t        j                  |||      }|j                         }| j                  |d       | j                  dt        |j                  d         d       y)r   Grandparentr?   r@   grandparentr   r   r   r   Childr   r   r   r   r   r   r   Nr   )r#   rJ   r   r   childr   r   r   s           r$   /test_move_page_form_child_parent_slug_collisionz=FormsTestCase.test_move_page_form_child_parent_slug_collisionX  s    ! "-1FS`a X'<dZef G%:DxX^_ "nnGG
 !!t%dC==?#QR:I&'5	
r&   N)r'   r(   r)   r1   r7   r<   rN   rV   rX   rn   ry   r}   r   r   r   r   r   r   r   r   r*   r&   r$   r,   r,   *   s]    %1CG0%;$	 ''0*?:/fb1kf2
>
r&   r,   c                       e Zd Zd Zd Zd Zy)PermissionFormTestCasec           	         t        ddd| j                               }| j                  ddd      }t        ||dd       | j	                  | j                               5  | j
                  j                  t        |j                  z        }| j                  |j                  d	       | j
                  j                  t        |j                  z        }| j                  |j                  d	       d d d        | j                  d
      5  |j                  d|j                  d}t        |d       }| j                  |j                                |j                  t         |j                  d}t        |d       }| j#                  |j                                |j%                          |j                  t&        dd|j                  d}t        |d       }d}| j                  |j                                | j#                  |t)        t+        |j,                              v        |j                  t         dd|j                  d}t        |d       }| j                  |j                                | j#                  dt)        t+        |j,                              v        d d d        y # 1 sw Y   xY w# 1 sw Y   y xY w)Npage_br?   r@   rb   
randomuserTr\   add_default_permissions)can_view
can_change   )CMS_RAW_ID_USERShello)rL   grant_onuserr   filesr   r;   )rL   r   can_addr   r   zm<li>Users can't create a page without permissions to change the created page. Edit permissions required.</li>z<li>Add page permission requires also access to children, or descendants, otherwise added page can't be changed by its creator.</li>)r   get_superuser_create_userr   rj   clientgetr   rG   r4   status_coder   settingsr   r   r   r   ru   ri   r   r   r   r   )r#   rL   normal_userresponser   r   error_messages          r$   test_permission_formsz,PermissionFormTestCase.test_permission_formsx  sM   8%:DTM_M_Mab''t]a'bD+N$$T%7%7%9: 	8{{'Cdgg'MNHX1137{{'?$'''IJHX1137		8 ]]D]1 -	##D
 1d$GDT]]_-'#D
 1d$GDOODMMO,IIK 4 #D 1d$GDN  T]]_-OOMXc$++6F-GGH'!#D 1d$GDT]]_-OO #+C,<#=>S-	 -		8 	8-	 -	s    BJ-FJ:-J7:Kc                    | j                  ddd      }| j                         }t        ddd|      }|j                  t        d|j                  d	d
}t        |d       }| j                  |j                                |j                  t        dd	d}t        |d       }| j                  |j                                |j                  t        d|j                  d}t        |d       }| j                  |j                                y )Nr   Tr   r   r?   r@   rb   Truer;   )rL   r   r   r   groupr   )rL   r   r   r  )rL   r   r   r   )
r   r   r   rG   r   r   ru   r   r	   r   )r#   r   current_userrL   r   r   s         r$   test_inlinesz#PermissionFormTestCase.test_inlines  s      VZ [))+8%:D\ZGG0GG
 .4tD(-ESYdfg,$dC) GG0GG	
 -$dC(r&   c                 f   | j                         }ddi}t        |d       }||_        | j                  |j	                         |j
                         |j                         }t        |d |      }||_        | j                  |j	                         |j
                         |j                          y )NrF   
test_groupr   )r   r   instance)r   r   _current_userru   r   r   ri   )r#   r   r   r   r  s        r$   test_user_formsz&PermissionFormTestCase.test_user_forms  s    !!#% d$7!599; d$J!5		r&   N)r'   r(   r)   r  r  r  r*   r&   r$   r   r   w  s    8t)4r&   r   N)+htmlr   django.contrib.authr   django.contrib.sites.modelsr   django.core.cacher   django.utils.translationr   rR   	cms.adminr   cms.admin.formsr	   r
   r   r   r   cms.apir   r   r   cms.forms.fieldsr   cms.forms.utilsr   r   r   cms.forms.widgetsr   
cms.modelsr   r   cms.test_utils.testcasesr   r   r   	cms.utilsr   r   r,   r   r*   r&   r$   <module>r     sx     . , # ?   J I 0 
 6 < 
 '=2 =J
K J
Z
a[ ar&   