<?php

namespace KupShop\AdminBundle\Tests;

use KupShop\AdminBundle\Resources\script\ChangeURLScript;
use Query\Operator;

class ChangeURLTest extends \DatabaseTestCase
{
    /** @var ChangeURLScript */
    protected $changeURL;

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

        $this->changeURL = new ChangeURLScript();
    }

    public function testChangeToNewURLIDToZAndSaveInDatabase()
    {
        $this->changeURL->run(['tables' => ['blocks' => ['content']]]);
        $resultEx = '<p style="text-align:center;">V minulém roce představené jistítko <a href="https://www.kupshop.local/beal-jisteni-birdie_z3/"><strong>Beal Birdie</strong></a>';
        $result = sqlQueryBuilder()->select('content')
            ->from('blocks')
            ->where(Operator::equals(['id' => 1]))
            ->execute()->fetch();
        $this->assertEquals($resultEx, $result['content']);
    }

    public function testChangeToNewURLSectionWithHTTPS()
    {
        $text = 'Test content <a href="https://www.kupshop.local/panske-obleceni-16">';
        $result = $this->changeURL->changeURLInText($text);
        $resultEx = 'Test content <a href="https://www.kupshop.local/muzi/">';
        $this->assertEquals($resultEx, $result);
    }

    public function testChangeToNewURLSectionWithoutDomain()
    {
        $text = 'Test content <a href="/kratasy-a-3-4-kalhoty-58">';
        $result = $this->changeURL->changeURLInText($text);
        $resultEx = 'Test content <a href="/panske-kratasy/">';
        $this->assertEquals($resultEx, $result);
    }

    public function testChangeToNewURLSectionWithoutDomainNotExist()
    {
        $text = 'Test content <a href="/kratasy-a-3-4-kalhoty-58999">';
        $result = $this->changeURL->changeURLInText($text);
        $resultEx = 'Test content <a href="/kratasy-a-3-4-kalhoty-58999">';
        $this->assertEquals($resultEx, $result);
    }

    public function testChangeToNewURLSectionWithoutDomainNotExistAndNotSaveToDB()
    {
        $this->changeURL->run(['tables' => ['blocks' => ['content']]]);
        $resultEx = 'Test content <a href="/kratasy-a-3-4-kalhoty-58999">';
        $result = sqlQueryBuilder()->select('content')
            ->from('blocks')
            ->where(Operator::equals(['id' => 3]))
            ->execute()->fetch();
        $this->assertEquals($resultEx, $result['content']);
    }

    public function testChangeToNewURLSectionMultipleURLs()
    {
        $text = 'Test content <a href="https://www.kupshop.local/kratasy-a-3-4-kalhoty-58"> test dalsiho textu  <a href="https://www.kupshop.local/panske-obleceni-16">';
        $result = $this->changeURL->changeURLInText($text);
        $resultEx = 'Test content <a href="https://www.kupshop.local/panske-kratasy/"> test dalsiho textu  <a href="https://www.kupshop.local/muzi/">';
        $this->assertEquals($resultEx, $result);
    }

    public function testChangeToNewURLSectionJSONsaveToDB()
    {
        $this->changeURL->run(['tables' => ['blocks' => ['json_content']]]);
        $result = sqlQueryBuilder()->select('json_content')
            ->from('blocks')
            ->where(Operator::equals(['id' => 2]))
            ->execute()->fetch();
        $resultEx = '[{"type":"legacy","id":"511f33d5-7ab9-4332-85d7-0da723ed6240","settings":{"html":"Test content <a href="https://www.kupshop.local/panske-kratasy/">"}}]';
        $this->assertEquals($resultEx, $result['json_content']);
    }

    protected function getDataSet()
    {
        sqlQuery('DELETE FROM articles');

        return $this->getJsonDataSetFromFile();
    }
}
