<commit_before><?php

namespace Kajona\System\Tests;

use Kajona\System\System\Carrier;
use Kajona\System\System\Template;
use Kajona\System\System\Testbase;

/**
 * Class class_test_templateTest
 * @todo add test to parse the attributes themselves
 *
 */
class TemplateTest extends Testbase
{

    public function testBasicSectionParser()
    {
        $strTemplate = <<<HTML

        <list>
            <content>test</content> test %%ende%%
        </list>
HTML;

        /** @var Template $objTemplate */
        $objTemplate = Carrier::getInstance()->getContainer()->offsetGet("template");
        $strTemplateID = $objTemplate->setTemplate($strTemplate);

        $this->assertTrue($objTemplate->containsSection($strTemplateID, "list"));
        $this->assertTrue(!$objTemplate->containsSection($strTemplateID, "lista"));


        $this->assertEquals("<content>test</content> test %%ende%%", trim($objTemplate->getSectionFromTemplate($strTemplate, "list")));
        $this->assertEquals(trim($strTemplate), trim($objTemplate->getSectionFromTemplate($strTemplate, "list", true)));

        $strSectionID = $objTemplate->setTemplate($objTemplate->getSectionFromTemplate($strTemplate, "list"));
        $strFilled = trim($objTemplate->fillCurrentTemplate(array(), false));
        $this->assertEquals("<content>test</content> test %%ende%%", $strFilled);

        $strFilled = trim($objTemplate->fillTemplate(array(), $strSectionID, false));
        $this->assertEquals("<content>test</content> test %%ende%%", $strFilled);

        $objTemplate->setTemplate($objTemplate->getSectionFromTemplate($strTemplate, "list"));
        $strFilled = trim($objTemplate->fillCurrentTemplate(array(), true));
        $this->assertEquals("<content>test</content> test", $strFilled);

        $strFilled = trim($objTemplate->fillTemplate(array(), $strSectionID, true));
        $this->assertEquals("<content>test</content> test", $strFilled);
    }

    public function testEmptySectionParser()
    {
        $strTemplate = <<<HTML

        <list1></list1>

        <list2>content</list2>

        <list3></list3>
HTML;


        /** @var Template $objTemplate */
        $objTemplate = Carrier::getInstance()->getContainer()->offsetGet("template");
        $strTemplateID = $objTemplate->setTemplate($strTemplate);

        $this->assertTrue($objTemplate->containsSection($strTemplateID, "list1"));
        $this->assertTrue($objTemplate->containsSection($strTemplateID, "list2"));
        $this->assertTrue($objTemplate->containsSection($strTemplateID, "list3"));
        $this->assertTrue(!$objTemplate->containsSection($strTemplateID, "lista"));


        $this->assertEquals("", trim($objTemplate->getSectionFromTemplate($strTemplate, "list1")));
        $this->assertEquals("<list1></list1>", trim($objTemplate->getSectionFromTemplate($strTemplate, "list1", true)));

        $this->assertEquals("content", trim($objTemplate->getSectionFromTemplate($strTemplate, "list2")));
        $this->assertEquals("<list2>content</list2>", trim($objTemplate->getSectionFromTemplate($strTemplate, "list2", true)));

        $this->assertEquals("", trim($objTemplate->getSectionFromTemplate($strTemplate, "list3")));
        $this->assertEquals("<list3></list3>", trim($objTemplate->getSectionFromTemplate($strTemplate, "list3", true)));

    }


    public function testFillCurrentTemplate()
    {
        $strTemplate = <<<HTML
            test %%ende%%
HTML;

        /** @var Template $objTemplate */
        $objTemplate = Carrier::getInstance()->getContainer()->offsetGet("template");
        $objTemplate->setTemplate($strTemplate);
        $strContent = trim($objTemplate->fillCurrentTemplate(array("ende" => "filled")));
        $this->assertEquals("test filled", $strContent);
    }


