# Path: lfc/manage/views.py
# def checkin(request, id):
#     """
#     Checks in the working copy with passed id.
#     """
#     working_copy = lfc.utils.get_content_object(pk=id)
# 
#     if not working_copy.is_working_copy():
#         url = reverse("lfc_manage_object", kwargs={"id": working_copy.id})
#         return MessageHttpResponseRedirect(url, _(u"Object is not a working copy."))
# 
#     working_copy.check_permission(request.user, "checkin")
# 
#     # Save some values of the base for later use.
#     base = working_copy.working_copy_base
#     base_id = base.id
#     base_slug = base.slug
#     base_parent = base.parent
#     base_parent_standard = base.parent.standard if base.parent else None
#     base_state = base.get_content_object().get_state()
#     base_position = base.position
#     base.delete()
# 
#     # Set default values
#     working_copy.slug = base_slug
#     working_copy.set_state(base_state)
#     working_copy.position = base_position
#     working_copy.save()
# 
#     # Point to wc if the base was standard object of the parent
#     if not base_parent:
#         base_parent = lfc.utils.get_portal()
# 
#     if base_parent_standard and (base_parent_standard.id == base_id):
#         base_parent.standard = working_copy.basecontent_ptr
#         base_parent.save()
#         lfc.utils.clear_cache()
# 
#     _update_positions(working_copy.parent)
# 
#     return HttpResponseRedirect(reverse("lfc_manage_object", kwargs={"id": working_copy.id}))
# 
# Path: lfc/manage/views.py
# def checkout(request, id):
#     """
#     Checks out a working copy for the object with the passed id.
# 
#     **Parameters:**
# 
#         id
#             The id of the object for which a working copy is created.
#     """
#     source_obj = lfc.utils.get_content_object(pk=id)
#     source_obj.check_permission(request.user, "checkout")
# 
#     if source_obj.has_working_copy():
#         url = reverse("lfc_manage_object", kwargs={"id": source_obj.id})
#         return MessageHttpResponseRedirect(url, _(u"Object has already a working copy."))
# 
#     new_obj = copy.deepcopy(source_obj)
#     new_obj.pk = None
#     new_obj.id = None
#     new_obj.parent = source_obj.parent
#     new_obj.position = source_obj.position + 1
# 
#     new_obj.slug = _generate_slug(new_obj, source_obj.parent)
# 
#     # Workaround for django-tagging
#     try:
#         new_obj.save()
#     except IntegrityError:
#         pass
# 
#     _copy_images(source_obj, new_obj)
#     _copy_files(source_obj, new_obj)
#     _copy_portlets(source_obj, new_obj)
#     _copy_translations(source_obj, new_obj)
#     _copy_history(source_obj, new_obj)
# 
#     # Prevent recursion
#     if (new_obj not in source_obj.get_descendants()) and (new_obj != source_obj):
#         _copy_descendants(source_obj, new_obj)
# 
#     new_obj.working_copy_base = source_obj
#     new_obj.save()
# 
#     _update_positions(new_obj.parent)
# 
#     return HttpResponseRedirect(reverse("lfc_manage_object", kwargs={"id": new_obj.id}))
# 
# Path: lfc/tests/utils.py
# def create_request(path="/", params=None):
#     """
#     """
#     if params is None:
#         params = {}
# 
#     rf = RequestFactory()
#     request = rf.get(path, params)
#     request.session = SessionStore()
# 
#     user = User()
#     user.is_superuser = True
#     user.save()
#     request.user = user
# 
#     return request


from django.contrib.auth.models import User
from django.core.management import call_command
from django.test import TestCase
from lfc.manage.views import checkin
from lfc.manage.views import checkout
from lfc.tests.utils import create_request
from lfc_page.models import Page
# django imports

# lfc imports

# lfc_page imports


class WorkingCopyTestCase(TestCase):
    """
    Tests related to working copy.
    """
    def setUp(self):
        self.user = User.objects.create(username="admin", is_active=True, is_superuser=True)
        call_command('lfc_init_simple')

        self.p1 = Page.objects.create(title="Page 1", slug="page-1")

    def test_working_cycle(self):

