Rename Tests
============

Basically, we want to make sure that renaming a Managed File doesn't
create references in the trashcan and that renaming a TEMP_DOCUMENT
works. We will have a rename test for each of the deletion policies,
so this doctest is very similar to the deletion doctest.

Give test user the Manager role and login:

  >>> from Testing.ZopeTestCase import user_name, user_password, folder_name
  >>> self.setRoles(['Manager'])
  >>> self.login(user_name)
  >>> request = self.portal.REQUEST

Create some temporary files, to make sure we have something to point to:

  >>> import os
  >>> import tempfile
  >>> tmp_dir = tempfile.mkdtemp()
  >>> for i in range(0, 2):
  ...   os.mkdir('%s/dir%s' % (tmp_dir, i))
  ...   for j in range(1, 4):
  ...     _  = open('%s/dir%s/file%s.txt' % (tmp_dir, i, j), 'wb')

Set the default repository path to point to the tmp_dir:

  >>> from Products.CMFManagedFile.config import *
  >>> self.tool.getRepository('default').edit(path=tmp_dir,
  ...                                         path_policy=FS_BASE)

Should have no orphaned files:

  >>> len(self.tool.orphanedFiles.objectIds())
  0

Rename under Manual Deletion Policy
-----------------------------------

Create a Managed File pointing to one of the created files:

  >>> obj = self.makeOne(id='file1', target_filepath='dir1/file1.txt')

Check the delete policy:

  >>> from Products.CMFManagedFile.config import *
  >>> obj.getProperty('deletePolicy') == DELETE_MANUAL
  True

Commit a subtransaction and rename the file:

  >>> get_transaction().commit(1)
  >>> self.portal.manage_renameObject('file1', 'new_file1')

Should not touch the trashcan::

  >>> len(self.tool.orphanedFiles.objectIds())
  0

File should still exist:

  >>> os.path.exists('%s/dir1/file1.txt' % tmp_dir)
  True

Rename using Deferred Deletion Policy
-------------------------------------

Check that the file exists:

  >>> os.path.exists('%s/dir1/file2.txt' % tmp_dir)
  True

Create a new Managed File with deferred delete policy:

  >>> obj = self.makeOne(id='file2', target_filepath='dir1/file2.txt',
  ...                    delete_policy=DELETE_DEFERRED)

Check the deletion policy:

  >>> obj.getProperty('deletePolicy') == DELETE_DEFERRED
  True

Commit a subtransaction and rename the file:

  >>> get_transaction().commit(1)
  >>> self.portal.manage_renameObject('file2', 'new_file2')

Should not touch the trashcan::

  >>> len(self.tool.orphanedFiles.objectIds())
  0

File should still be around:

  >>> os.path.exists('%s/dir1/file2.txt' % tmp_dir)
  True

Rename using Immediate Deletion Policy
--------------------------------------

Check that the file exists:

  >>> os.path.exists('%s/dir1/file3.txt' % tmp_dir)
  True

Create a new Managed File with immediate delete policy:

  >>> obj = self.makeOne(id='file3', target_filepath='dir1/file3.txt',
  ...                    delete_policy=DELETE_IMMEDIATE)

Check the deletion policy:

  >>> obj.getProperty('deletePolicy') == DELETE_IMMEDIATE
  True

Commit a subtransaction and rename the file:

  >>> get_transaction().commit(1)
  >>> self.portal.manage_renameObject('file3', 'new_file3')

Should not touch the trashcan::

  >>> len(self.tool.orphanedFiles.objectIds())
  0

File should still exist:

  >>> os.path.exists('%s/dir1/file3.txt' % tmp_dir)
  True

Rename using the Temp Document Policy
-------------------------------------

Create a Managed File pointing to one of the created files:

  >>> obj = self.makeOne(id='file1a', target_filepath='dir1/file1.txt',
  ...                    delete_policy=TEMP_DOCUMENT)
  >>> 'file1a' in self.portal.objectIds()
  True

Check the delete policy:

  >>> from Products.CMFManagedFile.config import *
  >>> obj.getProperty('deletePolicy') == TEMP_DOCUMENT
  True

Should create a current trashcan, and add a file reference to the
trash:

  >>> len(self.tool.orphanedFiles.objectIds())
  1

  >>> cur_trash = self.tool.orphanedFiles.objectValues()[0]
  >>> len(cur_trash.objectIds())
  1

Commit a subtransaction and rename the file:

  >>> get_transaction().commit(1)
  >>> self.portal.manage_renameObject('file1a', 'new_file1a')
  Traceback (most recent call last):
  ...
  Copy Error:...Cannot copy or rename temporary documents...

  >>> 'file1a' in self.portal.objectIds()
  True

  >>> 'new_file1a' in self.portal.objectIds()
  False

Should have the same amout of references inside the trashcan:

  >>> len(cur_trash.objectIds())
  1

File should still exist:

  >>> os.path.exists('%s/dir1/file1.txt' % tmp_dir)
  True

If we rename the reference inside trashcan, both the file inside Plone
and the file in the filesystem should still exist:

  >>> trash_id = cur_trash.objectIds()[0]
  >>> cur_trash.manage_renameObject(trash_id, 'trashy')
  >>> trashy = cur_trash._getOb('trashy')

  >>> os.path.exists('%s/dir1/file1.txt' % tmp_dir)
  True

If we delete the reference, it should still find the renamed object
and be able to delete it and the file on the filesystem:

  >>> cur_trash.manage_delObjects(ids=['trashy'])

  >>> 'file1a' in self.portal.objectIds()
  False

  >>> os.path.exists('%s/dir1/file1.txt' % tmp_dir)
  False

Cleanup the temp directory:

  >>> import shutil
  >>> shutil.rmtree(tmp_dir)
