testTemplate.py 9.78 KB
Newer Older
1
# -*- coding: utf-8 -*-
2 3
#############################################################################
#
4
# Copyright (c) 2007-2009 Nexedi SA and Contributors. All Rights Reserved.
5
#                    TAHARA Yusei <yusei@nexedi.com>
6
#                    Łukasz Nowak <luke@nexedi.com>
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
#
# 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.
#
##############################################################################
"""Tests Template functionality"""

import unittest
33
import transaction
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
from AccessControl.SecurityManagement import newSecurityManager
from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5TypeTestCase
from Products.ERP5Type import Permissions
from Products.ERP5Form.Document.Preference import Priority


class TestTemplate(ERP5TypeTestCase):
  """A test for Template.
  """

  def getTitle(self):
    return "Template"

  def getBusinessTemplateList(self):
    """Returns list of BT to be installed."""
    return ('erp5_base', 'erp5_ui_test')

  def login(self, name=None):
52
    """login with Member & Author roles."""
53 54 55
    if name is None:
      return
    uf = self.getPortal().acl_users
56
    uf._doAddUser(name, '', ['Member', 'Author'], [])
57 58 59 60
    user = uf.getUserById(name).__of__(uf)
    newSecurityManager(None, user)

  def afterSetUp(self):
61 62 63 64 65
    ERP5TypeTestCase.login(self, 'ERP5TypeTestCase')
    portal_preferences = self.portal.portal_preferences
    portal_preferences.deleteContent(list(portal_preferences.objectIds()))
    transaction.commit()
    self.tic()
66 67 68 69 70 71 72 73 74 75 76 77
    self.portal.portal_types['Preference'].allowed_content_types = ('Foo',)
    self.portal.foo_module.manage_role(role_to_manage='Author',
                                permissions=[Permissions.AddPortalContent,
                                             Permissions.CopyOrMove,
                                             ])

  def test_Template(self):
    self.login('yusei')
    preference = self.portal.portal_preferences.newContent(portal_type='Preference')
    preference.priority = Priority.USER
    preference.enable()

78
    transaction.commit()
79 80 81 82 83 84
    self.tic()

    document = self.portal.foo_module.newContent(portal_type='Foo')
    document.edit(title='My Foo 1')
    document.newContent(portal_type='Foo Line')

85
    transaction.commit()
86 87 88 89
    self.tic()

    document.Base_makeTemplateFromDocument(form_id=None)

90
    transaction.commit()
91 92 93 94 95 96 97 98 99 100
    self.tic()

    self.assertEqual(len(preference.objectIds()), 1)

    # make sure that subobjects are not unindexed after making template.
    subobject_uid = document.objectValues()[0].getUid()
    self.assertEqual(len(self.portal.portal_catalog(uid=subobject_uid)), 1)

    self.portal.foo_module.manage_delObjects(ids=[document.getId()])

101
    transaction.commit()
102 103 104 105 106 107 108 109 110 111
    self.tic()

    template = preference.objectValues()[0]

    cp = preference.manage_copyObjects(ids=[template.getId()], REQUEST=None, RESPONSE=None)
    new_document_list = self.portal.foo_module.manage_pasteObjects(cp)
    new_document_id = new_document_list[0]['new_id']
    new_document = self.portal.foo_module[new_document_id]
    new_document.makeTemplateInstance()

112
    transaction.commit()
113 114 115 116 117
    self.tic()

    self.assertEqual(new_document.getTitle(), 'My Foo 1')


118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
  def test_TemplateCreatePreferenceWithExistingUserPreference(self):
    self.login(self.id())
    user_preference = self.portal.portal_preferences.newContent(
        portal_type='Preference')
    user_preference.setPriority(Priority.USER)
    user_preference.enable()
    transaction.commit()
    self.tic()

    document = self.portal.foo_module.newContent(portal_type='Foo')
    transaction.commit()
    self.tic()

    document.Base_makeTemplateFromDocument(form_id=None)
    transaction.commit()
    self.tic()

    # created preference is reused to store template
    self.assertEquals('enabled', user_preference.getPreferenceState())
    self.assertEqual(len(user_preference.objectIds()), 1)

  def test_TemplateCreatePreferenceWithSystemPreferenceEnabled(self):
    ERP5TypeTestCase.login(self, 'ERP5TypeTestCase')
    system_preference = self.portal.portal_preferences.newContent(
        portal_type='System Preference')
    system_preference.setPriority(Priority.SITE)
    system_preference.enable()
    transaction.commit()
    self.tic()
    self.login(self.id())
    user_preference = self.portal.portal_preferences.newContent(
        portal_type='Preference')
    user_preference.setPriority(Priority.USER)
    user_preference.enable()
    transaction.commit()
    self.tic()

    document = self.portal.foo_module.newContent(portal_type='Foo')
    transaction.commit()
    self.tic()

    document.Base_makeTemplateFromDocument(form_id=None)
    transaction.commit()
    self.tic()

    # created preference is reused to store template
    self.assertEquals('enabled', user_preference.getPreferenceState())
    self.assertEqual(len(user_preference.objectIds()), 1)

