diff --git a/controllers/email_article_plugin_myprofile_controller.rb b/controllers/email_article_plugin_myprofile_controller.rb index 8b90b76..30a015b 100644 --- a/controllers/email_article_plugin_myprofile_controller.rb +++ b/controllers/email_article_plugin_myprofile_controller.rb @@ -1,88 +1,433 @@ -require 'nokogiri' -require 'open-uri' class EmailArticlePluginMyprofileController < MyProfileController needs_profile - - class Webpage < Nokogiri::HTML::Document - attr_accessor :url - class << self - def new(url) - html = open(url) - self.parse(html).tap do |d| - d.url = url - end - end - end - end + # def send_email + # if user.is_admin?(profile) + # article = profile.articles.find(params[:id]) + # #EmailArticlePluginMyprofileController::Sender.content(article).deliver + # Sender.content(article) + # render :text => "Email sent to queue" + # else + # render :status => :forbidden, :text => "Forbidden user" + # end + # end def send_email - if user.is_admin?(profile) - article = profile.articles.find(params[:id]) -# article = Article.find(params[:id]) - EmailArticlePluginMyprofileController::Sender.content(article).deliver - render :text => "Email sent to queue" - else + unless user.is_admin?(profile) render :status => :forbidden, :text => "Forbidden user" + return end + mailing_params = {"subject"=>"assunto", "body"=>"corpo"} + @mailing = profile.mailings.build(mailing_params) + # if request.post? + @mailing.locale = locale + @mailing.person = user + if @mailing.save + render :text => "Email sent to queue" + #session[:notice] = _('The e-mails are being sent') + # redirect_to_previous_location + else + render :status => :forbidden, :text => "Forbidden user" + # session[:notice] = _('Could not create the e-mail') + end + # end end - class Sender < ActionMailer::Base - def content(article) - doc = Nokogiri::HTML(article.body) - doc.css("a").each do |link| - unless link.attribute("href").nil? - relative_path = link.attribute("href").value - link.attribute("href").value = "http://#{article.environment.default_hostname}/#{relative_path}" - end - end - - doc.css("img").each do |link| - unless link.attribute("src").nil? - relative_path = link.attribute("src").value - link.attribute("src").value = "http://#{article.environment.default_hostname}/#{relative_path}" - end - end - + # before_filter :check_access_to_profile, :except => [:join, :join_not_logged, :index, :add] + # before_filter :store_location, :only => [:join, :join_not_logged, :report_abuse, :send_mail] + # before_filter :login_required, :only => [:add, :join, :leave, :unblock, :leave_scrap, :remove_scrap, :remove_activity, :view_more_activities, :view_more_network_activities, :report_abuse, :register_report, :leave_comment_on_activity, :send_mail] + # + # 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) + # @network_activities = @profile.tracked_notifications.visible.paginate(:per_page => 15, :page => params[:page]) if @network_activities.empty? + # @activities = @profile.activities.paginate(:per_page => 15, :page => params[:page]) + # end + # @tags = profile.article_tags + # allow_access_to_page + # end + # + # def tags + # @tags_cache_key = "tags_profile_#{profile.id.to_s}" + # if is_cache_expired?(@tags_cache_key) + # @tags = profile.article_tags + # end + # end + # + # def content_tagged + # @tag = params[:id] + # @tag_cache_key = "tag_#{CGI.escape(@tag.to_s)}_#{profile.id.to_s}_page_#{params[:npage]}" + # if is_cache_expired?(@tag_cache_key) + # @tagged = profile.tagged_with(@tag).paginate(:per_page => 20, :page => params[:npage]) + # end + # end + # + # def tag_feed + # @tag = params[:id] + # tagged = profile.articles.paginate(:per_page => 20, :page => 1, :order => 'published_at DESC', :include => :tags, :conditions => ['tags.name LIKE ?', @tag]) + # feed_writer = FeedWriter.new + # data = feed_writer.write( + # tagged, + # :title => _("%s's contents tagged with \"%s\"") % [profile.name, @tag], + # :description => _("%s's contents tagged with \"%s\"") % [profile.name, @tag], + # :link => url_for(profile.url) + # ) + # render :text => data, :content_type => "text/xml" + # end + # + # def communities + # if is_cache_expired?(profile.communities_cache_key(params)) + # @communities = profile.communities.includes(relations_to_include).paginate(:per_page => per_page, :page => params[:npage], :total_entries => profile.communities.count) + # end + # end + # + # def enterprises + # @enterprises = profile.enterprises.includes(relations_to_include) + # end + # + # def friends + # if is_cache_expired?(profile.friends_cache_key(params)) + # @friends = profile.friends.includes(relations_to_include).paginate(:per_page => per_page, :page => params[:npage], :total_entries => profile.friends.count) + # end + # end + # + # def members + # if is_cache_expired?(profile.members_cache_key(params)) + # @members = profile.members_by_name.includes(relations_to_include).paginate(:per_page => members_per_page, :page => params[:npage], :total_entries => profile.members.count) + # end + # end + # + # def fans + # @fans = profile.fans.includes(relations_to_include) + # end + # + # def favorite_enterprises + # @favorite_enterprises = profile.favorite_enterprises.includes(relations_to_include) + # end + # + # def sitemap + # @articles = profile.top_level_articles.includes([:profile, :parent]) + # end + # + # def join + # if !user.memberships.include?(profile) + # profile.add_member(user) + # if !profile.members.include?(user) + # render :text => {:message => _('%s administrator still needs to accept you as member.') % profile.name}.to_json + # else + # render :text => {:message => _('You just became a member of %s.') % profile.name}.to_json + # end + # else + # render :text => {:message => _('You are already a member of %s.') % profile.name}.to_json + # end + # end + # + # def join_not_logged + # session[:join] = profile.identifier + # + # if user + # redirect_to :controller => 'profile', :action => 'join' + # else + # redirect_to :controller => '/account', :action => 'login' + # end + # end + # + # def leave + # if current_person.memberships.include?(profile) + # if current_person.is_last_admin?(profile) + # render :text => {:redirect_to => url_for({:controller => 'profile_members', :action => 'last_admin', :person => current_person.id})}.to_json + # else + # render :text => current_person.leave(profile, params[:reload]) + # end + # else + # render :text => {:message => _('You are not a member of %s.') % profile.name}.to_json + # end + # end + # + # def check_membership + # unless logged_in? + # render :text => '' + # return + # end + # if user.memberships.include?(profile) + # render :text => 'true' + # else + # render :text => 'false' + # end + # end + # + # def add + # # FIXME this shouldn't be in Person model? + # if !user.memberships.include?(profile) + # AddFriend.create!(:person => user, :friend => profile) + # render :text => _('%s still needs to accept being your friend.') % profile.name + # else + # render :text => _('You are already a friend of %s.') % profile.name + # end + # end + # + # def check_friendship + # unless logged_in? + # render :text => '' + # return + # end + # if user == profile || user.already_request_friendship?(profile) || user.is_a_friend?(profile) + # render :text => 'true' + # else + # render :text => 'false' + # end + # end + # + # def unblock + # if current_user.person.is_admin?(profile.environment) + # profile.unblock + # session[:notice] = _("You have unblocked %s successfully. ") % profile.name + # redirect_to :controller => 'profile', :action => 'index' + # else + # message = _('You are not allowed to unblock enterprises in this environment.') + # render_access_denied(message) + # end + # end + # + # def leave_scrap + # sender = params[:sender_id].nil? ? current_user.person : Person.find(params[:sender_id]) + # receiver = params[:receiver_id].nil? ? @profile : Person.find(params[:receiver_id]) + # @scrap = Scrap.new(params[:scrap]) + # @scrap.sender= sender + # @scrap.receiver= receiver + # @tab_action = params[:tab_action] + # @message = @scrap.save ? _("Message successfully sent.") : _("You can't leave an empty message.") + # activities = @profile.activities.paginate(:per_page => 15, :page => params[:page]) if params[:not_load_scraps].nil? + # render :partial => 'profile_activities_list', :locals => {:activities => activities} + # end + # + # def leave_comment_on_activity + # @comment = Comment.new(params[:comment]) + # @comment.author = user + # @activity = ActionTracker::Record.find(params[:source_id]) + # @comment.source_type, @comment.source_id = (@activity.target_type == 'Article' ? ['Article', @activity.target_id] : [@activity.class.to_s, @activity.id]) + # @tab_action = params[:tab_action] + # @message = @comment.save ? _("Comment successfully added.") : _("You can't leave an empty comment.") + # if @tab_action == 'wall' + # activities = @profile.activities.paginate(:per_page => 15, :page => params[:page]) if params[:not_load_scraps].nil? + # render :partial => 'profile_activities_list', :locals => {:activities => activities} + # else + # network_activities = @profile.tracked_notifications.visible.paginate(:per_page => 15, :page => params[:page]) + # render :partial => 'profile_network_activities', :locals => {:network_activities => network_activities} + # end + # end + # + # def view_more_activities + # @activities = @profile.activities.paginate(:per_page => 10, :page => params[:page]) + # render :partial => 'profile_activities_list', :locals => {:activities => @activities} + # end + # + # def view_more_network_activities + # @activities = @profile.tracked_notifications.paginate(:per_page => 10, :page => params[:page]) + # render :partial => 'profile_network_activities', :locals => {:network_activities => @activities} + # end + # + # def more_comments + # profile_filter = @profile.person? ? {:user_id => @profile} : {:target_id => @profile} + # activity = ActionTracker::Record.find(:first, :conditions => {:id => params[:activity]}.merge(profile_filter)) + # comments_count = activity.comments.count + # comment_page = (params[:comment_page] || 1).to_i + # comments_per_page = 5 + # no_more_pages = comments_count <= comment_page * comments_per_page + # + # render :update do |page| + # page.insert_html :bottom, 'profile-wall-activities-comments-'+params[:activity], + # :partial => 'comment', :collection => activity.comments.flatten.paginate(:per_page => comments_per_page, :page => comment_page) + # + # if no_more_pages + # page.remove 'profile-wall-activities-comments-more-'+params[:activity] + # else + # page.replace_html 'profile-wall-activities-comments-more-'+params[:activity], + # :partial => 'more_comments', :locals => {:activity => activity, :comment_page => comment_page} + # end + # end + # end + # + # def more_replies + # activity = Scrap.find(:first, :conditions => {:id => params[:activity], :receiver_id => @profile, :scrap_id => nil}) + # comments_count = activity.replies.count + # comment_page = (params[:comment_page] || 1).to_i + # comments_per_page = 5 + # no_more_pages = comments_count <= comment_page * comments_per_page + # + # render :update do |page| + # page.insert_html :bottom, 'profile-wall-activities-comments-'+params[:activity], + # :partial => 'profile_scrap', :collection => activity.replies.paginate(:per_page => comments_per_page, :page => comment_page), :as => :scrap + # + # if no_more_pages + # page.remove 'profile-wall-activities-comments-more-'+params[:activity] + # else + # page.replace_html 'profile-wall-activities-comments-more-'+params[:activity], + # :partial => 'more_replies', :locals => {:activity => activity, :comment_page => comment_page} + # end + # end + # end + # + # def remove_scrap + # begin + # scrap = current_user.person.scraps(params[:scrap_id]) + # scrap.destroy + # finish_successful_removal 'Scrap successfully removed.' + # rescue + # finish_unsuccessful_removal 'You could not remove this scrap.' + # end + # end + # + # def remove_activity + # begin + # raise if !can_edit_profile + # activity = ActionTracker::Record.find(params[:activity_id]) + # if params[:only_hide] + # activity.update_attribute(:visible, false) + # else + # activity.destroy + # end + # finish_successful_removal 'Activity successfully removed.' + # rescue + # finish_unsuccessful_removal 'You could not remove this activity.' + # end + # end + # + # def remove_notification + # begin + # raise if !can_edit_profile + # notification = ActionTrackerNotification.find(:first, :conditions => {:profile_id => profile.id, :action_tracker_id => params[:activity_id]}) + # notification.destroy + # render :text => _('Notification successfully removed.') + # rescue + # render :text => _('You could not remove this notification.') + # end + # end + # + # def finish_successful_removal(msg) + # if request.xhr? + # render :text => {'ok' => true}.to_json, :content_type => 'application/json' + # else + # session[:notice] = _(msg) + # redirect_to :action => :index + # end + # end + # + # def finish_unsuccessful_removal(msg) + # session[:notice] = _(msg) + # if request.xhr? + # render :text => {'redirect' => url_for(:action => :index)}.to_json, :content_type => 'application/json' + # else + # redirect_to :action => :index + # end + # end + # + # def report_abuse + # @abuse_report = AbuseReport.new + # render :layout => false + # end + # + # def register_report + # unless user.is_admin? || verify_recaptcha + # render :text => { + # :ok => false, + # :error => { + # :code => 1, + # :message => _('You could not answer the captcha.') + # } + # }.to_json + # else + # begin + # abuse_report = AbuseReport.new(params[:abuse_report]) + # if !params[:content_type].blank? + # article = params[:content_type].constantize.find(params[:content_id]) + # abuse_report.content = article_reported_version(article) + # end + # + # user.register_report(abuse_report, profile) + # + # if !params[:content_type].blank? + # abuse_report = AbuseReport.find_by_reporter_id_and_abuse_complaint_id(user.id, profile.opened_abuse_complaint.id) + # Delayed::Job.enqueue DownloadReportedImagesJob.new(abuse_report, article) + # end + # + # render :text => { + # :ok => true, + # :message => _('Your abuse report was registered. The administrators are reviewing your report.'), + # }.to_json + # rescue Exception => exception + # logger.error(exception.to_s) + # render :text => { + # :ok => false, + # :error => { + # :code => 2, + # :message => _('Your report couldn\'t be saved due to some problem. Please contact the administrator.') + # } + # }.to_json + # end + # end + # end + # + # def remove_comment + # #FIXME Check whether these permissions are enough + # @comment = Comment.find(params[:comment_id]) + # if (user == @comment.author || user == profile || user.has_permission?(:moderate_comments, profile)) + # @comment.destroy + # finish_successful_removal 'Comment successfully removed.' + # else + # finish_unsuccessful_removal 'You could not remove this comment.' + # end + # end -# link.attributes["href"].value = "http://myproxy.com/?url=#{CGI.escape link.attributes["href"].value}" -# http://#{environment.default_hostname}/#{relative_path[1]} - - - body = doc.to_s - sender = Person.find(article.author_id) - members = article.profile.members - emails = [] - members.each{ |m| - emails.push(m.user.email) - } - mail( - content_type: 'text/html', - to: emails, - from: "#{article.author_name} <#{sender.contact_email}>", - reply_to: article.author.user.email, - subject: "[Artigo] " + article.title, - body: body -# body: set_absolute_path(article.body, article.environment) - ) - end - def set_absolute_path(body, environment) - parsed = Hpricot(body.to_s) - parsed.search('img[@src]').map { |i| change_element_path(i, 'src', environment) } - parsed.search('a[@href]').map { |i| change_element_path(i, 'href', environment) } - parsed.to_s - end - def change_element_path(el, attribute, environment) - fullpath = /^http/.match(el[attribute]) - if not fullpath - relative_path = /\/?(.*)/.match(el[attribute]) - el[attribute] = "http://#{environment.default_hostname}/#{relative_path[1]}" - end - end - end + + # protected + # + # def check_access_to_profile + # unless profile.display_info_to?(user) + # redirect_to :action => 'index' + # end + # end + # + # def store_location + # if session[:previous_location].nil? + # session[:previous_location] = request.referer + # end + # end + # + # def redirect_to_previous_location + # back = session[:previous_location] + # if back + # session[:previous_location] = nil + # redirect_to back + # else + # redirect_to profile.url + # end + # end + # + # def per_page + # Noosfero::Constants::PROFILE_PER_PAGE + # end + # + # def members_per_page + # 20 + # end + # + # def can_edit_profile + # @can_edit_profile ||= user && user.has_permission?('edit_profile', profile) + # end + # helper_method :can_edit_profile + # + # def relations_to_include + # [:image, :domains, :preferred_domain, :environment] + # end + end diff --git a/test/unit/test_email_article_article_mailer.rb b/test/unit/test_email_article_article_mailer.rb new file mode 100644 index 0000000..c157f25 --- /dev/null +++ b/test/unit/test_email_article_article_mailer.rb @@ -0,0 +1,21 @@ +require File.dirname(__FILE__) + '/../test_helper' + +class EmailArticlePluginTest < ActiveSupport::TestCase + + def setup + @plugin = EmailArticlePlugin.new + end + + should 'be a noosfero plugin' do + assert_kind_of Noosfero::Plugin, @plugin + end + + should 'have name' do + assert_equal 'Email Article to Community Members Plugin', EmailArticlePlugin.plugin_name + end + + should 'have description' do + assert_equal _("A plugin that emails an article to the members of the community"), EmailArticlePlugin.plugin_description + end + +end diff --git a/test/unit/test_email_article_plugin_article_mailer.rb b/test/unit/test_email_article_plugin_article_mailer.rb new file mode 100644 index 0000000..0bdccfd --- /dev/null +++ b/test/unit/test_email_article_plugin_article_mailer.rb @@ -0,0 +1,133 @@ +require File.dirname(__FILE__) + '/../test_helper' + +class EmailArticlePluginArticleMailerTest < ActiveSupport::TestCase + + def setup + @plugin = EmailArticlePlugin.new + 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 'deliver mailing to each member after create' do + mailing = create(OrganizationMailing, :source => community, :subject => 'Hello', :body => 'We have some news', :person => person) + community.reload + process_delayed_job_queue + assert_equal 2, ActionMailer::Base.deliveries.count + end + + should 'require source_id' do + mailing = OrganizationMailing.new + mailing.valid? + assert mailing.errors[:source_id.to_s].present? + + mailing.source_id = community.id + mailing.valid? + assert !mailing.errors[:source_id.to_s].present? + end + + should 'return community name' do + mailing = build(OrganizationMailing, :source => community) + assert_equal community.name, mailing.source.name + end + + should 'return community with source_id' do + mailing = build(OrganizationMailing, :source => community) + assert_equal community, mailing.source + end + + should 'return person with person_id' do + mailing = build(OrganizationMailing, :source => community, :person => person) + assert_equal person, mailing.person + end + + should 'display name and email on generate_from' do + mailing = build(OrganizationMailing, :source => community, :person => person) + assert_equal "#{person.name} <#{community.environment.noreply_email}>", mailing.generate_from + end + + should 'generate subject' do + mailing = build(OrganizationMailing, :source => community, :subject => 'Hello :)') + assert_equal "[#{community.name}] #{mailing.subject}", mailing.generate_subject + end + + should 'return signature message' do + mailing = build(OrganizationMailing, :source => community) + assert_equal "Sent by community #{community.name}.", mailing.signature_message + end + + should 'return url for organization on url' do + mailing = build(OrganizationMailing, :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 = create(OrganizationMailing, :source => community, :subject => 'Hello', :body => 'We have some news', :person => person) + community.reload + process_delayed_job_queue + assert_equal 2, ActionMailer::Base.deliveries.count + end + + should 'deliver mailing when there are many mailings created' do + 50.times { create(OrganizationMailing, :source => community, :subject => 'Hello', :body => 'We have some news', :person => person) } + community.reload + process_delayed_job_queue + assert_equal 50*community.members_count, ActionMailer::Base.deliveries.count + end + + should 'create mailing sent to each recipient after delivering mailing' do + mailing = create(OrganizationMailing, :source => community, :subject => 'Hello', :body => 'We have some news', :person => person) + community.reload + assert_difference 'MailingSent.count', 2 do + process_delayed_job_queue + end + end + + should 'change locale according to the mailing locale' do + mailing = create(OrganizationMailing, :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 = create(OrganizationMailing, :source => community, :subject => 'Hello', :body => 'We have some news', :person => person) + assert_equal community, Mailing.find(mailing.id).source + end + + should 'return recipient' do + mailing = create(OrganizationMailing, :source => community, :subject => 'Hello', :body => 'We have some news', :person => person) + assert_equal [Person['user_one'], Person['user_two']], mailing.recipients + end + + should 'return recipients according to limit' do + mailing = create(OrganizationMailing, :source => community, :subject => 'Hello', :body => 'We have some news', :person => person) + assert_equal [Person['user_one']], mailing.recipients(0, 1) + end + + should 'return true if already sent mailing to a recipient' do + member = Person['user_one'] + mailing = create(OrganizationMailing, :source => community, :subject => 'Hello', :body => 'We have some news', :person => person) + process_delayed_job_queue + assert mailing.mailing_sents.find_by_person_id(member.id) + end + + should 'return false if did not sent mailing to a recipient' do + recipient = fast_create(Person) + mailing = create(OrganizationMailing, :source => community, :subject => 'Hello', :body => 'We have some news', :person => person) + process_delayed_job_queue + + assert !mailing.mailing_sents.find_by_person_id(recipient.id) + end + + protected + include NoosferoTestHelper + +end -- libgit2 0.21.2