Skip to content
Snippets Groups Projects
Commit 1838bc92 authored by Bojan Živanović's avatar Bojan Živanović
Browse files

Issue #2910190 by sorabh.v6, casey, harings_rob, bojanz:...

Issue #2910190 by sorabh.v6, casey, harings_rob, bojanz: PriceTwigExtension::formatPrice() should accept empty values
parent 36926459
No related branches found
Tags 8.x-1.0-rc1
No related merge requests found
......@@ -28,7 +28,9 @@ class PriceTwigExtension extends \Twig_Extension {
/**
* Formats a price object/array.
*
* Example: {{ order.getTotalPrice|commerce_price_format }}
* Examples:
* {{ order.getTotalPrice|commerce_price_format }}
* {{ order.getTotalPrice|commerce_price_format|default('N/A') }}
*
* @param mixed $price
* Either a Price object, or an array with number and currency_code keys.
......@@ -39,10 +41,13 @@ class PriceTwigExtension extends \Twig_Extension {
* @throws \InvalidArgumentException
*/
public static function formatPrice($price) {
if (empty($price)) {
return '';
}
if ($price instanceof Price) {
$price = $price->toArray();
}
if (is_array($price) && isset($price['currency_code']) && isset($price['number'])) {
$number_formatter = \Drupal::service('commerce_price.number_formatter_factory')->createInstance();
$currency_storage = \Drupal::entityTypeManager()->getStorage('commerce_currency');
......
......@@ -5,37 +5,15 @@
* Test module for Price.
*/
use Drupal\commerce_price\Price;
/**
* Implements hook_theme().
*/
function commerce_price_test_theme($existing, $type, $theme, $path) {
return [
'working_commerce_price' => [
'template' => 'commerce-price-test-price-filter',
'variables' => [
// Correct keys.
'price' => [
'number' => '9.99',
'currency_code' => 'USD',
],
],
],
'broken_commerce_price' => [
'template' => 'commerce-price-test-price-filter',
'variables' => [
// Incorrect keys.
'price' => [
'numb' => '9.99',
'currency_cod' => 'USD',
],
],
],
'commerce_price_object' => [
'commerce_price_test' => [
'template' => 'commerce-price-test-price-filter',
'variables' => [
'price' => new Price('9.99', 'USD'),
'price' => NULL,
],
],
];
......
{{ price|commerce_price_format }}
{{ price|commerce_price_format|default('N/A') }}
......@@ -2,6 +2,7 @@
namespace Drupal\Tests\commerce_price\Kernel;
use Drupal\commerce_price\Price;
use Drupal\Tests\commerce\Kernel\CommerceKernelTestBase;
/**
......@@ -19,30 +20,50 @@ class PriceTwigExtensionTest extends CommerceKernelTestBase {
];
/**
* Tests an improperly formatted price array.
* Tests passing an invalid value.
*/
public function testBrokenPrice() {
$theme = ['#theme' => 'broken_commerce_price'];
public function testInvalidPrice() {
$theme = [
'#theme' => 'commerce_price_test',
'#price' => [
// Invalid keys.
'numb' => '9.99',
'currency_co' => 'USD',
],
];
$this->setExpectedException('InvalidArgumentException');
$this->render($theme);
}
/**
* Tests a properly formatted price array.
* Tests passing a valid value.
*/
public function testWorkingPrice() {
$theme = ['#theme' => 'working_commerce_price'];
public function testValidPrice() {
$theme = [
'#theme' => 'commerce_price_test',
'#price' => [
'number' => '9.99',
'currency_code' => 'USD',
],
];
$this->render($theme);
$this->assertText('$9.99');
$theme = [
'#theme' => 'commerce_price_test',
'#price' => new Price('20.99', 'USD'),
];
$this->render($theme);
$this->assertText('$20.99');
}
/**
* Tests a price object.
* Tests passing an empty value.
*/
public function testPriceObject() {
$theme = ['#theme' => 'commerce_price_object'];
public function testEmptyPrice() {
$theme = ['#theme' => 'commerce_price_test'];
$this->render($theme);
$this->assertText('$9.99');
$this->assertText('N/A');
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment