Commit caa6c62d1e7033ca3fd85cd29b414a48bde69c1e

Authored by JoenioCosta
1 parent 7bc4e5ce

ActionItem498: fixing tests for new categories selector

git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@2248 3f533792-8f58-4932-b0fe-aaf55b0a4547
app/controllers/my_profile/cms_controller.rb
@@ -121,7 +121,7 @@ class CmsController < MyProfileController @@ -121,7 +121,7 @@ class CmsController < MyProfileController
121 @current_category = Category.find(params[:category_id]) 121 @current_category = Category.find(params[:category_id])
122 @categories = @current_category.children 122 @categories = @current_category.children
123 else 123 else
124 - @categories = @categories = environment.top_level_categories.select{|i| !i.children.empty?} 124 + @categories = environment.top_level_categories.select{|i| !i.children.empty?}
125 end 125 end
126 render :partial => 'shared/select_categories', :locals => {:object_name => 'article', :multiple => true}, :layout => false 126 render :partial => 'shared/select_categories', :locals => {:object_name => 'article', :multiple => true}, :layout => false
127 end 127 end
app/controllers/my_profile/profile_editor_controller.rb
@@ -39,9 +39,14 @@ class ProfileEditorController < MyProfileController @@ -39,9 +39,14 @@ class ProfileEditorController < MyProfileController
39 end 39 end
40 40
41 def update_categories 41 def update_categories
42 - @current_category = Category.find(params[:category_id])  
43 - @categories = @current_category.children  
44 - render :partial => 'shared/select_categories', :locals => {:object_name => 'profile_data'}, :layout => false 42 + @object = profile
  43 + if params[:category_id]
  44 + @current_category = Category.find(params[:category_id])
  45 + @categories = @current_category.children
  46 + else
  47 + @categories = environment.top_level_categories.select{|i| !i.children.empty?}
  48 + end
  49 + render :partial => 'shared/select_categories', :locals => {:object_name => 'profile_data', :multiple => true}, :layout => false
45 end 50 end
46 51
47 end 52 end
app/models/profile.rb
@@ -127,7 +127,7 @@ class Profile < ActiveRecord::Base @@ -127,7 +127,7 @@ class Profile < ActiveRecord::Base
127 127
128 def category_ids=(ids) 128 def category_ids=(ids)
129 ProfileCategorization.remove_all_for(self) 129 ProfileCategorization.remove_all_for(self)
130 - ids.each do |item| 130 + ids.uniq.each do |item|
131 add_category(Category.find(item)) 131 add_category(Category.find(item))
132 end 132 end
133 end 133 end
app/views/profile_editor/edit.rhtml
@@ -36,7 +36,7 @@ @@ -36,7 +36,7 @@
36 </div> 36 </div>
37 </p> 37 </p>
38 38
39 - <%= select_categories(:profile_data, _('Select the categories of your interest'), 1) %> 39 + <%= select_categories(:profile_data, _('Select the categories of your interest'), :title_size => 1, :multiple => true) %>
40 40
41 <% button_bar do %> 41 <% button_bar do %>
42 <%= submit_button('save', _('Save'), :cancel => {:action => 'index'}) %> 42 <%= submit_button('save', _('Save'), :cancel => {:action => 'index'}) %>
test/functional/cms_controller_test.rb
@@ -296,7 +296,7 @@ class CmsControllerTest &lt; Test::Unit::TestCase @@ -296,7 +296,7 @@ class CmsControllerTest &lt; Test::Unit::TestCase
296 post :new, :type => TextileArticle.name, :profile => profile.identifier, :article => { :name => 'adding-categories-test', :category_ids => [ c1.id, c3.id, c3.id ] } 296 post :new, :type => TextileArticle.name, :profile => profile.identifier, :article => { :name => 'adding-categories-test', :category_ids => [ c1.id, c3.id, c3.id ] }
297 297
298 saved = profile.articles.find_by_name('adding-categories-test') 298 saved = profile.articles.find_by_name('adding-categories-test')
299 - assert_equal [c1, c3, c3], saved.categories 299 + assert_equal [c1, c3], saved.categories
300 end 300 end
301 301
302 should 'filter html from textile article name' do 302 should 'filter html from textile article name' do
test/functional/profile_editor_controller_test.rb
@@ -83,7 +83,7 @@ class ProfileEditorControllerTest &lt; Test::Unit::TestCase @@ -83,7 +83,7 @@ class ProfileEditorControllerTest &lt; Test::Unit::TestCase
83 get :edit, :profile => 'test_user' 83 get :edit, :profile => 'test_user'
84 assert_response :success 84 assert_response :success
85 assert_template 'edit' 85 assert_template 'edit'
86 - assert_tag :tag => 'input', :attributes => {:name => 'profile_data[category_ids][]', :value => cat2.id} 86 + assert_tag :tag => 'a', :attributes => { :id => "select-category-#{cat1.id}-link" }
87 end 87 end
88 88
89 should 'save categorization of profile' do 89 should 'save categorization of profile' do
@@ -259,12 +259,12 @@ class ProfileEditorControllerTest &lt; Test::Unit::TestCase @@ -259,12 +259,12 @@ class ProfileEditorControllerTest &lt; Test::Unit::TestCase
259 assert_tag :tag => 'input', :attributes => { :name => 'profile_data[image_builder][uploaded_data]' } 259 assert_tag :tag => 'input', :attributes => { :name => 'profile_data[image_builder][uploaded_data]' }
260 end 260 end
261 261
262 - should 'show categories field on edit profile' do 262 + should 'show categories links on edit profile' do
263 cat1 = Environment.default.categories.create!(:display_in_menu => true, :name => 'top category') 263 cat1 = Environment.default.categories.create!(:display_in_menu => true, :name => 'top category')
264 cat2 = Environment.default.categories.create!(:display_in_menu => true, :name => 'sub category', :parent => cat1) 264 cat2 = Environment.default.categories.create!(:display_in_menu => true, :name => 'sub category', :parent => cat1)
265 person = create_user('testuser').person 265 person = create_user('testuser').person
266 get :edit, :profile => 'testuser' 266 get :edit, :profile => 'testuser'
267 - assert_tag :tag => 'input', :attributes => { :type => 'checkbox', :name => 'profile_data[category_ids][]', :value => cat2.id} 267 + assert_tag :tag => 'a', :attributes => { :id => "select-category-#{cat1.id}-link" }
268 end 268 end
269 269
270 should 'render edit template' do 270 should 'render edit template' do
test/unit/profile_test.rb
@@ -677,6 +677,15 @@ class ProfileTest &lt; Test::Unit::TestCase @@ -677,6 +677,15 @@ class ProfileTest &lt; Test::Unit::TestCase
677 assert_kind_of TinyMceArticle, profile.home_page 677 assert_kind_of TinyMceArticle, profile.home_page
678 end 678 end
679 679
  680 + should 'not add a category twice to profile' do
  681 + c1 = Category.create!(:environment => Environment.default, :name => 'c1')
  682 + c2 = c1.children.create!(:environment => Environment.default, :name => 'c2')
  683 + c3 = c1.children.create!(:environment => Environment.default, :name => 'c3')
  684 + profile = create_user('testuser').person
  685 + profile.category_ids = [c2,c3,c3].map(&:id)
  686 + assert_equal [c2, c3], profile.categories(true)
  687 + end
  688 +
680 private 689 private
681 690
682 def assert_invalid_identifier(id) 691 def assert_invalid_identifier(id)