Commit a84184601ab3d4a45d5d1380132e998309f8ef5c

Authored by Rodrigo Souto
Committed by Antonio Terceiro
1 parent 1324c451

Articles published with the same name aren't ignored

  * When a user try to publish an article in some communities
    and there is another article with the same name already
    published in these communities, an exception is raised and
    the exception message will be shown listing the commuinties
    where this problem ocurred.

  * If the community is moderated, then no message
    will be shown to the user, but when the moderator try
    to accept the task, an exception message will be shown
    informing that that name is already in use.

ActionItem1309
app/controllers/my_profile/cms_controller.rb
@@ -222,11 +222,18 @@ class CmsController < MyProfileController @@ -222,11 +222,18 @@ class CmsController < MyProfileController
222 end 222 end
223 end.compact unless params[:marked_groups].nil? 223 end.compact unless params[:marked_groups].nil?
224 if request.post? 224 if request.post?
  225 + @failed = {}
225 @marked_groups.each do |item| 226 @marked_groups.each do |item|
226 task = ApproveArticle.create!(:article => @article, :name => item[:name], :target => item[:group], :requestor => profile) 227 task = ApproveArticle.create!(:article => @article, :name => item[:name], :target => item[:group], :requestor => profile)
227 - task.finish unless item[:group].moderated_articles? 228 + begin
  229 + task.finish unless item[:group].moderated_articles?
  230 + rescue Exception => ex
  231 + @failed[ex.clean_message] ? @failed[ex.clean_message] << item[:group].name : @failed[ex.clean_message] = [item[:group].name]
  232 + end
  233 + end
  234 + if @failed.blank?
  235 + redirect_back
228 end 236 end
229 - redirect_back  
230 end 237 end
231 end 238 end
232 239
app/controllers/my_profile/tasks_controller.rb
@@ -17,7 +17,11 @@ class TasksController &lt; MyProfileController @@ -17,7 +17,11 @@ class TasksController &lt; MyProfileController
17 if request.post? && VALID_DECISIONS.include?(decision) && params[:id] 17 if request.post? && VALID_DECISIONS.include?(decision) && params[:id]
18 task = profile.find_in_all_tasks(params[:id]) 18 task = profile.find_in_all_tasks(params[:id])
19 task.update_attributes!(params[:task]) 19 task.update_attributes!(params[:task])
20 - task.send(decision) 20 + begin
  21 + task.send(decision)
  22 + rescue Exception => ex
  23 + flash[:notice] = ex.clean_message
  24 + end
21 end 25 end
22 redirect_to :action => 'index' 26 redirect_to :action => 'index'
23 end 27 end
app/models/approve_article.rb
@@ -64,7 +64,7 @@ class ApproveArticle &lt; Task @@ -64,7 +64,7 @@ class ApproveArticle &lt; Task
64 end 64 end
65 65
66 def perform 66 def perform
67 - PublishedArticle.create(:name => name, :profile => target, :reference_article => article, :parent => article_parent, :highlighted => highlighted) 67 + PublishedArticle.create!(:name => name, :profile => target, :reference_article => article, :parent => article_parent, :highlighted => highlighted)
68 end 68 end
69 69
70 def target_notification_message 70 def target_notification_message
app/views/cms/publish.rhtml
1 <h1><%= _('Select the groups where you want to publish your article') %></h1> 1 <h1><%= _('Select the groups where you want to publish your article') %></h1>
2 2
  3 +<% if !@failed.blank? %>
  4 + <div class="errorExplanation" id="errorExplanation">
  5 + <p><%=_("There were errors with the following communities: ")%></p>
  6 + <% @failed.each do |error, communities|%>
  7 + <strong> <%= error %>: </strong>
  8 + <ul>
  9 + <% communities.each do |community| %>
  10 + <li> <%= community %> </li>
  11 + <% end %>
  12 + </ul>
  13 + <% end %>
  14 + </div>
  15 +<% end %>
  16 +
