diff --git a/app/controllers/admin/users_controller.rb b/app/controllers/admin/users_controller.rb
index 1fb95cd..f0e7869 100644
--- a/app/controllers/admin/users_controller.rb
+++ b/app/controllers/admin/users_controller.rb
@@ -19,4 +19,18 @@ class UsersController < AdminController
end
end
+ def send_mail
+ @mailing = environment.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 :action => 'index'
+ else
+ session[:notice] = _('Could not create the e-mail')
+ end
+ end
+ end
+
end
diff --git a/app/controllers/my_profile/profile_members_controller.rb b/app/controllers/my_profile/profile_members_controller.rb
index 2a2bfb2..e760c49 100644
--- a/app/controllers/my_profile/profile_members_controller.rb
+++ b/app/controllers/my_profile/profile_members_controller.rb
@@ -92,4 +92,18 @@ class ProfileMembersController < MyProfileController
render :layout => false
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 :action => 'index'
+ else
+ session[:notice] = _('Could not create the e-mail')
+ end
+ end
+ end
+
end
diff --git a/app/models/environment.rb b/app/models/environment.rb
index 1664168..ff7dd91 100644
--- a/app/models/environment.rb
+++ b/app/models/environment.rb
@@ -158,6 +158,8 @@ class Environment < ActiveRecord::Base
has_many :qualifiers
has_many :certifiers
+ has_many :mailings, :class_name => 'EnvironmentMailing', :foreign_key => :source_id, :as => 'source'
+
acts_as_accessible
def superior_intances
diff --git a/app/models/environment_mailing.rb b/app/models/environment_mailing.rb
new file mode 100644
index 0000000..329ab2d
--- /dev/null
+++ b/app/models/environment_mailing.rb
@@ -0,0 +1,24 @@
+class EnvironmentMailing < Mailing
+
+ def recipient(offset=0)
+ Person.find(:first, :conditions => [ "environment_id = ?", source_id], :order => :id, :offset => offset)
+ end
+
+ def each_recipient
+ offset = 0
+ while person = recipient(offset)
+ unless self.already_sent_mailing_to?(person)
+ yield person
+ end
+ offset = offset + 1
+ end
+ end
+
+ def signature_message
+ _('Sent by Noosfero %s.') % source.name
+ end
+
+ def url
+ source.top_url
+ end
+end
diff --git a/app/models/mailing.rb b/app/models/mailing.rb
new file mode 100644
index 0000000..81b848b
--- /dev/null
+++ b/app/models/mailing.rb
@@ -0,0 +1,56 @@
+class Mailing < ActiveRecord::Base
+
+ validates_presence_of :source_id, :subject, :body
+ belongs_to :source, :foreign_key => :source_id, :polymorphic => true
+ belongs_to :person
+
+ has_many :mailing_sents
+
+ xss_terminate :only => [ :subject, :body ], :with => 'white_list', :on => 'validation'
+
+ after_create do |mailing|
+ Delayed::Job.enqueue MailingJob.new(mailing.id)
+ end
+
+ def generate_from
+ "#{source.name} <#{source.contact_email}>"
+ end
+
+ def generate_subject
+ '[%s] %s' % [source.name, subject]
+ end
+
+ def signature_message
+ _('Sent by Noosfero.')
+ end
+
+ def url
+ ''
+ end
+
+ def already_sent_mailing_to?(recipient)
+ self.mailing_sents.find_by_person_id(recipient.id)
+ end
+
+ def deliver
+ offset = 0
+ people = []
+ each_recipient do |recipient|
+ Mailing::Sender.deliver_mail(self, recipient.email)
+ self.mailing_sents.create(:person => recipient)
+ end
+ end
+
+ class Sender < ActionMailer::Base
+ def mail(mailing, recipient)
+ content_type 'text/html'
+ recipients recipient
+ from mailing.generate_from
+ reply_to mailing.person.email
+ subject mailing.generate_subject
+ body :message => mailing.body,
+ :signature_message => mailing.signature_message,
+ :url => mailing.url
+ end
+ end
+end
diff --git a/app/models/mailing_sent.rb b/app/models/mailing_sent.rb
new file mode 100644
index 0000000..ef134b1
--- /dev/null
+++ b/app/models/mailing_sent.rb
@@ -0,0 +1,4 @@
+class MailingSent < ActiveRecord::Base
+ belongs_to :mailing
+ belongs_to :person
+end
diff --git a/app/models/organization.rb b/app/models/organization.rb
index 57d49b8..3f68a57 100644
--- a/app/models/organization.rb
+++ b/app/models/organization.rb
@@ -19,6 +19,8 @@ class Organization < Profile
has_many :validations, :class_name => 'CreateEnterprise', :foreign_key => :target_id
+ has_many :mailings, :class_name => 'OrganizationMailing', :foreign_key => :source_id, :as => 'source'
+
def validation_methodology
self.validation_info ? self.validation_info.validation_methodology : nil
end
diff --git a/app/models/organization_mailing.rb b/app/models/organization_mailing.rb
new file mode 100644
index 0000000..63a0bcd
--- /dev/null
+++ b/app/models/organization_mailing.rb
@@ -0,0 +1,30 @@
+class OrganizationMailing < Mailing
+
+ def generate_from
+ "#{person.name} <#{source.environment.contact_email}>"
+ end
+
+ def recipient(offset=0)
+ environment_id = source.environment_id
+ Person.find(:first, :conditions => ['environment_id = ? and role_assignments.resource_type = ? and role_assignments.resource_id = ?', environment_id, 'Profile', source.id], :include => :role_assignments, :order => "profiles.id", :offset => offset)
+ end
+
+ def each_recipient
+ offset = 0
+ while person = recipient(offset)
+ unless self.already_sent_mailing_to?(person)
+ yield person
+ end
+ offset = offset + 1
+ end
+ end
+
+ def signature_message
+ _('Sent by community %s.') % source.name
+ end
+
+ include ActionController::UrlWriter
+ def url
+ url_for(source.url)
+ end
+end
diff --git a/app/models/person.rb b/app/models/person.rb
index 9fce90a..b252c8b 100644
--- a/app/models/person.rb
+++ b/app/models/person.rb
@@ -14,6 +14,8 @@ class Person < Profile
has_many :requested_tasks, :class_name => 'Task', :foreign_key => :requestor_id, :dependent => :destroy
+ has_many :mailings
+
named_scope :more_popular,
:select => "#{Profile.qualified_column_names}, count(friend_id) as total",
:group => Profile.qualified_column_names,
diff --git a/app/models/profile.rb b/app/models/profile.rb
index 8cec437..6a698f3 100644
--- a/app/models/profile.rb
+++ b/app/models/profile.rb
@@ -587,9 +587,9 @@ private :generate_url, :url_options
end
include ActionView::Helpers::TextHelper
- def short_name
+ def short_name(chars = 15)
if self[:nickname].blank?
- truncate self.name, 15, '...'
+ truncate self.name, chars, '...'
else
self[:nickname]
end
diff --git a/app/views/mailing/sender/mail.rhtml b/app/views/mailing/sender/mail.rhtml
new file mode 100644
index 0000000..dc92944
--- /dev/null
+++ b/app/views/mailing/sender/mail.rhtml
@@ -0,0 +1,17 @@
+
+
+
+
+
+
+ <%= word_wrap(@message) %>
+
+ --
+ <%= @signature_message %>
+ <%= @url %>
+
+
+
+
+
+
diff --git a/app/views/profile_members/_index_buttons.rhtml b/app/views/profile_members/_index_buttons.rhtml
new file mode 100644
index 0000000..49e1f19
--- /dev/null
+++ b/app/views/profile_members/_index_buttons.rhtml
@@ -0,0 +1,8 @@
+<% button_bar do %>
+ <%= button :back, _('Back'), :controller => 'profile_editor' %>
+ <%= button :add, _('Add members'), :action => 'add_members' if profile.enterprise? %>
+ <% if profile.community? and user.has_permission?(:invite_members, profile) %>
+ <%= button :search, _('Invite your friends to join %s') % profile.short_name, :controller => 'invite', :action => 'select_address_book' %>
+ <% end %>
+ <%= button :send, _('Send e-mail to members'), :action => 'send_mail' %>
+<% end %>
diff --git a/app/views/profile_members/index.rhtml b/app/views/profile_members/index.rhtml
index 9892e8e..62b7b77 100644
--- a/app/views/profile_members/index.rhtml
+++ b/app/views/profile_members/index.rhtml
@@ -1,11 +1,9 @@
+<%= h profile.short_name(50) %>
+
+<%= render :partial => 'index_buttons' %>
+
<%= render :partial => 'members_list' %>
-<% button_bar do %>
- <%= button :back, _('Back'), :controller => 'profile_editor' %>
- <%= button :add, _('Add members'), :action => 'add_members' if profile.enterprise? %>
- <% if profile.community? and user.has_permission?(:invite_members, profile) %>
- <%= button :search, _('Invite your friends to join %s') % profile.name, :controller => 'invite', :action => 'select_address_book' %>
- <% end %>
-<% end %>
+<%= render :partial => 'index_buttons' %>
diff --git a/app/views/profile_members/send_mail.rhtml b/app/views/profile_members/send_mail.rhtml
new file mode 100644
index 0000000..63deffa
--- /dev/null
+++ b/app/views/profile_members/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)) %>
+ <%= submit_button(:send, _('Send')) %>
+ <%= button :cancel, _('Cancel e-mail'), :action => 'index' %>
+<% end %>
diff --git a/app/views/users/index.rhtml b/app/views/users/index.rhtml
index 1508a38..f40f241 100644
--- a/app/views/users/index.rhtml
+++ b/app/views/users/index.rhtml
@@ -2,9 +2,12 @@
-
- <%= _('Download users list') %>:
- <%= link_to '[CSV]', :format => 'csv' %>
- <%= link_to '[XML]', :format => 'xml' %>
+ <%= _('Download users list') %>:
+ <%= link_to '[CSV]', :format => 'csv' %>
+ <%= link_to '[XML]', :format => 'xml' %>
+
+ -
+ <%= link_to _('Send e-mail to users'), :action => 'send_mail' %>
diff --git a/app/views/users/send_mail.rhtml b/app/views/users/send_mail.rhtml
new file mode 100644
index 0000000..180109c
--- /dev/null
+++ b/app/views/users/send_mail.rhtml
@@ -0,0 +1,12 @@
+<%= _('Send e-mail to users') %>
+
+<%= error_messages_for :mailing %>
+
+<%= render :file => 'shared/tiny_mce' %>
+
+<% form_for :mailing, :url => {:action => 'send_mail'} do |f| %>
+ <%= labelled_form_field(_('Subject:'), f.text_field(:subject)) %>
+ <%= labelled_form_field(_('Body:'), f.text_area(:body)) %>
+ <%= submit_button(:send, _('Send')) %>
+ <%= button :cancel, _('Cancel e-mail'), :controller => 'users' %>
+<% end %>
diff --git a/db/migrate/20100902202247_create_mailings.rb b/db/migrate/20100902202247_create_mailings.rb
new file mode 100644
index 0000000..955c2a7
--- /dev/null
+++ b/db/migrate/20100902202247_create_mailings.rb
@@ -0,0 +1,18 @@
+class CreateMailings < ActiveRecord::Migration
+ def self.up
+ create_table :mailings do |t|
+ t.string :type
+ t.string :subject
+ t.text :body
+ t.integer :source_id
+ t.string :source_type
+ t.references :person
+ t.string :locale
+ t.timestamps
+ end
+ end
+
+ def self.down
+ drop_table :mailings
+ end
+end
diff --git a/db/migrate/20100909103951_create_mailing_sents.rb b/db/migrate/20100909103951_create_mailing_sents.rb
new file mode 100644
index 0000000..3467678
--- /dev/null
+++ b/db/migrate/20100909103951_create_mailing_sents.rb
@@ -0,0 +1,13 @@
+class CreateMailingSents < ActiveRecord::Migration
+ def self.up
+ create_table :mailing_sents do |t|
+ t.references :mailing
+ t.references :person
+ t.timestamps
+ end
+ end
+
+ def self.down
+ drop_table :mailing_sents
+ end
+end
diff --git a/db/schema.rb b/db/schema.rb
index 2dd8b8a..cc7b5e2 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -9,7 +9,7 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20100901203836) do
+ActiveRecord::Schema.define(:version => 20100909103951) do
create_table "article_versions", :force => true do |t|
t.integer "article_id"
@@ -261,6 +261,25 @@ ActiveRecord::Schema.define(:version => 20100901203836) do
t.boolean "is_from_solidarity_economy", :default => false
end
+ create_table "mailing_sents", :force => true do |t|
+ t.integer "mailing_id"
+ t.integer "person_id"
+ t.datetime "created_at"
+ t.datetime "updated_at"
+ end
+
+ create_table "mailings", :force => true do |t|
+ t.string "type"
+ t.string "subject"
+ t.text "body"
+ t.integer "source_id"
+ t.string "source_type"
+ t.integer "person_id"
+ t.string "locale"
+ t.datetime "created_at"
+ t.datetime "updated_at"
+ end
+
create_table "product_categorizations", :force => true do |t|
t.integer "category_id"
t.integer "product_id"
diff --git a/features/send_email_to_environment_members.feature b/features/send_email_to_environment_members.feature
new file mode 100644
index 0000000..a3a108b
--- /dev/null
+++ b/features/send_email_to_environment_members.feature
@@ -0,0 +1,53 @@
+Feature: send emails to environment members users
+ As an administrator
+ I want to send email to all users
+
+ Scenario: Cant access if not logged in
+ Given I am not logged in
+ When I go to /admin/users/send_mail
+ Then I should see "Access denied"
+
+ Scenario: Cant access as normal user
+ Given the following user
+ | login |
+ | ultraje |
+ And I am logged in as "ultraje"
+ When I go to /admin/users/send_mail
+ Then I should see "Access denied"
+
+ Scenario: Send e-mail to members
+ Given I am logged in as admin
+ When I follow "Administration"
+ And I follow "Manage users"
+ And I follow "Send e-mail to users"
+ And I fill in "Subject" with "Hello, user!"
+ And I fill in "body" with "We have some news"
+ When I press "Send"
+ Then I should be on /admin/users
+
+ Scenario: Not send e-mail to members if subject is blank
+ Given I am logged in as admin
+ When I follow "Administration"
+ And I follow "Manage users"
+ And I follow "Send e-mail to users"
+ And I fill in "body" with "We have some news"
+ When I press "Send"
+ Then I should be on /admin/users/send_mail
+
+ Scenario: Not send e-mail to members if body is blank
+ Given I am logged in as admin
+ When I follow "Administration"
+ And I follow "Manage users"
+ And I follow "Send e-mail to users"
+ And I fill in "Subject" with "Hello, user!"
+ When I press "Send"
+ Then I should be on /admin/users/send_mail
+
+ Scenario: Cancel creation of mailing
+ Given I am logged in as admin
+ When I follow "Administration"
+ And I follow "Manage users"
+ And I follow "Send e-mail to users"
+ Then I should be on /admin/users/send_mail
+ When I follow "Cancel e-mail"
+ Then I should be on /admin/users
diff --git a/features/send_email_to_organization_members.feature b/features/send_email_to_organization_members.feature
new file mode 100644
index 0000000..448c884
--- /dev/null
+++ b/features/send_email_to_organization_members.feature
@@ -0,0 +1,57 @@
+Feature: send emails to organization members
+ As a organization administrator
+ I want to send email to all members
+
+ Background:
+ Given the following users
+ | login | name |
+ | joaosilva | Joao Silva |
+ And the following communities
+ | identifier | name |
+ | sample-community | Sample Community |
+ And "Joao Silva" is admin of "Sample Community"
+
+ Scenario: Cant access if not logged in
+ Given I am not logged in
+ When I go to /myprofile/sample-community/profile_members/send_mail
+ Then I should be on login page
+
+ Scenario: Cant access as normal user
+ Given the following user
+ | login |
+ | josesilva |
+ And I am logged in as "josesilva"
+ When I go to /myprofile/sample-community/profile_members/send_mail
+ Then I should see "Access denied"
+
+ Scenario: Send e-mail to members
+ Given I am logged in as "joaosilva"
+ And I go to Sample Community's members management
+ 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 management
+
+ Scenario: Not send e-mail to members if subject is blank
+ Given I am logged in as "joaosilva"
+ And I go to Sample Community's members management
+ And I follow "Send e-mail to members"
+ And I fill in "body" with "We have some news"
+ When I press "Send"
+ Then I should be on /myprofile/sample-community/profile_members/send_mail
+
+ Scenario: Not send e-mail to members if body is blank
+ Given I am logged in as "joaosilva"
+ And I go to Sample Community's members management
+ And I follow "Send e-mail to members"
+ And I fill in "Subject" with "Hello, user!"
+ When I press "Send"
+ Then I should be on /myprofile/sample-community/profile_members/send_mail
+
+ Scenario: Cancel creation of mailing
+ Given I am logged in as "joaosilva"
+ And I go to Sample Community's members management
+ And I follow "Send e-mail to members"
+ When I follow "Cancel e-mail"
+ Then I should be on Sample Community's members management
diff --git a/lib/mailing_job.rb b/lib/mailing_job.rb
new file mode 100644
index 0000000..04d6b32
--- /dev/null
+++ b/lib/mailing_job.rb
@@ -0,0 +1,8 @@
+class MailingJob < Struct.new(:mailing_id)
+ def perform
+ mailing = Mailing.find(mailing_id)
+ Noosfero.with_locale(mailing.locale) do
+ mailing.deliver
+ end
+ end
+end
diff --git a/public/stylesheets/application.css b/public/stylesheets/application.css
index b5d76e4..b30ecea 100644
--- a/public/stylesheets/application.css
+++ b/public/stylesheets/application.css
@@ -3640,6 +3640,11 @@ h1#agenda-title {
text-align: left;
}
+.controller-profile_members #mailing-form .formfield textarea,
+.controller-profile_members #mailing-form .type-text input {
+ width: 100%;
+}
+
/* ==> public/stylesheets/controller_search.css <== */
/* @import url(pagination.css); ALREADY INCLUDED ABOVE */
diff --git a/test/functional/profile_members_controller_test.rb b/test/functional/profile_members_controller_test.rb
index 12531df..3a7b82e 100644
--- a/test/functional/profile_members_controller_test.rb
+++ b/test/functional/profile_members_controller_test.rb
@@ -280,4 +280,31 @@ class ProfileMembersControllerTest < Test::Unit::TestCase
assert p_roles, [r]
end
+ should 'add locale on mailing' do
+ community = fast_create(Community)
+ admin_user = create_user_with_permission('profile_admin_user', 'manage_memberships', community)
+ login_as('profile_admin_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 'save mailing' do
+ community = fast_create(Community)
+ admin_user = create_user_with_permission('profile_admin_user', 'manage_memberships', community)
+ login_as('profile_admin_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 => 'index'
+ end
+
+ should 'add the user logged on mailing' do
+ community = fast_create(Community)
+ admin_user = create_user_with_permission('profile_admin_user', 'manage_memberships', community)
+ login_as('profile_admin_user')
+ post :send_mail, :profile => community.identifier, :mailing => {:subject => 'Hello', :body => 'We have some news'}
+ assert_equal Profile['profile_admin_user'], assigns(:mailing).person
+ end
+
end
diff --git a/test/unit/environment_mailing_test.rb b/test/unit/environment_mailing_test.rb
new file mode 100644
index 0000000..31c84a7
--- /dev/null
+++ b/test/unit/environment_mailing_test.rb
@@ -0,0 +1,97 @@
+require File.dirname(__FILE__) + '/../test_helper'
+
+class EnvironmentMailingTest < ActiveSupport::TestCase
+
+ def setup
+ ActionMailer::Base.delivery_method = :test
+ ActionMailer::Base.perform_deliveries = true
+ ActionMailer::Base.deliveries = []
+ @environment = fast_create(Environment, :name => 'Network')
+ create_user('user_one', :environment_id => @environment.id)
+ create_user('user_two', :environment_id => @environment.id)
+ end
+ attr_reader :environment
+
+
+ should 'require source_id' do
+ mailing = EnvironmentMailing.new
+ mailing.valid?
+ assert mailing.errors.invalid?(:source_id)
+
+ mailing.source_id = environment.id
+ mailing.valid?
+ assert !mailing.errors.invalid?(:source_id)
+ end
+
+ should 'return environment name' do
+ mailing = EnvironmentMailing.new(:source => environment)
+ assert_equal environment.name, mailing.source.name
+ end
+
+ should 'return environment with source_id' do
+ mailing = EnvironmentMailing.new(:source => environment)
+ assert_equal environment, mailing.source
+ end
+
+ should 'return signature message' do
+ mailing = EnvironmentMailing.new(:source => environment)
+ assert_equal 'Sent by Noosfero Network.', mailing.signature_message
+ end
+
+ should 'return url for environment on url' do
+ mailing = EnvironmentMailing.new(:source => environment)
+ environment.domains << Domain.create(:name => 'noosfero.net', :is_default => true)
+ assert_equal 'http://noosfero.net', mailing.url
+ end
+
+ should 'display name and email on generate_from' do
+ person = Person['user_one']
+ mailing = EnvironmentMailing.new(:source => environment, :person => person)
+ assert_equal "#{environment.name} <#{environment.contact_email}>", mailing.generate_from
+ end
+
+ should 'deliver mailing to each recipient after create' do
+ person = Person['user_one']
+ mailing = EnvironmentMailing.create(:source => environment, :subject => 'Hello', :body => 'We have some news', :person => person)
+ process_delayed_job_queue
+ assert_equal 2, ActionMailer::Base.deliveries.count
+ end
+
+ should 'create mailing sent to each recipient after delivering mailing' do
+ person = Person['user_one']
+ mailing = EnvironmentMailing.create(:source => environment, :subject => 'Hello', :body => 'We have some news', :person => person)
+ assert_difference MailingSent, :count, 2 do
+ process_delayed_job_queue
+ end
+ end
+
+ should 'change locale according to the mailing locale' do
+ person = Person['user_one']
+ mailing = EnvironmentMailing.create(:source => environment, :subject => 'Hello', :body => 'We have some news', :locale => 'pt', :person => person)
+ Noosfero.expects(:with_locale).with('pt')
+ process_delayed_job_queue
+ end
+
+ should 'return recipient' do
+ mailing = EnvironmentMailing.new(:source => environment)
+ assert_equal Person['user_one'], mailing.recipient
+ end
+
+ should 'return true if already sent mailing to a recipient' do
+ person = Person['user_one']
+ mailing = EnvironmentMailing.create(:source => environment, :subject => 'Hello', :body => 'We have some news', :person => person)
+ process_delayed_job_queue
+
+ assert mailing.already_sent_mailing_to?(person)
+ end
+
+ should 'return false if did not sent mailing to a recipient' do
+ recipient = fast_create(Person)
+ person = Person['user_one']
+ mailing = EnvironmentMailing.create(:source => environment, :subject => 'Hello', :body => 'We have some news', :person => person)
+ process_delayed_job_queue
+
+ assert !mailing.already_sent_mailing_to?(recipient)
+ end
+
+end
diff --git a/test/unit/mailing_job_test.rb b/test/unit/mailing_job_test.rb
new file mode 100644
index 0000000..9b0e32d
--- /dev/null
+++ b/test/unit/mailing_job_test.rb
@@ -0,0 +1,23 @@
+require File.dirname(__FILE__) + '/../test_helper'
+
+class MailingJobTest < ActiveSupport::TestCase
+
+ def setup
+ @environment = fast_create(Environment)
+ @person_1 = create_user('user_one', :environment_id => @environment.id).person
+ create_user('user_two', :environment_id => @environment.id)
+ end
+ attr_reader :environment
+
+ should 'create delayed job' do
+ assert_difference Delayed::Job, :count, 1 do
+ mailing = EnvironmentMailing.create(:source_id => environment.id, :subject => 'Hello', :body => 'We have some news', :person => @person_1)
+ end
+ end
+
+ should 'change locale according to the locale informed' do
+ mailing = EnvironmentMailing.create(:source_id => environment.id, :subject => 'Hello', :body => 'We have some news', :locale => 'pt', :person => @person_1)
+ Noosfero.expects(:with_locale).with('pt')
+ process_delayed_job_queue
+ end
+end
diff --git a/test/unit/mailing_sent_test.rb b/test/unit/mailing_sent_test.rb
new file mode 100644
index 0000000..655d319
--- /dev/null
+++ b/test/unit/mailing_sent_test.rb
@@ -0,0 +1,13 @@
+require File.dirname(__FILE__) + '/../test_helper'
+
+class MailingSentTest < ActiveSupport::TestCase
+
+ should 'return mailing and person' do
+ person = fast_create(Person)
+ mailing = Mailing.create(:source => Environment.default, :subject => 'Hello', :body => 'We have some news')
+ sent = MailingSent.create(:mailing => mailing, :person => person)
+
+ mailing_sent = MailingSent.find(sent.id)
+ assert_equal [mailing, person], [mailing_sent.mailing, mailing_sent.person]
+ end
+end
diff --git a/test/unit/mailing_test.rb b/test/unit/mailing_test.rb
new file mode 100644
index 0000000..2191bd1
--- /dev/null
+++ b/test/unit/mailing_test.rb
@@ -0,0 +1,102 @@
+require File.dirname(__FILE__) + '/../test_helper'
+
+class MailingTest < ActiveSupport::TestCase
+
+ def setup
+ ActionMailer::Base.deliveries = []
+ @environment = fast_create(Environment, :name => 'Network')
+ create_user('user_one', :environment_id => @environment.id)
+ create_user('user_two', :environment_id => @environment.id)
+ end
+ attr_reader :environment
+
+ should 'source be able to polymorphic relationship' do
+ m = Mailing.new
+ assert_nothing_raised do
+ m.source = Environment.new
+ end
+ assert_nothing_raised do
+ m.source = Profile.new
+ end
+ end
+
+ should 'require source_id' do
+ mailing = Mailing.new
+ mailing.valid?
+ assert mailing.errors.invalid?(:source_id)
+
+ mailing.source_id = Environment.default.id
+ mailing.valid?
+ assert !mailing.errors.invalid?(:source_id)
+ end
+
+ should 'require subject' do
+ mailing = Mailing.new
+ mailing.valid?
+ assert mailing.errors.invalid?(:subject)
+
+ mailing.subject = 'Hello :)'
+ mailing.valid?
+ assert !mailing.errors.invalid?(:subject)
+ end
+
+ should 'require body' do
+ mailing = Mailing.new
+ mailing.valid?
+ assert mailing.errors.invalid?(:body)
+
+ mailing.body = 'We have some news!'
+ mailing.valid?
+ assert !mailing.errors.invalid?(:body)
+ end
+
+ should 'return source' do
+ mailing = Mailing.create(:source => environment, :subject => 'Hello', :body => 'We have some news')
+ assert_equal environment, Mailing.find(mailing.id).source
+ end
+
+ should 'return source name' do
+ mailing = Mailing.new(:source => environment)
+ assert_equal environment.name, mailing.source.name
+ end
+
+ should 'return source with source_id' do
+ mailing = Mailing.new(:source => environment)
+ assert_equal environment, mailing.source
+ end
+
+ should 'return person with person_id' do
+ person = Person['user_one']
+ mailing = Mailing.new(:source => environment, :person => person)
+ assert_equal person, mailing.person
+ end
+
+ should 'display name and email on generate_from' do
+ person = Person['user_one']
+ mailing = Mailing.new(:source => environment, :person => person)
+ assert_equal "#{environment.name} <#{environment.contact_email}>", mailing.generate_from
+ end
+
+ should 'generate subject' do
+ mailing = Mailing.new(:source => environment, :subject => 'Hello :)')
+ assert_equal "[#{environment.name}] #{mailing.subject}", mailing.generate_subject
+ end
+
+ should 'return signature message' do
+ mailing = Mailing.new(:source => environment)
+ assert_equal 'Sent by Noosfero.', mailing.signature_message
+ end
+
+ should 'return blank string on url' do
+ mailing = Mailing.new(:source => environment)
+ environment.domains << Domain.create(:name => 'noosfero.net', :is_default => true)
+ assert_equal '', mailing.url
+ end
+
+ should 'deliver mailing to each recipient after create' do
+ person = Person['user_one']
+ mailing = Mailing.create(:source => environment, :subject => 'Hello', :body => 'We have some news', :person => person)
+ process_delayed_job_queue
+ assert_equal [], ActionMailer::Base.deliveries
+ end
+end
diff --git a/test/unit/organization_mailing_test.rb b/test/unit/organization_mailing_test.rb
new file mode 100644
index 0000000..6823f09
--- /dev/null
+++ b/test/unit/organization_mailing_test.rb
@@ -0,0 +1,115 @@
+require File.dirname(__FILE__) + '/../test_helper'
+
+class OrganizationMailingTest < ActiveSupport::TestCase
+
+ def setup
+ ActionMailer::Base.delivery_method = :test
+ ActionMailer::Base.perform_deliveries = true
+ ActionMailer::Base.deliveries = []
+ @community = fast_create(Community)
+ @person = create_user('john').person
+ member_1 = create_user('user_one').person
+ member_2 = create_user('user_two').person
+ @community.add_member(member_1)
+ @community.add_member(member_2)
+ end
+ attr_reader :community, :person
+
+ should 'require source_id' do
+ mailing = OrganizationMailing.new
+ mailing.valid?
+ assert mailing.errors.invalid?(:source_id)
+
+ mailing.source_id = community.id
+ mailing.valid?
+ assert !mailing.errors.invalid?(:source_id)
+ end
+
+ should 'return community name' do
+ mailing = OrganizationMailing.new(:source => community)
+ assert_equal community.name, mailing.source.name
+ end
+
+ should 'return community with source_id' do
+ mailing = OrganizationMailing.new(:source => community)
+ assert_equal community, mailing.source
+ end
+
+ should 'return person with person_id' do
+ mailing = OrganizationMailing.new(:source => community, :person => person)
+ assert_equal person, mailing.person
+ end
+
+ should 'display name and email on generate_from' do
+ mailing = OrganizationMailing.new(:source => community, :person => person)
+ assert_equal "#{person.name} <#{community.environment.contact_email}>", mailing.generate_from
+ end
+
+ should 'generate subject' do
+ mailing = OrganizationMailing.new(:source => community, :subject => 'Hello :)')
+ assert_equal "[#{community.name}] #{mailing.subject}", mailing.generate_subject
+ end
+
+ should 'return signature message' do
+ mailing = OrganizationMailing.new(:source => community)
+ assert_equal "Sent by community #{community.name}.", mailing.signature_message
+ end
+
+ should 'return url for organization on url' do
+ mailing = OrganizationMailing.new(:source => community)
+ assert_equal "#{community.environment.top_url}/#{community.name.to_slug}", mailing.url
+ end
+
+ should 'deliver mailing to each member after create' do
+ mailing = OrganizationMailing.create(:source => community, :subject => 'Hello', :body => 'We have some news', :person => person)
+ process_delayed_job_queue
+ assert_equal 2, ActionMailer::Base.deliveries.count
+ end
+
+ should 'create mailing sent to each recipient after delivering mailing' do
+ mailing = OrganizationMailing.create(:source => community, :subject => 'Hello', :body => 'We have some news', :person => person)
+ assert_difference MailingSent, :count, 2 do
+ process_delayed_job_queue
+ end
+ end
+
+ should 'change locale according to the mailing locale' do
+ mailing = OrganizationMailing.create(:source => community, :subject => 'Hello', :body => 'We have some news', :locale => 'pt', :person => person)
+ Noosfero.expects(:with_locale).with('pt')
+ process_delayed_job_queue
+ end
+
+ should 'have community by source_id' do
+ mailing = community.mailings.build(:source => community, :subject => 'Hello', :body => 'We have some news', :person => person)
+ mailing.save!
+
+ assert_equal community, Mailing.find(mailing.id).source
+ end
+
+ should 'return recipient' do
+ mailing = OrganizationMailing.new(:source => community)
+ assert_equal Person['user_one'], mailing.recipient
+ end
+
+ should 'return true if already sent mailing to a recipient' do
+ member = Person['user_one']
+ mailing = OrganizationMailing.create(:source => community, :subject => 'Hello', :body => 'We have some news', :person => person)
+ process_delayed_job_queue
+ assert mailing.already_sent_mailing_to?(member)
+ end
+
+ should 'return false if did not sent mailing to a recipient' do
+ recipient = fast_create(Person)
+ mailing = OrganizationMailing.create(:source => community, :subject => 'Hello', :body => 'We have some news', :person => person)
+ process_delayed_job_queue
+
+ assert !mailing.already_sent_mailing_to?(recipient)
+ end
+
+ protected
+
+ def url_for(url)
+ url
+ end
+
+end
diff --git a/test/unit/profile_test.rb b/test/unit/profile_test.rb
index 4a31241..285d1d7 100644
--- a/test/unit/profile_test.rb
+++ b/test/unit/profile_test.rb
@@ -823,11 +823,16 @@ class ProfileTest < Test::Unit::TestCase
assert_equal 'code', p.nickname
end
- should 'return truncated name in short_name if nickname is blank' do
+ should 'return truncated name in short_name with 15 chars by default if nickname is blank' do
p = Profile.new(:name => 'a123456789abcdefghij')
assert_equal 'a123456789ab...', p.short_name
end
+ should 'return truncated name in short_name with chars size if nickname is blank' do
+ p = Profile.new(:name => 'a123456789abcdefghij')
+ assert_equal 'a123456...', p.short_name(10)
+ end
+
should 'provide custom header' do
assert_equal 'my custom header', Profile.new(:custom_header => 'my custom header').custom_header
end
--
libgit2 0.21.2