|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tests\Unit\Builder; |
| 4 | + |
| 5 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 6 | +use PHPUnit\Framework\TestCase; |
| 7 | +use Utopia\Database\Builder\Filtering; |
| 8 | +use Utopia\Database\Builder\MariaDB; |
| 9 | +use Utopia\Database\Builder\MySQL; |
| 10 | +use Utopia\Database\Builder\PostgreSQL; |
| 11 | +use Utopia\Database\Query; |
| 12 | + |
| 13 | +final class SearchTermTest extends TestCase |
| 14 | +{ |
| 15 | + /** |
| 16 | + * @return array<string, array{string, string}> |
| 17 | + */ |
| 18 | + public static function postgreSQLTerms(): array |
| 19 | + { |
| 20 | + return [ |
| 21 | + 'slash separates words' => ['foo/bar', 'foo or bar'], |
| 22 | + 'comma separates words' => ['foo,bar', 'foo or bar'], |
| 23 | + 'semicolon separates words' => ['foo;bar', 'foo or bar'], |
| 24 | + 'percent separates words' => ['foo%bar', 'foo or bar'], |
| 25 | + 'equals separates words' => ['foo=bar', 'foo or bar'], |
| 26 | + 'question mark separates words' => ['foo?bar', 'foo or bar'], |
| 27 | + 'hash separates words' => ['foo#bar', 'foo or bar'], |
| 28 | + 'colon separates words' => ['foo:bar', 'foo or bar'], |
| 29 | + 'pipe separates words' => ['foo|bar', 'foo or bar'], |
| 30 | + 'ampersand separates words' => ['foo&bar', 'foo or bar'], |
| 31 | + 'exclamation mark separates words' => ['foo!bar', 'foo or bar'], |
| 32 | + 'underscore stays inside a word' => ['foo_bar', 'foo_bar'], |
| 33 | + 'mixed separators and spaces' => ['baz, foo/bar; qux', 'baz or foo or bar or qux'], |
| 34 | + 'operators are dropped' => ['+foo -bar* @3 <baz> ~qux (quux)', 'foo or bar or 3 or baz or qux or quux'], |
| 35 | + 'accented words are kept' => ['@García!', 'García'], |
| 36 | + 'exact phrase keeps its quotes' => ['"foo/bar baz"', '"foo bar baz"'], |
| 37 | + 'unbalanced quote is not exact' => ['"foo/bar', 'foo or bar'], |
| 38 | + ]; |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * @return array<string, array{string, string}> |
| 43 | + */ |
| 44 | + public static function mySQLTerms(): array |
| 45 | + { |
| 46 | + return [ |
| 47 | + 'slash separates words' => ['foo/bar', 'foo bar*'], |
| 48 | + 'comma separates words' => ['foo,bar', 'foo bar*'], |
| 49 | + 'period separates words' => ['foo.bar', 'foo bar*'], |
| 50 | + 'apostrophe separates words' => ["foo'bar", 'foo bar*'], |
| 51 | + 'trailing punctuation keeps the prefix match on the word' => ['foo.', 'foo*'], |
| 52 | + 'underscore stays inside a word' => ['foo_bar', 'foo_bar*'], |
| 53 | + 'mixed separators and spaces' => ['baz, foo/bar; qux', 'baz foo bar qux*'], |
| 54 | + 'non-operator punctuation is dropped' => ['!!!foo...###', 'foo*'], |
| 55 | + 'exact phrase keeps its quotes' => ['"foo/bar baz"', '"foo bar baz"'], |
| 56 | + ]; |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * @return array<string, array{string}> |
| 61 | + */ |
| 62 | + public static function termsWithoutWords(): array |
| 63 | + { |
| 64 | + return [ |
| 65 | + 'separators only' => ['/,;%=?#'], |
| 66 | + 'operators only' => ['+-*@<>~()'], |
| 67 | + 'punctuation only' => ['!!!...###'], |
| 68 | + 'quoted punctuation' => ['"/"'], |
| 69 | + 'whitespace only' => [" \t "], |
| 70 | + ]; |
| 71 | + } |
| 72 | + |
| 73 | + #[DataProvider('postgreSQLTerms')] |
| 74 | + public function testPostgreSQLSearchesEachWordOfATerm(string $term, string $bound): void |
| 75 | + { |
| 76 | + $this->assertSame([$bound], $this->bindings(new PostgreSQL(), Query::search('title', $term))); |
| 77 | + $this->assertSame([$bound], $this->bindings(new PostgreSQL(), Query::notSearch('title', $term))); |
| 78 | + } |
| 79 | + |
| 80 | + #[DataProvider('mySQLTerms')] |
| 81 | + public function testMySQLSearchesEachWordOfATerm(string $term, string $bound): void |
| 82 | + { |
| 83 | + $this->assertSame([$bound], $this->bindings(new MySQL(), Query::search('title', $term))); |
| 84 | + $this->assertSame([$bound], $this->bindings(new MySQL(), Query::notSearch('title', $term))); |
| 85 | + } |
| 86 | + |
| 87 | + #[DataProvider('mySQLTerms')] |
| 88 | + public function testMariaDBSearchesEachWordOfATerm(string $term, string $bound): void |
| 89 | + { |
| 90 | + $this->assertSame([$bound], $this->bindings(new MariaDB(), Query::search('title', $term))); |
| 91 | + $this->assertSame([$bound], $this->bindings(new MariaDB(), Query::notSearch('title', $term))); |
| 92 | + } |
| 93 | + |
| 94 | + #[DataProvider('termsWithoutWords')] |
| 95 | + public function testATermWithoutWordsBindsNothing(string $term): void |
| 96 | + { |
| 97 | + foreach ([new PostgreSQL(), new MySQL(), new MariaDB()] as $builder) { |
| 98 | + $this->assertSame([], $this->bindings($builder, Query::search('title', $term)), $builder::class); |
| 99 | + $this->assertSame([], $this->bindings($builder, Query::notSearch('title', $term)), $builder::class); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + public function testATermIsAlwaysBoundAndNeverWrittenIntoTheCondition(): void |
| 104 | + { |
| 105 | + $term = "foo'); DROP TABLE docs; --"; |
| 106 | + |
| 107 | + foreach ([new PostgreSQL(), new MySQL(), new MariaDB()] as $builder) { |
| 108 | + $condition = $builder->compileFilters([Query::search('title', $term)]); |
| 109 | + |
| 110 | + $this->assertStringNotContainsString('DROP', $condition->expression, $builder::class); |
| 111 | + $this->assertStringNotContainsString('foo', $condition->expression, $builder::class); |
| 112 | + $this->assertCount(1, $condition->bindings, $builder::class); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * @return array<mixed> |
| 118 | + */ |
| 119 | + private function bindings(Filtering $builder, Query $query): array |
| 120 | + { |
| 121 | + return $builder->compileFilters([$query])->bindings; |
| 122 | + } |
| 123 | +} |
0 commit comments