<s> import json <EOL> from django . contrib . admin . sites import site <EOL> from django . template import Context <EOL> from django . template . base import Template <EOL> from django . utils . encoding import force_text <EOL> from cms . api import add_plugin <EOL> from cms . constants import PLUGIN_MOVE_ACTION , PLUGIN_COPY_ACTION <EOL> from cms . models import StaticPlaceholder , Placeholder , CMSPlugin <EOL> from cms . tests . test_plugins import PluginsTestBaseCase <EOL> from cms . utils . i18n import force_language <EOL> from cms . utils . urlutils import admin_reverse <EOL> class StaticPlaceholderTestCase ( PluginsTestBaseCase ) : <EOL> @ property <EOL> def admin_class ( self ) : <EOL> return site . _registry [ StaticPlaceholder ] <EOL> def fill_placeholder ( self , placeholder = None ) : <EOL> if placeholder is None : <EOL> placeholder = Placeholder ( slot = u"<STR_LIT>" ) <EOL> placeholder . save ( ) <EOL> plugin_1 = add_plugin ( placeholder , u"<STR_LIT>" , u"<STR_LIT>" , <EOL> body = u"<STR_LIT>" , <EOL> ) <EOL> plugin_1 . save ( ) <EOL> plugin_2 = add_plugin ( placeholder , u"<STR_LIT>" , u"<STR_LIT>" , <EOL> body = u"<STR_LIT>" , <EOL> ) <EOL> plugin_1 = self . reload ( plugin_1 ) <EOL> plugin_2 . parent = plugin_1 <EOL> plugin_2 . save ( ) <EOL> return placeholder <EOL> def get_admin ( self ) : <EOL> usr = self . _create_user ( "<STR_LIT>" , True , True ) <EOL> return usr <EOL> def test_template_creation ( self ) : <EOL> self . assertObjectDoesNotExist ( StaticPlaceholder . objects . all ( ) , code = '<STR_LIT>' ) <EOL> self . assertObjectDoesNotExist ( Placeholder . objects . all ( ) , slot = '<STR_LIT>' ) <EOL> t = Template ( '<STR_LIT>' ) <EOL> t . render ( self . get_context ( '<STR_LIT:/>' ) ) <EOL> self . assertObjectExist ( StaticPlaceholder . objects . all ( ) , code = '<STR_LIT>' , <EOL> creation_method = StaticPlaceholder . CREATION_BY_TEMPLATE ) <EOL> self . assertEqual ( Placeholder . objects . filter ( slot = '<STR_LIT>' ) . count ( ) , <NUM_LIT:2> ) <EOL> def test_empty ( self ) : <EOL> self . assertObjectDoesNotExist ( StaticPlaceholder . objects . all ( ) , code = '<STR_LIT>' ) <EOL> self . assertObjectDoesNotExist ( Placeholder . objects . all ( ) , slot = '<STR_LIT>' ) <EOL> t = Template ( '<STR_LIT>' ) <EOL> rendered = t . render ( self . get_context ( '<STR_LIT:/>' ) ) <EOL> self . assertIn ( "<STR_LIT>" , rendered ) <EOL> t = Template ( '<STR_LIT>' ) <EOL> rendered = t . render ( self . get_context ( '<STR_LIT:/>' ) ) <EOL> self . assertEqual ( "<STR_LIT>" , rendered ) <EOL> t = Template ( '<STR_LIT>' ) <EOL> rendered = t . render ( Context ( { '<STR_LIT:code>' : StaticPlaceholder . objects . all ( ) [ <NUM_LIT:0> ] } ) ) <EOL> self . assertIn ( "<STR_LIT>" , rendered ) <EOL> for p in Placeholder . objects . all ( ) : <EOL> add_plugin ( p , '<STR_LIT>' , '<STR_LIT>' , body = '<STR_LIT:test>' ) <EOL> t = Template ( '<STR_LIT>' ) <EOL> rendered = t . render ( self . get_context ( '<STR_LIT:/>' ) ) <EOL> self . assertNotIn ( "<STR_LIT>" , rendered ) <EOL> self . assertEqual ( StaticPlaceholder . objects . filter ( site_id__isnull = True , code = '<STR_LIT>' ) . count ( ) , <NUM_LIT:1> ) <EOL> def test_local ( self ) : <EOL> self . assertObjectDoesNotExist ( StaticPlaceholder . objects . all ( ) , code = '<STR_LIT>' ) <EOL> self . assertObjectDoesNotExist ( Placeholder . objects . all ( ) , slot = '<STR_LIT>' ) <EOL> t = Template ( '<STR_LIT>' ) <EOL> rendered = t . render ( self . get_context ( '<STR_LIT:/>' ) ) <EOL> self . assertIn ( "<STR_LIT>" , rendered ) <EOL> for p in Placeholder . objects . all ( ) : <EOL> add_plugin ( p , '<STR_LIT>' , '<STR_LIT>' , body = '<STR_LIT:test>' ) <EOL> rendered = t . render ( self . get_context ( '<STR_LIT:/>' ) ) <EOL> self . assertNotIn ( "<STR_LIT>" , rendered ) <EOL> self . assertEqual ( StaticPlaceholder . objects . filter ( site_id__isnull = False , code = '<STR_LIT>' ) . count ( ) , <NUM_LIT:1> ) <EOL> def test_publish_stack ( self ) : <EOL> static_placeholder = StaticPlaceholder . objects . create ( name = '<STR_LIT:foo>' , code = '<STR_LIT:bar>' , site_id = <NUM_LIT:1> ) <EOL> self . fill_placeholder ( static_placeholder . draft ) <EOL> static_placeholder . dirty = True <EOL> static_placeholder . save ( ) <EOL> self . assertEqual ( static_placeholder . draft . cmsplugin_set . all ( ) . count ( ) , <NUM_LIT:2> ) <EOL> self . assertEqual ( static_placeholder . public . cmsplugin_set . all ( ) . count ( ) , <NUM_LIT:0> ) <EOL> with self . login_user_context ( self . get_superuser ( ) ) : <EOL> response = self . client . post ( '<STR_LIT>' % ( admin_reverse ( "<STR_LIT>" , args = [ <NUM_LIT:1> , '<STR_LIT>' ] ) , static_placeholder . pk ) ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT> ) <EOL> def test_permissions ( self ) : <EOL> static_placeholder = StaticPlaceholder . objects . create ( name = '<STR_LIT:foo>' , code = '<STR_LIT:bar>' , site_id = <NUM_LIT:1> ) <EOL> request = self . get_request ( ) <EOL> request . user = self . _create_user ( '<STR_LIT>' , is_staff = True , is_superuser = False , permissions = [ '<STR_LIT>' ] ) <EOL> self . assertTrue ( static_placeholder . has_change_permission ( request ) ) <EOL> self . assertFalse ( static_placeholder . has_publish_permission ( request ) ) <EOL> request . user = self . _create_user ( '<STR_LIT>' , is_staff = True , is_superuser = False , permissions = [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> self . assertTrue ( static_placeholder . has_change_permission ( request ) ) <EOL> self . assertTrue ( static_placeholder . has_publish_permission ( request ) ) <EOL> request . user = self . get_superuser ( ) <EOL> self . assertTrue ( static_placeholder . has_change_permission ( request ) ) <EOL> self . assertTrue ( static_placeholder . has_publish_permission ( request ) ) <EOL> def test_move_plugin ( self ) : <EOL> static_placeholder_source = StaticPlaceholder . objects . create ( name = '<STR_LIT>' , code = '<STR_LIT>' , site_id = <NUM_LIT:1> ) <EOL> static_placeholder_target = StaticPlaceholder . objects . create ( name = '<STR_LIT>' , code = '<STR_LIT>' , site_id = <NUM_LIT:1> ) <EOL> sourceplugin = add_plugin ( static_placeholder_source . draft , '<STR_LIT>' , '<STR_LIT>' , body = '<STR_LIT:test>' ) <EOL> plugin_class = sourceplugin . get_plugin_class_instance ( ) <EOL> with force_language ( '<STR_LIT>' ) : <EOL> action_urls = sourceplugin . get_action_urls ( ) <EOL> expected = { <EOL> '<STR_LIT>' : plugin_class . requires_reload ( PLUGIN_MOVE_ACTION ) , <EOL> '<STR_LIT>' : action_urls , <EOL> } <EOL> admin = self . get_admin ( ) <EOL> with self . login_user_context ( admin ) : <EOL> request = self . get_request ( post_data = { '<STR_LIT>' : sourceplugin . pk , <EOL> '<STR_LIT>' : static_placeholder_target . draft . id , <EOL> '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } ) <EOL> response = self . admin_class . move_plugin ( request ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> self . assertEqual ( json . loads ( response . content . decode ( '<STR_LIT:utf8>' ) ) , expected ) <EOL> source = StaticPlaceholder . objects . get ( pk = static_placeholder_source . pk ) <EOL> target = StaticPlaceholder . objects . get ( pk = static_placeholder_target . pk ) <EOL> self . assertTrue ( source . dirty ) <EOL> self . assertTrue ( target . dirty ) <EOL> def test_copy_plugin ( self ) : <EOL> static_placeholder_source = StaticPlaceholder . objects . create ( name = '<STR_LIT>' , code = '<STR_LIT>' , site_id = <NUM_LIT:1> ) <EOL> static_placeholder_target = StaticPlaceholder . objects . create ( name = '<STR_LIT>' , code = '<STR_LIT>' , site_id = <NUM_LIT:1> ) <EOL> sourceplugin = add_plugin ( static_placeholder_source . draft , '<STR_LIT>' , '<STR_LIT>' , body = '<STR_LIT>' ) <EOL> targetplugin = add_plugin ( static_placeholder_target . draft , '<STR_LIT>' , '<STR_LIT>' , body = '<STR_LIT>' ) <EOL> StaticPlaceholder . objects . filter ( pk = static_placeholder_source . pk ) . update ( dirty = False ) <EOL> plugin_class = sourceplugin . get_plugin_class_instance ( ) <EOL> admin = self . get_admin ( ) <EOL> with self . login_user_context ( admin ) : <EOL> request = self . get_request ( post_data = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : static_placeholder_source . draft . pk , <EOL> '<STR_LIT>' : sourceplugin . pk , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : static_placeholder_target . draft . pk , <EOL> '<STR_LIT>' : targetplugin . pk , <EOL> } ) <EOL> response = self . admin_class . copy_plugins ( request ) <EOL> plugin_list = CMSPlugin . objects . filter ( <EOL> language = '<STR_LIT>' , placeholder_id = static_placeholder_target . draft . pk ) . order_by ( <EOL> '<STR_LIT>' , '<STR_LIT>' ) <EOL> reduced_list = [ ] <EOL> for plugin in plugin_list : <EOL> reduced_list . append ( <EOL> { <EOL> '<STR_LIT:id>' : plugin . pk , '<STR_LIT:type>' : plugin . plugin_type , '<STR_LIT>' : plugin . parent_id , <EOL> '<STR_LIT>' : plugin . position , '<STR_LIT>' : force_text ( plugin . get_short_description ( ) ) , <EOL> '<STR_LIT>' : plugin . language , '<STR_LIT>' : static_placeholder_target . draft . pk <EOL> } <EOL> ) <EOL> expected = json . loads ( <EOL> json . dumps ( { '<STR_LIT>' : reduced_list , '<STR_LIT>' : plugin_class . requires_reload ( PLUGIN_COPY_ACTION ) } ) ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> self . assertEqual ( json . loads ( response . content . decode ( '<STR_LIT:utf8>' ) ) , expected ) <EOL> source = StaticPlaceholder . objects . get ( pk = static_placeholder_source . pk ) <EOL> target = StaticPlaceholder . objects . get ( pk = static_placeholder_target . pk ) <EOL> self . assertFalse ( source . dirty ) <EOL> self . assertTrue ( target . dirty ) <EOL> def test_create_by_admin ( self ) : <EOL> url = admin_reverse ( "<STR_LIT>" ) <EOL> with self . login_user_context ( self . get_superuser ( ) ) : <EOL> response = self . client . post ( url , data = { '<STR_LIT:name>' : '<STR_LIT:Name>' , '<STR_LIT:code>' : '<STR_LIT:content>' } ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT> ) </s>
