<?php
/************************************************************************
 * This file is part of EspoCRM.
 *
 * EspoCRM - Open Source CRM application.
 * Copyright (C) 2014-2015 Yuri Kuznetsov, Taras Machyshyn, Oleksiy Avramenko
 * Website: http://www.espocrm.com
 *
 * EspoCRM is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * EspoCRM is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with EspoCRM. If not, see http://www.gnu.org/licenses/.
 *
 * The interactive user interfaces in modified source and object code versions
 * of this program must display Appropriate Legal Notices, as required under
 * Section 5 of the GNU General Public License version 3.
 *
 * In accordance with Section 7(b) of the GNU General Public License version 3,
 * these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
 ************************************************************************/

namespace tests\Espo\Core\Utils;

use Espo\Core\Utils\Util;

class UtilTest extends \PHPUnit_Framework_TestCase
{
    public function testGetSeparator()
    {
        $this->assertEquals(DIRECTORY_SEPARATOR, Util::getSeparator());
    }

    public function testToCamelCase()
    {
        $this->assertEquals('detail', Util::toCamelCase('detail'));
        $this->assertEquals('detailView', Util::toCamelCase('detail-view', '-'));
        $this->assertEquals('myDetailView', Util::toCamelCase('my_detail_view'));

        $input = array(
            'detail',
            'my_detail_view',
        );
        $result = array(
            'detail',
            'myDetailView',
        );
        $this->assertEquals($result, Util::toCamelCase($input));
    }

    public function testFromCamelCase()
    {
        $this->assertEquals('detail', Util::fromCamelCase('detail'));
        $this->assertEquals('detail-view', Util::fromCamelCase('detailView', '-'));
        $this->assertEquals('my_detail_view', Util::fromCamelCase('myDetailView'));
        $this->assertEquals('my_f_f', Util::fromCamelCase('myFF'));

        $input = array(
            'detail',
            'myDetailView',
            'myFF',
        );
        $result = array(
            'detail',
            'my_detail_view',
            'my_f_f',
        );
        $this->assertEquals($result, Util::fromCamelCase($input));
    }

    public function testToUnderScore()
    {
        $this->assertEquals('detail', Util::toUnderScore('detail'));
        $this->assertEquals('detail_view', Util::toUnderScore('detailView'));
        $this->assertEquals('my_detail_view', Util::toUnderScore('myDetailView'));
        $this->assertEquals('my_f_f', Util::toUnderScore('myFF'));

        $input = array(
            'detail',
            'detailView',
            'myDetailView',
            'myFF',
        );
        $result = array(
            'detail',
            'detail_view',
            'my_detail_view',
            'my_f_f',
        );
        $this->assertEquals($result, Util::toUnderScore($input));
    }

    public function testMerge2()
    {
        $d1 = array(
          'hello' => 'world',
          'man' => array(
            'test' => [
              0 => ['name' => 'test 1'],
              1 => ['name' => 'test 2']
            ]
          )
        );
        $d2 = array(
          'test' => []
        );
        $d3 = array(
          'man' => array(
            'test' => [
              0 => '__APPEND__',
              1 => ['name' => 'test 3']
            ]
          )
        );
        $expected = array(
            'test' => [],
            'hello' => 'world',
            'man' => array(
              'test' => [
                0 => ['name' => 'test 1'],
                1 => ['name' => 'test 2'],
                2 => ['name' => 'test 3']
              ]
            )
        );

        $result = Util::merge(Util::merge($d2, $d1), $d3);
        $this->assertEquals($expected, $result);
    }

