1 | import re |
---|
2 | import unittest |
---|
3 | |
---|
4 | from Products.PloneTestCase.PloneTestCase import portal_owner |
---|
5 | from Products.PloneTestCase.PloneTestCase import default_password |
---|
6 | |
---|
7 | from collective.referencedatagridfield.tests.base import TestCase |
---|
8 | from collective.referencedatagridfield.tests.base import FunctionalTestCase |
---|
9 | from collective.referencedatagridfield import ReferenceDataGridWidget |
---|
10 | |
---|
11 | |
---|
12 | class TestWidgetView(FunctionalTestCase): |
---|
13 | """ ReferenceDataGridWidget unit tests """ |
---|
14 | |
---|
15 | def afterSetUp(self): |
---|
16 | self.loginAsPortalOwner() |
---|
17 | # Prevent section links |
---|
18 | sp = self.portal.portal_properties.site_properties |
---|
19 | sp._updateProperty("disable_nonfolderish_sections", True) |
---|
20 | # Prepare testing data and data for functional test |
---|
21 | self.createDemo(wfaction="publish") |
---|
22 | self.demo_path = "/" + self.demo.absolute_url(1) |
---|
23 | self.basic_auth = ':'.join((portal_owner,default_password)) |
---|
24 | # Regexp for getting links |
---|
25 | self.relink = re.compile("<a\s+[^>]*?href=\"(.*?)\"[^>]*?>\s*(.*?)\s*</a>", |
---|
26 | re.I|re.S|re.M) |
---|
27 | |
---|
28 | def test_LinkDefaultTitle(self): |
---|
29 | self.demo.edit(demo_rdgf=[{"link": "http://google.com"}]) |
---|
30 | html = self.publish(self.demo_path, self.basic_auth).getBody() |
---|
31 | links = dict(self.relink.findall(html)) |
---|
32 | |
---|
33 | self.assertEqual(links.has_key("http://google.com"), True) |
---|
34 | self.assertEqual("http://google.com" in links["http://google.com"], True) |
---|
35 | |
---|
36 | def test_LinkCustomTitle(self): |
---|
37 | self.demo.edit(demo_rdgf=[{"link": "http://google.com", "title": "Google"}]) |
---|
38 | html = self.publish(self.demo_path, self.basic_auth).getBody() |
---|
39 | links = dict(self.relink.findall(html)) |
---|
40 | |
---|
41 | self.assertEqual(links.has_key("http://google.com"), True) |
---|
42 | self.assertEqual("Google" in links["http://google.com"], True) |
---|
43 | |
---|
44 | def test_UIDDefaultTitle(self): |
---|
45 | data = [{"uid": self.doc.UID(), "link": self.doc.absolute_url(1)}] |
---|
46 | self.demo.edit(demo_rdgf=data) |
---|
47 | html = self.publish(self.demo_path, self.basic_auth).getBody() |
---|
48 | links = dict(self.relink.findall(html)) |
---|
49 | |
---|
50 | doc_url = self.doc.absolute_url() |
---|
51 | doc_title = self.doc.Title() |
---|
52 | self.assertEqual(links.has_key(doc_url), True) |
---|
53 | self.assertEqual(doc_title in links[doc_url], True) |
---|
54 | |
---|
55 | def test_UIDCustomTitle(self): |
---|
56 | data = [{"uid": self.doc.UID(), "link": self.doc.absolute_url(1), |
---|
57 | "title": "Custom Title"},] |
---|
58 | self.demo.edit(demo_rdgf=data) |
---|
59 | html = self.publish(self.demo_path, self.basic_auth).getBody() |
---|
60 | links = dict(self.relink.findall(html)) |
---|
61 | |
---|
62 | doc_url = self.doc.absolute_url() |
---|
63 | self.assertEqual(links.has_key(doc_url), True) |
---|
64 | self.assertEqual("Custom Title" in links[doc_url], True) |
---|
65 | |
---|
66 | def test_LinksOrder(self): |
---|
67 | relink = re.compile("<a\s+[^>]*?href=\"(.*?)\"[^>]*?>", re.I|re.S) |
---|
68 | data = [{"link": "http://google.com"}, |
---|
69 | {"uid": self.doc.UID(), "link": self.doc.absolute_url(1)}] |
---|
70 | # First check in one order |
---|
71 | self.demo.edit(demo_rdgf=data) |
---|
72 | html = self.publish(self.demo_path, self.basic_auth).getBody() |
---|
73 | links = relink.findall(html) |
---|
74 | idx1 = links.index("http://google.com") |
---|
75 | idx2 = links.index(self.doc.absolute_url()) |
---|
76 | self.assertEqual( idx1 < idx2, True) |
---|
77 | # Now reverse rows order |
---|
78 | data.reverse() |
---|
79 | self.demo.edit(demo_rdgf=data) |
---|
80 | html = self.publish(self.demo_path, self.basic_auth).getBody() |
---|
81 | links = relink.findall(html) |
---|
82 | idx1 = links.index("http://google.com") |
---|
83 | idx2 = links.index(self.doc.absolute_url()) |
---|
84 | self.assertEqual( idx1 > idx2, True) |
---|
85 | |
---|
86 | |
---|
87 | class TestWidgetEditPresence(FunctionalTestCase): |
---|
88 | """ Test presence of columns and button |
---|
89 | in edit mode of ReferenceDataGridWidget. |
---|
90 | """ |
---|
91 | |
---|
92 | def afterSetUp(self): |
---|
93 | self.loginAsPortalOwner() |
---|
94 | # Prepare test data |
---|
95 | self.createDemo() |
---|
96 | self.demo.edit(demo_rdgf=[{"link": "http://google.com"}]) |
---|
97 | # Prepare html for test edit form |
---|
98 | edit_path = "/%s/edit" % self.demo.absolute_url(1) |
---|
99 | basic_auth = ':'.join((portal_owner,default_password)) |
---|
100 | self.html = self.publish(edit_path, basic_auth).getBody() |
---|
101 | |
---|
102 | def test_columnsPresence(self): |
---|
103 | # Get ReferenceDataGridField field inputs without hidden template row for add new data |
---|
104 | reinput = re.compile("<input\s+([^>]*?name=\"demo_rdgf\.(.*?):records\"[^>]*?)>", re.I|re.S) |
---|
105 | inputs = dict([(v,k) for k,v in reinput.findall(self.html) if not "demo_rdgf_new" in k]) |
---|
106 | # Title and Link columns is visible |
---|
107 | self.assertEqual('type="text"' in inputs["title"], True) |
---|
108 | self.assertEqual('type="text"' in inputs["link"], True) |
---|
109 | # UID column is hidden |
---|
110 | self.assertEqual('type="hidden"' in inputs["uid"], True) |
---|
111 | |
---|
112 | def test_addButtonPresence(self): |
---|
113 | # Button for adding reference also must present |
---|
114 | rebutt = re.compile("<input\s+[^>]*type=\"button\"\s*[^>]*>", re.I|re.S) |
---|
115 | buttons = filter(lambda k:not "_new" in k, rebutt.findall(self.html)) |
---|
116 | # Add... button must present |
---|
117 | self.assertEqual('value="Add..."' in buttons[0], True) |
---|
118 | |
---|
119 | |
---|
120 | class TestWidgetResources(TestCase): |
---|
121 | """Tests of widget resources.""" |
---|
122 | |
---|
123 | def afterSetUp(self): |
---|
124 | self.widget_props = ReferenceDataGridWidget._properties |
---|
125 | self.rdgw_skin_ids = self.portal.portal_skins.referencedatagridfield.objectIds() |
---|
126 | |
---|
127 | def test_helperJS(self): |
---|
128 | helper_js = self.widget_props.get("helper_js", "") |
---|
129 | self.assertEqual("referencedatagridwidget.js" in helper_js, True) |
---|
130 | self.assertEqual("referencedatagridwidget.js" in self.rdgw_skin_ids, True) |
---|
131 | |
---|
132 | def test_helperCSS(self): |
---|
133 | helper_css = self.widget_props.get("helper_css", "") |
---|
134 | self.assertEqual("referencedatagridwidget.css" in helper_css, True) |
---|
135 | self.assertEqual("referencedatagridwidget.css" in self.rdgw_skin_ids, True) |
---|
136 | |
---|
137 | |
---|
138 | def test_suite(): |
---|
139 | return unittest.TestSuite([ |
---|
140 | unittest.makeSuite(TestWidgetView), |
---|
141 | unittest.makeSuite(TestWidgetResources), |
---|
142 | unittest.makeSuite(TestWidgetEditPresence), |
---|
143 | ]) |
---|