Commit a4d28dc03ee45c00478c8e265acf9ee9e1523ab7
Exists in
master
and in
29 other branches
Merge branch 'suggest_article_refactor' into 'next'
Refactoring SuggestArticle Refactoring SuggestArticle and add two new features: - Accept suggestion from logged in users; - SuggestArticle model accept other article types. See merge request !561
Showing
11 changed files
with
169 additions
and
96 deletions
Show diff stats
app/controllers/my_profile/cms_controller.rb
@@ -357,7 +357,8 @@ class CmsController < MyProfileController | @@ -357,7 +357,8 @@ class CmsController < MyProfileController | ||
357 | @task.ip_address = request.remote_ip | 357 | @task.ip_address = request.remote_ip |
358 | @task.user_agent = request.user_agent | 358 | @task.user_agent = request.user_agent |
359 | @task.referrer = request.referrer | 359 | @task.referrer = request.referrer |
360 | - if verify_recaptcha(:model => @task, :message => _('Please type the words correctly')) && @task.save | 360 | + @task.requestor = current_person if logged_in? |
361 | + if (logged_in? || verify_recaptcha(:model => @task, :message => _('Please type the words correctly'))) && @task.save | ||
361 | session[:notice] = _('Thanks for your suggestion. The community administrators were notified.') | 362 | session[:notice] = _('Thanks for your suggestion. The community administrators were notified.') |
362 | redirect_to @back_to | 363 | redirect_to @back_to |
363 | end | 364 | end |
app/models/article.rb
@@ -5,7 +5,7 @@ class Article < ActiveRecord::Base | @@ -5,7 +5,7 @@ class Article < ActiveRecord::Base | ||
5 | :allow_members_to_edit, :translation_of_id, :language, | 5 | :allow_members_to_edit, :translation_of_id, :language, |
6 | :license_id, :parent_id, :display_posts_in_current_language, | 6 | :license_id, :parent_id, :display_posts_in_current_language, |
7 | :category_ids, :posts_per_page, :moderate_comments, | 7 | :category_ids, :posts_per_page, :moderate_comments, |
8 | - :accept_comments, :feed, :published, :source, | 8 | + :accept_comments, :feed, :published, :source, :source_name, |
9 | :highlighted, :notify_comments, :display_hits, :slug, | 9 | :highlighted, :notify_comments, :display_hits, :slug, |
10 | :external_feed_builder, :display_versions, :external_link, | 10 | :external_feed_builder, :display_versions, :external_link, |
11 | :image_builder, :show_to_followers | 11 | :image_builder, :show_to_followers |
app/models/suggest_article.rb
1 | class SuggestArticle < Task | 1 | class SuggestArticle < Task |
2 | 2 | ||
3 | - validates_presence_of :target_id, :article_name, :email, :name, :article_body | 3 | + validates_presence_of :target_id |
4 | + validates_presence_of :email, :name, :if => Proc.new { |task| task.requestor.blank? } | ||
5 | + validates_associated :article_object | ||
4 | 6 | ||
5 | settings_items :email, :type => String | 7 | settings_items :email, :type => String |
6 | settings_items :name, :type => String | 8 | settings_items :name, :type => String |
7 | - settings_items :article_name, :type => String | ||
8 | - settings_items :article_body, :type => String | ||
9 | - settings_items :article_abstract, :type => String | ||
10 | - settings_items :article_parent_id, :type => String | ||
11 | - settings_items :source, :type => String | ||
12 | - settings_items :source_name, :type => String | ||
13 | - settings_items :highlighted, :type => :boolean, :default => false | ||
14 | settings_items :ip_address, :type => String | 9 | settings_items :ip_address, :type => String |
15 | settings_items :user_agent, :type => String | 10 | settings_items :user_agent, :type => String |
16 | settings_items :referrer, :type => String | 11 | settings_items :referrer, :type => String |
12 | + settings_items :article, :type => Hash, :default => {} | ||
17 | 13 | ||
18 | after_create :schedule_spam_checking | 14 | after_create :schedule_spam_checking |
19 | 15 | ||
@@ -24,34 +20,45 @@ class SuggestArticle < Task | @@ -24,34 +20,45 @@ class SuggestArticle < Task | ||
24 | include Noosfero::Plugin::HotSpot | 20 | include Noosfero::Plugin::HotSpot |
25 | 21 | ||
26 | def sender | 22 | def sender |
27 | - "#{name} (#{email})" | 23 | + requestor ? "#{requestor.name}" : "#{name} (#{email})" |
24 | + end | ||
25 | + | ||
26 | + def article_object | ||
27 | + if @article_object.nil? | ||
28 | + @article_object = article_type.new(article.merge({:profile => target}).except(:type)) | ||
29 | + if requestor.present? | ||
30 | + @article_object.author = requestor | ||
31 | + else | ||
32 | + @article_object.author_name = name | ||
33 | + end | ||
34 | + end | ||
35 | + @article_object | ||
36 | + end | ||
37 | + | ||
38 | + def article_type | ||
39 | + (article[:type] || 'TinyMceArticle').constantize | ||
28 | end | 40 | end |
29 | 41 | ||
30 | def perform | 42 | def perform |
31 | - task = TinyMceArticle.new | ||
32 | - task.profile = target | ||
33 | - task.name = article_name | ||
34 | - task.author_name = name | ||
35 | - task.body = article_body | ||
36 | - task.abstract = article_abstract | ||
37 | - task.parent_id = article_parent_id | ||
38 | - task.source = source | ||
39 | - task.source_name = source_name | ||
40 | - task.highlighted = highlighted | ||
41 | - task.save! | 43 | + article_object.save! |
42 | end | 44 | end |
43 | 45 | ||
44 | def title | 46 | def title |
45 | _("Article suggestion") | 47 | _("Article suggestion") |
46 | end | 48 | end |
47 | 49 | ||
50 | + def article_name | ||
51 | + article[:name] | ||
52 | + end | ||
53 | + | ||
48 | def subject | 54 | def subject |
49 | article_name | 55 | article_name |
50 | end | 56 | end |
51 | 57 | ||
52 | def information | 58 | def information |
53 | - { :message => _('%{sender} suggested the publication of the article: %{subject}.'), | ||
54 | - :variables => {:sender => sender} } | 59 | + variables = requestor.blank? ? {:requestor => sender} : {} |
60 | + { :message => _('%{requestor} suggested the publication of the article: %{subject}.'), | ||
61 | + :variables => variables } | ||
55 | end | 62 | end |
56 | 63 | ||
57 | def accept_details | 64 | def accept_details |
@@ -63,8 +70,8 @@ class SuggestArticle < Task | @@ -63,8 +70,8 @@ class SuggestArticle < Task | ||
63 | end | 70 | end |
64 | 71 | ||
65 | def target_notification_description | 72 | def target_notification_description |
66 | - _('%{sender} suggested the publication of the article: %{article}.') % | ||
67 | - {:sender => sender, :article => article_name} | 73 | + _('%{requestor} suggested the publication of the article: %{article}.') % |
74 | + {:requestor => sender, :article => article_name} | ||
68 | end | 75 | end |
69 | 76 | ||
70 | def target_notification_message | 77 | def target_notification_message |
app/views/cms/suggest_an_article.html.erb
@@ -6,21 +6,22 @@ | @@ -6,21 +6,22 @@ | ||
6 | 6 | ||
7 | <%= labelled_form_for 'task' do |f| %> | 7 | <%= labelled_form_for 'task' do |f| %> |
8 | 8 | ||
9 | - <%= required labelled_form_field(_('Title'), text_field(:task, 'article_name', :size => 50)) %> | 9 | + <%= required labelled_form_field(_('Title'), text_field('task[article]', 'name', :size => 50)) %> |
10 | 10 | ||
11 | - <%= labelled_form_field(_('Source'), text_field(:task, 'source_name')) %> | 11 | + <%= labelled_form_field(_('Source'), text_field('task[article]', 'source_name')) %> |
12 | 12 | ||
13 | - <%= labelled_form_field(_('Source URL'), text_field(:task, 'source')) %> | 13 | + <%= labelled_form_field(_('Source URL'), text_field('task[article]', 'source')) %> |
14 | 14 | ||
15 | - <%= required labelled_form_field(_('Your name'), text_field(:task, 'name')) %> | ||
16 | - | ||
17 | - <%= required labelled_form_field(_('Email'), text_field(:task, 'email')) %> | 15 | + <% unless logged_in? %> |
16 | + <%= required labelled_form_field(_('Your name'), text_field(:task, 'name')) %> | ||
17 | + <%= required labelled_form_field(_('Email'), text_field(:task, 'email')) %> | ||
18 | + <% end %> | ||
18 | 19 | ||
19 | - <%= render :partial => 'shared/lead_and_body', :locals => {:tiny_mce => true, :object => :task, :abstract_method => 'article_abstract', :body_method => 'article_body'} %> | 20 | + <%= render :partial => 'shared/lead_and_body', :locals => {:tiny_mce => true, :object => 'task[article]'} %> |
20 | 21 | ||
21 | <%= hidden_field_tag('back_to', @back_to) %> | 22 | <%= hidden_field_tag('back_to', @back_to) %> |
22 | 23 | ||
23 | - <%= recaptcha_tags(:display => { :theme => 'clean' }, :ajax => true) %> | 24 | + <%= recaptcha_tags(:display => { :theme => 'clean' }, :ajax => true) unless logged_in? %> |
24 | 25 | ||
25 | <% button_bar do %> | 26 | <% button_bar do %> |
26 | <%= submit_button :save, _('Save') %> | 27 | <%= submit_button :save, _('Save') %> |
app/views/spam/_suggest_article.html.erb
@@ -7,13 +7,13 @@ | @@ -7,13 +7,13 @@ | ||
7 | <ul class="suggest-article-details" style="display: none"> | 7 | <ul class="suggest-article-details" style="display: none"> |
8 | <li><strong><%=_('Sent by')%></strong>: <%=task.name%> </li> | 8 | <li><strong><%=_('Sent by')%></strong>: <%=task.name%> </li> |
9 | <li><strong><%=_('Email')%></strong>: <%=task.email%> </li> | 9 | <li><strong><%=_('Email')%></strong>: <%=task.email%> </li> |
10 | - <li><strong><%=_('Source')%></strong>: <%=task.source_name%> </li> | ||
11 | - <li><strong><%=_('Source URL')%></strong>: <%=task.source%> </li> | ||
12 | - <li><strong><%=_('Folder')%></strong>: <%=(a = Article.find_by_id(task.article_parent_id))?a.name : '<em>' + s_('Folder|none') + '</em>'%> </li> | ||
13 | - <li><strong><%=_('Lead')%></strong>: <%=task.article_abstract.blank? ? '<em>' + s_('Abstract|empty') + '</em>' : task.article_abstract%> </li> | 10 | + <li><strong><%=_('Source')%></strong>: <%=task.article_object.source_name%> </li> |
11 | + <li><strong><%=_('Source URL')%></strong>: <%=task.article_object.source%> </li> | ||
12 | + <li><strong><%=_('Folder')%></strong>: <%=(a = Article.find_by_id(task.article_object.parent_id))?a.name : '<em>' + s_('Folder|none') + '</em>'%> </li> | ||
13 | + <li><strong><%=_('Lead')%></strong>: <%=task.article_object.abstract.blank? ? '<em>' + s_('Abstract|empty') + '</em>' : task.article_object.abstract%> </li> | ||
14 | <li><strong><%=_('Body')%></strong>: | 14 | <li><strong><%=_('Body')%></strong>: |
15 | <div class='suggest-article-body'> | 15 | <div class='suggest-article-body'> |
16 | - <%= task.article_body %> | 16 | + <%= task.article_object.body %> |
17 | </div> | 17 | </div> |
18 | </li> | 18 | </li> |
19 | </ul> | 19 | </ul> |
app/views/tasks/_suggest_article_accept_details.html.erb
1 | <%= render :file => 'shared/tiny_mce' %> | 1 | <%= render :file => 'shared/tiny_mce' %> |
2 | 2 | ||
3 | -<%= labelled_form_field(_("Sent by: "), f.text_field(:name)) %> | ||
4 | -<p><%= label_tag(_("Email: %s") % task.email) %> </p> | ||
5 | -<%= required labelled_form_field(_('Title'), f.text_field(:article_name, :size => 50)) %> | ||
6 | -<%= labelled_form_field(_('Source'), f.text_field(:source_name)) %> | ||
7 | -<%= labelled_form_field(_("Source URL"), f.text_field(:source)) %> | 3 | +<% unless task.requestor %> |
4 | + <%= labelled_form_field(_("Sent by: "), f.text_field(:name)) %> | ||
5 | + <p><%= label_tag(_("Email: %s") % task.email) %> </p> | ||
6 | +<% end %> | ||
8 | 7 | ||
9 | -<%= select_profile_folder(_('Select the folder where the article must be published'), "tasks[#{task.id}][task][article_parent_id]", task.target) %> | ||
10 | -<%= labelled_form_field(_('Highlight this article'), f.check_box(:highlighted)) %> | 8 | +<%= f.fields_for 'article', OpenStruct.new(task.article) do |a| %> |
9 | + <%= required labelled_form_field(_('Title'), a.text_field(:name, :size => 50)) %> | ||
10 | + <%= labelled_form_field(_('Source'), a.text_field(:source_name)) %> | ||
11 | + <%= labelled_form_field(_("Source URL"), a.text_field(:source)) %> | ||
11 | 12 | ||
12 | -<%= render :partial => 'shared/lead_and_body', :locals => {:tiny_mce => true, :f => f, :abstract_method => 'article_abstract', :body_method => 'article_body', :lead_id => task.id} %> | 13 | + <%= select_profile_folder(_('Select the folder where the article must be published'), "tasks[#{task.id}][task][article][parent_id]", task.target) %> |
14 | + <%= labelled_form_field(_('Highlight this article'), a.check_box(:highlighted)) %> | ||
15 | + | ||
16 | + <%= render :partial => 'shared/lead_and_body', :locals => {:tiny_mce => true, :f => a, :lead_id => task.id} %> | ||
17 | +<% end %> |
test/factories.rb
@@ -454,7 +454,7 @@ module Noosfero::Factory | @@ -454,7 +454,7 @@ module Noosfero::Factory | ||
454 | end | 454 | end |
455 | 455 | ||
456 | def defaults_for_suggest_article | 456 | def defaults_for_suggest_article |
457 | - { :name => 'Sender', :email => 'sender@example.com', :article_name => 'Some title', :article_body => 'some body text', :article_abstract => 'some abstract text'} | 457 | + { :name => 'Sender', :email => 'sender@example.com', :article => {:name => 'Some title', :body => 'some body text', :abstract => 'some abstract text'}} |
458 | end | 458 | end |
459 | 459 | ||
460 | def defaults_for_comment(params = {}) | 460 | def defaults_for_comment(params = {}) |
test/functional/cms_controller_test.rb
@@ -1407,22 +1407,57 @@ class CmsControllerTest < ActionController::TestCase | @@ -1407,22 +1407,57 @@ class CmsControllerTest < ActionController::TestCase | ||
1407 | assert_template 'suggest_an_article' | 1407 | assert_template 'suggest_an_article' |
1408 | end | 1408 | end |
1409 | 1409 | ||
1410 | + should 'display name and email when a not logged in user suggest an article' do | ||
1411 | + logout | ||
1412 | + get :suggest_an_article, :profile => profile.identifier, :back_to => 'action_view' | ||
1413 | + | ||
1414 | + assert_select '#task_name' | ||
1415 | + assert_select '#task_email' | ||
1416 | + end | ||
1417 | + | ||
1418 | + should 'do not display name and email when a logged in user suggest an article' do | ||
1419 | + get :suggest_an_article, :profile => profile.identifier, :back_to => 'action_view' | ||
1420 | + | ||
1421 | + assert_select '#task_name', 0 | ||
1422 | + assert_select '#task_email', 0 | ||
1423 | + end | ||
1424 | + | ||
1425 | + should 'display captcha when suggest an article for not logged in users' do | ||
1426 | + logout | ||
1427 | + get :suggest_an_article, :profile => profile.identifier, :back_to => 'action_view' | ||
1428 | + | ||
1429 | + assert_select '#dynamic_recaptcha' | ||
1430 | + end | ||
1431 | + | ||
1432 | + should 'not display captcha when suggest an article for logged in users' do | ||
1433 | + get :suggest_an_article, :profile => profile.identifier, :back_to => 'action_view' | ||
1434 | + | ||
1435 | + assert_select '#dynamic_recaptcha', 0 | ||
1436 | + end | ||
1437 | + | ||
1410 | should 'render TinyMce Editor on suggestion of article' do | 1438 | should 'render TinyMce Editor on suggestion of article' do |
1411 | logout | 1439 | logout |
1412 | get :suggest_an_article, :profile => profile.identifier | 1440 | get :suggest_an_article, :profile => profile.identifier |
1413 | 1441 | ||
1414 | - assert_tag :tag => 'textarea', :attributes => { :name => /article_abstract/, :class => 'mceEditor' } | ||
1415 | - assert_tag :tag => 'textarea', :attributes => { :name => /article_body/, :class => 'mceEditor' } | 1442 | + assert_tag :tag => 'textarea', :attributes => { :name => /task\[article\]\[abstract\]/, :class => 'mceEditor' } |
1443 | + assert_tag :tag => 'textarea', :attributes => { :name => /task\[article\]\[body\]/, :class => 'mceEditor' } | ||
1416 | end | 1444 | end |
1417 | 1445 | ||
1418 | should 'create a task suggest task to a profile' do | 1446 | should 'create a task suggest task to a profile' do |
1419 | c = Community.create!(:name => 'test comm', :identifier => 'test_comm', :moderated_articles => true) | 1447 | c = Community.create!(:name => 'test comm', :identifier => 'test_comm', :moderated_articles => true) |
1420 | 1448 | ||
1421 | assert_difference 'SuggestArticle.count' do | 1449 | assert_difference 'SuggestArticle.count' do |
1422 | - post :suggest_an_article, :profile => c.identifier, :back_to => 'action_view', :task => {:article_name => 'some name', :article_body => 'some body', :email => 'some@localhost.com', :name => 'some name'} | 1450 | + post :suggest_an_article, :profile => c.identifier, :back_to => 'action_view', :task => {:article => {:name => 'some name', :body => 'some body'}, :email => 'some@localhost.com', :name => 'some name'} |
1423 | end | 1451 | end |
1424 | end | 1452 | end |
1425 | 1453 | ||
1454 | + should 'create suggest task with logged in user as the article author' do | ||
1455 | + c = Community.create!(:name => 'test comm', :identifier => 'test_comm', :moderated_articles => true) | ||
1456 | + | ||
1457 | + post :suggest_an_article, :profile => c.identifier, :back_to => 'action_view', :task => {:article => {:name => 'some name', :body => 'some body'}} | ||
1458 | + assert_equal profile, SuggestArticle.last.requestor | ||
1459 | + end | ||
1460 | + | ||
1426 | should 'suggest an article from a profile' do | 1461 | should 'suggest an article from a profile' do |
1427 | c = Community.create!(:name => 'test comm', :identifier => 'test_comm', :moderated_articles => true) | 1462 | c = Community.create!(:name => 'test comm', :identifier => 'test_comm', :moderated_articles => true) |
1428 | get :suggest_an_article, :profile => c.identifier, :back_to => c.identifier | 1463 | get :suggest_an_article, :profile => c.identifier, :back_to => c.identifier |
test/functional/spam_controller_test.rb
@@ -10,7 +10,7 @@ class SpamControllerTest < ActionController::TestCase | @@ -10,7 +10,7 @@ class SpamControllerTest < ActionController::TestCase | ||
10 | @article = fast_create(TextileArticle, :profile_id => @community.id) | 10 | @article = fast_create(TextileArticle, :profile_id => @community.id) |
11 | @spam_comment = fast_create(Comment, :source_id => @article.id, :spam => true, :name => 'foo', :email => 'foo@example.com') | 11 | @spam_comment = fast_create(Comment, :source_id => @article.id, :spam => true, :name => 'foo', :email => 'foo@example.com') |
12 | 12 | ||
13 | - @spam_suggest_article = SuggestArticle.create!(:name => 'spammer', :article_name => 'Spam article', :email => 'spammer@shady.place', :article_body => "Something you don't need", :target => @community, :spam => true) | 13 | + @spam_suggest_article = SuggestArticle.create!(:name => 'spammer', :article => {:name => 'Spam article', :body => "Something you don't need"}, :email => 'spammer@shady.place', :target => @community, :spam => true) |
14 | login_as @profile.identifier | 14 | login_as @profile.identifier |
15 | end | 15 | end |
16 | 16 |
test/functional/tasks_controller_test.rb
@@ -264,11 +264,11 @@ class TasksControllerTest < ActionController::TestCase | @@ -264,11 +264,11 @@ class TasksControllerTest < ActionController::TestCase | ||
264 | c = fast_create(Community) | 264 | c = fast_create(Community) |
265 | c.add_admin profile | 265 | c.add_admin profile |
266 | @controller.stubs(:profile).returns(c) | 266 | @controller.stubs(:profile).returns(c) |
267 | - t = SuggestArticle.create!(:article_name => 'test name', :article_abstract => 'test abstract', :article_body => 'test body', :name => 'some name', :email => 'test@localhost.com', :target => c) | 267 | + t = SuggestArticle.create!(:article => {:name => 'test name', :abstract => 'test abstract', :body => 'test body'}, :name => 'some name', :email => 'test@localhost.com', :target => c) |
268 | 268 | ||
269 | get :index | 269 | get :index |
270 | - assert_tag :tag => 'textarea', :content => /test abstract/, :attributes => { :name => /article_abstract/, :class => 'mceEditor' } | ||
271 | - assert_tag :tag => 'textarea', :content => /test body/, :attributes => { :name => /article_body/, :class => 'mceEditor' } | 270 | + assert_tag :tag => 'textarea', :content => /test abstract/, :attributes => { :name => /tasks\[#{t.id}\]\[task\]\[article\]\[abstract\]/, :class => 'mceEditor' } |
271 | + assert_tag :tag => 'textarea', :content => /test body/, :attributes => { :name => /tasks\[#{t.id}\]\[task\]\[article\]\[body\]/, :class => 'mceEditor' } | ||
272 | end | 272 | end |
273 | 273 | ||
274 | should 'create TinyMceArticle article after finish approve suggested article task' do | 274 | should 'create TinyMceArticle article after finish approve suggested article task' do |
@@ -276,7 +276,7 @@ class TasksControllerTest < ActionController::TestCase | @@ -276,7 +276,7 @@ class TasksControllerTest < ActionController::TestCase | ||
276 | c = fast_create(Community) | 276 | c = fast_create(Community) |
277 | c.affiliate(profile, Profile::Roles.all_roles(profile.environment.id)) | 277 | c.affiliate(profile, Profile::Roles.all_roles(profile.environment.id)) |
278 | @controller.stubs(:profile).returns(c) | 278 | @controller.stubs(:profile).returns(c) |
279 | - t = SuggestArticle.create!(:article_name => 'test name', :article_body => 'test body', :name => 'some name', :email => 'test@localhost.com', :target => c) | 279 | + t = SuggestArticle.create!(:article => {:name => 'test name', :body => 'test body'}, :name => 'some name', :email => 'test@localhost.com', :target => c) |
280 | 280 | ||
281 | post :close, :tasks => {t.id => { :task => {}, :decision => "finish"}} | 281 | post :close, :tasks => {t.id => { :task => {}, :decision => "finish"}} |
282 | assert_not_nil TinyMceArticle.find(:first) | 282 | assert_not_nil TinyMceArticle.find(:first) |
@@ -288,16 +288,13 @@ class TasksControllerTest < ActionController::TestCase | @@ -288,16 +288,13 @@ class TasksControllerTest < ActionController::TestCase | ||
288 | c.affiliate(profile, Profile::Roles.all_roles(profile.environment.id)) | 288 | c.affiliate(profile, Profile::Roles.all_roles(profile.environment.id)) |
289 | @controller.stubs(:profile).returns(c) | 289 | @controller.stubs(:profile).returns(c) |
290 | t = SuggestArticle.new | 290 | t = SuggestArticle.new |
291 | - t.article_name = 'test name' | ||
292 | - t.article_body = 'test body' | 291 | + t.article = {:name => 'test name', :body => 'test body', :source => 'http://test.com', :source_name => 'some source name'} |
293 | t.name = 'some name' | 292 | t.name = 'some name' |
294 | - t.source = 'http://test.com' | ||
295 | - t.source_name = 'some source name' | ||
296 | t.email = 'test@localhost.com' | 293 | t.email = 'test@localhost.com' |
297 | t.target = c | 294 | t.target = c |
298 | t.save! | 295 | t.save! |
299 | 296 | ||
300 | - post :close, :tasks => {t.id => { :task => {:article_name => 'new article name', :article_body => 'new body', :source => 'http://www.noosfero.com', :source_name => 'new source', :name => 'new name'}, :decision => "finish"}} | 297 | + post :close, :tasks => {t.id => { :task => {:article => {:name => 'new article name', :body => 'new body', :source => 'http://www.noosfero.com', :source_name => 'new source'}, :name => 'new name'}, :decision => "finish"}} |
301 | assert_equal 'new article name', TinyMceArticle.find(:first).name | 298 | assert_equal 'new article name', TinyMceArticle.find(:first).name |
302 | assert_equal 'new name', TinyMceArticle.find(:first).author_name | 299 | assert_equal 'new name', TinyMceArticle.find(:first).author_name |
303 | assert_equal 'new body', TinyMceArticle.find(:first).body | 300 | assert_equal 'new body', TinyMceArticle.find(:first).body |
@@ -305,6 +302,28 @@ class TasksControllerTest < ActionController::TestCase | @@ -305,6 +302,28 @@ class TasksControllerTest < ActionController::TestCase | ||
305 | assert_equal 'new source', TinyMceArticle.find(:first).source_name | 302 | assert_equal 'new source', TinyMceArticle.find(:first).source_name |
306 | end | 303 | end |
307 | 304 | ||
305 | + should "display name from article suggestion when requestor was not setted" do | ||
306 | + Task.destroy_all | ||
307 | + c = fast_create(Community) | ||
308 | + c.add_admin profile | ||
309 | + @controller.stubs(:profile).returns(c) | ||
310 | + t = SuggestArticle.create!(:article => {:name => 'test name', :abstract => 'test abstract', :body => 'test body'}, :name => 'some name', :email => 'test@localhost.com', :target => c) | ||
311 | + | ||
312 | + get :index | ||
313 | + assert_select "#tasks_#{t.id}_task_name" | ||
314 | + end | ||
315 | + | ||
316 | + should "not display name from article suggestion when requestor was setted" do | ||
317 | + Task.destroy_all | ||
318 | + c = fast_create(Community) | ||
319 | + c.add_admin profile | ||
320 | + @controller.stubs(:profile).returns(c) | ||
321 | + t = SuggestArticle.create!(:article => {:name => 'test name', :abstract => 'test abstract', :body => 'test body'}, :requestor => fast_create(Person), :target => c) | ||
322 | + | ||
323 | + get :index | ||
324 | + assert_select "#tasks_#{t.id}_task_name", 0 | ||
325 | + end | ||
326 | + | ||
308 | should "not crash if accessing close without tasks parameter" do | 327 | should "not crash if accessing close without tasks parameter" do |
309 | assert_nothing_raised do | 328 | assert_nothing_raised do |
310 | post :close | 329 | post :close |
test/unit/suggest_article_test.rb
@@ -13,16 +13,9 @@ class SuggestArticleTest < ActiveSupport::TestCase | @@ -13,16 +13,9 @@ class SuggestArticleTest < ActiveSupport::TestCase | ||
13 | 13 | ||
14 | should 'have the article_name' do | 14 | should 'have the article_name' do |
15 | t = SuggestArticle.new | 15 | t = SuggestArticle.new |
16 | - assert !t.errors[:article_name.to_s].present? | ||
17 | - t.valid? | ||
18 | - assert t.errors[:article_name.to_s].present? | ||
19 | - end | ||
20 | - | ||
21 | - should 'have the article_body' do | ||
22 | - t = SuggestArticle.new | ||
23 | - assert !t.errors[:article_body.to_s].present? | ||
24 | - t.valid? | ||
25 | - assert t.errors[:article_body.to_s].present? | 16 | + assert !t.article_object.errors[:name].present? |
17 | + t.article_object.valid? | ||
18 | + assert t.article_object.errors[:name].present? | ||
26 | end | 19 | end |
27 | 20 | ||
28 | should 'have the email' do | 21 | should 'have the email' do |
@@ -46,19 +39,12 @@ class SuggestArticleTest < ActiveSupport::TestCase | @@ -46,19 +39,12 @@ class SuggestArticleTest < ActiveSupport::TestCase | ||
46 | assert t.errors[:target_id.to_s].present? | 39 | assert t.errors[:target_id.to_s].present? |
47 | end | 40 | end |
48 | 41 | ||
49 | - should 'have the article_abstract' do | ||
50 | - t = SuggestArticle.new | ||
51 | - assert t.respond_to?(:article_abstract) | ||
52 | - end | ||
53 | - | ||
54 | - should 'have the article_parent_id' do | ||
55 | - t = SuggestArticle.new | ||
56 | - assert t.respond_to?(:article_parent_id) | ||
57 | - end | ||
58 | - | ||
59 | - should 'source be defined' do | 42 | + should 'have the article' do |
60 | t = SuggestArticle.new | 43 | t = SuggestArticle.new |
61 | - assert t.respond_to?(:source) | 44 | + assert t.respond_to?(:article_object) |
45 | + assert !t.errors[:article_object].present? | ||
46 | + t.valid? | ||
47 | + assert t.errors[:article_object].present? | ||
62 | end | 48 | end |
63 | 49 | ||
64 | should 'create an article on with perfom method' do | 50 | should 'create an article on with perfom method' do |
@@ -66,9 +52,7 @@ class SuggestArticleTest < ActiveSupport::TestCase | @@ -66,9 +52,7 @@ class SuggestArticleTest < ActiveSupport::TestCase | ||
66 | name = 'some name' | 52 | name = 'some name' |
67 | body = 'some body' | 53 | body = 'some body' |
68 | abstract = 'some abstract' | 54 | abstract = 'some abstract' |
69 | - t.article_name = name | ||
70 | - t.article_body = body | ||
71 | - t.article_abstract = abstract | 55 | + t.article = {:name => name, :body => body, :abstract => abstract} |
72 | t.target = @profile | 56 | t.target = @profile |
73 | count = TinyMceArticle.count | 57 | count = TinyMceArticle.count |
74 | t.perform | 58 | t.perform |
@@ -77,8 +61,7 @@ class SuggestArticleTest < ActiveSupport::TestCase | @@ -77,8 +61,7 @@ class SuggestArticleTest < ActiveSupport::TestCase | ||
77 | 61 | ||
78 | should 'fill source name and URL into created article' do | 62 | should 'fill source name and URL into created article' do |
79 | t = build(SuggestArticle, :target => @profile) | 63 | t = build(SuggestArticle, :target => @profile) |
80 | - t.source_name = 'GNU project' | ||
81 | - t.source = 'http://www.gnu.org/' | 64 | + t.article.merge!({:source_name => 'GNU project', :source => 'http://www.gnu.org/'}) |
82 | t.perform | 65 | t.perform |
83 | 66 | ||
84 | article = TinyMceArticle.last | 67 | article = TinyMceArticle.last |
@@ -95,7 +78,7 @@ class SuggestArticleTest < ActiveSupport::TestCase | @@ -95,7 +78,7 @@ class SuggestArticleTest < ActiveSupport::TestCase | ||
95 | 78 | ||
96 | should 'highlight created article' do | 79 | should 'highlight created article' do |
97 | t = build(SuggestArticle, :target => @profile) | 80 | t = build(SuggestArticle, :target => @profile) |
98 | - t.highlighted = true | 81 | + t.article[:highlighted] = true |
99 | t.perform | 82 | t.perform |
100 | 83 | ||
101 | article = TinyMceArticle.last(:conditions => { :name => t.article_name}) # just to be sure | 84 | article = TinyMceArticle.last(:conditions => { :name => t.article_name}) # just to be sure |
@@ -132,19 +115,19 @@ class SuggestArticleTest < ActiveSupport::TestCase | @@ -132,19 +115,19 @@ class SuggestArticleTest < ActiveSupport::TestCase | ||
132 | end | 115 | end |
133 | 116 | ||
134 | should 'have target notification message' do | 117 | should 'have target notification message' do |
135 | - task = build(SuggestArticle, :target => @profile, :article_name => 'suggested article', :name => 'johndoe') | 118 | + task = build(SuggestArticle, :target => @profile, :article => {:name => 'suggested article'}, :name => 'johndoe') |
136 | 119 | ||
137 | assert_match(/#{task.name}.*suggested the publication of the article: #{task.subject}.*[\n]*.*to approve or reject/, task.target_notification_message) | 120 | assert_match(/#{task.name}.*suggested the publication of the article: #{task.subject}.*[\n]*.*to approve or reject/, task.target_notification_message) |
138 | end | 121 | end |
139 | 122 | ||
140 | should 'have target notification description' do | 123 | should 'have target notification description' do |
141 | - task = build(SuggestArticle,:target => @profile, :article_name => 'suggested article', :name => 'johndoe') | 124 | + task = build(SuggestArticle,:target => @profile, :article => {:name => 'suggested article'}, :name => 'johndoe') |
142 | 125 | ||
143 | assert_match(/#{task.name}.*suggested the publication of the article: #{task.subject}/, task.target_notification_description) | 126 | assert_match(/#{task.name}.*suggested the publication of the article: #{task.subject}/, task.target_notification_description) |
144 | end | 127 | end |
145 | 128 | ||
146 | should 'deliver target notification message' do | 129 | should 'deliver target notification message' do |
147 | - task = build(SuggestArticle, :target => @profile, :article_name => 'suggested article', :name => 'johndoe', :email => 'johndoe@example.com') | 130 | + task = build(SuggestArticle, :target => @profile, :article => {:name => 'suggested article'}, :name => 'johndoe', :email => 'johndoe@example.com') |
148 | 131 | ||
149 | email = TaskMailer.target_notification(task, task.target_notification_message).deliver | 132 | email = TaskMailer.target_notification(task, task.target_notification_message).deliver |
150 | 133 | ||
@@ -160,7 +143,7 @@ class SuggestArticleTest < ActiveSupport::TestCase | @@ -160,7 +143,7 @@ class SuggestArticleTest < ActiveSupport::TestCase | ||
160 | should 'delegate spam detection to plugins' do | 143 | should 'delegate spam detection to plugins' do |
161 | Environment.default.enable_plugin(EverythingIsSpam) | 144 | Environment.default.enable_plugin(EverythingIsSpam) |
162 | 145 | ||
163 | - t1 = build(SuggestArticle, :target => @profile, :article_name => 'suggested article', :name => 'johndoe', :email => 'johndoe@example.com') | 146 | + t1 = build(SuggestArticle, :target => @profile, :article => {:name => 'suggested article'}, :name => 'johndoe', :email => 'johndoe@example.com') |
164 | 147 | ||
165 | EverythingIsSpam.any_instance.expects(:check_for_spam) | 148 | EverythingIsSpam.any_instance.expects(:check_for_spam) |
166 | 149 | ||
@@ -189,7 +172,7 @@ class SuggestArticleTest < ActiveSupport::TestCase | @@ -189,7 +172,7 @@ class SuggestArticleTest < ActiveSupport::TestCase | ||
189 | should 'notify plugins of suggest_articles being marked as spam' do | 172 | should 'notify plugins of suggest_articles being marked as spam' do |
190 | Environment.default.enable_plugin(SpamNotification) | 173 | Environment.default.enable_plugin(SpamNotification) |
191 | 174 | ||
192 | - t = SuggestArticle.create!(:target => @profile, :article_name => 'suggested article', :name => 'johndoe', :article_body => 'wanna feel my body? my body baaaby', :email => 'johndoe@example.com') | 175 | + t = SuggestArticle.create!(:target => @profile, :article => {:name => 'suggested article', :body => 'wanna feel my body? my body baaaby'}, :name => 'johndoe', :email => 'johndoe@example.com') |
193 | 176 | ||
194 | t.spam! | 177 | t.spam! |
195 | process_delayed_job_queue | 178 | process_delayed_job_queue |
@@ -200,7 +183,7 @@ class SuggestArticleTest < ActiveSupport::TestCase | @@ -200,7 +183,7 @@ class SuggestArticleTest < ActiveSupport::TestCase | ||
200 | should 'notify plugins of suggest_articles being marked as ham' do | 183 | should 'notify plugins of suggest_articles being marked as ham' do |
201 | Environment.default.enable_plugin(SpamNotification) | 184 | Environment.default.enable_plugin(SpamNotification) |
202 | 185 | ||
203 | - t = SuggestArticle.create!(:target => @profile, :article_name => 'suggested article', :name => 'johndoe', :article_body => 'wanna feel my body? my body baaaby', :email => 'johndoe@example.com') | 186 | + t = SuggestArticle.create!(:target => @profile, :article => {:name => 'suggested article', :body => 'wanna feel my body? my body baaaby'}, :name => 'johndoe', :email => 'johndoe@example.com') |
204 | 187 | ||
205 | t.ham! | 188 | t.ham! |
206 | process_delayed_job_queue | 189 | process_delayed_job_queue |
@@ -219,10 +202,32 @@ class SuggestArticleTest < ActiveSupport::TestCase | @@ -219,10 +202,32 @@ class SuggestArticleTest < ActiveSupport::TestCase | ||
219 | end | 202 | end |
220 | 203 | ||
221 | should 'log spammer ip after marking comment as spam' do | 204 | should 'log spammer ip after marking comment as spam' do |
222 | - t = SuggestArticle.create!(:target => @profile, :article_name => 'suggested article', :name => 'johndoe', :article_body => 'wanna feel my body? my body baaaby', :email => 'johndoe@example.com', :ip_address => '192.168.0.1') | 205 | + t = SuggestArticle.create!(:target => @profile, :article => {:name => 'suggested article', :body => 'wanna feel my body? my body baaaby'}, :name => 'johndoe', :email => 'johndoe@example.com', :ip_address => '192.168.0.1') |
223 | t.spam! | 206 | t.spam! |
224 | log = File.open('log/test_spammers.log') | 207 | log = File.open('log/test_spammers.log') |
225 | assert_match "SuggestArticle-id: #{t.id} IP: 192.168.0.1", log.read | 208 | assert_match "SuggestArticle-id: #{t.id} IP: 192.168.0.1", log.read |
226 | end | 209 | end |
227 | 210 | ||
211 | + should 'not require name and email when requestor is present' do | ||
212 | + t = SuggestArticle.new(:requestor => fast_create(Person)) | ||
213 | + t.valid? | ||
214 | + assert t.errors[:email].blank? | ||
215 | + assert t.errors[:name].blank? | ||
216 | + end | ||
217 | + | ||
218 | + should 'return name as sender when requestor is setted' do | ||
219 | + person = fast_create(Person) | ||
220 | + t = SuggestArticle.new(:requestor => person) | ||
221 | + assert_equal person.name, t.sender | ||
222 | + end | ||
223 | + | ||
224 | + should 'create an event on perfom method' do | ||
225 | + t = SuggestArticle.new | ||
226 | + t.article = {:name => 'name', :body => 'body', :type => 'Event'} | ||
227 | + t.target = @profile | ||
228 | + assert_difference "Event.count" do | ||
229 | + t.perform | ||
230 | + end | ||
231 | + end | ||
232 | + | ||
228 | end | 233 | end |