Skip to content

Commit

Permalink
add test for search highlight
Browse files Browse the repository at this point in the history
  • Loading branch information
SwikritiT committed Sep 14, 2023
1 parent 6edb5d1 commit 622cbdb
Show file tree
Hide file tree
Showing 3 changed files with 217 additions and 228 deletions.
16 changes: 8 additions & 8 deletions tests/acceptance/features/apiSearch/contentSearch.feature
Original file line number Diff line number Diff line change
Expand Up @@ -187,16 +187,16 @@ Feature: content search
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"
Then the HTTP status code should be "207"
And the search result of user "Alice" 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 |
| 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 |
Expand Down
353 changes: 186 additions & 167 deletions tests/acceptance/features/bootstrap/SearchContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,168 +31,173 @@
/**
* context containing search related API steps
*/
class SearchContext implements Context {
private FeatureContext $featureContext;
class SearchContext implements Context
{
private FeatureContext $featureContext;

/**
* @When user :user searches for :pattern using the WebDAV API
* @When user :user searches for :pattern and limits the results to :limit items using the WebDAV API
* @When user :user searches for :pattern using the WebDAV API requesting these properties:
* @When user :user searches for :pattern and limits the results to :limit items using the WebDAV API requesting these properties:
* @When user :user searches for :pattern inside folder :scope using the WebDAV API
* @When user :user searches for :pattern inside folder :scope in space :spaceName using the WebDAV API
*
* @param string $user
* @param string $pattern
* @param string|null $limit
* @param string|null $scope
* @param string|null $spaceName
* @param TableNode|null $properties
*
* @return void
*/
public function userSearchesUsingWebDavAPI(
string $user,
string $pattern,
?string $limit = null,
?string $scope = null,
?string $spaceName = null,
TableNode $properties = null
):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);
$baseUrl = $this->featureContext->getBaseUrl();
$password = $this->featureContext->getPasswordForUser($user);
$body
= "<?xml version='1.0' encoding='utf-8' ?>\n" .
" <oc:search-files xmlns:a='DAV:' xmlns:oc='http://owncloud.org/ns' >\n" .
" <oc:search>\n";
if ($scope !== null) {
$scope = \trim($scope, "/");
$resourceID = $this->featureContext->spacesContext->getResourceId($user, $spaceName ?? "Personal", $scope);
$pattern .= " scope:$resourceID";
}
$body .= "<oc:pattern>$pattern</oc:pattern>\n";
if ($limit !== null) {
$body .= " <oc:limit>$limit</oc:limit>\n";
}
/**
* @When user :user searches for :pattern using the WebDAV API
* @When user :user searches for :pattern and limits the results to :limit items using the WebDAV API
* @When user :user searches for :pattern using the WebDAV API requesting these properties:
* @When user :user searches for :pattern and limits the results to :limit items using the WebDAV API requesting these properties:
* @When user :user searches for :pattern inside folder :scope using the WebDAV API
* @When user :user searches for :pattern inside folder :scope in space :spaceName using the WebDAV API
*
* @param string $user
* @param string $pattern
* @param string|null $limit
* @param string|null $scope
* @param string|null $spaceName
* @param TableNode|null $properties
*
* @return void
*/
public function userSearchesUsingWebDavAPI(
string $user,
string $pattern,
?string $limit = null,
?string $scope = null,
?string $spaceName = null,
TableNode $properties = null
): 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);
$baseUrl = $this->featureContext->getBaseUrl();
$password = $this->featureContext->getPasswordForUser($user);
$body
= "<?xml version='1.0' encoding='utf-8' ?>\n" .
" <oc:search-files xmlns:a='DAV:' xmlns:oc='http://owncloud.org/ns' >\n" .
" <oc:search>\n";
if ($scope !== null) {
$scope = \trim($scope, "/");
$resourceID = $this->featureContext->spacesContext->getResourceId($user, $spaceName ?? "Personal", $scope);
$pattern .= " scope:$resourceID";
}
$body .= "<oc:pattern>$pattern</oc:pattern>\n";
if ($limit !== null) {
$body .= " <oc:limit>$limit</oc:limit>\n";
}

$body .= " </oc:search>\n";
if ($properties !== null) {
$propertiesRows = $properties->getRows();
$body .= " <a:prop>";
foreach ($propertiesRows as $property) {
$body .= "<$property[0]/>";
}
$body .= " </a:prop>";
}
$body .= " </oc:search-files>";
$response = WebDavHelper::makeDavRequest(
$baseUrl,
$user,
$password,
"REPORT",
"/",
null,
$this->featureContext->getStepLineRef(),
$body,
$this->featureContext->getDavPathVersion()
);
$this->featureContext->setResponse($response);
}
$body .= " </oc:search>\n";
if ($properties !== null) {
$propertiesRows = $properties->getRows();
$body .= " <a:prop>";
foreach ($propertiesRows as $property) {
$body .= "<$property[0]/>";
}
$body .= " </a:prop>";
}
$body .= " </oc:search-files>";
$response = WebDavHelper::makeDavRequest(
$baseUrl,
$user,
$password,
"REPORT",
"/",
null,
$this->featureContext->getStepLineRef(),
$body,
$this->featureContext->getDavPathVersion()
);
$this->featureContext->setResponse($response);
}

