testDms.py 16.1 KB
Newer Older
Bartek Górny's avatar
Bartek Górny committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
##############################################################################
#
# 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.
#
##############################################################################

29 30 31
"""
  A test suite for Document Management System functionality.
  This will test:
Bartek Górny's avatar
Bartek Górny committed
32
  - creating Text Document objects
33 34 35 36 37 38 39 40 41 42 43 44 45
  - setting properties of a document, assigning local roles
  - setting relations between documents (explicit and implicity)
  - searching in basic and advanced modes
  - document publication workflow settings
  - sourcing external content
  - (...)
  This will NOT test:
  - contributing files of various types
  - convertion between many formats
  - metadata extraction and editing
  - email ingestion
  These are subject to another suite "testIngestion".
"""
Bartek Górny's avatar
Bartek Górny committed
46

47 48 49
# XXX test_02 works only with oood on
# XXX test_03 and test_04 work only WITHOUT oood (because of a known bug in erp5_dms)

Bartek Górny's avatar
Bartek Górny committed
50 51 52 53 54 55

#
# Skeleton ZopeTestCase
#

from random import randint
56
import time
Bartek Górny's avatar
Bartek Górny committed
57 58 59 60 61 62 63 64 65

from Testing import ZopeTestCase
from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5TypeTestCase
from AccessControl.SecurityManagement import newSecurityManager, noSecurityManager
from DateTime import DateTime
from Acquisition import aq_base, aq_inner
from zLOG import LOG
import os
from Products.ERP5Type import product_path
66
from Products.ERP5OOo.Document.OOoDocument import ConversionError
Bartek Górny's avatar
Bartek Górny committed
67

Bartek Górny's avatar
Bartek Górny committed
68 69 70 71 72 73 74 75 76 77
import os, sys
if __name__ == '__main__':
    execfile(os.path.join(sys.path[0], 'framework.py'))


# Needed in order to have a log file inside the current folder
os.environ['EVENT_LOG_FILE'] = os.path.join(os.getcwd(), 'zLOG.log')
os.environ['EVENT_LOG_SEVERITY'] = '-300'

QUIET = 0
78
RUN_ALL_TEST = 1
Bartek Górny's avatar
Bartek Górny committed
79 80 81 82

# Define the conversion server host
conversion_server_host = ('127.0.0.1', 8008)

83
TEST_FILES_HOME = os.path.join(os.path.dirname(__file__), 'test_document')
84 85 86
FILE_NAME_REGULAR_EXPRESSION = "(?P<reference>[A-Z]{3,6})-(?P<language>[a-z]{2})-(?P<version>[0-9]{3})"
REFERENCE_REGULAR_EXPRESSION = "(?P<reference>[A-Z]{3,6})(-(?P<language>[a-z]{2}))?(-(?P<version>[0-9]{3}))?"

Bartek Górny's avatar
Bartek Górny committed
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117

def printAndLog(msg):
  """
  A utility function to print a message
  to the standard output and to the LOG
  at the same time
  """
  if not QUIET:
    msg = str(msg)
    ZopeTestCase._print('\n ' + msg)
    LOG('Testing... ', 0, msg)


class FileUploadTest(file):

  __allow_access_to_unprotected_subobjects__=1

  def __init__(self, path, name):
    self.filename = name
    file.__init__(self, path)
    self.headers = {}


def makeFilePath(name):
  return os.getenv('INSTANCE_HOME') + '/../Products/ERP5OOo/tests/test_document/' + name


def makeFileUpload(name):
  path = makeFilePath(name)
  return FileUploadTest(path, name)

118
class DummyLocalizer:
Bartek Górny's avatar
Bartek Górny committed
119

Bartek Górny's avatar
Bartek Górny committed
120
  """
121
    A replacement for stock cookie - based localizer
Bartek Górny's avatar
Bartek Górny committed
122 123
  """

