From 70ccb75cd5f2506f9617c991ee8ff62f28a76836 Mon Sep 17 00:00:00 2001 From: Joenio Costa Date: Wed, 22 Aug 2012 18:56:19 -0300 Subject: [PATCH] Send e-Mail feature to all members of a community --- app/controllers/public/profile_controller.rb | 18 +++++++++++++++++- app/models/profile.rb | 1 + app/views/profile/members.rhtml | 9 +++++++-- app/views/profile/send_mail.rhtml | 14 ++++++++++++++ features/send_email_to_organization_members.feature | 37 ++++++++++++++++++++++++++++++++++++- features/step_definitions/noosfero_steps.rb | 6 ++++++ features/support/paths.rb | 3 +++ test/fixtures/roles.yml | 1 + test/functional/profile_controller_test.rb | 38 ++++++++++++++++++++++++++++++++++++++ 9 files changed, 123 insertions(+), 4 deletions(-) create mode 100644 app/views/profile/send_mail.rhtml diff --git a/app/controllers/public/profile_controller.rb b/app/controllers/public/profile_controller.rb index 2b40be7..712c45e 100644 --- a/app/controllers/public/profile_controller.rb +++ b/app/controllers/public/profile_controller.rb @@ -2,11 +2,13 @@ class ProfileController < PublicController needs_profile before_filter :check_access_to_profile, :except => [:join, :join_not_logged, :index, :add] - before_filter :store_location, :only => [:join, :join_not_logged, :report_abuse] + before_filter :store_location, :only => [:join, :join_not_logged, :report_abuse, :send_mail] before_filter :login_required, :only => [:add, :join, :join_not_logged, :leave, :unblock, :leave_scrap, :remove_scrap, :remove_activity, :view_more_activities, :view_more_network_activities, :report_abuse, :register_report, :leave_comment_on_activity] helper TagsHelper + protect 'send_mail_to_members', :profile, :only => [:send_mail] + def index @network_activities = !@profile.is_a?(Person) ? @profile.tracked_notifications.visible.paginate(:per_page => 15, :page => params[:page]) : [] if logged_in? && current_person.follows?(@profile) @@ -327,6 +329,20 @@ class ProfileController < PublicController end end + def send_mail + @mailing = profile.mailings.build(params[:mailing]) + if request.post? + @mailing.locale = locale + @mailing.person = user + if @mailing.save + session[:notice] = _('The e-mails are being sent') + redirect_to_previous_location + else + session[:notice] = _('Could not create the e-mail') + end + end + end + protected def check_access_to_profile diff --git a/app/models/profile.rb b/app/models/profile.rb index b0bd67d..0ef2d63 100644 --- a/app/models/profile.rb +++ b/app/models/profile.rb @@ -57,6 +57,7 @@ class Profile < ActiveRecord::Base 'view_private_content' => N_('View private content'), 'publish_content' => N_('Publish content'), 'invite_members' => N_('Invite members'), + 'send_mail_to_members' => N_('Send e-Mail to members'), } acts_as_accessible diff --git a/app/views/profile/members.rhtml b/app/views/profile/members.rhtml index 50ef8d6..3639c52 100644 --- a/app/views/profile/members.rhtml +++ b/app/views/profile/members.rhtml @@ -16,8 +16,13 @@ <% button_bar do %> <%= button :back, _('Go back'), { :controller => 'profile' } %> - <% if profile.community? and user and user.has_permission?(:invite_members, profile) %> - <%= button :search, _('Invite your friends to join %s') % profile.name, :controller => 'invite', :action => 'select_address_book' %> + <% if profile.community? and user %> + <% if user.has_permission?(:invite_members, profile) %> + <%= button :search, _('Invite your friends to join %s') % profile.name, :controller => 'invite', :action => 'select_address_book' %> + <% end %> + <% if user.has_permission?(:send_mail_to_members, profile) %> + <%= button :send, _('Send e-mail to members'), :controller => 'profile', :action => 'send_mail' %> + <% end %> <% end %> <% end %> diff --git a/app/views/profile/send_mail.rhtml b/app/views/profile/send_mail.rhtml new file mode 100644 index 0000000..7bc068e --- /dev/null +++ b/app/views/profile/send_mail.rhtml @@ -0,0 +1,14 @@ +

<%= h profile.short_name(50) %>

+ +

<%= _('Send e-mail to members') %>

