Skip to content

Commit

Permalink
Add test for searching content across files with different format wit…
Browse files Browse the repository at this point in the history
…h search highlight
  • Loading branch information
SwikritiT committed Sep 15, 2023
1 parent 9d09f00 commit 6e93b4b
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 13 deletions.
30 changes: 30 additions & 0 deletions tests/acceptance/features/apiSearch/contentSearch.feature
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
@tikaServiceNeeded
Feature: content search
As a user
I want to do search resources by content
Expand Down Expand Up @@ -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-version> 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 |
57 changes: 47 additions & 10 deletions tests/acceptance/features/bootstrap/SearchContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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 <mark> tags
preg_match('/<mark>(.*?)<\/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'"
);
}
}
}
13 changes: 10 additions & 3 deletions tests/acceptance/features/bootstrap/WebDav.php
Original file line number Diff line number Diff line change
Expand Up @@ -5355,17 +5355,19 @@ 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
*
* string if $entryNameToSearch is given and is found
* 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) {
Expand All @@ -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;
Expand Down

0 comments on commit 6e93b4b

Please sign in to comment.