|
23 | 23 | from enterprise_catalog.apps.catalog.constants import COURSE, PROGRAM |
24 | 24 | from enterprise_catalog.apps.catalog.tests.factories import ( |
25 | 25 | ContentMetadataFactory, |
| 26 | + ContentTranslationFactory, |
26 | 27 | ) |
27 | 28 | from enterprise_catalog.apps.curation.tests.factories import ( |
28 | 29 | EnterpriseCurationConfigFactory, |
@@ -526,6 +527,171 @@ def test_delete_not_allowed(self, is_catalog_staff, is_role_assigned_via_jwt): |
526 | 527 | assert response.status_code == status.HTTP_405_METHOD_NOT_ALLOWED |
527 | 528 |
|
528 | 529 |
|
| 530 | +@ddt.ddt |
| 531 | +class HighlightSetMultilingualTests(CurationAPITestBase): |
| 532 | + """ |
| 533 | + Test multilingual support in HighlightSetReadOnlyViewSet. |
| 534 | + """ |
| 535 | + def setUp(self): |
| 536 | + super().setUp() |
| 537 | + |
| 538 | + # Create Spanish translations for the first three content items |
| 539 | + self.spanish_titles = [ |
| 540 | + 'Título en Español 1', |
| 541 | + 'Título en Español 2', |
| 542 | + 'Título en Español 3', |
| 543 | + ] |
| 544 | + self.translations = [] |
| 545 | + for idx, content_metadata in enumerate(self.highlighted_content_metadata_one[:3]): |
| 546 | + translation = ContentTranslationFactory( |
| 547 | + content_metadata=content_metadata, |
| 548 | + language_code='es', |
| 549 | + title=self.spanish_titles[idx] |
| 550 | + ) |
| 551 | + self.translations.append(translation) |
| 552 | + |
| 553 | + def test_list_with_spanish_language_parameter(self): |
| 554 | + """ |
| 555 | + Test that requesting highlight sets with lang=es returns Spanish translations. |
| 556 | + """ |
| 557 | + url = reverse('api:v1:highlight-sets-list') + f'?enterprise_customer={self.enterprise_uuid}&lang=es' |
| 558 | + self.set_up_catalog_learner() |
| 559 | + |
| 560 | + response = self.client.get(url) |
| 561 | + assert response.status_code == status.HTTP_200_OK |
| 562 | + |
| 563 | + highlight_sets_results = response.json()['results'] |
| 564 | + assert len(highlight_sets_results) == 1 |
| 565 | + |
| 566 | + highlighted_content = highlight_sets_results[0]['highlighted_content'] |
| 567 | + # First three should have Spanish titles |
| 568 | + for idx in range(3): |
| 569 | + assert highlighted_content[idx]['title'] == self.spanish_titles[idx] |
| 570 | + |
| 571 | + # Last two should have original English titles (no translation) |
| 572 | + for idx in range(3, 5): |
| 573 | + original_title = self.highlighted_content_metadata_one[idx].json_metadata['title'] |
| 574 | + assert highlighted_content[idx]['title'] == original_title |
| 575 | + |
| 576 | + def test_list_with_english_language_parameter(self): |
| 577 | + """ |
| 578 | + Test that requesting highlight sets with lang=en returns original English titles. |
| 579 | + """ |
| 580 | + url = reverse('api:v1:highlight-sets-list') + f'?enterprise_customer={self.enterprise_uuid}&lang=en' |
| 581 | + self.set_up_catalog_learner() |
| 582 | + |
| 583 | + response = self.client.get(url) |
| 584 | + assert response.status_code == status.HTTP_200_OK |
| 585 | + |
| 586 | + highlight_sets_results = response.json()['results'] |
| 587 | + highlighted_content = highlight_sets_results[0]['highlighted_content'] |
| 588 | + |
| 589 | + # All should have original English titles |
| 590 | + for idx in range(5): |
| 591 | + original_title = self.highlighted_content_metadata_one[idx].json_metadata['title'] |
| 592 | + assert highlighted_content[idx]['title'] == original_title |
| 593 | + |
| 594 | + def test_list_with_unsupported_language(self): |
| 595 | + """ |
| 596 | + Test that requesting with an unsupported language defaults to English. |
| 597 | + """ |
| 598 | + url = reverse('api:v1:highlight-sets-list') + f'?enterprise_customer={self.enterprise_uuid}&lang=fr' |
| 599 | + self.set_up_catalog_learner() |
| 600 | + |
| 601 | + response = self.client.get(url) |
| 602 | + assert response.status_code == status.HTTP_200_OK |
| 603 | + |
| 604 | + highlight_sets_results = response.json()['results'] |
| 605 | + highlighted_content = highlight_sets_results[0]['highlighted_content'] |
| 606 | + |
| 607 | + # All should have original English titles (default behavior) |
| 608 | + for idx in range(5): |
| 609 | + original_title = self.highlighted_content_metadata_one[idx].json_metadata['title'] |
| 610 | + assert highlighted_content[idx]['title'] == original_title |
| 611 | + |
| 612 | + def test_list_without_language_parameter(self): |
| 613 | + """ |
| 614 | + Test that requesting highlight sets without lang parameter defaults to English. |
| 615 | + """ |
| 616 | + url = reverse('api:v1:highlight-sets-list') + f'?enterprise_customer={self.enterprise_uuid}' |
| 617 | + self.set_up_catalog_learner() |
| 618 | + |
| 619 | + response = self.client.get(url) |
| 620 | + assert response.status_code == status.HTTP_200_OK |
| 621 | + |
| 622 | + highlight_sets_results = response.json()['results'] |
| 623 | + highlighted_content = highlight_sets_results[0]['highlighted_content'] |
| 624 | + |
| 625 | + # All should have original English titles (default behavior) |
| 626 | + for idx in range(5): |
| 627 | + original_title = self.highlighted_content_metadata_one[idx].json_metadata['title'] |
| 628 | + assert highlighted_content[idx]['title'] == original_title |
| 629 | + |
| 630 | + def test_detail_with_spanish_language_parameter(self): |
| 631 | + """ |
| 632 | + Test that retrieving a specific highlight set with lang=es returns Spanish translations. |
| 633 | + """ |
| 634 | + detail_url = reverse('api:v1:highlight-sets-detail', kwargs={'uuid': str(self.highlight_set_one.uuid)}) |
| 635 | + detail_url += '?lang=es' |
| 636 | + self.set_up_catalog_learner() |
| 637 | + |
| 638 | + response = self.client.get(detail_url) |
| 639 | + assert response.status_code == status.HTTP_200_OK |
| 640 | + |
| 641 | + highlighted_content = response.json()['highlighted_content'] |
| 642 | + |
| 643 | + # First three should have Spanish titles |
| 644 | + for idx in range(3): |
| 645 | + assert highlighted_content[idx]['title'] == self.spanish_titles[idx] |
| 646 | + |
| 647 | + # Last two should have original English titles (no translation) |
| 648 | + for idx in range(3, 5): |
| 649 | + original_title = self.highlighted_content_metadata_one[idx].json_metadata['title'] |
| 650 | + assert highlighted_content[idx]['title'] == original_title |
| 651 | + |
| 652 | + def test_detail_with_unsupported_language(self): |
| 653 | + """ |
| 654 | + Test that requesting with an unsupported language defaults to English. |
| 655 | + """ |
| 656 | + detail_url = reverse('api:v1:highlight-sets-detail', kwargs={'uuid': str(self.highlight_set_one.uuid)}) |
| 657 | + detail_url += '?lang=fr' # French is not in AVAILABLE_TRANSLATION_LANGUAGES |
| 658 | + self.set_up_catalog_learner() |
| 659 | + |
| 660 | + response = self.client.get(detail_url) |
| 661 | + assert response.status_code == status.HTTP_200_OK |
| 662 | + |
| 663 | + highlighted_content = response.json()['highlighted_content'] |
| 664 | + |
| 665 | + # All should have original English titles (default behavior) |
| 666 | + for idx in range(5): |
| 667 | + original_title = self.highlighted_content_metadata_one[idx].json_metadata['title'] |
| 668 | + assert highlighted_content[idx]['title'] == original_title |
| 669 | + |
| 670 | + def test_translation_with_empty_title(self): |
| 671 | + """ |
| 672 | + Test that if a translation exists but has an empty title, it falls back to the original. |
| 673 | + """ |
| 674 | + # Create a translation with empty title |
| 675 | + content_metadata = self.highlighted_content_metadata_one[4] |
| 676 | + ContentTranslationFactory( |
| 677 | + content_metadata=content_metadata, |
| 678 | + language_code='es', |
| 679 | + title='' # Empty title |
| 680 | + ) |
| 681 | + |
| 682 | + detail_url = reverse('api:v1:highlight-sets-detail', kwargs={'uuid': str(self.highlight_set_one.uuid)}) |
| 683 | + detail_url += '?lang=es' |
| 684 | + self.set_up_catalog_learner() |
| 685 | + |
| 686 | + response = self.client.get(detail_url) |
| 687 | + assert response.status_code == status.HTTP_200_OK |
| 688 | + |
| 689 | + highlighted_content = response.json()['highlighted_content'] |
| 690 | + # Should fall back to original title when translation title is empty |
| 691 | + original_title = self.highlighted_content_metadata_one[4].json_metadata['title'] |
| 692 | + assert highlighted_content[4]['title'] == original_title |
| 693 | + |
| 694 | + |
529 | 695 | @ddt.ddt |
530 | 696 | class HighlightSetViewSetTests(CurationAPITestBase): |
531 | 697 | """ |
|
0 commit comments