testRSS.py 8.57 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

from xml.dom.minidom import parseString

36
import transaction
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 77


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'])
78 79
    if hasattr(self.portal.person_module, 'two'):
      self.portal.person_module.manage_delObjects(['two'])
80
    one = self.portal.person_module.newContent(id="one", title="One", description="Person One")
81
    two = self.portal.person_module.newContent(id="two", title="Two", description="Person Two")
82
    transaction.commit()
83
    one.reindexObject()
84
    two.reindexObject()
85
    self.tic()
Fabien Morin's avatar
Fabien Morin committed
86

87 88 89 90 91 92
  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")
93
    self.assertEquals(module.two.getTitle(), "Two")
Fabien Morin's avatar
Fabien Morin committed
94

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

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

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

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

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

    item = channel.getElementsByTagName('item')[0] # the two person, because we have default sorting in form
120 121 122 123 124 125 126 127
    self.assertEquals(getSubnodeContent(item, 'title'), 'Two')
    self.assertEquals(getSubnodeContent(item, 'description'), 'Person Two')
    self.assertEquals(getSubnodeContent(item, 'author'), 'seb')
    expected_link = two.absolute_url() + '/view'
    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
128

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

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

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

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

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

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

    listbox_link.manage_tales_xmlrpc(
        dict(default="python: cell.absolute_url() + '/view'"))
Fabien Morin's avatar
Fabien Morin committed
177

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

181 182 183 184 185 186
    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
187

188 189 190
    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
191 192

    item = channel.getElementsByTagName('item')[0] # the two person, because we have default sorting in form
193 194 195 196 197 198 199 200
    self.assertEquals(getSubnodeContent(item, 'title'), 'Two')
    self.assertEquals(getSubnodeContent(item, 'description'), 'Person Two')
    self.assertEquals(getSubnodeContent(item, 'author'), 'seb')
    expected_link = two.absolute_url() + '/view'
    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
201

202 203 204 205 206 207 208 209
    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')
    expected_link = one.absolute_url() + '/view'
    self.assertEquals(getSubnodeContent(item, 'link'), expected_link)
    self.assertEquals(len(item.getElementsByTagName('pubDate')), 1)
    # is date formatted correctly?
210
    self.assertEquals(one.getCreationDate().rfc822(), getSubnodeContent(item, 'pubDate'))
Fabien Morin's avatar
Fabien Morin committed
211

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