From e36d550e1904629517cb87d83e10f59e732081b2 Mon Sep 17 00:00:00 2001 From: Colin Kuskie Date: Thu, 7 May 2009 17:26:12 -0700 Subject: [PATCH] Add a whole raft of tests, many bug fixes. Removed unused parts of the original JSON collateral code, like getAllCollateral, etc. --- lib/WebGUI/FilePump/Bundle.pm | 103 ++++++++------------------- t/FilePump/Bundle.t | 130 +++++++++++++++++++++++++++++++--- 2 files changed, 149 insertions(+), 84 deletions(-) diff --git a/lib/WebGUI/FilePump/Bundle.pm b/lib/WebGUI/FilePump/Bundle.pm index 23e569938..4feacb554 100644 --- a/lib/WebGUI/FilePump/Bundle.pm +++ b/lib/WebGUI/FilePump/Bundle.pm @@ -5,6 +5,8 @@ use WebGUI::International; use WebGUI::Utility; use URI; use Path::Class::Dir; +use CSS::Minifier::XS; +use JavaScript::Minifier::XS; #------------------------------------------------------------------- @@ -28,13 +30,13 @@ it will return 0 and an error message. sub addFile { my ($self, $type, $uri) = @_; - return 0, 'No URI' unless $uri; return 0, 'Illegal type' unless WebGUI::Utility::isIn($type, 'JS', 'CSS', 'OTHER'); + return 0, 'No URI' unless $uri; my $collateralType = $type eq 'JS' ? 'jsFiles' : $type eq 'CSS' ? 'cssFiles' : 'otherFiles'; - my $files = $self->getAllCollateral($collateralType); - my $uriExists = $self->getCollateralDataIndex($files, 'uri', $uri) == -1; + my $files = $self->get($collateralType); + my $uriExists = $self->getCollateralDataIndex($files, 'uri', $uri) != -1 ? 1 : 0; return 0, 'Duplicate URI' if $uriExists; $self->setCollateral( $collateralType, @@ -144,17 +146,17 @@ sub crud_definition { }; $properties->{jsFiles} = { fieldName => 'textarea', - defaultValue => 0, + defaultValue => [], serialize => 1, }; $properties->{cssFiles} = { fieldName => 'textarea', - defaultValue => 0, + defaultValue => [], serialize => 1, }; $properties->{otherFiles} = { fieldName => 'textarea', - defaultValue => 0, + defaultValue => [], serialize => 1, }; return $definition; @@ -202,11 +204,11 @@ sub deleteCollateral { my $tableName = shift; my $keyName = shift; my $keyValue = shift; - my $table = $self->getAllCollateral($tableName); + my $table = $self->get($tableName); my $index = $self->getCollateralDataIndex($table, $keyName, $keyValue); return if $index == -1; splice @{ $table }, $index, 1; - $self->setAllCollateral($tableName); + $self->update({ $tableName => $table }); } #------------------------------------------------------------------- @@ -229,8 +231,8 @@ The unique collateral GUID to delete from the bundle. sub deleteFile { my ($self, $type, $fileId) = @_; - return 0, 'No fileId' unless $fileId; return 0, 'Illegal type' unless WebGUI::Utility::isIn($type, 'JS', 'CSS', 'OTHER'); + return 0, 'No fileId' unless $fileId; my $collateralType = $type eq 'JS' ? 'jsFiles' : $type eq 'CSS' ? 'cssFiles' : 'otherFiles'; @@ -243,36 +245,6 @@ sub deleteFile { return 1; } -#------------------------------------------------------------------- - -=head2 getAllCollateral ( tableName ) - -Returns an array reference to the translated JSON data for the -requested collateral table. - -=head3 tableName - -The name of the table you wish to retrieve the data from. - -=cut - -sub getAllCollateral { - my $self = shift; - my $tableName = shift; - return $self->{_collateral}->{$tableName} if exists $self->{_collateral}->{$tableName}; - my $json = $self->get($tableName); - my $table; - if ($json) { - $table = from_json($json); - } - else { - $table = []; - } - $self->{_collateral}->{$tableName} = $table; - return $table; -} - - #------------------------------------------------------------------- =head2 getCollateral ( tableName, keyName, keyValue ) @@ -306,7 +278,7 @@ sub getCollateral { if ($keyValue eq "new" || $keyValue eq "") { return {}; } - my $table = $self->getAllCollateral($tableName); + my $table = $self->get($tableName); my $index = $self->getCollateralDataIndex($table, $keyName, $keyValue); return {} if $index == -1; my %copy = %{ $table->[$index] }; @@ -343,7 +315,7 @@ sub getCollateralDataIndex { my $keyValue = shift; for (my $index=0; $index <= $#{ $table }; $index++) { return $index - if (exists $table->[$index]->{$keyName} and $table->[$index]->{$keyName} eq $keyValue ); + if (exists($table->[$index]->{$keyName}) && ($table->[$index]->{$keyName} eq $keyValue )); } return -1; } @@ -360,7 +332,7 @@ for this bundle. sub getPathClassDir { my ($self) = @_; return Path::Class::Dir->new( - $self->session->get('uploadsPath'), + $self->session->config->get('uploadsPath'), 'filepump', $self->get('bundleName') . $self->get('lastBuild') ); @@ -398,7 +370,8 @@ sub getOutOfDateBundles { =head2 moveCollateralDown ( tableName, keyName, keyValue ) Moves a collateral data item down one position. If called on the last element of the -collateral array then it does nothing. +collateral array then it does nothing. Returns 1 if the move is successful. Returns +undef or the empty array otherwise. =head3 tableName @@ -421,12 +394,13 @@ sub moveCollateralDown { my $keyName = shift; my $keyValue = shift; - my $table = $self->getAllCollateral($tableName); + my $table = $self->get($tableName); my $index = $self->getCollateralDataIndex($table, $keyName, $keyValue); return if $index == -1; return unless (abs($index) < $#{$table}); @{ $table }[$index,$index+1] = @{ $table }[$index+1,$index]; - $self->setAllCollateral($tableName); + $self->update({ $tableName => $table }); + return 1; } @@ -435,7 +409,9 @@ sub moveCollateralDown { =head2 moveCollateralUp ( tableName, keyName, keyValue ) Moves a collateral data item up one position. If called on the first element of the -collateral array then it does nothing. +collateral array then it does nothing. Returns 1 if the move is successful. Returns +undef or the empty array otherwise. + =head3 tableName @@ -458,12 +434,13 @@ sub moveCollateralUp { my $keyName = shift; my $keyValue = shift; - my $table = $self->getAllCollateral($tableName); + my $table = $self->get($tableName); my $index = $self->getCollateralDataIndex($table, $keyName, $keyValue); return if $index == -1; return unless $index && (abs($index) <= $#{$table}); @{ $table }[$index-1,$index] = @{ $table }[$index,$index-1]; - $self->setAllCollateral($tableName); + $self->update({ $tableName => $table }); + return 1; } #------------------------------------------------------------------- @@ -486,8 +463,8 @@ The unique collateral GUID to move in the bundle. sub moveFileDown { my ($self, $type, $fileId) = @_; - return 0, 'No fileId' unless $fileId; return 0, 'Illegal type' unless WebGUI::Utility::isIn($type, 'JS', 'CSS', 'OTHER'); + return 0, 'No fileId' unless $fileId; my $collateralType = $type eq 'JS' ? 'jsFiles' : $type eq 'CSS' ? 'cssFiles' : 'otherFiles'; @@ -520,8 +497,8 @@ The unique collateral GUID to move in the bundle. sub moveFileUp { my ($self, $type, $fileId) = @_; - return 0, 'No fileId' unless $fileId; return 0, 'Illegal type' unless WebGUI::Utility::isIn($type, 'JS', 'CSS', 'OTHER'); + return 0, 'No fileId' unless $fileId; my $collateralType = $type eq 'JS' ? 'jsFiles' : $type eq 'CSS' ? 'cssFiles' : 'otherFiles'; @@ -535,26 +512,6 @@ sub moveFileUp { } -#----------------------------------------------------------------- - -=head2 setAllCollateral ( tableName ) - -Update the db from the object cache. - -=head3 tableName - -The name of the table to insert the data. - -=cut - -sub setAllCollateral { - my $self = shift; - my $tableName = shift; - my $json = to_json($self->{_collateral}->{$tableName}); - $self->update({ $tableName => $json }); - return; -} - #----------------------------------------------------------------- =head2 setCollateral ( tableName, keyName, keyValue, properties ) @@ -594,7 +551,7 @@ sub setCollateral { my $properties = shift; ##Note, since this returns a reference, it is actually updating ##the object cache directly. - my $table = $self->getAllCollateral($tableName); + my $table = $self->get($tableName); if ($keyValue eq 'new' || $keyValue eq '') { if (! exists $properties->{$keyName} or $properties->{$keyName} eq 'new' @@ -602,13 +559,13 @@ sub setCollateral { $properties->{$keyName} = $self->session->id->generate; } push @{ $table }, $properties; - $self->setAllCollateral($tableName); + $self->update({$tableName => $table}); return $properties->{$keyName}; } my $index = $self->getCollateralDataIndex($table, $keyName, $keyValue); return if $index == -1; $table->[$index] = $properties; - $self->setAllCollateral($tableName); + $self->update({ $tableName => $table }); return $keyValue; } diff --git a/t/FilePump/Bundle.t b/t/FilePump/Bundle.t index ed363c91d..159abbfd6 100644 --- a/t/FilePump/Bundle.t +++ b/t/FilePump/Bundle.t @@ -29,7 +29,7 @@ my $session = WebGUI::Test->session; #---------------------------------------------------------------------------- # Tests -my $tests = 8; # Increment this number for each test you create +my $tests = 23; # Increment this number for each test you create plan tests => 1 + $tests; # 1 for the use_ok #---------------------------------------------------------------------------- @@ -69,16 +69,6 @@ cmp_deeply( '... checking error for no uri' ); -$bundle->setCollateral( - 'jsFiles', - 'fileId', - 'new', - { - uri => 'mysite', - lastUpdated => 0, - } -); - is( $bundle->addFile('JS', 'http://mysite.com/script.js'), 1, @@ -97,6 +87,124 @@ cmp_deeply( '... checking error message for duplicate URI' ); +$bundle->addFile('JS', 'http://mysite.com/helloworld.js'); +$bundle->addFile('JS', 'file:/data/domains/mysite.com/www/uploads/XX/YY/XXYYZZ/graviticEnergyDrive.js'); + +my @fileUris = map { $_->{uri} } @{ $bundle->get('jsFiles') }; +cmp_deeply( + [ @fileUris ], + [qw{ + http://mysite.com/script.js + http://mysite.com/helloworld.js + file:/data/domains/mysite.com/www/uploads/XX/YY/XXYYZZ/graviticEnergyDrive.js + }], + '... checking actual jsFiles data structure contents' +); + +################################################################### +# +# moveFile{Up,Down} +# +################################################################### + +cmp_deeply( + [ $bundle->moveFileUp() ], + [ 0, 'Illegal type' ], + 'moveFileUp: checking error for no type' +); + +cmp_deeply( + [ $bundle->moveFileUp('BEER') ], + [ 0, 'Illegal type' ], + '... checking error for bad type' +); + +cmp_deeply( + [ $bundle->moveFileUp('JS', ) ], + [ 0, 'No fileId' ], + '... checking error for no fileId' +); + +cmp_deeply( + [ $bundle->moveFileDown() ], + [ 0, 'Illegal type' ], + 'moveFileDown: checking error for no type' +); + +cmp_deeply( + [ $bundle->moveFileDown('BEER') ], + [ 0, 'Illegal type' ], + '... checking error for bad type' +); + +cmp_deeply( + [ $bundle->moveFileDown('JS', ) ], + [ 0, 'No fileId' ], + '... checking error for no fileId' +); + +my @fileIds = map { $_->{fileId} } @{ $bundle->get('jsFiles') }; + +ok($bundle->moveFileDown('JS', $fileIds[0]), 'moveFileDown returns 1 for a successful move'); +@fileUris = map { $_->{uri} } @{ $bundle->get('jsFiles') }; +cmp_deeply( + [ @fileUris ], + [qw{ + http://mysite.com/helloworld.js + http://mysite.com/script.js + file:/data/domains/mysite.com/www/uploads/XX/YY/XXYYZZ/graviticEnergyDrive.js + }], + '... checking the actual order of js files' +); + +ok($bundle->moveFileUp('JS', $fileIds[2]), 'moveFileUp returns 1 for a successful move'); +@fileUris = map { $_->{uri} } @{ $bundle->get('jsFiles') }; +cmp_deeply( + [ @fileUris ], + [qw{ + http://mysite.com/helloworld.js + file:/data/domains/mysite.com/www/uploads/XX/YY/XXYYZZ/graviticEnergyDrive.js + http://mysite.com/script.js + }], + '... checking the actual order of js files' +); + +################################################################### +# +# deleteFile +# +################################################################### + +cmp_deeply( + [ $bundle->deleteFile() ], + [ 0, 'Illegal type' ], + 'deleteFile: checking error for no type' +); + +cmp_deeply( + [ $bundle->deleteFile('BEER') ], + [ 0, 'Illegal type' ], + '... checking error for bad type' +); + +cmp_deeply( + [ $bundle->deleteFile('JS', ) ], + [ 0, 'No fileId' ], + '... checking error for no fileId' +); + +@fileIds = map { $_->{fileId} } @{ $bundle->get('jsFiles') }; +$bundle->deleteFile('JS', $fileIds[1]); +@fileUris = map { $_->{uri} } @{ $bundle->get('jsFiles') }; +cmp_deeply( + [ @fileUris ], + [qw{ + http://mysite.com/helloworld.js + http://mysite.com/script.js + }], + '... checking the actual deletion of js files' +); + $bundle->delete; }