testERP5Catalog.py 176 KB
Newer Older
1
# -*- coding: utf-8 -*-
Sebastien Robin's avatar
Sebastien Robin committed
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 28 29
##############################################################################
#
# Copyright (c) 2004 Nexedi SARL and Contributors. All Rights Reserved.
#          Sebastien Robin <seb@nexedi.com>
#
# 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.
#
##############################################################################

Jérome Perrin's avatar
Jérome Perrin committed
30 31
import unittest
import sys
32
from unittest import expectedFailure
Sebastien Robin's avatar
Sebastien Robin committed
33 34 35

from Testing import ZopeTestCase
from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5TypeTestCase
36
from AccessControl import getSecurityManager
37
from AccessControl.SecurityManagement import newSecurityManager
Sebastien Robin's avatar
Sebastien Robin committed
38 39
from zLOG import LOG
from DateTime import DateTime
40
from Products.ERP5Type.tests.utils import LogInterceptor
41 42
from Products.ERP5Type.tests.utils import createZODBPythonScript, todo_erp5, \
                                          getExtraSqlConnectionStringList
43 44 45
from Products.ZSQLCatalog.ZSQLCatalog import HOT_REINDEXING_FINISHED_STATE,\
      HOT_REINDEXING_RECORDING_STATE, HOT_REINDEXING_DOUBLE_INDEXING_STATE
from Products.CMFActivity.Errors import ActivityFlushError
46
from Products.ZSQLCatalog.SQLCatalog import Query, ComplexQuery, SimpleQuery
Sebastien Robin's avatar
Sebastien Robin committed
47

48

49 50 51 52
from OFS.ObjectManager import ObjectManager
from random import randint

class IndexableDocument(ObjectManager):
53 54 55

  # this property is required for dummy providesIMovement
  __allow_access_to_unprotected_subobjects__ = 1
56
  isRADContent = 0
57

58 59 60 61 62 63 64 65
  def getUid(self):
    uid = getattr(self, 'uid', None)
    if uid is None:
      self.uid = uid = randint(1, 100) + 100000
    return uid

  def __getattr__(self, name):
    # Case for all "is..." magic properties (isMovement, ...)
66 67 68
    if name.startswith('is') or \
       name.startswith('provides'):
      return lambda: 0
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
    raise AttributeError, name

  def getPath(self):
    return '' # Whatever

  def getRelativeUrl(self):
    return '' # Whatever

class FooDocument(IndexableDocument):
  def getReference(self):
    return 'foo'

class BarDocument(IndexableDocument):
  # Does not define any getReference method.
  pass

85
class TestERP5Catalog(ERP5TypeTestCase, LogInterceptor):
Sebastien Robin's avatar
Sebastien Robin committed
86
  """
87
    Tests for ERP5 Catalog.
Sebastien Robin's avatar
Sebastien Robin committed
88 89 90 91 92
  """

  def getTitle(self):
    return "ERP5Catalog"

Sebastien Robin's avatar
Sebastien Robin committed
93
  def getBusinessTemplateList(self):
94
    return ('erp5_full_text_mroonga_catalog', 'erp5_base',)
Sebastien Robin's avatar
Sebastien Robin committed
95

Sebastien Robin's avatar
Sebastien Robin committed
96
  # Different variables used for this test
97
  username = 'seb'
98 99 100
  new_erp5_sql_connection = 'erp5_sql_connection2'
  new_erp5_deferred_sql_connection = 'erp5_sql_deferred_connection2'
  new_catalog_id = 'erp5_mysql_innodb2'
101

102 103 104 105 106 107
  __cleanups = ()

  def _addCleanup(self, callable):
    self.__cleanups += (callable,)
    return callable

108
  def afterSetUp(self):
109 110 111
    uf = self.getPortal().acl_users
    uf._doAddUser(self.username, '', ['Manager'], [])

112
    self.loginByUserName(self.username)
113 114
    # make sure there is no message any more
    self.tic()
115 116

  def beforeTearDown(self):
117 118
    # restore default_catalog
    self.portal.portal_catalog.default_sql_catalog_id = 'erp5_mysql_innodb'
119
    self.portal.portal_catalog.hot_reindexing_state = None
120
    # clear Modules
121 122 123 124 125
    for module in [ self.getPersonModule(),
                    self.getOrganisationModule(),
                    self.getCategoryTool().region,
                    self.getCategoryTool().group ]:
      module.manage_delObjects(list(module.objectIds()))
126
      module.reindexObject()
127 128 129 130 131 132 133
    # Remove copied sql_connector and catalog
    if self.new_erp5_sql_connection in self.portal.objectIds():
      self.portal.manage_delObjects([self.new_erp5_sql_connection])
    if self.new_erp5_deferred_sql_connection in self.portal.objectIds():
      self.portal.manage_delObjects([self.new_erp5_deferred_sql_connection])
    if self.new_catalog_id in self.portal.portal_catalog.objectIds():
      self.portal.portal_catalog.manage_delObjects([self.new_catalog_id])
134 135
    for cleanup in self.__cleanups:
      cleanup(self)
136
    self.tic()
Sebastien Robin's avatar
Sebastien Robin committed
137

138
  def getSQLPathList(self,connection_id=None, sql=None):
Sebastien Robin's avatar
Sebastien Robin committed
139 140 141
    """
    Give the full list of path in the catalog
    """
142 143 144
    if connection_id is None:
      sql_connection = self.getSQLConnection()
    else:
145 146 147
      sql_connection = getattr(self.getPortal(), connection_id)
    if sql is None:
      sql = 'select distinct(path) from catalog'
Sebastien Robin's avatar
Sebastien Robin committed
148
    result = sql_connection.manage_test(sql)
149
    path_list = map(lambda x: x['path'], result)
Sebastien Robin's avatar
Sebastien Robin committed
150
    return path_list
151

152 153 154 155
  def getSQLPathListWithRolesAndUsers(self, connection_id):
    sql = 'select distinct(path) from catalog, roles_and_users\
           where catalog.security_uid=roles_and_users.uid'
    return self.getSQLPathList(connection_id, sql)
Sebastien Robin's avatar
Sebastien Robin committed
156

157 158
  def checkRelativeUrlInSQLPathList(self,url_list,connection_id=None):
    path_list = self.getSQLPathList(connection_id=connection_id)
Sebastien Robin's avatar
Sebastien Robin committed
159 160 161
    portal_id = self.getPortalId()
    for url in url_list:
      path = '/' + portal_id + '/' + url
162
      self.assertTrue(path in path_list)
163
      LOG('checkRelativeUrlInSQLPathList found path:',0,path)
Sebastien Robin's avatar
Sebastien Robin committed
164

165 166
  def checkRelativeUrlNotInSQLPathList(self,url_list,connection_id=None):
    path_list = self.getSQLPathList(connection_id=connection_id)
Sebastien Robin's avatar
Sebastien Robin committed
167 168 169
    portal_id = self.getPortalId()
    for url in url_list:
      path = '/' + portal_id + '/' + url
170
      self.assertTrue(path not in  path_list)
171
      LOG('checkRelativeUrlInSQLPathList not found path:',0,path)
Sebastien Robin's avatar
Sebastien Robin committed
172

Jérome Perrin's avatar
Jérome Perrin committed
173 174 175 176 177 178 179 180
  def test_01_HasEverything(self):
    self.assertNotEquals(self.getCategoryTool(), None)
    self.assertNotEquals(self.getSimulationTool(), None)
    self.assertNotEquals(self.getTypeTool(), None)
    self.assertNotEquals(self.getSQLConnection(), None)
    self.assertNotEquals(self.getCatalogTool(), None)

  def test_02_EverythingCatalogued(self):
Sebastien Robin's avatar
Sebastien Robin committed
181
    portal_catalog = self.getCatalogTool()
182
    self.tic()
Sebastien Robin's avatar
Sebastien Robin committed
183
    organisation_module_list = portal_catalog(portal_type='Organisation Module')
184
    self.assertEqual(len(organisation_module_list),1)
Sebastien Robin's avatar
Sebastien Robin committed
185

Jérome Perrin's avatar
Jérome Perrin committed
186
  def test_03_CreateAndDeleteObject(self):
Sebastien Robin's avatar
Sebastien Robin committed
187 188 189
    portal_catalog = self.getCatalogTool()
    person_module = self.getPersonModule()
    person = person_module.newContent(id='1',portal_type='Person')
190
    path_list = [person.getRelativeUrl()]
191
    self.checkRelativeUrlNotInSQLPathList(path_list)
192
    self.tic()
193
    self.checkRelativeUrlInSQLPathList(path_list)
Sebastien Robin's avatar
Sebastien Robin committed
194
    person_module.manage_delObjects('1')
195
    self.tic()
196
    self.checkRelativeUrlNotInSQLPathList(path_list)
Sebastien Robin's avatar
Sebastien Robin committed
197
    # Now we will ask to immediatly reindex
198
    person = person_module.newContent(id='2',
199
                                      portal_type='Person',)
200
    self.tic()
201
    path_list = [person.getRelativeUrl()]
202
    self.checkRelativeUrlInSQLPathList(path_list)
203
    self.tic()
204
    self.checkRelativeUrlInSQLPathList(path_list)
Sebastien Robin's avatar
Sebastien Robin committed
205
    person_module.manage_delObjects('2')
206
    self.tic()
207
    self.checkRelativeUrlNotInSQLPathList(path_list)
Sebastien Robin's avatar
Sebastien Robin committed
208 209
    # Now we will try with the method deleteContent
    person = person_module.newContent(id='3',portal_type='Person')
210
    path_list = [person.getRelativeUrl()]
211
    self.checkRelativeUrlNotInSQLPathList(path_list)
212
    self.tic()
213
    self.checkRelativeUrlInSQLPathList(path_list)
Sebastien Robin's avatar
Sebastien Robin committed
214
    person_module.deleteContent('3')
215
    # Now delete things is made with activities
216
    self.checkRelativeUrlNotInSQLPathList(path_list)
217
    self.tic()
218
    self.checkRelativeUrlNotInSQLPathList(path_list)
Sebastien Robin's avatar
Sebastien Robin committed
219

Jérome Perrin's avatar
Jérome Perrin committed
220
  def test_04_SearchFolderWithDeletedObjects(self):
Sebastien Robin's avatar
Sebastien Robin committed
221 222 223
    person_module = self.getPersonModule()
    # Now we will try the same thing as previous test and look at searchFolder
    folder_object_list = [x.getObject().getId() for x in person_module.searchFolder()]
224
    self.assertEqual([],folder_object_list)
225
    person = person_module.newContent(id='4',portal_type='Person',)
226
    self.tic()
Sebastien Robin's avatar
Sebastien Robin committed
227
    folder_object_list = [x.getObject().getId() for x in person_module.searchFolder()]
228
    self.assertEqual(['4'],folder_object_list)
229
    self.tic()
Sebastien Robin's avatar
Sebastien Robin committed
230
    person_module.manage_delObjects('4')
231
    self.tic()
Sebastien Robin's avatar
Sebastien Robin committed
232
    folder_object_list = [x.getObject().getId() for x in person_module.searchFolder()]
233
    self.assertEqual([],folder_object_list)
Sebastien Robin's avatar
Sebastien Robin committed
234

Jérome Perrin's avatar
Jérome Perrin committed
235
  def test_05_SearchFolderWithImmediateReindexObject(self):
Sebastien Robin's avatar
Sebastien Robin committed
236 237 238 239
    person_module = self.getPersonModule()

    # Now we will try the same thing as previous test and look at searchFolder
    folder_object_list = [x.getObject().getId() for x in person_module.searchFolder()]
240
    self.assertEqual([],folder_object_list)
Sebastien Robin's avatar
Sebastien Robin committed
241 242

    person = person_module.newContent(id='4',portal_type='Person')
243
    self.tic()
Sebastien Robin's avatar
Sebastien Robin committed
244
    folder_object_list = [x.getObject().getId() for x in person_module.searchFolder()]
245
    self.assertEqual(['4'],folder_object_list)
Jérome Perrin's avatar
Jérome Perrin committed
246

Sebastien Robin's avatar
Sebastien Robin committed
247
    person_module.manage_delObjects('4')
248
    self.tic()
Sebastien Robin's avatar
Sebastien Robin committed
249
    folder_object_list = [x.getObject().getId() for x in person_module.searchFolder()]
250
    self.assertEqual([],folder_object_list)
Sebastien Robin's avatar
Sebastien Robin committed
251

Jérome Perrin's avatar
Jérome Perrin committed
252
  def test_06_SearchFolderWithRecursiveImmediateReindexObject(self):
Sebastien Robin's avatar
Sebastien Robin committed
253 254 255 256
    person_module = self.getPersonModule()

    # Now we will try the same thing as previous test and look at searchFolder
    folder_object_list = [x.getObject().getId() for x in person_module.searchFolder()]
257
    self.assertEqual([],folder_object_list)
Sebastien Robin's avatar
Sebastien Robin committed
258 259 260 261

    person = person_module.newContent(id='4',portal_type='Person')
    person_module.recursiveImmediateReindexObject()
    folder_object_list = [x.getObject().getId() for x in person_module.searchFolder()]
262
    self.assertEqual(['4'],folder_object_list)
Jérome Perrin's avatar
Jérome Perrin committed
263

Sebastien Robin's avatar
Sebastien Robin committed
264
    person_module.manage_delObjects('4')
265
    self.tic()
Sebastien Robin's avatar
Sebastien Robin committed
266
    folder_object_list = [x.getObject().getId() for x in person_module.searchFolder()]
267
    self.assertEqual([],folder_object_list)
Sebastien Robin's avatar
Sebastien Robin committed
268

Jérome Perrin's avatar
Jérome Perrin committed
269
  def test_07_ClearCatalogAndTestNewContent(self):
Sebastien Robin's avatar
Sebastien Robin committed
270 271 272 273 274 275
    person_module = self.getPersonModule()

    # Clear catalog
    portal_catalog = self.getCatalogTool()
    portal_catalog.manage_catalogClear()

276
    person = person_module.newContent(id='4',portal_type='Person')
277
    self.tic()
Sebastien Robin's avatar
Sebastien Robin committed
278
    folder_object_list = [x.getObject().getId() for x in person_module.searchFolder()]
279
    self.assertEqual(['4'],folder_object_list)
Sebastien Robin's avatar
Sebastien Robin committed
280

Jérome Perrin's avatar
Jérome Perrin committed
281
  def test_08_ClearCatalogAndTestRecursiveImmediateReindexObject(self):
Sebastien Robin's avatar
Sebastien Robin committed
282 283 284 285 286 287 288 289 290
    person_module = self.getPersonModule()

    # Clear catalog
    portal_catalog = self.getCatalogTool()
    portal_catalog.manage_catalogClear()

    person = person_module.newContent(id='4',portal_type='Person')
    person_module.recursiveImmediateReindexObject()
    folder_object_list = [x.getObject().getId() for x in person_module.searchFolder()]
291
    self.assertEqual(['4'],folder_object_list)
Sebastien Robin's avatar
Sebastien Robin committed
292

Jérome Perrin's avatar
Jérome Perrin committed
293
  def test_09_ClearCatalogAndTestImmediateReindexObject(self):
Sebastien Robin's avatar
Sebastien Robin committed
294 295 296 297 298 299 300
    person_module = self.getPersonModule()

    # Clear catalog
    portal_catalog = self.getCatalogTool()
    portal_catalog.manage_catalogClear()

    person = person_module.newContent(id='4',portal_type='Person')
301
    self.tic()
Sebastien Robin's avatar
Sebastien Robin committed
302
    folder_object_list = [x.getObject().getId() for x in person_module.searchFolder()]
303
    self.assertEqual(['4'],folder_object_list)
Sebastien Robin's avatar
Sebastien Robin committed
304

Jérome Perrin's avatar
Jérome Perrin committed
305
  def test_10_OrderedSearchFolder(self):
Sebastien Robin's avatar
Sebastien Robin committed
306 307 308 309 310 311 312
    person_module = self.getPersonModule()

    # Clear catalog
    portal_catalog = self.getCatalogTool()
    portal_catalog.manage_catalogClear()

    person = person_module.newContent(id='a',portal_type='Person',title='a',description='z')
313
    self.tic()
Sebastien Robin's avatar
Sebastien Robin committed
314
    person = person_module.newContent(id='b',portal_type='Person',title='a',description='y')
315
    self.tic()
Sebastien Robin's avatar
Sebastien Robin committed
316
    person = person_module.newContent(id='c',portal_type='Person',title='a',description='x')
317
    self.tic()
318 319
    folder_object_list = [x.getObject().getId()
              for x in person_module.searchFolder(sort_on=[('id','ascending')])]
320
    self.assertEqual(['a','b','c'],folder_object_list)
321 322 323
    folder_object_list = [x.getObject().getId()
              for x in person_module.searchFolder(
              sort_on=[('title','ascending'), ('description','ascending')])]
324
    self.assertEqual(['c','b','a'],folder_object_list)
325 326 327
    folder_object_list = [x.getObject().getId()
              for x in person_module.searchFolder(
              sort_on=[('title','ascending'),('description','descending')])]
328
    self.assertEqual(['a','b','c'],folder_object_list)
Sebastien Robin's avatar
Sebastien Robin committed
329

Jérome Perrin's avatar
Jérome Perrin committed
330
  def test_11_CastStringAsInt(self):
331 332 333 334 335 336 337
    person_module = self.getPersonModule()

    # Clear catalog
    portal_catalog = self.getCatalogTool()
    portal_catalog.manage_catalogClear()

    person = person_module.newContent(id='a',portal_type='Person',title='1')
338
    self.tic()
339
    person = person_module.newContent(id='b',portal_type='Person',title='2')
340
    self.tic()
341
    person = person_module.newContent(id='c',portal_type='Person',title='12')
342
    self.tic()
343
    folder_object_list = [x.getObject().getTitle() for x in person_module.searchFolder(sort_on=[('title','ascending')])]
344
    self.assertEqual(['1','12','2'],folder_object_list)
345
    folder_object_list = [x.getObject().getTitle() for x in person_module.searchFolder(sort_on=[('title','ascending','int')])]
346
    self.assertEqual(['1','2','12'],folder_object_list)
347

Jérome Perrin's avatar
Jérome Perrin committed
348
  def test_12_TransactionalUidBuffer(self):
349 350
    portal_catalog = self.getCatalogTool()
    catalog = portal_catalog.getSQLCatalog()
351
    self.assertTrue(catalog is not None)
352 353

    # Clear out the uid buffer.
354 355 356 357 358
    #from Products.ZSQLCatalog.SQLCatalog import uid_buffer_dict, get_ident
    #uid_buffer_key = get_ident()
    #if uid_buffer_key in uid_buffer_dict:
    #  del uid_buffer_dict[uid_buffer_key]
    def getUIDBuffer(*args, **kw):
359 360
      with catalog.__class__._reserved_uid_lock:
        return catalog.getUIDBuffer(*args, **kw)
361 362

    getUIDBuffer(force_new_buffer=True)
363 364 365

    # Need to abort a transaction artificially, so commit the current
    # one, first.
366
    self.commit()
367 368

    catalog.newUid()
369
    uid_buffer = getUIDBuffer()
370
    self.assertTrue(len(uid_buffer) > 0)
Sebastien Robin's avatar
Sebastien Robin committed
371

372
    self.abort()
373
    uid_buffer = getUIDBuffer()
374
    self.assertTrue(len(uid_buffer) == 0)
Romain Courteaud's avatar
Romain Courteaud committed
375

Jérome Perrin's avatar
Jérome Perrin committed
376
  def test_13_ERP5Site_reindexAll(self):
Romain Courteaud's avatar
Romain Courteaud committed
377 378 379 380 381 382 383 384 385 386 387 388
    # Flush message queue
    self.tic()
    # Create some objects
    portal = self.getPortal()
    portal_category = self.getCategoryTool()
    base_category = portal_category.newContent(portal_type='Base Category',
                                               title="GreatTitle1")
    module = portal.getDefaultModule('Organisation')
    organisation = module.newContent(portal_type='Organisation',
                                     title="GreatTitle2")
    # Flush message queue
    self.tic()
389
    original_path_list = self.getSQLPathList()
Romain Courteaud's avatar
Romain Courteaud committed
390 391 392
    # Clear catalog
    portal_catalog = self.getCatalogTool()
    portal_catalog.manage_catalogClear()
393
    sql_connection = self.getSQLConnection()
Romain Courteaud's avatar
Romain Courteaud committed
394 395 396
    sql = 'select count(*) from catalog where portal_type!=NULL'
    result = sql_connection.manage_test(sql)
    message_count = result[0]['COUNT(*)']
397
    self.assertEqual(0, message_count)
Romain Courteaud's avatar
Romain Courteaud committed
398
    # Commit
399
    self.commit()
Romain Courteaud's avatar
Romain Courteaud committed
400 401 402
    # Reindex all
    portal.ERP5Site_reindexAll()
    self.tic()
403
    self.commit()
Romain Courteaud's avatar
Romain Courteaud committed
404 405 406 407
    # Check catalog
    sql = 'select count(*) from message'
    result = sql_connection.manage_test(sql)
    message_count = result[0]['COUNT(*)']
408
    self.assertEqual(0, message_count)
409 410
    # Check if all objects are catalogued as before
    new_path_list = self.getSQLPathList()
411
    self.assertTrue(set(original_path_list).issubset(new_path_list))
412

Jérome Perrin's avatar
Jérome Perrin committed
413 414 415
  def test_14_ReindexWithBrokenCategory(self):
    """Reindexing an object with 1 broken category must not affect other valid
    categories"""
416 417 418
    # Flush message queue
    self.tic()
    # Create some objects
Jérome Perrin's avatar
Jérome Perrin committed
419
    portal = self.portal
420 421 422 423 424 425 426 427
    portal_category = self.getCategoryTool()
    group_nexedi_category = portal_category.group\
                                .newContent( id = 'nexedi', )
    region_europe_category = portal_category.region\
                                .newContent( id = 'europe', )
    module = portal.getDefaultModule('Organisation')
    organisation = module.newContent(portal_type='Organisation',)
    organisation.setGroup('nexedi')
428
    self.assertEqual(organisation.getGroupValue(), group_nexedi_category)
429
    organisation.setRegion('europe')
430
    self.assertEqual(organisation.getRegionValue(), region_europe_category)
431
    organisation.setRole('not_exists')
432
    self.assertEqual(organisation.getRoleValue(), None)
433 434 435 436 437
    # Flush message queue
    self.tic()
    # Clear catalog
    portal_catalog = self.getCatalogTool()
    portal_catalog.manage_catalogClear()
438
    sql_connection = self.getSQLConnection()
439

440 441 442 443 444
    sql = 'SELECT COUNT(*) FROM category '\
        'WHERE uid=%s and category_strict_membership = 1' %\
        organisation.getUid()
    result = sql_connection.manage_test(sql)
    message_count = result[0]['COUNT(*)']
445
    self.assertEqual(0, message_count)
446 447 448
    # Commit
    self.tic()
    # Check catalog
449 450 451
    organisation.reindexObject()
    # Commit
    self.tic()
452 453 454
    sql = 'select count(*) from message'
    result = sql_connection.manage_test(sql)
    message_count = result[0]['COUNT(*)']
455
    self.assertEqual(0, message_count)
456 457 458 459 460 461 462 463 464 465 466
    # Check region and group categories are catalogued
    for base_cat, theorical_count in {
                                      'region':1,
                                      'group':1,
                                      'role':0}.items() :
      sql = """SELECT COUNT(*) FROM category
            WHERE category.uid=%s and category.category_strict_membership = 1
            AND category.base_category_uid = %s""" % (organisation.getUid(),
                    portal_category[base_cat].getUid())
      result = sql_connection.manage_test(sql)
      cataloged_obj_count = result[0]['COUNT(*)']
467
      self.assertEqual(theorical_count, cataloged_obj_count,
468 469
            'category %s is not cataloged correctly' % base_cat)

Jérome Perrin's avatar
Jérome Perrin committed
470
  def test_15_getObject(self,):
471
    # portal_catalog.getObject raises a ValueError if UID parameter is a string
472
    portal_catalog = self.getCatalogTool()
473
    self.assertRaises(ValueError, portal_catalog.getObject, "StringUID")
Jérome Perrin's avatar
Jérome Perrin committed
474

475 476
    obj = self._makeOrganisation()
    # otherwise it returns the object
477
    self.assertEqual(obj, portal_catalog.getObject(obj.getUid()).getObject())
478 479
    # but raises KeyError if object is not in catalog
    self.assertRaises(KeyError, portal_catalog.getObject, sys.maxint)
Jérome Perrin's avatar
Jérome Perrin committed
480

481
  def test_getRecordForUid(self):
482 483
    portal_catalog = self.getCatalogTool()
    obj = self._makeOrganisation()
484
    self.assertEqual(obj,
485
        portal_catalog.getSQLCatalog().getRecordForUid(obj.getUid()).getObject())
