Skip to content

Commit

Permalink
ESDEV-4774 Change updateProductAmountInBasket to changeBasket
Browse files Browse the repository at this point in the history
Rewrite method that it would be driver and theme agnostic.
Rename method to match already existing logic etc. addToBasket
  • Loading branch information
stasiukaitis-saulius committed Aug 23, 2017
1 parent c06dc1d commit be7175b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 32 deletions.
9 changes: 7 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -353,12 +353,17 @@ Add article to basket:
// This will add article with ID 1001 to basket.
$this->addToBasket("1001");
Update items amount in basket:
Update items amount in basket: *(Note that item must be in basket in order to change it)*

.. code:: php
// This will update article with ID 1001 in basket to have 2 items.
$this->updateProductAmountInBasket("1001", 2);
$this->changeBasket("1001", 2);
.. code:: php
// This will remove an item from basket.
$this->changeBasket("1001", 0);
Login user in front end side:

Expand Down
56 changes: 26 additions & 30 deletions library/AcceptanceTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ public function openArticle($articleId, $clearCache = false, $shopId = null)
}

/**
* Adds article to basket
* Adds article to basket.
*
* @param string $articleId Article id
* @param int $amount Amount of items to add
Expand Down Expand Up @@ -451,40 +451,36 @@ public function addToBasket(
$this->openNewWindow($this->_getShopUrl($aParams, $shopId), false);
}


/**
* Update basket with new amount for the product.
* Add needed amount or 0 to delete item from the basket.
* Change product in basket.
*
* @param string $productId oxid of the product.
* @param int $itemsAmount amount of the product.
* @param string $articleId Article id
* @param int $amount Amount of items to add
* @param string $controller Controller name which should be opened after article is added
* @param array $additionalParams Additional parameters (like persparam[details] for label)
* @param int $shopId Shop id
*/
public function updateProductAmountInBasket($productId, $itemsAmount)
{
$this->openBasket();
$this->getHtmlSource();

$basket = new \OxidEsales\Eshop\Application\Model\Basket();
$itemId = $basket->getItemKey($productId);

// There is a bug in goutte while clicking on button in a form with more than one button:
// only first button is available. In this case the third button is needed.
// Bug is in BrowserKitDriver->click($xpath) where $this->getCrawler()->filterXPath($xpath) is called.
// It returns a form which later on throws an exception.
// This code gets a form and push it without need to click a button.
if ($this->currentMinkDriver === "goutte") {
$updateButton = $this->getElement("//button[@id='basketRemove']");
$client = $updateButton->getSession()->getDriver()->getClient();
$updateItemsForm = $client->getCrawler()->filterXPath("//form[@name='basket']")->form();
$client->submit($updateItemsForm, ['aproducts[' . $itemId . '][am]' => $itemsAmount]);
} else {
$amountInput = $this->getElement("//input[@name='aproducts[$itemId][am]']");
$amountInput->setValue($itemsAmount);
public function changeBasket(
$articleId,
$amount = 1,
$controller = 'basket',
$additionalParams = array(),
$shopId = null
) {
$input = $this->getElement('stoken', false);
if ($input) {
$params['stoken'] = $input->getValue();
}
$params['cl'] = $controller;
$params['fnc'] = 'changebasket';
$params['aid'] = $articleId;
$params['am'] = $amount;
$params['anid'] = $articleId;

$productSelector = $this->getElement("//input[@name='aproducts[$itemId][remove]']");
$productSelector->check();
$params = array_merge($params, $additionalParams);

$this->clickAndWait("//button[@id='basketUpdate']");
}
$this->openNewWindow($this->_getShopUrl($params, $shopId), false);
}

/**
Expand Down

0 comments on commit be7175b

Please sign in to comment.