testRSS.py 8.41 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
##############################################################################
#
# Copyright (c) 2007 Nexedi SARL and Contributors. All Rights Reserved.
#
# WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsability of assessing all potential
# consequences resulting from its eventual inadequacies and bugs
# End users who are looking for a ready-to-use solution with commercial
# garantees and support are strongly adviced to contract a Free Software
# Service Company
#
# This program is Free Software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
##############################################################################

28
import unittest
29 30 31

from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5TypeTestCase
from AccessControl.SecurityManagement import newSecurityManager
32
from Products.ERP5Form.Form import ERP5Form
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76

from xml.dom.minidom import parseString



def getNodeContent(node):
  return node.childNodes[0].nodeValue

def getSubnodeContent(node, tagName, index=0):
  try:
    return getNodeContent(node.getElementsByTagName(tagName)[index])
  except IndexError:
    return None


class TestRSS(ERP5TypeTestCase):

  run_all_test = 1

  def getTitle(self):
    return "RSS Test"

  def getBusinessTemplateList(self):
    """  """
    return ('erp5_base', 'erp5_rss_style')

  def afterSetUp(self):
    self.portal = self.getPortal()
    self.makeDataObjects()
    #self.login()

  def login(self, quiet=0, run=run_all_test):
    uf = self.getPortal().acl_users
    uf._doAddUser('seb', '', ['Manager'], [])
    uf._doAddUser('ERP5TypeTestCase', '', ['Manager'], [])
    user = uf.getUserById('seb').__of__(uf)
    newSecurityManager(None, user)

  def makeDataObjects(self, quiet=0, run=run_all_test):
    """
      Create some Pesons so that we have something to feed.
    """
    if hasattr(self.portal.person_module, 'one'):
      self.portal.person_module.manage_delObjects(['one'])
77 78
    if hasattr(self.portal.person_module, 'two'):
      self.portal.person_module.manage_delObjects(['two'])
79 80
    self.portal.person_module.newContent(id="one", title="One", description="Person One")
    self.portal.person_module.newContent(id="two", title="Two", description="Person Two")
81
    self.tic()
Fabien Morin's avatar
Fabien Morin committed
82

83 84 85 86 87
  def test_00_haveData(self, quiet=0, run=run_all_test):
    """
      Check we have people.
    """
    module = self.portal.person_module
88 89
    self.assertEqual(module.one.getTitle(), "One")
    self.assertEqual(module.two.getTitle(), "Two")
Fabien Morin's avatar
Fabien Morin committed
90

91 92 93 94
  def test_01_renderRSS(self, quiet=0, run=run_all_test):
    """
      View person module as RSS, parse XML, see if everything is there.
    """
95 96
    portal=self.getPortal()
    request=self.app.REQUEST
Fabien Morin's avatar
Fabien Morin committed
97

98 99
    request.set('portal_skin', 'RSS');
    portal.portal_skins.changeSkin('RSS');
Fabien Morin's avatar
Fabien Morin committed
100

101
    one = self.portal.person_module.one
102
    two = self.portal.person_module.two
Fabien Morin's avatar
Fabien Morin committed
103

104 105 106 107
    feed_string = self.portal.person_module.Folder_viewContentListAsRSS()
    doc = parseString(feed_string)
    rss = doc.childNodes[0]
    channel = rss.getElementsByTagName('channel')[0]
108 109
    self.assertEqual(len(rss.getElementsByTagName('channel')), 1)
    self.assertEqual(len(channel.getElementsByTagName('item')), 2)
Fabien Morin's avatar
Fabien Morin committed
110

111 112
    titles = [getNodeContent(n) for n in channel.getElementsByTagName('title')]
    titles.sort()
113
    self.assertEqual(titles, ['One', 'Persons',  'Two']) # there is channel title and person titles
Fabien Morin's avatar
Fabien Morin committed
114 115

    item = channel.getElementsByTagName('item')[0] # the two person, because we have default sorting in form
116 117 118
    self.assertEqual(getSubnodeContent(item, 'title'), 'Two')
    self.assertEqual(getSubnodeContent(item, 'description'), 'Person Two')
    self.assertEqual(getSubnodeContent(item, 'author'), 'seb')
119
    expected_link = '%s/view' %two.absolute_url()
120 121
    self.assertEqual(getSubnodeContent(item, 'link'), expected_link)
    self.assertEqual(len(item.getElementsByTagName('pubDate')), 1)
122
    # is date formatted correctly?
123
    self.assertEqual(two.getCreationDate().rfc822(), getSubnodeContent(item, 'pubDate'))
Fabien Morin's avatar
Fabien Morin committed
124

125
    item = channel.getElementsByTagName('item')[1] # the one person
126 127 128
    self.assertEqual(getSubnodeContent(item, 'title'), 'One')
    self.assertEqual(getSubnodeContent(item, 'description'), 'Person One')
    self.assertEqual(getSubnodeContent(item, 'author'), 'seb')