Jérome Perrin's avatar
Jérome Perrin committed
486

487 488 489
  def test_path(self):
    portal_catalog = self.getCatalogTool()
    obj = self._makeOrganisation()
490
    self.assertEqual(obj.getPath(), portal_catalog.getpath(obj.getUid()))
491 492
    self.assertRaises(KeyError, portal_catalog.getpath, sys.maxint)

Jérome Perrin's avatar
Jérome Perrin committed
493
  def test_16_newUid(self):
494 495 496 497 498 499
    # newUid should not assign the same uid
    portal_catalog = self.getCatalogTool()
    from Products.ZSQLCatalog.SQLCatalog import UID_BUFFER_SIZE
    uid_dict = {}
    for i in xrange(UID_BUFFER_SIZE * 3):
      uid = portal_catalog.newUid()
500 501
      self.assertTrue(isinstance(uid, long))
      self.assertFalse(uid in uid_dict)
502
      uid_dict[uid] = None
Jérome Perrin's avatar
Jérome Perrin committed
503 504

  def test_17_CreationDate_ModificationDate(self):
505 506
    portal_catalog = self.getCatalogTool()
    portal = self.getPortal()
507
    sql_connection = self.getSQLConnection()
Jérome Perrin's avatar
Jérome Perrin committed
508

509 510
    module = portal.getDefaultModule('Organisation')
    organisation = module.newContent(portal_type='Organisation',)
511 512
    creation_date = organisation.getCreationDate().toZone('UTC').ISO()
    modification_date = organisation.getModificationDate().toZone('UTC').ISO()
513
    self.commit()
514
    now = DateTime()
515
    self.tic()
516
    sql = """select creation_date, modification_date
517 518
             from catalog where uid = %s""" % organisation.getUid()
    result = sql_connection.manage_test(sql)
519
    self.assertEqual(creation_date,
520
                      result[0]['creation_date'].ISO())
521
    self.assertEqual(modification_date,
522
                      result[0]['modification_date'].ISO())
523
    self.assertEqual(creation_date,
524
                      result[0]['modification_date'].ISO())
Jérome Perrin's avatar
Jérome Perrin committed
525

526 527 528 529
    import time; time.sleep(3)
    organisation.edit(title='edited')
    self.tic()
    result = sql_connection.manage_test(sql)
530
    self.assertEqual(creation_date, result[0]['creation_date'].ISO())
531 532 533
    modification_date = organisation.getModificationDate().toZone('UTC').ISO()
    self.assertNotEquals(modification_date,
                         organisation.getCreationDate())
534 535 536
    # This test was first written with a now variable initialized with
    # DateTime(). But since we are never sure of the time required in
    # order to execute some line of code
537
    self.assertEqual(modification_date,
538
                      result[0]['modification_date'].ISO())
539 540
    self.assertTrue(organisation.getModificationDate()>now)
    self.assertTrue(result[0]['creation_date']<result[0]['modification_date'])
Jérome Perrin's avatar
Jérome Perrin committed
541

542 543
  # TODO: this test is disabled (and maybe not complete), because this feature
  # is not implemented
Jérome Perrin's avatar
Jérome Perrin committed
544 545
  @todo_erp5
  def test_18_buildSQLQueryAnotherTable(self):
546 547 548 549 550
    """Tests that buildSQLQuery works with another query_table than 'catalog'"""
    portal = self.getPortal()
    portal_catalog = self.getCatalogTool()
    # clear catalog
    portal_catalog.manage_catalogClear()
551
    self.commit()
552

553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569
    # create some content to use destination_section_title as related key
    # FIXME: create the related key here ?
    module = portal.getDefaultModule('Organisation')
    source_organisation = module.newContent( portal_type='Organisation',
                                        title = 'source_organisation')
    destination_organisation = module.newContent( portal_type='Organisation',
                                        title = 'destination_organisation')
    source_organisation.setDestinationSectionValue(destination_organisation)
    source_organisation.recursiveReindexObject()
    destination_organisation.recursiveReindexObject()
    self.tic()

    # buildSQLQuery can use arbitrary table name.
    query_table = "node"
    sql_squeleton = """
    SELECT %(query_table)s.uid,
           %(query_table)s.id
570
    FROM
571
      <dtml-in prefix="table" expr="from_table_list">
572 573
        <dtml-var table_item> AS <dtml-var table_key>
        <dtml-unless sequence-end>, </dtml-unless>
574 575
      </dtml-in>
    <dtml-if where_expression>
576
    WHERE
577 578 579 580 581 582
      <dtml-var where_expression>
    </dtml-if>
    <dtml-if order_by_expression>
      ORDER BY <dtml-var order_by_expression>
    </dtml-if>
    """ % {'query_table' : query_table}
583

584 585 586 587 588 589 590 591 592 593
    portal_skins_custom = portal.portal_skins.custom
    portal_skins_custom.manage_addProduct['ZSQLMethods'].manage_addZSQLMethod(
          id = 'testMethod',
          title = '',
          connection_id = 'erp5_sql_connection',
          arguments = "\n".join([ 'from_table_list',
                                  'where_expression',
                                  'order_by_expression' ]),
          template = sql_squeleton)
    testMethod = portal_skins_custom['testMethod']
594

595 596 597 598 599
    default_parametrs = {}
    default_parametrs['portal_type'] = 'Organisation'
    default_parametrs['from_table_list'] = {}
    default_parametrs['where_expression'] = ""
    default_parametrs['order_by_expression'] = None
600

601 602 603 604 605
    # check that we retrieve our 2 organisations by default.
    kw = default_parametrs.copy()
    kw.update( portal_catalog.buildSQLQuery(
                  query_table = query_table,
                  **kw) )
606 607
    LOG('kw', 0, kw)
    LOG('SQL', 0, testMethod(src__=1, **kw))
608
    self.assertEqual(len(testMethod(**kw)), 2)
609

610 611 612 613 614 615
    # check we can make a simple filter on title.
    kw = default_parametrs.copy()
    kw.update( portal_catalog.buildSQLQuery(
                  query_table = query_table,
                  title = 'source_organisation',
                  **kw) )
616 617
    LOG('kw', 1, kw)
    LOG('SQL', 1, testMethod(src__=1, **kw))
618
    self.assertEqual( len(testMethod(**kw)), 1,
619
                       testMethod(src__=1, **kw) )
620
    self.assertEqual( testMethod(**kw)[0]['uid'],
621 622
                        source_organisation.getUid(),
                        testMethod(src__=1, **kw) )
623

624 625 626 627 628 629
    # check sort
    kw = default_parametrs.copy()
    kw.update(portal_catalog.buildSQLQuery(
                  query_table = query_table,
                  sort_on = [('id', 'ascending')],
                  **kw))
630 631
    LOG('kw', 2, kw)
    LOG('SQL', 2, testMethod(src__=1, **kw))
632
    brains = testMethod(**kw)
633
    self.assertEqual( len(brains), 2,
634
                       testMethod(src__=1, **kw))
635
    self.assertFalse( brains[0]['id'] > brains[1]['id'],
636
                 testMethod(src__=1, **kw) )
637

638 639 640 641 642 643
    # check related keys works
    kw = default_parametrs.copy()
    kw.update(portal_catalog.buildSQLQuery(
                  query_table = query_table,
                  destination_section_title = 'organisation_destination'),
                  **kw)
644 645
    LOG('kw', 3, kw)
    LOG('SQL', 3, testMethod(src__=1, **kw))
646
    brains = testMethod(**kw)
647 648
    self.assertEqual( len(brains), 1, testMethod(src__=1, **kw) )
    self.assertEqual( brains[0]['uid'],
649 650
                       source_organisation.getUid(),
                       testMethod(src__=1, **kw) )
Sebastien Robin's avatar
Sebastien Robin committed
651

Jérome Perrin's avatar
Jérome Perrin committed
652
  def test_19_SearchFolderWithNonAsciiCharacter(self):
Sebastien Robin's avatar
Sebastien Robin committed
653 654
    person_module = self.getPersonModule()

655
    title = 'Sébastien'
Sebastien Robin's avatar
Sebastien Robin committed
656
    person = person_module.newContent(id='5',portal_type='Person',title=title)
657
    self.tic()
Sebastien Robin's avatar
Sebastien Robin committed
658
    folder_object_list = [x.getObject().getId() for x in person_module.searchFolder()]
659
    self.assertEqual(['5'],folder_object_list)
660
    folder_object_list = [x.getObject().getId() for x in
Sebastien Robin's avatar
Sebastien Robin committed
661
                              person_module.searchFolder(title=title)]
662
    self.assertEqual(['5'],folder_object_list)
663

664 665 666 667 668
  def test_Collation(self):
    person_module = self.getPersonModule()

    title = 'Sébastien'
    person = person_module.newContent(id='5',portal_type='Person', title=title)
669
    self.tic()
670 671
    folder_object_list = [x.getObject().getId() for x in
                              person_module.searchFolder(title=title)]
672
    self.assertEqual(['5'],folder_object_list)
673 674 675 676

    # Searching for Sebastien should also find Sébastien
    folder_object_list = [x.getObject().getId() for x in
                              person_module.searchFolder(title='Sebastien')]
677
    self.assertEqual(['5'],folder_object_list)
678 679 680 681

    # Same for sebastien, as catalog searches are case insensitive
    folder_object_list = [x.getObject().getId() for x in
                              person_module.searchFolder(title='sebastien')]
682
    self.assertEqual(['5'],folder_object_list)
683

684

Jérome Perrin's avatar
Jérome Perrin committed
685
  def test_20_SearchFolderWithDynamicRelatedKey(self):
686 687 688 689 690 691 692
    # Create some objects
    portal = self.getPortal()
    portal_category = self.getCategoryTool()
    portal_category.group.manage_delObjects([x for x in
        portal_category.group.objectIds()])
    group_nexedi_category = portal_category.group\
                                .newContent( id = 'nexedi', title='Nexedi',
693
                                             reference='a')
694 695
    group_nexedi_category2 = portal_category.group\
                                .newContent( id = 'storever', title='Storever',
696
                                             reference='b')
697 698 699
    module = portal.getDefaultModule('Organisation')
    organisation = module.newContent(portal_type='Organisation',)
    organisation.setGroup('nexedi')
700
    self.assertEqual(organisation.getGroupValue(), group_nexedi_category)
701 702
    organisation2 = module.newContent(portal_type='Organisation',)
    organisation2.setGroup('storever')
703
    organisation2.setTitle('Organisation 2')
704
    self.assertEqual(organisation2.getGroupValue(), group_nexedi_category2)
705 706 707 708
    # Flush message queue
    self.tic()

    # Try to get the organisation with the group title Nexedi
709
    organisation_list = [x.getObject() for x in
710
                         module.searchFolder(group_title='Nexedi')]
711
    self.assertEqual(organisation_list,[organisation])
712
    organisation_list = [x.getObject() for x in
713
                         module.searchFolder(default_group_title='Nexedi')]
714
    self.assertEqual(organisation_list,[organisation])
715 716
    # Try to get the organisation with the group id
    organisation_list = [x.getObject() for x in
717
                         module.searchFolder(group_id='storever')]
718
    self.assertEqual(organisation_list,[organisation2])
719
    # Try to get the organisation with the group reference 'a'
720
    organisation_list = [x.getObject() for x in
721
                         module.searchFolder(group_reference='a')]
722
    self.assertEqual(organisation_list,[organisation])
723
    # Try to get the organisation with the group reference 'c'
724
    organisation_list = [x.getObject() for x in
725
                         module.searchFolder(group_reference='c')]
726
    self.assertEqual(organisation_list,[])
727
    # Try to get the organisation with the default group reference 'c'
728
    organisation_list = [x.getObject() for x in
729
                         module.searchFolder(default_group_reference='c')]
730
    self.assertEqual(organisation_list,[])
731 732
    # Try to get the organisation with group relative_url
    group_relative_url = group_nexedi_category.getRelativeUrl()
733
    organisation_list = [x.getObject() for x in
734
                 module.searchFolder(group_relative_url=group_relative_url)]
735
    self.assertEqual(organisation_list, [organisation])
736
    # Try to get the organisation with group uid
737
    organisation_list = [x.getObject() for x in
738
                 module.searchFolder(group_uid=group_nexedi_category.getUid())]
739
    self.assertEqual(organisation_list, [organisation])
740
    # Try to get the organisation with the group id AND title of the document
741
    organisation_list = [x.getObject() for x in
742 743
                         module.searchFolder(group_id='storever',
                                             title='Organisation 2')]
744
    self.assertEqual(organisation_list,[organisation2])
745

746

Jérome Perrin's avatar
Jérome Perrin committed
747
  def test_21_SearchFolderWithDynamicStrictRelatedKey(self):
748 749 750 751 752 753 754
    # Create some objects
    portal = self.getPortal()
    portal_category = self.getCategoryTool()
    portal_category.group.manage_delObjects([x for x in
        portal_category.group.objectIds()])
    group_nexedi_category = portal_category.group\
                                .newContent( id = 'nexedi', title='Nexedi',
755
                                             reference='a')
756 757
    sub_group_nexedi = group_nexedi_category\
                                .newContent( id = 'erp5', title='ERP5',
758
                                             reference='b')
759 760 761
    module = portal.getDefaultModule('Organisation')
    organisation = module.newContent(portal_type='Organisation',)
    organisation.setGroup('nexedi/erp5')
762
    self.assertEqual(organisation.getGroupValue(), sub_group_nexedi)
763 764 765 766
    # Flush message queue
    self.tic()

    # Try to get the organisation with the group title Nexedi
767
    organisation_list = [x.getObject() for x in
768
                         module.searchFolder(strict_group_title='Nexedi')]
769
    self.assertEqual(organisation_list,[])
770
    # Try to get the organisation with the group title ERP5
771
    organisation_list = [x.getObject() for x in
772
                         module.searchFolder(strict_group_title='ERP5')]
773
    self.assertEqual(organisation_list,[organisation])
774
    # Try to get the organisation with the group reference a
775
    organisation_list = [x.getObject() for x in
776
                         module.searchFolder(strict_group_reference='a')]
777
    self.assertEqual(organisation_list,[])
778
    # Try to get the organisation with the group reference b
779
    organisation_list = [x.getObject() for x in
780
                         module.searchFolder(strict_group_reference='b')]
781
    self.assertEqual(organisation_list,[organisation])
782

Jérome Perrin's avatar
Jérome Perrin committed
783
  def test_22_SearchingWithUnicode(self):
784 785 786 787
    person_module = self.getPersonModule()
    person_module.newContent(portal_type='Person', title='A Person')
    self.tic()
    self.assertNotEquals([], self.getCatalogTool().searchResults(
788
                                     portal_type='Person', title=u'A Person'))
Jérome Perrin's avatar
Jérome Perrin committed
789 790

  def test_23_DeleteObjectRaiseErrorWhenQueryFail(self):
791 792 793 794
    portal_catalog = self.getCatalogTool()
    person_module = self.getPersonModule()
    # Now we will ask to immediatly reindex
    person = person_module.newContent(id='2',
795
                                      portal_type='Person',)
796
    self.tic()
797
    path_list = [person.getRelativeUrl()]
798
    self.checkRelativeUrlInSQLPathList(path_list)
799 800 801 802 803
    # We will delete the connector
    # in order to make sure it will not work any more
    portal = self.getPortal()
    portal.manage_delObjects('erp5_sql_connection')
    # Then it must be impossible to delete an object
804 805 806
    uid = person.getUid()
    unindex = portal_catalog.unindexObject
    self.assertRaises(AttributeError,unindex,person,uid=person.getUid())
807
    self.abort()
Sebastien Robin's avatar
Sebastien Robin committed
808

Jérome Perrin's avatar
Jérome Perrin committed
809
  def test_24_SortOn(self):
810
    self.assertTrue(
811
            self.getCatalogTool().buildSQLQuery(
812
            sort_on=(('catalog.title', 'ascending'),))['order_by_expression'] in \
813
            ('catalog.title', '`catalog`.`title` ASC', 'catalog.title ASC'))
814

Jérome Perrin's avatar
Jérome Perrin committed
815
  def test_25_SortOnDescending(self):
816
    self.assertTrue(
817
            self.getCatalogTool().buildSQLQuery(
818 819
            sort_on=(('catalog.title', 'descending'),))['order_by_expression'] in \
            ('catalog.title DESC', '`catalog`.`title` DESC'))
Jérome Perrin's avatar
Jérome Perrin committed
820 821

  def test_26_SortOnUnknownKeys(self):
822
    self.assertEqual('',
823
          self.getCatalogTool().buildSQLQuery(select_list=('uid', 'path'),
824
          sort_on=(('ignored', 'ascending'),))['order_by_expression'])
Jérome Perrin's avatar
Jérome Perrin committed
825 826

  def test_27_SortOnAmbigousKeys(self):
827 828
    # if the sort key is found on the catalog table, it will use that catalog
    # table.
829
    self.assertTrue(
830
          self.getCatalogTool().buildSQLQuery(
831 832
          sort_on=(('title', 'ascending'),))['order_by_expression'] in \
          ('catalog.title', '`catalog`.`title` ASC'))
Jérome Perrin's avatar
Jérome Perrin committed
833

834 835
    # if not found on catalog, it won't do any filtering
    # in the above, start_date exists both in delivery and movement table.
836 837 838
    self.assertRaises(ValueError, self.getCatalogTool().buildSQLQuery,
          sort_on=(('start_date', 'ascending'),))

839
    # of course, in that case, it's possible to prefix with table name
840
    self.assertTrue(
841 842
          self.getCatalogTool().buildSQLQuery(
          sort_on=(('delivery.start_date', 'ascending'),
843 844
                    ))['order_by_expression'] in \
          ('delivery.start_date', 'delivery.start_date ASC'))
Jérome Perrin's avatar
Jérome Perrin committed
845 846

  def test_28_SortOnMultipleKeys(self):
847
    self.assertTrue(
848 849 850
              self.getCatalogTool().buildSQLQuery(
              sort_on=(('catalog.title', 'ascending'),
                       ('catalog.id', 'asc')))
851
                       ['order_by_expression'].replace(' ', '') in \
852
              ('catalog.title,catalog.id', '`catalog`.`title`ASC,`catalog`.`id`ASC', 'catalog.titleASC,catalog.idASC'))
853

Jérome Perrin's avatar
Jérome Perrin committed
854
  def test_29_SortOnRelatedKey(self):
855 856
    """Sort-on parameter and related key. (Assumes that region_title is a \
    valid related key)"""
857
    self.assertTrue(
858 859
              self.getCatalogTool().buildSQLQuery(region_reference='foo',
              sort_on=(('region_reference', 'ascending'),))['order_by_expression'].endswith('.`reference` ASC'))
860
    self.assertTrue(
861 862
              self.getCatalogTool().buildSQLQuery(region_reference='foo',
              sort_on=(('region_reference', 'descending'),))['order_by_expression'].endswith('.`reference` DESC'))
863 864
    self.assertTrue(
              self.getCatalogTool().buildSQLQuery(
865
              sort_on=(('region_reference', 'ascending'),))['order_by_expression'].endswith('.`reference` ASC'),
866 867 868
              'sort_on parameter must be taken into account even if related key '
              'is not a parameter of the current query')
    self.assertTrue(
869
              self.getCatalogTool().buildSQLQuery(
870
              sort_on=(('region_reference', 'descending'),))['order_by_expression'].endswith('.`reference` DESC'),
871 872 873
              'sort_on parameter must be taken into account even if related key '
              'is not a parameter of the current query')

874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915
  def test_sortOnRelatedKeyWithUnsetRelation(self):
    """
      Check that sorting on a related key does not filter out objects for
      which the relation is not set.
    """
    portal = self.getPortalObject()
    organisation = portal.organisation_module.\
                   newContent(portal_type="Organisation")
    person_module = portal.person_module
    person_1 = person_module.newContent(portal_type="Person")
    person_2 = person_module.newContent(portal_type="Person",
                 career_subordination_value=organisation)
    self.tic()
    self.assertEqual(len(person_module.searchFolder()),
                     len(person_module.searchFolder(sort_on=[('subordination_title', 'ascending')])))

  def test_sortOnRelatedKeyWithoutLeftJoinSupport(self):
    """Check that sorting on a related key that does not support left join.
    """
    portal = self.getPortalObject()
    org_a = self._makeOrganisation(title='abc', default_address_city='abc')

    # now turn the z_related_grand_parent into an old-style method, without
    # RELATED_QUERY_SEPARATOR
    method = portal.portal_catalog.getSQLCatalog().z_related_grand_parent
    old_src = method.src

    @self._addCleanup
    def cleanGrandParentMethod(self):
      method.manage_edit(method.title, method.connection_id,
                         method.arguments_src, old_src)

    src = old_src.replace('<dtml-var RELATED_QUERY_SEPARATOR>', ' AND ')
    method.manage_edit(method.title, method.connection_id, method.arguments_src,
                       src)

    query = dict(grand_parent_portal_type="Organisation Module",
                 parent_reference=org_a.getReference())
    self.tic()
    self.assertNotEquals(0, len(portal.portal_catalog(
      portal_type='Address',
      sort_on=[('grand_parent_portal_type', 'ascending')])))
916

917 918 919 920 921 922 923 924 925 926 927 928 929 930
  def _makeOrganisation(self, **kw):
    """Creates an Organisation in it's default module and reindex it.
    By default, it creates a group/nexedi category, and make the organisation a
    member of this category.
    """
    group_cat = self.getCategoryTool().group
    if not hasattr(group_cat, 'nexedi'):
      group_cat.newContent(id='nexedi', title='Nexedi Group',)
    module = self.getPortal().getDefaultModule('Organisation')
    organisation = module.newContent(portal_type='Organisation')
    kw.setdefault('group', 'group/nexedi')
    organisation.edit(**kw)
    self.tic()
    return organisation
Jérome Perrin's avatar
Jérome Perrin committed
931 932

  def test_30_SimpleQueryDict(self):
933 934 935 936
    """use a dict as a keyword parameter.
    """
    organisation_title = 'Nexedi Organisation'
    organisation = self._makeOrganisation(title=organisation_title)
937
    self.assertEqual([organisation.getPath()],
938 939 940
        [x.path for x in self.getCatalogTool()(
                title={'query': organisation_title})])

Jérome Perrin's avatar
Jérome Perrin committed
941
  def test_31_RelatedKeySimpleQueryDict(self):
942 943 944
    """use a dict as a keyword parameter, but using a related key
    """
    organisation = self._makeOrganisation()
945
    self.assertEqual([organisation.getPath()],
946 947 948 949 950 951
        [x.path for x in self.getCatalogTool()(
                group_title={'query': 'Nexedi Group'},
                # have to filter on portal type, because the group category is
                # also member of itself
                portal_type=organisation.getPortalTypeName())])

Jérome Perrin's avatar
Jérome Perrin committed
952
  def test_32_SimpleQueryDictWithOrOperator(self):
953 954 955 956
    """use a dict as a keyword parameter, with OR operator.
    """
    organisation_title = 'Nexedi Organisation'
    organisation = self._makeOrganisation(title=organisation_title)
957

958
    self.assertEqual([organisation.getPath()],
959
        [x.path for x in self.getCatalogTool()(
960 961
                **{'catalog.title':{'query': (organisation_title, 'something else'),
                                    'operator': 'or'}})])
962

Jérome Perrin's avatar
Jérome Perrin committed
963
  def test_33_SimpleQueryDictWithAndOperator(self):
964 965 966 967
    """use a dict as a keyword parameter, with AND operator.
    """
    organisation_title = 'Nexedi Organisation'
    organisation = self._makeOrganisation(title=organisation_title)
968

969
    self.assertEqual([organisation.getPath()],
970 971 972 973 974
        [x.path for x in self.getCatalogTool()(
                # this is useless, we must find a better use case
                title={'query': (organisation_title, organisation_title),
                       'operator': 'and'})])

Jérome Perrin's avatar
Jérome Perrin committed
975
  def test_34_SimpleQueryDictWithMaxRangeParameter(self):
976 977 978 979 980
    """use a dict as a keyword parameter, with max range parameter ( < )
    """
    org_a = self._makeOrganisation(title='A')
    org_b = self._makeOrganisation(title='B')
    org_c = self._makeOrganisation(title='C')
Jérome Perrin's avatar
Jérome Perrin committed
981

982
    self.assertEqual([org_a.getPath()],
983 984 985
        [x.path for x in self.getCatalogTool()(
                portal_type='Organisation',
                title={'query': 'B', 'range': 'max'})])
Jérome Perrin's avatar
Jérome Perrin committed
986 987

  def test_35_SimpleQueryDictWithMinRangeParameter(self):
988 989 990 991 992
    """use a dict as a keyword parameter, with min range parameter ( >= )
    """
    org_a = self._makeOrganisation(title='A')
    org_b = self._makeOrganisation(title='B')
    org_c = self._makeOrganisation(title='C')
Jérome Perrin's avatar
Jérome Perrin committed
993

