testRSS.py 8.52 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
    one = self.portal.person_module.newContent(id="one", title="One", description="Person One")
80
    two = self.portal.person_module.newContent(id="two", title="Two", description="Person Two")
81
    self.commit()
82
    one.reindexObject()
83
    two.reindexObject()
84
    self.tic()
Fabien Morin's avatar
Fabien Morin committed
85

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

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

101 102
    request.set('portal_skin', 'RSS');
    portal.portal_skins.changeSkin('RSS');
Fabien Morin's avatar
Fabien Morin committed
103

104
    one = self.portal.person_module.one
105
    two = self.portal.person_module.two
Fabien Morin's avatar
Fabien Morin committed
106

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

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

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

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

138 139 140 141 142 143 144 145
  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
146

147 148
    request.set('portal_skin', 'RSS');
    portal.portal_skins.changeSkin('RSS');
Fabien Morin's avatar
Fabien Morin committed
149

150 151 152 153
    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
154

155 156 157 158
    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
159

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

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

177 178
    one = self.portal.person_module.one
    two = self.portal.person_module.two
Fabien Morin's avatar
Fabien Morin committed
179

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

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

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

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

211 212 213 214
def test_suite():
  suite = unittest.TestSuite()
  suite.addTest(unittest.makeSuite(TestRSS))
  return suite