11# -*- coding: utf-8 -*-
22
3+ from collective .documentgenerator .browser .table import SubTemplateUsageModelVariableColumn
4+ from collective .documentgenerator .browser .table import SubTemplateColumn
5+ from collective .documentgenerator .browser .table import SubTemplateUsagePathColumn
6+ from collective .documentgenerator .browser .table import SubTemplateUsageTemplatesColumn
37from collective .documentgenerator .testing import PODTemplateIntegrationTest
48from plone import api
59from Products .CMFPlone .utils import _createObjectByType
@@ -16,10 +20,27 @@ def setUp(self):
1620 super (TestSubTemplatesUsageView , self ).setUp ()
1721 self .view = self .portal .restrictedTraverse ("@@sub-templates-usage" )
1822
23+ def _rows (self ):
24+ """Add a second usage group for the demo sub-template and an unused sub-template.
25+
26+ Returns the 3 table rows: the demo sub-template first group, its second
27+ group (a continuation row) and the unused sub-template.
28+ """
29+ pod = self .portal .podtemplates
30+ folder = _createObjectByType ("Folder" , pod , "folder_z" , title = u"Zeta" )
31+ api .content .create (type = "SubTemplate" , id = "st_z" , title = u"In Zeta" , container = folder )
32+ api .content .create (type = "ConfigurablePODTemplate" , id = "in_zeta" , title = u"In Zeta template" ,
33+ container = folder ,
34+ merge_templates = [{"template" : pod .sub_template .UID (),
35+ "pod_context_name" : u"zeta_var" , "do_rendering" : False }])
36+ self .view .update ()
37+ return self .view .table .results
38+
1939 def test_sub_templates_usage (self ):
2040 entries = self .view .sub_templates_usage ()
2141 # one entry per sub-template (here the single demo 'sub_template')
2242 self .assertEqual ([e ["sub_template" ].getId for e in entries ], ["sub_template" ])
43+ self .assertEqual (entries [0 ]["sub_template_uid" ], self .portal .podtemplates .sub_template .UID ())
2344 # the only sub-template defines the whole common path -> empty rel_path
2445 self .assertEqual (entries [0 ]["rel_path" ], u"" )
2546 # both using templates share the same folder -> one group, sorted by title
@@ -49,3 +70,73 @@ def test_rel_path_strips_common_part(self):
4970 # the common 'podtemplates' segment never appears in the displayed path
5071 pod_title = pod .Title ()
5172 self .assertFalse (any (pod_title in rel_path for rel_path in rel_paths ))
73+
74+ def test_update (self ):
75+ rows = self ._rows ()
76+ self .assertEqual ([col .__name__ for col in self .view .table .columns ],
77+ ["SubTemplateColumn" , "SubTemplateUsagePathColumn" , "SubTemplateUsageTemplatesColumn" ,
78+ "SubTemplateUsageModelVariableColumn" ])
79+ # one row per (sub-template, path group), 'first' marking a group start
80+ self .assertEqual ([(r ["sub_template" ].getId , r ["first" ], r ["group" ] is None ) for r in rows ],
81+ [("sub_template" , True , False ), ("sub_template" , False , False ),
82+ ("st_z" , True , True )])
83+ # the whole page renders the table
84+ self .assertIn (u'class="listing nosort sub-templates-usage"' , self .view ())
85+
86+ def test_SubTemplateColumn (self ):
87+ rows = self ._rows ()
88+ column = SubTemplateColumn (self .view .context , self .view .request , self .view .table )
89+ self .assertEqual (column .renderHeadCell (), u"Sous-modèle" )
90+ self .assertEqual (column .renderCell (rows [0 ]),
91+ u'<a href="http://nohost/plone/podtemplates/sub_template">Header</a>' )
92+ # a continuation row leaves the cell empty, as the removed rowspan did
93+ self .assertEqual (column .renderCell (rows [1 ]), u"" )
94+ # a sub-template out of the common path is prefixed by its relative path
95+ self .assertEqual (column .renderCell (rows [2 ]),
96+ u'<span>Zeta</span> / '
97+ u'<a href="http://nohost/plone/podtemplates/folder_z/st_z">In Zeta</a>' )
98+
99+ def test_SubTemplateUsagePathColumn (self ):
100+ rows = self ._rows ()
101+ column = SubTemplateUsagePathColumn (self .view .context , self .view .request , self .view .table )
102+ self .assertEqual (column .renderHeadCell (),
103+ u"Chemin des modèles utilisant ce sous-modèle" )
104+ self .assertEqual (column .renderCell (rows [0 ]), u"POD Templates" )
105+ self .assertEqual (column .renderCell (rows [1 ]), u"POD Templates / Zeta" )
106+ # an unused sub-template has no path to show
107+ self .assertEqual (column .renderCell (rows [2 ]), u"-" )
108+
109+ def test_SubTemplateUsageTemplatesColumn (self ):
110+ rows = self ._rows ()
111+ column = SubTemplateUsageTemplatesColumn (self .view .context , self .view .request , self .view .table )
112+ self .assertEqual (column .renderHeadCell (), u"Modèles concernés" )
113+ self .assertEqual (column .renderCell (rows [0 ]),
114+ u'<ul><li><a href="http://nohost/plone/podtemplates/test_template_bis">'
115+ u'Collection template</a></li>'
116+ u'<li><a href="http://nohost/plone/podtemplates/test_template_multiple">'
117+ u'Multiple format template</a></li></ul>' )
118+ self .assertEqual (column .renderCell (rows [1 ]),
119+ u'<ul><li><a href="http://nohost/plone/podtemplates/folder_z/in_zeta">'
120+ u'In Zeta template</a></li></ul>' )
121+ self .assertEqual (column .renderCell (rows [2 ]), u"<em>Pas utilisé</em>" )
122+ # render_template is the seam imio.dms.mail overrides to flag the signer rules
123+ self .assertEqual (column .render_template (self .portal .podtemplates .test_template ),
124+ u'<a href="http://nohost/plone/podtemplates/test_template">General template</a>' )
125+
126+ def test_SubTemplateUsageModelVariableColumn (self ):
127+ rows = self ._rows ()
128+ column = SubTemplateUsageModelVariableColumn (self .view .context , self .view .request , self .view .table )
129+ self .assertEqual (column .renderHeadCell (), u"Model variable" )
130+ # one variable name per using template, in the same order as the templates column
131+ self .assertEqual (column .renderCell (rows [0 ]), u"<ul><li>header</li><li>header</li></ul>" )
132+ self .assertEqual (column .renderCell (rows [1 ]), u"<ul><li>zeta_var</li></ul>" )
133+ self .assertEqual (column .renderCell (rows [2 ]), u"" )
134+
135+ # _get_pod_context_name finds nothing for a template not merging the sub-template,
136+ # for a template without the field at all and for an unknown uid
137+ pod = self .portal .podtemplates
138+ sub_uid = pod .sub_template .UID ()
139+ self .assertEqual (column ._get_pod_context_name (pod .test_template_multiple , sub_uid ), "header" )
140+ self .assertEqual (column ._get_pod_context_name (pod .test_template , sub_uid ), u"" )
141+ self .assertEqual (column ._get_pod_context_name (pod .test_style_template , sub_uid ), u"" )
142+ self .assertEqual (column ._get_pod_context_name (pod .test_template_multiple , "unknown-uid" ), u"" )
0 commit comments