    public function testSectionWithAttributesParser()
    {
        $strTemplate = <<<HTML

        <list attribute1="value1" attribute2="value2">
            <content>test</content> test %%ende%%
        </list>
HTML;


        /** @var Template $objTemplate */
        $objTemplate = Carrier::getInstance()->getContainer()->offsetGet("template");
        $strTemplateID = $objTemplate->setTemplate($strTemplate);

        $this->assertTrue($objTemplate->containsSection($strTemplateID, "list"));
        $this->assertTrue(!$objTemplate->containsSection($strTemplateID, "lista"));


        $strSectionID = $objTemplate->setTemplate($objTemplate->getSectionFromTemplate($strTemplate, "list"));
        $strFilled = trim($objTemplate->fillCurrentTemplate(array(), false));
        $this->assertEquals("<content>test</content> test %%ende%%", $strFilled);

        $strFilled = trim($objTemplate->fillTemplate(array(), $strSectionID, false));
        $this->assertEquals("<content>test</content> test %%ende%%", $strFilled);

        $objTemplate->setTemplate($objTemplate->getSectionFromTemplate($strTemplate, "list"));
        $strFilled = trim($objTemplate->fillCurrentTemplate(array(), true));
        $this->assertEquals("<content>test</content> test", $strFilled);

        $strFilled = trim($objTemplate->fillTemplate(array(), $strSectionID, true));
        $this->assertEquals("<content>test</content> test", $strFilled);
    }


