<commit_before><?php

namespace SilverStripe\AssetAdmin\Tests\Forms;

use Embed\Exceptions\InvalidUrlException;
use SilverStripe\AssetAdmin\Forms\RemoteFileFormFactory;
use SilverStripe\AssetAdmin\Tests\Forms\RemoteFileFormFactoryTest\MockEmbed;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\Forms\Form;
use SilverStripe\View\Embed\Embeddable;

class RemoteFileFormFactoryTest extends SapphireTest
{

    protected function setUp()
    {
        parent::setUp();

        // Reset all configs
        RemoteFileFormFactory::config()->set('fileurl_scheme_whitelist', []);
        RemoteFileFormFactory::config()->set('fileurl_scheme_blacklist', []);
        RemoteFileFormFactory::config()->set('fileurl_port_whitelist', []);
        RemoteFileFormFactory::config()->set('fileurl_port_blacklist', []);
        RemoteFileFormFactory::config()->set('fileurl_domain_whitelist', []);
        RemoteFileFormFactory::config()->set('fileurl_domain_blacklist', []);

        // mock embed
        Injector::inst()->load([
            Embeddable::class => [
                'class' => MockEmbed::class,
            ]
        ]);
    }

    public function providerTestAcceptedURLs()
    {
        return [
            [
                ['fileurl_scheme_blacklist' => ['https']],
                'http://www.google.com',
            ],
            [
                ['fileurl_scheme_whitelist' => ['http']],
                'http://www.google.com',
            ],
            [
                ['fileurl_domain_blacklist' => ['www.amazon.com']],
                'http://www.google.com',
            ],
            [
                ['fileurl_domain_whitelist' => ['www.google.com']],
                'http://www.google.com',
            ],
            [
                # port-ommitted urls ignored. Needs schema blacklist also
                ['fileurl_port_blacklist' => [80]],
                'http://www.google.com',
            ],
            [
                # port-ommitted urls ignored
                ['fileurl_port_whitelist' => [80]],
                'http://www.google.com',
            ],
            [
                ['fileurl_port_blacklist' => [443]],
                'http://www.google.com:80',
            ],
            [
                ['fileurl_port_whitelist' => [443]],
                'https://www.google.com:443',
            ],
        ];
    }

    /**
     * @dataProvider providerTestAcceptedURLs
     * @param array $config Config to merge
     * @param string $acceptedURL OK url
     */
    public function testAcceptedURLs($config, $acceptedURL)
    {
        foreach ($config as $key => $value) {
            RemoteFileFormFactory::config()->set($key, $value);
        }

        // Should pass
        $builder = new RemoteFileFormFactory();
        $fields = $builder->getForm(null, 'Form', [
            'type' => 'edit',
            'url' => $acceptedURL,
        ]);
        $this->assertInstanceOf(Form::class, $fields);
    }

    public function providerTestRejectedURLs()
    {
        return [
            [
                ['fileurl_scheme_blacklist' => ['https']],
                'https://www.google.com', 'This file scheme is not allowed',
            ],
            [
                ['fileurl_scheme_whitelist' => ['http']],
                'https://www.google.com', 'This file scheme is not allowed',
            ],
            [
                ['fileurl_domain_blacklist' => ['www.amazon.com']],
                'http://www.amazon.com', 'This file hostname is not allowed',
            ],
            [
                ['fileurl_domain_whitelist' => ['www.google.com']],
                'http://www.amazon.com', 'This file hostname is not allowed',
            ],
            [
                # ipv4 blacklist
                ['fileurl_domain_blacklist' => ['127.0.0.1']],
                'http://127.0.0.1/', 'This file hostname is not allowed',
            ],
            [
                # ipv6 blacklist
                ['fileurl_domain_blacklist' => ['[0:0:0:0:0:0:0:1]']],
                'http://[0:0:0:0:0:0:0:1]/', 'This file hostname is not allowed',
            ],
            [
                # ipv6 blacklist
                ['fileurl_domain_blacklist' => ['[::1]']],
                'http://[::1]/', 'This file hostname is not allowed',
            ],
            [
                ['fileurl_port_blacklist' => [80]],
                'http://www.google.com:80', 'This file port is not allowed',
            ],
            [
                # port-ommitted urls ignored
                ['fileurl_port_whitelist' => [80]],
                'http://www.google.com:443', 'This file port is not allowed',
            ],
        ];
    }