994 995 996 997 998 999
    self.failIfDifferentSet([org_b.getPath(), org_c.getPath()],
        [x.path for x in self.getCatalogTool()(
                portal_type='Organisation',
                title={'query': 'B', 'range': 'min'})])


Jérome Perrin's avatar
Jérome Perrin committed
1000
  def test_36_SimpleQueryDictWithNgtRangeParameter(self):
1001 1002 1003 1004 1005
    """use a dict as a keyword parameter, with ngt range parameter ( <= )
    """
    org_a = self._makeOrganisation(title='A')
    org_b = self._makeOrganisation(title='B')
    org_c = self._makeOrganisation(title='C')
Jérome Perrin's avatar
Jérome Perrin committed
1006

1007 1008 1009 1010 1011
    self.failIfDifferentSet([org_a.getPath(), org_b.getPath()],
        [x.path for x in self.getCatalogTool()(
                portal_type='Organisation',
                title={'query': 'B', 'range': 'ngt'})])

Jérome Perrin's avatar
Jérome Perrin committed
1012
  def test_37_SimpleQueryDictWithMinMaxRangeParameter(self):
1013 1014 1015 1016 1017
    """use a dict as a keyword parameter, with minmax range parameter ( >=  < )
    """
    org_a = self._makeOrganisation(title='A')
    org_b = self._makeOrganisation(title='B')
    org_c = self._makeOrganisation(title='C')
Jérome Perrin's avatar
Jérome Perrin committed
1018

1019
    self.assertEqual([org_b.getPath()],
1020 1021 1022
        [x.path for x in self.getCatalogTool()(
                portal_type='Organisation',
                title={'query': ('B', 'C'), 'range': 'minmax'})])
Jérome Perrin's avatar
Jérome Perrin committed
1023 1024

  def test_38_SimpleQueryDictWithMinNgtRangeParameter(self):
1025 1026 1027 1028 1029
    """use a dict as a keyword parameter, with minngt range parameter ( >= <= )
    """
    org_a = self._makeOrganisation(title='A')
    org_b = self._makeOrganisation(title='B')
    org_c = self._makeOrganisation(title='C')
Jérome Perrin's avatar
Jérome Perrin committed
1030

1031 1032 1033 1034 1035
    self.failIfDifferentSet([org_b.getPath(), org_c.getPath()],
        [x.path for x in self.getCatalogTool()(
                portal_type='Organisation',
                title={'query': ('B', 'C'), 'range': 'minngt'})])

1036 1037 1038 1039 1040 1041
  def test_QueryDictFromRequest(self):
    """use a dict from REQUEST as a keyword parameter.
    """
    org_a = self._makeOrganisation(title='A')
    org_b = self._makeOrganisation(title='B')
    org_c = self._makeOrganisation(title='C')
Jérome Perrin's avatar
Jérome Perrin committed
1042

1043 1044 1045 1046 1047 1048
    query_dict = {'query': ('B', 'C'), 'range': 'minngt'}
    from ZPublisher.HTTPRequest import record
    query_record = record()
    for k, v in query_dict.items():
      setattr(query_record, k, v)

1049 1050 1051
    self.assertEqual({org_b.getPath(), org_c.getPath()},
      {x.path for x in self.getCatalogTool()(portal_type='Organisation',
                                             title=query_record)})
1052

Jérome Perrin's avatar
Jérome Perrin committed
1053
  def test_39_DeferredConnection(self):
1054 1055 1056 1057 1058
    """ERP5Catalog uses a deferred connection for full text indexing.
    """
    erp5_sql_deferred_connection = getattr(self.getPortal(),
                                    'erp5_sql_deferred_connection',
                                    None)
1059 1060
    self.assertTrue(erp5_sql_deferred_connection is not None)
    self.assertEqual('Z MySQL Deferred Database Connection',
1061
                      erp5_sql_deferred_connection.meta_type)
1062 1063
    for method in ['z0_uncatalog_fulltext',
                   'z_catalog_fulltext_list']:
1064
      self.assertEqual('erp5_sql_deferred_connection',
1065 1066 1067
                getattr(self.getCatalogTool().getSQLCatalog(),
                              method).connection_id)

Jérome Perrin's avatar
Jérome Perrin committed
1068
  def test_40_DeleteObject(self):
1069 1070 1071 1072 1073 1074 1075
    """Simple test to exercise object deletion
    """
    folder = self.getOrganisationModule()
    ob = folder.newContent()
    self.tic()
    folder.manage_delObjects([ob.getId()])
    self.tic()
1076
    self.assertEqual(0, len(folder.searchFolder()))
1077

Jérome Perrin's avatar
Jérome Perrin committed
1078
  def test_41_ProxyRolesInRestrictedPython(self):
1079 1080 1081 1082 1083 1084 1085 1086
    """test that proxy roles apply to catalog queries within python scripts
    """
    perm = 'View'

    uf = self.getPortal().acl_users
    uf._doAddUser('alice', '', ['Member', 'Manager', 'Assignor'], [])
    uf._doAddUser('bob', '', ['Member'], [])
    # create restricted object
1087
    self.loginByUserName('alice')
1088 1089 1090 1091 1092 1093 1094 1095 1096
    folder = self.getOrganisationModule()
    ob = folder.newContent()
    # make sure permissions are correctly set
    folder.manage_permission('Access contents information', ['Member'], 1)
    folder.manage_permission(perm, ['Member'], 1)
    ob.manage_permission('Access contents information', ['Member'], 1)
    ob.manage_permission(perm, ['Manager'], 0)
    self.tic()
    # check access
1097 1098
    self.assertEqual(1, getSecurityManager().checkPermission(perm, folder))
    self.assertEqual(1, getSecurityManager().checkPermission(perm, ob))
1099
    self.loginByUserName('bob')
1100 1101
    self.assertEqual(1, getSecurityManager().checkPermission(perm, folder))
    self.assertEqual(None, getSecurityManager().checkPermission(perm, ob))
1102
    # add a script that calls a catalog method
1103
    self.loginByUserName('alice')
1104 1105 1106 1107
    script = createZODBPythonScript(self.getPortal().portal_skins.custom,
        'catalog_test_script', '', "return len(context.searchFolder())")

    # test without proxy role
1108
    self.assertEqual(1, folder.catalog_test_script())
1109
    self.loginByUserName('bob')
1110
    self.assertEqual(0, folder.catalog_test_script())
1111 1112

    # test with proxy role and correct role
1113
    self.loginByUserName('alice')
1114
    script.manage_proxy(['Manager'])
1115
    self.assertEqual(1, folder.catalog_test_script())
1116
    self.loginByUserName('bob')
1117
    self.assertEqual(1, folder.catalog_test_script())
1118 1119

    # test with proxy role and wrong role
1120
    self.loginByUserName('alice')
1121 1122 1123
    script.manage_proxy(['Assignor'])
    # proxy roles must overwrite the user's roles, even if he is the owner
    # of the script
1124
    self.assertEqual(0, folder.catalog_test_script())
1125
    self.loginByUserName('bob')
1126
    self.assertEqual(0, folder.catalog_test_script())
1127

Jérome Perrin's avatar
Jérome Perrin committed
1128
  def test_42_SearchableText(self):
1129 1130 1131 1132 1133
    """Tests SearchableText is working in ERP5Catalog
    """
    folder = self.getOrganisationModule()
    ob = folder.newContent()
    ob.setTitle('The title of this object')
1134
    self.assertTrue('this' in ob.SearchableText(), ob.SearchableText())
1135 1136
    # add some other objects, we need to create a minimum quantity of data for
    # full text queries to work correctly
1137 1138 1139
    for i in range(10):
      otherob = folder.newContent()
      otherob.setTitle('Something different')
1140
      self.assertFalse('this' in otherob.SearchableText(), otherob.SearchableText())
1141 1142
    # catalog those objects
    self.tic()
1143
    catalog_tool = self.getCatalogTool()
1144
    self.assertEqual([ob],
1145 1146
        [x.getObject() for x in catalog_tool(portal_type='Organisation',
                                             SearchableText='title')])
1147
    self.assertEqual(1,
1148 1149 1150
                      catalog_tool.countResults(portal_type='Organisation',
                                                SearchableText='title')[0][0])

1151
    # 'different' is found in more than 50% of records
1152 1153 1154 1155 1156 1157 1158
    # MySQL ignores such a word, but Mroonga does not ignore.
    if 'ENGINE=Mroonga' in self.portal.erp5_sql_connection.manage_test(
        'SHOW CREATE TABLE full_text')[0][1]:
      # Mroonga
      self.assertEqual(10, self.getCatalogTool().countResults(
                portal_type='Organisation', SearchableText='different')[0][0])
    else:
1159
      # MySQL
1160
      self.assertEqual([],
1161 1162
          [x.getObject for x in self.getCatalogTool()(
                  portal_type='Organisation', SearchableText='different')])
1163
      self.assertEqual(0, self.getCatalogTool().countResults(
1164
                portal_type='Organisation', SearchableText='different')[0][0])
Jérome Perrin's avatar
Jérome Perrin committed
1165 1166

  def test_43_ManagePasteObject(self):
1167 1168 1169 1170 1171 1172 1173 1174 1175
    portal_catalog = self.getCatalogTool()
    person_module = self.getPersonModule()
    person = person_module.newContent(id='1',portal_type='Person')
    self.tic()
    copy_data = person_module.manage_copyObjects([person.getId()])
    new_id = person_module.manage_pasteObjects(copy_data)[0]['new_id']
    new_person = person_module[new_id]
    self.tic()
    path_list = [new_person.getRelativeUrl()]
1176
    self.checkRelativeUrlInSQLPathList(path_list)
1177

Jérome Perrin's avatar
Jérome Perrin committed
1178
  def test_44_ParentRelatedKeys(self):
1179 1180 1181 1182 1183
    portal_catalog = self.getCatalogTool()
    person_module = self.getPersonModule()
    person_module.reindexObject()
    person = person_module.newContent(id='1',portal_type='Person')
    self.tic()
1184
    self.assertEqual([person],
1185 1186
        [x.getObject() for x in self.getCatalogTool()(
               parent_title=person_module.getTitle())])
Jérome Perrin's avatar
Jérome Perrin committed
1187 1188

  def test_45_QueryAndComplexQuery(self):
1189 1190 1191 1192 1193 1194 1195 1196
    """
    """
    org_a = self._makeOrganisation(title='abc',description='abc')
    org_b = self._makeOrganisation(title='bcd',description='bcd')
    org_c = self._makeOrganisation(title='efg',description='efg')
    org_e = self._makeOrganisation(title='foo',description='bir')
    org_f = self._makeOrganisation(title='foo',description='bar')

1197 1198 1199 1200 1201 1202
    # uid=[]
    catalog_kw= {'query':Query(uid=[])}
    self.failIfDifferentSet(
        [x.getPath() for x in (org_a, org_b, org_c, org_e, org_f)],
        [x.path for x in self.getCatalogTool()(
                portal_type='Organisation',**catalog_kw)])
1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215
    # title='abc'
    catalog_kw= {'title':Query(title='abc')}
    self.failIfDifferentSet([org_a.getPath()],
        [x.path for x in self.getCatalogTool()(
                portal_type='Organisation',**catalog_kw)])
    # title with b and c
    catalog_kw= {'title':Query(title=['%b%','%c%'],operator='AND')}
    self.failIfDifferentSet([org_a.getPath(), org_b.getPath()],
        [x.path for x in self.getCatalogTool()(
                portal_type='Organisation',**catalog_kw)])
    # title='bcd' OR description='efg'
    catalog_kw = {'query':ComplexQuery(Query(title='bcd'),
                                       Query(description='efg'),
1216
                                       logical_operator='OR')}
1217 1218 1219 1220
    self.failIfDifferentSet([org_b.getPath(), org_c.getPath()],
        [x.path for x in self.getCatalogTool()(
                portal_type='Organisation',**catalog_kw)])
    # Recursive Complex Query
1221
    # (title='abc' and description='abc') OR
1222
    #  title='foo' and description='bar'
1223 1224
    catalog_kw = {'query':ComplexQuery(ComplexQuery(SimpleQuery(title='abc'),
                                                    SimpleQuery(description='abc'),
1225
                                                    logical_operator='AND'),
1226 1227
                                       ComplexQuery(SimpleQuery(title='foo'),
                                                    SimpleQuery(description='bar'),
1228 1229
                                                    logical_operator='AND'),
                                       logical_operator='OR')}
1230 1231 1232
    self.failIfDifferentSet([org_a.getPath(), org_f.getPath()],
        [x.path for x in self.getCatalogTool()(
                portal_type='Organisation',**catalog_kw)])
1233

Jérome Perrin's avatar
Jérome Perrin committed
1234
  def test_46_TestLimit(self):
1235
    ctool = self.getCatalogTool()
1236 1237
    old_default_result_limit = ctool.default_result_limit
    max_ = ctool.default_result_limit = 3
1238 1239 1240 1241 1242
    #Create max + 2 Organisations
    for i in xrange(max_ + 2):
      self._makeOrganisation(title='abc%s' % (i), description='abc')
    self.assertEqual(max_,
                     len(self.getCatalogTool()(portal_type='Organisation')))
1243
    self.assertEqual(max_ + 2,
1244 1245
            len(self.getCatalogTool()(portal_type='Organisation', limit=None)))
    ctool.default_result_limit = old_default_result_limit
1246

1247
  def playActivityList(self, method_id_list):
1248
    self.commit()
1249 1250 1251 1252
    portal_activities = self.getActivityTool()
    for i in range(0,100):
      message_list = portal_activities.getMessageList()
      for message in message_list:
1253
        #if message.method_id=='_setHotReindexingState':
1254 1255 1256 1257 1258 1259
        #  import pdb;pdb.set_trace()
        if message.method_id in method_id_list:
          try:
            portal_activities.manageInvoke(message.object_path,message.method_id)
          except ActivityFlushError,m:
            pass
1260
      self.commit()
1261

Jérome Perrin's avatar
Jérome Perrin committed
1262
  def test_48_ERP5Site_hotReindexAll(self):
1263
    """
Jérome Perrin's avatar
Jérome Perrin committed
1264
      test the hot reindexing of catalog -> catalog2
1265 1266 1267
      then a hot reindexing detailed catalog2 -> catalog
      this test use the variable environment: extra_sql_connection_string_list
    """
Jérome Perrin's avatar
Jérome Perrin committed
1268
    portal = self.portal
1269
    self.original_connection_id = 'erp5_sql_connection'
1270 1271
    self.original_deferred_connection_id = self.new_erp5_deferred_sql_connection
    self.new_connection_id = self.new_erp5_sql_connection
1272
    self.new_deferred_connection_id = 'erp5_sql_deferred_connection2'
1273
    new_connection_string = getExtraSqlConnectionStringList()[0]
1274 1275 1276 1277 1278 1279 1280 1281 1282

    # Skip this test if default connection string is not "test test".
    original_connection = getattr(portal, self.original_connection_id)
    connection_string = original_connection.connection_string
    if (connection_string == new_connection_string):
      message = 'SKIPPED: default connection string is the same as the one for hot-reindex catalog'
      ZopeTestCase._print(message)
      LOG('Testing... ',0,message)

1283
    portal_category = self.getCategoryTool()
1284
    portal_activities = self.getActivityTool()
1285 1286 1287 1288 1289 1290 1291
    self.base_category = portal_category.newContent(portal_type='Base Category',
                                               title="GreatTitle1")
    module = portal.getDefaultModule('Organisation')
    self.organisation = module.newContent(portal_type='Organisation',
                                     title="GreatTitle2")
    # Flush message queue
    self.tic()
1292 1293
    addSQLConnection = portal.manage_addProduct['ZMySQLDA'] \
      .manage_addZMySQLConnection
1294
    # Create new connectors
1295
    addSQLConnection(self.new_connection_id,'', new_connection_string)
1296 1297
    new_connection = portal[self.new_connection_id]
    new_connection.manage_open_connection()
1298
    addSQLConnection(self.new_deferred_connection_id,'', new_connection_string)
1299 1300 1301 1302 1303 1304
    new_connection = portal[self.new_deferred_connection_id]
    new_connection.manage_open_connection()
    # the transactionless connector must not be change because this one
    # create the portal_ids otherwise it create of conflicts with uid
    # objects

1305 1306 1307 1308 1309 1310
    # Create new catalog
    portal_catalog = self.getCatalogTool()
    self.original_catalog_id = 'erp5_mysql_innodb'
    self.new_catalog_id = self.original_catalog_id + '2'
    cp_data = portal_catalog.manage_copyObjects(ids=('erp5_mysql_innodb',))
    new_id = portal_catalog.manage_pasteObjects(cp_data)[0]['new_id']
1311
    portal_catalog.manage_renameObject(id=new_id, new_id=self.new_catalog_id)
1312 1313 1314

    # Parse all methods in the new catalog in order to change the connector
    new_catalog = portal_catalog[self.new_catalog_id]
1315 1316 1317 1318
    source_sql_connection_id_list=list((self.original_connection_id,
                                  self.original_deferred_connection_id))
    destination_sql_connection_id_list=list((self.new_connection_id,
                                       self.new_deferred_connection_id))
1319
    #launch the full hot reindexing
1320 1321 1322 1323 1324 1325
    portal_catalog.manage_hotReindexAll(source_sql_catalog_id=self.original_catalog_id,
                 destination_sql_catalog_id=self.new_catalog_id,
                 source_sql_connection_id_list=source_sql_connection_id_list,
                 destination_sql_connection_id_list=destination_sql_connection_id_list,
                 update_destination_sql_catalog=True)

1326 1327
    # Flush message queue
    self.tic()
1328 1329
    original_path_list = self.getSQLPathList(self.original_connection_id)
    new_path_list = self.getSQLPathList(self.new_connection_id)
1330
    self.assertTrue(set(original_path_list).issubset(new_path_list))
1331 1332
    self.organisation2 = module.newContent(portal_type='Organisation',
                                     title="GreatTitle2")
1333
    first_deleted_url = self.organisation2.getRelativeUrl()
1334 1335
    self.tic()
    path_list = [self.organisation.getRelativeUrl()]
1336 1337
    self.checkRelativeUrlInSQLPathList(path_list, connection_id=self.original_connection_id)
    self.checkRelativeUrlInSQLPathList(path_list, connection_id=self.new_connection_id)
1338
    path_list = [first_deleted_url]
1339 1340
    self.checkRelativeUrlNotInSQLPathList(path_list, connection_id=self.original_connection_id)
    self.checkRelativeUrlInSQLPathList(path_list, connection_id=self.new_connection_id)
1341 1342

    # Make sure some zsql method use the right connection_id
1343
    zsql_method = portal.portal_skins.erp5_core.Resource_zGetInventoryList
1344
    self.assertEqual(getattr(zsql_method,'connection_id'),self.new_connection_id)
1345

1346
    self.assertEqual(portal_catalog.getHotReindexingState(),
1347 1348
                      HOT_REINDEXING_FINISHED_STATE)

1349 1350
    # Do a hot reindex in the reverse way, but this time a more
    # complicated hot reindex
1351 1352 1353 1354 1355 1356
    portal_catalog.manage_hotReindexAll(
      source_sql_catalog_id=self.new_catalog_id,
      destination_sql_catalog_id=self.original_catalog_id,
      source_sql_connection_id_list=destination_sql_connection_id_list,
      destination_sql_connection_id_list=source_sql_connection_id_list,
      update_destination_sql_catalog=True)
1357
    self.commit()
1358
    self.assertEqual(portal_catalog.getHotReindexingState(),
1359
                      HOT_REINDEXING_RECORDING_STATE)
1360 1361
    self.organisation3 = module.newContent(portal_type='Organisation',
                                     title="GreatTitle2")
1362 1363 1364 1365 1366
    # Try something more complicated, create new object, reindex it
    # and then delete it
    self.deleted_organisation = module.newContent(portal_type='Organisation',
                                     title="GreatTitle2")
    self.deleted_organisation.immediateReindexObject()
1367
    self.commit()
1368 1369
    deleted_url = self.deleted_organisation.getRelativeUrl()
    module.manage_delObjects(ids=[self.deleted_organisation.getId()])
1370
    self.commit()
1371 1372 1373 1374 1375 1376
    # We will invoke acitivities one by one in order to make sure we can test
    # the double indexing state of hot reindexing
    self.playActivityList(('Folder_reindexAll',
                         'InventoryModule_reindexMovementList',
                         'immediateReindexObject',
                         'Folder_reindexObjectList',
1377 1378 1379 1380 1381
                         'unindexObject',
                         'recursiveImmediateReindexObject'))
    # try to delete objects in double indexing state
    module.manage_delObjects(ids=[self.organisation2.getId()])
    self.playActivityList(('immediateReindexObject',
1382 1383 1384 1385
                         'unindexObject',
                         'recursiveImmediateReindexObject',
                         'playBackRecordedObjectList',
                         'getId',
1386
                         '_setHotReindexingState'))
1387
    self.assertEqual(portal_catalog.getHotReindexingState(),
1388 1389 1390 1391 1392
                      HOT_REINDEXING_DOUBLE_INDEXING_STATE)
    # Now we have started an double indexing
    self.next_deleted_organisation = module.newContent(portal_type='Organisation',
                                     title="GreatTitle2",id='toto')
    next_deleted_url = self.next_deleted_organisation.getRelativeUrl()
1393
    self.commit()
1394 1395 1396 1397 1398 1399
    self.playActivityList(( 'immediateReindexObject',
                         'recursiveImmediateReindexObject',))
    path_list=[next_deleted_url]
    self.checkRelativeUrlInSQLPathList(path_list,connection_id=self.new_connection_id)
    self.checkRelativeUrlInSQLPathList(path_list,connection_id=self.original_connection_id)
    module.manage_delObjects(ids=[self.next_deleted_organisation.getId()])
1400 1401 1402 1403
    #Create object during the double indexing to check the security object
    #after the hot reindexing
    self.organisation4 = module.newContent(portal_type='Organisation',
                                     title="GreatTitle2")
1404
    self.tic()
1405
    self.assertEqual(portal_catalog.getHotReindexingState(),
1406
                      HOT_REINDEXING_FINISHED_STATE)
1407 1408 1409 1410 1411
    # Check Security UID object exist in roles and users
    # compare the number object in the catalog
    count_catalog = len(self.getSQLPathList(self.original_connection_id))
    count_restricted_catalog = len(self.getSQLPathListWithRolesAndUsers(\
                               self.original_connection_id))
1412
    self.assertEqual(count_catalog, count_restricted_catalog)
1413

1414 1415 1416
    path_list = [self.organisation3.getRelativeUrl()]
    self.checkRelativeUrlInSQLPathList(path_list,connection_id=self.new_connection_id)
    self.checkRelativeUrlInSQLPathList(path_list,connection_id=self.original_connection_id)
1417
    path_list = [first_deleted_url,deleted_url,next_deleted_url]
1418 1419
    self.checkRelativeUrlNotInSQLPathList(path_list,connection_id=self.new_connection_id)
    self.checkRelativeUrlNotInSQLPathList(path_list,connection_id=self.original_connection_id)
1420 1421 1422 1423
    # Make sure module are there
    path_list = [module.getRelativeUrl()]
    self.checkRelativeUrlInSQLPathList(path_list, connection_id=self.new_connection_id)
    self.checkRelativeUrlInSQLPathList(path_list, connection_id=self.original_connection_id)
1424 1425 1426

  def test_48bis_ERP5Site_hotReindexAllCheckCachedValues(self):
    """
1427
      test the hot reindexing of catalog -> catalog2
1428 1429 1430
      Check that cached values are invalidated due to
      catalog migration
    """
Jérome Perrin's avatar
Jérome Perrin committed
1431
    portal = self.portal
1432 1433
    original_connection_id = 'erp5_sql_connection'
    original_deferred_connection_id = 'erp5_sql_deferred_connection'
1434 1435 1436
    new_connection_string = getExtraSqlConnectionStringList()[0]

    # Skip this test if default connection string is not "test test".
1437
    original_connection = getattr(portal, original_connection_id)
1438 1439 1440 1441 1442 1443 1444
    connection_string = original_connection.connection_string
    if (connection_string == new_connection_string):
      message = 'SKIPPED: default connection string is the same as the one for hot-reindex catalog'
      ZopeTestCase._print(message)
      LOG('Testing... ',0, message)

    # Create new connectors
1445 1446 1447
    addSQLConnection = portal.manage_addProduct['ZMySQLDA'] \
      .manage_addZMySQLConnection
    addSQLConnection(self.new_erp5_sql_connection,'', new_connection_string)
1448 1449
    new_connection = portal[self.new_erp5_sql_connection]
    new_connection.manage_open_connection()
1450
    addSQLConnection(self.new_erp5_deferred_sql_connection,'',
1451 1452 1453 1454 1455 1456 1457 1458 1459
                                      new_connection_string)
    new_connection = portal[self.new_erp5_deferred_sql_connection]
    new_connection.manage_open_connection()
    # the transactionless connector must not be change because this one
    # create the portal_ids otherwise it create of conflicts with uid
    # objects

    # Create new catalog
    portal_catalog = self.getCatalogTool()
