ef9d5a02ae14bd19dc29b330c8515ace98eb2f44
[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 TYPO3\CMS\Core\Database\ConnectionPool;
17 use TYPO3\CMS\Core\Utility\GeneralUtility;
18 use TYPO3\CMS\IndexedSearch\Utility\LikeWildcard;
19
20 /**
21 * This class contains unit tests for the LikeQueryUtility
22 */
23 class LikeWildcardTest extends \TYPO3\TestingFramework\Core\Functional\FunctionalTestCase
24 {
25 /**
26 * @test
27 * @param string $tableName
28 * @param string $fieldName
29 * @param string $likeValue
30 * @param int $wildcard
31 * @param string $expected
32 * @dataProvider getLikeQueryPartDataProvider
33 */
34 public function getLikeQueryPart($tableName, $fieldName, $likeValue, $wildcard, $expected)
35 {
36 $connection = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable($tableName);
37 $subject = LikeWildcard::cast($wildcard);
38 $expected = $connection->quoteIdentifier($fieldName) . ' ' . $expected;
39 $this->assertSame($expected, $subject->getLikeQueryPart($tableName, $fieldName, $likeValue));
40 }
41
42 /**
43 * Returns data sets for the test getLikeQueryPart
44 * Each dataset is an array with the following elements:
45 * - the table name
46 * - the field name
47 * - the search value
48 * - the wildcard mode
49 * - the expected result
50 *
51 * @return array
52 */
53 public function getLikeQueryPartDataProvider()
54 {
55 return [
56 'no placeholders and no wildcard mode' => [
57 'tt_content',
58 'body',
59 'searchstring',
60 LikeWildcard::NONE,
61 "LIKE 'searchstring'"
62 ],
63 'no placeholders and left wildcard mode' => [
64 'tt_content',
65 'body',
66 'searchstring',
67 LikeWildcard::LEFT,
68 "LIKE '%searchstring'"
69 ],
70 'no placeholders and right wildcard mode' => [
71 'tt_content',
72 'body',
73 'searchstring',
74 LikeWildcard::RIGHT,
75 "LIKE 'searchstring%'"
76 ],
77 'no placeholders and both wildcards mode' => [
78 'tt_content',
79 'body',
80 'searchstring',
81 LikeWildcard::BOTH,
82 "LIKE '%searchstring%'"
83 ],
84 'underscore placeholder and left wildcard mode' => [
85 'tt_content',
86 'body',
87 'search_string',
88 LikeWildcard::LEFT,
89 "LIKE '%search\\\\_string'"
90 ],
91 'percent placeholder and right wildcard mode' => [
92 'tt_content',
93 'body',
94 'search%string',
95 LikeWildcard::RIGHT,
96 "LIKE 'search\\\\%string%'"
97 ],
98 'percent and underscore placeholder and both wildcards mode' => [
99 'tt_content',
100 'body',
101 '_search%string_',
102 LikeWildcard::RIGHT,
103 "LIKE '\\\\_search\\\\%string\\\\_%'"
104 ],
105 ];
106 }
107 }