Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 38 | class QueryTest extends UnitTest |
||
| 39 | { |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @test |
||
| 43 | */ |
||
| 44 | public function noFiltersAreSetAfterInitialization() |
||
| 45 | { |
||
| 46 | $query = GeneralUtility::makeInstance('ApacheSolrForTypo3\\Solr\\Query', |
||
| 47 | 'test'); |
||
| 48 | $filters = $query->getFilters(); |
||
| 49 | |||
| 50 | $this->assertTrue( |
||
| 51 | empty($filters), |
||
| 52 | 'Query already contains filters after intialization.' |
||
| 53 | ); |
||
| 54 | } |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @test |
||
| 58 | */ |
||
| 59 | public function addsCorrectAccessFilterForAnonymousUser() |
||
| 60 | { |
||
| 61 | $query = GeneralUtility::makeInstance('ApacheSolrForTypo3\\Solr\\Query', |
||
| 62 | 'test'); |
||
| 63 | $query->setUserAccessGroups(array(-1, 0)); |
||
| 64 | $filters = $query->getFilters(); |
||
| 65 | |||
| 66 | $this->assertContains( |
||
| 67 | '{!typo3access}-1,0', |
||
| 68 | $filters, |
||
| 69 | 'Access filter not found in [' . implode('], [', $filters) . ']' |
||
| 70 | ); |
||
| 71 | } |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @test |
||
| 75 | */ |
||
| 76 | public function grantsAccessToGroupZeroIfNoGroupsProvided() |
||
| 77 | { |
||
| 78 | $query = GeneralUtility::makeInstance('ApacheSolrForTypo3\\Solr\\Query', |
||
| 79 | 'test'); |
||
| 80 | $query->setUserAccessGroups(array()); |
||
| 81 | $filters = $query->getFilters(); |
||
| 82 | |||
| 83 | $this->assertContains( |
||
| 84 | '{!typo3access}0', |
||
| 85 | $filters, |
||
| 86 | 'Access filter not found in [' . implode('], [', $filters) . ']' |
||
| 87 | ); |
||
| 88 | } |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @test |
||
| 92 | */ |
||
| 93 | public function grantsAccessToGroupZeroIfZeroNotProvided() |
||
| 94 | { |
||
| 95 | $query = GeneralUtility::makeInstance('ApacheSolrForTypo3\\Solr\\Query', |
||
| 96 | 'test'); |
||
| 97 | $query->setUserAccessGroups(array(5)); |
||
| 98 | $filters = $query->getFilters(); |
||
| 99 | |||
| 100 | $this->assertContains( |
||
| 101 | '{!typo3access}0,5', |
||
| 102 | $filters, |
||
| 103 | 'Access filter not found in [' . implode('], [', $filters) . ']' |
||
| 104 | ); |
||
| 105 | } |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @test |
||
| 109 | */ |
||
| 110 | public function filtersDuplicateAccessGroups() |
||
| 111 | { |
||
| 112 | $query = GeneralUtility::makeInstance('ApacheSolrForTypo3\\Solr\\Query', |
||
| 113 | 'test'); |
||
| 114 | $query->setUserAccessGroups(array(1, 1)); |
||
| 115 | $filters = $query->getFilters(); |
||
| 116 | |||
| 117 | $this->assertContains( |
||
| 118 | '{!typo3access}0,1', |
||
| 119 | $filters, |
||
| 120 | 'Access filter not found in [' . implode('], [', $filters) . ']' |
||
| 121 | ); |
||
| 122 | } |
||
| 123 | |||
| 124 | /** |
||
| 125 | * @test |
||
| 126 | */ |
||
| 127 | public function allowsOnlyOneAccessFilter() |
||
| 128 | { |
||
| 129 | $query = GeneralUtility::makeInstance('ApacheSolrForTypo3\\Solr\\Query', |
||
| 130 | 'test'); |
||
| 131 | $query->setUserAccessGroups(array(1)); |
||
| 132 | $query->setUserAccessGroups(array(2)); |
||
| 133 | $filters = $query->getFilters(); |
||
| 134 | |||
| 135 | $this->assertSame( |
||
| 136 | count($filters), |
||
| 137 | 1, |
||
| 138 | 'Too many filters in [' . implode('], [', $filters) . ']' |
||
| 139 | ); |
||
| 140 | } |
||
| 141 | |||
| 142 | // TODO if user is in group -2 (logged in), disallow access to group -1 |
||
| 143 | |||
| 144 | |||
| 145 | // grouping |
||
| 146 | |||
| 147 | |||
| 148 | /** |
||
| 149 | * @test |
||
| 150 | */ |
||
| 151 | public function groupingIsNotActiveAfterInitialization() |
||
| 152 | { |
||
| 153 | $query = GeneralUtility::makeInstance('ApacheSolrForTypo3\\Solr\\Query', |
||
| 154 | 'test'); |
||
| 155 | |||
| 156 | $queryParameters = $query->getQueryParameters(); |
||
| 157 | foreach ($queryParameters as $queryParameter => $value) { |
||
| 158 | $this->assertTrue( |
||
| 159 | !GeneralUtility::isFirstPartOfStr($queryParameter, 'group'), |
||
| 160 | 'Query already contains grouping parameter "' . $queryParameter . '"' |
||
| 161 | ); |
||
| 162 | } |
||
| 163 | } |
||
| 164 | |||
| 165 | /** |
||
| 166 | * @test |
||
| 167 | */ |
||
| 168 | public function settingGroupingTrueActivatesGrouping() |
||
| 169 | { |
||
| 170 | $query = GeneralUtility::makeInstance('ApacheSolrForTypo3\\Solr\\Query', |
||
| 171 | 'test'); |
||
| 172 | |||
| 173 | $query->setGrouping(true); |
||
| 174 | |||
| 175 | $queryParameters = $query->getQueryParameters(); |
||
| 176 | $this->assertArrayHasKey('group', $queryParameters); |
||
| 177 | $this->assertEquals('true', $queryParameters['group']); |
||
| 178 | |||
| 179 | $this->assertArrayHasKey('group.format', $queryParameters); |
||
| 180 | $this->assertEquals('grouped', $queryParameters['group.format']); |
||
| 181 | |||
| 182 | $this->assertArrayHasKey('group.ngroups', $queryParameters); |
||
| 183 | $this->assertEquals('true', $queryParameters['group.ngroups']); |
||
| 184 | |||
| 185 | return $query; |
||
| 186 | } |
||
| 187 | |||
| 188 | /** |
||
| 189 | * @test |
||
| 190 | * @depends settingGroupingTrueActivatesGrouping |
||
| 191 | */ |
||
| 192 | public function settingGroupingFalseDeactivatesGrouping(Query $query) |
||
| 193 | { |
||
| 194 | $query->setGrouping(false); |
||
| 195 | |||
| 196 | $queryParameters = $query->getQueryParameters(); |
||
| 197 | |||
| 198 | foreach ($queryParameters as $queryParameter => $value) { |
||
| 199 | $this->assertTrue( |
||
| 200 | !GeneralUtility::isFirstPartOfStr($queryParameter, 'group'), |
||
| 201 | 'Query contains grouping parameter "' . $queryParameter . '"' |
||
| 202 | ); |
||
| 203 | } |
||
| 204 | } |
||
| 205 | |||
| 206 | /** |
||
| 207 | * @test |
||
| 208 | */ |
||
| 209 | public function canEnableHighlighting() |
||
| 210 | { |
||
| 211 | /** @var $query \ApacheSolrForTypo3\Solr\Query */ |
||
| 212 | $query = GeneralUtility::makeInstance('ApacheSolrForTypo3\\Solr\\Query', 'test'); |
||
| 213 | $query->setHighlighting(true); |
||
| 214 | $queryParameters = $query->getQueryParameters(); |
||
| 215 | |||
| 216 | $this->assertSame("true", $queryParameters["hl"], 'Enable highlighting did not set the "hl" query parameter'); |
||
| 217 | $this->assertSame(200, $queryParameters["hl.fragsize"], 'hl.fragsize was not set to the default value of 200'); |
||
| 218 | } |
||
| 219 | |||
| 220 | /** |
||
| 221 | * @test |
||
| 222 | */ |
||
| 223 | public function canSetHighlightingFieldList() |
||
| 224 | { |
||
| 225 | $fakeConfigurationArray = array(); |
||
| 226 | $fakeConfigurationArray['plugin.']['tx_solr.']['search.']['results.']['resultsHighlighting.']['highlightFields'] = 'title'; |
||
| 227 | $fakeConfiguration = new TypoScriptConfiguration($fakeConfigurationArray); |
||
| 228 | |||
| 229 | /** @var $query \ApacheSolrForTypo3\Solr\Query */ |
||
| 230 | $query = GeneralUtility::makeInstance('ApacheSolrForTypo3\\Solr\\Query', 'test', $fakeConfiguration); |
||
| 231 | $query->setHighlighting(true); |
||
| 232 | $queryParameters = $query->getQueryParameters(); |
||
| 233 | |||
| 234 | $this->assertSame("true", $queryParameters["hl"], 'Enable highlighting did not set the "hl" query parameter'); |
||
| 235 | $this->assertSame("title", $queryParameters["hl.fl"], 'Can set highlighting field list'); |
||
| 236 | } |
||
| 237 | |||
| 238 | /** |
||
| 239 | * @test |
||
| 240 | */ |
||
| 241 | public function canPassCustomWrapForHighlighting() |
||
| 242 | { |
||
| 243 | $fakeConfigurationArray = array(); |
||
| 244 | $fakeConfigurationArray['plugin.']['tx_solr.']['search.']['results.']['resultsHighlighting.']['wrap'] = '[A]|[B]'; |
||
| 245 | $fakeConfiguration = new TypoScriptConfiguration($fakeConfigurationArray); |
||
| 246 | |||
| 247 | /** @var $query \ApacheSolrForTypo3\Solr\Query */ |
||
| 248 | $query = GeneralUtility::makeInstance('ApacheSolrForTypo3\\Solr\\Query', 'test', $fakeConfiguration); |
||
| 249 | $query->setHighlighting(true); |
||
| 250 | $queryParameters = $query->getQueryParameters(); |
||
| 251 | |||
| 252 | $this->assertSame("[A]", $queryParameters["hl.tag.pre"], 'Can set highlighting hl.tag.pre'); |
||
| 253 | $this->assertSame("[B]", $queryParameters["hl.tag.post"], 'Can set highlighting hl.tag.post'); |
||
| 254 | $this->assertSame("[A]", $queryParameters["hl.simple.pre"], 'Can set highlighting hl.tag.pre'); |
||
| 255 | $this->assertSame("[B]", $queryParameters["hl.simple.post"], 'Can set highlighting hl.tag.post'); |
||
| 256 | } |
||
| 257 | |||
| 258 | /** |
||
| 259 | * @test |
||
| 260 | */ |
||
| 261 | public function simplePreAndPostIsUsedWhenFastVectorHighlighterCouldNotBeUsed() |
||
| 262 | { |
||
| 263 | $fakeConfigurationArray = array(); |
||
| 264 | $fakeConfigurationArray['plugin.']['tx_solr.']['search.']['results.']['resultsHighlighting.']['wrap'] = '[A]|[B]'; |
||
| 265 | $fakeConfiguration = new TypoScriptConfiguration($fakeConfigurationArray); |
||
| 266 | |||
| 267 | /** @var $query \ApacheSolrForTypo3\Solr\Query */ |
||
| 268 | $query = GeneralUtility::makeInstance('ApacheSolrForTypo3\\Solr\\Query', 'test', $fakeConfiguration); |
||
| 269 | |||
| 270 | // fragSize 10 is to small for FastVectorHighlighter |
||
| 271 | $query->setHighlighting(true, 17); |
||
| 272 | $queryParameters = $query->getQueryParameters(); |
||
| 273 | $this->assertSame("[A]", $queryParameters["hl.simple.pre"], 'Can set highlighting field list'); |
||
| 274 | $this->assertSame("[B]", $queryParameters["hl.simple.post"], 'Can set highlighting field list'); |
||
| 275 | $this->assertEmpty($queryParameters["hl.tag.pre"], 'When the highlighting fragment size is to small hl.tag.pre should not be used because FastVectoreHighlighter will not be used'); |
||
| 276 | $this->assertEmpty($queryParameters["hl.tag.post"], 'When the highlighting fragment size is to small hl.tag.post should not be used because FastVectoreHighlighter will not be used'); |
||
| 277 | } |
||
| 278 | |||
| 279 | /** |
||
| 280 | * @test |
||
| 281 | */ |
||
| 282 | public function canUseFastVectorHighlighting() |
||
| 283 | { |
||
| 284 | $fakeConfigurationArray = array(); |
||
| 285 | $fakeConfiguration = new TypoScriptConfiguration($fakeConfigurationArray); |
||
| 286 | |||
| 287 | /** @var $query \ApacheSolrForTypo3\Solr\Query */ |
||
| 288 | $query = GeneralUtility::makeInstance('ApacheSolrForTypo3\\Solr\\Query', 'test', $fakeConfiguration); |
||
| 289 | $query->setHighlighting(true, 200); |
||
| 290 | $queryParameters = $query->getQueryParameters(); |
||
| 291 | |||
| 292 | $this->assertSame("true", $queryParameters["hl"], 'Enable highlighting did not set the "hl" query parameter'); |
||
| 293 | $this->assertSame("true", $queryParameters["hl.useFastVectorHighlighter"], 'Enable highlighting did not set the "hl.useFastVectorHighlighter" query parameter'); |
||
| 294 | } |
||
| 295 | |||
| 296 | /** |
||
| 297 | * @test |
||
| 298 | */ |
||
| 299 | public function fastVectorHighlighterIsDisabledWhenFragSizeIsLessThen18() |
||
| 300 | { |
||
| 301 | $fakeConfigurationArray = array(); |
||
| 302 | $fakeConfiguration = new TypoScriptConfiguration($fakeConfigurationArray); |
||
| 303 | |||
| 304 | /** @var $query \ApacheSolrForTypo3\Solr\Query */ |
||
| 305 | $query = GeneralUtility::makeInstance('ApacheSolrForTypo3\\Solr\\Query', 'test', $fakeConfiguration); |
||
| 306 | $query->setHighlighting(true, 0); |
||
| 307 | $queryParameters = $query->getQueryParameters(); |
||
| 308 | |||
| 309 | $this->assertSame("true", $queryParameters["hl"], 'Enable highlighting did not set the "hl" query parameter'); |
||
| 310 | $this->assertNull($queryParameters["hl.useFastVectorHighlighter"], 'FastVectorHighlighter was disabled but still requested'); |
||
| 311 | } |
||
| 312 | |||
| 313 | |||
| 314 | /** |
||
| 315 | * @test |
||
| 316 | */ |
||
| 317 | public function canGetQueryFieldsAsStringWhenPassedFromConfiguration() |
||
| 318 | { |
||
| 319 | $input = 'content^10, title^5'; |
||
| 320 | $fakeConfigurationArray = array(); |
||
| 321 | $fakeConfigurationArray['plugin.']['tx_solr.']['search.']['query.']['queryFields'] = $input; |
||
| 322 | $fakeConfiguration = new TypoScriptConfiguration($fakeConfigurationArray); |
||
| 323 | |||
| 324 | /** @var $query \ApacheSolrForTypo3\Solr\Query */ |
||
| 325 | $query = GeneralUtility::makeInstance('ApacheSolrForTypo3\\Solr\\Query', 'test', $fakeConfiguration); |
||
| 326 | |||
| 327 | $output = $query->getQueryFieldsAsString(); |
||
| 328 | $expectedOutput = 'content^10.0 title^5.0'; |
||
| 329 | |||
| 330 | $this->assertSame($output, $expectedOutput, 'Passed and retrieved query fields are not the same'); |
||
| 331 | } |
||
| 332 | |||
| 333 | /** |
||
| 334 | * @test |
||
| 335 | */ |
||
| 336 | public function canReturnEmptyStringAsQueryFieldStringWhenNothingWasPassed() |
||
| 337 | { |
||
| 338 | $fakeConfigurationArray = array(); |
||
| 339 | $fakeConfiguration = new TypoScriptConfiguration($fakeConfigurationArray); |
||
| 340 | |||
| 341 | /** @var $query \ApacheSolrForTypo3\Solr\Query */ |
||
| 342 | $query = GeneralUtility::makeInstance('ApacheSolrForTypo3\\Solr\\Query', 'test', $fakeConfiguration); |
||
| 343 | |||
| 344 | $output = $query->getQueryFieldsAsString(); |
||
| 345 | $expectedOutput = ''; |
||
| 346 | |||
| 347 | $this->assertSame($output, $expectedOutput, 'Unexpected output from getQueryFieldsAsString when no configuration was passed'); |
||
| 348 | } |
||
| 349 | |||
| 350 | /** |
||
| 351 | * @test |
||
| 352 | */ |
||
| 353 | public function canReturnFieldListWhenConfigurationWithReturnFieldsWasPassed() |
||
| 354 | { |
||
| 355 | $input = 'abstract, price'; |
||
| 356 | $fakeConfigurationArray = array(); |
||
| 357 | $fakeConfigurationArray['plugin.']['tx_solr.']['search.']['query.']['returnFields'] = $input; |
||
| 358 | $fakeConfiguration = new TypoScriptConfiguration($fakeConfigurationArray); |
||
| 359 | /** @var $query \ApacheSolrForTypo3\Solr\Query */ |
||
| 360 | $query = GeneralUtility::makeInstance('ApacheSolrForTypo3\\Solr\\Query', 'test', $fakeConfiguration); |
||
| 361 | |||
| 362 | $output = $query->getFieldList(); |
||
| 363 | $expectedOutput = array('abstract', 'price'); |
||
| 364 | $this->assertSame($output, $expectedOutput, 'Did not parse returnsFields as expected'); |
||
| 365 | } |
||
| 366 | |||
| 367 | /** |
||
| 368 | * @test |
||
| 369 | */ |
||
| 370 | public function canReturnDefaultFieldListWhenNoConfigurationWasPassed() |
||
| 371 | { |
||
| 372 | $fakeConfigurationArray = array(); |
||
| 373 | $fakeConfiguration = new TypoScriptConfiguration($fakeConfigurationArray); |
||
| 374 | /** @var $query \ApacheSolrForTypo3\Solr\Query */ |
||
| 375 | $query = GeneralUtility::makeInstance('ApacheSolrForTypo3\\Solr\\Query', 'test', $fakeConfiguration); |
||
| 376 | |||
| 377 | $output = $query->getFieldList(); |
||
| 378 | $expectedOutput = array('*', 'score'); |
||
| 379 | $this->assertSame($output, $expectedOutput, 'Did not parse returnsFields as expected'); |
||
| 380 | } |
||
| 381 | |||
| 382 | /** |
||
| 383 | * @test |
||
| 384 | */ |
||
| 385 | public function canSetTargetPageFromConfiguration() |
||
| 386 | { |
||
| 387 | $input = 4711; |
||
| 388 | $fakeConfigurationArray = array(); |
||
| 389 | $fakeConfigurationArray['plugin.']['tx_solr.']['search.']['targetPage'] = $input; |
||
| 390 | $fakeConfiguration = new TypoScriptConfiguration($fakeConfigurationArray); |
||
| 391 | |||
| 392 | /** @var $query \ApacheSolrForTypo3\Solr\Query */ |
||
| 393 | $query = GeneralUtility::makeInstance('ApacheSolrForTypo3\\Solr\\Query', 'test', $fakeConfiguration); |
||
| 394 | $this->assertEquals($input, $query->getLinkTargetPageId()); |
||
| 395 | } |
||
| 396 | |||
| 397 | /** |
||
| 398 | * @test |
||
| 399 | */ |
||
| 400 | public function canFallbackToTSFEIdWhenNoTargetPageConfigured() |
||
| 401 | { |
||
| 402 | $fakeConfigurationArray = array(); |
||
| 403 | $fakeConfiguration = new TypoScriptConfiguration($fakeConfigurationArray, 8000); |
||
| 404 | |||
| 405 | /** @var $query \ApacheSolrForTypo3\Solr\Query */ |
||
| 406 | $query = GeneralUtility::makeInstance('ApacheSolrForTypo3\\Solr\\Query', 'test', $fakeConfiguration); |
||
| 407 | $this->assertEquals(8000, $query->getLinkTargetPageId()); |
||
| 408 | } |
||
| 409 | |||
| 410 | /** |
||
| 411 | * @test |
||
| 412 | */ |
||
| 413 | public function canEnableFaceting() |
||
| 414 | { |
||
| 415 | /** @var $query \ApacheSolrForTypo3\Solr\Query */ |
||
| 416 | $query = GeneralUtility::makeInstance('ApacheSolrForTypo3\\Solr\\Query', 'test'); |
||
| 417 | $query->setFaceting(true); |
||
| 418 | $queryParameters = $query->getQueryParameters(); |
||
| 419 | |||
| 420 | $this->assertSame("true", $queryParameters["facet"], 'Enable faceting did not set the "facet" query parameter'); |
||
| 421 | } |
||
| 422 | |||
| 423 | /** |
||
| 424 | * @test |
||
| 425 | */ |
||
| 426 | public function canUseFacetMinCountFromConfiguration() |
||
| 427 | { |
||
| 428 | $input = 10; |
||
| 429 | $fakeConfigurationArray = array(); |
||
| 430 | $fakeConfigurationArray['plugin.']['tx_solr.']['search.']['faceting.']['minimumCount'] = $input; |
||
| 431 | $fakeConfiguration = new TypoScriptConfiguration($fakeConfigurationArray); |
||
| 432 | |||
| 433 | /** @var $query \ApacheSolrForTypo3\Solr\Query */ |
||
| 434 | $query = GeneralUtility::makeInstance('ApacheSolrForTypo3\\Solr\\Query', 'test', $fakeConfiguration); |
||
| 435 | $query->setFaceting(true); |
||
| 436 | $queryParameters = $query->getQueryParameters(); |
||
| 437 | |||
| 438 | $this->assertSame(10, $queryParameters["facet.mincount"], 'Can not use facet.minimumCount from configuration'); |
||
| 439 | } |
||
| 440 | |||
| 441 | /** |
||
| 442 | * @test |
||
| 443 | */ |
||
| 444 | public function canUseFacetSortByFromConfiguration() |
||
| 445 | { |
||
| 446 | $input = 'alpha'; |
||
| 447 | $fakeConfigurationArray = array(); |
||
| 448 | $fakeConfigurationArray['plugin.']['tx_solr.']['search.']['faceting.']['sortBy'] = $input; |
||
| 449 | $fakeConfiguration = new TypoScriptConfiguration($fakeConfigurationArray); |
||
| 450 | |||
| 451 | /** @var $query \ApacheSolrForTypo3\Solr\Query */ |
||
| 452 | $query = GeneralUtility::makeInstance('ApacheSolrForTypo3\\Solr\\Query', 'test', $fakeConfiguration); |
||
| 453 | $query->setFaceting(true); |
||
| 454 | $queryParameters = $query->getQueryParameters(); |
||
| 455 | |||
| 456 | $this->assertSame('index', $queryParameters["facet.sort"], 'Can not use facet.sort from configuration'); |
||
| 457 | } |
||
| 458 | |||
| 459 | /** |
||
| 460 | * @test |
||
| 461 | */ |
||
| 462 | public function canSetSpellChecking() |
||
| 463 | { |
||
| 464 | /** @var $query \ApacheSolrForTypo3\Solr\Query */ |
||
| 465 | $query = GeneralUtility::makeInstance('ApacheSolrForTypo3\\Solr\\Query', 'test'); |
||
| 466 | $query->setSpellchecking(true); |
||
| 467 | $queryParameters = $query->getQueryParameters(); |
||
| 468 | |||
| 469 | $this->assertSame("true", $queryParameters["spellcheck"], 'Enable spellchecking did not set the "spellcheck" query parameter'); |
||
| 470 | } |
||
| 471 | |||
| 472 | /** |
||
| 473 | * @test |
||
| 474 | */ |
||
| 475 | public function canTestNumberOfSuggestionsToTryFromConfiguration() |
||
| 476 | { |
||
| 477 | $input = 9; |
||
| 478 | $fakeConfigurationArray = array(); |
||
| 479 | $fakeConfigurationArray['plugin.']['tx_solr.']['search.']['spellchecking.']['numberOfSuggestionsToTry'] = $input; |
||
| 480 | $fakeConfiguration = new TypoScriptConfiguration($fakeConfigurationArray); |
||
| 481 | |||
| 482 | /** @var $query \ApacheSolrForTypo3\Solr\Query */ |
||
| 483 | $query = GeneralUtility::makeInstance('ApacheSolrForTypo3\\Solr\\Query', 'test', $fakeConfiguration); |
||
| 484 | $query->setSpellchecking(true); |
||
| 485 | $queryParameters = $query->getQueryParameters(); |
||
| 486 | |||
| 487 | $this->assertSame($input, $queryParameters['spellcheck.maxCollationTries'], 'Could not set spellcheck.maxCollationTries as expected'); |
||
| 488 | } |
||
| 489 | |||
| 490 | /** |
||
| 491 | * @test |
||
| 492 | */ |
||
| 493 | public function canWriteALogForAFilterWhenLoggingIsEnabled() |
||
| 494 | { |
||
| 495 | $fakeConfigurationArray = array(); |
||
| 496 | $fakeConfigurationArray['plugin.']['tx_solr.']['logging.']['query.']['filters'] = true; |
||
| 497 | $fakeConfiguration = new TypoScriptConfiguration($fakeConfigurationArray); |
||
| 498 | |||
| 499 | /** @var $query \ApacheSolrForTypo3\Solr\Query */ |
||
| 500 | $query = $this->getMockBuilder(\ApacheSolrForTypo3\Solr\Query::class) |
||
| 501 | ->setMethods(['writeDevLog']) |
||
| 502 | ->setConstructorArgs(['test', $fakeConfiguration]) |
||
| 503 | ->getMock(); |
||
| 504 | $query->expects($this->once())->method('writeDevLog'); |
||
| 505 | $query->addFilter('foo'); |
||
| 506 | } |
||
| 507 | |||
| 508 | /** |
||
| 509 | * @return array |
||
| 510 | */ |
||
| 511 | public function escapeQueryDataProvider() |
||
| 512 | { |
||
| 513 | return array( |
||
| 514 | 'empty' => array('input' => '', 'expectedOutput' => ''), |
||
| 515 | 'simple' => array('input' => 'foo', 'expectedOutput' => 'foo'), |
||
| 516 | 'single quoted word' => array('input' => '"world"', 'expectedOutput' => '"world"'), |
||
| 517 | 'simple quoted phrase' => array('input' => '"hello world"', 'expectedOutput' => '"hello world"'), |
||
| 518 | 'simple quoted phrase with ~' => array('input' => '"hello world~"', 'expectedOutput' => '"hello world~"'), |
||
| 519 | 'simple phrase with ~' => array('input' => 'hello world~', 'expectedOutput' => 'hello world\~'), |
||
| 520 | 'single quote' => array('input' => '20" monitor', 'expectedOutput' => '20\" monitor'), |
||
| 521 | 'rounded brackets many words' => array('input' => 'hello (world)', 'expectedOutput' => 'hello \(world\)'), |
||
| 522 | 'rounded brackets one word' => array('input' => '(world)', 'expectedOutput' => '\(world\)'), |
||
| 523 | 'plus character is kept' => array('input' => 'foo +bar -world', 'expectedOutput' => 'foo +bar -world'), |
||
| 524 | '&& character is kept' => array('input' => 'hello && world', 'expectedOutput' => 'hello && world'), |
||
| 525 | '! character is kept' => array('input' => 'hello !world', 'expectedOutput' => 'hello !world'), |
||
| 526 | '* character is kept' => array('input' => 'hello *world', 'expectedOutput' => 'hello *world'), |
||
| 527 | '? character is kept' => array('input' => 'hello ?world', 'expectedOutput' => 'hello ?world'), |
||
| 528 | 'ö character is kept' => array('input' => 'schöner tag', 'expectedOutput' => 'schöner tag'), |
||
| 529 | 'numeric is kept' => array('input' => 42, 'expectedOutput' => 42), |
||
| 530 | 'combined quoted phrase' => array('input' => '"hello world" or planet', 'expectedOutput' => '"hello world" or planet'), |
||
| 531 | 'two combined quoted phrases' => array('input' => '"hello world" or "hello planet"', 'expectedOutput' => '"hello world" or "hello planet"'), |
||
| 532 | 'combined quoted phrase mixed with escape character' => array('input' => '"hello world" or (planet)', 'expectedOutput' => '"hello world" or \(planet\)') |
||
| 533 | ); |
||
| 534 | } |
||
| 535 | |||
| 536 | /** |
||
| 537 | * @dataProvider escapeQueryDataProvider |
||
| 538 | * @test |
||
| 539 | */ |
||
| 540 | public function canEscapeAsExpected($input, $expectedOutput) |
||
| 541 | { |
||
| 542 | $fakeConfigurationArray = array(); |
||
| 543 | $fakeConfiguration = new TypoScriptConfiguration($fakeConfigurationArray); |
||
| 544 | |||
| 545 | /** @var $query \ApacheSolrForTypo3\Solr\Query */ |
||
| 546 | $query = GeneralUtility::makeInstance('ApacheSolrForTypo3\\Solr\\Query', 'test', $fakeConfiguration); |
||
| 547 | |||
| 548 | $output = $query->escape($input); |
||
| 549 | $this->assertSame($expectedOutput, $output, 'Query was not escaped as expected'); |
||
| 550 | } |
||
| 551 | |||
| 552 | /** |
||
| 553 | * @test |
||
| 554 | */ |
||
| 555 | public function canUseConfiguredVariantsFieldWhenVariantsAreActive() |
||
| 556 | { |
||
| 557 | $fakeConfigurationArray['plugin.']['tx_solr.']['search.']['variants'] = 1; |
||
| 558 | $fakeConfigurationArray['plugin.']['tx_solr.']['search.']['variants.'] = [ |
||
| 559 | 'variantField' => 'myField' |
||
| 560 | ]; |
||
| 561 | |||
| 562 | $fakeConfiguration = new TypoScriptConfiguration($fakeConfigurationArray); |
||
| 563 | |||
| 564 | /** @var $query \ApacheSolrForTypo3\Solr\Query */ |
||
| 565 | $query = GeneralUtility::makeInstance(Query::class, 'test', $fakeConfiguration); |
||
| 566 | |||
| 567 | $configuredField = $query->getVariantField(); |
||
| 568 | $this->assertTrue($query->getIsCollapsing(), 'Collapsing was enabled but not indicated to be enabled'); |
||
| 569 | $this->assertSame('myField', $configuredField, 'Did not use the configured collapseField'); |
||
| 570 | } |
||
| 571 | |||
| 572 | /** |
||
| 573 | * @test |
||
| 574 | */ |
||
| 575 | public function variantsAreDisabledWhenNothingWasConfigured() |
||
| 576 | { |
||
| 577 | $fakeConfiguration = new TypoScriptConfiguration([]); |
||
| 578 | /** @var $query \ApacheSolrForTypo3\Solr\Query */ |
||
| 579 | $query = GeneralUtility::makeInstance(Query::class, 'test', $fakeConfiguration); |
||
| 580 | $this->assertFalse($query->getIsCollapsing(), 'Collapsing was not disabled by default'); |
||
| 581 | } |
||
| 582 | } |
||
| 583 |