1460 1461 1462 1463 1464
    original_catalog = portal_catalog.getSQLCatalog()
    original_catalog_id = original_catalog.getId()
    cp_data = portal_catalog.manage_copyObjects(ids=(original_catalog_id,))
    new_catalog_id = portal_catalog.manage_pasteObjects(cp_data)[0]['new_id']
    new_catalog = portal_catalog[new_catalog_id]
1465 1466 1467 1468 1469 1470 1471

    # Add new searchable table in new catalog
    create_dummy_table_sql = """
    CREATE TABLE `dummy` (
    `uid` BIGINT UNSIGNED NOT NULL,
    `dummy_title` varchar(32) NOT NULL default '',
    PRIMARY KEY  (`uid`)
1472
    ) ENGINE=InnoDB;
1473 1474 1475 1476
    """
    drop_summy_table_sql = """
    DROP TABLE IF EXISTS `dummy`
    """
1477 1478 1479 1480 1481 1482 1483
    for catalog, connection_id in ((original_catalog, original_connection_id),
        (new_catalog, self.new_erp5_sql_connection)):
      catalog.manage_addProduct['ZSQLMethods'].manage_addZSQLMethod(
                    id='z_create_dummy_table', title='', arguments="",
                    connection_id=connection_id,
                    template=create_dummy_table_sql)
      catalog.manage_addProduct['ZSQLMethods'].manage_addZSQLMethod(
1484
                    id='z0_drop_dummy_table', title='', arguments="",
1485
                    connection_id=connection_id,
1486 1487 1488
                    template=drop_summy_table_sql)

    # update catalog configuration and declare new ZSQLMethods
1489
    sql_clear_catalog_list = list(original_catalog.sql_clear_catalog)
1490 1491 1492
    sql_clear_catalog_list.extend(['z0_drop_dummy_table',
                                   'z_create_dummy_table'])
    sql_clear_catalog_list.sort()
1493 1494
    original_catalog.sql_clear_catalog = new_catalog.sql_clear_catalog = \
      tuple(sql_clear_catalog_list)
1495

1496
    sql_search_table_list = list(original_catalog.sql_search_tables)
1497 1498
    sql_search_table_list.append('dummy')
    sql_search_table_list.sort()
1499 1500 1501
    original_catalog.sql_search_tables = new_catalog.sql_search_tables = \
      tuple(sql_search_table_list)

1502
    portal_catalog.manage_catalogClear()
1503
    self.commit()
1504 1505 1506 1507 1508 1509 1510
    # Catalog structure changed, so we should be able to build new queries
    # with new table columns
    # Check that column map is updated according new structure of catalog.
    self.assertTrue('dummy.dummy_title' in portal_catalog.getSQLCatalog().getColumnMap())
    # Check more cached methods of SQLCatalog by building SQLQuery
    query = portal_catalog.getSQLCatalog().buildQuery(kw={'dummy.dummy_title': 'Foo'})
    self.assertTrue(query.query_list)
1511 1512

    # prepare arguments for hot reindex
1513 1514
    source_sql_connection_id_list=list((original_connection_id,
                                  original_deferred_connection_id))
1515 1516
    destination_sql_connection_id_list=list((self.new_erp5_sql_connection,
                                       self.new_erp5_deferred_sql_connection))
1517
    # launch the full hot reindexing
1518 1519
    portal_catalog.manage_hotReindexAll(source_sql_catalog_id=original_catalog_id,
                 destination_sql_catalog_id=new_catalog_id,
1520 1521 1522 1523 1524 1525
                 source_sql_connection_id_list=source_sql_connection_id_list,
                 destination_sql_connection_id_list=destination_sql_connection_id_list,
                 update_destination_sql_catalog=True)

    # Flush message queue
    self.tic()
1526
    self.assertEqual(portal_catalog.getSQLCatalog().getId(), new_catalog_id)
1527 1528 1529 1530 1531
    # Check that column map is updated according new structure of catalog.
    self.assertTrue('dummy.dummy_title' in portal_catalog.getSQLCatalog().getColumnMap())
    # Check more cached methods of SQLCatalog by building SQLQuery
    query = portal_catalog.getSQLCatalog().buildQuery(kw={'dummy.dummy_title': 'Foo'})
    self.assertTrue(query.query_list)
1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542
    # We need to reset SQL connections in skin folder's zsql methods
    sql_connection_id_dict = {}
    for destination_sql_connection_id, source_sql_connection_id in \
          zip(destination_sql_connection_id_list,
              source_sql_connection_id_list):
        if source_sql_connection_id != destination_sql_connection_id:
          sql_connection_id_dict[destination_sql_connection_id] = \
              source_sql_connection_id
    portal_catalog.changeSQLConnectionIds(
      folder=portal.portal_skins,
      sql_connection_id_dict = sql_connection_id_dict)
1543

Jérome Perrin's avatar
Jérome Perrin committed
1544
  def test_47_Unrestricted(self):
1545 1546 1547 1548 1549
    """test unrestricted search/count results.
    """
    uf = self.getPortal().acl_users
    uf._doAddUser('alice', '', ['Member', 'Manager', 'Assignor'], [])
    uf._doAddUser('bob', '', ['Member'], [])
1550

1551
    # create a document that only alice can view
1552
    self.loginByUserName('alice')
1553 1554 1555 1556
    folder = self.getOrganisationModule()
    ob = folder.newContent(title='Object Title')
    ob.manage_permission('View', ['Manager'], 0)
    self.tic()
Jérome Perrin's avatar
Jérome Perrin committed
1557

1558
    # bob cannot see the document
1559
    self.loginByUserName('bob')
1560
    ctool = self.getCatalogTool()
1561 1562
    self.assertEqual(0, len(ctool.searchResults(title='Object Title')))
    self.assertEqual(0, ctool.countResults(title='Object Title')[0][0])
Jérome Perrin's avatar
Jérome Perrin committed
1563

1564
    # unless using unrestricted searches
1565
    self.assertEqual(1,
1566
                len(ctool.unrestrictedSearchResults(title='Object Title')))
1567
    self.assertEqual(1,
1568
                ctool.unrestrictedCountResults(title='Object Title')[0][0])
Aurel's avatar
Aurel committed
1569

Jérome Perrin's avatar
Jérome Perrin committed
1570 1571
  @todo_erp5
  def test_49_IndexInOrderedSearchFolder(self):
Aurel's avatar
Aurel committed
1572 1573 1574 1575 1576 1577 1578 1579
    person_module = self.getPersonModule()

    # Clear catalog
    portal_catalog = self.getCatalogTool()
    portal_catalog.manage_catalogClear()
    catalog = portal_catalog.objectValues()[0]

    person = person_module.newContent(id='a',portal_type='Person',title='a',description='z')
1580
    self.tic()
Aurel's avatar
Aurel committed
1581
    person = person_module.newContent(id='b',portal_type='Person',title='a',description='y')
1582
    self.tic()
Aurel's avatar
Aurel committed
1583
    person = person_module.newContent(id='c',portal_type='Person',title='a',description='x')
1584
    self.tic()
Aurel's avatar
Aurel committed
1585 1586 1587 1588 1589
    index_columns = getattr(catalog, 'sql_catalog_index_on_order_keys', None)
    self.assertNotEqual(index_columns, None)
    self.assertEqual(len(index_columns), 0)
    # Check catalog don't tell to use index if nothing defined
    sql = person_module.searchFolder(src__=1)
1590
    self.assertTrue('use index' not in sql)
Aurel's avatar
Aurel committed
1591
    sql = person_module.searchFolder(src__=1, sort_on=[('id','ascending')])
1592
    self.assertTrue('use index' not in sql)
Aurel's avatar
Aurel committed
1593
    sql = person_module.searchFolder(src__=1, sort_on=[('title','ascending')])
1594
    self.assertTrue('use index' not in sql)
Aurel's avatar
Aurel committed
1595 1596 1597 1598 1599 1600 1601 1602
    # Defined that catalog must tell to use index when order by catalog.title
    index_columns = ('catalog.title',)
    setattr(catalog, 'sql_catalog_index_on_order_keys', index_columns)
    index_columns = getattr(catalog, 'sql_catalog_index_on_order_keys', None)
    self.assertNotEqual(index_columns, None)
    self.assertEqual(len(index_columns), 1)
    # Check catalog tell to use index only when ordering by catalog.title
    sql = person_module.searchFolder(src__=1)
1603
    self.assertTrue('use index' not in sql)
Aurel's avatar
Aurel committed
1604
    sql = person_module.searchFolder(src__=1, sort_on=[('id','ascending')])
1605
    self.assertTrue('use index' not in sql)
Aurel's avatar
Aurel committed
1606
    sql = person_module.searchFolder(src__=1, sort_on=[('title','ascending')])
1607
    self.assertTrue('use index' in sql)
Aurel's avatar
Aurel committed
1608

Jérome Perrin's avatar
Jérome Perrin committed
1609
  def test_50_LocalRolesArgument(self):
1610 1611 1612 1613 1614 1615 1616 1617 1618 1619
    """test local_roles= argument
    """
    uf = self.getPortal().acl_users
    uf._doAddUser('bob', '', ['Member'], [])

    # create two documents, one with Assignee local roles, one without
    folder = self.getOrganisationModule()
    ob1 = folder.newContent(title='Object Title')
    ob1.manage_permission('View', ['Member'], 1)
    ob2 = folder.newContent(title='Object Title')
1620
    ob2_id = ob2.getId()
1621 1622
    ob2.manage_addLocalRoles('bob', ['Assignee'])
    self.tic()
Jérome Perrin's avatar
Jérome Perrin committed
1623

1624
    # by default bob can see those 2 documents
1625
    self.loginByUserName('bob')
1626
    ctool = self.getCatalogTool()
1627 1628
    self.assertEqual(2, len(ctool.searchResults(title='Object Title')))
    self.assertEqual(2, ctool.countResults(title='Object Title')[0][0])
1629

1630 1631
    # if we specify local_roles= it will only returns documents on with bob has
    # a local roles
1632
    self.assertEqual(0,
1633 1634
                len(ctool.searchResults(title='Object Title',
                                        local_roles='UnexistingRole')))
1635
    self.assertEqual(0,
1636 1637
                len(ctool.searchResults(title='Object Title',
                                        local_roles='Assignor')))
1638
    self.assertEqual(1,
1639 1640
                len(ctool.searchResults(title='Object Title',
                                        local_roles='Assignee')))
1641
    self.assertEqual(1,
1642 1643 1644
                ctool.countResults(title='Object Title',
                                   local_roles='Assignee')[0][0])

1645
    # this also work for searchFolder and countFolder
1646
    self.assertEqual(1, len(folder.searchFolder(title='Object Title',
1647
                                             local_roles='Assignee')))
1648
    self.assertEqual(1, folder.countFolder(title='Object Title',
1649
                                             local_roles='Assignee')[0][0])
1650

1651 1652
    # and local_roles can be a list, then this a OR (ie. you must have at least
    # one role).
1653
    self.assertEqual(1,
1654 1655
                len(ctool.searchResults(title='Object Title',
                                       local_roles=['Assignee', 'Auditor'])))
1656
    self.assertEqual(1,
1657 1658 1659 1660
                ctool.countResults(title='Object Title',
                                   local_roles=['Assignee', 'Auditor'])[0][0])

    # this list can also be given in ; form, for worklists URL
1661
    self.assertEqual(1,
1662 1663
                len(ctool.searchResults(title='Object Title',
                                       local_roles='Assignee;Auditor')))
1664
    self.assertEqual(1,
1665 1666 1667
                ctool.countResults(title='Object Title',
                                   local_roles='Assignee;Auditor')[0][0])

1668 1669
    #Test if bob can't see object even if Assignee role (without View permission) is defined on object
    ob1.manage_addLocalRoles('bob', ['Assignee'])
1670 1671
    ob1.manage_permission('View', ['Assignor'], 0)
    ob1.reindexObject()
1672
    self.tic()
1673 1674 1675 1676
    user = getSecurityManager().getUser()
    self.assertFalse(user.has_permission('View', ob1))
    self.assertTrue(user.has_role('Assignee', ob1))
    result_list = [r.getId() for r in ctool(title='Object Title', local_roles='Assignee')]
1677 1678 1679
    self.assertEqual(1, len(result_list))
    self.assertEqual([ob2_id], result_list)
    self.assertEqual(1,
1680 1681 1682 1683
                ctool.countResults(title='Object Title',
                                   local_roles='Assignee')[0][0])

    # this also work for searchFolder and countFolder
1684
    self.assertEqual(1, len(folder.searchFolder(title='Object Title',
1685
                                             local_roles='Assignee')))
1686
    self.assertEqual(1, folder.countFolder(title='Object Title',
1687 1688
                                             local_roles='Assignee')[0][0])

Jérome Perrin's avatar
Jérome Perrin committed
1689
  def test_51_SearchWithKeyWords(self):
1690 1691 1692 1693 1694 1695 1696 1697
    person_module = self.getPersonModule()
    and_ = person_module.newContent(portal_type='Person', title='AND')
    or_ = person_module.newContent(portal_type='Person', title='OR')
    like_ = person_module.newContent(portal_type='Person', title='LIKE')
    select_ = person_module.newContent(portal_type='Person', title='SELECT')

    self.tic()
    ctool = self.getCatalogTool()
1698
    self.assertEqual([and_], [x.getObject() for x in
1699 1700
                                   ctool(portal_type='Person', title='AND')])

1701
    self.assertEqual([or_], [x.getObject() for x in
1702 1703
                                   ctool(portal_type='Person', title='OR')])

1704
    self.assertEqual([like_], [x.getObject() for x in
1705 1706
                                   ctool(portal_type='Person', title='LIKE')])

1707
    self.assertEqual([select_], [x.getObject() for x in
1708 1709
                                   ctool(portal_type='Person', title='SELECT')])

Jérome Perrin's avatar
Jérome Perrin committed
1710
  def test_52_QueryAndTableAlias(self):
1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721
    """
    Make sure we can use aliases for tables wich will
    be used by related keys. This allow in some particular
    cases to decrease a lot the number of aliases
    """
    org_a = self._makeOrganisation(title='abc',default_address_city='abc')
    module = self.getOrganisationModule()
    module.immediateReindexObject()
    # First try without aliases
    query1 = Query(parent_portal_type="Organisation")
    query2 = Query(grand_parent_portal_type="Organisation Module")
1722
    complex_query = ComplexQuery(query1, query2, logical_operator="AND")
1723 1724
    self.failIfDifferentSet([org_a.getPath() + '/default_address'],
        [x.path for x in self.getCatalogTool()(query=complex_query)])
1725
    # Then try with aliases
1726
    query1 = Query(parent_portal_type="Organisation",
1727 1728
                   table_alias_list=(("catalog" , "parent"),))
    query2 = Query(grand_parent_portal_type="Organisation Module",
1729
                   table_alias_list=(("catalog" , "parent"),
1730
                                    ("catalog", "grand_parent")))
1731
    complex_query = ComplexQuery(query1, query2, logical_operator="AND")
1732 1733 1734 1735 1736 1737 1738 1739 1740
    self.failIfDifferentSet([org_a.getPath() + '/default_address'],
        [x.path for x in self.getCatalogTool()(query=complex_query)])
    sql_kw = self.getCatalogTool().buildSQLQuery(query=complex_query)
    # Make sure we have the right list of aliases
    table_alias_list = sql_kw["from_table_list"]
    self.failIfDifferentSet((("catalog","catalog"),
                             ("parent","catalog"),
                             ("grand_parent","catalog")),
                             table_alias_list)
Jérome Perrin's avatar
Jérome Perrin committed
1741 1742

  def test_53_DateFormat(self):
1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786
    org_a = self._makeOrganisation(title='org_a')
    org_b = self._makeOrganisation(title='org_b')
    sql_connection = self.getSQLConnection()
    # Add a method in order to directly put values we want into
    # the catalog.
    def updateDate(organisation,date):
      uid = organisation.getUid()
      sql = "UPDATE catalog SET modification_date='%s' '\
          'WHERE uid=%s" %\
          (date,uid)
      result = sql_connection.manage_test(sql)
    updateDate(org_a,'2007-01-12 01:02:03')
    updateDate(org_b,'2006-02-24 15:09:06')

    catalog_kw = {'modification_date':{'query':'24/02/2006',
                               'format':'%d/%m/%Y',
                               'type':'date'}}
    self.failIfDifferentSet([org_b.getPath()],
        [x.path for x in self.getCatalogTool()(
                portal_type='Organisation',**catalog_kw)])
    catalog_kw = {'modification_date':{'query':'2007-01-12',
                               'format':'%Y-%m-%d',
                               'type':'date'}}
    self.failIfDifferentSet([org_a.getPath()],
        [x.path for x in self.getCatalogTool()(
                portal_type='Organisation',**catalog_kw)])
    catalog_kw = {'modification_date':{'query':'>31/12/2006',
                               'format':'%d/%m/%Y',
                               'type':'date'}}
    self.failIfDifferentSet([org_a.getPath()],
        [x.path for x in self.getCatalogTool()(
                portal_type='Organisation',**catalog_kw)])
    catalog_kw = {'modification_date':{'query':'2006',
                               'format':'%Y',
                               'type':'date'}}
    self.failIfDifferentSet([org_b.getPath()],
        [x.path for x in self.getCatalogTool()(
                portal_type='Organisation',**catalog_kw)])
    catalog_kw = {'modification_date':{'query':'>2006',
                               'format':'%Y',
                               'type':'date'}}
    self.failIfDifferentSet([org_a.getPath()],
        [x.path for x in self.getCatalogTool()(
                portal_type='Organisation',**catalog_kw)])
1787 1788 1789 1790 1791 1792 1793
    # If the date is an empty string, check that all objects are displayed.
    catalog_kw = {'modification_date':{'query':'',
                               'format':'%d/%m/%Y',
                               'type':'date'}}
    self.failIfDifferentSet([org_a.getPath(), org_b.getPath()],
        [x.path for x in self.getCatalogTool()(
                portal_type='Organisation',**catalog_kw)])
1794

Jérome Perrin's avatar
Jérome Perrin committed
1795
  def test_54_FixIntUid(self):
1796 1797 1798 1799 1800 1801 1802
    portal_catalog = self.getCatalogTool()
    portal = self.getPortal()

    module = portal.getDefaultModule('Organisation')
    organisation = module.newContent(portal_type='Organisation',)
    # Ensure that the new uid is long.
    uid = organisation.uid
1803
    self.assertTrue(isinstance(uid, long))
1804 1805 1806
    self.tic()

    # Ensure that the uid did not change after the indexing.
1807
    self.assertEqual(organisation.uid, uid)
1808 1809 1810 1811 1812 1813 1814

    # Force to convert the uid to int.
    self.uid = int(uid)
    self.tic()

    # After the indexing, the uid must be converted to long automatically,
    # and the value must be equivalent.
1815 1816
    self.assertTrue(isinstance(uid, long))
    self.assertEqual(organisation.uid, uid)
1817

Jérome Perrin's avatar
Jérome Perrin committed
1818
  def test_55_FloatFormat(self):
Jérome Perrin's avatar
Jérome Perrin committed
1819 1820 1821
    catalog_kw = {'uid': {'query': '2 567.54',
                          'format': '1 234.12',
                          'type': 'float'}}
1822 1823 1824
    sql_src = self.getCatalogTool().buildSQLQuery(**catalog_kw)['where_expression']
    self.assertTrue("TRUNCATE(catalog.uid,2) = '2567.54'" in sql_src or \
                    'TRUNCATE(`catalog`.`uid`, 2) = 2567.54' in sql_src, sql_src)
1825

Jérome Perrin's avatar
Jérome Perrin committed
1826
  def test_56_CreateUidDuringClearCatalog(self):
1827 1828 1829 1830 1831 1832 1833
    """
      Create a script in the catalog to generate a uid list
      Check the creation some objects, or activities, during a clear
    """
    # Add a script to create uid list
    catalog = self.getCatalogTool().getSQLCatalog()
    script_id = 'z0_zCreateUid'
1834 1835
    script_content = "context.getPortalObject().portal_ids.generateNewIdList(id_generator='uid',\
                                                                             id_group='text_uid')"
1836 1837
    script = createZODBPythonScript(catalog, script_id,
                          '*args,**kw', script_content)
1838 1839 1840 1841 1842 1843 1844 1845
    sql_clear_catalog = list(catalog.sql_clear_catalog)
    sql_clear_catalog.append(script_id)
    sql_clear_catalog.sort()
    catalog.sql_clear_catalog = tuple(sql_clear_catalog)
    # launch the sql_clear_catalog with the script after the drop tables and
    # before the recreate tables of catalog
    catalog.manage_catalogClear()

Jérome Perrin's avatar
Jérome Perrin committed
1846
  def test_SearchOnOwner(self):
1847 1848 1849 1850 1851 1852 1853 1854
    # owner= can be used a search key in the catalog to have all documents for
    # a specific owner and on which he have the View permission.
    obj = self._makeOrganisation(title='The Document')
    obj2 = self._makeOrganisation(title='The Document')
    obj2.manage_permission('View', [], 0)
    obj2.reindexObject()
    self.tic()
    ctool = self.getCatalogTool()
1855
    self.assertEqual([obj], [x.getObject() for x in
1856 1857
                                   ctool(title='The Document',
                                         owner=self.username)])
1858
    self.assertEqual([], [x.getObject() for x in
1859 1860 1861
                                   ctool(title='The Document',
                                         owner='somebody else')])

Jérome Perrin's avatar
Jérome Perrin committed
1862

Jérome Perrin's avatar
Jérome Perrin committed
1863
  def test_SubDocumentsSecurityIndexing(self):
1864 1865 1866 1867 1868 1869 1870 1871
    # make sure indexing of security on sub-documents works as expected
    uf = self.getPortal().acl_users
    uf._doAddUser('bob', '', ['Member'], [])
    obj = self._makeOrganisation(title='The Document')
    obj2 = obj.newContent(portal_type='Bank Account')
    obj2.manage_addLocalRoles('bob', ['Auditor'])
    self.tic()

1872
    self.loginByUserName('bob')
1873
    self.assertEqual([obj2], [x.getObject() for x in
1874 1875 1876 1877 1878
                               obj.searchFolder(portal_type='Bank Account')])
    # now if we pass the bank account in deleted state, it's no longer returned
    # by searchFolder.
    # This will work as long as Bank Account are associated to a workflow that
    # allow deletion.
1879
    self.login()
1880 1881
    obj2.delete()
    self.tic()
1882
    self.loginByUserName('bob')
1883
    self.assertEqual([], [x.getObject() for x in
1884 1885
                           obj.searchFolder(portal_type='Bank Account')])

1886
  @todo_erp5
Jérome Perrin's avatar
Jérome Perrin committed
1887
  def test_SubDocumentsWithAcquireLocalRoleSecurityIndexing(self):
1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921
    # Check that sub object indexation is compatible with ZODB settings
    # when the sub object acquires the parent local roles
    perm = 'View'

    # Create some users
    logout = self.logout
    user1 = 'local_foo_1'
    user2 = 'local_bar_1'
    uf = self.getPortal().acl_users
    uf._doAddUser(user1, user1, ['Member', ], [])
    uf._doAddUser(user2, user2, ['Member', ], [])

    container_portal_type = 'Organisation'
    # Create a container, define a local role, and set view permission
    folder = self.getOrganisationModule()

    # user1 should be auditor on container
    # user2 should be assignor on subdocument
    container = folder.newContent(portal_type=container_portal_type)
    container.manage_setLocalRoles(user1, ['Auditor'])
#     container.manage_setLocalRoles(user2, [])
    container.manage_permission(perm, ['Owner', 'Auditor', 'Assignor'], 0)

    # By default, local roles are acquired from container for Email portal type
    object_portal_type = 'Email'
    obj = container.newContent(portal_type=object_portal_type)
    # Acquire permission from parent
    obj.manage_permission(perm, [], 1)
    obj.manage_setLocalRoles(user2, ['Assignor'])

    obj.reindexObject()
    self.tic()

    logout()
1922
    self.loginByUserName(user1)
1923 1924
    result = obj.portal_catalog(portal_type=object_portal_type)
    self.assertSameSet([obj, ], [x.getObject() for x in result])
1925
    result = obj.portal_catalog(portal_type=object_portal_type,
1926 1927
                                local_roles='Owner')
    self.assertSameSet([], [x.getObject() for x in result])
1928
    result = obj.portal_catalog(portal_type=object_portal_type,
1929 1930
                                local_roles='Assignor')
    self.assertSameSet([], [x.getObject() for x in result])
1931
    result = obj.portal_catalog(portal_type=object_portal_type,
1932 1933 1934 1935
                                local_roles='Auditor')
    self.assertSameSet([obj], [x.getObject() for x in result])

    logout()
1936
    self.loginByUserName(user2)
