From f33afe7444d2e8a03775e1ddf48f9c9195bc9272 Mon Sep 17 00:00:00 2001 From: thomascorthals Date: Mon, 27 Nov 2023 13:14:23 +0100 Subject: [PATCH] Phrase query test (#1107) * Phrase query test * Add more reserved characters to test phrase Co-authored-by: Alexander Schranz * Add back `\r\n` sequence --------- Co-authored-by: Alexander Schranz Co-authored-by: Markus Kalkbrenner --- .../AbstractTechproductsTestCase.php | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/tests/Integration/AbstractTechproductsTestCase.php b/tests/Integration/AbstractTechproductsTestCase.php index 0cf862253..2fb3143fd 100644 --- a/tests/Integration/AbstractTechproductsTestCase.php +++ b/tests/Integration/AbstractTechproductsTestCase.php @@ -303,6 +303,7 @@ public function testEscapes(string $requestFormat) $update->addCommit(true, true); self::$client->update($update); + // check if stored correctly in index $select = self::$client->createSelect(); $select->setQuery('id:%T1%', ['solarium-test-escapes']); $result = self::$client->select($select); @@ -332,6 +333,62 @@ public function testEscapes(string $requestFormat) $this->assertCount(0, $result); } + /** + * @see https://github.com/solariumphp/solarium/issues/1104 + * + * @dataProvider updateRequestFormatProvider + */ + public function testPhraseQuery(string $requestFormat) + { + $phrase = "^The 17\" O'Conner && O`Series \n OR a || 1%2 1~2 1*2 \r\n book? \r \twhat \\ text: }{ )( ][ - + // \n\r ok? end$"; + + $update = self::$client->createUpdate(); + $update->setRequestFormat($requestFormat); + $doc = $update->createDocument(); + $doc->setField('id', 'solarium-test-phrase'); + $doc->setField('name', 'Solarium Test Phrase Query'); + $doc->setField('cat', [$phrase]); + $update->addDocument($doc); + $update->addCommit(true, true); + self::$client->update($update); + + if ($update::REQUEST_FORMAT_XML === $requestFormat) { + /* + * Per https://www.w3.org/TR/REC-xml/#sec-line-ends line breaks are normalized + * + * [...] by translating both the two-character sequence #xD #xA and + * any #xD that is not followed by #xA to a single #xA character. + */ + $phrase = str_replace(["\r\n", "\r"], ["\n", "\n"], $phrase); + } + + // check if stored correctly in index + $select = self::$client->createSelect(); + $select->setQuery('id:solarium-test-phrase'); + $result = self::$client->select($select); + $this->assertSame([$phrase], $result->getIterator()->current()->getFields()['cat']); + + // as term + $select->setQuery('cat:%T1%', [$phrase]); + $result = self::$client->select($select); + $this->assertCount(1, $result); + $this->assertSame('solarium-test-phrase', $result->getIterator()->current()->getFields()['id']); + + // as phrase + $select->setQuery('cat:%P1%', [$phrase]); + $result = self::$client->select($select); + $this->assertCount(1, $result); + $this->assertSame('solarium-test-phrase', $result->getIterator()->current()->getFields()['id']); + + // cleanup + $update->addDeleteQuery('cat:%P1%', [$phrase]); + $update->addCommit(true, true); + self::$client->update($update); + $select->setQuery('id:solarium-test-phrase'); + $result = self::$client->select($select); + $this->assertCount(0, $result); + } + /** * @see https://github.com/solariumphp/solarium/issues/974 * @see https://solr.apache.org/guide/local-parameters-in-queries.html#basic-syntax-of-local-parameters