‪TYPO3CMS  11.5
LikeWildcardTest.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use Doctrine\DBAL\Platforms\MySqlPlatform;
21 use Doctrine\DBAL\Platforms\PostgreSqlPlatform;
25 use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
26 
30 class ‪LikeWildcardTest extends FunctionalTestCase
31 {
41  public function ‪getLikeQueryPart(string $tableName, string $fieldName, string $likeValue, int $wildcard, string $expected): void
42  {
43  $connection = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable($tableName);
44  $subject = ‪LikeWildcard::cast($wildcard);
45  if ($connection->getDatabasePlatform() instanceof PostgreSqlPlatform) {
46  $expected = str_replace('LIKE', 'ILIKE', $expected);
47  }
48  // MySQL has support for backslash escape sequences, the expected results needs to take
49  // the additional quoting into account.
50  if ($connection->getDatabasePlatform() instanceof MySqlPlatform) {
51  $expected = addcslashes($expected, '\\');
52  }
53  $expected = $connection->quoteIdentifier($fieldName) . ' ' . $expected;
54  if (!$connection->getDatabasePlatform() instanceof PostgreSQLPlatform) {
55  $expected .= ' ESCAPE ' . $connection->quote('\\');
56  }
57  self::assertSame($expected, $subject->getLikeQueryPart($tableName, $fieldName, $likeValue));
58  }
59 
71  public function ‪getLikeQueryPartDataProvider(): array
72  {
73  return [
74  'no placeholders and no wildcard mode' => [
75  'tt_content',
76  'body',
77  'searchstring',
79  "LIKE 'searchstring'",
80  ],
81  'no placeholders and left wildcard mode' => [
82  'tt_content',
83  'body',
84  'searchstring',
86  "LIKE '%searchstring'",
87  ],
88  'no placeholders and right wildcard mode' => [
89  'tt_content',
90  'body',
91  'searchstring',
93  "LIKE 'searchstring%'",
94  ],
95  'no placeholders and both wildcards mode' => [
96  'tt_content',
97  'body',
98  'searchstring',
100  "LIKE '%searchstring%'",
101  ],
102  'underscore placeholder and left wildcard mode' => [
103  'tt_content',
104  'body',
105  'search_string',
107  "LIKE '%search\\_string'",
108  ],
109  'percent placeholder and right wildcard mode' => [
110  'tt_content',
111  'body',
112  'search%string',
114  "LIKE 'search\\%string%'",
115  ],
116  'percent and underscore placeholder and both wildcards mode' => [
117  'tt_content',
118  'body',
119  '_search%string_',
121  "LIKE '\\_search\\%string\\_%'",
122  ],
123  ];
124  }
125 }
‪TYPO3\CMS\IndexedSearch\Tests\Functional\Utility
Definition: LikeWildcardTest.php:18
‪TYPO3\CMS\IndexedSearch\Tests\Functional\Utility\LikeWildcardTest\getLikeQueryPartDataProvider
‪array getLikeQueryPartDataProvider()
Definition: LikeWildcardTest.php:71
‪TYPO3\CMS\IndexedSearch\Utility\LikeWildcard
Definition: LikeWildcard.php:27
‪TYPO3\CMS\IndexedSearch\Utility\LikeWildcard\LEFT
‪const LEFT
Definition: LikeWildcard.php:34
‪TYPO3\CMS\Core\Type\Enumeration\cast
‪static static cast($value)
Definition: Enumeration.php:186
‪TYPO3\CMS\IndexedSearch\Tests\Functional\Utility\LikeWildcardTest\getLikeQueryPart
‪getLikeQueryPart(string $tableName, string $fieldName, string $likeValue, int $wildcard, string $expected)
Definition: LikeWildcardTest.php:41
‪TYPO3\CMS\IndexedSearch\Utility\LikeWildcard\BOTH
‪const BOTH
Definition: LikeWildcard.php:40
‪TYPO3\CMS\IndexedSearch\Tests\Functional\Utility\LikeWildcardTest
Definition: LikeWildcardTest.php:31
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:46
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪TYPO3\CMS\IndexedSearch\Utility\LikeWildcard\RIGHT
‪const RIGHT
Definition: LikeWildcard.php:37
‪TYPO3\CMS\IndexedSearch\Utility\LikeWildcard\NONE
‪const NONE
Definition: LikeWildcard.php:31