    /**
     * @dataProvider providerTestRejectedURLs
     * @param array $config Config to merge
     * @param string $rejectedURL rejected url
     * @param string $rejectedMessage
     */
    public function testRejectedURLS($config, $rejectedURL, $rejectedMessage)
    {
        $this->expectException(InvalidUrlException::class);
        $this->expectExceptionMessage($rejectedMessage);

        // Set config
        foreach ($config as $key => $value) {
            RemoteFileFormFactory::config()->set($key, $value);<commit_msg>Update tests for compliance with new translations<commit_after><?php

namespace SilverStripe\AssetAdmin\Tests\Forms;

use Embed\Exceptions\InvalidUrlException;
use SilverStripe\AssetAdmin\Forms\RemoteFileFormFactory;
use SilverStripe\AssetAdmin\Tests\Forms\RemoteFileFormFactoryTest\MockEmbed;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\Forms\Form;
use SilverStripe\View\Embed\Embeddable;

class RemoteFileFormFactoryTest extends SapphireTest
{

    protected function setUp()
    {
        parent::setUp();

        // Reset all configs
        RemoteFileFormFactory::config()->set('fileurl_scheme_whitelist', []);
        RemoteFileFormFactory::config()->set('fileurl_scheme_blacklist', []);
        RemoteFileFormFactory::config()->set('fileurl_port_whitelist', []);
        RemoteFileFormFactory::config()->set('fileurl_port_blacklist', []);
        RemoteFileFormFactory::config()->set('fileurl_domain_whitelist', []);
        RemoteFileFormFactory::config()->set('fileurl_domain_blacklist', []);

        // mock embed
        Injector::inst()->load([
            Embeddable::class => [
                'class' => MockEmbed::class,
            ]
        ]);
    }

    public function providerTestAcceptedURLs()
    {
        return [
            [
                ['fileurl_scheme_blacklist' => ['https']],
                'http://www.google.com',
            ],
            [
                ['fileurl_scheme_whitelist' => ['http']],
                'http://www.google.com',
            ],
            [
                ['fileurl_domain_blacklist' => ['www.amazon.com']],
                'http://www.google.com',
            ],
            [
                ['fileurl_domain_whitelist' => ['www.google.com']],
                'http://www.google.com',
            ],
            [
                # port-ommitted urls ignored. Needs schema blacklist also
                ['fileurl_port_blacklist' => [80]],
                'http://www.google.com',
            ],
            [
                # port-ommitted urls ignored
                ['fileurl_port_whitelist' => [80]],
                'http://www.google.com',
            ],
            [
                ['fileurl_port_blacklist' => [443]],
                'http://www.google.com:80',
            ],
            [
                ['fileurl_port_whitelist' => [443]],
                'https://www.google.com:443',
            ],
        ];
    }

    /**
     * @dataProvider providerTestAcceptedURLs
     * @param array $config Config to merge
     * @param string $acceptedURL OK url
     */
    public function testAcceptedURLs($config, $acceptedURL)
    {
        foreach ($config as $key => $value) {
            RemoteFileFormFactory::config()->set($key, $value);
        }

        // Should pass
        $builder = new RemoteFileFormFactory();
        $fields = $builder->getForm(null, 'Form', [
            'type' => 'edit',
            'url' => $acceptedURL,
        ]);
        $this->assertInstanceOf(Form::class, $fields);
    }

    public function providerTestRejectedURLs()
    {
        return [
            [
                ['fileurl_scheme_blacklist' => ['https']],
                'https://www.google.com', 'This file scheme is not included in the whitelist',
            ],
            [
                ['fileurl_scheme_whitelist' => ['http']],
                'https://www.google.com', 'This file scheme is not included in the whitelist',
            ],
            [
                ['fileurl_domain_blacklist' => ['www.amazon.com']],
                'http://www.amazon.com', 'This file hostname is not included in the whitelist',
            ],
            [
                ['fileurl_domain_whitelist' => ['www.google.com']],
                'http://www.amazon.com', 'This file hostname is not included in the whitelist',
            ],
            [
                # ipv4 blacklist
                ['fileurl_domain_blacklist' => ['127.0.0.1']],
                'http://127.0.0.1/', 'This file hostname is not included in the whitelist',
            ],
            [
                # ipv6 blacklist
                ['fileurl_domain_blacklist' => ['[0:0:0:0:0:0:0:1]']],
                'http://[0:0:0:0:0:0:0:1]/', 'This file hostname is not included in the whitelist',
            ],
            [
                # ipv6 blacklist
                ['fileurl_domain_blacklist' => ['[::1]']],
                'http://[::1]/', 'This file hostname is not included in the whitelist',
            ],
            [
                ['fileurl_port_blacklist' => [80]],
                'http://www.google.com:80', 'This file port is not allowed',
            ],
            [
                # port-ommitted urls ignored
                ['fileurl_port_whitelist' => [80]],
                'http://www.google.com:443', 'This file port is not allowed',
            ],
        ];
    }

    /**
     * @dataProvider providerTestRejectedURLs
     * @param array $config Config to merge
     * @param string $rejectedURL rejected url
     * @param string $rejectedMessage
     */
    public function testRejectedURLS($config, $rejectedURL, $rejectedMessage)
    {
        $this->expectException(InvalidUrlException::class);
        $this->expectExceptionMessage($rejectedMessage);

        // Set config
        foreach ($config as $key => $value) {
            RemoteFileFormFactory::config()->set($key, $value);
{'fraction_non_alphanumeric': 0.14147069802164988, 'fraction_numerical': 0.010638297872340425, 'mean_word_length': 1.7353241449719246, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 75, 'https://': 6, 'lorem ipsum': 0, 'www.': 36, 'xml': 0}, 'pii_count': 4, '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': '2226681', 'n_tokens_mistral': 3119, 'n_tokens_neox': 2960, 'n_words': 510}