diff --git a/common/djangoapps/student/tests/test_recent_enrollments.py b/common/djangoapps/student/tests/test_recent_enrollments.py index bb647703c0..21fe3339b6 100644 --- a/common/djangoapps/student/tests/test_recent_enrollments.py +++ b/common/djangoapps/student/tests/test_recent_enrollments.py @@ -93,7 +93,7 @@ class TestRecentEnrollments(ModuleStoreTestCase, XssTestMixin): """ Test that the list of newly created courses are properly sorted to show the most recent enrollments first. - + Also test recent enrollment message rendered appropriately for more than two courses. """ self._configure_message_timeout(600) @@ -122,14 +122,61 @@ class TestRecentEnrollments(ModuleStoreTestCase, XssTestMixin): self.assertEqual(recent_course_list[3].course.id, courses[2].id) self.assertEqual(recent_course_list[4].course.id, courses[3].id) - def test_dashboard_rendering(self): + self.client.login(username=self.student.username, password=self.PASSWORD) + response = self.client.get(reverse("dashboard")) + + # verify recent enrollment message + self.assertContains( + response, + 'Thank you for enrolling in:'.format(course_name=self.course.display_name) + ) + self.assertContains( + response, + ', '.join(enrollment.course.display_name for enrollment in recent_course_list) + ) + + def test_dashboard_rendering_with_single_course(self): """ - Tests that the dashboard renders the recent enrollment messages appropriately. + Tests that the dashboard renders the recent enrollment message appropriately for single course. """ self._configure_message_timeout(600) self.client.login(username=self.student.username, password=self.PASSWORD) response = self.client.get(reverse("dashboard")) - self.assertContains(response, "Thank you for enrolling in") + self.assertContains( + response, + "Thank you for enrolling in {course_name}".format(course_name=self.course.display_name) + ) + + def test_dashboard_rendering_with_two_courses(self): + """ + Tests that the dashboard renders the recent enrollment message appropriately for two courses. + """ + self._configure_message_timeout(600) + course_location = locator.CourseLocator( + 'Org2', + 'Course2', + 'Run2' + ) + course, _ = self._create_course_and_enrollment(course_location) + + self.client.login(username=self.student.username, password=self.PASSWORD) + response = self.client.get(reverse("dashboard")) + + courses_enrollments = list(get_course_enrollments(self.student, None, [])) + courses_enrollments.sort(key=lambda x: x.created, reverse=True) + self.assertEqual(len(courses_enrollments), 3) + + recent_course_enrollments = _get_recently_enrolled_courses(courses_enrollments) + self.assertEqual(len(recent_course_enrollments), 2) + + self.assertContains( + response, + "Thank you for enrolling in:".format(course_name=self.course.display_name) + ) + self.assertContains( + response, + ' and '.join(enrollment.course.display_name for enrollment in recent_course_enrollments) + ) def test_dashboard_escaped_rendering(self): """ diff --git a/common/djangoapps/student/views.py b/common/djangoapps/student/views.py index 2c857443f5..f71f451b6a 100644 --- a/common/djangoapps/student/views.py +++ b/common/djangoapps/student/views.py @@ -933,20 +933,32 @@ def _create_recent_enrollment_message(course_enrollments, course_modes): # pyli recently_enrolled_courses = _get_recently_enrolled_courses(course_enrollments) if recently_enrolled_courses: - enroll_messages = [ - { - "course_id": enrollment.course_overview.id, - "course_name": enrollment.course_overview.display_name, - "allow_donation": _allow_donation(course_modes, enrollment.course_overview.id, enrollment) - } + enrollments_count = len(recently_enrolled_courses) + course_name_separator = ', ' + # If length of enrolled course 2, join names with 'and' + if enrollments_count == 2: + course_name_separator = _(' and ') + + course_names = course_name_separator.join( + [enrollment.course_overview.display_name for enrollment in recently_enrolled_courses] + ) + + allow_donations = any( + _allow_donation(course_modes, enrollment.course_overview.id, enrollment) for enrollment in recently_enrolled_courses - ] + ) platform_name = configuration_helpers.get_value('platform_name', settings.PLATFORM_NAME) return render_to_string( 'enrollment/course_enrollment_message.html', - {'course_enrollment_messages': enroll_messages, 'platform_name': platform_name} + { + 'course_names': course_names, + 'enrollments_count': enrollments_count, + 'allow_donations': allow_donations, + 'platform_name': platform_name, + 'course_id': recently_enrolled_courses[0].course_overview.id if enrollments_count == 1 else None + } ) diff --git a/lms/templates/enrollment/course_enrollment_message.html b/lms/templates/enrollment/course_enrollment_message.html index e37a192f0f..cfc2ec8637 100644 --- a/lms/templates/enrollment/course_enrollment_message.html +++ b/lms/templates/enrollment/course_enrollment_message.html @@ -1,27 +1,35 @@ <%! from django.utils.translation import ugettext as _ %> <%page expression_filter="h"/> -% for course_msg in course_enrollment_messages: -
${_("Thank you for enrolling in {enrolled_course}. We hope you enjoy the course.").format(enrolled_course=course_msg["course_name"])}
- % if course_msg["allow_donation"]: -- ${_("{platform_name} is a nonprofit bringing high-quality education to everyone, everywhere. " - "Your help allows us to continuously improve the learning experience for millions and " - "make a better future one learner at a time.").format(platform_name=platform_name)} -
- % endif -${_("Thank you for enrolling in {course_names}. We hope you enjoy the course.").format(course_names=course_names)}
+ % else: +${_("Thank you for enrolling in:")}
+ ${course_names} +${_("We hope you enjoy the course.")}
+ % endif + % if allow_donations: ++ ${_("{platform_name} is a nonprofit bringing high-quality education to everyone, everywhere. " + "Your help allows us to continuously improve the learning experience for millions and " + "make a better future one learner at a time.").format(platform_name=platform_name)} +
+ % endif