[BUGFIX] Fix functional tests for EXT:indexed_search on PostgreSQL
[Packages/TYPO3.CMS.git] / typo3 / sysext / indexed_search / Tests / Functional / Utility / LikeWildcardTest.php
1 <?php
2 namespace TYPO3\CMS\IndexedSearch\Tests\Unit\Utility;
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 use Doctrine\DBAL\Platforms\MySqlPlatform;
17 use TYPO3\CMS\Core\Database\ConnectionPool;
18 use TYPO3\CMS\Core\Utility\GeneralUtility;
19 use TYPO3\CMS\IndexedSearch\Utility\LikeWildcard;
20
21 /**
22 * This class contains unit tests for the LikeQueryUtility
23 */
24 class LikeWildcardTest extends \TYPO3\TestingFramework\Core\Functional\FunctionalTestCase
25 {
26 /**
27 * @test
28 * @param string $tableName
29 * @param string $fieldName
30 * @param string $likeValue
31 * @param int $wildcard
32 * @param string $expected
33 * @dataProvider getLikeQueryPartDataProvider
34 */
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
48 /**
49 * Returns data sets for the test getLikeQueryPart
50 * Each dataset is an array with the following elements:
51 * - the table name
52 * - the field name
53 * - the search value
54 * - the wildcard mode
55 * - the expected result
56 *
57 * @return array
58 */
59 public function getLikeQueryPartDataProvider()
60 {
61 return [
62 'no placeholders and no wildcard mode' => [
63 'tt_content',
64 'body',
65 'searchstring',
66 LikeWildcard::NONE,
67 "LIKE 'searchstring'"
68 ],
69 'no placeholders and left wildcard mode' => [
70 'tt_content',
71 'body',
72 'searchstring',
73 LikeWildcard::LEFT,
74 "LIKE '%searchstring'"
75 ],
76 'no placeholders and right wildcard mode' => [
77 'tt_content',
78 'body',
79 'searchstring',
80 LikeWildcard::RIGHT,
81 "LIKE 'searchstring%'"
82 ],
83 'no placeholders and both wildcards mode' => [
84 'tt_content',
85 'body',
86 'searchstring',
87 LikeWildcard::BOTH,
88 "LIKE '%searchstring%'"
89 ],
90 'underscore placeholder and left wildcard mode' => [
91 'tt_content',
92 'body',
93 'search_string',
94 LikeWildcard::LEFT,
95 "LIKE '%search\\_string'"
96 ],
97 'percent placeholder and right wildcard mode' => [
98 'tt_content',
99 'body',
100 'search%string',
101 LikeWildcard::RIGHT,
102 "LIKE 'search\\%string%'"
103 ],
104 'percent and underscore placeholder and both wildcards mode' => [
105 'tt_content',
106 'body',
107 '_search%string_',
108 LikeWildcard::RIGHT,
109 "LIKE '\\_search\\%string\\_%'"
110 ],
111 ];
112 }
113 }