OrEqualsToInTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace Test\Files\Search\QueryOptimizer;
  7. use OC\Files\Search\QueryOptimizer\FlattenSingleArgumentBinaryOperation;
  8. use OC\Files\Search\QueryOptimizer\OrEqualsToIn;
  9. use OC\Files\Search\SearchBinaryOperator;
  10. use OC\Files\Search\SearchComparison;
  11. use OCP\Files\Search\ISearchBinaryOperator;
  12. use OCP\Files\Search\ISearchComparison;
  13. use Test\TestCase;
  14. class OrEqualsToInTest extends TestCase {
  15. private $optimizer;
  16. private $simplifier;
  17. protected function setUp(): void {
  18. parent::setUp();
  19. $this->optimizer = new OrEqualsToIn();
  20. $this->simplifier = new FlattenSingleArgumentBinaryOperation();
  21. }
  22. public function testOrs(): void {
  23. $operator = new SearchBinaryOperator(
  24. ISearchBinaryOperator::OPERATOR_OR,
  25. [
  26. new SearchComparison(ISearchComparison::COMPARE_EQUAL, 'path', 'foo'),
  27. new SearchComparison(ISearchComparison::COMPARE_EQUAL, 'path', 'bar'),
  28. new SearchComparison(ISearchComparison::COMPARE_EQUAL, 'path', 'asd'),
  29. ]
  30. );
  31. $this->assertEquals('(path eq "foo" or path eq "bar" or path eq "asd")', $operator->__toString());
  32. $this->optimizer->processOperator($operator);
  33. $this->simplifier->processOperator($operator);
  34. $this->assertEquals('path in ["foo","bar","asd"]', $operator->__toString());
  35. }
  36. public function testOrsMultipleFields(): void {
  37. $operator = new SearchBinaryOperator(
  38. ISearchBinaryOperator::OPERATOR_OR,
  39. [
  40. new SearchComparison(ISearchComparison::COMPARE_EQUAL, 'path', 'foo'),
  41. new SearchComparison(ISearchComparison::COMPARE_EQUAL, 'path', 'bar'),
  42. new SearchComparison(ISearchComparison::COMPARE_EQUAL, 'fileid', 1),
  43. new SearchComparison(ISearchComparison::COMPARE_EQUAL, 'fileid', 2),
  44. new SearchComparison(ISearchComparison::COMPARE_EQUAL, 'mimetype', 'asd'),
  45. ]
  46. );
  47. $this->assertEquals('(path eq "foo" or path eq "bar" or fileid eq 1 or fileid eq 2 or mimetype eq "asd")', $operator->__toString());
  48. $this->optimizer->processOperator($operator);
  49. $this->simplifier->processOperator($operator);
  50. $this->assertEquals('(path in ["foo","bar"] or fileid in [1,2] or mimetype eq "asd")', $operator->__toString());
  51. }
  52. public function testPreserveHints(): void {
  53. $operator = new SearchBinaryOperator(
  54. ISearchBinaryOperator::OPERATOR_OR,
  55. [
  56. new SearchComparison(ISearchComparison::COMPARE_EQUAL, 'path', 'foo'),
  57. new SearchComparison(ISearchComparison::COMPARE_EQUAL, 'path', 'bar'),
  58. new SearchComparison(ISearchComparison::COMPARE_EQUAL, 'path', 'asd'),
  59. ]
  60. );
  61. foreach ($operator->getArguments() as $argument) {
  62. $argument->setQueryHint(ISearchComparison::HINT_PATH_EQ_HASH, false);
  63. }
  64. $this->assertEquals('(path eq "foo" or path eq "bar" or path eq "asd")', $operator->__toString());
  65. $this->optimizer->processOperator($operator);
  66. $this->simplifier->processOperator($operator);
  67. $this->assertEquals('path in ["foo","bar","asd"]', $operator->__toString());
  68. $this->assertEquals(false, $operator->getQueryHint(ISearchComparison::HINT_PATH_EQ_HASH, true));
  69. }
  70. public function testOrSomeEq(): void {
  71. $operator = new SearchBinaryOperator(
  72. ISearchBinaryOperator::OPERATOR_OR,
  73. [
  74. new SearchComparison(ISearchComparison::COMPARE_EQUAL, 'path', 'foo'),
  75. new SearchComparison(ISearchComparison::COMPARE_LIKE, 'path', 'foo%'),
  76. new SearchComparison(ISearchComparison::COMPARE_EQUAL, 'path', 'bar'),
  77. ]
  78. );
  79. $this->assertEquals('(path eq "foo" or path like "foo%" or path eq "bar")', $operator->__toString());
  80. $this->optimizer->processOperator($operator);
  81. $this->simplifier->processOperator($operator);
  82. $this->assertEquals('(path in ["foo","bar"] or path like "foo%")', $operator->__toString());
  83. }
  84. public function testOrsInside(): void {
  85. $operator = new SearchBinaryOperator(
  86. ISearchBinaryOperator::OPERATOR_AND,
  87. [
  88. new SearchComparison(ISearchComparison::COMPARE_EQUAL, 'mimetype', 'text'),
  89. new SearchBinaryOperator(
  90. ISearchBinaryOperator::OPERATOR_OR,
  91. [
  92. new SearchComparison(ISearchComparison::COMPARE_EQUAL, 'path', 'foo'),
  93. new SearchComparison(ISearchComparison::COMPARE_EQUAL, 'path', 'bar'),
  94. new SearchComparison(ISearchComparison::COMPARE_EQUAL, 'path', 'asd'),
  95. ]
  96. )
  97. ]
  98. );
  99. $this->assertEquals('(mimetype eq "text" and (path eq "foo" or path eq "bar" or path eq "asd"))', $operator->__toString());
  100. $this->optimizer->processOperator($operator);
  101. $this->simplifier->processOperator($operator);
  102. $this->assertEquals('(mimetype eq "text" and path in ["foo","bar","asd"])', $operator->__toString());
  103. }
  104. }