    public function testMerge()
    {
        $array1= array(
            'defaultPermissions',
            'logger',
            'devMode'
        );
        $array2Main= array(
            45 => '125',
            'sub' =>  array (
                'subV' => '125',
            ),
        );
        $result= array(
            'defaultPermissions',
            'logger',
            'devMode',
            45 => '125',
            'sub' =>  array (
                'subV' => '125',
            ),
        );
        $this->assertEquals($result, Util::merge($array1, $array2Main));


        $array1= array(
            'datetime' =>
              array (
                'dateFormat' => 'Y-m-d',
                'timeFormat' => 'H:i:s',
              ),
        );
        $array2Main= array(
            'datetime' =>
              array (
                'dateFormat' => 'MyDateFormat',
              ),
        );
        $result= array(
            'datetime' =>
              array (
                'dateFormat' => 'MyDateFormat',
                'timeFormat' => 'H:i:s',
              ),
        );
        $this->assertEquals($result, Util::merge($array1, $array2Main));


        $array1= array(
            'database' =>
              array (
                'driver' => 'pdo_mysql',
                'host' => 'localhost',
                'dbname' => 'espocrm',
                'user' => 'root',
                'password' => '',
              ),
        );
        $array2Main= array(
            'database' =>
              array (
                'password' => 'MyPass',
              ),
        );
        $result= array(
            'database' =>
              array (
                'driver' => 'pdo_mysql',
                'host' => 'localhost',
                'dbname' => 'espocrm',
                'user' => 'root',
                'password' => 'MyPass',
              ),
        );
        $this->assertEquals($result, Util::merge($array1, $array2Main));
    }

    public function testMergeWithAppend()
    {
        $currentArray = array(
            'entityDefs' =>
              array (
                'Attachment' =>
                array (
                  'fields' =>
                  array (
                    'name' =>
                    array (
                      'type' => 'varchar',
                      'required' => true,
                    ),
                    'type' =>
                    array (
                      'type' => 'varchar',
                      'maxLength' => 36,
                    ),
                    'size' =>
                    array (
                      'type' => 'enum',
                      'value' => ["v1", "v2", "v3"],
                    ),
                    'sizeInt' =>
                    array (
                      'type' => 'enum',
                      'value' => [0, 1, 2],
                    ),
                    'merged' =>
                    array (
                      'type' => 'enum',
                      'value' => ["v1", "v2", "v3"],
                    ),
                    'mergedInt' =>
                    array (
                      'type' => 'enum',
                      'value' => [0, 1, 2],
                    ),
                  ),
                ),
                'Contact' =>
                array (
                  'fields' =>
                  array (
                    'name' =>
                    array (
                      'type' => 'varchar',
                      'required' => true,
                    ),
                    'type' =>
                    array (
                      'type' => 'varchar',
                      'maxLength' => 36,
                    ),
                    'size' =>
                    array (
                      'type' => 'enum',
                      'value' => ["v1", "v2", "v3"],
                    ),
                    'merged' =>
                    array (
                      'type' => 'enum',
                      'value' => ["v1", "v2", "v3"],
                    ),
                  ),
                ),
            ),
        'MyCustom' =>
            array (
              'fields' =>
              array (
                'name' =>
                array (
                  'type' => 'varchar',
                  'required' => true,
                ),
              ),
            ),
        );

        $newArray = array(
            'entityDefs' =>
              array (
                'Attachment' =>
                array (
                  'fields' =>
                  array (
                    'name' =>
                    array (
                      'type' => 'varchar',
                      'required' => false,
                      'NEW' => 'NEWVAL',
                    ),
                    'type' =>
                    array (
                      'type' => 'NETYPE',
                    ),
                    'size' =>
                    array (
                      'type' => 'enum',
                      'value' => ["B1", "B2", "B3"],
                    ),
                    'sizeInt' =>
                    array (
                      'type' => 'enum',
                      'value' => [5, 8, 9],
                    ),
                    'merged' =>
                    array (
                      'type' => 'enum',
                      'value' => ["__APPEND__", "B1", "B2", "B3"],
                    ),
                    'mergedInt' =>
                    array (
                      'type' => 'enum',
                      'value' => ['__APPEND__', 5, 8, 9],
                    ),
                  ),
                  'list' =>
                  array (
                    'test' => 'Here',
                  ),
                ),
                'Contact' =>
                array (
                  'fields' =>
                  array (
                    'name' =>
                    array (
                      'type' => 'varchar',
                      'required' => false,
                      'NEW' => 'NEWVAL',
                    ),
                    'type' =>
                    array (
                      'type' => 'NEW',
                      'maxLength' => 1000000,
                    ),
                    'size' =>
                    array (
                      'type' => 'enum',
                      'value' => ["B1", "B2", "B3"],
                    ),
                    'merged' =>
                    array (
                      'type' => 'enum',
                      'value' => ["__APPEND__", "B1", "B2", "B3"],
                    ),
                  ),
                ),
            ),
        );


        $result = array (
          'entityDefs' =>
          array (
            'Attachment' =>
            array (
              'fields' =>
              array (
                'name' =>
                array (
                  'type' => 'varchar',
                  'required' => false,
                  'NEW' => 'NEWVAL',
                ),
                'type' =>
                array (
                  'type' => 'NETYPE',
                  'maxLength' => 36,
                ),
                'size' =>
                array (
                  'type' => 'enum',
                  'value' =>
                  array (
                    0 => 'B1',
                    1 => 'B2',
                    2 => 'B3',
                  ),
                ),
                'sizeInt' =>
                array (
                  'type' => 'enum',
                  'value' =>
                  array (
                    0 => 5,
                    1 => 8,
                    2 => 9,
                  ),
                ),
                'merged' =>
                array (
                  'type' => 'enum',
                  'value' =>
                  array (
                    0 => 'v1',
                    1 => 'v2',
                    2 => 'v3',
                    3 => 'B1',
                    4 => 'B2',
                    5 => 'B3',
                  ),
                ),
                'mergedInt' =>
                array (
                  'type' => 'enum',
                  'value' =>
                  array (
                    0 => 0,
                    1 => 1,
                    2 => 2,
                    3 => 5,
                    4 => 8,
                    5 => 9,
                  ),
                ),
              ),
              'list' =>
              array (
                'test' => 'Here',
              ),
            ),
            'Contact' =>
            array (
              'fields' =>
              array (
                'name' =>
                array (
                  'type' => 'varchar',
                  'required' => false,
                  'NEW' => 'NEWVAL',
                ),
                'type' =>
                array (
                  'type' => 'NEW',
                  'maxLength' => 1000000,
                ),
                'size' =>
                array (
                  'type' => 'enum',
                  'value' =>
                  array (
                    0 => 'B1',
                    1 => 'B2',
                    2 => 'B3',
                  ),
                ),
                'merged' =>
                array (
                  'type' => 'enum',
                  'value' =>
                  array (
                    0 => 'v1',
                    1 => 'v2',
                    2 => 'v3',
                    3 => 'B1',
                    4 => 'B2',
                    5 => 'B3',
                  ),
                ),
              ),
            ),
          ),
          'MyCustom' =>
          array (
            'fields' =>
            array (
              'name' =>
              array (
                'type' => 'varchar',
                'required' => true,
              ),
            ),
          ),
        );

        $this->assertEquals($result, Util::merge($currentArray, $newArray));
    }

