<?php


namespace Aimeos\Aimeos\Tests\Unit\Scheduler\Provider;


use TYPO3\CMS\Scheduler\Controller\SchedulerModuleController;
use TYPO3\CMS\Scheduler\Task\Enumeration\Action;


class Typo6Test extends \TYPO3\CMS\Core\Tests\UnitTestCase
{
    private $object;


    public function setUp()
    {
        $this->object = new \Aimeos\Aimeos\Scheduler\Provider\Typo6();
    }


    public function tearDown()
    {
        unset($this->object);
    }


    /**
     * @test
     */
    public function getAdditionalFields()
    {
        $taskInfo = [];
        $module = new SchedulerModuleController();
        $module->setCurrentAction(new Action('EDIT'));

        $result = $this->object->getAdditionalFields($taskInfo, $this->object, $module);

        $this->assertInternalType('array', $result);
        $this->assertArrayHasKey('aimeos_controller', $result);
        $this->assertArrayHasKey('aimeos_sitecode', $result);
        $this->assertArrayHasKey('aimeos_config', $result);
    }


    /**
     * @test
     */
    public function getAdditionalFieldsException()
    {
        $taskInfo = [];
        $module = new SchedulerModuleController();
        $module->setCurrentAction(new Action('EDIT'));

        $mock = $this->getMockBuilder('\Aimeos\Aimeos\Scheduler\Provider\Typo6')
            ->setMethods(['getFields'])->getMock();

        $mock->expects($this->once())->method('getFields')
            ->will($this->throwException(new \RuntimeException()));

        $result = $mock->getAdditionalFields($taskInfo, $mock, $module);

        $this->assertEquals([], $result);
    }


    /**
     * @test
     */
    public function saveAdditionalFields()
    {
        $data = [
            'aimeos_sitecode' => 'testsite',
            'aimeos_controller' => 'testcntl',
            'aimeos_config' => 'testconf',
        ];
        $task = new \Aimeos\Aimeos\Scheduler\Task\Typo6();

        $this->object->saveAdditionalFields($data, $task);

        $this->assertEquals('testsite', $task->aimeos_sitecode);
        $this->assertEquals('testcntl', $task->aimeos_controller);
        $this->assertEquals('testconf', $task->aimeos_config);
    }


    /**
     * @test
     */
    public function validateAdditionalFieldsNoController()
    {
        $data = [];
        $module = new SchedulerModuleController();

        $this->assertFalse($this->object->validateAdditionalFields($data, $module));
    }


    /**
     * @test
     */
    public function validateAdditionalFieldsNoSite()
    {
        $data = [
            'aimeos_controller' => 'testcntl',
        ];
        $module = new SchedulerModuleController();

        $this->assertFalse($this->object->validateAdditionalFields($data, $module));
    }


    /**
     * @test
     */
    public function validateAdditionalFieldsNoSiteFound()
    {
        $data = [
            'aimeos_controller' => 'testcntl',
            'aimeos_sitecode' => 'testsite',
            'aimeos_config' => 'testconf',
        ];
        $module = new SchedulerModuleController();

        $this->assertFalse($this->object->validateAdditionalFields($data, $module));
    }


    /**
     * @test
     */
    public function validateAdditionalFields()
    {
        $data = [
            'aimeos_sitecode' => 'default',
            'aimeos_controller' => 'index/optimize',
        ];
        $module = new SchedulerModuleController();

        $this->assertTrue($this->object->validateAdditionalFields($data, $module));
    }
}
