<?php

use KupShop\CatalogBundle\Section\SectionTree;
use PHPUnit\DbUnit\DataSet\ArrayDataSet;

/**
 * Created by PhpStorm.
 * User: filip
 * Date: 6/6/16
 * Time: 11:33 AM.
 */
class MenuSectionTreeTest extends DatabaseTestCase
{
    private $catShowEmpty;

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

        global $dbcfg;
        $this->catShowEmpty = $dbcfg->cat_show_empty;
        $dbcfg->cat_show_empty = 'Y';
    }

    public function testNameShortOverridesName()
    {
        $result = $this->get(SectionTree::class)->getTree();

        $this->assertArrayHasKey(1, $result);
        $this->assertArrayHasKey(2, $result);

        $this->assertSame('Normal name', $result[1]['title']);
        $this->assertSame('Long name', $result[2]['title']);
        $this->assertSame('Normal name', $result[1]['title_short']);
        $this->assertSame('Short name', $result[2]['title_short']);
        $this->assertSame('Long name', $result[2]['parents'][2]);
        $this->assertTrue(isset($result[1]['flags']));
        $this->assertSame('1', $result[2]['flags']['L']);
    }

    public function testTranslationsVisibleSection()
    {
        $this->switchLanguage();

        $result = $this->get(SectionTree::class)->getTree();

        $this->assertArrayHasKey(2, $result);
        $this->assertEquals('Y', $result[2]['figure']);
    }

    public function testTranslationsHiddenSections()
    {
        $this->switchLanguage();

        $result = $this->get(SectionTree::class)->getTree();

        $this->assertArrayHasKey(1, $result);
        $this->assertEquals('N', $result[1]['figure']);
    }

    public function testMultipleLanguageSectionTree()
    {
        $sectionTree = $this->get(SectionTree::class);

        $this->assertEquals('Nářadí/Kleště', $sectionTree->getFullPath(4, 'cs'));

        $this->assertEquals('Náradie/Kliešte', $sectionTree->getFullPath(4, 'sk'));
    }

    public function getDataSet()
    {
        return new ArrayDataSet([
            'sections' => [
                [
                    'id' => 2,
                    'name' => 'Long name',
                    'name_short' => 'Short name',
                    'flags' => 'A,L',
                ],
                [
                    'id' => 1,
                    'name' => 'Normal name',
                    'flags' => '',
                ],
                [
                    'id' => 3,
                    'name' => 'Nářadí',
                    'name_short' => 'Short name',
                    'flags' => 'A,L',
                ],
                [
                    'id' => 4,
                    'name' => 'Kleště',
                    'name_short' => 'Short name',
                    'flags' => '',
                ],
            ],
            'sections_relation' => [
                [
                    'id_section' => 3,
                    'id_topsection' => null,
                    'position' => 1,
                ],
                [
                    'id_section' => 4,
                    'id_topsection' => 3,
                    'position' => 1,
                ],
            ],
            'sections_translations' => [
                [
                    'id' => 1,
                    'id_section' => 1,
                    'id_language' => 'sk',
                    'id_admin' => null,
                    'created' => '',
                    'updated' => null,
                    'name' => null,
                    'name_short' => null,
                    'meta_title' => null,
                    'meta_description' => null,
                    'meta_keywords' => null,
                    'lead_text' => null,
                    'url' => null,
                    'figure' => 'N',
                ],
                [
                    'id' => 2,
                    'id_section' => 2,
                    'id_language' => 'sk',
                    'id_admin' => null,
                    'created' => '',
                    'updated' => null,
                    'name' => null,
                    'name_short' => null,
                    'meta_title' => null,
                    'meta_description' => null,
                    'meta_keywords' => null,
                    'lead_text' => null,
                    'url' => null,
                    'figure' => 'Y',
                ],
                [
                    'id' => 3,
                    'id_section' => 3,
                    'id_language' => 'sk',
                    'id_admin' => null,
                    'created' => '',
                    'updated' => null,
                    'name' => 'Náradie',
                    'name_short' => null,
                    'meta_title' => null,
                    'meta_description' => null,
                    'meta_keywords' => null,
                    'lead_text' => null,
                    'url' => null,
                    'figure' => 'N',
                ],
                [
                    'id' => 4,
                    'id_section' => 4,
                    'id_language' => 'sk',
                    'id_admin' => null,
                    'created' => '',
                    'updated' => null,
                    'name' => 'Kliešte',
                    'name_short' => null,
                    'meta_title' => null,
                    'meta_description' => null,
                    'meta_keywords' => null,
                    'lead_text' => null,
                    'url' => null,
                    'figure' => 'Y',
                ],
            ],
        ]);
    }

    public function tearDown(): void
    {
        global $dbcfg;
        $dbcfg->cat_show_empty = $this->catShowEmpty;

        parent::tearDown();
    }
}
