Deletion Policies
=================

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

Manual Deletion Policy
----------------------

Using a manual deletion policy means:

  - When the Managed File is deleted from Plone, a reference is
    created on the trashcan

  - When the reference is deleted from the trashcan, the file is *not*
    removed from the filesystem.

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

Delete the file:

  >>> self.portal.manage_delObjects(ids=['file1'])

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

File should still exist:

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

If we delete the file from the trashcan, the file on the filesystem
should still exist:

  >>> cur_trash.manage_delObjects(ids=cur_trash.objectIds())
  >>> os.path.exists('%s/dir1/file1.txt' % tmp_dir)
  True

Temporary Document Policy
-------------------------

Using a temporary document policy means:

  - When the Manage File is *created* a reference is created on the
    trashcan pointing to the Managed File inside Plone

  - When the Managed File is deleted from Plone, *no* reference is
    created on the trashcan

  - When the trashcan is called to purge temporary documents, the
    reference is looked up and deletes the Managed File inside Plone

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 have added a file reference to the trash:

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

File should still exist:

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

If we delete the file from the trashcan, both the file inside Plone
and the file in the filesystem should be gone:

  >>> cur_trash.manage_delObjects(ids=cur_trash.objectIds())

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

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

Deferred Deletion Policy
------------------------

When a deferred delete policy is used, the following happens:

  - Managed File is deleted from Plone, a reference to the file is
    created on the trashcan

  - When the file is deleted from trashcan, the referenced file is
    removed from the filesystem

Create a new Managed File with deferred delete policy:

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

Check that the file exists:

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

Check the deletion policy:

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

Delete the file:

  >>> self.portal.manage_delObjects(ids=['file2'])

File should still be around:

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

Should have added an orphaned file:

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

If we delete the file from the trashcan, the file on the filesystem
should be gone as well:

  >>> cur_trash.manage_delObjects(ids=cur_trash.objectIds())
  >>> os.path.exists('%s/dir1/file2.txt' % tmp_dir)
  False

Immediate Deletion Policy
-------------------------

When a immediate delete policy is used, the following happens:

  - Managed file is deleted from Plone

  - The referenced file is immediately removed from the filesystem

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

Delete the file:

  >>> self.portal.manage_delObjects(ids=['file3'])

Trash count should still be the same (file shouldn't end in the trash):

  >>> len(cur_trash.objectIds())
  0

File should be immediately gone:

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

Cleanup the temp directory:

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