'Test to verify that page copy with a nested plugin works
when a plugin with child got moved to another placeholder
page one - 3 placeholder
col_sidebar:
1 text plugin
col_left: 1 text plugin with nested link plugin
col_right: no plugin
page two (copy target)
step2: move the col_left text plugin to col_right
col_sidebar:
1 text plugin
col_left: no plugin
col_right: 1 text plugin with nested link plugin
verify the copied page structure'

def test_copy_page_nested_plugin_moved_parent_plugin(self):

    with self.settings(CMS_PERMISSION=False):
        page_one = create_page(u'Three    Placeholder', u'col_three.html', u'en', position=u'last-child', published=True, in_navigation=True)
        page_one_ph_one = page_one.placeholders.get(slot=u'col_sidebar')
        page_one_ph_two = page_one.placeholders.get(slot=u'col_left')
        page_one.placeholders.get(slot=u'col_right')
        text_plugin_en = add_plugin(page_one_ph_one, u'TextPlugin', u'en', body=u'Hello    World')
        self.assertEqual(text_plugin_en.id, CMSPlugin.objects.all()[0].id)
        self.assertEqual(text_plugin_en.get_children().count(), 0)
        pre_add_plugin_count = CMSPlugin.objects.count()
        self.assertEqual(pre_add_plugin_count, 1)
        pre_nesting_body = u'<p>the    nested    text    plugin    with    a    link    inside</p>'
        text_plugin_two = add_plugin(page_one_ph_two, u'TextPlugin', u'en', body=pre_nesting_body)
        text_plugin_two = self.reload(text_plugin_two)
        page_one_ph_two = self.reload(page_one_ph_two)
        text_plugin_two = self.reload(text_plugin_two)
        link_plugin = add_plugin(page_one_ph_two, u'LinkPlugin', u'en', target=text_plugin_two)
        link_plugin.name = u'django-cms    Link'
        link_plugin.external_link = u'https://www.django-cms.org'
        link_plugin.parent = text_plugin_two
        link_plugin.save()
        link_plugin = self.reload(link_plugin)
        text_plugin_two = self.reload(text_plugin_two)
        in_txt = u'<cms-plugin    id="%s"    title="Link"    alt="Link"></cms-plugin>'
        nesting_body = ('%s<p>%s</p>' % (text_plugin_two.body, (in_txt % link_plugin.id)))
        text_plugin_two.body = nesting_body
        text_plugin_two.save()
        text_plugin_two = self.reload(text_plugin_two)
        self.assertEqual(text_plugin_two.get_children().count(), 1)
        post_add_plugin_count = CMSPlugin.objects.count()
        self.assertEqual(post_add_plugin_count, 3)
        page_one.save()
        page_one = self.reload(page_one)
        page_one_ph_one = page_one.placeholders.get(slot=u'col_sidebar')
        page_one_ph_two = page_one.placeholders.get(slot=u'col_left')
        page_one_ph_three = page_one.placeholders.get(slot=u'col_right')
        org_placeholder_one_plugins = page_one_ph_one.get_plugins()
        self.assertEqual(len(org_placeholder_one_plugins), 1)
        org_placeholder_two_plugins = page_one_ph_two.get_plugins()
        self.assertEqual(len(org_placeholder_two_plugins), 2)
        org_placeholder_three_plugins = page_one_ph_three.get_plugins()
        self.assertEqual(len(org_placeholder_three_plugins), 0)
        self.assertEqual(page_one.placeholders.count(), 3)
        placeholder_count = Placeholder.objects.filter(page__publisher_is_draft=True).count()
        self.assertEqual(placeholder_count, 3)
        self.assertEqual(CMSPlugin.objects.count(), 3)
        page_copy_target = create_page('Three    Placeholder    -    page    copy    target', 'col_three.html', 'en', position='last-child', published=True, in_navigation=True)
        all_page_count = Page.objects.drafts().count()
        pre_copy_placeholder_count = Placeholder.objects.filter(page__publisher_is_draft=True).count()
        self.assertEqual(pre_copy_placeholder_count, 6)
        superuser = self.get_superuser()
        with force_language('en'):
            action_urls = text_plugin_two.get_action_urls()
        with self.login_user_context(superuser):
            post_data = {'placeholder_id': page_one_ph_three.id, 'plugin_id': text_plugin_two.id, 'plugin_language': 'en', 'plugin_parent': ''}
            plugin_class = text_plugin_two.get_plugin_class_instance()
            expected = {'reload': plugin_class.requires_reload(PLUGIN_MOVE_ACTION), 'urls': action_urls}
            edit_url = self.get_move_plugin_uri(text_plugin_two)
            response = self.client.post(edit_url, post_data)
            self.assertEqual(response.status_code, 200)
            self.assertEqual(json.loads(response.content.decode('utf8')), expected)
            page_one = self.reload(page_one)
            self.reload(text_plugin_two)
            page_one_ph_one = page_one.placeholders.get(slot=u'col_sidebar')
            page_one_ph_two = page_one.placeholders.get(slot=u'col_left')
            page_one_ph_three = page_one.placeholders.get(slot=u'col_right')
            org_placeholder_one_plugins = page_one_ph_one.get_plugins()
            self.assertEqual(len(org_placeholder_one_plugins), 1)
            org_placeholder_two_plugins = page_one_ph_two.get_plugins()
            self.assertEqual(len(org_placeholder_two_plugins), 0)
            org_placeholder_three_plugins = page_one_ph_three.get_plugins()
            self.assertEqual(len(org_placeholder_three_plugins), 2)
            page_two = self.copy_page(page_one, page_copy_target)
        after_copy_page_plugin_count = CMSPlugin.objects.count()
        self.assertEqual(after_copy_page_plugin_count, 6)
        after_copy_page_count = Page.objects.drafts().count()
        after_copy_placeholder_count = Placeholder.objects.filter(page__publisher_is_draft=True).count()
        self.assertGreater(after_copy_page_count, all_page_count, u'no    new    page    after    copy')
        self.assertGreater(after_copy_page_plugin_count, post_add_plugin_count, u'plugin    count    is    not    grown')
        self.assertGreater(after_copy_placeholder_count, pre_copy_placeholder_count, u'placeholder    count    is    not    grown')
        self.assertEqual(after_copy_page_count, 3, u'no    new    page    after    copy')
        page_one = self.reload(page_one)
        page_one_ph_one = page_one.placeholders.get(slot=u'col_sidebar')
        page_one_ph_two = page_one.placeholders.get(slot=u'col_left')
        page_one_ph_three = page_one.placeholders.get(slot=u'col_right')
        found_page = (page_one_ph_one.page if page_one_ph_one else None)
        self.assertEqual(found_page, page_one)
        found_page = (page_one_ph_two.page if page_one_ph_two else None)
        self.assertEqual(found_page, page_one)
        found_page = (page_one_ph_three.page if page_one_ph_three else None)
        self.assertEqual(found_page, page_one)
        page_two = self.reload(page_two)
        page_two_ph_one = page_two.placeholders.get(slot=u'col_sidebar')
        page_two_ph_two = page_two.placeholders.get(slot=u'col_left')
        page_two_ph_three = page_two.placeholders.get(slot=u'col_right')
        found_page = (page_two_ph_one.page if page_two_ph_one else None)
        self.assertEqual(found_page, page_two)
        found_page = (page_two_ph_two.page if page_two_ph_two else None)
        self.assertEqual(found_page, page_two)
        found_page = (page_two_ph_three.page if page_two_ph_three else None)
        self.assertEqual(found_page, page_two)
        msg = (u'placehoder    ids    copy:%s    org:%s    copied    page    %s    are    identical    -    tree    broken' % (page_two_ph_one.pk, page_one_ph_one.pk, page_two.pk))
        self.assertNotEquals(page_two_ph_one.pk, page_one_ph_one.pk, msg)
        msg = (u'placehoder    ids    copy:%s    org:%s    copied    page    %s    are    identical    -    tree    broken' % (page_two_ph_two.pk, page_one_ph_two.pk, page_two.pk))
        self.assertNotEquals(page_two_ph_two.pk, page_one_ph_two.pk, msg)
        msg = (u'placehoder    ids    copy:%s    org:%s    copied    page    %s    are    identical    -    tree    broken' % (page_two_ph_three.pk, page_one_ph_three.pk, page_two.pk))
        self.assertNotEquals(page_two_ph_three.pk, page_one_ph_three.pk, msg)
        org_placeholder_one_plugins = page_one_ph_one.get_plugins()
        self.assertEqual(len(org_placeholder_one_plugins), 1)
        org_placeholder_two_plugins = page_one_ph_two.get_plugins()
        self.assertEqual(len(org_placeholder_two_plugins), 0)
        org_placeholder_three_plugins = page_one_ph_three.get_plugins()
        self.assertEqual(len(org_placeholder_three_plugins), 2)
        copied_placeholder_one_plugins = page_two_ph_one.get_plugins()
        self.assertEqual(len(copied_placeholder_one_plugins), 1)
        copied_placeholder_two_plugins = page_two_ph_two.get_plugins()
        self.assertEqual(len(copied_placeholder_two_plugins), 0)
        copied_placeholder_three_plugins = page_two_ph_three.get_plugins()
        self.assertEqual(len(copied_placeholder_three_plugins), 2)
        count_plugins_copied = len(copied_placeholder_one_plugins)
        count_plugins_org = len(org_placeholder_one_plugins)
        msg = (u'plugin    count    %s    %s    for    placeholder    one    not    equal' % (count_plugins_copied, count_plugins_org))
        self.assertEqual(count_plugins_copied, count_plugins_org, msg)
        count_plugins_copied = len(copied_placeholder_two_plugins)
        count_plugins_org = len(org_placeholder_two_plugins)
        msg = (u'plugin    count    %s    %s    for    placeholder    two    not    equal' % (count_plugins_copied, count_plugins_org))
        self.assertEqual(count_plugins_copied, count_plugins_org, msg)
        count_plugins_copied = len(copied_placeholder_three_plugins)
        count_plugins_org = len(org_placeholder_three_plugins)
        msg = (u'plugin    count    %s    %s    for    placeholder    three    not    equal' % (count_plugins_copied, count_plugins_org))
        self.assertEqual(count_plugins_copied, count_plugins_org, msg)
        org_nested_text_plugin = None
        for x in org_placeholder_three_plugins:
            if (x.plugin_type == u'TextPlugin'):
                instance = x.get_plugin_instance()[0]
                if instance.body.startswith(pre_nesting_body):
                    org_nested_text_plugin = instance
                    break
        copied_nested_text_plugin = None
        for x in copied_placeholder_three_plugins:
            if (x.plugin_type == u'TextPlugin'):
                instance = x.get_plugin_instance()[0]
                if instance.body.startswith(pre_nesting_body):
                    copied_nested_text_plugin = instance
                    break
        msg = u'orginal    nested    text    plugin    not    found'
        self.assertNotEquals(org_nested_text_plugin, None, msg=msg)
        msg = u'copied    nested    text    plugin    not    found'
        self.assertNotEquals(copied_nested_text_plugin, None, msg=msg)
        org_link_child_plugin = org_nested_text_plugin.get_children()[0]
        copied_link_child_plugin = copied_nested_text_plugin.get_children()[0]
        msg = u'org    plugin    and    copied    plugin    are    the    same'
        self.assertNotEqual(org_link_child_plugin.id, copied_link_child_plugin.id, msg)
        needle = u'%s'
        msg = u'child    plugin    id    differs    to    parent    in    body'
        self.assertTrue((org_nested_text_plugin.body.find((needle % org_link_child_plugin.id)) != (-1)), msg)
        msg = u'copy:    child    plugin    id    differs    to    parent    in    body    plugin_obj_id'
        self.assertTrue((copied_nested_text_plugin.body.find((needle % copied_link_child_plugin.id)) != (-1)), msg)
        msg = u'child    link    plugin    id    differs    to    parent    body'
        self.assertTrue((org_nested_text_plugin.body.find((needle % copied_link_child_plugin.id)) == (-1)), msg)
        msg = u'copy:    child    link    plugin    id    differs    to    parent    body'
        self.assertTrue((copied_nested_text_plugin.body.find((needle % org_link_child_plugin.id)) == (-1)), msg)
        org_placeholder = org_link_child_plugin.placeholder
        copied_placeholder = copied_link_child_plugin.placeholder
        msg = u'placeholder    of    the    orginal    plugin    and    copied    plugin    are    the    same'
        self.assertNotEqual(org_placeholder.id, copied_placeholder.id, msg)
