|
| 1 | +import datetime |
| 2 | + |
| 3 | +from django.template.loader import render_to_string |
| 4 | +from django.test import TestCase |
| 5 | + |
| 6 | +from ..models import Event, EventLocation, Calendar |
| 7 | + |
| 8 | + |
| 9 | +class TimeTagTemplateTests(TestCase): |
| 10 | + def test_single_day_event_year_rendering(self): |
| 11 | + """ |
| 12 | + Verify that a single-day event does not render the year twice (Issue #2626). |
| 13 | + """ |
| 14 | + # Create a single day event in the future to trigger the year rendering condition |
| 15 | + future_year = datetime.date.today().year + 1 |
| 16 | + |
| 17 | + calendar = Calendar.objects.create(name="Test Calendar") |
| 18 | + |
| 19 | + event = Event.objects.create( |
| 20 | + title="Single Day Future Event", |
| 21 | + description="Test event", |
| 22 | + calendar=calendar, |
| 23 | + ) |
| 24 | + |
| 25 | + # Manually create the next_time context object |
| 26 | + class MockTime: |
| 27 | + def __init__(self): |
| 28 | + self.dt_start = datetime.datetime(future_year, 5, 25, 12, 0) |
| 29 | + self.dt_end = datetime.datetime(future_year, 5, 25, 14, 0) |
| 30 | + self.single_day = True |
| 31 | + self.all_day = False |
| 32 | + self.valid_dt_end = True |
| 33 | + |
| 34 | + context = { |
| 35 | + "next_time": MockTime(), |
| 36 | + "scheduled_start_this_year": False, # Event is in the future year |
| 37 | + "scheduled_end_this_year": False, |
| 38 | + "object": event, |
| 39 | + } |
| 40 | + |
| 41 | + rendered = render_to_string("events/includes/time_tag.html", context) |
| 42 | + |
| 43 | + # The year should only appear visibly once in the output (not counting the datetime ISO tag). |
| 44 | + year_str = str(future_year) |
| 45 | + # Using string splitting to exclude the `datetime="2027...` occurrence by checking how many times |
| 46 | + # it appears wrapped with whitespace or inside a span. |
| 47 | + visible_occurrences = rendered.split(">\n " + year_str + "\n </span>") |
| 48 | + self.assertEqual( |
| 49 | + len(visible_occurrences) - 1, |
| 50 | + 1, |
| 51 | + f"Expected the visible span containing {year_str} to appear exactly once, but it was duplicated: {rendered}" |
| 52 | + ) |
0 commit comments