|
| 1 | +import re |
1 | 2 | from django.test import TestCase |
2 | 3 | from django.urls import reverse |
3 | 4 | from taggit.models import Tag |
4 | 5 | from library.models import LibraryItem |
5 | 6 | from library.factories import LibraryItemFactory |
6 | 7 | from magazine.factories import MagazineArticleFactory |
7 | | -from magazine.models import MagazineArticle |
| 8 | +from magazine.models import MagazineArticle, MagazineArticleAuthor |
8 | 9 | from news.factories import NewsItemFactory |
9 | 10 | from news.models import NewsItem |
10 | 11 | from wf_pages.factories import WfPageFactory |
11 | 12 | from wf_pages.models import WfPage |
| 13 | +from contact.factories import PersonFactory |
12 | 14 |
|
13 | 15 |
|
14 | 16 | class TaggedPageListViewQuerysetAndContentOrderTest(TestCase): |
@@ -157,3 +159,65 @@ def test_pagination_second_page(self): |
157 | 159 | paginated_context.page.previous_page_number(), |
158 | 160 | expected_previous_page, |
159 | 161 | ) |
| 162 | + |
| 163 | + |
| 164 | +class TaggedPageListViewTemplateRenderingTest(TestCase): |
| 165 | + def setUp(self): |
| 166 | + self.tag = Tag.objects.create(name="Authors Tag", slug="authors-tag") |
| 167 | + self.url = reverse("tags:tagged_page_list", kwargs={"tag": self.tag.slug}) |
| 168 | + |
| 169 | + # Create a magazine article and attach authors |
| 170 | + self.article = MagazineArticleFactory(title="Article With Authors") |
| 171 | + self.article.tags.add(self.tag) |
| 172 | + self.article.save() |
| 173 | + |
| 174 | + self.person_live = PersonFactory() |
| 175 | + # Publish person_live so template renders it as a link |
| 176 | + self.person_live.save_revision().publish() |
| 177 | + |
| 178 | + self.person_not_live = PersonFactory() |
| 179 | + # Ensure non-live stays unpublished for template logic |
| 180 | + self.person_not_live.live = False |
| 181 | + self.person_not_live.save() |
| 182 | + |
| 183 | + MagazineArticleAuthor.objects.create( |
| 184 | + article=self.article, |
| 185 | + author=self.person_live, |
| 186 | + ) |
| 187 | + MagazineArticleAuthor.objects.create( |
| 188 | + article=self.article, |
| 189 | + author=self.person_not_live, |
| 190 | + ) |
| 191 | + |
| 192 | + def test_authors_and_issue_render_with_accessible_markup(self): |
| 193 | + response = self.client.get(self.url) |
| 194 | + html = response.content.decode() |
| 195 | + |
| 196 | + # Authors label and list present with dynamic id |
| 197 | + self.assertIn("Authors", html) |
| 198 | + # Extract actual authors-label id from the span |
| 199 | + m = re.search( |
| 200 | + r"<span[^>]*id=\"(authors-label-[^\"]+)\"[^>]*>\s*Authors\s*:</span>", |
| 201 | + html, |
| 202 | + ) |
| 203 | + self.assertIsNotNone(m, "Could not find authors label span with id") |
| 204 | + label_id = m.group(1) |
| 205 | + # Verify aria-labelledby matches the extracted id |
| 206 | + self.assertIn(f'aria-labelledby="{label_id}"', html) |
| 207 | + |
| 208 | + # Live author should be linked |
| 209 | + live_title = self.person_live.title |
| 210 | + self.assertRegex(html, rf"<a[^>]*>\s*{re.escape(live_title)}\s*</a>") |
| 211 | + # Non-live author should be plain text (no anchor) |
| 212 | + non_live_title = self.person_not_live.title |
| 213 | + self.assertIn(non_live_title, html) |
| 214 | + self.assertIsNone( |
| 215 | + re.search(rf"<a[^>]*>\s*{re.escape(non_live_title)}\s*</a>", html), |
| 216 | + ) |
| 217 | + |
| 218 | + # Issue label and machine-readable date present |
| 219 | + self.assertIn("Issue", html) |
| 220 | + expected_date = self.article.get_parent().specific.publication_date.strftime( |
| 221 | + "%Y-%m-%d", |
| 222 | + ) |
| 223 | + self.assertIn(f'<time datetime="{expected_date}"', html) |
0 commit comments