1937 1938
    result = obj.portal_catalog(portal_type=object_portal_type)
    self.assertSameSet([obj, ], [x.getObject() for x in result])
1939
    result = obj.portal_catalog(portal_type=object_portal_type,
1940 1941
                                local_roles='Owner')
    self.assertSameSet([], [x.getObject() for x in result])
1942
    result = obj.portal_catalog(portal_type=object_portal_type,
1943 1944
                                local_roles='Assignor')
    self.assertSameSet([obj], [x.getObject() for x in result])
1945
    result = obj.portal_catalog(portal_type=object_portal_type,
1946 1947 1948
                                local_roles='Auditor')
    self.assertSameSet([], [x.getObject() for x in result])

Jérome Perrin's avatar
Jérome Perrin committed
1949
  def test_60_ViewableOwnerIndexing(self):
1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961
    logout = self.logout
    uf = self.getPortal().acl_users
    uf._doAddUser('super_owner', '', ['Member', 'Author', 'Assignee'], [])
    uf._doAddUser('little_owner', '', ['Member', 'Author'], [])

    perm = 'View'
    folder = self.getOrganisationModule()
    portal_type = 'Organisation'
    sub_portal_type_id = 'Address'
    sub_portal_type = self.getPortal().portal_types._getOb(sub_portal_type_id)

    sql_connection = self.getSQLConnection()
1962
    sql = 'select viewable_owner as owner from catalog where uid=%s'
1963

1964
    self.loginByUserName('super_owner')
1965 1966 1967 1968 1969 1970

    # Check that Owner is not catalogued if he can't view the object
    obj = folder.newContent(portal_type='Organisation')
    obj.manage_permission(perm, [], 0)
    self.tic()
    result = sql_connection.manage_test(sql % obj.getUid())
1971
    self.assertSameSet([''], [x.owner for x in result])
1972 1973 1974 1975 1976 1977 1978 1979

    # Check that Owner is catalogued when he can view the object
    obj = folder.newContent(portal_type='Organisation')
    obj.manage_permission(perm, ['Owner'], 0)
    self.tic()
    result = sql_connection.manage_test(sql % obj.getUid())
    self.assertSameSet(['super_owner'], [x.owner for x in result])

1980
    # Check that Owner is not catalogued when he can view the
1981 1982 1983 1984 1985
    # object because he has another role
    obj = folder.newContent(portal_type='Organisation')
    obj.manage_permission(perm, ['Assignee'], 0)
    self.tic()
    result = sql_connection.manage_test(sql % obj.getUid())
1986
    self.assertSameSet([''], [x.owner for x in result])
1987

1988
    # Check that Owner is not catalogued when he can't view the
1989 1990 1991 1992
    # object and when the portal type does not acquire the local roles.
    sub_portal_type.acquire_local_roles = False
    self.portal.portal_caches.clearAllCache()
    logout()
1993
    self.loginByUserName('super_owner')
1994 1995 1996
    obj = folder.newContent(portal_type='Organisation')
    obj.manage_permission(perm, ['Owner'], 0)
    logout()
1997
    self.loginByUserName('little_owner')
1998 1999 2000 2001
    sub_obj = obj.newContent(portal_type='Address')
    sub_obj.manage_permission(perm, [], 0)
    self.tic()
    result = sql_connection.manage_test(sql % sub_obj.getUid())
2002
    self.assertSameSet([''], [x.owner for x in result])
2003

2004
    # Check that Owner is catalogued when he can view the
2005 2006 2007 2008
    # object and when the portal type does not acquire the local roles.
    sub_portal_type.acquire_local_roles = False
    self.portal.portal_caches.clearAllCache()
    logout()
2009
    self.loginByUserName('super_owner')
2010 2011 2012
    obj = folder.newContent(portal_type='Organisation')
    obj.manage_permission(perm, ['Owner'], 0)
    logout()
2013
    self.loginByUserName('little_owner')
2014 2015 2016 2017 2018 2019
    sub_obj = obj.newContent(portal_type='Address')
    sub_obj.manage_permission(perm, ['Owner'], 0)
    self.tic()
    result = sql_connection.manage_test(sql % sub_obj.getUid())
    self.assertSameSet(['little_owner'], [x.owner for x in result])

2020
    # Check that Owner is catalogued when he can view the
2021 2022 2023 2024 2025
    # object because permissions are acquired and when the portal type does not
    # acquire the local roles.
    sub_portal_type.acquire_local_roles = False
    self.portal.portal_caches.clearAllCache()
    logout()
2026
    self.loginByUserName('super_owner')
2027 2028 2029
    obj = folder.newContent(portal_type='Organisation')
    obj.manage_permission(perm, ['Owner'], 0)
    logout()
2030
    self.loginByUserName('little_owner')
2031 2032 2033 2034 2035 2036
    sub_obj = obj.newContent(portal_type='Address')
    sub_obj.manage_permission(perm, [], 1)
    self.tic()
    result = sql_connection.manage_test(sql % sub_obj.getUid())
    self.assertSameSet(['little_owner'], [x.owner for x in result])

2037
    # Check that Owner is not catalogued when he can't view the
2038 2039 2040 2041
    # object and when the portal type acquires the local roles.
    sub_portal_type.acquire_local_roles = True
    self.portal.portal_caches.clearAllCache()
    logout()
2042
    self.loginByUserName('super_owner')
2043 2044 2045
    obj = folder.newContent(portal_type='Organisation')
    obj.manage_permission(perm, ['Owner'], 0)
    logout()
2046
    self.loginByUserName('little_owner')
2047 2048 2049 2050
    sub_obj = obj.newContent(portal_type='Address')
    sub_obj.manage_permission(perm, [], 0)
    self.tic()
    result = sql_connection.manage_test(sql % sub_obj.getUid())
2051
    self.assertSameSet([''], [x.owner for x in result])
2052

2053
    # Check that Owner is catalogued when he can view the
2054 2055 2056 2057
    # object and when the portal type acquires the local roles.
    sub_portal_type.acquire_local_roles = True
    self.portal.portal_caches.clearAllCache()
    logout()
2058
    self.loginByUserName('super_owner')
2059 2060 2061
    obj = folder.newContent(portal_type='Organisation')
    obj.manage_permission(perm, ['Owner'], 0)
    logout()
2062
    self.loginByUserName('little_owner')
2063 2064 2065 2066 2067 2068
    sub_obj = obj.newContent(portal_type='Address')
    sub_obj.manage_permission(perm, ['Owner'], 0)
    self.tic()
    result = sql_connection.manage_test(sql % sub_obj.getUid())
    self.assertSameSet(['little_owner'], [x.owner for x in result])

2069
    # Check that Owner is catalogued when he can view the
2070 2071 2072 2073 2074
    # object because permissions are acquired and when the portal type
    # acquires the local roles.
    sub_portal_type.acquire_local_roles = True
    self.portal.portal_caches.clearAllCache()
    logout()
2075
    self.loginByUserName('super_owner')
2076 2077 2078
    obj = folder.newContent(portal_type='Organisation')
    obj.manage_permission(perm, ['Owner'], 0)
    logout()
2079
    self.loginByUserName('little_owner')
2080 2081 2082 2083 2084 2085
    sub_obj = obj.newContent(portal_type='Address')
    sub_obj.manage_permission(perm, [], 1)
    self.tic()
    result = sql_connection.manage_test(sql % sub_obj.getUid())
    self.assertSameSet(['little_owner'], [x.owner for x in result])

Jérome Perrin's avatar
Jérome Perrin committed
2086
  def test_ExactMatchSearch(self):
2087 2088 2089 2090 2091 2092
    # test exact match search with queries
    doc = self._makeOrganisation(title='Foo%')
    other_doc = self._makeOrganisation(title='FooBar')
    ctool = self.getCatalogTool()

    # by default, % in catalog search is a wildcard:
2093
    self.assertSameSet([doc, other_doc], [x.getObject() for x in
2094
        ctool(portal_type='Organisation', title='Foo%')])
2095
    # ... but you can force searches with an exact match key
2096
    self.assertEqual([doc], [x.getObject() for x in
2097 2098
       ctool(portal_type='Organisation', title=dict(query='Foo%',
                                                    key='ExactMatch'))])
2099

Jérome Perrin's avatar
Jérome Perrin committed
2100
  def test_KeywordSearch(self):
2101 2102 2103 2104 2105 2106 2107
    # test keyword search with queries
    doc = self._makeOrganisation(description='Foo')
    other_doc = self._makeOrganisation(description='Foobar')
    ctool = self.getCatalogTool()

    # description is not a keyword by default. (This might change in the
    # future, in this case, this test have to be updated)
2108
    self.assertSameSet([doc], [x.getObject() for x in
2109
        ctool(portal_type='Organisation', description='=Foo')])
2110
    self.assertEqual({doc, other_doc}, {x.getObject() for x in
2111
      ctool(portal_type='Organisation', description=dict(query='Foo',
2112
                                                         key='Keyword'))})
2113 2114


Jérome Perrin's avatar
Jérome Perrin committed
2115
  def test_ignore_empty_string(self):
2116
    # ERP5Catalog ignore empty strings by default
2117 2118
    doc_with_description = self._makeOrganisation(description='X')
    doc_with_empty_description = self._makeOrganisation(description='')
2119 2120 2121
    ctool = self.getCatalogTool()
    def searchResults(**kw):
      kw['portal_type'] = 'Organisation'
2122
      return {x.getObject() for x in ctool.searchResults(**kw)}
2123

2124
    # description='' is ignored
2125
    self.assertEqual({doc_with_empty_description, doc_with_description},
2126
                      searchResults(description=''))
2127
    # unless we exlicitly say we don't want to ignore empty strings
2128
    self.assertEqual({doc_with_empty_description},
2129
                      searchResults(ignore_empty_string=0, description=''))
2130 2131 2132

  def test_ignore_empty_string_related_key(self):
    # ERP5Catalog ignore empty strings by default, also on related keys
2133 2134 2135 2136 2137
    region_with_empty_description = self.portal.portal_categories.region.newContent(
                                        portal_type='Category', description='')
    doc_with_empty_region_description = self._makeOrganisation(
                            region_value=region_with_empty_description)
    doc_without_region = self._makeOrganisation()
2138 2139 2140
    ctool = self.getCatalogTool()
    def searchResults(**kw):
      kw['portal_type'] = 'Organisation'
2141
      return {x.getObject() for x in ctool.searchResults(**kw)}
2142

2143
    self.assertEqual({doc_with_empty_region_description, doc_without_region},
2144
                      searchResults(region_description=''))
2145
    self.assertEqual({doc_with_empty_region_description},
2146
        searchResults(ignore_empty_string=0, region_description=''))
2147

Jérome Perrin's avatar
Jérome Perrin committed
2148
  def test_complex_query(self):
Yusei Tahara's avatar
Yusei Tahara committed
2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182
    # Make sure that complex query works on real environment.
    catalog = self.getCatalogTool()
    person_module = self.getPersonModule()

    # Add categories
    portal_category = self.getCategoryTool()
    africa = portal_category.region.newContent(id='africa')
    asia = portal_category.region.newContent(id='asia')
    europe = portal_category.region.newContent(id='europe')

    # A from Africa
    person_module.newContent(id='A', first_name='A', last_name='ERP5',
                             region='africa')

    # B from Asia
    person_module.newContent(id='B', first_name='B', last_name='ZOPE',
                             region='asia')

    # C from Europe
    person_module.newContent(id='C', first_name='C', last_name='PYTHON',
                             region='europe')

    # D from ????
    person_module.newContent(id='D', first_name='D', last_name='ERP5')

    self.tic()

    # simple query
    query = Query(portal_type='Person')
    self.assertEqual(len(catalog(query=query)), 4)

    # complex query
    query = ComplexQuery(Query(portal_type='Person'),
                         Query(region_uid=asia.getUid()),
2183
                         logical_operator='AND')
Yusei Tahara's avatar
Yusei Tahara committed
2184 2185 2186 2187 2188
    self.assertEqual(len(catalog(query=query)), 1)

    # complex query
    query = ComplexQuery(Query(portal_type='Person'),
                         Query(region_uid=(africa.getUid(), asia.getUid())),
2189
                         logical_operator='AND')
Yusei Tahara's avatar
Yusei Tahara committed
2190 2191 2192 2193 2194
    self.assertEqual(len(catalog(query=query)), 2)

    # more complex query
    query_find_european = ComplexQuery(Query(portal_type='Person'),
                                       Query(region_uid=europe.getUid()),
2195
                                       logical_operator='AND')
Yusei Tahara's avatar
Yusei Tahara committed
2196
    self.assertEqual(len(catalog(query=query_find_european)), 1)
2197

Yusei Tahara's avatar
Yusei Tahara committed
2198 2199
    query_find_name_erp5 = ComplexQuery(Query(portal_type='Person'),
                                        Query(title='%ERP5'),
2200
                                        logical_operator='AND')
Yusei Tahara's avatar
Yusei Tahara committed
2201 2202
    self.assertEqual(len(catalog(query=query_find_name_erp5)), 2)

2203
    self.assertRaises(NotImplementedError, ComplexQuery, query_find_european, query_find_name_erp5, logical_operator='OR')
2204

Jérome Perrin's avatar
Jérome Perrin committed
2205
  def test_check_security_table_content(self):
2206 2207 2208
    sql_connection = self.getSQLConnection()
    portal = self.getPortalObject()
    portal_types = portal.portal_types
2209 2210 2211 2212

    uf = self.getPortal().acl_users
    uf._doAddUser('foo', 'foo', ['Member', ], [])
    uf._doAddUser('ERP5TypeTestCase', 'ERP5TypeTestCase', ['Member', ], [])
2213
    self.commit()
2214 2215 2216 2217 2218
    portal_catalog = self.getCatalogTool()
    portal_catalog.manage_catalogClear()
    self.getPortal().ERP5Site_reindexAll()
    self.tic()

2219 2220 2221 2222 2223 2224 2225 2226 2227 2228
    # Person stuff
    person_module = portal.person_module
    person = 'Person'
    person_portal_type = portal_types._getOb(person)
    person_portal_type.acquire_local_roles = False
    # Organisation stuff
    organisation_module = portal.organisation_module
    organisation = 'Organisation'
    organisation_portal_type = portal_types._getOb(organisation)
    organisation_portal_type.acquire_local_roles = True
2229

2230
    self.portal.portal_caches.clearAllCache()
2231

2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251
    def newContent(container, portal_type, acquire_view_permission, view_role_list, local_role_dict):
      document = container.newContent(portal_type=portal_type)
      document.manage_permission('View', roles=view_role_list, acquire=acquire_view_permission)
      for user, role_list in local_role_dict.iteritems():
        document.manage_setLocalRoles(userid=user, roles=role_list)
      return document

    # Create documents for all combinations
    object_dict = {}

    def getObjectDictKey():
      """
        Get values from enclosing environment.
        Uggly, but makes calls less verbose.
      """
      return (portal_type, acquire_view_permission,
              tuple(view_role_list),
              tuple([(x, tuple(y))
                     for x, y in local_role_dict.iteritems()])
             )
2252

2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276
    for container, portal_type in ((person_module, person),
                                   (organisation_module, organisation)):
      for acquire_view_permission in (True, False):
        for view_role_list in ([],
                               ['Owner'],
                               ['Owner', 'Author'],
                               ['Author']):
          for local_role_dict in ({},
                                  {'foo': ['Owner']},
                                  {'foo': ['Author']},
                                  {'foo': ['Owner'],
                                   'bar': ['Author']},
                                  {'foo': ['Owner', 'Author'],
                                   'bar': ['Whatever']},
                                  {'foo': ['Owner', 'Author'],
                                   'bar': ['Whatever', 'Author']}):
            object_dict[getObjectDictKey()] = \
              newContent(container, portal_type, acquire_view_permission,
                         view_role_list, local_role_dict)
    self.tic()

    def query(sql):
      result = sql_connection.manage_test(sql)
      return result.dictionaries()
2277

2278 2279 2280 2281 2282
    # Check that there is no Owner role in security table
    # Note: this tests *all* lines from security table. Not just the ones
    # inserted in this test.
    result = query('SELECT * FROM roles_and_users WHERE allowedRolesAndUsers LIKE "%:Owner"')
    self.assertEqual(len(result), 0, repr(result))
2283

2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304
    # Check that for each "user:<user>:<role>" line there is exactly one
    # "user:<user>" line with the same uid.
    # Also, check that for each "user:<user>" there is at least one
    # "user:<user>:<role>" line with same uid.
    # Also, check if "user:..." lines are well-formed.
    # Note: this tests *all* lines from security table. Not just the ones
    # inserted in this test.
    line_list = query('SELECT * FROM roles_and_users WHERE allowedRolesAndUsers LIKE "user:%"')
    for line in line_list:
      role_list = line['allowedRolesAndUsers'].split(':')
      uid = line['uid']
      if len(role_list) == 3:
        stripped_role = ':'.join(role_list[:-1])
        result = query('SELECT * FROM roles_and_users WHERE allowedRolesAndUsers = "%s" AND uid = %i' % (stripped_role, uid) )
        self.assertEqual(len(result), 1, repr(result))
      elif len(role_list) == 2:
        result = query('SELECT * FROM roles_and_users WHERE allowedRolesAndUsers LIKE "%s:%%" AND uid = %i' % (line['allowedRolesAndUsers'], uid) )
        self.assertNotEqual(len(result), 0, 'No line found for allowedRolesAndUsers=%r and uid=%i' % (line['allowedRolesAndUsers'], uid))
      else:
        raise Exception, 'Malformed allowedRolesAndUsers value: %r' % (line['allowedRolesAndUsers'], )

2305 2306
    # Check that object that 'bar' can view because of 'Author' role can *not*
    # be found when searching for his other 'Whatever' role.
2307 2308 2309 2310 2311 2312 2313 2314 2315
    local_role_dict = {'foo': ['Owner', 'Author'],
                       'bar': ['Whatever', 'Author']}
    for container, portal_type in ((person_module, person),
                                   (organisation_module, organisation)):
      for acquire_view_permission in (True, False):
        for view_role_list in (['Owner', 'Author'],
                               ['Author']):
          object = object_dict[getObjectDictKey()]
          result = query('SELECT roles_and_users.uid FROM roles_and_users, catalog WHERE roles_and_users.uid = catalog.security_uid AND catalog.uid = %i AND allowedRolesAndUsers = "user:bar:Whatever"' % (object.uid, ))
2316
          self.assertEqual(len(result), 0, '%r: len(%r) != 0' % (getObjectDictKey(), result))
2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329

    # Check that no 'bar' role are in security table when 'foo' has local
    # roles allowing him to view an object but 'bar' can't.
    local_role_dict = {'foo': ['Owner', 'Author'],
                       'bar': ['Whatever']}
    for container, portal_type in ((person_module, person),
                                   (organisation_module, organisation)):
      for acquire_view_permission in (True, False):
        for view_role_list in (['Owner', 'Author'],
                               ['Author']):
          object = object_dict[getObjectDictKey()]
          result = query('SELECT roles_and_users.uid, roles_and_users.allowedRolesAndUsers FROM roles_and_users, catalog WHERE roles_and_users.uid = catalog.security_uid AND catalog.uid = %i AND roles_and_users.allowedRolesAndUsers LIKE "user:bar%%"' % (object.uid, ))
          self.assertEqual(len(result), 0, '%r: len(%r) != 0' % (getObjectDictKey(), result))
2330

Jérome Perrin's avatar
Jérome Perrin committed
2331
  def test_RealOwnerIndexing(self):
2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346
    logout = self.logout
    user1 = 'local_foo'
    user2 = 'local_bar'
    uf = self.getPortal().acl_users
    uf._doAddUser(user1, user1, ['Member', ], [])
    uf._doAddUser(user2, user2, ['Member', ], [])

    perm = 'View'
    folder = self.getOrganisationModule()
    folder.manage_setLocalRoles(user1, ['Author', 'Auditor'])
    folder.manage_setLocalRoles(user2, ['Author', 'Auditor'])
    portal_type = 'Organisation'

    sql_connection = self.getSQLConnection()

2347
    self.loginByUserName(user2)
2348 2349 2350 2351
    obj2 = folder.newContent(portal_type=portal_type)
    obj2.manage_setLocalRoles(user1, ['Auditor'])
    obj2.manage_permission(perm, ['Owner', 'Auditor'], 0)

2352
    self.loginByUserName(user1)
2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400

    obj = folder.newContent(portal_type=portal_type)
    obj.manage_setLocalRoles(user1, ['Owner', 'Auditor'])

    # Check that nothing is returned when user can not view the object
    obj.manage_permission(perm, [], 0)
    obj.reindexObject()
    self.tic()
    result = obj.portal_catalog(portal_type=portal_type)
    self.assertSameSet([obj2, ], [x.getObject() for x in result])
    result = obj.portal_catalog(portal_type=portal_type, local_roles='Owner')
    self.assertSameSet([], [x.getObject() for x in result])
    result = obj.portal_catalog(portal_type=portal_type, local_roles='Auditor')
    self.assertSameSet([obj2, ], [x.getObject() for x in result])

    # Check that object is returned when he can view the object
    obj.manage_permission(perm, ['Auditor'], 0)
    obj.reindexObject()
    self.tic()
    result = obj.portal_catalog(portal_type=portal_type)
    self.assertSameSet([obj2, obj], [x.getObject() for x in result])
    result = obj.portal_catalog(portal_type=portal_type, local_roles='Owner')
    self.assertSameSet([], [x.getObject() for x in result])
    result = obj.portal_catalog(portal_type=portal_type, local_roles='Auditor')
    self.assertSameSet([obj2, obj], [x.getObject() for x in result])

    # Check that object is returned when he can view the object
    obj.manage_permission(perm, ['Owner'], 0)
    obj.reindexObject()
    self.tic()
    result = obj.portal_catalog(portal_type=portal_type)
    self.assertSameSet([obj2, obj], [x.getObject() for x in result])
    result = obj.portal_catalog(portal_type=portal_type, local_roles='Owner')
    self.assertSameSet([obj], [x.getObject() for x in result])
    result = obj.portal_catalog(portal_type=portal_type, local_roles='Auditor')
    self.assertSameSet([obj2, ], [x.getObject() for x in result])

    # Add a new table to the catalog
    sql_catalog = self.portal.portal_catalog.getSQLCatalog()

    local_roles_table = "test_local_roles"

    create_local_role_table_sql = """
CREATE TABLE `%s` (
  `uid` BIGINT UNSIGNED NOT NULL,
  `owner_reference` varchar(32) NOT NULL default '',
  PRIMARY KEY  (`uid`),
  KEY `version` (`owner_reference`)
2401
) ENGINE=InnoDB;
2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425
    """ % local_roles_table
    sql_catalog.manage_addProduct['ZSQLMethods'].manage_addZSQLMethod(
          id = 'z_create_%s' % local_roles_table,
          title = '',
          arguments = "",
          connection_id = 'erp5_sql_connection',
          template = create_local_role_table_sql)

    drop_local_role_table_sql = """
DROP TABLE IF EXISTS %s
    """ % local_roles_table
    sql_catalog.manage_addProduct['ZSQLMethods'].manage_addZSQLMethod(
          id = 'z0_drop_%s' % local_roles_table,
          title = '',
          arguments = "",
          connection_id = 'erp5_sql_connection',
          template = drop_local_role_table_sql)

    catalog_local_role_sql = """
REPLACE INTO
  %s
VALUES
<dtml-in prefix="loop" expr="_.range(_.len(uid))">
(
2426
  <dtml-sqlvar expr="uid[loop_item]" type="int">,
2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442
  <dtml-sqlvar expr="Base_getOwnerId[loop_item]" type="string" optional>
)
<dtml-if sequence-end>
<dtml-else>
,
</dtml-if>
</dtml-in>
    """ % local_roles_table
    sql_catalog.manage_addProduct['ZSQLMethods'].manage_addZSQLMethod(
          id = 'z_catalog_%s_list' % local_roles_table,
          title = '',
          connection_id = 'erp5_sql_connection',
          arguments = "\n".join(['uid',
                                 'Base_getOwnerId']),
          template = catalog_local_role_sql)

2443
    self.commit()
2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458
    current_sql_catalog_object_list = sql_catalog.sql_catalog_object_list
    sql_catalog.sql_catalog_object_list = \
      current_sql_catalog_object_list + \
         ('z_catalog_%s_list' % local_roles_table,)
    current_sql_clear_catalog = sql_catalog.sql_clear_catalog
    sql_catalog.sql_clear_catalog = \
      current_sql_clear_catalog + \
         ('z0_drop_%s' % local_roles_table, 'z_create_%s' % local_roles_table)
    current_sql_catalog_local_role_keys = \
          sql_catalog.sql_catalog_local_role_keys
    sql_catalog.sql_catalog_local_role_keys = ('Owner | %s.owner_reference' % \
       local_roles_table,)
    current_sql_search_tables = sql_catalog.sql_search_tables
    sql_catalog.sql_search_tables = sql_catalog.sql_search_tables + \
        [local_roles_table]
