testTemplate.py 12.5 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
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."""
49
    return ('erp5_base', 'erp5_knowledge_pad', 'erp5_ui_test')
50 51

  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
    self.portal.portal_types['Preference'].allowed_content_types = ('Foo', 'Knowledge Pad')
67 68 69 70 71 72
    self.portal.foo_module.manage_role(role_to_manage='Author',
                                permissions=[Permissions.AddPortalContent,
                                             Permissions.CopyOrMove,
                                             ])

  def test_Template(self):
73
    self.login(self.id())
74 75 76 77
    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 167 168 169 170
  def test_TemplateDeletable(self):
    self.login(self.id())
    preference = self.portal.portal_preferences.newContent(portal_type='Preference')
    preference.priority = Priority.USER
    preference.enable()

    transaction.commit()
    self.tic()

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

    transaction.commit()
    self.tic()

    document.Base_makeTemplateFromDocument(form_id=None)

    transaction.commit()
    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()])

    transaction.commit()
    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()

    transaction.commit()
    self.tic()

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

    # as templates are not indexable it is required to know, if they would
    # appear on Folder_getDeleteObjectList
    template_uid = template.getUid()
    self.assertEqual(
      1,
      len(preference.Folder_getDeleteObjectList(uid = [template_uid]))
    )

171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
  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):
Łukasz Nowak's avatar
Łukasz Nowak committed
193 194
    # TODO: This test *might* be removed if it is good to trust
    #       getActivePreference to return only Preference portal type
195 196 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
    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)

222 223 224
  def test_TemplateCreatePreference(self):
    self.login('another user with no active preference')
    active_user_preference_list = [p for p in
225
        self.portal.portal_preferences._getSortedPreferenceList()
226 227 228 229 230
        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')
231
    transaction.commit()
232 233 234
    self.tic()

    document.Base_makeTemplateFromDocument(form_id=None)
235
    transaction.commit()
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
    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)

252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
  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)
289

290
  def _testTemplateNotIndexable(self, document):
291
    # template documents are not indexable
292 293 294
    self.portal.portal_activities.manage_enableActivityTracking()
    self.portal.portal_activities.manage_enableActivityTimingLogging()
    self.portal.portal_activities.manage_enableActivityCreationTrace()
295
    self.login(self.id())
296 297 298 299
    preference = self.portal.portal_preferences.newContent(portal_type='Preference')
    preference.priority = Priority.USER
    preference.enable()

300
    transaction.commit()
301 302 303
    self.tic()

    document.Base_makeTemplateFromDocument(form_id=None)
304
    transaction.commit()
305 306 307
    self.tic()
    self.assertTrue(document.isIndexable)
    self.assertEqual(len(preference.objectIds()), 1)
308 309 310 311 312 313 314 315 316 317
    template = preference.objectValues()[0]
    self.assertFalse(template.isIndexable)
    
    # Because they are not indexable, they cannot be found by catalog
    transaction.commit()
    self.tic()
    self.assertEquals(0, len(self.portal.portal_catalog(uid=template.getUid())))
    template_line = template.objectValues()[0]
    self.assertEquals(0,
        len(self.portal.portal_catalog(uid=template_line.getUid())))
318 319 320 321

    # and this is still true if you create two templates from the same document
    # #929
    document.Base_makeTemplateFromDocument(form_id=None)
322
    transaction.commit()
323 324 325 326 327 328 329
    self.tic()

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

330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
  def test_DeliveryTemplateNotIndexable(self):
    document = self.portal.foo_module.newContent(portal_type='Foo')
    document.edit(title='My Foo 1')
    document.newContent(portal_type='Foo Line')
    transaction.commit()
    self.tic()
    self._testTemplateNotIndexable(document)

  def test_NonDeliveryTemplateNotIndexable(self):
    document = self.portal.knowledge_pad_module.newContent(portal_type='Knowledge Pad')
    document.edit(title='My Knowledge Pad 1')
    document.newContent(portal_type='Knowledge Box')
    transaction.commit()
    self.tic()
    self._testTemplateNotIndexable(document)

346

347 348 349 350
def test_suite():
  suite = unittest.TestSuite()
  suite.addTest(unittest.makeSuite(TestTemplate))
  return suite