<?php

declare(strict_types=1);

namespace KupShop\I18nBundle\Tests;

use KupShop\I18nBundle\Util\TranslationUtil;

class LinkTranslationTest extends \DatabaseTestCase
{
    private TranslationUtil $translationUtil;

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

        $this->translationUtil = $this->get(TranslationUtil::class);
    }

    public function data_getTranslateObjectLink(): array
    {
        return [
            ['cs', 'product', 100, '/damska-pletena-cepice-merino-kama-a119-bila_z100/'],
            ['sk', 'product', 100, '/damska-pletena-ciapka-merino-kama-a119-biela_z100/'],
            ['cs', 'article', 9, '/clanek/9/funkce-ovci-vlny/'],
            ['sk', 'article', 9, '/clanok/9/funkcie-ovcej-vlny/'],
        ];
    }

    /**
     * @dataProvider data_getTranslateObjectLink
     */
    public function testGetTranslateObjectLink(string $language, string $type, int $id, string $expected)
    {
        $this->assertEquals($expected, $this->translationUtil->translateLink($language, $type, $id));
    }

    public function testTranslateHTML()
    {
        $html = <<<__HTML__
        &nbsp;
        Lorem ipsum dolor sit amet, consectetuer adipiscing <b>elit.</b>
        <br>
        <hr />
        <i>Maecenas 
            <strong>sollicitudin. 
                <a href="/ tohl\eTuNebde" data-id="100" data-type="product">hasellus rhoncus.</a>
            </strong>
        <p>In sem justo, commodo ut, suscipit at, pharetra vitae, <a href="/lkasjdhaskjflhskjdfhdskfjg"
         data-type="article" data-id="9">orci.</a></p>
        __HTML__;

        $translatedCS = $this->translationUtil->translateLinksInHTML('cs', $html);

        $dom = new \DOMDocument();
        $dom->loadHTML($translatedCS);

        $this->assertEquals(
            '/damska-pletena-cepice-merino-kama-a119-bila_z100/',
            $dom->getElementsByTagName('a')->item(0)->getAttribute('href'),
        );

        $this->assertEquals(
            '/clanek/9/funkce-ovci-vlny/',
            $dom->getElementsByTagName('a')->item(1)->getAttribute('href'),
        );

        $translatedSK = $this->translationUtil->translateLinksInHTML('sk', $html);

        $dom = new \DOMDocument();
        $dom->loadHTML($translatedSK);

        $this->assertEquals(
            '/damska-pletena-ciapka-merino-kama-a119-biela_z100/',
            $dom->getElementsByTagName('a')->item(0)->getAttribute('href'),
        );

        $this->assertEquals(
            '/clanok/9/funkcie-ovcej-vlny/',
            $dom->getElementsByTagName('a')->item(1)->getAttribute('href'),
        );
    }

    protected function getDataSet()
    {
        return $this->getJsonDataSetFromFile();
    }
}