2459
    self.commit()
2460 2461 2462 2463 2464

    try:
      # Clear catalog
      portal_catalog = self.getCatalogTool()
      portal_catalog.manage_catalogClear()
2465
      self.commit()
2466
      self.portal.portal_caches.clearAllCache()
2467
      self.commit()
2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510
      obj2.reindexObject()

      # Check that nothing is returned when user can not view the object
      obj.manage_permission(perm, [], 0)
      obj.reindexObject()
      self.tic()
      result = obj.portal_catalog(portal_type=portal_type)
      self.assertSameSet([obj2, ], [x.getObject() for x in result])
      method = obj.portal_catalog
      result = obj.portal_catalog(portal_type=portal_type, local_roles='Owner')
      self.assertSameSet([], [x.getObject() for x in result])
      result = obj.portal_catalog(portal_type=portal_type, local_roles='Auditor')
      self.assertSameSet([obj2, ], [x.getObject() for x in result])

      # Check that object is returned when he can view the object
      obj.manage_permission(perm, ['Auditor'], 0)
      obj.reindexObject()
      self.tic()
      result = obj.portal_catalog(portal_type=portal_type)
      self.assertSameSet([obj2, obj], [x.getObject() for x in result])
      result = obj.portal_catalog(portal_type=portal_type, local_roles='Owner')
      self.assertSameSet([obj], [x.getObject() for x in result])
      result = obj.portal_catalog(portal_type=portal_type, local_roles='Auditor')
      self.assertSameSet([obj2, obj], [x.getObject() for x in result])

      # Check that object is returned when he can view the object
      obj.manage_permission(perm, ['Owner'], 0)
      obj.reindexObject()
      self.tic()
      result = obj.portal_catalog(portal_type=portal_type)
      self.assertSameSet([obj2, obj], [x.getObject() for x in result])
      result = obj.portal_catalog(portal_type=portal_type, local_roles='Owner')
      self.assertSameSet([obj], [x.getObject() for x in result])
      result = obj.portal_catalog(portal_type=portal_type, local_roles='Auditor')
      self.assertSameSet([obj2, ], [x.getObject() for x in result])
    finally:
      sql_catalog.sql_catalog_object_list = \
        current_sql_catalog_object_list
      sql_catalog.sql_clear_catalog = \
        current_sql_clear_catalog
      sql_catalog.sql_catalog_local_role_keys = \
          current_sql_catalog_local_role_keys
      sql_catalog.sql_search_tables = current_sql_search_tables
2511
      self.commit()
2512

Jérome Perrin's avatar
Jérome Perrin committed
2513
  def test_MonoValueAssigneeIndexing(self):
2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528
    logout = self.logout
    user1 = 'local_foo'
    user2 = 'local_bar'
    uf = self.getPortal().acl_users
    uf._doAddUser(user1, user1, ['Member', ], [])
    uf._doAddUser(user2, user2, ['Member', ], [])

    perm = 'View'
    folder = self.getOrganisationModule()
    folder.manage_setLocalRoles(user1, ['Author', 'Auditor'])
    folder.manage_setLocalRoles(user2, ['Author', 'Auditor'])
    portal_type = 'Organisation'

    sql_connection = self.getSQLConnection()

2529
    self.loginByUserName(user2)
2530 2531 2532 2533
    obj2 = folder.newContent(portal_type=portal_type)
    obj2.manage_setLocalRoles(user1, ['Auditor'])
    obj2.manage_permission(perm, ['Assignee', 'Auditor'], 0)

2534
    self.loginByUserName(user1)
2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584

    obj = folder.newContent(portal_type=portal_type)
    obj.manage_setLocalRoles(user1, ['Assignee', 'Auditor'])

    # Check that nothing is returned when user can not view the object
    obj.manage_permission(perm, [], 0)
    obj.reindexObject()
    self.tic()
    result = obj.portal_catalog(portal_type=portal_type)
    self.assertSameSet([obj2, ], [x.getObject() for x in result])
    result = obj.portal_catalog(portal_type=portal_type, local_roles='Assignee')
    self.assertSameSet([], [x.getObject() for x in result])
    result = obj.portal_catalog(portal_type=portal_type, local_roles='Auditor')
    self.assertSameSet([obj2, ], [x.getObject() for x in result])

    # Check that object is returned when he can view the object
    obj.manage_permission(perm, ['Auditor'], 0)
    obj.reindexObject()
    self.tic()
    result = obj.portal_catalog(portal_type=portal_type)
    self.assertSameSet([obj2, obj], [x.getObject() for x in result])
    result = obj.portal_catalog(portal_type=portal_type, local_roles='Assignee')
    self.assertSameSet([], [x.getObject() for x in result])
    result = obj.portal_catalog(portal_type=portal_type, local_roles='Auditor')
    self.assertSameSet([obj2, obj], [x.getObject() for x in result])

    # Check that object is returned when he can view the object
    obj.manage_permission(perm, ['Assignee'], 0)
    obj.reindexObject()
    self.tic()
    result = obj.portal_catalog(portal_type=portal_type)
    self.assertSameSet([obj2, obj], [x.getObject() for x in result])
    result = obj.portal_catalog(portal_type=portal_type, local_roles='Assignee')
    self.assertSameSet([obj], [x.getObject() for x in result])
    result = obj.portal_catalog(portal_type=portal_type, local_roles='Auditor')
    self.assertSameSet([obj2, ], [x.getObject() for x in result])

    # Add a new table to the catalog
    sql_catalog = self.portal.portal_catalog.getSQLCatalog()

    local_roles_table = "test_assignee_local_roles"

    create_local_role_table_sql = """
CREATE TABLE `%s` (
  `uid` BIGINT UNSIGNED NOT NULL,
  `assignee_reference` varchar(32) NOT NULL default '',
  `viewable_assignee_reference` varchar(32) NOT NULL default '',
  PRIMARY KEY  (`uid`),
  KEY `assignee_reference` (`assignee_reference`),
  KEY `viewable_assignee_reference` (`viewable_assignee_reference`)
2585
) ENGINE=InnoDB;
2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609
    """ % local_roles_table
    sql_catalog.manage_addProduct['ZSQLMethods'].manage_addZSQLMethod(
          id = 'z_create_%s' % local_roles_table,
          title = '',
          arguments = "",
          connection_id = 'erp5_sql_connection',
          template = create_local_role_table_sql)

    drop_local_role_table_sql = """
DROP TABLE IF EXISTS %s
    """ % local_roles_table
    sql_catalog.manage_addProduct['ZSQLMethods'].manage_addZSQLMethod(
          id = 'z0_drop_%s' % local_roles_table,
          title = '',
          arguments = "",
          connection_id = 'erp5_sql_connection',
          template = drop_local_role_table_sql)

    catalog_local_role_sql = """
REPLACE INTO
  %s
VALUES
<dtml-in prefix="loop" expr="_.range(_.len(uid))">
(
2610
  <dtml-sqlvar expr="uid[loop_item]" type="int">,
2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628
  <dtml-sqlvar expr="getAssignee[loop_item] or ''" type="string" optional>,
  <dtml-sqlvar expr="getViewPermissionAssignee[loop_item] or ''" type="string" optional>
)
<dtml-if sequence-end>
<dtml-else>
,
</dtml-if>
</dtml-in>
    """ % local_roles_table
    sql_catalog.manage_addProduct['ZSQLMethods'].manage_addZSQLMethod(
          id = 'z_catalog_%s_list' % local_roles_table,
          title = '',
          connection_id = 'erp5_sql_connection',
          arguments = "\n".join(['uid',
                                 'getAssignee',
                                 'getViewPermissionAssignee']),
          template = catalog_local_role_sql)

2629
    self.commit()
2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651
    current_sql_catalog_object_list = sql_catalog.sql_catalog_object_list
    sql_catalog.sql_catalog_object_list = \
      current_sql_catalog_object_list + \
         ('z_catalog_%s_list' % local_roles_table,)
    current_sql_clear_catalog = sql_catalog.sql_clear_catalog
    sql_catalog.sql_clear_catalog = \
      current_sql_clear_catalog + \
         ('z0_drop_%s' % local_roles_table, 'z_create_%s' % local_roles_table)
    current_sql_catalog_local_role_keys = \
          sql_catalog.sql_catalog_local_role_keys
    sql_catalog.sql_catalog_local_role_keys = ('Assignee | %s.assignee_reference' % \
       local_roles_table,)

    current_sql_catalog_role_keys = \
          sql_catalog.sql_catalog_role_keys
    sql_catalog.sql_catalog_role_keys = (
        'Assignee | %s.viewable_assignee_reference' % \
       local_roles_table,)

    current_sql_search_tables = sql_catalog.sql_search_tables
    sql_catalog.sql_search_tables = sql_catalog.sql_search_tables + \
        [local_roles_table]
2652
    self.commit()
2653 2654 2655 2656 2657

    try:
      # Clear catalog
      portal_catalog = self.getCatalogTool()
      portal_catalog.manage_catalogClear()
2658
      self.commit()
2659
      self.portal.portal_caches.clearAllCache()
2660
      self.commit()
2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705
      obj2.reindexObject()

      # Check that nothing is returned when user can not view the object
      obj.manage_permission(perm, [], 0)
      obj.reindexObject()
      self.tic()
      result = obj.portal_catalog(portal_type=portal_type)
      self.assertSameSet([obj2, ], [x.getObject() for x in result])
      method = obj.portal_catalog
      result = obj.portal_catalog(portal_type=portal_type, local_roles='Assignee')
      self.assertSameSet([], [x.getObject() for x in result])
      result = obj.portal_catalog(portal_type=portal_type, local_roles='Auditor')
      self.assertSameSet([obj2, ], [x.getObject() for x in result])

      # Check that object is returned when he can view the object
      obj.manage_permission(perm, ['Auditor'], 0)
      obj.reindexObject()
      self.tic()
      result = obj.portal_catalog(portal_type=portal_type)
      self.assertSameSet([obj2, obj], [x.getObject() for x in result])
      result = obj.portal_catalog(portal_type=portal_type, local_roles='Assignee')
      self.assertSameSet([obj], [x.getObject() for x in result])
      result = obj.portal_catalog(portal_type=portal_type, local_roles='Auditor')
      self.assertSameSet([obj2, obj], [x.getObject() for x in result])

      # Check that object is returned when he can view the object
      obj.manage_permission(perm, ['Assignee'], 0)
      obj.reindexObject()
      self.tic()
      result = obj.portal_catalog(portal_type=portal_type)
      self.assertSameSet([obj2, obj], [x.getObject() for x in result])
      result = obj.portal_catalog(portal_type=portal_type, local_roles='Assignee')
      self.assertSameSet([obj], [x.getObject() for x in result])
      result = obj.portal_catalog(portal_type=portal_type, local_roles='Auditor')
      self.assertSameSet([obj2, ], [x.getObject() for x in result])
    finally:
      sql_catalog.sql_catalog_object_list = \
        current_sql_catalog_object_list
      sql_catalog.sql_clear_catalog = \
        current_sql_clear_catalog
      sql_catalog.sql_catalog_local_role_keys = \
          current_sql_catalog_local_role_keys
      sql_catalog.sql_catalog_role_keys = \
          current_sql_catalog_role_keys
      sql_catalog.sql_search_tables = current_sql_search_tables
2706
      self.commit()
2707

Jérome Perrin's avatar
Jérome Perrin committed
2708
  def test_UserOrGroupRoleIndexing(self):
2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727
    logout = self.logout
    user1 = 'a_great_user_name'
    user1_group = 'a_great_user_group'
    uf = self.getPortal().acl_users
    uf._doAddUser(user1, user1, ['Member', ], [])
    uf.zodb_groups.addGroup( user1_group, user1_group, user1_group)
    new = uf.zodb_groups.addPrincipalToGroup( user1, user1_group )

    perm = 'View'
    folder = self.getOrganisationModule()
    folder.manage_setLocalRoles(user1, ['Author', 'Auditor'])
    portal_type = 'Organisation'
    organisation = folder.newContent(portal_type=portal_type)

    sql_connection = self.getSQLConnection()
    def query(sql):
      result = sql_connection.manage_test(sql)
      return result.dictionaries()

2728
    self.loginByUserName(user1)
2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742

    # Add a new table to the catalog
    sql_catalog = self.portal.portal_catalog.getSQLCatalog()

    local_roles_table = "test_user_or_group_local_roles"

    create_local_role_table_sql = """
CREATE TABLE `%s` (
  `uid` BIGINT UNSIGNED NOT NULL,
  `assignee_reference` varchar(32) NOT NULL default '',
  `viewable_assignee_reference` varchar(32) NOT NULL default '',
  PRIMARY KEY  (`uid`),
  KEY `assignee_reference` (`assignee_reference`),
  KEY `viewable_assignee_reference` (`viewable_assignee_reference`)
2743
) ENGINE=InnoDB;
2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767
    """ % local_roles_table
    sql_catalog.manage_addProduct['ZSQLMethods'].manage_addZSQLMethod(
          id = 'z_create_%s' % local_roles_table,
          title = '',
          arguments = "",
          connection_id = 'erp5_sql_connection',
          template = create_local_role_table_sql)

    drop_local_role_table_sql = """
DROP TABLE IF EXISTS %s
    """ % local_roles_table
    sql_catalog.manage_addProduct['ZSQLMethods'].manage_addZSQLMethod(
          id = 'z0_drop_%s' % local_roles_table,
          title = '',
          arguments = "",
          connection_id = 'erp5_sql_connection',
          template = drop_local_role_table_sql)

    catalog_local_role_sql = """
REPLACE INTO
  %s
VALUES
<dtml-in prefix="loop" expr="_.range(_.len(uid))">
(
2768
  <dtml-sqlvar expr="uid[loop_item]" type="int">,
2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786
  <dtml-sqlvar expr="getAssignee[loop_item] or ''" type="string" optional>,
  <dtml-sqlvar expr="getViewPermissionAssignee[loop_item] or ''" type="string" optional>
)
<dtml-if sequence-end>
<dtml-else>
,
</dtml-if>
</dtml-in>
    """ % local_roles_table
    sql_catalog.manage_addProduct['ZSQLMethods'].manage_addZSQLMethod(
          id = 'z_catalog_%s_list' % local_roles_table,
          title = '',
          connection_id = 'erp5_sql_connection',
          arguments = "\n".join(['uid',
                                 'getAssignee',
                                 'getViewPermissionAssignee']),
          template = catalog_local_role_sql)

2787
    self.commit()
2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813
    current_sql_catalog_object_list = sql_catalog.sql_catalog_object_list
    sql_catalog.sql_catalog_object_list = \
      current_sql_catalog_object_list + \
         ('z_catalog_%s_list' % local_roles_table,)
    current_sql_clear_catalog = sql_catalog.sql_clear_catalog
    sql_catalog.sql_clear_catalog = \
      current_sql_clear_catalog + \
         ('z0_drop_%s' % local_roles_table, 'z_create_%s' % local_roles_table)
    current_sql_catalog_local_role_keys = \
          sql_catalog.sql_catalog_local_role_keys
    sql_catalog.sql_catalog_local_role_keys = (
        'Owner | viewable_owner',
        'Assignee | %s.assignee_reference' % \
       local_roles_table,)

    current_sql_catalog_role_keys = \
          sql_catalog.sql_catalog_role_keys
    sql_catalog.sql_catalog_role_keys = (
        'Assignee | %s.viewable_assignee_reference' % \
       local_roles_table,)

    current_sql_search_tables = sql_catalog.sql_search_tables
    sql_catalog.sql_search_tables = sql_catalog.sql_search_tables + \
        [local_roles_table]

    portal = self.getPortal()
2814
    self.commit()
2815 2816 2817 2818 2819

    try:
      # Clear catalog
      portal_catalog = self.getCatalogTool()
      portal_catalog.manage_catalogClear()
2820
      self.commit()
2821
      self.portal.portal_caches.clearAllCache()
2822
      self.commit()
2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854

      organisation_relative_url = organisation.getRelativeUrl()
      countResults = organisation.portal_catalog.countResults
      count_result_kw = {'relative_url': organisation_relative_url}

      use_case_number = 0
      for view_permission_role_list, security_group_list, \
          global_view, associate_view, assignee_view, both_view in \
          [
              # No view permission
              ([], [], 0, 0, 0, 0),
              ([], [(user1, ['Associate'])], 0, 0, 0, 0),
              ([], [(user1, ['Assignee'])], 0, 0, 0, 0),
              ([], [(user1, ['Assignee', 'Associate'])], 0, 0, 0, 0),
              ([], [(user1_group, ['Assignee'])], 0, 0, 0, 0),
              ([], [(user1_group, ['Assignee', 'Associate'])], 0, 0, 0, 0),
              ([], [(user1, ['Assignee']),
                    (user1_group, ['Assignee'])], 0, 0, 0, 0),
              ([], [(user1, ['Assignee']),
                    (user1_group, ['Assignee', 'Associate'])], 0, 0, 0, 0),

              # View permission for Assignee
              (['Assignee'], [], 0, 0, 0, 0),
              (['Assignee'], [(user1, ['Associate'])], 0, 0, 0, 0),
              (['Assignee'], [(user1, ['Assignee'])], 1, 0, 1, 1),
              (['Assignee'], [(user1, ['Assignee', 'Associate'])], 1, 0, 1, 1),
              (['Assignee'], [(user1_group, ['Assignee'])], 1, 0, 0, 0),
              (['Assignee'], [(user1_group, ['Assignee', 'Associate'])],
                              1, 0, 0, 0),
              (['Assignee'], [(user1, ['Assignee']),
                              (user1_group, ['Assignee'])], 1, 0, 1, 1),
              (['Assignee'], [(user1, ['Assignee']),
2855
                              (user1_group, ['Assignee', 'Associate'])],
2856 2857 2858 2859 2860 2861 2862 2863
                               1, 0, 1, 1),

              # View permission for Associate
              (['Associate'], [], 0, 0, 0, 0),
              (['Associate'], [(user1, ['Associate'])], 1, 1, 0, 0),
              (['Associate'], [(user1, ['Assignee'])], 0, 0, 0, 0),
              (['Associate'], [(user1, ['Assignee', 'Associate'])], 1, 1, 1, 1),
              (['Associate'], [(user1_group, ['Assignee'])], 0, 0, 0, 0),
2864
              (['Associate'], [(user1_group, ['Assignee', 'Associate'])],
2865 2866 2867 2868
                               1, 1, 0, 0),
              (['Associate'], [(user1, ['Assignee']),
                              (user1_group, ['Assignee'])], 0, 0, 0, 0),
              (['Associate'], [(user1, ['Assignee']),
2869
                              (user1_group, ['Assignee', 'Associate'])],
2870 2871 2872 2873 2874 2875
                               1, 1, 1, 1),

              # View permission for Associate and Assignee
              (['Associate', 'Assignee'], [], 0, 0, 0, 0),
              (['Associate', 'Assignee'], [(user1, ['Associate'])], 1, 1, 0, 0),
              (['Associate', 'Assignee'], [(user1, ['Assignee'])], 1, 0, 1, 1),
2876
              (['Associate', 'Assignee'],
2877
                     [(user1, ['Assignee', 'Associate'])], 1, 1, 1, 1),
2878
              (['Associate', 'Assignee'],
2879
                     [(user1_group, ['Assignee'])], 1, 0, 0, 0),
2880
              (['Associate', 'Assignee'],
2881 2882 2883 2884
                     [(user1_group, ['Assignee', 'Associate'])], 1, 1, 0, 0),
              (['Associate', 'Assignee'], [(user1, ['Assignee']),
                              (user1_group, ['Assignee'])], 1, 0, 1, 1),
              (['Associate', 'Assignee'], [(user1, ['Assignee']),
2885
                              (user1_group, ['Assignee', 'Associate'])],
2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939
                               1, 1, 1, 1),
              ]:

        use_case_number += 1
        organisation.manage_permission(perm, view_permission_role_list, 0)
        organisation.manage_delLocalRoles([user1, user1_group])
        for security_group, local_role_list in security_group_list:
          organisation.manage_setLocalRoles(security_group, local_role_list)
        organisation.reindexObject()
        self.tic()

        for expected_result, local_roles in \
            [
                (global_view, None),
                (associate_view, 'Associate'),
                (assignee_view, 'Assignee'),
                (both_view, ['Associate', 'Assignee']),
                ]:

          object_security_uid = query(
            'SELECT security_uid FROM catalog WHERE relative_url="%s"' % \
            organisation_relative_url
              )[0]['security_uid']

          if object_security_uid is not None:
            roles_and_users = query(
              'SELECT allowedRolesAndUsers FROM roles_and_users WHERE uid="%s"' % \
              object_security_uid
                )
          else:
            roles_and_users = ''

          monovalue_references = query(
              'SELECT * FROM test_user_or_group_local_roles WHERE uid="%s"' % \
                  organisation.getUid())[0]
          assignee_reference = monovalue_references['assignee_reference']
          viewable_assignee_reference = \
            monovalue_references['viewable_assignee_reference']

          result = countResults(local_roles=local_roles, **count_result_kw)[0][0]
          if result != expected_result:
            countResults(local_roles=local_roles, src__=1,
                         **count_result_kw)
            self.fail('Use case %s\n\tView permission is given to: %s\n\t' \
                      'Local roles are: %s\n\t' \
                      'local_roles parameter is: %s\n\t' \
                      'Object IS %s returned by portal_catalog!\n\t' \
                      '\n\tSecurity uid is: %s\n\t'
                      'Roles and users:  %s\n\t'
                      'assignee_reference:  %s\n\t'
                      'viewable_assignee_reference:  %s\n\t'
                      '\n\tSQL generated: \n\n%s' \
                      '' % \
                      (use_case_number,
2940
                       view_permission_role_list,
2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959
                       organisation.__ac_local_roles__,
                       local_roles, ['NOT', ''][result],
                       object_security_uid,
                       str([x['allowedRolesAndUsers'] for x in roles_and_users]),
                       assignee_reference,
                       viewable_assignee_reference,
                       countResults(local_roles=local_roles, src__=1,
                                    **count_result_kw)))

    finally:
      sql_catalog.sql_catalog_object_list = \
        current_sql_catalog_object_list
      sql_catalog.sql_clear_catalog = \
        current_sql_clear_catalog
      sql_catalog.sql_catalog_local_role_keys = \
          current_sql_catalog_local_role_keys
      sql_catalog.sql_catalog_role_keys = \
          current_sql_catalog_role_keys
      sql_catalog.sql_search_tables = current_sql_search_tables
2960
      self.commit()
2961

Jérome Perrin's avatar
Jérome Perrin committed
2962
  def test_UserOrGroupLocalRoleIndexing(self):
2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981
    logout = self.logout
    user1 = 'another_great_user_name'
    user1_group = 'another_great_user_group'
    uf = self.getPortal().acl_users
    uf._doAddUser(user1, user1, ['Member', ], [])
    uf.zodb_groups.addGroup( user1_group, user1_group, user1_group)
    new = uf.zodb_groups.addPrincipalToGroup( user1, user1_group )

    perm = 'View'
    folder = self.getOrganisationModule()
    folder.manage_setLocalRoles(user1, ['Author', 'Auditor'])
    portal_type = 'Organisation'
    organisation = folder.newContent(portal_type=portal_type)

    sql_connection = self.getSQLConnection()
    def query(sql):
      result = sql_connection.manage_test(sql)
      return result.dictionaries()

2982
    self.loginByUserName(user1)
2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994

    # Add a new table to the catalog
    sql_catalog = self.portal.portal_catalog.getSQLCatalog()

    local_roles_table = "another_test_user_or_group_local_roles"

    create_local_role_table_sql = """
CREATE TABLE `%s` (
  `uid` BIGINT UNSIGNED NOT NULL,
  `viewable_assignee_reference` varchar(32) NOT NULL default '',
  PRIMARY KEY  (`uid`),
  KEY `viewable_assignee_reference` (`viewable_assignee_reference`)
2995
) ENGINE=InnoDB;
2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019
    """ % local_roles_table
    sql_catalog.manage_addProduct['ZSQLMethods'].manage_addZSQLMethod(
          id = 'z_create_%s' % local_roles_table,
          title = '',
          arguments = "",
          connection_id = 'erp5_sql_connection',
          template = create_local_role_table_sql)

    drop_local_role_table_sql = """
DROP TABLE IF EXISTS %s
    """ % local_roles_table
    sql_catalog.manage_addProduct['ZSQLMethods'].manage_addZSQLMethod(
          id = 'z0_drop_%s' % local_roles_table,
          title = '',
          arguments = "",
          connection_id = 'erp5_sql_connection',
          template = drop_local_role_table_sql)

    catalog_local_role_sql = """
REPLACE INTO
  %s
VALUES
<dtml-in prefix="loop" expr="_.range(_.len(uid))">
(
3020
  <dtml-sqlvar expr="uid[loop_item]" type="int">,
3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036
  <dtml-sqlvar expr="getViewPermissionAssignee[loop_item] or ''" type="string" optional>
)
<dtml-if sequence-end>
<dtml-else>
,
</dtml-if>
</dtml-in>
    """ % local_roles_table
    sql_catalog.manage_addProduct['ZSQLMethods'].manage_addZSQLMethod(
          id = 'z_catalog_%s_list' % local_roles_table,
          title = '',
          connection_id = 'erp5_sql_connection',
          arguments = "\n".join(['uid',
                                 'getViewPermissionAssignee']),
          template = catalog_local_role_sql)

