From 855d90c81ccf44ba1b6a01ffe7b1dc12f5807eb7 Mon Sep 17 00:00:00 2001 From: gmorel Date: Thu, 12 Sep 2013 17:55:03 +0200 Subject: [PATCH] Working : Coupon Controller refactor --- .../Controller/Admin/CouponController.php | 7 +- tests/functionnal/casperjs/exe/30_coupons.js | 178 ---------------- .../casperjs/exe/31_coupons_rule.js | 195 ++++++++++++++++++ 3 files changed, 199 insertions(+), 181 deletions(-) create mode 100644 tests/functionnal/casperjs/exe/31_coupons_rule.js diff --git a/core/lib/Thelia/Controller/Admin/CouponController.php b/core/lib/Thelia/Controller/Admin/CouponController.php index 0611f8873..8a4d283f9 100755 --- a/core/lib/Thelia/Controller/Admin/CouponController.php +++ b/core/lib/Thelia/Controller/Admin/CouponController.php @@ -137,7 +137,7 @@ class CouponController extends BaseAdminController } /** @var Coupon $coupon */ - $coupon = CouponQuery::create()->findOneById($couponId); + $coupon = CouponQuery::create()->findPk($couponId); if (!$coupon) { $this->pageNotFound(); } @@ -150,6 +150,7 @@ class CouponController extends BaseAdminController $lang = $this->getSession()->getLang(); $eventToDispatch = TheliaEvents::COUPON_UPDATE; + // Create if ($this->getRequest()->isMethod('POST')) { $this->validateCreateOrUpdateForm( $i18n, @@ -158,9 +159,9 @@ class CouponController extends BaseAdminController 'updated', 'update' ); - } else { - // Prepare the data that will hydrate the form + } else { // Update + // Prepare the data that will hydrate the form /** @var ConstraintFactory $constraintFactory */ $constraintFactory = $this->container->get('thelia.constraint.factory'); $rules = $constraintFactory->unserializeCouponRuleCollection( diff --git a/tests/functionnal/casperjs/exe/30_coupons.js b/tests/functionnal/casperjs/exe/30_coupons.js index b7774fd15..7cbd70578 100644 --- a/tests/functionnal/casperjs/exe/30_coupons.js +++ b/tests/functionnal/casperjs/exe/30_coupons.js @@ -17,184 +17,6 @@ casper.test.comment('Testing coupons'); ////CREATE // @todo implement -//UPDATE COUPON RULE -casper.start(thelia2_login_coupon_update_url, function() { - this.test.comment('Now on : ' + this.getCurrentUrl()); - this.capture('tests/functionnal/casperjs/screenshot/coupons/init.png'); - this.test.comment('COUPON RULE - EDIT'); - this.test.assertTitle('Update coupon - Thelia Back Office', 'Web page title OK'); - this.test.assertSelectorHasText('tbody#constraint-list tr:nth-child(1)', 'If cart total amount is superior to 40 EUR','1) 1st default rule found'); - this.test.assertSelectorHasText('tbody#constraint-list tr:nth-child(2)', 'If cart total amount is inferior to 400 EUR','1) 2nd default rule found'); - - // Create rule - this.evaluate(function() { - $('#category-rule').val('thelia.constraint.rule.available_for_x_articles').change(); - return true; - }); - this.capture('tests/functionnal/casperjs/screenshot/coupons/rule-selected.png'); -}); - -casper.wait(1000, function() { - this.echo("\nWaiting...."); -}); - -// Test Rule updating -casper.then(function(){ - this.evaluate(function() { - $('#quantity-operator').val('>=').change(); - return true; - }); - this.sendKeys('input#quantity-value', '4'); - this.click('#constraint-save-btn'); -}); - -casper.wait(1000, function() { - this.echo("\nWaiting...."); -}); - -casper.then(function(){ - this.capture('tests/functionnal/casperjs/screenshot/coupons/rule-added.png'); - this.test.assertSelectorHasText('tbody#constraint-list tr:nth-child(1)', 'If cart total amount is superior to 40 EUR','2) 1st default rule found'); - this.test.assertSelectorHasText('tbody#constraint-list tr:nth-child(2)', 'If cart total amount is inferior to 400 EUR','2) 2nd default rule found'); - this.test.assertSelectorHasText('tbody#constraint-list tr:nth-child(3)', ' If cart products quantity is superior or equal to 4','2) 3rd rule found'); - - // Click on Edit button - this.click('tbody#constraint-list tr:nth-child(3) .constraint-update-btn'); -}); - -casper.wait(2000, function() { - this.echo("\nWaiting...."); -}); - -casper.then(function(){ - this.evaluate(function() { - $('#quantity-operator').val('==').change(); - return true; - }); - - // Removing old value -// casper.evaluate(function triggerKeyDownEvent() { -// var e = $.Event("keydown"); -// e.which = 8; -// e.keyCode = 8; -// $("#quantity-value").trigger(e); -// }); - this.evaluate(function() { - $("#quantity-value").val('').change(); - return true; - }); - - // Adding new value - this.sendKeys('#quantity-value', '5'); - this.capture('tests/functionnal/casperjs/screenshot/coupons/rule-being-edited.png'); - this.click('#constraint-save-btn'); -}); - -casper.wait(2000, function() { - this.echo("\nWaiting...."); -}); -// Check if updated rule has been saved and list refreshed -casper.then(function(){ - this.capture('tests/functionnal/casperjs/screenshot/coupons/rule-edited.png'); - this.test.assertSelectorHasText('tbody#constraint-list tr:nth-child(1)', 'If cart total amount is superior to 40 EUR','3) 1st default rule found'); - this.test.assertSelectorHasText('tbody#constraint-list tr:nth-child(2)', 'If cart total amount is inferior to 400 EUR','3) 2nd default rule found'); - this.test.assertSelectorHasText('tbody#constraint-list tr:nth-child(3)', 'If cart products quantity is equal to 5','3) 3rd rule updated found'); -}); - -// Check if updated rule has been well saved -casper.thenOpen(thelia2_login_coupon_update_url, function() { - this.test.comment('Now on : ' + this.getCurrentUrl()); - this.capture('tests/functionnal/casperjs/screenshot/coupons/rule-edited-refreshed.png'); - this.test.assertSelectorHasText('tbody#constraint-list tr:nth-child(1)', 'If cart total amount is superior to 40 EUR','4) 1st default rule found'); - this.test.assertSelectorHasText('tbody#constraint-list tr:nth-child(2)', 'If cart total amount is inferior to 400 EUR','4) 2nd default rule found'); - this.test.assertSelectorHasText('tbody#constraint-list tr:nth-child(3)', 'If cart products quantity is equal to 5','4) 3rd rule updated found'); - - // Click on Delete button - this.click('tbody#constraint-list tr:nth-child(2) .constraint-delete-btn'); -}); - -casper.wait(2000, function() { - this.echo("\nWaiting...."); -}); - -casper.then(function(){ - this.test.assertSelectorHasText('tbody#constraint-list tr:nth-child(1)', 'If cart total amount is superior to 40 EUR','5) 1st default rule found'); - this.test.assertSelectorDoesntHaveText('tbody#constraint-list tr:nth-child(2)', 'If cart total amount is inferior to 400 EUR','5) 2nd default rule found'); - this.test.assertSelectorHasText('tbody#constraint-list tr:nth-child(2)', 'If cart products quantity is equal to 5','5) 3rd rule updated found'); -}); - -// Check if updated rule has been well saved -casper.thenOpen(thelia2_login_coupon_update_url, function() { - this.test.comment('Now on : ' + this.getCurrentUrl()); - this.capture('tests/functionnal/casperjs/screenshot/coupons/rule-deleted-refreshed.png'); - this.test.assertSelectorHasText('tbody#constraint-list tr:nth-child(1)', 'If cart total amount is superior to 40 EUR','6) 1st default rule found'); - this.test.assertSelectorDoesntHaveText('tbody#constraint-list tr:nth-child(2)', 'If cart total amount is inferior to 400 EUR','6) 2nd default rule found'); - this.test.assertSelectorHasText('tbody#constraint-list tr:nth-child(2)', 'If cart products quantity is equal to 5','6) 3rd rule updated found'); -}); - -// Test creating rule that won't be edited -casper.then(function(){ -// Create rule - this.evaluate(function() { - $('#category-rule').val('thelia.constraint.rule.available_for_total_amount').change(); - return true; - }); - this.capture('tests/functionnal/casperjs/screenshot/coupons/rule-selected2.png'); -}); - -casper.wait(2000, function() { - this.echo("\nWaiting...."); -}); - -// Test Rule creation -casper.then(function(){ - this.evaluate(function() { - $('#price-operator').val('<=').change(); - return true; - }); - // Removing old value -// casper.evaluate(function triggerKeyDownEvent() { -// var e = $.Event("keydown"); -// e.which = 8; -// e.keyCode = 8; -// $("input#price-value").trigger(e); -// }); - this.evaluate(function() { - $("input#price-value").val('').change(); - return true; - }); - - // Changing 400 to 401 - this.sendKeys('input#price-value', '401'); - this.evaluate(function() { - $('#currency-value').val('GBP').change(); - return true; - }); - this.capture('tests/functionnal/casperjs/screenshot/coupons/rule-saved-edited-before-click-save.png'); - this.click('#constraint-save-btn'); -}); - -casper.wait(2000, function() { - this.echo("\nWaiting...."); -}); - -casper.then(function(){ - this.test.assertSelectorHasText('tbody#constraint-list tr:nth-child(1)', 'If cart total amount is superior to 40 EUR','7) 1st default rule found'); - this.test.assertSelectorDoesntHaveText('tbody#constraint-list tr:nth-child(2)', 'If cart total amount is inferior to 400 EUR','7) 2nd default rule found'); - this.test.assertSelectorHasText('tbody#constraint-list tr:nth-child(2)', 'If cart products quantity is equal to 5','7) 3rd rule updated found'); - this.test.assertSelectorHasText('tbody#constraint-list tr:nth-child(3)', 'If cart total amount is inferior or equal to 401 GBP','7) 4rd rule created found'); -}); - -// Check if created rule has been well saved -casper.thenOpen(thelia2_login_coupon_update_url, function() { - this.test.comment('Now on : ' + this.getCurrentUrl()); - this.capture('tests/functionnal/casperjs/screenshot/coupons/rule-added-refreshed.png'); - this.test.assertSelectorHasText('tbody#constraint-list tr:nth-child(1)', 'If cart total amount is superior to 40 EUR','8) 1st default rule found'); - this.test.assertSelectorDoesntHaveText('tbody#constraint-list tr:nth-child(2)', 'If cart total amount is inferior to 400 EUR','8) 2nd default rule found'); - this.test.assertSelectorHasText('tbody#constraint-list tr:nth-child(2)', 'If cart products quantity is equal to 5','8) 3rd rule updated found'); - this.test.assertSelectorHasText('tbody#constraint-list tr:nth-child(3)', 'If cart total amount is inferior or equal to 401 GBP','8) 4rd rule created found'); -}); - ////EDIT CHECK // @todo implement diff --git a/tests/functionnal/casperjs/exe/31_coupons_rule.js b/tests/functionnal/casperjs/exe/31_coupons_rule.js new file mode 100644 index 000000000..a9b7c7177 --- /dev/null +++ b/tests/functionnal/casperjs/exe/31_coupons_rule.js @@ -0,0 +1,195 @@ +// +//var casper = require('casper').create({ +// viewportSize:{ +// width:1024, height:768 +// }, +// pageSettings:{ +// userAgent:'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_2) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.79 Safari/535.11' +// }, +// verbose:true +//}); + +casper.test.comment('Testing coupons rules'); + +//UPDATE COUPON RULE +casper.start(thelia2_login_coupon_update_url, function() { + this.test.comment('Now on : ' + this.getCurrentUrl()); + this.capture('tests/functionnal/casperjs/screenshot/coupons/init.png'); + this.test.comment('COUPON RULE - EDIT'); + this.test.assertTitle('Update coupon - Thelia Back Office', 'Web page title OK'); + this.test.assertSelectorHasText('tbody#constraint-list tr:nth-child(1)', 'If cart total amount is superior to 40 EUR','1) 1st default rule found'); + this.test.assertSelectorHasText('tbody#constraint-list tr:nth-child(2)', 'If cart total amount is inferior to 400 EUR','1) 2nd default rule found'); + + // Create rule + this.evaluate(function() { + $('#category-rule').val('thelia.constraint.rule.available_for_x_articles').change(); + return true; + }); + this.capture('tests/functionnal/casperjs/screenshot/coupons/rule-selected.png'); +}); + +casper.wait(1000, function() { + this.echo("\nWaiting...."); +}); + +// Test Rule updating +casper.then(function(){ + this.evaluate(function() { + $('#quantity-operator').val('>=').change(); + return true; + }); + this.sendKeys('input#quantity-value', '4'); + this.click('#constraint-save-btn'); +}); + +casper.wait(1000, function() { + this.echo("\nWaiting...."); +}); + +casper.then(function(){ + this.capture('tests/functionnal/casperjs/screenshot/coupons/rule-added.png'); + this.test.assertSelectorHasText('tbody#constraint-list tr:nth-child(1)', 'If cart total amount is superior to 40 EUR','2) 1st default rule found'); + this.test.assertSelectorHasText('tbody#constraint-list tr:nth-child(2)', 'If cart total amount is inferior to 400 EUR','2) 2nd default rule found'); + this.test.assertSelectorHasText('tbody#constraint-list tr:nth-child(3)', ' If cart products quantity is superior or equal to 4','2) 3rd rule found'); + + // Click on Edit button + this.click('tbody#constraint-list tr:nth-child(3) .constraint-update-btn'); +}); + +casper.wait(2000, function() { + this.echo("\nWaiting...."); +}); + +casper.then(function(){ + this.evaluate(function() { + $('#quantity-operator').val('==').change(); + return true; + }); + + // Removing old value +// casper.evaluate(function triggerKeyDownEvent() { +// var e = $.Event("keydown"); +// e.which = 8; +// e.keyCode = 8; +// $("#quantity-value").trigger(e); +// }); + this.evaluate(function() { + $("#quantity-value").val('').change(); + return true; + }); + + // Adding new value + this.sendKeys('#quantity-value', '5'); + this.capture('tests/functionnal/casperjs/screenshot/coupons/rule-being-edited.png'); + this.click('#constraint-save-btn'); +}); + +casper.wait(2000, function() { + this.echo("\nWaiting...."); +}); +// Check if updated rule has been saved and list refreshed +casper.then(function(){ + this.capture('tests/functionnal/casperjs/screenshot/coupons/rule-edited.png'); + this.test.assertSelectorHasText('tbody#constraint-list tr:nth-child(1)', 'If cart total amount is superior to 40 EUR','3) 1st default rule found'); + this.test.assertSelectorHasText('tbody#constraint-list tr:nth-child(2)', 'If cart total amount is inferior to 400 EUR','3) 2nd default rule found'); + this.test.assertSelectorHasText('tbody#constraint-list tr:nth-child(3)', 'If cart products quantity is equal to 5','3) 3rd rule updated found'); +}); + +// Check if updated rule has been well saved +casper.thenOpen(thelia2_login_coupon_update_url, function() { + this.test.comment('Now on : ' + this.getCurrentUrl()); + this.capture('tests/functionnal/casperjs/screenshot/coupons/rule-edited-refreshed.png'); + this.test.assertSelectorHasText('tbody#constraint-list tr:nth-child(1)', 'If cart total amount is superior to 40 EUR','4) 1st default rule found'); + this.test.assertSelectorHasText('tbody#constraint-list tr:nth-child(2)', 'If cart total amount is inferior to 400 EUR','4) 2nd default rule found'); + this.test.assertSelectorHasText('tbody#constraint-list tr:nth-child(3)', 'If cart products quantity is equal to 5','4) 3rd rule updated found'); + + // Click on Delete button + this.click('tbody#constraint-list tr:nth-child(2) .constraint-delete-btn'); +}); + +casper.wait(2000, function() { + this.echo("\nWaiting...."); +}); + +casper.then(function(){ + this.test.assertSelectorHasText('tbody#constraint-list tr:nth-child(1)', 'If cart total amount is superior to 40 EUR','5) 1st default rule found'); + this.test.assertSelectorDoesntHaveText('tbody#constraint-list tr:nth-child(2)', 'If cart total amount is inferior to 400 EUR','5) 2nd default rule found'); + this.test.assertSelectorHasText('tbody#constraint-list tr:nth-child(2)', 'If cart products quantity is equal to 5','5) 3rd rule updated found'); +}); + +// Check if updated rule has been well saved +casper.thenOpen(thelia2_login_coupon_update_url, function() { + this.test.comment('Now on : ' + this.getCurrentUrl()); + this.capture('tests/functionnal/casperjs/screenshot/coupons/rule-deleted-refreshed.png'); + this.test.assertSelectorHasText('tbody#constraint-list tr:nth-child(1)', 'If cart total amount is superior to 40 EUR','6) 1st default rule found'); + this.test.assertSelectorDoesntHaveText('tbody#constraint-list tr:nth-child(2)', 'If cart total amount is inferior to 400 EUR','6) 2nd default rule found'); + this.test.assertSelectorHasText('tbody#constraint-list tr:nth-child(2)', 'If cart products quantity is equal to 5','6) 3rd rule updated found'); +}); + +// Test creating rule that won't be edited +casper.then(function(){ +// Create rule + this.evaluate(function() { + $('#category-rule').val('thelia.constraint.rule.available_for_total_amount').change(); + return true; + }); + this.capture('tests/functionnal/casperjs/screenshot/coupons/rule-selected2.png'); +}); + +casper.wait(2000, function() { + this.echo("\nWaiting...."); +}); + +// Test Rule creation +casper.then(function(){ + this.evaluate(function() { + $('#price-operator').val('<=').change(); + return true; + }); + // Removing old value +// casper.evaluate(function triggerKeyDownEvent() { +// var e = $.Event("keydown"); +// e.which = 8; +// e.keyCode = 8; +// $("input#price-value").trigger(e); +// }); + this.evaluate(function() { + $("input#price-value").val('').change(); + return true; + }); + + // Changing 400 to 401 + this.sendKeys('input#price-value', '401'); + this.evaluate(function() { + $('#currency-value').val('GBP').change(); + return true; + }); + this.capture('tests/functionnal/casperjs/screenshot/coupons/rule-saved-edited-before-click-save.png'); + this.click('#constraint-save-btn'); +}); + +casper.wait(2000, function() { + this.echo("\nWaiting...."); +}); + +casper.then(function(){ + this.test.assertSelectorHasText('tbody#constraint-list tr:nth-child(1)', 'If cart total amount is superior to 40 EUR','7) 1st default rule found'); + this.test.assertSelectorDoesntHaveText('tbody#constraint-list tr:nth-child(2)', 'If cart total amount is inferior to 400 EUR','7) 2nd default rule found'); + this.test.assertSelectorHasText('tbody#constraint-list tr:nth-child(2)', 'If cart products quantity is equal to 5','7) 3rd rule updated found'); + this.test.assertSelectorHasText('tbody#constraint-list tr:nth-child(3)', 'If cart total amount is inferior or equal to 401 GBP','7) 4rd rule created found'); +}); + +// Check if created rule has been well saved +casper.thenOpen(thelia2_login_coupon_update_url, function() { + this.test.comment('Now on : ' + this.getCurrentUrl()); + this.capture('tests/functionnal/casperjs/screenshot/coupons/rule-added-refreshed.png'); + this.test.assertSelectorHasText('tbody#constraint-list tr:nth-child(1)', 'If cart total amount is superior to 40 EUR','8) 1st default rule found'); + this.test.assertSelectorDoesntHaveText('tbody#constraint-list tr:nth-child(2)', 'If cart total amount is inferior to 400 EUR','8) 2nd default rule found'); + this.test.assertSelectorHasText('tbody#constraint-list tr:nth-child(2)', 'If cart products quantity is equal to 5','8) 3rd rule updated found'); + this.test.assertSelectorHasText('tbody#constraint-list tr:nth-child(3)', 'If cart total amount is inferior or equal to 401 GBP','8) 4rd rule created found'); +}); + +//RUN +casper.run(function() { + this.test.done(); +}); \ No newline at end of file