Commit 9c9e90ee8f58507a1416fe24bd2d9ddd8d7ddf9b
Exists in
staging
and in
42 other branches
Merge branch 'master' of https://gitlab.com/noosfero/noosfero into merging
Showing
23 changed files
with
550 additions
and
5 deletions
Show diff stats
app/models/person.rb
| @@ -490,6 +490,17 @@ class Person < Profile | @@ -490,6 +490,17 @@ class Person < Profile | ||
| 490 | gravatar_profile_image_url(self.email, :size=>20, :d => gravatar_default) | 490 | gravatar_profile_image_url(self.email, :size=>20, :d => gravatar_default) |
| 491 | end | 491 | end |
| 492 | 492 | ||
| 493 | + settings_items :last_notification, :type => DateTime | ||
| 494 | + settings_items :notification_time, :type => :integer, :default => 0 | ||
| 495 | + | ||
| 496 | + def notifier | ||
| 497 | + @notifier ||= PersonNotifier.new(self) | ||
| 498 | + end | ||
| 499 | + | ||
| 500 | + after_update do |person| | ||
| 501 | + person.notifier.reschedule_next_notification_mail | ||
| 502 | + end | ||
| 503 | + | ||
| 493 | protected | 504 | protected |
| 494 | 505 | ||
| 495 | def followed_by?(profile) | 506 | def followed_by?(profile) |
| @@ -0,0 +1,89 @@ | @@ -0,0 +1,89 @@ | ||
| 1 | +class PersonNotifier | ||
| 2 | + | ||
| 3 | + def initialize(person) | ||
| 4 | + @person = person | ||
| 5 | + end | ||
| 6 | + | ||
| 7 | + def self.schedule_all_next_notification_mail | ||
| 8 | + Delayed::Job.enqueue(NotifyAllJob.new) unless NotifyAllJob.exists? | ||
| 9 | + end | ||
| 10 | + | ||
| 11 | + def schedule_next_notification_mail | ||
| 12 | + dispatch_notification_mail if !NotifyJob.exists?(@person.id) | ||
| 13 | + end | ||
| 14 | + | ||
| 15 | + def dispatch_notification_mail | ||
| 16 | + Delayed::Job.enqueue(NotifyJob.new(@person.id), nil, @person.notification_time.hours.from_now) if @person.notification_time>0 | ||
| 17 | + end | ||
| 18 | + | ||
| 19 | + def reschedule_next_notification_mail | ||
| 20 | + return nil unless @person.setting_changed?(:notification_time) || @person.setting_changed?(:last_notification) | ||
| 21 | + NotifyJob.find(@person.id).delete_all | ||
| 22 | + schedule_next_notification_mail | ||
| 23 | + end | ||
| 24 | + | ||
| 25 | + def notify | ||
| 26 | + if @person.notification_time && @person.notification_time > 0 | ||
| 27 | + from = @person.last_notification || DateTime.now - @person.notification_time.hours | ||
| 28 | + notifications = @person.tracked_notifications.find(:all, :conditions => ["created_at > ?", from]) | ||
| 29 | + Noosfero.with_locale @person.environment.default_language do | ||
| 30 | + Mailer::deliver_content_summary(@person, notifications) unless notifications.empty? | ||
| 31 | + end | ||
| 32 | + @person.settings[:last_notification] = DateTime.now | ||
| 33 | + @person.save! | ||
| 34 | + end | ||
| 35 | + end | ||
| 36 | + | ||
| 37 | + class NotifyAllJob | ||
| 38 | + def self.exists? | ||
| 39 | + Delayed::Job.where(:handler => "--- !ruby/object:PersonNotifier::NotifyAllJob {}\n\n").count > 0 | ||
| 40 | + end | ||
| 41 | + | ||
| 42 | + def perform | ||
| 43 | + Person.find_each {|person| person.notifier.schedule_next_notification_mail } | ||
| 44 | + end | ||
| 45 | + end | ||
| 46 | + | ||
| 47 | + class NotifyJob < Struct.new(:person_id) | ||
| 48 | + | ||
| 49 | + def self.exists?(person_id) | ||
| 50 | + !find(person_id).empty? | ||
| 51 | + end | ||
| 52 | + | ||
| 53 | + def self.find(person_id) | ||
| 54 | + Delayed::Job.where(:handler => "--- !ruby/struct:PersonNotifier::NotifyJob \nperson_id: #{person_id}\n") | ||
| 55 | + end | ||
| 56 | + | ||
| 57 | + def perform | ||
| 58 | + Person.find(person_id).notifier.notify | ||
| 59 | + end | ||
| 60 | + | ||
| 61 | + def on_permanent_failure | ||
| 62 | + person = Person.find(person_id) | ||
| 63 | + person.notifier.dispatch_notification_mail | ||
| 64 | + end | ||
| 65 | + | ||
| 66 | + end | ||
| 67 | + | ||
| 68 | + class Mailer < ActionMailer::Base | ||
| 69 | + | ||
| 70 | + add_template_helper(PersonNotifierHelper) | ||
| 71 | + | ||
| 72 | + def session | ||
| 73 | + {:theme => nil} | ||
| 74 | + end | ||
| 75 | + | ||
| 76 | + def content_summary(person, notifications) | ||
| 77 | + @current_theme = 'default' | ||
| 78 | + @profile = person | ||
| 79 | + recipients person.email | ||
| 80 | + from "#{@profile.environment.name} <#{@profile.environment.contact_email}>" | ||
| 81 | + subject _("[%s] Network Activity") % [@profile.environment.name] | ||
| 82 | + body :recipient => @profile.nickname || @profile.name, | ||
| 83 | + :environment => @profile.environment.name, | ||
| 84 | + :url => @profile.environment.top_url, | ||
| 85 | + :notifications => notifications | ||
| 86 | + content_type "text/html" | ||
| 87 | + end | ||
| 88 | + end | ||
| 89 | +end |
app/views/person_notifier/mailer/_add_member_in_community.rhtml
0 → 100644
| @@ -0,0 +1 @@ | @@ -0,0 +1 @@ | ||
| 1 | +<%= render :partial => 'default_activity', :locals => { :activity => activity } %> |
| @@ -0,0 +1,33 @@ | @@ -0,0 +1,33 @@ | ||
| 1 | +<% Comment %> | ||
| 2 | +<% Profile %> | ||
| 3 | +<% Person %> | ||
| 4 | + | ||
| 5 | +<table style="background: #f0f0f1;border-bottom: 1px solid #d2d2d2 !important;border-top: 1px solid #fff;margin-bottom: 0;"> | ||
| 6 | +<tr> | ||
| 7 | + <td> | ||
| 8 | + <% if comment.author %> | ||
| 9 | + <%= link_to profile_image(comment.author, :minor), | ||
| 10 | + comment.author_url, | ||
| 11 | + :class => 'comment-picture', | ||
| 12 | + :title => comment.author_name | ||
| 13 | + %> | ||
| 14 | + <% end %> | ||
| 15 | + </td> | ||
| 16 | + <td> | ||
| 17 | + <%= comment.author.present? ? link_to(comment.author_name, comment.author.url, :style => "font-size: 12px; color: #333; font-weight: bold; text-decoration: none;") : content_tag('strong', comment.author_name) %> | ||
| 18 | + <% unless comment.title.blank? %> | ||
| 19 | + <span style="font-size: 12px;"><%= comment.title %></span><br/> | ||
| 20 | + <% end %> | ||
| 21 | + <span style="font-size: 10px;"><%= txt2html comment.body %></span><br/> | ||
| 22 | + <span style="font-size: 8px; color: #444444"><%= time_ago_as_sentence(comment.created_at) %></span> | ||
| 23 | + <br style="clear: both;" /> | ||
| 24 | + | ||
| 25 | + <% unless comment.replies.blank? %> | ||
| 26 | + <ul class="comment-replies"> | ||
| 27 | + <% comment.replies.each do |reply| %> | ||
| 28 | + <%= render :partial => 'comment', :locals => { :comment => reply } %> | ||
| 29 | + <% end %> | ||
| 30 | + </ul> | ||
| 31 | + <% end %> | ||
| 32 | + </td> | ||
| 33 | +</table> |
| @@ -0,0 +1,27 @@ | @@ -0,0 +1,27 @@ | ||
| 1 | +<table> | ||
| 2 | +<tr> | ||
| 3 | + <td> | ||
| 4 | + <%= link_to(profile_image(activity.user, :minor), activity.user.url) %> | ||
| 5 | + </td> | ||
| 6 | + <td> | ||
| 7 | + <p style="width:550px"> | ||
| 8 | + <span style="font-size: 14px;"><%= link_to activity.user.short_name(20), activity.user.url %></span> | ||
| 9 | + <span style="font-size: 14px;"><%= _("has published on community %s") % link_to(activity.target.profile.short_name(20), activity.target.profile.url, :style => "color: #333; font-weight: bold; text-decoration: none;") if activity.target.profile.is_a?(Community) %></span> | ||
| 10 | + <span style="font-size: 10px; color: #444444; float:right;"><%= time_ago_as_sentence(activity.created_at) %></span> | ||
| 11 | + </p> | ||
| 12 | + <p> | ||
| 13 | + <span style="font-size: 14px;"><%= link_to(activity.params['name'], activity.params['url'], :style => "color: #333; font-weight: bold; text-decoration: none;") %></span> | ||
| 14 | + <br/> | ||
| 15 | + <span title='<%= activity.target.class.short_description %>' class='profile-activity-icon icon-new icon-new<%= activity.target.class.icon_name %>'></span> | ||
| 16 | + <%= image_tag(activity.params['first_image']) unless activity.params['first_image'].blank? %><%= strip_tags(truncate(activity.params['lead'], :length => 1000, :ommision => '...')).gsub(/(\xC2\xA0|\s)+/, ' ').gsub(/^\s+/, '') %> | ||
| 17 | + </p> | ||
| 18 | + <p><%= content_tag(:p, link_to(_('See complete forum'), activity.get_url), :class => 'see-forum') if activity.target.is_a?(Forum) %></p> | ||
| 19 | + </td> | ||
| 20 | +</tr> | ||
| 21 | +<tr> | ||
| 22 | + <td></td> | ||
| 23 | + <td> | ||
| 24 | + <%= render :partial => 'profile_comments', :locals => { :activity => activity } %> | ||
| 25 | + </td> | ||
| 26 | +</tr> | ||
| 27 | +</table> |
app/views/person_notifier/mailer/_default_activity.rhtml
0 → 100644
| @@ -0,0 +1,19 @@ | @@ -0,0 +1,19 @@ | ||
| 1 | +<table> | ||
| 2 | +<tr> | ||
| 3 | + <td> | ||
| 4 | + <%= link_to(profile_image(activity.user, :minor), activity.user.url) %> | ||
| 5 | + </td> | ||
| 6 | + <td> | ||
| 7 | + <p style="width:550px"> | ||
| 8 | + <span style="font-size: 14px;"><%= link_to activity.user.name, activity.user.url %> <%= describe activity %></span> | ||
| 9 | + <span style="font-size: 10px; color: #444444; float: right;"><%= time_ago_as_sentence(activity.created_at) %></span> | ||
| 10 | + </p> | ||
| 11 | + </td> | ||
| 12 | +</tr> | ||
| 13 | +<tr> | ||
| 14 | + <td></td> | ||
| 15 | + <td> | ||
| 16 | + <%= render :partial => 'profile_comments', :locals => { :activity => activity } %> | ||
| 17 | + </td> | ||
| 18 | +</tr> | ||
| 19 | +</table> |
| @@ -0,0 +1 @@ | @@ -0,0 +1 @@ | ||
| 1 | +<%= render :partial => 'default_activity', :locals => { :activity => activity } %> |
| @@ -0,0 +1 @@ | @@ -0,0 +1 @@ | ||
| 1 | +<%= render :partial => 'default_activity', :locals => { :activity => activity } %> |
app/views/person_notifier/mailer/_leave_scrap_to_self.rhtml
0 → 120000
| @@ -0,0 +1 @@ | @@ -0,0 +1 @@ | ||
| 1 | +<%= render :partial => 'default_activity', :locals => { :activity => activity } %> |
app/views/person_notifier/mailer/_profile_comments.rhtml
0 → 100644
| @@ -0,0 +1,13 @@ | @@ -0,0 +1,13 @@ | ||
| 1 | +<% if activity.comments_count > 2 %> | ||
| 2 | + <div style="font-size: 10px;"> | ||
| 3 | + <% if activity.params['url'].blank? %> | ||
| 4 | + <%= _("%s comments") % activity.comments_count %> | ||
| 5 | + <% else %> | ||
| 6 | + <%= link_to(_("View all %s comments") % activity.comments_count, activity.params['url']) %> | ||
| 7 | + <% end %> | ||
| 8 | + </div> | ||
| 9 | +<% else %> | ||
| 10 | + <ul> | ||
| 11 | + <%= render :partial => 'comment', :collection => activity.comments %> | ||
| 12 | + </ul> | ||
| 13 | +<% end %> |
app/views/person_notifier/mailer/_reply_scrap_on_self.rhtml
0 → 120000
| @@ -0,0 +1,15 @@ | @@ -0,0 +1,15 @@ | ||
| 1 | +<table> | ||
| 2 | + <tr> | ||
| 3 | + <td> | ||
| 4 | + <%= link_to(profile_image(activity.user, :minor), activity.user.url) %> | ||
| 5 | + </td> | ||
| 6 | + <td> | ||
| 7 | + <p style="width:550px"> | ||
| 8 | + <span style="font-size: 14px;"><%= link_to activity.user.name, activity.user.url %> <%= describe activity %></span> | ||
| 9 | + <span style="font-size: 10px; color: #444444; float:right;"><%= time_ago_as_sentence(activity.created_at) %></span> | ||
| 10 | + </p> | ||
| 11 | + </td> | ||
| 12 | +</tr> | ||
| 13 | +</table> | ||
| 14 | +<div title='<%= activity.target.class.short_description %>' class='profile-activity-icon icon-new icon-newgallery'></div> | ||
| 15 | +<br/> |
| @@ -0,0 +1,18 @@ | @@ -0,0 +1,18 @@ | ||
| 1 | +<h3><%= _("%s's network activity") % @profile.name %></h3> | ||
| 2 | +<br/> | ||
| 3 | +<div> | ||
| 4 | +<% @notifications.each do |activity| %> | ||
| 5 | + <div style="border-left:none;border-right:none;border-top:1px solid #ccc;border-bottom:none;padding:10px;width:600px"> | ||
| 6 | + <%= render :partial => activity.verb, :locals => { :activity => activity } rescue "cannot render notification for #{activity.verb}" %> | ||
| 7 | + </div> | ||
| 8 | +<% end %> | ||
| 9 | +</div> | ||
| 10 | + | ||
| 11 | +<div style="color:#444444;font-size:11px;"> | ||
| 12 | +<p><%= _("Greetings,") %></p> | ||
| 13 | +<br/> | ||
| 14 | +<p>--</p> | ||
| 15 | +<p><%= _('%s team.') % @environment %></p> | ||
| 16 | +<p><%= url_for @url %></p> | ||
| 17 | +</div> | ||
| 18 | +<br/> |
app/views/profile_editor/_person.rhtml
| @@ -19,3 +19,8 @@ | @@ -19,3 +19,8 @@ | ||
| 19 | <%= @plugins.dispatch(:profile_info_extra_contents).collect { |content| instance_eval(&content) }.join("") %> | 19 | <%= @plugins.dispatch(:profile_info_extra_contents).collect { |content| instance_eval(&content) }.join("") %> |
| 20 | 20 | ||
| 21 | <%= render :partial => 'person_form', :locals => {:f => f} %> | 21 | <%= render :partial => 'person_form', :locals => {:f => f} %> |
| 22 | + | ||
| 23 | + <h2><%= _('Notification options') %></h2> | ||
| 24 | + <div> | ||
| 25 | + <%= select_tag 'profile_data[notification_time]', options_for_select([[_('Disabled'), 0], [_('Hourly'), 1], [_('Half Day'), 12], [_('Daily'), 24]], @profile.notification_time) %> | ||
| 26 | + </div> |
| @@ -0,0 +1 @@ | @@ -0,0 +1 @@ | ||
| 1 | +PersonNotifier.schedule_all_next_notification_mail |
lib/acts_as_having_settings.rb
| @@ -14,6 +14,17 @@ module ActsAsHavingSettings | @@ -14,6 +14,17 @@ module ActsAsHavingSettings | ||
| 14 | def #{settings_field} | 14 | def #{settings_field} |
| 15 | self[:#{settings_field}] ||= Hash.new | 15 | self[:#{settings_field}] ||= Hash.new |
| 16 | end | 16 | end |
| 17 | + | ||
| 18 | + def setting_changed?(setting_field) | ||
| 19 | + setting_field = setting_field.to_sym | ||
| 20 | + changed_settings = self.changes['#{settings_field}'] | ||
| 21 | + return false if changed_settings.nil? | ||
| 22 | + | ||
| 23 | + old_setting_value = changed_settings.first.nil? ? nil : changed_settings.first[setting_field] | ||
| 24 | + new_setting_value = changed_settings.last[setting_field] | ||
| 25 | + old_setting_value != new_setting_value | ||
| 26 | + end | ||
| 27 | + | ||
| 17 | before_save :symbolize_settings_keys | 28 | before_save :symbolize_settings_keys |
| 18 | private | 29 | private |
| 19 | def symbolize_settings_keys | 30 | def symbolize_settings_keys |
| @@ -36,11 +47,9 @@ module ActsAsHavingSettings | @@ -36,11 +47,9 @@ module ActsAsHavingSettings | ||
| 36 | val.nil? ? (#{default}.is_a?(String) ? gettext(#{default}) : #{default}) : val | 47 | val.nil? ? (#{default}.is_a?(String) ? gettext(#{default}) : #{default}) : val |
| 37 | end | 48 | end |
| 38 | def #{setting}=(value) | 49 | def #{setting}=(value) |
| 39 | - | ||
| 40 | - #UPGRADE Leandro: I add this line to save the serialize attribute | ||
| 41 | - send(self.class.settings_field.to_s + '_will_change!') | ||
| 42 | - | ||
| 43 | - send(self.class.settings_field)[:#{setting}] = self.class.acts_as_having_settings_type_cast(value, #{data_type.inspect}) | 50 | + h = send(self.class.settings_field).clone |
| 51 | + h[:#{setting}] = self.class.acts_as_having_settings_type_cast(value, #{data_type.inspect}) | ||
| 52 | + send(self.class.settings_field.to_s + '=', h) | ||
| 44 | end | 53 | end |
| 45 | CODE | 54 | CODE |
| 46 | end | 55 | end |
test/functional/invite_controller_test.rb
| @@ -7,6 +7,7 @@ class InviteControllerTest < ActionController::TestCase | @@ -7,6 +7,7 @@ class InviteControllerTest < ActionController::TestCase | ||
| 7 | @friend = create_user('thefriend').person | 7 | @friend = create_user('thefriend').person |
| 8 | @community = fast_create(Community) | 8 | @community = fast_create(Community) |
| 9 | login_as ('testuser') | 9 | login_as ('testuser') |
| 10 | + Delayed::Job.destroy_all | ||
| 10 | end | 11 | end |
| 11 | attr_accessor :profile, :friend, :community | 12 | attr_accessor :profile, :friend, :community |
| 12 | 13 |
test/unit/acts_as_having_settings_test.rb
| @@ -80,4 +80,38 @@ class ActsAsHavingSettingsTest < ActiveSupport::TestCase | @@ -80,4 +80,38 @@ class ActsAsHavingSettingsTest < ActiveSupport::TestCase | ||
| 80 | assert obj.save | 80 | assert obj.save |
| 81 | end | 81 | end |
| 82 | 82 | ||
| 83 | + should 'setting_changed be true if a setting passed as parameter was changed' do | ||
| 84 | + obj = TestClass.new | ||
| 85 | + obj.flag = true | ||
| 86 | + assert obj.setting_changed? 'flag' | ||
| 87 | + end | ||
| 88 | + | ||
| 89 | + should 'setting_changed be false if a setting passed as parameter was not changed' do | ||
| 90 | + obj = TestClass.new | ||
| 91 | + assert !obj.setting_changed?('flag') | ||
| 92 | + end | ||
| 93 | + | ||
| 94 | + should 'setting_changed be false if a setting passed as parameter was changed with the same value' do | ||
| 95 | + obj = TestClass.new | ||
| 96 | + obj.flag = true | ||
| 97 | + obj.save | ||
| 98 | + obj.flag = true | ||
| 99 | + assert !obj.setting_changed?('flag') | ||
| 100 | + end | ||
| 101 | + | ||
| 102 | + should 'setting_changed be false if a setting passed as parameter was not changed but another setting is changed' do | ||
| 103 | + obj = TestClass.new(:name => 'some name') | ||
| 104 | + obj.save | ||
| 105 | + obj.name = 'antoher nme' | ||
| 106 | + assert !obj.setting_changed?('flag') | ||
| 107 | + end | ||
| 108 | + | ||
| 109 | + should 'setting_changed be true for all changed fields' do | ||
| 110 | + obj = TestClass.new(:name => 'some name', :flag => false) | ||
| 111 | + obj.save | ||
| 112 | + obj.name = 'another nme' | ||
| 113 | + obj.flag = true | ||
| 114 | + assert obj.setting_changed?('flag') | ||
| 115 | + assert obj.setting_changed?('name') | ||
| 116 | + end | ||
| 83 | end | 117 | end |
| @@ -0,0 +1,24 @@ | @@ -0,0 +1,24 @@ | ||
| 1 | +require File.dirname(__FILE__) + '/../test_helper' | ||
| 2 | + | ||
| 3 | +class PersonNotifierHelperTest < ActiveSupport::TestCase | ||
| 4 | + | ||
| 5 | + include PersonNotifierHelper | ||
| 6 | + include ActionView::Helpers::TagHelper | ||
| 7 | + | ||
| 8 | + def setup | ||
| 9 | + @profile = mock | ||
| 10 | + @env = Environment.new | ||
| 11 | + end | ||
| 12 | + attr_reader :profile, :env | ||
| 13 | + | ||
| 14 | + should 'append top url of environment at image path' do | ||
| 15 | + profile.expects(:environment).returns(env).at_least_once | ||
| 16 | + assert_match /src="http:\/\/localhost\/image.png"/, image_tag("/image.png") | ||
| 17 | + end | ||
| 18 | + | ||
| 19 | + should 'return original path if do not have an environment' do | ||
| 20 | + profile.expects(:environment).returns(nil).at_least_once | ||
| 21 | + assert_match /src="\/image.png"/, image_tag("/image.png") | ||
| 22 | + end | ||
| 23 | + | ||
| 24 | +end |
| @@ -0,0 +1,214 @@ | @@ -0,0 +1,214 @@ | ||
| 1 | +require File.dirname(__FILE__) + '/../test_helper' | ||
| 2 | + | ||
| 3 | +class PersonNotifierTest < ActiveSupport::TestCase | ||
| 4 | + FIXTURES_PATH = File.dirname(__FILE__) + '/../fixtures' | ||
| 5 | + CHARSET = "utf-8" | ||
| 6 | + | ||
| 7 | + def setup | ||
| 8 | + ActionMailer::Base.delivery_method = :test | ||
| 9 | + ActionMailer::Base.perform_deliveries = true | ||
| 10 | + ActionMailer::Base.deliveries = [] | ||
| 11 | + Person.destroy_all | ||
| 12 | + @admin = create_user('adminuser').person | ||
| 13 | + @member = create_user('member').person | ||
| 14 | + @admin.notification_time = 24 | ||
| 15 | + @member.notification_time = 24 | ||
| 16 | + @admin.save! | ||
| 17 | + @member.save! | ||
| 18 | + @community = fast_create(Community) | ||
| 19 | + @community.add_member(@admin) | ||
| 20 | + @article = fast_create(TextileArticle, :name => 'Article test', :profile_id => @community.id, :notify_comments => true) | ||
| 21 | + Delayed::Job.destroy_all | ||
| 22 | + end | ||
| 23 | + | ||
| 24 | + should 'deliver mail to community members' do | ||
| 25 | + @community.add_member(@member) | ||
| 26 | + notify | ||
| 27 | + sent = ActionMailer::Base.deliveries.first | ||
| 28 | + assert_equal [@member.email], sent.to | ||
| 29 | + end | ||
| 30 | + | ||
| 31 | + should 'do not send mail if do not have notifications' do | ||
| 32 | + @community.add_member(@member) | ||
| 33 | + ActionTracker::Record.delete_all | ||
| 34 | + notify | ||
| 35 | + assert ActionMailer::Base.deliveries.empty? | ||
| 36 | + end | ||
| 37 | + | ||
| 38 | + should 'do not send mail to people not joined to community' do | ||
| 39 | + ActionMailer::Base.deliveries = [] | ||
| 40 | + Comment.create!(:author => @admin, :title => 'test comment 2', :body => 'body 2!', :source => @article) | ||
| 41 | + notify | ||
| 42 | + sent = ActionMailer::Base.deliveries.first | ||
| 43 | + assert !sent | ||
| 44 | + end | ||
| 45 | + | ||
| 46 | + should 'display author name in delivered mail' do | ||
| 47 | + @community.add_member(@member) | ||
| 48 | + Comment.create!(:author => @admin, :title => 'test comment', :body => 'body!', :source => @article) | ||
| 49 | + notify | ||
| 50 | + sent = ActionMailer::Base.deliveries.first | ||
| 51 | + assert_match /#{@admin.name}/, sent.body | ||
| 52 | + end | ||
| 53 | + | ||
| 54 | + should 'do not include comment created before last notification' do | ||
| 55 | + @community.add_member(@member) | ||
| 56 | + ActionTracker::Record.delete_all | ||
| 57 | + comment = Comment.create!(:author => @admin, :title => 'test comment', :body => 'body!', :source => @article ) | ||
| 58 | + @member.last_notification = DateTime.now + 1.day | ||
| 59 | + notify | ||
| 60 | + assert ActionMailer::Base.deliveries.empty? | ||
| 61 | + end | ||
| 62 | + | ||
| 63 | + should 'update last notification date' do | ||
| 64 | + Comment.create!(:author => @admin, :title => 'test comment 2', :body => 'body 2!', :source => @article) | ||
| 65 | + @community.add_member(@member) | ||
| 66 | + assert_equal nil, @member.last_notification | ||
| 67 | + notify | ||
| 68 | + assert @member.last_notification | ||
| 69 | + end | ||
| 70 | + | ||
| 71 | + should 'reschedule after notification' do | ||
| 72 | + Comment.create!(:author => @admin, :title => 'test comment 2', :body => 'body 2!', :source => @article) | ||
| 73 | + @community.add_member(@member) | ||
| 74 | + assert_equal nil, @member.last_notification | ||
| 75 | + notify | ||
| 76 | + assert PersonNotifier::NotifyJob.find(@member.id) | ||
| 77 | + end | ||
| 78 | + | ||
| 79 | + should 'schedule next mail at notification time' do | ||
| 80 | + @member.notification_time = 12 | ||
| 81 | + @member.notifier.schedule_next_notification_mail | ||
| 82 | + assert_equal @member.notification_time, DateTime.now.hour - Delayed::Job.first.run_at.hour | ||
| 83 | + end | ||
| 84 | + | ||
| 85 | + should 'do not schedule duplicated notification mail' do | ||
| 86 | + @member.notification_time = 12 | ||
| 87 | + @member.notifier.schedule_next_notification_mail | ||
| 88 | + @member.notifier.schedule_next_notification_mail | ||
| 89 | + assert_equal 1, Delayed::Job.count | ||
| 90 | + end | ||
| 91 | + | ||
| 92 | + should 'do not schedule next mail if notification time is zero' do | ||
| 93 | + @member.notification_time = 0 | ||
| 94 | + @member.notifier.schedule_next_notification_mail | ||
| 95 | + assert_equal 0, Delayed::Job.count | ||
| 96 | + end | ||
| 97 | + | ||
| 98 | + should 'schedule next notifications for all person with notification time greater than zero' do | ||
| 99 | + @member.notification_time = 1 | ||
| 100 | + @admin.notification_time = 0 | ||
| 101 | + @admin.save! | ||
| 102 | + @member.save! | ||
| 103 | + Delayed::Job.delete_all | ||
| 104 | + PersonNotifier.schedule_all_next_notification_mail | ||
| 105 | + process_delayed_job_queue | ||
| 106 | + assert_equal 1, Delayed::Job.count | ||
| 107 | + end | ||
| 108 | + | ||
| 109 | + should 'do not create duplicated job' do | ||
| 110 | + PersonNotifier.schedule_all_next_notification_mail | ||
| 111 | + PersonNotifier.schedule_all_next_notification_mail | ||
| 112 | + assert_equal 1, Delayed::Job.count | ||
| 113 | + end | ||
| 114 | + | ||
| 115 | + should 'schedule after update and set a valid notification time' do | ||
| 116 | + @member.notification_time = 0 | ||
| 117 | + @member.save! | ||
| 118 | + assert_equal 0, Delayed::Job.count | ||
| 119 | + @member.notification_time = 12 | ||
| 120 | + @member.save! | ||
| 121 | + assert_equal 1, Delayed::Job.count | ||
| 122 | + end | ||
| 123 | + | ||
| 124 | + should 'reschedule with changed notification time' do | ||
| 125 | + @member.notification_time = 2 | ||
| 126 | + @member.save! | ||
| 127 | + assert_equal 1, Delayed::Job.count | ||
| 128 | + @member.notification_time = 12 | ||
| 129 | + @member.save! | ||
| 130 | + assert_equal 1, Delayed::Job.count | ||
| 131 | + assert_equal @member.notification_time, DateTime.now.hour - Delayed::Job.first.run_at.hour | ||
| 132 | + end | ||
| 133 | + | ||
| 134 | + should 'display error message if fail to render a notificiation' do | ||
| 135 | + @community.add_member(@member) | ||
| 136 | + Comment.create!(:author => @admin, :title => 'test comment', :body => 'body!', :source => @article) | ||
| 137 | + ActionTracker::Record.any_instance.stubs(:verb).returns("some_invalid_verb") | ||
| 138 | + notify | ||
| 139 | + sent = ActionMailer::Base.deliveries.first | ||
| 140 | + assert_match /cannot render notification for some_invalid_verb/, sent.body | ||
| 141 | + end | ||
| 142 | + | ||
| 143 | + ActionTrackerConfig.verb_names.each do |verb| | ||
| 144 | + should "render notification for verb #{verb}" do | ||
| 145 | + action = mock() | ||
| 146 | + action.stubs(:verb).returns(verb) | ||
| 147 | + action.stubs(:user).returns(@member) | ||
| 148 | + action.stubs(:created_at).returns(DateTime.now) | ||
| 149 | + action.stubs(:target).returns(fast_create(Forum)) | ||
| 150 | + action.stubs(:comments_count).returns(0) | ||
| 151 | + action.stubs(:comments).returns([]) | ||
| 152 | + action.stubs(:params).returns({'name' => 'home', 'url' => '/', 'lead' => ''}) | ||
| 153 | + action.stubs(:get_url).returns('') | ||
| 154 | + | ||
| 155 | + notifications = [] | ||
| 156 | + notifications.stubs(:find).returns([action]) | ||
| 157 | + Person.any_instance.stubs(:tracked_notifications).returns(notifications) | ||
| 158 | + | ||
| 159 | + notify | ||
| 160 | + sent = ActionMailer::Base.deliveries.first | ||
| 161 | + assert_no_match /cannot render notification for #{verb}/, sent.body | ||
| 162 | + end | ||
| 163 | + end | ||
| 164 | + | ||
| 165 | + should 'exists? method in NotifyAllJob return false if there is no instance of this class created' do | ||
| 166 | + Delayed::Job.enqueue(PersonNotifier::NotifyJob.new) | ||
| 167 | + assert !PersonNotifier::NotifyAllJob.exists? | ||
| 168 | + end | ||
| 169 | + | ||
| 170 | + should 'exists? method in NotifyAllJob return false if there is no jobs created' do | ||
| 171 | + assert !PersonNotifier::NotifyAllJob.exists? | ||
| 172 | + end | ||
| 173 | + | ||
| 174 | + should 'exists? method in NotifyAllJob return true if there is at least one instance of this class' do | ||
| 175 | + Delayed::Job.enqueue(PersonNotifier::NotifyAllJob.new) | ||
| 176 | + assert PersonNotifier::NotifyAllJob.exists? | ||
| 177 | + end | ||
| 178 | + | ||
| 179 | + should 'perform create NotifyJob for all users with notification_time' do | ||
| 180 | + Delayed::Job.enqueue(PersonNotifier::NotifyAllJob.new) | ||
| 181 | + process_delayed_job_queue | ||
| 182 | + assert_equal 2, Delayed::Job.count | ||
| 183 | + end | ||
| 184 | + | ||
| 185 | + should 'perform create NotifyJob for all users with notification_time defined greater than zero' do | ||
| 186 | + @member.notification_time = 1 | ||
| 187 | + @admin.notification_time = 0 | ||
| 188 | + @admin.save! | ||
| 189 | + @member.save! | ||
| 190 | + Delayed::Job.delete_all | ||
| 191 | + Delayed::Job.enqueue(PersonNotifier::NotifyAllJob.new) | ||
| 192 | + process_delayed_job_queue | ||
| 193 | + assert_equal 1, Delayed::Job.count | ||
| 194 | + end | ||
| 195 | + | ||
| 196 | + should 'NotifyJob failed jobs create a new NotifyJob on permanent failure' do | ||
| 197 | + Delayed::Job.enqueue(PersonNotifier::NotifyJob.new(@member.id)) | ||
| 198 | + | ||
| 199 | + PersonNotifier.any_instance.stubs(:notify).raises('error') | ||
| 200 | + | ||
| 201 | + process_delayed_job_queue | ||
| 202 | + jobs = Delayed::Job.all | ||
| 203 | + | ||
| 204 | + assert 1, jobs.select{|j| j.failed?}.size | ||
| 205 | + assert 1, jobs.select{|j| !j.failed?}.size | ||
| 206 | + end | ||
| 207 | + | ||
| 208 | + def notify | ||
| 209 | + ActionTracker::Record.all.map{|action| Person.notify_activity(action)} | ||
| 210 | + process_delayed_job_queue | ||
| 211 | + @member.notifier.notify | ||
| 212 | + end | ||
| 213 | + | ||
| 214 | +end |
test/unit/person_test.rb
| @@ -1401,6 +1401,17 @@ class PersonTest < ActiveSupport::TestCase | @@ -1401,6 +1401,17 @@ class PersonTest < ActiveSupport::TestCase | ||
| 1401 | assert_equal person.activities, [] | 1401 | assert_equal person.activities, [] |
| 1402 | end | 1402 | end |
| 1403 | 1403 | ||
| 1404 | + should 'person notifier be PersonNotifier class' do | ||
| 1405 | + p = Person.new | ||
| 1406 | + assert p.notifier.kind_of?(PersonNotifier) | ||
| 1407 | + end | ||
| 1408 | + | ||
| 1409 | + should 'reschedule next notification after update' do | ||
| 1410 | + p = fast_create(Person, :user_id => fast_create(User).id) | ||
| 1411 | + PersonNotifier.any_instance.expects(:reschedule_next_notification_mail).once | ||
| 1412 | + assert p.update_attribute(:name, 'Person name changed') | ||
| 1413 | + end | ||
| 1414 | + | ||
| 1404 | should 'merge memberships of plugins to original memberships' do | 1415 | should 'merge memberships of plugins to original memberships' do |
| 1405 | class Plugin1 < Noosfero::Plugin | 1416 | class Plugin1 < Noosfero::Plugin |
| 1406 | def person_memberships(person) | 1417 | def person_memberships(person) |