124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
  lang = 'en'

  def get_selected_language(self):
    return self.lang

  def changeLanguage(self, lang):
    self.lang = lang

  def translate(self, dictionnary, word):
    return word

class TestDocument(ERP5TypeTestCase):
  """
    Test basic document - related operations
  """
Bartek Górny's avatar
Bartek Górny committed
139 140 141 142

  def getTitle(self):
    return "DMS"

143 144
  ## setup

145
  def afterSetUp(self, quiet=QUIET, run=0):
146
    self.portal = self.getPortal()
147
    self.setSystemPreference()
148 149
    # set a dummy localizer (because normally it is cookie based)
    self.portal.Localizer = DummyLocalizer()
Bartek Górny's avatar
Bartek Górny committed
150

151 152 153 154 155 156 157 158
  def setSystemPreference(self):
    default_pref = self.portal.portal_preferences.default_site_preference
    default_pref.setPreferredOoodocServerAddress(conversion_server_host[0])
    default_pref.setPreferredOoodocServerPortNumber(conversion_server_host[1])
    default_pref.setPreferredDocumentFileNameRegularExpression(FILE_NAME_REGULAR_EXPRESSION)
    default_pref.setPreferredDocumentReferenceRegularExpression(REFERENCE_REGULAR_EXPRESSION)
    default_pref.enable()

Bartek Górny's avatar
Bartek Górny committed
159 160 161 162
  def getDocumentModule(self):
    return getattr(self.getPortal(),'document_module')

  def getBusinessTemplateList(self):
163
    return ('erp5_base', 'erp5_web', 'erp5_dms_mysql_innodb_catalog', 'erp5_dms')
Bartek Górny's avatar
Bartek Górny committed
164 165

  def getNeededCategoryList(self):
166
    return ()
Bartek Górny's avatar
Bartek Górny committed
167

168
  def beforeTearDown(self):
169 170 171 172 173
    """
      Do some stuff after each test:
      - clear document module
    """
    self.clearDocumentModule()
174

175
  def clearDocumentModule(self):
Bartek Górny's avatar
Bartek Górny committed
176
    """
177
      Remove everything after each run
Bartek Górny's avatar
Bartek Górny committed
178
    """
179
    printAndLog("clearing document module...")
180 181
    get_transaction().abort()
    self.tic()
182 183 184 185 186 187 188
    doc_module = self.getDocumentModule()
    ids = [i for i in doc_module.objectIds()]
    doc_module.manage_delObjects(ids)
    get_transaction().commit()
    self.tic()

  ## helper methods
Bartek Górny's avatar
Bartek Górny committed
189

190
  def createTestDocument(self, file_name=None, portal_type='Text', reference='TEST', version='002', language='en'):
Bartek Górny's avatar
Bartek Górny committed
191 192 193
    """
      Creates a text document
    """
194
    dm=self.getPortal().document_module
195
    doctext=dm.newContent(portal_type=portal_type)
Bartek Górny's avatar
Bartek Górny committed
196
    if file_name is not None:
197
      f = open(makeFilePath(file_name), 'rb')
Bartek Górny's avatar
Bartek Górny committed
198 199 200 201 202
      doctext.setTextContent(f.read())
      f.close()
    doctext.setReference(reference)
    doctext.setVersion(version)
    doctext.setLanguage(language)
203
    return doctext
Bartek Górny's avatar
Bartek Górny committed
204

Bartek Górny's avatar
Bartek Górny committed
205 206 207 208 209 210 211 212
  def getDocument(self, id):
    """
      Returns a document with given ID in the
      document module.
    """
    document_module = self.portal.document_module
    return getattr(document_module, id)

213 214 215
  def clearCache(self):
    self.portal.portal_caches.clearAllCache()

Bartek Górny's avatar
Bartek Górny committed
216 217 218 219 220
  ## steps
  
  def stepTic(self, sequence=None, sequence_list=None, **kw):
    self.tic()

221 222
  ## tests

