<?php
/**
 * baserCMS :  Based Website Development Project <https://basercms.net>
 * Copyright (c) baserCMS Users Community <https://basercms.net/community/>
 *
 * @copyright       Copyright (c) baserCMS Users Community
 * @link            https://basercms.net baserCMS Project
 * @since           baserCMS v 3.0.0
 * @license         https://basercms.net/license/index.html
 */

namespace BcWidgetArea\Test\TestCase\View\Helper;

use BaserCore\Test\Scenario\InitAppScenario;
use BaserCore\TestSuite\BcTestCase;
use BaserCore\View\BcFrontAppView;
use BaserCore\View\Helper\BcTextHelper;
use BcWidgetArea\Test\Factory\WidgetAreaFactory;
use BcWidgetArea\View\Helper\BcWidgetAreaHelper;
use Cake\View\View;
use CakephpFixtureFactories\Scenario\ScenarioAwareTrait;

/**
 * text helper library.
 *
 * @property BcTextHelper $Helper
 * @property BcWidgetAreaHelper $BcWidgetArea
 */
class BcWidgetAreaHelperTest extends BcTestCase
{
    /**
     * ScenarioAwareTrait
     */
    use ScenarioAwareTrait;

    public function setUp(): void
    {
        parent::setUp();
        $View = new View();
        $this->BcWidgetArea = new BcWidgetAreaHelper($View);
    }

    public function tearDown(): void
    {
        unset($this->BcWidgetArea);
        parent::tearDown();
    }

    /**
     * ウィジェットエリアを表示する
     *
     * @dataProvider showDataProvider
     *
     * @param $no ウィジェットエリアNO
     * @param array $options オプション
     * @param string $expected 期待値
     */
    public function testShow($fileName, $no, $expected)
    {
        $this->loadFixtureScenario(InitAppScenario::class);
        $this->BcWidgetArea = new BcWidgetAreaHelper(new BcFrontAppView($this->loginAdmin($this->getRequest())));
        WidgetAreaFactory::make([
            'id' => 1,
            'name' => '標準サイドバー',
            'widgets' => 'YTozOntpOjA7YToxOntzOjc6IldpZGdldDIiO2E6OTp7czoyOiJpZCI7czoxOiIyIjtzOjQ6InR5cGUiO3M6MzM6IuODreODvOOCq+ODq+ODiuODk+OCsuODvOOCt+ODp+ODsyI7czo3OiJlbGVtZW50IjtzOjEwOiJsb2NhbF9uYXZpIjtzOjY6InBsdWdpbiI7czo5OiJCYXNlckNvcm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jgZnjgII8L3NtYWxsPjwvcD4iO3M6OToidXNlX3RpdGxlIjtzOjE6IjEiO3M6Njoic3RhdHVzIjtzOjE6IjEiO319fQ=='
        ])->persist();

        ob_start();
        $this->BcWidgetArea->show($no);
        $result = ob_get_clean();

        $this->assertStringContainsString($expected, $result);
    }

    public static function showDataProvider()
    {
        return [
            ['test', 1, '<h2 class="bs-widget-head">サイト内検索</h2>'],
            ['test', 2, '']
        ];
    }

    /**
     * ウィジェットエリアを出力する
     *
     * $noを指定していない場合、ウィジェットが出力されません。
     *
     * @param string $url 現在のURL
     * @param int $no
     * @param string $expected 期待値
     * @dataProvider getWidgetAreaDataProvider
     * @TODO: $noが指定されてない(null)場合のテストを記述する
     */
    public function testGetWidgetArea($url, $no, $expected)
    {
        $view = new BcFrontAppView($this->getRequest($url));
        $view->set('currentWidgetAreaId', 1)->loadHelper('BcWidgetArea.BcWidgetArea');
        $this->assertMatchesRegularExpression('/' . $expected . '/', $view->BcWidgetArea->getWidgetArea($no));
    }

    public static function getWidgetAreaDataProvider()
    {
        return [
            ['/company', 1, '<div class="bs-widget-area bs-widget-area-1">'],
            ['/company', 2, '<div class="bs-widget-area bs-widget-area-2">'],
            ['/company', null, '<div class="bs-widget-area bs-widget-area-1">'],
        ];
    }

}