# Path: lfc/manage/views.py
# def checkin(request, id):
#     """
#     Checks in the working copy with passed id.
#     """
#     working_copy = lfc.utils.get_content_object(pk=id)
# 
#     if not working_copy.is_working_copy():
#         url = reverse("lfc_manage_object", kwargs={"id": working_copy.id})
#         return MessageHttpResponseRedirect(url, _(u"Object is not a working copy."))
# 
#     working_copy.check_permission(request.user, "checkin")
# 
#     # Save some values of the base for later use.
#     base = working_copy.working_copy_base
#     base_id = base.id
#     base_slug = base.slug
#     base_parent = base.parent
#     base_parent_standard = base.parent.standard if base.parent else None
#     base_state = base.get_content_object().get_state()
#     base_position = base.position
#     base.delete()
# 
#     # Set default values
#     working_copy.slug = base_slug
#     working_copy.set_state(base_state)
#     working_copy.position = base_position
#     working_copy.save()
# 
#     # Point to wc if the base was standard object of the parent
#     if not base_parent:
#         base_parent = lfc.utils.get_portal()
# 
#     if base_parent_standard and (base_parent_standard.id == base_id):
#         base_parent.standard = working_copy.basecontent_ptr
#         base_parent.save()
#         lfc.utils.clear_cache()
# 
#     _update_positions(working_copy.parent)
# 
#     return HttpResponseRedirect(reverse("lfc_manage_object", kwargs={"id": working_copy.id}))
# 
# Path: lfc/manage/views.py
# def checkout(request, id):
#     """
#     Checks out a working copy for the object with the passed id.
# 
#     **Parameters:**
# 
#         id
#             The id of the object for which a working copy is created.
#     """
#     source_obj = lfc.utils.get_content_object(pk=id)
#     source_obj.check_permission(request.user, "checkout")
# 
#     if source_obj.has_working_copy():
#         url = reverse("lfc_manage_object", kwargs={"id": source_obj.id})
#         return MessageHttpResponseRedirect(url, _(u"Object has already a working copy."))
# 
#     new_obj = copy.deepcopy(source_obj)
#     new_obj.pk = None
#     new_obj.id = None
#     new_obj.parent = source_obj.parent
#     new_obj.position = source_obj.position + 1
# 
#     new_obj.slug = _generate_slug(new_obj, source_obj.parent)
# 
#     # Workaround for django-tagging
#     try:
#         new_obj.save()
#     except IntegrityError:
#         pass
# 
#     _copy_images(source_obj, new_obj)
#     _copy_files(source_obj, new_obj)
#     _copy_portlets(source_obj, new_obj)
#     _copy_translations(source_obj, new_obj)
#     _copy_history(source_obj, new_obj)
# 
#     # Prevent recursion
#     if (new_obj not in source_obj.get_descendants()) and (new_obj != source_obj):
#         _copy_descendants(source_obj, new_obj)
# 
#     new_obj.working_copy_base = source_obj
#     new_obj.save()
# 
#     _update_positions(new_obj.parent)
# 
#     return HttpResponseRedirect(reverse("lfc_manage_object", kwargs={"id": new_obj.id}))
# 
# Path: lfc/tests/utils.py
# def create_request(path="/", params=None):
#     """
#     """
#     if params is None:
#         params = {}
# 
#     rf = RequestFactory()
#     request = rf.get(path, params)
#     request.session = SessionStore()
# 
#     user = User()
#     user.is_superuser = True
#     user.save()
#     request.user = user
# 
#     return request


# Path: lfc/tests/working_copy_tests.py
from django.contrib.auth.models import User
from django.core.management import call_command
from django.test import TestCase
from lfc.manage.views import checkin
from lfc.manage.views import checkout
from lfc.tests.utils import create_request
from lfc_page.models import Page

# django imports

# lfc imports

# lfc_page imports


class WorkingCopyTestCase(TestCase):
    """
    Tests related to working copy.
    """
    def setUp(self):
        self.user = User.objects.create(username="admin", is_active=True, is_superuser=True)
        call_command('lfc_init_simple')

        self.p1 = Page.objects.create(title="Page 1", slug="page-1")

    def test_working_cycle(self):

        request = create_request()