2023-09-21 07:49:16 -04:00
< ? php
2025-06-30 09:04:05 -04:00
2024-05-10 09:09:14 -04:00
/**
* SPDX - FileCopyrightText : 2023 Nextcloud GmbH and Nextcloud contributors
* SPDX - License - Identifier : AGPL - 3.0 - or - later
*/
2023-09-21 07:49:16 -04:00
namespace Test\Files\Search\QueryOptimizer ;
use OC\Files\Search\QueryOptimizer\QueryOptimizer ;
use OC\Files\Search\SearchBinaryOperator ;
use OC\Files\Search\SearchComparison ;
use OCP\Files\Search\ISearchBinaryOperator ;
use OCP\Files\Search\ISearchComparison ;
use Test\TestCase ;
class CombinedTests extends TestCase {
2025-10-20 19:52:40 -04:00
public function __construct () {
parent :: __construct ( static :: class );
}
2023-09-21 07:49:16 -04:00
private QueryOptimizer $optimizer ;
protected function setUp () : void {
parent :: setUp ();
$this -> optimizer = new QueryOptimizer ();
}
2024-09-15 16:32:31 -04:00
public function testBasicOrOfAnds () : void {
2023-09-21 07:49:16 -04:00
$operator = new SearchBinaryOperator (
ISearchBinaryOperator :: OPERATOR_OR ,
[
new SearchBinaryOperator ( ISearchBinaryOperator :: OPERATOR_AND , [
2024-08-23 09:10:27 -04:00
new SearchComparison ( ISearchComparison :: COMPARE_EQUAL , 'storage' , 1 ),
new SearchComparison ( ISearchComparison :: COMPARE_EQUAL , 'path' , 'foo' ),
2023-09-21 07:49:16 -04:00
]),
new SearchBinaryOperator ( ISearchBinaryOperator :: OPERATOR_AND , [
2024-08-23 09:10:27 -04:00
new SearchComparison ( ISearchComparison :: COMPARE_EQUAL , 'storage' , 1 ),
new SearchComparison ( ISearchComparison :: COMPARE_EQUAL , 'path' , 'bar' ),
2023-09-21 07:49:16 -04:00
]),
new SearchBinaryOperator ( ISearchBinaryOperator :: OPERATOR_AND , [
2024-08-23 09:10:27 -04:00
new SearchComparison ( ISearchComparison :: COMPARE_EQUAL , 'storage' , 1 ),
new SearchComparison ( ISearchComparison :: COMPARE_EQUAL , 'path' , 'asd' ),
2023-09-21 07:49:16 -04:00
])
]
);
$this -> assertEquals ( '((storage eq 1 and path eq "foo") or (storage eq 1 and path eq "bar") or (storage eq 1 and path eq "asd"))' , $operator -> __toString ());
$this -> optimizer -> processOperator ( $operator );
$this -> assertEquals ( '(storage eq 1 and path in ["foo","bar","asd"])' , $operator -> __toString ());
}
2024-02-25 15:11:27 -05:00
2024-09-15 16:32:31 -04:00
public function testComplexSearchPattern1 () : void {
2024-02-25 15:11:27 -05:00
$operator = new SearchBinaryOperator (
ISearchBinaryOperator :: OPERATOR_OR ,
[
new SearchBinaryOperator ( ISearchBinaryOperator :: OPERATOR_AND , [
2024-08-23 09:10:27 -04:00
new SearchComparison ( ISearchComparison :: COMPARE_EQUAL , 'storage' , 1 ),
2024-02-25 15:11:27 -05:00
]),
new SearchBinaryOperator ( ISearchBinaryOperator :: OPERATOR_AND , [
2024-08-23 09:10:27 -04:00
new SearchComparison ( ISearchComparison :: COMPARE_EQUAL , 'storage' , 2 ),
2024-02-25 15:11:27 -05:00
new SearchBinaryOperator ( ISearchBinaryOperator :: OPERATOR_OR , [
2024-08-23 09:10:27 -04:00
new SearchComparison ( ISearchComparison :: COMPARE_EQUAL , 'path' , '201' ),
new SearchComparison ( ISearchComparison :: COMPARE_LIKE , 'path' , '201/%' ),
2024-02-25 15:11:27 -05:00
]),
]),
new SearchBinaryOperator ( ISearchBinaryOperator :: OPERATOR_AND , [
2024-08-23 09:10:27 -04:00
new SearchComparison ( ISearchComparison :: COMPARE_EQUAL , 'storage' , 3 ),
new SearchComparison ( ISearchComparison :: COMPARE_EQUAL , 'path' , '301' ),
2024-02-25 15:11:27 -05:00
]),
new SearchBinaryOperator ( ISearchBinaryOperator :: OPERATOR_AND , [
2024-08-23 09:10:27 -04:00
new SearchComparison ( ISearchComparison :: COMPARE_EQUAL , 'storage' , 4 ),
new SearchComparison ( ISearchComparison :: COMPARE_EQUAL , 'path' , '401' ),
2024-02-25 15:11:27 -05:00
]),
new SearchBinaryOperator ( ISearchBinaryOperator :: OPERATOR_AND , [
2024-08-23 09:10:27 -04:00
new SearchComparison ( ISearchComparison :: COMPARE_EQUAL , 'storage' , 3 ),
new SearchComparison ( ISearchComparison :: COMPARE_EQUAL , 'path' , '302' ),
2024-02-25 15:11:27 -05:00
]),
new SearchBinaryOperator ( ISearchBinaryOperator :: OPERATOR_AND , [
2024-08-23 09:10:27 -04:00
new SearchComparison ( ISearchComparison :: COMPARE_EQUAL , 'storage' , 4 ),
new SearchComparison ( ISearchComparison :: COMPARE_EQUAL , 'path' , '402' ),
2024-02-25 15:11:27 -05:00
]),
]
);
$this -> assertEquals ( '((storage eq 1) or (storage eq 2 and (path eq "201" or path like "201\/%")) or (storage eq 3 and path eq "301") or (storage eq 4 and path eq "401") or (storage eq 3 and path eq "302") or (storage eq 4 and path eq "402"))' , $operator -> __toString ());
$this -> optimizer -> processOperator ( $operator );
$this -> assertEquals ( '(storage eq 1 or (storage eq 2 and (path eq "201" or path like "201\/%")) or (storage eq 3 and path in ["301","302"]) or (storage eq 4 and path in ["401","402"]))' , $operator -> __toString ());
}
2024-09-15 16:32:31 -04:00
public function testComplexSearchPattern2 () : void {
2024-02-25 15:11:27 -05:00
$operator = new SearchBinaryOperator (
ISearchBinaryOperator :: OPERATOR_OR ,
[
2024-08-23 09:10:27 -04:00
new SearchComparison ( ISearchComparison :: COMPARE_EQUAL , 'storage' , 1 ),
2024-02-25 15:11:27 -05:00
new SearchBinaryOperator ( ISearchBinaryOperator :: OPERATOR_AND , [
2024-08-23 09:10:27 -04:00
new SearchComparison ( ISearchComparison :: COMPARE_EQUAL , 'storage' , 2 ),
2024-02-25 15:11:27 -05:00
new SearchBinaryOperator ( ISearchBinaryOperator :: OPERATOR_OR , [
2024-08-23 09:10:27 -04:00
new SearchComparison ( ISearchComparison :: COMPARE_EQUAL , 'path' , '201' ),
new SearchComparison ( ISearchComparison :: COMPARE_LIKE , 'path' , '201/%' ),
2024-02-25 15:11:27 -05:00
]),
]),
new SearchBinaryOperator ( ISearchBinaryOperator :: OPERATOR_AND , [
2024-08-23 09:10:27 -04:00
new SearchComparison ( ISearchComparison :: COMPARE_EQUAL , 'storage' , 3 ),
new SearchComparison ( ISearchComparison :: COMPARE_EQUAL , 'path' , '301' ),
2024-02-25 15:11:27 -05:00
]),
new SearchBinaryOperator ( ISearchBinaryOperator :: OPERATOR_AND , [
2024-08-23 09:10:27 -04:00
new SearchComparison ( ISearchComparison :: COMPARE_EQUAL , 'storage' , 4 ),
new SearchComparison ( ISearchComparison :: COMPARE_EQUAL , 'path' , '401' ),
2024-02-25 15:11:27 -05:00
]),
new SearchBinaryOperator ( ISearchBinaryOperator :: OPERATOR_AND , [
2024-08-23 09:10:27 -04:00
new SearchComparison ( ISearchComparison :: COMPARE_EQUAL , 'storage' , 3 ),
new SearchComparison ( ISearchComparison :: COMPARE_EQUAL , 'path' , '302' ),
2024-02-25 15:11:27 -05:00
]),
new SearchBinaryOperator ( ISearchBinaryOperator :: OPERATOR_AND , [
2024-08-23 09:10:27 -04:00
new SearchComparison ( ISearchComparison :: COMPARE_EQUAL , 'storage' , 4 ),
new SearchComparison ( ISearchComparison :: COMPARE_EQUAL , 'path' , '402' ),
2024-02-25 15:11:27 -05:00
]),
]
);
$this -> assertEquals ( '(storage eq 1 or (storage eq 2 and (path eq "201" or path like "201\/%")) or (storage eq 3 and path eq "301") or (storage eq 4 and path eq "401") or (storage eq 3 and path eq "302") or (storage eq 4 and path eq "402"))' , $operator -> __toString ());
$this -> optimizer -> processOperator ( $operator );
$this -> assertEquals ( '(storage eq 1 or (storage eq 2 and (path eq "201" or path like "201\/%")) or (storage eq 3 and path in ["301","302"]) or (storage eq 4 and path in ["401","402"]))' , $operator -> __toString ());
}
2024-09-15 16:32:31 -04:00
public function testApplySearchConstraints1 () : void {
2024-02-25 15:11:27 -05:00
$operator = new SearchBinaryOperator ( ISearchBinaryOperator :: OPERATOR_AND , [
new SearchBinaryOperator ( ISearchBinaryOperator :: OPERATOR_AND , [
new SearchBinaryOperator ( ISearchBinaryOperator :: OPERATOR_OR , [
2024-08-23 09:10:27 -04:00
new SearchComparison ( ISearchComparison :: COMPARE_EQUAL , 'mimetype' , 'image/png' ),
new SearchComparison ( ISearchComparison :: COMPARE_EQUAL , 'mimetype' , 'image/jpeg' ),
2024-02-25 15:11:27 -05:00
]),
]),
new SearchBinaryOperator ( ISearchBinaryOperator :: OPERATOR_OR , [
new SearchBinaryOperator ( ISearchBinaryOperator :: OPERATOR_AND , [
2024-08-23 09:10:27 -04:00
new SearchComparison ( ISearchComparison :: COMPARE_EQUAL , 'storage' , 1 ),
2024-02-25 15:11:27 -05:00
new SearchBinaryOperator ( ISearchBinaryOperator :: OPERATOR_OR , [
2024-08-23 09:10:27 -04:00
new SearchComparison ( ISearchComparison :: COMPARE_EQUAL , 'path' , 'files' ),
new SearchComparison ( ISearchComparison :: COMPARE_LIKE , 'path' , 'files/%' ),
2024-02-25 15:11:27 -05:00
]),
]),
2024-08-23 09:10:27 -04:00
new SearchComparison ( ISearchComparison :: COMPARE_EQUAL , 'storage' , 2 ),
2024-02-25 15:11:27 -05:00
new SearchBinaryOperator ( ISearchBinaryOperator :: OPERATOR_AND , [
2024-08-23 09:10:27 -04:00
new SearchComparison ( ISearchComparison :: COMPARE_EQUAL , 'storage' , 3 ),
new SearchComparison ( ISearchComparison :: COMPARE_EQUAL , 'path' , 'files/301' ),
2024-02-25 15:11:27 -05:00
]),
new SearchBinaryOperator ( ISearchBinaryOperator :: OPERATOR_AND , [
2024-08-23 09:10:27 -04:00
new SearchComparison ( ISearchComparison :: COMPARE_EQUAL , 'storage' , 3 ),
new SearchComparison ( ISearchComparison :: COMPARE_EQUAL , 'path' , 'files/302' ),
2024-02-25 15:11:27 -05:00
]),
]),
]);
$this -> assertEquals ( '(((mimetype eq "image\/png" or mimetype eq "image\/jpeg")) and ((storage eq 1 and (path eq "files" or path like "files\/%")) or storage eq 2 or (storage eq 3 and path eq "files\/301") or (storage eq 3 and path eq "files\/302")))' , $operator -> __toString ());
$this -> optimizer -> processOperator ( $operator );
$this -> assertEquals ( '(mimetype in ["image\/png","image\/jpeg"] and ((storage eq 1 and (path eq "files" or path like "files\/%")) or storage eq 2 or (storage eq 3 and path in ["files\/301","files\/302"])))' , $operator -> __toString ());
}
2024-09-15 16:32:31 -04:00
public function testApplySearchConstraints2 () : void {
2024-02-25 15:11:27 -05:00
$operator = new SearchBinaryOperator ( ISearchBinaryOperator :: OPERATOR_AND , [
new SearchBinaryOperator ( ISearchBinaryOperator :: OPERATOR_AND , [
new SearchBinaryOperator ( ISearchBinaryOperator :: OPERATOR_OR , [
2024-08-23 09:10:27 -04:00
new SearchComparison ( ISearchComparison :: COMPARE_EQUAL , 'mimetype' , 'image/png' ),
new SearchComparison ( ISearchComparison :: COMPARE_EQUAL , 'mimetype' , 'image/jpeg' ),
2024-02-25 15:11:27 -05:00
]),
]),
new SearchBinaryOperator ( ISearchBinaryOperator :: OPERATOR_OR , [
new SearchBinaryOperator ( ISearchBinaryOperator :: OPERATOR_AND , [
2024-08-23 09:10:27 -04:00
new SearchComparison ( ISearchComparison :: COMPARE_EQUAL , 'storage' , 1 ),
2024-02-25 15:11:27 -05:00
new SearchBinaryOperator ( ISearchBinaryOperator :: OPERATOR_OR , [
2024-08-23 09:10:27 -04:00
new SearchComparison ( ISearchComparison :: COMPARE_EQUAL , 'path' , 'files' ),
new SearchComparison ( ISearchComparison :: COMPARE_LIKE , 'path' , 'files/%' ),
2024-02-25 15:11:27 -05:00
]),
]),
new SearchBinaryOperator ( ISearchBinaryOperator :: OPERATOR_AND , [
2024-08-23 09:10:27 -04:00
new SearchComparison ( ISearchComparison :: COMPARE_EQUAL , 'storage' , 2 ),
2024-02-25 15:11:27 -05:00
]),
new SearchBinaryOperator ( ISearchBinaryOperator :: OPERATOR_AND , [
2024-08-23 09:10:27 -04:00
new SearchComparison ( ISearchComparison :: COMPARE_EQUAL , 'storage' , 3 ),
new SearchComparison ( ISearchComparison :: COMPARE_EQUAL , 'path' , 'files/301' ),
2024-02-25 15:11:27 -05:00
]),
new SearchBinaryOperator ( ISearchBinaryOperator :: OPERATOR_AND , [
2024-08-23 09:10:27 -04:00
new SearchComparison ( ISearchComparison :: COMPARE_EQUAL , 'storage' , 3 ),
new SearchComparison ( ISearchComparison :: COMPARE_EQUAL , 'path' , 'files/302' ),
2024-02-25 15:11:27 -05:00
]),
]),
]);
$this -> assertEquals ( '(((mimetype eq "image\/png" or mimetype eq "image\/jpeg")) and ((storage eq 1 and (path eq "files" or path like "files\/%")) or (storage eq 2) or (storage eq 3 and path eq "files\/301") or (storage eq 3 and path eq "files\/302")))' , $operator -> __toString ());
$this -> optimizer -> processOperator ( $operator );
$this -> assertEquals ( '(mimetype in ["image\/png","image\/jpeg"] and ((storage eq 1 and (path eq "files" or path like "files\/%")) or storage eq 2 or (storage eq 3 and path in ["files\/301","files\/302"])))' , $operator -> __toString ());
}
2023-09-21 07:49:16 -04:00
}