tao-test/app/tao/test/unit/helpers/Base64Test.php

111 lines
7.4 KiB
PHP

<?php
/**
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; under version 2
* of the License (non-upgradable).
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Copyright (c) 2020 (original work) Open Assessment Technologies SA;
*/
declare(strict_types=1);
namespace oat\tao\test\unit\helpers;
use stdClass;
use oat\tao\helpers\Base64;
use oat\generis\test\TestCase;
/**
* Class Base64Test
*
* @package oat\tao\test\unit\helpers
*/
class Base64Test extends TestCase
{
private const ENCODED_FILE = 'data:@file/plain;base64,UFMtMjAxLjY2NjguMTUz';
private const ENCODED_IMAGE = 'data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD//gA+Q1JFQVRPUjogZ2QtanBlZyB2MS4wICh1c2luZyBJSkcgSlBFRyB2NjIpLCBkZWZhdWx0IHF1YWxpdHkK/9sAQwAIBgYHBgUIBwcHCQkICgwUDQwLCwwZEhMPFB0aHx4dGhwcICQuJyAiLCMcHCg3KSwwMTQ0NB8nOT04MjwuMzQy/9sAQwEJCQkMCwwYDQ0YMiEcITIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIy/8AAEQgBLAEsAwEiAAIRAQMRAf/EAB8AAAEFAQEBAQEBAAAAAAAAAAABAgMEBQYHCAkKC//EALUQAAIBAwMCBAMFBQQEAAABfQECAwAEEQUSITFBBhNRYQcicRQygZGhCCNCscEVUtHwJDNicoIJChYXGBkaJSYnKCkqNDU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6g4S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WgA9KSlpKAFzxRSUuaAEpaKKACjNFJQAvSjPFJS4oABRRRQAc55HNGaKXaduevt6UAJR24P1FGetGMcHrQAUlLQKACjijilBI6EigBDjPFKDg8daQ0UAFFLx+NIKAFJGcgUlKaSgBTgAYz054pKKWgBKPrQevFHagAooxxmgjFABRj6UGigAPHHFHXpRR1oASilpKAFpKWkoAWigdeaKADiiikoAWkpeMe9JQAtJSj1pKAClooznrQAE0HrRxRQAUe/eiigA70dKKMfSgANFBAoOM8dKADvS59qTvQKAFopMUUAHej60uTnOM89D3pM0ALzg4zgCgknAJOB0o/Gg4zjI+tAB/OkpcEjNJQAUY6e/NGaKAA0HknoPaijORQAUpBxnHA4Jx0pDQelABSUUtABSUtJQAvejt0oFFAB069aOv1o69BQaACj6UUDjmgAFFFJQAuaSiigBaKSigApaDnjOaKAE7UtGKSgAopaKACjiigGgAzil69BSE5NHGOlACjGeelIR9ac2BlRtbB+8M802gA7UZopQf8A9dACUH2oo7UAB6+lB9qB160UAFLxgYJzjnNJR2oAM8UUUlABS/hSUtAASScmkpT9MUlABS0lKRgmgANJS+3NHTHvQAUZ5zRSUAFFFL/OgAooAycD9aSgBaKKO9AACAwJAI9PWijtRigA7UlFLQAfWig9qCMHBByOtACk5x0o+8RgYpD1PSigBSxPfp0HpR3zzRn3pP0oAO/FFHeigBaSijrQAdu+aBjNLSUAFB60UUAFFFFAB+GaUsWABJOBge1J19qSgApaKSgBcnpmjtR2pKAFooooASijtRQAUUueP/rUlAC0lL3ooAP5UdTSUtABRRRQAUlLyeaKAEpaKSgApcH0NJS0AHGeKOgFGMngGjHvQAuPSko7dqKADviig4zxRQAUUp4yKSgA7UUYooAPwoo46frRQAUDr0o6UUAH5UUUlABRS8YHJz6YpKAFpKKKAClzSUtACUUUtABRRRQAetJRRQAtFAooAKPag0YoASilAyccc0lACgUlLQKADvR+NJSgUAKoywycDOMntQKTvRQAd6XjFAODkUnagBc0nrSk/LjjAPp6/wD6qSgAoxR0ooAO1HFFFAB+NGPeijHWgA60UUAE8Dk0AHbpQaKMUAJRRRQAUvIPoRRSUALRRRQAUdDRSUAFFLRQAUHHXIoooABijFFJQAvJo4I96KKAAUUUlACgcHPGBnnvSUUuOKACiiigAopSpAzg4PQ460CgBKXtg8UfTpSflQAtJS44zSdqADrRSgrg5B6cc0nWgAoo7d6DQAZ5yefrRRRQAZ5oo5Pajv1oAKKPWigBKKKKACilpKAF7UAc4AyelFFACU48HGMYptLQAlFKKSgBaSiigBR19u+KKKOtABRR+FBxk4JI7ZoAKKO1HGKADtRSUpoASlz/AJzSUtABg4zg46Zo/SgkH60o6UAAAzyeKSijtQAo65zzSUH1ooAKKO1FAC/nR0H/ANekooAOpooo56ZoAD+FFGKKACg0YooASiinADcN3APXHOPwoATt1pKWkoAKWkpSScZJOOBmgBKWgdaO4oAOPxoooFABSGlJz2xRQAUlLRxmgA78UUEHGccHpxSUALxSUtBOfwoAKSlooASlCkjgE0UUAFLjNJ3ooAPxo7UdqM0ABHtQBwaOhooAKKOxoHNABQaU9KT2oAOxo7HkU4AGMt3BA/nTT1oAKKO9BoAKXkjgUlB6UAJRRT3UKFx3Gf1IoAbRSU5VBGfcCgBKSilNABRRj5sUUAGaASCCDgjoaDwaSgApTjAwSeOeOlJS5oAB1oo70AfKTQAE5ycUdTzSUZoAKWjHFJQAtFJRQAtAJHr+dB6migD/2Q==';
/**
* @dataProvider base64EncodingDataProvider
*
* @param $value
* @param bool $result
*/
public function testBase64Encoding($value, bool $result): void
{
$this->assertEquals($result, Base64::isEncoded($value));
}
/**
* @dataProvider base64EncodingImageDataProvider
*
* @param $value
* @param bool $result
*/
public function testBase64EncodingImage($value, bool $result): void
{
$this->assertEquals($result, Base64::isEncodedImage($value));
}
/**
* @return array
*/
public function base64EncodingDataProvider(): array
{
return [
'object' => [
'value' => new stdClass(),
'result' => false,
],
'string' => [
'value' => 'string',
'result' => false,
],
'encodedString' => [
'value' => self::ENCODED_FILE,
'result' => true,
],
'encodedImage' => [
'value' => self::ENCODED_IMAGE,
'result' => true,
],
];
}
/**
* @return array
*/
public function base64EncodingImageDataProvider(): array
{
return [
'object' => [
'value' => new stdClass(),
'result' => false,
],
'string' => [
'value' => 'string',
'result' => false,
],
'encodedString' => [
'value' => self::ENCODED_FILE,
'result' => false,
],
'encodedImage' => [
'value' => self::ENCODED_IMAGE,
'result' => true,
],
];
}
}