-
Notifications
You must be signed in to change notification settings - Fork 199
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7615 from Automattic/fix/prevent-welcome-email-fo…
…r-future-access Prevent sending welcome email immediately on enrole in case their access starts in the future
- Loading branch information
Showing
5 changed files
with
134 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -70,9 +70,13 @@ public function testInit_WhenCalled_AddsHooksForInitializingIndividualEmails() { | |
$generator->init(); | ||
|
||
/* Assert. */ | ||
do_action( 'sensei_user_course_start', 1, 1 ); | ||
$priority = has_action( 'sensei_user_course_start', [ $generator, 'welcome_to_course_for_student' ] ); | ||
self::assertSame( 10, $priority ); | ||
do_action( 'sensei_course_enrolment_status_changed', 1, 1 ); | ||
do_action( 'sensei_pro_course_access_start_student_email_send', 1, 1 ); | ||
|
||
$priority_for_immediate_start = has_action( 'sensei_course_enrolment_status_changed', [ $generator, 'welcome_to_course_for_student' ] ); | ||
$priority_for_access_start = has_action( 'sensei_pro_course_access_start_student_email_send', [ $generator, 'welcome_to_course_for_student' ] ); | ||
self::assertSame( 10, $priority_for_immediate_start ); | ||
self::assertSame( 10, $priority_for_access_start ); | ||
} | ||
|
||
public function testWelcomeToCourseForStudent_WhenCalled_CallsSenseiEmailSendFilterWithMatchingArguments() { | ||
|
@@ -102,16 +106,98 @@ public function testWelcomeToCourseForStudent_WhenCalled_CallsSenseiEmailSendFil | |
$generator = new Course_Welcome( $email_repository ); | ||
|
||
$actual_data = []; | ||
$filter = function( $email, $options ) use ( &$actual_data ) { | ||
$filter = function ( $email, $options ) use ( &$actual_data ) { | ||
$actual_data = [ | ||
'email' => $email, | ||
'options' => $options, | ||
]; | ||
}; | ||
add_filter( 'sensei_email_send', $filter, 10, 2 ); | ||
|
||
/* Act. */ | ||
$generator->welcome_to_course_for_student( $student_id, $course_id ); | ||
|
||
/* Assert. */ | ||
$expected = [ | ||
'email' => 'course_welcome', | ||
'options' => [ | ||
'[email protected]' => [ | ||
'teacher:id' => $teacher_id, | ||
'teacher:displayname' => 'Test Teacher', | ||
'student:id' => $student_id, | ||
'student:displayname' => 'Test Student', | ||
'course:id' => $course_id, | ||
'course:name' => '“Course with Special Characters…?”', | ||
'course:url' => esc_url( | ||
get_permalink( $course_id ) | ||
), | ||
], | ||
], | ||
]; | ||
self::assertSame( $expected, $actual_data ); | ||
|
||
/* Cleanup. */ | ||
remove_filter( 'sensei_email_send', $filter, 10 ); | ||
$factory->tearDown(); | ||
} | ||
|
||
public function testWelcomeToCourseForStudent_WhenCalledForWPMLCopy_CallsEmailSendActionOnlyForTheRealCourse() { | ||
/* Arrange. */ | ||
$factory = new \Sensei_Factory(); | ||
$student_id = $factory->user->create( | ||
[ | ||
'display_name' => 'Test Student', | ||
'user_email' => '[email protected]', | ||
] | ||
); | ||
$teacher_id = $factory->user->create( | ||
[ | ||
'display_name' => 'Test Teacher', | ||
] | ||
); | ||
$course_id = $factory->course->create( | ||
[ | ||
'post_title' => '“Course with Special Characters…?”', | ||
'post_author' => $teacher_id, | ||
] | ||
); | ||
|
||
$course_id_translated = $factory->course->create( | ||
[ | ||
'post_title' => '“Course with Special Characters…? Translated”', | ||
'post_author' => $teacher_id, | ||
] | ||
); | ||
|
||
$email_repository = $this->createMock( Email_Repository::class ); | ||
$email_repository->method( 'get' )->with( 'course_welcome' )->willReturn( new \WP_Post( (object) [ 'post_status' => 'publish' ] ) ); | ||
|
||
$generator = new Course_Welcome( $email_repository ); | ||
|
||
$actual_data = []; | ||
$filter = function ( $email, $options ) use ( &$actual_data ) { | ||
$actual_data = [ | ||
'email' => $email, | ||
'options' => $options, | ||
]; | ||
}; | ||
add_filter( 'sensei_email_send', $filter, 10, 2 ); | ||
|
||
add_filter( | ||
'wpml_original_element_id', | ||
function ( $modifiable, $current_course_id ) use ( $course_id_translated, $course_id ) { | ||
if ( $current_course_id === $course_id_translated ) { | ||
return "$course_id"; | ||
} | ||
return $current_course_id; | ||
}, | ||
10, | ||
3 | ||
); | ||
|
||
/* Act. */ | ||
$generator->welcome_to_course_for_student( $student_id, $course_id ); | ||
$generator->welcome_to_course_for_student( $student_id, $course_id_translated ); | ||
|
||
/* Assert. */ | ||
$expected = [ | ||
|