Commit 33510025 authored by Dennis A. Torres Rodriguez's avatar Dennis A. Torres Rodriguez Committed by pcate
Browse files

Issue #3246405: Add filter to encode/decode base64 strings

parent 4eaa7e95
Loading
Loading
Loading
Loading
+52 −0
Original line number Diff line number Diff line
@@ -19,6 +19,8 @@ class TwigConvert extends \Twig_Extension {
      new \Twig_SimpleFilter('md5', [$this, 'md5Value']),
      new \Twig_SimpleFilter('json_decode', [$this, 'jsonDecode']),
      new \Twig_SimpleFilter('date_from_format', [$this, 'dateFromFormat']),
      new \Twig_SimpleFilter('base64_encode', [$this, 'base64Encode']),
      new \Twig_SimpleFilter('base64_decode', [$this, 'base64Decode']),
    ];
  }

@@ -147,4 +149,54 @@ class TwigConvert extends \Twig_Extension {
    return $converted_date->format($to_format);
  }

  /**
   * Encode string value to Base64 string.
   *
   * @param string $value
   *   The string value to encode.
   *
   * @return string
   *   The encoded string.
   *
   * @throws \Twig_Error_Runtime
   */
  public static function base64Encode($value) {
    if (empty($value)) {
      return '';
    }

    if (!is_string($value)) {
      throw new \Twig_Error_Runtime(sprintf('The "base64_encode" filter expects a string as value, got "%s".',
        \gettype($value)));
    }

    return base64_encode($value);
  }

  /**
   * Decode string from Base64.
   *
   * @param string $value
   *   The encoded string value.
   * @param bool $strict
   *   If set to TRUE, enforce base64 alphabet.
   *
   * @return false|string
   *   Returns decoded string or FALSE on failure.
   *
   * @throws \Twig_Error_Runtime
   */
  public static function base64Decode($value, $strict = FALSE) {
    if (empty($value)) {
      return '';
    }

    if (!is_string($value)) {
      throw new \Twig_Error_Runtime(sprintf('The "base64_decode" filter expects a string as value, got "%s".',
        \gettype($value)));
    }

    return base64_decode($value, $strict);
  }

}
+58 −0
Original line number Diff line number Diff line
@@ -315,6 +315,64 @@ class TwigConvertTest extends UnitTestCase {

  }

  /**
   * @covers ::base64Encode
   *
   * @dataProvider providerTestBase64Encode
   */
  public function testBase64Encode($template, $expected) {
    $result = $this->twig->render($template);
    $this->assertSame($expected, $result);
  }

  /**
   * Provides test data for testBase64Encode.
   *
   * @return array
   *   An array of test strings in plain text and base64 format.
   */
  public function providerTestBase64Encode() {
    return [
      [
        "{{ 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi dapibus.' | base64_encode }}",
        "TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGNvbnNlY3RldHVyIGFkaXBpc2NpbmcgZWxpdC4gTW9yYmkgZGFwaWJ1cy4=",
      ],
      [
        '{{ \'{"array":[1,2,3],"boolean":true,"color":"gold","null":null,"number":123,"object":{"a":"b","c":"d"},"string":"Hello World"}\' | base64_encode }}',
        "eyJhcnJheSI6WzEsMiwzXSwiYm9vbGVhbiI6dHJ1ZSwiY29sb3IiOiJnb2xkIiwibnVsbCI6bnVsbCwibnVtYmVyIjoxMjMsIm9iamVjdCI6eyJhIjoiYiIsImMiOiJkIn0sInN0cmluZyI6IkhlbGxvIFdvcmxkIn0=",
      ],
    ];
  }

  /**
   * @covers ::base64Decode
   *
   * @dataProvider providerTestBase64Decode
   */
  public function testBase64Decode($template, $expected) {
    $result = $this->twig->render($template);
    $this->assertSame($expected, $result);
  }

  /**
   * Provides test data for testBase64Decode.
   *
   * @return array
   *   An array of test strings in plain text and base64 format.
   */
  public function providerTestBase64Decode() {
    return [
      [
        "{{ 'TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGNvbnNlY3RldHVyIGFkaXBpc2NpbmcgZWxpdC4gTW9yYmkgZGFwaWJ1cy4=' | base64_decode }}",
        'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi dapibus.',
      ],
      [
        "{{ 'eyJhcnJheSI6WzEsMiwzXSwiYm9vbGVhbiI6dHJ1ZSwiY29sb3IiOiJnb2xkIiwibnVsbCI6bnVsbCwibnVtYmVyIjoxMjMsIm9iamVjdCI6eyJhIjoiYiIsImMiOiJkIn0sInN0cmluZyI6IkhlbGxvIFdvcmxkIn0=' | base64_decode | raw }}",
        '{"array":[1,2,3],"boolean":true,"color":"gold","null":null,"number":123,"object":{"a":"b","c":"d"},"string":"Hello World"}',
      ],
    ];
  }

  /**
   * Unset the test object.
   */