diff --git a/tests/acceptance/features/apiSearch/contentSearch.feature b/tests/acceptance/features/apiSearch/contentSearch.feature index 7ee8079b6ac..14113c1dff7 100644 --- a/tests/acceptance/features/apiSearch/contentSearch.feature +++ b/tests/acceptance/features/apiSearch/contentSearch.feature @@ -1,3 +1,4 @@ +@tikaServiceNeeded Feature: content search As a user I want to do search resources by content @@ -172,3 +173,32 @@ Feature: content search | content:hel* | 2 | /technical task.txt | /task comments.txt | | content:hel* AND tag:test | 1 | /technical task.txt | | | (name:*task* AND content:hel*) NOT tag:test | 1 | /task comments.txt | | + + + Scenario Outline: search across files with different format with search text highlight + Given using DAV path + And the administrator has assigned the role "Space Admin" to user "Alice" using the Graph API + And user "Alice" has created a space "project-space" with the default quota using the GraphApi + And user "Alice" has uploaded file with content "this is a simple text file" to "test-text-file.txt" + And user "Alice" has uploaded file with content "this is a simple pdf file" to "test-pdf-file.pdf" + And user "Alice" has uploaded file with content "this is a simple cpp file" to "test-cpp-file.cpp" + And user "Alice" has uploaded file with content "this is another text file" to "testfile.txt" + And user "Alice" has uploaded file "filesForUpload/testavatar.png" to "/testavatar.png" + And user "Alice" has uploaded a file inside space "project-space" with content "this is a simple markdown file" to "test-md-file.md" + And user "Alice" has uploaded a file inside space "project-space" with content "this is a simple odt file" to "test-odt-file.odt" + When user "Alice" searches for "Content:simple" using the WebDAV API + Then the HTTP status code should be "207" + And the search result should contain these entries with highlight on keyword "simple" + | test-text-file.txt | + | test-pdf-file.pdf | + | test-cpp-file.cpp | + | test-md-file.md | + | test-odt-file.odt | + But the search result of user "Alice" should not contain these entries: + | testavatar.png | + | testfile.txt | + Examples: + | dav-path-version | + | old | + | new | + | spaces | diff --git a/tests/acceptance/features/bootstrap/SearchContext.php b/tests/acceptance/features/bootstrap/SearchContext.php index a3163aa304b..2c843e2a687 100644 --- a/tests/acceptance/features/bootstrap/SearchContext.php +++ b/tests/acceptance/features/bootstrap/SearchContext.php @@ -52,13 +52,13 @@ class SearchContext implements Context { * @return void */ public function userSearchesUsingWebDavAPI( - string $user, - string $pattern, - ?string $limit = null, - ?string $scope = null, - ?string $spaceName = null, + string $user, + string $pattern, + ?string $limit = null, + ?string $scope = null, + ?string $spaceName = null, TableNode $properties = null - ):void { + ): void { // Because indexing of newly uploaded files or directories with ocis is decoupled and occurs asynchronously, a short wait is necessary before searching files or folders. sleep(4); $user = $this->featureContext->getActualUsername($user); @@ -113,10 +113,10 @@ public function userSearchesUsingWebDavAPI( * @throws Exception */ public function fileOrFolderInTheSearchResultShouldContainProperties( - string $path, - string $user, + string $path, + string $user, TableNode $properties - ):void { + ): void { $user = $this->featureContext->getActualUsername($user); $this->featureContext->verifyTableNodeColumns($properties, ['name', 'value']); $properties = $properties->getHash(); @@ -163,10 +163,47 @@ public function fileOrFolderInTheSearchResultShouldContainProperties( * * @return void */ - public function before(BeforeScenarioScope $scope):void { + public function before(BeforeScenarioScope $scope): void { // Get the environment $environment = $scope->getEnvironment(); // Get all the contexts you need in this context $this->featureContext = $environment->getContext('FeatureContext'); } + + /** + * @Then /^the search result should contain these (?:files|entries) with highlight on keyword "([^"]*)"/ + * + * @param TableNode $expectedFiles + * @param string $expectedContent + * + * @return void + * + * @throws Exception + */ + public function theSearchResultShouldContainEntriesWithHighlight( + TableNode $expectedFiles, + string $expectedContent + ): void { + $this->featureContext->verifyTableNodeColumnsCount($expectedFiles, 1); + $elementRows = $expectedFiles->getRows(); + $foundEntries = $this->featureContext->findEntryFromSearchResponse( + null, + true + ); + foreach ($elementRows as $index => $expectedFile) { + $filename = $expectedFile[0]; + $content = $foundEntries[$filename]; + // Extract the content between the tags + preg_match('/(.*?)<\/mark>/s', $content, $matches); + $actualContent = isset($matches[1]) ? $matches[1] : ''; + + // Remove any leading/trailing whitespace for comparison + $actualContent = trim($actualContent); + Assert::assertEquals( + $expectedContent, + $actualContent, + "Expected text highlight to be '$expectedContent' but found '$actualContent'" + ); + } + } } diff --git a/tests/acceptance/features/bootstrap/WebDav.php b/tests/acceptance/features/bootstrap/WebDav.php index d2eb5a926bb..74571332663 100644 --- a/tests/acceptance/features/bootstrap/WebDav.php +++ b/tests/acceptance/features/bootstrap/WebDav.php @@ -5355,6 +5355,7 @@ public function findEntryFromPropfindResponse( * and returns found search results if found else returns false * * @param string|null $entryNameToSearch + * @param bool|null $searchForHighlightString * * @return string|array|boolean * @@ -5362,10 +5363,11 @@ public function findEntryFromPropfindResponse( * array if $entryNameToSearch is not given * boolean false if $entryNameToSearch is given and is not found * - * @throws GuzzleException + * @throws Exception */ public function findEntryFromSearchResponse( - ?string $entryNameToSearch = null + ?string $entryNameToSearch = null, + ?bool $searchForHighlightString = false ) { // trim any leading "/" passed by the caller, we can just match the "raw" name if ($entryNameToSearch !== null) { @@ -5387,7 +5389,12 @@ public function findEntryFromSearchResponse( if ($entryNameToSearch === $resourcePath) { return $resourcePath; } - $results[] = $resourcePath; + if ($searchForHighlightString) { + $actualHighlightString = $item->xpath("d:propstat//oc:highlights"); + $results[$resourcePath] = (string)$actualHighlightString[0]; + } else { + $results[] = $resourcePath; + } } if ($entryNameToSearch === null) { return $results;