/**
* @Then file/folder :path in the search result of user :user should contain these properties:
*
* @param string $path
* @param string $user
* @param TableNode $properties
*
* @return void
* @throws Exception
*/
public function fileOrFolderInTheSearchResultShouldContainProperties(
string $path,
string $user,
TableNode $properties
):void {
$user = $this->featureContext->getActualUsername($user);
$this->featureContext->verifyTableNodeColumns($properties, ['name', 'value']);
$properties = $properties->getHash();
$fileResult = $this->featureContext->findEntryFromPropfindResponse(
$path,
$user,
"REPORT",
);
Assert::assertNotFalse(
$fileResult,
"could not find file/folder '$path'"
);
$fileProperties = $fileResult['value'][1]['value'][0]['value'];
foreach ($properties as $property) {
$foundProperty = false;
$property['value'] = $this->featureContext->substituteInLineCodes(
$property['value'],
$user
);
foreach ($fileProperties as $fileProperty) {
if ($fileProperty['name'] === $property['name']) {
Assert::assertMatchesRegularExpression(
"/" . $property['value'] . "/",
$fileProperty['value']
);
$foundProperty = true;
break;
}
}
Assert::assertTrue(
$foundProperty,
"could not find property '" . $property['name'] . "'"
);
}
}
/**
* @Then file/folder :path in the search result of user :user should contain these properties:
*
* @param string $path
* @param string $user
* @param TableNode $properties
*
* @return void
* @throws Exception
*/
public function fileOrFolderInTheSearchResultShouldContainProperties(
string $path,
string $user,
TableNode $properties
): void
{
$user = $this->featureContext->getActualUsername($user);
$this->featureContext->verifyTableNodeColumns($properties, ['name', 'value']);
$properties = $properties->getHash();
$fileResult = $this->featureContext->findEntryFromPropfindResponse(
$path,
$user,
"REPORT",
);
Assert::assertNotFalse(
$fileResult,
"could not find file/folder '$path'"
);
$fileProperties = $fileResult['value'][1]['value'][0]['value'];
foreach ($properties as $property) {
$foundProperty = false;
$property['value'] = $this->featureContext->substituteInLineCodes(
$property['value'],
$user
);
foreach ($fileProperties as $fileProperty) {
if ($fileProperty['name'] === $property['name']) {
Assert::assertMatchesRegularExpression(
"/" . $property['value'] . "/",
$fileProperty['value']
);
$foundProperty = true;
break;
}
}
Assert::assertTrue(
$foundProperty,
"could not find property '" . $property['name'] . "'"
);
}
}

/**
* This will run before EVERY scenario.
* It will set the properties for this object.
*
* @BeforeScenario
*
* @param BeforeScenarioScope $scope
*
* @return 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');
}
/**
* This will run before EVERY scenario.
* It will set the properties for this object.
*
* @BeforeScenario
*
* @param BeforeScenarioScope $scope
*
* @return 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 by tags for user :user should contain these entries:
*
* @param string|null $user
* @param TableNode $expectedEntries
*
* @return void
* @throws Exception
*/
public function theSearchResultByTagsForUserShouldContainTheseEntries(
?string $user,
TableNode $expectedEntries
):void {
$user = $this->featureContext->getActualUsername($user);
$this->featureContext->verifyTableNodeColumnsCount($expectedEntries, 1);
$expectedEntries = $expectedEntries->getRows();
$expectedEntriesArray = [];
$responseResourcesArray = $this->featureContext->findEntryFromReportResponse($user);
foreach ($expectedEntries as $item) {
$expectedEntriesArray[] = $item[0];
}
Assert::assertEqualsCanonicalizing($expectedEntriesArray, $responseResourcesArray);
}
/**
* @Then the search result by tags for user :user should contain these entries:
*
* @param string|null $user
* @param TableNode $expectedEntries
*
* @return void
* @throws Exception
*/
public function theSearchResultByTagsForUserShouldContainTheseEntries(
?string $user,
TableNode $expectedEntries
): void
{
$user = $this->featureContext->getActualUsername($user);
$this->featureContext->verifyTableNodeColumnsCount($expectedEntries, 1);
$expectedEntries = $expectedEntries->getRows();
$expectedEntriesArray = [];
$responseResourcesArray = $this->featureContext->findEntryFromReportResponse($user);
foreach ($expectedEntries as $item) {
$expectedEntriesArray[] = $item[0];
}
Assert::assertEqualsCanonicalizing($expectedEntriesArray, $responseResourcesArray);
}


/**
Expand All @@ -206,18 +211,32 @@ public function theSearchResultByTagsForUserShouldContainTheseEntries(
* @throws Exception
*/
public function theSearchResultShouldContainEntriesWithHighlight(
string $user,
string $user,
TableNode $expectedFiles,
string $keyword
):void {
$user = $this->featureContext->getActualUsername($user);
string $expectedContent
): void
{
$this->featureContext->verifyTableNodeColumnsCount($expectedFiles, 1);
$expectedEntries = $expectedFiles->getRows();
$expectedEntriesArray = [];
$responseResourcesArray = $this->featureContext->findEntryFromReportResponse($user,$keyword);
foreach ($expectedEntries as $item) {
$expectedEntriesArray[] = $item[0];
$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);
var_dump($actualContent);
Assert::assertEquals(
$expectedContent,
$actualContent,
"Expected text highlight to be $expectedContent but found $actualContent"
);
}
Assert::assertEqualsCanonicalizing($expectedEntriesArray, $responseResourcesArray);
}
}
Loading

0 comments on commit 622cbdb

Please sign in to comment.