3 <% form_tag do%> 17 <% form_tag do%>
4 <%= hidden_field_tag :back_to, @back_to %> 18 <%= hidden_field_tag :back_to, @back_to %>
5 <% @groups.each do |group| %> 19 <% @groups.each do |group| %>
features/publish_article.feature 0 → 100644
@@ -0,0 +1,125 @@ @@ -0,0 +1,125 @@
  1 +Feature: publish article
  2 + As a user
  3 + I want to publish an article
  4 + In order to share it with other users
  5 +
  6 + Background:
  7 + Given the following users
  8 + | login | name |
  9 + | joaosilva | Joao Silva |
  10 + | mariasilva | Maria Silva |
  11 + And the following communities
  12 + | identifier | name |
  13 + | sample-community | Sample Community |
  14 + And the following articles
  15 + | owner | name | body |
  16 + | joaosilva | Sample Article | This is the first published article |
  17 +
  18 + Scenario: publishing an article that doesn't exists in the community
  19 + Given I am logged in as "joaosilva"
  20 + And "Joao Silva" is a member of "Sample Community"
  21 + And I am on Joao Silva's control panel
  22 + And I follow "Manage Content"
  23 + #These "deletes" are to remove default articles and spread the correct created one
  24 + And I follow "Delete"
  25 + And I follow "Delete"
  26 + And I follow "Delete"
  27 + And I follow "Spread"
  28 + And I check "Sample Community"
  29 + And I press "Publish"
  30 + And I am on Sample Community's homepage
  31 + And I follow "View profile"
  32 + And I follow "Site map"
  33 + When I follow "Sample Article"
  34 + Then I should see "This is the first published article"
  35 +
  36 +
  37 + Scenario: getting an error message when publishing article with same name
  38 + Given I am logged in as "joaosilva"
  39 + And "Joao Silva" is a member of "Sample Community"
  40 + And I am on Joao Silva's control panel
  41 + And I follow "Manage Content"
  42 + And I follow "Delete"
  43 + And I follow "Delete"
  44 + And I follow "Delete"
  45 + And I follow "Spread"
  46 + And I check "Sample Community"
  47 + And I press "Publish"
  48 + And I am logged in as "mariasilva"
  49 + And "Maria Silva" is a member of "Sample Community"
  50 + And I am on Maria Silva's control panel
  51 + And I follow "Manage Content"
  52 + And I follow "New article"
  53 + And I follow "Text article with Textile markup language"
  54 + And I fill in the following:
  55 + | Title | Sample Article |
  56 + | Text | this is Maria's first published article |
  57 + And I press "Save"
  58 + And I follow "Delete"
  59 + And I follow "Delete"
  60 + And I follow "Delete"
  61 + And I follow "Spread"
  62 + And I check "Sample Community"
  63 + When I press "Publish"
  64 + Then I should see "Validation failed: Slug (the code generated from the article name) is already being used by another article.:"
  65 +
  66 + Scenario: publishing an article in many communities and listing the communities that couldn't publish the article again,
  67 + stills publishing the article in the other communities.
  68 + Given the following communities
  69 + | identifier | name |
  70 + | another-community1 | Another Community1 |
  71 + | another-community2 | Another Community2 |
  72 + And I am logged in as "joaosilva"
  73 + And "Joao Silva" is a member of "Sample Community"
  74 + And "Joao Silva" is a member of "Another Community1"
  75 + And "Joao Silva" is a member of "Another Community2"
  76 + And I am on Joao Silva's control panel
  77 + And I follow "Manage Content"
  78 + And I follow "Delete"
  79 + And I follow "Delete"
  80 + And I follow "Delete"
  81 + And I follow "Spread"
  82 + And I check "Sample Community"
  83 + And I press "Publish"
  84 + And I should not see "This article name is already in use in the following community(ies):"
  85 + And I follow "Spread"
  86 + And I check "Sample Community"
  87 + And I check "Another Community1"
  88 + And I check "Another Community2"
  89 + When I press "Publish"
  90 + Then I should see "Validation failed: Slug (the code generated from the article name) is already being used by another article.:"
  91 + And I am on Another Community1's homepage
  92 + And I follow "View profile"
  93 + When I follow "Site map"
  94 + Then I should see "Sample Article"
  95 + And I am on Another Community2's homepage
  96 + And I follow "View profile"
  97 + When I follow "Site map"
  98 + Then I should see "Sample Article"
  99 +
  100 + Scenario: publishing articles with the same name in a moderated community
  101 + Given I am logged in as "joaosilva"
  102 + And "Joao Silva" is a member of "Sample Community"
  103 + And "Joao Silva" is admin of "Sample Community"
  104 + And I am on Sample Community's control panel
  105 + And I follow "Community Info and settings"
  106 + And I choose "profile_data_moderated_articles_true"
  107 + And I press "Save"
  108 + And I am on Joao Silva's control panel
  109 + And I follow "Manage Content"
  110 + And I follow "Delete"
  111 + And I follow "Delete"
  112 + And I follow "Delete"
  113 + And I follow "Spread"
  114 + And I check "Sample Community"
  115 + And I press "Publish"
  116 + And I follow "Spread"
  117 + And I check "Sample Community"
  118 + And I press "Publish"
  119 + And I am on Sample Community's control panel
  120 + And I follow "Tasks"
  121 + And I press "Ok!"
  122 + And I should not see "Validation failed: Slug (the code generated from the article name) is already being used by another article.:"
  123 + When I press "Ok!"
  124 + Then I should see "Validation failed: Slug (the code generated from the article name) is already being used by another article."
  125 +
features/step_definitions/noosfero_steps.rb
@@ -94,6 +94,10 @@ Given /^feature &quot;(.+)&quot; is disabled on environment$/ do |feature| @@ -94,6 +94,10 @@ Given /^feature &quot;(.+)&quot; is disabled on environment$/ do |feature|
94 e.save 94 e.save
95 end 95 end
96 96
  97 +Given /^"(.+)" is a member of "(.+)"$/ do |person,profile|
  98 + Profile.find_by_name(profile).add_member(Profile.find_by_name(person))
  99 +end
  100 +
97 Given /^"(.+)" should be a member of "(.+)"$/ do |person,profile| 101 Given /^"(.+)" should be a member of "(.+)"$/ do |person,profile|
98 Profile.find_by_name(profile).members.should include(Person.find_by_name(person)) 102 Profile.find_by_name(profile).members.should include(Person.find_by_name(person))
99 end 103 end