Bartek Górny's avatar
Bartek Górny committed
223 224 225 226
  def test_01_HasEverything(self, quiet=QUIET, run=RUN_ALL_TEST):
    """
      Standard test to make sure we have everything we need - all the tools etc
    """
227
    if not run: return
Bartek Górny's avatar
Bartek Górny committed
228
    printAndLog('\nTest Has Everything ')
229 230 231 232 233 234 235
    self.failUnless(self.getCategoryTool()!=None)
    self.failUnless(self.getSimulationTool()!=None)
    self.failUnless(self.getTypeTool()!=None)
    self.failUnless(self.getSQLConnection()!=None)
    self.failUnless(self.getCatalogTool()!=None)
    self.failUnless(self.getWorkflowTool()!=None)

236
  def test_02_RevisionSystem(self,quiet=QUIET,run=RUN_ALL_TEST):
Bartek Górny's avatar
Bartek Górny committed
237 238 239 240 241 242
    """
      Test revision mechanism
    """
    if not run: return
    printAndLog('\nTest Revision System')
    # create a test document
243
    # revision should be 0
Bartek Górny's avatar
Bartek Górny committed
244
    # upload file (can be the same) into it
245
    # revision should now be 1
Bartek Górny's avatar
Bartek Górny committed
246
    # edit the document with any value or no values
247
    # revision should now be 2
Bartek Górny's avatar
Bartek Górny committed
248
    # contribute the same file through portal_contributions
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
    # the same document should now have revision 3 (because it should have done mergeRevision)
    # getRevisionList should return (0, 1, 2, 3)
    filename = 'TEST-en-002.doc'
    file = makeFileUpload(filename)
    document = self.portal.portal_contributions.newContent(file=file)
    document.immediateReindexObject()
    get_transaction().commit()
    self.tic()
    document_url = document.getRelativeUrl()
    def getTestDocument():
      return self.portal.restrictedTraverse(document_url)
    self.failUnless(getTestDocument().getRevision() == '0')
    getTestDocument().edit(file=file)
    get_transaction().commit()
    self.tic()
    self.failUnless(getTestDocument().getRevision() == '1')
    getTestDocument().edit(title='Hey Joe')
    get_transaction().commit()
    self.tic()
    self.failUnless(getTestDocument().getRevision() == '2')
    another_document = self.portal.portal_contributions.newContent(file=file)
    get_transaction().commit()
    self.tic()
    self.failUnless(getTestDocument().getRevision() == '3')
    self.failUnless(getTestDocument().getRevisionList() == ['0', '1', '2'] )
Bartek Górny's avatar
Bartek Górny committed
274 275 276 277 278 279 280

  def test_03_Versioning(self,quiet=QUIET,run=RUN_ALL_TEST):
    """
      Test versioning
    """
    if not run: return
    printAndLog('\nTest Versioning System')
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
    # create a document 1, set coordinates (reference=TEST, version=002, language=en)
    # create a document 2, set coordinates (reference=TEST, version=002, language=en)
    # create a document 3, set coordinates (reference=TEST, version=004, language=en)
    # run isVersionUnique on 1, 2, 3 (should return False, False, True)
    # change version of 2 to 003
    # run isVersionUnique on 1, 2, 3  (should return True)
    # run getLatestVersionValue on all (should return 3)
    # run getVersionValueList on 2 (should return [3, 2, 1])
    document_module = self.getDocumentModule()
    docs = {}
    docs[1] = self.createTestDocument(reference='TEST', version='002', language='en')
    docs[2] = self.createTestDocument(reference='TEST', version='002', language='en')
    docs[3] = self.createTestDocument(reference='TEST', version='004', language='en')
    docs[4] = self.createTestDocument(reference='ANOTHER', version='002', language='en')
    get_transaction().commit()
    self.tic()
    self.failIf(docs[1].isVersionUnique())
    self.failIf(docs[2].isVersionUnique())
    self.failUnless(docs[3].isVersionUnique())
    docs[2].setVersion('003')
    get_transaction().commit()
    self.tic()
    self.failUnless(docs[1].isVersionUnique())
    self.failUnless(docs[2].isVersionUnique())
    self.failUnless(docs[3].isVersionUnique())
    self.failUnless(docs[1].getLatestVersionValue() == docs[3])
    self.failUnless(docs[2].getLatestVersionValue() == docs[3])
    self.failUnless(docs[3].getLatestVersionValue() == docs[3])
    version_list = [br.getRelativeUrl() for br in docs[2].getVersionValueList()]
    self.failUnless(version_list == [docs[3].getRelativeUrl(), docs[2].getRelativeUrl(), docs[1].getRelativeUrl()])
