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