diff --git a/t/AdSpace.t b/t/AdSpace.t index d92c109d2..378f0d2e8 100644 --- a/t/AdSpace.t +++ b/t/AdSpace.t @@ -35,200 +35,191 @@ $numTests += 2 * scalar keys %{ $newAdSpaceSettings }; plan tests => $numTests; my $session = WebGUI::Test->session; -my ($adSpace, $alfred, $alfred2, $bruce, $catWoman, $villianClone, $defaultAdSpace ); +my ($adSpace, $alfred, $alfred2, $bruce, $catWoman, ); my ($jokerAd, $penguinAd, $twoFaceAd); - local $ENV{REMOTE_ADDR} = '10.0.0.1'; - local $ENV{HTTP_USER_AGENT} = 'Mozilla/5.0'; +local $ENV{REMOTE_ADDR} = '10.0.0.1'; +local $ENV{HTTP_USER_AGENT} = 'Mozilla/5.0'; - $adSpace = WebGUI::AdSpace->create($session, {name=>"Alfred"}); +$adSpace = WebGUI::AdSpace->create($session, {name=>"Alfred"}); - isa_ok($adSpace, 'WebGUI::AdSpace'); +isa_ok($adSpace, 'WebGUI::AdSpace'); +WebGUI::Test->addToCleanup($adSpace); - my $data = $session->db->quickHashRef("select adSpaceId, name from adSpace where adSpaceId=?",[$adSpace->getId]); +my $data = $session->db->quickHashRef("select adSpaceId, name from adSpace where adSpaceId=?",[$adSpace->getId]); - ok(exists $data->{adSpaceId}, "create()"); - is($data->{name}, $adSpace->get("name"), "get()"); - is($data->{adSpaceId}, $adSpace->getId, "getId()"); +ok(exists $data->{adSpaceId}, "create()"); +is($data->{name}, $adSpace->get("name"), "get()"); +is($data->{adSpaceId}, $adSpace->getId, "getId()"); - $alfred = WebGUI::AdSpace->newByName($session, 'Alfred'); +$alfred = WebGUI::AdSpace->newByName($session, 'Alfred'); +WebGUI::Test->addToCleanup($alfred); - cmp_deeply($adSpace, $alfred, 'newByName returns identical object if name exists'); +cmp_deeply($adSpace, $alfred, 'newByName returns identical object if name exists'); - $bruce = WebGUI::AdSpace->newByName($session, 'Bruce'); - is($bruce, undef, 'newByName returns undef if the name does not exist'); +$bruce = WebGUI::AdSpace->newByName($session, 'Bruce'); +is($bruce, undef, 'newByName returns undef if the name does not exist'); - $bruce = WebGUI::AdSpace->new($session, $session->getId); - is($bruce, undef, 'new returns undef if the id does not exist'); +$bruce = WebGUI::AdSpace->new($session, $session->getId); +is($bruce, undef, 'new returns undef if the id does not exist'); - $alfred2 = WebGUI::AdSpace->create($session); - is($alfred2, undef, 'create returns undef unless you pass it a name'); +$alfred2 = WebGUI::AdSpace->create($session); +is($alfred2, undef, 'create returns undef unless you pass it a name'); - $alfred2 = WebGUI::AdSpace->create($session, {name => 'Alfred'}); - is($alfred2, undef, 'create returns undef if the name already exists'); +$alfred2 = WebGUI::AdSpace->create($session, {name => 'Alfred'}); +is($alfred2, undef, 'create returns undef if the name already exists'); - isa_ok($alfred->session, 'WebGUI::Session'); +isa_ok($alfred->session, 'WebGUI::Session'); - undef $alfred2; +undef $alfred2; - $alfred->set({title => "Alfred's Ad"}); - is($alfred->get('title'), "Alfred's Ad", "get, set work on title"); +$alfred->set({title => "Alfred's Ad"}); +is($alfred->get('title'), "Alfred's Ad", "get, set work on title"); - $bruce = WebGUI::AdSpace->create($session, {name => 'Bruce'}); - $bruce->set({title => "Bruce's Ad"}); +$bruce = WebGUI::AdSpace->create($session, {name => 'Bruce'}); +$bruce->set({title => "Bruce's Ad"}); +WebGUI::Test->addToCleanup($bruce); - $catWoman = WebGUI::AdSpace->create($session, {name => 'CatWoman'}); - $catWoman->set({title => "CatWoman's Ad"}); +$catWoman = WebGUI::AdSpace->create($session, {name => 'CatWoman'}); +$catWoman->set({title => "CatWoman's Ad"}); +WebGUI::Test->addToCleanup($catWoman); - my $adSpaces = WebGUI::AdSpace->getAdSpaces($session); +my $adSpaces = WebGUI::AdSpace->getAdSpaces($session); - cmp_deeply($adSpaces, [$alfred, $bruce, $catWoman], 'getAdSpaces returns all AdSpaces in alphabetical order by title'); +cmp_deeply($adSpaces, [$alfred, $bruce, $catWoman], 'getAdSpaces returns all AdSpaces in alphabetical order by title'); - $catWoman->set($newAdSpaceSettings); - - foreach my $setting (keys %{ $newAdSpaceSettings } ) { - is($newAdSpaceSettings->{$setting}, $catWoman->get($setting), - sprintf "set and get for %s", $setting); - } - - ##Bare call to set doesn't change anything - $catWoman->set(); - - foreach my $setting (keys %{ $newAdSpaceSettings } ) { - is($newAdSpaceSettings->{$setting}, $catWoman->get($setting), - sprintf "empty call to set does not change %s", $setting); - } - - $catWoman->set({title => '', name => '', description => '', }); - is ($catWoman->get('title'), '', 'set can clear the title'); - is ($catWoman->get('description'), '', 'set can clear the title'); - is ($catWoman->get('name' ), $newAdSpaceSettings->{'name'}, 'set can not clear the name'); - - ##Create a set of ads for general purpose testing - - ##The Joker and Penguin Ads go in the bruce adSpace - ##The Two Face ad goes in the catWoman adSpace - - $jokerAd = WebGUI::AdSpace::Ad->create($session, $bruce->getId, - { - title => 'Joker', - url => '/ha_ha', - type => 'rich', - richMedia => 'Joker', - priority => 2, - isActive => 1, - clicksBought => 0, - impressionsBought => 2, - } - ); - $penguinAd = WebGUI::AdSpace::Ad->create($session, $bruce->getId, - { - title => 'Penguin', - url => '/fishy', - type => 'rich', - richMedia => 'Penguin', - priority => 1, - isActive => 1, - clicksBought => 4, - impressionsBought => 0, - } - ); - $twoFaceAd = WebGUI::AdSpace::Ad->create($session, $catWoman->getId, - { - title => 'Two Face', - url => '/dent', - type => 'rich', - richMedia => 'Two Face', - priority => 500, - isActive => 1, - clicksBought => 0, - impressionsBought => 0, - } - ); - - ##getAds - my @bruceAdTitles = map { $_->get('title') } @{ $bruce->getAds }; - my @catWomanAdTitles = map { $_->get('title') } @{ $catWoman->getAds }; - - cmp_bag(\@bruceAdTitles, ['Joker', 'Penguin'], 'Got the set of Ads for bruce'); - cmp_bag(\@catWomanAdTitles, ['Two Face'], 'Got the set of Ads for catWoman'); - - ##countClicks - my $penguinUrl = WebGUI::AdSpace->countClick($session, $penguinAd->getId); - is($penguinUrl, $penguinAd->get('url'), 'clicking on the penguin ad returns the penguin url'); - WebGUI::AdSpace->countClick($session, $penguinAd->getId); - WebGUI::AdSpace->countClick($session, $penguinAd->getId); - - my $jokerUrl = WebGUI::AdSpace->countClick($session, $jokerAd->getId); - is($jokerUrl, $jokerAd->get('url'), 'clicking on the joker ad returns the joker url'); - - my $twoFaceUrl = WebGUI::AdSpace->countClick($session, $twoFaceAd->getId); - is($twoFaceUrl, $twoFaceAd->get('url'), 'clicking on the twoFace ad returns the twoFace url'); - - my ($penguinClicks) = $session->db->quickArray('select clicks from advertisement where adId=?',[$penguinAd->getId]); - is($penguinClicks, 3, 'counted penguin clicks correctly'); - - my ($jokerClicks) = $session->db->quickArray('select clicks from advertisement where adId=?',[$jokerAd->getId]); - is($jokerClicks, 1, 'counted joker clicks correctly'); - - my ($twoFaceClicks) = $session->db->quickArray('select clicks from advertisement where adId=?',[$twoFaceAd->getId]); - is($twoFaceClicks, 1, 'counted twoFace clicks correctly'); - - ##displayImpression - my ($twoFaceImpressions, $twoFacePriority) = - $session->db->quickArray('select impressions,nextInPriority from advertisement where adId=?',[$twoFaceAd->getId]); - is($catWoman->displayImpression(1), $twoFaceAd->get('renderedAd'), 'displayImpression returns the ad'); - cmp_bag( - [$twoFaceImpressions, $twoFacePriority], - [$session->db->quickArray('select impressions,nextInPriority from advertisement where adId=?',[$twoFaceAd->getId])], - 'displayImpressions: impresssions and nextInPriority are not updated when dontCount=1', - ); - - $catWoman->displayImpression(); - my $twoFaceTime = time(); - is( - $session->db->quickArray('select impressions from advertisement where adId=?',[$twoFaceAd->getId]), - 1, 'displayImpression added 1 impression' - ); - my ($newTwoFacePriority) = $session->db->quickArray('select nextInPriority from advertisement where adId=?',[$twoFaceAd->getId]); - isnt($newTwoFacePriority, $twoFacePriority, 'displayImpression changed the nextInPriority'); - cmp_ok( - abs($twoFaceTime + $twoFaceAd->get('priority') - $newTwoFacePriority), - '<=', - '2', - 'displayImpression set the nextInPriority correctly' - ); - - my ($twoFaceIsActive) = $session->db->quickArray('select isActive from advertisement where adId=?',[$twoFaceAd->getId]); - is($twoFaceIsActive, 0, 'displayImpression deactivates an ad if enough impressions and clicks are bought'); - - $session->db->write('update advertisement set nextInPriority=UNIX_TIMESTAMP()+100000 where adId=?',[$jokerAd->getId]); - is($bruce->displayImpression(), $penguinAd->get('renderedAd'), 'displayImpression returns earliest by nextInPriority, penguin has 3 clicks'); - WebGUI::AdSpace->countClick($session, $penguinAd->getId); ##4 clicks - is($bruce->displayImpression(), $penguinAd->get('renderedAd'), 'displayImpression returns still returns penguinAd, but deactivates it after 4 clicks'); - - my ($penguinActive) = $session->db->quickArray('select isActive from advertisement where adId=?',[$penguinAd->getId]); - is($penguinActive, 0, 'displayImpression deactiveated penguinAd'); - is($bruce->displayImpression(), $jokerAd->get('renderedAd'), 'displayImpression now returns jokerAd'); - - my ($jokerActive) = $session->db->quickArray('select isActive from advertisement where adId=?',[$jokerAd->getId]); - is($jokerActive, 1, 'displayImpression did not deactiveate jokerAd after one impression'); - - $bruce->displayImpression(); - ($jokerActive) = $session->db->quickArray('select isActive from advertisement where adId=?',[$jokerAd->getId]); - is($jokerActive, 0, 'displayImpression deactivated jokerAd after two impressions'); - -END { - foreach my $ad_space ($adSpace, $bruce, $alfred, $alfred2, $catWoman, $defaultAdSpace ) { - if (defined $ad_space and ref $ad_space eq 'WebGUI::AdSpace') { - $ad_space->delete; - } - } - - foreach my $advert ($jokerAd, $penguinAd, $villianClone, $twoFaceAd) { - if (defined $advert and ref $advert eq 'WebGUI::AdSpace::Ad') { - $advert->delete; - } - } +$catWoman->set($newAdSpaceSettings); +foreach my $setting (keys %{ $newAdSpaceSettings } ) { + is($newAdSpaceSettings->{$setting}, $catWoman->get($setting), + sprintf "set and get for %s", $setting); } + +##Bare call to set doesn't change anything +$catWoman->set(); + +foreach my $setting (keys %{ $newAdSpaceSettings } ) { + is($newAdSpaceSettings->{$setting}, $catWoman->get($setting), + sprintf "empty call to set does not change %s", $setting); +} + +$catWoman->set({title => '', name => '', description => '', }); +is ($catWoman->get('title'), '', 'set can clear the title'); +is ($catWoman->get('description'), '', 'set can clear the title'); +is ($catWoman->get('name' ), $newAdSpaceSettings->{'name'}, 'set can not clear the name'); + +##Create a set of ads for general purpose testing + +##The Joker and Penguin Ads go in the bruce adSpace +##The Two Face ad goes in the catWoman adSpace + +$jokerAd = WebGUI::AdSpace::Ad->create($session, $bruce->getId, + { + title => 'Joker', + url => '/ha_ha', + type => 'rich', + richMedia => 'Joker', + priority => 2, + isActive => 1, + clicksBought => 0, + impressionsBought => 2, + } +); +$penguinAd = WebGUI::AdSpace::Ad->create($session, $bruce->getId, + { + title => 'Penguin', + url => '/fishy', + type => 'rich', + richMedia => 'Penguin', + priority => 1, + isActive => 1, + clicksBought => 4, + impressionsBought => 0, + } +); +$twoFaceAd = WebGUI::AdSpace::Ad->create($session, $catWoman->getId, + { + title => 'Two Face', + url => '/dent', + type => 'rich', + richMedia => 'Two Face', + priority => 500, + isActive => 1, + clicksBought => 0, + impressionsBought => 0, + } +); + +##getAds +my @bruceAdTitles = map { $_->get('title') } @{ $bruce->getAds }; +my @catWomanAdTitles = map { $_->get('title') } @{ $catWoman->getAds }; + +cmp_bag(\@bruceAdTitles, ['Joker', 'Penguin'], 'Got the set of Ads for bruce'); +cmp_bag(\@catWomanAdTitles, ['Two Face'], 'Got the set of Ads for catWoman'); + +##countClicks +my $penguinUrl = WebGUI::AdSpace->countClick($session, $penguinAd->getId); +is($penguinUrl, $penguinAd->get('url'), 'clicking on the penguin ad returns the penguin url'); +WebGUI::AdSpace->countClick($session, $penguinAd->getId); +WebGUI::AdSpace->countClick($session, $penguinAd->getId); + +my $jokerUrl = WebGUI::AdSpace->countClick($session, $jokerAd->getId); +is($jokerUrl, $jokerAd->get('url'), 'clicking on the joker ad returns the joker url'); + +my $twoFaceUrl = WebGUI::AdSpace->countClick($session, $twoFaceAd->getId); +is($twoFaceUrl, $twoFaceAd->get('url'), 'clicking on the twoFace ad returns the twoFace url'); + +my ($penguinClicks) = $session->db->quickArray('select clicks from advertisement where adId=?',[$penguinAd->getId]); +is($penguinClicks, 3, 'counted penguin clicks correctly'); + +my ($jokerClicks) = $session->db->quickArray('select clicks from advertisement where adId=?',[$jokerAd->getId]); +is($jokerClicks, 1, 'counted joker clicks correctly'); + +my ($twoFaceClicks) = $session->db->quickArray('select clicks from advertisement where adId=?',[$twoFaceAd->getId]); +is($twoFaceClicks, 1, 'counted twoFace clicks correctly'); + +##displayImpression +my ($twoFaceImpressions, $twoFacePriority) = + $session->db->quickArray('select impressions,nextInPriority from advertisement where adId=?',[$twoFaceAd->getId]); +is($catWoman->displayImpression(1), $twoFaceAd->get('renderedAd'), 'displayImpression returns the ad'); +cmp_bag( + [$twoFaceImpressions, $twoFacePriority], + [$session->db->quickArray('select impressions,nextInPriority from advertisement where adId=?',[$twoFaceAd->getId])], + 'displayImpressions: impresssions and nextInPriority are not updated when dontCount=1', +); + +$catWoman->displayImpression(); +my $twoFaceTime = time(); +is( + $session->db->quickArray('select impressions from advertisement where adId=?',[$twoFaceAd->getId]), + 1, 'displayImpression added 1 impression' +); +my ($newTwoFacePriority) = $session->db->quickArray('select nextInPriority from advertisement where adId=?',[$twoFaceAd->getId]); +isnt($newTwoFacePriority, $twoFacePriority, 'displayImpression changed the nextInPriority'); +cmp_ok( + abs($twoFaceTime + $twoFaceAd->get('priority') - $newTwoFacePriority), + '<=', + '2', + 'displayImpression set the nextInPriority correctly' +); + +my ($twoFaceIsActive) = $session->db->quickArray('select isActive from advertisement where adId=?',[$twoFaceAd->getId]); +is($twoFaceIsActive, 0, 'displayImpression deactivates an ad if enough impressions and clicks are bought'); + +$session->db->write('update advertisement set nextInPriority=UNIX_TIMESTAMP()+100000 where adId=?',[$jokerAd->getId]); +is($bruce->displayImpression(), $penguinAd->get('renderedAd'), 'displayImpression returns earliest by nextInPriority, penguin has 3 clicks'); +WebGUI::AdSpace->countClick($session, $penguinAd->getId); ##4 clicks +is($bruce->displayImpression(), $penguinAd->get('renderedAd'), 'displayImpression returns still returns penguinAd, but deactivates it after 4 clicks'); + +my ($penguinActive) = $session->db->quickArray('select isActive from advertisement where adId=?',[$penguinAd->getId]); +is($penguinActive, 0, 'displayImpression deactiveated penguinAd'); +is($bruce->displayImpression(), $jokerAd->get('renderedAd'), 'displayImpression now returns jokerAd'); + +my ($jokerActive) = $session->db->quickArray('select isActive from advertisement where adId=?',[$jokerAd->getId]); +is($jokerActive, 1, 'displayImpression did not deactiveate jokerAd after one impression'); + +$bruce->displayImpression(); +($jokerActive) = $session->db->quickArray('select isActive from advertisement where adId=?',[$jokerAd->getId]); +is($jokerActive, 0, 'displayImpression deactivated jokerAd after two impressions'); + +#vim:ft=perl diff --git a/t/Crud/serialize.t b/t/Crud/serialize.t index b94b8535b..cd758f2fe 100644 --- a/t/Crud/serialize.t +++ b/t/Crud/serialize.t @@ -34,6 +34,9 @@ plan tests => 10; # Increment this number for each test you create use_ok('WebGUI::Serialize'); WebGUI::Serialize->crud_createTable($session); +WebGUI::Test->addToCleanup(sub { + WebGUI::Serialize->crud_dropTable($session); +}) my $cereal = WebGUI::Serialize->create($session); isa_ok($cereal, 'WebGUI::Serialize'); @@ -112,11 +115,4 @@ cmp_deeply( 'get: returns safe references' ); -#---------------------------------------------------------------------------- -# Cleanup -END { - -WebGUI::Serialize->crud_dropTable($session); - -} #vim:ft=perl diff --git a/t/Help/isa.t b/t/Help/isa.t index 38eb0baac..276cf62d0 100644 --- a/t/Help/isa.t +++ b/t/Help/isa.t @@ -31,6 +31,9 @@ my $session = WebGUI::Test->session; plan tests => 4; installCollateral(); +WebGUI::Test->addToCleanup(sub { + unlink File::Spec->catfile(WebGUI::Test->lib, qw/WebGUI Help HelpTest.pm/); +}); my $allHelp = WebGUI::Operation::Help::_load($session, 'HelpTest'); @@ -173,7 +176,4 @@ sub installCollateral { ); } -END { - unlink File::Spec->catfile(WebGUI::Test->lib, qw/WebGUI Help HelpTest.pm/); -} - +#vim:ft=perl diff --git a/t/International.t b/t/International.t index b608efe59..304be4d19 100644 --- a/t/International.t +++ b/t/International.t @@ -50,6 +50,11 @@ is($i18n->get('topicName'), 'Assets', 'get: get English label for topicName in A is($i18n->get('topicName', 'WebGUI'), 'WebGUI', 'get: test manual namespace override'); installPigLatin(); +WebGUI::Test->addToCleanup(sub { + unlink File::Spec->catfile(WebGUI::Test->lib, qw/WebGUI i18n PigLatin WebGUI.pm/); + unlink File::Spec->catfile(WebGUI::Test->lib, qw/WebGUI i18n PigLatin.pm/); + rmdir File::Spec->catdir(WebGUI::Test->lib, qw/WebGUI i18n PigLatin/); +}); #tests for sub new my $i18nNew1 = WebGUI::International->new($session); @@ -131,8 +136,5 @@ my $newi18n = WebGUI::International->new($session); $newi18n->get('104', 'WebGUI', 'English'), 'Language check after SetLanguage contentHandler : key from missing file return English key' ); -END { - unlink File::Spec->catfile(WebGUI::Test->lib, qw/WebGUI i18n PigLatin WebGUI.pm/); - unlink File::Spec->catfile(WebGUI::Test->lib, qw/WebGUI i18n PigLatin.pm/); - rmdir File::Spec->catdir(WebGUI::Test->lib, qw/WebGUI i18n PigLatin/); -} + +#vim:ft=perl