diff --git a/app/models/article_notifier.rb b/app/models/article_notifier.rb index f85ee675170..a13e924f6a9 100644 --- a/app/models/article_notifier.rb +++ b/app/models/article_notifier.rb @@ -3,7 +3,7 @@ class ArticleNotifier def call(payload) article = payload[:article] - return if article.wip? + return unless article.saved_change_to_attribute?(:published_at, from: nil) receivers = User.students_trainees_mentors_and_admins.reject { |receiver| receiver == article.user } send_notification(article:, receivers:) diff --git a/test/system/notification/articles_test.rb b/test/system/notification/articles_test.rb new file mode 100644 index 00000000000..5efd26e284f --- /dev/null +++ b/test/system/notification/articles_test.rb @@ -0,0 +1,49 @@ +# frozen_string_literal: true + +require 'application_system_test_case' + +class ArticlesTest < ApplicationSystemTestCase + setup do + @article = articles(:article1) + @delivery_mode = AbstractNotifier.delivery_mode + AbstractNotifier.delivery_mode = :normal + end + + teardown do + AbstractNotifier.delivery_mode = @delivery_mode + end + + test 'the notification is sent only when the article is first published' do + visit_with_auth new_article_path, 'komagata' + fill_in('article_title', with: '通知テスト1回目') + fill_in('article_body', with: 'test') + click_on '公開する' + assert_text '記事を作成しました' + + visit_with_auth notifications_path, 'hajime' + within first('.card-list-item.is-unread') do + assert_text 'komagataさんがブログに「通知テスト1回目」を投稿しました。' + end + click_link '全て既読にする' + + visit_with_auth edit_article_path(@article), 'komagata' + fill_in('article_title', with: '通知テスト2回目') + click_on '更新する' + + visit_with_auth notifications_path, 'hajime' + assert_no_selector '.card-list-item.is-unread' + assert_no_text 'komagataさんがブログに「通知テスト2回目」を投稿しました。' + end + + test 'the notification is not sent when the article with WIP is saved' do + visit_with_auth new_article_path, 'komagata' + fill_in('article_title', with: '通知テストwip') + fill_in('article_body', with: 'test') + click_on 'WIP' + assert_text '記事をWIPとして保存しました' + + visit_with_auth notifications_path, 'hajime' + assert_no_selector '.card-list-item.is-unread' + assert_no_text 'komagataさんがブログに「通知テストwip」を投稿しました。' + end +end