Skip to content

Commit

Permalink
Merge pull request #758 from maykinmedia/feature/1709-add-info-no-sea…
Browse files Browse the repository at this point in the history
…rch-results

[#1709] Add info for when there are zero search results
  • Loading branch information
alextreme authored Sep 18, 2023
2 parents c294eef + 4d40316 commit 1503f78
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 57 deletions.
95 changes: 53 additions & 42 deletions src/open_inwoner/search/tests/test_feedback.py
Original file line number Diff line number Diff line change
@@ -1,170 +1,181 @@
import urllib

from django.contrib import messages
from django.test import tag
from django.test import override_settings, tag
from django.urls import reverse
from django.utils.translation import ugettext_lazy as _

from django_webtest import WebTest
from furl import furl

from open_inwoner.accounts.tests.factories import UserFactory
from open_inwoner.pdc.tests.factories import CategoryFactory, TagFactory
from open_inwoner.pdc.tests.factories import CategoryFactory, ProductFactory, TagFactory

from ..models import Feedback
from .factories import FeedbackFactory
from .utils import ESMixin


@tag("elastic")
@override_settings(ROOT_URLCONF="open_inwoner.cms.tests.urls")
class TestFeedbackFunctionality(ESMixin, WebTest):
def setUp(self):
self.user = UserFactory()
self.feedback = FeedbackFactory.build()
self.category1 = CategoryFactory.build()
self.category2 = CategoryFactory.build()
self.tag1 = TagFactory.build()
self.tag2 = TagFactory.build()
self.product1 = ProductFactory.create(
name="Name",
summary="Some summary",
content="Some content",
keywords=["keyword1", "keyword2"],
)
self.update_index()

def test_positive_feedback_is_saved_with_authenticated_user_and_without_filters(
self,
):
self.app.set_user(user=self.user)
params = {"query": self.feedback.search_query}
params = {"query": "keyword1"}
url = f"{reverse('search:search')}?{urllib.parse.urlencode(params, doseq=True)}"
response = self.app.get(url)

self.assertNotEquals(response.context["paginator"].count, 0)
feedback_form = response.forms["feedback_form"]
feedback_form["remark"] = self.feedback.remark
feedback_form["remark"] = "Some remark"
feedback_form["positive"] = "true"
feedback_form.submit()

feedback = Feedback.objects.all()[0]
feedback = Feedback.objects.get()

self.assertEqual(feedback.search_query, f"query: {self.feedback.search_query}")
self.assertEqual(feedback.search_query, f"query: keyword1")
self.assertEqual(feedback.search_url, url)
self.assertTrue(feedback.positive)
self.assertEqual(feedback.remark, self.feedback.remark)
self.assertEqual(feedback.remark, "Some remark")
self.assertEqual(feedback.searched_by, self.user)

def test_negative_feedback_is_saved_with_authenticated_user_and_without_filters(
self,
):
self.app.set_user(user=self.user)
params = {"query": self.feedback.search_query}
params = {"query": "keyword1"}
url = f"{reverse('search:search')}?{urllib.parse.urlencode(params, doseq=True)}"
response = self.app.get(url)

feedback_form = response.forms["feedback_form"]
feedback_form["remark"] = self.feedback.remark
feedback_form["remark"] = "Some remark"
feedback_form["positive"] = "false"
feedback_form.submit()

feedback = Feedback.objects.all()[0]
feedback = Feedback.objects.get()

self.assertEqual(feedback.search_query, f"query: {self.feedback.search_query}")
self.assertEqual(feedback.search_query, f"query: keyword1")
self.assertEqual(feedback.search_url, url)
self.assertFalse(feedback.positive)
self.assertEqual(feedback.remark, self.feedback.remark)
self.assertEqual(feedback.remark, "Some remark")
self.assertEqual(feedback.searched_by, self.user)

def test_positive_feedback_is_saved_with_unauthenticated_user_and_without_filters(
self,
):
params = {"query": self.feedback.search_query}
params = {"query": "keyword1"}
url = f"{reverse('search:search')}?{urllib.parse.urlencode(params, doseq=True)}"
response = self.app.get(url)

feedback_form = response.forms["feedback_form"]
feedback_form["remark"] = self.feedback.remark
feedback_form["remark"] = "Some remark"
feedback_form["positive"] = "true"
feedback_form.submit()

feedback = Feedback.objects.all()[0]
feedback = Feedback.objects.get()

self.assertEqual(feedback.search_query, f"query: {self.feedback.search_query}")
self.assertEqual(feedback.search_query, f"query: keyword1")
self.assertEqual(feedback.search_url, url)
self.assertTrue(feedback.positive)
self.assertEqual(feedback.remark, self.feedback.remark)
self.assertEqual(feedback.remark, "Some remark")
self.assertIsNone(feedback.searched_by)

def test_negative_feedback_is_saved_with_unauthenticated_user_and_without_filters(
self,
):
params = {"query": self.feedback.search_query}
params = {"query": "keyword1"}
url = f"{reverse('search:search')}?{urllib.parse.urlencode(params, doseq=True)}"
response = self.app.get(url)

feedback_form = response.forms["feedback_form"]
feedback_form["remark"] = self.feedback.remark
feedback_form["remark"] = "Some remark"
feedback_form["positive"] = "false"
feedback_form.submit()

feedback = Feedback.objects.all()[0]
feedback = Feedback.objects.get()

self.assertEqual(feedback.search_query, f"query: {self.feedback.search_query}")
self.assertEqual(feedback.search_query, f"query: keyword1")
self.assertEqual(feedback.search_url, url)
self.assertFalse(feedback.positive)
self.assertEqual(feedback.remark, self.feedback.remark)
self.assertEqual(feedback.remark, "Some remark")
self.assertIsNone(feedback.searched_by)

def test_positive_feedback_is_saved_with_authenticated_user_and_with_filters(self):
category1 = CategoryFactory()
category2 = CategoryFactory()
tag1 = TagFactory()
tag2 = TagFactory()
self.product1.tags.add(tag1, tag2)
self.product1.categories.add(category1, category2)

self.update_index()

self.app.set_user(user=self.user)
params = {
"query": [self.feedback.search_query],
"categories": [self.category1.name, self.category2.name],
"tags": [self.tag1.name, self.tag2.name],
"query": ["keyword1"],
"categories": [category1.slug, category2.slug],
"tags": [tag1.slug, tag2.slug],
}
url = f"{reverse('search:search')}?{urllib.parse.urlencode(params, doseq=True)}"
response = self.app.get(url)

feedback_form = response.forms["feedback_form"]
feedback_form["remark"] = self.feedback.remark
feedback_form["remark"] = "Some remark"
feedback_form["positive"] = "true"
feedback_form.submit()

feedback = Feedback.objects.all()[0]
feedback = Feedback.objects.get()

self.assertEqual(
feedback.search_query,
f"query: {self.feedback.search_query} | categories: {self.category1.name}, {self.category2.name} | tags: {self.tag1.name}, {self.tag2.name}",
f"query: keyword1 | categories: {category1.slug}, {category2.slug} | tags: {tag1.slug}, {tag2.slug}",
)
self.assertEqual(feedback.search_url, url)
self.assertTrue(feedback.positive)
self.assertEqual(feedback.remark, self.feedback.remark)
self.assertEqual(feedback.remark, "Some remark")
self.assertEqual(feedback.searched_by, self.user)

def test_positive_feedback_is_saved_with_unauthenticated_user_and_with_filters(
self,
):
params = {"query": self.feedback.search_query}
params = {"query": "keyword1"}
url = f"{reverse('search:search')}?{urllib.parse.urlencode(params, doseq=True)}"
response = self.app.get(url)

feedback_form = response.forms["feedback_form"]
feedback_form["remark"] = self.feedback.remark
feedback_form["remark"] = "Some remark"
feedback_form["positive"] = "true"
feedback_form.submit()

feedback = Feedback.objects.all()[0]
feedback = Feedback.objects.get()

self.assertEqual(feedback.search_query, f"query: {self.feedback.search_query}")
self.assertEqual(feedback.search_query, f"query: keyword1")
self.assertEqual(feedback.search_url, url)
self.assertTrue(feedback.positive)
self.assertEqual(feedback.remark, self.feedback.remark)
self.assertEqual(feedback.remark, "Some remark")
self.assertIsNone(feedback.searched_by)

def test_feedback_form_not_displayed_after_submit(self):
params = {"query": self.feedback.search_query}
params = {"query": "keyword1"}
url = furl(reverse("search:search")).add(params).url
response = self.app.get(url)

self.assertIsNotNone(response.html.find(id="feedback_form"))
self.assertEqual(list(response.context["messages"]), [])

feedback_form = response.forms["feedback_form"]
feedback_form["remark"] = self.feedback.remark
feedback_form["remark"] = "Some remark"
feedback_form["positive"] = "true"

post_response = feedback_form.submit().follow()
Expand Down
8 changes: 4 additions & 4 deletions src/open_inwoner/search/tests/test_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def test_not_show_results_and_filter_without_search(self):
# check that facet fields are not shown
self.assertNotIn(facet, search_form.fields)

results_div = response.html.find("div", {"class": "search-results"})
results_div = response.html.find("div", {"class": "search-results__list"})
self.assertIsNone(results_div) # check that results are not shown

def test_show_results_and_filter_with_search(self):
Expand All @@ -75,7 +75,7 @@ def test_show_results_and_filter_with_search(self):
results_div = response.html.find("div", {"class": "search-results"})
self.assertIsNotNone(results_div) # check that results are shown

results = response.context["results"].results
results = response.context["paginator"].object_list
self.assertEqual(len(results), 2)

def test_search_with_filter(self):
Expand Down Expand Up @@ -105,7 +105,7 @@ def test_search_with_filter(self):
results_div = response.html.find("div", {"class": "search-results"})
self.assertIsNotNone(results_div) # check that results are shown

results = response.context["results"].results
results = response.context["paginator"].object_list
self.assertEqual(len(results), 1)
self.assertEqual(results[0].slug, self.product1.slug)

Expand All @@ -118,7 +118,7 @@ def test_pagination_links(self):

self.assertEqual(response.status_code, 200)

results = response.context["results"].results
results = response.context["paginator"].object_list
self.assertEqual(len(results), 21)

pagination_div = response.html.find("div", {"class": "pagination"})
Expand Down
3 changes: 1 addition & 2 deletions src/open_inwoner/search/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ def search(self, form):
paginator_dict = self.paginate_with_context(results.results)

context.update(paginator_dict)
context.update({"results": results})

return self.render_to_response(context)

Expand Down Expand Up @@ -142,4 +141,4 @@ def form_valid(self, form):
@property
def display_restricted(self):
config = SiteConfiguration.get_solo()
return config.hide_categories_from_anonymous_users is True
return config.hide_categories_from_anonymous_users
31 changes: 22 additions & 9 deletions src/open_inwoner/templates/pages/search.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
{% render_form form=form method="GET" id="search-form" %}
{# zoek button section #}
<div class="grid">
{% spaceless %}<div class="grid__sidebar">
&nbsp;
</div>{% endspaceless %}
<div class="grid__sidebar"></div>
<div class="grid__main">
<h1 class="h1">{% trans "Zoeken naar " %} "{{ form.query.value }}"</h1>
<div class="form form--columns-2 form--inline form--align-end form--search">
Expand All @@ -18,7 +16,7 @@ <h1 class="h1">{% trans "Zoeken naar " %} "{{ form.query.value }}"</h1>
</div>

{# facets and results section #}
{% if results %}
{% if paginator.count %}
<div class="grid">
<aside class="grid__sidebar" aria-label="{% trans "Zoekfilters" %}">
{% filter field=form.categories %}
Expand All @@ -29,7 +27,7 @@ <h1 class="h1">{% trans "Zoeken naar " %} "{{ form.query.value }}"</h1>
<div class="grid__main">
<div class="search-results">
<h2 class="h2 search-results__title">
Zoekresultaten
{% trans "Zoekresultaten" %}
<span class="search-results__title-small">{{paginator.count}} zoekresultaten</span>

</h2>
Expand All @@ -48,14 +46,29 @@ <h3 class="search-results__item-title">{{ hit.name }}</h3>
{% pagination page_obj=page_obj paginator=paginator request=request %}
</div>
</div>
{% else %}
<div class="grid">
<div class="grid__sidebar"></div>
<div class="grid__main">
<div class="search-results search-results--none">
<h2 class="h2 search-results__title">
{% trans "Geen zoekresultaten" %}
</h2>
<p class="p">{% trans "Helaas, wij vonden geen resultaten voor jouw zoekopdracht." %}</p>
</div>
<ul class="ul">
<li class="li">{% trans "Controleer de spelling van je zoekopdracht." %}</li>
<li class="li">{% trans "Probeer een andere zoekopdracht." %}</li>
<li class="li">{% trans "Vragen? Neem contact met ons op." %}</li>
</ul>
</div>
</div>
{% endif %}
{% endrender_form %}

{% if results and not messages %}
{% if paginator.count and not messages %}
<div class="grid">
{% spaceless %}<div class="grid__sidebar">
&nbsp;
</div>{% endspaceless %}
<div class="grid__sidebar"></div>
<div class="grid__main">
<div class="grid">
<aside class="feedback" aria-label="{% trans "Pagina feedback" %}">
Expand Down

0 comments on commit 1503f78

Please sign in to comment.