    public function testMergeWithAppend2()
    {
        $currentArray = json_decode('{
         "controller": "Controllers.Record",
         "boolFilterList": ["onlyMy"],
         "sidePanels":{
            "detail":[
               {
                  "name":"optedOut",
                  "label":"Opted Out",
                  "view":"Crm:TargetList.Record.Panels.OptedOut"
               }
            ]
         }
        }', true);

        $newArray = json_decode('{
          "views":{
               "detail":"Advanced:TargetList.Detail"
           },
          "recordViews": {
            "detail": "Advanced:TargetList.Record.Detail"
          },
          "sidePanels": {
            "detail": [
              "__APPEND__",
                {
                   "name":"populating",
                   "label":"Populating",
                   "view":"Advanced:TargetList.Record.Panels.Populating"
                }
            ],
            "edit": [
              "__APPEND__",
                {
                   "name":"populating",
                   "label":"Populating",
                   "view":"Advanced:TargetList.Record.Panels.Populating"
                }
            ]
          }
        }', true);

        $result = json_decode('{
          "controller": "Controllers.Record",
          "boolFilterList": [
            "onlyMy"
          ],
          "sidePanels": {
            "detail": [
              {
                "name": "optedOut",
                "label": "Opted Out",
                "view": "Crm:TargetList.Record.Panels.OptedOut"
              },
              {
                "name": "populating",
                "label": "Populating",
                "view": "Advanced:TargetList.Record.Panels.Populating"
              }
            ],
            "edit": [
              {
                "name": "populating",
                "label": "Populating",
                "view": "Advanced:TargetList.Record.Panels.Populating"
              }
            ]
          },
          "views": {
            "detail": "Advanced:TargetList.Detail"
          },
          "recordViews": {
            "detail": "Advanced:TargetList.Record.Detail"
          }
        }', true);

        $this->assertEquals($result, Util::merge($currentArray, $newArray));
    }

