recherche: première ébauche
[auf_framonde.git] / project / aldryn_search / tests.py
CommitLineData
5f23ec55
PH
1from django.template import Template
2from django.test import TestCase
3from django.test.utils import override_settings
4
5from cms.plugin_base import CMSPluginBase
6from cms.plugin_pool import plugin_pool
7from cms.models.placeholdermodel import Placeholder
8from cms.models import CMSPlugin
9
10from aldryn_search.search_indexes import TitleIndex
11from .helpers import get_plugin_index_data, get_request
12
13
14test_settings = {
15 'ALLOWED_HOSTS': ['localhost'],
16 'CMS_LANGUAGES': {1: [{'code': 'en', 'name': 'English'}]},
17 'CMS_TEMPLATES': (("whee.html", "Whee Template"),),
18 'LANGUAGES': (('en', 'English'),),
19 'LANGUAGE_CODE': 'en',
20 'TEMPLATE_LOADERS': ('aldryn_search.tests.FakeTemplateLoader',),
21}
22
23
24class FakeTemplateLoader(object):
25 is_usable = True
26
27 def __init__(self, name, dirs):
28 pass
29
30 def __iter__(self):
31 yield self.__class__
32 yield "{{baz}}"
33
34
35class NotIndexedPlugin(CMSPluginBase):
36 model = CMSPlugin
37 plugin_content = 'rendered plugin content'
38 render_template = Template(plugin_content)
39
40 def render(self, context, instance, placeholder):
41 return context
42
43plugin_pool.register_plugin(NotIndexedPlugin)
44
45
46@override_settings(**test_settings)
47class PluginIndexingTests(TestCase):
48
49 def setUp(self):
50 self.index = TitleIndex()
51 self.request = get_request(language='en')
52
53 def get_plugin(self):
54 instance = CMSPlugin(
55 language='en',
56 plugin_type="NotIndexedPlugin",
57 placeholder=Placeholder(id=1235)
58 )
59 instance.cmsplugin_ptr = instance
60 instance.pk = 1234 # otherwise plugin_meta_context_processor() crashes
61 return instance
62
63 def test_plugin_indexing_is_enabled_by_default(self):
64 cms_plugin = self.get_plugin()
65 indexed_content = self.index.get_plugin_search_text(
66 cms_plugin, self.request)
67 self.assertEqual(NotIndexedPlugin.plugin_content, indexed_content)
68
69 def test_plugin_indexing_can_be_disabled_on_model(self):
70 cms_plugin = self.get_plugin()
71 cms_plugin.search_fulltext = False
72 indexed_content = self.index.get_plugin_search_text(
73 cms_plugin, self.request)
74 self.assertEqual('', indexed_content)
75
76 def test_plugin_indexing_can_be_disabled_on_plugin(self):
77 NotIndexedPlugin.search_fulltext = False
78
79 try:
80 self.assertEqual(
81 '', self.index.get_plugin_search_text(self.get_plugin(), self.request))
82 finally:
83 del NotIndexedPlugin.search_fulltext
84
85 def test_page_title_is_indexed_using_prepare(self):
86 """This tests the indexing path way used by update_index mgmt command"""
87 from cms.api import create_page
88 page = create_page(
89 title="Whoopee", template="whee.html", language="en")
90
91 from haystack import connections
92 from haystack.constants import DEFAULT_ALIAS
93 search_conn = connections[DEFAULT_ALIAS]
94 unified_index = search_conn.get_unified_index()
95
96 from cms.models import Title
97 index = unified_index.get_index(Title)
98
99 title = Title.objects.get(pk=page.title_set.all()[0].pk)
100 index.index_queryset(DEFAULT_ALIAS) # initialises index._backend_alias
101 indexed = index.prepare(title)
102 self.assertEqual('Whoopee', indexed['title'])
103 self.assertEqual('Whoopee', indexed['text'])
104
105 def test_page_title_is_indexed_using_update_object(self):
106 """This tests the indexing path way used by the RealTimeSignalProcessor"""
107 from cms.api import create_page
108 page = create_page(
109 title="Whoopee", template="whee.html", language="en")
110
111 from haystack import connections
112 from haystack.constants import DEFAULT_ALIAS
113 search_conn = connections[DEFAULT_ALIAS]
114 unified_index = search_conn.get_unified_index()
115
116 from cms.models import Title
117 index = unified_index.get_index(Title)
118
119 title = Title.objects.get(pk=page.title_set.all()[0].pk)
120 index.update_object(title, using=DEFAULT_ALIAS)
121 indexed = index.prepared_data
122 self.assertEqual('Whoopee', indexed['title'])
123 self.assertEqual('Whoopee', indexed['text'])