1 from django
.template
import Template
2 from django
.test
import TestCase
3 from django
.test
.utils
import override_settings
5 from cms
.plugin_base
import CMSPluginBase
6 from cms
.plugin_pool
import plugin_pool
7 from cms
.models
.placeholdermodel
import Placeholder
8 from cms
.models
import CMSPlugin
10 from aldryn_search
.search_indexes
import TitleIndex
11 from .helpers
import get_plugin_index_data
, get_request
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',),
24 class FakeTemplateLoader(object):
27 def __init__(self
, name
, dirs
):
35 class NotIndexedPlugin(CMSPluginBase
):
37 plugin_content
= 'rendered plugin content'
38 render_template
= Template(plugin_content
)
40 def render(self
, context
, instance
, placeholder
):
43 plugin_pool
.register_plugin(NotIndexedPlugin
)
46 @override_settings(**test_settings
)
47 class PluginIndexingTests(TestCase
):
50 self
.index
= TitleIndex()
51 self
.request
= get_request(language
='en')
56 plugin_type
="NotIndexedPlugin",
57 placeholder
=Placeholder(id=1235)
59 instance
.cmsplugin_ptr
= instance
60 instance
.pk
= 1234 # otherwise plugin_meta_context_processor() crashes
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
)
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
)
76 def test_plugin_indexing_can_be_disabled_on_plugin(self
):
77 NotIndexedPlugin
.search_fulltext
= False
81 '', self
.index
.get_plugin_search_text(self
.get_plugin(), self
.request
))
83 del NotIndexedPlugin
.search_fulltext
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
89 title
="Whoopee", template
="whee.html", language
="en")
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()
96 from cms
.models
import Title
97 index
= unified_index
.get_index(Title
)
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'])
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
109 title
="Whoopee", template
="whee.html", language
="en")
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()
116 from cms
.models
import Title
117 index
= unified_index
.get_index(Title
)
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'])