    public function testMergeWithBool()
    {
        $currentArray = array (
          'fields' =>
          array (
            'accountId' =>
            array (
              'type' => 'varchar',
              'where' =>
              array (
                '=' => 'contact.id IN ({value})',
              ),
              'len' => 255,
            ),
            'deleted' =>
            array (
              'type' => 'bool',
              'default' => false,
              'trueValue' => true,
            ),
          ),
          'relations' =>
          array (
          ),
        );

        $newArray = array (
          'fields' =>
          array (
            'accountName' =>
            array (
              'type' => 'foreign',
              'relation' => 'account',
              'foreign' => 'name',
            ),
            'accountId' =>
            array (
              'type' => 'foreignId',
              'index' => true,
            ),
          ),
          'relations' =>
          array (
            'createdBy' =>
            array (
              'type' => 'belongsTo',
              'entity' => 'User',
              'key' => 'createdById',
              'foreignKey' => 'id',
            ),
          ),
        );

        $result = array (
          'fields' =>
          array (
            'accountName' =>
            array (
              'type' => 'foreign',
              'relation' => 'account',
              'foreign' => 'name',
            ),
            'accountId' =>
            array (
              'type' => 'foreignId',
              'index' => true,
              'where' =>
              array (
                '=' => 'contact.id IN ({value})',
              ),
              'len' => 255,
            ),
            'deleted' =>
            array (
              'type' => 'bool',
              'default' => false,
              'trueValue' => true,
            ),
          ),
          'relations' =>
          array (
            'createdBy' =>
            array (
              'type' => 'belongsTo',
              'entity' => 'User',
              'key' => 'createdById',
              'foreignKey' => 'id',
            ),
          ),
        );

        $this->assertEquals($result, Util::merge($currentArray, $newArray));
    }

    public function testMergeWithFieldsDefs()
    {
        $currentArray = array (
          'fields' =>
          array (
            'aaa1' =>
            array (
              'type' => 'enum',
              'required' => false,
              'options' =>
              array (
                0 => 'a1',
                1 => 'a3',
                2 => 'a3',
              ),
              'isCustom' => true,
            ),
            'hfghgfh' =>
            array (
              'type' => 'varchar',
              'required' => false,
              'isCustom' => true,
              'default' => 'hfghfgh',
            ),
            'jghjghj' =>
            array (
              'type' => 'varchar',
              'required' => false,
              'isCustom' => true,
              'default' => 'jghjghjhg',
            ),
            'gdfgdfg' =>
            array (
              'type' => 'varchar',
              'required' => false,
              'isCustom' => true,
              'default' => 'gdfgdfg',
              'maxLength' => 70,
            ),
          ),
        );

        $newArray = array (
          'fields' =>
          array (
            'aaa1' =>
            array (
              'type' => 'enum',
              'required' => false,
              'options' =>
              array (
                0 => 'a1',
              ),
              'isCustom' => true,
            ),
          ),
        );

        $result = array (
          'fields' =>
          array (
            'aaa1' =>
            array (
              'type' => 'enum',
              'required' => false,
              'options' =>
              array (
                0 => 'a1',
              ),
              'isCustom' => true,
            ),
            'hfghgfh' =>
            array (
              'type' => 'varchar',
              'required' => false,
              'isCustom' => true,
              'default' => 'hfghfgh',
            ),
            'jghjghj' =>
            array (
              'type' => 'varchar',
              'required' => false,
              'isCustom' => true,
              'default' => 'jghjghjhg',
            ),
            'gdfgdfg' =>
            array (
              'type' => 'varchar',
              'required' => false,
              'isCustom' => true,
              'default' => 'gdfgdfg',
              'maxLength' => 70,
            ),
          ),
        );

        $this->assertEquals($result, Util::merge($currentArray, $newArray));
    }

