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\FlattenSingleArgumentBinaryOperation ;
use OC\Files\Search\QueryOptimizer\MergeDistributiveOperations ;
use OC\Files\Search\SearchBinaryOperator ;
use OC\Files\Search\SearchComparison ;
use OCP\Files\Search\ISearchBinaryOperator ;
use OCP\Files\Search\ISearchComparison ;
use Test\TestCase ;
class MergeDistributiveOperationsTest extends TestCase {
private $optimizer ;
private $simplifier ;
protected function setUp () : void {
parent :: setUp ();
$this -> optimizer = new MergeDistributiveOperations ();
$this -> simplifier = new FlattenSingleArgumentBinaryOperation ();
}
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 -> simplifier -> processOperator ( $operator );
$this -> assertEquals ( '(storage eq 1 and (path eq "foo" or path eq "bar" or path eq "asd"))' , $operator -> __toString ());
}
2024-09-15 16:32:31 -04:00
public function testDontTouchIfNotSame () : 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' , 2 ),
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' , 3 ),
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 2 and path eq "bar") or (storage eq 3 and path eq "asd"))' , $operator -> __toString ());
$this -> optimizer -> processOperator ( $operator );
$this -> simplifier -> processOperator ( $operator );
$this -> assertEquals ( '((storage eq 1 and path eq "foo") or (storage eq 2 and path eq "bar") or (storage eq 3 and path eq "asd"))' , $operator -> __toString ());
}
2024-09-15 16:32:31 -04:00
public function testMergePartial () : 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' , 2 ),
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 2 and path eq "asd"))' , $operator -> __toString ());
$this -> optimizer -> processOperator ( $operator );
$this -> simplifier -> processOperator ( $operator );
$this -> assertEquals ( '((storage eq 1 and (path eq "foo" or path eq "bar")) or (storage eq 2 and path eq "asd"))' , $operator -> __toString ());
}
2024-09-15 16:32:31 -04:00
public function testOptimizeInside () : void {
2023-09-21 07:49:16 -04:00
$operator = new SearchBinaryOperator (
ISearchBinaryOperator :: OPERATOR_AND ,
[
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
])
]
),
2024-08-23 09:10:27 -04:00
new SearchComparison ( ISearchComparison :: COMPARE_EQUAL , 'mimetype' , 'text' )
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")) and mimetype eq "text")' , $operator -> __toString ());
$this -> optimizer -> processOperator ( $operator );
$this -> simplifier -> processOperator ( $operator );
$this -> assertEquals ( '((storage eq 1 and (path eq "foo" or path eq "bar" or path eq "asd")) and mimetype eq "text")' , $operator -> __toString ());
}
2024-02-15 12:16:13 -05:00
2024-09-15 16:32:31 -04:00
public function testMoveInnerOperations () : void {
2024-02-15 12:16:13 -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 ),
new SearchComparison ( ISearchComparison :: COMPARE_EQUAL , 'path' , 'foo' ),
2024-02-15 12:16:13 -05: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' ),
2024-02-15 12:16:13 -05: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' ),
new SearchComparison ( ISearchComparison :: COMPARE_GREATER_THAN , 'size' , '100' ),
2024-02-15 12:16:13 -05: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" and size gt "100"))' , $operator -> __toString ());
$this -> optimizer -> processOperator ( $operator );
$this -> simplifier -> processOperator ( $operator );
$this -> assertEquals ( '(storage eq 1 and (path eq "foo" or path eq "bar" or (path eq "asd" and size gt "100")))' , $operator -> __toString ());
}
2023-09-21 07:49:16 -04:00
}