diff --git a/app/Activity/Notifications/MessageParts/LinkedMailMessageLine.php b/app/Activity/Notifications/MessageParts/LinkedMailMessageLine.php index 8f6a4e2b9..45ae82571 100644 --- a/app/Activity/Notifications/MessageParts/LinkedMailMessageLine.php +++ b/app/Activity/Notifications/MessageParts/LinkedMailMessageLine.php @@ -3,13 +3,14 @@ namespace BookStack\Activity\Notifications\MessageParts; use Illuminate\Contracts\Support\Htmlable; +use Stringable; /** * A line of text with linked text included, intended for use * in MailMessages. The line should have a ':link' placeholder for * where the link should be inserted within the line. */ -class LinkedMailMessageLine implements Htmlable +class LinkedMailMessageLine implements Htmlable, Stringable { public function __construct( protected string $url, @@ -23,4 +24,10 @@ class LinkedMailMessageLine implements Htmlable $link = '' . e($this->linkText) . ''; return str_replace(':link', $link, e($this->line)); } + + public function __toString(): string + { + $link = "{$this->linkText} ({$this->url})"; + return str_replace(':link', $link, $this->line); + } } diff --git a/app/Activity/Notifications/MessageParts/ListMessageLine.php b/app/Activity/Notifications/MessageParts/ListMessageLine.php index f808d2561..9a729aa22 100644 --- a/app/Activity/Notifications/MessageParts/ListMessageLine.php +++ b/app/Activity/Notifications/MessageParts/ListMessageLine.php @@ -3,12 +3,13 @@ namespace BookStack\Activity\Notifications\MessageParts; use Illuminate\Contracts\Support\Htmlable; +use Stringable; /** * A bullet point list of content, where the keys of the given list array * are bolded header elements, and the values follow. */ -class ListMessageLine implements Htmlable +class ListMessageLine implements Htmlable, Stringable { public function __construct( protected array $list @@ -23,4 +24,13 @@ class ListMessageLine implements Htmlable } return implode("
\n", $list); } + + public function __toString(): string + { + $list = []; + foreach ($this->list as $header => $content) { + $list[] = $header . ' ' . $content; + } + return implode("\n", $list); + } } diff --git a/app/Activity/Notifications/Messages/BaseActivityNotification.php b/app/Activity/Notifications/Messages/BaseActivityNotification.php index eb6eb0cc8..a2045c8dc 100644 --- a/app/Activity/Notifications/Messages/BaseActivityNotification.php +++ b/app/Activity/Notifications/Messages/BaseActivityNotification.php @@ -4,12 +4,11 @@ namespace BookStack\Activity\Notifications\Messages; use BookStack\Activity\Models\Loggable; use BookStack\Activity\Notifications\MessageParts\LinkedMailMessageLine; +use BookStack\Notifications\MailNotification; use BookStack\Users\Models\User; use Illuminate\Bus\Queueable; -use Illuminate\Notifications\Messages\MailMessage; -use Illuminate\Notifications\Notification; -abstract class BaseActivityNotification extends Notification +abstract class BaseActivityNotification extends MailNotification { use Queueable; @@ -19,22 +18,6 @@ abstract class BaseActivityNotification extends Notification ) { } - /** - * Get the notification's delivery channels. - * - * @param mixed $notifiable - * @return array - */ - public function via($notifiable) - { - return ['mail']; - } - - /** - * Get the mail representation of the notification. - */ - abstract public function toMail(mixed $notifiable): MailMessage; - /** * Get the array representation of the notification. * @@ -52,12 +35,12 @@ abstract class BaseActivityNotification extends Notification /** * Build the common reason footer line used in mail messages. */ - protected function buildReasonFooterLine(): LinkedMailMessageLine + protected function buildReasonFooterLine(string $language): LinkedMailMessageLine { return new LinkedMailMessageLine( url('/preferences/notifications'), - trans('notifications.footer_reason'), - trans('notifications.footer_reason_link'), + trans('notifications.footer_reason', [], $language), + trans('notifications.footer_reason_link', [], $language), ); } } diff --git a/app/Activity/Notifications/Messages/CommentCreationNotification.php b/app/Activity/Notifications/Messages/CommentCreationNotification.php index ce358724b..5db61b04b 100644 --- a/app/Activity/Notifications/Messages/CommentCreationNotification.php +++ b/app/Activity/Notifications/Messages/CommentCreationNotification.php @@ -5,26 +5,29 @@ namespace BookStack\Activity\Notifications\Messages; use BookStack\Activity\Models\Comment; use BookStack\Activity\Notifications\MessageParts\ListMessageLine; use BookStack\Entities\Models\Page; +use BookStack\Users\Models\User; use Illuminate\Notifications\Messages\MailMessage; class CommentCreationNotification extends BaseActivityNotification { - public function toMail(mixed $notifiable): MailMessage + public function toMail(User $notifiable): MailMessage { /** @var Comment $comment */ $comment = $this->detail; /** @var Page $page */ $page = $comment->entity; - return (new MailMessage()) - ->subject(trans('notifications.new_comment_subject', ['pageName' => $page->getShortName()])) - ->line(trans('notifications.new_comment_intro', ['appName' => setting('app-name')])) + $language = $notifiable->getLanguage(); + + return $this->newMailMessage($language) + ->subject(trans('notifications.new_comment_subject', ['pageName' => $page->getShortName()], $language)) + ->line(trans('notifications.new_comment_intro', ['appName' => setting('app-name')], $language)) ->line(new ListMessageLine([ - trans('notifications.detail_page_name') => $page->name, - trans('notifications.detail_commenter') => $this->user->name, - trans('notifications.detail_comment') => strip_tags($comment->html), + trans('notifications.detail_page_name', [], $language) => $page->name, + trans('notifications.detail_commenter', [], $language) => $this->user->name, + trans('notifications.detail_comment', [], $language) => strip_tags($comment->html), ])) - ->action(trans('notifications.action_view_comment'), $page->getUrl('#comment' . $comment->local_id)) - ->line($this->buildReasonFooterLine()); + ->action(trans('notifications.action_view_comment', [], $language), $page->getUrl('#comment' . $comment->local_id)) + ->line($this->buildReasonFooterLine($language)); } } diff --git a/app/Activity/Notifications/Messages/PageCreationNotification.php b/app/Activity/Notifications/Messages/PageCreationNotification.php index 068f95acc..110829469 100644 --- a/app/Activity/Notifications/Messages/PageCreationNotification.php +++ b/app/Activity/Notifications/Messages/PageCreationNotification.php @@ -4,23 +4,26 @@ namespace BookStack\Activity\Notifications\Messages; use BookStack\Activity\Notifications\MessageParts\ListMessageLine; use BookStack\Entities\Models\Page; +use BookStack\Users\Models\User; use Illuminate\Notifications\Messages\MailMessage; class PageCreationNotification extends BaseActivityNotification { - public function toMail(mixed $notifiable): MailMessage + public function toMail(User $notifiable): MailMessage { /** @var Page $page */ $page = $this->detail; - return (new MailMessage()) - ->subject(trans('notifications.new_page_subject', ['pageName' => $page->getShortName()])) - ->line(trans('notifications.new_page_intro', ['appName' => setting('app-name')])) + $language = $notifiable->getLanguage(); + + return $this->newMailMessage($language) + ->subject(trans('notifications.new_page_subject', ['pageName' => $page->getShortName()], $language)) + ->line(trans('notifications.new_page_intro', ['appName' => setting('app-name')], $language)) ->line(new ListMessageLine([ - trans('notifications.detail_page_name') => $page->name, - trans('notifications.detail_created_by') => $this->user->name, + trans('notifications.detail_page_name', [], $language) => $page->name, + trans('notifications.detail_created_by', [], $language) => $this->user->name, ])) - ->action(trans('notifications.action_view_page'), $page->getUrl()) - ->line($this->buildReasonFooterLine()); + ->action(trans('notifications.action_view_page', [], $language), $page->getUrl()) + ->line($this->buildReasonFooterLine($language)); } } diff --git a/app/Activity/Notifications/Messages/PageUpdateNotification.php b/app/Activity/Notifications/Messages/PageUpdateNotification.php index c4a6de0bd..8e2d27fa4 100644 --- a/app/Activity/Notifications/Messages/PageUpdateNotification.php +++ b/app/Activity/Notifications/Messages/PageUpdateNotification.php @@ -4,24 +4,27 @@ namespace BookStack\Activity\Notifications\Messages; use BookStack\Activity\Notifications\MessageParts\ListMessageLine; use BookStack\Entities\Models\Page; +use BookStack\Users\Models\User; use Illuminate\Notifications\Messages\MailMessage; class PageUpdateNotification extends BaseActivityNotification { - public function toMail(mixed $notifiable): MailMessage + public function toMail(User $notifiable): MailMessage { /** @var Page $page */ $page = $this->detail; - return (new MailMessage()) - ->subject(trans('notifications.updated_page_subject', ['pageName' => $page->getShortName()])) - ->line(trans('notifications.updated_page_intro', ['appName' => setting('app-name')])) + $language = $notifiable->getLanguage(); + + return $this->newMailMessage($language) + ->subject(trans('notifications.updated_page_subject', ['pageName' => $page->getShortName()], $language)) + ->line(trans('notifications.updated_page_intro', ['appName' => setting('app-name')], $language)) ->line(new ListMessageLine([ - trans('notifications.detail_page_name') => $page->name, - trans('notifications.detail_updated_by') => $this->user->name, + trans('notifications.detail_page_name', [], $language) => $page->name, + trans('notifications.detail_updated_by', [], $language) => $this->user->name, ])) - ->line(trans('notifications.updated_page_debounce')) - ->action(trans('notifications.action_view_page'), $page->getUrl()) - ->line($this->buildReasonFooterLine()); + ->line(trans('notifications.updated_page_debounce', [], $language)) + ->action(trans('notifications.action_view_page', [], $language), $page->getUrl()) + ->line($this->buildReasonFooterLine($language)); } } diff --git a/app/Notifications/ConfirmEmail.php b/app/Notifications/ConfirmEmail.php index 5399c25a8..6b395e137 100644 --- a/app/Notifications/ConfirmEmail.php +++ b/app/Notifications/ConfirmEmail.php @@ -2,28 +2,17 @@ namespace BookStack\Notifications; +use BookStack\Users\Models\User; +use Illuminate\Notifications\Messages\MailMessage; + class ConfirmEmail extends MailNotification { - public $token; - - /** - * Create a new notification instance. - * - * @param string $token - */ - public function __construct($token) - { - $this->token = $token; + public function __construct( + public string $token + ) { } - /** - * Get the mail representation of the notification. - * - * @param mixed $notifiable - * - * @return \Illuminate\Notifications\Messages\MailMessage - */ - public function toMail($notifiable) + public function toMail(User $notifiable): MailMessage { $appName = ['appName' => setting('app-name')]; diff --git a/app/Notifications/MailNotification.php b/app/Notifications/MailNotification.php index 12159b278..1f40219f9 100644 --- a/app/Notifications/MailNotification.php +++ b/app/Notifications/MailNotification.php @@ -2,15 +2,21 @@ namespace BookStack\Notifications; +use BookStack\Users\Models\User; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Notifications\Messages\MailMessage; use Illuminate\Notifications\Notification; -class MailNotification extends Notification implements ShouldQueue +abstract class MailNotification extends Notification implements ShouldQueue { use Queueable; + /** + * Get the mail representation of the notification. + */ + abstract public function toMail(User $notifiable): MailMessage; + /** * Get the notification's channels. * @@ -25,14 +31,14 @@ class MailNotification extends Notification implements ShouldQueue /** * Create a new mail message. - * - * @return MailMessage */ - protected function newMailMessage() + protected function newMailMessage(string $language = ''): MailMessage { + $data = ['language' => $language ?: null]; + return (new MailMessage())->view([ 'html' => 'vendor.notifications.email', 'text' => 'vendor.notifications.email-plain', - ]); + ], $data); } } diff --git a/app/Notifications/ResetPassword.php b/app/Notifications/ResetPassword.php index 7fa146596..2d7e9c361 100644 --- a/app/Notifications/ResetPassword.php +++ b/app/Notifications/ResetPassword.php @@ -2,31 +2,17 @@ namespace BookStack\Notifications; +use BookStack\Users\Models\User; +use Illuminate\Notifications\Messages\MailMessage; + class ResetPassword extends MailNotification { - /** - * The password reset token. - * - * @var string - */ - public $token; - - /** - * Create a notification instance. - * - * @param string $token - */ - public function __construct($token) - { - $this->token = $token; + public function __construct( + public string $token + ) { } - /** - * Build the mail representation of the notification. - * - * @return \Illuminate\Notifications\Messages\MailMessage - */ - public function toMail() + public function toMail(User $notifiable): MailMessage { return $this->newMailMessage() ->subject(trans('auth.email_reset_subject', ['appName' => setting('app-name')])) diff --git a/app/Notifications/TestEmail.php b/app/Notifications/TestEmail.php index 7f59ff70f..af9e5847f 100644 --- a/app/Notifications/TestEmail.php +++ b/app/Notifications/TestEmail.php @@ -2,16 +2,12 @@ namespace BookStack\Notifications; +use BookStack\Users\Models\User; +use Illuminate\Notifications\Messages\MailMessage; + class TestEmail extends MailNotification { - /** - * Get the mail representation of the notification. - * - * @param mixed $notifiable - * - * @return \Illuminate\Notifications\Messages\MailMessage - */ - public function toMail($notifiable) + public function toMail(User $notifiable): MailMessage { return $this->newMailMessage() ->subject(trans('settings.maint_send_test_email_mail_subject')) diff --git a/app/Notifications/UserInvite.php b/app/Notifications/UserInvite.php index 8804df586..87ea31c04 100644 --- a/app/Notifications/UserInvite.php +++ b/app/Notifications/UserInvite.php @@ -7,25 +7,17 @@ use Illuminate\Notifications\Messages\MailMessage; class UserInvite extends MailNotification { - public $token; - - /** - * Create a new notification instance. - */ - public function __construct(string $token) - { - $this->token = $token; + public function __construct( + public string $token + ) { } - /** - * Get the mail representation of the notification. - */ public function toMail(User $notifiable): MailMessage { $appName = ['appName' => setting('app-name')]; - $language = setting()->getUser($notifiable, 'language'); + $language = $notifiable->getLanguage(); - return $this->newMailMessage() + return $this->newMailMessage($language) ->subject(trans('auth.user_invite_email_subject', $appName, $language)) ->greeting(trans('auth.user_invite_email_greeting', $appName, $language)) ->line(trans('auth.user_invite_email_text', [], $language)) diff --git a/app/Translation/LanguageManager.php b/app/Translation/LanguageManager.php index 39dee8461..5132929b1 100644 --- a/app/Translation/LanguageManager.php +++ b/app/Translation/LanguageManager.php @@ -2,6 +2,7 @@ namespace BookStack\Translation; +use BookStack\Users\Models\User; use Illuminate\Http\Request; class LanguageManager @@ -80,6 +81,15 @@ class LanguageManager return setting()->getUser($user, 'language', $default); } + /** + * Get the language for the given user. + */ + public function getLanguageForUser(User $user): string + { + $default = config('app.locale'); + return setting()->getUser($user, 'language', $default); + } + /** * Check if the given BookStack language value is a right-to-left language. */ diff --git a/app/Users/Models/User.php b/app/Users/Models/User.php index be3e9b9b3..a2b54f708 100644 --- a/app/Users/Models/User.php +++ b/app/Users/Models/User.php @@ -11,6 +11,7 @@ use BookStack\App\Model; use BookStack\App\Sluggable; use BookStack\Entities\Tools\SlugGenerator; use BookStack\Notifications\ResetPassword; +use BookStack\Translation\LanguageManager; use BookStack\Uploads\Image; use Carbon\Carbon; use Exception; @@ -338,6 +339,14 @@ class User extends Model implements AuthenticatableContract, CanResetPasswordCon return ''; } + /** + * Get the system language for this user. + */ + public function getLanguage(): string + { + return app()->make(LanguageManager::class)->getLanguageForUser($this); + } + /** * Send the password reset notification. * diff --git a/phpunit.xml b/phpunit.xml index 704372c5c..183e45637 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -21,6 +21,7 @@ + diff --git a/resources/views/vendor/notifications/email.blade.php b/resources/views/vendor/notifications/email.blade.php index 88cdbd890..f5d9c328d 100644 --- a/resources/views/vendor/notifications/email.blade.php +++ b/resources/views/vendor/notifications/email.blade.php @@ -159,7 +159,7 @@ $style = [

- {{ trans('common.email_action_help', ['actionText' => $actionText]) }} + {{ trans('common.email_action_help', ['actionText' => $actionText], $language) }}

@@ -187,7 +187,7 @@ $style = [

© {{ date('Y') }} {{ setting('app-name') }}. - {{ trans('common.email_rights') }} + {{ trans('common.email_rights', [], $language) }}

diff --git a/tests/Activity/WatchTest.php b/tests/Activity/WatchTest.php index fd86029d3..72e6b37a5 100644 --- a/tests/Activity/WatchTest.php +++ b/tests/Activity/WatchTest.php @@ -2,9 +2,13 @@ namespace Tests\Activity; +use BookStack\Activity\ActivityType; +use BookStack\Activity\Models\Comment; +use BookStack\Activity\Notifications\Messages\BaseActivityNotification; use BookStack\Activity\Notifications\Messages\CommentCreationNotification; use BookStack\Activity\Notifications\Messages\PageCreationNotification; use BookStack\Activity\Notifications\Messages\PageUpdateNotification; +use BookStack\Activity\Tools\ActivityLogger; use BookStack\Activity\Tools\UserEntityWatchOptions; use BookStack\Activity\WatchLevels; use BookStack\Entities\Models\Entity; @@ -253,7 +257,7 @@ class WatchTest extends TestCase $notifications->assertSentTo($editor, function (CommentCreationNotification $notification) use ($editor, $admin, $entities) { $mail = $notification->toMail($editor); - $mailContent = html_entity_decode(strip_tags($mail->render())); + $mailContent = html_entity_decode(strip_tags($mail->render()), ENT_QUOTES); return $mail->subject === 'New comment on page: ' . $entities['page']->getShortName() && str_contains($mailContent, 'View Comment') && str_contains($mailContent, 'Page Name: ' . $entities['page']->name) @@ -276,7 +280,7 @@ class WatchTest extends TestCase $notifications->assertSentTo($editor, function (PageUpdateNotification $notification) use ($editor, $admin) { $mail = $notification->toMail($editor); - $mailContent = html_entity_decode(strip_tags($mail->render())); + $mailContent = html_entity_decode(strip_tags($mail->render()), ENT_QUOTES); return $mail->subject === 'Updated page: Updated page' && str_contains($mailContent, 'View Page') && str_contains($mailContent, 'Page Name: Updated page') @@ -305,7 +309,7 @@ class WatchTest extends TestCase $notifications->assertSentTo($editor, function (PageCreationNotification $notification) use ($editor, $admin) { $mail = $notification->toMail($editor); - $mailContent = html_entity_decode(strip_tags($mail->render())); + $mailContent = html_entity_decode(strip_tags($mail->render()), ENT_QUOTES); return $mail->subject === 'New page: My new page' && str_contains($mailContent, 'View Page') && str_contains($mailContent, 'Page Name: My new page') @@ -313,6 +317,43 @@ class WatchTest extends TestCase }); } + public function test_notifications_sent_in_right_language() + { + $editor = $this->users->editor(); + $admin = $this->users->admin(); + setting()->putUser($editor, 'language', 'de'); + $entities = $this->entities->createChainBelongingToUser($editor); + $watches = new UserEntityWatchOptions($editor, $entities['book']); + $watches->updateLevelByValue(WatchLevels::COMMENTS); + + $activities = [ + ActivityType::PAGE_CREATE => $entities['page'], + ActivityType::PAGE_UPDATE => $entities['page'], + ActivityType::COMMENT_CREATE => (new Comment([]))->forceFill(['entity_id' => $entities['page']->id, 'entity_type' => $entities['page']->getMorphClass()]), + ]; + + $notifications = Notification::fake(); + $logger = app()->make(ActivityLogger::class); + $this->actingAs($admin); + + foreach ($activities as $activityType => $detail) { + $logger->add($activityType, $detail); + } + + $sent = $notifications->sentNotifications()[get_class($editor)][$editor->id]; + $this->assertCount(3, $sent); + + foreach ($sent as $notificationInfo) { + $notification = $notificationInfo[0]['notification']; + $this->assertInstanceOf(BaseActivityNotification::class, $notification); + $mail = $notification->toMail($editor); + $mailContent = html_entity_decode(strip_tags($mail->render()), ENT_QUOTES); + $this->assertStringContainsString('Name der Seite:', $mailContent); + $this->assertStringContainsString('Diese Benachrichtigung wurde', $mailContent); + $this->assertStringContainsString('Sollte es beim Anklicken der Schaltfläche', $mailContent); + } + } + public function test_notifications_not_sent_if_lacking_view_permission_for_related_item() { $notifications = Notification::fake();