<commit_before><?php
namespace Search\Utility;

use Cake\Event\EventManager;
use Cake\ORM\TableRegistry;
use Cake\TestSuite\TestCase;
use Search\Utility\BasicSearch;

/**
 * Search\Utility\BasicSearch Test Case
 */
class BasicSearchTest extends TestCase
{
    public $fixtures = [
        'plugin.search.dashboards',
        'plugin.roles_capabilities.roles'
    ];

    /**
     * setUp method
     *
     * @return void
     */
    public function setUp()
    {
        parent::setUp();

        EventManager::instance()->on('Search.Model.Search.searchabeFields', function ($event, $table) {
            $tableName = $table->getRegistryAlias();

            $result = [];
            switch ($tableName) {
                case 'Dashboards':
                    $result = [
                        'Dashboards.name' => ['type' => 'string', 'operators' => [
                            'contains' => ['label' => 'contains', 'operator' => 'LIKE', 'pattern' => '%{{value}}%', 'emptyCriteria' => [
                                'aggregator' => 'OR', 'values' => ['IS NULL', '= ""']
                            ]]
                        ]],
                        'Dashboards.image' => ['type' => 'blob', 'operators' => [
                            'contains' => ['label' => 'contains', 'operator' => 'LIKE', 'pattern' => '%{{value}}%', 'emptyCriteria' => [
                                'aggregator' => 'OR', 'values' => ['IS NULL', '= ""']
                            ]],
                        ]],
                        'Dashboards.role_id' => ['type' => 'related', 'source' => 'Roles', 'operators' => [
                            'is' => ['label' => 'is', 'operator' => 'IN', 'emptyCriteria' => [
                                'aggregator' => 'OR', 'values' => ['IS NULL', '= ""']
                            ]],
                            'is_not' => ['label' => 'is not', 'operator' => 'NOT IN', 'emptyCriteria' => [
                                'aggregator' => 'AND', 'values' => ['IS NOT NULL', '!= ""']
                            ]],
                        ]],
                        'Dashboards.modified' => ['type' => 'datetime', 'operators' => [
                            'is' => ['label' => 'is', 'operator' => 'IN', 'emptyCriteria' => [
                                'aggregator' => 'OR', 'valuesF' => ['IS NULL', '= ""', '= "0000-00-00 00:00:00"']
                            ]],
                            'greater' => ['label' => 'from', 'operator' => '>', 'emptyCriteria' => [
                                'aggregator' => 'AND', 'values' => ['IS NOT NULL', '!= ""', '!= "0000-00-00 00:00:00"']
                            ]]
                        ]],
                        'Dashboards.created' => ['type' => 'datetime', 'operators' => [
                            'is' => ['label' => 'is', 'operator' => 'IN', 'emptyCriteria' => [
                                'aggregator' => 'OR', 'values' => ['IS NULL', '= ""', '= "0000-00-00 00:00:00"']
                            ]],
                            'greater' => ['label' => 'from', 'operator' => '>', 'emptyCriteria' => [
                                'aggregator' => 'AND', 'values' => ['IS NOT NULL', '!= ""', '!= "0000-00-00 00:00:00"']
                            ]]
                        ]]
                    ];
                    break;

                case 'Roles':
                    $result = [
                        'Roles.name' => ['type' => 'string', 'operators' => [
                            'contains' => ['label' => 'contains', 'operator' => 'LIKE', 'pattern' => '%{{value}}%', 'emptyCriteria' => [
                                'aggregator' => 'OR', 'values' => ['IS NULL', '= ""']
                            ]]
                        ]]
                    ];
                    break;
            }

            return $result;
        });

        EventManager::instance()->on('Search.Model.Search.basicSearchFields', function ($event, $table) {
            $result = [
                'Dashboards.name',
                'Dashboards.image',
                'Dashboards.role_id',
                'Dashboards.modified',
                'Dashboards.created'
            ];

            return $result;
        });

        $this->user = ['id' => '00000000-0000-0000-0000-000000000001'];
        $this->BasicSearch = new BasicSearch(TableRegistry::get('Dashboards'), $this->user);
    }

    /**
     * tearDown method
     *
     * @return void
     */
    public function tearDown()
    {
        unset($this->BasicSearch);
        unset($this->user);

        parent::tearDown();
    }

