Skip to content

Commit

Permalink
Fix test for template selection
Browse files Browse the repository at this point in the history
  • Loading branch information
yakky committed May 15, 2020
1 parent b5fc731 commit c1aecba
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 22 deletions.
16 changes: 8 additions & 8 deletions djangocms_blog/cms_plugins.py
Expand Up @@ -18,19 +18,19 @@ class BlogPlugin(CMSPluginBase):
module = get_setting('PLUGIN_MODULE_NAME')

def get_render_template(self, context, instance, placeholder):

templates = [os.path.join('djangocms_blog',
instance.template_folder,
self.base_render_template)]

templates = [
os.path.join('djangocms_blog', instance.template_folder, self.base_render_template)
]
if instance.app_config and instance.app_config.template_prefix:
templates.insert(0, os.path.join(instance.app_config.template_prefix,
instance.template_folder,
self.base_render_template))
templates.insert(0, os.path.join(
instance.app_config.template_prefix, instance.template_folder,
self.base_render_template
))

selected = select_template(templates)
return selected.template.name


class BlogLatestEntriesPlugin(BlogPlugin):
"""
Non cached plugin which returns the latest posts taking into account the
Expand Down
52 changes: 38 additions & 14 deletions tests/test_plugins.py
Expand Up @@ -200,28 +200,52 @@ def test_blog_archive_plugin(self):
self.assertEqual(context['dates'][0]['count'], 1)

def test_templates(self):
def _test_custom_templates_path(parts):
templates_path = os.path.join(os.path.dirname(__file__), 'test_utils', 'templates')

self.app_config_1.app_data.config.template_prefix = parts[0]
self.app_config_1.save()
tmp = plugin.template_folder
plugin.template_folder = parts[1]
plugin.save()
dir_parts = (templates_path,) + parts
template_parts = parts + (plugin_class.base_render_template, )
try:
os.makedirs(os.path.join(*dir_parts))
except OSError:
pass
fake_template = os.path.join(*template_parts)
with open(os.path.join(templates_path, fake_template), 'w'):
self.assertEqual(
plugin_class.get_render_template(context, plugin, ph),
fake_template
)
plugin.template_folder = tmp
plugin.save()
self.app_config_1.app_data.config.template_prefix = ''
self.app_config_1.save()
os.unlink(os.path.join(templates_path, fake_template))

posts = self.get_posts()
pages = self.get_pages()

ph = pages[0].placeholders.get(slot='content')
plugin = add_plugin(ph, 'BlogLatestEntriesPlugin', language='en', app_config=self.app_config_1)
plugin = add_plugin(
ph, 'BlogLatestEntriesPlugin', language='en', app_config=self.app_config_1
)

context = self.get_plugin_context(pages[0], 'en', plugin)
plugin_class = plugin.get_plugin_class_instance()
self.assertEqual(plugin_class.get_render_template(context, plugin, ph),
os.path.join('djangocms_blog', plugin.template_folder, plugin_class.base_render_template))
self.assertEqual(
plugin_class.get_render_template(context, plugin, ph),
os.path.join('djangocms_blog', plugin.template_folder, plugin_class.base_render_template)
)

self.app_config_1.app_data.config.template_prefix = 'whatever'
self.app_config_1.save()
tmp = plugin.template_folder
plugin.template_folder = 'whereever'
plugin.save()
self.assertEqual(plugin_class.get_render_template(context, plugin, ph),
os.path.join('whatever', 'whereever', plugin_class.base_render_template))
plugin.template_folder = tmp
plugin.save()
self.app_config_1.app_data.config.template_prefix = ''
self.app_config_1.save()
custom_parts = ('whatever', 'whereever')
_test_custom_templates_path(custom_parts)

custom_parts = ('djangocms_blog', 'whereever')
_test_custom_templates_path(custom_parts)


class PluginTest10(BaseTest):
Expand Down

0 comments on commit c1aecba

Please sign in to comment.