‪TYPO3CMS  9.5
LikeWildcardTest.php
Go to the documentation of this file.
1 <?php
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
17 use Doctrine\DBAL\Platforms\MySqlPlatform;
21 use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
22 
26 class ‪LikeWildcardTest extends FunctionalTestCase
27 {
37  public function ‪getLikeQueryPart($tableName, $fieldName, $likeValue, $wildcard, $expected)
38  {
39  $connection = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable($tableName);
40  $subject = ‪LikeWildcard::cast($wildcard);
41  // MySQL has support for backslash escape sequences, the expected results needs to take
42  // the additional quoting into account.
43  if ($connection->getDatabasePlatform() instanceof MySqlPlatform) {
44  $expected = addcslashes($expected, '\\');
45  }
46  $expected = $connection->quoteIdentifier($fieldName) . ' ' . $expected;
47  $this->assertSame($expected, $subject->getLikeQueryPart($tableName, $fieldName, $likeValue));
48  }
49 
62  {
63  return [
64  'no placeholders and no wildcard mode' => [
65  'tt_content',
66  'body',
67  'searchstring',
69  "LIKE 'searchstring'"
70  ],
71  'no placeholders and left wildcard mode' => [
72  'tt_content',
73  'body',
74  'searchstring',
76  "LIKE '%searchstring'"
77  ],
78  'no placeholders and right wildcard mode' => [
79  'tt_content',
80  'body',
81  'searchstring',
83  "LIKE 'searchstring%'"
84  ],
85  'no placeholders and both wildcards mode' => [
86  'tt_content',
87  'body',
88  'searchstring',
90  "LIKE '%searchstring%'"
91  ],
92  'underscore placeholder and left wildcard mode' => [
93  'tt_content',
94  'body',
95  'search_string',
97  "LIKE '%search\\_string'"
98  ],
99  'percent placeholder and right wildcard mode' => [
100  'tt_content',
101  'body',
102  'search%string',
104  "LIKE 'search\\%string%'"
105  ],
106  'percent and underscore placeholder and both wildcards mode' => [
107  'tt_content',
108  'body',
109  '_search%string_',
111  "LIKE '\\_search\\%string\\_%'"
112  ],
113  ];
114  }
115 }
‪TYPO3\CMS\IndexedSearch\Tests\Functional\Utility
Definition: LikeWildcardTest.php:2
‪TYPO3\CMS\IndexedSearch\Tests\Functional\Utility\LikeWildcardTest\getLikeQueryPartDataProvider
‪array getLikeQueryPartDataProvider()
Definition: LikeWildcardTest.php:61
‪TYPO3\CMS\IndexedSearch\Utility\LikeWildcard
Definition: LikeWildcard.php:25
‪TYPO3\CMS\IndexedSearch\Utility\LikeWildcard\LEFT
‪const LEFT
Definition: LikeWildcard.php:32
‪TYPO3\CMS\Core\Type\Enumeration\cast
‪static static cast($value)
Definition: Enumeration.php:182
‪TYPO3\CMS\IndexedSearch\Utility\LikeWildcard\BOTH
‪const BOTH
Definition: LikeWildcard.php:38
‪TYPO3\CMS\IndexedSearch\Tests\Functional\Utility\LikeWildcardTest
Definition: LikeWildcardTest.php:27
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:44
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\IndexedSearch\Utility\LikeWildcard\RIGHT
‪const RIGHT
Definition: LikeWildcard.php:35
‪TYPO3\CMS\IndexedSearch\Tests\Functional\Utility\LikeWildcardTest\getLikeQueryPart
‪getLikeQueryPart($tableName, $fieldName, $likeValue, $wildcard, $expected)
Definition: LikeWildcardTest.php:37
‪TYPO3\CMS\IndexedSearch\Utility\LikeWildcard\NONE
‪const NONE
Definition: LikeWildcard.php:29