Bartek Górny's avatar
Bartek Górny committed
311 312 313 314 315 316 317 318 319 320 321 322 323

  def test_04_VersioningWithLanguage(self,quiet=QUIET,run=RUN_ALL_TEST):
    """
      Test versioning with multi-language support
    """
    if not run: return
    printAndLog('\nTest Versioning With Language')
    # create empty test documents, set their coordinates as follows:
    # (1) TEST, 002, en
    # (2) TEST, 002, fr
    # (3) TEST, 002, pl
    # (4) TEST, 003, en
    # (5) TEST, 003, sp
324
    # the following calls (on any doc) should produce the following output:
Bartek Górny's avatar
Bartek Górny committed
325 326 327 328 329 330 331
    # getOriginalLanguage() = 'en'
    # getLanguageList = ('en', 'fr', 'pl', 'sp')
    # getLatestVersionValue() = 4
    # getLatestVersionValue('en') = 4
    # getLatestVersionValue('fr') = 2
    # getLatestVersionValue('pl') = 3
    # getLatestVersionValue('ru') = None
332
    # change user language into 'sp'
Bartek Górny's avatar
Bartek Górny committed
333
    # getLatestVersionValue() = 5
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
    # add documents:
    # (6) TEST, 004, pl
    # (7) TEST, 004, en
    # getLatestVersionValue() = 7
    localizer = self.portal.Localizer
    document_module = self.getDocumentModule()
    docs = {}
    docs[1] = self.createTestDocument(reference='TEST', version='002', language='en')
    time.sleep(1) # time span here because catalog records only full seconds
    docs[2] = self.createTestDocument(reference='TEST', version='002', language='fr')
    time.sleep(1)
    docs[3] = self.createTestDocument(reference='TEST', version='002', language='pl')
    time.sleep(1)
    docs[4] = self.createTestDocument(reference='TEST', version='003', language='en')
    time.sleep(1)
    docs[5] = self.createTestDocument(reference='TEST', version='003', language='sp')
    time.sleep(1)
    get_transaction().commit()
    self.tic()
    doc = docs[2] # can be any
    self.failUnless(doc.getOriginalLanguage() == 'en')
    self.failUnless(doc.getLanguageList() == ['en', 'fr', 'pl', 'sp'])
    self.failUnless(doc.getLatestVersionValue() == docs[4]) # there are two latest - it chooses the one in user language
    self.failUnless(doc.getLatestVersionValue('en') == docs[4])
    self.failUnless(doc.getLatestVersionValue('fr') == docs[2])
    self.failUnless(doc.getLatestVersionValue('pl') == docs[3])
    self.failUnless(doc.getLatestVersionValue('ru') == None)
    localizer.changeLanguage('sp') # change user language
    self.failUnless(doc.getLatestVersionValue() == docs[5]) # there are two latest - it chooses the one in user language
    docs[6] = document_module.newContent(reference='TEST', version='004', language='pl')
    docs[7] = document_module.newContent(reference='TEST', version='004', language='en')
    get_transaction().commit()
    self.tic()
    self.failUnless(doc.getLatestVersionValue() == docs[7]) # there are two latest, neither in user language - it chooses the one in original language
