TYPO3 CMS  TYPO3_8-7
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  */
20 
24 class LikeWildcardTest extends \TYPO3\TestingFramework\Core\Functional\FunctionalTestCase
25 {
35  public function getLikeQueryPart($tableName, $fieldName, $likeValue, $wildcard, $expected)
36  {
37  $connection = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable($tableName);
38  $subject = LikeWildcard::cast($wildcard);
39  // MySQL has support for backslash escape sequences, the expected results needs to take
40  // the additional quoting into account.
41  if ($connection->getDatabasePlatform() instanceof MySqlPlatform) {
42  $expected = addcslashes($expected, '\\');
43  }
44  $expected = $connection->quoteIdentifier($fieldName) . ' ' . $expected;
45  $this->assertSame($expected, $subject->getLikeQueryPart($tableName, $fieldName, $likeValue));
46  }
47 
59  public function getLikeQueryPartDataProvider()
60  {
61  return [
62  'no placeholders and no wildcard mode' => [
63  'tt_content',
64  'body',
65  'searchstring',
67  "LIKE 'searchstring'"
68  ],
69  'no placeholders and left wildcard mode' => [
70  'tt_content',
71  'body',
72  'searchstring',
74  "LIKE '%searchstring'"
75  ],
76  'no placeholders and right wildcard mode' => [
77  'tt_content',
78  'body',
79  'searchstring',
81  "LIKE 'searchstring%'"
82  ],
83  'no placeholders and both wildcards mode' => [
84  'tt_content',
85  'body',
86  'searchstring',
88  "LIKE '%searchstring%'"
89  ],
90  'underscore placeholder and left wildcard mode' => [
91  'tt_content',
92  'body',
93  'search_string',
95  "LIKE '%search\\_string'"
96  ],
97  'percent placeholder and right wildcard mode' => [
98  'tt_content',
99  'body',
100  'search%string',
102  "LIKE 'search\\%string%'"
103  ],
104  'percent and underscore placeholder and both wildcards mode' => [
105  'tt_content',
106  'body',
107  '_search%string_',
109  "LIKE '\\_search\\%string\\_%'"
110  ],
111  ];
112  }
113 }
static makeInstance($className,... $constructorArguments)
getLikeQueryPart($tableName, $fieldName, $likeValue, $wildcard, $expected)