From 86621e255b1feef5fa09943fa5bc4028bda47273 Mon Sep 17 00:00:00 2001 From: Colin Kuskie Date: Sun, 26 Apr 2009 04:02:38 +0000 Subject: [PATCH] Fix a bug where changing fieldTypes in the userProfileField did not update the database field types. --- docs/changelog/7.x.x.txt | 1 + lib/WebGUI/ProfileField.pm | 18 ++++++++++++------ t/ProfileField.t | 35 +++++++++++++++++++++++++++++++---- 3 files changed, 44 insertions(+), 10 deletions(-) diff --git a/docs/changelog/7.x.x.txt b/docs/changelog/7.x.x.txt index 03dca6a7c..b70a5286a 100644 --- a/docs/changelog/7.x.x.txt +++ b/docs/changelog/7.x.x.txt @@ -3,6 +3,7 @@ - fixed #10223: Calendar List View Ignores Event Permissions (dhelsten) - fixed #10226: html2text dropping text - fixed #10210: Generated Message-Id invalid (patch basis from tektek) + - fixed #10209: Changing existing user profile field type doesn't change underlying database column type 7.7.4 - rfe: Extend DateTime for Week-Nrs (#9151) diff --git a/lib/WebGUI/ProfileField.pm b/lib/WebGUI/ProfileField.pm index cc1f14ed5..5faf0897c 100644 --- a/lib/WebGUI/ProfileField.pm +++ b/lib/WebGUI/ProfileField.pm @@ -828,8 +828,19 @@ sub set { } $properties->{fieldName} = $self->getId; + ##Save the fieldType now. It can't be chacked against getFormControlClass now + ##because it will return the OLD formControlClass, not the new one that we need + ##to check against. + my $originalFieldType = $self->get('fieldType'); + + # Update the record + $db->setRow("userProfileField","fieldName",$properties); + foreach my $key (keys %{$properties}) { + $self->{_properties}{$key} = $properties->{$key}; + } + # If the fieldType has changed, modify the userProfileData column - if ($properties->{fieldType} ne $self->get("fieldType")) { + if ($properties->{fieldType} ne $originalFieldType) { # Create a copy of the new properties so we don't mess them up my $fieldClass = $self->getFormControlClass; eval "use $fieldClass;"; @@ -845,11 +856,6 @@ sub set { $db->write($sql); } - # Update the record - $db->setRow("userProfileField","fieldName",$properties); - foreach my $key (keys %{$properties}) { - $self->{_properties}{$key} = $properties->{$key}; - } } #------------------------------------------------------------------- diff --git a/t/ProfileField.t b/t/ProfileField.t index 16ae27df3..0b20789bc 100644 --- a/t/ProfileField.t +++ b/t/ProfileField.t @@ -17,19 +17,23 @@ use FindBin; use strict; use lib "$FindBin::Bin/lib"; use Test::More; +use Data::Dumper; use WebGUI::Test; # Must use this before any other WebGUI modules use WebGUI::Session; +use WebGUI::Form::Text; +use WebGUI::Form::HTMLArea; #---------------------------------------------------------------------------- # Init my $session = WebGUI::Test->session; my $newUser = WebGUI::User->create( $session ); +WebGUI::Test->usersToDelete($newUser); #---------------------------------------------------------------------------- # Tests -plan tests => 17; # Increment this number for each test you create +plan tests => 20; # Increment this number for each test you create #---------------------------------------------------------------------------- # Test the creation of ProfileField @@ -66,19 +70,42 @@ like( $ff, qr/value="$ffvalue"[^>]+selected/, 'html returned contains value, uiL $ff = undef; $ffvalue = undef; ok( $ff = $aliasField->formField(undef, undef, $newUser), 'formField method returns something, alias field, defaulted user' ); -my $ffvalue = $newUser->profileField('alias'); +$ffvalue = $newUser->profileField('alias'); like( $ff, qr/$ffvalue/, 'html returned contains value, alias field, defaulted user' ); $ff = undef; $ffvalue = undef; ok( $ff = $uilevelField->formField(undef, undef, $newUser), 'formField method returns something, uiLevel field, defaulted user' ); -my $ffvalue = $newUser->profileField('uiLevel'); +$ffvalue = $newUser->profileField('uiLevel'); like( $ff, qr/$ffvalue/, 'html returned contains value, uiLevel field, defaulted user' ); +######################################################### +# +# set, changing fieldTypes +# +######################################################### + +my $newProfileField = WebGUI::ProfileField->create($session, 'testField', { + fieldType => 'Text', + label => 'Test Field', +}); + +my $textFieldType = lc WebGUI::Form::Text->getDatabaseFieldType(); +my $htmlFieldType = lc WebGUI::Form::HTMLArea->getDatabaseFieldType(); + +my $fieldSpec = $session->db->quickHashRef('describe userProfileData testField'); +is (lc $fieldSpec->{Type}, $textFieldType, 'test field created with correct type for text field'); + +$newProfileField->set({ fieldType => 'HTMLArea' }); +is($newProfileField->get('fieldType'), 'HTMLArea', 'test field updated to HTMLArea'); + +$fieldSpec = $session->db->quickHashRef('describe userProfileData testField'); +is (lc $fieldSpec->{Type}, $htmlFieldType, 'database updated along with profile field object'); + #---------------------------------------------------------------------------- # Cleanup END { - $newUser->delete; + $newProfileField->delete; }