Bartek Górny's avatar
Bartek Górny committed
368

369
  def no_test_05_UniqueReference(self,quiet=QUIET,run=RUN_ALL_TEST):
Bartek Górny's avatar
Bartek Górny committed
370 371 372 373 374 375 376 377 378 379 380
    """
      Test automatic setting of unique reference
    """
    if not run: return
    printAndLog('\nTest Automatic Setting Unique Reference')
    # create three empty test documents
    # run setUniqueReference on the second
    # reference of the second doc should now be TEST-auto-2
    # run setUniqueReference('uniq') on the third
    # reference of the third doc should now be TEST-uniq-1

381
  def no_test_06_testExplicitRelations(self,quiet=QUIET,run=RUN_ALL_TEST):
Bartek Górny's avatar
Bartek Górny committed
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
    """
      Test explicit relations.
      Explicit relations are just like any other relation, so no need to test them here
      except for similarity cloud which we test.
    """
    if not run: return
    printAndLog('\nTest Explicit Relations')
    # create test documents:
    # (1) TEST, 002, en
    # (2) TEST, 003, en
    # (3) ONE, 001, en
    # (4) TWO, 001, en
    # (5) THREE, 001, en
    # set 3 similar to 1, 4 to 3, 5 to 4
    # getSimilarCloudValueList on 4 should return 2, 3 and 5
    # getSimilarCloudValueList(depth=1) on 4 should return 3 and 5

399
  def no_test_07_testImplicitRelations(self,quiet=QUIET,run=RUN_ALL_TEST):
Bartek Górny's avatar
Bartek Górny committed
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
    """
      Test implicit (wiki-like) relations.
    """
    # XXX this test should be extended to check more elaborate language selection
    if not run: return
    printAndLog('\nTest Implicit Relations')
    # create docs to be referenced:
    # (1) TEST, 002, en
    # (2) TEST, 002, fr
    # (3) TEST, 003, en
    # create docs to contain references in text_content:
    # REF, 001, en; "I use reference to look up TEST"
    # REF, 002, en; "I use reference to look up TEST"
    # REFLANG, 001, en: "I use reference and language to look up TEST-fr"
    # REFVER, 001, en: "I use reference and version to look up TEST-002"
    # REFVERLANG, 001, en: "I use reference, version and language to look up TEST-002-en"
    printAndLog('\nTesting Implicit Predecessors')
    # the implicit predecessors should be:
    # for (1): REF-002, REFVER, REFVERLANG
    # for (2): REF-002, REFLANG, REFVER
    # for (3): REF-002
    printAndLog('\nTesting Implicit Successors')
    # the implicit successors should be:
    # for REF: (3)
    # for REFLANG: (2)
    # for REFVER: (3)
    # for REFVERLANG: (3)
Bartek Górny's avatar
Bartek Górny committed
427

428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
  def testOOoDocument_get_size(self):
    # test get_size on OOoDocument
    doc = self.portal.document_module.newContent(portal_type='Spreadsheet')
    doc.edit(file=makeFileUpload('import_data_list.ods'))
    self.assertEquals(len(makeFileUpload('import_data_list.ods').read()),
                      doc.get_size())

  def testTempOOoDocument_get_size(self):
    # test get_size on temporary OOoDocument
    from Products.ERP5Type.Document import newTempOOoDocument
    doc = newTempOOoDocument(self.portal, 'tmp')
    doc.edit(base_data='OOo')
    self.assertEquals(len('OOo'), doc.get_size())


Bartek Górny's avatar
Bartek Górny committed
443 444 445 446 447 448
if __name__ == '__main__':
    framework()
else:
    import unittest
    def test_suite():
        suite = unittest.TestSuite()
449
        suite.addTest(unittest.makeSuite(TestDocument))
Bartek Górny's avatar
Bartek Górny committed
450 451 452 453
        return suite


# vim: syntax=python shiftwidth=2