167 168 169
  def test_TemplateCreatePreference(self):
    self.login('another user with no active preference')
    active_user_preference_list = [p for p in
170
        self.portal.portal_preferences._getSortedPreferenceList()
171 172 173 174 175
        if p.getPriority() == Priority.USER]
    self.assertEquals([], active_user_preference_list)

    preference_id_list = list(self.portal.portal_preferences.objectIds())
    document = self.portal.foo_module.newContent(portal_type='Foo')
176
    transaction.commit()
177 178 179
    self.tic()

    document.Base_makeTemplateFromDocument(form_id=None)
180
    transaction.commit()
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
    self.tic()

    # a new preference is created
    new_preference_id_list = list(self.portal.portal_preferences.objectIds())
    self.assertEqual(len(preference_id_list) + 1, len(new_preference_id_list))
    preference_id = [x for x in new_preference_id_list if x not in
                            preference_id_list][0]
    preference = self.portal.portal_preferences._getOb(preference_id)

    self.assertEquals('Preference', preference.getPortalType())
    self.assertEquals('Document Template Container', preference.getTitle())
    self.assertEquals(Priority.USER, preference.getPriority())
    self.assertEquals('enabled', preference.getPreferenceState())

    self.assertEqual(len(preference.objectIds()), 1)

197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
  def test_manyTemplatesWithoutReindexation(self):
    """Check what happen when templates are created one by one without reindexation"""
    self.login(self.id())
    active_user_preference_list = [p for p in
        self.portal.portal_preferences._getSortedPreferenceList()
        if p.getPriority() == Priority.USER]
    self.assertEquals([], active_user_preference_list)

    preference_id_list = list(self.portal.portal_preferences.objectIds())

    document1 = self.portal.foo_module.newContent(portal_type='Foo')
    transaction.commit()
    document2 = self.portal.foo_module.newContent(portal_type='Foo')
    transaction.commit()
    self.tic()

    document1.Base_makeTemplateFromDocument(form_id=None)
    transaction.commit()

    document2.Base_makeTemplateFromDocument(form_id=None)
    transaction.commit()

    self.tic()

    # a new preference is created
    new_preference_id_list = list(self.portal.portal_preferences.objectIds())
    self.assertEqual(len(preference_id_list) + 1, len(new_preference_id_list))
    preference_id = [x for x in new_preference_id_list if x not in
                            preference_id_list][0]
    preference = self.portal.portal_preferences._getOb(preference_id)

    self.assertEquals('Preference', preference.getPortalType())
    self.assertEquals('Document Template Container', preference.getTitle())
    self.assertEquals(Priority.USER, preference.getPriority())
    self.assertEquals('enabled', preference.getPreferenceState())

    self.assertEqual(len(preference.objectIds()), 2)
234

235 236 237 238 239 240 241
  def test_TemplateNotIndexable(self):
    # template documents are not indexable
    self.login('yusei')
    preference = self.portal.portal_preferences.newContent(portal_type='Preference')
    preference.priority = Priority.USER
    preference.enable()

242
    transaction.commit()
243 244 245 246 247 248
    self.tic()

    document = self.portal.foo_module.newContent(portal_type='Foo')
    document.edit(title='My Foo 1')
    document.newContent(portal_type='Foo Line')

249
    transaction.commit()
250 251 252
    self.tic()

    document.Base_makeTemplateFromDocument(form_id=None)
253
    transaction.commit()
254 255 256 257 258 259 260 261 262
    self.tic()
    self.assertTrue(document.isIndexable)
    self.assertEqual(len(preference.objectIds()), 1)
    for template in preference.objectValues():
      self.assertFalse(template.isIndexable)

    # and this is still true if you create two templates from the same document
    # #929
    document.Base_makeTemplateFromDocument(form_id=None)
263
    transaction.commit()
264 265 266 267 268 269 270 271
    self.tic()

    self.assertTrue(document.isIndexable)
    self.assertEqual(len(preference.objectIds()), 2)
    for template in preference.objectValues():
      self.assertFalse(template.isIndexable)


272 273 274 275
def test_suite():
  suite = unittest.TestSuite()
  suite.addTest(unittest.makeSuite(TestTemplate))
  return suite