    public function testMergeEmptyArray()
    {
        $currentArray = array(
          'Call' =>array (
            'fields' =>
            array (
              'accountId' =>
              array (
                'type' => 'varchar',
                'where' =>
                array (
                  '=' => 'contact.id IN ({value})',
                ),
                'len' => 255,
              ),
              'deleted' =>
              array (
                'type' => 'bool',
                'default' => false,
                'trueValue' => true,
              ),
            ),
          ),
        );

        $newArray = array(
          'Call' =>array (
            'fields' =>
            array (
            ),
          ),
        );

        $result = $currentArray;

        $this->assertEquals($result, Util::merge($currentArray, $newArray));
    }

    public function testMergeEmptyArray2()
    {
        $currentArray = array(
          'Call' => array (
            'fields' =>
            array (
              'accountId' =>
              array (
                'type' => 'varchar',
                'where' =>
                array (
                  '=' => 'contact.id IN ({value})',
                ),
                'len' => 255,
              ),
              'deleted' =>
              array (
                'type' => 'bool',
                'default' => false,
                'trueValue' => true,
              ),
            ),
          ),
        );

        $newArray = array(
          'Call' => array (),
        );

        $result = $currentArray;

        $this->assertEquals($result, Util::merge($currentArray, $newArray));
    }

    public function testMergeEmptyArray3()
    {
        $currentArray = array(
          'Call' =>array (
            'fields' =>
            array (
              'accountId' =>
              array (
                'type' => 'varchar',
                'where' =>
                array (
                  '=' => 'contact.id IN ({value})',
                ),
                'len' => 255,
              ),
              'deleted' =>
              array (
                'type' => 'bool',
                'default' => false,
                'trueValue' => true,
              ),
            ),
          ),
        );

        $newArray = array(
        );

        $result = $currentArray;

        $this->assertEquals($result, Util::merge($currentArray, $newArray));
    }

    public function testMergeCompleteTest()
    {
        $currentArray = array (
            'fields' =>
            array (
                'aaa1' =>
                array (
                    'type' => 'enum',
                    'required' => false,
                    'options' =>
                    array (
                        0 => 'a1',
                        1 => 'a3',
                        2 => 'a3',
                    ),
                    'isCustom' => true,
                ),
                'append' =>
                array (
                    'type' => 'enum',
                    'required' => false,
                    'options' =>
                    array (
                        'b1',
                        'b3',
                        'b3',
                    ),
                ),
                't1111' =>
                array (
                    'type' => 'varchar',
                    'required' => false,
                    'isCustom' => true,
                    'default' => '11111',
                ),
                't2222' =>
                array (
                    'type' => 'varchar',
                    'required' => false,
                    'isCustom' => true,
                    'default' => '2222',
                ),
                't3333' =>
                array (
                    'type' => 'varchar',
                    'required' => false,
                    'isCustom' => true,
                    'default' => '3333',
                    'maxLength' => 70,
                ),
            ),
        );

        $newArray = array (
            'fields' =>
            array (
                'aaa1' =>
                array (
                    'type' => 'enum',
                    'required' => false,
                    'options' =>
                    array (
                        'a1',
                    ),
                    'isCustom' => false,
                    'newValue' => 'NNNNN',
                ),
                'new111' =>
                array (
                    'type' => 'varchar',
                    'required' => false,
                ),
                'append' =>
                array (
                    'type' => 'enum',
                    'required' => false,
                    'options' =>
                    array (
                        '__APPEND__',
                        'b4',
                        'b5',
                    ),
                ),
                'aloneAppend' =>
                array (
                    'type' => 'enum',
                    'required' => false,
                    'options' =>
                    array (
                        '__APPEND__',
                        'c1',
                        'c2',
                    ),
                ),
            ),
        );

        $result = array (
            'fields' =>
            array (
                'aaa1' =>
                array (
                    'type' => 'enum',
                    'required' => false,
                    'options' =>
                    array (
                        0 => 'a1',
                    ),
                    'isCustom' => false,
                    'newValue' => 'NNNNN',
                ),
                'append' =>
                array (
                    'type' => 'enum',
                    'required' => false,
                    'options' =>
                    array (
                        'b1',
                        'b3',
                        'b3',
                        'b4',
                        'b5',
                    ),
                ),
                't1111' =>
                array (
                    'type' => 'varchar',
                    'required' => false,
                    'isCustom' => true,
                    'default' => '11111',
                ),
                't2222' =>
                array (
                    'type' => 'varchar',
                    'required' => false,
                    'isCustom' => true,
                    'default' => '2222',
                ),
                't3333' =>
                array (
                    'type' => 'varchar',
                    'required' => false,
                    'isCustom' => true,
                    'default' => '3333',
                    'maxLength' => 70,
                ),
                'new111' =>
                array (
                    'type' => 'varchar',
                    'required' => false,
                ),
                'aloneAppend' =>
                array (
                    'type' => 'enum',
                    'required' => false,
                    'options' =>
                    array (
                        'c1',
                        'c2',
                    ),
                ),
            ),
        );

        $this->assertEquals($result, Util::merge($currentArray, $newArray));
    }

