Commit 5d49527b070d9f9215ec916bae713808b939ac6d

Authored by Antonio Terceiro
1 parent 9d5ea852

ActionItem833: enterprise validation must ignore validation

Since administrators can add or set as mandatory new fields after the
enterprises are already in the database, enabling the enterprises must
bypass the full validation process otherwise it will fail if there are
invalid fields (according to the newly created validation rules!).
app/models/enterprise.rb
... ... @@ -74,7 +74,7 @@ class Enterprise < Organization
74 74 if environment.replace_enterprise_template_when_enable
75 75 apply_template(template)
76 76 end
77   - save
  77 + save_without_validation!
78 78 end
79 79  
80 80 def question
... ...
app/models/profile.rb
... ... @@ -221,7 +221,14 @@ class Profile < ActiveRecord::Base
221 221 def apply_template(template)
222 222 copy_blocks_from(template)
223 223 copy_articles_from(template)
224   - self.update_attributes!(:layout_template => template.layout_template, :custom_footer => template[:custom_footer], :custom_header => template[:custom_header])
  224 +
  225 + # copy interesting attributes
  226 + self.layout_template = template.layout_template
  227 + self.custom_footer = template[:custom_footer]
  228 + self.custom_header = template[:custom_header]
  229 +
  230 + # flush
  231 + self.save_without_validation!
225 232 end
226 233  
227 234 xss_terminate :only => [ :name, :nickname, :address, :contact_phone ]
... ...
test/unit/enterprise_test.rb
... ... @@ -214,6 +214,22 @@ class EnterpriseTest < Test::Unit::TestCase
214 214 end
215 215 end
216 216  
  217 + should 'be able to enable even if there are mandatory fields blank' do
  218 + # enterprise is created, waiting for being enabled
  219 + environment = Environment.create!(:name => 'my test environment')
  220 + enterprise = Enterprise.create!(:name => 'test enterprise', :identifier => 'test_ent', :enabled => false, :environment => environment)
  221 +
  222 + # administrator decides now that the 'city' field is mandatory
  223 + environment.custom_enterprise_fields = { 'city' => { 'active' => 'true', 'required' => 'true' } }
  224 + environment.save!
  225 + assert_equal ['city'], environment.required_enterprise_fields
  226 +
  227 + # then we try to enable the enterprise with a required field is blank
  228 + enterprise = Enterprise.find(enterprise.id)
  229 + person = profiles(:ze)
  230 + assert enterprise.enable(person)
  231 + end
  232 +
217 233 should 'list product categories full name' do
218 234 full_name = mock
219 235 ent = Enterprise.create!(:name => 'test ent', :identifier => 'test_ent')
... ...
test/unit/profile_test.rb
... ... @@ -1036,6 +1036,23 @@ class ProfileTest < Test::Unit::TestCase
1036 1036 assert_equal 'Profile address', p.custom_footer_expanded
1037 1037 end
1038 1038  
  1039 + should 'ignore failing validation when applying template' do
  1040 + template = Profile.create!(:name => 'test template', :identifier => 'test_template', :address => 'Template address', :layout_template => 'leftbar', :custom_footer => 'my custom footer', :custom_header => 'my custom header')
  1041 + template.save!
  1042 +
  1043 + p = Profile.create!(:name => 'test prof', :identifier => 'test_prof', :address => 'Profile address')
  1044 + def p.validate
  1045 + self.errors.add('identifier', 'is invalid')
  1046 + end
  1047 +
  1048 + p.apply_template(template)
  1049 +
  1050 + p.reload
  1051 + assert_equal 'leftbar', p.layout_template
  1052 + assert_equal 'my custom footer', p.custom_footer
  1053 + assert_equal 'my custom header', p.custom_header
  1054 + end
  1055 +
1039 1056 should 'copy homepage when applying template' do
1040 1057 template = Profile.create!(:name => 'test template', :identifier => 'test_template', :address => 'Template address')
1041 1058 template.articles.destroy_all
... ...