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