    public function testToFormat()
    {
       $this->assertEquals('/Espo/Core/Utils', Util::toFormat('/Espo/Core/Utils', '/'));
       $this->assertEquals('\Espo\Core\Utils', Util::toFormat('/Espo/Core/Utils', '\\'));

       $this->assertEquals('/Espo/Core/Utils', Util::toFormat('\Espo\Core\Utils', '/'));
       $this->assertEquals('\Espo\Core\Utils', Util::toFormat('\Espo\Core\Utils', '\\'));
    }

    public function testConcatPath()
    {
        $result= Util::fixPath('dir1/dir2/file1.json');
        $this->assertEquals($result, Util::concatPath('dir1/dir2', 'file1.json'));

        $result= Util::fixPath('dir1/dir2/file1.json');
        $this->assertEquals($result, Util::concatPath('dir1/dir2/', 'file1.json'));

        $result= Util::fixPath('dir1/dir2/file1.json');
        $this->assertEquals($result, Util::concatPath('dir1/dir2/file1.json'));

        $input = array('dir1/dir2', 'file1.json');
        $result= Util::fixPath('dir1/dir2/file1.json');
        $this->assertEquals($result, Util::concatPath($input));

        $input = array('dir1/', 'dir2', 'file1.json');
        $result = Util::fixPath('dir1/dir2/file1.json');
        $this->assertEquals($result, Util::concatPath($input));
    }

    public function testArrayToObject()
    {
        $testArr= array(
            'useCache' => true,
            'sub' =>  array (
                'subV' => '125',
                'subO' => array(
                    'subOV' => '125',
                ),
            ),
        );

        $testResult= (object) array(
            'useCache' => true,
        );
        $testResult->sub = (object) array (
                'subV' => '125',
        );
        $testResult->sub->subO = (object) array (
                'subOV' => '125',
        );

        $this->assertEquals($testResult, Util::arrayToObject($testArr));
    }

    public function testObjectToArray()
    {
        $testObj= (object) array(
            'useCache' => true,
        );
        $testObj->sub = (object) array (
                'subV' => '125',
        );
        $testObj->sub->subO = (object) array (
                'subOV' => '125',
        );

        $testResult= array(
            'useCache' => true,
            'sub' =>  array (
                'subV' => '125',
                'subO' => array(
                    'subOV' => '125',
                ),
            ),
        );

        $this->assertEquals($testResult, Util::objectToArray($testObj));
    }

    public function testGetNaming()
    {
        $this->assertEquals('myPrefixMyName', Util::getNaming('myName', 'myPrefix', 'prefix'));

        $this->assertEquals('myNameMyPostfix', Util::getNaming('myName', 'myPostfix', 'postfix'));
        $this->assertEquals('myNameMyPostfix', Util::getNaming('my_name', 'myPostfix', 'postfix', '_'));
        $this->assertEquals('myNameMyPostfix', Util::getNaming('my_name', 'my_postfix', 'postfix', '_'));
    }

