<commit_before><?php

namespace Concrete\Tests\File\Image\Svg;

use Concrete\Core\File\Image\Svg\Sanitizer;
use Concrete\Core\File\Image\Svg\SanitizerOptions;
use Concrete\Core\Support\Facade\Application;
use Illuminate\Filesystem\Filesystem;
use Mockery;
use PHPUnit_Framework_TestCase;

class SanitizerTest extends PHPUnit_Framework_TestCase
{
    /**
     * @var \Concrete\Core\File\Image\Svg\Sanitizer
     */
    protected static $sanitizer;

    /**
     * @var \Concrete\Core\File\Image\Svg\SanitizerOptions
     */
    protected static $sanitizerOptions;

    /**
     * {@inheritdoc}
     *
     * @see PHPUnit_Framework_TestCase::setupBeforeClass()
     */
    public static function setupBeforeClass()
    {
        parent::setUpBeforeClass();
        $app = Application::getFacadeApplication();
        self::$sanitizer = $app->build(Sanitizer::class);
        self::$sanitizerOptions = new SanitizerOptions();
        self::$sanitizerOptions
            ->setUnsafeElements('script script2')
            ->setElementWhitelist('script2')
            ->setUnsafeAttributes('onload onload2 onclick')
            ->setAttributeWhitelist('onload2')
        ;
    }

    /**
     * {@inheritdoc}
     *
     * @see PHPUnit_Framework_TestCase::tearDown()
     */
    public function tearDown()
    {
        Mockery::close();
    }

    /**
     * @return array
     */
    public function provideSanitizeWithDefaultSettings()
    {
        return [
            ['<svg/>', '<svg/>'],
            ['<svg good="1" />', '<svg good="1"/>'],
            ['<svg><script>alert(1);</script></svg>', '<svg/>'],
            ['<svg><script2>alert(1);</script2></svg>', '<svg><script2>alert(1);</script2></svg>'],
            ['<svg onload="alert(1)" />', '<svg/>'],
            ['<svg foo="1" onload="alert(1)" bar="2" />', '<svg foo="1" bar="2"/>'],
            ['<svg foo="1" OnLoad="alert(1)" OnLoad2="alert(1)" bar="2" />', '<svg foo="1" OnLoad2="alert(1)" bar="2"/>'],
            ['<svg><script></script><g onLoad="alert(1)"><rect /></g></svg>', '<svg><g><rect/></g></svg>'],
        ];
    }

    /**
     * @param string $input
     * @param string $expectedOutput
     * @dataProvider provideSanitizeWithDefaultSettings
     */
    public function testSanitizeWithDefaultSettings($input, $expectedOutput)
    {
        $sanitized = self::$sanitizer->sanitizeData($input, self::$sanitizerOptions);
        $lines = explode("\n", $sanitized);
        $this->assertSame('<?xml version="1.0"?>', array_shift($lines));
        $xml = trim(implode('', $lines));
        $this->assertSame($expectedOutput, $xml);
    }

    /**
     * @expectedException Concrete\Core\File\Image\Svg\SanitizerException
     */
    public function testLoadInvalidFile()
    {
        self::$sanitizer->sanitizeFile(__DIR__ . '/does-not-exist');
    }

    /**
     * @return array
     */
    public function provideInvalidData()
    {
        return [
            ['<svg'],
        ];
    }

    /**
     * @expectedException Concrete\Core\File\Image\Svg\SanitizerException<commit_msg>Don't expect a specific XML header<commit_after><?php

namespace Concrete\Tests\File\Image\Svg;

use Concrete\Core\File\Image\Svg\Sanitizer;
use Concrete\Core\File\Image\Svg\SanitizerOptions;
use Concrete\Core\Support\Facade\Application;
use Illuminate\Filesystem\Filesystem;
use Mockery;
use PHPUnit_Framework_TestCase;

class SanitizerTest extends PHPUnit_Framework_TestCase
{
    /**
     * @var \Concrete\Core\File\Image\Svg\Sanitizer
     */
    protected static $sanitizer;

    /**
     * @var \Concrete\Core\File\Image\Svg\SanitizerOptions
     */
    protected static $sanitizerOptions;

    /**
     * {@inheritdoc}
     *
     * @see PHPUnit_Framework_TestCase::setupBeforeClass()
     */
    public static function setupBeforeClass()
    {
        parent::setUpBeforeClass();
        $app = Application::getFacadeApplication();
        self::$sanitizer = $app->build(Sanitizer::class);
        self::$sanitizerOptions = new SanitizerOptions();
        self::$sanitizerOptions
            ->setUnsafeElements('script script2')
            ->setElementWhitelist('script2')
            ->setUnsafeAttributes('onload onload2 onclick')
            ->setAttributeWhitelist('onload2')
        ;
    }

    /**
     * {@inheritdoc}
     *
     * @see PHPUnit_Framework_TestCase::tearDown()
     */
    public function tearDown()
    {
        Mockery::close();
    }

    /**
     * @return array
     */
    public function provideSanitizeWithDefaultSettings()
    {
        return [
            ['<svg/>', '<svg/>'],
            ['<svg good="1" />', '<svg good="1"/>'],
            ['<svg><script>alert(1);</script></svg>', '<svg/>'],
            ['<svg><script2>alert(1);</script2></svg>', '<svg><script2>alert(1);</script2></svg>'],
            ['<svg onload="alert(1)" />', '<svg/>'],
            ['<svg foo="1" onload="alert(1)" bar="2" />', '<svg foo="1" bar="2"/>'],
            ['<svg foo="1" OnLoad="alert(1)" OnLoad2="alert(1)" bar="2" />', '<svg foo="1" OnLoad2="alert(1)" bar="2"/>'],
            ['<svg><script></script><g onLoad="alert(1)"><rect /></g></svg>', '<svg><g><rect/></g></svg>'],
        ];
    }

    /**
     * @param string $input
     * @param string $expectedOutput
     * @dataProvider provideSanitizeWithDefaultSettings
     */
    public function testSanitizeWithDefaultSettings($input, $expectedOutput)
    {
        $sanitized = self::$sanitizer->sanitizeData($input, self::$sanitizerOptions);
        $lines = explode("\n", $sanitized);
        $this->assertRegExp('/^<\?xml\b[^>]*\?>$/', array_shift($lines));
        $xml = trim(implode('', $lines));
        $this->assertSame($expectedOutput, $xml);
    }

    /**
     * @expectedException Concrete\Core\File\Image\Svg\SanitizerException
     */
    public function testLoadInvalidFile()
    {
        self::$sanitizer->sanitizeFile(__DIR__ . '/does-not-exist');
    }

    /**
     * @return array
     */
    public function provideInvalidData()
    {
        return [
            ['<svg'],
        ];
    }

    /**
     * @expectedException Concrete\Core\File\Image\Svg\SanitizerException
{'fraction_non_alphanumeric': 0.18124187256176852, 'fraction_numerical': 0.00975292587776333, 'mean_word_length': 2.6778242677824267, 'pattern_counts': {'":': 0, '<': 79, '<?xml version=': 1, '>': 94, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 6}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4431493', 'n_tokens_mistral': 1922, 'n_tokens_neox': 1930, 'n_words': 283}