3037
    self.commit()
3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058
    current_sql_catalog_object_list = sql_catalog.sql_catalog_object_list
    sql_catalog.sql_catalog_object_list = \
      current_sql_catalog_object_list + \
         ('z_catalog_%s_list' % local_roles_table,)
    current_sql_clear_catalog = sql_catalog.sql_clear_catalog
    sql_catalog.sql_clear_catalog = \
      current_sql_clear_catalog + \
         ('z0_drop_%s' % local_roles_table, 'z_create_%s' % local_roles_table)

    current_sql_catalog_role_keys = \
          sql_catalog.sql_catalog_role_keys
    sql_catalog.sql_catalog_role_keys = (
        'Owner | viewable_owner',
        'Assignee | %s.viewable_assignee_reference' % \
       local_roles_table,)

    current_sql_search_tables = sql_catalog.sql_search_tables
    sql_catalog.sql_search_tables = sql_catalog.sql_search_tables + \
        [local_roles_table]

    portal = self.getPortal()
3059
    self.commit()
3060 3061 3062 3063 3064

    try:
      # Clear catalog
      portal_catalog = self.getCatalogTool()
      portal_catalog.manage_catalogClear()
3065
      self.commit()
3066
      self.portal.portal_caches.clearAllCache()
3067
      self.commit()
3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116

      organisation_relative_url = organisation.getRelativeUrl()
      countResults = organisation.portal_catalog.countResults
      count_result_kw = {'relative_url': organisation_relative_url}

      use_case_number = 0
      for view_permission_role_list, security_group_list, \
          associate_view, assignee_view in \
          [
              # No view permission
              ([], [], 0, 0),
              ([], [(user1, ['Associate'])], 0, 0),
              ([], [(user1, ['Assignee'])], 0, 0),
              ([], [(user1, ['Assignee', 'Associate'])], 0, 0),
              ([], [(user1_group, ['Assignee'])], 0, 0),
              ([], [(user1_group, ['Assignee', 'Associate'])], 0, 0),
              ([], [(user1, ['Assignee']),
                    (user1_group, ['Assignee'])], 0, 0),
              ([], [(user1, ['Assignee']),
                    (user1_group, ['Assignee', 'Associate'])], 0, 0),

              # View permission for Assignee
              (['Assignee'], [], 0, 0),
              (['Assignee'], [(user1, ['Associate'])], 0, 0),
              (['Assignee'], [(user1, ['Assignee'])], 0, 1),
              (['Assignee'], [(user1, ['Assignee', 'Associate'])], 0, 1),
              (['Assignee'], [(user1_group, ['Assignee'])], 0, 1),
              (['Assignee'], [(user1_group, ['Assignee', 'Associate'])], 0, 1),
              (['Assignee'], [(user1, ['Assignee']),
                              (user1_group, ['Assignee'])], 0, 1),
              (['Assignee'], [(user1, ['Assignee']),
                              (user1_group, ['Assignee', 'Associate'])], 0, 1),

              # View permission for Associate
              (['Associate'], [], 0, 0),
              (['Associate'], [(user1, ['Associate'])], 1, 0),
              (['Associate'], [(user1, ['Assignee'])], 0, 0),
              (['Associate'], [(user1, ['Assignee', 'Associate'])], 1, 0),
              (['Associate'], [(user1_group, ['Assignee'])], 0, 0),
              (['Associate'], [(user1_group, ['Assignee', 'Associate'])], 1, 0),
              (['Associate'], [(user1, ['Assignee']),
                              (user1_group, ['Assignee'])], 0, 0),
              (['Associate'], [(user1, ['Assignee']),
                              (user1_group, ['Assignee', 'Associate'])], 1, 0),

              # View permission for Associate and Assignee
              (['Associate', 'Assignee'], [], 0, 0),
              (['Associate', 'Assignee'], [(user1, ['Associate'])], 1, 0),
              (['Associate', 'Assignee'], [(user1, ['Assignee'])], 0, 1),
3117
              (['Associate', 'Assignee'],
3118
                     [(user1, ['Assignee', 'Associate'])], 1, 1),
3119
              (['Associate', 'Assignee'],
3120
                     [(user1_group, ['Assignee'])], 0, 1),
3121
              (['Associate', 'Assignee'],
3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177
                     [(user1_group, ['Assignee', 'Associate'])], 1, 1),
              (['Associate', 'Assignee'], [(user1, ['Assignee']),
                              (user1_group, ['Assignee'])], 0, 1),
              (['Associate', 'Assignee'], [(user1, ['Assignee']),
                              (user1_group, ['Assignee', 'Associate'])], 1, 1),
              ]:

        use_case_number += 1
        organisation.manage_permission(perm, view_permission_role_list, 0)
        organisation.manage_delLocalRoles([user1, user1_group])
        for security_group, local_role_list in security_group_list:
          organisation.manage_setLocalRoles(security_group, local_role_list)
        organisation.reindexObject()
        self.tic()

        for expected_result, local_roles in \
            [
                (associate_view or assignee_view, None),
                (associate_view, 'Associate'),
                (assignee_view, 'Assignee'),
                (associate_view or assignee_view, ['Associate', 'Assignee']),
                ]:

          object_security_uid = query(
            'SELECT security_uid FROM catalog WHERE relative_url="%s"' % \
            organisation_relative_url
              )[0]['security_uid']

          if object_security_uid is not None:
            roles_and_users = query(
              'SELECT allowedRolesAndUsers FROM roles_and_users WHERE uid="%s"' % \
              object_security_uid
                )
          else:
            roles_and_users = ''

          monovalue_references = query(
              'SELECT * FROM %s WHERE uid="%s"' % \
                 (local_roles_table, organisation.getUid()))[0]
          viewable_assignee_reference = \
            monovalue_references['viewable_assignee_reference']

          result = countResults(local_roles=local_roles, **count_result_kw)[0][0]
          if result != expected_result:
            countResults(local_roles=local_roles, src__=1,
                         **count_result_kw)
            self.fail('Use case %s\n\tView permission is given to: %s\n\t' \
                      'Local roles are: %s\n\t' \
                      'local_roles parameter is: %s\n\t' \
                      'Object IS %s returned by portal_catalog!\n\t' \
                      '\n\tSecurity uid is: %s\n\t'
                      'Roles and users:  %s\n\t'
                      'viewable_assignee_reference:  %s\n\t'
                      '\n\tSQL generated: \n\n%s' \
                      '' % \
                      (use_case_number,
3178
                       view_permission_role_list,
3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194
                       organisation.__ac_local_roles__,
                       local_roles, ['NOT', ''][result],
                       object_security_uid,
                       str([x['allowedRolesAndUsers'] for x in roles_and_users]),
                       viewable_assignee_reference,
                       countResults(local_roles=local_roles, src__=1,
                                    **count_result_kw)))

    finally:
      sql_catalog.sql_catalog_object_list = \
        current_sql_catalog_object_list
      sql_catalog.sql_clear_catalog = \
        current_sql_clear_catalog
      sql_catalog.sql_catalog_role_keys = \
          current_sql_catalog_role_keys
      sql_catalog.sql_search_tables = current_sql_search_tables
3195
      self.commit()
3196

3197 3198 3199
  # Low priority bug, which needs a lot of time to be fixed
  # Marked as expectedFailure
  @expectedFailure
3200
  def test_PersonDocumentWithMonovaluedLocalRole(self):
3201 3202 3203 3204 3205
    """Test when user is added, which has local roles on own Person Document

    This is a case when Person document containting reference with local role
    which shall be monovalued is reindexed for first time.
    """
3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223
    user = 'person_document_user_name'

    sql_connection = self.getSQLConnection()
    def query(sql):
      result = sql_connection.manage_test(sql)
      return result.dictionaries()

    # Add a new table to the catalog
    sql_catalog = self.portal.portal_catalog.getSQLCatalog()

    local_roles_table = "person_document_test_user_or_group_local_roles"

    create_local_role_table_sql = """
CREATE TABLE `%s` (
  `uid` BIGINT UNSIGNED NOT NULL,
  `viewable_assignee_reference` varchar(32) NOT NULL default '',
  PRIMARY KEY  (`uid`),
  KEY `viewable_assignee_reference` (`viewable_assignee_reference`)
3224
) ENGINE=InnoDB;
3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248
    """ % local_roles_table
    sql_catalog.manage_addProduct['ZSQLMethods'].manage_addZSQLMethod(
          id = 'z_create_%s' % local_roles_table,
          title = '',
          arguments = "",
          connection_id = 'erp5_sql_connection',
          template = create_local_role_table_sql)

    drop_local_role_table_sql = """
DROP TABLE IF EXISTS %s
    """ % local_roles_table
    sql_catalog.manage_addProduct['ZSQLMethods'].manage_addZSQLMethod(
          id = 'z0_drop_%s' % local_roles_table,
          title = '',
          arguments = "",
          connection_id = 'erp5_sql_connection',
          template = drop_local_role_table_sql)

    catalog_local_role_sql = """
REPLACE INTO
  %s
VALUES
<dtml-in prefix="loop" expr="_.range(_.len(uid))">
(
3249
  <dtml-sqlvar expr="uid[loop_item]" type="int">,
3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265
  <dtml-sqlvar expr="getViewPermissionAssignee[loop_item] or ''" type="string" optional>
)
<dtml-if sequence-end>
<dtml-else>
,
</dtml-if>
</dtml-in>
    """ % local_roles_table
    sql_catalog.manage_addProduct['ZSQLMethods'].manage_addZSQLMethod(
          id = 'z_catalog_%s_list' % local_roles_table,
          title = '',
          connection_id = 'erp5_sql_connection',
          arguments = "\n".join(['uid',
                                 'getViewPermissionAssignee']),
          template = catalog_local_role_sql)

3266
    self.commit()
3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287
    current_sql_catalog_object_list = sql_catalog.sql_catalog_object_list
    sql_catalog.sql_catalog_object_list = \
      current_sql_catalog_object_list + \
         ('z_catalog_%s_list' % local_roles_table,)
    current_sql_clear_catalog = sql_catalog.sql_clear_catalog
    sql_catalog.sql_clear_catalog = \
      current_sql_clear_catalog + \
         ('z0_drop_%s' % local_roles_table, 'z_create_%s' % local_roles_table)

    current_sql_catalog_role_keys = \
          sql_catalog.sql_catalog_role_keys
    sql_catalog.sql_catalog_role_keys = (
        'Owner | viewable_owner',
        'Assignee | %s.viewable_assignee_reference' % \
       local_roles_table,)

    current_sql_search_tables = sql_catalog.sql_search_tables
    sql_catalog.sql_search_tables = sql_catalog.sql_search_tables + \
        [local_roles_table]

    portal = self.getPortal()
3288
    self.commit()
3289 3290 3291 3292 3293

    try:
      # Clear catalog
      portal_catalog = self.getCatalogTool()
      portal_catalog.manage_catalogClear()
3294
      self.commit()
3295
      self.portal.portal_caches.clearAllCache()
3296
      self.commit()
3297 3298 3299 3300 3301 3302 3303 3304

      person = self.portal.person_module.newContent(portal_type='Person',
          reference=user)
      person.manage_setLocalRoles(user, ['Assignee'])
      self.tic()

      roles_and_users_result = query('select * from roles_and_users where uid = (select security_uid from catalog where uid = %s)' % person.getUid())
      local_roles_table_result = query('select * from %s where uid = %s' % (local_roles_table, person.getUid()))[0]
3305

3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320
      # check that local seucirty table is clean about created person object
      self.assertSameSet(
          sorted([q['allowedRolesAndUsers'] for q in roles_and_users_result]),
          ['Assignee', 'Assignor', 'Associate', 'Auditor', 'Author', 'Manager']
      )
      # check that user has optimised security declaration
      self.assertEqual(local_roles_table_result['viewable_assignee_reference'], user)
    finally:
      sql_catalog.sql_catalog_object_list = \
        current_sql_catalog_object_list
      sql_catalog.sql_clear_catalog = \
        current_sql_clear_catalog
      sql_catalog.sql_catalog_role_keys = \
          current_sql_catalog_role_keys
      sql_catalog.sql_search_tables = current_sql_search_tables
3321
      self.commit()
3322

Jérome Perrin's avatar
Jérome Perrin committed
3323 3324
  def test_ObjectReindexationConcurency(self):
    portal = self.portal
3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339

    portal_activities = getattr(portal, 'portal_activities', None)
    if portal_activities is None:
      ZopeTestCase._print('\n Skipping test_ObjectReindexatoinConcurency (portal_activities not found)')
      return

    container = organisation_module = portal.organisation_module
    document_1 = container.newContent()
    document_1_1 = document_1.newContent()
    document_1_2 = document_1.newContent()
    document_2 = container.newContent()
    self.tic()
    # First case: parent, then child
    document_1.reindexObject()
    self.assertEqual(len(portal_activities.getMessageList()), 0)
3340
    self.commit()
3341 3342 3343 3344
    self.assertEqual(len(portal_activities.getMessageList()), 1)
    portal_activities.distribute()
    self.assertEqual(len([x for x in portal_activities.getMessageList() if x.processing_node == 0]), 1)
    document_1_1.reindexObject()
3345
    self.commit()
3346 3347 3348 3349 3350 3351 3352 3353
    self.assertEqual(len(portal_activities.getMessageList()), 2)
    portal_activities.distribute()
    self.assertEqual(len([x for x in portal_activities.getMessageList() if x.processing_node == 0]), 1)
    self.tic()
    # Variation of first case: parent's borther along
    document_1.reindexObject()
    document_2.reindexObject()
    self.assertEqual(len(portal_activities.getMessageList()), 0)
3354
    self.commit()
3355 3356 3357 3358
    self.assertEqual(len(portal_activities.getMessageList()), 2)
    portal_activities.distribute()
    self.assertEqual(len([x for x in portal_activities.getMessageList() if x.processing_node == 0]), 2)
    document_1_1.reindexObject()
3359
    self.commit()
3360 3361 3362 3363 3364 3365 3366
    self.assertEqual(len(portal_activities.getMessageList()), 3)
    portal_activities.distribute()
    self.assertEqual(len([x for x in portal_activities.getMessageList() if x.processing_node == 0]), 2)
    self.tic()
    # Second case: child, then parent
    document_1_1.reindexObject()
    self.assertEqual(len(portal_activities.getMessageList()), 0)
3367
    self.commit()
3368 3369 3370 3371
    self.assertEqual(len(portal_activities.getMessageList()), 1)
    portal_activities.distribute()
    self.assertEqual(len([x for x in portal_activities.getMessageList() if x.processing_node == 0]), 1)
    document_1.reindexObject()
3372
    self.commit()
3373 3374 3375 3376 3377 3378 3379 3380
    self.assertEqual(len(portal_activities.getMessageList()), 2)
    portal_activities.distribute()
    self.assertEqual(len([x for x in portal_activities.getMessageList() if x.processing_node == 0]), 1)
    self.tic()
    # Variation of second case: parent's borther along
    document_1_1.reindexObject()
    document_2.reindexObject()
    self.assertEqual(len(portal_activities.getMessageList()), 0)
3381
    self.commit()
3382 3383 3384 3385
    self.assertEqual(len(portal_activities.getMessageList()), 2)
    portal_activities.distribute()
    self.assertEqual(len([x for x in portal_activities.getMessageList() if x.processing_node == 0]), 2)
    document_1.reindexObject()
3386
    self.commit()
3387 3388 3389 3390 3391 3392 3393
    self.assertEqual(len(portal_activities.getMessageList()), 3)
    portal_activities.distribute()
    self.assertEqual(len([x for x in portal_activities.getMessageList() if x.processing_node == 0]), 2)
    self.tic()
    # Third case: child 1, then child 2
    document_1_1.reindexObject()
    self.assertEqual(len(portal_activities.getMessageList()), 0)
3394
    self.commit()
3395 3396 3397 3398
    self.assertEqual(len(portal_activities.getMessageList()), 1)
    portal_activities.distribute()
    self.assertEqual(len([x for x in portal_activities.getMessageList() if x.processing_node == 0]), 1)
    document_1_2.reindexObject()
3399
    self.commit()
3400 3401 3402 3403 3404 3405 3406 3407
    self.assertEqual(len(portal_activities.getMessageList()), 2)
    portal_activities.distribute()
    self.assertEqual(len([x for x in portal_activities.getMessageList() if x.processing_node == 0]), 1)
    self.tic()
    # Variation of third case: parent's borther along
    document_1_1.reindexObject()
    document_2.reindexObject()
    self.assertEqual(len(portal_activities.getMessageList()), 0)
3408
    self.commit()
3409 3410 3411 3412
    self.assertEqual(len(portal_activities.getMessageList()), 2)
    portal_activities.distribute()
    self.assertEqual(len([x for x in portal_activities.getMessageList() if x.processing_node == 0]), 2)
    document_1_2.reindexObject()
3413
    self.commit()
3414 3415 3416 3417 3418
    self.assertEqual(len(portal_activities.getMessageList()), 3)
    portal_activities.distribute()
    self.assertEqual(len([x for x in portal_activities.getMessageList() if x.processing_node == 0]), 2)
    self.tic()

Jérome Perrin's avatar
Jérome Perrin committed
3419
  def test_PercentCharacter(self):
3420 3421 3422 3423 3424 3425 3426 3427
    """
    Check expected behaviour of % character for simple query
    """
    portal_type = 'Organisation'
    folder = self.getOrganisationModule()
    folder.newContent(portal_type=portal_type, title='foo_organisation_1')
    folder.newContent(portal_type=portal_type, title='foo_organisation_2')
    self.tic()
3428
    self.assertEqual(1, len(folder.portal_catalog(portal_type=portal_type,
3429
                                                   title='foo_organisation_1')))
3430
    self.assertEqual(1, len(folder.portal_catalog(portal_type=portal_type,
3431
                                                   title='foo_organisation_2')))
3432
    self.assertEqual(1, len(folder.portal_catalog(portal_type=portal_type,
3433
                                                   title='%organisation_1')))
3434
    self.assertEqual(2, len(folder.portal_catalog(portal_type=portal_type,
3435
                                                   title='foo_organisation%')))
3436
    self.assertEqual(1, len(folder.portal_catalog(portal_type=portal_type,
3437 3438
                                                   title='foo_org%ion_1')))

Jérome Perrin's avatar
Jérome Perrin committed
3439
  def test_SearchedStringIsNotStripped(self):
3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453
    """
      Check that extra spaces in lookup values are preserved
    """
    portal_type = 'Organisation'
    folder = self.getOrganisationModule()
    first_doc = folder.newContent(portal_type=portal_type, reference="foo")
    second_doc = folder.newContent(portal_type=portal_type, reference=" foo")
    self.tic()
    def compareSet(reference, document_list):
      result = folder.portal_catalog(portal_type=portal_type,
                                     reference=reference)
      self.assertSameSet(document_list, [x.getObject() for x in result])
    compareSet('foo', [first_doc])
    compareSet(' foo', [second_doc])
3454 3455 3456 3457 3458
    # XXX: Those will hardly work, and it probably not the responsability of python code:
    # MySQL ignores trailing spaces in conditions.
    # So it's probably not really part of this test.
    #compareSet('foo ', [])
    #compareSet(' foo ', [])
3459

Jérome Perrin's avatar
Jérome Perrin committed
3460
  def test_WildcardMatchesUnsetValue(self):
3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471
    """
      Check that the "%" wildcard matches unset values.
    """
    portal_type = 'Organisation'
    folder = self.getOrganisationModule()
    first_doc = folder.newContent(portal_type=portal_type, reference="doc 1")
    second_doc = folder.newContent(portal_type=portal_type, reference="doc 2", description="test")
    self.tic()
    result = folder.portal_catalog(portal_type=portal_type, reference='doc %', description='%')
    self.assertEqual(len(result), 2)

Jérome Perrin's avatar
Jérome Perrin committed
3472
  def test_multipleRelatedKeyDoMultipleJoins(self):
3473 3474 3475 3476 3477 3478 3479 3480 3481 3482
    """
      Check that when multiple related keys are present in the same query,
      each one does a separate join.
      ie:
        Searching for an object whose site_title is "foo" and
        site_description is "bar" will yeld a result set which is the union
        of:
          - objects whose site_title is foo
          - objects whose site_description is bar
    """
Jérome Perrin's avatar
Jérome Perrin committed
3483
    portal = self.portal
3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527
    def _create(**kw):
      return portal.organisation_module.newContent(portal_type='Organisation', **kw)
    def create(id, related_obect_list):
      return _create(id=id,
        site_value_list=related_obect_list,
        function_value_list=related_obect_list)
    def check(expected_result, description, query):
      result = [x.getObject() for x in portal.portal_catalog(portal_type='Organisation', query=query)]
      self.assertSameSet(expected_result, result,
        '%s:\nExpected: %r\nGot: %r' % (description,
           [x.getId() for x in expected_result],
           [x.getId() for x in result]))
    # completely artificial example, we just need relations
    related_1 = _create(title='foo1', reference='foo', description='bar')
    related_2 = _create(title='foo2', reference='foo'                   )
    related_3 = _create(                               description='bar')
    related_4 = _create()
    object_1  = create('object_1',  [related_1])
    object_2  = create('object_2',  [related_2])
    object_3  = create('object_3',  [related_3])
    object_4  = create('object_4',  [related_4])
    object_12 = create('object_12', [related_1, related_2])
    object_13 = create('object_13', [related_1, related_3])
    object_14 = create('object_14', [related_1, related_4])
    object_23 = create('object_23', [related_2, related_3])
    object_24 = create('object_24', [related_2, related_4])
    object_34 = create('object_34', [related_3, related_4])
    reference_object_list =   [object_1, object_2,                     object_12, object_13, object_14, object_23, object_24           ]
    description_object_list = [object_1,           object_3,           object_12, object_13, object_14, object_23,            object_34]
    both_object_list =        [object_1,                               object_12, object_13, object_14, object_23                      ]
    title_object_list =       [                                        object_12                                                       ]
    self.tic()
    # Single join
    check(reference_object_list,
          'site_reference="foo"',
          Query(site_reference='foo'))
    check(description_object_list,
          'site_description="bar"',
          Query(site_description='bar'))
    # Double join on different relations
    check(both_object_list,
          'site_reference="foo" AND function_description="bar"',
          ComplexQuery(Query(site_reference='foo'),
                       Query(function_description='bar'),
3528
                       logical_operator='AND'))
3529 3530 3531 3532 3533
    # Double join on same relation
    check(both_object_list,
          'site_reference="foo" AND site_description="bar"',
          ComplexQuery(Query(site_reference='foo'),
                       Query(site_description='bar'),
3534
                       logical_operator='AND'))
3535 3536 3537 3538 3539
    # Double join on same related key
    check(title_object_list,
          'site_title="foo1" AND site_title="foo2"',
          ComplexQuery(Query(site_title='=foo1'),
                       Query(site_title='=foo2'),
3540
                       logical_operator='AND'))
3541

Jérome Perrin's avatar
Jérome Perrin committed
3542
  def test_SearchFolderWithRelatedDynamicRelatedKey(self):
3543
    # Create some objects
Jérome Perrin's avatar
Jérome Perrin committed
3544
    portal = self.portal
3545 3546 3547 3548 3549
    portal_category = self.getCategoryTool()
    portal_category.group.manage_delObjects([x for x in
        portal_category.group.objectIds()])
    group_nexedi_category = portal_category.group\
                                .newContent( id = 'nexedi', title='Nexedi',
3550
                                             reference='a')
3551 3552
    group_nexedi_category2 = portal_category.group\
                                .newContent( id = 'storever', title='Storever',
3553
                                             reference='b')
3554 3555 3556
    module = portal.getDefaultModule('Organisation')
    organisation = module.newContent(portal_type='Organisation',
                                     title='Nexedi Orga',
3557
                                     reference='c')
3558
    organisation.setGroup('nexedi')
3559
    self.assertEqual(organisation.getGroupValue(), group_nexedi_category)
3560 3561
    organisation2 = module.newContent(portal_type='Organisation',
                                      title='Storever Orga',
3562
                                      reference='d')
3563 3564
    organisation2.setGroup('storever')
    organisation2.setTitle('Organisation 2')
3565
    self.assertEqual(organisation2.getGroupValue(), group_nexedi_category2)
3566 3567 3568 3569 3570 3571
    # Flush message queue
    self.tic()

    base_category = portal_category.group
    # Try to get the category with the group related organisation title Nexedi
    # Orga
