View source
<?php
namespace Drupal\Tests\twig_tools\Unit;
use Drupal\Core\Template\Loader\StringLoader;
use Drupal\Tests\UnitTestCase;
use Drupal\twig_tools\TwigExtension\TwigConvert;
class TwigConvertTest extends UnitTestCase {
public function setUp() {
parent::setUp();
$loader = new StringLoader();
$this->twig = new \Twig_Environment($loader);
$twigTools = new TwigConvert();
$this->twig
->addExtension($twigTools);
}
public function testBooleanValue($template, $expected) {
$result = $this->twig
->render($template);
$this
->assertSame($expected, $result);
}
public function providerTestBooleanValues() {
return [
[
"{{ 0|boolean is same as (false) ? 'false' : 'true' }}",
"false",
],
[
"{{ 42|boolean is same as (false) ? 'false' : 'true' }}",
"true",
],
[
"{{ 0.0|boolean is same as (false) ? 'false' : 'true' }}",
"false",
],
[
"{{ -1|boolean is same as (false) ? 'false' : 'true' }}",
"true",
],
[
"{{ 4.2|boolean is same as (false) ? 'false' : 'true' }}",
"true",
],
[
"{{ ''|boolean is same as (false) ? 'false' : 'true' }}",
"false",
],
[
"{{ 'string'|boolean is same as (false) ? 'false' : 'true' }}",
"true",
],
[
"{{ 'true'|boolean is same as (false) ? 'false' : 'true' }}",
"true",
],
[
"{{ 'false'|boolean is same as (false) ? 'false' : 'true' }}",
"true",
],
[
"{{ '0'|boolean is same as (false) ? 'false' : 'true' }}",
"false",
],
[
"{{ '1'|boolean is same as (false) ? 'false' : 'true' }}",
"true",
],
[
"{{ [1, 2]|boolean is same as (false) ? 'false' : 'true' }}",
"true",
],
[
"{{ [0]|boolean is same as (false) ? 'false' : 'true' }}",
"true",
],
[
"{{ [0, 0]|boolean is same as (false) ? 'false' : 'true' }}",
"true",
],
[
"{{ []|boolean is same as (false) ? 'false' : 'true' }}",
"false",
],
[
"{{ false|boolean is same as (false) ? 'false' : 'true' }}",
"false",
],
[
"{{ true|boolean is same as (false) ? 'false' : 'true' }}",
"true",
],
[
"{{ null|boolean is same as (false) ? 'false' : 'true' }}",
"false",
],
];
}
public function testIntegerValue($template, $expected) {
$result = $this->twig
->render($template);
$this
->assertSame($expected, $result);
}
public function providerTestIntegerValues() {
return [
[
"{{ 42|integer }}",
'42',
],
[
"{{ 4.2|integer }}",
'4',
],
[
"{{ '42'|integer }}",
'42',
],
[
"{{ '+42'|integer }}",
'42',
],
[
"{{ '-42'|integer }}",
'-42',
],
[
"{{ 042|integer }}",
'42',
],
[
"{{ '042'|integer }}",
'42',
],
[
"{{ 42000000|integer }}",
'42000000',
],
[
"{{ []|integer }}",
'0',
],
[
"{{ ['foo', 'bar']|integer }}",
'1',
],
[
"{{ FALSE|integer }}",
'0',
],
[
"{{ TRUE|integer }}",
'1',
],
[
"{{ NULL|integer }}",
'0',
],
[
"{{ 0|integer }}",
'0',
],
[
"{{ 1|integer }}",
'1',
],
[
"{{ 0.0|integer }}",
'0',
],
[
"{{ 1.0|integer }}",
'1',
],
];
}
public function testFloatValue($template, $expected) {
$result = $this->twig
->render($template);
$this
->assertSame($expected, $result);
}
public function providerTestFloatValues() {
return [
[
"{{ 42|float }}",
'42',
],
[
"{{ 4.2|float }}",
'4.2',
],
[
"{{ 0.42|float }}",
'0.42',
],
[
"{{ 42000000.00|float }}",
'42000000',
],
[
"{{ 42.0000000|float }}",
'42',
],
[
"{{ -42.0000000|float }}",
'-42',
],
[
"{{ +42.0000000|float }}",
'42',
],
[
"{{ 42.00000001|float }}",
'42.00000001',
],
[
"{{ 0000042.00000001|float }}",
'42.00000001',
],
[
"{{ '42.00000001The'|float }}",
'42.00000001',
],
[
"{{ 'The42.00000001'|float }}",
'0',
],
[
"{{ '42'|float }}",
'42',
],
[
"{{ '+42'|float }}",
'42',
],
[
"{{ '-42'|float }}",
'-42',
],
[
"{{ 042|float }}",
'42',
],
[
"{{ '042'|float }}",
'42',
],
[
"{{ 42000000|float }}",
'42000000',
],
[
"{{ []|float }}",
'0',
],
[
"{{ ['foo', 'bar']|float }}",
'1',
],
[
"{{ FALSE|float }}",
'0',
],
[
"{{ TRUE|float }}",
'1',
],
[
"{{ NULL|float }}",
'0',
],
[
"{{ 0|float }}",
'0',
],
[
"{{ 1|float }}",
'1',
],
[
"{{ 0.0|float }}",
'0',
],
[
"{{ 1.0|float }}",
'1',
],
];
}
public function testStringValue($template, $expected) {
$result = $this->twig
->render($template);
$this
->assertSame($expected, $result);
}
public function providerTestStringValues() {
return [
[
"{{ 42|string }}",
"42",
],
[
"{{ 4.2|string }}",
"4.2",
],
[
"{{ '42'|string }}",
"42",
],
[
"{{ '+42'|string }}",
"+42",
],
[
"{{ '-42'|string }}",
"-42",
],
[
"{{ 042|string }}",
"42",
],
[
"{{ '042'|string }}",
"042",
],
[
"{{ 42000000|string }}",
"42000000",
],
[
"{{ FALSE|string }}",
"",
],
[
"{{ TRUE|string }}",
"1",
],
[
"{{ NULL|string }}",
"",
],
[
"{{ 0|string }}",
"0",
],
[
"{{ 1|string }}",
"1",
],
[
"{{ 0.0|string }}",
"0",
],
[
"{{ 1.0|string }}",
"1",
],
];
}
public function testMd5Value($template, $expected) {
$result = $this->twig
->render($template);
$this
->assertSame($expected, $result);
}
public function providerTestMd5Values() {
return [
[
"{{ '42'|md5 }}",
'a1d0c6e83f027327d8461063f4ac58a6',
],
[
"{{ '4.2'|md5 }}",
'8653d5c7898950016e5d019df6815626',
],
[
"{{ '+42'|md5 }}",
'deda8ddbf790f3682d5cf69d237bb0b2',
],
[
"{{ '-42'|md5 }}",
'8dfcb89fd8620e3e7fb6a03a53f307dc',
],
[
"{{ '42'|md5 }}",
'a1d0c6e83f027327d8461063f4ac58a6',
],
[
"{{ 'Test'|md5 }}",
'0cbc6611f5540bd0809a388dc95a615b',
],
[
"{{ '0'|md5 }}",
'cfcd208495d565ef66e7dff9f98764da',
],
[
"{{ '0'|md5 }}",
'cfcd208495d565ef66e7dff9f98764da',
],
[
"{{ '1'|md5 }}",
'c4ca4238a0b923820dcc509a6f75849b',
],
];
}
public function testJsonDecode($template, $expected) {
$result = $this->twig
->render($template);
$this
->assertSame($expected, $result);
}
public function providerTestJsonDecodeValues() {
return [
[
'{% set json = \'{
"a": 1,
"b": 2,
"c": 3,
"d": 4,
"e": 5
}\'|json_decode(true) %}{{ json|join(",") }}',
'1,2,3,4,5',
],
[
'{% set json = \'{
"aliceblue": "#f0f8ff",
"antiquewhite": "#faebd7",
"aqua": "#00ffff",
"aquamarine": "#7fffd4",
"azure": "#f0ffff",
"beige": "#f5f5dc",
"bisque": "#ffe4c4",
"black": "#000000",
"blanchedalmond": "#ffebcd",
"blue": "#0000ff",
"blueviolet": "#8a2be2",
"brown": "#a52a2a"
}\'|json_decode %}{{ json|join(", ") }}',
'#f0f8ff, #faebd7, #00ffff, #7fffd4, #f0ffff, #f5f5dc, #ffe4c4, #000000, #ffebcd, #0000ff, #8a2be2, #a52a2a',
],
[
'{% set json = \'{
"string": "string",
"boolean_true": true,
"boolean_false": false,
"integer": 42,
"float": 4.2
}\'|json_decode %}{{ json|join(", ") }}',
'string, 1, , 42, 4.2',
],
];
}
public function testDateFromFormat($template, $expected) {
$result = $this->twig
->render($template);
$this
->assertSame($expected, $result);
}
public function providerTestDateFromFormat() {
return [
[
"{{ '2009-Feb-15'|date_from_format('Y-M-j', 'm/d/y') }}",
"02/15/09",
],
[
"{{ '2010-04-23 10:29:35'|date_from_format('Y-m-d H:i:s', 'Y-m-d\\\\TH:i:s', 'America/New_York', 'America/New_York') }}",
"2010-04-23T10:29:35",
],
[
"{{ '2010-04-23 00:00:00'|date_from_format('Y-m-d H:i:s', 'c', 'America/New_York', 'UTC') }}",
"2010-04-23T04:00:00+00:00",
],
[
"{{ '2019-01-01 00:00:00'|date_from_format('Y-m-d H:i:s', 'Y-m-d\\\\TH:i:s', 'America/New_York', 'America/Los_Angeles') }}",
"2018-12-31T21:00:00",
],
[
"{{ '1 1 19'|date_from_format('!j n y', 'Y-m-j\\\\TH:i:s', 'America/New_York', 'America/Los_Angeles') }}",
"2018-12-31T21:00:00",
],
[
"{{ 0|date_from_format('U', 'Y-m-d') }}",
"1970-01-01",
],
[
"{{ '0'|date_from_format('U', 'Y-m-d') }}",
"1970-01-01",
],
[
"{{ '0000-00-00 00:00:00'|date_from_format('Y-m-d H:i:s', 'Y-m-d H:i:s', 'UTC', 'UTC') }}",
"-0001-11-30 00:00:00",
],
[
"{{ '01/05/1955 10:43:22'|date_from_format('m/d/Y H:i:s', 'Y-m-d\\\\TH:i:s e') }}",
"1955-01-05T10:43:22 Australia/Sydney",
],
[
"{{ '2004-11-19 10:25:33'|date_from_format('Y-m-d h:i:s', 'U', 'America/Managua') }}",
"1100881533",
],
[
"{{ '2004-12-19 10:19:42'|date_from_format('Y-m-d H:i:s', 'Y-m-d H:i:s e', NULL, 'America/Managua') }}",
"2004-12-18 17:19:42 America/Managua",
],
[
"{{ ''|date_from_format('m/d/Y', 'Y-m-d') }}",
"",
],
];
}
public function tearDown() {
unset($this->twigTools, $this->twig);
}
}