    public function testReplaceInArray()
    {
        $testArray = array(
            'option' => array(
                'default' => '{0}',
                 'testKey' => array(
                    '{0}' => 'testVal',
                 ),
            ),
        );

        $testResult = array(
            'option' => array(
                'default' => 'DONE',
                 'testKey' => array(
                    'DONE' => 'testVal',
                 ),
            ),
        );

        $this->assertEquals($testResult, Util::replaceInArray('{0}', 'DONE', $testArray, true));
    }


    public function testGetClassName()
    {
        $this->assertEquals('\Espo\EntryPoints\Donwload', Util::getClassName('application/Espo/EntryPoints/Donwload.php'));
        $this->assertEquals('\Espo\EntryPoints\Donwload', Util::getClassName('custom/Espo/EntryPoints/Donwload.php'));
        $this->assertEquals('\Espo\EntryPoints\Donwload', Util::getClassName('Espo/EntryPoints/Donwload.php'));
        $this->assertEquals('\Espo\EntryPoints\Donwload', Util::getClassName('application/Espo/EntryPoints/Donwload'));
    }

    public function testUnsetInArrayNotSingle()
    {
        $input = array(
            'Account' => array(
                'useCache' => true,
                'sub' =>  array (
                    'subV' => '125',
                    'subO' => array(
                        'subOV' => '125',
                        'subOV2' => '125',
                    ),
                ),
            ),
        );

        $unsets = array(
            'Account' => array(
                'sub.subO.subOV', 'sub.subV',
            ),
        );

        $result = array(
            'Account' => array(
                'useCache' => true,
                'sub' =>  array (
                    'subO' => array(
                        'subOV2' => '125',
                    ),
                ),
            ),
        );

        $this->assertEquals($result, Util::unsetInArray($input, $unsets));
    }

    public function testUnsetInArraySingle()
    {
        $input = array(
            'Account' => array(
                'useCache' => true,
                'sub' =>  array (
                    'subV' => '125',
                    'subO' => array(
                        'subOV' => '125',
                        'subOV2' => '125',
                    ),
                ),
            ),
        );

        $unsets = array(
            'Account.sub.subO.subOV', 'Account.sub.subV',
        );

        $result = array(
            'Account' => array(
                'useCache' => true,
                'sub' =>  array (
                    'subO' => array(
                        'subOV2' => '125',
                    ),
                ),
            ),
        );

        $this->assertEquals($result, Util::unsetInArray($input, $unsets));
    }

    public function testUnsetInArrayTogether()
    {
        $input = array(
            'Account' => array(
                'useCache' => true,
                'sub' =>  array (
                    'subV' => '125',
                    'subO' => array(
                        'subOV' => '125',
                        'subOV2' => '125',
                    ),
                ),
            ),
        );

        $unsets = array(
            'Account' => array(
                'sub.subO.subOV',
            ),
            'Account.sub.subV',
        );

        $result = array(
            'Account' => array(
                'useCache' => true,
                'sub' =>  array (
                    'subO' => array(
                        'subOV2' => '125',
                    ),
                ),
            ),
        );

        $this->assertEquals($result, Util::unsetInArray($input, $unsets));
    }

    public function testUnsetInArray()
    {
        $input = array(
            'Account' => array(
                'useCache' => true,
                'sub' =>  array (
                    'subV' => '125',
                    'subO' => array(
                        'subOV' => '125',
                        'subOV2' => '125',
                    ),
                ),
            ),
            'Contact' => array(
                'useCache' => true,
            ),
        );

        $unsets = array(
            'Account',
        );

        $result = array(
            'Contact' => array(
                'useCache' => true,
            ),
        );

        $this->assertEquals($result, Util::unsetInArray($input, $unsets));
    }

    public function testUnsetInArrayByString()
    {
        $input = array(
            'Account' => array(
                'useCache' => true,
            ),
            'Contact' => array(
                'useCache' => true,
            ),
        );

        $unsets = 'Account.useCache';

        $result = array(
            'Account' => array(
            ),
            'Contact' => array(
                'useCache' => true,
            ),
        );

        $this->assertEquals($result, Util::unsetInArray($input, $unsets));
    }