3572
    category_list = [x.getObject() for x in
3573 3574
                         base_category.searchFolder(
                           group_related_title='Nexedi Orga')]
3575
    self.assertEqual(category_list, [group_nexedi_category])
3576
    category_list = [x.getObject() for x in
3577 3578
                         base_category.searchFolder(
                           default_group_related_title='Nexedi Orga')]
3579
    self.assertEqual(category_list, [group_nexedi_category])
3580
    # Try to get the category with the group related organisation id
3581
    category_list = [x.getObject() for x in
3582
                         base_category.searchFolder(group_related_id='storever')]
3583
    self.assertEqual(category_list,[group_nexedi_category2])
3584
    # Try to get the category with the group related organisation reference 'd'
3585
    category_list = [x.getObject() for x in
3586
                         base_category.searchFolder(group_related_reference='d')]
3587
    self.assertEqual(category_list,[group_nexedi_category2])
3588
    # Try to get the category with the group related organisation reference
3589
    # 'e'
3590
    category_list = [x.getObject() for x in
3591
                         base_category.searchFolder(group_related_reference='e')]
3592
    self.assertEqual(category_list,[])
3593
    # Try to get the category with the default group related organisation reference
3594
    # 'e'
3595
    category_list = [x.getObject() for x in
3596
                         base_category.searchFolder(default_group_related_reference='e')]
3597
    self.assertEqual(category_list,[])
3598 3599
    # Try to get the category with the group related organisation relative_url
    organisation_relative_url = organisation.getRelativeUrl()
3600
    category_list = [x.getObject() for x in
3601
                 base_category.searchFolder(group_related_relative_url=organisation_relative_url)]
3602
    self.assertEqual(category_list, [group_nexedi_category])
3603
    # Try to get the category with the group related organisation uid
3604
    category_list = [x.getObject() for x in
3605
                 base_category.searchFolder(group_related_uid=organisation.getUid())]
3606
    self.assertEqual(category_list, [group_nexedi_category])
3607 3608
    # Try to get the category with the group related organisation id and title
    # of the category
3609
    category_list = [x.getObject() for x in
3610 3611
                         base_category.searchFolder(group_related_id=organisation2.getId(),
                                             title='Storever')]
3612
    self.assertEqual(category_list,[group_nexedi_category2])
3613

Jérome Perrin's avatar
Jérome Perrin committed
3614
  def test_SearchFolderWithRelatedDynamicStrictRelatedKey(self):
3615
    # Create some objects
Jérome Perrin's avatar
Jérome Perrin committed
3616
    portal = self.portal
3617 3618 3619 3620 3621
    portal_category = self.getCategoryTool()
    portal_category.group.manage_delObjects([x for x in
        portal_category.group.objectIds()])
    group_nexedi_category = portal_category.group\
                                .newContent( id = 'nexedi', title='Nexedi',
3622
                                             reference='a')
3623 3624
    sub_group_nexedi = group_nexedi_category\
                                .newContent( id = 'erp5', title='ERP5',
3625
                                             reference='b')
3626 3627 3628
    module = portal.getDefaultModule('Organisation')
    organisation = module.newContent(portal_type='Organisation',
                                     title='ERP5 Orga',
3629
                                     reference='c')
3630
    organisation.setGroup('nexedi/erp5')
3631
    self.assertEqual(organisation.getGroupValue(), sub_group_nexedi)
3632 3633
    organisation2 = module.newContent(portal_type='Organisation',
                                     title='Nexedi Orga',
3634
                                     reference='d')
3635 3636 3637 3638 3639 3640 3641 3642
    organisation2.setGroup('nexedi')
    # Flush message queue
    self.tic()

    base_category = portal_category.group

    # Try to get the category with the group related organisation title Nexedi
    # Orga
3643
    category_list = [x.getObject() for x in
3644 3645
                         base_category.portal_catalog(
                             strict_group_related_title='Nexedi Orga')]
3646
    self.assertEqual(category_list,[group_nexedi_category])
3647 3648
    # Try to get the category with the group related organisation title ERP5
    # Orga
3649
    category_list = [x.getObject() for x in
3650 3651
                         base_category.portal_catalog(
                           strict_group_related_title='ERP5 Orga')]
3652
    self.assertEqual(category_list,[sub_group_nexedi])
3653
    # Try to get the category with the group related organisation reference d
3654
    category_list = [x.getObject() for x in
3655
                         base_category.portal_catalog(
3656
                           strict_group_related_reference='d')]
3657
    self.assertEqual(category_list,[group_nexedi_category])
3658
    # Try to get the category with the group related organisation reference c
3659
    category_list = [x.getObject() for x in
3660
                         base_category.portal_catalog(
3661
                           strict_group_related_reference='c')]
3662
    self.assertEqual(category_list,[sub_group_nexedi])
3663

Jérome Perrin's avatar
Jérome Perrin committed
3664
  def test_EscapingLoginInSescurityQuery(self):
3665 3666 3667 3668 3669 3670 3671 3672 3673
    # Create some objects
    reference = "aaa.o'connor@fake.ie"
    portal = self.getPortal()
    uf = self.portal.acl_users
    uf._doAddUser(reference, 'secret', ['Member'], [])
    user = uf.getUserById(reference).__of__(uf)
    newSecurityManager(None, user)
    portal.view()

Jérome Perrin's avatar
Jérome Perrin committed
3674
  def test_IndexationContextIndependence(self):
3675 3676
    def doCatalog(catalog, document):
      catalog.catalogObjectList([document], check_uid=0)
3677
      result = catalog(select_list=['reference'], uid=document.getUid())
3678 3679 3680
      self.assertEqual(len(result), 1)
      return result[0].reference

3681
    # Create some dummy documents
3682 3683 3684
    portal = self.getPortalObject()
    portal.foo = FooDocument()
    portal.bar = BarDocument()
3685

3686 3687 3688 3689
    # Get instances, wrapping them in acquisition context implicitely.
    foo = portal.foo
    bar = portal.bar

3690
    # Consistency checks
3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717
    self.assertTrue(getattr(foo, 'getReference', None) is not None)
    self.assertTrue(getattr(bar, 'getReference', None) is None)

    # Clean indexing
    portal_catalog = portal.portal_catalog
    self.assertEqual(doCatalog(portal_catalog, foo), 'foo')
    self.assertEqual(doCatalog(portal_catalog, bar), None)

    # Index an object wrapped in a "poisoned" acquisition chain
    bar_on_foo = portal.foo.bar
    self.assertTrue(getattr(bar_on_foo, 'getReference', None) is not None)
    self.assertEqual(bar_on_foo.getReference(), 'foo')
    self.assertEqual(doCatalog(portal_catalog, bar_on_foo), None)

    # Index an object with catalog wrapped in a "poisoned" acquisition chain
    portal_catalog_on_foo = portal.foo.portal_catalog
    self.assertTrue(getattr(portal_catalog_on_foo, 'getReference', None) is not None)
    self.assertEqual(portal_catalog_on_foo.getReference(), 'foo')
    self.assertEqual(doCatalog(portal_catalog_on_foo, foo), 'foo')
    self.assertEqual(doCatalog(portal_catalog_on_foo, bar), None)

    # Poison everything
    self.assertEqual(doCatalog(portal_catalog_on_foo, bar_on_foo), None)

    delattr(portal, 'foo')
    delattr(portal, 'bar')

Jérome Perrin's avatar
Jérome Perrin committed
3718
  def test_distinct_select_expression(self):
3719 3720
    person = self.portal.person_module.newContent(portal_type='Person')
    self.tic()
3721 3722
    portal_catalog = self.getCatalogTool()
    res = portal_catalog.searchResults(
3723 3724 3725 3726
      select_dict={
        'count_reference': 'count(DISTINCT reference)',
      },
      group_by=['reference'],
3727
      portal_type='Person',
3728
    )
3729 3730
    self.assertEqual(1, len(res))
    self.assertEqual(person, res[0].getObject())
3731

Jérome Perrin's avatar
Jérome Perrin committed
3732
  def test_CatalogUidDuplicates(self):
3733 3734 3735 3736 3737 3738
    """
    Initially, the catalog was changing uids when a duplicate was found.

    This operation was really too dangerous, so now we raise errors in this
    case. Here we now check that the error is raised
    """
3739 3740 3741 3742 3743 3744 3745
    # Create an object just to allocate a new valid uid.
    person_module = self.getPersonModule()
    person = person_module.newContent(portal_type='Person')
    self.tic()

    # Make sure that the new object is catalogued.
    portal_catalog = self.getPortalObject().portal_catalog
3746
    self.assertEqual(person, portal_catalog(uid=person.uid)[0].getObject())
3747 3748 3749 3750 3751 3752 3753

    # Delete the new object to free the uid.
    available_uid = person.uid
    person_module.manage_delObjects(uids=[available_uid])
    self.tic()

    # Make sure that the uid is not used any longer.
3754
    self.assertEqual(0, len(portal_catalog(uid=person.uid)))
3755 3756 3757 3758 3759 3760 3761 3762 3763

    # Now, we create two new objects without indexing, so the catalog
    # will not know anything about these objects.
    person1 = person_module.newContent(portal_type='Person', is_indexable=False)
    person2 = person_module.newContent(portal_type='Person', is_indexable=False)

    # Force to assign the same uid, and catalog them.
    person1.uid = person2.uid = available_uid
    person1.is_indexable = person2.is_indexable = True
3764
    self.assertRaises(ValueError, portal_catalog.catalogObjectList,[person1, person2])
3765

Jérome Perrin's avatar
Jérome Perrin committed
3766
  def test_SearchFolderWithParenthesis(self):
3767 3768 3769 3770 3771 3772 3773
    person_module = self.getPersonModule()

    # Make sure that the catalog will not split it with such research :
    # title=foo AND title=bar
    title='foo (bar)'
    person = person_module.newContent(portal_type='Person',title=title)
    person_id = person.getId()
3774
    self.tic()
3775 3776
    folder_object_list = [x.getObject().getId() for x in person_module.searchFolder()]
    self.assertTrue(person_id in folder_object_list)
3777
    folder_object_list = [x.getObject().getId() for x in
3778
                              person_module.searchFolder(title=title)]
3779
    self.assertEqual([person_id],folder_object_list)
3780

Jérome Perrin's avatar
Jérome Perrin committed
3781
  def test_SearchFolderWithMultipleSpaces(self):
3782 3783 3784 3785 3786
    person_module = self.getPersonModule()

    # Make sure that the catalog will not split it with such research :
    # title=foo AND title=bar
    title='foo bar'
3787 3788
    person_module.newContent(portal_type='Person',title=title)
    self.tic()
3789 3790 3791
    title = title.replace(' ', '  ')
    person = person_module.newContent(portal_type='Person',title=title)
    person_id = person.getId()
3792
    self.tic()
3793 3794
    folder_object_list = [x.getObject().getId() for x in person_module.searchFolder()]
    self.assertTrue(person_id in folder_object_list)
3795
    folder_object_list = [x.getObject().getId() for x in
3796
                              person_module.searchFolder(**{'catalog.title':title})]
3797
    self.assertEqual([person_id],folder_object_list)
3798

Jérome Perrin's avatar
Jérome Perrin committed
3799
  def test_SearchFolderWithSingleQuote(self):
3800 3801 3802 3803 3804 3805 3806
    person_module = self.getPersonModule()

    # Make sure that the catalog will not split it with such research :
    # title=foo AND title=bar
    title="foo 'bar"
    person = person_module.newContent(portal_type='Person',title=title)
    person_id = person.getId()
3807
    self.tic()
3808 3809
    folder_object_list = [x.getObject().getId() for x in person_module.searchFolder()]
    self.assertTrue(person_id in folder_object_list)
3810
    folder_object_list = [x.getObject().getId() for x in
3811
                              person_module.searchFolder(title=title)]
3812
    self.assertEqual([person_id],folder_object_list)
3813

Jérome Perrin's avatar
Jérome Perrin committed
3814
  def test_ParameterSelectDict(self):
3815 3816 3817 3818
    person_module = self.getPersonModule()

    # Make sure that we are able to retrieve data directly from mysql
    # without retrieving real objects
Jérome Perrin's avatar
Jérome Perrin committed
3819
    title = "foo"
3820 3821 3822
    description = "foobar"
    person = person_module.newContent(portal_type='Person',title=title,
                                      description=description)
3823
    person_uid = person.getUid()
3824
    self.tic()
3825 3826
    folder_object_list = person_module.searchFolder(uid=person_uid, select_dict={'title': None})
    new_title = 'bar'
3827
    new_description = 'foobarfoo'
3828
    person.setTitle(new_title)
3829
    person.setDescription(new_description)
3830
    self.assertEqual(new_title, person.getTitle())
3831
    expected_sql_title_list = [title]
3832
    self.assertEqual([x.title for x in folder_object_list],
3833
                      expected_sql_title_list)
3834
    self.assertEqual([x.getProperty('title') for x in
3835 3836
                      folder_object_list], expected_sql_title_list)
    expected_sql_description_list = [new_description]
3837
    self.assertEqual([x.getProperty('description') for x in
3838
                      folder_object_list], expected_sql_description_list)
3839
    real_title_list = [new_title]
3840
    self.assertEqual([x.getTitle() for x in
3841
                      folder_object_list], real_title_list)
3842

Jérome Perrin's avatar
Jérome Perrin committed
3843
  def test_countResultsUsesFromExpression(self):
3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859
    person_module = self.getPersonModule()
    module_len = len(person_module)
    if module_len == 0:
      person = person_module.newContent(portal_type='Person')
      module_len = len(person_module)
    module_uid = person_module.getUid()

    self.tic()
    catalog = self.getCatalogTool()

    # Test sanity checks
    self.assertEqual(len(catalog.searchResults(parent_uid=module_uid)),
      module_len)
    self.assertEqual(catalog.countResults(parent_uid=module_uid)[0][0],
      module_len)

3860 3861 3862 3863 3864 3865 3866
    from_expression = {
      'catalog': '(SELECT sub_catalog.* FROM catalog AS sub_catalog'
                 ' WHERE sub_catalog.parent_uid=%i)'
                 ' AS catalog' % (module_uid, ),
    }
    count = catalog.countResults(from_expression=from_expression)[0][0]
    self.assertEqual(count, module_len)
3867

Jérome Perrin's avatar
Jérome Perrin committed
3868
  def test_getParentUid(self):
3869 3870
    from Products.ERP5.Document.Assignment import Assignment
    import erp5.portal_type
3871 3872
    person_module = self.getPersonModule()

3873 3874
    person_id = person_module.generateNewId()
    person = erp5.portal_type.Person(person_id)
3875
    person.setDefaultReindexParameters(activate_kw={'after_tag': self.id()})
3876
    person = person_module[person_module._setObject(person_id, person)]
3877 3878
    self.assertFalse('uid' in person.__dict__)
    person.uid = None
3879 3880 3881

    assignment_id = person.generateNewId()
    assignment = erp5.portal_type.Assignment(assignment_id)
3882
    assignment.setDefaultReindexParameters(activate_kw={'tag': self.id()})
3883
    assignment = person[person._setObject(assignment_id, assignment)]
3884 3885
    self.assertFalse('uid' in assignment.__dict__)
    assignment.uid = None
3886
    self.commit()
3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905

    person_uid_list = []
    catalog_result_list = []
    Assignment_getParentUid = Assignment.getParentUid
    def getParentUid(self):
      person_uid_list.append(person.uid)
      uid = Assignment_getParentUid(self)
      catalog_result_list.append(len(self.portal_catalog(uid=uid)))
      return uid
    Assignment.getParentUid = getParentUid
    try:
      self.tic()
    finally:
      Assignment.getParentUid = Assignment_getParentUid
    self.assertEqual(catalog_result_list[0], 0)
    self.assertEqual(person_uid_list[0], None)
    self.assertTrue(int(person.uid))
    self.assertEqual(person.uid, assignment.getParentUid())

Jérome Perrin's avatar
Jérome Perrin committed
3906
  def test_queriesEndingWithSemicolon(self):
3907 3908
    connector = self.getPortal().erp5_sql_connection
    result = connector.manage_test('select 1 as foo;')
3909
    self.assertEqual(1, result[0].foo)
3910

3911
  def _createSomeGroupCategories(self):
3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923
    portal_category = self.getCategoryTool()
    group_category = portal_category.group
    group_data_map = dict(nexedi=('Nexedi', 'Nexedi Group'),
                          tiolive=('TIOLive', 'TioLive Group'),)
    existing_group_id_list = group_category.objectIds()
    for group_id, (title, description) in group_data_map.items():
      if group_id in existing_group_id_list:
        group = group_category[group_id]
      else:
        group = group_category.newContent(id=group_id)
      group.edit(title=title, description=description)

Jérome Perrin's avatar
Jérome Perrin committed
3924
  def test_SelectDictWithDynamicRelatedKey(self):
3925 3926 3927 3928 3929
    self._createSomeGroupCategories()

    # Create some orgs associated with varying association with the
    # groups created above.
    module = self.portal.getDefaultModule('Organisation')
3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941
    # org1 has no groups
    org1 = module.newContent(portal_type='Organisation', title='org1')
    # org2 has group nexedi
    org2 = module.newContent(portal_type='Organisation', title='org2')
    org2.setGroupList(['nexedi'])
    # org3 has group tiolive
    org3 = module.newContent(portal_type='Organisation', title='org3')
    org3.setGroupList(['tiolive'])
    # org4 has both groups
    org4 = module.newContent(portal_type='Organisation', title='org4')
    org4.setGroupList(['nexedi', 'tiolive'])
    # check associations are correct
3942 3943
    actual_group_title_map = {org.getTitle(): sorted(org.getGroupTitleList())
                              for org in (org1, org2, org3, org4)}
3944 3945 3946 3947
    expected_group_title_map = dict(org1=[],
                                    org2=['Nexedi'],
                                    org3=['TIOLive'],
                                    org4=['Nexedi', 'TIOLive'])
3948
    self.assertEqual(actual_group_title_map, expected_group_title_map)
3949 3950 3951
    # Flush message queue
    self.tic()

3952 3953
    # we will restrict our search to orgs with these ids to be resilient
    # to preexisting orgs:
3954
    org_id_list = sorted(org.getId() for org in (org1, org2, org3, org4))
3955 3956 3957
    # and we'll sort on title to make the output predictable
    search_kw = dict(id=org_id_list,
                     sort_on='title')
3958
    # Try to get the organisations with the group title Nexedi to make sure
3959
    # searching works correctly
3960
    organisation_list = [x.getObject() for x in
3961 3962
                         module.searchFolder(strict_group_title='Nexedi',
                                             **search_kw)]
3963
    self.assertEqual(organisation_list, [org2, org4])
3964 3965 3966
    # Now lets fetch the titles of groups of the above orgs using select_dict.
    search_kw.update(select_dict=dict(strict_group_title=None))
    records = module.searchFolder(**search_kw)
3967 3968 3969 3970 3971
    # By default the catalog returns all items, and the selected
    # strict_group_title is set to None for documents without groups
    # Besides, some entries will appear many times, according to the number of
    # relationships each catalog entry has in that related key.
    results = [(rec.title, rec.strict_group_title)
3972
               for rec in records]
3973
    self.assertEqual(sorted(results),
3974 3975
                      [('org1', None),
                       ('org2', 'Nexedi'),
3976 3977 3978
                       ('org3', 'TIOLive'),
                       ('org4', 'Nexedi'),
                       ('org4', 'TIOLive')])
3979 3980
    # This also works if we force a left join on the column.
    # They'll still be repeated according to their relationships, though.
3981 3982
    search_kw.update(left_join_list=('strict_group_title',))
    records = module.searchFolder(**search_kw)
3983
    results = [(rec.title, rec.strict_group_title)
3984
               for rec in records]
3985
    self.assertEqual(sorted(results),
3986
                      [('org1', None),
3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000
                       ('org2', 'Nexedi'),
                       ('org3', 'TIOLive'),
                       ('org4', 'Nexedi'),
                       ('org4', 'TIOLive')])
    # To get only one of each org, we need to group by one of the
    # catalog keys.

    # Note that this relies on a non-standard behaviour
    # of MySQL: If a selected column is not present in the GROUP BY
    # clause, only the first ocurrence is taken.  Other databases,
    # like Oracle, assume that selected columns are either GROUPed BY
    # or are inside an aggregation function (COUNT, SUM, GROUP_CONCAT,
    # ...), and consider the query to be in error otherwise.
    search_kw.update(group_by_list=('uid',))
4001
    organisation_list = [x.getObject() for x in
4002
                         module.searchFolder(**search_kw)]
4003
    self.assertEqual(organisation_list, [org1, org2, org3, org4])
4004

Jérome Perrin's avatar
Jérome Perrin committed
4005 4006
  def test_BackwardCompatibilityWithOldMethods(self):
    'Dealing with RelatedKey methods missing the proper separator'
4007 4008 4009
    module = self.getOrganisationModule()
    org_a = self._makeOrganisation(title='abc',default_address_city='abc')
    org_a.setReference(org_a.getId())
4010 4011 4012 4013 4014
    # sometimes the module itself is not indexed yet...
    module.reindexObject()

    # Flush message queue
    self.tic()
4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029

    # make a query to fetch the address of the organisation above by
    # querying, among other things, the grand_parent
    query = dict(grand_parent_portal_type="Organisation Module",
                 parent_reference=org_a.getReference())
    catalog = self.getCatalogTool()
    # check the query works normally
    self.assertEqual([x.getObject() for x in catalog.searchResults(**query)],
                     [org_a.default_address])

    # even if we do a left_join
    query_lj = query.copy()
    query_lj.update(left_join_list=('grand_parent_portal_type',))
    self.assertEqual([x.getObject() for x in catalog.searchResults(**query_lj)],
                     [org_a.default_address])
4030

4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052
    # now turn the z_related_grand_parent into an old-style method, without
    # RELATED_QUERY_SEPARATOR
    method = catalog.getSQLCatalog().z_related_grand_parent
    old_src = method.src

    @self._addCleanup
    def cleanGrandParentMethod(self):
      method.manage_edit(method.title, method.connection_id,
                         method.arguments_src, old_src)

    src = old_src.replace('<dtml-var RELATED_QUERY_SEPARATOR>', ' AND ')
    method.manage_edit(method.title, method.connection_id, method.arguments_src,
                       src)

    # check that it still works
    self.assertEqual([x.getObject() for x in catalog.searchResults(**query)],
                     [org_a.default_address])

    # now try to do a left-join on grand_parent_portal_type which
    # shouldn't work
    self.assertRaises(RuntimeError, lambda: catalog.searchResults(**query_lj))

4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065
    # Neither should it work if a left-join is attempted in a column
    # that has proper related-key rendering, but is present in the
    # same query as a column that hasn't, as the whole query is
    # converted into implicit inner joins.
    self.tic()
    query_lj.update(left_join_list=('strict_group_title',),
                    select_dict=('strict_group_title',))
    self.assertRaises(RuntimeError, lambda: catalog.searchResults(**query_lj))
    # though it should work on queries that don't use the broken related-key
    del query_lj['grand_parent_portal_type']
    self.assertEqual([x.getObject() for x in catalog.searchResults(**query_lj)],
                     [org_a.default_address])

4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110
  def testSearchAndActivateWithGroupMethodId(self):
    """
    Make sure searchAndActivate method could be used with a grouping method,
    and in particular make sure sure searchAndActivate can calls himself
    properly.

    We create 300 organisations and use a group method cost of 0.5.
    So this means searchAndActivate should first create 200 activities
    that will be grouped by 2. Then searchAndActivate will call himself
    another time to activate the last 100 organisations, and in their turn
    they will be grouped by 2.
    """
    group_method_call_list = []
    def doSomething(self, message_list):
      r = []
      for m in message_list:
        m.result = r.append(m.object.getPath())
      r.sort()
      group_method_call_list.append(r)
    self.portal.portal_activities.__class__.doSomething = doSomething
    now = DateTime()
    try:
        organisation_list = []
        for x in xrange(0,300):
          organisation_list.append(
            self.portal.organisation_module.newContent().getPath())
        self.tic()
        self.portal.portal_catalog.searchAndActivate(
             creation_date={'query': now, 'range': 'min'},
             method_id="dummyDoSomething",
             group_kw = {"group_method_id" : "portal_activities/doSomething",
                         "group_method_cost": 0.5},
        )
        self.tic()
        self.assertEqual(150, len(group_method_call_list))
        organisation_call_list = []
        for call_path_list in group_method_call_list:
          self.assertEqual(2, len(call_path_list))
          organisation_call_list.extend(call_path_list)
        organisation_call_list.sort()
        organisation_list.sort()
        self.assertEqual(organisation_call_list, organisation_list)
    finally:
      del self.portal.portal_activities.__class__.doSomething

Jérome Perrin's avatar
Jérome Perrin committed
4111 4112 4113 4114
def test_suite():
  suite = unittest.TestSuite()
  suite.addTest(unittest.makeSuite(TestERP5Catalog))
  return suite