4
0

BooleanSearchTest.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. <?php
  2. declare(strict_types=1);
  3. use PHPUnit\Framework\Attributes\DataProvider;
  4. final class BooleanSearchTest extends \PHPUnit\Framework\TestCase {
  5. /**
  6. * `FreshRSS_BooleanSearch::prepend()` is used to restrict an existing search with an extra condition,
  7. * such as the maximum publication date of the “mark as read → articles older than one day/week” action.
  8. * @return list<array{string,string,list<string|int>}>
  9. */
  10. public static function providePrependMaxPubdate(): array {
  11. return [
  12. ['', '(e.date <= ?)', [1700000000]],
  13. ['intitle:sale', '(e.date <= ?) AND ((e.title LIKE ?))', [1700000000, '%sale%']],
  14. ['intitle:a OR intitle:b', '(e.date <= ?) AND ((e.title LIKE ?) OR (e.title LIKE ?))', [1700000000, '%a%', '%b%']],
  15. ];
  16. }
  17. /** @param list<string|int> $expectedValues */
  18. #[DataProvider('providePrependMaxPubdate')]
  19. public function test_prepend_restrictsTheSearchInsteadOfWideningIt(string $input, string $expectedSql, array $expectedValues): void {
  20. $booleanSearch = new FreshRSS_BooleanSearch($input);
  21. $maxPubdate = new FreshRSS_Search('');
  22. $maxPubdate->setMaxPubdate(1700000000);
  23. $booleanSearch->prepend($maxPubdate);
  24. [$values, $sql] = FreshRSS_EntryDAO::sqlBooleanSearch('e.', $booleanSearch);
  25. self::assertSame($expectedSql, trim($sql));
  26. self::assertSame($expectedValues, $values);
  27. }
  28. /** @return list<list{string}> */
  29. public static function provideTooLongOrTooDeepSearches(): array {
  30. $tooLong = str_repeat('ab ', 1400); // Long enough to exceed the maximum search length
  31. $tooDeep = str_repeat('(', 40) . 'ab' . str_repeat(')', 40); // Deeper than the maximum parentheses depth
  32. return [
  33. [$tooLong],
  34. [$tooDeep],
  35. ];
  36. }
  37. #[DataProvider('provideTooLongOrTooDeepSearches')]
  38. public function test_constructor_rejectsTooLongOrTooDeepSearches(string $input): void {
  39. self::expectException(Minz_BadRequestException::class);
  40. // Tests run at the default PHP memory limit; a brute-force 1400-deep search would consume too much memory
  41. new FreshRSS_BooleanSearch($input);
  42. }
  43. }