    public function testGetCriteria()
    {
        $result = $this->BasicSearch->getCriteria('foo');

        $expected = ['Dashboards.name' => [
            ['type' => 'string', 'operator' => 'contains', 'value' => 'foo']
        ]];
        $this->assertEquals($result, $expected);
    }

    public function testGetCriteriaWithoutSearchableFields()
    {
        $basicSearch = new BasicSearch(TableRegistry::get('SomeRandomModel'), $this->user);
        $result = $basicSearch->getCriteria('foo');

        $this->assertInternalType('array', $result);
        $this->assertEmpty($result);
    }

    public function testGetCriteriaWithRelatedField()
    {
        $result = $this->BasicSearch->getCriteria('Lorem');

        $expected = [
            'Dashboards.name' => [
                ['type' => 'string', 'operator' => 'contains', 'value' => 'Lorem']
            ],
            'Dashboards.role_id' => [
                ['type' => 'related', 'operator' => 'is', 'value' => '79928943-0016-4677-869a-e37728ff6564']
            ]
        ];

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

    public function testGetCriteriaWithDefaultFields()
    {
        EventManager::instance()->on('Search.Model.Search.basicSearchFields', function ($event, $table) {
            return [
                'Dashboards.virtual_field'
            ];
        });

        $basicSearch = new BasicSearch(TableRegistry::get('Dashboards'), ['id' => '00000000-0000-0000-0000-000000000001']);

        $expected = [
            'Dashboards.name' => [
                ['type' => 'string', 'operator' => 'contains', 'value' => 'Lorem']
            ]
        ];

        $this->assertEquals($expected, $basicSearch->getCriteria('Lorem'));
    }
}<commit_msg>Adjusted tests to work with updated Roles fixtures (task #7441)<commit_after><?php
namespace Search\Utility;

use Cake\Event\EventManager;
use Cake\ORM\TableRegistry;
use Cake\TestSuite\TestCase;
use Search\Utility\BasicSearch;

/**
 * Search\Utility\BasicSearch Test Case
 */
class BasicSearchTest extends TestCase
{
    public $fixtures = [
        'plugin.search.dashboards',
        'plugin.roles_capabilities.roles'
    ];

    /**
     * setUp method
     *
     * @return void
     */
    public function setUp()
    {
        parent::setUp();

        EventManager::instance()->on('Search.Model.Search.searchabeFields', function ($event, $table) {
            $tableName = $table->getRegistryAlias();

            $result = [];
            switch ($tableName) {
                case 'Search.Dashboards':
                    $result = [
                        'Dashboards.name' => ['type' => 'string', 'operators' => [
                            'contains' => ['label' => 'contains', 'operator' => 'LIKE', 'pattern' => '%{{value}}%', 'emptyCriteria' => [
                                'aggregator' => 'OR', 'values' => ['IS NULL', '= ""']
                            ]]
                        ]],
                        'Dashboards.image' => ['type' => 'blob', 'operators' => [
                            'contains' => ['label' => 'contains', 'operator' => 'LIKE', 'pattern' => '%{{value}}%', 'emptyCriteria' => [
                                'aggregator' => 'OR', 'values' => ['IS NULL', '= ""']
                            ]],
                        ]],
                        'Dashboards.role_id' => ['type' => 'related', 'source' => 'RolesCapabilities.Roles', 'operators' => [
                            'is' => ['label' => 'is', 'operator' => 'IN', 'emptyCriteria' => [
                                'aggregator' => 'OR', 'values' => ['IS NULL', '= ""']
                            ]],
                            'is_not' => ['label' => 'is not', 'operator' => 'NOT IN', 'emptyCriteria' => [
                                'aggregator' => 'AND', 'values' => ['IS NOT NULL', '!= ""']
                            ]],
                        ]],
                        'Dashboards.modified' => ['type' => 'datetime', 'operators' => [
                            'is' => ['label' => 'is', 'operator' => 'IN', 'emptyCriteria' => [
                                'aggregator' => 'OR', 'valuesF' => ['IS NULL', '= ""', '= "0000-00-00 00:00:00"']
                            ]],
                            'greater' => ['label' => 'from', 'operator' => '>', 'emptyCriteria' => [
                                'aggregator' => 'AND', 'values' => ['IS NOT NULL', '!= ""', '!= "0000-00-00 00:00:00"']
                            ]]
                        ]],
                        'Dashboards.created' => ['type' => 'datetime', 'operators' => [
                            'is' => ['label' => 'is', 'operator' => 'IN', 'emptyCriteria' => [
                                'aggregator' => 'OR', 'values' => ['IS NULL', '= ""', '= "0000-00-00 00:00:00"']
                            ]],
                            'greater' => ['label' => 'from', 'operator' => '>', 'emptyCriteria' => [
                                'aggregator' => 'AND', 'values' => ['IS NOT NULL', '!= ""', '!= "0000-00-00 00:00:00"']
                            ]]
                        ]]
                    ];
                    break;

                case 'RolesCapabilities.Roles':
                    $result = [
                        'Roles.name' => ['type' => 'string', 'operators' => [
                            'contains' => ['label' => 'contains', 'operator' => 'LIKE', 'pattern' => '%{{value}}%', 'emptyCriteria' => [
                                'aggregator' => 'OR', 'values' => ['IS NULL', '= ""']
                            ]]
                        ]]
                    ];
                    break;
            }

            return $result;
        });

        EventManager::instance()->on('Search.Model.Search.basicSearchFields', function ($event, $table) {
            $tableName = $table->getRegistryAlias();

            if ('Search.Dashboards' === $tableName) {
                return [
                    'Dashboards.name',
                    'Dashboards.image',
                    'Dashboards.role_id',
                    'Dashboards.modified',
                    'Dashboards.created'
                ];
            }

            if ('RolesCapabilities.Roles' === $tableName) {
                return [
                    'Roles.name'
                ];
            }

            return [];
        });

        $this->user = ['id' => '00000000-0000-0000-0000-000000000001'];
        $this->BasicSearch = new BasicSearch(TableRegistry::get('Search.Dashboards'), $this->user);
    }

    /**
     * tearDown method
     *
     * @return void
     */
    public function tearDown()
    {
        unset($this->BasicSearch);
        unset($this->user);

        parent::tearDown();
    }

    public function testGetCriteria()
    {
        $result = $this->BasicSearch->getCriteria('foo');

        $expected = ['Dashboards.name' => [
            ['type' => 'string', 'operator' => 'contains', 'value' => 'foo']
        ]];
        $this->assertEquals($result, $expected);
    }

    public function testGetCriteriaWithoutSearchableFields()
    {
        $basicSearch = new BasicSearch(TableRegistry::get('NonExistingTable'), $this->user);
        $result = $basicSearch->getCriteria('foo');

        $this->assertInternalType('array', $result);
        $this->assertEmpty($result);
    }

    public function testGetCriteriaWithRelatedField()
    {
        $value = 'Everyone';
        $result = $this->BasicSearch->getCriteria($value);

        $expected = [
            'Dashboards.name' => [
                ['type' => 'string', 'operator' => 'contains', 'value' => $value]
            ],
            'Dashboards.role_id' => [
                ['type' => 'related', 'operator' => 'is', 'value' => '00000000-0000-0000-0000-000000000002']
            ]
        ];

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

    public function testGetCriteriaWithDefaultFields()
    {
        EventManager::instance()->on('Search.Model.Search.basicSearchFields', function ($event, $table) {
            return [
                'Dashboards.non_existing_field'
            ];
        });

        $basicSearch = new BasicSearch(TableRegistry::get('Search.Dashboards'), ['id' => '00000000-0000-0000-0000-000000000001']);

        $value = 'Everyone';
        $expected = [
            'Dashboards.name' => [
                ['type' => 'string', 'operator' => 'contains', 'value' => $value]
            ],
            'Dashboards.role_id' => [
                ['type' => 'related', 'operator' => 'is', 'value' => '00000000-0000-0000-0000-000000000002']
            ]
        ];

        $this->assertEquals($expected, $basicSearch->getCriteria($value));
    }
}
{'fraction_non_alphanumeric': 0.18277640726620317, 'fraction_numerical': 0.02511773940345369, 'mean_word_length': 1.4742001109672647, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 242, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 12, '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': '2878641', 'n_tokens_mistral': 3967, 'n_tokens_neox': 3544, 'n_words': 615}