    public function testRemoveSection()
    {
        $strTemplate1 = <<<HTML
a
<list attribute1="value1" attribute2="value2">
    <content>test</content> test %%ende%%
</list>
b
HTML;

        $strTemplate2 = <<<HTML
a
<list>
    <content>test</content> test %%ende%%
</list>
b
HTML;

        $strTemplate3 = <<<HTML
a
<list attribute1="value1" attribute2="value2">
    <content>test</content> test %%ende%%
</list>
b
<list attribute1="value1" attribute2="value2">
    <content>test</content> test %%ende%%
</list>
c
HTML;

        $objTemplate = Template::getInstance();
        $this->assertEquals(
            "a

b", $objTemplate->removeSection($strTemplate1, "list"));

        $this->assertEquals(
            "a

b", $objTemplate->removeSection($strTemplate2, "list"));

        $this->assertEquals(
            "a

b

c", $objTemplate->removeSection($strTemplate3, "list"));<commit_msg>BUG: system| TemplateTest.php -> replaced a legacy getInstance call<commit_after><?php

namespace Kajona\System\Tests;

use Kajona\System\System\Carrier;
use Kajona\System\System\Template;
use Kajona\System\System\Testbase;

/**
 * Class class_test_templateTest
 * @todo add test to parse the attributes themselves
 *
 */
class TemplateTest extends Testbase
{

    public function testBasicSectionParser()
    {
        $strTemplate = <<<HTML

        <list>
            <content>test</content> test %%ende%%
        </list>
HTML;

        /** @var Template $objTemplate */
        $objTemplate = Carrier::getInstance()->getContainer()->offsetGet("template");
        $strTemplateID = $objTemplate->setTemplate($strTemplate);

        $this->assertTrue($objTemplate->containsSection($strTemplateID, "list"));
        $this->assertTrue(!$objTemplate->containsSection($strTemplateID, "lista"));


        $this->assertEquals("<content>test</content> test %%ende%%", trim($objTemplate->getSectionFromTemplate($strTemplate, "list")));
        $this->assertEquals(trim($strTemplate), trim($objTemplate->getSectionFromTemplate($strTemplate, "list", true)));

        $strSectionID = $objTemplate->setTemplate($objTemplate->getSectionFromTemplate($strTemplate, "list"));
        $strFilled = trim($objTemplate->fillCurrentTemplate(array(), false));
        $this->assertEquals("<content>test</content> test %%ende%%", $strFilled);

        $strFilled = trim($objTemplate->fillTemplate(array(), $strSectionID, false));
        $this->assertEquals("<content>test</content> test %%ende%%", $strFilled);

        $objTemplate->setTemplate($objTemplate->getSectionFromTemplate($strTemplate, "list"));
        $strFilled = trim($objTemplate->fillCurrentTemplate(array(), true));
        $this->assertEquals("<content>test</content> test", $strFilled);

        $strFilled = trim($objTemplate->fillTemplate(array(), $strSectionID, true));
        $this->assertEquals("<content>test</content> test", $strFilled);
    }

    public function testEmptySectionParser()
    {
        $strTemplate = <<<HTML

        <list1></list1>

        <list2>content</list2>

        <list3></list3>
HTML;


        /** @var Template $objTemplate */
        $objTemplate = Carrier::getInstance()->getContainer()->offsetGet("template");
        $strTemplateID = $objTemplate->setTemplate($strTemplate);

        $this->assertTrue($objTemplate->containsSection($strTemplateID, "list1"));
        $this->assertTrue($objTemplate->containsSection($strTemplateID, "list2"));
        $this->assertTrue($objTemplate->containsSection($strTemplateID, "list3"));
        $this->assertTrue(!$objTemplate->containsSection($strTemplateID, "lista"));


        $this->assertEquals("", trim($objTemplate->getSectionFromTemplate($strTemplate, "list1")));
        $this->assertEquals("<list1></list1>", trim($objTemplate->getSectionFromTemplate($strTemplate, "list1", true)));

        $this->assertEquals("content", trim($objTemplate->getSectionFromTemplate($strTemplate, "list2")));
        $this->assertEquals("<list2>content</list2>", trim($objTemplate->getSectionFromTemplate($strTemplate, "list2", true)));

        $this->assertEquals("", trim($objTemplate->getSectionFromTemplate($strTemplate, "list3")));
        $this->assertEquals("<list3></list3>", trim($objTemplate->getSectionFromTemplate($strTemplate, "list3", true)));

    }


    public function testFillCurrentTemplate()
    {
        $strTemplate = <<<HTML
            test %%ende%%
HTML;

        /** @var Template $objTemplate */
        $objTemplate = Carrier::getInstance()->getContainer()->offsetGet("template");
        $objTemplate->setTemplate($strTemplate);
        $strContent = trim($objTemplate->fillCurrentTemplate(array("ende" => "filled")));
        $this->assertEquals("test filled", $strContent);
    }


    public function testSectionWithAttributesParser()
    {
        $strTemplate = <<<HTML

        <list attribute1="value1" attribute2="value2">
            <content>test</content> test %%ende%%
        </list>
HTML;


        /** @var Template $objTemplate */
        $objTemplate = Carrier::getInstance()->getContainer()->offsetGet("template");
        $strTemplateID = $objTemplate->setTemplate($strTemplate);

        $this->assertTrue($objTemplate->containsSection($strTemplateID, "list"));
        $this->assertTrue(!$objTemplate->containsSection($strTemplateID, "lista"));


        $strSectionID = $objTemplate->setTemplate($objTemplate->getSectionFromTemplate($strTemplate, "list"));
        $strFilled = trim($objTemplate->fillCurrentTemplate(array(), false));
        $this->assertEquals("<content>test</content> test %%ende%%", $strFilled);

        $strFilled = trim($objTemplate->fillTemplate(array(), $strSectionID, false));
        $this->assertEquals("<content>test</content> test %%ende%%", $strFilled);

        $objTemplate->setTemplate($objTemplate->getSectionFromTemplate($strTemplate, "list"));
        $strFilled = trim($objTemplate->fillCurrentTemplate(array(), true));
        $this->assertEquals("<content>test</content> test", $strFilled);

        $strFilled = trim($objTemplate->fillTemplate(array(), $strSectionID, true));
        $this->assertEquals("<content>test</content> test", $strFilled);
    }


    public function testRemoveSection()
    {
        $strTemplate1 = <<<HTML
a
<list attribute1="value1" attribute2="value2">
    <content>test</content> test %%ende%%
</list>
b
HTML;

        $strTemplate2 = <<<HTML
a
<list>
    <content>test</content> test %%ende%%
</list>
b
HTML;

        $strTemplate3 = <<<HTML
a
<list attribute1="value1" attribute2="value2">
    <content>test</content> test %%ende%%
</list>
b
<list attribute1="value1" attribute2="value2">
    <content>test</content> test %%ende%%
</list>
c
HTML;

        $objTemplate = Carrier::getInstance()->getContainer()->offsetGet("template");
        $this->assertEquals(
            "a

b", $objTemplate->removeSection($strTemplate1, "list"));

        $this->assertEquals(
            "a

b", $objTemplate->removeSection($strTemplate2, "list"));

        $this->assertEquals(
            "a

b

c", $objTemplate->removeSection($strTemplate3, "list"));
{'fraction_non_alphanumeric': 0.1735987002437043, 'fraction_numerical': 0.006986190089358245, 'mean_word_length': 4.157519899455384, 'pattern_counts': {'":': 0, '<': 155, '<?xml version=': 0, '>': 268, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '6523062', 'n_tokens_mistral': 3430, 'n_tokens_neox': 3353, 'n_words': 550}