Render enrollment emails in the student's language

Enrollment, unenrollment and beta role emails should be rendered in the
student's language, and not the instructor's language.
This commit is contained in:
Régis Behmo
2014-11-26 11:56:47 +01:00
parent 7a2f0640b7
commit f3419bb5bb
4 changed files with 196 additions and 14 deletions

View File

@@ -0,0 +1,84 @@
# -*- coding: utf-8 -*-
"""
Unit tests for the localization of emails sent by instructor.api methods.
"""
from django.core import mail
from django.core.urlresolvers import reverse
from django.test import TestCase
from courseware.tests.factories import InstructorFactory
from lang_pref import LANGUAGE_KEY
from student.models import CourseEnrollment
from student.tests.factories import UserFactory
from openedx.core.djangoapps.user_api.models import UserPreference
from xmodule.modulestore.tests.factories import CourseFactory
class TestInstructorAPIEnrollmentEmailLocalization(TestCase):
"""
Test whether the enroll, unenroll and beta role emails are sent in the
proper language, i.e: the student's language.
"""
def setUp(self):
# Platform language is English, instructor's language is Chinese,
# student's language is French, so the emails should all be sent in
# French.
self.course = CourseFactory.create()
self.instructor = InstructorFactory(course_key=self.course.id)
UserPreference.set_preference(self.instructor, LANGUAGE_KEY, 'zh-cn')
self.client.login(username=self.instructor.username, password='test')
self.student = UserFactory.create()
UserPreference.set_preference(self.student, LANGUAGE_KEY, 'fr')
def update_enrollement(self, action, student_email):
"""
Update the current student enrollment status.
"""
url = reverse('students_update_enrollment', kwargs={'course_id': self.course.id.to_deprecated_string()})
args = {'identifiers': student_email, 'email_students': 'true', 'action': action}
response = self.client.post(url, args)
return response
def check_outbox_is_french(self):
"""
Check that the email outbox contains exactly one message for which both
the message subject and body contain a certain French string.
"""
return self.check_outbox(u"Vous avez été")
def check_outbox(self, expected_message):
"""
Check that the email outbox contains exactly one message for which both
the message subject and body contain a certain string.
"""
self.assertEqual(1, len(mail.outbox))
self.assertIn(expected_message, mail.outbox[0].subject)
self.assertIn(expected_message, mail.outbox[0].body)
def test_enroll(self):
self.update_enrollement("enroll", self.student.email)
self.check_outbox_is_french()
def test_unenroll(self):
CourseEnrollment.enroll(
self.student,
self.course.id
)
self.update_enrollement("unenroll", self.student.email)
self.check_outbox_is_french()
def test_set_beta_role(self):
url = reverse('bulk_beta_modify_access', kwargs={'course_id': self.course.id.to_deprecated_string()})
self.client.post(url, {'identifiers': self.student.email, 'action': 'add', 'email_students': 'true'})
self.check_outbox_is_french()
def test_enroll_unsubscribed_student(self):
# Student is unknown, so the platform language should be used
self.update_enrollement("enroll", "newuser@hotmail.com")
self.check_outbox("You have been")

View File

@@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
"""
Unit tests for instructor.enrollment methods.
"""
@@ -9,6 +10,8 @@ from courseware.models import StudentModule
from django.conf import settings
from django.test import TestCase
from django.test.utils import override_settings
from django.utils.translation import get_language
from django.utils.translation import override as override_language
from student.tests.factories import UserFactory
from xmodule.modulestore.tests.factories import CourseFactory
from xmodule.modulestore.tests.django_utils import TEST_DATA_MOCK_MODULESTORE
@@ -20,7 +23,8 @@ from instructor.enrollment import (
get_email_params,
reset_student_attempts,
send_beta_role_email,
unenroll_email
unenroll_email,
render_message_to_string,
)
from opaque_keys.edx.locations import SlashSeparatedCourseKey
@@ -472,3 +476,50 @@ class TestGetEmailParams(ModuleStoreTestCase):
self.assertEqual(result['course_about_url'], None)
self.assertEqual(result['registration_url'], self.registration_url)
self.assertEqual(result['course_url'], self.course_url)
class TestRenderMessageToString(TestCase):
"""
Test that email templates can be rendered in a language chosen manually.
"""
def setUp(self):
self.subject_template = 'emails/enroll_email_allowedsubject.txt'
self.message_template = 'emails/enroll_email_allowedmessage.txt'
self.course = CourseFactory.create()
def get_email_params(self):
"""
Returns a dictionary of parameters used to render an email.
"""
email_params = get_email_params(self.course, True)
email_params["email_address"] = "user@example.com"
email_params["full_name"] = "Jean Reno"
return email_params
def get_subject_and_message(self, language):
"""
Returns the subject and message rendered in the specified language.
"""
return render_message_to_string(
self.subject_template,
self.message_template,
self.get_email_params(),
language=language
)
def test_subject_and_message_translation(self):
subject, message = self.get_subject_and_message('fr')
language_after_rendering = get_language()
you_have_been_invited_in_french = u"Vous avez été invité"
self.assertIn(you_have_been_invited_in_french, subject)
self.assertIn(you_have_been_invited_in_french, message)
self.assertEqual(settings.LANGUAGE_CODE, language_after_rendering)
def test_platform_language_is_used_for_logged_in_user(self):
with override_language('zh_CN'): # simulate a user login
subject, message = self.get_subject_and_message(None)
self.assertIn("You have been", subject)
self.assertIn("You have been", message)