129
    expected_link = '%s/view' %one.absolute_url()
130 131
    self.assertEqual(getSubnodeContent(item, 'link'), expected_link)
    self.assertEqual(len(item.getElementsByTagName('pubDate')), 1)
132
    # is date formatted correctly?
133
    self.assertEqual(one.getCreationDate().rfc822(), getSubnodeContent(item, 'pubDate'))
134

135 136 137 138 139 140 141 142
  def test_02_renderRSS(self, quiet=0, run=run_all_test):
    """
      View person module as RSS, parse XML, see if everything is there.
      In this case pt for render current form('Test_view') is default page template
      and some listbox's columns(i.e. description) label not present in required channel fields
    """
    portal=self.getPortal()
    request=self.app.REQUEST
Fabien Morin's avatar
Fabien Morin committed
143

144 145
    request.set('portal_skin', 'RSS');
    portal.portal_skins.changeSkin('RSS');
Fabien Morin's avatar
Fabien Morin committed
146

147 148 149 150
    self.getPortal()._setObject('Test_view',
                      ERP5Form('Test_view', 'View'))
    portal.Test_view.manage_addField('listbox', 'listbox', 'ListBox')
    portal.Test_view.manage_addField('listbox_link',  'listbox_link',  'StringField')
Fabien Morin's avatar
Fabien Morin committed
151

152 153 154 155
    listbox=portal.Test_view.listbox
    self.assertNotEquals(listbox, None)
    listbox_link=portal.Test_view.listbox_link
    self.assertNotEquals(listbox_link,  None)
Fabien Morin's avatar
Fabien Morin committed
156

157 158 159 160
    listbox.manage_edit_xmlrpc(
        dict(columns=[('title', 'Title'),
                      ('creation_date', 'pubDate'),
                      ('Base_getRSSAuthor','author'),
Fabien Morin's avatar
Fabien Morin committed
161
                      ('link','link'),
162
                      ('absolute_url', 'guid')],
163
             sort=[('creation_date', 'descending')],
164 165 166 167 168 169 170 171
             list_action='list',
             search=1,
             select=1,
             list_method='searchFolder',
             count_method='countFolder',
             selection_name='rss_folder_selection'))

    listbox_link.manage_tales_xmlrpc(
172
        dict(default="python: cell.absolute_url()"))
Fabien Morin's avatar
Fabien Morin committed
173

174 175
    one = self.portal.person_module.one
    two = self.portal.person_module.two
Fabien Morin's avatar
Fabien Morin committed
176

177 178 179 180
    feed_string = self.portal.person_module.Test_view()
    doc = parseString(feed_string)
    rss = doc.childNodes[0]
    channel = rss.getElementsByTagName('channel')[0]
181 182
    self.assertEqual(len(rss.getElementsByTagName('channel')), 1)
    self.assertEqual(len(channel.getElementsByTagName('item')), 2)
Fabien Morin's avatar
Fabien Morin committed
183

184 185
    titles = [getNodeContent(n) for n in channel.getElementsByTagName('title')]
    titles.sort()
186
    self.assertEqual(titles, ['One', 'Persons',  'Two']) # there is channel title and person titles
Fabien Morin's avatar
Fabien Morin committed
187 188

    item = channel.getElementsByTagName('item')[0] # the two person, because we have default sorting in form
189 190 191
    self.assertEqual(getSubnodeContent(item, 'title'), 'Two')
    self.assertEqual(getSubnodeContent(item, 'description'), 'Person Two')
    self.assertEqual(getSubnodeContent(item, 'author'), 'seb')
192
    expected_link = two.absolute_url()
193 194
    self.assertEqual(getSubnodeContent(item, 'link'), expected_link)
    self.assertEqual(len(item.getElementsByTagName('pubDate')), 1)
195
    # is date formatted correctly?
196
    self.assertEqual(two.getCreationDate().rfc822(), getSubnodeContent(item, 'pubDate'))
Fabien Morin's avatar
Fabien Morin committed
197

198
    item = channel.getElementsByTagName('item')[1] # the one person
199 200 201
    self.assertEqual(getSubnodeContent(item, 'title'), 'One')
    self.assertEqual(getSubnodeContent(item, 'description'), 'Person One')
    self.assertEqual(getSubnodeContent(item, 'author'), 'seb')
202
    expected_link = one.absolute_url()
203 204
    self.assertEqual(getSubnodeContent(item, 'link'), expected_link)
    self.assertEqual(len(item.getElementsByTagName('pubDate')), 1)
205
    # is date formatted correctly?
206
    self.assertEqual(one.getCreationDate().rfc822(), getSubnodeContent(item, 'pubDate'))
Fabien Morin's avatar
Fabien Morin committed
207

208 209 210 211
def test_suite():
  suite = unittest.TestSuite()
  suite.addTest(unittest.makeSuite(TestRSS))
  return suite