+ +<%= error_messages_for :mailing %> + +<%= render :file => 'shared/tiny_mce' %> + +<% form_for :mailing, :url => {:action => 'send_mail'}, :html => {:id => 'mailing-form'} do |f| %> + <%= labelled_form_field(_('Subject:'), f.text_field(:subject)) %> + <%= labelled_form_field(_('Body:'), f.text_area(:body, :class => 'mceEditor')) %> + <%= submit_button(:send, _('Send')) %> + <%= button :cancel, _('Cancel e-mail'), :action => 'members' %> +<% end %> diff --git a/features/send_email_to_organization_members.feature b/features/send_email_to_organization_members.feature index 448c884..cdc8ceb 100644 --- a/features/send_email_to_organization_members.feature +++ b/features/send_email_to_organization_members.feature @@ -1,15 +1,19 @@ Feature: send emails to organization members - As a organization administrator + As a organization administrator or moderator I want to send email to all members Background: Given the following users | login | name | | joaosilva | Joao Silva | + | jose | Jose Silva | + | manoel | Manoel Silva | And the following communities | identifier | name | | sample-community | Sample Community | And "Joao Silva" is admin of "Sample Community" + And "Jose Silva" is moderator of "Sample Community" + And "Manoel Silva" is a member of "Sample Community" Scenario: Cant access if not logged in Given I am not logged in @@ -55,3 +59,34 @@ Feature: send emails to organization members And I follow "Send e-mail to members" When I follow "Cancel e-mail" Then I should be on Sample Community's members management + + Scenario: Cant access if has no send_mail_to_members permission + Given I am logged in as "manoel" + When I go to /profile/sample-community/send_mail + Then I should see "Access denied" + + Scenario: Show button "Send e-Mail to members" of community to an moderator + Given I am logged in as "jose" + When I go to Sample Community's members page + Then I should see "Send e-mail to members" link + + Scenario: Not show button "Send e-Mail to members" if user has no right permission + Given I am logged in as "manoel" + When I go to Sample Community's members page + Then I should not see "Send e-mail to members" link + + Scenario: Redirect back to profile members page after send mail + Given I am logged in as "jose" + When I go to Sample Community's members page + And I follow "Send e-mail to members" + And I fill in "Subject" with "Hello, member!" + And I fill in "body" with "We have some news" + When I press "Send" + Then I should be on Sample Community's members page + + Scenario: Back to profile members page after cancel creation of mailing + Given I am logged in as "jose" + And I go to Sample Community's members page + And I follow "Send e-mail to members" + When I follow "Cancel e-mail" + Then I should be on Sample Community's members page diff --git a/features/step_definitions/noosfero_steps.rb b/features/step_definitions/noosfero_steps.rb index d1b362e..82473c5 100644 --- a/features/step_definitions/noosfero_steps.rb +++ b/features/step_definitions/noosfero_steps.rb @@ -355,6 +355,12 @@ Given /^"(.+)" is admin of "(.+)"$/ do |person, organization| org.add_admin(user) end +Given /^"(.+)" is moderator of "(.+)"$/ do |person, organization| + org = Profile.find_by_name(organization) + user = Profile.find_by_name(person) + org.add_moderator(user) +end + Then /^"(.+)" should be admin of "(.+)"$/ do |person, organization| org = Organization.find_by_name(organization) user = Person.find_by_name(person) diff --git a/features/support/paths.rb b/features/support/paths.rb index f940dc1..ac707c8 100644 --- a/features/support/paths.rb +++ b/features/support/paths.rb @@ -108,6 +108,9 @@ module NavigationHelpers when /the user data path/ '/account/user_data' + when /^(.+)'s members page/ + '/profile/%s/members' % Profile.find_by_name($1).identifier + # Add more mappings here. # Here is a more fancy example: # diff --git a/test/fixtures/roles.yml b/test/fixtures/roles.yml index 76f3344..8b8da4a 100644 --- a/test/fixtures/roles.yml +++ b/test/fixtures/roles.yml @@ -70,6 +70,7 @@ profile_moderator: permissions: - moderate_comments - view_private_content + - send_mail_to_members environment_administrator: id: 8 environment_id: 1 diff --git a/test/functional/profile_controller_test.rb b/test/functional/profile_controller_test.rb index 196d760..c9072f9 100644 --- a/test/functional/profile_controller_test.rb +++ b/test/functional/profile_controller_test.rb @@ -1324,4 +1324,42 @@ class ProfileControllerTest < ActionController::TestCase assert_tag :tag => 'span', :content => '(unauthenticated user)', :attributes => {:class => 'comment-user-status comment-user-status-wall icon-user-unknown'} end + + should 'add locale on mailing' do + community = fast_create(Community) + create_user_with_permission('profile_moderator_user', 'send_mail_to_members', community) + login_as('profile_moderator_user') + @controller.stubs(:locale).returns('pt') + post :send_mail, :profile => community.identifier, :mailing => {:subject => 'Hello', :body => 'We have some news'} + assert_equal 'pt', assigns(:mailing).locale + end + + should 'queue mailing to process later' do + community = fast_create(Community) + create_user_with_permission('profile_moderator_user', 'send_mail_to_members', community) + login_as('profile_moderator_user') + @controller.stubs(:locale).returns('pt') + assert_difference Delayed::Job, :count, 1 do + post :send_mail, :profile => community.identifier, :mailing => {:subject => 'Hello', :body => 'We have some news'} + end + end + + should 'save mailing' do + community = fast_create(Community) + create_user_with_permission('profile_moderator_user', 'send_mail_to_members', community) + login_as('profile_moderator_user') + @controller.stubs(:locale).returns('pt') + post :send_mail, :profile => community.identifier, :mailing => {:subject => 'Hello', :body => 'We have some news'} + assert_equal ['Hello', 'We have some news'], [assigns(:mailing).subject, assigns(:mailing).body] + assert_redirected_to :action => 'members' + end + + should 'add the user logged on mailing' do + community = fast_create(Community) + create_user_with_permission('profile_moderator_user', 'send_mail_to_members', community) + login_as('profile_moderator_user') + post :send_mail, :profile => community.identifier, :mailing => {:subject => 'Hello', :body => 'We have some news'} + assert_equal Profile['profile_moderator_user'], assigns(:mailing).person + end + end -- libgit2 0.21.2