Commit 3a446bc6 authored by Jérome Perrin's avatar Jérome Perrin Committed by Arnaud Fontaine

tests: makeFileUpload: Use unittest addCleanup() to close files.

This executes even if setup() encounters an error and the cleanup hook is added
when opening the file, not later on.

Also, unify makeFileUpload() and makeFilePath() to remove duplicated code.
Co-authored-by: Arnaud Fontaine's avatarArnaud Fontaine <arnaud.fontaine@nexedi.com>
parent f4fa30e4
Pipeline #36336 failed with stage
in 0 seconds
......@@ -31,8 +31,6 @@
import os
from DateTime import DateTime
from Products.ERP5Type.tests.Sequence import SequenceList
from Products.ERP5Type.tests.runUnitTest import tests_home
from Products.ERP5Type.tests.utils import FileUpload
from erp5.component.module.ConfiguratorTestMixin import \
TestLiveConfiguratorWorkflowMixin
......@@ -1978,26 +1976,21 @@ class TestConsultingConfiguratorWorkflow(StandardConfigurationMixin):
stepCheckInstanceIsConfigured%(country)s
"""
def uploadFile(self, file_id):
file_obj = getattr(self.portal, file_id)
file_path = tests_home + '/%s' % file_id
temp_file = open(file_path, 'w+b')
try:
temp_file.write(str(file_obj))
finally:
temp_file.close()
def makeFileUpload(self, filename, *_, **__):
from Products.ERP5Type.tests.runUnitTest import tests_home
return (file_path, FileUpload(file_path, file_id))
with open(os.path.join(tests_home, filename), 'w+b') as temp_file:
temp_file.write(bytes(getattr(self.portal, filename)))
return super(TestConsultingConfiguratorWorkflow,
self).makeFileUpload(filename, path=tests_home)
def afterSetUp(self):
TestLiveConfiguratorWorkflowMixin.afterSetUp(self)
categories_file_id = 'consulting_configurator_sample_categories.ods'
self.categories_file_path, self.categories_file_upload = \
self.uploadFile(categories_file_id)
roles_file_id = 'standard_portal_types_roles.ods'
self.roles_file_path, self.roles_file_upload = \
self.uploadFile(roles_file_id)
self.categories_file_upload = self.makeFileUpload(
'consulting_configurator_sample_categories.ods')
self.roles_file_upload = self.makeFileUpload(
'standard_portal_types_roles.ods')
# set the company employees number
self.company_employees_number = '3'
......@@ -2065,10 +2058,6 @@ class TestConsultingConfiguratorWorkflow(StandardConfigurationMixin):
# login as manager
self.login()
def beforeTearDown(self):
os.remove(self.categories_file_path)
os.remove(self.roles_file_path)
def stepCheckConfigureCategoriesForm(self, sequence=None, sequence_list=None, **kw):
""" Check if Confire Categories step was showed """
response_dict = sequence.get("response_dict")
......
......@@ -34,7 +34,7 @@ from DateTime import DateTime
from Products.ERP5Type.Utils import convertToUpperCase
from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5TypeTestCase
from Products.ERP5Type.tests.Sequence import SequenceList
from Products.ERP5Type.tests.utils import createZODBPythonScript, FileUpload
from Products.ERP5Type.tests.utils import createZODBPythonScript
from AccessControl.SecurityManagement import newSecurityManager
class TestERP5Base(ERP5TypeTestCase):
......@@ -2076,11 +2076,11 @@ class Base_getDialogSectionCategoryItemListTest(ERP5TypeTestCase):
class TestImage(ERP5TypeTestCase):
"""Tests for images support.
"""
def makeImageFileUpload(self, filename):
def _getTestDataPath(self):
import Products.ERP5.tests
return FileUpload(
os.path.join(os.path.dirname(Products.ERP5.tests.__file__),
'test_data', 'images', filename))
return os.path.join(os.path.dirname(Products.ERP5.tests.__file__),
'test_data',
'images')
def test_CreateImage(self):
# We can add Images inside Persons and Organisation
......@@ -2092,7 +2092,7 @@ class TestImage(ERP5TypeTestCase):
def test_ConvertImage(self):
image = self.portal.newContent(portal_type='Image', id='test_image')
image.edit(file=self.makeImageFileUpload('erp5_logo.png'))
image.edit(file=self.makeFileUpload('erp5_logo.png'))
self.assertEqual('image/png', image.getContentType())
self.assertEqual((320, 250), (image.getWidth(), image.getHeight()))
......@@ -2109,7 +2109,7 @@ class TestImage(ERP5TypeTestCase):
def test_ConvertImagePdata(self):
image = self.portal.newContent(portal_type='Image', id='test_image')
image.edit(file=self.makeImageFileUpload('erp5_logo.bmp'))
image.edit(file=self.makeFileUpload('erp5_logo.bmp'))
from OFS.Image import Pdata
self.assertTrue(isinstance(image.data, Pdata))
......@@ -2132,7 +2132,7 @@ class TestImage(ERP5TypeTestCase):
('../broken_html.html', (-1, -1)),
):
image = self.portal.newContent(portal_type='Image', id=self.id())
image.edit(file=self.makeImageFileUpload(filename))
image.edit(file=self.makeFileUpload(filename))
self.assertEqual(
(image.getWidth(), image.getHeight()),
size,
......@@ -2152,7 +2152,7 @@ class TestImage(ERP5TypeTestCase):
('../broken_html.html', 'application/unknown'),
):
image = self.portal.newContent(portal_type='Image', id=self.id())
image.edit(data=self.makeImageFileUpload(filename).read())
image.edit(data=self.makeFileUpload(filename).read())
self.assertEqual(
image.getContentType(),
content_type,
......@@ -2172,7 +2172,7 @@ class TestImage(ERP5TypeTestCase):
('empty.png', 'application/unknown'),
):
image = self.portal.newContent(portal_type='Image', id=self.id())
image.edit(file=self.makeImageFileUpload(filename))
image.edit(file=self.makeFileUpload(filename))
self.assertEqual(
image.getContentType(),
content_type,
......
......@@ -33,7 +33,6 @@ import textwrap
from unittest import expectedFailure
from Products.CMFCore.WorkflowCore import WorkflowException
from Products.ERP5Type.tests.utils import FileUpload
from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5TypeTestCase
from Products.ERP5OOo.tests.testIngestion import FILENAME_REGULAR_EXPRESSION
from Products.ERP5OOo.tests.testIngestion import REFERENCE_REGULAR_EXPRESSION
......@@ -46,14 +45,6 @@ from DateTime import DateTime
import Products.ERP5.tests
def makeFilePath(name):
return os.path.join(os.path.dirname(Products.ERP5.tests.__file__),
'test_data', 'crm_emails', name)
def makeFileUpload(name):
path = makeFilePath(name)
return FileUpload(path, name)
clear_module_name_list = """
campaign_module
event_module
......@@ -80,6 +71,11 @@ class BaseTestCRM(ERP5TypeTestCase):
self.tic()
super(BaseTestCRM, self).beforeTearDown()
def _getTestDataPath(self):
return os.path.join(os.path.dirname(Products.ERP5.tests.__file__),
'test_data',
'crm_emails')
class TestCRM(BaseTestCRM):
def getTitle(self):
return "CRM"
......@@ -709,14 +705,10 @@ class TestCRMMailIngestion(BaseTestCRM):
# make sure customers are available to catalog
self.tic()
def _readTestData(self, filename):
"""read test data from data directory."""
return open(makeFilePath(filename)).read()
def _ingestMail(self, filename=None, data=None):
"""ingest an email from the mail in data dir named `filename`"""
if data is None:
data=self._readTestData(filename)
data = self.makeFileUpload(filename).read()
return self.portal.portal_contributions.newContent(
container_path='event_module',
filename='postfix_mail.eml',
......@@ -889,7 +881,9 @@ class TestCRMMailIngestion(BaseTestCRM):
return object_list[-1]
portal = self.portal
message = message_from_string(self._readTestData('simple'))
message_string = self.makeFileUpload('simple').read()
message = message_from_string(message_string)
message.replace_header('subject', 'Visit:Company A')
data = message.as_string()
self._ingestMail(data=data)
......@@ -897,7 +891,7 @@ class TestCRMMailIngestion(BaseTestCRM):
document = getLastCreatedEvent(portal.event_module)
self.assertEqual(document.getPortalType(), 'Visit')
message = message_from_string(self._readTestData('simple'))
message = message_from_string(message_string)
message.replace_header('subject', 'Fax:Company B')
data = message.as_string()
self._ingestMail(data=data)
......@@ -905,7 +899,7 @@ class TestCRMMailIngestion(BaseTestCRM):
document = getLastCreatedEvent(portal.event_module)
self.assertEqual(document.getPortalType(), 'Fax Message')
message = message_from_string(self._readTestData('simple'))
message = message_from_string(message_string)
message.replace_header('subject', 'TEST:Company B')
data = message.as_string()
self._ingestMail(data=data)
......@@ -913,7 +907,7 @@ class TestCRMMailIngestion(BaseTestCRM):
document = getLastCreatedEvent(portal.event_module)
self.assertEqual(document.getPortalType(), 'Mail Message')
message = message_from_string(self._readTestData('simple'))
message = message_from_string(message_string)
message.replace_header('subject', 'visit:Company A')
data = message.as_string()
self._ingestMail(data=data)
......@@ -921,7 +915,7 @@ class TestCRMMailIngestion(BaseTestCRM):
document = getLastCreatedEvent(portal.event_module)
self.assertEqual(document.getPortalType(), 'Visit')
message = message_from_string(self._readTestData('simple'))
message = message_from_string(message_string)
message.replace_header('subject', 'phone:Company B')
data = message.as_string()
self._ingestMail(data=data)
......@@ -929,7 +923,7 @@ class TestCRMMailIngestion(BaseTestCRM):
document = portal.event_module[portal.event_module.objectIds()[-1]]
self.assertEqual(document.getPortalType(), 'Phone Call')
message = message_from_string(self._readTestData('simple'))
message = message_from_string(message_string)
message.replace_header('subject', 'LETTER:Company C')
data = message.as_string()
self._ingestMail(data=data)
......@@ -937,7 +931,7 @@ class TestCRMMailIngestion(BaseTestCRM):
document = getLastCreatedEvent(portal.event_module)
self.assertEqual(document.getPortalType(), 'Letter')
message = message_from_string(self._readTestData('simple'))
message = message_from_string(message_string)
body = message.get_payload()
message.set_payload('Visit:%s' % body)
data = message.as_string()
......@@ -946,7 +940,7 @@ class TestCRMMailIngestion(BaseTestCRM):
document = getLastCreatedEvent(portal.event_module)
self.assertEqual(document.getPortalType(), 'Visit')
message = message_from_string(self._readTestData('simple'))
message = message_from_string(message_string)
body = message.get_payload()
message.set_payload('PHONE CALL:%s' % body)
data = message.as_string()
......@@ -1281,7 +1275,7 @@ class TestCRMMailSend(BaseTestCRM):
# Add a document which will be attached.
# pdf
filename = 'sample_attachment.pdf'
file_object = makeFileUpload(filename)
file_object = self.makeFileUpload(filename)
document = self.portal.portal_contributions.newContent(file=file_object)
self.tic()
......@@ -1328,7 +1322,7 @@ class TestCRMMailSend(BaseTestCRM):
"""
# Add a document which will be attached.
filename = 'sample_attachment.odt'
file_object = makeFileUpload(filename)
file_object = self.makeFileUpload(filename)
document = self.portal.portal_contributions.newContent(file=file_object)
self.tic()
......@@ -1375,7 +1369,7 @@ class TestCRMMailSend(BaseTestCRM):
"""
# Add a document which will be attached.
filename = 'sample_attachment.zip'
file_object = makeFileUpload(filename)
file_object = self.makeFileUpload(filename)
document = self.portal.portal_contributions.newContent(file=file_object)
self.tic()
......@@ -1420,7 +1414,7 @@ class TestCRMMailSend(BaseTestCRM):
"""
# Add a document which will be attached.
filename = 'sample_attachment.gif'
file_object = makeFileUpload(filename)
file_object = self.makeFileUpload(filename)
document = self.portal.portal_contributions.newContent(file=file_object)
self.tic()
......@@ -1517,7 +1511,7 @@ class TestCRMMailSend(BaseTestCRM):
# Add a document which will be attached.
# pdf
filename = 'sample_attachment.pdf'
file_object = makeFileUpload(filename)
file_object = self.makeFileUpload(filename)
# Add a ticket
ticket = self.portal.campaign_module.newContent(portal_type='Campaign',
......@@ -1565,7 +1559,7 @@ class TestCRMMailSend(BaseTestCRM):
"""
# Add a document which will be attached.
filename = 'sample_attachment.zip'
file_object = makeFileUpload(filename)
file_object = self.makeFileUpload(filename)
# Add a ticket
ticket = self.portal.campaign_module.newContent(portal_type='Campaign',
......@@ -1614,7 +1608,7 @@ class TestCRMMailSend(BaseTestCRM):
"""
# Add a document which will be attached.
filename = 'sample_attachment.zip'
file_object = makeFileUpload(filename)
file_object = self.makeFileUpload(filename)
# Add a ticket
ticket = self.portal.campaign_module.newContent(portal_type='Campaign',
......@@ -1691,7 +1685,7 @@ class TestCRMMailSend(BaseTestCRM):
# Add a document on a person which will be attached.
def add_document(filename, container, portal_type):
f = makeFileUpload(filename)
f = self.makeFileUpload(filename)
document = container.newContent(portal_type=portal_type)
document.edit(file=f, reference=filename)
return document
......@@ -1748,7 +1742,7 @@ class TestCRMMailSend(BaseTestCRM):
# Add a document on a person which will be attached.
def add_document(filename, container, portal_type):
f = makeFileUpload(filename)
f = self.makeFileUpload(filename)
document = container.newContent(portal_type=portal_type)
document.edit(file=f, reference=filename)
return document
......
......@@ -31,11 +31,10 @@
import unittest
from AccessControl.SecurityManagement import newSecurityManager
from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5TypeTestCase
from erp5.component.test.testDms import makeFileUpload
from erp5.component.test.testDms import DocumentUploadTestCase
class TestERP5Discussion(ERP5TypeTestCase):
class TestERP5Discussion(DocumentUploadTestCase):
"""Test for erp5_discussion business template.
"""
def getTitle(self):
......@@ -128,7 +127,7 @@ class TestERP5Discussion(ERP5TypeTestCase):
self.assertEqual(0, len(attachment_list))
# check attachment creation
file_ = makeFileUpload('TEST-en-002.doc')
file_ = self.makeFileUpload('TEST-en-002.doc')
web_section1.WebSection_createNewDiscussionThread('test1-new-with-attachment', 'test1 body', file=file_)
discussion_thread, = [x for x in self.portal.discussion_thread_module.objectValues() \
if x.getId() not in discussion_thread_id_set]
......@@ -225,7 +224,7 @@ class TestERP5Discussion(ERP5TypeTestCase):
web_site_value = self.portal.web_site_module.newContent(portal_type='Web Site')
web_section_value = web_site_value.newContent(portal_type='Web Section')
file_ = makeFileUpload('simple.csv')
file_ = self.makeFileUpload('simple.csv')
web_section_value.WebSection_createNewDiscussionThread(
"Thread Title",
"Post Content",
......
......@@ -53,7 +53,6 @@ from subprocess import Popen, PIPE
from unittest import expectedFailure
from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5TypeTestCase
from Products.ERP5Type.tests.utils import FileUpload
from Products.ERP5Type.tests.utils import DummyLocalizer
from Products.ERP5Type.Utils import bytes2str, str2bytes
from Products.ERP5OOo.OOoUtils import OOoBuilder
......@@ -77,28 +76,15 @@ from OFS.Image import Pdata
QUIET = 0
from Products.ERP5OOo import tests
TEST_FILES_HOME = os.path.join(tests.__path__[0], 'test_document')
FILENAME_REGULAR_EXPRESSION = "(?P<reference>[A-Z]{3,10})-(?P<language>[a-z]{2})-(?P<version>[0-9]{3})"
REFERENCE_REGULAR_EXPRESSION = "(?P<reference>[A-Z]{3,10})(-(?P<language>[a-z]{2}))?(-(?P<version>[0-9]{3}))?"
def makeFilePath(name):
return os.path.join(TEST_FILES_HOME, name)
class DocumentUploadTestCase(ERP5TypeTestCase):
def _getTestDataPath(self):
from Products.ERP5OOo import tests
return os.path.join(os.path.join(tests.__path__[0], 'test_document'))
def makeFileUpload(name, as_name=None):
if as_name is None:
as_name = name
path = makeFilePath(name)
return FileUpload(path, as_name)
def getFileSize(name):
path = makeFilePath(name)
f = open(path, "r")
file_size = len(f.read())
f.close()
return file_size
class TestDocumentMixin(ERP5TypeTestCase):
class TestDocumentMixin(DocumentUploadTestCase):
business_template_list = ['erp5_core_proxy_field_legacy',
'erp5_jquery',
......@@ -202,9 +188,7 @@ class TestDocument(TestDocumentMixin):
dm=self.getPortal().document_module
doctext=dm.newContent(portal_type=portal_type)
if filename is not None:
f = open(makeFilePath(filename), 'rb')
doctext.setTextContent(f.read())
f.close()
doctext.setTextContent(self.makeFileUpload(filename).read())
doctext.setReference(reference)
doctext.setVersion(version)
doctext.setLanguage(language)
......@@ -277,7 +261,7 @@ class TestDocument(TestDocumentMixin):
# the same document should now have revision 4 (because it should have done mergeRevision)
# getRevisionList should return (1, 2, 3, 4)
filename = 'TEST-en-002.doc'
file_ = makeFileUpload(filename)
file_ = self.makeFileUpload(filename)
document = self.portal.portal_contributions.newContent(file=file_)
self.tic()
document_url = document.getRelativeUrl()
......@@ -489,38 +473,38 @@ class TestDocument(TestDocumentMixin):
# create docs to be referenced:
# (1) TEST, 002, en
filename = 'TEST-en-002.odt'
file_ = makeFileUpload(filename)
file_ = self.makeFileUpload(filename)
document1 = self.portal.portal_contributions.newContent(file=file_)
# (2) TEST, 002, fr
as_name = 'TEST-fr-002.odt'
file_ = makeFileUpload(filename, as_name)
file_ = self.makeFileUpload(filename, as_name)
document2 = self.portal.portal_contributions.newContent(file=file_)
# (3) TEST, 003, en
as_name = 'TEST-en-003.odt'
file_ = makeFileUpload(filename, as_name)
file_ = self.makeFileUpload(filename, as_name)
document3 = self.portal.portal_contributions.newContent(file=file_)
# create docs to contain references in text_content:
# REF, 002, en; "I use reference to look up TEST"
filename = 'REF-en-002.odt'
file_ = makeFileUpload(filename)
file_ = self.makeFileUpload(filename)
document5 = self.portal.portal_contributions.newContent(file=file_)
# REFLANG, 001, en: "I use reference and language to look up TEST-fr"
filename = 'REFLANG-en-001.odt'
file_ = makeFileUpload(filename)
file_ = self.makeFileUpload(filename)
document6 = self.portal.portal_contributions.newContent(file=file_)
# REFVER, 001, en: "I use reference and version to look up TEST-002"
filename = 'REFVER-en-001.odt'
file_ = makeFileUpload(filename)
file_ = self.makeFileUpload(filename)
document7 = self.portal.portal_contributions.newContent(file=file_)
# REFVERLANG, 001, en: "I use reference, version and language to look up TEST-002-en"
filename = 'REFVERLANG-en-001.odt'
file_ = makeFileUpload(filename)
file_ = self.makeFileUpload(filename)
document8 = self.portal.portal_contributions.newContent(file=file_)
self.tic()
......@@ -576,7 +560,7 @@ class TestDocument(TestDocumentMixin):
def test_catalog_search_by_size(self):
doc = self.portal.document_module.newContent(
portal_type='Spreadsheet',
file=makeFileUpload('import_data_list.ods'))
file=self.makeFileUpload('import_data_list.ods'))
self.tic()
self.assertEqual(
[x.getObject() for x in self.portal.portal_catalog(size=doc.getSize())], [doc])
......@@ -584,8 +568,8 @@ class TestDocument(TestDocumentMixin):
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.assertEqual(len(makeFileUpload('import_data_list.ods').read()),
doc.edit(file=self.makeFileUpload('import_data_list.ods'))
self.assertEqual(len(self.makeFileUpload('import_data_list.ods').read()),
doc.get_size())
def testTempOOoDocument_get_size(self):
......@@ -598,14 +582,14 @@ class TestDocument(TestDocumentMixin):
# test hasData on OOoDocument
doc = self.portal.document_module.newContent(portal_type='Spreadsheet')
self.assertFalse(doc.hasData())
doc.edit(file=makeFileUpload('import_data_list.ods'))
doc.edit(file=self.makeFileUpload('import_data_list.ods'))
self.assertTrue(doc.hasData())
def testTempOOoDocument_hasData(self):
# test hasData on TempOOoDocument
doc = self.portal.newContent(temp_object=True, portal_type='OOo Document', id='tmp')
self.assertFalse(doc.hasData())
doc.edit(file=makeFileUpload('import_data_list.ods'))
doc.edit(file=self.makeFileUpload('import_data_list.ods'))
self.assertTrue(doc.hasData())
def test_Owner_Base_download(self):
......@@ -615,7 +599,7 @@ class TestDocument(TestDocumentMixin):
doc = self.portal.document_module.newContent(
filename='test.ods',
portal_type='Spreadsheet')
doc.edit(file=makeFileUpload('TEST-en-002.doc'))
doc.edit(file=self.makeFileUpload('TEST-en-002.doc'))
self.tic()
uf = self.portal.acl_users
......@@ -625,7 +609,7 @@ class TestDocument(TestDocumentMixin):
response = self.publish('%s/Base_download' % doc.getPath(),
basic='member_user1:secret')
self.assertEqual(makeFileUpload('TEST-en-002.doc').read(),
self.assertEqual(self.makeFileUpload('TEST-en-002.doc').read(),
response.getBody())
self.assertEqual('application/msword',
response.headers['content-type'])
......@@ -654,7 +638,7 @@ class TestDocument(TestDocumentMixin):
doc = self.portal.document_module.newContent(
filename='test.ods',
portal_type='Spreadsheet')
doc.edit(file=makeFileUpload('import.file.with.dot.in.filename.ods'))
doc.edit(file=self.makeFileUpload('import.file.with.dot.in.filename.ods'))
doc.publish()
self.tic()
......@@ -732,13 +716,13 @@ class TestDocument(TestDocumentMixin):
def test_csv(self):
doc = self.portal.document_module.newContent(
portal_type='Spreadsheet',
file=makeFileUpload('simple.csv'),
file=self.makeFileUpload('simple.csv'),
)
self.assertEqual(doc.getContentType(), 'text/csv')
doc.publish()
self.tic()
response = self.publish('%s?format=' % doc.getPath())
self.assertEqual(response.getBody(), makeFileUpload('simple.csv').read())
self.assertEqual(response.getBody(), self.makeFileUpload('simple.csv').read())
self.assertEqual(response.getHeader('Content-Type'), 'text/csv; charset=utf-8')
self.assertEqual(response.getHeader('Content-Disposition'), 'attachment; filename="simple.csv"')
......@@ -760,7 +744,7 @@ class TestDocument(TestDocumentMixin):
is not draft
"""
filename = 'TEST-en-002.doc'
file_ = makeFileUpload(filename)
file_ = self.makeFileUpload(filename)
document = self.portal.portal_contributions.newContent(file=file_)
self.assertEqual('converting', document.getExternalProcessingState())
......@@ -825,7 +809,7 @@ class TestDocument(TestDocumentMixin):
document.
"""
filename = 'EmbeddedImage-en-002.odt'
file_ = makeFileUpload(filename)
file_ = self.makeFileUpload(filename)
document = self.portal.portal_contributions.newContent(file=file_)
self.tic()
......@@ -1309,7 +1293,7 @@ class TestDocument(TestDocumentMixin):
re_html_nbsp = re.compile('&(nbsp|#160);')
def test_PDFTextContent(self):
upload_file = makeFileUpload('REF-en-001.pdf')
upload_file = self.makeFileUpload('REF-en-001.pdf')
document = self.portal.portal_contributions.newContent(file=upload_file)
self.assertEqual('PDF', document.getPortalType())
self.assertEqual('I use reference to look up TEST\n',
......@@ -1320,7 +1304,7 @@ class TestDocument(TestDocumentMixin):
document.SearchableText())
def test_PDFToHTML(self):
upload_file = makeFileUpload('REF-en-001.pdf')
upload_file = self.makeFileUpload('REF-en-001.pdf')
document = self.portal.portal_contributions.newContent(file=upload_file)
self.assertEqual('PDF', document.getPortalType())
......@@ -1331,7 +1315,7 @@ class TestDocument(TestDocumentMixin):
self.tic()
def test_PDFToPng(self):
upload_file = makeFileUpload('REF-en-001.pdf')
upload_file = self.makeFileUpload('REF-en-001.pdf')
document = self.portal.portal_contributions.newContent(file=upload_file)
self.assertEqual('PDF', document.getPortalType())
......@@ -1343,7 +1327,7 @@ class TestDocument(TestDocumentMixin):
self.assertEqual(image_data[1:4], b'PNG')
def test_PDFToJpg(self):
upload_file = makeFileUpload('REF-en-001.pdf')
upload_file = self.makeFileUpload('REF-en-001.pdf')
document = self.portal.portal_contributions.newContent(file=upload_file)
self.assertEqual('PDF', document.getPortalType())
......@@ -1354,7 +1338,7 @@ class TestDocument(TestDocumentMixin):
self.assertEqual(image_data[6:10], b'JFIF')
def test_PDFToGif(self):
upload_file = makeFileUpload('REF-en-001.pdf')
upload_file = self.makeFileUpload('REF-en-001.pdf')
document = self.portal.portal_contributions.newContent(file=upload_file)
self.assertEqual('PDF', document.getPortalType())
......@@ -1365,7 +1349,7 @@ class TestDocument(TestDocumentMixin):
self.assertEqual(image_data[0:4], b'GIF8')
def test_PDFToTiff(self):
upload_file = makeFileUpload('REF-en-001.pdf')
upload_file = self.makeFileUpload('REF-en-001.pdf')
document = self.portal.portal_contributions.newContent(file=upload_file)
self.assertEqual('PDF', document.getPortalType())
......@@ -1377,7 +1361,7 @@ class TestDocument(TestDocumentMixin):
def test_PDF_content_information(self):
upload_file = makeFileUpload('REF-en-001.pdf')
upload_file = self.makeFileUpload('REF-en-001.pdf')
document = self.portal.portal_contributions.newContent(file=upload_file)
self.assertEqual('PDF', document.getPortalType())
content_information = document.getContentInformation()
......@@ -1389,7 +1373,7 @@ class TestDocument(TestDocumentMixin):
def test_PDF_content_information_extra_metadata(self):
# Extra metadata, such as those stored by pdftk update_info are also
# available in document.getContentInformation()
upload_file = makeFileUpload('metadata.pdf', as_name='REF-en-001.pdf')
upload_file = self.makeFileUpload('metadata.pdf', as_filename='REF-en-001.pdf')
document = self.portal.portal_contributions.newContent(file=upload_file)
self.tic()
self.assertEqual('PDF', document.getPortalType())
......@@ -1400,7 +1384,7 @@ class TestDocument(TestDocumentMixin):
# contribute file which will be merged to current document in synchronous mode
# and check content_type recalculated
upload_file = makeFileUpload('Forty-Two.Pages-en-001.pdf', as_name='REF-en-001.pdf')
upload_file = self.makeFileUpload('Forty-Two.Pages-en-001.pdf', as_filename='REF-en-001.pdf')
contributed_document = self.portal.Base_contribute(file=upload_file, \
synchronous_metadata_discovery=True)
self.tic()
......@@ -1414,7 +1398,7 @@ class TestDocument(TestDocumentMixin):
document.getContentInformation()['Pages'])
# upload with another file and check content_type recalculated
upload_file = makeFileUpload('REF-en-001.pdf')
upload_file = self.makeFileUpload('REF-en-001.pdf')
document.setFile(upload_file)
self.tic()
content_information = document.getContentInformation()
......@@ -1432,7 +1416,7 @@ class TestDocument(TestDocumentMixin):
# IndirectObject instance which is not picklable
document = self.portal.document_module.newContent(
portal_type='PDF',
file=makeFileUpload('apple_metadata.pdf'))
file=self.makeFileUpload('apple_metadata.pdf'))
# content_information is picklable
content_information = document.getContentInformation()
from pickle import dumps
......@@ -1445,7 +1429,7 @@ class TestDocument(TestDocumentMixin):
""" Test that pypdf2 handle wrong formatted PDF """
pdf = self.portal.document_module.newContent(
portal_type='PDF',
file=makeFileUpload('FEUILLE BLANCHE.pdf'),
file=self.makeFileUpload('FEUILLE BLANCHE.pdf'),
title='Bad PDF')
self.tic()
pdf.share()
......@@ -1455,7 +1439,7 @@ class TestDocument(TestDocumentMixin):
self.assertNotEqual(result, None)
def test_PDF_content_content_type(self):
upload_file = makeFileUpload('REF-en-001.pdf')
upload_file = self.makeFileUpload('REF-en-001.pdf')
document = self.portal.document_module.newContent(portal_type='PDF')
# Here we use edit instead of setFile,
# because only edit method set filename as filename.
......@@ -1464,11 +1448,11 @@ class TestDocument(TestDocumentMixin):
def test_PDF_watermark(self):
original_document = self.portal.portal_contributions.newContent(
file=makeFileUpload('REF-en-001.pdf'))
file=self.makeFileUpload('REF-en-001.pdf'))
# This watermark.pdf document is a pdf with a transparent background. Such
# document can be created using GIMP
watermark_document = self.portal.portal_contributions.newContent(
file=makeFileUpload('watermark.pdf'))
file=self.makeFileUpload('watermark.pdf'))
watermarked_data = original_document.getWatermarkedData(
watermark_data=watermark_document.getData(),
repeat_watermark=False)
......@@ -1487,9 +1471,9 @@ class TestDocument(TestDocumentMixin):
def test_PDF_watermark_repeat(self):
# watermark a pdf, repeating the watermark
original_document = self.portal.portal_contributions.newContent(
file=makeFileUpload('Forty-Two.Pages-en-001.pdf'))
file=self.makeFileUpload('Forty-Two.Pages-en-001.pdf'))
watermark_document = self.portal.portal_contributions.newContent(
file=makeFileUpload('watermark.pdf'))
file=self.makeFileUpload('watermark.pdf'))
watermarked_data = original_document.getWatermarkedData(
watermark_data=watermark_document.getData(),
repeat_watermark=True)
......@@ -1505,9 +1489,9 @@ class TestDocument(TestDocumentMixin):
def test_PDF_watermark_start_page(self):
# watermark a pdf, starting on the second page
original_document = self.portal.portal_contributions.newContent(
file=makeFileUpload('Forty-Two.Pages-en-001.pdf'))
file=self.makeFileUpload('Forty-Two.Pages-en-001.pdf'))
watermark_document = self.portal.portal_contributions.newContent(
file=makeFileUpload('watermark.pdf'))
file=self.makeFileUpload('watermark.pdf'))
watermarked_data = original_document.getWatermarkedData(
watermark_data=watermark_document.getData(),
repeat_watermark=False,
......@@ -1522,7 +1506,7 @@ class TestDocument(TestDocumentMixin):
watermarked_document.getData())
def test_checkVisibleTextInPresentationToImageConversion(self):
odp = makeFileUpload("TEST-en-003.odp")
odp = self.makeFileUpload("TEST-en-003.odp")
presentation = self.portal.document_module.newContent(
portal_type="Presentation",
data=odp,
......@@ -1542,7 +1526,7 @@ class TestDocument(TestDocumentMixin):
self.assertIn("ERP5 DMS page 1", txt)
def test_Document_getStandardFilename(self):
upload_file = makeFileUpload('metadata.pdf')
upload_file = self.makeFileUpload('metadata.pdf')
document = self.portal.document_module.newContent(portal_type='PDF')
document.edit(file=upload_file)
self.assertEqual(document.getStandardFilename(), 'metadata.pdf')
......@@ -1554,7 +1538,7 @@ class TestDocument(TestDocumentMixin):
self.assertEqual(document.getStandardFilename(format='png'),
'metadata-001-en.png')
# check when format contains multiple '.'
upload_file = makeFileUpload('TEST-en-003.odp')
upload_file = self.makeFileUpload('TEST-en-003.odp')
document = self.portal.document_module.newContent(portal_type='Presentation')
document.edit(file=upload_file)
self.assertEqual(document.getStandardFilename(), 'TEST-en-003.odp')
......@@ -1562,7 +1546,7 @@ class TestDocument(TestDocumentMixin):
def test_CMYKImageTextContent(self):
upload_file = makeFileUpload('cmyk_sample.jpg')
upload_file = self.makeFileUpload('cmyk_sample.jpg')
document = self.portal.portal_contributions.newContent(file=upload_file)
self.assertEqual('Image', document.getPortalType())
for _ in ('empty_cache', 'cache'):
......@@ -1570,7 +1554,7 @@ class TestDocument(TestDocumentMixin):
self.tic()
def test_MonochromeImageResize(self):
upload_file = makeFileUpload('monochrome_sample.tiff')
upload_file = self.makeFileUpload('monochrome_sample.tiff')
document = self.portal.portal_contributions.newContent(file=upload_file)
self.assertEqual('Image', document.getPortalType())
resized_image = document.convert(format='png', display='small')[1]
......@@ -1585,7 +1569,7 @@ class TestDocument(TestDocumentMixin):
document = self.portal.document_module.newContent(portal_type='Drawing')
self.assertEqual('empty', document.getExternalProcessingState())
upload_file = makeFileUpload('TEST-en-002.odt')
upload_file = self.makeFileUpload('TEST-en-002.odt')
document.edit(file=upload_file)
self.tic()
self.assertEqual('converted', document.getExternalProcessingState())
......@@ -1600,7 +1584,7 @@ class TestDocument(TestDocumentMixin):
document.Base_showFoundText())
# upload again good content
upload_file = makeFileUpload('TEST-en-002.odt')
upload_file = self.makeFileUpload('TEST-en-002.odt')
document.edit(file=upload_file)
self.tic()
self.assertEqual('converted', document.getExternalProcessingState())
......@@ -1644,7 +1628,7 @@ class TestDocument(TestDocumentMixin):
.newContent(portal_type=image_portal_type)
# edit content and publish it
upload_file = makeFileUpload('cmyk_sample.jpg')
upload_file = self.makeFileUpload('cmyk_sample.jpg')
image.edit(reference=image_reference,
version='001',
language='en',
......@@ -1680,7 +1664,7 @@ class TestDocument(TestDocumentMixin):
# Let's continue with Presentation Document as embbeded image
document = self.portal.document_module.newContent(portal_type='Presentation')
upload_file = makeFileUpload('TEST-en-003.odp')
upload_file = self.makeFileUpload('TEST-en-003.odp')
image_reference = 'IMAGE-odp'
document.edit(file=upload_file, reference=image_reference)
document.publish()
......@@ -1918,7 +1902,7 @@ document.write('<sc'+'ript type="text/javascript" src="http://somosite.bg/utb.ph
self.assertNotIn('#FFAA44', safe_html)
filename = 'broken_html.html'
file_object = makeFileUpload(filename)
file_object = self.makeFileUpload(filename)
web_page.edit(file=file_object)
assert web_page.convert('html')[1]
......@@ -1973,7 +1957,7 @@ document.write('<sc'+'ript type="text/javascript" src="http://somosite.bg/utb.ph
document_module = self.portal.getDefaultModule(portal_type)
document = document_module.newContent(portal_type=portal_type)
upload_file = makeFileUpload('Forty-Two.Pages-en-001.pdf')
upload_file = self.makeFileUpload('Forty-Two.Pages-en-001.pdf')
document.edit(file=upload_file)
pages_number = int(document.getContentInformation()['Pages'])
self.tic()
......@@ -2076,7 +2060,7 @@ document.write('<sc'+'ript type="text/javascript" src="http://somosite.bg/utb.ph
"""
web_page_portal_type = 'Web Page'
module = self.portal.getDefaultModule(web_page_portal_type)
upload_file = makeFileUpload('TEST-text-iso8859-1.txt')
upload_file = self.makeFileUpload('TEST-text-iso8859-1.txt')
web_page = module.newContent(portal_type=web_page_portal_type,
file=upload_file)
self.tic()
......@@ -2100,7 +2084,7 @@ document.write('<sc'+'ript type="text/javascript" src="http://somosite.bg/utb.ph
def _test_PDFDocument_asTextConversion(self, strict):
document = self.portal.document_module.newContent(
portal_type='PDF',
file=makeFileUpload('TEST.Embedded.Image.pdf'))
file=self.makeFileUpload('TEST.Embedded.Image.pdf'))
for _ in ('empty_cache', 'cache'):
if strict:
self.assertEqual(document.asText(), 'ERP5 is a free software.')
......@@ -2121,7 +2105,7 @@ document.write('<sc'+'ript type="text/javascript" src="http://somosite.bg/utb.ph
self.tic() # no activity failure
def test_password_protected_pdf_asText(self):
pdf_reader = PyPDF2.PdfFileReader(makeFileUpload('TEST.Embedded.Image.pdf'))
pdf_reader = PyPDF2.PdfFileReader(self.makeFileUpload('TEST.Embedded.Image.pdf'))
pdf_writer = PyPDF2.PdfFileWriter()
pdf_writer.addPage(pdf_reader.getPage(0))
pdf_writer.encrypt('secret')
......@@ -2147,7 +2131,7 @@ return 1
def _test_document_conversion_to_base_format_no_original_format_access(self,
portal_type, filename):
module = self.portal.getDefaultModule(portal_type)
upload_file = makeFileUpload(filename)
upload_file = self.makeFileUpload(filename)
document = module.newContent(portal_type=portal_type,
file=upload_file)
......@@ -2204,7 +2188,7 @@ return 1
# Create document with good content
document = self.portal.document_module.newContent(portal_type='Presentation')
upload_file = makeFileUpload('TEST-en-003.odp')
upload_file = self.makeFileUpload('TEST-en-003.odp')
document.edit(file=upload_file)
self.tic()
self.assertEqual('converted', document.getExternalProcessingState())
......@@ -2229,7 +2213,7 @@ return 1
portal_type = 'PDF'
module = self.portal.getDefaultModule(portal_type)
upload_file = makeFileUpload('TEST.Large.Document.pdf')
upload_file = self.makeFileUpload('TEST.Large.Document.pdf')
pdf = module.newContent(portal_type=portal_type, file=upload_file)
self.assertIn('html', pdf.getTargetFormatList())
......@@ -2250,7 +2234,7 @@ return 1
self.assertSameSet([], presentation.getTargetFormatList())
# test uploading some data
upload_file = makeFileUpload('Foo_001.odg')
upload_file = self.makeFileUpload('Foo_001.odg')
presentation.edit(file=upload_file)
self.tic()
self.assertIn('odg', presentation.getTargetFormatList())
......@@ -2278,7 +2262,7 @@ return 1
# images from same instance accessed by reference and wrong conversion arguments (dispay NOT display)
# code should be more resilient
upload_file = makeFileUpload('cmyk_sample.jpg')
upload_file = self.makeFileUpload('cmyk_sample.jpg')
image = self.portal.image_module.newContent(portal_type='Image',
reference='Embedded-XXX',
version='001',
......@@ -2321,15 +2305,15 @@ return 1
"""
# Create OOo document
ooo_document = self.portal.document_module.newContent(portal_type='Presentation')
upload_file = makeFileUpload('TEST-en-003.odp')
upload_file = self.makeFileUpload('TEST-en-003.odp')
ooo_document.edit(file=upload_file)
pdf_document = self.portal.document_module.newContent(portal_type='PDF')
upload_file = makeFileUpload('TEST-en-002.pdf')
upload_file = self.makeFileUpload('TEST-en-002.pdf')
pdf_document.edit(file=upload_file)
image_document = self.portal.image_module.newContent(portal_type='Image')
upload_file = makeFileUpload('TEST-en-002.png')
upload_file = self.makeFileUpload('TEST-en-002.png')
image_document.edit(file=upload_file)
web_page_document = self.portal.web_page_module.newContent(portal_type="Web Page")
......@@ -2417,15 +2401,15 @@ return 1
return urllib.urlopen(url)
ooo_document = self.portal.document_module.newContent(portal_type='Presentation')
upload_file = makeFileUpload('TEST-en-003.odp')
upload_file = self.makeFileUpload('TEST-en-003.odp')
ooo_document.edit(file=upload_file)
pdf_document = self.portal.document_module.newContent(portal_type='PDF')
upload_file = makeFileUpload('TEST-en-002.pdf')
upload_file = self.makeFileUpload('TEST-en-002.pdf')
pdf_document.edit(file=upload_file)
image_document = self.portal.image_module.newContent(portal_type='Image')
upload_file = makeFileUpload('TEST-en-002.png')
upload_file = self.makeFileUpload('TEST-en-002.png')
image_document.edit(file=upload_file)
web_page_document = self.portal.web_page_module.newContent(portal_type="Web Page")
......@@ -2436,12 +2420,14 @@ return 1
response = getURL(image_document.absolute_url(), **{'format':''})
self.assertIn('Content-Type: image/png\r\n', response.info().headers)
self.assertIn('Content-Length: %s\r\n' %getFileSize('TEST-en-002.png'), response.info().headers)
self.assertIn('Content-Length: %s\r\n' % len(self.makeFileUpload('TEST-en-002.png').read()),
response.info().headers)
response = getURL(ooo_document.absolute_url(), **{'format':''})
self.assertIn('Content-Type: application/vnd.oasis.opendocument.presentation\r\n', response.info().headers)
self.assertIn('Content-Disposition: attachment; filename="TEST-en-003.odp"\r\n', response.info().headers)
self.assertIn('Content-Length: %s\r\n' %getFileSize('TEST-en-003.odp'), response.info().headers)
self.assertIn('Content-Length: %s\r\n' % len(self.makeFileUpload('TEST-en-003.odp').read()),
response.info().headers)
response = getURL(pdf_document.absolute_url(), **{'format':''})
self.assertIn('Content-Type: application/pdf\r\n', response.info().headers)
......@@ -2460,7 +2446,7 @@ return 1
"""
portal_type = 'PDF'
module = self.portal.getDefaultModule(portal_type)
upload_file = makeFileUpload('TEST.Large.Document.pdf')
upload_file = self.makeFileUpload('TEST.Large.Document.pdf')
pdf = module.newContent(portal_type=portal_type, file=upload_file)
# if PDF size is larger than A4 format system should deny conversion
......@@ -2469,7 +2455,7 @@ return 1
# raster -> svg image should deny conversion if image width or height > 128 px
portal_type = 'Image'
module = self.portal.getDefaultModule(portal_type)
upload_file = makeFileUpload('TEST-en-002.jpg')
upload_file = self.makeFileUpload('TEST-en-002.jpg')
image = module.newContent(portal_type=portal_type, file=upload_file)
self.assertRaises(Unauthorized, image.convert, format='svg')
......@@ -2479,7 +2465,7 @@ return 1
return a default (i.e. indicating a conversion failures)
"""
doc = self.portal.document_module.newContent(portal_type='Presentation')
upload_file = makeFileUpload('TEST-en-003.odp')
upload_file = self.makeFileUpload('TEST-en-003.odp')
doc.edit(file=upload_file)
doc.publish()
self.tic()
......@@ -2607,7 +2593,7 @@ return 1
Test various cases of contributing to an existing document
"""
# contribute a document, then make it not editable and check we can not contribute to it
upload_file = makeFileUpload('TEST-en-002.doc')
upload_file = self.makeFileUpload('TEST-en-002.doc')
kw = dict(file=upload_file, synchronous_metadata_discovery=True)
document = self.portal.Base_contribute(**kw)
self.tic()
......@@ -2632,12 +2618,12 @@ return 1
"""
# contribute a document, then make it not editable and check we can not contribute to it
kw=dict(synchronous_metadata_discovery=True)
upload_file = makeFileUpload('TEST-en-002.doc')
upload_file = self.makeFileUpload('TEST-en-002.doc')
kw = dict(file=upload_file, synchronous_metadata_discovery=True)
document = self.portal.Base_contribute(**kw)
self.tic()
upload_file = makeFileUpload('TEST-en-003.odp', 'TEST-en-002.doc')
upload_file = self.makeFileUpload('TEST-en-003.odp', 'TEST-en-002.doc')
kw = dict(file=upload_file, synchronous_metadata_discovery=True)
document = self.portal.Base_contribute(**kw)
self.tic()
......@@ -2671,7 +2657,7 @@ return 1
def test_base_convertable_uses_pdata_for_base_data(self):
document = self.portal.document_module.newContent(
portal_type='Spreadsheet',
file=makeFileUpload('import_big_spreadsheet.ods'))
file=self.makeFileUpload('import_big_spreadsheet.ods'))
self.tic()
# for large documents base_data is stored as Pdata
self.assertIsInstance(document.base_data, Pdata)
......@@ -2681,7 +2667,7 @@ return 1
# for small documents, it's bytes directly
document = self.portal.document_module.newContent(
portal_type='Text',
file=makeFileUpload('TEST-en-002.odt'))
file=self.makeFileUpload('TEST-en-002.odt'))
self.tic()
self.assertIsInstance(document.base_data, bytes)
self.assertIsInstance(document.getBaseData(), bytes)
......@@ -2693,7 +2679,7 @@ return 1
When cloning a document base_data must not be computed once again.
"""
# create a document
upload_file = makeFileUpload('TEST-en-002.doc')
upload_file = self.makeFileUpload('TEST-en-002.doc')
kw = dict(file=upload_file, synchronous_metadata_discovery=True)
document = self.portal.Base_contribute(**kw)
self.tic()
......@@ -2711,7 +2697,7 @@ return 1
# Update document with another content by using setData:
# base_data must be recomputed
document.edit(data=makeFileUpload('TEST-en-002.odt').read())
document.edit(data=self.makeFileUpload('TEST-en-002.odt').read())
self.tic()
self.assertTrue(document.hasBaseData())
self.assertNotEqual(previous_base_data, document.getBaseData(),
......@@ -2723,7 +2709,7 @@ return 1
# Update document with another content by using setFile:
# base_data must be recomputed
document.edit(file=makeFileUpload('TEST-en-002.doc'))
document.edit(file=self.makeFileUpload('TEST-en-002.doc'))
self.tic()
self.assertTrue(document.hasBaseData())
self.assertNotEqual(previous_base_data, document.getBaseData(),
......@@ -2744,7 +2730,7 @@ return 1
back to empty state
"""
# create a document
upload_file = makeFileUpload('TEST-en-002.doc')
upload_file = self.makeFileUpload('TEST-en-002.doc')
kw = dict(file=upload_file, synchronous_metadata_discovery=True)
document = self.portal.Base_contribute(**kw)
self.tic()
......@@ -2892,7 +2878,7 @@ return 1
""" Test "visible" instances of a doc are auto archived when a new
instance is made "visible" except when they have a future effective date.
"""
upload_file = makeFileUpload('TEST-en-002.doc')
upload_file = self.makeFileUpload('TEST-en-002.doc')
kw = dict(file=upload_file, synchronous_metadata_discovery=True)
document_002 = self.portal.Base_contribute(**kw)
document_002.publish()
......@@ -2951,7 +2937,7 @@ return 1
self.tic()
def testFileWithNotDefinedMimeType(self):
upload_file = makeFileUpload('TEST-001-en.dummy')
upload_file = self.makeFileUpload('TEST-001-en.dummy')
kw = dict(file=upload_file, synchronous_metadata_discovery=True,
portal_type='File')
document = self.portal.Base_contribute(**kw)
......@@ -2968,7 +2954,7 @@ return 1
Checks Base_getRelatedDocumentList works correctly with both
related (follow_up) Documents and with sub-object Embedded Files
"""
uploaded_file = makeFileUpload('TEST-001-en.dummy')
uploaded_file = self.makeFileUpload('TEST-001-en.dummy')
document_value = self.portal.Base_contribute(
file=uploaded_file,
synchronous_metadata_discovery=True,
......@@ -3021,7 +3007,7 @@ class TestDocumentWithSecurity(TestDocumentMixin):
Make sure that uploader can preview document after submitted.
"""
filename = 'REF-en-001.odt'
upload_file = makeFileUpload(filename)
upload_file = self.makeFileUpload(filename)
document = self.portal.portal_contributions.newContent(file=upload_file)
self.tic()
......@@ -3044,9 +3030,8 @@ class TestDocumentWithSecurity(TestDocumentMixin):
text_document = document_module.newContent(portal_type=portal_type,
reference='Foo_001',
title='Foo_OO1')
f = makeFileUpload('Foo_001.odt')
f = self.makeFileUpload('Foo_001.odt')
text_document.edit(file=f)
f.close()
self.tic()
# the document should be automatically converted to html
......@@ -3115,14 +3100,14 @@ class TestDocumentWithSecurity(TestDocumentMixin):
def test_mergeRevision(self):
document1 = self.portal.portal_contributions.newContent(
file=makeFileUpload('TEST-en-002.doc'))
file=self.makeFileUpload('TEST-en-002.doc'))
self.tic()
self.assertEqual(
(document1.getReference(), document1.getLanguage(), document1.getVersion()),
('TEST', 'en', '002'))
self.assertNotIn('This document is modified', document1.asText())
document2 = self.portal.portal_contributions.newContent(
file=makeFileUpload('TEST-en-002-modified.doc'))
file=self.makeFileUpload('TEST-en-002-modified.doc'))
self.tic()
self.assertIn('This document is modified', document2.asText())
self.assertEqual(
......@@ -3141,7 +3126,7 @@ class TestDocumentWithSecurity(TestDocumentMixin):
with self.assertRaisesRegex(
Unauthorized,
"You are not allowed to update the existing document"):
self.portal.portal_contributions.newContent(file=makeFileUpload('TEST-en-002.doc'))
self.portal.portal_contributions.newContent(file=self.makeFileUpload('TEST-en-002.doc'))
# this also works with another user which can not see the document
another_user_id = self.id()
......@@ -3151,7 +3136,7 @@ class TestDocumentWithSecurity(TestDocumentMixin):
with self.assertRaisesRegex(
Unauthorized,
"You are not allowed to update the existing document"):
self.portal.portal_contributions.newContent(file=makeFileUpload('TEST-en-002.doc'))
self.portal.portal_contributions.newContent(file=self.makeFileUpload('TEST-en-002.doc'))
def test_mergeRevision_with_node_reference_local_reference_filename_regular_expression(self):
# this filename regular expression comes from configurator
......@@ -3160,14 +3145,14 @@ class TestDocumentWithSecurity(TestDocumentMixin):
)
self.tic()
document1 = self.portal.portal_contributions.newContent(
file=makeFileUpload('TEST-en-002.doc', as_name='P-PROJ-TEST-002-en.doc'))
file=self.makeFileUpload('TEST-en-002.doc', as_filename='P-PROJ-TEST-002-en.doc'))
self.tic()
self.assertEqual(
(document1.getReference(), document1.getLanguage(), document1.getVersion()),
('P-PROJ-TEST', 'en', '002'))
self.assertNotIn('This document is modified', document1.asText())
document2 = self.portal.portal_contributions.newContent(
file=makeFileUpload('TEST-en-002-modified.doc', as_name='P-PROJ-TEST-002-en.doc'))
file=self.makeFileUpload('TEST-en-002-modified.doc', as_filename='P-PROJ-TEST-002-en.doc'))
self.tic()
self.assertIn('This document is modified', document2.asText())
self.assertEqual(
......@@ -3187,7 +3172,7 @@ class TestDocumentWithSecurity(TestDocumentMixin):
Unauthorized,
"You are not allowed to update the existing document"):
self.portal.portal_contributions.newContent(
file=makeFileUpload('TEST-en-002.doc', as_name='P-PROJ-TEST-002-en.doc'))
file=self.makeFileUpload('TEST-en-002.doc', as_filename='P-PROJ-TEST-002-en.doc'))
# this also works with another user which can not see the document
another_user_id = self.id()
......@@ -3198,7 +3183,7 @@ class TestDocumentWithSecurity(TestDocumentMixin):
Unauthorized,
"You are not allowed to update the existing document"):
self.portal.portal_contributions.newContent(
file=makeFileUpload('TEST-en-002.doc', as_name='P-PROJ-TEST-002-en.doc'))
file=self.makeFileUpload('TEST-en-002.doc', as_filename='P-PROJ-TEST-002-en.doc'))
class TestDocumentPerformance(TestDocumentMixin):
......@@ -3208,7 +3193,7 @@ class TestDocumentPerformance(TestDocumentMixin):
Test large OOoDocument to image conversion
"""
ooo_document = self.portal.document_module.newContent(portal_type='Spreadsheet')
upload_file = makeFileUpload('import_big_spreadsheet.ods')
upload_file = self.makeFileUpload('import_big_spreadsheet.ods')
ooo_document.edit(file=upload_file)
self.tic()
before = time.time()
......@@ -3227,7 +3212,7 @@ class TestDocumentPerformance(TestDocumentMixin):
req_time)
class DocumentConsistencyTestCase(ERP5TypeTestCase):
class DocumentConsistencyTestCase(DocumentUploadTestCase):
portal_type = NotImplemented
content_type = NotImplemented
filename = NotImplemented
......@@ -3237,10 +3222,8 @@ class DocumentConsistencyTestCase(ERP5TypeTestCase):
def afterSetUp(self):
self.document = self._getDocumentModule().newContent(portal_type=self.portal_type)
self.file_upload = makeFileUpload(self.filename)
with open(makeFilePath(self.filename)) as f:
self.file_data = f.read()
self.file_size = len(self.file_data)
self.file_upload = self.makeFileUpload(self.filename)
self.file_data = self.makeFileUpload(self.filename).read()
def _checkDocument(self):
self.assertEqual(self.document.checkConsistency(), [])
......@@ -3253,7 +3236,7 @@ class DocumentConsistencyTestCase(ERP5TypeTestCase):
[],
)
self.assertEqual(self.document.getData(), self.file_data)
self.assertEqual(self.document.getSize(), self.file_size)
self.assertEqual(self.document.getSize(), len(self.file_data))
self.assertEqual(self.document.getContentType(), self.content_type)
self.assertEqual(self.document.getFilename(), self.filename)
......
......@@ -41,7 +41,7 @@ import transaction
from AccessControl import Unauthorized
from Testing import ZopeTestCase
from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5TypeTestCase
from Products.ERP5Type.tests.utils import FileUpload, createZODBPythonScript
from Products.ERP5Type.tests.utils import createZODBPythonScript
from Products.ERP5Type.Utils import bytes2str, str2bytes
from erp5.component.document.Document import ConversionError
......@@ -67,17 +67,6 @@ Y5pFvE34AgSXzrOPljMwMCgKWeIPJZxG43fR2UfLIRbAbfJjcERTgz1ASE0PcGsIGE1GOsMK0APk
/6e3ek9E9ERmk2rQv49vGHgkcBotISHBQDbglkDTzjjaCKab0QBziJyFukqO6AAAAABJRU5ErkJg
gg==''')
def makeFilePath(name):
from Products.ERP5 import tests
return os.path.join(tests.__path__[0], 'test_data', name)
def makeFileUpload(name, as_name=None):
if as_name is None:
as_name = name
path = makeFilePath(name)
return FileUpload(path, as_name)
def process_image(image, size=(40, 40)):
# open the images to compare, resize them, and convert to grayscale
# get the rgb values of the pixels in the image
......@@ -151,6 +140,10 @@ class TestERP5WebWithDms(ERP5TypeTestCase, ZopeTestCase.Functional):
self.clearModule(self.portal.web_site_module)
self.clearModule(self.portal.web_page_module)
def _getTestDataPath(self):
from Products.ERP5 import tests
return os.path.join(tests.__path__[0], 'test_data')
def setupWebSite(self, **kw):
"""
Setup Web Site
......@@ -472,7 +465,7 @@ class TestERP5WebWithDms(ERP5TypeTestCase, ZopeTestCase.Functional):
self.assertEqual(modification_date, last_modified_header)
# Upload a presentation with 3 pages.
upload_file = makeFileUpload('P-DMS-Presentation.3.Pages-001-en.odp')
upload_file = self.makeFileUpload('P-DMS-Presentation.3.Pages-001-en.odp')
document = document_module.newContent(portal_type='Presentation',
file=upload_file)
reference = 'P-DMS-Presentation.3.Pages'
......@@ -529,7 +522,7 @@ class TestERP5WebWithDms(ERP5TypeTestCase, ZopeTestCase.Functional):
document_reference = 'NXD-Presentation'
document_module = portal.getDefaultModule(portal_type='Presentation')
upload_file = makeFileUpload('P-DMS-Presentation.3.Pages-001-en.odp')
upload_file = self.makeFileUpload('P-DMS-Presentation.3.Pages-001-en.odp')
document = document_module.newContent(portal_type='Presentation',
reference=document_reference,
file=upload_file)
......@@ -537,7 +530,7 @@ class TestERP5WebWithDms(ERP5TypeTestCase, ZopeTestCase.Functional):
image_reference = 'NXD-IMAGE'
image_module = portal.getDefaultModule(portal_type='Image')
upload_file = makeFileUpload('tiolive-ERP5.Freedom.TioLive.Logo-001-en.png')
upload_file = self.makeFileUpload('tiolive-ERP5.Freedom.TioLive.Logo-001-en.png')
image = image_module.newContent(portal_type='Image',
file=upload_file,
reference=image_reference)
......@@ -636,7 +629,7 @@ class TestERP5WebWithDms(ERP5TypeTestCase, ZopeTestCase.Functional):
self.assertEqual(modification_date, last_modified_header)
# Upload a presentation with 3 pages.
upload_file = makeFileUpload('P-DMS-Presentation.3.Pages-001-en.odp')
upload_file = self.makeFileUpload('P-DMS-Presentation.3.Pages-001-en.odp')
document = document_module.newContent(portal_type='Presentation',
file=upload_file)
reference = 'P-DMS-Presentation-001-.3.Pages'
......@@ -711,7 +704,7 @@ return True
website.newContent(portal_type=web_section_portal_type)
document_reference = 'tiolive-ERP5.Freedom.TioLive'
upload_file = makeFileUpload('tiolive-ERP5.Freedom.TioLive-001-en.odp')
upload_file = self.makeFileUpload('tiolive-ERP5.Freedom.TioLive-001-en.odp')
document = self.portal.document_module.newContent(
portal_type='Presentation',
reference=document_reference,
......@@ -809,7 +802,7 @@ return True
image_reference = 'NXD-IMAGE'
module = portal.getDefaultModule(portal_type=image_portal_type)
upload_file = makeFileUpload('tiolive-ERP5.Freedom.TioLive.Logo-001-en.png')
upload_file = self.makeFileUpload('tiolive-ERP5.Freedom.TioLive.Logo-001-en.png')
image = module.newContent(portal_type=image_portal_type,
file=upload_file,
reference=image_reference)
......@@ -879,7 +872,7 @@ return True
"""
portal = self.portal
module = portal.getDefaultModule(portal_type=portal_type)
upload_file = makeFileUpload('%s.svg' % filename)
upload_file = self.makeFileUpload('%s.svg' % filename)
image = module.newContent(portal_type=portal_type,
file=upload_file,
reference="NXD-DOCUMENT")
......@@ -888,7 +881,7 @@ return True
self.assertEqual(image.getContentType(), 'image/svg+xml')
mime, converted_data = image.convert("png")
self.assertEqual(mime, 'image/png')
expected_image = makeFileUpload('%s.png' % filename)
expected_image = self.makeFileUpload('%s.png' % filename)
# Compare images and accept some minimal difference,
difference_value = compare_image(StringIO(converted_data), expected_image)
......@@ -903,7 +896,7 @@ return True
"""
portal = self.portal
module = portal.getDefaultModule(portal_type=portal_type)
upload_file = makeFileUpload('user-TESTSVG-CASE-URL-TEMPLATE.svg')
upload_file = self.makeFileUpload('user-TESTSVG-CASE-URL-TEMPLATE.svg')
svg_content = upload_file.read().replace("REPLACE_THE_URL_HERE", image_url)
# Add image using data instead file this time as it is not the goal of
......@@ -918,7 +911,7 @@ return True
self.assertEqual(image.getContentType(), 'image/svg+xml')
mime, converted_data = image.convert("png")
self.assertEqual(mime, 'image/png')
expected_image = makeFileUpload('user-TESTSVG-CASE-URL.png')
expected_image = self.makeFileUpload('user-TESTSVG-CASE-URL.png')
# Compare images and accept some minimal difference,
difference_value = compare_image(StringIO(converted_data), expected_image)
......@@ -935,7 +928,7 @@ return True
This is not used by ERP5 in production, but this is way that
prooves that conversion from SVG to PNG can use external images.
"""
image_url = "file://" + makeFilePath("user-TESTSVG-BACKGROUND-IMAGE.png")
image_url = "file://" + self.makeFileUploadPath("user-TESTSVG-BACKGROUND-IMAGE.png")
self._testImageConversionFromSVGToPNG_url(image_url, portal_type)
def _testImageConversionFromSVGToPNG_http_url(self, portal_type="Image"):
......@@ -945,7 +938,7 @@ return True
"""
portal = self.portal
module = portal.getDefaultModule(portal_type=portal_type)
upload_file = makeFileUpload('user-TESTSVG-BACKGROUND-IMAGE.png')
upload_file = self.makeFileUpload('user-TESTSVG-BACKGROUND-IMAGE.png')
background_image = module.newContent(portal_type=portal_type,
file=upload_file,
reference="NXD-BACKGROUND")
......@@ -966,11 +959,11 @@ return True
"""
portal = self.portal
module = portal.getDefaultModule(portal_type=portal_type)
upload_file = makeFileUpload('user-TESTSVG-CASE-URL-TEMPLATE.svg')
upload_file = self.makeFileUpload('user-TESTSVG-CASE-URL-TEMPLATE.svg')
svg_content = upload_file.read().replace("REPLACE_THE_URL_HERE",
"http://soidjsoidjqsoijdqsoidjqsdoijsqd.idjsijds/../user-XXX-XXX")
upload_file = makeFileUpload('user-TESTSVG-CASE-URL-TEMPLATE.svg')
upload_file = self.makeFileUpload('user-TESTSVG-CASE-URL-TEMPLATE.svg')
svg2_content = upload_file.read().replace("REPLACE_THE_URL_HERE",
"https://www.erp5.com/usXXX-XXX")
......
......@@ -31,7 +31,7 @@
import unittest
from DateTime import DateTime
from erp5.component.test.testDms import TestDocumentMixin, makeFileUpload
from erp5.component.test.testDms import TestDocumentMixin
try:
import magic
......@@ -59,7 +59,7 @@ class TestDocumentConversionCache(TestDocumentMixin):
def test_image_conversion(self):
filename = 'TEST-en-002.doc'
file_ = makeFileUpload(filename)
file_ = self.makeFileUpload(filename)
document = self.portal.portal_contributions.newContent(file=file_)
self.tic()
format_ = 'png'
......@@ -96,7 +96,7 @@ class TestDocumentConversionCache(TestDocumentMixin):
Test Conversion Cache mechanism
"""
filename = 'TEST-en-002.doc'
file_ = makeFileUpload(filename)
file_ = self.makeFileUpload(filename)
document = self.portal.portal_contributions.newContent(file=file_)
self.tic()
document_url = document.getRelativeUrl()
......@@ -129,7 +129,7 @@ class TestDocumentConversionCache(TestDocumentMixin):
def test_02_VolatileCacheConversionOfTempObject(self):
filename = 'TEST-en-002.doc'
file_ = makeFileUpload(filename)
file_ = self.makeFileUpload(filename)
document = self.portal.portal_contributions.newContent(file=file_, temp_object=1)
document.uploadFile()
document.processFile()
......@@ -163,8 +163,8 @@ class TestDocumentConversionCache(TestDocumentMixin):
def test_03_CacheConversionOfTempObjectIsNotMixed(self):
filename1 = 'TEST-en-002.doc'
filename2 = 'TEST-en-002.odt'
file1 = makeFileUpload(filename1)
file2 = makeFileUpload(filename2)
file1 = self.makeFileUpload(filename1)
file2 = self.makeFileUpload(filename2)
document1 = self.portal.portal_contributions.newContent(file=file1, temp_object=1)
document1.uploadFile()
document1.processFile()
......@@ -187,7 +187,7 @@ class TestDocumentConversionCache(TestDocumentMixin):
self.portal.portal_caches.clearAllCache()
self.tic()
filename = 'TEST-en-002.doc'
file_ = makeFileUpload(filename)
file_ = self.makeFileUpload(filename)
document = self.portal.portal_contributions.newContent(file=file_)
self.tic()
document_url = document.getRelativeUrl()
......@@ -223,7 +223,7 @@ class TestDocumentConversionCache(TestDocumentMixin):
Test Conversion Cache return expected value with checksum
"""
filename = 'TEST-en-002.doc'
file_ = makeFileUpload(filename)
file_ = self.makeFileUpload(filename)
document = self.portal.portal_contributions.newContent(file=file_)
self.tic()
document_url = document.getRelativeUrl()
......@@ -252,7 +252,7 @@ class TestDocumentConversionCache(TestDocumentMixin):
Check that md5 checksum is well updated when upload a file
"""
filename = 'TEST-en-002.doc'
file_ = makeFileUpload(filename)
file_ = self.makeFileUpload(filename)
document = self.portal.portal_contributions.newContent(file=file_)
self.tic()
document_url = document.getRelativeUrl()
......@@ -260,7 +260,7 @@ class TestDocumentConversionCache(TestDocumentMixin):
md5sum = document.getContentMd5()
self.assertTrue(md5sum)
filename2 = 'TEST-en-002.odt'
file2 = makeFileUpload(filename2)
file2 = self.makeFileUpload(filename2)
document.edit(file=file2)
self.assertNotEqual(md5sum, document.getContentMd5())
self.tic()
......@@ -275,7 +275,7 @@ class TestDocumentConversionCache(TestDocumentMixin):
self.portal.portal_caches.clearAllCache()
self.tic()
filename = 'TEST-en-002.doc'
file_ = makeFileUpload(filename)
file_ = self.makeFileUpload(filename)
document_id = 'an id with spaces'
portal_type = 'Text'
module = self.portal.getDefaultModule(portal_type)
......@@ -314,7 +314,7 @@ class TestDocumentConversionCache(TestDocumentMixin):
self.assertEqual(len(portal_type_list), len([pt for pt in portal_type_list if pt in data_mapping]))
for portal_type in portal_type_list:
module = self.portal.getDefaultModule(portal_type=portal_type)
upload_file = makeFileUpload(data_mapping[portal_type])
upload_file = self.makeFileUpload(data_mapping[portal_type])
document = module.newContent(portal_type=portal_type)
document.edit(file=upload_file)
self.tic()
......
......@@ -25,12 +25,11 @@
#
##############################################################################
from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5TypeTestCase
from erp5.component.test.testDms import makeFileUpload
from erp5.component.test.testDms import DocumentUploadTestCase
from Products.ERP5Form.PreferenceTool import Priority
class TestOOoConversionServerRetry(ERP5TypeTestCase):
class TestOOoConversionServerRetry(DocumentUploadTestCase):
def getBusinessTemplateList(self):
business_template_list = ['erp5_core_proxy_field_legacy',
'erp5_jquery',
......@@ -80,7 +79,7 @@ class TestOOoConversionServerRetry(ERP5TypeTestCase):
self.tic()
filename = 'monochrome_sample.tiff'
file_ = makeFileUpload(filename)
file_ = self.makeFileUpload(filename)
document = self.portal.document_module.newContent(portal_type='Text')
document.edit(file = file_)
message = document.Document_tryToConvertToBaseFormat()
......@@ -95,7 +94,7 @@ class TestOOoConversionServerRetry(ERP5TypeTestCase):
system_pref.setPreferredDocumentConversionServerUrlList(['https://broken.url'])
self.tic()
filename = 'TEST-en-002.doc'
file_ = makeFileUpload(filename)
file_ = self.makeFileUpload(filename)
document = self.portal.portal_contributions.newContent(file=file_)
message = document.Document_tryToConvertToBaseFormat()
......@@ -110,7 +109,7 @@ class TestOOoConversionServerRetry(ERP5TypeTestCase):
system_pref.setPreferredOoodocServerTimeout(1)
self.tic()
filename = 'TEST-en-002.doc'
file_ = makeFileUpload(filename)
file_ = self.makeFileUpload(filename)
document = self.portal.portal_contributions.newContent(file=file_)
message = document.Document_tryToConvertToBaseFormat()
......
......@@ -31,7 +31,6 @@
import unittest
import os
from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5TypeTestCase
from Products.ERP5Type.tests.utils import FileUpload
from unittest import expectedFailure
import httplib
......@@ -40,20 +39,13 @@ from DateTime import DateTime
from lxml import etree
def makeFilePath(name):
import Products.ERP5.tests
return os.path.join(os.path.dirname(Products.ERP5.tests.__file__),
'test_data', name)
def makeFileUpload(name, as_name=None):
if as_name is None:
as_name = name
path = makeFilePath(name)
return FileUpload(path, as_name)
class TestWebDavSupport(ERP5TypeTestCase):
"""Test for WEBDAV access.
"""
def _getTestDataPath(self):
import Products.ERP5.tests
return os.path.join(os.path.dirname(Products.ERP5.tests.__file__), 'test_data')
def getTitle(self):
return "Test WebDav Support"
......@@ -90,7 +82,7 @@ class TestWebDavSupport(ERP5TypeTestCase):
"""
person = self.portal.person_module.newContent()
self.tic()
file_object = makeFileUpload('images/erp5_logo.png')
file_object = self.makeFileUpload('images/erp5_logo.png')
response = self.publish(person.getPath() + '/erp5_logo.png',
request_method='PUT',
stdin=file_object,
......@@ -109,7 +101,7 @@ class TestWebDavSupport(ERP5TypeTestCase):
# Create a new document via FTP/DAV
path = self.portal.portal_contributions.getPath()
filename = 'P-DMS-Presentation.3.Pages-001-en.odp'
file_object = makeFileUpload(filename)
file_object = self.makeFileUpload(filename)
response = self.publish('%s/%s' % (path, filename),
request_method='PUT',
stdin=file_object,
......@@ -131,7 +123,7 @@ class TestWebDavSupport(ERP5TypeTestCase):
# Create a new document via FTP/DAV
path = self.portal.portal_contributions.getPath()
filename = 'P-DMS-Presentation.3.Pages-001-en.odp'
file_object = makeFileUpload(filename)
file_object = self.makeFileUpload(filename)
response = self.publish('%s/%s' % (path, filename),
request_method='PUT',
stdin=file_object,
......@@ -202,7 +194,7 @@ class TestWebDavSupport(ERP5TypeTestCase):
"""
path = self.portal.portal_contributions.getPath()
filename = 'P-DMS-Presentation.3.Pages-001-en.odp'
file_object = makeFileUpload(filename)
file_object = self.makeFileUpload(filename)
response = self.publish('%s/%s' % (path, filename),
request_method='PUT',
stdin=file_object,
......@@ -229,7 +221,7 @@ class TestWebDavSupport(ERP5TypeTestCase):
"""
path = self.portal.portal_contributions.getPath()
filename = 'P-DMS-Presentation.3.Pages-001-en.odp'
file_object = makeFileUpload(filename)
file_object = self.makeFileUpload(filename)
response = self.publish('%s/%s' % (path, filename),
request_method='PUT',
stdin=file_object,
......@@ -267,7 +259,7 @@ class TestWebDavSupport(ERP5TypeTestCase):
"""
path = self.portal.portal_contributions.getPath()
filename = 'P-DMS-Presentation.3.Pages-001-en.odp'
file_object = makeFileUpload(filename)
file_object = self.makeFileUpload(filename)
response = self.publish('%s/%s' % (path, filename),
request_method='PUT',
stdin=file_object,
......
......@@ -28,7 +28,7 @@
##############################################################################
import unittest
from erp5.component.test.testDms import TestDocument, makeFileUpload
from erp5.component.test.testDms import TestDocument
class TestDocumentWithPreConversion(TestDocument):
"""
......@@ -40,7 +40,7 @@ class TestDocumentWithPreConversion(TestDocument):
def test_preConvertedReferencedImageInWebPageContent(self):
# create an image
upload_file = makeFileUpload('cmyk_sample.jpg')
upload_file = self.makeFileUpload('cmyk_sample.jpg')
image = self.portal.image_module.newContent(portal_type='Image',
reference='Embedded-XXX',
version='001',
......@@ -75,7 +75,7 @@ class TestDocumentWithPreConversion(TestDocument):
language='en')
# draft image is not convertible
upload_file = makeFileUpload('cmyk_sample.jpg')
upload_file = self.makeFileUpload('cmyk_sample.jpg')
image.edit(file=upload_file)
self.tic()
self.assertEqual(False, image.Base_isConvertible())
......
......@@ -30,7 +30,7 @@
import unittest
from unittest import expectedFailure
from Products.ERP5Type.Base import TempBase
from erp5.component.test.testDms import makeFileUpload, TestDocumentMixin
from erp5.component.test.testDms import TestDocumentMixin
def _getGadgetInstanceUrlFromKnowledgePad(knowledge_pad, gadget):
""" Get Knowledge Box's relative URL specialising a gadget in a Knowledge Pad."""
......@@ -998,38 +998,38 @@ class TestKMSearch(TestKMMixIn):
# create docs to be referenced:
# (1) TEST, 002, en
filename = 'TEST-en-002.odt'
file_ = makeFileUpload(filename)
file_ = self.makeFileUpload(filename)
self.portal.portal_contributions.newContent(file=file_)
# (2) TEST, 002, fr
as_name = 'TEST-fr-002.odt'
file_ = makeFileUpload(filename, as_name)
file_ = self.makeFileUpload(filename, as_name)
document2 = self.portal.portal_contributions.newContent(file=file_)
# (3) TEST, 003, en
as_name = 'TEST-en-003.odt'
file_ = makeFileUpload(filename, as_name)
file_ = self.makeFileUpload(filename, as_name)
document3 = self.portal.portal_contributions.newContent(file=file_)
# create docs to contain references in text_content:
# REF, 002, en; "I use reference to look up TEST"
filename = 'REF-en-002.odt'
file_ = makeFileUpload(filename)
file_ = self.makeFileUpload(filename)
document5 = self.portal.portal_contributions.newContent(file=file_)
# REFLANG, 001, en: "I use reference and language to look up TEST-fr"
#filename = 'REFLANG-en-001.odt'
#file = makeFileUpload(filename)
#file = self.makeFileUpload(filename)
#document6 = self.portal.portal_contributions.newContent(file=file)
# REFVER, 001, en: "I use reference and version to look up TEST-002"
#filename = 'REFVER-en-001.odt'
#file = makeFileUpload(filename)
#file = self.makeFileUpload(filename)
#document7 = self.portal.portal_contributions.newContent(file=file)
# REFVERLANG, 001, en: "I use reference, version and language to look up TEST-002-en"
#filename = 'REFVERLANG-en-001.odt'
#file = makeFileUpload(filename)
#file = self.makeFileUpload(filename)
#document8 = self.portal.portal_contributions.newContent(file=file)
self.tic()
......
......@@ -26,14 +26,13 @@
##############################################################################
import warnings
from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5TypeTestCase
from erp5.component.test.testDms import makeFileUpload
from erp5.component.test.testDms import DocumentUploadTestCase
original_warnings_showwarnings = warnings.showwarning
class TestERP5PDFMerge(ERP5TypeTestCase):
class TestERP5PDFMerge(DocumentUploadTestCase):
def test_showwarning_issue(self):
"""
......@@ -42,7 +41,7 @@ class TestERP5PDFMerge(ERP5TypeTestCase):
"""
self.assertEqual(warnings.showwarning, original_warnings_showwarnings)
document = self.portal.portal_contributions.newContent(
file=makeFileUpload('REF-en-001.pdf'))
file=self.makeFileUpload('REF-en-001.pdf'))
merged_pdf_data = self.portal.ERP5Site_mergePDFList(
[document.getData(), document.getData()])
self.portal.document_module.newContent(
......@@ -53,7 +52,7 @@ class TestERP5PDFMerge(ERP5TypeTestCase):
def test_erp5_merge_pdf(self):
document = self.portal.portal_contributions.newContent(
file=makeFileUpload('REF-en-001.pdf'))
file=self.makeFileUpload('REF-en-001.pdf'))
merged_pdf_data = self.portal.ERP5Site_mergePDFList(
[document.getData(), document.getData()])
merged_document = self.portal.document_module.newContent(
......@@ -63,7 +62,7 @@ class TestERP5PDFMerge(ERP5TypeTestCase):
def test_erp5_merge_pdf_start_on_recto(self):
document = self.portal.portal_contributions.newContent(
file=makeFileUpload('REF-en-001.pdf'))
file=self.makeFileUpload('REF-en-001.pdf'))
merged_pdf_data = self.portal.ERP5Site_mergePDFList(
[document.getData(), document.getData()], start_on_recto=True)
merged_document = self.portal.document_module.newContent(
......
......@@ -26,12 +26,12 @@
#
##############################################################################
from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5TypeTestCase
from erp5.component.test.testDms import makeFileUpload
from erp5.component.test.testDms import DocumentUploadTestCase
from time import time
import base64
class TestRunMyDoc(ERP5TypeTestCase):
class TestRunMyDoc(DocumentUploadTestCase):
"""
Basic Test for internal implementation of RunMyDocs
"""
......@@ -96,7 +96,7 @@ class TestRunMyDoc(ERP5TypeTestCase):
Test Screeshot upload script used by Zelenium to
update screenshots of the documents.
"""
image_upload = makeFileUpload('TEST-en-002.png')
image_upload = self.makeFileUpload('TEST-en-002.png')
self.assertNotEqual(None, image_upload)
# Create a web page, and check if the content is not overwriten
......
......@@ -3,16 +3,7 @@ import PIL.Image as PIL_Image
import os
import transaction
from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5TypeTestCase
class FileUpload(file):
"""Act as an uploaded file.
"""
__allow_access_to_unprotected_subobjects__ = 1
def __init__(self, path, name):
self.filename = name
file.__init__(self, path)
self.headers = {}
from Products.ERP5Type.tests.utils import FileUpload
def makeFilePath(name):
# return os.path.join(os.path.dirname(__file__), 'tmp', name)
......
from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5TypeTestCase
import transaction
from io import FileIO
import os
class FileUpload(FileIO):
"""Act as an uploaded file.
"""
__allow_access_to_unprotected_subobjects__ = 1
def __init__(self, path, name):
self.filename = name
super(FileUpload, self).__init__(path)
self.headers = {}
def makeFilePath(name):
#return os.path.join(os.path.dirname(__file__), 'tmp', name)
return name
def makeFileUpload(name, as_name=None):
if as_name is None:
as_name = name
path = makeFilePath(name)
return FileUpload(path, as_name)
class TestSafeImage(ERP5TypeTestCase):
def afterSetUp(self):
portal = self.getPortalObject()
......@@ -45,7 +23,7 @@ class TestSafeImage(ERP5TypeTestCase):
fd = os.open(path_image, os.O_CREAT | os.O_RDWR)
os.write(fd,str(image.data))
os.close(fd)
_image = makeFileUpload(path_image)
_image = self.makeFileUpload(path_image)
image = self.image_module.newContent(portal_type='Image',title='testImage',
id='testImage',file=_image,filename='testImage')
return image
......@@ -57,7 +35,7 @@ class TestSafeImage(ERP5TypeTestCase):
fd = os.open(path_image, os.O_CREAT | os.O_RDWR)
os.write(fd,str(image.data))
os.close(fd)
tile_image = makeFileUpload(path_image)
tile_image = self.makeFileUpload(path_image)
tile = self.image_module.newContent(portal_type='Image Tile',title='testTile',
id='testTile',file=tile_image,filename='testTile')
return tile
......@@ -69,7 +47,7 @@ class TestSafeImage(ERP5TypeTestCase):
fd = os.open(path_image, os.O_CREAT | os.O_RDWR)
os.write(fd,str(image.data))
os.close(fd)
tile_image_transformed = makeFileUpload(path_image)
tile_image_transformed = self.makeFileUpload(path_image)
tile_transformed = self.image_module.newContent(portal_type='Image Tile Transformed',
title='testTileTransformed',id='testTileTransformed',
file=tile_image_transformed,filename='testTileTransformed')
......
......@@ -35,22 +35,15 @@ import unittest
from AccessControl.SecurityManagement import newSecurityManager
from Products.ERP5Type.tests.runUnitTest import tests_home
from Products.ERP5Type.tests.utils import FileUpload
from erp5.component.tool import SynchronizationTool
from erp5.component.test.testERP5SyncML import TestERP5SyncMLMixin
from erp5.component.document import SyncMLSubscription
import Products.ERP5.tests
test_files = os.path.join(os.path.dirname(Products.ERP5.tests.__file__), 'test_data')
FILENAME_REGULAR_EXPRESSION = "(?P<reference>[A-Z]{3,10})-\
(?P<language>[a-z]{2})-(?P<version>[0-9]{3})"
REFERENCE_REGULAR_EXPRESSION = "(?P<reference>[A-Z]{3,10})(-\
(?P<language>[a-z]{2}))?(-(?P<version>[0-9]{3}))?"
def makeFileUpload(name):
path = os.path.join(test_files, name)
return FileUpload(path, name)
class TestERP5DocumentSyncMLMixin(TestERP5SyncMLMixin):
nb_objects = 10
......@@ -63,13 +56,21 @@ class TestERP5DocumentSyncMLMixin(TestERP5SyncMLMixin):
#for documents (encoding in unicode for utf-8)
#files
filename_text = 'TEST-en-002.txt'
size_filename_text = len(makeFileUpload(filename_text).read())
@property
def size_filename_text(self):
return len(self.makeFileUpload(self.filename_text).read())
filename_odt = 'TEST-en-002.odt'
size_filename_odt = len(makeFileUpload(filename_odt).read())
@property
def size_filename_odt(self):
return len(self.makeFileUpload(self.filename_odt).read())
filename_ppt = 'TEST-en-002.ppt'
size_filename_ppt = len(makeFileUpload(filename_ppt).read())
@property
def size_filename_ppt(self):
return len(self.makeFileUpload(self.filename_ppt).read())
filename_pdf = 'TEST-en-002.pdf'
size_filename_pdf = len(makeFileUpload(filename_pdf).read())
@property
def size_filename_pdf(self):
return len(self.makeFileUpload(self.filename_pdf).read())
#properties
reference1 = 'P-SYNCML.Text'
version1 = '001'
......@@ -135,6 +136,9 @@ class TestERP5DocumentSyncMLMixin(TestERP5SyncMLMixin):
self.clearDocumentModules()
self.clearPublicationsAndSubscriptions()
def _getTestDataPath(self):
import Products.ERP5.tests
return os.path.join(os.path.dirname(Products.ERP5.tests.__file__), 'test_data')
def clearFiles(self):
# reset files, because we do sync by files
......@@ -280,7 +284,7 @@ class TestERP5DocumentSyncMLMixin(TestERP5SyncMLMixin):
kw = {'reference': self.reference1, 'Version': self.version1,
'Language': self.language1, 'Description': self.description1}
document_text.edit(**kw)
file_ = makeFileUpload(self.filename_text)
file_ = self.makeFileUpload(self.filename_text)
document_text.edit(file=file_)
self.tic()
document_pdf = document_server.newContent(id=self.id2,
......@@ -288,7 +292,7 @@ class TestERP5DocumentSyncMLMixin(TestERP5SyncMLMixin):
kw = {'reference': self.reference2, 'Version': self.version2,
'Language': self.language2, 'Description': self.description2}
document_pdf.edit(**kw)
file_ = makeFileUpload(self.filename_pdf)
file_ = self.makeFileUpload(self.filename_pdf)
document_pdf.edit(file=file_)
self.tic()
nb_document = len(document_server)
......@@ -305,7 +309,7 @@ class TestERP5DocumentSyncMLMixin(TestERP5SyncMLMixin):
kw = {'reference': reference, 'version': version, 'language': language}
doc_text.edit(**kw)
if file_name is not None:
file_ = makeFileUpload(file_name)
file_ = self.makeFileUpload(file_name)
doc_text.edit(file=file_)
return doc_text
......@@ -480,7 +484,7 @@ class TestERP5DocumentSyncML(TestERP5DocumentSyncMLMixin):
# Then we do only modification on a client (the gid) of client => add a object
kw = {'reference':self.reference1,'version':self.version3}
document_c.edit(**kw)
file_ = makeFileUpload(self.filename_odt)
file_ = self.makeFileUpload(self.filename_odt)
document_c.edit(file=file_)
self.tic()
self.synchronize(self.sub_id1)
......@@ -591,14 +595,14 @@ class TestERP5DocumentSyncML(TestERP5DocumentSyncMLMixin):
doc_s = document_server._getOb(id_text)
kw = {'description':self.description1}
doc_s.edit(**kw)
file_ = makeFileUpload(self.filename_odt)
file_ = self.makeFileUpload(self.filename_odt)
doc_s.edit(file=file_)
# Side client modification gid of a odt document
id_odt = str(self.ids[self.id_max_text+1])
doc_c = document_client1._getOb(id_odt)
kw = {'description':self.description3}
doc_c.edit(**kw)
file_ = makeFileUpload(self.filename_text)
file_ = self.makeFileUpload(self.filename_text)
doc_c.edit(file=file_)
self.tic()
self.synchronize(self.sub_id1)
......@@ -692,7 +696,7 @@ class TestERP5DocumentSyncML(TestERP5DocumentSyncMLMixin):
document_s = document_server._getOb(self.id1)
kw = {'description': self.description2, 'short_title': self.short_title2 }
document_s.edit(**kw)
file_ = makeFileUpload(self.filename_ppt)
file_ = self.makeFileUpload(self.filename_ppt)
document_s.edit(file=file_)
self.tic()
......@@ -701,7 +705,7 @@ class TestERP5DocumentSyncML(TestERP5DocumentSyncMLMixin):
document_c1 = document_client1._getOb(self.id1)
kw = {'description': self.description3, 'short_title': self.short_title3 }
document_c1.edit(**kw)
file_ = makeFileUpload(self.filename_odt)
file_ = self.makeFileUpload(self.filename_odt)
document_c1.edit(file=file_)
self.tic()
......@@ -822,7 +826,7 @@ class TestERP5DocumentSyncML(TestERP5DocumentSyncMLMixin):
self.assertXMLViewIsEqual(self.sub_id_from_server, document_s, document_c, force=1)
# Then we change things on both sides and we look if there
# is synchronization from only one way
file_ = makeFileUpload(self.filename_odt)
file_ = self.makeFileUpload(self.filename_odt)
document_c.edit(file=file_)
kw = {'short_title' : self.short_title2}
......
......@@ -42,8 +42,7 @@ from Products.ERP5Type.tests.ERP5TypeTestCase import (
ERP5TypeTestCase, _getConversionServerUrlList)
from Products.CMFCore.WorkflowCore import WorkflowException
from Products.ERP5Type.tests.Sequence import SequenceList
from Products.ERP5Type.tests.utils import FileUpload, removeZODBPythonScript, \
createZODBPythonScript
from Products.ERP5Type.tests.utils import removeZODBPythonScript, createZODBPythonScript
from Products.ERP5OOo.OOoUtils import OOoBuilder
from Products.CMFCore.utils import getToolByName
from zExceptions import BadRequest
......@@ -57,21 +56,10 @@ import urlparse
import base64
import mock
# test files' home
TEST_FILES_HOME = os.path.join(os.path.dirname(__file__), 'test_document')
FILENAME_REGULAR_EXPRESSION = "(?P<reference>[A-Z&é@{]{3,7})-(?P<language>[a-z]{2})-(?P<version>[0-9]{3})"
REFERENCE_REGULAR_EXPRESSION = "(?P<reference>[A-Z&é@{]{3,7})(-(?P<language>[a-z]{2}))?(-(?P<version>[0-9]{3}))?"
def makeFilePath(name):
return os.path.join(TEST_FILES_HOME, name)
def makeFileUpload(name, as_name=None):
if as_name is None:
as_name = name
path = makeFilePath(name)
return FileUpload(path, as_name)
class IngestionTestCase(ERP5TypeTestCase):
def getBusinessTemplateList(self):
......@@ -131,6 +119,8 @@ class IngestionTestCase(ERP5TypeTestCase):
skin_tool.custom._delObject(script_id)
self.commit()
def _getTestDataPath(self):
return os.path.join(os.path.dirname(__file__), 'test_document')
class TestIngestion(IngestionTestCase):
"""
......@@ -278,7 +268,7 @@ class TestIngestion(IngestionTestCase):
"""
for revision, format in enumerate(format_list):
filename = 'TEST-en-002.%s' %format
f = makeFileUpload(filename)
f = self.makeFileUpload(filename)
document.edit(file=f)
self.tic()
self.assertTrue(document.hasFile())
......@@ -298,7 +288,7 @@ class TestIngestion(IngestionTestCase):
can be converted to any of the formats in asserted_target_list
"""
filename = 'TEST-en-002.' + format
f = makeFileUpload(filename)
f = self.makeFileUpload(filename)
document.edit(file=f)
self.tic()
# We call clear cache to be sure that the target list is updated
......@@ -328,7 +318,7 @@ class TestIngestion(IngestionTestCase):
old_portal_type = ''
for extension, portal_type in extension_to_type:
filename = 'TEST-en-002.%s' %extension
file = makeFileUpload(filename)
file = self.makeFileUpload(filename)
# if we change portal type we must change version because
# mergeRevision would fail
if portal_type != old_portal_type:
......@@ -525,7 +515,7 @@ class TestIngestion(IngestionTestCase):
document = self.portal.restrictedTraverse(sequence.get('document_path'))
# First revision is 1 (like web pages)
self.assertEqual(document.getRevision(), '1')
f = makeFileUpload(filename)
f = self.makeFileUpload(filename)
document.edit(file=f)
self.assertTrue(document.hasFile())
self.assertEqual(document.getFilename(), filename)
......@@ -539,7 +529,7 @@ class TestIngestion(IngestionTestCase):
Upload a file from view form and make sure this increases the revision
"""
document = self.portal.restrictedTraverse(sequence.get('document_path'))
f = makeFileUpload('TEST-en-002.doc')
f = self.makeFileUpload('TEST-en-002.doc')
revision = document.getRevision()
document.edit(file=f)
self.assertEqual(document.getRevision(), str(int(revision) + 1))
......@@ -550,7 +540,7 @@ class TestIngestion(IngestionTestCase):
"""
Upload a file from contribution.
"""
f = makeFileUpload('TEST-en-002.doc')
f = self.makeFileUpload('TEST-en-002.doc')
document = self.portal.portal_contributions.newContent(file=f)
sequence.edit(document_path=document.getPath())
self.commit()
......@@ -565,7 +555,7 @@ class TestIngestion(IngestionTestCase):
number_of_document = len(self.portal.document_module.objectIds())
self.assertNotIn('This document is modified.', document.asText())
f = makeFileUpload('TEST-en-002-modified.doc')
f = self.makeFileUpload('TEST-en-002-modified.doc')
f.filename = 'TEST-en-002.doc'
self.portal.portal_contributions.newContent(file=f)
......@@ -581,7 +571,7 @@ class TestIngestion(IngestionTestCase):
"""
Upload another file from contribution.
"""
f = makeFileUpload('ANOTHE-en-001.doc')
f = self.makeFileUpload('ANOTHE-en-001.doc')
document = self.portal.portal_contributions.newContent(id='two', file=f)
sequence.edit(document_path=document.getPath())
self.tic()
......@@ -610,7 +600,7 @@ class TestIngestion(IngestionTestCase):
self.assertEqual(property_dict['description'], 'comments')
self.assertEqual(property_dict['subject_list'], ['keywords'])
# Then make sure metadata discovery works
f = makeFileUpload(filename)
f = self.makeFileUpload(filename)
document.edit(file=f)
self.assertEqual(document.getReference(), 'TEST')
self.assertEqual(document.getLanguage(), 'en')
......@@ -644,7 +634,7 @@ class TestIngestion(IngestionTestCase):
Upload with custom getPropertyDict methods
check that all metadata are correct
"""
f = makeFileUpload('TEST-en-002.doc')
f = self.makeFileUpload('TEST-en-002.doc')
document = self.portal.portal_contributions.newContent(file=f)
self.tic()
# Then make sure content discover works
......@@ -798,7 +788,7 @@ class TestIngestion(IngestionTestCase):
Try to export PDF to text and HTML
"""
document = self.portal.restrictedTraverse(sequence.get('document_path'))
f = makeFileUpload('TEST-en-002.pdf')
f = self.makeFileUpload('TEST-en-002.pdf')
document.edit(file=f)
mime, text = document.convert('text')
self.assertIn('magic', text)
......@@ -812,7 +802,7 @@ class TestIngestion(IngestionTestCase):
Check we are able to resize images
"""
image = self.portal.restrictedTraverse(sequence.get('document_path'))
f = makeFileUpload('TEST-en-002.jpg')
f = self.makeFileUpload('TEST-en-002.jpg')
image.edit(file=f)
self.tic()
mime, data = image.convert(None)
......@@ -941,8 +931,7 @@ class TestIngestion(IngestionTestCase):
"""
Email was sent in by someone to ERP5.
"""
f = open(makeFilePath('email_from.txt'))
document = self.receiveEmail(f.read())
self.receiveEmail(self.makeFileUpload('email_from.txt').read())
self.tic()
def stepReceiveMultipleAttachmentsEmail(self, sequence=None,
......@@ -950,8 +939,7 @@ class TestIngestion(IngestionTestCase):
"""
Email was sent in by someone to ERP5.
"""
f = open(makeFilePath('email_multiple_attachments.eml'))
document = self.receiveEmail(f.read())
self.receiveEmail(self.makeFileUpload('email_multiple_attachments.eml').read())
self.tic()
def stepVerifyEmailedMultipleDocumentsInitialContribution(self, sequence=None, sequence_list=None, **kw):
......@@ -1378,7 +1366,7 @@ class TestIngestion(IngestionTestCase):
"""
Upload a file from contribution.
"""
f = makeFileUpload('TEST-en-002.doc', 'T&é@{T-en-002.doc')
f = self.makeFileUpload('TEST-en-002.doc', 'T&é@{T-en-002.doc')
document = self.portal.portal_contributions.newContent(file=f)
sequence.edit(document_path=document.getPath())
self.commit()
......@@ -1450,7 +1438,7 @@ class TestIngestion(IngestionTestCase):
"""
portal = self.portal
contribution_tool = getToolByName(portal, 'portal_contributions')
file_object = makeFileUpload('TEST-en-002.doc')
file_object = self.makeFileUpload('TEST-en-002.doc')
document = contribution_tool.newContent(file=file_object)
self.assertEqual(document.getFilename(), 'TEST-en-002.doc')
my_filename = 'Something.doc'
......@@ -1473,7 +1461,7 @@ class TestIngestion(IngestionTestCase):
site='arctic/spitsbergen'))
portal.document_module.manage_setLocalRoles(user.Person_getUserId(), ['Assignor',])
self.tic()
file_object = makeFileUpload('TEST-en-002.doc')
file_object = self.makeFileUpload('TEST-en-002.doc')
document = contribution_tool.newContent(file=file_object)
document.discoverMetadata(document.getFilename(), user.Person_getUserId())
self.tic()
......@@ -1497,7 +1485,7 @@ class TestIngestion(IngestionTestCase):
portal.document_module.manage_setLocalRoles(other_user.Person_getUserId(), ['Assignor',])
self.tic()
file_object = makeFileUpload('TEST-en-002.doc')
file_object = self.makeFileUpload('TEST-en-002.doc')
document = contribution_tool.newContent(file=file_object)
# We only consider the higher group of assignments
......@@ -1915,8 +1903,7 @@ return result
as a application/octet-stream without explicit extension, become
a Spreadsheet ?
"""
path = makeFilePath('import_region_category.ods')
data = open(path, 'r').read()
data = self.makeFileUpload('import_region_category.ods').read()
document = self.portal.portal_contributions.newContent(filename='toto',
data=data,
......@@ -1996,8 +1983,7 @@ return result
def test_User_Portal_Type_parameter_is_honoured(self):
"""Check that given portal_type is always honoured
"""
path = makeFilePath('import_region_category.xls')
data = open(path, 'r').read()
data = self.makeFileUpload('import_region_category.xls').read()
document = self.portal.portal_contributions.newContent(
filename='import_region_category.xls',
......@@ -2014,8 +2000,7 @@ return result
def test_User_ID_parameter_is_honoured(self):
"""Check that given id is always honoured
"""
path = makeFilePath('import_region_category.xls')
data = open(path, 'r').read()
data = self.makeFileUpload('import_region_category.xls').read()
document = self.portal.portal_contributions.newContent(
id='this_id',
......@@ -2039,8 +2024,7 @@ return result
def test_newContent_trough_http(self):
filename = 'import_region_category.xls'
path = makeFilePath(filename)
data = open(path, 'r').read()
data = self.makeFileUpload(filename).read()
reference = 'ITISAREFERENCE'
portal_url = self.portal.absolute_url()
......@@ -2134,7 +2118,7 @@ class Base_contributeMixin:
version=None,
description=None,
attach_document_to_context=True,
file=makeFileUpload('TEST-en-002.odt'))
file=self.makeFileUpload('TEST-en-002.odt'))
self.assertEqual('Text', contributed_document.getPortalType())
self.tic()
document_list = person.getFollowUpRelatedValueList()
......@@ -2179,7 +2163,7 @@ class Base_contributeMixin:
person = self.portal.person_module.newContent(portal_type='Person')
contributed_document = person.Base_contribute(
portal_type='PDF',
file=makeFileUpload('TEST-en-002.odt'))
file=self.makeFileUpload('TEST-en-002.odt'))
self.assertEqual('PDF', contributed_document.getPortalType())
def test_Base_contribute_input_parameter_dict(self):
......@@ -2188,7 +2172,7 @@ class Base_contributeMixin:
person = self.portal.person_module.newContent(portal_type='Person')
contributed_document = person.Base_contribute(
title='user supplied title',
file=makeFileUpload('TEST-en-002.pdf'))
file=self.makeFileUpload('TEST-en-002.pdf'))
self.tic()
self.assertEqual('user supplied title', contributed_document.getTitle())
......@@ -2201,7 +2185,7 @@ class Base_contributeMixin:
# we use as_name, to prevent regular expression from detecting a
# reference during ingestion, so that we can upload multiple documents
# in one test.
file=makeFileUpload('TEST-en-002.pdf', as_name='doc.pdf'))
file=self.makeFileUpload('TEST-en-002.pdf', as_filename='doc.pdf'))
self.tic()
self.assertEqual(contributed_document.getValidationState(), 'draft')
contributed_document.setReference(None)
......@@ -2210,7 +2194,7 @@ class Base_contributeMixin:
contributed_document = person.Base_contribute(
publication_state='shared',
synchronous_metadata_discovery=False,
file=makeFileUpload('TEST-en-002.pdf', as_name='doc.pdf'))
file=self.makeFileUpload('TEST-en-002.pdf', as_filename='doc.pdf'))
self.tic()
self.assertEqual(contributed_document.getValidationState(), 'shared')
contributed_document.setReference(None)
......@@ -2219,7 +2203,7 @@ class Base_contributeMixin:
contributed_document = person.Base_contribute(
publication_state='shared',
synchronous_metadata_discovery=True,
file=makeFileUpload('TEST-en-002.pdf', as_name='doc.pdf'))
file=self.makeFileUpload('TEST-en-002.pdf', as_filename='doc.pdf'))
self.tic()
self.assertEqual(contributed_document.getValidationState(), 'shared')
contributed_document.setReference(None)
......@@ -2228,7 +2212,7 @@ class Base_contributeMixin:
contributed_document = person.Base_contribute(
publication_state='released',
synchronous_metadata_discovery=False,
file=makeFileUpload('TEST-en-002.pdf', as_name='doc.pdf'))
file=self.makeFileUpload('TEST-en-002.pdf', as_filename='doc.pdf'))
self.tic()
self.assertEqual(contributed_document.getValidationState(), 'released')
contributed_document.setReference(None)
......@@ -2237,7 +2221,7 @@ class Base_contributeMixin:
contributed_document = person.Base_contribute(
publication_state='released',
synchronous_metadata_discovery=True,
file=makeFileUpload('TEST-en-002.pdf', as_name='doc.pdf'))
file=self.makeFileUpload('TEST-en-002.pdf', as_filename='doc.pdf'))
self.tic()
self.assertEqual(contributed_document.getValidationState(), 'released')
contributed_document.setReference(None)
......@@ -2246,7 +2230,7 @@ class Base_contributeMixin:
contributed_document = person.Base_contribute(
synchronous_metadata_discovery=False,
publication_state='published',
file=makeFileUpload('TEST-en-002.pdf', as_name='doc.pdf'))
file=self.makeFileUpload('TEST-en-002.pdf', as_filename='doc.pdf'))
self.tic()
self.assertEqual(contributed_document.getValidationState(), 'published')
contributed_document.setReference(None)
......@@ -2255,7 +2239,7 @@ class Base_contributeMixin:
contributed_document = person.Base_contribute(
synchronous_metadata_discovery=True,
publication_state='published',
file=makeFileUpload('TEST-en-002.pdf', as_name='doc.pdf'))
file=self.makeFileUpload('TEST-en-002.pdf', as_filename='doc.pdf'))
self.tic()
self.assertEqual(contributed_document.getValidationState(), 'published')
......@@ -2275,7 +2259,7 @@ class Base_contributeMixin:
contributed_document = person.Base_contribute(
publication_state='shared',
synchronous_metadata_discovery=True,
file=makeFileUpload('TEST-en-002.pdf', as_name='doc.pdf'))
file=self.makeFileUpload('TEST-en-002.pdf', as_filename='doc.pdf'))
self.tic()
self.assertEqual(contributed_document.getValidationState(), 'shared')
contributed_document.setReference(None)
......@@ -2284,14 +2268,14 @@ class Base_contributeMixin:
contributed_document = person.Base_contribute(
publication_state='shared',
synchronous_metadata_discovery=False,
file=makeFileUpload('TEST-en-002.pdf', as_name='doc.pdf'))
file=self.makeFileUpload('TEST-en-002.pdf', as_filename='doc.pdf'))
self.tic()
self.assertEqual(contributed_document.getValidationState(), 'shared')
contributed_document.setReference(None)
contributed_document = person.Base_contribute(
publication_state=None,
file=makeFileUpload('TEST-en-002.pdf', as_name='doc.pdf'))
file=self.makeFileUpload('TEST-en-002.pdf', as_filename='doc.pdf'))
self.tic()
self.assertEqual(contributed_document.getValidationState(), 'published')
......@@ -2315,7 +2299,7 @@ class TestBase_contributeWithSecurity(IngestionTestCase, Base_contributeMixin):
ret = person.Base_contribute(
redirect_to_context=True,
synchronous_metadata_discovery=True,
file=makeFileUpload('TEST-en-002.pdf'))
file=self.makeFileUpload('TEST-en-002.pdf'))
self.assertIn(
('portal_status_message', 'PDF created successfully.'),
urlparse.parse_qsl(urlparse.urlparse(ret).query))
......@@ -2333,7 +2317,7 @@ class TestBase_contributeWithSecurity(IngestionTestCase, Base_contributeMixin):
ret = person.Base_contribute(
redirect_to_context=True,
synchronous_metadata_discovery=True,
file=makeFileUpload('TEST-en-002.pdf'))
file=self.makeFileUpload('TEST-en-002.pdf'))
self.assertIn(
('portal_status_message', 'PDF updated successfully.'),
urlparse.parse_qsl(urlparse.urlparse(ret).query))
......@@ -2356,7 +2340,7 @@ class TestBase_contributeWithSecurity(IngestionTestCase, Base_contributeMixin):
person.Base_contribute(
redirect_to_context=True,
synchronous_metadata_discovery=synchronous_metadata_discovery,
file=makeFileUpload('TEST-en-002.pdf'))
file=self.makeFileUpload('TEST-en-002.pdf'))
self.assertIn(
('portal_status_message',
'You are not allowed to update the existing document which has the same coordinates.'),
......@@ -2374,7 +2358,7 @@ class TestBase_contributeWithSecurity(IngestionTestCase, Base_contributeMixin):
"You are not allowed to update the existing document which has the same coordinates"):
person.Base_contribute(
synchronous_metadata_discovery=synchronous_metadata_discovery,
file=makeFileUpload('TEST-en-002.pdf'))
file=self.makeFileUpload('TEST-en-002.pdf'))
self.assertEqual(document.getData(), b'')
def test_Base_contribute_publication_state_unauthorized(self):
......@@ -2394,7 +2378,7 @@ class TestBase_contributeWithSecurity(IngestionTestCase, Base_contributeMixin):
publication_state='published',
redirect_to_context=True,
synchronous_metadata_discovery=True,
file=makeFileUpload('TEST-en-002.pdf'))
file=self.makeFileUpload('TEST-en-002.pdf'))
self.assertIn(
('portal_status_message', 'You are not allowed to contribute document in that state.'),
urlparse.parse_qsl(urlparse.urlparse(str(ctx.exception)).query))
......@@ -2409,7 +2393,7 @@ class TestBase_contributeWithSecurity(IngestionTestCase, Base_contributeMixin):
person.Base_contribute(
publication_state='published',
synchronous_metadata_discovery=True,
file=makeFileUpload('TEST-en-002.pdf'))
file=self.makeFileUpload('TEST-en-002.pdf'))
# when using asynchronous metadata discovery, an error occurs in activity,
# but not document is published
......@@ -2417,7 +2401,7 @@ class TestBase_contributeWithSecurity(IngestionTestCase, Base_contributeMixin):
publication_state='published',
redirect_to_context=True,
synchronous_metadata_discovery=False,
file=makeFileUpload('TEST-en-002.pdf'))
file=self.makeFileUpload('TEST-en-002.pdf'))
with self.assertRaisesRegex(
Exception,
"Transition document_publication_workflow/publish unsupported"):
......
......@@ -32,23 +32,18 @@ import os
from AccessControl.SecurityManagement import newSecurityManager
from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5TypeTestCase
from Products.ERP5Type.tests.utils import FileUpload
from Products.ERP5Type.tests.Sequence import SequenceList
from Products.ERP5OOo.OOoUtils import OOoParser
from DateTime import DateTime
import six
def makeFilePath(name):
return os.path.join(os.path.dirname(__file__), 'test_document', name)
def makeFileUpload(name):
path = makeFilePath(name)
return FileUpload(path, name)
class TestOOoImportMixin(ERP5TypeTestCase):
gender_base_cat_id = 'gender'
function_base_cat_id = 'function'
def _getTestDataPath(self):
return os.path.join(os.path.dirname(__file__), 'test_document')
def afterSetUp(self):
"""
Initialize the ERP5 site.
......@@ -119,7 +114,7 @@ class TestOOoImport(TestOOoImportMixin):
## Basic steps
##################################
def stepImportRawDataFile(self, sequence=None, sequence_list=None, **kw):
f = makeFileUpload('import_data_list.ods')
f = self.makeFileUpload('import_data_list.ods')
person_module = self.getPortal().person_module
listbox=(
{ 'listbox_key': '001',
......@@ -285,7 +280,7 @@ class TestOOoImport(TestOOoImportMixin):
sorted([organisation_list[i].getEmailText() for i in range(num)]))
def stepImportFileNoMapping(self, sequence=None, sequence_list=None, **kw):
f = makeFileUpload('import_data_list.ods')
f = self.makeFileUpload('import_data_list.ods')
person_module = self.getPortal().person_module
listbox = (
......@@ -302,7 +297,7 @@ class TestOOoImport(TestOOoImportMixin):
'portal_status_message=Please%20Define%20a%20mapping.'))
def stepImportFileWithBlankLine(self, sequence=None, sequence_list=None, **kw):
f = makeFileUpload('import_data_list_blank_line.ods')
f = self.makeFileUpload('import_data_list_blank_line.ods')
person_module = self.getPortal().person_module
listbox=(
{ 'listbox_key': '001',
......@@ -317,7 +312,7 @@ class TestOOoImport(TestOOoImportMixin):
person_module.Base_importFile(import_file=f, listbox=listbox)
def stepImportFileWithCategory(self, sequence=None, sequence_list=None, **kw):
f = makeFileUpload('import_data_with_categories.ods')
f = self.makeFileUpload('import_data_with_categories.ods')
# create some regions
region = self.portal.portal_categories.region
europe = region.newContent(portal_type='Category',
......@@ -343,7 +338,7 @@ class TestOOoImport(TestOOoImportMixin):
person_module.Base_importFile(import_file=f, listbox=listbox)
def stepImportFileWithDates(self, sequence=None, sequence_list=None, **kw):
f = makeFileUpload('import_data_with_dates.ods')
f = self.makeFileUpload('import_data_with_dates.ods')
person_module = self.getPortal().person_module
listbox=(
{ 'listbox_key': '001',
......@@ -362,7 +357,7 @@ class TestOOoImport(TestOOoImportMixin):
This test make sure that either floats (1000,9), sientific numbers (1,00E+003)
or percentage (19%) are correctly imported .
"""
f = makeFileUpload('import_float_and_percentage.ods')
f = self.makeFileUpload('import_float_and_percentage.ods')
currency_module = self.getPortal().currency_module
listbox=(
{ 'listbox_key': '001',
......@@ -373,7 +368,7 @@ class TestOOoImport(TestOOoImportMixin):
currency_module.Base_importFile(import_file=f, listbox=listbox)
def stepImportOrganisation(self, sequence=None, sequence_list=None, **kw):
f = makeFileUpload('import_organisation_list.ods')
f = self.makeFileUpload('import_organisation_list.ods')
organisation_module = self.getPortal().organisation_module
listbox=(
{ 'listbox_key': '001',
......@@ -403,7 +398,7 @@ class TestOOoImport(TestOOoImportMixin):
user = user_folder.getUserById(user_name).__of__(user_folder)
newSecurityManager(None, user)
f = makeFileUpload('import_data_with_categories.ods')
f = self.makeFileUpload('import_data_with_categories.ods')
person_module = self.getPortal().person_module
listbox=(
{ 'listbox_key': '001',
......@@ -420,7 +415,7 @@ class TestOOoImport(TestOOoImportMixin):
person_module.Base_importFile(import_file=f, listbox=listbox)
def stepImportFileWithFreeText(self, sequence=None, sequence_list=None, **kw):
f = makeFileUpload('import_data_with_categories.ods')
f = self.makeFileUpload('import_data_with_categories.ods')
person_module = self.getPortal().person_module
listbox=(
{ 'listbox_key': '001',
......@@ -435,7 +430,7 @@ class TestOOoImport(TestOOoImportMixin):
person_module.Base_importFile(import_file=f, listbox=listbox)
def stepImportFileWithAccentuatedText(self, sequence=None, sequence_list=None, **kw):
f = makeFileUpload('import_data_accentuated_text.ods')
f = self.makeFileUpload('import_data_accentuated_text.ods')
person_module = self.getPortal().person_module
listbox=(
{ 'listbox_key': '001',
......@@ -450,7 +445,7 @@ class TestOOoImport(TestOOoImportMixin):
person_module.Base_importFile(import_file=f, listbox=listbox)
def stepImportXLSFile(self, sequence=None, sequence_list=None, **kw):
f = makeFileUpload('import_data_list.xls')
f = self.makeFileUpload('import_data_list.xls')
person_module = self.getPortal().person_module
listbox=(
{ 'listbox_key': '001',
......@@ -465,7 +460,7 @@ class TestOOoImport(TestOOoImportMixin):
person_module.Base_importFile(import_file=f, listbox=listbox)
def stepImportBigFile_1(self, sequence=None, sequence_list=None, **kw):
f = makeFileUpload('import_data_big_file_1.ods')
f = self.makeFileUpload('import_data_big_file_1.ods')
person_module = self.getPortal().person_module
listbox=(
{ 'listbox_key': '001',
......@@ -480,7 +475,7 @@ class TestOOoImport(TestOOoImportMixin):
person_module.Base_importFile(import_file=f, listbox=listbox)
def stepImportBigFile_2(self, sequence=None, sequence_list=None, **kw):
f = makeFileUpload('import_data_big_file_2.ods')
f = self.makeFileUpload('import_data_big_file_2.ods')
person_module = self.getPortal().person_module
listbox=(
{ 'listbox_key': '001',
......@@ -631,7 +626,7 @@ class TestOOoImport(TestOOoImportMixin):
def test_CategoryTool_importCategoryFile(self):
# tests simple use of CategoryTool_importCategoryFile script
self.portal.portal_categories.CategoryTool_importCategoryFile(
import_file=makeFileUpload('import_region_category.sxc'))
import_file=self.makeFileUpload('import_region_category.sxc'))
self.tic()
region = self.portal.portal_categories.region
self.assertEqual(2, len(region))
......@@ -651,7 +646,7 @@ class TestOOoImport(TestOOoImportMixin):
region.newContent(id='dummy_region')
self.tic()
self.portal.portal_categories.CategoryTool_importCategoryFile(
import_file=makeFileUpload('import_region_category.sxc'),
import_file=self.makeFileUpload('import_region_category.sxc'),
existing_category_list='delete')
self.tic()
self.assertEqual(2, len(region))
......@@ -674,7 +669,7 @@ class TestOOoImport(TestOOoImportMixin):
)
self.tic()
self.portal.portal_categories.CategoryTool_importCategoryFile(
import_file=makeFileUpload('import_region_category.sxc'),
import_file=self.makeFileUpload('import_region_category.sxc'),
existing_category_list='delete')
self.tic()
self.assertEqual(3, len(region))
......@@ -690,7 +685,7 @@ class TestOOoImport(TestOOoImportMixin):
)
self.tic()
self.portal.portal_categories.CategoryTool_importCategoryFile(
import_file=makeFileUpload('import_region_category.sxc'),
import_file=self.makeFileUpload('import_region_category.sxc'),
existing_category_list='force_delete')
self.tic()
self.assertEqual(2, len(region))
......@@ -702,7 +697,7 @@ class TestOOoImport(TestOOoImportMixin):
region.newContent(id='dummy_region')
self.tic()
self.portal.portal_categories.CategoryTool_importCategoryFile(
import_file=makeFileUpload('import_region_category.sxc'),
import_file=self.makeFileUpload('import_region_category.sxc'),
existing_category_list='expire')
self.tic()
self.assertEqual(3, len(region))
......@@ -720,7 +715,7 @@ class TestOOoImport(TestOOoImportMixin):
def test_CategoryTool_importCategoryFileXLS(self):
# tests that CategoryTool_importCategoryFile supports .xls files
self.portal.portal_categories.CategoryTool_importCategoryFile(
import_file=makeFileUpload('import_region_category.xls'))
import_file=self.makeFileUpload('import_region_category.xls'))
self.tic()
region = self.portal.portal_categories.region
self.assertEqual(2, len(region))
......@@ -736,7 +731,7 @@ class TestOOoImport(TestOOoImportMixin):
def test_CategoryTool_importCategoryFile_PathStars(self):
# tests CategoryTool_importCategoryFile with * in the paths columns
self.portal.portal_categories.CategoryTool_importCategoryFile(
import_file=makeFileUpload('import_region_category_path_stars.sxc'))
import_file=self.makeFileUpload('import_region_category_path_stars.sxc'))
self.tic()
region = self.portal.portal_categories.region
self.assertEqual(2, len(region))
......@@ -753,7 +748,7 @@ class TestOOoImport(TestOOoImportMixin):
# tests CategoryTool_importCategoryFile with * in the paths columns, and no
# ID column, and non ascii titles
self.portal.portal_categories.CategoryTool_importCategoryFile(
import_file=makeFileUpload(
import_file=self.makeFileUpload(
'import_region_category_path_stars_non_ascii.sxc'))
self.tic()
region = self.portal.portal_categories.region
......@@ -772,7 +767,7 @@ class TestOOoImport(TestOOoImportMixin):
# categories ID at different level (a good candidate for an acquisition
# bug)
self.portal.portal_categories.CategoryTool_importCategoryFile(
import_file=makeFileUpload('import_region_category_duplicate_ids.sxc'))
import_file=self.makeFileUpload('import_region_category_duplicate_ids.sxc'))
self.tic()
region = self.portal.portal_categories.region
self.assertEqual(1, len(region))
......@@ -786,7 +781,7 @@ class TestOOoImport(TestOOoImportMixin):
def test_Base_getCategoriesSpreadSheetMapping(self):
# test structure returned by Base_getCategoriesSpreadSheetMapping
mapping = self.portal.Base_getCategoriesSpreadSheetMapping(
import_file=makeFileUpload('import_region_category.sxc'))
import_file=self.makeFileUpload('import_region_category.sxc'))
self.assertTrue(isinstance(mapping, dict))
self.assertEqual(['region'], list(mapping.keys()))
region = mapping['region']
......@@ -816,7 +811,7 @@ class TestOOoImport(TestOOoImportMixin):
def test_Base_getCategoriesSpreadSheetMapping_DuplicateIdsAtSameLevel(self):
# tests Base_getCategoriesSpreadSheetMapping when a document contain same
# categories ID at the same level, in that case, a ValueError is raised
import_file = makeFileUpload(
import_file = self.makeFileUpload(
'import_region_category_duplicate_ids_same_level.sxc')
try:
self.portal.portal_categories.Base_getCategoriesSpreadSheetMapping(
......@@ -834,7 +829,7 @@ class TestOOoImport(TestOOoImportMixin):
def on_invalid_spreadsheet(message):
message_list.append(message)
import_file = makeFileUpload(
import_file = self.makeFileUpload(
'import_region_category_duplicate_ids_same_level.sxc')
self.portal.portal_categories.Base_getCategoriesSpreadSheetMapping(import_file,
invalid_spreadsheet_error_handler=on_invalid_spreadsheet)
......@@ -845,7 +840,7 @@ class TestOOoImport(TestOOoImportMixin):
def test_Base_getCategoriesSpreadSheetMapping_WrongHierarchy(self):
# tests Base_getCategoriesSpreadSheetMapping when the spreadsheet has an
# invalid hierarchy (#788)
import_file = makeFileUpload(
import_file = self.makeFileUpload(
'import_region_category_wrong_hierarchy.sxc')
try:
self.portal.portal_categories.Base_getCategoriesSpreadSheetMapping(
......@@ -859,7 +854,7 @@ class TestOOoImport(TestOOoImportMixin):
def test_Base_getCategoriesSpreadSheetMapping_MultiplePaths(self):
# If multiple paths is defined (for instance more than one * in paths
# columns), then it should be an error and the error must be reported
import_file = makeFileUpload(
import_file = self.makeFileUpload(
'import_region_category_multiple_paths.ods')
try:
self.portal.portal_categories.Base_getCategoriesSpreadSheetMapping(
......@@ -871,7 +866,7 @@ class TestOOoImport(TestOOoImportMixin):
def test_Base_getCategoriesSpreadSheetMapping_Id_is_reserved_property_name(self):
# tests Base_getCategoriesSpreadSheetMapping reserved property name are only test for path column, not all.
import_file = makeFileUpload(
import_file = self.makeFileUpload(
'import_region_category_with_reserved_id_in_title.sxc')
mapping = self.portal.portal_categories.Base_getCategoriesSpreadSheetMapping(
import_file=import_file)
......@@ -897,7 +892,7 @@ class TestOOoImport(TestOOoImportMixin):
return True
self.portal.portal_categories.Base_getCategoriesSpreadSheetMapping(
import_file=makeFileUpload('import_category_with_reserved_id_in_id.sxc'),
import_file=self.makeFileUpload('import_category_with_reserved_id_in_id.sxc'),
invalid_spreadsheet_error_handler=on_invalid_spreadsheet)
self.assertEqual(message_set, {
......@@ -916,7 +911,7 @@ class TestOOoImport(TestOOoImportMixin):
"""Test than OOoimport can parse a file with more than 40000 lines
"""
parser = OOoParser()
parser.openFile(open(makeFilePath('import_big_spreadsheet.ods'), 'rb'))
parser.openFile(self.makeFileUpload('import_big_spreadsheet.ods'))
mapping = parser.getSpreadsheetsMapping()
not_ok = 1
for spread, values in six.iteritems(mapping):
......@@ -947,7 +942,7 @@ class TestOOoImportWeb(TestOOoImportMixin):
dummy_expired_region.expire()
self.tic()
self.portal.portal_categories.CategoryTool_importCategoryFile(
import_file=makeFileUpload('import_region_category.sxc'),
import_file=self.makeFileUpload('import_region_category.sxc'),
existing_category_list='expire')
self.tic()
self.assertEqual(4, len(region))
......
......@@ -52,6 +52,7 @@ from Products.ERP5Type.tests.backportUnittest import SetupSiteError
from Products.ERP5Type.tests.utils import addUserToDeveloperRole
from Products.ERP5Type.tests.utils import parseListeningAddress
from Products.ERP5Type.tests.utils import timeZoneContext
from Products.ERP5Type.tests.utils import FileUpload
# Quiet messages when installing business templates
install_bt5_quiet = 0
......@@ -928,6 +929,28 @@ class ERP5TypeTestCaseMixin(ProcessingNodeTestCase, PortalTestCase, functional.F
),
)
def _getTestDataPath(self):
"""
Where the tests data resides, to be overriden in children classes. By
default returns the current directory.
"""
return ''
def makeFileUploadPath(self, filename):
return os.path.join(self._getTestDataPath(), filename)
def makeFileUpload(self, filename, as_filename=None, path=None):
"""
Upload the given `filename` as `as_filename
"""
if as_filename is None:
as_filename = filename
if path is None:
path = self._getTestDataPath()
file_upload = FileUpload(os.path.join(path, filename), as_filename)
self.addCleanup(file_upload.close)
return file_upload
class ERP5TypeCommandLineTestCase(ERP5TypeTestCaseMixin):
__original_ZMySQLDA_connect = None
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment