2022-12-21 17:22:22 +05:45
<? php declare ( strict_types = 1 );
/**
* ownCloud
*
* @author Artur Neumann <artur@jankaritech.com>
* @copyright Copyright (c) 2018 Artur Neumann artur@jankaritech.com
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License,
* as published by the Free Software Foundation;
* either version 3 of the License, or any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
use Behat\Behat\Context\Context ;
use Behat\Behat\Hook\Scope\BeforeScenarioScope ;
use Behat\Gherkin\Node\TableNode ;
use PHPUnit\Framework\Assert ;
use TestHelpers\WebDavHelper ;
require_once 'bootstrap.php' ;
/**
* context containing search related API steps
*/
class SearchContext implements Context {
2023-05-02 17:25:56 +05:45
private FeatureContext $featureContext ;
2022-12-21 17:22:22 +05:45
/**
* @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:
2023-08-03 09:51:28 +05:45
* @When user :user searches for :pattern inside folder :scope using the WebDAV API
2023-08-07 14:52:38 +05:45
* @When user :user searches for :pattern inside folder :scope in space :spaceName using the WebDAV API
2022-12-21 17:22:22 +05:45
*
* @param string $user
* @param string $pattern
* @param string|null $limit
2023-08-03 09:51:28 +05:45
* @param string|null $scope
2023-08-07 14:52:38 +05:45
* @param string|null $spaceName
2022-12-21 17:22:22 +05:45
* @param TableNode|null $properties
*
* @return void
*/
public function userSearchesUsingWebDavAPI (
2023-09-15 15:31:46 +05:45
string $user ,
string $pattern ,
? string $limit = null ,
? string $scope = null ,
? string $spaceName = null ,
2022-12-21 17:22:22 +05:45
TableNode $properties = null
2023-09-15 15:31:46 +05:45
) : void {
2022-12-21 17:22:22 +05:45
// 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.
2023-03-09 16:24:31 +05:45
sleep ( 4 );
2022-12-21 17:22:22 +05:45
$user = $this -> featureContext -> getActualUsername ( $user );
$baseUrl = $this -> featureContext -> getBaseUrl ();
$password = $this -> featureContext -> getPasswordForUser ( $user );
2023-09-21 17:17:28 +02:00
if ( str_contains ( $pattern , '$' )) {
$date = explode ( "$" , $pattern );
switch ( $date [ 1 ]) {
case "today" :
$pattern = $date [ 0 ] . date ( 'Y-m-d' , strtotime ( 'today' ));
break ;
case "yesterday" :
$pattern = $date [ 0 ] . date ( 'Y-m-d' , strtotime ( 'yesterday' ));
break ;
default :
throw new Exception ( "cannot convert the date" );
}
}
2022-12-21 17:22:22 +05:45
$body
= "<?xml version='1.0' encoding='utf-8' ?> \n " .
" <oc:search-files xmlns:a='DAV:' xmlns:oc='http://owncloud.org/ns' > \n " .
2023-08-03 09:51:28 +05:45
" <oc:search> \n " ;
2023-09-07 11:13:33 +02:00
if ( $scope !== null ) {
2023-08-07 14:52:38 +05:45
$scope = \trim ( $scope , "/" );
2023-09-07 11:13:33 +02:00
$resourceID = $this -> featureContext -> spacesContext -> getResourceId ( $user , $spaceName ?? "Personal" , $scope );
$pattern .= " scope: $resourceID " ;
2023-08-03 09:51:28 +05:45
}
$body .= "<oc:pattern> $pattern </oc:pattern> \n " ;
2022-12-21 17:22:22 +05:45
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" ,
"/" ,
2023-09-06 16:07:53 +05:45
null ,
2022-12-21 17:22:22 +05:45
$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 (
2023-09-15 15:31:46 +05:45
string $path ,
string $user ,
2022-12-21 17:22:22 +05:45
TableNode $properties
2023-09-15 15:31:46 +05:45
) : void {
2022-12-21 17:22:22 +05:45
$user = $this -> featureContext -> getActualUsername ( $user );
$this -> featureContext -> verifyTableNodeColumns ( $properties , [ 'name' , 'value' ]);
$properties = $properties -> getHash ();
2023-10-10 12:57:41 +05:45
$fileResult = $this -> featureContext -> findEntryFromSearchResponse (
$path
2022-12-21 17:22:22 +05:45
);
Assert :: assertNotFalse (
$fileResult ,
"could not find file/folder ' $path '"
);
foreach ( $properties as $property ) {
$property [ 'value' ] = $this -> featureContext -> substituteInLineCodes (
$property [ 'value' ],
$user
);
2023-10-10 12:57:41 +05:45
$fileResultProperty = $fileResult -> xpath ( "d:propstat//" . $property [ 'name' ]);
if ( $fileResultProperty ) {
Assert :: assertMatchesRegularExpression (
"/" . $property [ 'value' ] . "/" ,
\trim (( string ) $fileResultProperty [ 0 ])
);
continue ;
2022-12-21 17:22:22 +05:45
}
2023-10-10 12:57:41 +05:45
throw new Error ( "Could not find property '" . $property [ 'name' ] . "'" );
2022-12-21 17:22:22 +05:45
}
}
/**
* This will run before EVERY scenario.
* It will set the properties for this object.
*
* @BeforeScenario
*
* @param BeforeScenarioScope $scope
*
* @return void
*/
2023-09-15 15:31:46 +05:45
public function before ( BeforeScenarioScope $scope ) : void {
2022-12-21 17:22:22 +05:45
// Get the environment
$environment = $scope -> getEnvironment ();
// Get all the contexts you need in this context
$this -> featureContext = $environment -> getContext ( 'FeatureContext' );
}
2023-09-15 15:31:46 +05:45
/**
* @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 '"
);
}
}
2022-12-21 17:22:22 +05:45
}