Skip to content

Commit

Permalink
Merge pull request PrestaShop#8425 from Quetzacoalt91/BOOM-3861
Browse files Browse the repository at this point in the history
Use regexp instead of simple explode for complex attributes in CartPresenter
  • Loading branch information
mickaelandrieu authored Oct 31, 2017
2 parents 3cf897a + e1a1327 commit f299586
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 29 deletions.
35 changes: 25 additions & 10 deletions src/Adapter/Cart/CartPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,7 @@ private function presentProduct(array $rawProduct)
$settings->showPrices = Configuration::showPrices();

if (isset($rawProduct['attributes']) && is_string($rawProduct['attributes'])) {
// return an array of attributes
$rawProduct['attributes'] = explode(Configuration::get('PS_ATTRIBUTE_ANCHOR_SEPARATOR'), $rawProduct['attributes']);
$attributesArray = array();

foreach ($rawProduct['attributes'] as $attribute) {
list($key, $value) = explode(':', $attribute);
$attributesArray[trim($key)] = ltrim($value);
}

$rawProduct['attributes'] = $attributesArray;
$rawProduct['attributes'] = $this->getAttributesArrayFromString($rawProduct['attributes']);
}
$rawProduct['remove_from_cart_url'] = $this->link->getRemoveFromCartURL(
$rawProduct['id_product'],
Expand Down Expand Up @@ -495,4 +486,28 @@ private function getTemplateVarVouchers(Cart $cart)
'added' => $vouchers,
);
}

/**
* Receives a string containing a list of attributes affected to the product and returns them as an array
*
* @param string $attributes
* @return array Converted attributes in an array
*/
protected function getAttributesArrayFromString($attributes)
{
$separator = Configuration::get('PS_ATTRIBUTE_ANCHOR_SEPARATOR');
$pattern = '/(?>(?P<attribute>[^:]+:[^:]+)'.$separator.'+(?!'.$separator.'([^:'.$separator.'])+:))/';
$attributesArray = array();
$matches = array();
if (!preg_match_all($pattern, $attributes.$separator, $matches)) {
return $attributesArray;
}

foreach ($matches['attribute'] as $attribute) {
list($key, $value) = explode(':', $attribute);
$attributesArray[trim($key)] = ltrim($value);
}

return $attributesArray;
}
}
27 changes: 27 additions & 0 deletions tests/TestCase/UnitTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ protected function setUp()

$this->setupContextualTemplateEngineMock();
$this->setupContextualLanguageMock();
$this->setupContextualLinkMock();
$this->setupContextualEmployeeMock();
$this->setupContextualCookieMock();
$this->setupContextualCurrencyMock();
Expand Down Expand Up @@ -162,6 +163,13 @@ protected function setupContextualLanguageMock()
return $this->context->language;
}

protected function setupContextualLinkMock()
{
$this->context->link = Phake::mock('Link');

return $this->context->link;
}

protected function setupContextualCookieMock() {
$this->context->cookie = Phake::mock('Cookie');

Expand Down Expand Up @@ -210,4 +218,23 @@ public function teardown()
$container = $container_builder->build();
ServiceLocator::setServiceContainerInstance($container);
}

/**
* Call protected/private method of a class.
*
* @param object &$object Instantiated object that we will run method on.
* @param string $methodName Method name to call
* @param array $parameters Array of parameters to pass into method.
*
* @return mixed Method return.
* @link https://jtreminio.com/2013/03/unit-testing-tutorial-part-3-testing-protected-private-methods-coverage-reports-and-crap/
*/
protected function invokeMethod(&$object, $methodName, array $parameters = array())
{
$reflection = new \ReflectionClass(get_class($object));
$method = $reflection->getMethod($methodName);
$method->setAccessible(true);

return $method->invokeArgs($object, $parameters);
}
}
19 changes: 0 additions & 19 deletions tests/Unit/Adapter/Module/Tab/ModuleTabRegisterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,23 +222,4 @@ public function testTabNames()
$expectedResult = array(1 => $names['fr'], 2 => $names['en'], 3 => $names['en']);
$this->assertEquals($expectedResult, $this->invokeMethod($this->tabRegister, 'getTabNames', array($names)));
}

/**
* Call protected/private method of a class.
*
* @param object &$object Instantiated object that we will run method on.
* @param string $methodName Method name to call
* @param array $parameters Array of parameters to pass into method.
*
* @return mixed Method return.
* @link https://jtreminio.com/2013/03/unit-testing-tutorial-part-3-testing-protected-private-methods-coverage-reports-and-crap/
*/
protected function invokeMethod(&$object, $methodName, array $parameters = array())
{
$reflection = new \ReflectionClass(get_class($object));
$method = $reflection->getMethod($methodName);
$method->setAccessible(true);

return $method->invokeArgs($object, $parameters);
}
}

0 comments on commit f299586

Please sign in to comment.