'Tests CMSPlugin.fix_tree by creating a plugin structure, setting the
position value to Null for all the plugins and then rebuild the tree.
The structure below isn\'t arbitrary, but has been designed to test
various conditions, including:
* nodes four levels deep
* siblings with and without children
1
2
4
10
8
3
9
5
6
7'

def test_plugin_fix_tree(self):

    placeholder = Placeholder(slot=u'some_slot')
    placeholder.save()
    plugin_1 = add_plugin(placeholder, u'TextPlugin', u'en', body=u'01')
    plugin_1 = self.reload(plugin_1)
    plugin_2 = add_plugin(placeholder, u'TextPlugin', u'en', body=u'02', target=plugin_1)
    plugin_1 = self.reload(plugin_1)
    plugin_3 = add_plugin(placeholder, u'TextPlugin', u'en', body=u'03', target=plugin_1)
    plugin_2 = self.reload(plugin_2)
    plugin_4 = add_plugin(placeholder, u'TextPlugin', u'en', body=u'04', target=plugin_2)
    plugin_1 = self.reload(plugin_1)
    plugin_5 = add_plugin(placeholder, u'TextPlugin', u'en', body=u'05')
    left = CMSPlugin.objects.filter(parent__isnull=True).order_by('path')[0]
    plugin_5 = self.reload(plugin_5)
    plugin_5 = plugin_5.move(left, pos='right')
    self.reorder_positions(plugin_5)
    self.reorder_positions(plugin_2)
    plugin_5 = self.reload(plugin_5)
    plugin_6 = add_plugin(placeholder, u'TextPlugin', u'en', body=u'06', target=plugin_5)
    plugin_5 = self.reload(plugin_5)
    plugin_7 = add_plugin(placeholder, u'TextPlugin', u'en', body=u'07', target=plugin_5)
    plugin_2 = self.reload(plugin_2)
    plugin_8 = add_plugin(placeholder, u'TextPlugin', u'en', body=u'08', target=plugin_2)
    plugin_3 = self.reload(plugin_3)
    plugin_9 = add_plugin(placeholder, u'TextPlugin', u'en', body=u'09', target=plugin_3)
    plugin_4 = self.reload(plugin_4)
    plugin_10 = add_plugin(placeholder, u'TextPlugin', u'en', body=u'10', target=plugin_4)
    plugins = CMSPlugin.objects.filter(placeholder=placeholder)
    original_plugin_positions = dict(plugins.order_by('position').values_list('pk', 'position'))
    original_plugin_ids = list(plugins.order_by('position', 'path').values_list('pk', flat=True))
    CMSPlugin.objects.update(position=1)
    CMSPlugin.fix_tree()
    new_plugin_positions = dict(plugins.order_by('position').values_list('pk', 'position'))
    new_plugin_ids = list(plugins.order_by('position', 'path').values_list('pk', flat=True))
    self.assertDictEqual(original_plugin_positions, new_plugin_positions)
    self.assertSequenceEqual(original_plugin_ids, new_plugin_ids)
    reorder_plugins(placeholder, None, u'en', [plugin_5.pk, plugin_1.pk])
    reordered_plugins = list(placeholder.get_plugins().order_by('position', 'path'))
    CMSPlugin.fix_tree()
    new_plugins = list(placeholder.get_plugins().order_by('position', 'path'))
    self.assertSequenceEqual(reordered_plugins, new_plugins, 'Plugin    order    not    preserved    during    fix_tree().')