    public function testUnsetInArrayEmptyParent()
    {
        $input = array(
            'Account' => array(
                'useCache' => true,
                'sub' =>  array (
                    'subV' => '125',
                    'subO' => array(
                        'subOV' => '125',
                        'subOV2' => '125',
                    ),
                ),
            ),
            'Contact' => array(
                'useCache' => true,
            ),
            'Lead' => array(
                'useCache' => true,
            )
        );

        $unsets = array(
            'Account.useCache',
            'Account.sub',
            'Lead.useCache'
        );

        $result = array(
            'Contact' => array(
                'useCache' => true,
            )
        );

        $this->assertEquals($result, Util::unsetInArray($input, $unsets, true));
    }

    public function testGetValueByKey()
    {
        $inputArray = array(
            'Account' => array(
                'useCache' => true,
                'sub' =>  array (
                    'subV' => '125',
                    'subO' => array(
                        'subOV' => '125',
                        'subOV2' => '125',
                    ),
                ),
            ),
            'Contact' => array(
                'useCache' => true,
            ),
        );


        $this->assertEquals($inputArray, Util::getValueByKey($inputArray));
        $this->assertEquals($inputArray, Util::getValueByKey($inputArray, ''));

        $this->assertEquals('125', Util::getValueByKey($inputArray, 'Account.sub.subV'));

        $result = array('useCache' => true,    );
        $this->assertEquals($result, Util::getValueByKey($inputArray, 'Contact'));

        $this->assertNull(Util::getValueByKey($inputArray, 'Contact.notExists'));

        $this->assertEquals('customReturns', Util::getValueByKey($inputArray, 'Contact.notExists', 'customReturns'));
        $this->assertNotEquals('customReturns', Util::getValueByKey($inputArray, 'Contact.useCache', 'customReturns'));
    }

    public function testGetValueByKey2()
    {
        $inputArray = array(
            'fields' => array(
                'varchar' => array (
                      'params' =>
                      array (
                        array (
                          'name' => 'required',
                          'type' => 'bool',
                          'default' => false,
                        ),
                        array (
                          'name' => 'default',
                          'type' => 'varchar',
                        ),
                        array (
                          'name' => 'maxLength',
                          'type' => 'int',
                        ),
                        array (
                          'name' => 'trim',
                          'type' => 'bool',
                          'default' => true,
                        ),
                        array (
                          'name' => 'audited',
                          'type' => 'bool',
                        ),
                      ),
                      'filter' => true,
                ),
            )
        );

        $this->assertNull(Util::getValueByKey($inputArray, 'fields.varchar.hookClassName'));
        $this->assertNull(Util::getValueByKey($inputArray, ['fields', 'varchar', 'hookClassName']));
        $this->assertEquals('customReturns', Util::getValueByKey($inputArray, 'Contact.notExists', 'customReturns'));
    }

    public function testUnsetInArrayByValue()
    {
        $newArray = json_decode('[
          "__APPEND__",
            {
               "name":"populating",
               "label":"Populating",
               "view":"Advanced:TargetList.Record.Panels.Populating"
            }
        ]', true);

        $result = json_decode('[
            {
               "name":"populating",
               "label":"Populating",
               "view":"Advanced:TargetList.Record.Panels.Populating"
            }
        ]', true);

        $this->assertEquals($result, Util::unsetInArrayByValue('__APPEND__', $newArray));
    }

    public function testUnsetInArrayByValueWithoutReindex()
    {
        $newArray = json_decode('[
          "__APPEND__",
            {
               "name":"populating",
               "label":"Populating",
               "view":"Advanced:TargetList.Record.Panels.Populating"
            }
        ]', true);

        $result = json_decode('{
          "1": {
            "name": "populating",
            "label": "Populating",
            "view": "Advanced:TargetList.Record.Panels.Populating"
          }
        }', true);

        $this->assertEquals($result, Util::unsetInArrayByValue('__APPEND__', $newArray, false));
    }
}

